Refactor and fix some issues
[llpp.git] / main.ml
blobf40d69b2e690eaa93b385e847645fb14cef07b05
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 | Uunexpected of string
7 | Ulaunch of string
8 | Unamed of string
9 | Uremote of (string * int)
10 and facename = string;;
12 let dolog fmt = Printf.kprintf prerr_endline fmt;;
13 let now = Unix.gettimeofday;;
15 type params = (angle * proportional * trimparams
16 * texcount * sliceheight * memsize
17 * colorspace * fontpath)
18 and pageno = int
19 and width = int
20 and height = int
21 and leftx = int
22 and opaque = string
23 and recttype = int
24 and pixmapsize = int
25 and angle = int
26 and proportional = bool
27 and trimmargins = bool
28 and interpagespace = int
29 and texcount = int
30 and sliceheight = int
31 and gen = int
32 and top = float
33 and fontpath = string
34 and memsize = int
35 and aalevel = int
36 and irect = (int * int * int * int)
37 and trimparams = (trimmargins * irect)
38 and colorspace = | Rgb | Bgr | Gray
41 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
42 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
44 type pipe = (Unix.file_descr * Unix.file_descr);;
46 external init : pipe -> params -> unit = "ml_init";;
47 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
48 external copysel : string -> opaque -> unit = "ml_copysel";;
49 external getpdimrect : int -> float array = "ml_getpdimrect";;
50 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
51 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
52 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
53 external measurestr : int -> string -> float = "ml_measure_string";;
54 external getmaxw : unit -> float = "ml_getmaxw";;
55 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
56 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
57 external platform : unit -> platform = "ml_platform";;
58 external setaalevel : int -> unit = "ml_setaalevel";;
59 external realloctexts : int -> bool = "ml_realloctexts";;
60 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
62 let platform_to_string = function
63 | Punknown -> "unknown"
64 | Plinux -> "Linux"
65 | Posx -> "OSX"
66 | Psun -> "Sun"
67 | Pfreebsd -> "FreeBSD"
68 | Pdragonflybsd -> "DragonflyBSD"
69 | Popenbsd -> "OpenBSD"
70 | Pnetbsd -> "NetBSD"
71 | Pcygwin -> "Cygwin"
74 let platform = platform ();;
76 type x = int
77 and y = int
78 and tilex = int
79 and tiley = int
80 and tileparams = (x * y * width * height * tilex * tiley)
83 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
85 type mpos = int * int
86 and mstate =
87 | Msel of (mpos * mpos)
88 | Mpan of mpos
89 | Mscrolly | Mscrollx
90 | Mzoom of (int * int)
91 | Mzoomrect of (mpos * mpos)
92 | Mnone
95 type textentry = string * string * onhist option * onkey * ondone
96 and onkey = string -> int -> te
97 and ondone = string -> unit
98 and histcancel = unit -> unit
99 and onhist = ((histcmd -> string) * histcancel)
100 and histcmd = HCnext | HCprev | HCfirst | HClast
101 and te =
102 | TEstop
103 | TEdone of string
104 | TEcont of string
105 | TEswitch of textentry
108 type 'a circbuf =
109 { store : 'a array
110 ; mutable rc : int
111 ; mutable wc : int
112 ; mutable len : int
116 let bound v minv maxv =
117 max minv (min maxv v);
120 let cbnew n v =
121 { store = Array.create n v
122 ; rc = 0
123 ; wc = 0
124 ; len = 0
128 let drawstring size x y s =
129 Gl.enable `blend;
130 Gl.enable `texture_2d;
131 ignore (drawstr size x y s);
132 Gl.disable `blend;
133 Gl.disable `texture_2d;
136 let drawstring1 size x y s =
137 drawstr size x y s;
140 let drawstring2 size x y fmt =
141 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
144 let cbcap b = Array.length b.store;;
146 let cbput b v =
147 let cap = cbcap b in
148 b.store.(b.wc) <- v;
149 b.wc <- (b.wc + 1) mod cap;
150 b.rc <- b.wc;
151 b.len <- min (b.len + 1) cap;
154 let cbempty b = b.len = 0;;
156 let cbgetg b circular dir =
157 if cbempty b
158 then b.store.(0)
159 else
160 let rc = b.rc + dir in
161 let rc =
162 if circular
163 then (
164 if rc = -1
165 then b.len-1
166 else (
167 if rc = b.len
168 then 0
169 else rc
172 else max 0 (min rc (b.len-1))
174 b.rc <- rc;
175 b.store.(rc);
178 let cbget b = cbgetg b false;;
179 let cbgetc b = cbgetg b true;;
181 type page =
182 { pageno : int
183 ; pagedimno : int
184 ; pagew : int
185 ; pageh : int
186 ; pagex : int
187 ; pagey : int
188 ; pagevw : int
189 ; pagevh : int
190 ; pagedispx : int
191 ; pagedispy : int
195 let debugl l =
196 dolog "l %d dim=%d {" l.pageno l.pagedimno;
197 dolog " WxH %dx%d" l.pagew l.pageh;
198 dolog " vWxH %dx%d" l.pagevw l.pagevh;
199 dolog " pagex,y %d,%d" l.pagex l.pagey;
200 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
201 dolog "}";
204 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
205 dolog "rect {";
206 dolog " x0,y0=(% f, % f)" x0 y0;
207 dolog " x1,y1=(% f, % f)" x1 y1;
208 dolog " x2,y2=(% f, % f)" x2 y2;
209 dolog " x3,y3=(% f, % f)" x3 y3;
210 dolog "}";
213 type columns =
214 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
215 and multicol = columncount * covercount * covercount
216 and pdimno = int
217 and columncount = int
218 and covercount = int;;
220 type conf =
221 { mutable scrollbw : int
222 ; mutable scrollh : int
223 ; mutable icase : bool
224 ; mutable preload : bool
225 ; mutable pagebias : int
226 ; mutable verbose : bool
227 ; mutable debug : bool
228 ; mutable scrollstep : int
229 ; mutable maxhfit : bool
230 ; mutable crophack : bool
231 ; mutable autoscrollstep : int
232 ; mutable maxwait : float option
233 ; mutable hlinks : bool
234 ; mutable underinfo : bool
235 ; mutable interpagespace : interpagespace
236 ; mutable zoom : float
237 ; mutable presentation : bool
238 ; mutable angle : angle
239 ; mutable winw : int
240 ; mutable winh : int
241 ; mutable savebmarks : bool
242 ; mutable proportional : proportional
243 ; mutable trimmargins : trimmargins
244 ; mutable trimfuzz : irect
245 ; mutable memlimit : memsize
246 ; mutable texcount : texcount
247 ; mutable sliceheight : sliceheight
248 ; mutable thumbw : width
249 ; mutable jumpback : bool
250 ; mutable bgcolor : float * float * float
251 ; mutable bedefault : bool
252 ; mutable scrollbarinpm : bool
253 ; mutable tilew : int
254 ; mutable tileh : int
255 ; mutable mustoresize : memsize
256 ; mutable checkers : bool
257 ; mutable aalevel : int
258 ; mutable urilauncher : string
259 ; mutable pathlauncher : string
260 ; mutable colorspace : colorspace
261 ; mutable invert : bool
262 ; mutable colorscale : float
263 ; mutable redirectstderr : bool
264 ; mutable ghyllscroll : (int * int * int) option
265 ; mutable columns : columns option
266 ; mutable beyecolumns : columncount option
267 ; mutable selcmd : string
268 ; mutable updatecurs : bool
272 type anchor = pageno * top;;
274 type outline = string * int * anchor;;
276 type rect = float * float * float * float * float * float * float * float;;
278 type tile = opaque * pixmapsize * elapsed
279 and elapsed = float;;
280 type pagemapkey = pageno * gen;;
281 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
282 and row = int
283 and col = int;;
285 let emptyanchor = (0, 0.0);;
287 type infochange = | Memused | Docinfo | Pdim;;
289 class type uioh = object
290 method display : unit
291 method key : int -> int -> uioh
292 method button : int -> bool -> int -> int -> int -> uioh
293 method motion : int -> int -> uioh
294 method pmotion : int -> int -> uioh
295 method infochanged : infochange -> unit
296 method scrollpw : (int * float * float)
297 method scrollph : (int * float * float)
298 end;;
300 type mode =
301 | Birdseye of (conf * leftx * pageno * pageno * anchor)
302 | Textentry of (textentry * onleave)
303 | View
304 and onleave = leavetextentrystatus -> unit
305 and leavetextentrystatus = | Cancel | Confirm
306 and helpitem = string * int * action
307 and action =
308 | Noaction
309 | Action of (uioh -> uioh)
312 let isbirdseye = function Birdseye _ -> true | _ -> false;;
313 let istextentry = function Textentry _ -> true | _ -> false;;
315 type currently =
316 | Idle
317 | Loading of (page * gen)
318 | Tiling of (
319 page * opaque * colorspace * angle * gen * col * row * width * height
321 | Outlining of outline list
324 let nouioh : uioh = object (self)
325 method display = ()
326 method key _ _ = self
327 method button _ _ _ _ _ = self
328 method motion _ _ = self
329 method pmotion _ _ = self
330 method infochanged _ = ()
331 method scrollpw = (0, nan, nan)
332 method scrollph = (0, nan, nan)
333 end;;
335 type state =
336 { mutable sr : Unix.file_descr
337 ; mutable sw : Unix.file_descr
338 ; mutable wsfd : Unix.file_descr
339 ; mutable errfd : Unix.file_descr option
340 ; mutable stderr : Unix.file_descr
341 ; mutable errmsgs : Buffer.t
342 ; mutable newerrmsgs : bool
343 ; mutable w : int
344 ; mutable x : int
345 ; mutable y : int
346 ; mutable scrollw : int
347 ; mutable hscrollh : int
348 ; mutable anchor : anchor
349 ; mutable ranchors : (string * string * anchor) list
350 ; mutable maxy : int
351 ; mutable layout : page list
352 ; pagemap : (pagemapkey, opaque) Hashtbl.t
353 ; tilemap : (tilemapkey, tile) Hashtbl.t
354 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
355 ; mutable pdims : (pageno * width * height * leftx) list
356 ; mutable pagecount : int
357 ; mutable currently : currently
358 ; mutable mstate : mstate
359 ; mutable searchpattern : string
360 ; mutable rects : (pageno * recttype * rect) list
361 ; mutable rects1 : (pageno * recttype * rect) list
362 ; mutable text : string
363 ; mutable fullscreen : (width * height) option
364 ; mutable mode : mode
365 ; mutable uioh : uioh
366 ; mutable outlines : outline array
367 ; mutable bookmarks : outline list
368 ; mutable path : string
369 ; mutable password : string
370 ; mutable invalidated : int
371 ; mutable memused : memsize
372 ; mutable gen : gen
373 ; mutable throttle : (page list * int * float) option
374 ; mutable autoscroll : int option
375 ; mutable ghyll : (int option -> unit)
376 ; mutable help : helpitem array
377 ; mutable docinfo : (int * string) list
378 ; mutable texid : GlTex.texture_id option
379 ; hists : hists
380 ; mutable prevzoom : float
381 ; mutable progress : float
382 ; mutable redisplay : bool
383 ; mutable mpos : mpos
385 and hists =
386 { pat : string circbuf
387 ; pag : string circbuf
388 ; nav : anchor circbuf
389 ; sel : string circbuf
393 let defconf =
394 { scrollbw = 7
395 ; scrollh = 12
396 ; icase = true
397 ; preload = true
398 ; pagebias = 0
399 ; verbose = false
400 ; debug = false
401 ; scrollstep = 24
402 ; maxhfit = true
403 ; crophack = false
404 ; autoscrollstep = 2
405 ; maxwait = None
406 ; hlinks = false
407 ; underinfo = false
408 ; interpagespace = 2
409 ; zoom = 1.0
410 ; presentation = false
411 ; angle = 0
412 ; winw = 900
413 ; winh = 900
414 ; savebmarks = true
415 ; proportional = true
416 ; trimmargins = false
417 ; trimfuzz = (0,0,0,0)
418 ; memlimit = 32 lsl 20
419 ; texcount = 256
420 ; sliceheight = 24
421 ; thumbw = 76
422 ; jumpback = true
423 ; bgcolor = (0.5, 0.5, 0.5)
424 ; bedefault = false
425 ; scrollbarinpm = true
426 ; tilew = 2048
427 ; tileh = 2048
428 ; mustoresize = 128 lsl 20
429 ; checkers = true
430 ; aalevel = 8
431 ; urilauncher =
432 (match platform with
433 | Plinux | Pfreebsd | Pdragonflybsd
434 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
435 | Posx -> "open \"%s\""
436 | Pcygwin -> "cygstart %s"
437 | Punknown -> "echo %s")
438 ; pathlauncher = "lp \"%s\""
439 ; selcmd =
440 (match platform with
441 | Plinux | Pfreebsd | Pdragonflybsd
442 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
443 | Posx -> "pbcopy"
444 | Pcygwin -> "wsel"
445 | Punknown -> "cat")
446 ; colorspace = Rgb
447 ; invert = false
448 ; colorscale = 1.0
449 ; redirectstderr = false
450 ; ghyllscroll = None
451 ; columns = None
452 ; beyecolumns = None
453 ; updatecurs = false
457 let conf = { defconf with angle = defconf.angle };;
459 type fontstate =
460 { mutable fontsize : int
461 ; mutable wwidth : float
462 ; mutable maxrows : int
466 let fstate =
467 { fontsize = 14
468 ; wwidth = nan
469 ; maxrows = -1
473 let setfontsize n =
474 fstate.fontsize <- n;
475 fstate.wwidth <- measurestr fstate.fontsize "w";
476 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
479 let geturl s =
480 let colonpos = try String.index s ':' with Not_found -> -1 in
481 let len = String.length s in
482 if colonpos >= 0 && colonpos + 3 < len
483 then (
484 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
485 then
486 let schemestartpos =
487 try String.rindex_from s colonpos ' '
488 with Not_found -> -1
490 let scheme =
491 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
493 match scheme with
494 | "http" | "ftp" | "mailto" ->
495 let epos =
496 try String.index_from s colonpos ' '
497 with Not_found -> len
499 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
500 | _ -> ""
501 else ""
503 else ""
506 let popen =
507 let shell, farg = "/bin/sh", "-c" in
508 fun s ->
509 let args = [|shell; farg; s|] in
510 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
513 let gotouri uri =
514 if String.length conf.urilauncher = 0
515 then print_endline uri
516 else (
517 let url = geturl uri in
518 if String.length url = 0
519 then print_endline uri
520 else
521 let re = Str.regexp "%s" in
522 let command = Str.global_replace re url conf.urilauncher in
523 try popen command
524 with exn ->
525 Printf.eprintf
526 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
527 flush stderr;
531 let version () =
532 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
533 (platform_to_string platform) Sys.word_size Sys.ocaml_version
536 let makehelp () =
537 let strings = version () :: "" :: Help.keys in
538 Array.of_list (
539 List.map (fun s ->
540 let url = geturl s in
541 if String.length url > 0
542 then (s, 0, Action (fun u -> gotouri url; u))
543 else (s, 0, Noaction)
544 ) strings);
547 let noghyll _ = ();;
549 let state =
550 { sr = Unix.stdin
551 ; sw = Unix.stdin
552 ; wsfd = Unix.stdin
553 ; errfd = None
554 ; stderr = Unix.stderr
555 ; errmsgs = Buffer.create 0
556 ; newerrmsgs = false
557 ; x = 0
558 ; y = 0
559 ; w = 0
560 ; scrollw = 0
561 ; hscrollh = 0
562 ; anchor = emptyanchor
563 ; ranchors = []
564 ; layout = []
565 ; maxy = max_int
566 ; tilelru = Queue.create ()
567 ; pagemap = Hashtbl.create 10
568 ; tilemap = Hashtbl.create 10
569 ; pdims = []
570 ; pagecount = 0
571 ; currently = Idle
572 ; mstate = Mnone
573 ; rects = []
574 ; rects1 = []
575 ; text = ""
576 ; mode = View
577 ; fullscreen = None
578 ; searchpattern = ""
579 ; outlines = [||]
580 ; bookmarks = []
581 ; path = ""
582 ; password = ""
583 ; invalidated = 0
584 ; hists =
585 { nav = cbnew 10 (0, 0.0)
586 ; pat = cbnew 10 ""
587 ; pag = cbnew 10 ""
588 ; sel = cbnew 10 ""
590 ; memused = 0
591 ; gen = 0
592 ; throttle = None
593 ; autoscroll = None
594 ; ghyll = noghyll
595 ; help = makehelp ()
596 ; docinfo = []
597 ; texid = None
598 ; prevzoom = 1.0
599 ; progress = -1.0
600 ; uioh = nouioh
601 ; redisplay = false
602 ; mpos = (-1, -1)
606 let vlog fmt =
607 if conf.verbose
608 then
609 Printf.kprintf prerr_endline fmt
610 else
611 Printf.kprintf ignore fmt
614 let launchpath () =
615 if String.length conf.pathlauncher = 0
616 then print_endline state.path
617 else (
618 let re = Str.regexp "%s" in
619 let command = Str.global_replace re state.path conf.pathlauncher in
620 try popen command
621 with exn ->
622 Printf.eprintf
623 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
624 flush stderr;
628 let redirectstderr () =
629 if conf.redirectstderr
630 then
631 let rfd, wfd = Unix.pipe () in
632 state.stderr <- Unix.dup Unix.stderr;
633 state.errfd <- Some rfd;
634 Unix.dup2 wfd Unix.stderr;
635 else (
636 state.newerrmsgs <- false;
637 begin match state.errfd with
638 | Some fd ->
639 Unix.close fd;
640 Unix.dup2 state.stderr Unix.stderr;
641 state.errfd <- None;
642 | None -> ()
643 end;
644 prerr_string (Buffer.contents state.errmsgs);
645 flush stderr;
646 Buffer.clear state.errmsgs;
650 module G =
651 struct
652 let postRedisplay who =
653 if conf.verbose
654 then prerr_endline ("redisplay for " ^ who);
655 state.redisplay <- true;
657 end;;
659 let getopaque pageno =
660 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
661 with Not_found -> None
664 let putopaque pageno opaque =
665 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
668 let pagetranslatepoint l x y =
669 let dy = y - l.pagedispy in
670 let y = dy + l.pagey in
671 let dx = x - l.pagedispx in
672 let x = dx + l.pagex in
673 (x, y);
676 let getunder x y =
677 let rec f = function
678 | l :: rest ->
679 begin match getopaque l.pageno with
680 | Some opaque ->
681 let x0 = l.pagedispx in
682 let x1 = x0 + l.pagevw in
683 let y0 = l.pagedispy in
684 let y1 = y0 + l.pagevh in
685 if y >= y0 && y <= y1 && x >= x0 && x <= x1
686 then
687 let px, py = pagetranslatepoint l x y in
688 match whatsunder opaque px py with
689 | Unone -> f rest
690 | under -> under
691 else f rest
692 | _ ->
693 f rest
695 | [] -> Unone
697 f state.layout
700 let showtext c s =
701 state.text <- Printf.sprintf "%c%s" c s;
702 G.postRedisplay "showtext";
705 let updateunder x y =
706 match getunder x y with
707 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
708 | Ulinkuri uri ->
709 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
710 Wsi.setcursor Wsi.CURSOR_INFO
711 | Ulinkgoto (page, _) ->
712 if conf.underinfo
713 then showtext 'p' ("age: " ^ string_of_int (page+1));
714 Wsi.setcursor Wsi.CURSOR_INFO
715 | Utext s ->
716 if conf.underinfo then showtext 'f' ("ont: " ^ s);
717 Wsi.setcursor Wsi.CURSOR_TEXT
718 | Uunexpected s ->
719 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
720 Wsi.setcursor Wsi.CURSOR_INHERIT
721 | Ulaunch s ->
722 if conf.underinfo then showtext 'l' ("launch: " ^ s);
723 Wsi.setcursor Wsi.CURSOR_INHERIT
724 | Unamed s ->
725 if conf.underinfo then showtext 'n' ("named: " ^ s);
726 Wsi.setcursor Wsi.CURSOR_INHERIT
727 | Uremote (filename, pageno) ->
728 if conf.underinfo then showtext 'r'
729 (Printf.sprintf "emote: %s (%d)" filename pageno);
730 Wsi.setcursor Wsi.CURSOR_INFO
733 let addchar s c =
734 let b = Buffer.create (String.length s + 1) in
735 Buffer.add_string b s;
736 Buffer.add_char b c;
737 Buffer.contents b;
740 let colorspace_of_string s =
741 match String.lowercase s with
742 | "rgb" -> Rgb
743 | "bgr" -> Bgr
744 | "gray" -> Gray
745 | _ -> failwith "invalid colorspace"
748 let int_of_colorspace = function
749 | Rgb -> 0
750 | Bgr -> 1
751 | Gray -> 2
754 let colorspace_of_int = function
755 | 0 -> Rgb
756 | 1 -> Bgr
757 | 2 -> Gray
758 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
761 let colorspace_to_string = function
762 | Rgb -> "rgb"
763 | Bgr -> "bgr"
764 | Gray -> "gray"
767 let intentry_with_suffix text key =
768 let c =
769 if key >= 32 && key < 127
770 then Char.chr key
771 else '\000'
773 match Char.lowercase c with
774 | '0' .. '9' ->
775 let text = addchar text c in
776 TEcont text
778 | 'k' | 'm' | 'g' ->
779 let text = addchar text c in
780 TEcont text
782 | _ ->
783 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
784 TEcont text
787 let columns_to_string (n, a, b) =
788 if a = 0 && b = 0
789 then Printf.sprintf "%d" n
790 else Printf.sprintf "%d,%d,%d" n a b;
793 let columns_of_string s =
795 (int_of_string s, 0, 0)
796 with _ ->
797 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
800 let writecmd fd s =
801 let len = String.length s in
802 let n = 4 + len in
803 let b = Buffer.create n in
804 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
805 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
806 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
807 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
808 Buffer.add_string b s;
809 let s' = Buffer.contents b in
810 let n' = Unix.write fd s' 0 n in
811 if n' != n then failwith "write failed";
814 let readcmd fd =
815 let s = "xxxx" in
816 let n = Unix.read fd s 0 4 in
817 if n != 4 then failwith "incomplete read(len)";
818 let len = 0
819 lor (Char.code s.[0] lsl 24)
820 lor (Char.code s.[1] lsl 16)
821 lor (Char.code s.[2] lsl 8)
822 lor (Char.code s.[3] lsl 0)
824 let s = String.create len in
825 let n = Unix.read fd s 0 len in
826 if n != len then failwith "incomplete read(data)";
830 let makecmd s l =
831 let b = Buffer.create 10 in
832 Buffer.add_string b s;
833 let rec combine = function
834 | [] -> b
835 | x :: xs ->
836 Buffer.add_char b ' ';
837 let s =
838 match x with
839 | `b b -> if b then "1" else "0"
840 | `s s -> s
841 | `i i -> string_of_int i
842 | `f f -> string_of_float f
843 | `I f -> string_of_int (truncate f)
845 Buffer.add_string b s;
846 combine xs;
848 combine l;
851 let wcmd s l =
852 let cmd = Buffer.contents (makecmd s l) in
853 writecmd state.sw cmd;
856 let calcips h =
857 if conf.presentation
858 then
859 let d = conf.winh - h in
860 max 0 ((d + 1) / 2)
861 else
862 conf.interpagespace
865 let calcheight () =
866 let rec f pn ph pi fh l =
867 match l with
868 | (n, _, h, _) :: rest ->
869 let ips = calcips h in
870 let fh =
871 if conf.presentation
872 then fh+ips
873 else (
874 if isbirdseye state.mode && pn = 0
875 then fh + ips
876 else fh
879 let fh = fh + ((n - pn) * (ph + pi)) in
880 f n h ips fh rest;
882 | [] ->
883 let inc =
884 if conf.presentation || (isbirdseye state.mode && pn = 0)
885 then 0
886 else -pi
888 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
889 max 0 fh
891 let fh = f 0 0 0 0 state.pdims in
895 let calcheight () =
896 match conf.columns with
897 | None -> calcheight ()
898 | Some (_, b) ->
899 if Array.length b > 0
900 then
901 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
902 y + h
903 else 0
906 let getpageyh pageno =
907 let rec f pn ph pi y l =
908 match l with
909 | (n, _, h, _) :: rest ->
910 let ips = calcips h in
911 if n >= pageno
912 then
913 let h = if n = pageno then h else ph in
914 if conf.presentation && n = pageno
915 then
916 y + (pageno - pn) * (ph + pi) + pi, h
917 else
918 y + (pageno - pn) * (ph + pi), h
919 else
920 let y = y + (if conf.presentation then pi else 0) in
921 let y = y + (n - pn) * (ph + pi) in
922 f n h ips y rest
924 | [] ->
925 y + (pageno - pn) * (ph + pi), ph
927 f 0 0 0 0 state.pdims
930 let getpageyh pageno =
931 match conf.columns with
932 | None -> getpageyh pageno
933 | Some (_, b) ->
934 let (_, _, y, (_, _, h, _)) = b.(pageno) in
935 y, h
938 let getpagedim pageno =
939 let rec f ppdim l =
940 match l with
941 | (n, _, _, _) as pdim :: rest ->
942 if n >= pageno
943 then (if n = pageno then pdim else ppdim)
944 else f pdim rest
946 | [] -> ppdim
948 f (-1, -1, -1, -1) state.pdims
951 let getpagey pageno = fst (getpageyh pageno);;
953 let layout1 y sh =
954 let sh = sh - state.hscrollh in
955 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
956 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
957 match pdims with
958 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
959 let ips = calcips h in
960 let yinc =
961 if conf.presentation || (isbirdseye state.mode && pageno = 0)
962 then ips
963 else 0
965 (w, h, ips, xoff), rest, pdimno + 1, yinc
966 | _ ->
967 prev, pdims, pdimno, 0
969 let dy = dy + yinc in
970 let py = py + yinc in
971 if pageno = state.pagecount || dy >= sh
972 then
973 accu
974 else
975 let vy = y + dy in
976 if py + h <= vy - yinc
977 then
978 let py = py + h + ips in
979 let dy = max 0 (py - y) in
980 f ~pageno:(pageno+1)
981 ~pdimno
982 ~prev:curr
985 ~pdims:rest
986 ~accu
987 else
988 let pagey = vy - py in
989 let pagevh = h - pagey in
990 let pagevh = min (sh - dy) pagevh in
991 let off = if yinc > 0 then py - vy else 0 in
992 let py = py + h + ips in
993 let pagex, dx =
994 let xoff = xoff +
995 if state.w < conf.winw - state.scrollw
996 then (conf.winw - state.scrollw - state.w) / 2
997 else 0
999 let dispx = xoff + state.x in
1000 if dispx < 0
1001 then (-dispx, 0)
1002 else (0, dispx)
1004 let pagevw =
1005 let lw = w - pagex in
1006 min lw (conf.winw - state.scrollw)
1008 let e =
1009 { pageno = pageno
1010 ; pagedimno = pdimno
1011 ; pagew = w
1012 ; pageh = h
1013 ; pagex = pagex
1014 ; pagey = pagey + off
1015 ; pagevw = pagevw
1016 ; pagevh = pagevh - off
1017 ; pagedispx = dx
1018 ; pagedispy = dy + off
1021 let accu = e :: accu in
1022 f ~pageno:(pageno+1)
1023 ~pdimno
1024 ~prev:curr
1026 ~dy:(dy+pagevh+ips)
1027 ~pdims:rest
1028 ~accu
1030 if state.invalidated = 0
1031 then (
1032 let accu =
1034 ~pageno:0
1035 ~pdimno:~-1
1036 ~prev:(0,0,0,0)
1037 ~py:0
1038 ~dy:0
1039 ~pdims:state.pdims
1040 ~accu:[]
1042 List.rev accu
1044 else
1048 let layoutN ((columns, coverA, coverB), b) y sh =
1049 let sh = sh - state.hscrollh in
1050 let rec fold accu n =
1051 if n = Array.length b
1052 then accu
1053 else
1054 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1055 if (vy - y) > sh &&
1056 (n = coverA - 1
1057 || n = state.pagecount - coverB
1058 || (n - coverA) mod columns = columns - 1)
1059 then accu
1060 else
1061 let accu =
1062 if vy + h > y
1063 then
1064 let pagey = max 0 (y - vy) in
1065 let pagedispy = if pagey > 0 then 0 else vy - y in
1066 let pagedispx, pagex, pagevw =
1067 let pdx =
1068 if n = coverA - 1 || n = state.pagecount - coverB
1069 then state.x + (conf.winw - state.scrollw - w) / 2
1070 else dx + xoff + state.x
1072 if pdx < 0
1073 then 0, -pdx, w + pdx
1074 else pdx, 0, min (conf.winw - state.scrollw) w
1076 let pagevh = min (h - pagey) (sh - pagedispy) in
1077 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1078 then
1079 let e =
1080 { pageno = n
1081 ; pagedimno = pdimno
1082 ; pagew = w
1083 ; pageh = h
1084 ; pagex = pagex
1085 ; pagey = pagey
1086 ; pagevw = pagevw
1087 ; pagevh = pagevh
1088 ; pagedispx = pagedispx
1089 ; pagedispy = pagedispy
1092 e :: accu
1093 else
1094 accu
1095 else
1096 accu
1098 fold accu (n+1)
1100 if state.invalidated = 0
1101 then List.rev (fold [] 0)
1102 else []
1105 let layout y sh =
1106 match conf.columns with
1107 | None -> layout1 y sh
1108 | Some c -> layoutN c y sh
1111 let clamp incr =
1112 let y = state.y + incr in
1113 let y = max 0 y in
1114 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1118 let itertiles l f =
1119 let tilex = l.pagex mod conf.tilew in
1120 let tiley = l.pagey mod conf.tileh in
1122 let col = l.pagex / conf.tilew in
1123 let row = l.pagey / conf.tileh in
1125 let vw =
1126 let a = l.pagew - l.pagex in
1127 let b = conf.winw - state.scrollw in
1128 min a b
1129 and vh = l.pagevh in
1131 let rec rowloop row y0 dispy h =
1132 if h = 0
1133 then ()
1134 else (
1135 let dh = conf.tileh - y0 in
1136 let dh = min h dh in
1137 let rec colloop col x0 dispx w =
1138 if w = 0
1139 then ()
1140 else (
1141 let dw = conf.tilew - x0 in
1142 let dw = min w dw in
1144 f col row dispx dispy x0 y0 dw dh;
1145 colloop (col+1) 0 (dispx+dw) (w-dw)
1148 colloop col tilex l.pagedispx vw;
1149 rowloop (row+1) 0 (dispy+dh) (h-dh)
1152 if vw > 0 && vh > 0
1153 then rowloop row tiley l.pagedispy vh;
1156 let gettileopaque l col row =
1157 let key =
1158 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1160 try Some (Hashtbl.find state.tilemap key)
1161 with Not_found -> None
1164 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1165 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1166 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1169 let drawtiles l color =
1170 GlDraw.color color;
1171 let f col row x y tilex tiley w h =
1172 match gettileopaque l col row with
1173 | Some (opaque, _, t) ->
1174 let params = x, y, w, h, tilex, tiley in
1175 if conf.invert
1176 then (
1177 Gl.enable `blend;
1178 GlFunc.blend_func `zero `one_minus_src_color;
1180 drawtile params opaque;
1181 if conf.invert
1182 then Gl.disable `blend;
1183 if conf.debug
1184 then (
1185 let s = Printf.sprintf
1186 "%d[%d,%d] %f sec"
1187 l.pageno col row t
1189 let w = measurestr fstate.fontsize s in
1190 GlMisc.push_attrib [`current];
1191 GlDraw.color (0.0, 0.0, 0.0);
1192 GlDraw.rect
1193 (float (x-2), float (y-2))
1194 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1195 GlDraw.color (1.0, 1.0, 1.0);
1196 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1197 GlMisc.pop_attrib ();
1200 | _ ->
1201 let w =
1202 let lw = conf.winw - state.scrollw - x in
1203 min lw w
1204 and h =
1205 let lh = conf.winh - y in
1206 min lh h
1208 Gl.enable `texture_2d;
1209 begin match state.texid with
1210 | Some id ->
1211 GlTex.bind_texture `texture_2d id;
1212 let x0 = float x
1213 and y0 = float y
1214 and x1 = float (x+w)
1215 and y1 = float (y+h) in
1217 let tw = float w /. 64.0
1218 and th = float h /. 64.0 in
1219 let tx0 = float tilex /. 64.0
1220 and ty0 = float tiley /. 64.0 in
1221 let tx1 = tx0 +. tw
1222 and ty1 = ty0 +. th in
1223 GlDraw.begins `quads;
1224 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1225 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1226 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1227 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1228 GlDraw.ends ();
1230 Gl.disable `texture_2d;
1231 | None ->
1232 GlDraw.color (1.0, 1.0, 1.0);
1233 GlDraw.rect
1234 (float x, float y)
1235 (float (x+w), float (y+h));
1236 end;
1237 if w > 128 && h > fstate.fontsize + 10
1238 then (
1239 GlDraw.color (0.0, 0.0, 0.0);
1240 let c, r =
1241 if conf.verbose
1242 then (col*conf.tilew, row*conf.tileh)
1243 else col, row
1245 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1247 GlDraw.color color;
1249 itertiles l f
1252 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1254 let tilevisible1 l x y =
1255 let ax0 = l.pagex
1256 and ax1 = l.pagex + l.pagevw
1257 and ay0 = l.pagey
1258 and ay1 = l.pagey + l.pagevh in
1260 let bx0 = x
1261 and by0 = y in
1262 let bx1 = min (bx0 + conf.tilew) l.pagew
1263 and by1 = min (by0 + conf.tileh) l.pageh in
1265 let rx0 = max ax0 bx0
1266 and ry0 = max ay0 by0
1267 and rx1 = min ax1 bx1
1268 and ry1 = min ay1 by1 in
1270 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1271 nonemptyintersection
1274 let tilevisible layout n x y =
1275 let rec findpageinlayout = function
1276 | l :: _ when l.pageno = n -> tilevisible1 l x y
1277 | _ :: rest -> findpageinlayout rest
1278 | [] -> false
1280 findpageinlayout layout
1283 let tileready l x y =
1284 tilevisible1 l x y &&
1285 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1288 let tilepage n p layout =
1289 let rec loop = function
1290 | l :: rest ->
1291 if l.pageno = n
1292 then
1293 let f col row _ _ _ _ _ _ =
1294 if state.currently = Idle
1295 then
1296 match gettileopaque l col row with
1297 | Some _ -> ()
1298 | None ->
1299 let x = col*conf.tilew
1300 and y = row*conf.tileh in
1301 let w =
1302 let w = l.pagew - x in
1303 min w conf.tilew
1305 let h =
1306 let h = l.pageh - y in
1307 min h conf.tileh
1309 wcmd "tile"
1310 [`s p
1311 ;`i x
1312 ;`i y
1313 ;`i w
1314 ;`i h
1316 state.currently <-
1317 Tiling (
1318 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1319 conf.tilew, conf.tileh
1322 itertiles l f;
1323 else
1324 loop rest
1326 | [] -> ()
1328 if state.invalidated = 0 then loop layout;
1331 let preloadlayout visiblepages =
1332 let presentation = conf.presentation in
1333 let interpagespace = conf.interpagespace in
1334 let maxy = state.maxy in
1335 conf.presentation <- false;
1336 conf.interpagespace <- 0;
1337 state.maxy <- calcheight ();
1338 let y =
1339 match visiblepages with
1340 | [] -> 0
1341 | l :: _ -> getpagey l.pageno + l.pagey
1343 let y = if y < conf.winh then 0 else y - conf.winh in
1344 let h = state.y - y + conf.winh*3 in
1345 let pages = layout y h in
1346 conf.presentation <- presentation;
1347 conf.interpagespace <- interpagespace;
1348 state.maxy <- maxy;
1349 pages;
1352 let load pages =
1353 let rec loop pages =
1354 if state.currently != Idle
1355 then ()
1356 else
1357 match pages with
1358 | l :: rest ->
1359 begin match getopaque l.pageno with
1360 | None ->
1361 wcmd "page" [`i l.pageno; `i l.pagedimno];
1362 state.currently <- Loading (l, state.gen);
1363 | Some opaque ->
1364 tilepage l.pageno opaque pages;
1365 loop rest
1366 end;
1367 | _ -> ()
1369 if state.invalidated = 0 then loop pages
1372 let preload pages =
1373 load pages;
1374 if conf.preload && state.currently = Idle
1375 then load (preloadlayout pages);
1378 let layoutready layout =
1379 let rec fold all ls =
1380 all && match ls with
1381 | l :: rest ->
1382 let seen = ref false in
1383 let allvisible = ref true in
1384 let foo col row _ _ _ _ _ _ =
1385 seen := true;
1386 allvisible := !allvisible &&
1387 begin match gettileopaque l col row with
1388 | Some _ -> true
1389 | None -> false
1392 itertiles l foo;
1393 fold (!seen && !allvisible) rest
1394 | [] -> true
1396 let alltilesvisible = fold true layout in
1397 alltilesvisible;
1400 let gotoy y =
1401 let y = bound y 0 state.maxy in
1402 let y, layout, proceed =
1403 match conf.maxwait with
1404 | Some time when state.ghyll == noghyll ->
1405 begin match state.throttle with
1406 | None ->
1407 let layout = layout y conf.winh in
1408 let ready = layoutready layout in
1409 if not ready
1410 then (
1411 load layout;
1412 state.throttle <- Some (layout, y, now ());
1414 else G.postRedisplay "gotoy showall (None)";
1415 y, layout, ready
1416 | Some (_, _, started) ->
1417 let dt = now () -. started in
1418 if dt > time
1419 then (
1420 state.throttle <- None;
1421 let layout = layout y conf.winh in
1422 load layout;
1423 G.postRedisplay "maxwait";
1424 y, layout, true
1426 else -1, [], false
1429 | _ ->
1430 let layout = layout y conf.winh in
1431 if true || layoutready layout
1432 then G.postRedisplay "gotoy ready";
1433 y, layout, true
1435 if proceed
1436 then (
1437 state.y <- y;
1438 state.layout <- layout;
1439 begin match state.mode with
1440 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1441 if not (pagevisible layout pageno)
1442 then (
1443 match state.layout with
1444 | [] -> ()
1445 | l :: _ ->
1446 state.mode <- Birdseye (
1447 conf, leftx, l.pageno, hooverpageno, anchor
1450 | _ -> ()
1451 end;
1452 preload layout;
1454 state.ghyll <- noghyll;
1457 let conttiling pageno opaque =
1458 tilepage pageno opaque
1459 (if conf.preload then preloadlayout state.layout else state.layout)
1462 let gotoy_and_clear_text y =
1463 gotoy y;
1464 if not conf.verbose then state.text <- "";
1467 let getanchor () =
1468 match state.layout with
1469 | [] -> emptyanchor
1470 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1473 let getanchory (n, top) =
1474 let y, h = getpageyh n in
1475 y + (truncate (top *. float h));
1478 let gotoanchor anchor =
1479 gotoy (getanchory anchor);
1482 let addnav () =
1483 cbput state.hists.nav (getanchor ());
1486 let getnav dir =
1487 let anchor = cbgetc state.hists.nav dir in
1488 getanchory anchor;
1491 let gotoghyll y =
1492 let rec scroll f n a b =
1493 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1494 let snake f a b =
1495 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1496 if f < a
1497 then s (float f /. float a)
1498 else (
1499 if f > b
1500 then 1.0 -. s ((float (f-b) /. float (n-b)))
1501 else 1.0
1504 snake f a b
1505 and summa f n a b =
1506 (* courtesy:
1507 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1508 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1509 let iv1 = iv f in
1510 let ins = float a *. iv1
1511 and outs = float (n-b) *. iv1 in
1512 let ones = b - a in
1513 ins +. outs +. float ones
1515 let rec set (_N, _A, _B) y sy =
1516 let sum = summa 1.0 _N _A _B in
1517 let dy = float (y - sy) in
1518 state.ghyll <- (
1519 let rec gf n y1 o =
1520 if n >= _N
1521 then state.ghyll <- noghyll
1522 else
1523 let go n =
1524 let s = scroll n _N _A _B in
1525 let y1 = y1 +. ((s *. dy) /. sum) in
1526 gotoy_and_clear_text (truncate y1);
1527 state.ghyll <- gf (n+1) y1;
1529 match o with
1530 | None -> go n
1531 | Some y' -> set (_N/2, 0, 0) y' state.y
1533 gf 0 (float state.y)
1536 match conf.ghyllscroll with
1537 | None ->
1538 gotoy_and_clear_text y
1539 | Some nab ->
1540 if state.ghyll == noghyll
1541 then set nab y state.y
1542 else state.ghyll (Some y)
1545 let gotopage n top =
1546 let y, h = getpageyh n in
1547 let y = y + (truncate (top *. float h)) in
1548 gotoghyll y
1551 let gotopage1 n top =
1552 let y = getpagey n in
1553 let y = y + top in
1554 gotoghyll y
1557 let invalidate () =
1558 state.layout <- [];
1559 state.pdims <- [];
1560 state.rects <- [];
1561 state.rects1 <- [];
1562 state.invalidated <- state.invalidated + 1;
1565 let writeopen path password =
1566 writecmd state.sw ("open " ^ path ^ "\000" ^ password ^ "\000");
1569 let opendoc path password =
1570 invalidate ();
1571 state.path <- path;
1572 state.password <- password;
1573 state.gen <- state.gen + 1;
1574 state.docinfo <- [];
1576 setaalevel conf.aalevel;
1577 writeopen path password;
1578 Wsi.settitle ("llpp " ^ Filename.basename path);
1579 wcmd "geometry" [`i state.w; `i conf.winh];
1582 let scalecolor c =
1583 let c = c *. conf.colorscale in
1584 (c, c, c);
1587 let scalecolor2 (r, g, b) =
1588 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1591 let represent () =
1592 let docolumns = function
1593 | None -> ()
1594 | Some ((columns, coverA, coverB), _) ->
1595 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1596 let rec loop pageno pdimno pdim x y rowh pdims =
1597 if pageno = state.pagecount
1598 then ()
1599 else
1600 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1601 match pdims with
1602 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1603 pdimno+1, pdim, rest
1604 | _ ->
1605 pdimno, pdim, pdims
1607 let x, y, rowh' =
1608 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1609 then (
1610 (conf.winw - state.scrollw - w) / 2,
1611 y + rowh + conf.interpagespace, h
1613 else (
1614 if (pageno - coverA) mod columns = 0
1615 then 0, y + rowh + conf.interpagespace, h
1616 else x, y, max rowh h
1619 let rec fixrow m = if m = pageno then () else
1620 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1621 if h < rowh
1622 then (
1623 let y = y + (rowh - h) / 2 in
1624 a.(m) <- (pdimno, x, y, pdim);
1626 fixrow (m+1)
1628 if pageno > 1 && (pageno - coverA) mod columns = 0
1629 then fixrow (pageno - columns);
1630 a.(pageno) <- (pdimno, x, y, pdim);
1631 let x = x + w + xoff*2 + conf.interpagespace in
1632 loop (pageno+1) pdimno pdim x y rowh' pdims
1634 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1635 conf.columns <- Some ((columns, coverA, coverB), a);
1637 docolumns conf.columns;
1638 state.maxy <- calcheight ();
1639 state.hscrollh <-
1640 if state.w <= conf.winw - state.scrollw
1641 then 0
1642 else state.scrollw
1644 match state.mode with
1645 | Birdseye (_, _, pageno, _, _) ->
1646 let y, h = getpageyh pageno in
1647 let top = (conf.winh - h) / 2 in
1648 gotoy (max 0 (y - top))
1649 | _ -> gotoanchor state.anchor
1652 let reshape =
1653 let firsttime = ref true in
1654 fun ~w ~h ->
1655 GlDraw.viewport 0 0 w h;
1656 if state.invalidated = 0 && not !firsttime
1657 then state.anchor <- getanchor ();
1659 firsttime := false;
1660 conf.winw <- w;
1661 let w = truncate (float w *. conf.zoom) - state.scrollw in
1662 let w = max w 2 in
1663 state.w <- w;
1664 conf.winh <- h;
1665 setfontsize fstate.fontsize;
1666 GlMat.mode `modelview;
1667 GlMat.load_identity ();
1669 GlMat.mode `projection;
1670 GlMat.load_identity ();
1671 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1672 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1673 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1675 let w =
1676 match conf.columns with
1677 | None -> w
1678 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1680 invalidate ();
1681 wcmd "geometry" [`i w; `i h];
1684 let enttext () =
1685 let len = String.length state.text in
1686 let drawstring s =
1687 let hscrollh =
1688 match state.mode with
1689 | View -> state.hscrollh
1690 | _ -> 0
1692 let rect x w =
1693 GlDraw.rect
1694 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1695 (x+.w, float (conf.winh - hscrollh))
1698 let w = float (conf.winw - state.scrollw - 1) in
1699 if state.progress >= 0.0 && state.progress < 1.0
1700 then (
1701 GlDraw.color (0.3, 0.3, 0.3);
1702 let w1 = w *. state.progress in
1703 rect 0.0 w1;
1704 GlDraw.color (0.0, 0.0, 0.0);
1705 rect w1 (w-.w1)
1707 else (
1708 GlDraw.color (0.0, 0.0, 0.0);
1709 rect 0.0 w;
1712 GlDraw.color (1.0, 1.0, 1.0);
1713 drawstring fstate.fontsize
1714 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1716 let s =
1717 match state.mode with
1718 | Textentry ((prefix, text, _, _, _), _) ->
1719 let s =
1720 if len > 0
1721 then
1722 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1723 else
1724 Printf.sprintf "%s%s_" prefix text
1728 | _ -> state.text
1730 let s =
1731 if state.newerrmsgs
1732 then (
1733 if not (istextentry state.mode)
1734 then
1735 let s1 = "(press 'e' to review error messasges)" in
1736 if String.length s > 0 then s ^ " " ^ s1 else s1
1737 else s
1739 else s
1741 if String.length s > 0
1742 then drawstring s
1745 let gctiles () =
1746 let len = Queue.length state.tilelru in
1747 let rec loop qpos =
1748 if state.memused <= conf.memlimit
1749 then ()
1750 else (
1751 if qpos < len
1752 then
1753 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1754 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1755 let (_, pw, ph, _) = getpagedim n in
1757 gen = state.gen
1758 && colorspace = conf.colorspace
1759 && angle = conf.angle
1760 && pagew = pw
1761 && pageh = ph
1762 && (
1763 let layout =
1764 match state.throttle with
1765 | None ->
1766 if conf.preload
1767 then preloadlayout state.layout
1768 else state.layout
1769 | Some (layout, _, _) ->
1770 layout
1772 let x = col*conf.tilew
1773 and y = row*conf.tileh in
1774 tilevisible layout n x y
1776 then Queue.push lruitem state.tilelru
1777 else (
1778 wcmd "freetile" [`s p];
1779 state.memused <- state.memused - s;
1780 state.uioh#infochanged Memused;
1781 Hashtbl.remove state.tilemap k;
1783 loop (qpos+1)
1786 loop 0
1789 let flushtiles () =
1790 Queue.iter (fun (k, p, s) ->
1791 wcmd "freetile" [`s p];
1792 state.memused <- state.memused - s;
1793 state.uioh#infochanged Memused;
1794 Hashtbl.remove state.tilemap k;
1795 ) state.tilelru;
1796 Queue.clear state.tilelru;
1797 load state.layout;
1800 let logcurrently = function
1801 | Idle -> dolog "Idle"
1802 | Loading (l, gen) ->
1803 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1804 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1805 dolog
1806 "Tiling %d[%d,%d] page=%s cs=%s angle"
1807 l.pageno col row pageopaque
1808 (colorspace_to_string colorspace)
1810 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1811 angle gen conf.angle state.gen
1812 tilew tileh
1813 conf.tilew conf.tileh
1815 | Outlining _ ->
1816 dolog "outlining"
1819 let act cmds =
1820 (* dolog "%S" cmds; *)
1821 let op, args =
1822 let spacepos =
1823 try String.index cmds ' '
1824 with Not_found -> -1
1826 if spacepos = -1
1827 then cmds, ""
1828 else
1829 let l = String.length cmds in
1830 let op = String.sub cmds 0 spacepos in
1831 op, begin
1832 if l - spacepos < 2 then ""
1833 else String.sub cmds (spacepos+1) (l-spacepos-1)
1836 match op with
1837 | "clear" ->
1838 state.uioh#infochanged Pdim;
1839 state.pdims <- [];
1841 | "clearrects" ->
1842 state.rects <- state.rects1;
1843 G.postRedisplay "clearrects";
1845 | "continue" ->
1846 let n =
1847 try Scanf.sscanf args "%u" (fun n -> n)
1848 with exn ->
1849 dolog "error processing 'continue' %S: %s"
1850 cmds (Printexc.to_string exn);
1851 exit 1;
1853 state.pagecount <- n;
1854 state.invalidated <- state.invalidated - 1;
1855 begin match state.currently with
1856 | Outlining l ->
1857 state.currently <- Idle;
1858 state.outlines <- Array.of_list (List.rev l)
1859 | _ -> ()
1860 end;
1861 if state.invalidated = 0
1862 then represent ();
1863 if conf.maxwait = None
1864 then G.postRedisplay "continue";
1866 | "title" ->
1867 Wsi.settitle args
1869 | "msg" ->
1870 showtext ' ' args
1872 | "vmsg" ->
1873 if conf.verbose
1874 then showtext ' ' args
1876 | "progress" ->
1877 let progress, text =
1879 Scanf.sscanf args "%f %n"
1880 (fun f pos ->
1881 f, String.sub args pos (String.length args - pos))
1882 with exn ->
1883 dolog "error processing 'progress' %S: %s"
1884 cmds (Printexc.to_string exn);
1885 exit 1;
1887 state.text <- text;
1888 state.progress <- progress;
1889 G.postRedisplay "progress"
1891 | "firstmatch" ->
1892 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1894 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1895 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1896 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1897 with exn ->
1898 dolog "error processing 'firstmatch' %S: %s"
1899 cmds (Printexc.to_string exn);
1900 exit 1;
1902 let y = (getpagey pageno) + truncate y0 in
1903 addnav ();
1904 gotoy y;
1905 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1907 | "match" ->
1908 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1910 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1911 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1912 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1913 with exn ->
1914 dolog "error processing 'match' %S: %s"
1915 cmds (Printexc.to_string exn);
1916 exit 1;
1918 state.rects1 <-
1919 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1921 | "page" ->
1922 let pageopaque, t =
1924 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1925 with exn ->
1926 dolog "error processing 'page' %S: %s"
1927 cmds (Printexc.to_string exn);
1928 exit 1;
1930 begin match state.currently with
1931 | Loading (l, gen) ->
1932 vlog "page %d took %f sec" l.pageno t;
1933 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1934 begin match state.throttle with
1935 | None ->
1936 let preloadedpages =
1937 if conf.preload
1938 then preloadlayout state.layout
1939 else state.layout
1941 let evict () =
1942 let module IntSet =
1943 Set.Make (struct type t = int let compare = (-) end) in
1944 let set =
1945 List.fold_left (fun s l -> IntSet.add l.pageno s)
1946 IntSet.empty preloadedpages
1948 let evictedpages =
1949 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1950 if not (IntSet.mem pageno set)
1951 then (
1952 wcmd "freepage" [`s opaque];
1953 key :: accu
1955 else accu
1956 ) state.pagemap []
1958 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1960 evict ();
1961 state.currently <- Idle;
1962 if gen = state.gen
1963 then (
1964 tilepage l.pageno pageopaque state.layout;
1965 load state.layout;
1966 load preloadedpages;
1967 if pagevisible state.layout l.pageno
1968 && layoutready state.layout
1969 then G.postRedisplay "page";
1972 | Some (layout, _, _) ->
1973 state.currently <- Idle;
1974 tilepage l.pageno pageopaque layout;
1975 load state.layout
1976 end;
1978 | _ ->
1979 dolog "Inconsistent loading state";
1980 logcurrently state.currently;
1981 exit 1
1984 | "tile" ->
1985 let (x, y, opaque, size, t) =
1987 Scanf.sscanf args "%u %u %s %u %f"
1988 (fun x y p size t -> (x, y, p, size, t))
1989 with exn ->
1990 dolog "error processing 'tile' %S: %s"
1991 cmds (Printexc.to_string exn);
1992 exit 1;
1994 begin match state.currently with
1995 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1996 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1998 if tilew != conf.tilew || tileh != conf.tileh
1999 then (
2000 wcmd "freetile" [`s opaque];
2001 state.currently <- Idle;
2002 load state.layout;
2004 else (
2005 puttileopaque l col row gen cs angle opaque size t;
2006 state.memused <- state.memused + size;
2007 state.uioh#infochanged Memused;
2008 gctiles ();
2009 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2010 opaque, size) state.tilelru;
2012 let layout =
2013 match state.throttle with
2014 | None -> state.layout
2015 | Some (layout, _, _) -> layout
2018 state.currently <- Idle;
2019 if gen = state.gen
2020 && conf.colorspace = cs
2021 && conf.angle = angle
2022 && tilevisible layout l.pageno x y
2023 then conttiling l.pageno pageopaque;
2025 begin match state.throttle with
2026 | None ->
2027 preload state.layout;
2028 if gen = state.gen
2029 && conf.colorspace = cs
2030 && conf.angle = angle
2031 && tilevisible state.layout l.pageno x y
2032 then G.postRedisplay "tile nothrottle";
2034 | Some (layout, y, _) ->
2035 let ready = layoutready layout in
2036 if ready
2037 then (
2038 state.y <- y;
2039 state.layout <- layout;
2040 state.throttle <- None;
2041 G.postRedisplay "throttle";
2043 else load layout;
2044 end;
2047 | _ ->
2048 dolog "Inconsistent tiling state";
2049 logcurrently state.currently;
2050 exit 1
2053 | "pdim" ->
2054 let pdim =
2056 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2057 with exn ->
2058 dolog "error processing 'pdim' %S: %s"
2059 cmds (Printexc.to_string exn);
2060 exit 1;
2062 state.uioh#infochanged Pdim;
2063 state.pdims <- pdim :: state.pdims
2065 | "o" ->
2066 let (l, n, t, h, pos) =
2068 Scanf.sscanf args "%u %u %d %u %n"
2069 (fun l n t h pos -> l, n, t, h, pos)
2070 with exn ->
2071 dolog "error processing 'o' %S: %s"
2072 cmds (Printexc.to_string exn);
2073 exit 1;
2075 let s = String.sub args pos (String.length args - pos) in
2076 let outline = (s, l, (n, float t /. float h)) in
2077 begin match state.currently with
2078 | Outlining outlines ->
2079 state.currently <- Outlining (outline :: outlines)
2080 | Idle ->
2081 state.currently <- Outlining [outline]
2082 | currently ->
2083 dolog "invalid outlining state";
2084 logcurrently currently
2087 | "info" ->
2088 state.docinfo <- (1, args) :: state.docinfo
2090 | "infoend" ->
2091 state.uioh#infochanged Docinfo;
2092 state.docinfo <- List.rev state.docinfo
2094 | _ ->
2095 dolog "unknown cmd `%S'" cmds
2098 let onhist cb =
2099 let rc = cb.rc in
2100 let action = function
2101 | HCprev -> cbget cb ~-1
2102 | HCnext -> cbget cb 1
2103 | HCfirst -> cbget cb ~-(cb.rc)
2104 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2105 and cancel () = cb.rc <- rc
2106 in (action, cancel)
2109 let search pattern forward =
2110 if String.length pattern > 0
2111 then
2112 let pn, py =
2113 match state.layout with
2114 | [] -> 0, 0
2115 | l :: _ ->
2116 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2118 let cmd =
2119 let b = makecmd "search"
2120 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
2122 Buffer.add_char b ',';
2123 Buffer.add_string b pattern;
2124 Buffer.add_char b '\000';
2125 Buffer.contents b;
2127 writecmd state.sw cmd;
2130 let intentry text key =
2131 let c =
2132 if key >= 32 && key < 127
2133 then Char.chr key
2134 else '\000'
2136 match c with
2137 | '0' .. '9' ->
2138 let text = addchar text c in
2139 TEcont text
2141 | _ ->
2142 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2143 TEcont text
2146 let textentry text key =
2147 if key land 0xff00 = 0xff00
2148 then TEcont text
2149 else TEcont (text ^ Wsi.toutf8 key)
2152 let reqlayout angle proportional =
2153 match state.throttle with
2154 | None ->
2155 if state.invalidated = 0 then state.anchor <- getanchor ();
2156 conf.angle <- angle mod 360;
2157 conf.proportional <- proportional;
2158 invalidate ();
2159 wcmd "reqlayout" [`i conf.angle; `b proportional];
2160 | _ -> ()
2163 let settrim trimmargins trimfuzz =
2164 if state.invalidated = 0 then state.anchor <- getanchor ();
2165 conf.trimmargins <- trimmargins;
2166 conf.trimfuzz <- trimfuzz;
2167 let x0, y0, x1, y1 = trimfuzz in
2168 invalidate ();
2169 wcmd "settrim" [
2170 `b conf.trimmargins;
2171 `i x0;
2172 `i y0;
2173 `i x1;
2174 `i y1;
2176 Hashtbl.iter (fun _ opaque ->
2177 wcmd "freepage" [`s opaque];
2178 ) state.pagemap;
2179 Hashtbl.clear state.pagemap;
2182 let setzoom zoom =
2183 match state.throttle with
2184 | None ->
2185 let zoom = max 0.01 zoom in
2186 if zoom <> conf.zoom
2187 then (
2188 state.prevzoom <- conf.zoom;
2189 let relx =
2190 if zoom <= 1.0
2191 then (state.x <- 0; 0.0)
2192 else float state.x /. float state.w
2194 conf.zoom <- zoom;
2195 reshape conf.winw conf.winh;
2196 if zoom > 1.0
2197 then (
2198 let x = relx *. float state.w in
2199 state.x <- truncate x;
2201 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2204 | Some (layout, y, started) ->
2205 let time =
2206 match conf.maxwait with
2207 | None -> 0.0
2208 | Some t -> t
2210 let dt = now () -. started in
2211 if dt > time
2212 then (
2213 state.y <- y;
2214 load layout;
2218 let setcolumns columns coverA coverB =
2219 if columns < 2
2220 then (
2221 conf.columns <- None;
2222 state.x <- 0;
2223 setzoom 1.0;
2225 else (
2226 conf.columns <- Some ((columns, coverA, coverB), [||]);
2227 conf.zoom <- 1.0;
2229 reshape conf.winw conf.winh;
2232 let enterbirdseye () =
2233 let zoom = float conf.thumbw /. float conf.winw in
2234 let birdseyepageno =
2235 let cy = conf.winh / 2 in
2236 let fold = function
2237 | [] -> 0
2238 | l :: rest ->
2239 let rec fold best = function
2240 | [] -> best.pageno
2241 | l :: rest ->
2242 let d = cy - (l.pagedispy + l.pagevh/2)
2243 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2244 if abs d < abs dbest
2245 then fold l rest
2246 else best.pageno
2247 in fold l rest
2249 fold state.layout
2251 state.mode <- Birdseye (
2252 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2254 conf.zoom <- zoom;
2255 conf.presentation <- false;
2256 conf.interpagespace <- 10;
2257 conf.hlinks <- false;
2258 state.x <- 0;
2259 state.mstate <- Mnone;
2260 conf.maxwait <- None;
2261 conf.columns <- (
2262 match conf.beyecolumns with
2263 | Some c ->
2264 conf.zoom <- 1.0;
2265 Some ((c, 0, 0), [||])
2266 | None -> None
2268 Wsi.setcursor Wsi.CURSOR_INHERIT;
2269 if conf.verbose
2270 then
2271 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2272 (100.0*.zoom)
2273 else
2274 state.text <- ""
2276 reshape conf.winw conf.winh;
2279 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2280 state.mode <- View;
2281 conf.zoom <- c.zoom;
2282 conf.presentation <- c.presentation;
2283 conf.interpagespace <- c.interpagespace;
2284 conf.maxwait <- c.maxwait;
2285 conf.hlinks <- c.hlinks;
2286 conf.beyecolumns <- (
2287 match conf.columns with
2288 | Some ((c, _, _), _) -> Some c
2289 | None -> None
2291 conf.columns <- (
2292 match c.columns with
2293 | Some (c, _) -> Some (c, [||])
2294 | None -> None
2296 state.x <- leftx;
2297 if conf.verbose
2298 then
2299 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2300 (100.0*.conf.zoom)
2302 reshape conf.winw conf.winh;
2303 state.anchor <- if goback then anchor else (pageno, 0.0);
2306 let togglebirdseye () =
2307 match state.mode with
2308 | Birdseye vals -> leavebirdseye vals true
2309 | View -> enterbirdseye ()
2310 | _ -> ()
2313 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2314 let pageno = max 0 (pageno - incr) in
2315 let rec loop = function
2316 | [] -> gotopage1 pageno 0
2317 | l :: _ when l.pageno = pageno ->
2318 if l.pagedispy >= 0 && l.pagey = 0
2319 then G.postRedisplay "upbirdseye"
2320 else gotopage1 pageno 0
2321 | _ :: rest -> loop rest
2323 loop state.layout;
2324 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2327 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2328 let pageno = min (state.pagecount - 1) (pageno + incr) in
2329 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2330 let rec loop = function
2331 | [] ->
2332 let y, h = getpageyh pageno in
2333 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2334 gotoy (clamp dy)
2335 | l :: _ when l.pageno = pageno ->
2336 if l.pagevh != l.pageh
2337 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2338 else G.postRedisplay "downbirdseye"
2339 | _ :: rest -> loop rest
2341 loop state.layout
2344 let optentry mode _ key =
2345 let btos b = if b then "on" else "off" in
2346 if key >= 32 && key < 127
2347 then
2348 let c = Char.chr key in
2349 match c with
2350 | 's' ->
2351 let ondone s =
2352 try conf.scrollstep <- int_of_string s with exc ->
2353 state.text <- Printf.sprintf "bad integer `%s': %s"
2354 s (Printexc.to_string exc)
2356 TEswitch ("scroll step: ", "", None, intentry, ondone)
2358 | 'A' ->
2359 let ondone s =
2361 conf.autoscrollstep <- int_of_string s;
2362 if state.autoscroll <> None
2363 then state.autoscroll <- Some conf.autoscrollstep
2364 with exc ->
2365 state.text <- Printf.sprintf "bad integer `%s': %s"
2366 s (Printexc.to_string exc)
2368 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2370 | 'C' ->
2371 let ondone s =
2373 let n, a, b = columns_of_string s in
2374 setcolumns n a b;
2375 with exc ->
2376 state.text <- Printf.sprintf "bad columns `%s': %s"
2377 s (Printexc.to_string exc)
2379 TEswitch ("columns: ", "", None, textentry, ondone)
2381 | 'Z' ->
2382 let ondone s =
2384 let zoom = float (int_of_string s) /. 100.0 in
2385 setzoom zoom
2386 with exc ->
2387 state.text <- Printf.sprintf "bad integer `%s': %s"
2388 s (Printexc.to_string exc)
2390 TEswitch ("zoom: ", "", None, intentry, ondone)
2392 | 't' ->
2393 let ondone s =
2395 conf.thumbw <- bound (int_of_string s) 2 4096;
2396 state.text <-
2397 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2398 begin match mode with
2399 | Birdseye beye ->
2400 leavebirdseye beye false;
2401 enterbirdseye ();
2402 | _ -> ();
2404 with exc ->
2405 state.text <- Printf.sprintf "bad integer `%s': %s"
2406 s (Printexc.to_string exc)
2408 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2410 | 'R' ->
2411 let ondone s =
2412 match try
2413 Some (int_of_string s)
2414 with exc ->
2415 state.text <- Printf.sprintf "bad integer `%s': %s"
2416 s (Printexc.to_string exc);
2417 None
2418 with
2419 | Some angle -> reqlayout angle conf.proportional
2420 | None -> ()
2422 TEswitch ("rotation: ", "", None, intentry, ondone)
2424 | 'i' ->
2425 conf.icase <- not conf.icase;
2426 TEdone ("case insensitive search " ^ (btos conf.icase))
2428 | 'p' ->
2429 conf.preload <- not conf.preload;
2430 gotoy state.y;
2431 TEdone ("preload " ^ (btos conf.preload))
2433 | 'v' ->
2434 conf.verbose <- not conf.verbose;
2435 TEdone ("verbose " ^ (btos conf.verbose))
2437 | 'd' ->
2438 conf.debug <- not conf.debug;
2439 TEdone ("debug " ^ (btos conf.debug))
2441 | 'h' ->
2442 conf.maxhfit <- not conf.maxhfit;
2443 state.maxy <-
2444 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2445 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2447 | 'c' ->
2448 conf.crophack <- not conf.crophack;
2449 TEdone ("crophack " ^ btos conf.crophack)
2451 | 'a' ->
2452 let s =
2453 match conf.maxwait with
2454 | None ->
2455 conf.maxwait <- Some infinity;
2456 "always wait for page to complete"
2457 | Some _ ->
2458 conf.maxwait <- None;
2459 "show placeholder if page is not ready"
2461 TEdone s
2463 | 'f' ->
2464 conf.underinfo <- not conf.underinfo;
2465 TEdone ("underinfo " ^ btos conf.underinfo)
2467 | 'P' ->
2468 conf.savebmarks <- not conf.savebmarks;
2469 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2471 | 'S' ->
2472 let ondone s =
2474 let pageno, py =
2475 match state.layout with
2476 | [] -> 0, 0
2477 | l :: _ ->
2478 l.pageno, l.pagey
2480 conf.interpagespace <- int_of_string s;
2481 state.maxy <- calcheight ();
2482 let y = getpagey pageno in
2483 gotoy (y + py)
2484 with exc ->
2485 state.text <- Printf.sprintf "bad integer `%s': %s"
2486 s (Printexc.to_string exc)
2488 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2490 | 'l' ->
2491 reqlayout conf.angle (not conf.proportional);
2492 TEdone ("proportional display " ^ btos conf.proportional)
2494 | 'T' ->
2495 settrim (not conf.trimmargins) conf.trimfuzz;
2496 TEdone ("trim margins " ^ btos conf.trimmargins)
2498 | 'I' ->
2499 conf.invert <- not conf.invert;
2500 TEdone ("invert colors " ^ btos conf.invert)
2502 | 'x' ->
2503 let ondone s =
2504 cbput state.hists.sel s;
2505 conf.selcmd <- s;
2507 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2508 textentry, ondone)
2510 | _ ->
2511 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2512 TEstop
2513 else
2514 TEcont state.text
2517 class type lvsource = object
2518 method getitemcount : int
2519 method getitem : int -> (string * int)
2520 method hasaction : int -> bool
2521 method exit :
2522 uioh:uioh ->
2523 cancel:bool ->
2524 active:int ->
2525 first:int ->
2526 pan:int ->
2527 qsearch:string ->
2528 uioh option
2529 method getactive : int
2530 method getfirst : int
2531 method getqsearch : string
2532 method setqsearch : string -> unit
2533 method getpan : int
2534 end;;
2536 class virtual lvsourcebase = object
2537 val mutable m_active = 0
2538 val mutable m_first = 0
2539 val mutable m_qsearch = ""
2540 val mutable m_pan = 0
2541 method getactive = m_active
2542 method getfirst = m_first
2543 method getqsearch = m_qsearch
2544 method getpan = m_pan
2545 method setqsearch s = m_qsearch <- s
2546 end;;
2548 let withoutlastutf8 s =
2549 let len = String.length s in
2550 if len = 0
2551 then s
2552 else
2553 let rec find pos =
2554 if pos = 0
2555 then pos
2556 else
2557 let b = Char.code s.[pos] in
2558 if b land 0b110000 = 0b11000000
2559 then find (pos-1)
2560 else pos-1
2562 let first =
2563 if Char.code s.[len-1] land 0x80 = 0
2564 then len-1
2565 else find (len-1)
2567 String.sub s 0 first;
2570 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2571 let enttext te =
2572 state.mode <- Textentry (te, onleave);
2573 state.text <- "";
2574 enttext ();
2575 G.postRedisplay "textentrykeyboard enttext";
2577 let histaction cmd =
2578 match opthist with
2579 | None -> ()
2580 | Some (action, _) ->
2581 state.mode <- Textentry (
2582 (c, action cmd, opthist, onkey, ondone), onleave
2584 G.postRedisplay "textentry histaction"
2586 match key with
2587 | 0xff08 -> (* backspace *)
2588 let s = withoutlastutf8 text in
2589 let len = String.length s in
2590 if len = 0
2591 then (
2592 onleave Cancel;
2593 G.postRedisplay "textentrykeyboard after cancel";
2595 else (
2596 enttext (c, s, opthist, onkey, ondone)
2599 | 0xff0d ->
2600 ondone text;
2601 onleave Confirm;
2602 G.postRedisplay "textentrykeyboard after confirm"
2604 | 0xff52 -> histaction HCprev
2605 | 0xff54 -> histaction HCnext
2606 | 0xff50 -> histaction HCfirst
2607 | 0xff57 -> histaction HClast
2609 | 0xff1b -> (* escape*)
2610 if String.length text = 0
2611 then (
2612 begin match opthist with
2613 | None -> ()
2614 | Some (_, onhistcancel) -> onhistcancel ()
2615 end;
2616 onleave Cancel;
2617 state.text <- "";
2618 G.postRedisplay "textentrykeyboard after cancel2"
2620 else (
2621 enttext (c, "", opthist, onkey, ondone)
2624 | 0xff9f | 0xffff -> () (* delete *)
2626 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2627 begin match onkey text key with
2628 | TEdone text ->
2629 ondone text;
2630 onleave Confirm;
2631 G.postRedisplay "textentrykeyboard after confirm2";
2633 | TEcont text ->
2634 enttext (c, text, opthist, onkey, ondone);
2636 | TEstop ->
2637 onleave Cancel;
2638 G.postRedisplay "textentrykeyboard after cancel3"
2640 | TEswitch te ->
2641 state.mode <- Textentry (te, onleave);
2642 G.postRedisplay "textentrykeyboard switch";
2643 end;
2645 | _ ->
2646 vlog "unhandled key %#x" key
2649 let firstof first active =
2650 if first > active || abs (first - active) > fstate.maxrows - 1
2651 then max 0 (active - (fstate.maxrows/2))
2652 else first
2655 let calcfirst first active =
2656 if active > first
2657 then
2658 let rows = active - first in
2659 if rows > fstate.maxrows then active - fstate.maxrows else first
2660 else active
2663 let scrollph y maxy =
2664 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2665 let sh = float conf.winh /. sh in
2666 let sh = max sh (float conf.scrollh) in
2668 let percent =
2669 if y = state.maxy
2670 then 1.0
2671 else float y /. float maxy
2673 let position = (float conf.winh -. sh) *. percent in
2675 let position =
2676 if position +. sh > float conf.winh
2677 then float conf.winh -. sh
2678 else position
2680 position, sh;
2683 let coe s = (s :> uioh);;
2685 class listview ~(source:lvsource) ~trusted =
2686 object (self)
2687 val m_pan = source#getpan
2688 val m_first = source#getfirst
2689 val m_active = source#getactive
2690 val m_qsearch = source#getqsearch
2691 val m_prev_uioh = state.uioh
2693 method private elemunder y =
2694 let n = y / (fstate.fontsize+1) in
2695 if m_first + n < source#getitemcount
2696 then (
2697 if source#hasaction (m_first + n)
2698 then Some (m_first + n)
2699 else None
2701 else None
2703 method display =
2704 Gl.enable `blend;
2705 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2706 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2707 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2708 GlDraw.color (1., 1., 1.);
2709 Gl.enable `texture_2d;
2710 let fs = fstate.fontsize in
2711 let nfs = fs + 1 in
2712 let ww = fstate.wwidth in
2713 let tabw = 30.0*.ww in
2714 let itemcount = source#getitemcount in
2715 let rec loop row =
2716 if (row - m_first) * nfs > conf.winh
2717 then ()
2718 else (
2719 if row >= 0 && row < itemcount
2720 then (
2721 let (s, level) = source#getitem row in
2722 let y = (row - m_first) * nfs in
2723 let x = 5.0 +. float (level + m_pan) *. ww in
2724 if row = m_active
2725 then (
2726 Gl.disable `texture_2d;
2727 GlDraw.polygon_mode `both `line;
2728 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2729 GlDraw.rect (1., float (y + 1))
2730 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2731 GlDraw.polygon_mode `both `fill;
2732 GlDraw.color (1., 1., 1.);
2733 Gl.enable `texture_2d;
2736 let drawtabularstring s =
2737 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2738 if trusted
2739 then
2740 let tabpos = try String.index s '\t' with Not_found -> -1 in
2741 if tabpos > 0
2742 then
2743 let len = String.length s - tabpos - 1 in
2744 let s1 = String.sub s 0 tabpos
2745 and s2 = String.sub s (tabpos + 1) len in
2746 let nx = drawstr x s1 in
2747 let sw = nx -. x in
2748 let x = x +. (max tabw sw) in
2749 drawstr x s2
2750 else
2751 drawstr x s
2752 else
2753 drawstr x s
2755 let _ = drawtabularstring s in
2756 loop (row+1)
2760 loop m_first;
2761 Gl.disable `blend;
2762 Gl.disable `texture_2d;
2764 method updownlevel incr =
2765 let len = source#getitemcount in
2766 let curlevel =
2767 if m_active >= 0 && m_active < len
2768 then snd (source#getitem m_active)
2769 else -1
2771 let rec flow i =
2772 if i = len then i-1 else if i = -1 then 0 else
2773 let _, l = source#getitem i in
2774 if l != curlevel then i else flow (i+incr)
2776 let active = flow m_active in
2777 let first = calcfirst m_first active in
2778 G.postRedisplay "outline updownlevel";
2779 {< m_active = active; m_first = first >}
2781 method private key1 key mask =
2782 let set1 active first qsearch =
2783 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2785 let search active pattern incr =
2786 let dosearch re =
2787 let rec loop n =
2788 if n >= 0 && n < source#getitemcount
2789 then (
2790 let s, _ = source#getitem n in
2792 (try ignore (Str.search_forward re s 0); true
2793 with Not_found -> false)
2794 then Some n
2795 else loop (n + incr)
2797 else None
2799 loop active
2802 let re = Str.regexp_case_fold pattern in
2803 dosearch re
2804 with Failure s ->
2805 state.text <- s;
2806 None
2808 let itemcount = source#getitemcount in
2809 let find start incr =
2810 let rec find i =
2811 if i = -1 || i = itemcount
2812 then -1
2813 else (
2814 if source#hasaction i
2815 then i
2816 else find (i + incr)
2819 find start
2821 let set active first =
2822 let first = bound first 0 (itemcount - fstate.maxrows) in
2823 state.text <- "";
2824 coe {< m_active = active; m_first = first >}
2826 let navigate incr =
2827 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2828 let active, first =
2829 let incr1 = if incr > 0 then 1 else -1 in
2830 if isvisible m_first m_active
2831 then
2832 let next =
2833 let next = m_active + incr in
2834 let next =
2835 if next < 0 || next >= itemcount
2836 then -1
2837 else find next incr1
2839 if next = -1 || abs (m_active - next) > fstate.maxrows
2840 then -1
2841 else next
2843 if next = -1
2844 then
2845 let first = m_first + incr in
2846 let first = bound first 0 (itemcount - 1) in
2847 let next =
2848 let next = m_active + incr in
2849 let next = bound next 0 (itemcount - 1) in
2850 find next ~-incr1
2852 let active = if next = -1 then m_active else next in
2853 active, first
2854 else
2855 let first = min next m_first in
2856 let first =
2857 if abs (next - first) > fstate.maxrows
2858 then first + incr
2859 else first
2861 next, first
2862 else
2863 let first = m_first + incr in
2864 let first = bound first 0 (itemcount - 1) in
2865 let active =
2866 let next = m_active + incr in
2867 let next = bound next 0 (itemcount - 1) in
2868 let next = find next incr1 in
2869 let active =
2870 if next = -1 || abs (m_active - first) > fstate.maxrows
2871 then (
2872 let active = if m_active = -1 then next else m_active in
2873 active
2875 else next
2877 if isvisible first active
2878 then active
2879 else -1
2881 active, first
2883 G.postRedisplay "listview navigate";
2884 set active first;
2886 match key with
2887 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2888 let incr = if key = 0x72 then -1 else 1 in
2889 let active, first =
2890 match search (m_active + incr) m_qsearch incr with
2891 | None ->
2892 state.text <- m_qsearch ^ " [not found]";
2893 m_active, m_first
2894 | Some active ->
2895 state.text <- m_qsearch;
2896 active, firstof m_first active
2898 G.postRedisplay "listview ctrl-r/s";
2899 set1 active first m_qsearch;
2901 | 0xff08 -> (* backspace *)
2902 if String.length m_qsearch = 0
2903 then coe self
2904 else (
2905 let qsearch = withoutlastutf8 m_qsearch in
2906 let len = String.length qsearch in
2907 if len = 0
2908 then (
2909 state.text <- "";
2910 G.postRedisplay "listview empty qsearch";
2911 set1 m_active m_first "";
2913 else
2914 let active, first =
2915 match search m_active qsearch ~-1 with
2916 | None ->
2917 state.text <- qsearch ^ " [not found]";
2918 m_active, m_first
2919 | Some active ->
2920 state.text <- qsearch;
2921 active, firstof m_first active
2923 G.postRedisplay "listview backspace qsearch";
2924 set1 active first qsearch
2927 | key when ((key >= 32 && key < 127)
2928 || (key != 0 && key land 0xff00 != 0xff00)) ->
2929 let pattern =
2930 if key >= 32 && key < 127
2931 then addchar m_qsearch (Char.chr key)
2932 else m_qsearch ^ Wsi.toutf8 key
2934 let active, first =
2935 match search m_active pattern 1 with
2936 | None ->
2937 state.text <- pattern ^ " [not found]";
2938 m_active, m_first
2939 | Some active ->
2940 state.text <- pattern;
2941 active, firstof m_first active
2943 G.postRedisplay "listview qsearch add";
2944 set1 active first pattern;
2946 | 0xff1b -> (* escape *)
2947 state.text <- "";
2948 if String.length m_qsearch = 0
2949 then (
2950 G.postRedisplay "list view escape";
2951 begin
2952 match
2953 source#exit (coe self) true m_active m_first m_pan m_qsearch
2954 with
2955 | None -> m_prev_uioh
2956 | Some uioh -> uioh
2959 else (
2960 G.postRedisplay "list view kill qsearch";
2961 source#setqsearch "";
2962 coe {< m_qsearch = "" >}
2965 | 0xff0d -> (* return *)
2966 state.text <- "";
2967 let self = {< m_qsearch = "" >} in
2968 source#setqsearch "";
2969 let opt =
2970 G.postRedisplay "listview enter";
2971 if m_active >= 0 && m_active < source#getitemcount
2972 then (
2973 source#exit (coe self) false m_active m_first m_pan "";
2975 else (
2976 source#exit (coe self) true m_active m_first m_pan "";
2979 begin match opt with
2980 | None -> m_prev_uioh
2981 | Some uioh -> uioh
2984 | 0xff9f | 0xffff -> (* delete *)
2985 coe self
2987 | 0xff52 -> navigate ~-1 (* up *)
2988 | 0xff54 -> navigate 1 (* down *)
2989 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
2990 | 0xff56 -> navigate fstate.maxrows (* next *)
2992 | 0xff53 -> (* right *)
2993 state.text <- "";
2994 G.postRedisplay "listview right";
2995 coe {< m_pan = m_pan - 1 >}
2997 | 0xff51 -> (* left *)
2998 state.text <- "";
2999 G.postRedisplay "listview left";
3000 coe {< m_pan = m_pan + 1 >}
3002 | 0xff50 -> (* home *)
3003 let active = find 0 1 in
3004 G.postRedisplay "listview home";
3005 set active 0;
3007 | 0xff57 -> (* end *)
3008 let first = max 0 (itemcount - fstate.maxrows) in
3009 let active = find (itemcount - 1) ~-1 in
3010 G.postRedisplay "listview end";
3011 set active first;
3013 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3014 coe self
3016 | _ ->
3017 dolog "listview unknown key %#x" key; coe self
3019 method key key mask =
3020 match state.mode with
3021 | Textentry te -> textentrykeyboard key mask te; coe self
3022 | _ -> self#key1 key mask
3024 method button button down x y _ =
3025 let opt =
3026 match button with
3027 | 1 when x > conf.winw - conf.scrollbw ->
3028 G.postRedisplay "listview scroll";
3029 if down
3030 then
3031 let _, position, sh = self#scrollph in
3032 if y > truncate position && y < truncate (position +. sh)
3033 then (
3034 state.mstate <- Mscrolly;
3035 Some (coe self)
3037 else
3038 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3039 let first = truncate (s *. float source#getitemcount) in
3040 let first = min source#getitemcount first in
3041 Some (coe {< m_first = first; m_active = first >})
3042 else (
3043 state.mstate <- Mnone;
3044 Some (coe self);
3046 | 1 when not down ->
3047 begin match self#elemunder y with
3048 | Some n ->
3049 G.postRedisplay "listview click";
3050 source#exit
3051 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3052 | _ ->
3053 Some (coe self)
3055 | n when (n == 4 || n == 5) && not down ->
3056 let len = source#getitemcount in
3057 let first =
3058 if n = 5 && m_first + fstate.maxrows >= len
3059 then
3060 m_first
3061 else
3062 let first = m_first + (if n == 4 then -1 else 1) in
3063 bound first 0 (len - 1)
3065 G.postRedisplay "listview wheel";
3066 Some (coe {< m_first = first >})
3067 | _ ->
3068 Some (coe self)
3070 match opt with
3071 | None -> m_prev_uioh
3072 | Some uioh -> uioh
3074 method motion _ y =
3075 match state.mstate with
3076 | Mscrolly ->
3077 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3078 let first = truncate (s *. float source#getitemcount) in
3079 let first = min source#getitemcount first in
3080 G.postRedisplay "listview motion";
3081 coe {< m_first = first; m_active = first >}
3082 | _ -> coe self
3084 method pmotion x y =
3085 if x < conf.winw - conf.scrollbw
3086 then
3087 let n =
3088 match self#elemunder y with
3089 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3090 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3092 let o =
3093 if n != m_active
3094 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3095 else self
3097 coe o
3098 else (
3099 Wsi.setcursor Wsi.CURSOR_INHERIT;
3100 coe self
3103 method infochanged _ = ()
3105 method scrollpw = (0, 0.0, 0.0)
3106 method scrollph =
3107 let nfs = fstate.fontsize + 1 in
3108 let y = m_first * nfs in
3109 let itemcount = source#getitemcount in
3110 let maxi = max 0 (itemcount - fstate.maxrows) in
3111 let maxy = maxi * nfs in
3112 let p, h = scrollph y maxy in
3113 conf.scrollbw, p, h
3114 end;;
3116 class outlinelistview ~source =
3117 object (self)
3118 inherit listview ~source:(source :> lvsource) ~trusted:false as super
3120 method key key mask =
3121 let calcfirst first active =
3122 if active > first
3123 then
3124 let rows = active - first in
3125 if rows > fstate.maxrows then active - fstate.maxrows else first
3126 else active
3128 let navigate incr =
3129 let active = m_active + incr in
3130 let active = bound active 0 (source#getitemcount - 1) in
3131 let first = calcfirst m_first active in
3132 G.postRedisplay "outline navigate";
3133 coe {< m_active = active; m_first = first >}
3135 let ctrl = Wsi.withctrl mask in
3136 match key with
3137 | 110 when ctrl -> (* ctrl-n *)
3138 source#narrow m_qsearch;
3139 G.postRedisplay "outline ctrl-n";
3140 coe {< m_first = 0; m_active = 0 >}
3142 | 117 when ctrl -> (* ctrl-u *)
3143 source#denarrow;
3144 G.postRedisplay "outline ctrl-u";
3145 state.text <- "";
3146 coe {< m_first = 0; m_active = 0 >}
3148 | 108 when ctrl -> (* ctrl-l *)
3149 let first = m_active - (fstate.maxrows / 2) in
3150 G.postRedisplay "outline ctrl-l";
3151 coe {< m_first = first >}
3153 | 0xff9f | 0xffff -> (* delete *)
3154 source#remove m_active;
3155 G.postRedisplay "outline delete";
3156 let active = max 0 (m_active-1) in
3157 coe {< m_first = firstof m_first active;
3158 m_active = active >}
3160 | 0xff52 -> navigate ~-1 (* up *)
3161 | 0xff54 -> navigate 1 (* down *)
3162 | 0xff55 -> (* prior *)
3163 navigate ~-(fstate.maxrows)
3164 | 0xff56 -> (* next *)
3165 navigate fstate.maxrows
3167 | 0xff53 -> (* [ctrl-]right *)
3168 let o =
3169 if ctrl
3170 then (
3171 G.postRedisplay "outline ctrl right";
3172 {< m_pan = m_pan + 1 >}
3174 else self#updownlevel 1
3176 coe o
3178 | 0xff51 -> (* [ctrl-]left *)
3179 let o =
3180 if ctrl
3181 then (
3182 G.postRedisplay "outline ctrl left";
3183 {< m_pan = m_pan - 1 >}
3185 else self#updownlevel ~-1
3187 coe o
3189 | 0xff50 -> (* home *)
3190 G.postRedisplay "outline home";
3191 coe {< m_first = 0; m_active = 0 >}
3193 | 0xff57 -> (* end *)
3194 let active = source#getitemcount - 1 in
3195 let first = max 0 (active - fstate.maxrows) in
3196 G.postRedisplay "outline end";
3197 coe {< m_active = active; m_first = first >}
3199 | _ -> super#key key mask
3202 let outlinesource usebookmarks =
3203 let empty = [||] in
3204 (object
3205 inherit lvsourcebase
3206 val mutable m_items = empty
3207 val mutable m_orig_items = empty
3208 val mutable m_prev_items = empty
3209 val mutable m_narrow_pattern = ""
3210 val mutable m_hadremovals = false
3212 method getitemcount =
3213 Array.length m_items + (if m_hadremovals then 1 else 0)
3215 method getitem n =
3216 if n == Array.length m_items && m_hadremovals
3217 then
3218 ("[Confirm removal]", 0)
3219 else
3220 let s, n, _ = m_items.(n) in
3221 (s, n)
3223 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3224 ignore (uioh, first, qsearch);
3225 let confrimremoval = m_hadremovals && active = Array.length m_items in
3226 let items =
3227 if String.length m_narrow_pattern = 0
3228 then m_orig_items
3229 else m_items
3231 if not cancel
3232 then (
3233 if not confrimremoval
3234 then(
3235 let _, _, anchor = m_items.(active) in
3236 gotoanchor anchor;
3237 m_items <- items;
3239 else (
3240 state.bookmarks <- Array.to_list m_items;
3241 m_orig_items <- m_items;
3244 else m_items <- items;
3245 m_pan <- pan;
3246 None
3248 method hasaction _ = true
3250 method greetmsg =
3251 if Array.length m_items != Array.length m_orig_items
3252 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3253 else ""
3255 method narrow pattern =
3256 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3257 match reopt with
3258 | None -> ()
3259 | Some re ->
3260 let rec loop accu n =
3261 if n = -1
3262 then (
3263 m_narrow_pattern <- pattern;
3264 m_items <- Array.of_list accu
3266 else
3267 let (s, _, _) as o = m_items.(n) in
3268 let accu =
3269 if (try ignore (Str.search_forward re s 0); true
3270 with Not_found -> false)
3271 then o :: accu
3272 else accu
3274 loop accu (n-1)
3276 loop [] (Array.length m_items - 1)
3278 method denarrow =
3279 m_orig_items <- (
3280 if usebookmarks
3281 then Array.of_list state.bookmarks
3282 else state.outlines
3284 m_items <- m_orig_items
3286 method remove m =
3287 if usebookmarks
3288 then
3289 if m >= 0 && m < Array.length m_items
3290 then (
3291 m_hadremovals <- true;
3292 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3293 let n = if n >= m then n+1 else n in
3294 m_items.(n)
3298 method reset anchor items =
3299 m_hadremovals <- false;
3300 if m_orig_items == empty || m_prev_items != items
3301 then (
3302 m_orig_items <- items;
3303 if String.length m_narrow_pattern = 0
3304 then m_items <- items;
3306 m_prev_items <- items;
3307 let rely = getanchory anchor in
3308 let active =
3309 let rec loop n best bestd =
3310 if n = Array.length m_items
3311 then best
3312 else
3313 let (_, _, anchor) = m_items.(n) in
3314 let orely = getanchory anchor in
3315 let d = abs (orely - rely) in
3316 if d < bestd
3317 then loop (n+1) n d
3318 else loop (n+1) best bestd
3320 loop 0 ~-1 max_int
3322 m_active <- active;
3323 m_first <- firstof m_first active
3324 end)
3327 let enterselector usebookmarks =
3328 let source = outlinesource usebookmarks in
3329 fun errmsg ->
3330 let outlines =
3331 if usebookmarks
3332 then Array.of_list state.bookmarks
3333 else state.outlines
3335 if Array.length outlines = 0
3336 then (
3337 showtext ' ' errmsg;
3339 else (
3340 state.text <- source#greetmsg;
3341 Wsi.setcursor Wsi.CURSOR_INHERIT;
3342 let anchor = getanchor () in
3343 source#reset anchor outlines;
3344 state.uioh <- coe (new outlinelistview ~source);
3345 G.postRedisplay "enter selector";
3349 let enteroutlinemode =
3350 let f = enterselector false in
3351 fun ()-> f "Document has no outline";
3354 let enterbookmarkmode =
3355 let f = enterselector true in
3356 fun () -> f "Document has no bookmarks (yet)";
3359 let color_of_string s =
3360 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3361 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3365 let color_to_string (r, g, b) =
3366 let r = truncate (r *. 256.0)
3367 and g = truncate (g *. 256.0)
3368 and b = truncate (b *. 256.0) in
3369 Printf.sprintf "%d/%d/%d" r g b
3372 let irect_of_string s =
3373 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3376 let irect_to_string (x0,y0,x1,y1) =
3377 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3380 let makecheckers () =
3381 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3382 following to say:
3383 converted by Issac Trotts. July 25, 2002 *)
3384 let image_height = 64
3385 and image_width = 64 in
3387 let make_image () =
3388 let image =
3389 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3391 for i = 0 to image_width - 1 do
3392 for j = 0 to image_height - 1 do
3393 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3394 (if (i land 8 ) lxor (j land 8) = 0
3395 then [|255;255;255|] else [|200;200;200|])
3396 done
3397 done;
3398 image
3400 let image = make_image () in
3401 let id = GlTex.gen_texture () in
3402 GlTex.bind_texture `texture_2d id;
3403 GlPix.store (`unpack_alignment 1);
3404 GlTex.image2d image;
3405 List.iter (GlTex.parameter ~target:`texture_2d)
3406 [ `wrap_s `repeat;
3407 `wrap_t `repeat;
3408 `mag_filter `nearest;
3409 `min_filter `nearest ];
3413 let setcheckers enabled =
3414 match state.texid with
3415 | None ->
3416 if enabled then state.texid <- Some (makecheckers ())
3418 | Some texid ->
3419 if not enabled
3420 then (
3421 GlTex.delete_texture texid;
3422 state.texid <- None;
3426 let int_of_string_with_suffix s =
3427 let l = String.length s in
3428 let s1, shift =
3429 if l > 1
3430 then
3431 let suffix = Char.lowercase s.[l-1] in
3432 match suffix with
3433 | 'k' -> String.sub s 0 (l-1), 10
3434 | 'm' -> String.sub s 0 (l-1), 20
3435 | 'g' -> String.sub s 0 (l-1), 30
3436 | _ -> s, 0
3437 else s, 0
3439 let n = int_of_string s1 in
3440 let m = n lsl shift in
3441 if m < 0 || m < n
3442 then raise (Failure "value too large")
3443 else m
3446 let string_with_suffix_of_int n =
3447 if n = 0
3448 then "0"
3449 else
3450 let n, s =
3451 if n = 0
3452 then 0, ""
3453 else (
3454 if n land ((1 lsl 20) - 1) = 0
3455 then n lsr 20, "M"
3456 else (
3457 if n land ((1 lsl 10) - 1) = 0
3458 then n lsr 10, "K"
3459 else n, ""
3463 let rec loop s n =
3464 let h = n mod 1000 in
3465 let n = n / 1000 in
3466 if n = 0
3467 then string_of_int h ^ s
3468 else (
3469 let s = Printf.sprintf "_%03d%s" h s in
3470 loop s n
3473 loop "" n ^ s;
3476 let defghyllscroll = (40, 8, 32);;
3477 let ghyllscroll_of_string s =
3478 let (n, a, b) as nab =
3479 if s = "default"
3480 then defghyllscroll
3481 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3483 if n <= a || n <= b || a >= b
3484 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3485 nab;
3488 let ghyllscroll_to_string ((n, a, b) as nab) =
3489 if nab = defghyllscroll
3490 then "default"
3491 else Printf.sprintf "%d,%d,%d" n a b;
3494 let describe_location () =
3495 let f (fn, _) l =
3496 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3498 let fn, ln = List.fold_left f (-1, -1) state.layout in
3499 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3500 let percent =
3501 if maxy <= 0
3502 then 100.
3503 else (100. *. (float state.y /. float maxy))
3505 if fn = ln
3506 then
3507 Printf.sprintf "page %d of %d [%.2f%%]"
3508 (fn+1) state.pagecount percent
3509 else
3510 Printf.sprintf
3511 "pages %d-%d of %d [%.2f%%]"
3512 (fn+1) (ln+1) state.pagecount percent
3515 let enterinfomode =
3516 let btos b = if b then "\xe2\x88\x9a" else "" in
3517 let showextended = ref false in
3518 let leave mode = function
3519 | Confirm -> state.mode <- mode
3520 | Cancel -> state.mode <- mode in
3521 let src =
3522 (object
3523 val mutable m_first_time = true
3524 val mutable m_l = []
3525 val mutable m_a = [||]
3526 val mutable m_prev_uioh = nouioh
3527 val mutable m_prev_mode = View
3529 inherit lvsourcebase
3531 method reset prev_mode prev_uioh =
3532 m_a <- Array.of_list (List.rev m_l);
3533 m_l <- [];
3534 m_prev_mode <- prev_mode;
3535 m_prev_uioh <- prev_uioh;
3536 if m_first_time
3537 then (
3538 let rec loop n =
3539 if n >= Array.length m_a
3540 then ()
3541 else
3542 match m_a.(n) with
3543 | _, _, _, Action _ -> m_active <- n
3544 | _ -> loop (n+1)
3546 loop 0;
3547 m_first_time <- false;
3550 method int name get set =
3551 m_l <-
3552 (name, `int get, 1, Action (
3553 fun u ->
3554 let ondone s =
3555 try set (int_of_string s)
3556 with exn ->
3557 state.text <- Printf.sprintf "bad integer `%s': %s"
3558 s (Printexc.to_string exn)
3560 state.text <- "";
3561 let te = name ^ ": ", "", None, intentry, ondone in
3562 state.mode <- Textentry (te, leave m_prev_mode);
3564 )) :: m_l
3566 method int_with_suffix name get set =
3567 m_l <-
3568 (name, `intws get, 1, Action (
3569 fun u ->
3570 let ondone s =
3571 try set (int_of_string_with_suffix s)
3572 with exn ->
3573 state.text <- Printf.sprintf "bad integer `%s': %s"
3574 s (Printexc.to_string exn)
3576 state.text <- "";
3577 let te =
3578 name ^ ": ", "", None, intentry_with_suffix, ondone
3580 state.mode <- Textentry (te, leave m_prev_mode);
3582 )) :: m_l
3584 method bool ?(offset=1) ?(btos=btos) name get set =
3585 m_l <-
3586 (name, `bool (btos, get), offset, Action (
3587 fun u ->
3588 let v = get () in
3589 set (not v);
3591 )) :: m_l
3593 method color name get set =
3594 m_l <-
3595 (name, `color get, 1, Action (
3596 fun u ->
3597 let invalid = (nan, nan, nan) in
3598 let ondone s =
3599 let c =
3600 try color_of_string s
3601 with exn ->
3602 state.text <- Printf.sprintf "bad color `%s': %s"
3603 s (Printexc.to_string exn);
3604 invalid
3606 if c <> invalid
3607 then set c;
3609 let te = name ^ ": ", "", None, textentry, ondone in
3610 state.text <- color_to_string (get ());
3611 state.mode <- Textentry (te, leave m_prev_mode);
3613 )) :: m_l
3615 method string name get set =
3616 m_l <-
3617 (name, `string get, 1, Action (
3618 fun u ->
3619 let ondone s = set s in
3620 let te = name ^ ": ", "", None, textentry, ondone in
3621 state.mode <- Textentry (te, leave m_prev_mode);
3623 )) :: m_l
3625 method colorspace name get set =
3626 m_l <-
3627 (name, `string get, 1, Action (
3628 fun _ ->
3629 let source =
3630 let vals = [| "rgb"; "bgr"; "gray" |] in
3631 (object
3632 inherit lvsourcebase
3634 initializer
3635 m_active <- int_of_colorspace conf.colorspace;
3636 m_first <- 0;
3638 method getitemcount = Array.length vals
3639 method getitem n = (vals.(n), 0)
3640 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3641 ignore (uioh, first, pan, qsearch);
3642 if not cancel then set active;
3643 None
3644 method hasaction _ = true
3645 end)
3647 state.text <- "";
3648 coe (new listview ~source ~trusted:true)
3649 )) :: m_l
3651 method caption s offset =
3652 m_l <- (s, `empty, offset, Noaction) :: m_l
3654 method caption2 s f offset =
3655 m_l <- (s, `string f, offset, Noaction) :: m_l
3657 method getitemcount = Array.length m_a
3659 method getitem n =
3660 let tostr = function
3661 | `int f -> string_of_int (f ())
3662 | `intws f -> string_with_suffix_of_int (f ())
3663 | `string f -> f ()
3664 | `color f -> color_to_string (f ())
3665 | `bool (btos, f) -> btos (f ())
3666 | `empty -> ""
3668 let name, t, offset, _ = m_a.(n) in
3669 ((let s = tostr t in
3670 if String.length s > 0
3671 then Printf.sprintf "%s\t%s" name s
3672 else name),
3673 offset)
3675 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3676 let uiohopt =
3677 if not cancel
3678 then (
3679 m_qsearch <- qsearch;
3680 let uioh =
3681 match m_a.(active) with
3682 | _, _, _, Action f -> f uioh
3683 | _ -> uioh
3685 Some uioh
3687 else None
3689 m_active <- active;
3690 m_first <- first;
3691 m_pan <- pan;
3692 uiohopt
3694 method hasaction n =
3695 match m_a.(n) with
3696 | _, _, _, Action _ -> true
3697 | _ -> false
3698 end)
3700 let rec fillsrc prevmode prevuioh =
3701 let sep () = src#caption "" 0 in
3702 let colorp name get set =
3703 src#string name
3704 (fun () -> color_to_string (get ()))
3705 (fun v ->
3707 let c = color_of_string v in
3708 set c
3709 with exn ->
3710 state.text <- Printf.sprintf "bad color `%s': %s"
3711 v (Printexc.to_string exn);
3714 let oldmode = state.mode in
3715 let birdseye = isbirdseye state.mode in
3717 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3719 src#bool "presentation mode"
3720 (fun () -> conf.presentation)
3721 (fun v ->
3722 conf.presentation <- v;
3723 state.anchor <- getanchor ();
3724 represent ());
3726 src#bool "ignore case in searches"
3727 (fun () -> conf.icase)
3728 (fun v -> conf.icase <- v);
3730 src#bool "preload"
3731 (fun () -> conf.preload)
3732 (fun v -> conf.preload <- v);
3734 src#bool "highlight links"
3735 (fun () -> conf.hlinks)
3736 (fun v -> conf.hlinks <- v);
3738 src#bool "under info"
3739 (fun () -> conf.underinfo)
3740 (fun v -> conf.underinfo <- v);
3742 src#bool "persistent bookmarks"
3743 (fun () -> conf.savebmarks)
3744 (fun v -> conf.savebmarks <- v);
3746 src#bool "proportional display"
3747 (fun () -> conf.proportional)
3748 (fun v -> reqlayout conf.angle v);
3750 src#bool "trim margins"
3751 (fun () -> conf.trimmargins)
3752 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3754 src#bool "persistent location"
3755 (fun () -> conf.jumpback)
3756 (fun v -> conf.jumpback <- v);
3758 sep ();
3759 src#int "inter-page space"
3760 (fun () -> conf.interpagespace)
3761 (fun n ->
3762 conf.interpagespace <- n;
3763 let pageno, py =
3764 match state.layout with
3765 | [] -> 0, 0
3766 | l :: _ ->
3767 l.pageno, l.pagey
3769 state.maxy <- calcheight ();
3770 let y = getpagey pageno in
3771 gotoy (y + py)
3774 src#int "page bias"
3775 (fun () -> conf.pagebias)
3776 (fun v -> conf.pagebias <- v);
3778 src#int "scroll step"
3779 (fun () -> conf.scrollstep)
3780 (fun n -> conf.scrollstep <- n);
3782 src#int "auto scroll step"
3783 (fun () ->
3784 match state.autoscroll with
3785 | Some step -> step
3786 | _ -> conf.autoscrollstep)
3787 (fun n ->
3788 if state.autoscroll <> None
3789 then state.autoscroll <- Some n;
3790 conf.autoscrollstep <- n);
3792 src#int "zoom"
3793 (fun () -> truncate (conf.zoom *. 100.))
3794 (fun v -> setzoom ((float v) /. 100.));
3796 src#int "rotation"
3797 (fun () -> conf.angle)
3798 (fun v -> reqlayout v conf.proportional);
3800 src#int "scroll bar width"
3801 (fun () -> state.scrollw)
3802 (fun v ->
3803 state.scrollw <- v;
3804 conf.scrollbw <- v;
3805 reshape conf.winw conf.winh;
3808 src#int "scroll handle height"
3809 (fun () -> conf.scrollh)
3810 (fun v -> conf.scrollh <- v;);
3812 src#int "thumbnail width"
3813 (fun () -> conf.thumbw)
3814 (fun v ->
3815 conf.thumbw <- min 4096 v;
3816 match oldmode with
3817 | Birdseye beye ->
3818 leavebirdseye beye false;
3819 enterbirdseye ()
3820 | _ -> ()
3823 src#string "columns"
3824 (fun () ->
3825 match conf.columns with
3826 | None -> "1"
3827 | Some (multicol, _) -> columns_to_string multicol)
3828 (fun v ->
3829 let n, a, b = columns_of_string v in
3830 setcolumns n a b);
3832 sep ();
3833 src#caption "Presentation mode" 0;
3834 src#bool "scrollbar visible"
3835 (fun () -> conf.scrollbarinpm)
3836 (fun v ->
3837 if v != conf.scrollbarinpm
3838 then (
3839 conf.scrollbarinpm <- v;
3840 if conf.presentation
3841 then (
3842 state.scrollw <- if v then conf.scrollbw else 0;
3843 reshape conf.winw conf.winh;
3848 sep ();
3849 src#caption "Pixmap cache" 0;
3850 src#int_with_suffix "size (advisory)"
3851 (fun () -> conf.memlimit)
3852 (fun v -> conf.memlimit <- v);
3854 src#caption2 "used"
3855 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3856 (string_with_suffix_of_int state.memused)
3857 (Hashtbl.length state.tilemap)) 1;
3859 sep ();
3860 src#caption "Layout" 0;
3861 src#caption2 "Dimension"
3862 (fun () ->
3863 Printf.sprintf "%dx%d (virtual %dx%d)"
3864 conf.winw conf.winh
3865 state.w state.maxy)
3867 if conf.debug
3868 then
3869 src#caption2 "Position" (fun () ->
3870 Printf.sprintf "%dx%d" state.x state.y
3872 else
3873 src#caption2 "Visible" (fun () -> describe_location ()) 1
3876 sep ();
3877 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3878 "Save these parameters as global defaults at exit"
3879 (fun () -> conf.bedefault)
3880 (fun v -> conf.bedefault <- v)
3883 sep ();
3884 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3885 src#bool ~offset:0 ~btos "Extended parameters"
3886 (fun () -> !showextended)
3887 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3888 if !showextended
3889 then (
3890 src#bool "checkers"
3891 (fun () -> conf.checkers)
3892 (fun v -> conf.checkers <- v; setcheckers v);
3893 src#bool "update cursor"
3894 (fun () -> conf.updatecurs)
3895 (fun v -> conf.updatecurs <- v);
3896 src#bool "verbose"
3897 (fun () -> conf.verbose)
3898 (fun v -> conf.verbose <- v);
3899 src#bool "invert colors"
3900 (fun () -> conf.invert)
3901 (fun v -> conf.invert <- v);
3902 src#bool "max fit"
3903 (fun () -> conf.maxhfit)
3904 (fun v -> conf.maxhfit <- v);
3905 src#bool "redirect stderr"
3906 (fun () -> conf.redirectstderr)
3907 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3908 src#string "uri launcher"
3909 (fun () -> conf.urilauncher)
3910 (fun v -> conf.urilauncher <- v);
3911 src#string "path launcher"
3912 (fun () -> conf.pathlauncher)
3913 (fun v -> conf.pathlauncher <- v);
3914 src#string "tile size"
3915 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3916 (fun v ->
3918 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3919 conf.tileh <- max 64 w;
3920 conf.tilew <- max 64 h;
3921 flushtiles ();
3922 with exn ->
3923 state.text <- Printf.sprintf "bad tile size `%s': %s"
3924 v (Printexc.to_string exn));
3925 src#int "texture count"
3926 (fun () -> conf.texcount)
3927 (fun v ->
3928 if realloctexts v
3929 then conf.texcount <- v
3930 else showtext '!' " Failed to set texture count please retry later"
3932 src#int "slice height"
3933 (fun () -> conf.sliceheight)
3934 (fun v ->
3935 conf.sliceheight <- v;
3936 wcmd "sliceh" [`i conf.sliceheight];
3938 src#int "anti-aliasing level"
3939 (fun () -> conf.aalevel)
3940 (fun v ->
3941 conf.aalevel <- bound v 0 8;
3942 state.anchor <- getanchor ();
3943 opendoc state.path state.password;
3945 src#int "ui font size"
3946 (fun () -> fstate.fontsize)
3947 (fun v -> setfontsize (bound v 5 100));
3948 colorp "background color"
3949 (fun () -> conf.bgcolor)
3950 (fun v -> conf.bgcolor <- v);
3951 src#bool "crop hack"
3952 (fun () -> conf.crophack)
3953 (fun v -> conf.crophack <- v);
3954 src#string "trim fuzz"
3955 (fun () -> irect_to_string conf.trimfuzz)
3956 (fun v ->
3958 conf.trimfuzz <- irect_of_string v;
3959 if conf.trimmargins
3960 then settrim true conf.trimfuzz;
3961 with exn ->
3962 state.text <- Printf.sprintf "bad irect `%s': %s"
3963 v (Printexc.to_string exn)
3965 src#string "throttle"
3966 (fun () ->
3967 match conf.maxwait with
3968 | None -> "show place holder if page is not ready"
3969 | Some time ->
3970 if time = infinity
3971 then "wait for page to fully render"
3972 else
3973 "wait " ^ string_of_float time
3974 ^ " seconds before showing placeholder"
3976 (fun v ->
3978 let f = float_of_string v in
3979 if f <= 0.0
3980 then conf.maxwait <- None
3981 else conf.maxwait <- Some f
3982 with exn ->
3983 state.text <- Printf.sprintf "bad time `%s': %s"
3984 v (Printexc.to_string exn)
3986 src#string "ghyll scroll"
3987 (fun () ->
3988 match conf.ghyllscroll with
3989 | None -> ""
3990 | Some nab -> ghyllscroll_to_string nab
3992 (fun v ->
3994 let gs =
3995 if String.length v = 0
3996 then None
3997 else Some (ghyllscroll_of_string v)
3999 conf.ghyllscroll <- gs
4000 with exn ->
4001 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4002 v (Printexc.to_string exn)
4004 src#string "selection command"
4005 (fun () -> conf.selcmd)
4006 (fun v -> conf.selcmd <- v);
4007 src#colorspace "color space"
4008 (fun () -> colorspace_to_string conf.colorspace)
4009 (fun v ->
4010 conf.colorspace <- colorspace_of_int v;
4011 wcmd "cs" [`i v];
4012 load state.layout;
4016 sep ();
4017 src#caption "Document" 0;
4018 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4019 src#caption2 "Pages"
4020 (fun () -> string_of_int state.pagecount) 1;
4021 src#caption2 "Dimensions"
4022 (fun () -> string_of_int (List.length state.pdims)) 1;
4023 if conf.trimmargins
4024 then (
4025 sep ();
4026 src#caption "Trimmed margins" 0;
4027 src#caption2 "Dimensions"
4028 (fun () -> string_of_int (List.length state.pdims)) 1;
4031 src#reset prevmode prevuioh;
4033 fun () ->
4034 state.text <- "";
4035 let prevmode = state.mode
4036 and prevuioh = state.uioh in
4037 fillsrc prevmode prevuioh;
4038 let source = (src :> lvsource) in
4039 state.uioh <- coe (object (self)
4040 inherit listview ~source ~trusted:true as super
4041 val mutable m_prevmemused = 0
4042 method infochanged = function
4043 | Memused ->
4044 if m_prevmemused != state.memused
4045 then (
4046 m_prevmemused <- state.memused;
4047 G.postRedisplay "memusedchanged";
4049 | Pdim -> G.postRedisplay "pdimchanged"
4050 | Docinfo -> fillsrc prevmode prevuioh
4052 method key key mask =
4053 if not (Wsi.withctrl mask)
4054 then
4055 match key with
4056 | 0xff51 -> coe (self#updownlevel ~-1)
4057 | 0xff53 -> coe (self#updownlevel 1)
4058 | _ -> super#key key mask
4059 else super#key key mask
4060 end);
4061 G.postRedisplay "info";
4064 let enterhelpmode =
4065 let source =
4066 (object
4067 inherit lvsourcebase
4068 method getitemcount = Array.length state.help
4069 method getitem n =
4070 let s, n, _ = state.help.(n) in
4071 (s, n)
4073 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4074 let optuioh =
4075 if not cancel
4076 then (
4077 m_qsearch <- qsearch;
4078 match state.help.(active) with
4079 | _, _, Action f -> Some (f uioh)
4080 | _ -> Some (uioh)
4082 else None
4084 m_active <- active;
4085 m_first <- first;
4086 m_pan <- pan;
4087 optuioh
4089 method hasaction n =
4090 match state.help.(n) with
4091 | _, _, Action _ -> true
4092 | _ -> false
4094 initializer
4095 m_active <- -1
4096 end)
4097 in fun () ->
4098 state.uioh <- coe (new listview ~source ~trusted:true);
4099 G.postRedisplay "help";
4102 let entermsgsmode =
4103 let msgsource =
4104 let re = Str.regexp "[\r\n]" in
4105 (object
4106 inherit lvsourcebase
4107 val mutable m_items = [||]
4109 method getitemcount = 1 + Array.length m_items
4111 method getitem n =
4112 if n = 0
4113 then "[Clear]", 0
4114 else m_items.(n-1), 0
4116 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4117 ignore uioh;
4118 if not cancel
4119 then (
4120 if active = 0
4121 then Buffer.clear state.errmsgs;
4122 m_qsearch <- qsearch;
4124 m_active <- active;
4125 m_first <- first;
4126 m_pan <- pan;
4127 None
4129 method hasaction n =
4130 n = 0
4132 method reset =
4133 state.newerrmsgs <- false;
4134 let l = Str.split re (Buffer.contents state.errmsgs) in
4135 m_items <- Array.of_list l
4137 initializer
4138 m_active <- 0
4139 end)
4140 in fun () ->
4141 state.text <- "";
4142 msgsource#reset;
4143 let source = (msgsource :> lvsource) in
4144 state.uioh <- coe (object
4145 inherit listview ~source ~trusted:false as super
4146 method display =
4147 if state.newerrmsgs
4148 then msgsource#reset;
4149 super#display
4150 end);
4151 G.postRedisplay "msgs";
4154 let quickbookmark ?title () =
4155 match state.layout with
4156 | [] -> ()
4157 | l :: _ ->
4158 let title =
4159 match title with
4160 | None ->
4161 let sec = Unix.gettimeofday () in
4162 let tm = Unix.localtime sec in
4163 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4164 (l.pageno+1)
4165 tm.Unix.tm_mday
4166 tm.Unix.tm_mon
4167 (tm.Unix.tm_year + 1900)
4168 tm.Unix.tm_hour
4169 tm.Unix.tm_min
4170 | Some title -> title
4172 state.bookmarks <-
4173 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4174 :: state.bookmarks
4177 let doreshape w h =
4178 state.fullscreen <- None;
4179 Wsi.reshape w h;
4182 let setautoscrollspeed step goingdown =
4183 let incr = max 1 ((abs step) / 2) in
4184 let incr = if goingdown then incr else -incr in
4185 let astep = step + incr in
4186 state.autoscroll <- Some astep;
4189 let viewkeyboard key mask =
4190 let enttext te =
4191 let mode = state.mode in
4192 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4193 state.text <- "";
4194 enttext ();
4195 G.postRedisplay "view:enttext"
4197 let ctrl = Wsi.withctrl mask in
4198 match key with
4199 | 81 -> (* Q *)
4200 exit 0
4202 | 0xff1b | 113 -> (* escape / q *)
4203 begin match state.mstate with
4204 | Mzoomrect _ ->
4205 state.mstate <- Mnone;
4206 Wsi.setcursor Wsi.CURSOR_INHERIT;
4207 G.postRedisplay "kill zoom rect";
4208 | _ ->
4209 match state.ranchors with
4210 | [] -> raise Wsi.Quit
4211 | (path, password, anchor) :: rest ->
4212 state.ranchors <- rest;
4213 state.anchor <- anchor;
4214 opendoc path password
4215 end;
4217 | 0xff08 -> (* backspace *)
4218 let y = getnav ~-1 in
4219 gotoy_and_clear_text y
4221 | 111 -> (* o *)
4222 enteroutlinemode ()
4224 | 117 -> (* u *)
4225 state.rects <- [];
4226 state.text <- "";
4227 G.postRedisplay "dehighlight";
4229 | 47 | 63 -> (* / ? *)
4230 let ondone isforw s =
4231 cbput state.hists.pat s;
4232 state.searchpattern <- s;
4233 search s isforw
4235 let s = String.create 1 in
4236 s.[0] <- Char.chr key;
4237 enttext (s, "", Some (onhist state.hists.pat),
4238 textentry, ondone (key = 47))
4240 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4241 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4242 setzoom (conf.zoom +. incr)
4244 | 43 | 0xffab -> (* + *)
4245 let ondone s =
4246 let n =
4247 try int_of_string s with exc ->
4248 state.text <- Printf.sprintf "bad integer `%s': %s"
4249 s (Printexc.to_string exc);
4250 max_int
4252 if n != max_int
4253 then (
4254 conf.pagebias <- n;
4255 state.text <- "page bias is now " ^ string_of_int n;
4258 enttext ("page bias: ", "", None, intentry, ondone)
4260 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4261 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4262 setzoom (max 0.01 (conf.zoom -. decr))
4264 | 45 | 0xffad -> (* - *)
4265 let ondone msg = state.text <- msg in
4266 enttext (
4267 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4268 optentry state.mode, ondone
4271 | 48 when ctrl -> (* ctrl-0 *)
4272 setzoom 1.0
4274 | 49 when ctrl -> (* 1 *)
4275 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4276 if zoom < 1.0
4277 then setzoom zoom
4279 | 0xffc6 -> (* f9 *)
4280 togglebirdseye ()
4282 | 57 when ctrl -> (* ctrl-9 *)
4283 togglebirdseye ()
4285 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4286 when not ctrl -> (* 0..9 *)
4287 let ondone s =
4288 let n =
4289 try int_of_string s with exc ->
4290 state.text <- Printf.sprintf "bad integer `%s': %s"
4291 s (Printexc.to_string exc);
4294 if n >= 0
4295 then (
4296 addnav ();
4297 cbput state.hists.pag (string_of_int n);
4298 gotopage1 (n + conf.pagebias - 1) 0;
4301 let pageentry text key =
4302 match Char.unsafe_chr key with
4303 | 'g' -> TEdone text
4304 | _ -> intentry text key
4306 let text = "x" in text.[0] <- Char.chr key;
4307 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4309 | 98 -> (* b *)
4310 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4311 reshape conf.winw conf.winh;
4313 | 108 -> (* l *)
4314 conf.hlinks <- not conf.hlinks;
4315 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4316 G.postRedisplay "toggle highlightlinks";
4318 | 97 -> (* a *)
4319 begin match state.autoscroll with
4320 | Some step ->
4321 conf.autoscrollstep <- step;
4322 state.autoscroll <- None
4323 | None ->
4324 if conf.autoscrollstep = 0
4325 then state.autoscroll <- Some 1
4326 else state.autoscroll <- Some conf.autoscrollstep
4329 | 80 -> (* P *)
4330 conf.presentation <- not conf.presentation;
4331 if conf.presentation
4332 then (
4333 if not conf.scrollbarinpm
4334 then state.scrollw <- 0;
4336 else
4337 state.scrollw <- conf.scrollbw;
4339 showtext ' ' ("presentation mode " ^
4340 if conf.presentation then "on" else "off");
4341 state.anchor <- getanchor ();
4342 represent ()
4344 | 102 -> (* f *)
4345 begin match state.fullscreen with
4346 | None ->
4347 state.fullscreen <- Some (conf.winw, conf.winh);
4348 Wsi.fullscreen ()
4349 | Some (w, h) ->
4350 state.fullscreen <- None;
4351 doreshape w h
4354 | 103 -> (* g *)
4355 gotoy_and_clear_text 0
4357 | 71 -> (* G *)
4358 gotopage1 (state.pagecount - 1) 0
4360 | 112 | 78 -> (* p|N *)
4361 search state.searchpattern false
4363 | 110 | 0xffc0 -> (* n|F3 *)
4364 search state.searchpattern true
4366 | 116 -> (* t *)
4367 begin match state.layout with
4368 | [] -> ()
4369 | l :: _ ->
4370 gotoy_and_clear_text (getpagey l.pageno)
4373 | 32 -> (* ' ' *)
4374 begin match List.rev state.layout with
4375 | [] -> ()
4376 | l :: _ ->
4377 let pageno = min (l.pageno+1) (state.pagecount-1) in
4378 gotoy_and_clear_text (getpagey pageno)
4381 | 0xff9f | 0xffff -> (* delete *)
4382 begin match state.layout with
4383 | [] -> ()
4384 | l :: _ ->
4385 let pageno = max 0 (l.pageno-1) in
4386 gotoy_and_clear_text (getpagey pageno)
4389 | 61 -> (* = *)
4390 showtext ' ' (describe_location ());
4392 | 119 -> (* w *)
4393 begin match state.layout with
4394 | [] -> ()
4395 | l :: _ ->
4396 doreshape (l.pagew + state.scrollw) l.pageh;
4397 G.postRedisplay "w"
4400 | 39 -> (* ' *)
4401 enterbookmarkmode ()
4403 | 104 | 0xffbe -> (* h|F1 *)
4404 enterhelpmode ()
4406 | 105 -> (* i *)
4407 enterinfomode ()
4409 | 101 when conf.redirectstderr -> (* e *)
4410 entermsgsmode ()
4412 | 109 -> (* m *)
4413 let ondone s =
4414 match state.layout with
4415 | l :: _ ->
4416 state.bookmarks <-
4417 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4418 :: state.bookmarks
4419 | _ -> ()
4421 enttext ("bookmark: ", "", None, textentry, ondone)
4423 | 126 -> (* ~ *)
4424 quickbookmark ();
4425 showtext ' ' "Quick bookmark added";
4427 | 122 -> (* z *)
4428 begin match state.layout with
4429 | l :: _ ->
4430 let rect = getpdimrect l.pagedimno in
4431 let w, h =
4432 if conf.crophack
4433 then
4434 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4435 truncate (1.2 *. (rect.(3) -. rect.(0))))
4436 else
4437 (truncate (rect.(1) -. rect.(0)),
4438 truncate (rect.(3) -. rect.(0)))
4440 let w = truncate ((float w)*.conf.zoom)
4441 and h = truncate ((float h)*.conf.zoom) in
4442 if w != 0 && h != 0
4443 then (
4444 state.anchor <- getanchor ();
4445 doreshape (w + state.scrollw) (h + conf.interpagespace)
4447 G.postRedisplay "z";
4449 | [] -> ()
4452 | 50 when ctrl -> (* ctrl-2 *)
4453 let maxw = getmaxw () in
4454 if maxw > 0.0
4455 then setzoom (maxw /. float conf.winw)
4457 | 60 | 62 -> (* < > *)
4458 reqlayout (conf.angle + (if key = 60 then 30 else -30)) conf.proportional
4460 | 91 | 93 -> (* [ ] *)
4461 conf.colorscale <-
4462 bound (conf.colorscale +. (if key = 92 then 0.1 else -0.1)) 0.0 1.0
4464 G.postRedisplay "brightness";
4466 | 107 | 0xff52 -> (* k up *)
4467 begin match state.autoscroll with
4468 | None ->
4469 begin match state.mode with
4470 | Birdseye beye -> upbirdseye 1 beye
4471 | _ ->
4472 if ctrl
4473 then gotoy (clamp ~-(conf.winh/2))
4474 else gotoy (clamp (-conf.scrollstep))
4476 | Some n ->
4477 setautoscrollspeed n false
4480 | 106 | 0xff54 -> (* j down *)
4481 begin match state.autoscroll with
4482 | None ->
4483 begin match state.mode with
4484 | Birdseye beye -> downbirdseye 1 beye
4485 | _ ->
4486 if ctrl
4487 then gotoy_and_clear_text (clamp (conf.winh/2))
4488 else gotoy_and_clear_text (clamp conf.scrollstep)
4490 | Some n ->
4491 setautoscrollspeed n true
4494 | 0xff51 | 0xff53 when conf.zoom > 1.0 -> (* left / right *)
4495 if conf.zoom > 1.0
4496 then
4497 let dx =
4498 if ctrl
4499 then conf.winw / 2
4500 else 10
4502 let dx = if key = 0xff51 then dx else -dx in
4503 state.x <- state.x + dx;
4504 gotoy_and_clear_text state.y
4505 else (
4506 state.text <- "";
4507 G.postRedisplay "lef/right"
4510 | 0xff55 -> (* prior *)
4511 let y =
4512 if ctrl
4513 then
4514 match state.layout with
4515 | [] -> state.y
4516 | l :: _ -> state.y - l.pagey
4517 else
4518 clamp (-conf.winh)
4520 gotoghyll y
4522 | 0xff56 -> (* next *)
4523 let y =
4524 if ctrl
4525 then
4526 match List.rev state.layout with
4527 | [] -> state.y
4528 | l :: _ -> getpagey l.pageno
4529 else
4530 clamp conf.winh
4532 gotoghyll y
4534 | 0xff50 -> gotoghyll 0
4535 | 0xff57 -> gotoghyll (clamp state.maxy)
4536 | 0xff53 when Wsi.withalt mask ->
4537 gotoghyll (getnav ~-1)
4538 | 0xff51 when Wsi.withalt mask ->
4539 gotoghyll (getnav 1)
4541 | 114 -> (* r *)
4542 state.anchor <- getanchor ();
4543 opendoc state.path state.password
4545 | 76 -> (* L *)
4546 launchpath ()
4548 | 118 when conf.debug -> (* v *)
4549 state.rects <- [];
4550 List.iter (fun l ->
4551 match getopaque l.pageno with
4552 | None -> ()
4553 | Some opaque ->
4554 let x0, y0, x1, y1 = pagebbox opaque in
4555 let a,b = float x0, float y0 in
4556 let c,d = float x1, float y0 in
4557 let e,f = float x1, float y1 in
4558 let h,j = float x0, float y1 in
4559 let rect = (a,b,c,d,e,f,h,j) in
4560 debugrect rect;
4561 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4562 ) state.layout;
4563 G.postRedisplay "v";
4565 | _ ->
4566 vlog "huh? %d" key
4569 let keyboard key mask =
4570 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4571 then wcmd "interrupt" []
4572 else state.uioh <- state.uioh#key key mask
4575 let birdseyekeyboard key mask
4576 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4577 let incr =
4578 match conf.columns with
4579 | None -> 1
4580 | Some ((c, _, _), _) -> c
4582 match key with
4583 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4584 let y, h = getpageyh pageno in
4585 let top = (conf.winh - h) / 2 in
4586 gotoy (max 0 (y - top))
4587 | 0xff0d -> leavebirdseye beye false
4588 | 0xff1b -> leavebirdseye beye true (* escape *)
4589 | 0xff52 -> upbirdseye incr beye (* prior *)
4590 | 0xff54 -> downbirdseye incr beye (* next *)
4591 | 0xff51 -> upbirdseye 1 beye (* up *)
4592 | 0xff53 -> downbirdseye 1 beye (* down *)
4594 | 0xff55 ->
4595 begin match state.layout with
4596 | l :: _ ->
4597 if l.pagey != 0
4598 then (
4599 state.mode <- Birdseye (
4600 oconf, leftx, l.pageno, hooverpageno, anchor
4602 gotopage1 l.pageno 0;
4604 else (
4605 let layout = layout (state.y-conf.winh) conf.winh in
4606 match layout with
4607 | [] -> gotoy (clamp (-conf.winh))
4608 | l :: _ ->
4609 state.mode <- Birdseye (
4610 oconf, leftx, l.pageno, hooverpageno, anchor
4612 gotopage1 l.pageno 0
4615 | [] -> gotoy (clamp (-conf.winh))
4616 end;
4618 | 0xff56 ->
4619 begin match List.rev state.layout with
4620 | l :: _ ->
4621 let layout = layout (state.y + conf.winh) conf.winh in
4622 begin match layout with
4623 | [] ->
4624 let incr = l.pageh - l.pagevh in
4625 if incr = 0
4626 then (
4627 state.mode <-
4628 Birdseye (
4629 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4631 G.postRedisplay "birdseye pagedown";
4633 else gotoy (clamp (incr + conf.interpagespace*2));
4635 | l :: _ ->
4636 state.mode <-
4637 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4638 gotopage1 l.pageno 0;
4641 | [] -> gotoy (clamp conf.winh)
4642 end;
4644 | 0xff50 ->
4645 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4646 gotopage1 0 0
4648 | 0xff57 ->
4649 let pageno = state.pagecount - 1 in
4650 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4651 if not (pagevisible state.layout pageno)
4652 then
4653 let h =
4654 match List.rev state.pdims with
4655 | [] -> conf.winh
4656 | (_, _, h, _) :: _ -> h
4658 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4659 else G.postRedisplay "birdseye end";
4660 | _ -> viewkeyboard key mask
4663 let drawpage l =
4664 let color =
4665 match state.mode with
4666 | Textentry _ -> scalecolor 0.4
4667 | View -> scalecolor 1.0
4668 | Birdseye (_, _, pageno, hooverpageno, _) ->
4669 if l.pageno = hooverpageno
4670 then scalecolor 0.9
4671 else (
4672 if l.pageno = pageno
4673 then scalecolor 1.0
4674 else scalecolor 0.8
4677 drawtiles l color;
4678 begin match getopaque l.pageno with
4679 | Some opaque ->
4680 if tileready l l.pagex l.pagey
4681 then
4682 let x = l.pagedispx - l.pagex
4683 and y = l.pagedispy - l.pagey in
4684 postprocess opaque conf.hlinks x y;
4686 | _ -> ()
4687 end;
4690 let scrollindicator () =
4691 let sbw, ph, sh = state.uioh#scrollph in
4692 let sbh, pw, sw = state.uioh#scrollpw in
4694 GlDraw.color (0.64, 0.64, 0.64);
4695 GlDraw.rect
4696 (float (conf.winw - sbw), 0.)
4697 (float conf.winw, float conf.winh)
4699 GlDraw.rect
4700 (0., float (conf.winh - sbh))
4701 (float (conf.winw - state.scrollw - 1), float conf.winh)
4703 GlDraw.color (0.0, 0.0, 0.0);
4705 GlDraw.rect
4706 (float (conf.winw - sbw), ph)
4707 (float conf.winw, ph +. sh)
4709 GlDraw.rect
4710 (pw, float (conf.winh - sbh))
4711 (pw +. sw, float conf.winh)
4715 let showsel () =
4716 match state.mstate with
4717 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4720 | Msel ((x0, y0), (x1, y1)) ->
4721 let rec loop = function
4722 | l :: ls ->
4723 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4724 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4725 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4726 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4727 then
4728 match getopaque l.pageno with
4729 | Some opaque ->
4730 let dx, dy = pagetranslatepoint l 0 0 in
4731 let x0 = x0 + dx
4732 and y0 = y0 + dy
4733 and x1 = x1 + dx
4734 and y1 = y1 + dy in
4735 GlMat.mode `modelview;
4736 GlMat.push ();
4737 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4738 seltext opaque (x0, y0, x1, y1);
4739 GlMat.pop ();
4740 | _ -> ()
4741 else loop ls
4742 | [] -> ()
4744 loop state.layout
4747 let showrects () =
4748 Gl.enable `blend;
4749 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4750 GlDraw.polygon_mode `both `fill;
4751 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4752 List.iter
4753 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4754 List.iter (fun l ->
4755 if l.pageno = pageno
4756 then (
4757 let dx = float (l.pagedispx - l.pagex) in
4758 let dy = float (l.pagedispy - l.pagey) in
4759 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4760 GlDraw.begins `quads;
4762 GlDraw.vertex2 (x0+.dx, y0+.dy);
4763 GlDraw.vertex2 (x1+.dx, y1+.dy);
4764 GlDraw.vertex2 (x2+.dx, y2+.dy);
4765 GlDraw.vertex2 (x3+.dx, y3+.dy);
4767 GlDraw.ends ();
4769 ) state.layout
4770 ) state.rects
4772 Gl.disable `blend;
4775 let display () =
4776 GlClear.color (scalecolor2 conf.bgcolor);
4777 GlClear.clear [`color];
4778 List.iter drawpage state.layout;
4779 showrects ();
4780 showsel ();
4781 state.uioh#display;
4782 scrollindicator ();
4783 begin match state.mstate with
4784 | Mzoomrect ((x0, y0), (x1, y1)) ->
4785 Gl.enable `blend;
4786 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4787 GlDraw.polygon_mode `both `fill;
4788 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4789 GlDraw.rect (float x0, float y0)
4790 (float x1, float y1);
4791 Gl.disable `blend;
4792 | _ -> ()
4793 end;
4794 enttext ();
4795 if conf.updatecurs
4796 then (
4797 let mx, my = state.mpos in
4798 updateunder mx my;
4800 Wsi.swapb ();
4803 let zoomrect x y x1 y1 =
4804 let x0 = min x x1
4805 and x1 = max x x1
4806 and y0 = min y y1 in
4807 gotoy (state.y + y0);
4808 state.anchor <- getanchor ();
4809 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4810 let margin =
4811 if state.w < conf.winw - state.scrollw
4812 then (conf.winw - state.scrollw - state.w) / 2
4813 else 0
4815 state.x <- (state.x + margin) - x0;
4816 setzoom zoom;
4817 Wsi.setcursor Wsi.CURSOR_INHERIT;
4818 state.mstate <- Mnone;
4821 let scrollx x =
4822 let winw = conf.winw - state.scrollw - 1 in
4823 let s = float x /. float winw in
4824 let destx = truncate (float (state.w + winw) *. s) in
4825 state.x <- winw - destx;
4826 gotoy_and_clear_text state.y;
4827 state.mstate <- Mscrollx;
4830 let scrolly y =
4831 let s = float y /. float conf.winh in
4832 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4833 gotoy_and_clear_text desty;
4834 state.mstate <- Mscrolly;
4837 let viewmouse button down x y mask =
4838 match button with
4839 | n when (n == 4 || n == 5) && not down ->
4840 if Wsi.withctrl mask
4841 then (
4842 match state.mstate with
4843 | Mzoom (oldn, i) ->
4844 if oldn = n
4845 then (
4846 if i = 2
4847 then
4848 let incr =
4849 match n with
4850 | 5 ->
4851 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4852 | _ ->
4853 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4855 let zoom = conf.zoom -. incr in
4856 setzoom zoom;
4857 state.mstate <- Mzoom (n, 0);
4858 else
4859 state.mstate <- Mzoom (n, i+1);
4861 else state.mstate <- Mzoom (n, 0)
4863 | _ -> state.mstate <- Mzoom (n, 0)
4865 else (
4866 match state.autoscroll with
4867 | Some step -> setautoscrollspeed step (n=4)
4868 | None ->
4869 let incr =
4870 if n = 4
4871 then -conf.scrollstep
4872 else conf.scrollstep
4874 let incr = incr * 2 in
4875 let y = clamp incr in
4876 gotoy_and_clear_text y
4879 | 1 when Wsi.withctrl mask ->
4880 if down
4881 then (
4882 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
4883 state.mstate <- Mpan (x, y)
4885 else
4886 state.mstate <- Mnone
4888 | 3 ->
4889 if down
4890 then (
4891 Wsi.setcursor Wsi.CURSOR_CYCLE;
4892 let p = (x, y) in
4893 state.mstate <- Mzoomrect (p, p)
4895 else (
4896 match state.mstate with
4897 | Mzoomrect ((x0, y0), _) ->
4898 if abs (x-x0) > 10 && abs (y - y0) > 10
4899 then zoomrect x0 y0 x y
4900 else (
4901 state.mstate <- Mnone;
4902 Wsi.setcursor Wsi.CURSOR_INHERIT;
4903 G.postRedisplay "kill accidental zoom rect";
4905 | _ ->
4906 Wsi.setcursor Wsi.CURSOR_INHERIT;
4907 state.mstate <- Mnone
4910 | 1 when x > conf.winw - state.scrollw ->
4911 if down
4912 then
4913 let _, position, sh = state.uioh#scrollph in
4914 if y > truncate position && y < truncate (position +. sh)
4915 then state.mstate <- Mscrolly
4916 else scrolly y
4917 else
4918 state.mstate <- Mnone
4920 | 1 when y > conf.winh - state.hscrollh ->
4921 if down
4922 then
4923 let _, position, sw = state.uioh#scrollpw in
4924 if x > truncate position && x < truncate (position +. sw)
4925 then state.mstate <- Mscrollx
4926 else scrollx x
4927 else
4928 state.mstate <- Mnone
4930 | 1 ->
4931 let dest = if down then getunder x y else Unone in
4932 begin match dest with
4933 | Ulinkgoto (pageno, top) ->
4934 if pageno >= 0
4935 then (
4936 addnav ();
4937 gotopage1 pageno top;
4940 | Ulinkuri s ->
4941 gotouri s
4943 | Uremote (filename, pageno) ->
4944 let path =
4945 if Sys.file_exists filename
4946 then filename
4947 else
4948 let dir = Filename.dirname state.path in
4949 let path = Filename.concat dir filename in
4950 if Sys.file_exists path
4951 then path
4952 else ""
4954 if String.length path > 0
4955 then (
4956 let anchor = getanchor () in
4957 let ranchor = state.path, state.password, anchor in
4958 state.anchor <- (pageno, 0.0);
4959 state.ranchors <- ranchor :: state.ranchors;
4960 opendoc path "";
4962 else showtext '!' ("Could not find " ^ filename)
4964 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
4966 | Unone when down ->
4967 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
4968 state.mstate <- Mpan (x, y);
4970 | Unone | Utext _ ->
4971 if down
4972 then (
4973 if conf.angle mod 360 = 0
4974 then (
4975 state.mstate <- Msel ((x, y), (x, y));
4976 G.postRedisplay "mouse select";
4979 else (
4980 match state.mstate with
4981 | Mnone -> ()
4983 | Mzoom _ | Mscrollx | Mscrolly ->
4984 state.mstate <- Mnone
4986 | Mzoomrect ((x0, y0), _) ->
4987 zoomrect x0 y0 x y
4989 | Mpan _ ->
4990 Wsi.setcursor Wsi.CURSOR_INHERIT;
4991 state.mstate <- Mnone
4993 | Msel ((_, y0), (_, y1)) ->
4994 let rec loop = function
4995 | [] -> ()
4996 | l :: rest ->
4997 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4998 || ((y1 >= l.pagedispy
4999 && y1 <= (l.pagedispy + l.pagevh)))
5000 then
5001 match getopaque l.pageno with
5002 | Some opaque ->
5003 copysel conf.selcmd opaque;
5004 G.postRedisplay "copysel"
5005 | _ -> ()
5006 else loop rest
5008 loop state.layout;
5009 Wsi.setcursor Wsi.CURSOR_INHERIT;
5010 state.mstate <- Mnone;
5014 | _ -> ()
5017 let birdseyemouse button down x y mask
5018 (conf, leftx, _, hooverpageno, anchor) =
5019 match button with
5020 | 1 when down ->
5021 let rec loop = function
5022 | [] -> ()
5023 | l :: rest ->
5024 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5025 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5026 then (
5027 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5029 else loop rest
5031 loop state.layout
5032 | 3 -> ()
5033 | _ -> viewmouse button down x y mask
5036 let mouse button down x y mask =
5037 state.uioh <- state.uioh#button button down x y mask;
5040 let motion ~x ~y =
5041 state.uioh <- state.uioh#motion x y
5044 let pmotion ~x ~y =
5045 state.uioh <- state.uioh#pmotion x y;
5048 let uioh = object
5049 method display = ()
5051 method key key mask =
5052 begin match state.mode with
5053 | Textentry textentry -> textentrykeyboard key mask textentry
5054 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5055 | View -> viewkeyboard key mask
5056 end;
5057 state.uioh
5059 method button button bstate x y mask =
5060 begin match state.mode with
5061 | View -> viewmouse button bstate x y mask
5062 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5063 | Textentry _ -> ()
5064 end;
5065 state.uioh
5067 method motion x y =
5068 begin match state.mode with
5069 | Textentry _ -> ()
5070 | View | Birdseye _ ->
5071 match state.mstate with
5072 | Mzoom _ | Mnone -> ()
5074 | Mpan (x0, y0) ->
5075 let dx = x - x0
5076 and dy = y0 - y in
5077 state.mstate <- Mpan (x, y);
5078 if conf.zoom > 1.0 then state.x <- state.x + dx;
5079 let y = clamp dy in
5080 gotoy_and_clear_text y
5082 | Msel (a, _) ->
5083 state.mstate <- Msel (a, (x, y));
5084 G.postRedisplay "motion select";
5086 | Mscrolly ->
5087 let y = min conf.winh (max 0 y) in
5088 scrolly y
5090 | Mscrollx ->
5091 let x = min conf.winw (max 0 x) in
5092 scrollx x
5094 | Mzoomrect (p0, _) ->
5095 state.mstate <- Mzoomrect (p0, (x, y));
5096 G.postRedisplay "motion zoomrect";
5097 end;
5098 state.uioh
5100 method pmotion x y =
5101 begin match state.mode with
5102 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5103 let rec loop = function
5104 | [] ->
5105 if hooverpageno != -1
5106 then (
5107 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5108 G.postRedisplay "pmotion birdseye no hoover";
5110 | l :: rest ->
5111 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5112 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5113 then (
5114 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5115 G.postRedisplay "pmotion birdseye hoover";
5117 else loop rest
5119 loop state.layout
5121 | Textentry _ -> ()
5123 | View ->
5124 match state.mstate with
5125 | Mnone -> updateunder x y
5126 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5128 end;
5129 state.uioh
5131 method infochanged _ = ()
5133 method scrollph =
5134 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5135 let p, h = scrollph state.y maxy in
5136 state.scrollw, p, h
5138 method scrollpw =
5139 let winw = conf.winw - state.scrollw - 1 in
5140 let fwinw = float winw in
5141 let sw =
5142 let sw = fwinw /. float state.w in
5143 let sw = fwinw *. sw in
5144 max sw (float conf.scrollh)
5146 let position, sw =
5147 let f = state.w+winw in
5148 let r = float (winw-state.x) /. float f in
5149 let p = fwinw *. r in
5150 p-.sw/.2., sw
5152 let sw =
5153 if position +. sw > fwinw
5154 then fwinw -. position
5155 else sw
5157 state.hscrollh, position, sw
5158 end;;
5160 module Config =
5161 struct
5162 open Parser
5164 let fontpath = ref "";;
5166 let unent s =
5167 let l = String.length s in
5168 let b = Buffer.create l in
5169 unent b s 0 l;
5170 Buffer.contents b;
5173 let home =
5174 try Sys.getenv "HOME"
5175 with exn ->
5176 prerr_endline
5177 ("Can not determine home directory location: " ^
5178 Printexc.to_string exn);
5182 let config_of c attrs =
5183 let apply c k v =
5185 match k with
5186 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5187 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5188 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5189 | "preload" -> { c with preload = bool_of_string v }
5190 | "page-bias" -> { c with pagebias = int_of_string v }
5191 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5192 | "auto-scroll-step" ->
5193 { c with autoscrollstep = max 0 (int_of_string v) }
5194 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5195 | "crop-hack" -> { c with crophack = bool_of_string v }
5196 | "throttle" ->
5197 let mw =
5198 match String.lowercase v with
5199 | "true" -> Some infinity
5200 | "false" -> None
5201 | f -> Some (float_of_string f)
5203 { c with maxwait = mw}
5204 | "highlight-links" -> { c with hlinks = bool_of_string v }
5205 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5206 | "vertical-margin" ->
5207 { c with interpagespace = max 0 (int_of_string v) }
5208 | "zoom" ->
5209 let zoom = float_of_string v /. 100. in
5210 let zoom = max zoom 0.0 in
5211 { c with zoom = zoom }
5212 | "presentation" -> { c with presentation = bool_of_string v }
5213 | "rotation-angle" -> { c with angle = int_of_string v }
5214 | "width" -> { c with winw = max 20 (int_of_string v) }
5215 | "height" -> { c with winh = max 20 (int_of_string v) }
5216 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5217 | "proportional-display" -> { c with proportional = bool_of_string v }
5218 | "pixmap-cache-size" ->
5219 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5220 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5221 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5222 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5223 | "persistent-location" -> { c with jumpback = bool_of_string v }
5224 | "background-color" -> { c with bgcolor = color_of_string v }
5225 | "scrollbar-in-presentation" ->
5226 { c with scrollbarinpm = bool_of_string v }
5227 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5228 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5229 | "mupdf-store-size" ->
5230 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5231 | "checkers" -> { c with checkers = bool_of_string v }
5232 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5233 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5234 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5235 | "uri-launcher" -> { c with urilauncher = unent v }
5236 | "path-launcher" -> { c with pathlauncher = unent v }
5237 | "color-space" -> { c with colorspace = colorspace_of_string v }
5238 | "invert-colors" -> { c with invert = bool_of_string v }
5239 | "brightness" -> { c with colorscale = float_of_string v }
5240 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5241 | "ghyllscroll" ->
5242 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5243 | "columns" ->
5244 let nab = columns_of_string v in
5245 { c with columns = Some (nab, [||]) }
5246 | "birds-eye-columns" ->
5247 { c with beyecolumns = Some (max (int_of_string v) 2) }
5248 | "selection-command" -> { c with selcmd = unent v }
5249 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5250 | _ -> c
5251 with exn ->
5252 prerr_endline ("Error processing attribute (`" ^
5253 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5256 let rec fold c = function
5257 | [] -> c
5258 | (k, v) :: rest ->
5259 let c = apply c k v in
5260 fold c rest
5262 fold c attrs;
5265 let fromstring f pos n v d =
5266 try f v
5267 with exn ->
5268 dolog "Error processing attribute (%S=%S) at %d\n%s"
5269 n v pos (Printexc.to_string exn)
5274 let bookmark_of attrs =
5275 let rec fold title page rely = function
5276 | ("title", v) :: rest -> fold v page rely rest
5277 | ("page", v) :: rest -> fold title v rely rest
5278 | ("rely", v) :: rest -> fold title page v rest
5279 | _ :: rest -> fold title page rely rest
5280 | [] -> title, page, rely
5282 fold "invalid" "0" "0" attrs
5285 let doc_of attrs =
5286 let rec fold path page rely pan = function
5287 | ("path", v) :: rest -> fold v page rely pan rest
5288 | ("page", v) :: rest -> fold path v rely pan rest
5289 | ("rely", v) :: rest -> fold path page v pan rest
5290 | ("pan", v) :: rest -> fold path page rely v rest
5291 | _ :: rest -> fold path page rely pan rest
5292 | [] -> path, page, rely, pan
5294 fold "" "0" "0" "0" attrs
5297 let setconf dst src =
5298 dst.scrollbw <- src.scrollbw;
5299 dst.scrollh <- src.scrollh;
5300 dst.icase <- src.icase;
5301 dst.preload <- src.preload;
5302 dst.pagebias <- src.pagebias;
5303 dst.verbose <- src.verbose;
5304 dst.scrollstep <- src.scrollstep;
5305 dst.maxhfit <- src.maxhfit;
5306 dst.crophack <- src.crophack;
5307 dst.autoscrollstep <- src.autoscrollstep;
5308 dst.maxwait <- src.maxwait;
5309 dst.hlinks <- src.hlinks;
5310 dst.underinfo <- src.underinfo;
5311 dst.interpagespace <- src.interpagespace;
5312 dst.zoom <- src.zoom;
5313 dst.presentation <- src.presentation;
5314 dst.angle <- src.angle;
5315 dst.winw <- src.winw;
5316 dst.winh <- src.winh;
5317 dst.savebmarks <- src.savebmarks;
5318 dst.memlimit <- src.memlimit;
5319 dst.proportional <- src.proportional;
5320 dst.texcount <- src.texcount;
5321 dst.sliceheight <- src.sliceheight;
5322 dst.thumbw <- src.thumbw;
5323 dst.jumpback <- src.jumpback;
5324 dst.bgcolor <- src.bgcolor;
5325 dst.scrollbarinpm <- src.scrollbarinpm;
5326 dst.tilew <- src.tilew;
5327 dst.tileh <- src.tileh;
5328 dst.mustoresize <- src.mustoresize;
5329 dst.checkers <- src.checkers;
5330 dst.aalevel <- src.aalevel;
5331 dst.trimmargins <- src.trimmargins;
5332 dst.trimfuzz <- src.trimfuzz;
5333 dst.urilauncher <- src.urilauncher;
5334 dst.colorspace <- src.colorspace;
5335 dst.invert <- src.invert;
5336 dst.colorscale <- src.colorscale;
5337 dst.redirectstderr <- src.redirectstderr;
5338 dst.ghyllscroll <- src.ghyllscroll;
5339 dst.columns <- src.columns;
5340 dst.beyecolumns <- src.beyecolumns;
5341 dst.selcmd <- src.selcmd;
5342 dst.updatecurs <- src.updatecurs;
5343 dst.pathlauncher <- src.pathlauncher;
5346 let get s =
5347 let h = Hashtbl.create 10 in
5348 let dc = { defconf with angle = defconf.angle } in
5349 let rec toplevel v t spos _ =
5350 match t with
5351 | Vdata | Vcdata | Vend -> v
5352 | Vopen ("llppconfig", _, closed) ->
5353 if closed
5354 then v
5355 else { v with f = llppconfig }
5356 | Vopen _ ->
5357 error "unexpected subelement at top level" s spos
5358 | Vclose _ -> error "unexpected close at top level" s spos
5360 and llppconfig v t spos _ =
5361 match t with
5362 | Vdata | Vcdata -> v
5363 | Vend -> error "unexpected end of input in llppconfig" s spos
5364 | Vopen ("defaults", attrs, closed) ->
5365 let c = config_of dc attrs in
5366 setconf dc c;
5367 if closed
5368 then v
5369 else { v with f = skip "defaults" (fun () -> v) }
5371 | Vopen ("ui-font", attrs, closed) ->
5372 let rec getsize size = function
5373 | [] -> size
5374 | ("size", v) :: rest ->
5375 let size =
5376 fromstring int_of_string spos "size" v fstate.fontsize in
5377 getsize size rest
5378 | l -> getsize size l
5380 fstate.fontsize <- getsize fstate.fontsize attrs;
5381 if closed
5382 then v
5383 else { v with f = uifont (Buffer.create 10) }
5385 | Vopen ("doc", attrs, closed) ->
5386 let pathent, spage, srely, span = doc_of attrs in
5387 let path = unent pathent
5388 and pageno = fromstring int_of_string spos "page" spage 0
5389 and rely = fromstring float_of_string spos "rely" srely 0.0
5390 and pan = fromstring int_of_string spos "pan" span 0 in
5391 let c = config_of dc attrs in
5392 let anchor = (pageno, rely) in
5393 if closed
5394 then (Hashtbl.add h path (c, [], pan, anchor); v)
5395 else { v with f = doc path pan anchor c [] }
5397 | Vopen _ ->
5398 error "unexpected subelement in llppconfig" s spos
5400 | Vclose "llppconfig" -> { v with f = toplevel }
5401 | Vclose _ -> error "unexpected close in llppconfig" s spos
5403 and uifont b v t spos epos =
5404 match t with
5405 | Vdata | Vcdata ->
5406 Buffer.add_substring b s spos (epos - spos);
5408 | Vopen (_, _, _) ->
5409 error "unexpected subelement in ui-font" s spos
5410 | Vclose "ui-font" ->
5411 if String.length !fontpath = 0
5412 then fontpath := Buffer.contents b;
5413 { v with f = llppconfig }
5414 | Vclose _ -> error "unexpected close in ui-font" s spos
5415 | Vend -> error "unexpected end of input in ui-font" s spos
5417 and doc path pan anchor c bookmarks v t spos _ =
5418 match t with
5419 | Vdata | Vcdata -> v
5420 | Vend -> error "unexpected end of input in doc" s spos
5421 | Vopen ("bookmarks", _, closed) ->
5422 if closed
5423 then v
5424 else { v with f = pbookmarks path pan anchor c bookmarks }
5426 | Vopen (_, _, _) ->
5427 error "unexpected subelement in doc" s spos
5429 | Vclose "doc" ->
5430 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5431 { v with f = llppconfig }
5433 | Vclose _ -> error "unexpected close in doc" s spos
5435 and pbookmarks path pan anchor c bookmarks v t spos _ =
5436 match t with
5437 | Vdata | Vcdata -> v
5438 | Vend -> error "unexpected end of input in bookmarks" s spos
5439 | Vopen ("item", attrs, closed) ->
5440 let titleent, spage, srely = bookmark_of attrs in
5441 let page = fromstring int_of_string spos "page" spage 0
5442 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5443 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5444 if closed
5445 then { v with f = pbookmarks path pan anchor c bookmarks }
5446 else
5447 let f () = v in
5448 { v with f = skip "item" f }
5450 | Vopen _ ->
5451 error "unexpected subelement in bookmarks" s spos
5453 | Vclose "bookmarks" ->
5454 { v with f = doc path pan anchor c bookmarks }
5456 | Vclose _ -> error "unexpected close in bookmarks" s spos
5458 and skip tag f v t spos _ =
5459 match t with
5460 | Vdata | Vcdata -> v
5461 | Vend ->
5462 error ("unexpected end of input in skipped " ^ tag) s spos
5463 | Vopen (tag', _, closed) ->
5464 if closed
5465 then v
5466 else
5467 let f' () = { v with f = skip tag f } in
5468 { v with f = skip tag' f' }
5469 | Vclose ctag ->
5470 if tag = ctag
5471 then f ()
5472 else error ("unexpected close in skipped " ^ tag) s spos
5475 parse { f = toplevel; accu = () } s;
5476 h, dc;
5479 let do_load f ic =
5481 let len = in_channel_length ic in
5482 let s = String.create len in
5483 really_input ic s 0 len;
5484 f s;
5485 with
5486 | Parse_error (msg, s, pos) ->
5487 let subs = subs s pos in
5488 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5489 failwith ("parse error: " ^ s)
5491 | exn ->
5492 failwith ("config load error: " ^ Printexc.to_string exn)
5495 let defconfpath =
5496 let dir =
5498 let dir = Filename.concat home ".config" in
5499 if Sys.is_directory dir then dir else home
5500 with _ -> home
5502 Filename.concat dir "llpp.conf"
5505 let confpath = ref defconfpath;;
5507 let load1 f =
5508 if Sys.file_exists !confpath
5509 then
5510 match
5511 (try Some (open_in_bin !confpath)
5512 with exn ->
5513 prerr_endline
5514 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5515 Printexc.to_string exn);
5516 None
5518 with
5519 | Some ic ->
5520 begin try
5521 f (do_load get ic)
5522 with exn ->
5523 prerr_endline
5524 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5525 Printexc.to_string exn);
5526 end;
5527 close_in ic;
5529 | None -> ()
5530 else
5531 f (Hashtbl.create 0, defconf)
5534 let load () =
5535 let f (h, dc) =
5536 let pc, pb, px, pa =
5538 Hashtbl.find h (Filename.basename state.path)
5539 with Not_found -> dc, [], 0, (0, 0.0)
5541 setconf defconf dc;
5542 setconf conf pc;
5543 state.bookmarks <- pb;
5544 state.x <- px;
5545 state.scrollw <- conf.scrollbw;
5546 if conf.jumpback
5547 then state.anchor <- pa;
5548 cbput state.hists.nav pa;
5550 load1 f
5553 let add_attrs bb always dc c =
5554 let ob s a b =
5555 if always || a != b
5556 then Printf.bprintf bb "\n %s='%b'" s a
5557 and oi s a b =
5558 if always || a != b
5559 then Printf.bprintf bb "\n %s='%d'" s a
5560 and oI s a b =
5561 if always || a != b
5562 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5563 and oz s a b =
5564 if always || a <> b
5565 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5566 and oF s a b =
5567 if always || a <> b
5568 then Printf.bprintf bb "\n %s='%f'" s a
5569 and oc s a b =
5570 if always || a <> b
5571 then
5572 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5573 and oC s a b =
5574 if always || a <> b
5575 then
5576 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5577 and oR s a b =
5578 if always || a <> b
5579 then
5580 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5581 and os s a b =
5582 if always || a <> b
5583 then
5584 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5585 and og s a b =
5586 if always || a <> b
5587 then
5588 match a with
5589 | None -> ()
5590 | Some (_N, _A, _B) ->
5591 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5592 and oW s a b =
5593 if always || a <> b
5594 then
5595 let v =
5596 match a with
5597 | None -> "false"
5598 | Some f ->
5599 if f = infinity
5600 then "true"
5601 else string_of_float f
5603 Printf.bprintf bb "\n %s='%s'" s v
5604 and oco s a b =
5605 if always || a <> b
5606 then
5607 match a with
5608 | Some ((n, a, b), _) when n > 1 ->
5609 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5610 | _ -> ()
5611 and obeco s a b =
5612 if always || a <> b
5613 then
5614 match a with
5615 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5616 | _ -> ()
5618 let w, h =
5619 if always
5620 then dc.winw, dc.winh
5621 else
5622 match state.fullscreen with
5623 | Some wh -> wh
5624 | None -> c.winw, c.winh
5626 let zoom, presentation, interpagespace, maxwait =
5627 if always
5628 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5629 else
5630 match state.mode with
5631 | Birdseye (bc, _, _, _, _) ->
5632 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5633 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5635 oi "width" w dc.winw;
5636 oi "height" h dc.winh;
5637 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5638 oi "scroll-handle-height" c.scrollh dc.scrollh;
5639 ob "case-insensitive-search" c.icase dc.icase;
5640 ob "preload" c.preload dc.preload;
5641 oi "page-bias" c.pagebias dc.pagebias;
5642 oi "scroll-step" c.scrollstep dc.scrollstep;
5643 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5644 ob "max-height-fit" c.maxhfit dc.maxhfit;
5645 ob "crop-hack" c.crophack dc.crophack;
5646 oW "throttle" maxwait dc.maxwait;
5647 ob "highlight-links" c.hlinks dc.hlinks;
5648 ob "under-cursor-info" c.underinfo dc.underinfo;
5649 oi "vertical-margin" interpagespace dc.interpagespace;
5650 oz "zoom" zoom dc.zoom;
5651 ob "presentation" presentation dc.presentation;
5652 oi "rotation-angle" c.angle dc.angle;
5653 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5654 ob "proportional-display" c.proportional dc.proportional;
5655 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5656 oi "tex-count" c.texcount dc.texcount;
5657 oi "slice-height" c.sliceheight dc.sliceheight;
5658 oi "thumbnail-width" c.thumbw dc.thumbw;
5659 ob "persistent-location" c.jumpback dc.jumpback;
5660 oc "background-color" c.bgcolor dc.bgcolor;
5661 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5662 oi "tile-width" c.tilew dc.tilew;
5663 oi "tile-height" c.tileh dc.tileh;
5664 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5665 ob "checkers" c.checkers dc.checkers;
5666 oi "aalevel" c.aalevel dc.aalevel;
5667 ob "trim-margins" c.trimmargins dc.trimmargins;
5668 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5669 os "uri-launcher" c.urilauncher dc.urilauncher;
5670 os "path-launcher" c.pathlauncher dc.pathlauncher;
5671 oC "color-space" c.colorspace dc.colorspace;
5672 ob "invert-colors" c.invert dc.invert;
5673 oF "brightness" c.colorscale dc.colorscale;
5674 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5675 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
5676 oco "columns" c.columns dc.columns;
5677 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
5678 os "selection-command" c.selcmd dc.selcmd;
5679 ob "update-cursor" c.updatecurs dc.updatecurs;
5682 let save () =
5683 let uifontsize = fstate.fontsize in
5684 let bb = Buffer.create 32768 in
5685 let f (h, dc) =
5686 let dc = if conf.bedefault then conf else dc in
5687 Buffer.add_string bb "<llppconfig>\n";
5689 if String.length !fontpath > 0
5690 then
5691 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5692 uifontsize
5693 !fontpath
5694 else (
5695 if uifontsize <> 14
5696 then
5697 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5700 Buffer.add_string bb "<defaults ";
5701 add_attrs bb true dc dc;
5702 Buffer.add_string bb "/>\n";
5704 let adddoc path pan anchor c bookmarks =
5705 if bookmarks == [] && c = dc && anchor = emptyanchor
5706 then ()
5707 else (
5708 Printf.bprintf bb "<doc path='%s'"
5709 (enent path 0 (String.length path));
5711 if anchor <> emptyanchor
5712 then (
5713 let n, y = anchor in
5714 Printf.bprintf bb " page='%d'" n;
5715 if y > 1e-6
5716 then
5717 Printf.bprintf bb " rely='%f'" y
5721 if pan != 0
5722 then Printf.bprintf bb " pan='%d'" pan;
5724 add_attrs bb false dc c;
5726 begin match bookmarks with
5727 | [] -> Buffer.add_string bb "/>\n"
5728 | _ ->
5729 Buffer.add_string bb ">\n<bookmarks>\n";
5730 List.iter (fun (title, _level, (page, rely)) ->
5731 Printf.bprintf bb
5732 "<item title='%s' page='%d'"
5733 (enent title 0 (String.length title))
5734 page
5736 if rely > 1e-6
5737 then
5738 Printf.bprintf bb " rely='%f'" rely
5740 Buffer.add_string bb "/>\n";
5741 ) bookmarks;
5742 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5743 end;
5747 let pan, conf =
5748 match state.mode with
5749 | Birdseye (c, pan, _, _, _) ->
5750 let beyecolumns =
5751 match conf.columns with
5752 | Some ((c, _, _), _) -> Some c
5753 | None -> None
5754 and columns =
5755 match c.columns with
5756 | Some (c, _) -> Some (c, [||])
5757 | None -> None
5759 pan, { c with beyecolumns = beyecolumns; columns = columns }
5760 | _ -> state.x, conf
5762 let basename = Filename.basename state.path in
5763 adddoc basename pan (getanchor ())
5764 { conf with
5765 autoscrollstep =
5766 match state.autoscroll with
5767 | Some step -> step
5768 | None -> conf.autoscrollstep }
5769 (if conf.savebmarks then state.bookmarks else []);
5771 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5772 if basename <> path
5773 then adddoc path x y c bookmarks
5774 ) h;
5775 Buffer.add_string bb "</llppconfig>";
5777 load1 f;
5778 if Buffer.length bb > 0
5779 then
5781 let tmp = !confpath ^ ".tmp" in
5782 let oc = open_out_bin tmp in
5783 Buffer.output_buffer oc bb;
5784 close_out oc;
5785 Unix.rename tmp !confpath;
5786 with exn ->
5787 prerr_endline
5788 ("error while saving configuration: " ^ Printexc.to_string exn)
5790 end;;
5792 let () =
5793 Arg.parse
5794 (Arg.align
5795 [("-p", Arg.String (fun s -> state.password <- s) ,
5796 "<password> Set password");
5798 ("-f", Arg.String (fun s -> Config.fontpath := s),
5799 "<path> Set path to the user interface font");
5801 ("-c", Arg.String (fun s -> Config.confpath := s),
5802 "<path> Set path to the configuration file");
5804 ("-v", Arg.Unit (fun () ->
5805 Printf.printf
5806 "%s\nconfiguration path: %s\n"
5807 (version ())
5808 Config.defconfpath
5810 exit 0), " Print version and exit");
5813 (fun s -> state.path <- s)
5814 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5816 if String.length state.path = 0
5817 then (prerr_endline "file name missing"; exit 1);
5819 Config.load ();
5821 state.wsfd <- Wsi.init (object
5822 method display = display ()
5823 method reshape w h = reshape w h
5824 method mouse b d x y m = mouse b d x y m
5825 method motion x y = state.mpos <- (x, y); motion x y
5826 method pmotion x y = state.mpos <- (x, y); pmotion x y
5827 method key c m = keyboard c m
5828 method enter x y = state.mpos <- (x, y); pmotion x y
5829 method leave = state.mpos <- (-1, -1)
5830 end) conf.winw conf.winh;
5832 if not (
5833 List.exists GlMisc.check_extension
5834 [ "GL_ARB_texture_rectangle"
5835 ; "GL_EXT_texture_recangle"
5836 ; "GL_NV_texture_rectangle" ]
5838 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5840 let cr, sw = Unix.pipe ()
5841 and sr, cw = Unix.pipe () in
5843 cloexec cr;
5844 cloexec sw;
5845 cloexec sr;
5846 cloexec cw;
5848 setcheckers conf.checkers;
5849 redirectstderr ();
5851 init (cr, cw) (
5852 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5853 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5854 !Config.fontpath
5856 state.sr <- sr;
5857 state.sw <- sw;
5858 state.text <- "Opening " ^ state.path;
5859 setaalevel conf.aalevel;
5860 writeopen state.path state.password;
5861 state.uioh <- uioh;
5862 setfontsize fstate.fontsize;
5863 doreshape conf.winw conf.winh;
5865 let rec loop deadline =
5866 let r =
5867 match state.errfd with
5868 | None -> [state.sr; state.wsfd]
5869 | Some fd -> [state.sr; state.wsfd; fd]
5871 if state.redisplay
5872 then (
5873 state.redisplay <- false;
5874 display ();
5876 let timeout =
5877 let now = now () in
5878 if deadline > now
5879 then (
5880 if deadline = infinity
5881 then ~-.1.0
5882 else max 0.0 (deadline -. now)
5884 else 0.0
5886 let r, _, _ =
5887 try Unix.select r [] [] timeout
5888 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
5890 begin match r with
5891 | [] ->
5892 state.ghyll None;
5893 let newdeadline =
5894 match state.autoscroll with
5895 | Some step when step != 0 ->
5896 let y = state.y + step in
5897 let y =
5898 if y < 0
5899 then state.maxy
5900 else if y >= state.maxy then 0 else y
5902 gotoy y;
5903 if state.mode = View
5904 then state.text <- "";
5905 deadline +. 0.01
5906 | _ ->
5907 if state.ghyll == noghyll then infinity else deadline +. 0.01
5909 loop newdeadline
5911 | l ->
5912 let rec checkfds = function
5913 | [] -> ()
5914 | fd :: rest when fd = state.sr ->
5915 let cmd = readcmd state.sr in
5916 act cmd;
5917 checkfds rest
5919 | fd :: rest when fd = state.wsfd ->
5920 Wsi.readresp fd;
5921 checkfds rest
5923 | fd :: rest ->
5924 let s = String.create 80 in
5925 let n = Unix.read fd s 0 80 in
5926 if conf.redirectstderr
5927 then (
5928 Buffer.add_substring state.errmsgs s 0 n;
5929 state.newerrmsgs <- true;
5930 state.redisplay <- true;
5932 else (
5933 prerr_string (String.sub s 0 n);
5934 flush stderr;
5936 checkfds rest
5938 checkfds l;
5939 let newdeadline =
5940 let deadline1 =
5941 if deadline = infinity
5942 then now () +. 0.01
5943 else deadline
5945 match state.autoscroll with
5946 | Some step when step != 0 -> deadline1
5947 | _ -> if state.ghyll == noghyll then infinity else deadline1
5949 loop newdeadline
5950 end;
5953 loop infinity;
5954 with Wsi.Quit ->
5955 wcmd "quit" [];
5956 Config.save ();
5957 exit 0;