Simplify
[llpp.git] / main.ml
blobdc5ef70ff1f043041c5b1b6946fab177d023b10a
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
10 and facename = string;;
12 let dolog fmt = Printf.kprintf prerr_endline fmt;;
13 let now = Unix.gettimeofday;;
15 exception Quit;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * wmclasshack * fontpath)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and fontpath = string
36 and memsize = int
37 and aalevel = int
38 and wmclasshack = bool
39 and irect = (int * int * int * int)
40 and trimparams = (trimmargins * irect)
41 and colorspace = | Rgb | Bgr | Gray
44 type platform = | Punknown | Plinux | Pwindows | Posx | Psun
45 | Pfreebsd | Pdragonflybsd | Popenbsd | Pnetbsd
46 | Pmingw | Pcygwin;;
48 external init : Unix.file_descr -> params -> unit = "ml_init";;
49 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
50 external copysel : string -> opaque -> unit = "ml_copysel";;
51 external getpdimrect : int -> float array = "ml_getpdimrect";;
52 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
53 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
54 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
55 external measurestr : int -> string -> float = "ml_measure_string";;
56 external getmaxw : unit -> float = "ml_getmaxw";;
57 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
58 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
59 external platform : unit -> platform = "ml_platform";;
60 external setaalevel : int -> unit = "ml_setaalevel";;
61 external realloctexts : int -> bool = "ml_realloctexts";;
63 let platform_to_string = function
64 | Punknown -> "unknown"
65 | Plinux -> "Linux"
66 | Pwindows -> "Windows"
67 | Posx -> "OSX"
68 | Psun -> "Sun"
69 | Pfreebsd -> "FreeBSD"
70 | Pdragonflybsd -> "DragonflyBSD"
71 | Popenbsd -> "OpenBSD"
72 | Pnetbsd -> "NetBSD"
73 | Pcygwin -> "Cygwin"
74 | Pmingw -> "MingW"
77 let platform = platform ();;
79 let is_windows =
80 match platform with
81 | Pwindows | Pmingw -> true
82 | _ -> false
85 type x = int
86 and y = int
87 and tilex = int
88 and tiley = int
89 and tileparams = (x * y * width * height * tilex * tiley)
92 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
94 type mpos = int * int
95 and mstate =
96 | Msel of (mpos * mpos)
97 | Mpan of mpos
98 | Mscrolly | Mscrollx
99 | Mzoom of (int * int)
100 | Mzoomrect of (mpos * mpos)
101 | Mnone
104 type textentry = string * string * onhist option * onkey * ondone
105 and onkey = string -> int -> te
106 and ondone = string -> unit
107 and histcancel = unit -> unit
108 and onhist = ((histcmd -> string) * histcancel)
109 and histcmd = HCnext | HCprev | HCfirst | HClast
110 and te =
111 | TEstop
112 | TEdone of string
113 | TEcont of string
114 | TEswitch of textentry
117 type 'a circbuf =
118 { store : 'a array
119 ; mutable rc : int
120 ; mutable wc : int
121 ; mutable len : int
125 let bound v minv maxv =
126 max minv (min maxv v);
129 let cbnew n v =
130 { store = Array.create n v
131 ; rc = 0
132 ; wc = 0
133 ; len = 0
137 let drawstring size x y s =
138 Gl.enable `blend;
139 Gl.enable `texture_2d;
140 ignore (drawstr size x y s);
141 Gl.disable `blend;
142 Gl.disable `texture_2d;
145 let drawstring1 size x y s =
146 drawstr size x y s;
149 let drawstring2 size x y fmt =
150 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
153 let cbcap b = Array.length b.store;;
155 let cbput b v =
156 let cap = cbcap b in
157 b.store.(b.wc) <- v;
158 b.wc <- (b.wc + 1) mod cap;
159 b.rc <- b.wc;
160 b.len <- min (b.len + 1) cap;
163 let cbempty b = b.len = 0;;
165 let cbgetg b circular dir =
166 if cbempty b
167 then b.store.(0)
168 else
169 let rc = b.rc + dir in
170 let rc =
171 if circular
172 then (
173 if rc = -1
174 then b.len-1
175 else (
176 if rc = b.len
177 then 0
178 else rc
181 else max 0 (min rc (b.len-1))
183 b.rc <- rc;
184 b.store.(rc);
187 let cbget b = cbgetg b false;;
188 let cbgetc b = cbgetg b true;;
190 type page =
191 { pageno : int
192 ; pagedimno : int
193 ; pagew : int
194 ; pageh : int
195 ; pagex : int
196 ; pagey : int
197 ; pagevw : int
198 ; pagevh : int
199 ; pagedispx : int
200 ; pagedispy : int
204 let debugl l =
205 dolog "l %d dim=%d {" l.pageno l.pagedimno;
206 dolog " WxH %dx%d" l.pagew l.pageh;
207 dolog " vWxH %dx%d" l.pagevw l.pagevh;
208 dolog " pagex,y %d,%d" l.pagex l.pagey;
209 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
210 dolog "}";
213 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
214 dolog "rect {";
215 dolog " x0,y0=(% f, % f)" x0 y0;
216 dolog " x1,y1=(% f, % f)" x1 y1;
217 dolog " x2,y2=(% f, % f)" x2 y2;
218 dolog " x3,y3=(% f, % f)" x3 y3;
219 dolog "}";
222 type columns =
223 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
224 and multicol = columncount * covercount * covercount
225 and pdimno = int
226 and columncount = int
227 and covercount = int;;
229 type conf =
230 { mutable scrollbw : int
231 ; mutable scrollh : int
232 ; mutable icase : bool
233 ; mutable preload : bool
234 ; mutable pagebias : int
235 ; mutable verbose : bool
236 ; mutable debug : bool
237 ; mutable scrollstep : int
238 ; mutable maxhfit : bool
239 ; mutable crophack : bool
240 ; mutable autoscrollstep : int
241 ; mutable maxwait : float option
242 ; mutable hlinks : bool
243 ; mutable underinfo : bool
244 ; mutable interpagespace : interpagespace
245 ; mutable zoom : float
246 ; mutable presentation : bool
247 ; mutable angle : angle
248 ; mutable winw : int
249 ; mutable winh : int
250 ; mutable savebmarks : bool
251 ; mutable proportional : proportional
252 ; mutable trimmargins : trimmargins
253 ; mutable trimfuzz : irect
254 ; mutable memlimit : memsize
255 ; mutable texcount : texcount
256 ; mutable sliceheight : sliceheight
257 ; mutable thumbw : width
258 ; mutable jumpback : bool
259 ; mutable bgcolor : float * float * float
260 ; mutable bedefault : bool
261 ; mutable scrollbarinpm : bool
262 ; mutable tilew : int
263 ; mutable tileh : int
264 ; mutable mustoresize : memsize
265 ; mutable checkers : bool
266 ; mutable aalevel : int
267 ; mutable urilauncher : string
268 ; mutable colorspace : colorspace
269 ; mutable invert : bool
270 ; mutable colorscale : float
271 ; mutable redirectstderr : bool
272 ; mutable ghyllscroll : (int * int * int) option
273 ; mutable columns : columns option
274 ; mutable beyecolumns : columncount option
275 ; mutable selcmd : string
279 type anchor = pageno * top;;
281 type outline = string * int * anchor;;
283 type rect = float * float * float * float * float * float * float * float;;
285 type tile = opaque * pixmapsize * elapsed
286 and elapsed = float;;
287 type pagemapkey = pageno * gen;;
288 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
289 and row = int
290 and col = int;;
292 let emptyanchor = (0, 0.0);;
294 type infochange = | Memused | Docinfo | Pdim;;
296 class type uioh = object
297 method display : unit
298 method key : int -> uioh
299 method special : Glut.special_key_t -> uioh
300 method button :
301 Glut.button_t -> Glut.mouse_button_state_t -> int -> int -> uioh
302 method motion : int -> int -> uioh
303 method pmotion : int -> int -> uioh
304 method infochanged : infochange -> unit
305 method scrollpw : (int * float * float)
306 method scrollph : (int * float * float)
307 end;;
309 type mode =
310 | Birdseye of (conf * leftx * pageno * pageno * anchor)
311 | Textentry of (textentry * onleave)
312 | View
313 and onleave = leavetextentrystatus -> unit
314 and leavetextentrystatus = | Cancel | Confirm
315 and helpitem = string * int * action
316 and action =
317 | Noaction
318 | Action of (uioh -> uioh)
321 let isbirdseye = function Birdseye _ -> true | _ -> false;;
322 let istextentry = function Textentry _ -> true | _ -> false;;
324 type currently =
325 | Idle
326 | Loading of (page * gen)
327 | Tiling of (
328 page * opaque * colorspace * angle * gen * col * row * width * height
330 | Outlining of outline list
333 let nouioh : uioh = object (self)
334 method display = ()
335 method key _ = self
336 method special _ = self
337 method button _ _ _ _ = self
338 method motion _ _ = self
339 method pmotion _ _ = self
340 method infochanged _ = ()
341 method scrollpw = (0, nan, nan)
342 method scrollph = (0, nan, nan)
343 end;;
345 type state =
346 { mutable csock : Unix.file_descr
347 ; mutable ssock : Unix.file_descr
348 ; mutable errfd : Unix.file_descr option
349 ; mutable stderr : Unix.file_descr
350 ; mutable errmsgs : Buffer.t
351 ; mutable newerrmsgs : bool
352 ; mutable w : int
353 ; mutable x : int
354 ; mutable y : int
355 ; mutable scrollw : int
356 ; mutable hscrollh : int
357 ; mutable anchor : anchor
358 ; mutable maxy : int
359 ; mutable layout : page list
360 ; pagemap : (pagemapkey, opaque) Hashtbl.t
361 ; tilemap : (tilemapkey, tile) Hashtbl.t
362 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
363 ; mutable pdims : (pageno * width * height * leftx) list
364 ; mutable pagecount : int
365 ; mutable currently : currently
366 ; mutable mstate : mstate
367 ; mutable searchpattern : string
368 ; mutable rects : (pageno * recttype * rect) list
369 ; mutable rects1 : (pageno * recttype * rect) list
370 ; mutable text : string
371 ; mutable fullscreen : (width * height) option
372 ; mutable mode : mode
373 ; mutable uioh : uioh
374 ; mutable outlines : outline array
375 ; mutable bookmarks : outline list
376 ; mutable path : string
377 ; mutable password : string
378 ; mutable invalidated : int
379 ; mutable memused : memsize
380 ; mutable gen : gen
381 ; mutable throttle : (page list * int * float) option
382 ; mutable autoscroll : int option
383 ; mutable ghyll : int option -> unit
384 ; mutable help : helpitem array
385 ; mutable docinfo : (int * string) list
386 ; mutable deadline : float
387 ; mutable texid : GlTex.texture_id option
388 ; hists : hists
389 ; mutable prevzoom : float
390 ; mutable progress : float
392 and hists =
393 { pat : string circbuf
394 ; pag : string circbuf
395 ; nav : anchor circbuf
396 ; sel : string circbuf
400 let defconf =
401 { scrollbw = 7
402 ; scrollh = 12
403 ; icase = true
404 ; preload = true
405 ; pagebias = 0
406 ; verbose = false
407 ; debug = false
408 ; scrollstep = 24
409 ; maxhfit = true
410 ; crophack = false
411 ; autoscrollstep = 2
412 ; maxwait = None
413 ; hlinks = false
414 ; underinfo = false
415 ; interpagespace = 2
416 ; zoom = 1.0
417 ; presentation = false
418 ; angle = 0
419 ; winw = 900
420 ; winh = 900
421 ; savebmarks = true
422 ; proportional = true
423 ; trimmargins = false
424 ; trimfuzz = (0,0,0,0)
425 ; memlimit = 32 lsl 20
426 ; texcount = 256
427 ; sliceheight = 24
428 ; thumbw = 76
429 ; jumpback = true
430 ; bgcolor = (0.5, 0.5, 0.5)
431 ; bedefault = false
432 ; scrollbarinpm = true
433 ; tilew = 2048
434 ; tileh = 2048
435 ; mustoresize = 128 lsl 20
436 ; checkers = true
437 ; aalevel = 8
438 ; urilauncher =
439 (match platform with
440 | Plinux
441 | Pfreebsd | Pdragonflybsd | Popenbsd | Pnetbsd
442 | Psun -> "xdg-open \"%s\""
443 | Posx -> "open \"%s\""
444 | Pwindows | Pcygwin | Pmingw -> "start %s"
445 | Punknown -> "echo %s")
446 ; selcmd =
447 (match platform with
448 | Plinux
449 | Pfreebsd | Pdragonflybsd | Popenbsd | Pnetbsd
450 | Psun -> "xsel -i"
451 | Posx -> "pbcopy"
452 | Pwindows | Pcygwin | Pmingw -> "wsel"
453 | Punknown -> "cat")
454 ; colorspace = Rgb
455 ; invert = false
456 ; colorscale = 1.0
457 ; redirectstderr = false
458 ; ghyllscroll = None
459 ; columns = None
460 ; beyecolumns = None
464 let conf = { defconf with angle = defconf.angle };;
466 type fontstate =
467 { mutable fontsize : int
468 ; mutable wwidth : float
469 ; mutable maxrows : int
473 let fstate =
474 { fontsize = 14
475 ; wwidth = nan
476 ; maxrows = -1
480 let setfontsize n =
481 fstate.fontsize <- n;
482 fstate.wwidth <- measurestr fstate.fontsize "w";
483 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
486 let geturl s =
487 let colonpos = try String.index s ':' with Not_found -> -1 in
488 let len = String.length s in
489 if colonpos >= 0 && colonpos + 3 < len
490 then (
491 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
492 then
493 let schemestartpos =
494 try String.rindex_from s colonpos ' '
495 with Not_found -> -1
497 let scheme =
498 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
500 match scheme with
501 | "http" | "ftp" | "mailto" ->
502 let epos =
503 try String.index_from s colonpos ' '
504 with Not_found -> len
506 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
507 | _ -> ""
508 else ""
510 else ""
513 let popen =
514 let shell, farg =
515 if is_windows
516 then (try Sys.getenv "COMSPEC" with Not_found -> "cmd"), "/c"
517 else "/bin/sh", "-c"
519 fun s ->
520 let args = [|shell; farg; s|] in
521 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
524 let gotouri uri =
525 if String.length conf.urilauncher = 0
526 then print_endline uri
527 else (
528 let url = geturl uri in
529 if String.length url = 0
530 then print_endline uri
531 else
532 let re = Str.regexp "%s" in
533 let command = Str.global_replace re url conf.urilauncher in
534 try popen command
535 with exn ->
536 Printf.eprintf
537 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
538 flush stderr;
542 let version () =
543 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
544 (platform_to_string platform) Sys.word_size Sys.ocaml_version
547 let makehelp () =
548 let strings = version () :: "" :: Help.keys in
549 Array.of_list (
550 List.map (fun s ->
551 let url = geturl s in
552 if String.length url > 0
553 then (s, 0, Action (fun u -> gotouri url; u))
554 else (s, 0, Noaction)
555 ) strings);
558 let noghyll _ = ();;
560 let state =
561 { csock = Unix.stdin
562 ; ssock = Unix.stdin
563 ; errfd = None
564 ; stderr = Unix.stderr
565 ; errmsgs = Buffer.create 0
566 ; newerrmsgs = false
567 ; x = 0
568 ; y = 0
569 ; w = 0
570 ; scrollw = 0
571 ; hscrollh = 0
572 ; anchor = emptyanchor
573 ; layout = []
574 ; maxy = max_int
575 ; tilelru = Queue.create ()
576 ; pagemap = Hashtbl.create 10
577 ; tilemap = Hashtbl.create 10
578 ; pdims = []
579 ; pagecount = 0
580 ; currently = Idle
581 ; mstate = Mnone
582 ; rects = []
583 ; rects1 = []
584 ; text = ""
585 ; mode = View
586 ; fullscreen = None
587 ; searchpattern = ""
588 ; outlines = [||]
589 ; bookmarks = []
590 ; path = ""
591 ; password = ""
592 ; invalidated = 0
593 ; hists =
594 { nav = cbnew 10 (0, 0.0)
595 ; pat = cbnew 10 ""
596 ; pag = cbnew 10 ""
597 ; sel = cbnew 10 ""
599 ; memused = 0
600 ; gen = 0
601 ; throttle = None
602 ; autoscroll = None
603 ; ghyll = noghyll
604 ; help = makehelp ()
605 ; docinfo = []
606 ; deadline = nan
607 ; texid = None
608 ; prevzoom = 1.0
609 ; progress = -1.0
610 ; uioh = nouioh
614 let vlog fmt =
615 if conf.verbose
616 then
617 Printf.kprintf prerr_endline fmt
618 else
619 Printf.kprintf ignore fmt
622 let redirectstderr () =
623 if conf.redirectstderr
624 then
625 let rfd, wfd = Unix.pipe () in
626 state.stderr <- Unix.dup Unix.stderr;
627 state.errfd <- Some rfd;
628 Unix.dup2 wfd Unix.stderr;
629 else (
630 state.newerrmsgs <- false;
631 begin match state.errfd with
632 | Some fd ->
633 Unix.close fd;
634 Unix.dup2 state.stderr Unix.stderr;
635 state.errfd <- None;
636 | None -> ()
637 end;
638 prerr_string (Buffer.contents state.errmsgs);
639 flush stderr;
640 Buffer.clear state.errmsgs;
644 module G =
645 struct
646 let postRedisplay who =
647 if conf.verbose
648 then prerr_endline ("redisplay for " ^ who);
649 Glut.postRedisplay ();
651 end;;
653 let addchar s c =
654 let b = Buffer.create (String.length s + 1) in
655 Buffer.add_string b s;
656 Buffer.add_char b c;
657 Buffer.contents b;
660 let colorspace_of_string s =
661 match String.lowercase s with
662 | "rgb" -> Rgb
663 | "bgr" -> Bgr
664 | "gray" -> Gray
665 | _ -> failwith "invalid colorspace"
668 let int_of_colorspace = function
669 | Rgb -> 0
670 | Bgr -> 1
671 | Gray -> 2
674 let colorspace_of_int = function
675 | 0 -> Rgb
676 | 1 -> Bgr
677 | 2 -> Gray
678 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
681 let colorspace_to_string = function
682 | Rgb -> "rgb"
683 | Bgr -> "bgr"
684 | Gray -> "gray"
687 let intentry_with_suffix text key =
688 let c = Char.unsafe_chr key in
689 match Char.lowercase c with
690 | '0' .. '9' ->
691 let text = addchar text c in
692 TEcont text
694 | 'k' | 'm' | 'g' ->
695 let text = addchar text c in
696 TEcont text
698 | _ ->
699 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
700 TEcont text
703 let columns_to_string (n, a, b) =
704 if a = 0 && b = 0
705 then Printf.sprintf "%d" n
706 else Printf.sprintf "%d,%d,%d" n a b;
709 let columns_of_string s =
711 (int_of_string s, 0, 0)
712 with _ ->
713 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
716 let writecmd fd s =
717 let len = String.length s in
718 let n = 4 + len in
719 let b = Buffer.create n in
720 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
721 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
722 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
723 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
724 Buffer.add_string b s;
725 let s' = Buffer.contents b in
726 let n' = Unix.write fd s' 0 n in
727 if n' != n then failwith "write failed";
730 let readcmd fd =
731 let s = "xxxx" in
732 let n = Unix.read fd s 0 4 in
733 if n != 4 then failwith "incomplete read(len)";
734 let len = 0
735 lor (Char.code s.[0] lsl 24)
736 lor (Char.code s.[1] lsl 16)
737 lor (Char.code s.[2] lsl 8)
738 lor (Char.code s.[3] lsl 0)
740 let s = String.create len in
741 let n =
742 if is_windows
743 then
744 let rec loop n =
745 if n = 10
746 then failwith "EWOULDBLOCK encountered 10 times"
747 else
749 Unix.read fd s 0 len
750 with Unix.Unix_error (Unix.EWOULDBLOCK, _, _) ->
751 let _, _, _ = Unix.select [fd] [] [] 0.01 in
752 loop (n+1)
753 in loop 0
754 else
755 Unix.read fd s 0 len
757 if n != len then failwith "incomplete read(data)";
761 let makecmd s l =
762 let b = Buffer.create 10 in
763 Buffer.add_string b s;
764 let rec combine = function
765 | [] -> b
766 | x :: xs ->
767 Buffer.add_char b ' ';
768 let s =
769 match x with
770 | `b b -> if b then "1" else "0"
771 | `s s -> s
772 | `i i -> string_of_int i
773 | `f f -> string_of_float f
774 | `I f -> string_of_int (truncate f)
776 Buffer.add_string b s;
777 combine xs;
779 combine l;
782 let wcmd s l =
783 let cmd = Buffer.contents (makecmd s l) in
784 writecmd state.csock cmd;
787 let calcips h =
788 if conf.presentation
789 then
790 let d = conf.winh - h in
791 max 0 ((d + 1) / 2)
792 else
793 conf.interpagespace
796 let calcheight () =
797 let rec f pn ph pi fh l =
798 match l with
799 | (n, _, h, _) :: rest ->
800 let ips = calcips h in
801 let fh =
802 if conf.presentation
803 then fh+ips
804 else (
805 if isbirdseye state.mode && pn = 0
806 then fh + ips
807 else fh
810 let fh = fh + ((n - pn) * (ph + pi)) in
811 f n h ips fh rest;
813 | [] ->
814 let inc =
815 if conf.presentation || (isbirdseye state.mode && pn = 0)
816 then 0
817 else -pi
819 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
820 max 0 fh
822 let fh = f 0 0 0 0 state.pdims in
826 let calcheight () =
827 match conf.columns with
828 | None -> calcheight ()
829 | Some (_, b) ->
830 if Array.length b > 0
831 then
832 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
833 y + h
834 else 0
837 let getpageyh pageno =
838 let rec f pn ph pi y l =
839 match l with
840 | (n, _, h, _) :: rest ->
841 let ips = calcips h in
842 if n >= pageno
843 then
844 let h = if n = pageno then h else ph in
845 if conf.presentation && n = pageno
846 then
847 y + (pageno - pn) * (ph + pi) + pi, h
848 else
849 y + (pageno - pn) * (ph + pi), h
850 else
851 let y = y + (if conf.presentation then pi else 0) in
852 let y = y + (n - pn) * (ph + pi) in
853 f n h ips y rest
855 | [] ->
856 y + (pageno - pn) * (ph + pi), ph
858 f 0 0 0 0 state.pdims
861 let getpageyh pageno =
862 match conf.columns with
863 | None -> getpageyh pageno
864 | Some (_, b) ->
865 let (_, _, y, (_, _, h, _)) = b.(pageno) in
866 y, h
869 let getpagedim pageno =
870 let rec f ppdim l =
871 match l with
872 | (n, _, _, _) as pdim :: rest ->
873 if n >= pageno
874 then (if n = pageno then pdim else ppdim)
875 else f pdim rest
877 | [] -> ppdim
879 f (-1, -1, -1, -1) state.pdims
882 let getpagey pageno = fst (getpageyh pageno);;
884 let layout1 y sh =
885 let sh = sh - state.hscrollh in
886 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
887 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
888 match pdims with
889 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
890 let ips = calcips h in
891 let yinc =
892 if conf.presentation || (isbirdseye state.mode && pageno = 0)
893 then ips
894 else 0
896 (w, h, ips, xoff), rest, pdimno + 1, yinc
897 | _ ->
898 prev, pdims, pdimno, 0
900 let dy = dy + yinc in
901 let py = py + yinc in
902 if pageno = state.pagecount || dy >= sh
903 then
904 accu
905 else
906 let vy = y + dy in
907 if py + h <= vy - yinc
908 then
909 let py = py + h + ips in
910 let dy = max 0 (py - y) in
911 f ~pageno:(pageno+1)
912 ~pdimno
913 ~prev:curr
916 ~pdims:rest
917 ~accu
918 else
919 let pagey = vy - py in
920 let pagevh = h - pagey in
921 let pagevh = min (sh - dy) pagevh in
922 let off = if yinc > 0 then py - vy else 0 in
923 let py = py + h + ips in
924 let pagex, dx =
925 let xoff = xoff +
926 if state.w < conf.winw - state.scrollw
927 then (conf.winw - state.scrollw - state.w) / 2
928 else 0
930 let dispx = xoff + state.x in
931 if dispx < 0
932 then (-dispx, 0)
933 else (0, dispx)
935 let pagevw =
936 let lw = w - pagex in
937 min lw (conf.winw - state.scrollw)
939 let e =
940 { pageno = pageno
941 ; pagedimno = pdimno
942 ; pagew = w
943 ; pageh = h
944 ; pagex = pagex
945 ; pagey = pagey + off
946 ; pagevw = pagevw
947 ; pagevh = pagevh - off
948 ; pagedispx = dx
949 ; pagedispy = dy + off
952 let accu = e :: accu in
953 f ~pageno:(pageno+1)
954 ~pdimno
955 ~prev:curr
957 ~dy:(dy+pagevh+ips)
958 ~pdims:rest
959 ~accu
961 if state.invalidated = 0
962 then (
963 let accu =
965 ~pageno:0
966 ~pdimno:~-1
967 ~prev:(0,0,0,0)
968 ~py:0
969 ~dy:0
970 ~pdims:state.pdims
971 ~accu:[]
973 List.rev accu
975 else
979 let layoutN ((columns, coverA, coverB), b) y sh =
980 let sh = sh - state.hscrollh in
981 let rec fold accu n =
982 if n = Array.length b
983 then accu
984 else
985 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
986 if (vy - y) > sh &&
987 (n = coverA - 1
988 || n = state.pagecount - coverB
989 || (n - coverA) mod columns = columns - 1)
990 then accu
991 else
992 let accu =
993 if vy + h > y
994 then
995 let pagey = max 0 (y - vy) in
996 let pagedispy = if pagey > 0 then 0 else vy - y in
997 let pagedispx, pagex, pagevw =
998 let pdx =
999 if n = coverA - 1 || n = state.pagecount - coverB
1000 then state.x + (conf.winw - state.scrollw - w) / 2
1001 else dx + xoff + state.x
1003 if pdx < 0
1004 then 0, -pdx, w + pdx
1005 else pdx, 0, min (conf.winw - state.scrollw) w
1007 let pagevh = min (h - pagey) (sh - pagedispy) in
1008 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1009 then
1010 let e =
1011 { pageno = n
1012 ; pagedimno = pdimno
1013 ; pagew = w
1014 ; pageh = h
1015 ; pagex = pagex
1016 ; pagey = pagey
1017 ; pagevw = pagevw
1018 ; pagevh = pagevh
1019 ; pagedispx = pagedispx
1020 ; pagedispy = pagedispy
1023 e :: accu
1024 else
1025 accu
1026 else
1027 accu
1029 fold accu (n+1)
1031 if state.invalidated = 0
1032 then List.rev (fold [] 0)
1033 else []
1036 let layout y sh =
1037 match conf.columns with
1038 | None -> layout1 y sh
1039 | Some c -> layoutN c y sh
1042 let clamp incr =
1043 let y = state.y + incr in
1044 let y = max 0 y in
1045 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1049 let getopaque pageno =
1050 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
1051 with Not_found -> None
1054 let putopaque pageno opaque =
1055 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
1058 let itertiles l f =
1059 let tilex = l.pagex mod conf.tilew in
1060 let tiley = l.pagey mod conf.tileh in
1062 let col = l.pagex / conf.tilew in
1063 let row = l.pagey / conf.tileh in
1065 let vw =
1066 let a = l.pagew - l.pagex in
1067 let b = conf.winw - state.scrollw in
1068 min a b
1069 and vh = l.pagevh in
1071 let rec rowloop row y0 dispy h =
1072 if h = 0
1073 then ()
1074 else (
1075 let dh = conf.tileh - y0 in
1076 let dh = min h dh in
1077 let rec colloop col x0 dispx w =
1078 if w = 0
1079 then ()
1080 else (
1081 let dw = conf.tilew - x0 in
1082 let dw = min w dw in
1084 f col row dispx dispy x0 y0 dw dh;
1085 colloop (col+1) 0 (dispx+dw) (w-dw)
1088 colloop col tilex l.pagedispx vw;
1089 rowloop (row+1) 0 (dispy+dh) (h-dh)
1092 if vw > 0 && vh > 0
1093 then rowloop row tiley l.pagedispy vh;
1096 let gettileopaque l col row =
1097 let key =
1098 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1100 try Some (Hashtbl.find state.tilemap key)
1101 with Not_found -> None
1104 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1105 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1106 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1109 let drawtiles l color =
1110 GlDraw.color color;
1111 let f col row x y tilex tiley w h =
1112 match gettileopaque l col row with
1113 | Some (opaque, _, t) ->
1114 let params = x, y, w, h, tilex, tiley in
1115 if conf.invert
1116 then (
1117 Gl.enable `blend;
1118 GlFunc.blend_func `zero `one_minus_src_color;
1120 drawtile params opaque;
1121 if conf.invert
1122 then Gl.disable `blend;
1123 if conf.debug
1124 then (
1125 let s = Printf.sprintf
1126 "%d[%d,%d] %f sec"
1127 l.pageno col row t
1129 let w = measurestr fstate.fontsize s in
1130 GlMisc.push_attrib [`current];
1131 GlDraw.color (0.0, 0.0, 0.0);
1132 GlDraw.rect
1133 (float (x-2), float (y-2))
1134 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1135 GlDraw.color (1.0, 1.0, 1.0);
1136 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1137 GlMisc.pop_attrib ();
1140 | _ ->
1141 let w =
1142 let lw = conf.winw - state.scrollw - x in
1143 min lw w
1144 and h =
1145 let lh = conf.winh - y in
1146 min lh h
1148 Gl.enable `texture_2d;
1149 begin match state.texid with
1150 | Some id ->
1151 GlTex.bind_texture `texture_2d id;
1152 let x0 = float x
1153 and y0 = float y
1154 and x1 = float (x+w)
1155 and y1 = float (y+h) in
1157 let tw = float w /. 64.0
1158 and th = float h /. 64.0 in
1159 let tx0 = float tilex /. 64.0
1160 and ty0 = float tiley /. 64.0 in
1161 let tx1 = tx0 +. tw
1162 and ty1 = ty0 +. th in
1163 GlDraw.begins `quads;
1164 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1165 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1166 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1167 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1168 GlDraw.ends ();
1170 Gl.disable `texture_2d;
1171 | None ->
1172 GlDraw.color (1.0, 1.0, 1.0);
1173 GlDraw.rect
1174 (float x, float y)
1175 (float (x+w), float (y+h));
1176 end;
1177 if w > 128 && h > fstate.fontsize + 10
1178 then (
1179 GlDraw.color (0.0, 0.0, 0.0);
1180 let c, r =
1181 if conf.verbose
1182 then (col*conf.tilew, row*conf.tileh)
1183 else col, row
1185 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1187 GlDraw.color color;
1189 itertiles l f
1192 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1194 let tilevisible1 l x y =
1195 let ax0 = l.pagex
1196 and ax1 = l.pagex + l.pagevw
1197 and ay0 = l.pagey
1198 and ay1 = l.pagey + l.pagevh in
1200 let bx0 = x
1201 and by0 = y in
1202 let bx1 = min (bx0 + conf.tilew) l.pagew
1203 and by1 = min (by0 + conf.tileh) l.pageh in
1205 let rx0 = max ax0 bx0
1206 and ry0 = max ay0 by0
1207 and rx1 = min ax1 bx1
1208 and ry1 = min ay1 by1 in
1210 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1211 nonemptyintersection
1214 let tilevisible layout n x y =
1215 let rec findpageinlayout = function
1216 | l :: _ when l.pageno = n -> tilevisible1 l x y
1217 | _ :: rest -> findpageinlayout rest
1218 | [] -> false
1220 findpageinlayout layout
1223 let tileready l x y =
1224 tilevisible1 l x y &&
1225 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1228 let tilepage n p layout =
1229 let rec loop = function
1230 | l :: rest ->
1231 if l.pageno = n
1232 then
1233 let f col row _ _ _ _ _ _ =
1234 if state.currently = Idle
1235 then
1236 match gettileopaque l col row with
1237 | Some _ -> ()
1238 | None ->
1239 let x = col*conf.tilew
1240 and y = row*conf.tileh in
1241 let w =
1242 let w = l.pagew - x in
1243 min w conf.tilew
1245 let h =
1246 let h = l.pageh - y in
1247 min h conf.tileh
1249 wcmd "tile"
1250 [`s p
1251 ;`i x
1252 ;`i y
1253 ;`i w
1254 ;`i h
1256 state.currently <-
1257 Tiling (
1258 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1259 conf.tilew, conf.tileh
1262 itertiles l f;
1263 else
1264 loop rest
1266 | [] -> ()
1268 if state.invalidated = 0 then loop layout;
1271 let preloadlayout visiblepages =
1272 let presentation = conf.presentation in
1273 let interpagespace = conf.interpagespace in
1274 let maxy = state.maxy in
1275 conf.presentation <- false;
1276 conf.interpagespace <- 0;
1277 state.maxy <- calcheight ();
1278 let y =
1279 match visiblepages with
1280 | [] -> 0
1281 | l :: _ -> getpagey l.pageno + l.pagey
1283 let y = if y < conf.winh then 0 else y - conf.winh in
1284 let h = state.y - y + conf.winh*3 in
1285 let pages = layout y h in
1286 conf.presentation <- presentation;
1287 conf.interpagespace <- interpagespace;
1288 state.maxy <- maxy;
1289 pages;
1292 let load pages =
1293 let rec loop pages =
1294 if state.currently != Idle
1295 then ()
1296 else
1297 match pages with
1298 | l :: rest ->
1299 begin match getopaque l.pageno with
1300 | None ->
1301 wcmd "page" [`i l.pageno; `i l.pagedimno];
1302 state.currently <- Loading (l, state.gen);
1303 | Some opaque ->
1304 tilepage l.pageno opaque pages;
1305 loop rest
1306 end;
1307 | _ -> ()
1309 if state.invalidated = 0 then loop pages
1312 let preload pages =
1313 load pages;
1314 if conf.preload && state.currently = Idle
1315 then load (preloadlayout pages);
1318 let layoutready layout =
1319 let rec fold all ls =
1320 all && match ls with
1321 | l :: rest ->
1322 let seen = ref false in
1323 let allvisible = ref true in
1324 let foo col row _ _ _ _ _ _ =
1325 seen := true;
1326 allvisible := !allvisible &&
1327 begin match gettileopaque l col row with
1328 | Some _ -> true
1329 | None -> false
1332 itertiles l foo;
1333 fold (!seen && !allvisible) rest
1334 | [] -> true
1336 let alltilesvisible = fold true layout in
1337 alltilesvisible;
1340 let gotoy y =
1341 let y = bound y 0 state.maxy in
1342 let y, layout, proceed =
1343 match conf.maxwait with
1344 | Some time when state.ghyll == noghyll ->
1345 begin match state.throttle with
1346 | None ->
1347 let layout = layout y conf.winh in
1348 let ready = layoutready layout in
1349 if not ready
1350 then (
1351 load layout;
1352 state.throttle <- Some (layout, y, now ());
1354 else G.postRedisplay "gotoy showall (None)";
1355 y, layout, ready
1356 | Some (_, _, started) ->
1357 let dt = now () -. started in
1358 if dt > time
1359 then (
1360 state.throttle <- None;
1361 let layout = layout y conf.winh in
1362 load layout;
1363 G.postRedisplay "maxwait";
1364 y, layout, true
1366 else -1, [], false
1369 | _ ->
1370 let layout = layout y conf.winh in
1371 if true || layoutready layout
1372 then G.postRedisplay "gotoy ready";
1373 y, layout, true
1375 if proceed
1376 then (
1377 state.y <- y;
1378 state.layout <- layout;
1379 begin match state.mode with
1380 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1381 if not (pagevisible layout pageno)
1382 then (
1383 match state.layout with
1384 | [] -> ()
1385 | l :: _ ->
1386 state.mode <- Birdseye (
1387 conf, leftx, l.pageno, hooverpageno, anchor
1390 | _ -> ()
1391 end;
1392 preload layout;
1394 state.ghyll <- noghyll;
1397 let conttiling pageno opaque =
1398 tilepage pageno opaque
1399 (if conf.preload then preloadlayout state.layout else state.layout)
1402 let gotoy_and_clear_text y =
1403 gotoy y;
1404 if not conf.verbose then state.text <- "";
1407 let getanchor () =
1408 match state.layout with
1409 | [] -> emptyanchor
1410 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1413 let getanchory (n, top) =
1414 let y, h = getpageyh n in
1415 y + (truncate (top *. float h));
1418 let gotoanchor anchor =
1419 gotoy (getanchory anchor);
1422 let addnav () =
1423 cbput state.hists.nav (getanchor ());
1426 let getnav dir =
1427 let anchor = cbgetc state.hists.nav dir in
1428 getanchory anchor;
1431 let gotoghyll y =
1432 let rec scroll f n a b =
1433 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1434 let snake f a b =
1435 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1436 if f < a
1437 then s (float f /. float a)
1438 else (
1439 if f > b
1440 then 1.0 -. s ((float (f-b) /. float (n-b)))
1441 else 1.0
1444 snake f a b
1445 and summa f n a b =
1446 (* courtesy:
1447 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1448 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1449 let iv1 = iv f in
1450 let ins = float a *. iv1
1451 and outs = float (n-b) *. iv1 in
1452 let ones = b - a in
1453 ins +. outs +. float ones
1455 let rec set (_N, _A, _B) y sy =
1456 let sum = summa 1.0 _N _A _B in
1457 let dy = float (y - sy) in
1458 state.ghyll <- (
1459 let rec gf n y1 o =
1460 if n >= _N
1461 then state.ghyll <- noghyll
1462 else
1463 let go n =
1464 let s = scroll n _N _A _B in
1465 let y1 = y1 +. ((s *. dy) /. sum) in
1466 gotoy_and_clear_text (truncate y1);
1467 state.ghyll <- gf (n+1) y1;
1469 match o with
1470 | None -> go n
1471 | Some y' -> set (_N/2, 0, 0) y' state.y
1473 gf 0 (float state.y)
1476 match conf.ghyllscroll with
1477 | None ->
1478 gotoy_and_clear_text y
1479 | Some nab ->
1480 if state.ghyll == noghyll
1481 then set nab y state.y
1482 else state.ghyll (Some y)
1485 let gotopage n top =
1486 let y, h = getpageyh n in
1487 let y = y + (truncate (top *. float h)) in
1488 gotoghyll y
1491 let gotopage1 n top =
1492 let y = getpagey n in
1493 let y = y + top in
1494 gotoghyll y
1497 let invalidate () =
1498 state.layout <- [];
1499 state.pdims <- [];
1500 state.rects <- [];
1501 state.rects1 <- [];
1502 state.invalidated <- state.invalidated + 1;
1505 let writeopen path password =
1506 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1509 let opendoc path password =
1510 invalidate ();
1511 state.path <- path;
1512 state.password <- password;
1513 state.gen <- state.gen + 1;
1514 state.docinfo <- [];
1516 setaalevel conf.aalevel;
1517 writeopen path password;
1518 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1519 wcmd "geometry" [`i state.w; `i conf.winh];
1522 let scalecolor c =
1523 let c = c *. conf.colorscale in
1524 (c, c, c);
1527 let scalecolor2 (r, g, b) =
1528 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1531 let represent () =
1532 let docolumns = function
1533 | None -> ()
1534 | Some ((columns, coverA, coverB), _) ->
1535 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1536 let rec loop pageno pdimno pdim x y rowh pdims =
1537 if pageno = state.pagecount
1538 then ()
1539 else
1540 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1541 match pdims with
1542 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1543 pdimno+1, pdim, rest
1544 | _ ->
1545 pdimno, pdim, pdims
1547 let x, y, rowh' =
1548 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1549 then (
1550 (conf.winw - state.scrollw - w) / 2,
1551 y + rowh + conf.interpagespace, h
1553 else (
1554 if (pageno - coverA) mod columns = 0
1555 then 0, y + rowh + conf.interpagespace, h
1556 else x, y, max rowh h
1559 let rec fixrow m = if m = pageno then () else
1560 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1561 if h < rowh
1562 then (
1563 let y = y + (rowh - h) / 2 in
1564 a.(m) <- (pdimno, x, y, pdim);
1566 fixrow (m+1)
1568 if pageno > 1 && (pageno - coverA) mod columns = 0
1569 then fixrow (pageno - columns);
1570 a.(pageno) <- (pdimno, x, y, pdim);
1571 let x = x + w + xoff*2 + conf.interpagespace in
1572 loop (pageno+1) pdimno pdim x y rowh' pdims
1574 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1575 conf.columns <- Some ((columns, coverA, coverB), a);
1577 docolumns conf.columns;
1578 state.maxy <- calcheight ();
1579 state.hscrollh <-
1580 if state.w <= conf.winw - state.scrollw
1581 then 0
1582 else state.scrollw
1584 match state.mode with
1585 | Birdseye (_, _, pageno, _, _) ->
1586 let y, h = getpageyh pageno in
1587 let top = (conf.winh - h) / 2 in
1588 gotoy (max 0 (y - top))
1589 | _ -> gotoanchor state.anchor
1592 let reshape =
1593 let firsttime = ref true in
1594 fun ~w ~h ->
1595 GlDraw.viewport 0 0 w h;
1596 if state.invalidated = 0 && not !firsttime
1597 then state.anchor <- getanchor ();
1599 firsttime := false;
1600 conf.winw <- w;
1601 let w = truncate (float w *. conf.zoom) - state.scrollw in
1602 let w = max w 2 in
1603 state.w <- w;
1604 conf.winh <- h;
1605 setfontsize fstate.fontsize;
1606 GlMat.mode `modelview;
1607 GlMat.load_identity ();
1609 GlMat.mode `projection;
1610 GlMat.load_identity ();
1611 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1612 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1613 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1615 let w =
1616 match conf.columns with
1617 | None -> w
1618 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1620 invalidate ();
1621 wcmd "geometry" [`i w; `i h];
1624 let enttext () =
1625 let len = String.length state.text in
1626 let drawstring s =
1627 let hscrollh =
1628 match state.mode with
1629 | View -> state.hscrollh
1630 | _ -> 0
1632 let rect x w =
1633 GlDraw.rect
1634 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1635 (x+.w, float (conf.winh - hscrollh))
1638 let w = float (conf.winw - state.scrollw - 1) in
1639 if state.progress >= 0.0 && state.progress < 1.0
1640 then (
1641 GlDraw.color (0.3, 0.3, 0.3);
1642 let w1 = w *. state.progress in
1643 rect 0.0 w1;
1644 GlDraw.color (0.0, 0.0, 0.0);
1645 rect w1 (w-.w1)
1647 else (
1648 GlDraw.color (0.0, 0.0, 0.0);
1649 rect 0.0 w;
1652 GlDraw.color (1.0, 1.0, 1.0);
1653 drawstring fstate.fontsize
1654 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1656 let s =
1657 match state.mode with
1658 | Textentry ((prefix, text, _, _, _), _) ->
1659 let s =
1660 if len > 0
1661 then
1662 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1663 else
1664 Printf.sprintf "%s%s_" prefix text
1668 | _ -> state.text
1670 let s =
1671 if state.newerrmsgs
1672 then (
1673 if not (istextentry state.mode)
1674 then
1675 let s1 = "(press 'e' to review error messasges)" in
1676 if String.length s > 0 then s ^ " " ^ s1 else s1
1677 else s
1679 else s
1681 if String.length s > 0
1682 then drawstring s
1685 let showtext c s =
1686 state.text <- Printf.sprintf "%c%s" c s;
1687 G.postRedisplay "showtext";
1690 let gctiles () =
1691 let len = Queue.length state.tilelru in
1692 let rec loop qpos =
1693 if state.memused <= conf.memlimit
1694 then ()
1695 else (
1696 if qpos < len
1697 then
1698 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1699 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1700 let (_, pw, ph, _) = getpagedim n in
1702 gen = state.gen
1703 && colorspace = conf.colorspace
1704 && angle = conf.angle
1705 && pagew = pw
1706 && pageh = ph
1707 && (
1708 let layout =
1709 match state.throttle with
1710 | None ->
1711 if conf.preload
1712 then preloadlayout state.layout
1713 else state.layout
1714 | Some (layout, _, _) ->
1715 layout
1717 let x = col*conf.tilew
1718 and y = row*conf.tileh in
1719 tilevisible layout n x y
1721 then Queue.push lruitem state.tilelru
1722 else (
1723 wcmd "freetile" [`s p];
1724 state.memused <- state.memused - s;
1725 state.uioh#infochanged Memused;
1726 Hashtbl.remove state.tilemap k;
1728 loop (qpos+1)
1731 loop 0
1734 let flushtiles () =
1735 Queue.iter (fun (k, p, s) ->
1736 wcmd "freetile" [`s p];
1737 state.memused <- state.memused - s;
1738 state.uioh#infochanged Memused;
1739 Hashtbl.remove state.tilemap k;
1740 ) state.tilelru;
1741 Queue.clear state.tilelru;
1742 load state.layout;
1745 let logcurrently = function
1746 | Idle -> dolog "Idle"
1747 | Loading (l, gen) ->
1748 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1749 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1750 dolog
1751 "Tiling %d[%d,%d] page=%s cs=%s angle"
1752 l.pageno col row pageopaque
1753 (colorspace_to_string colorspace)
1755 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1756 angle gen conf.angle state.gen
1757 tilew tileh
1758 conf.tilew conf.tileh
1760 | Outlining _ ->
1761 dolog "outlining"
1764 let act cmds =
1765 (* dolog "%S" cmds; *)
1766 let op, args =
1767 let spacepos =
1768 try String.index cmds ' '
1769 with Not_found -> -1
1771 if spacepos = -1
1772 then cmds, ""
1773 else
1774 let l = String.length cmds in
1775 let op = String.sub cmds 0 spacepos in
1776 op, begin
1777 if l - spacepos < 2 then ""
1778 else String.sub cmds (spacepos+1) (l-spacepos-1)
1781 match op with
1782 | "clear" ->
1783 state.uioh#infochanged Pdim;
1784 state.pdims <- [];
1786 | "clearrects" ->
1787 state.rects <- state.rects1;
1788 G.postRedisplay "clearrects";
1790 | "continue" ->
1791 let n =
1792 try Scanf.sscanf args "%u" (fun n -> n)
1793 with exn ->
1794 dolog "error processing 'continue' %S: %s"
1795 cmds (Printexc.to_string exn);
1796 exit 1;
1798 state.pagecount <- n;
1799 state.invalidated <- state.invalidated - 1;
1800 begin match state.currently with
1801 | Outlining l ->
1802 state.currently <- Idle;
1803 state.outlines <- Array.of_list (List.rev l)
1804 | _ -> ()
1805 end;
1806 if state.invalidated = 0
1807 then represent ();
1808 if conf.maxwait = None
1809 then G.postRedisplay "continue";
1811 | "title" ->
1812 Glut.setWindowTitle args
1814 | "msg" ->
1815 showtext ' ' args
1817 | "vmsg" ->
1818 if conf.verbose
1819 then showtext ' ' args
1821 | "progress" ->
1822 let progress, text =
1824 Scanf.sscanf args "%f %n"
1825 (fun f pos ->
1826 f, String.sub args pos (String.length args - pos))
1827 with exn ->
1828 dolog "error processing 'progress' %S: %s"
1829 cmds (Printexc.to_string exn);
1830 exit 1;
1832 state.text <- text;
1833 state.progress <- progress;
1834 G.postRedisplay "progress"
1836 | "firstmatch" ->
1837 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1839 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1840 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1841 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1842 with exn ->
1843 dolog "error processing 'firstmatch' %S: %s"
1844 cmds (Printexc.to_string exn);
1845 exit 1;
1847 let y = (getpagey pageno) + truncate y0 in
1848 addnav ();
1849 gotoy y;
1850 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1852 | "match" ->
1853 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1855 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1856 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1857 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1858 with exn ->
1859 dolog "error processing 'match' %S: %s"
1860 cmds (Printexc.to_string exn);
1861 exit 1;
1863 state.rects1 <-
1864 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1866 | "page" ->
1867 let pageopaque, t =
1869 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1870 with exn ->
1871 dolog "error processing 'page' %S: %s"
1872 cmds (Printexc.to_string exn);
1873 exit 1;
1875 begin match state.currently with
1876 | Loading (l, gen) ->
1877 vlog "page %d took %f sec" l.pageno t;
1878 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1879 begin match state.throttle with
1880 | None ->
1881 let preloadedpages =
1882 if conf.preload
1883 then preloadlayout state.layout
1884 else state.layout
1886 let evict () =
1887 let module IntSet =
1888 Set.Make (struct type t = int let compare = (-) end) in
1889 let set =
1890 List.fold_left (fun s l -> IntSet.add l.pageno s)
1891 IntSet.empty preloadedpages
1893 let evictedpages =
1894 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1895 if not (IntSet.mem pageno set)
1896 then (
1897 wcmd "freepage" [`s opaque];
1898 key :: accu
1900 else accu
1901 ) state.pagemap []
1903 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1905 evict ();
1906 state.currently <- Idle;
1907 if gen = state.gen
1908 then (
1909 tilepage l.pageno pageopaque state.layout;
1910 load state.layout;
1911 load preloadedpages;
1912 if pagevisible state.layout l.pageno
1913 && layoutready state.layout
1914 then G.postRedisplay "page";
1917 | Some (layout, _, _) ->
1918 state.currently <- Idle;
1919 tilepage l.pageno pageopaque layout;
1920 load state.layout
1921 end;
1923 | _ ->
1924 dolog "Inconsistent loading state";
1925 logcurrently state.currently;
1926 raise Quit;
1929 | "tile" ->
1930 let (x, y, opaque, size, t) =
1932 Scanf.sscanf args "%u %u %s %u %f"
1933 (fun x y p size t -> (x, y, p, size, t))
1934 with exn ->
1935 dolog "error processing 'tile' %S: %s"
1936 cmds (Printexc.to_string exn);
1937 exit 1;
1939 begin match state.currently with
1940 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1941 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1943 if tilew != conf.tilew || tileh != conf.tileh
1944 then (
1945 wcmd "freetile" [`s opaque];
1946 state.currently <- Idle;
1947 load state.layout;
1949 else (
1950 puttileopaque l col row gen cs angle opaque size t;
1951 state.memused <- state.memused + size;
1952 state.uioh#infochanged Memused;
1953 gctiles ();
1954 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1955 opaque, size) state.tilelru;
1957 let layout =
1958 match state.throttle with
1959 | None -> state.layout
1960 | Some (layout, _, _) -> layout
1963 state.currently <- Idle;
1964 if gen = state.gen
1965 && conf.colorspace = cs
1966 && conf.angle = angle
1967 && tilevisible layout l.pageno x y
1968 then conttiling l.pageno pageopaque;
1970 begin match state.throttle with
1971 | None ->
1972 preload state.layout;
1973 if gen = state.gen
1974 && conf.colorspace = cs
1975 && conf.angle = angle
1976 && tilevisible state.layout l.pageno x y
1977 then G.postRedisplay "tile nothrottle";
1979 | Some (layout, y, _) ->
1980 let ready = layoutready layout in
1981 if ready
1982 then (
1983 state.y <- y;
1984 state.layout <- layout;
1985 state.throttle <- None;
1986 G.postRedisplay "throttle";
1988 else load layout;
1989 end;
1992 | _ ->
1993 dolog "Inconsistent tiling state";
1994 logcurrently state.currently;
1995 raise Quit;
1998 | "pdim" ->
1999 let pdim =
2001 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2002 with exn ->
2003 dolog "error processing 'pdim' %S: %s"
2004 cmds (Printexc.to_string exn);
2005 exit 1;
2007 state.uioh#infochanged Pdim;
2008 state.pdims <- pdim :: state.pdims
2010 | "o" ->
2011 let (l, n, t, h, pos) =
2013 Scanf.sscanf args "%u %u %d %u %n"
2014 (fun l n t h pos -> l, n, t, h, pos)
2015 with exn ->
2016 dolog "error processing 'o' %S: %s"
2017 cmds (Printexc.to_string exn);
2018 exit 1;
2020 let s = String.sub args pos (String.length args - pos) in
2021 let outline = (s, l, (n, float t /. float h)) in
2022 begin match state.currently with
2023 | Outlining outlines ->
2024 state.currently <- Outlining (outline :: outlines)
2025 | Idle ->
2026 state.currently <- Outlining [outline]
2027 | currently ->
2028 dolog "invalid outlining state";
2029 logcurrently currently
2032 | "info" ->
2033 state.docinfo <- (1, args) :: state.docinfo
2035 | "infoend" ->
2036 state.uioh#infochanged Docinfo;
2037 state.docinfo <- List.rev state.docinfo
2039 | _ ->
2040 dolog "unknown cmd `%S'" cmds
2043 let idle () =
2044 if state.deadline == nan then state.deadline <- now ();
2045 let r =
2046 match state.errfd with
2047 | None -> [state.csock]
2048 | Some fd -> [state.csock; fd]
2050 let rec loop delay =
2051 let deadline =
2052 if state.ghyll == noghyll
2053 then state.deadline
2054 else now () +. 0.02
2056 let timeout =
2057 if delay > 0.0
2058 then max 0.0 (deadline -. now ())
2059 else 0.0
2061 let r, _, _ =
2062 try Unix.select r [] [] timeout
2063 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [] ,[]
2065 begin match r with
2066 | [] ->
2067 state.ghyll None;
2068 begin match state.autoscroll with
2069 | Some step when step != 0 ->
2070 let y = state.y + step in
2071 let y =
2072 if y < 0
2073 then state.maxy
2074 else if y >= state.maxy then 0 else y
2076 gotoy y;
2077 if state.mode = View
2078 then state.text <- "";
2079 state.deadline <- state.deadline +. 0.005;
2081 | _ ->
2082 state.deadline <- state.deadline +. delay;
2083 end;
2085 | l ->
2086 let rec checkfds c = function
2087 | [] -> c
2088 | fd :: rest when fd = state.csock ->
2089 let cmd = readcmd state.csock in
2090 act cmd;
2091 checkfds true rest
2092 | fd :: rest ->
2093 let s = String.create 80 in
2094 let n = Unix.read fd s 0 80 in
2095 if conf.redirectstderr
2096 then (
2097 Buffer.add_substring state.errmsgs s 0 n;
2098 state.newerrmsgs <- true;
2099 Glut.postRedisplay ();
2101 else (
2102 prerr_string (String.sub s 0 n);
2103 flush stderr;
2105 checkfds c rest
2107 if checkfds false l
2108 then loop 0.0
2109 end;
2110 in loop 0.007
2113 let onhist cb =
2114 let rc = cb.rc in
2115 let action = function
2116 | HCprev -> cbget cb ~-1
2117 | HCnext -> cbget cb 1
2118 | HCfirst -> cbget cb ~-(cb.rc)
2119 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2120 and cancel () = cb.rc <- rc
2121 in (action, cancel)
2124 let search pattern forward =
2125 if String.length pattern > 0
2126 then
2127 let pn, py =
2128 match state.layout with
2129 | [] -> 0, 0
2130 | l :: _ ->
2131 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2133 let cmd =
2134 let b = makecmd "search"
2135 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
2137 Buffer.add_char b ',';
2138 Buffer.add_string b pattern;
2139 Buffer.add_char b '\000';
2140 Buffer.contents b;
2142 writecmd state.csock cmd;
2145 let intentry text key =
2146 let c = Char.unsafe_chr key in
2147 match c with
2148 | '0' .. '9' ->
2149 let text = addchar text c in
2150 TEcont text
2152 | _ ->
2153 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2154 TEcont text
2157 let textentry text key =
2158 let c = Char.unsafe_chr key in
2159 match c with
2160 | _ when key >= 32 && key < 127 ->
2161 let text = addchar text c in
2162 TEcont text
2164 | _ ->
2165 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
2166 TEcont text
2169 let reqlayout angle proportional =
2170 match state.throttle with
2171 | None ->
2172 if state.invalidated = 0 then state.anchor <- getanchor ();
2173 conf.angle <- angle mod 360;
2174 conf.proportional <- proportional;
2175 invalidate ();
2176 wcmd "reqlayout" [`i conf.angle; `b proportional];
2177 | _ -> ()
2180 let settrim trimmargins trimfuzz =
2181 if state.invalidated = 0 then state.anchor <- getanchor ();
2182 conf.trimmargins <- trimmargins;
2183 conf.trimfuzz <- trimfuzz;
2184 let x0, y0, x1, y1 = trimfuzz in
2185 invalidate ();
2186 wcmd "settrim" [
2187 `b conf.trimmargins;
2188 `i x0;
2189 `i y0;
2190 `i x1;
2191 `i y1;
2193 Hashtbl.iter (fun _ opaque ->
2194 wcmd "freepage" [`s opaque];
2195 ) state.pagemap;
2196 Hashtbl.clear state.pagemap;
2199 let setzoom zoom =
2200 match state.throttle with
2201 | None ->
2202 let zoom = max 0.01 zoom in
2203 if zoom <> conf.zoom
2204 then (
2205 state.prevzoom <- conf.zoom;
2206 let relx =
2207 if zoom <= 1.0
2208 then (state.x <- 0; 0.0)
2209 else float state.x /. float state.w
2211 conf.zoom <- zoom;
2212 reshape conf.winw conf.winh;
2213 if zoom > 1.0
2214 then (
2215 let x = relx *. float state.w in
2216 state.x <- truncate x;
2218 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2221 | Some (layout, y, started) ->
2222 let time =
2223 match conf.maxwait with
2224 | None -> 0.0
2225 | Some t -> t
2227 let dt = now () -. started in
2228 if dt > time
2229 then (
2230 state.y <- y;
2231 load layout;
2235 let setcolumns columns coverA coverB =
2236 if columns < 2
2237 then (
2238 conf.columns <- None;
2239 state.x <- 0;
2240 setzoom 1.0;
2242 else (
2243 conf.columns <- Some ((columns, coverA, coverB), [||]);
2244 conf.zoom <- 1.0;
2246 reshape conf.winw conf.winh;
2249 let enterbirdseye () =
2250 let zoom = float conf.thumbw /. float conf.winw in
2251 let birdseyepageno =
2252 let cy = conf.winh / 2 in
2253 let fold = function
2254 | [] -> 0
2255 | l :: rest ->
2256 let rec fold best = function
2257 | [] -> best.pageno
2258 | l :: rest ->
2259 let d = cy - (l.pagedispy + l.pagevh/2)
2260 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2261 if abs d < abs dbest
2262 then fold l rest
2263 else best.pageno
2264 in fold l rest
2266 fold state.layout
2268 state.mode <- Birdseye (
2269 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2271 conf.zoom <- zoom;
2272 conf.presentation <- false;
2273 conf.interpagespace <- 10;
2274 conf.hlinks <- false;
2275 state.x <- 0;
2276 state.mstate <- Mnone;
2277 conf.maxwait <- None;
2278 conf.columns <- (
2279 match conf.beyecolumns with
2280 | Some c ->
2281 conf.zoom <- 1.0;
2282 Some ((c, 0, 0), [||])
2283 | None -> None
2285 Glut.setCursor Glut.CURSOR_INHERIT;
2286 if conf.verbose
2287 then
2288 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2289 (100.0*.zoom)
2290 else
2291 state.text <- ""
2293 reshape conf.winw conf.winh;
2296 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2297 state.mode <- View;
2298 conf.zoom <- c.zoom;
2299 conf.presentation <- c.presentation;
2300 conf.interpagespace <- c.interpagespace;
2301 conf.maxwait <- c.maxwait;
2302 conf.hlinks <- c.hlinks;
2303 conf.beyecolumns <- (
2304 match conf.columns with
2305 | Some ((c, _, _), _) -> Some c
2306 | None -> None
2308 conf.columns <- (
2309 match c.columns with
2310 | Some (c, _) -> Some (c, [||])
2311 | None -> None
2313 state.x <- leftx;
2314 if conf.verbose
2315 then
2316 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2317 (100.0*.conf.zoom)
2319 reshape conf.winw conf.winh;
2320 state.anchor <- if goback then anchor else (pageno, 0.0);
2323 let togglebirdseye () =
2324 match state.mode with
2325 | Birdseye vals -> leavebirdseye vals true
2326 | View -> enterbirdseye ()
2327 | _ -> ()
2330 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2331 let pageno = max 0 (pageno - incr) in
2332 let rec loop = function
2333 | [] -> gotopage1 pageno 0
2334 | l :: _ when l.pageno = pageno ->
2335 if l.pagedispy >= 0 && l.pagey = 0
2336 then G.postRedisplay "upbirdseye"
2337 else gotopage1 pageno 0
2338 | _ :: rest -> loop rest
2340 loop state.layout;
2341 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2344 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2345 let pageno = min (state.pagecount - 1) (pageno + incr) in
2346 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2347 let rec loop = function
2348 | [] ->
2349 let y, h = getpageyh pageno in
2350 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2351 gotoy (clamp dy)
2352 | l :: _ when l.pageno = pageno ->
2353 if l.pagevh != l.pageh
2354 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2355 else G.postRedisplay "downbirdseye"
2356 | _ :: rest -> loop rest
2358 loop state.layout
2361 let optentry mode _ key =
2362 let btos b = if b then "on" else "off" in
2363 let c = Char.unsafe_chr key in
2364 match c with
2365 | 's' ->
2366 let ondone s =
2367 try conf.scrollstep <- int_of_string s with exc ->
2368 state.text <- Printf.sprintf "bad integer `%s': %s"
2369 s (Printexc.to_string exc)
2371 TEswitch ("scroll step: ", "", None, intentry, ondone)
2373 | 'A' ->
2374 let ondone s =
2376 conf.autoscrollstep <- int_of_string s;
2377 if state.autoscroll <> None
2378 then state.autoscroll <- Some conf.autoscrollstep
2379 with exc ->
2380 state.text <- Printf.sprintf "bad integer `%s': %s"
2381 s (Printexc.to_string exc)
2383 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2385 | 'C' ->
2386 let ondone s =
2388 let n, a, b = columns_of_string s in
2389 setcolumns n a b;
2390 with exc ->
2391 state.text <- Printf.sprintf "bad columns `%s': %s"
2392 s (Printexc.to_string exc)
2394 TEswitch ("columns: ", "", None, textentry, ondone)
2396 | 'Z' ->
2397 let ondone s =
2399 let zoom = float (int_of_string s) /. 100.0 in
2400 setzoom zoom
2401 with exc ->
2402 state.text <- Printf.sprintf "bad integer `%s': %s"
2403 s (Printexc.to_string exc)
2405 TEswitch ("zoom: ", "", None, intentry, ondone)
2407 | 't' ->
2408 let ondone s =
2410 conf.thumbw <- bound (int_of_string s) 2 4096;
2411 state.text <-
2412 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2413 begin match mode with
2414 | Birdseye beye ->
2415 leavebirdseye beye false;
2416 enterbirdseye ();
2417 | _ -> ();
2419 with exc ->
2420 state.text <- Printf.sprintf "bad integer `%s': %s"
2421 s (Printexc.to_string exc)
2423 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2425 | 'R' ->
2426 let ondone s =
2427 match try
2428 Some (int_of_string s)
2429 with exc ->
2430 state.text <- Printf.sprintf "bad integer `%s': %s"
2431 s (Printexc.to_string exc);
2432 None
2433 with
2434 | Some angle -> reqlayout angle conf.proportional
2435 | None -> ()
2437 TEswitch ("rotation: ", "", None, intentry, ondone)
2439 | 'i' ->
2440 conf.icase <- not conf.icase;
2441 TEdone ("case insensitive search " ^ (btos conf.icase))
2443 | 'p' ->
2444 conf.preload <- not conf.preload;
2445 gotoy state.y;
2446 TEdone ("preload " ^ (btos conf.preload))
2448 | 'v' ->
2449 conf.verbose <- not conf.verbose;
2450 TEdone ("verbose " ^ (btos conf.verbose))
2452 | 'd' ->
2453 conf.debug <- not conf.debug;
2454 TEdone ("debug " ^ (btos conf.debug))
2456 | 'h' ->
2457 conf.maxhfit <- not conf.maxhfit;
2458 state.maxy <-
2459 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2460 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2462 | 'c' ->
2463 conf.crophack <- not conf.crophack;
2464 TEdone ("crophack " ^ btos conf.crophack)
2466 | 'a' ->
2467 let s =
2468 match conf.maxwait with
2469 | None ->
2470 conf.maxwait <- Some infinity;
2471 "always wait for page to complete"
2472 | Some _ ->
2473 conf.maxwait <- None;
2474 "show placeholder if page is not ready"
2476 TEdone s
2478 | 'f' ->
2479 conf.underinfo <- not conf.underinfo;
2480 TEdone ("underinfo " ^ btos conf.underinfo)
2482 | 'P' ->
2483 conf.savebmarks <- not conf.savebmarks;
2484 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2486 | 'S' ->
2487 let ondone s =
2489 let pageno, py =
2490 match state.layout with
2491 | [] -> 0, 0
2492 | l :: _ ->
2493 l.pageno, l.pagey
2495 conf.interpagespace <- int_of_string s;
2496 state.maxy <- calcheight ();
2497 let y = getpagey pageno in
2498 gotoy (y + py)
2499 with exc ->
2500 state.text <- Printf.sprintf "bad integer `%s': %s"
2501 s (Printexc.to_string exc)
2503 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2505 | 'l' ->
2506 reqlayout conf.angle (not conf.proportional);
2507 TEdone ("proportional display " ^ btos conf.proportional)
2509 | 'T' ->
2510 settrim (not conf.trimmargins) conf.trimfuzz;
2511 TEdone ("trim margins " ^ btos conf.trimmargins)
2513 | 'I' ->
2514 conf.invert <- not conf.invert;
2515 TEdone ("invert colors " ^ btos conf.invert)
2517 | 'x' ->
2518 let ondone s =
2519 cbput state.hists.sel s;
2520 conf.selcmd <- s;
2522 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2523 textentry, ondone)
2525 | _ ->
2526 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2527 TEstop
2530 class type lvsource = object
2531 method getitemcount : int
2532 method getitem : int -> (string * int)
2533 method hasaction : int -> bool
2534 method exit :
2535 uioh:uioh ->
2536 cancel:bool ->
2537 active:int ->
2538 first:int ->
2539 pan:int ->
2540 qsearch:string ->
2541 uioh option
2542 method getactive : int
2543 method getfirst : int
2544 method getqsearch : string
2545 method setqsearch : string -> unit
2546 method getpan : int
2547 end;;
2549 class virtual lvsourcebase = object
2550 val mutable m_active = 0
2551 val mutable m_first = 0
2552 val mutable m_qsearch = ""
2553 val mutable m_pan = 0
2554 method getactive = m_active
2555 method getfirst = m_first
2556 method getqsearch = m_qsearch
2557 method getpan = m_pan
2558 method setqsearch s = m_qsearch <- s
2559 end;;
2561 let textentryspecial key = function
2562 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2563 let s =
2564 match key with
2565 | Glut.KEY_UP -> action HCprev
2566 | Glut.KEY_DOWN -> action HCnext
2567 | Glut.KEY_HOME -> action HCfirst
2568 | Glut.KEY_END -> action HClast
2569 | _ -> state.text
2571 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2572 G.postRedisplay "special textentry";
2573 | _ -> ()
2576 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2577 let enttext te =
2578 state.mode <- Textentry (te, onleave);
2579 state.text <- "";
2580 enttext ();
2581 G.postRedisplay "textentrykeyboard enttext";
2583 match Char.unsafe_chr key with
2584 | '\008' -> (* backspace *)
2585 let len = String.length text in
2586 if len = 0
2587 then (
2588 onleave Cancel;
2589 G.postRedisplay "textentrykeyboard after cancel";
2591 else (
2592 let s = String.sub text 0 (len - 1) in
2593 enttext (c, s, opthist, onkey, ondone)
2596 | '\r' | '\n' ->
2597 ondone text;
2598 onleave Confirm;
2599 G.postRedisplay "textentrykeyboard after confirm"
2601 | '\007' (* ctrl-g *)
2602 | '\027' -> (* escape *)
2603 if String.length text = 0
2604 then (
2605 begin match opthist with
2606 | None -> ()
2607 | Some (_, onhistcancel) -> onhistcancel ()
2608 end;
2609 onleave Cancel;
2610 state.text <- "";
2611 G.postRedisplay "textentrykeyboard after cancel2"
2613 else (
2614 enttext (c, "", opthist, onkey, ondone)
2617 | '\127' -> () (* delete *)
2619 | _ ->
2620 begin match onkey text key with
2621 | TEdone text ->
2622 ondone text;
2623 onleave Confirm;
2624 G.postRedisplay "textentrykeyboard after confirm2";
2626 | TEcont text ->
2627 enttext (c, text, opthist, onkey, ondone);
2629 | TEstop ->
2630 onleave Cancel;
2631 G.postRedisplay "textentrykeyboard after cancel3"
2633 | TEswitch te ->
2634 state.mode <- Textentry (te, onleave);
2635 G.postRedisplay "textentrykeyboard switch";
2636 end;
2639 let firstof first active =
2640 if first > active || abs (first - active) > fstate.maxrows - 1
2641 then max 0 (active - (fstate.maxrows/2))
2642 else first
2645 let calcfirst first active =
2646 if active > first
2647 then
2648 let rows = active - first in
2649 if rows > fstate.maxrows then active - fstate.maxrows else first
2650 else active
2653 let scrollph y maxy =
2654 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2655 let sh = float conf.winh /. sh in
2656 let sh = max sh (float conf.scrollh) in
2658 let percent =
2659 if y = state.maxy
2660 then 1.0
2661 else float y /. float maxy
2663 let position = (float conf.winh -. sh) *. percent in
2665 let position =
2666 if position +. sh > float conf.winh
2667 then float conf.winh -. sh
2668 else position
2670 position, sh;
2673 let coe s = (s :> uioh);;
2675 class listview ~(source:lvsource) ~trusted =
2676 object (self)
2677 val m_pan = source#getpan
2678 val m_first = source#getfirst
2679 val m_active = source#getactive
2680 val m_qsearch = source#getqsearch
2681 val m_prev_uioh = state.uioh
2683 method private elemunder y =
2684 let n = y / (fstate.fontsize+1) in
2685 if m_first + n < source#getitemcount
2686 then (
2687 if source#hasaction (m_first + n)
2688 then Some (m_first + n)
2689 else None
2691 else None
2693 method display =
2694 Gl.enable `blend;
2695 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2696 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2697 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2698 GlDraw.color (1., 1., 1.);
2699 Gl.enable `texture_2d;
2700 let fs = fstate.fontsize in
2701 let nfs = fs + 1 in
2702 let ww = fstate.wwidth in
2703 let tabw = 30.0*.ww in
2704 let itemcount = source#getitemcount in
2705 let rec loop row =
2706 if (row - m_first) * nfs > conf.winh
2707 then ()
2708 else (
2709 if row >= 0 && row < itemcount
2710 then (
2711 let (s, level) = source#getitem row in
2712 let y = (row - m_first) * nfs in
2713 let x = 5.0 +. float (level + m_pan) *. ww in
2714 if row = m_active
2715 then (
2716 Gl.disable `texture_2d;
2717 GlDraw.polygon_mode `both `line;
2718 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2719 GlDraw.rect (1., float (y + 1))
2720 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2721 GlDraw.polygon_mode `both `fill;
2722 GlDraw.color (1., 1., 1.);
2723 Gl.enable `texture_2d;
2726 let drawtabularstring s =
2727 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2728 if trusted
2729 then
2730 let tabpos = try String.index s '\t' with Not_found -> -1 in
2731 if tabpos > 0
2732 then
2733 let len = String.length s - tabpos - 1 in
2734 let s1 = String.sub s 0 tabpos
2735 and s2 = String.sub s (tabpos + 1) len in
2736 let nx = drawstr x s1 in
2737 let sw = nx -. x in
2738 let x = x +. (max tabw sw) in
2739 drawstr x s2
2740 else
2741 drawstr x s
2742 else
2743 drawstr x s
2745 let _ = drawtabularstring s in
2746 loop (row+1)
2750 loop m_first;
2751 Gl.disable `blend;
2752 Gl.disable `texture_2d;
2754 method updownlevel incr =
2755 let len = source#getitemcount in
2756 let curlevel =
2757 if m_active >= 0 && m_active < len
2758 then snd (source#getitem m_active)
2759 else -1
2761 let rec flow i =
2762 if i = len then i-1 else if i = -1 then 0 else
2763 let _, l = source#getitem i in
2764 if l != curlevel then i else flow (i+incr)
2766 let active = flow m_active in
2767 let first = calcfirst m_first active in
2768 G.postRedisplay "special outline updownlevel";
2769 {< m_active = active; m_first = first >}
2771 method private key1 key =
2772 let set active first qsearch =
2773 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2775 let search active pattern incr =
2776 let dosearch re =
2777 let rec loop n =
2778 if n >= 0 && n < source#getitemcount
2779 then (
2780 let s, _ = source#getitem n in
2782 (try ignore (Str.search_forward re s 0); true
2783 with Not_found -> false)
2784 then Some n
2785 else loop (n + incr)
2787 else None
2789 loop active
2792 let re = Str.regexp_case_fold pattern in
2793 dosearch re
2794 with Failure s ->
2795 state.text <- s;
2796 None
2798 match key with
2799 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2800 let incr = if key = 18 then -1 else 1 in
2801 let active, first =
2802 match search (m_active + incr) m_qsearch incr with
2803 | None ->
2804 state.text <- m_qsearch ^ " [not found]";
2805 m_active, m_first
2806 | Some active ->
2807 state.text <- m_qsearch;
2808 active, firstof m_first active
2810 G.postRedisplay "listview ctrl-r/s";
2811 set active first m_qsearch;
2813 | 8 -> (* backspace *)
2814 let len = String.length m_qsearch in
2815 if len = 0
2816 then coe self
2817 else (
2818 if len = 1
2819 then (
2820 state.text <- "";
2821 G.postRedisplay "listview empty qsearch";
2822 set m_active m_first "";
2824 else
2825 let qsearch = String.sub m_qsearch 0 (len - 1) in
2826 let active, first =
2827 match search m_active qsearch ~-1 with
2828 | None ->
2829 state.text <- qsearch ^ " [not found]";
2830 m_active, m_first
2831 | Some active ->
2832 state.text <- qsearch;
2833 active, firstof m_first active
2835 G.postRedisplay "listview backspace qsearch";
2836 set active first qsearch
2839 | _ when key >= 32 && key < 127 ->
2840 let pattern = addchar m_qsearch (Char.chr key) in
2841 let active, first =
2842 match search m_active pattern 1 with
2843 | None ->
2844 state.text <- pattern ^ " [not found]";
2845 m_active, m_first
2846 | Some active ->
2847 state.text <- pattern;
2848 active, firstof m_first active
2850 G.postRedisplay "listview qsearch add";
2851 set active first pattern;
2853 | 27 -> (* escape *)
2854 state.text <- "";
2855 if String.length m_qsearch = 0
2856 then (
2857 G.postRedisplay "list view escape";
2858 begin
2859 match
2860 source#exit (coe self) true m_active m_first m_pan m_qsearch
2861 with
2862 | None -> m_prev_uioh
2863 | Some uioh -> uioh
2866 else (
2867 G.postRedisplay "list view kill qsearch";
2868 source#setqsearch "";
2869 coe {< m_qsearch = "" >}
2872 | 13 -> (* enter *)
2873 state.text <- "";
2874 let self = {< m_qsearch = "" >} in
2875 source#setqsearch "";
2876 let opt =
2877 G.postRedisplay "listview enter";
2878 if m_active >= 0 && m_active < source#getitemcount
2879 then (
2880 source#exit (coe self) false m_active m_first m_pan "";
2882 else (
2883 source#exit (coe self) true m_active m_first m_pan "";
2886 begin match opt with
2887 | None -> m_prev_uioh
2888 | Some uioh -> uioh
2891 | 127 -> (* delete *)
2892 coe self
2894 | _ -> dolog "unknown key %d" key; coe self
2896 method private special1 key =
2897 let itemcount = source#getitemcount in
2898 let find start incr =
2899 let rec find i =
2900 if i = -1 || i = itemcount
2901 then -1
2902 else (
2903 if source#hasaction i
2904 then i
2905 else find (i + incr)
2908 find start
2910 let set active first =
2911 let first = bound first 0 (itemcount - fstate.maxrows) in
2912 state.text <- "";
2913 coe {< m_active = active; m_first = first >}
2915 let navigate incr =
2916 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2917 let active, first =
2918 let incr1 = if incr > 0 then 1 else -1 in
2919 if isvisible m_first m_active
2920 then
2921 let next =
2922 let next = m_active + incr in
2923 let next =
2924 if next < 0 || next >= itemcount
2925 then -1
2926 else find next incr1
2928 if next = -1 || abs (m_active - next) > fstate.maxrows
2929 then -1
2930 else next
2932 if next = -1
2933 then
2934 let first = m_first + incr in
2935 let first = bound first 0 (itemcount - 1) in
2936 let next =
2937 let next = m_active + incr in
2938 let next = bound next 0 (itemcount - 1) in
2939 find next ~-incr1
2941 let active = if next = -1 then m_active else next in
2942 active, first
2943 else
2944 let first = min next m_first in
2945 let first =
2946 if abs (next - first) > fstate.maxrows
2947 then first + incr
2948 else first
2950 next, first
2951 else
2952 let first = m_first + incr in
2953 let first = bound first 0 (itemcount - 1) in
2954 let active =
2955 let next = m_active + incr in
2956 let next = bound next 0 (itemcount - 1) in
2957 let next = find next incr1 in
2958 let active =
2959 if next = -1 || abs (m_active - first) > fstate.maxrows
2960 then (
2961 let active = if m_active = -1 then next else m_active in
2962 active
2964 else next
2966 if isvisible first active
2967 then active
2968 else -1
2970 active, first
2972 G.postRedisplay "listview navigate";
2973 set active first;
2975 begin match key with
2976 | Glut.KEY_UP -> navigate ~-1
2977 | Glut.KEY_DOWN -> navigate 1
2978 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2979 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2981 | Glut.KEY_RIGHT ->
2982 state.text <- "";
2983 G.postRedisplay "listview right";
2984 coe {< m_pan = m_pan - 1 >}
2986 | Glut.KEY_LEFT ->
2987 state.text <- "";
2988 G.postRedisplay "listview left";
2989 coe {< m_pan = m_pan + 1 >}
2991 | Glut.KEY_HOME ->
2992 let active = find 0 1 in
2993 G.postRedisplay "listview home";
2994 set active 0;
2996 | Glut.KEY_END ->
2997 let first = max 0 (itemcount - fstate.maxrows) in
2998 let active = find (itemcount - 1) ~-1 in
2999 G.postRedisplay "listview end";
3000 set active first;
3002 | _ -> coe self
3003 end;
3005 method key key =
3006 match state.mode with
3007 | Textentry te -> textentrykeyboard key te; coe self
3008 | _ -> self#key1 key
3010 method special key =
3011 match state.mode with
3012 | Textentry te -> textentryspecial key te; coe self
3013 | _ -> self#special1 key
3015 method button button bstate x y =
3016 let opt =
3017 match button with
3018 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollbw ->
3019 G.postRedisplay "listview scroll";
3020 if bstate = Glut.DOWN
3021 then
3022 let _, position, sh = self#scrollph in
3023 if y > truncate position && y < truncate (position +. sh)
3024 then (
3025 state.mstate <- Mscrolly;
3026 Some (coe self)
3028 else
3029 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3030 let first = truncate (s *. float source#getitemcount) in
3031 let first = min source#getitemcount first in
3032 Some (coe {< m_first = first; m_active = first >})
3033 else (
3034 state.mstate <- Mnone;
3035 Some (coe self);
3037 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3038 begin match self#elemunder y with
3039 | Some n ->
3040 G.postRedisplay "listview click";
3041 source#exit
3042 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3043 | _ ->
3044 Some (coe self)
3046 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
3047 let len = source#getitemcount in
3048 let first =
3049 if n = 4 && m_first + fstate.maxrows >= len
3050 then
3051 m_first
3052 else
3053 let first = m_first + (if n == 3 then -1 else 1) in
3054 bound first 0 (len - 1)
3056 G.postRedisplay "listview wheel";
3057 Some (coe {< m_first = first >})
3058 | _ ->
3059 Some (coe self)
3061 match opt with
3062 | None -> m_prev_uioh
3063 | Some uioh -> uioh
3065 method motion _ y =
3066 match state.mstate with
3067 | Mscrolly ->
3068 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3069 let first = truncate (s *. float source#getitemcount) in
3070 let first = min source#getitemcount first in
3071 G.postRedisplay "listview motion";
3072 coe {< m_first = first; m_active = first >}
3073 | _ -> coe self
3075 method pmotion x y =
3076 if x < conf.winw - conf.scrollbw
3077 then
3078 let n =
3079 match self#elemunder y with
3080 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
3081 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
3083 let o =
3084 if n != m_active
3085 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3086 else self
3088 coe o
3089 else (
3090 Glut.setCursor Glut.CURSOR_INHERIT;
3091 coe self
3094 method infochanged _ = ()
3096 method scrollpw = (0, 0.0, 0.0)
3097 method scrollph =
3098 let nfs = fstate.fontsize + 1 in
3099 let y = m_first * nfs in
3100 let itemcount = source#getitemcount in
3101 let maxi = max 0 (itemcount - fstate.maxrows) in
3102 let maxy = maxi * nfs in
3103 let p, h = scrollph y maxy in
3104 conf.scrollbw, p, h
3105 end;;
3107 class outlinelistview ~source =
3108 object (self)
3109 inherit listview ~source:(source :> lvsource) ~trusted:false as super
3111 method key key =
3112 match key with
3113 | 14 -> (* ctrl-n *)
3114 source#narrow m_qsearch;
3115 G.postRedisplay "outline ctrl-n";
3116 coe {< m_first = 0; m_active = 0 >}
3118 | 21 -> (* ctrl-u *)
3119 source#denarrow;
3120 G.postRedisplay "outline ctrl-u";
3121 state.text <- "";
3122 coe {< m_first = 0; m_active = 0 >}
3124 | 12 -> (* ctrl-l *)
3125 let first = m_active - (fstate.maxrows / 2) in
3126 G.postRedisplay "outline ctrl-l";
3127 coe {< m_first = first >}
3129 | 127 -> (* delete *)
3130 source#remove m_active;
3131 G.postRedisplay "outline delete";
3132 let active = max 0 (m_active-1) in
3133 coe {< m_first = firstof m_first active;
3134 m_active = active >}
3136 | key -> super#key key
3138 method special key =
3139 let calcfirst first active =
3140 if active > first
3141 then
3142 let rows = active - first in
3143 if rows > fstate.maxrows then active - fstate.maxrows else first
3144 else active
3146 let navigate incr =
3147 let active = m_active + incr in
3148 let active = bound active 0 (source#getitemcount - 1) in
3149 let first = calcfirst m_first active in
3150 G.postRedisplay "special outline navigate";
3151 coe {< m_active = active; m_first = first >}
3153 match key with
3154 | Glut.KEY_UP -> navigate ~-1
3155 | Glut.KEY_DOWN -> navigate 1
3156 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
3157 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
3159 | Glut.KEY_RIGHT ->
3160 let o =
3161 if Glut.getModifiers () land Glut.active_ctrl != 0
3162 then (
3163 G.postRedisplay "special outline right";
3164 {< m_pan = m_pan + 1 >}
3166 else self#updownlevel 1
3168 coe o
3170 | Glut.KEY_LEFT ->
3171 let o =
3172 if Glut.getModifiers () land Glut.active_ctrl != 0
3173 then (
3174 G.postRedisplay "special outline left";
3175 {< m_pan = m_pan - 1 >}
3177 else self#updownlevel ~-1
3179 coe o
3181 | Glut.KEY_HOME ->
3182 G.postRedisplay "special outline home";
3183 coe {< m_first = 0; m_active = 0 >}
3185 | Glut.KEY_END ->
3186 let active = source#getitemcount - 1 in
3187 let first = max 0 (active - fstate.maxrows) in
3188 G.postRedisplay "special outline end";
3189 coe {< m_active = active; m_first = first >}
3191 | _ -> super#special key
3194 let outlinesource usebookmarks =
3195 let empty = [||] in
3196 (object
3197 inherit lvsourcebase
3198 val mutable m_items = empty
3199 val mutable m_orig_items = empty
3200 val mutable m_prev_items = empty
3201 val mutable m_narrow_pattern = ""
3202 val mutable m_hadremovals = false
3204 method getitemcount =
3205 Array.length m_items + (if m_hadremovals then 1 else 0)
3207 method getitem n =
3208 if n == Array.length m_items && m_hadremovals
3209 then
3210 ("[Confirm removal]", 0)
3211 else
3212 let s, n, _ = m_items.(n) in
3213 (s, n)
3215 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3216 ignore (uioh, first, qsearch);
3217 let confrimremoval = m_hadremovals && active = Array.length m_items in
3218 let items =
3219 if String.length m_narrow_pattern = 0
3220 then m_orig_items
3221 else m_items
3223 if not cancel
3224 then (
3225 if not confrimremoval
3226 then(
3227 let _, _, anchor = m_items.(active) in
3228 gotoanchor anchor;
3229 m_items <- items;
3231 else (
3232 state.bookmarks <- Array.to_list m_items;
3233 m_orig_items <- m_items;
3236 else m_items <- items;
3237 m_pan <- pan;
3238 None
3240 method hasaction _ = true
3242 method greetmsg =
3243 if Array.length m_items != Array.length m_orig_items
3244 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3245 else ""
3247 method narrow pattern =
3248 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3249 match reopt with
3250 | None -> ()
3251 | Some re ->
3252 let rec loop accu n =
3253 if n = -1
3254 then (
3255 m_narrow_pattern <- pattern;
3256 m_items <- Array.of_list accu
3258 else
3259 let (s, _, _) as o = m_items.(n) in
3260 let accu =
3261 if (try ignore (Str.search_forward re s 0); true
3262 with Not_found -> false)
3263 then o :: accu
3264 else accu
3266 loop accu (n-1)
3268 loop [] (Array.length m_items - 1)
3270 method denarrow =
3271 m_orig_items <- (
3272 if usebookmarks
3273 then Array.of_list state.bookmarks
3274 else state.outlines
3276 m_items <- m_orig_items
3278 method remove m =
3279 if usebookmarks
3280 then
3281 if m >= 0 && m < Array.length m_items
3282 then (
3283 m_hadremovals <- true;
3284 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3285 let n = if n >= m then n+1 else n in
3286 m_items.(n)
3290 method reset anchor items =
3291 m_hadremovals <- false;
3292 if m_orig_items == empty || m_prev_items != items
3293 then (
3294 m_orig_items <- items;
3295 if String.length m_narrow_pattern = 0
3296 then m_items <- items;
3298 m_prev_items <- items;
3299 let rely = getanchory anchor in
3300 let active =
3301 let rec loop n best bestd =
3302 if n = Array.length m_items
3303 then best
3304 else
3305 let (_, _, anchor) = m_items.(n) in
3306 let orely = getanchory anchor in
3307 let d = abs (orely - rely) in
3308 if d < bestd
3309 then loop (n+1) n d
3310 else loop (n+1) best bestd
3312 loop 0 ~-1 max_int
3314 m_active <- active;
3315 m_first <- firstof m_first active
3316 end)
3319 let enterselector usebookmarks =
3320 let source = outlinesource usebookmarks in
3321 fun errmsg ->
3322 let outlines =
3323 if usebookmarks
3324 then Array.of_list state.bookmarks
3325 else state.outlines
3327 if Array.length outlines = 0
3328 then (
3329 showtext ' ' errmsg;
3331 else (
3332 state.text <- source#greetmsg;
3333 Glut.setCursor Glut.CURSOR_INHERIT;
3334 let anchor = getanchor () in
3335 source#reset anchor outlines;
3336 state.uioh <- coe (new outlinelistview ~source);
3337 G.postRedisplay "enter selector";
3341 let enteroutlinemode =
3342 let f = enterselector false in
3343 fun ()-> f "Document has no outline";
3346 let enterbookmarkmode =
3347 let f = enterselector true in
3348 fun () -> f "Document has no bookmarks (yet)";
3351 let color_of_string s =
3352 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3353 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3357 let color_to_string (r, g, b) =
3358 let r = truncate (r *. 256.0)
3359 and g = truncate (g *. 256.0)
3360 and b = truncate (b *. 256.0) in
3361 Printf.sprintf "%d/%d/%d" r g b
3364 let irect_of_string s =
3365 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3368 let irect_to_string (x0,y0,x1,y1) =
3369 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3372 let makecheckers () =
3373 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3374 following to say:
3375 converted by Issac Trotts. July 25, 2002 *)
3376 let image_height = 64
3377 and image_width = 64 in
3379 let make_image () =
3380 let image =
3381 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3383 for i = 0 to image_width - 1 do
3384 for j = 0 to image_height - 1 do
3385 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3386 (if (i land 8 ) lxor (j land 8) = 0
3387 then [|255;255;255|] else [|200;200;200|])
3388 done
3389 done;
3390 image
3392 let image = make_image () in
3393 let id = GlTex.gen_texture () in
3394 GlTex.bind_texture `texture_2d id;
3395 GlPix.store (`unpack_alignment 1);
3396 GlTex.image2d image;
3397 List.iter (GlTex.parameter ~target:`texture_2d)
3398 [ `wrap_s `repeat;
3399 `wrap_t `repeat;
3400 `mag_filter `nearest;
3401 `min_filter `nearest ];
3405 let setcheckers enabled =
3406 match state.texid with
3407 | None ->
3408 if enabled then state.texid <- Some (makecheckers ())
3410 | Some texid ->
3411 if not enabled
3412 then (
3413 GlTex.delete_texture texid;
3414 state.texid <- None;
3418 let int_of_string_with_suffix s =
3419 let l = String.length s in
3420 let s1, shift =
3421 if l > 1
3422 then
3423 let suffix = Char.lowercase s.[l-1] in
3424 match suffix with
3425 | 'k' -> String.sub s 0 (l-1), 10
3426 | 'm' -> String.sub s 0 (l-1), 20
3427 | 'g' -> String.sub s 0 (l-1), 30
3428 | _ -> s, 0
3429 else s, 0
3431 let n = int_of_string s1 in
3432 let m = n lsl shift in
3433 if m < 0 || m < n
3434 then raise (Failure "value too large")
3435 else m
3438 let string_with_suffix_of_int n =
3439 if n = 0
3440 then "0"
3441 else
3442 let n, s =
3443 if n = 0
3444 then 0, ""
3445 else (
3446 if n land ((1 lsl 20) - 1) = 0
3447 then n lsr 20, "M"
3448 else (
3449 if n land ((1 lsl 10) - 1) = 0
3450 then n lsr 10, "K"
3451 else n, ""
3455 let rec loop s n =
3456 let h = n mod 1000 in
3457 let n = n / 1000 in
3458 if n = 0
3459 then string_of_int h ^ s
3460 else (
3461 let s = Printf.sprintf "_%03d%s" h s in
3462 loop s n
3465 loop "" n ^ s;
3468 let defghyllscroll = (40, 8, 32);;
3469 let ghyllscroll_of_string s =
3470 let (n, a, b) as nab =
3471 if s = "default"
3472 then defghyllscroll
3473 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3475 if n <= a || n <= b || a >= b
3476 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3477 nab;
3480 let ghyllscroll_to_string ((n, a, b) as nab) =
3481 if nab = defghyllscroll
3482 then "default"
3483 else Printf.sprintf "%d,%d,%d" n a b;
3486 let describe_location () =
3487 let f (fn, _) l =
3488 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3490 let fn, ln = List.fold_left f (-1, -1) state.layout in
3491 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3492 let percent =
3493 if maxy <= 0
3494 then 100.
3495 else (100. *. (float state.y /. float maxy))
3497 if fn = ln
3498 then
3499 Printf.sprintf "page %d of %d [%.2f%%]"
3500 (fn+1) state.pagecount percent
3501 else
3502 Printf.sprintf
3503 "pages %d-%d of %d [%.2f%%]"
3504 (fn+1) (ln+1) state.pagecount percent
3507 let enterinfomode =
3508 let btos b = if b then "\xe2\x88\x9a" else "" in
3509 let showextended = ref false in
3510 let leave mode = function
3511 | Confirm -> state.mode <- mode
3512 | Cancel -> state.mode <- mode in
3513 let src =
3514 (object
3515 val mutable m_first_time = true
3516 val mutable m_l = []
3517 val mutable m_a = [||]
3518 val mutable m_prev_uioh = nouioh
3519 val mutable m_prev_mode = View
3521 inherit lvsourcebase
3523 method reset prev_mode prev_uioh =
3524 m_a <- Array.of_list (List.rev m_l);
3525 m_l <- [];
3526 m_prev_mode <- prev_mode;
3527 m_prev_uioh <- prev_uioh;
3528 if m_first_time
3529 then (
3530 let rec loop n =
3531 if n >= Array.length m_a
3532 then ()
3533 else
3534 match m_a.(n) with
3535 | _, _, _, Action _ -> m_active <- n
3536 | _ -> loop (n+1)
3538 loop 0;
3539 m_first_time <- false;
3542 method int name get set =
3543 m_l <-
3544 (name, `int get, 1, Action (
3545 fun u ->
3546 let ondone s =
3547 try set (int_of_string s)
3548 with exn ->
3549 state.text <- Printf.sprintf "bad integer `%s': %s"
3550 s (Printexc.to_string exn)
3552 state.text <- "";
3553 let te = name ^ ": ", "", None, intentry, ondone in
3554 state.mode <- Textentry (te, leave m_prev_mode);
3556 )) :: m_l
3558 method int_with_suffix name get set =
3559 m_l <-
3560 (name, `intws get, 1, Action (
3561 fun u ->
3562 let ondone s =
3563 try set (int_of_string_with_suffix s)
3564 with exn ->
3565 state.text <- Printf.sprintf "bad integer `%s': %s"
3566 s (Printexc.to_string exn)
3568 state.text <- "";
3569 let te =
3570 name ^ ": ", "", None, intentry_with_suffix, ondone
3572 state.mode <- Textentry (te, leave m_prev_mode);
3574 )) :: m_l
3576 method bool ?(offset=1) ?(btos=btos) name get set =
3577 m_l <-
3578 (name, `bool (btos, get), offset, Action (
3579 fun u ->
3580 let v = get () in
3581 set (not v);
3583 )) :: m_l
3585 method color name get set =
3586 m_l <-
3587 (name, `color get, 1, Action (
3588 fun u ->
3589 let invalid = (nan, nan, nan) in
3590 let ondone s =
3591 let c =
3592 try color_of_string s
3593 with exn ->
3594 state.text <- Printf.sprintf "bad color `%s': %s"
3595 s (Printexc.to_string exn);
3596 invalid
3598 if c <> invalid
3599 then set c;
3601 let te = name ^ ": ", "", None, textentry, ondone in
3602 state.text <- color_to_string (get ());
3603 state.mode <- Textentry (te, leave m_prev_mode);
3605 )) :: m_l
3607 method string name get set =
3608 m_l <-
3609 (name, `string get, 1, Action (
3610 fun u ->
3611 let ondone s = set s in
3612 let te = name ^ ": ", "", None, textentry, ondone in
3613 state.mode <- Textentry (te, leave m_prev_mode);
3615 )) :: m_l
3617 method colorspace name get set =
3618 m_l <-
3619 (name, `string get, 1, Action (
3620 fun _ ->
3621 let source =
3622 let vals = [| "rgb"; "bgr"; "gray" |] in
3623 (object
3624 inherit lvsourcebase
3626 initializer
3627 m_active <- int_of_colorspace conf.colorspace;
3628 m_first <- 0;
3630 method getitemcount = Array.length vals
3631 method getitem n = (vals.(n), 0)
3632 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3633 ignore (uioh, first, pan, qsearch);
3634 if not cancel then set active;
3635 None
3636 method hasaction _ = true
3637 end)
3639 state.text <- "";
3640 coe (new listview ~source ~trusted:true)
3641 )) :: m_l
3643 method caption s offset =
3644 m_l <- (s, `empty, offset, Noaction) :: m_l
3646 method caption2 s f offset =
3647 m_l <- (s, `string f, offset, Noaction) :: m_l
3649 method getitemcount = Array.length m_a
3651 method getitem n =
3652 let tostr = function
3653 | `int f -> string_of_int (f ())
3654 | `intws f -> string_with_suffix_of_int (f ())
3655 | `string f -> f ()
3656 | `color f -> color_to_string (f ())
3657 | `bool (btos, f) -> btos (f ())
3658 | `empty -> ""
3660 let name, t, offset, _ = m_a.(n) in
3661 ((let s = tostr t in
3662 if String.length s > 0
3663 then Printf.sprintf "%s\t%s" name s
3664 else name),
3665 offset)
3667 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3668 let uiohopt =
3669 if not cancel
3670 then (
3671 m_qsearch <- qsearch;
3672 let uioh =
3673 match m_a.(active) with
3674 | _, _, _, Action f -> f uioh
3675 | _ -> uioh
3677 Some uioh
3679 else None
3681 m_active <- active;
3682 m_first <- first;
3683 m_pan <- pan;
3684 uiohopt
3686 method hasaction n =
3687 match m_a.(n) with
3688 | _, _, _, Action _ -> true
3689 | _ -> false
3690 end)
3692 let rec fillsrc prevmode prevuioh =
3693 let sep () = src#caption "" 0 in
3694 let colorp name get set =
3695 src#string name
3696 (fun () -> color_to_string (get ()))
3697 (fun v ->
3699 let c = color_of_string v in
3700 set c
3701 with exn ->
3702 state.text <- Printf.sprintf "bad color `%s': %s"
3703 v (Printexc.to_string exn);
3706 let oldmode = state.mode in
3707 let birdseye = isbirdseye state.mode in
3709 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3711 src#bool "presentation mode"
3712 (fun () -> conf.presentation)
3713 (fun v ->
3714 conf.presentation <- v;
3715 state.anchor <- getanchor ();
3716 represent ());
3718 src#bool "ignore case in searches"
3719 (fun () -> conf.icase)
3720 (fun v -> conf.icase <- v);
3722 src#bool "preload"
3723 (fun () -> conf.preload)
3724 (fun v -> conf.preload <- v);
3726 src#bool "highlight links"
3727 (fun () -> conf.hlinks)
3728 (fun v -> conf.hlinks <- v);
3730 src#bool "under info"
3731 (fun () -> conf.underinfo)
3732 (fun v -> conf.underinfo <- v);
3734 src#bool "persistent bookmarks"
3735 (fun () -> conf.savebmarks)
3736 (fun v -> conf.savebmarks <- v);
3738 src#bool "proportional display"
3739 (fun () -> conf.proportional)
3740 (fun v -> reqlayout conf.angle v);
3742 src#bool "trim margins"
3743 (fun () -> conf.trimmargins)
3744 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3746 src#bool "persistent location"
3747 (fun () -> conf.jumpback)
3748 (fun v -> conf.jumpback <- v);
3750 sep ();
3751 src#int "inter-page space"
3752 (fun () -> conf.interpagespace)
3753 (fun n ->
3754 conf.interpagespace <- n;
3755 let pageno, py =
3756 match state.layout with
3757 | [] -> 0, 0
3758 | l :: _ ->
3759 l.pageno, l.pagey
3761 state.maxy <- calcheight ();
3762 let y = getpagey pageno in
3763 gotoy (y + py)
3766 src#int "page bias"
3767 (fun () -> conf.pagebias)
3768 (fun v -> conf.pagebias <- v);
3770 src#int "scroll step"
3771 (fun () -> conf.scrollstep)
3772 (fun n -> conf.scrollstep <- n);
3774 src#int "auto scroll step"
3775 (fun () ->
3776 match state.autoscroll with
3777 | Some step -> step
3778 | _ -> conf.autoscrollstep)
3779 (fun n ->
3780 if state.autoscroll <> None
3781 then state.autoscroll <- Some n;
3782 conf.autoscrollstep <- n);
3784 src#int "zoom"
3785 (fun () -> truncate (conf.zoom *. 100.))
3786 (fun v -> setzoom ((float v) /. 100.));
3788 src#int "rotation"
3789 (fun () -> conf.angle)
3790 (fun v -> reqlayout v conf.proportional);
3792 src#int "scroll bar width"
3793 (fun () -> state.scrollw)
3794 (fun v ->
3795 state.scrollw <- v;
3796 conf.scrollbw <- v;
3797 reshape conf.winw conf.winh;
3800 src#int "scroll handle height"
3801 (fun () -> conf.scrollh)
3802 (fun v -> conf.scrollh <- v;);
3804 src#int "thumbnail width"
3805 (fun () -> conf.thumbw)
3806 (fun v ->
3807 conf.thumbw <- min 4096 v;
3808 match oldmode with
3809 | Birdseye beye ->
3810 leavebirdseye beye false;
3811 enterbirdseye ()
3812 | _ -> ()
3815 src#string "columns"
3816 (fun () ->
3817 match conf.columns with
3818 | None -> "1"
3819 | Some (multicol, _) -> columns_to_string multicol)
3820 (fun v ->
3821 let n, a, b = columns_of_string v in
3822 setcolumns n a b);
3824 sep ();
3825 src#caption "Presentation mode" 0;
3826 src#bool "scrollbar visible"
3827 (fun () -> conf.scrollbarinpm)
3828 (fun v ->
3829 if v != conf.scrollbarinpm
3830 then (
3831 conf.scrollbarinpm <- v;
3832 if conf.presentation
3833 then (
3834 state.scrollw <- if v then conf.scrollbw else 0;
3835 reshape conf.winw conf.winh;
3840 sep ();
3841 src#caption "Pixmap cache" 0;
3842 src#int_with_suffix "size (advisory)"
3843 (fun () -> conf.memlimit)
3844 (fun v -> conf.memlimit <- v);
3846 src#caption2 "used"
3847 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3848 (string_with_suffix_of_int state.memused)
3849 (Hashtbl.length state.tilemap)) 1;
3851 sep ();
3852 src#caption "Layout" 0;
3853 src#caption2 "Dimension"
3854 (fun () ->
3855 Printf.sprintf "%dx%d (virtual %dx%d)"
3856 conf.winw conf.winh
3857 state.w state.maxy)
3859 if conf.debug
3860 then
3861 src#caption2 "Position" (fun () ->
3862 Printf.sprintf "%dx%d" state.x state.y
3864 else
3865 src#caption2 "Visible" (fun () -> describe_location ()) 1
3868 sep ();
3869 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3870 "Save these parameters as global defaults at exit"
3871 (fun () -> conf.bedefault)
3872 (fun v -> conf.bedefault <- v)
3875 sep ();
3876 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3877 src#bool ~offset:0 ~btos "Extended parameters"
3878 (fun () -> !showextended)
3879 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3880 if !showextended
3881 then (
3882 src#bool "checkers"
3883 (fun () -> conf.checkers)
3884 (fun v -> conf.checkers <- v; setcheckers v);
3885 src#bool "verbose"
3886 (fun () -> conf.verbose)
3887 (fun v -> conf.verbose <- v);
3888 src#bool "invert colors"
3889 (fun () -> conf.invert)
3890 (fun v -> conf.invert <- v);
3891 src#bool "max fit"
3892 (fun () -> conf.maxhfit)
3893 (fun v -> conf.maxhfit <- v);
3894 src#bool "redirect stderr"
3895 (fun () -> conf.redirectstderr)
3896 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3897 src#string "uri launcher"
3898 (fun () -> conf.urilauncher)
3899 (fun v -> conf.urilauncher <- v);
3900 src#string "tile size"
3901 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3902 (fun v ->
3904 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3905 conf.tileh <- max 64 w;
3906 conf.tilew <- max 64 h;
3907 flushtiles ();
3908 with exn ->
3909 state.text <- Printf.sprintf "bad tile size `%s': %s"
3910 v (Printexc.to_string exn));
3911 src#int "texture count"
3912 (fun () -> conf.texcount)
3913 (fun v ->
3914 if realloctexts v
3915 then conf.texcount <- v
3916 else showtext '!' " Failed to set texture count please retry later"
3918 src#int "slice height"
3919 (fun () -> conf.sliceheight)
3920 (fun v ->
3921 conf.sliceheight <- v;
3922 wcmd "sliceh" [`i conf.sliceheight];
3924 src#int "anti-aliasing level"
3925 (fun () -> conf.aalevel)
3926 (fun v ->
3927 conf.aalevel <- bound v 0 8;
3928 state.anchor <- getanchor ();
3929 opendoc state.path state.password;
3931 src#int "ui font size"
3932 (fun () -> fstate.fontsize)
3933 (fun v -> setfontsize (bound v 5 100));
3934 colorp "background color"
3935 (fun () -> conf.bgcolor)
3936 (fun v -> conf.bgcolor <- v);
3937 src#bool "crop hack"
3938 (fun () -> conf.crophack)
3939 (fun v -> conf.crophack <- v);
3940 src#string "trim fuzz"
3941 (fun () -> irect_to_string conf.trimfuzz)
3942 (fun v ->
3944 conf.trimfuzz <- irect_of_string v;
3945 if conf.trimmargins
3946 then settrim true conf.trimfuzz;
3947 with exn ->
3948 state.text <- Printf.sprintf "bad irect `%s': %s"
3949 v (Printexc.to_string exn)
3951 src#string "throttle"
3952 (fun () ->
3953 match conf.maxwait with
3954 | None -> "show place holder if page is not ready"
3955 | Some time ->
3956 if time = infinity
3957 then "wait for page to fully render"
3958 else
3959 "wait " ^ string_of_float time
3960 ^ " seconds before showing placeholder"
3962 (fun v ->
3964 let f = float_of_string v in
3965 if f <= 0.0
3966 then conf.maxwait <- None
3967 else conf.maxwait <- Some f
3968 with exn ->
3969 state.text <- Printf.sprintf "bad time `%s': %s"
3970 v (Printexc.to_string exn)
3972 src#string "ghyll scroll"
3973 (fun () ->
3974 match conf.ghyllscroll with
3975 | None -> ""
3976 | Some nab -> ghyllscroll_to_string nab
3978 (fun v ->
3980 let gs =
3981 if String.length v = 0
3982 then None
3983 else Some (ghyllscroll_of_string v)
3985 conf.ghyllscroll <- gs
3986 with exn ->
3987 state.text <- Printf.sprintf "bad ghyll `%s': %s"
3988 v (Printexc.to_string exn)
3990 src#string "selection command"
3991 (fun () -> conf.selcmd)
3992 (fun v -> conf.selcmd <- v);
3993 src#colorspace "color space"
3994 (fun () -> colorspace_to_string conf.colorspace)
3995 (fun v ->
3996 conf.colorspace <- colorspace_of_int v;
3997 wcmd "cs" [`i v];
3998 load state.layout;
4002 sep ();
4003 src#caption "Document" 0;
4004 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4005 src#caption2 "Pages"
4006 (fun () -> string_of_int state.pagecount) 1;
4007 src#caption2 "Dimensions"
4008 (fun () -> string_of_int (List.length state.pdims)) 1;
4009 if conf.trimmargins
4010 then (
4011 sep ();
4012 src#caption "Trimmed margins" 0;
4013 src#caption2 "Dimensions"
4014 (fun () -> string_of_int (List.length state.pdims)) 1;
4017 src#reset prevmode prevuioh;
4019 fun () ->
4020 state.text <- "";
4021 let prevmode = state.mode
4022 and prevuioh = state.uioh in
4023 fillsrc prevmode prevuioh;
4024 let source = (src :> lvsource) in
4025 state.uioh <- coe (object (self)
4026 inherit listview ~source ~trusted:true as super
4027 val mutable m_prevmemused = 0
4028 method infochanged = function
4029 | Memused ->
4030 if m_prevmemused != state.memused
4031 then (
4032 m_prevmemused <- state.memused;
4033 G.postRedisplay "memusedchanged";
4035 | Pdim -> G.postRedisplay "pdimchanged"
4036 | Docinfo -> fillsrc prevmode prevuioh
4038 method special key =
4039 if Glut.getModifiers () land Glut.active_ctrl = 0
4040 then
4041 match key with
4042 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
4043 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
4044 | _ -> super#special key
4045 else super#special key
4046 end);
4047 G.postRedisplay "info";
4050 let enterhelpmode =
4051 let source =
4052 (object
4053 inherit lvsourcebase
4054 method getitemcount = Array.length state.help
4055 method getitem n =
4056 let s, n, _ = state.help.(n) in
4057 (s, n)
4059 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4060 let optuioh =
4061 if not cancel
4062 then (
4063 m_qsearch <- qsearch;
4064 match state.help.(active) with
4065 | _, _, Action f -> Some (f uioh)
4066 | _ -> Some (uioh)
4068 else None
4070 m_active <- active;
4071 m_first <- first;
4072 m_pan <- pan;
4073 optuioh
4075 method hasaction n =
4076 match state.help.(n) with
4077 | _, _, Action _ -> true
4078 | _ -> false
4080 initializer
4081 m_active <- -1
4082 end)
4083 in fun () ->
4084 state.uioh <- coe (new listview ~source ~trusted:true);
4085 G.postRedisplay "help";
4088 let entermsgsmode =
4089 let msgsource =
4090 let re = Str.regexp "[\r\n]" in
4091 (object
4092 inherit lvsourcebase
4093 val mutable m_items = [||]
4095 method getitemcount = 1 + Array.length m_items
4097 method getitem n =
4098 if n = 0
4099 then "[Clear]", 0
4100 else m_items.(n-1), 0
4102 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4103 ignore uioh;
4104 if not cancel
4105 then (
4106 if active = 0
4107 then Buffer.clear state.errmsgs;
4108 m_qsearch <- qsearch;
4110 m_active <- active;
4111 m_first <- first;
4112 m_pan <- pan;
4113 None
4115 method hasaction n =
4116 n = 0
4118 method reset =
4119 state.newerrmsgs <- false;
4120 let l = Str.split re (Buffer.contents state.errmsgs) in
4121 m_items <- Array.of_list l
4123 initializer
4124 m_active <- 0
4125 end)
4126 in fun () ->
4127 state.text <- "";
4128 msgsource#reset;
4129 let source = (msgsource :> lvsource) in
4130 state.uioh <- coe (object
4131 inherit listview ~source ~trusted:false as super
4132 method display =
4133 if state.newerrmsgs
4134 then msgsource#reset;
4135 super#display
4136 end);
4137 G.postRedisplay "msgs";
4140 let quickbookmark ?title () =
4141 match state.layout with
4142 | [] -> ()
4143 | l :: _ ->
4144 let title =
4145 match title with
4146 | None ->
4147 let sec = Unix.gettimeofday () in
4148 let tm = Unix.localtime sec in
4149 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4150 (l.pageno+1)
4151 tm.Unix.tm_mday
4152 tm.Unix.tm_mon
4153 (tm.Unix.tm_year + 1900)
4154 tm.Unix.tm_hour
4155 tm.Unix.tm_min
4156 | Some title -> title
4158 state.bookmarks <-
4159 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4160 :: state.bookmarks
4163 let doreshape w h =
4164 state.fullscreen <- None;
4165 Glut.reshapeWindow w h;
4168 let viewkeyboard key =
4169 let enttext te =
4170 let mode = state.mode in
4171 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4172 state.text <- "";
4173 enttext ();
4174 G.postRedisplay "view:enttext"
4176 let c = Char.chr key in
4177 match c with
4178 | '\027' | 'q' -> (* escape *)
4179 begin match state.mstate with
4180 | Mzoomrect _ ->
4181 state.mstate <- Mnone;
4182 Glut.setCursor Glut.CURSOR_INHERIT;
4183 G.postRedisplay "kill zoom rect";
4184 | _ ->
4185 raise Quit
4186 end;
4188 | '\008' -> (* backspace *)
4189 let y = getnav ~-1 in
4190 gotoy_and_clear_text y
4192 | 'o' ->
4193 enteroutlinemode ()
4195 | 'u' ->
4196 state.rects <- [];
4197 state.text <- "";
4198 G.postRedisplay "dehighlight";
4200 | '/' | '?' ->
4201 let ondone isforw s =
4202 cbput state.hists.pat s;
4203 state.searchpattern <- s;
4204 search s isforw
4206 let s = String.create 1 in
4207 s.[0] <- c;
4208 enttext (s, "", Some (onhist state.hists.pat),
4209 textentry, ondone (c ='/'))
4211 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4212 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4213 setzoom (conf.zoom +. incr)
4215 | '+' ->
4216 let ondone s =
4217 let n =
4218 try int_of_string s with exc ->
4219 state.text <- Printf.sprintf "bad integer `%s': %s"
4220 s (Printexc.to_string exc);
4221 max_int
4223 if n != max_int
4224 then (
4225 conf.pagebias <- n;
4226 state.text <- "page bias is now " ^ string_of_int n;
4229 enttext ("page bias: ", "", None, intentry, ondone)
4231 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4232 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4233 setzoom (max 0.01 (conf.zoom -. decr))
4235 | '-' ->
4236 let ondone msg = state.text <- msg in
4237 enttext (
4238 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4239 optentry state.mode, ondone
4242 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4243 setzoom 1.0
4245 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4246 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4247 if zoom < 1.0
4248 then setzoom zoom
4250 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4251 togglebirdseye ()
4253 | '0' .. '9' ->
4254 let ondone s =
4255 let n =
4256 try int_of_string s with exc ->
4257 state.text <- Printf.sprintf "bad integer `%s': %s"
4258 s (Printexc.to_string exc);
4261 if n >= 0
4262 then (
4263 addnav ();
4264 cbput state.hists.pag (string_of_int n);
4265 gotopage1 (n + conf.pagebias - 1) 0;
4268 let pageentry text key =
4269 match Char.unsafe_chr key with
4270 | 'g' -> TEdone text
4271 | _ -> intentry text key
4273 let text = "x" in text.[0] <- c;
4274 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4276 | 'b' ->
4277 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4278 reshape conf.winw conf.winh;
4280 | 'l' ->
4281 conf.hlinks <- not conf.hlinks;
4282 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4283 G.postRedisplay "toggle highlightlinks";
4285 | 'a' ->
4286 begin match state.autoscroll with
4287 | Some step ->
4288 conf.autoscrollstep <- step;
4289 state.autoscroll <- None
4290 | None ->
4291 if conf.autoscrollstep = 0
4292 then state.autoscroll <- Some 1
4293 else state.autoscroll <- Some conf.autoscrollstep
4296 | 'P' ->
4297 conf.presentation <- not conf.presentation;
4298 if conf.presentation
4299 then (
4300 if not conf.scrollbarinpm
4301 then state.scrollw <- 0;
4303 else
4304 state.scrollw <- conf.scrollbw;
4306 showtext ' ' ("presentation mode " ^
4307 if conf.presentation then "on" else "off");
4308 state.anchor <- getanchor ();
4309 represent ()
4311 | 'f' ->
4312 begin match state.fullscreen with
4313 | None ->
4314 state.fullscreen <- Some (conf.winw, conf.winh);
4315 Glut.fullScreen ()
4316 | Some (w, h) ->
4317 state.fullscreen <- None;
4318 doreshape w h
4321 | 'g' ->
4322 gotoy_and_clear_text 0
4324 | 'G' ->
4325 gotopage1 (state.pagecount - 1) 0
4327 | 'n' ->
4328 search state.searchpattern true
4330 | 'p' | 'N' ->
4331 search state.searchpattern false
4333 | 't' ->
4334 begin match state.layout with
4335 | [] -> ()
4336 | l :: _ ->
4337 gotoy_and_clear_text (getpagey l.pageno)
4340 | ' ' ->
4341 begin match List.rev state.layout with
4342 | [] -> ()
4343 | l :: _ ->
4344 let pageno = min (l.pageno+1) (state.pagecount-1) in
4345 gotoy_and_clear_text (getpagey pageno)
4348 | '\127' -> (* del *)
4349 begin match state.layout with
4350 | [] -> ()
4351 | l :: _ ->
4352 let pageno = max 0 (l.pageno-1) in
4353 gotoy_and_clear_text (getpagey pageno)
4356 | '=' ->
4357 showtext ' ' (describe_location ());
4359 | 'w' ->
4360 begin match state.layout with
4361 | [] -> ()
4362 | l :: _ ->
4363 doreshape (l.pagew + state.scrollw) l.pageh;
4364 G.postRedisplay "w"
4367 | '\'' ->
4368 enterbookmarkmode ()
4370 | 'h' ->
4371 enterhelpmode ()
4373 | 'i' ->
4374 enterinfomode ()
4376 | 'e' when conf.redirectstderr ->
4377 entermsgsmode ()
4379 | 'm' ->
4380 let ondone s =
4381 match state.layout with
4382 | l :: _ ->
4383 state.bookmarks <-
4384 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4385 :: state.bookmarks
4386 | _ -> ()
4388 enttext ("bookmark: ", "", None, textentry, ondone)
4390 | '~' ->
4391 quickbookmark ();
4392 showtext ' ' "Quick bookmark added";
4394 | 'z' ->
4395 begin match state.layout with
4396 | l :: _ ->
4397 let rect = getpdimrect l.pagedimno in
4398 let w, h =
4399 if conf.crophack
4400 then
4401 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4402 truncate (1.2 *. (rect.(3) -. rect.(0))))
4403 else
4404 (truncate (rect.(1) -. rect.(0)),
4405 truncate (rect.(3) -. rect.(0)))
4407 let w = truncate ((float w)*.conf.zoom)
4408 and h = truncate ((float h)*.conf.zoom) in
4409 if w != 0 && h != 0
4410 then (
4411 state.anchor <- getanchor ();
4412 doreshape (w + state.scrollw) (h + conf.interpagespace)
4414 G.postRedisplay "z";
4416 | [] -> ()
4419 | '\000' -> (* ctrl-2 *)
4420 let maxw = getmaxw () in
4421 if maxw > 0.0
4422 then setzoom (maxw /. float conf.winw)
4424 | '<' | '>' ->
4425 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
4427 | '[' | ']' ->
4428 conf.colorscale <-
4429 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
4431 G.postRedisplay "brightness";
4433 | 'k' ->
4434 begin match state.mode with
4435 | Birdseye beye -> upbirdseye 1 beye
4436 | _ -> gotoy (clamp (-conf.scrollstep))
4439 | 'j' ->
4440 begin match state.mode with
4441 | Birdseye beye -> downbirdseye 1 beye
4442 | _ -> gotoy (clamp conf.scrollstep)
4445 | 'r' ->
4446 state.anchor <- getanchor ();
4447 opendoc state.path state.password
4449 | 'v' when not conf.debug ->
4450 List.iter debugl state.layout;
4452 | 'v' when conf.debug ->
4453 state.rects <- [];
4454 List.iter (fun l ->
4455 match getopaque l.pageno with
4456 | None -> ()
4457 | Some opaque ->
4458 let x0, y0, x1, y1 = pagebbox opaque in
4459 let a,b = float x0, float y0 in
4460 let c,d = float x1, float y0 in
4461 let e,f = float x1, float y1 in
4462 let h,j = float x0, float y1 in
4463 let rect = (a,b,c,d,e,f,h,j) in
4464 debugrect rect;
4465 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4466 ) state.layout;
4467 G.postRedisplay "v";
4469 | _ ->
4470 vlog "huh? %d %c" key (Char.chr key);
4473 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
4474 match key with
4475 | 27 -> (* escape *)
4476 leavebirdseye beye true
4478 | 12 -> (* ctrl-l *)
4479 let y, h = getpageyh pageno in
4480 let top = (conf.winh - h) / 2 in
4481 gotoy (max 0 (y - top))
4483 | 13 -> (* enter *)
4484 leavebirdseye beye false
4486 | _ ->
4487 viewkeyboard key
4490 let keyboard ~key ~x ~y =
4491 ignore x;
4492 ignore y;
4493 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
4494 then wcmd "interrupt" []
4495 else state.uioh <- state.uioh#key key
4498 let birdseyespecial key ((oconf, leftx, _, hooverpageno, anchor) as beye) =
4499 let incr =
4500 match conf.columns with
4501 | None -> 1
4502 | Some ((c, _, _), _) -> c
4504 match key with
4505 | Glut.KEY_UP -> upbirdseye incr beye
4506 | Glut.KEY_DOWN -> downbirdseye incr beye
4507 | Glut.KEY_LEFT -> upbirdseye 1 beye
4508 | Glut.KEY_RIGHT -> downbirdseye 1 beye
4510 | Glut.KEY_PAGE_UP ->
4511 begin match state.layout with
4512 | l :: _ ->
4513 if l.pagey != 0
4514 then (
4515 state.mode <- Birdseye (
4516 oconf, leftx, l.pageno, hooverpageno, anchor
4518 gotopage1 l.pageno 0;
4520 else (
4521 let layout = layout (state.y-conf.winh) conf.winh in
4522 match layout with
4523 | [] -> gotoy (clamp (-conf.winh))
4524 | l :: _ ->
4525 state.mode <- Birdseye (
4526 oconf, leftx, l.pageno, hooverpageno, anchor
4528 gotopage1 l.pageno 0
4531 | [] -> gotoy (clamp (-conf.winh))
4532 end;
4534 | Glut.KEY_PAGE_DOWN ->
4535 begin match List.rev state.layout with
4536 | l :: _ ->
4537 let layout = layout (state.y + conf.winh) conf.winh in
4538 begin match layout with
4539 | [] ->
4540 let incr = l.pageh - l.pagevh in
4541 if incr = 0
4542 then (
4543 state.mode <-
4544 Birdseye (
4545 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4547 G.postRedisplay "birdseye pagedown";
4549 else gotoy (clamp (incr + conf.interpagespace*2));
4551 | l :: _ ->
4552 state.mode <-
4553 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4554 gotopage1 l.pageno 0;
4557 | [] -> gotoy (clamp conf.winh)
4558 end;
4560 | Glut.KEY_HOME ->
4561 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4562 gotopage1 0 0
4564 | Glut.KEY_END ->
4565 let pageno = state.pagecount - 1 in
4566 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4567 if not (pagevisible state.layout pageno)
4568 then
4569 let h =
4570 match List.rev state.pdims with
4571 | [] -> conf.winh
4572 | (_, _, h, _) :: _ -> h
4574 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4575 else G.postRedisplay "birdseye end";
4576 | _ -> ()
4579 let setautoscrollspeed step goingdown =
4580 let incr = max 1 ((abs step) / 2) in
4581 let incr = if goingdown then incr else -incr in
4582 let astep = step + incr in
4583 state.autoscroll <- Some astep;
4586 let special ~key ~x ~y =
4587 ignore x;
4588 ignore y;
4589 state.uioh <- state.uioh#special key
4592 let drawpage l =
4593 let color =
4594 match state.mode with
4595 | Textentry _ -> scalecolor 0.4
4596 | View -> scalecolor 1.0
4597 | Birdseye (_, _, pageno, hooverpageno, _) ->
4598 if l.pageno = hooverpageno
4599 then scalecolor 0.9
4600 else (
4601 if l.pageno = pageno
4602 then scalecolor 1.0
4603 else scalecolor 0.8
4606 drawtiles l color;
4607 begin match getopaque l.pageno with
4608 | Some opaque ->
4609 if tileready l l.pagex l.pagey
4610 then
4611 let x = l.pagedispx - l.pagex
4612 and y = l.pagedispy - l.pagey in
4613 postprocess opaque conf.hlinks x y;
4615 | _ -> ()
4616 end;
4619 let scrollindicator () =
4620 let sbw, ph, sh = state.uioh#scrollph in
4621 let sbh, pw, sw = state.uioh#scrollpw in
4623 GlDraw.color (0.64, 0.64, 0.64);
4624 GlDraw.rect
4625 (float (conf.winw - sbw), 0.)
4626 (float conf.winw, float conf.winh)
4628 GlDraw.rect
4629 (0., float (conf.winh - sbh))
4630 (float (conf.winw - state.scrollw - 1), float conf.winh)
4632 GlDraw.color (0.0, 0.0, 0.0);
4634 GlDraw.rect
4635 (float (conf.winw - sbw), ph)
4636 (float conf.winw, ph +. sh)
4638 GlDraw.rect
4639 (pw, float (conf.winh - sbh))
4640 (pw +. sw, float conf.winh)
4644 let pagetranslatepoint l x y =
4645 let dy = y - l.pagedispy in
4646 let y = dy + l.pagey in
4647 let dx = x - l.pagedispx in
4648 let x = dx + l.pagex in
4649 (x, y);
4652 let showsel () =
4653 match state.mstate with
4654 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4657 | Msel ((x0, y0), (x1, y1)) ->
4658 let rec loop = function
4659 | l :: ls ->
4660 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4661 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4662 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4663 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4664 then
4665 match getopaque l.pageno with
4666 | Some opaque ->
4667 let dx, dy = pagetranslatepoint l 0 0 in
4668 let x0 = x0 + dx
4669 and y0 = y0 + dy
4670 and x1 = x1 + dx
4671 and y1 = y1 + dy in
4672 GlMat.mode `modelview;
4673 GlMat.push ();
4674 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4675 seltext opaque (x0, y0, x1, y1);
4676 GlMat.pop ();
4677 | _ -> ()
4678 else loop ls
4679 | [] -> ()
4681 loop state.layout
4684 let showrects () =
4685 Gl.enable `blend;
4686 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4687 GlDraw.polygon_mode `both `fill;
4688 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4689 List.iter
4690 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4691 List.iter (fun l ->
4692 if l.pageno = pageno
4693 then (
4694 let dx = float (l.pagedispx - l.pagex) in
4695 let dy = float (l.pagedispy - l.pagey) in
4696 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4697 GlDraw.begins `quads;
4699 GlDraw.vertex2 (x0+.dx, y0+.dy);
4700 GlDraw.vertex2 (x1+.dx, y1+.dy);
4701 GlDraw.vertex2 (x2+.dx, y2+.dy);
4702 GlDraw.vertex2 (x3+.dx, y3+.dy);
4704 GlDraw.ends ();
4706 ) state.layout
4707 ) state.rects
4709 Gl.disable `blend;
4712 let display () =
4713 GlClear.color (scalecolor2 conf.bgcolor);
4714 GlClear.clear [`color];
4715 List.iter drawpage state.layout;
4716 showrects ();
4717 showsel ();
4718 state.uioh#display;
4719 scrollindicator ();
4720 begin match state.mstate with
4721 | Mzoomrect ((x0, y0), (x1, y1)) ->
4722 Gl.enable `blend;
4723 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4724 GlDraw.polygon_mode `both `fill;
4725 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4726 GlDraw.rect (float x0, float y0)
4727 (float x1, float y1);
4728 Gl.disable `blend;
4729 | _ -> ()
4730 end;
4731 enttext ();
4732 Glut.swapBuffers ();
4735 let getunder x y =
4736 let rec f = function
4737 | l :: rest ->
4738 begin match getopaque l.pageno with
4739 | Some opaque ->
4740 let x0 = l.pagedispx in
4741 let x1 = x0 + l.pagevw in
4742 let y0 = l.pagedispy in
4743 let y1 = y0 + l.pagevh in
4744 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4745 then
4746 let px, py = pagetranslatepoint l x y in
4747 match whatsunder opaque px py with
4748 | Unone -> f rest
4749 | under -> under
4750 else f rest
4751 | _ ->
4752 f rest
4754 | [] -> Unone
4756 f state.layout
4759 let zoomrect x y x1 y1 =
4760 let x0 = min x x1
4761 and x1 = max x x1
4762 and y0 = min y y1 in
4763 gotoy (state.y + y0);
4764 state.anchor <- getanchor ();
4765 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4766 let margin =
4767 if state.w < conf.winw - state.scrollw
4768 then (conf.winw - state.scrollw - state.w) / 2
4769 else 0
4771 state.x <- (state.x + margin) - x0;
4772 setzoom zoom;
4773 Glut.setCursor Glut.CURSOR_INHERIT;
4774 state.mstate <- Mnone;
4777 let scrollx x =
4778 let winw = conf.winw - state.scrollw - 1 in
4779 let s = float x /. float winw in
4780 let destx = truncate (float (state.w + winw) *. s) in
4781 state.x <- winw - destx;
4782 gotoy_and_clear_text state.y;
4783 state.mstate <- Mscrollx;
4786 let scrolly y =
4787 let s = float y /. float conf.winh in
4788 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4789 gotoy_and_clear_text desty;
4790 state.mstate <- Mscrolly;
4793 let viewmouse button bstate x y =
4794 match button with
4795 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4796 if Glut.getModifiers () land Glut.active_ctrl != 0
4797 then (
4798 match state.mstate with
4799 | Mzoom (oldn, i) ->
4800 if oldn = n
4801 then (
4802 if i = 2
4803 then
4804 let incr =
4805 match n with
4806 | 4 ->
4807 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4808 | _ ->
4809 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4811 let zoom = conf.zoom -. incr in
4812 setzoom zoom;
4813 state.mstate <- Mzoom (n, 0);
4814 else
4815 state.mstate <- Mzoom (n, i+1);
4817 else state.mstate <- Mzoom (n, 0)
4819 | _ -> state.mstate <- Mzoom (n, 0)
4821 else (
4822 match state.autoscroll with
4823 | Some step -> setautoscrollspeed step (n=4)
4824 | None ->
4825 let incr =
4826 if n = 3
4827 then -conf.scrollstep
4828 else conf.scrollstep
4830 let incr = incr * 2 in
4831 let y = clamp incr in
4832 gotoy_and_clear_text y
4835 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4836 if bstate = Glut.DOWN
4837 then (
4838 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4839 state.mstate <- Mpan (x, y)
4841 else
4842 state.mstate <- Mnone
4844 | Glut.RIGHT_BUTTON ->
4845 if bstate = Glut.DOWN
4846 then (
4847 Glut.setCursor Glut.CURSOR_CYCLE;
4848 let p = (x, y) in
4849 state.mstate <- Mzoomrect (p, p)
4851 else (
4852 match state.mstate with
4853 | Mzoomrect ((x0, y0), _) ->
4854 if abs (x-x0) > 10 && abs (y - y0) > 10
4855 then zoomrect x0 y0 x y
4856 else (
4857 state.mstate <- Mnone;
4858 Glut.setCursor Glut.CURSOR_INHERIT;
4859 G.postRedisplay "kill accidental zoom rect";
4861 | _ ->
4862 Glut.setCursor Glut.CURSOR_INHERIT;
4863 state.mstate <- Mnone
4866 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4867 if bstate = Glut.DOWN
4868 then
4869 let _, position, sh = state.uioh#scrollph in
4870 if y > truncate position && y < truncate (position +. sh)
4871 then state.mstate <- Mscrolly
4872 else scrolly y
4873 else
4874 state.mstate <- Mnone
4876 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4877 if bstate = Glut.DOWN
4878 then
4879 let _, position, sw = state.uioh#scrollpw in
4880 if x > truncate position && x < truncate (position +. sw)
4881 then state.mstate <- Mscrollx
4882 else scrollx x
4883 else
4884 state.mstate <- Mnone
4886 | Glut.LEFT_BUTTON ->
4887 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4888 begin match dest with
4889 | Ulinkgoto (pageno, top) ->
4890 if pageno >= 0
4891 then (
4892 addnav ();
4893 gotopage1 pageno top;
4896 | Ulinkuri s ->
4897 gotouri s
4899 | Uremote _ | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
4901 | Unone when bstate = Glut.DOWN ->
4902 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4903 state.mstate <- Mpan (x, y);
4905 | Unone | Utext _ ->
4906 if bstate = Glut.DOWN
4907 then (
4908 if conf.angle mod 360 = 0
4909 then (
4910 state.mstate <- Msel ((x, y), (x, y));
4911 G.postRedisplay "mouse select";
4914 else (
4915 match state.mstate with
4916 | Mnone -> ()
4918 | Mzoom _ | Mscrollx | Mscrolly ->
4919 state.mstate <- Mnone
4921 | Mzoomrect ((x0, y0), _) ->
4922 zoomrect x0 y0 x y
4924 | Mpan _ ->
4925 Glut.setCursor Glut.CURSOR_INHERIT;
4926 state.mstate <- Mnone
4928 | Msel ((_, y0), (_, y1)) ->
4929 let rec loop = function
4930 | [] -> ()
4931 | l :: rest ->
4932 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4933 || ((y1 >= l.pagedispy
4934 && y1 <= (l.pagedispy + l.pagevh)))
4935 then
4936 match getopaque l.pageno with
4937 | Some opaque ->
4938 copysel conf.selcmd opaque;
4939 G.postRedisplay "copysel"
4940 | _ -> ()
4941 else loop rest
4943 loop state.layout;
4944 Glut.setCursor Glut.CURSOR_INHERIT;
4945 state.mstate <- Mnone;
4949 | _ -> ()
4952 let birdseyemouse button bstate x y
4953 (conf, leftx, _, hooverpageno, anchor) =
4954 match button with
4955 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4956 let rec loop = function
4957 | [] -> ()
4958 | l :: rest ->
4959 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4960 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4961 then (
4962 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4964 else loop rest
4966 loop state.layout
4967 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4968 | _ -> ()
4971 let mouse bstate button x y =
4972 state.uioh <- state.uioh#button button bstate x y;
4975 let mouse ~button ~state ~x ~y = mouse state button x y;;
4977 let motion ~x ~y =
4978 state.uioh <- state.uioh#motion x y
4981 let pmotion ~x ~y =
4982 state.uioh <- state.uioh#pmotion x y;
4985 let uioh = object
4986 method display = ()
4988 method key key =
4989 begin match state.mode with
4990 | Textentry textentry -> textentrykeyboard key textentry
4991 | Birdseye birdseye -> birdseyekeyboard key birdseye
4992 | View -> viewkeyboard key
4993 end;
4994 state.uioh
4996 method special key =
4997 begin match state.mode with
4998 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4999 togglebirdseye ()
5001 | Birdseye vals ->
5002 birdseyespecial key vals
5004 | View when key = Glut.KEY_F1 ->
5005 enterhelpmode ()
5007 | View ->
5008 begin match state.autoscroll with
5009 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
5010 setautoscrollspeed step (key = Glut.KEY_DOWN)
5012 | _ ->
5013 let y =
5014 match key with
5015 | Glut.KEY_F3 -> search state.searchpattern true; state.y
5016 | Glut.KEY_UP ->
5017 if Glut.getModifiers () land Glut.active_ctrl != 0
5018 then
5019 if Glut.getModifiers () land Glut.active_shift != 0
5020 then (setzoom state.prevzoom; state.y)
5021 else clamp (-conf.winh/2)
5022 else clamp (-conf.scrollstep)
5023 | Glut.KEY_DOWN ->
5024 if Glut.getModifiers () land Glut.active_ctrl != 0
5025 then
5026 if Glut.getModifiers () land Glut.active_shift != 0
5027 then (setzoom state.prevzoom; state.y)
5028 else clamp (conf.winh/2)
5029 else clamp (conf.scrollstep)
5030 | Glut.KEY_PAGE_UP ->
5031 if Glut.getModifiers () land Glut.active_ctrl != 0
5032 then
5033 match state.layout with
5034 | [] -> state.y
5035 | l :: _ -> state.y - l.pagey
5036 else
5037 clamp (-conf.winh)
5038 | Glut.KEY_PAGE_DOWN ->
5039 if Glut.getModifiers () land Glut.active_ctrl != 0
5040 then
5041 match List.rev state.layout with
5042 | [] -> state.y
5043 | l :: _ -> getpagey l.pageno
5044 else
5045 clamp conf.winh
5046 | Glut.KEY_HOME ->
5047 addnav ();
5049 | Glut.KEY_END ->
5050 addnav ();
5051 state.maxy - (if conf.maxhfit then conf.winh else 0)
5053 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
5054 Glut.getModifiers () land Glut.active_alt != 0 ->
5055 getnav (if key = Glut.KEY_LEFT then 1 else -1)
5057 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
5058 let dx =
5059 if Glut.getModifiers () land Glut.active_ctrl != 0
5060 then (conf.winw / 2)
5061 else 10
5063 state.x <- state.x - dx;
5064 state.y
5065 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
5066 let dx =
5067 if Glut.getModifiers () land Glut.active_ctrl != 0
5068 then (conf.winw / 2)
5069 else 10
5071 state.x <- state.x + dx;
5072 state.y
5074 | _ -> state.y
5076 if abs (state.y - y) > conf.scrollstep*2
5077 then gotoghyll y
5078 else gotoy_and_clear_text y
5081 | Textentry te -> textentryspecial key te
5082 end;
5083 state.uioh
5085 method button button bstate x y =
5086 begin match state.mode with
5087 | View -> viewmouse button bstate x y
5088 | Birdseye beye -> birdseyemouse button bstate x y beye
5089 | Textentry _ -> ()
5090 end;
5091 state.uioh
5093 method motion x y =
5094 begin match state.mode with
5095 | Textentry _ -> ()
5096 | View | Birdseye _ ->
5097 match state.mstate with
5098 | Mzoom _ | Mnone -> ()
5100 | Mpan (x0, y0) ->
5101 let dx = x - x0
5102 and dy = y0 - y in
5103 state.mstate <- Mpan (x, y);
5104 if conf.zoom > 1.0 then state.x <- state.x + dx;
5105 let y = clamp dy in
5106 gotoy_and_clear_text y
5108 | Msel (a, _) ->
5109 state.mstate <- Msel (a, (x, y));
5110 G.postRedisplay "motion select";
5112 | Mscrolly ->
5113 let y = min conf.winh (max 0 y) in
5114 scrolly y
5116 | Mscrollx ->
5117 let x = min conf.winw (max 0 x) in
5118 scrollx x
5120 | Mzoomrect (p0, _) ->
5121 state.mstate <- Mzoomrect (p0, (x, y));
5122 G.postRedisplay "motion zoomrect";
5123 end;
5124 state.uioh
5126 method pmotion x y =
5127 begin match state.mode with
5128 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5129 let rec loop = function
5130 | [] ->
5131 if hooverpageno != -1
5132 then (
5133 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5134 G.postRedisplay "pmotion birdseye no hoover";
5136 | l :: rest ->
5137 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5138 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5139 then (
5140 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5141 G.postRedisplay "pmotion birdseye hoover";
5143 else loop rest
5145 loop state.layout
5147 | Textentry _ -> ()
5149 | View ->
5150 match state.mstate with
5151 | Mnone ->
5152 begin match getunder x y with
5153 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
5154 | Ulinkuri uri ->
5155 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
5156 Glut.setCursor Glut.CURSOR_INFO
5157 | Ulinkgoto (page, _) ->
5158 if conf.underinfo
5159 then showtext 'p' ("age: " ^ string_of_int (page+1));
5160 Glut.setCursor Glut.CURSOR_INFO
5161 | Utext s ->
5162 if conf.underinfo then showtext 'f' ("ont: " ^ s);
5163 Glut.setCursor Glut.CURSOR_TEXT
5164 | Uunexpected s ->
5165 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
5166 Glut.setCursor Glut.CURSOR_INHERIT
5167 | Ulaunch s ->
5168 if conf.underinfo then showtext 'l' ("launch: " ^ s);
5169 Glut.setCursor Glut.CURSOR_INHERIT
5170 | Unamed s ->
5171 if conf.underinfo then showtext 'n' ("named: " ^ s);
5172 Glut.setCursor Glut.CURSOR_INHERIT
5173 | Uremote s ->
5174 if conf.underinfo then showtext 'r' ("emote: " ^ s);
5175 Glut.setCursor Glut.CURSOR_INHERIT
5178 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5180 end;
5181 state.uioh
5183 method infochanged _ = ()
5185 method scrollph =
5186 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5187 let p, h = scrollph state.y maxy in
5188 state.scrollw, p, h
5190 method scrollpw =
5191 let winw = conf.winw - state.scrollw - 1 in
5192 let fwinw = float winw in
5193 let sw =
5194 let sw = fwinw /. float state.w in
5195 let sw = fwinw *. sw in
5196 max sw (float conf.scrollh)
5198 let position, sw =
5199 let f = state.w+winw in
5200 let r = float (winw-state.x) /. float f in
5201 let p = fwinw *. r in
5202 p-.sw/.2., sw
5204 let sw =
5205 if position +. sw > fwinw
5206 then fwinw -. position
5207 else sw
5209 state.hscrollh, position, sw
5210 end;;
5212 module Config =
5213 struct
5214 open Parser
5216 let fontpath = ref "";;
5217 let wmclasshack = ref false;;
5219 let unent s =
5220 let l = String.length s in
5221 let b = Buffer.create l in
5222 unent b s 0 l;
5223 Buffer.contents b;
5226 let home =
5228 match platform with
5229 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
5230 | _ -> Sys.getenv "HOME"
5231 with exn ->
5232 prerr_endline
5233 ("Can not determine home directory location: " ^
5234 Printexc.to_string exn);
5238 let config_of c attrs =
5239 let apply c k v =
5241 match k with
5242 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5243 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5244 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5245 | "preload" -> { c with preload = bool_of_string v }
5246 | "page-bias" -> { c with pagebias = int_of_string v }
5247 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5248 | "auto-scroll-step" ->
5249 { c with autoscrollstep = max 0 (int_of_string v) }
5250 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5251 | "crop-hack" -> { c with crophack = bool_of_string v }
5252 | "throttle" ->
5253 let mw =
5254 match String.lowercase v with
5255 | "true" -> Some infinity
5256 | "false" -> None
5257 | f -> Some (float_of_string f)
5259 { c with maxwait = mw}
5260 | "highlight-links" -> { c with hlinks = bool_of_string v }
5261 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5262 | "vertical-margin" ->
5263 { c with interpagespace = max 0 (int_of_string v) }
5264 | "zoom" ->
5265 let zoom = float_of_string v /. 100. in
5266 let zoom = max zoom 0.0 in
5267 { c with zoom = zoom }
5268 | "presentation" -> { c with presentation = bool_of_string v }
5269 | "rotation-angle" -> { c with angle = int_of_string v }
5270 | "width" -> { c with winw = max 20 (int_of_string v) }
5271 | "height" -> { c with winh = max 20 (int_of_string v) }
5272 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5273 | "proportional-display" -> { c with proportional = bool_of_string v }
5274 | "pixmap-cache-size" ->
5275 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5276 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5277 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5278 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5279 | "persistent-location" -> { c with jumpback = bool_of_string v }
5280 | "background-color" -> { c with bgcolor = color_of_string v }
5281 | "scrollbar-in-presentation" ->
5282 { c with scrollbarinpm = bool_of_string v }
5283 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5284 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5285 | "mupdf-store-size" ->
5286 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5287 | "checkers" -> { c with checkers = bool_of_string v }
5288 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5289 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5290 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5291 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
5292 | "uri-launcher" -> { c with urilauncher = unent v }
5293 | "color-space" -> { c with colorspace = colorspace_of_string v }
5294 | "invert-colors" -> { c with invert = bool_of_string v }
5295 | "brightness" -> { c with colorscale = float_of_string v }
5296 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5297 | "ghyllscroll" ->
5298 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5299 | "columns" ->
5300 let nab = columns_of_string v in
5301 { c with columns = Some (nab, [||]) }
5302 | "birds-eye-columns" ->
5303 { c with beyecolumns = Some (max (int_of_string v) 2) }
5304 | "selection-command" -> { c with selcmd = unent v }
5305 | _ -> c
5306 with exn ->
5307 prerr_endline ("Error processing attribute (`" ^
5308 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5311 let rec fold c = function
5312 | [] -> c
5313 | (k, v) :: rest ->
5314 let c = apply c k v in
5315 fold c rest
5317 fold c attrs;
5320 let fromstring f pos n v d =
5321 try f v
5322 with exn ->
5323 dolog "Error processing attribute (%S=%S) at %d\n%s"
5324 n v pos (Printexc.to_string exn)
5329 let bookmark_of attrs =
5330 let rec fold title page rely = function
5331 | ("title", v) :: rest -> fold v page rely rest
5332 | ("page", v) :: rest -> fold title v rely rest
5333 | ("rely", v) :: rest -> fold title page v rest
5334 | _ :: rest -> fold title page rely rest
5335 | [] -> title, page, rely
5337 fold "invalid" "0" "0" attrs
5340 let doc_of attrs =
5341 let rec fold path page rely pan = function
5342 | ("path", v) :: rest -> fold v page rely pan rest
5343 | ("page", v) :: rest -> fold path v rely pan rest
5344 | ("rely", v) :: rest -> fold path page v pan rest
5345 | ("pan", v) :: rest -> fold path page rely v rest
5346 | _ :: rest -> fold path page rely pan rest
5347 | [] -> path, page, rely, pan
5349 fold "" "0" "0" "0" attrs
5352 let setconf dst src =
5353 dst.scrollbw <- src.scrollbw;
5354 dst.scrollh <- src.scrollh;
5355 dst.icase <- src.icase;
5356 dst.preload <- src.preload;
5357 dst.pagebias <- src.pagebias;
5358 dst.verbose <- src.verbose;
5359 dst.scrollstep <- src.scrollstep;
5360 dst.maxhfit <- src.maxhfit;
5361 dst.crophack <- src.crophack;
5362 dst.autoscrollstep <- src.autoscrollstep;
5363 dst.maxwait <- src.maxwait;
5364 dst.hlinks <- src.hlinks;
5365 dst.underinfo <- src.underinfo;
5366 dst.interpagespace <- src.interpagespace;
5367 dst.zoom <- src.zoom;
5368 dst.presentation <- src.presentation;
5369 dst.angle <- src.angle;
5370 dst.winw <- src.winw;
5371 dst.winh <- src.winh;
5372 dst.savebmarks <- src.savebmarks;
5373 dst.memlimit <- src.memlimit;
5374 dst.proportional <- src.proportional;
5375 dst.texcount <- src.texcount;
5376 dst.sliceheight <- src.sliceheight;
5377 dst.thumbw <- src.thumbw;
5378 dst.jumpback <- src.jumpback;
5379 dst.bgcolor <- src.bgcolor;
5380 dst.scrollbarinpm <- src.scrollbarinpm;
5381 dst.tilew <- src.tilew;
5382 dst.tileh <- src.tileh;
5383 dst.mustoresize <- src.mustoresize;
5384 dst.checkers <- src.checkers;
5385 dst.aalevel <- src.aalevel;
5386 dst.trimmargins <- src.trimmargins;
5387 dst.trimfuzz <- src.trimfuzz;
5388 dst.urilauncher <- src.urilauncher;
5389 dst.colorspace <- src.colorspace;
5390 dst.invert <- src.invert;
5391 dst.colorscale <- src.colorscale;
5392 dst.redirectstderr <- src.redirectstderr;
5393 dst.ghyllscroll <- src.ghyllscroll;
5394 dst.columns <- src.columns;
5395 dst.beyecolumns <- src.beyecolumns;
5396 dst.selcmd <- src.selcmd;
5399 let get s =
5400 let h = Hashtbl.create 10 in
5401 let dc = { defconf with angle = defconf.angle } in
5402 let rec toplevel v t spos _ =
5403 match t with
5404 | Vdata | Vcdata | Vend -> v
5405 | Vopen ("llppconfig", _, closed) ->
5406 if closed
5407 then v
5408 else { v with f = llppconfig }
5409 | Vopen _ ->
5410 error "unexpected subelement at top level" s spos
5411 | Vclose _ -> error "unexpected close at top level" s spos
5413 and llppconfig v t spos _ =
5414 match t with
5415 | Vdata | Vcdata -> v
5416 | Vend -> error "unexpected end of input in llppconfig" s spos
5417 | Vopen ("defaults", attrs, closed) ->
5418 let c = config_of dc attrs in
5419 setconf dc c;
5420 if closed
5421 then v
5422 else { v with f = skip "defaults" (fun () -> v) }
5424 | Vopen ("ui-font", attrs, closed) ->
5425 let rec getsize size = function
5426 | [] -> size
5427 | ("size", v) :: rest ->
5428 let size =
5429 fromstring int_of_string spos "size" v fstate.fontsize in
5430 getsize size rest
5431 | l -> getsize size l
5433 fstate.fontsize <- getsize fstate.fontsize attrs;
5434 if closed
5435 then v
5436 else { v with f = uifont (Buffer.create 10) }
5438 | Vopen ("doc", attrs, closed) ->
5439 let pathent, spage, srely, span = doc_of attrs in
5440 let path = unent pathent
5441 and pageno = fromstring int_of_string spos "page" spage 0
5442 and rely = fromstring float_of_string spos "rely" srely 0.0
5443 and pan = fromstring int_of_string spos "pan" span 0 in
5444 let c = config_of dc attrs in
5445 let anchor = (pageno, rely) in
5446 if closed
5447 then (Hashtbl.add h path (c, [], pan, anchor); v)
5448 else { v with f = doc path pan anchor c [] }
5450 | Vopen _ ->
5451 error "unexpected subelement in llppconfig" s spos
5453 | Vclose "llppconfig" -> { v with f = toplevel }
5454 | Vclose _ -> error "unexpected close in llppconfig" s spos
5456 and uifont b v t spos epos =
5457 match t with
5458 | Vdata | Vcdata ->
5459 Buffer.add_substring b s spos (epos - spos);
5461 | Vopen (_, _, _) ->
5462 error "unexpected subelement in ui-font" s spos
5463 | Vclose "ui-font" ->
5464 if String.length !fontpath = 0
5465 then fontpath := Buffer.contents b;
5466 { v with f = llppconfig }
5467 | Vclose _ -> error "unexpected close in ui-font" s spos
5468 | Vend -> error "unexpected end of input in ui-font" s spos
5470 and doc path pan anchor c bookmarks v t spos _ =
5471 match t with
5472 | Vdata | Vcdata -> v
5473 | Vend -> error "unexpected end of input in doc" s spos
5474 | Vopen ("bookmarks", _, closed) ->
5475 if closed
5476 then v
5477 else { v with f = pbookmarks path pan anchor c bookmarks }
5479 | Vopen (_, _, _) ->
5480 error "unexpected subelement in doc" s spos
5482 | Vclose "doc" ->
5483 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5484 { v with f = llppconfig }
5486 | Vclose _ -> error "unexpected close in doc" s spos
5488 and pbookmarks path pan anchor c bookmarks v t spos _ =
5489 match t with
5490 | Vdata | Vcdata -> v
5491 | Vend -> error "unexpected end of input in bookmarks" s spos
5492 | Vopen ("item", attrs, closed) ->
5493 let titleent, spage, srely = bookmark_of attrs in
5494 let page = fromstring int_of_string spos "page" spage 0
5495 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5496 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5497 if closed
5498 then { v with f = pbookmarks path pan anchor c bookmarks }
5499 else
5500 let f () = v in
5501 { v with f = skip "item" f }
5503 | Vopen _ ->
5504 error "unexpected subelement in bookmarks" s spos
5506 | Vclose "bookmarks" ->
5507 { v with f = doc path pan anchor c bookmarks }
5509 | Vclose _ -> error "unexpected close in bookmarks" s spos
5511 and skip tag f v t spos _ =
5512 match t with
5513 | Vdata | Vcdata -> v
5514 | Vend ->
5515 error ("unexpected end of input in skipped " ^ tag) s spos
5516 | Vopen (tag', _, closed) ->
5517 if closed
5518 then v
5519 else
5520 let f' () = { v with f = skip tag f } in
5521 { v with f = skip tag' f' }
5522 | Vclose ctag ->
5523 if tag = ctag
5524 then f ()
5525 else error ("unexpected close in skipped " ^ tag) s spos
5528 parse { f = toplevel; accu = () } s;
5529 h, dc;
5532 let do_load f ic =
5534 let len = in_channel_length ic in
5535 let s = String.create len in
5536 really_input ic s 0 len;
5537 f s;
5538 with
5539 | Parse_error (msg, s, pos) ->
5540 let subs = subs s pos in
5541 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5542 failwith ("parse error: " ^ s)
5544 | exn ->
5545 failwith ("config load error: " ^ Printexc.to_string exn)
5548 let defconfpath =
5549 let dir =
5551 let dir = Filename.concat home ".config" in
5552 if Sys.is_directory dir then dir else home
5553 with _ -> home
5555 Filename.concat dir "llpp.conf"
5558 let confpath = ref defconfpath;;
5560 let load1 f =
5561 if Sys.file_exists !confpath
5562 then
5563 match
5564 (try Some (open_in_bin !confpath)
5565 with exn ->
5566 prerr_endline
5567 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5568 Printexc.to_string exn);
5569 None
5571 with
5572 | Some ic ->
5573 begin try
5574 f (do_load get ic)
5575 with exn ->
5576 prerr_endline
5577 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5578 Printexc.to_string exn);
5579 end;
5580 close_in ic;
5582 | None -> ()
5583 else
5584 f (Hashtbl.create 0, defconf)
5587 let load () =
5588 let f (h, dc) =
5589 let pc, pb, px, pa =
5591 Hashtbl.find h (Filename.basename state.path)
5592 with Not_found -> dc, [], 0, (0, 0.0)
5594 setconf defconf dc;
5595 setconf conf pc;
5596 state.bookmarks <- pb;
5597 state.x <- px;
5598 state.scrollw <- conf.scrollbw;
5599 if conf.jumpback
5600 then state.anchor <- pa;
5601 cbput state.hists.nav pa;
5603 load1 f
5606 let add_attrs bb always dc c =
5607 let ob s a b =
5608 if always || a != b
5609 then Printf.bprintf bb "\n %s='%b'" s a
5610 and oi s a b =
5611 if always || a != b
5612 then Printf.bprintf bb "\n %s='%d'" s a
5613 and oI s a b =
5614 if always || a != b
5615 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5616 and oz s a b =
5617 if always || a <> b
5618 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5619 and oF s a b =
5620 if always || a <> b
5621 then Printf.bprintf bb "\n %s='%f'" s a
5622 and oc s a b =
5623 if always || a <> b
5624 then
5625 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5626 and oC s a b =
5627 if always || a <> b
5628 then
5629 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5630 and oR s a b =
5631 if always || a <> b
5632 then
5633 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5634 and os s a b =
5635 if always || a <> b
5636 then
5637 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5638 and og s a b =
5639 if always || a <> b
5640 then
5641 match a with
5642 | None -> ()
5643 | Some (_N, _A, _B) ->
5644 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5645 and oW s a b =
5646 if always || a <> b
5647 then
5648 let v =
5649 match a with
5650 | None -> "false"
5651 | Some f ->
5652 if f = infinity
5653 then "true"
5654 else string_of_float f
5656 Printf.bprintf bb "\n %s='%s'" s v
5657 and oco s a b =
5658 if always || a <> b
5659 then
5660 match a with
5661 | Some ((n, a, b), _) when n > 1 ->
5662 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5663 | _ -> ()
5664 and obeco s a b =
5665 if always || a <> b
5666 then
5667 match a with
5668 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5669 | _ -> ()
5671 let w, h =
5672 if always
5673 then dc.winw, dc.winh
5674 else
5675 match state.fullscreen with
5676 | Some wh -> wh
5677 | None -> c.winw, c.winh
5679 let zoom, presentation, interpagespace, maxwait =
5680 if always
5681 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5682 else
5683 match state.mode with
5684 | Birdseye (bc, _, _, _, _) ->
5685 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5686 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5688 oi "width" w dc.winw;
5689 oi "height" h dc.winh;
5690 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5691 oi "scroll-handle-height" c.scrollh dc.scrollh;
5692 ob "case-insensitive-search" c.icase dc.icase;
5693 ob "preload" c.preload dc.preload;
5694 oi "page-bias" c.pagebias dc.pagebias;
5695 oi "scroll-step" c.scrollstep dc.scrollstep;
5696 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5697 ob "max-height-fit" c.maxhfit dc.maxhfit;
5698 ob "crop-hack" c.crophack dc.crophack;
5699 oW "throttle" maxwait dc.maxwait;
5700 ob "highlight-links" c.hlinks dc.hlinks;
5701 ob "under-cursor-info" c.underinfo dc.underinfo;
5702 oi "vertical-margin" interpagespace dc.interpagespace;
5703 oz "zoom" zoom dc.zoom;
5704 ob "presentation" presentation dc.presentation;
5705 oi "rotation-angle" c.angle dc.angle;
5706 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5707 ob "proportional-display" c.proportional dc.proportional;
5708 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5709 oi "tex-count" c.texcount dc.texcount;
5710 oi "slice-height" c.sliceheight dc.sliceheight;
5711 oi "thumbnail-width" c.thumbw dc.thumbw;
5712 ob "persistent-location" c.jumpback dc.jumpback;
5713 oc "background-color" c.bgcolor dc.bgcolor;
5714 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5715 oi "tile-width" c.tilew dc.tilew;
5716 oi "tile-height" c.tileh dc.tileh;
5717 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5718 ob "checkers" c.checkers dc.checkers;
5719 oi "aalevel" c.aalevel dc.aalevel;
5720 ob "trim-margins" c.trimmargins dc.trimmargins;
5721 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5722 os "uri-launcher" c.urilauncher dc.urilauncher;
5723 oC "color-space" c.colorspace dc.colorspace;
5724 ob "invert-colors" c.invert dc.invert;
5725 oF "brightness" c.colorscale dc.colorscale;
5726 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5727 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
5728 oco "columns" c.columns dc.columns;
5729 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
5730 if always
5731 then ob "wmclass-hack" !wmclasshack false;
5732 os "selection-command" c.selcmd dc.selcmd;
5735 let save () =
5736 let uifontsize = fstate.fontsize in
5737 let bb = Buffer.create 32768 in
5738 let f (h, dc) =
5739 let dc = if conf.bedefault then conf else dc in
5740 Buffer.add_string bb "<llppconfig>\n";
5742 if String.length !fontpath > 0
5743 then
5744 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5745 uifontsize
5746 !fontpath
5747 else (
5748 if uifontsize <> 14
5749 then
5750 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5753 Buffer.add_string bb "<defaults ";
5754 add_attrs bb true dc dc;
5755 Buffer.add_string bb "/>\n";
5757 let adddoc path pan anchor c bookmarks =
5758 if bookmarks == [] && c = dc && anchor = emptyanchor
5759 then ()
5760 else (
5761 Printf.bprintf bb "<doc path='%s'"
5762 (enent path 0 (String.length path));
5764 if anchor <> emptyanchor
5765 then (
5766 let n, y = anchor in
5767 Printf.bprintf bb " page='%d'" n;
5768 if y > 1e-6
5769 then
5770 Printf.bprintf bb " rely='%f'" y
5774 if pan != 0
5775 then Printf.bprintf bb " pan='%d'" pan;
5777 add_attrs bb false dc c;
5779 begin match bookmarks with
5780 | [] -> Buffer.add_string bb "/>\n"
5781 | _ ->
5782 Buffer.add_string bb ">\n<bookmarks>\n";
5783 List.iter (fun (title, _level, (page, rely)) ->
5784 Printf.bprintf bb
5785 "<item title='%s' page='%d'"
5786 (enent title 0 (String.length title))
5787 page
5789 if rely > 1e-6
5790 then
5791 Printf.bprintf bb " rely='%f'" rely
5793 Buffer.add_string bb "/>\n";
5794 ) bookmarks;
5795 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5796 end;
5800 let pan, conf =
5801 match state.mode with
5802 | Birdseye (c, pan, _, _, _) ->
5803 let beyecolumns =
5804 match conf.columns with
5805 | Some ((c, _, _), _) -> Some c
5806 | None -> None
5807 and columns =
5808 match c.columns with
5809 | Some (c, _) -> Some (c, [||])
5810 | None -> None
5812 pan, { c with beyecolumns = beyecolumns; columns = columns }
5813 | _ -> state.x, conf
5815 let basename = Filename.basename state.path in
5816 adddoc basename pan (getanchor ())
5817 { conf with
5818 autoscrollstep =
5819 match state.autoscroll with
5820 | Some step -> step
5821 | None -> conf.autoscrollstep }
5822 (if conf.savebmarks then state.bookmarks else []);
5824 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5825 if basename <> path
5826 then adddoc path x y c bookmarks
5827 ) h;
5828 Buffer.add_string bb "</llppconfig>";
5830 load1 f;
5831 if Buffer.length bb > 0
5832 then
5834 let tmp = !confpath ^ ".tmp" in
5835 let oc = open_out_bin tmp in
5836 Buffer.output_buffer oc bb;
5837 close_out oc;
5838 Unix.rename tmp !confpath;
5839 with exn ->
5840 prerr_endline
5841 ("error while saving configuration: " ^ Printexc.to_string exn)
5843 end;;
5845 let () =
5846 Arg.parse
5847 (Arg.align
5848 [("-p", Arg.String (fun s -> state.password <- s) ,
5849 "<password> Set password");
5851 ("-f", Arg.String (fun s -> Config.fontpath := s),
5852 "<path> Set path to the user interface font");
5854 ("-c", Arg.String (fun s -> Config.confpath := s),
5855 "<path> Set path to the configuration file");
5857 ("-v", Arg.Unit (fun () ->
5858 Printf.printf
5859 "%s\nconfiguration path: %s\n"
5860 (version ())
5861 Config.defconfpath
5863 exit 0), " Print version and exit");
5866 (fun s -> state.path <- s)
5867 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5869 if String.length state.path = 0
5870 then (prerr_endline "file name missing"; exit 1);
5872 Config.load ();
5874 let _ = Glut.init Sys.argv in
5875 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5876 let () = Glut.initWindowSize conf.winw conf.winh in
5877 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5879 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5880 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5881 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5883 let csock, ssock =
5884 if not is_windows
5885 then
5886 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5887 else
5888 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5889 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5890 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5891 Unix.bind sock addr;
5892 Unix.listen sock 1;
5893 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5894 Unix.connect csock addr;
5895 let ssock, _ = Unix.accept sock in
5896 Unix.close sock;
5897 let opts sock =
5898 Unix.setsockopt sock Unix.TCP_NODELAY true;
5899 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5901 opts ssock;
5902 opts csock;
5903 ssock, csock
5906 let () = Glut.displayFunc display in
5907 let () = Glut.reshapeFunc reshape in
5908 let () = Glut.keyboardFunc keyboard in
5909 let () = Glut.specialFunc special in
5910 let () = Glut.idleFunc (Some idle) in
5911 let () = Glut.mouseFunc mouse in
5912 let () = Glut.motionFunc motion in
5913 let () = Glut.passiveMotionFunc pmotion in
5915 setcheckers conf.checkers;
5916 init ssock (
5917 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5918 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5919 !Config.wmclasshack, !Config.fontpath
5921 state.csock <- csock;
5922 state.ssock <- ssock;
5923 state.text <- "Opening " ^ state.path;
5924 setaalevel conf.aalevel;
5925 writeopen state.path state.password;
5926 state.uioh <- uioh;
5927 setfontsize fstate.fontsize;
5929 redirectstderr ();
5931 while true do
5933 Glut.mainLoop ();
5934 with
5935 | Glut.BadEnum "key in special_of_int" ->
5936 showtext '!' " LablGlut bug: special key not recognized";
5938 | Quit ->
5939 wcmd "quit" [];
5940 Config.save ();
5941 exit 0
5943 | exn when conf.redirectstderr ->
5944 let s =
5945 Printf.sprintf "exception %s\n%s"
5946 (Printexc.to_string exn)
5947 (Printexc.get_backtrace ())
5949 ignore (try
5950 Unix.single_write state.stderr s 0 (String.length s);
5951 with _ -> 0);
5952 exit 1
5953 done;