Move pdfinfo before initpdims
[llpp.git] / main.ml
blob9a47d2de068f3447976104a4ceeb773fac5fd248
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
9 let now = Unix.gettimeofday;;
11 exception Quit;;
13 type params = (angle * proportional * trimparams
14 * texcount * sliceheight * memsize
15 * colorspace * wmclasshack * fontpath)
16 and pageno = int
17 and width = int
18 and height = int
19 and leftx = int
20 and opaque = string
21 and recttype = int
22 and pixmapsize = int
23 and angle = int
24 and proportional = bool
25 and trimmargins = bool
26 and interpagespace = int
27 and texcount = int
28 and sliceheight = int
29 and gen = int
30 and top = float
31 and fontpath = string
32 and memsize = int
33 and aalevel = int
34 and wmclasshack = bool
35 and irect = (int * int * int * int)
36 and trimparams = (trimmargins * irect)
37 and colorspace = | Rgb | Bgr | Gray
40 type platform = | Punknown | Plinux | Pwindows | Posx | Psun
41 | Pfreebsd | Pdragonflybsd | Popenbsd | Pmingw | Pcygwin;;
43 external init : Unix.file_descr -> params -> unit = "ml_init";;
44 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
45 external copysel : string -> unit = "ml_copysel";;
46 external getpdimrect : int -> float array = "ml_getpdimrect";;
47 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
48 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
49 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
50 external measurestr : int -> string -> float = "ml_measure_string";;
51 external getmaxw : unit -> float = "ml_getmaxw";;
52 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
53 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
54 external platform : unit -> platform = "ml_platform";;
55 external setaalevel : int -> unit = "ml_setaalevel";;
57 let platform_to_string = function
58 | Punknown -> "unknown"
59 | Plinux -> "Linux"
60 | Pwindows -> "Windows"
61 | Posx -> "OSX"
62 | Psun -> "Sun"
63 | Pfreebsd -> "FreeBSD"
64 | Pdragonflybsd -> "DragonflyBSD"
65 | Popenbsd -> "OpenBSD"
66 | Pcygwin -> "Cygwin"
67 | Pmingw -> "MingW"
70 let platform = platform ();;
72 let is_windows =
73 match platform with
74 | Pwindows | Pmingw -> true
75 | _ -> false
78 type x = int
79 and y = int
80 and tilex = int
81 and tiley = int
82 and tileparams = (x * y * width * height * tilex * tiley)
85 external drawtile : tileparams -> string -> unit = "ml_drawtile";;
87 type mpos = int * int
88 and mstate =
89 | Msel of (mpos * mpos)
90 | Mpan of mpos
91 | Mscrolly | Mscrollx
92 | Mzoom of (int * int)
93 | Mzoomrect of (mpos * mpos)
94 | Mnone
97 type textentry = string * string * onhist option * onkey * ondone
98 and onkey = string -> int -> te
99 and ondone = string -> unit
100 and histcancel = unit -> unit
101 and onhist = ((histcmd -> string) * histcancel)
102 and histcmd = HCnext | HCprev | HCfirst | HClast
103 and te =
104 | TEstop
105 | TEdone of string
106 | TEcont of string
107 | TEswitch of textentry
110 type 'a circbuf =
111 { store : 'a array
112 ; mutable rc : int
113 ; mutable wc : int
114 ; mutable len : int
118 let bound v minv maxv =
119 max minv (min maxv v);
122 let cbnew n v =
123 { store = Array.create n v
124 ; rc = 0
125 ; wc = 0
126 ; len = 0
130 let drawstring size x y s =
131 Gl.enable `blend;
132 Gl.enable `texture_2d;
133 ignore (drawstr size x y s);
134 Gl.disable `blend;
135 Gl.disable `texture_2d;
138 let drawstring1 size x y s =
139 drawstr size x y s;
142 let drawstring2 size x y fmt =
143 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
146 let cbcap b = Array.length b.store;;
148 let cbput b v =
149 let cap = cbcap b in
150 b.store.(b.wc) <- v;
151 b.wc <- (b.wc + 1) mod cap;
152 b.rc <- b.wc;
153 b.len <- min (b.len + 1) cap;
156 let cbempty b = b.len = 0;;
158 let cbgetg b circular dir =
159 if cbempty b
160 then b.store.(0)
161 else
162 let rc = b.rc + dir in
163 let rc =
164 if circular
165 then (
166 if rc = -1
167 then b.len-1
168 else (
169 if rc = b.len
170 then 0
171 else rc
174 else max 0 (min rc (b.len-1))
176 b.rc <- rc;
177 b.store.(rc);
180 let cbget b = cbgetg b false;;
181 let cbgetc b = cbgetg b true;;
183 type page =
184 { pageno : int
185 ; pagedimno : int
186 ; pagew : int
187 ; pageh : int
188 ; pagex : int
189 ; pagey : int
190 ; pagevw : int
191 ; pagevh : int
192 ; pagedispx : int
193 ; pagedispy : int
197 let debugl l =
198 dolog "l %d dim=%d {" l.pageno l.pagedimno;
199 dolog " WxH %dx%d" l.pagew l.pageh;
200 dolog " vWxH %dx%d" l.pagevw l.pagevh;
201 dolog " pagex,y %d,%d" l.pagex l.pagey;
202 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
203 dolog "}";
206 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
207 dolog "rect {";
208 dolog " x0,y0=(% f, % f)" x0 y0;
209 dolog " x1,y1=(% f, % f)" x1 y1;
210 dolog " x2,y2=(% f, % f)" x2 y2;
211 dolog " x3,y3=(% f, % f)" x3 y3;
212 dolog "}";
215 type conf =
216 { mutable scrollbw : int
217 ; mutable scrollh : int
218 ; mutable icase : bool
219 ; mutable preload : bool
220 ; mutable pagebias : int
221 ; mutable verbose : bool
222 ; mutable debug : bool
223 ; mutable scrollstep : int
224 ; mutable maxhfit : bool
225 ; mutable crophack : bool
226 ; mutable autoscrollstep : int
227 ; mutable maxwait : float option
228 ; mutable hlinks : bool
229 ; mutable underinfo : bool
230 ; mutable interpagespace : interpagespace
231 ; mutable zoom : float
232 ; mutable presentation : bool
233 ; mutable angle : angle
234 ; mutable winw : int
235 ; mutable winh : int
236 ; mutable savebmarks : bool
237 ; mutable proportional : proportional
238 ; mutable trimmargins : trimmargins
239 ; mutable trimfuzz : irect
240 ; mutable memlimit : memsize
241 ; mutable texcount : texcount
242 ; mutable sliceheight : sliceheight
243 ; mutable thumbw : width
244 ; mutable jumpback : bool
245 ; mutable bgcolor : float * float * float
246 ; mutable bedefault : bool
247 ; mutable scrollbarinpm : bool
248 ; mutable tilew : int
249 ; mutable tileh : int
250 ; mutable mumemlimit : memsize
251 ; mutable checkers : bool
252 ; mutable aalevel : int
253 ; mutable urilauncher : string
254 ; mutable colorspace : colorspace
255 ; mutable invert : bool
256 ; mutable colorscale : float
260 type anchor = pageno * top;;
262 type outline = string * int * anchor;;
264 type rect = float * float * float * float * float * float * float * float;;
266 type tile = opaque * pixmapsize * elapsed
267 and elapsed = float;;
268 type pagemapkey = pageno * gen;;
269 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
270 and row = int
271 and col = int;;
273 let emptyanchor = (0, 0.0);;
275 type infochange = | Memused | Docinfo | Pdim;;
277 class type uioh = object
278 method display : unit
279 method key : int -> uioh
280 method special : Glut.special_key_t -> uioh
281 method button :
282 Glut.button_t -> Glut.mouse_button_state_t -> int -> int -> uioh
283 method motion : int -> int -> uioh
284 method pmotion : int -> int -> uioh
285 method infochanged : infochange -> unit
286 end;;
288 type mode =
289 | Birdseye of (conf * leftx * pageno * pageno * anchor)
290 | Textentry of (textentry * onleave)
291 | View
292 and onleave = leavetextentrystatus -> unit
293 and leavetextentrystatus = | Cancel | Confirm
294 and helpitem = string * int * action
295 and action =
296 | Noaction
297 | Action of (uioh -> uioh)
300 let isbirdseye = function Birdseye _ -> true | _ -> false;;
301 let istextentry = function Textentry _ -> true | _ -> false;;
303 type currently =
304 | Idle
305 | Loading of (page * gen)
306 | Tiling of (
307 page * opaque * colorspace * angle * gen * col * row * width * height
309 | Outlining of outline list
312 let nouioh : uioh = object (self)
313 method display = ()
314 method key _ = self
315 method special _ = self
316 method button _ _ _ _ = self
317 method motion _ _ = self
318 method pmotion _ _ = self
319 method infochanged _ = ()
320 end;;
322 type state =
323 { mutable csock : Unix.file_descr
324 ; mutable ssock : Unix.file_descr
325 ; mutable w : int
326 ; mutable x : int
327 ; mutable y : int
328 ; mutable scrollw : int
329 ; mutable hscrollh : int
330 ; mutable anchor : anchor
331 ; mutable maxy : int
332 ; mutable layout : page list
333 ; pagemap : (pagemapkey, opaque) Hashtbl.t
334 ; tilemap : (tilemapkey, tile) Hashtbl.t
335 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
336 ; mutable pdims : (pageno * width * height * leftx) list
337 ; mutable pagecount : int
338 ; mutable currently : currently
339 ; mutable mstate : mstate
340 ; mutable searchpattern : string
341 ; mutable rects : (pageno * recttype * rect) list
342 ; mutable rects1 : (pageno * recttype * rect) list
343 ; mutable text : string
344 ; mutable fullscreen : (width * height) option
345 ; mutable mode : mode
346 ; mutable uioh : uioh
347 ; mutable outlines : outline array
348 ; mutable bookmarks : outline list
349 ; mutable path : string
350 ; mutable password : string
351 ; mutable invalidated : int
352 ; mutable memused : memsize
353 ; mutable gen : gen
354 ; mutable throttle : (page list * int * float) option
355 ; mutable autoscroll : int option
356 ; mutable help : helpitem array
357 ; mutable docinfo : (int * string) list
358 ; mutable deadline : float
359 ; mutable texid : GlTex.texture_id option
360 ; hists : hists
361 ; mutable prevzoom : float
362 ; mutable progress : float
364 and hists =
365 { pat : string circbuf
366 ; pag : string circbuf
367 ; nav : anchor circbuf
371 let defconf =
372 { scrollbw = 7
373 ; scrollh = 12
374 ; icase = true
375 ; preload = true
376 ; pagebias = 0
377 ; verbose = false
378 ; debug = false
379 ; scrollstep = 24
380 ; maxhfit = true
381 ; crophack = false
382 ; autoscrollstep = 2
383 ; maxwait = None
384 ; hlinks = false
385 ; underinfo = false
386 ; interpagespace = 2
387 ; zoom = 1.0
388 ; presentation = false
389 ; angle = 0
390 ; winw = 900
391 ; winh = 900
392 ; savebmarks = true
393 ; proportional = true
394 ; trimmargins = false
395 ; trimfuzz = (0,0,0,0)
396 ; memlimit = 32 lsl 20
397 ; texcount = 256
398 ; sliceheight = 24
399 ; thumbw = 76
400 ; jumpback = true
401 ; bgcolor = (0.5, 0.5, 0.5)
402 ; bedefault = false
403 ; scrollbarinpm = true
404 ; tilew = 2048
405 ; tileh = 2048
406 ; mumemlimit = 128 lsl 20
407 ; checkers = true
408 ; aalevel = 8
409 ; urilauncher =
410 (match platform with
411 | Plinux | Pfreebsd | Pdragonflybsd | Popenbsd | Psun -> "xdg-open \"%s\""
412 | Posx -> "open \"%s\""
413 | Pwindows | Pcygwin | Pmingw -> "iexplore \"%s\""
414 | _ -> "")
415 ; colorspace = Rgb
416 ; invert = false
417 ; colorscale = 1.0
421 let conf = { defconf with angle = defconf.angle };;
423 type fontstate =
424 { mutable fontsize : int
425 ; mutable wwidth : float
426 ; mutable maxrows : int
430 let fstate =
431 { fontsize = 14
432 ; wwidth = nan
433 ; maxrows = -1
437 let setfontsize n =
438 fstate.fontsize <- n;
439 fstate.wwidth <- measurestr fstate.fontsize "w";
440 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
443 let gotouri uri =
444 if String.length conf.urilauncher = 0
445 then print_endline uri
446 else
447 let re = Str.regexp "%s" in
448 let command = Str.global_replace re uri conf.urilauncher in
449 let optic =
450 try Some (Unix.open_process_in command)
451 with exn ->
452 Printf.eprintf
453 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
454 flush stderr;
455 None
457 match optic with
458 | Some ic -> close_in ic
459 | None -> ()
462 let makehelp () =
463 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
464 Array.of_list (
465 let r = Str.regexp "\\(http://[^ ]+\\)" in
466 List.map (fun s ->
467 if (try Str.search_forward r s 0 with Not_found -> -1) >= 0
468 then
469 let uri = Str.matched_string s in
470 (s, 0, Action (fun u -> gotouri uri; u))
471 else s, 0, Noaction) strings
475 let state =
476 { csock = Unix.stdin
477 ; ssock = Unix.stdin
478 ; x = 0
479 ; y = 0
480 ; w = 0
481 ; scrollw = 0
482 ; hscrollh = 0
483 ; anchor = emptyanchor
484 ; layout = []
485 ; maxy = max_int
486 ; tilelru = Queue.create ()
487 ; pagemap = Hashtbl.create 10
488 ; tilemap = Hashtbl.create 10
489 ; pdims = []
490 ; pagecount = 0
491 ; currently = Idle
492 ; mstate = Mnone
493 ; rects = []
494 ; rects1 = []
495 ; text = ""
496 ; mode = View
497 ; fullscreen = None
498 ; searchpattern = ""
499 ; outlines = [||]
500 ; bookmarks = []
501 ; path = ""
502 ; password = ""
503 ; invalidated = 0
504 ; hists =
505 { nav = cbnew 10 (0, 0.0)
506 ; pat = cbnew 1 ""
507 ; pag = cbnew 1 ""
509 ; memused = 0
510 ; gen = 0
511 ; throttle = None
512 ; autoscroll = None
513 ; help = makehelp ()
514 ; docinfo = []
515 ; deadline = nan
516 ; texid = None
517 ; prevzoom = 1.0
518 ; progress = -1.0
519 ; uioh = nouioh
523 let vlog fmt =
524 if conf.verbose
525 then
526 Printf.kprintf prerr_endline fmt
527 else
528 Printf.kprintf ignore fmt
531 module G =
532 struct
533 let postRedisplay who =
534 if conf.verbose
535 then prerr_endline ("redisplay for " ^ who);
536 Glut.postRedisplay ();
538 end;;
540 let addchar s c =
541 let b = Buffer.create (String.length s + 1) in
542 Buffer.add_string b s;
543 Buffer.add_char b c;
544 Buffer.contents b;
547 let colorspace_of_string s =
548 match String.lowercase s with
549 | "rgb" -> Rgb
550 | "bgr" -> Bgr
551 | "gray" -> Gray
552 | _ -> failwith "invalid colorspace"
555 let int_of_colorspace = function
556 | Rgb -> 0
557 | Bgr -> 1
558 | Gray -> 2
561 let colorspace_of_int = function
562 | 0 -> Rgb
563 | 1 -> Bgr
564 | 2 -> Gray
565 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
568 let colorspace_to_string = function
569 | Rgb -> "rgb"
570 | Bgr -> "bgr"
571 | Gray -> "gray"
574 let intentry_with_suffix text key =
575 let c = Char.unsafe_chr key in
576 match Char.lowercase c with
577 | '0' .. '9' ->
578 let text = addchar text c in
579 TEcont text
581 | 'k' | 'm' | 'g' ->
582 let text = addchar text c in
583 TEcont text
585 | _ ->
586 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
587 TEcont text
590 let writecmd fd s =
591 let len = String.length s in
592 let n = 4 + len in
593 let b = Buffer.create n in
594 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
595 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
596 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
597 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
598 Buffer.add_string b s;
599 let s' = Buffer.contents b in
600 let n' = Unix.write fd s' 0 n in
601 if n' != n then failwith "write failed";
604 let readcmd fd =
605 let s = "xxxx" in
606 let n = Unix.read fd s 0 4 in
607 if n != 4 then failwith "incomplete read(len)";
608 let len = 0
609 lor (Char.code s.[0] lsl 24)
610 lor (Char.code s.[1] lsl 16)
611 lor (Char.code s.[2] lsl 8)
612 lor (Char.code s.[3] lsl 0)
614 let s = String.create len in
615 let n = Unix.read fd s 0 len in
616 if n != len then failwith "incomplete read(data)";
620 let makecmd s l =
621 let b = Buffer.create 10 in
622 Buffer.add_string b s;
623 let rec combine = function
624 | [] -> b
625 | x :: xs ->
626 Buffer.add_char b ' ';
627 let s =
628 match x with
629 | `b b -> if b then "1" else "0"
630 | `s s -> s
631 | `i i -> string_of_int i
632 | `f f -> string_of_float f
633 | `I f -> string_of_int (truncate f)
635 Buffer.add_string b s;
636 combine xs;
638 combine l;
641 let wcmd s l =
642 let cmd = Buffer.contents (makecmd s l) in
643 writecmd state.csock cmd;
646 let calcips h =
647 if conf.presentation
648 then
649 let d = conf.winh - h in
650 max 0 ((d + 1) / 2)
651 else
652 conf.interpagespace
655 let calcheight () =
656 let rec f pn ph pi fh l =
657 match l with
658 | (n, _, h, _) :: rest ->
659 let ips = calcips h in
660 let fh =
661 if conf.presentation
662 then fh+ips
663 else (
664 if isbirdseye state.mode && pn = 0
665 then fh + ips
666 else fh
669 let fh = fh + ((n - pn) * (ph + pi)) in
670 f n h ips fh rest;
672 | [] ->
673 let inc =
674 if conf.presentation || (isbirdseye state.mode && pn = 0)
675 then 0
676 else -pi
678 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
679 max 0 fh
681 let fh = f 0 0 0 0 state.pdims in
685 let getpageyh pageno =
686 let rec f pn ph pi y l =
687 match l with
688 | (n, _, h, _) :: rest ->
689 let ips = calcips h in
690 if n >= pageno
691 then
692 let h = if n = pageno then h else ph in
693 if conf.presentation && n = pageno
694 then
695 y + (pageno - pn) * (ph + pi) + pi, h
696 else
697 y + (pageno - pn) * (ph + pi), h
698 else
699 let y = y + (if conf.presentation then pi else 0) in
700 let y = y + (n - pn) * (ph + pi) in
701 f n h ips y rest
703 | [] ->
704 y + (pageno - pn) * (ph + pi), ph
706 f 0 0 0 0 state.pdims
709 let getpagedim pageno =
710 let rec f ppdim l =
711 match l with
712 | (n, _, _, _) as pdim :: rest ->
713 if n >= pageno
714 then (if n = pageno then pdim else ppdim)
715 else f pdim rest
717 | [] -> ppdim
719 f (-1, -1, -1, -1) state.pdims
722 let getpageh pageno =
723 let _, _, h, _ = getpagedim pageno in
727 let getpagew pageno =
728 let _, w, _, _ = getpagedim pageno in
732 let getpagey pageno = fst (getpageyh pageno);;
734 let layout y sh =
735 let sh = sh - state.hscrollh in
736 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
737 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
738 match pdims with
739 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
740 let ips = calcips h in
741 let yinc =
742 if conf.presentation || (isbirdseye state.mode && pageno = 0)
743 then ips
744 else 0
746 (w, h, ips, xoff), rest, pdimno + 1, yinc
747 | _ ->
748 prev, pdims, pdimno, 0
750 let dy = dy + yinc in
751 let py = py + yinc in
752 if pageno = state.pagecount || dy >= sh
753 then
754 accu
755 else
756 let vy = y + dy in
757 if py + h <= vy - yinc
758 then
759 let py = py + h + ips in
760 let dy = max 0 (py - y) in
761 f ~pageno:(pageno+1)
762 ~pdimno
763 ~prev:curr
766 ~pdims:rest
767 ~accu
768 else
769 let pagey = vy - py in
770 let pagevh = h - pagey in
771 let pagevh = min (sh - dy) pagevh in
772 let off = if yinc > 0 then py - vy else 0 in
773 let py = py + h + ips in
774 let pagex, dx =
775 let xoff = xoff +
776 if state.w < conf.winw - state.scrollw
777 then (conf.winw - state.scrollw - state.w) / 2
778 else 0
780 let dispx = xoff + state.x in
781 if dispx < 0
782 then (-dispx, 0)
783 else (0, dispx)
785 let pagevw =
786 let lw = w - pagex in
787 min lw (conf.winw - state.scrollw)
789 let e =
790 { pageno = pageno
791 ; pagedimno = pdimno
792 ; pagew = w
793 ; pageh = h
794 ; pagex = pagex
795 ; pagey = pagey + off
796 ; pagevw = pagevw
797 ; pagevh = pagevh - off
798 ; pagedispx = dx
799 ; pagedispy = dy + off
802 let accu = e :: accu in
803 f ~pageno:(pageno+1)
804 ~pdimno
805 ~prev:curr
807 ~dy:(dy+pagevh+ips)
808 ~pdims:rest
809 ~accu
811 if state.invalidated = 0
812 then (
813 let accu =
815 ~pageno:0
816 ~pdimno:~-1
817 ~prev:(0,0,0,0)
818 ~py:0
819 ~dy:0
820 ~pdims:state.pdims
821 ~accu:[]
823 List.rev accu
825 else
829 let clamp incr =
830 let y = state.y + incr in
831 let y = max 0 y in
832 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
836 let getopaque pageno =
837 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
838 with Not_found -> None
841 let putopaque pageno opaque =
842 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
845 let itertiles l f =
846 let tilex = l.pagex mod conf.tilew in
847 let tiley = l.pagey mod conf.tileh in
849 let col = l.pagex / conf.tilew in
850 let row = l.pagey / conf.tileh in
852 let vw =
853 let a = l.pagew - l.pagex in
854 let b = conf.winw - state.scrollw in
855 min a b
856 and vh = l.pagevh in
858 let rec rowloop row y0 dispy h =
859 if h = 0
860 then ()
861 else (
862 let dh = conf.tileh - y0 in
863 let dh = min h dh in
864 let rec colloop col x0 dispx w =
865 if w = 0
866 then ()
867 else (
868 let dw = conf.tilew - x0 in
869 let dw = min w dw in
871 f col row dispx dispy x0 y0 dw dh;
872 colloop (col+1) 0 (dispx+dw) (w-dw)
875 colloop col tilex l.pagedispx vw;
876 rowloop (row+1) 0 (dispy+dh) (h-dh)
879 if vw > 0 && vh > 0
880 then rowloop row tiley l.pagedispy vh;
883 let gettileopaque l col row =
884 let key =
885 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
887 try Some (Hashtbl.find state.tilemap key)
888 with Not_found -> None
891 let puttileopaque l col row gen colorspace angle opaque size elapsed =
892 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
893 Hashtbl.add state.tilemap key (opaque, size, elapsed)
896 let drawtiles l color =
897 GlDraw.color color;
898 let f col row x y tilex tiley w h =
899 match gettileopaque l col row with
900 | Some (opaque, _, t) ->
901 let params = x, y, w, h, tilex, tiley in
902 if conf.invert
903 then (
904 Gl.enable `blend;
905 GlFunc.blend_func `zero `one_minus_src_color;
907 drawtile params opaque;
908 if conf.invert
909 then Gl.disable `blend;
910 if conf.debug
911 then (
912 let s = Printf.sprintf
913 "%d[%d,%d] %f sec"
914 l.pageno col row t
916 let ww = fstate.wwidth in
917 GlMisc.push_attrib [`current];
918 GlDraw.color (0.0, 0.0, 0.0);
919 GlDraw.rect
920 (float (x-2), float (y-2))
921 (float (x+2) +. ww, float (y + fstate.fontsize + 2));
922 GlDraw.color (1.0, 1.0, 1.0);
923 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
924 GlMisc.pop_attrib ();
927 | _ ->
928 let w =
929 let lw = conf.winw - state.scrollw - x in
930 min lw w
931 and h =
932 let lh = conf.winh - y in
933 min lh h
935 Gl.enable `texture_2d;
936 begin match state.texid with
937 | Some id ->
938 GlTex.bind_texture `texture_2d id;
939 let x0 = float x
940 and y0 = float y
941 and x1 = float (x+w)
942 and y1 = float (y+h) in
944 let tw = float w /. 64.0
945 and th = float h /. 64.0 in
946 let tx0 = float tilex /. 64.0
947 and ty0 = float tiley /. 64.0 in
948 let tx1 = tx0 +. tw
949 and ty1 = ty0 +. th in
950 GlDraw.begins `quads;
951 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
952 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
953 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
954 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
955 GlDraw.ends ();
957 Gl.disable `texture_2d;
958 | None ->
959 GlDraw.color (1.0, 1.0, 1.0);
960 GlDraw.rect
961 (float x, float y)
962 (float (x+w), float (y+h));
963 end;
964 if w > 128 && h > fstate.fontsize + 10
965 then (
966 GlDraw.color (0.0, 0.0, 0.0);
967 let c, r =
968 if conf.verbose
969 then (col*conf.tilew, row*conf.tileh)
970 else col, row
972 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
974 GlDraw.color color;
976 itertiles l f
979 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
981 let tilevisible1 l x y =
982 let ax0 = l.pagex
983 and ax1 = l.pagex + l.pagevw
984 and ay0 = l.pagey
985 and ay1 = l.pagey + l.pagevh in
987 let bx0 = x
988 and by0 = y in
989 let bx1 = min (bx0 + conf.tilew) l.pagew
990 and by1 = min (by0 + conf.tileh) l.pageh in
992 let rx0 = max ax0 bx0
993 and ry0 = max ay0 by0
994 and rx1 = min ax1 bx1
995 and ry1 = min ay1 by1 in
997 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
998 nonemptyintersection
1001 let tilevisible layout n x y =
1002 let rec findpageinlayout = function
1003 | l :: _ when l.pageno = n -> tilevisible1 l x y
1004 | _ :: rest -> findpageinlayout rest
1005 | [] -> false
1007 findpageinlayout layout
1010 let tileready l x y =
1011 tilevisible1 l x y &&
1012 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1015 let tilepage n p layout =
1016 let rec loop = function
1017 | l :: rest ->
1018 if l.pageno = n
1019 then
1020 let f col row _ _ _ _ _ _ =
1021 if state.currently = Idle
1022 then
1023 match gettileopaque l col row with
1024 | Some _ -> ()
1025 | None ->
1026 let x = col*conf.tilew
1027 and y = row*conf.tileh in
1028 let w =
1029 let w = l.pagew - x in
1030 min w conf.tilew
1032 let h =
1033 let h = l.pageh - y in
1034 min h conf.tileh
1036 wcmd "tile"
1037 [`s p
1038 ;`i x
1039 ;`i y
1040 ;`i w
1041 ;`i h
1043 state.currently <-
1044 Tiling (
1045 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1046 conf.tilew, conf.tileh
1049 itertiles l f;
1050 else
1051 loop rest
1053 | [] -> ()
1055 if state.invalidated = 0 then loop layout;
1058 let preloadlayout visiblepages =
1059 let presentation = conf.presentation in
1060 let interpagespace = conf.interpagespace in
1061 let maxy = state.maxy in
1062 conf.presentation <- false;
1063 conf.interpagespace <- 0;
1064 state.maxy <- calcheight ();
1065 let y =
1066 match visiblepages with
1067 | [] -> 0
1068 | l :: _ -> getpagey l.pageno + l.pagey
1070 let y = if y < conf.winh then 0 else y - conf.winh in
1071 let h = state.y - y + conf.winh*3 in
1072 let pages = layout y h in
1073 conf.presentation <- presentation;
1074 conf.interpagespace <- interpagespace;
1075 state.maxy <- maxy;
1076 pages
1079 let load pages =
1080 let rec loop pages =
1081 if state.currently != Idle
1082 then ()
1083 else
1084 match pages with
1085 | l :: rest ->
1086 begin match getopaque l.pageno with
1087 | None ->
1088 wcmd "page" [`i l.pageno; `i l.pagedimno];
1089 state.currently <- Loading (l, state.gen);
1090 | Some opaque ->
1091 tilepage l.pageno opaque pages;
1092 loop rest
1093 end;
1094 | _ -> ()
1096 if state.invalidated = 0 then loop pages
1099 let preload pages =
1100 load pages;
1101 if conf.preload && state.currently = Idle
1102 then load (preloadlayout pages);
1105 let layoutready layout =
1106 let rec fold all ls =
1107 all && match ls with
1108 | l :: rest ->
1109 let seen = ref false in
1110 let allvisible = ref true in
1111 let foo col row _ _ _ _ _ _ =
1112 seen := true;
1113 allvisible := !allvisible &&
1114 begin match gettileopaque l col row with
1115 | Some _ -> true
1116 | None -> false
1119 itertiles l foo;
1120 fold (!seen && !allvisible) rest
1121 | [] -> true
1123 let alltilesvisible = fold true layout in
1124 alltilesvisible;
1127 let gotoy y =
1128 let y = bound y 0 state.maxy in
1129 let y, layout, proceed =
1130 match conf.maxwait with
1131 | Some time ->
1132 begin match state.throttle with
1133 | None ->
1134 let layout = layout y conf.winh in
1135 let ready = layoutready layout in
1136 if not ready
1137 then (
1138 load layout;
1139 state.throttle <- Some (layout, y, now ());
1141 else G.postRedisplay "gotoy showall (None)";
1142 y, layout, ready
1143 | Some (_, _, started) ->
1144 let dt = now () -. started in
1145 if dt > time
1146 then (
1147 state.throttle <- None;
1148 let layout = layout y conf.winh in
1149 load layout;
1150 G.postRedisplay "maxwait";
1151 y, layout, true
1153 else -1, [], false
1156 | None ->
1157 let layout = layout y conf.winh in
1158 if true || layoutready layout
1159 then G.postRedisplay "gotoy ready";
1160 y, layout, true
1162 if proceed
1163 then (
1164 state.y <- y;
1165 state.layout <- layout;
1166 begin match state.mode with
1167 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1168 if not (pagevisible layout pageno)
1169 then (
1170 match state.layout with
1171 | [] -> ()
1172 | l :: _ ->
1173 state.mode <- Birdseye (
1174 conf, leftx, l.pageno, hooverpageno, anchor
1177 | _ -> ()
1178 end;
1179 preload layout;
1183 let conttiling pageno opaque =
1184 tilepage pageno opaque
1185 (if conf.preload then preloadlayout state.layout else state.layout)
1188 let gotoy_and_clear_text y =
1189 gotoy y;
1190 if not conf.verbose then state.text <- "";
1193 let getanchor () =
1194 match state.layout with
1195 | [] -> emptyanchor
1196 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1199 let getanchory (n, top) =
1200 let y, h = getpageyh n in
1201 y + (truncate (top *. float h));
1204 let gotoanchor anchor =
1205 gotoy (getanchory anchor);
1208 let addnav () =
1209 cbput state.hists.nav (getanchor ());
1212 let getnav dir =
1213 let anchor = cbgetc state.hists.nav dir in
1214 getanchory anchor;
1217 let gotopage n top =
1218 let y, h = getpageyh n in
1219 gotoy_and_clear_text (y + (truncate (top *. float h)));
1222 let gotopage1 n top =
1223 let y = getpagey n in
1224 gotoy_and_clear_text (y + top);
1227 let invalidate () =
1228 state.layout <- [];
1229 state.pdims <- [];
1230 state.rects <- [];
1231 state.rects1 <- [];
1232 state.invalidated <- state.invalidated + 1;
1235 let writeopen path password =
1236 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1239 let opendoc path password =
1240 invalidate ();
1241 state.path <- path;
1242 state.password <- password;
1243 state.gen <- state.gen + 1;
1244 state.docinfo <- [];
1246 setaalevel conf.aalevel;
1247 writeopen path password;
1248 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1249 wcmd "geometry" [`i state.w; `i conf.winh];
1252 let scalecolor c =
1253 let c = c *. conf.colorscale in
1254 (c, c, c);
1257 let scalecolor2 (r, g, b) =
1258 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1261 let represent () =
1262 state.maxy <- calcheight ();
1263 state.hscrollh <-
1264 if state.w <= conf.winw - state.scrollw
1265 then 0
1266 else state.scrollw
1268 match state.mode with
1269 | Birdseye (_, _, pageno, _, _) ->
1270 let y, h = getpageyh pageno in
1271 let top = (conf.winh - h) / 2 in
1272 gotoy (max 0 (y - top))
1273 | _ -> gotoanchor state.anchor
1276 let reshape =
1277 let firsttime = ref true in
1278 fun ~w ~h ->
1279 GlDraw.viewport 0 0 w h;
1280 if state.invalidated = 0 && not !firsttime
1281 then state.anchor <- getanchor ();
1283 firsttime := false;
1284 conf.winw <- w;
1285 let w = truncate (float w *. conf.zoom) - state.scrollw in
1286 let w = max w 2 in
1287 state.w <- w;
1288 conf.winh <- h;
1289 setfontsize fstate.fontsize;
1290 GlMat.mode `modelview;
1291 GlMat.load_identity ();
1293 GlMat.mode `projection;
1294 GlMat.load_identity ();
1295 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1296 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1297 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1299 invalidate ();
1300 wcmd "geometry" [`i w; `i h];
1303 let enttext () =
1304 let len = String.length state.text in
1305 let drawstring s =
1306 let hscrollh =
1307 match state.mode with
1308 | View -> state.hscrollh
1309 | _ -> 0
1311 let rect x w =
1312 GlDraw.rect
1313 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1314 (x+.w, float (conf.winh - hscrollh))
1317 let w = float (conf.winw - state.scrollw - 1) in
1318 if state.progress >= 0.0 && state.progress < 1.0
1319 then (
1320 GlDraw.color (0.3, 0.3, 0.3);
1321 let w1 = w *. state.progress in
1322 rect 0.0 w1;
1323 GlDraw.color (0.0, 0.0, 0.0);
1324 rect w1 (w-.w1)
1326 else (
1327 GlDraw.color (0.0, 0.0, 0.0);
1328 rect 0.0 w;
1331 GlDraw.color (1.0, 1.0, 1.0);
1332 drawstring fstate.fontsize
1333 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1335 match state.mode with
1336 | Textentry ((prefix, text, _, _, _), _) ->
1337 let s =
1338 if len > 0
1339 then
1340 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1341 else
1342 Printf.sprintf "%s%s_" prefix text
1344 drawstring s
1346 | _ ->
1347 if len > 0 then drawstring state.text
1350 let showtext c s =
1351 state.text <- Printf.sprintf "%c%s" c s;
1352 G.postRedisplay "showtext";
1355 let gctiles () =
1356 let len = Queue.length state.tilelru in
1357 let rec loop qpos =
1358 if state.memused <= conf.memlimit
1359 then ()
1360 else (
1361 if qpos < len
1362 then
1363 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1364 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1366 gen = state.gen
1367 && colorspace = conf.colorspace
1368 && angle = conf.angle
1369 && pagew = getpagew n
1370 && pageh = getpageh n
1371 && (
1372 let layout =
1373 if conf.preload
1374 then preloadlayout state.layout
1375 else state.layout
1377 let x = col*conf.tilew
1378 and y = row*conf.tileh in
1379 tilevisible layout n x y
1381 then Queue.push lruitem state.tilelru
1382 else (
1383 wcmd "freetile" [`s p];
1384 state.memused <- state.memused - s;
1385 state.uioh#infochanged Memused;
1386 Hashtbl.remove state.tilemap k;
1388 loop (qpos+1)
1391 loop 0
1394 let flushtiles () =
1395 Queue.iter (fun (k, p, s) ->
1396 wcmd "freetile" [`s p];
1397 state.memused <- state.memused - s;
1398 state.uioh#infochanged Memused;
1399 Hashtbl.remove state.tilemap k;
1400 ) state.tilelru;
1401 Queue.clear state.tilelru;
1402 load state.layout;
1405 let logcurrently = function
1406 | Idle -> dolog "Idle"
1407 | Loading (l, gen) ->
1408 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1409 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1410 dolog
1411 "Tiling %d[%d,%d] page=%s cs=%s angle"
1412 l.pageno col row pageopaque
1413 (colorspace_to_string colorspace)
1415 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1416 angle gen conf.angle state.gen
1417 tilew tileh
1418 conf.tilew conf.tileh
1420 | Outlining _ ->
1421 dolog "outlining"
1424 let act cmds =
1425 (* dolog "%S" cmds; *)
1426 let op, args =
1427 let spacepos =
1428 try String.index cmds ' '
1429 with Not_found -> -1
1431 if spacepos = -1
1432 then cmds, ""
1433 else
1434 let l = String.length cmds in
1435 let op = String.sub cmds 0 spacepos in
1436 op, begin
1437 if l - spacepos < 2 then ""
1438 else String.sub cmds (spacepos+1) (l-spacepos-1)
1441 match op with
1442 | "clear" ->
1443 state.uioh#infochanged Pdim;
1444 state.pdims <- [];
1446 | "clearrects" ->
1447 state.rects <- state.rects1;
1448 G.postRedisplay "clearrects";
1450 | "continue" ->
1451 let n = Scanf.sscanf args "%u" (fun n -> n) in
1452 state.pagecount <- n;
1453 state.invalidated <- state.invalidated - 1;
1454 begin match state.currently with
1455 | Outlining l ->
1456 state.currently <- Idle;
1457 state.outlines <- Array.of_list (List.rev l)
1458 | _ -> ()
1459 end;
1460 if state.invalidated = 0
1461 then represent ();
1462 if conf.maxwait = None
1463 then G.postRedisplay "continue";
1465 | "title" ->
1466 Glut.setWindowTitle args
1468 | "msg" ->
1469 showtext ' ' args
1471 | "vmsg" ->
1472 if conf.verbose
1473 then showtext ' ' args
1475 | "progress" ->
1476 let progress, text = Scanf.sscanf args "%f %n"
1477 (fun f pos ->
1478 f, String.sub args pos (String.length args - pos)
1481 state.text <- text;
1482 state.progress <- progress;
1483 G.postRedisplay "progress"
1485 | "firstmatch" ->
1486 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1487 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1488 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1489 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1491 let y = (getpagey pageno) + truncate y0 in
1492 addnav ();
1493 gotoy y;
1494 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1496 | "match" ->
1497 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1498 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1499 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1500 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1502 state.rects1 <-
1503 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1505 | "page" ->
1506 let pageopaque, t = Scanf.sscanf args "%s %f" (fun p t -> p, t) in
1507 begin match state.currently with
1508 | Loading (l, gen) ->
1509 vlog "page %d took %f sec" l.pageno t;
1510 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1511 begin match state.throttle with
1512 | None ->
1513 let preloadedpages =
1514 if conf.preload
1515 then preloadlayout state.layout
1516 else state.layout
1518 let evict () =
1519 let module IntSet =
1520 Set.Make (struct type t = int let compare = (-) end) in
1521 let set =
1522 List.fold_left (fun s l -> IntSet.add l.pageno s)
1523 IntSet.empty preloadedpages
1525 let evictedpages =
1526 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1527 if not (IntSet.mem pageno set)
1528 then (
1529 wcmd "freepage" [`s opaque];
1530 key :: accu
1532 else accu
1533 ) state.pagemap []
1535 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1537 evict ();
1538 state.currently <- Idle;
1539 if gen = state.gen
1540 then (
1541 tilepage l.pageno pageopaque state.layout;
1542 load state.layout;
1543 load preloadedpages;
1544 if pagevisible state.layout l.pageno
1545 && layoutready state.layout
1546 then G.postRedisplay "page";
1549 | Some (layout, _, _) ->
1550 state.currently <- Idle;
1551 tilepage l.pageno pageopaque layout;
1552 load state.layout
1553 end;
1555 | _ ->
1556 dolog "Inconsistent loading state";
1557 logcurrently state.currently;
1558 raise Quit;
1561 | "tile" ->
1562 let (x, y, opaque, size, t) =
1563 Scanf.sscanf args "%u %u %s %u %f"
1564 (fun x y p size t -> (x, y, p, size, t))
1566 begin match state.currently with
1567 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1568 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1570 if tilew != conf.tilew || tileh != conf.tileh
1571 then (
1572 wcmd "freetile" [`s opaque];
1573 state.currently <- Idle;
1574 load state.layout;
1576 else (
1577 puttileopaque l col row gen cs angle opaque size t;
1578 state.memused <- state.memused + size;
1579 state.uioh#infochanged Memused;
1580 gctiles ();
1581 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1582 opaque, size) state.tilelru;
1584 state.currently <- Idle;
1585 if gen = state.gen
1586 && conf.colorspace = cs
1587 && conf.angle = angle
1588 && tilevisible state.layout l.pageno x y
1589 then conttiling l.pageno pageopaque;
1591 begin match state.throttle with
1592 | None ->
1593 preload state.layout;
1594 if gen = state.gen
1595 && conf.colorspace = cs
1596 && conf.angle = angle
1597 && tilevisible state.layout l.pageno x y
1598 then G.postRedisplay "tile nothrottle";
1600 | Some (layout, y, _) ->
1601 let ready = layoutready layout in
1602 if ready
1603 then (
1604 state.y <- y;
1605 state.layout <- layout;
1606 state.throttle <- None;
1607 G.postRedisplay "throttle";
1609 else load layout;
1610 end;
1613 | _ ->
1614 dolog "Inconsistent tiling state";
1615 logcurrently state.currently;
1616 raise Quit;
1619 | "pdim" ->
1620 let pdim =
1621 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1623 state.uioh#infochanged Pdim;
1624 state.pdims <- pdim :: state.pdims
1626 | "o" ->
1627 let (l, n, t, h, pos) =
1628 Scanf.sscanf args "%u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
1630 let s = String.sub args pos (String.length args - pos) in
1631 let outline = (s, l, (n, float t /. float h)) in
1632 begin match state.currently with
1633 | Outlining outlines ->
1634 state.currently <- Outlining (outline :: outlines)
1635 | Idle ->
1636 state.currently <- Outlining [outline]
1637 | currently ->
1638 dolog "invalid outlining state";
1639 logcurrently currently
1642 | "info" ->
1643 state.docinfo <- (1, args) :: state.docinfo
1645 | "infoend" ->
1646 state.uioh#infochanged Docinfo;
1647 state.docinfo <- List.rev state.docinfo
1649 | _ ->
1650 dolog "unknown cmd `%S'" cmds
1653 let idle () =
1654 if state.deadline == nan then state.deadline <- now ();
1655 let rec loop delay =
1656 let timeout =
1657 if delay > 0.0
1658 then max 0.0 (state.deadline -. now ())
1659 else 0.0
1661 let r, _, _ = Unix.select [state.csock] [] [] timeout in
1662 begin match r with
1663 | [] ->
1664 begin match state.autoscroll with
1665 | Some step when step != 0 ->
1666 let y = state.y + step in
1667 let y =
1668 if y < 0
1669 then state.maxy
1670 else if y >= state.maxy then 0 else y
1672 gotoy y;
1673 if state.mode = View
1674 then state.text <- "";
1675 state.deadline <- state.deadline +. 0.005;
1677 | _ ->
1678 state.deadline <- state.deadline +. delay;
1679 end;
1681 | _ ->
1682 let cmd = readcmd state.csock in
1683 act cmd;
1684 loop 0.0
1685 end;
1686 in loop 0.007
1689 let onhist cb =
1690 let rc = cb.rc in
1691 let action = function
1692 | HCprev -> cbget cb ~-1
1693 | HCnext -> cbget cb 1
1694 | HCfirst -> cbget cb ~-(cb.rc)
1695 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1696 and cancel () = cb.rc <- rc
1697 in (action, cancel)
1700 let search pattern forward =
1701 if String.length pattern > 0
1702 then
1703 let pn, py =
1704 match state.layout with
1705 | [] -> 0, 0
1706 | l :: _ ->
1707 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1709 let cmd =
1710 let b = makecmd "search"
1711 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1713 Buffer.add_char b ',';
1714 Buffer.add_string b pattern;
1715 Buffer.add_char b '\000';
1716 Buffer.contents b;
1718 writecmd state.csock cmd;
1721 let intentry text key =
1722 let c = Char.unsafe_chr key in
1723 match c with
1724 | '0' .. '9' ->
1725 let text = addchar text c in
1726 TEcont text
1728 | _ ->
1729 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1730 TEcont text
1733 let textentry text key =
1734 let c = Char.unsafe_chr key in
1735 match c with
1736 | _ when key >= 32 && key < 127 ->
1737 let text = addchar text c in
1738 TEcont text
1740 | _ ->
1741 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1742 TEcont text
1745 let reqlayout angle proportional =
1746 match state.throttle with
1747 | None ->
1748 if state.invalidated = 0 then state.anchor <- getanchor ();
1749 conf.angle <- angle mod 360;
1750 conf.proportional <- proportional;
1751 invalidate ();
1752 wcmd "reqlayout" [`i conf.angle; `b proportional];
1753 | _ -> ()
1756 let settrim trimmargins trimfuzz =
1757 if state.invalidated = 0 then state.anchor <- getanchor ();
1758 conf.trimmargins <- trimmargins;
1759 conf.trimfuzz <- trimfuzz;
1760 let x0, y0, x1, y1 = trimfuzz in
1761 invalidate ();
1762 wcmd "settrim" [
1763 `b conf.trimmargins;
1764 `i x0;
1765 `i y0;
1766 `i x1;
1767 `i y1;
1769 Hashtbl.iter (fun _ opaque ->
1770 wcmd "freepage" [`s opaque];
1771 ) state.pagemap;
1772 Hashtbl.clear state.pagemap;
1775 let setzoom zoom =
1776 match state.throttle with
1777 | None ->
1778 let zoom = max 0.01 zoom in
1779 if zoom <> conf.zoom
1780 then (
1781 state.prevzoom <- conf.zoom;
1782 let relx =
1783 if zoom <= 1.0
1784 then (state.x <- 0; 0.0)
1785 else float state.x /. float state.w
1787 conf.zoom <- zoom;
1788 reshape conf.winw conf.winh;
1789 if zoom > 1.0
1790 then (
1791 let x = relx *. float state.w in
1792 state.x <- truncate x;
1794 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1797 | _ -> ()
1800 let enterbirdseye () =
1801 let zoom = float conf.thumbw /. float conf.winw in
1802 let birdseyepageno =
1803 let cy = conf.winh / 2 in
1804 let fold = function
1805 | [] -> 0
1806 | l :: rest ->
1807 let rec fold best = function
1808 | [] -> best.pageno
1809 | l :: rest ->
1810 let d = cy - (l.pagedispy + l.pagevh/2)
1811 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1812 if abs d < abs dbest
1813 then fold l rest
1814 else best.pageno
1815 in fold l rest
1817 fold state.layout
1819 state.mode <- Birdseye (
1820 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1822 conf.zoom <- zoom;
1823 conf.presentation <- false;
1824 conf.interpagespace <- 10;
1825 conf.hlinks <- false;
1826 state.x <- 0;
1827 state.mstate <- Mnone;
1828 conf.maxwait <- None;
1829 Glut.setCursor Glut.CURSOR_INHERIT;
1830 if conf.verbose
1831 then
1832 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1833 (100.0*.zoom)
1834 else
1835 state.text <- ""
1837 reshape conf.winw conf.winh;
1840 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1841 state.mode <- View;
1842 conf.zoom <- c.zoom;
1843 conf.presentation <- c.presentation;
1844 conf.interpagespace <- c.interpagespace;
1845 conf.maxwait <- c.maxwait;
1846 conf.hlinks <- c.hlinks;
1847 state.x <- leftx;
1848 if conf.verbose
1849 then
1850 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1851 (100.0*.conf.zoom)
1853 reshape conf.winw conf.winh;
1854 state.anchor <- if goback then anchor else (pageno, 0.0);
1857 let togglebirdseye () =
1858 match state.mode with
1859 | Birdseye vals -> leavebirdseye vals true
1860 | View -> enterbirdseye ()
1861 | _ -> ()
1864 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1865 let pageno = max 0 (pageno - 1) in
1866 let rec loop = function
1867 | [] -> gotopage1 pageno 0
1868 | l :: _ when l.pageno = pageno ->
1869 if l.pagedispy >= 0 && l.pagey = 0
1870 then G.postRedisplay "upbirdseye"
1871 else gotopage1 pageno 0
1872 | _ :: rest -> loop rest
1874 loop state.layout;
1875 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1878 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1879 let pageno = min (state.pagecount - 1) (pageno + 1) in
1880 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1881 let rec loop = function
1882 | [] ->
1883 let y, h = getpageyh pageno in
1884 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1885 gotoy (clamp dy)
1886 | l :: _ when l.pageno = pageno ->
1887 if l.pagevh != l.pageh
1888 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1889 else G.postRedisplay "downbirdseye"
1890 | _ :: rest -> loop rest
1892 loop state.layout
1895 let optentry mode _ key =
1896 let btos b = if b then "on" else "off" in
1897 let c = Char.unsafe_chr key in
1898 match c with
1899 | 's' ->
1900 let ondone s =
1901 try conf.scrollstep <- int_of_string s with exc ->
1902 state.text <- Printf.sprintf "bad integer `%s': %s"
1903 s (Printexc.to_string exc)
1905 TEswitch ("scroll step: ", "", None, intentry, ondone)
1907 | 'A' ->
1908 let ondone s =
1910 conf.autoscrollstep <- int_of_string s;
1911 if state.autoscroll <> None
1912 then state.autoscroll <- Some conf.autoscrollstep
1913 with exc ->
1914 state.text <- Printf.sprintf "bad integer `%s': %s"
1915 s (Printexc.to_string exc)
1917 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1919 | 'Z' ->
1920 let ondone s =
1922 let zoom = float (int_of_string s) /. 100.0 in
1923 setzoom zoom
1924 with exc ->
1925 state.text <- Printf.sprintf "bad integer `%s': %s"
1926 s (Printexc.to_string exc)
1928 TEswitch ("zoom: ", "", None, intentry, ondone)
1930 | 't' ->
1931 let ondone s =
1933 conf.thumbw <- bound (int_of_string s) 2 4096;
1934 state.text <-
1935 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1936 begin match mode with
1937 | Birdseye beye ->
1938 leavebirdseye beye false;
1939 enterbirdseye ();
1940 | _ -> ();
1942 with exc ->
1943 state.text <- Printf.sprintf "bad integer `%s': %s"
1944 s (Printexc.to_string exc)
1946 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
1948 | 'R' ->
1949 let ondone s =
1950 match try
1951 Some (int_of_string s)
1952 with exc ->
1953 state.text <- Printf.sprintf "bad integer `%s': %s"
1954 s (Printexc.to_string exc);
1955 None
1956 with
1957 | Some angle -> reqlayout angle conf.proportional
1958 | None -> ()
1960 TEswitch ("rotation: ", "", None, intentry, ondone)
1962 | 'i' ->
1963 conf.icase <- not conf.icase;
1964 TEdone ("case insensitive search " ^ (btos conf.icase))
1966 | 'p' ->
1967 conf.preload <- not conf.preload;
1968 gotoy state.y;
1969 TEdone ("preload " ^ (btos conf.preload))
1971 | 'v' ->
1972 conf.verbose <- not conf.verbose;
1973 TEdone ("verbose " ^ (btos conf.verbose))
1975 | 'd' ->
1976 conf.debug <- not conf.debug;
1977 TEdone ("debug " ^ (btos conf.debug))
1979 | 'h' ->
1980 conf.maxhfit <- not conf.maxhfit;
1981 state.maxy <-
1982 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1983 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1985 | 'c' ->
1986 conf.crophack <- not conf.crophack;
1987 TEdone ("crophack " ^ btos conf.crophack)
1989 | 'a' ->
1990 let s =
1991 match conf.maxwait with
1992 | None ->
1993 conf.maxwait <- Some infinity;
1994 "always wait for page to complete"
1995 | Some _ ->
1996 conf.maxwait <- None;
1997 "show placeholder if page is not ready"
1999 TEdone s
2001 | 'f' ->
2002 conf.underinfo <- not conf.underinfo;
2003 TEdone ("underinfo " ^ btos conf.underinfo)
2005 | 'P' ->
2006 conf.savebmarks <- not conf.savebmarks;
2007 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2009 | 'S' ->
2010 let ondone s =
2012 let pageno, py =
2013 match state.layout with
2014 | [] -> 0, 0
2015 | l :: _ ->
2016 l.pageno, l.pagey
2018 conf.interpagespace <- int_of_string s;
2019 state.maxy <- calcheight ();
2020 let y = getpagey pageno in
2021 gotoy (y + py)
2022 with exc ->
2023 state.text <- Printf.sprintf "bad integer `%s': %s"
2024 s (Printexc.to_string exc)
2026 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2028 | 'l' ->
2029 reqlayout conf.angle (not conf.proportional);
2030 TEdone ("proportional display " ^ btos conf.proportional)
2032 | 'T' ->
2033 settrim (not conf.trimmargins) conf.trimfuzz;
2034 TEdone ("trim margins " ^ btos conf.trimmargins)
2036 | 'I' ->
2037 conf.invert <- not conf.invert;
2038 TEdone ("invert colors " ^ btos conf.invert)
2040 | _ ->
2041 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2042 TEstop
2045 class type lvsource = object
2046 method getitemcount : int
2047 method getitem : int -> (string * int)
2048 method hasaction : int -> bool
2049 method exit :
2050 uioh:uioh ->
2051 cancel:bool ->
2052 active:int ->
2053 first:int ->
2054 pan:int ->
2055 qsearch:string ->
2056 uioh option
2057 method getactive : int
2058 method getfirst : int
2059 method getqsearch : string
2060 method setqsearch : string -> unit
2061 method getpan : int
2062 end;;
2064 class virtual lvsourcebase = object
2065 val mutable m_active = 0
2066 val mutable m_first = 0
2067 val mutable m_qsearch = ""
2068 val mutable m_pan = 0
2069 method getactive = m_active
2070 method getfirst = m_first
2071 method getqsearch = m_qsearch
2072 method getpan = m_pan
2073 method setqsearch s = m_qsearch <- s
2074 end;;
2076 let textentryspecial key = function
2077 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2078 let s =
2079 match key with
2080 | Glut.KEY_UP -> action HCprev
2081 | Glut.KEY_DOWN -> action HCnext
2082 | Glut.KEY_HOME -> action HCfirst
2083 | Glut.KEY_END -> action HClast
2084 | _ -> state.text
2086 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2087 G.postRedisplay "special textentry";
2088 | _ -> ()
2091 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2092 let enttext te =
2093 state.mode <- Textentry (te, onleave);
2094 state.text <- "";
2095 enttext ();
2096 G.postRedisplay "textentrykeyboard enttext";
2098 match Char.unsafe_chr key with
2099 | '\008' -> (* backspace *)
2100 let len = String.length text in
2101 if len = 0
2102 then (
2103 onleave Cancel;
2104 G.postRedisplay "textentrykeyboard after cancel";
2106 else (
2107 let s = String.sub text 0 (len - 1) in
2108 enttext (c, s, opthist, onkey, ondone)
2111 | '\r' | '\n' ->
2112 ondone text;
2113 onleave Confirm;
2114 G.postRedisplay "textentrykeyboard after confirm"
2116 | '\007' (* ctrl-g *)
2117 | '\027' -> (* escape *)
2118 if String.length text = 0
2119 then (
2120 begin match opthist with
2121 | None -> ()
2122 | Some (_, onhistcancel) -> onhistcancel ()
2123 end;
2124 onleave Cancel;
2125 state.text <- "";
2126 G.postRedisplay "textentrykeyboard after cancel2"
2128 else (
2129 enttext (c, "", opthist, onkey, ondone)
2132 | '\127' -> () (* delete *)
2134 | _ ->
2135 begin match onkey text key with
2136 | TEdone text ->
2137 ondone text;
2138 onleave Confirm;
2139 G.postRedisplay "textentrykeyboard after confirm2";
2141 | TEcont text ->
2142 enttext (c, text, opthist, onkey, ondone);
2144 | TEstop ->
2145 onleave Cancel;
2146 state.text <- "";
2147 G.postRedisplay "textentrykeyboard after cancel3"
2149 | TEswitch te ->
2150 state.mode <- Textentry (te, onleave);
2151 G.postRedisplay "textentrykeyboard switch";
2152 end;
2155 let firstof first active =
2156 if first > active || abs (first - active) > fstate.maxrows - 1
2157 then max 0 (active - (fstate.maxrows/2))
2158 else first
2161 let calcfirst first active =
2162 if active > first
2163 then
2164 let rows = active - first in
2165 if rows > fstate.maxrows then active - fstate.maxrows else first
2166 else active
2169 let coe s = (s :> uioh);;
2171 class listview ~(source:lvsource) ~trusted =
2172 object (self)
2173 val m_pan = source#getpan
2174 val m_first = source#getfirst
2175 val m_active = source#getactive
2176 val m_qsearch = source#getqsearch
2177 val m_prev_uioh = state.uioh
2179 method private elemunder y =
2180 let n = y / (fstate.fontsize+1) in
2181 if m_first + n < source#getitemcount
2182 then (
2183 if source#hasaction (m_first + n)
2184 then Some (m_first + n)
2185 else None
2187 else None
2189 method display =
2190 Gl.enable `blend;
2191 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2192 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2193 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2194 GlDraw.color (1., 1., 1.);
2195 Gl.enable `texture_2d;
2196 let fs = fstate.fontsize in
2197 let nfs = fs + 1 in
2198 let ww = fstate.wwidth in
2199 let tabw = 30.0*.ww in
2200 let rec loop row =
2201 if (row - m_first) * nfs > conf.winh
2202 then ()
2203 else (
2204 if row >= 0 && row < source#getitemcount
2205 then (
2206 let (s, level) = source#getitem row in
2207 let y = (row - m_first) * nfs in
2208 let x = 5.0 +. float (level + m_pan) *. ww in
2209 if row = m_active
2210 then (
2211 Gl.disable `texture_2d;
2212 GlDraw.polygon_mode `both `line;
2213 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2214 GlDraw.rect (1., float (y + 1))
2215 (float (conf.winw - 1), float (y + fs + 3));
2216 GlDraw.polygon_mode `both `fill;
2217 GlDraw.color (1., 1., 1.);
2218 Gl.enable `texture_2d;
2221 let drawtabularstring s =
2222 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2223 if trusted
2224 then
2225 let tabpos = try String.index s '\t' with Not_found -> -1 in
2226 if tabpos > 0
2227 then
2228 let len = String.length s - tabpos - 1 in
2229 let s1 = String.sub s 0 tabpos
2230 and s2 = String.sub s (tabpos + 1) len in
2231 let nx = drawstr x s1 in
2232 let sw = nx -. x in
2233 let x = x +. (max tabw sw) in
2234 drawstr x s2
2235 else
2236 drawstr x s
2237 else
2238 drawstr x s
2240 let _ = drawtabularstring s in
2241 loop (row+1)
2245 loop 0;
2246 Gl.disable `blend;
2247 Gl.disable `texture_2d;
2249 method updownlevel incr =
2250 let len = source#getitemcount in
2251 let _, curlevel = source#getitem m_active in
2252 let rec flow i =
2253 if i = len then i-1 else if i = -1 then 0 else
2254 let _, l = source#getitem i in
2255 if l != curlevel then i else flow (i+incr)
2257 let active = flow m_active in
2258 let first = calcfirst m_first active in
2259 G.postRedisplay "special outline updownlevel";
2260 {< m_active = active; m_first = first >}
2262 method private key1 key =
2263 let set active first qsearch =
2264 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2266 let search active pattern incr =
2267 let dosearch re =
2268 let rec loop n =
2269 if n >= 0 && n < source#getitemcount
2270 then (
2271 let s, _ = source#getitem n in
2273 (try ignore (Str.search_forward re s 0); true
2274 with Not_found -> false)
2275 then Some n
2276 else loop (n + incr)
2278 else None
2280 loop active
2283 let re = Str.regexp_case_fold pattern in
2284 dosearch re
2285 with Failure s ->
2286 state.text <- s;
2287 None
2289 match key with
2290 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2291 let incr = if key = 18 then -1 else 1 in
2292 let active, first =
2293 match search (m_active + incr) m_qsearch incr with
2294 | None ->
2295 state.text <- m_qsearch ^ " [not found]";
2296 m_active, m_first
2297 | Some active ->
2298 state.text <- m_qsearch;
2299 active, firstof m_first active
2301 G.postRedisplay "listview ctrl-r/s";
2302 set active first m_qsearch;
2304 | 8 -> (* backspace *)
2305 let len = String.length m_qsearch in
2306 if len = 0
2307 then coe self
2308 else (
2309 if len = 1
2310 then (
2311 state.text <- "";
2312 G.postRedisplay "listview empty qsearch";
2313 set m_active m_first "";
2315 else
2316 let qsearch = String.sub m_qsearch 0 (len - 1) in
2317 let active, first =
2318 match search m_active qsearch ~-1 with
2319 | None ->
2320 state.text <- qsearch ^ " [not found]";
2321 m_active, m_first
2322 | Some active ->
2323 state.text <- qsearch;
2324 active, firstof m_first active
2326 G.postRedisplay "listview backspace qsearch";
2327 set active first qsearch
2330 | _ when key >= 32 && key < 127 ->
2331 let pattern = addchar m_qsearch (Char.chr key) in
2332 let active, first =
2333 match search m_active pattern 1 with
2334 | None ->
2335 state.text <- pattern ^ " [not found]";
2336 m_active, m_first
2337 | Some active ->
2338 state.text <- pattern;
2339 active, firstof m_first active
2341 G.postRedisplay "listview qsearch add";
2342 set active first pattern;
2344 | 27 -> (* escape *)
2345 state.text <- "";
2346 if String.length m_qsearch = 0
2347 then (
2348 G.postRedisplay "list view escape";
2349 begin
2350 match
2351 source#exit (coe self) true m_active m_first m_pan m_qsearch
2352 with
2353 | None -> m_prev_uioh
2354 | Some uioh -> uioh
2357 else (
2358 G.postRedisplay "list view kill qsearch";
2359 source#setqsearch "";
2360 coe {< m_qsearch = "" >}
2363 | 13 -> (* enter *)
2364 state.text <- "";
2365 let self = {< m_qsearch = "" >} in
2366 source#setqsearch "";
2367 let opt =
2368 G.postRedisplay "listview enter";
2369 if m_active >= 0 && m_active < source#getitemcount
2370 then (
2371 source#exit (coe self) false m_active m_first m_pan "";
2373 else (
2374 source#exit (coe self) true m_active m_first m_pan "";
2377 begin match opt with
2378 | None -> m_prev_uioh
2379 | Some uioh -> uioh
2382 | 127 -> (* delete *)
2383 coe self
2385 | _ -> dolog "unknown key %d" key; coe self
2387 method private special1 key =
2388 let itemcount = source#getitemcount in
2389 let find start incr =
2390 let rec find i =
2391 if i = -1 || i = itemcount
2392 then -1
2393 else (
2394 if source#hasaction i
2395 then i
2396 else find (i + incr)
2399 find start
2401 let set active first =
2402 let first = bound first 0 (itemcount - fstate.maxrows) in
2403 state.text <- "";
2404 coe {< m_active = active; m_first = first >}
2406 let navigate incr =
2407 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2408 let active, first =
2409 let incr1 = if incr > 0 then 1 else -1 in
2410 if isvisible m_first m_active
2411 then
2412 let next =
2413 let next = m_active + incr in
2414 let next =
2415 if next < 0 || next >= itemcount
2416 then -1
2417 else find next incr1
2419 if next = -1 || abs (m_active - next) > fstate.maxrows
2420 then -1
2421 else next
2423 if next = -1
2424 then
2425 let first = m_first + incr in
2426 let first = bound first 0 (itemcount - 1) in
2427 let next =
2428 let next = m_active + incr in
2429 let next = bound next 0 (itemcount - 1) in
2430 find next ~-incr1
2432 let active = if next = -1 then m_active else next in
2433 active, first
2434 else
2435 let first = min next m_first in
2436 next, first
2437 else
2438 let first = m_first + incr in
2439 let first = bound first 0 (itemcount - 1) in
2440 let active =
2441 let next = m_active + incr in
2442 let next = bound next 0 (itemcount - 1) in
2443 let next = find next incr1 in
2444 if next = -1 || abs (m_active - first) > fstate.maxrows
2445 then m_active
2446 else next
2448 active, first
2450 G.postRedisplay "listview navigate";
2451 set active first;
2453 begin match key with
2454 | Glut.KEY_UP -> navigate ~-1
2455 | Glut.KEY_DOWN -> navigate 1
2456 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2457 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2459 | Glut.KEY_RIGHT ->
2460 state.text <- "";
2461 G.postRedisplay "listview right";
2462 coe {< m_pan = m_pan - 1 >}
2464 | Glut.KEY_LEFT ->
2465 state.text <- "";
2466 G.postRedisplay "listview left";
2467 coe {< m_pan = m_pan + 1 >}
2469 | Glut.KEY_HOME ->
2470 let active = find 0 1 in
2471 G.postRedisplay "listview home";
2472 set active 0;
2474 | Glut.KEY_END ->
2475 let first = max 0 (itemcount - fstate.maxrows) in
2476 let active = find (itemcount - 1) ~-1 in
2477 G.postRedisplay "listview end";
2478 set active first;
2480 | _ -> coe self
2481 end;
2483 method key key =
2484 match state.mode with
2485 | Textentry te -> textentrykeyboard key te; coe self
2486 | _ -> self#key1 key
2488 method special key =
2489 match state.mode with
2490 | Textentry te -> textentryspecial key te; coe self
2491 | _ -> self#special1 key
2493 method button button bstate _ y =
2494 let opt =
2495 match button with
2496 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2497 begin match self#elemunder y with
2498 | Some n ->
2499 G.postRedisplay "listview click";
2500 source#exit
2501 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2502 | _ ->
2503 Some (coe self)
2505 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2506 let len = source#getitemcount in
2507 let first =
2508 if m_first + fstate.maxrows >= len
2509 then
2510 m_first
2511 else
2512 let first = m_first + (if n == 3 then -1 else 1) in
2513 bound first 0 (len - 1)
2515 G.postRedisplay "listview wheel";
2516 Some (coe {< m_first = first >})
2517 | _ ->
2518 Some (coe self)
2520 match opt with
2521 | None -> m_prev_uioh
2522 | Some uioh -> uioh
2524 method motion _ _ = coe self
2526 method pmotion _ y =
2527 let n =
2528 match self#elemunder y with
2529 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2530 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2532 let o =
2533 if n != m_active
2534 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2535 else self
2537 coe o
2539 method infochanged _ = ()
2540 end;;
2542 class outlinelistview ~source =
2543 object (self)
2544 inherit listview ~source:(source :> lvsource) ~trusted:false as super
2546 method key key =
2547 match key with
2548 | 14 -> (* ctrl-n *)
2549 source#narrow m_qsearch;
2550 G.postRedisplay "outline ctrl-n";
2551 coe {< m_first = 0; m_active = 0 >}
2553 | 21 -> (* ctrl-u *)
2554 source#denarrow;
2555 G.postRedisplay "outline ctrl-u";
2556 coe {< m_first = 0; m_active = 0 >}
2558 | 12 -> (* ctrl-l *)
2559 let first = m_active - (fstate.maxrows / 2) in
2560 G.postRedisplay "outline ctrl-l";
2561 coe {< m_first = first >}
2563 | 127 -> (* delete *)
2564 source#remove m_active;
2565 G.postRedisplay "outline delete";
2566 let active = max 0 (m_active-1) in
2567 coe {< m_first = firstof m_first active;
2568 m_active = active >}
2570 | key -> super#key key
2572 method special key =
2573 let calcfirst first active =
2574 if active > first
2575 then
2576 let rows = active - first in
2577 if rows > fstate.maxrows then active - fstate.maxrows else first
2578 else active
2580 let navigate incr =
2581 let active = m_active + incr in
2582 let active = bound active 0 (source#getitemcount - 1) in
2583 let first = calcfirst m_first active in
2584 G.postRedisplay "special outline navigate";
2585 coe {< m_active = active; m_first = first >}
2587 match key with
2588 | Glut.KEY_UP -> navigate ~-1
2589 | Glut.KEY_DOWN -> navigate 1
2590 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2591 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2593 | Glut.KEY_RIGHT ->
2594 let o =
2595 if Glut.getModifiers () land Glut.active_ctrl != 0
2596 then (
2597 G.postRedisplay "special outline right";
2598 {< m_pan = m_pan + 1 >}
2600 else self#updownlevel 1
2602 coe o
2604 | Glut.KEY_LEFT ->
2605 let o =
2606 if Glut.getModifiers () land Glut.active_ctrl != 0
2607 then (
2608 G.postRedisplay "special outline left";
2609 {< m_pan = m_pan - 1 >}
2611 else self#updownlevel ~-1
2613 coe o
2615 | Glut.KEY_HOME ->
2616 G.postRedisplay "special outline home";
2617 coe {< m_first = 0; m_active = 0 >}
2619 | Glut.KEY_END ->
2620 let active = source#getitemcount - 1 in
2621 let first = max 0 (active - fstate.maxrows) in
2622 G.postRedisplay "special outline end";
2623 coe {< m_active = active; m_first = first >}
2625 | _ -> super#special key
2628 let outlinesource usebookmarks =
2629 let empty = [||] in
2630 (object
2631 inherit lvsourcebase
2632 val mutable m_items = empty
2633 val mutable m_orig_items = empty
2634 val mutable m_prev_items = empty
2635 val mutable m_narrow_pattern = ""
2636 val mutable m_hadremovals = false
2638 method getitemcount =
2639 Array.length m_items + (if m_hadremovals then 1 else 0)
2641 method getitem n =
2642 if n == Array.length m_items && m_hadremovals
2643 then
2644 ("[Confirm removal]", 0)
2645 else
2646 let s, n, _ = m_items.(n) in
2647 (s, n)
2649 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
2650 ignore (uioh, first, pan, qsearch);
2651 let confrimremoval = m_hadremovals && active = Array.length m_items in
2652 let items =
2653 if String.length m_narrow_pattern = 0
2654 then m_orig_items
2655 else m_items
2657 if not cancel
2658 then (
2659 if not confrimremoval
2660 then(
2661 let _, _, anchor = m_items.(active) in
2662 gotoanchor anchor;
2663 m_items <- items;
2665 else (
2666 state.bookmarks <- Array.to_list m_items;
2667 m_orig_items <- m_items;
2670 else m_items <- items;
2671 None
2673 method hasaction _ = true
2675 method greetmsg =
2676 if Array.length m_items != Array.length m_orig_items
2677 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
2678 else ""
2680 method narrow pattern =
2681 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2682 match reopt with
2683 | None -> ()
2684 | Some re ->
2685 let rec loop accu n =
2686 if n = -1
2687 then (
2688 m_narrow_pattern <- pattern;
2689 m_items <- Array.of_list accu
2691 else
2692 let (s, _, _) as o = m_items.(n) in
2693 let accu =
2694 if (try ignore (Str.search_forward re s 0); true
2695 with Not_found -> false)
2696 then o :: accu
2697 else accu
2699 loop accu (n-1)
2701 loop [] (Array.length m_items - 1)
2703 method denarrow =
2704 m_orig_items <- (
2705 if usebookmarks
2706 then Array.of_list state.bookmarks
2707 else state.outlines
2709 m_items <- m_orig_items
2711 method remove m =
2712 if usebookmarks
2713 then
2714 if m >= 0 && m < Array.length m_items
2715 then (
2716 m_hadremovals <- true;
2717 m_items <- Array.init (Array.length m_items - 1) (fun n ->
2718 let n = if n >= m then n+1 else n in
2719 m_items.(n)
2723 method reset pageno items =
2724 m_hadremovals <- false;
2725 if m_orig_items == empty || m_prev_items != items
2726 then (
2727 m_orig_items <- items;
2728 if String.length m_narrow_pattern = 0
2729 then m_items <- items;
2731 m_prev_items <- items;
2732 let active =
2733 let rec loop n best bestd =
2734 if n = Array.length m_items
2735 then best
2736 else
2737 let (_, _, (outlinepageno, _)) = m_items.(n) in
2738 let d = abs (outlinepageno - pageno) in
2739 if d < bestd
2740 then loop (n+1) n d
2741 else loop (n+1) best bestd
2743 loop 0 ~-1 max_int
2745 m_active <- active;
2746 m_first <- firstof m_first active
2747 end)
2750 let enterselector usebookmarks =
2751 let source = outlinesource usebookmarks in
2752 fun errmsg ->
2753 let outlines =
2754 if usebookmarks
2755 then Array.of_list state.bookmarks
2756 else state.outlines
2758 if Array.length outlines = 0
2759 then (
2760 showtext ' ' errmsg;
2762 else (
2763 state.text <- source#greetmsg;
2764 Glut.setCursor Glut.CURSOR_INHERIT;
2765 let pageno =
2766 match state.layout with
2767 | [] -> -1
2768 | {pageno=pageno} :: _ -> pageno
2770 source#reset pageno outlines;
2771 state.uioh <- coe (new outlinelistview ~source);
2772 G.postRedisplay "enter selector";
2776 let enteroutlinemode =
2777 let f = enterselector false in
2778 fun ()-> f "Document has no outline";
2781 let enterbookmarkmode =
2782 let f = enterselector true in
2783 fun () -> f "Document has no bookmarks (yet)";
2786 let color_of_string s =
2787 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
2788 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
2792 let color_to_string (r, g, b) =
2793 let r = truncate (r *. 256.0)
2794 and g = truncate (g *. 256.0)
2795 and b = truncate (b *. 256.0) in
2796 Printf.sprintf "%d/%d/%d" r g b
2799 let irect_of_string s =
2800 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
2803 let irect_to_string (x0,y0,x1,y1) =
2804 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
2807 let makecheckers () =
2808 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
2809 following to say:
2810 converted by Issac Trotts. July 25, 2002 *)
2811 let image_height = 64
2812 and image_width = 64 in
2814 let make_image () =
2815 let image =
2816 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
2818 for i = 0 to image_width - 1 do
2819 for j = 0 to image_height - 1 do
2820 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
2821 (if (i land 8 ) lxor (j land 8) = 0
2822 then [|255;255;255|] else [|200;200;200|])
2823 done
2824 done;
2825 image
2827 let image = make_image () in
2828 let id = GlTex.gen_texture () in
2829 GlTex.bind_texture `texture_2d id;
2830 GlPix.store (`unpack_alignment 1);
2831 GlTex.image2d image;
2832 List.iter (GlTex.parameter ~target:`texture_2d)
2833 [ `wrap_s `repeat;
2834 `wrap_t `repeat;
2835 `mag_filter `nearest;
2836 `min_filter `nearest ];
2840 let setcheckers enabled =
2841 match state.texid with
2842 | None ->
2843 if enabled then state.texid <- Some (makecheckers ())
2845 | Some texid ->
2846 if not enabled
2847 then (
2848 GlTex.delete_texture texid;
2849 state.texid <- None;
2853 let int_of_string_with_suffix s =
2854 let l = String.length s in
2855 let s1, shift =
2856 if l > 1
2857 then
2858 let suffix = Char.lowercase s.[l-1] in
2859 match suffix with
2860 | 'k' -> String.sub s 0 (l-1), 10
2861 | 'm' -> String.sub s 0 (l-1), 20
2862 | 'g' -> String.sub s 0 (l-1), 30
2863 | _ -> s, 0
2864 else s, 0
2866 let n = int_of_string s1 in
2867 let m = n lsl shift in
2868 if m < 0 || m < n
2869 then raise (Failure "value too large")
2870 else m
2873 let string_with_suffix_of_int n =
2874 if n = 0
2875 then "0"
2876 else
2877 let n, s =
2878 if n = 0
2879 then 0, ""
2880 else (
2881 if n land ((1 lsl 20) - 1) = 0
2882 then n lsr 20, "M"
2883 else (
2884 if n land ((1 lsl 10) - 1) = 0
2885 then n lsr 10, "K"
2886 else n, ""
2890 let rec loop s n =
2891 let h = n mod 1000 in
2892 let n = n / 1000 in
2893 if n = 0
2894 then string_of_int h ^ s
2895 else (
2896 let s = Printf.sprintf "_%03d%s" h s in
2897 loop s n
2900 loop "" n ^ s;
2903 let describe_location () =
2904 let f (fn, _) l =
2905 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
2907 let fn, ln = List.fold_left f (-1, -1) state.layout in
2908 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2909 let percent =
2910 if maxy <= 0
2911 then 100.
2912 else (100. *. (float state.y /. float maxy))
2914 if fn = ln
2915 then
2916 Printf.sprintf "page %d of %d [%.2f%%]"
2917 (fn+1) state.pagecount percent
2918 else
2919 Printf.sprintf
2920 "pages %d-%d of %d [%.2f%%]"
2921 (fn+1) (ln+1) state.pagecount percent
2924 let enterinfomode =
2925 let btos b = if b then "\xe2\x88\x9a" else "" in
2926 let showextended = ref false in
2927 let leave mode = function
2928 | Confirm -> state.mode <- mode
2929 | Cancel -> state.mode <- mode in
2930 let src =
2931 (object
2932 val mutable m_first_time = true
2933 val mutable m_l = []
2934 val mutable m_a = [||]
2935 val mutable m_prev_uioh = nouioh
2936 val mutable m_prev_mode = View
2938 inherit lvsourcebase
2940 method reset prev_mode prev_uioh =
2941 m_a <- Array.of_list (List.rev m_l);
2942 m_l <- [];
2943 m_prev_mode <- prev_mode;
2944 m_prev_uioh <- prev_uioh;
2945 if m_first_time
2946 then (
2947 let rec loop n =
2948 if n >= Array.length m_a
2949 then ()
2950 else
2951 match m_a.(n) with
2952 | _, _, _, Action _ -> m_active <- n
2953 | _ -> loop (n+1)
2955 loop 0;
2956 m_first_time <- false;
2959 method int name get set =
2960 m_l <-
2961 (name, `int get, 1, Action (
2962 fun u ->
2963 let ondone s =
2964 try set (int_of_string s)
2965 with exn ->
2966 state.text <- Printf.sprintf "bad integer `%s': %s"
2967 s (Printexc.to_string exn)
2969 state.text <- "";
2970 let te = name ^ ": ", "", None, intentry, ondone in
2971 state.mode <- Textentry (te, leave m_prev_mode);
2973 )) :: m_l
2975 method int_with_suffix name get set =
2976 m_l <-
2977 (name, `intws get, 1, Action (
2978 fun u ->
2979 let ondone s =
2980 try set (int_of_string_with_suffix s)
2981 with exn ->
2982 state.text <- Printf.sprintf "bad integer `%s': %s"
2983 s (Printexc.to_string exn)
2985 state.text <- "";
2986 let te =
2987 name ^ ": ", "", None, intentry_with_suffix, ondone
2989 state.mode <- Textentry (te, leave m_prev_mode);
2991 )) :: m_l
2993 method bool ?(offset=1) ?(btos=btos) name get set =
2994 m_l <-
2995 (name, `bool (btos, get), offset, Action (
2996 fun u ->
2997 let v = get () in
2998 set (not v);
3000 )) :: m_l
3002 method color name get set =
3003 m_l <-
3004 (name, `color get, 1, Action (
3005 fun u ->
3006 let invalid = (nan, nan, nan) in
3007 let ondone s =
3008 let c =
3009 try color_of_string s
3010 with exn ->
3011 state.text <- Printf.sprintf "bad color `%s': %s"
3012 s (Printexc.to_string exn);
3013 invalid
3015 if c <> invalid
3016 then set c;
3018 let te = name ^ ": ", "", None, textentry, ondone in
3019 state.text <- color_to_string (get ());
3020 state.mode <- Textentry (te, leave m_prev_mode);
3022 )) :: m_l
3024 method string name get set =
3025 m_l <-
3026 (name, `string get, 1, Action (
3027 fun u ->
3028 let ondone s = set s in
3029 let te = name ^ ": ", "", None, textentry, ondone in
3030 state.mode <- Textentry (te, leave m_prev_mode);
3032 )) :: m_l
3034 method colorspace name get set =
3035 m_l <-
3036 (name, `string get, 1, Action (
3037 fun _ ->
3038 let source =
3039 let vals = [| "rgb"; "bgr"; "gray" |] in
3040 (object
3041 inherit lvsourcebase
3043 initializer
3044 m_active <- int_of_colorspace conf.colorspace;
3045 m_first <- 0;
3047 method getitemcount = Array.length vals
3048 method getitem n = (vals.(n), 0)
3049 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3050 ignore (uioh, first, pan, qsearch);
3051 if not cancel then set active;
3052 None
3053 method hasaction _ = true
3054 end)
3056 state.text <- "";
3057 coe (new listview ~source ~trusted:true)
3058 )) :: m_l
3060 method caption s offset =
3061 m_l <- (s, `empty, offset, Noaction) :: m_l
3063 method caption2 s f offset =
3064 m_l <- (s, `string f, offset, Noaction) :: m_l
3066 method getitemcount = Array.length m_a
3068 method getitem n =
3069 let tostr = function
3070 | `int f -> string_of_int (f ())
3071 | `intws f -> string_with_suffix_of_int (f ())
3072 | `string f -> f ()
3073 | `color f -> color_to_string (f ())
3074 | `bool (btos, f) -> btos (f ())
3075 | `empty -> ""
3077 let name, t, offset, _ = m_a.(n) in
3078 ((let s = tostr t in
3079 if String.length s > 0
3080 then Printf.sprintf "%s\t%s" name s
3081 else name),
3082 offset)
3084 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3085 let uiohopt =
3086 if not cancel
3087 then (
3088 m_qsearch <- qsearch;
3089 let uioh =
3090 match m_a.(active) with
3091 | _, _, _, Action f -> f uioh
3092 | _ -> uioh
3094 Some uioh
3096 else None
3098 m_active <- active;
3099 m_first <- first;
3100 m_pan <- pan;
3101 uiohopt
3103 method hasaction n =
3104 match m_a.(n) with
3105 | _, _, _, Action _ -> true
3106 | _ -> false
3107 end)
3109 let rec fillsrc prevmode prevuioh =
3110 let sep () = src#caption "" 0 in
3111 let colorp name get set =
3112 src#string name
3113 (fun () -> color_to_string (get ()))
3114 (fun v ->
3116 let c = color_of_string v in
3117 set c
3118 with exn ->
3119 state.text <- Printf.sprintf "bad color `%s': %s"
3120 v (Printexc.to_string exn);
3123 let oldmode = state.mode in
3124 let birdseye = isbirdseye state.mode in
3126 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3128 src#bool "presentation mode"
3129 (fun () -> conf.presentation)
3130 (fun v ->
3131 conf.presentation <- v;
3132 state.anchor <- getanchor ();
3133 represent ());
3135 src#bool "ignore case in searches"
3136 (fun () -> conf.icase)
3137 (fun v -> conf.icase <- v);
3139 src#bool "preload"
3140 (fun () -> conf.preload)
3141 (fun v -> conf.preload <- v);
3143 src#bool "highlight links"
3144 (fun () -> conf.hlinks)
3145 (fun v -> conf.hlinks <- v);
3147 src#bool "under info"
3148 (fun () -> conf.underinfo)
3149 (fun v -> conf.underinfo <- v);
3151 src#bool "persistent bookmarks"
3152 (fun () -> conf.savebmarks)
3153 (fun v -> conf.savebmarks <- v);
3155 src#bool "proportional display"
3156 (fun () -> conf.proportional)
3157 (fun v -> reqlayout conf.angle v);
3159 src#bool "trim margins"
3160 (fun () -> conf.trimmargins)
3161 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3163 src#bool "persistent location"
3164 (fun () -> conf.jumpback)
3165 (fun v -> conf.jumpback <- v);
3167 sep ();
3168 src#int "vertical margin"
3169 (fun () -> conf.interpagespace)
3170 (fun n ->
3171 conf.interpagespace <- n;
3172 let pageno, py =
3173 match state.layout with
3174 | [] -> 0, 0
3175 | l :: _ ->
3176 l.pageno, l.pagey
3178 state.maxy <- calcheight ();
3179 let y = getpagey pageno in
3180 gotoy (y + py)
3183 src#int "page bias"
3184 (fun () -> conf.pagebias)
3185 (fun v -> conf.pagebias <- v);
3187 src#int "scroll step"
3188 (fun () -> conf.scrollstep)
3189 (fun n -> conf.scrollstep <- n);
3191 src#int "auto scroll step"
3192 (fun () ->
3193 match state.autoscroll with
3194 | Some step -> step
3195 | _ -> conf.autoscrollstep)
3196 (fun n ->
3197 if state.autoscroll <> None
3198 then state.autoscroll <- Some n;
3199 conf.autoscrollstep <- n);
3201 src#int "zoom"
3202 (fun () -> truncate (conf.zoom *. 100.))
3203 (fun v -> setzoom ((float v) /. 100.));
3205 src#int "rotation"
3206 (fun () -> conf.angle)
3207 (fun v -> reqlayout v conf.proportional);
3209 src#int "scroll bar width"
3210 (fun () -> state.scrollw)
3211 (fun v ->
3212 state.scrollw <- v;
3213 conf.scrollbw <- v;
3214 reshape conf.winw conf.winh;
3217 src#int "scroll handle height"
3218 (fun () -> conf.scrollh)
3219 (fun v -> conf.scrollh <- v;);
3221 src#int "thumbnail width"
3222 (fun () -> conf.thumbw)
3223 (fun v ->
3224 conf.thumbw <- min 4096 v;
3225 match oldmode with
3226 | Birdseye beye ->
3227 leavebirdseye beye false;
3228 enterbirdseye ()
3229 | _ -> ()
3232 sep ();
3233 src#caption "Presentation mode" 0;
3234 src#bool "scrollbar visible"
3235 (fun () -> conf.scrollbarinpm)
3236 (fun v ->
3237 if v != conf.scrollbarinpm
3238 then (
3239 conf.scrollbarinpm <- v;
3240 if conf.presentation
3241 then (
3242 state.scrollw <- if v then conf.scrollbw else 0;
3243 reshape conf.winw conf.winh;
3248 sep ();
3249 src#caption "Pixmap cache" 0;
3250 src#int_with_suffix "size (advisory)"
3251 (fun () -> conf.memlimit)
3252 (fun v -> conf.memlimit <- v);
3254 src#caption2 "used"
3255 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3256 (string_with_suffix_of_int state.memused)
3257 (Hashtbl.length state.tilemap)) 1;
3259 sep ();
3260 src#caption "Layout" 0;
3261 src#caption2 "Dimension"
3262 (fun () ->
3263 Printf.sprintf "%dx%d (virtual %dx%d)"
3264 conf.winw conf.winh
3265 state.w state.maxy)
3267 if conf.debug
3268 then
3269 src#caption2 "Position" (fun () ->
3270 Printf.sprintf "%dx%d" state.x state.y
3272 else
3273 src#caption2 "Visible" (fun () -> describe_location ()) 1
3276 sep ();
3277 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3278 "Save these parameters as global defaults at exit"
3279 (fun () -> conf.bedefault)
3280 (fun v -> conf.bedefault <- v)
3283 sep ();
3284 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3285 src#bool ~offset:0 ~btos "Extended parameters"
3286 (fun () -> !showextended)
3287 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3288 if !showextended
3289 then (
3290 src#bool "checkers"
3291 (fun () -> conf.checkers)
3292 (fun v -> conf.checkers <- v; setcheckers v);
3293 src#bool "verbose"
3294 (fun () -> conf.verbose)
3295 (fun v -> conf.verbose <- v);
3296 src#bool "invert colors"
3297 (fun () -> conf.invert)
3298 (fun v -> conf.invert <- v);
3299 src#bool "max fit"
3300 (fun () -> conf.maxhfit)
3301 (fun v -> conf.maxhfit <- v);
3302 src#string "uri launcher"
3303 (fun () -> conf.urilauncher)
3304 (fun v -> conf.urilauncher <- v);
3305 src#string "tile size"
3306 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3307 (fun v ->
3309 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3310 conf.tileh <- max 64 w;
3311 conf.tilew <- max 64 h;
3312 flushtiles ();
3313 with exn ->
3314 state.text <- Printf.sprintf "bad tile size `%s': %s"
3315 v (Printexc.to_string exn));
3316 src#int "anti-aliasing level"
3317 (fun () -> conf.aalevel)
3318 (fun v ->
3319 conf.aalevel <- bound v 0 8;
3320 state.anchor <- getanchor ();
3321 opendoc state.path state.password;
3323 src#int "ui font size"
3324 (fun () -> fstate.fontsize)
3325 (fun v -> setfontsize (bound v 5 100));
3326 colorp "background color"
3327 (fun () -> conf.bgcolor)
3328 (fun v -> conf.bgcolor <- v);
3329 src#bool "crop hack"
3330 (fun () -> conf.crophack)
3331 (fun v -> conf.crophack <- v);
3332 src#string "trim fuzz"
3333 (fun () -> irect_to_string conf.trimfuzz)
3334 (fun v ->
3336 conf.trimfuzz <- irect_of_string v;
3337 if conf.trimmargins
3338 then settrim true conf.trimfuzz;
3339 with exn ->
3340 state.text <- Printf.sprintf "bad irect `%s': %s"
3341 v (Printexc.to_string exn)
3343 src#string "throttle"
3344 (fun () ->
3345 match conf.maxwait with
3346 | None -> "show place holder if page is not ready"
3347 | Some time ->
3348 if time = infinity
3349 then "wait for page to fully render"
3350 else
3351 "wait " ^ string_of_float time
3352 ^ " seconds before showing placeholder"
3354 (fun v ->
3356 let f = float_of_string v in
3357 if f <= 0.0
3358 then conf.maxwait <- None
3359 else conf.maxwait <- Some f
3360 with exn ->
3361 state.text <- Printf.sprintf "bad time `%s': %s"
3362 v (Printexc.to_string exn)
3364 src#colorspace "color space"
3365 (fun () -> colorspace_to_string conf.colorspace)
3366 (fun v ->
3367 conf.colorspace <- colorspace_of_int v;
3368 wcmd "cs" [`i v];
3369 load state.layout;
3373 sep ();
3374 src#caption "Document" 0;
3375 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3376 if conf.trimmargins
3377 then (
3378 sep ();
3379 src#caption "Trimmed margins" 0;
3380 src#caption2 "Dimensions"
3381 (fun () -> string_of_int (List.length state.pdims)) 1;
3384 src#reset prevmode prevuioh;
3386 fun () ->
3387 state.text <- "";
3388 let prevmode = state.mode
3389 and prevuioh = state.uioh in
3390 fillsrc prevmode prevuioh;
3391 let source = (src :> lvsource) in
3392 state.uioh <- coe (object (self)
3393 inherit listview ~source ~trusted:true as super
3394 val mutable m_prevmemused = 0
3395 method infochanged = function
3396 | Memused ->
3397 if m_prevmemused != state.memused
3398 then (
3399 m_prevmemused <- state.memused;
3400 G.postRedisplay "memusedchanged";
3402 | Pdim -> G.postRedisplay "pdimchanged"
3403 | Docinfo -> fillsrc prevmode prevuioh
3405 method special key =
3406 if Glut.getModifiers () land Glut.active_ctrl = 0
3407 then
3408 match key with
3409 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
3410 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
3411 | _ -> super#special key
3412 else super#special key
3413 end);
3414 G.postRedisplay "info";
3417 let enterhelpmode =
3418 let source =
3419 (object
3420 inherit lvsourcebase
3421 method getitemcount = Array.length state.help
3422 method getitem n =
3423 let s, n, _ = state.help.(n) in
3424 (s, n)
3426 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3427 let optuioh =
3428 if not cancel
3429 then (
3430 m_qsearch <- qsearch;
3431 match state.help.(active) with
3432 | _, _, Action f -> Some (f uioh)
3433 | _ -> Some (uioh)
3435 else None
3437 m_active <- active;
3438 m_first <- first;
3439 m_pan <- pan;
3440 optuioh
3442 method hasaction n =
3443 match state.help.(n) with
3444 | _, _, Action _ -> true
3445 | _ -> false
3447 initializer
3448 m_active <- -1
3449 end)
3450 in fun () ->
3451 state.uioh <- coe (new listview ~source ~trusted:true);
3452 G.postRedisplay "help";
3455 let quickbookmark ?title () =
3456 match state.layout with
3457 | [] -> ()
3458 | l :: _ ->
3459 let title =
3460 match title with
3461 | None ->
3462 let sec = Unix.gettimeofday () in
3463 let tm = Unix.localtime sec in
3464 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
3465 (l.pageno+1)
3466 tm.Unix.tm_mday
3467 tm.Unix.tm_mon
3468 (tm.Unix.tm_year + 1900)
3469 tm.Unix.tm_hour
3470 tm.Unix.tm_min
3471 | Some title -> title
3473 state.bookmarks <-
3474 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
3475 :: state.bookmarks
3478 let doreshape w h =
3479 state.fullscreen <- None;
3480 Glut.reshapeWindow w h;
3483 let viewkeyboard key =
3484 let enttext te =
3485 let mode = state.mode in
3486 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3487 state.text <- "";
3488 enttext ();
3489 G.postRedisplay "view:enttext"
3491 let c = Char.chr key in
3492 match c with
3493 | '\027' | 'q' -> (* escape *)
3494 begin match state.mstate with
3495 | Mzoomrect _ ->
3496 state.mstate <- Mnone;
3497 Glut.setCursor Glut.CURSOR_INHERIT;
3498 G.postRedisplay "kill zoom rect";
3499 | _ ->
3500 raise Quit
3501 end;
3503 | '\008' -> (* backspace *)
3504 let y = getnav ~-1 in
3505 gotoy_and_clear_text y
3507 | 'o' ->
3508 enteroutlinemode ()
3510 | 'u' ->
3511 state.rects <- [];
3512 state.text <- "";
3513 G.postRedisplay "dehighlight";
3515 | '/' | '?' ->
3516 let ondone isforw s =
3517 cbput state.hists.pat s;
3518 state.searchpattern <- s;
3519 search s isforw
3521 let s = String.create 1 in
3522 s.[0] <- c;
3523 enttext (s, "", Some (onhist state.hists.pat),
3524 textentry, ondone (c ='/'))
3526 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3527 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3528 setzoom (conf.zoom +. incr)
3530 | '+' ->
3531 let ondone s =
3532 let n =
3533 try int_of_string s with exc ->
3534 state.text <- Printf.sprintf "bad integer `%s': %s"
3535 s (Printexc.to_string exc);
3536 max_int
3538 if n != max_int
3539 then (
3540 conf.pagebias <- n;
3541 state.text <- "page bias is now " ^ string_of_int n;
3544 enttext ("page bias: ", "", None, intentry, ondone)
3546 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3547 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3548 setzoom (max 0.01 (conf.zoom -. decr))
3550 | '-' ->
3551 let ondone msg = state.text <- msg in
3552 enttext (
3553 "option [acfhilpstvAPRSZTI]: ", "", None,
3554 optentry state.mode, ondone
3557 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3558 setzoom 1.0
3560 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3561 let zoom = zoomforh conf.winw conf.winh state.scrollw in
3562 if zoom < 1.0
3563 then setzoom zoom
3565 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3566 togglebirdseye ()
3568 | '0' .. '9' ->
3569 let ondone s =
3570 let n =
3571 try int_of_string s with exc ->
3572 state.text <- Printf.sprintf "bad integer `%s': %s"
3573 s (Printexc.to_string exc);
3576 if n >= 0
3577 then (
3578 addnav ();
3579 cbput state.hists.pag (string_of_int n);
3580 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
3583 let pageentry text key =
3584 match Char.unsafe_chr key with
3585 | 'g' -> TEdone text
3586 | _ -> intentry text key
3588 let text = "x" in text.[0] <- c;
3589 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
3591 | 'b' ->
3592 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
3593 reshape conf.winw conf.winh;
3595 | 'l' ->
3596 conf.hlinks <- not conf.hlinks;
3597 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3598 G.postRedisplay "toggle highlightlinks";
3600 | 'a' ->
3601 begin match state.autoscroll with
3602 | Some step ->
3603 conf.autoscrollstep <- step;
3604 state.autoscroll <- None
3605 | None ->
3606 if conf.autoscrollstep = 0
3607 then state.autoscroll <- Some 1
3608 else state.autoscroll <- Some conf.autoscrollstep
3611 | 'P' ->
3612 conf.presentation <- not conf.presentation;
3613 if conf.presentation
3614 then (
3615 if not conf.scrollbarinpm
3616 then state.scrollw <- 0;
3618 else
3619 state.scrollw <- conf.scrollbw;
3621 showtext ' ' ("presentation mode " ^
3622 if conf.presentation then "on" else "off");
3623 state.anchor <- getanchor ();
3624 represent ()
3626 | 'f' ->
3627 begin match state.fullscreen with
3628 | None ->
3629 state.fullscreen <- Some (conf.winw, conf.winh);
3630 Glut.fullScreen ()
3631 | Some (w, h) ->
3632 state.fullscreen <- None;
3633 doreshape w h
3636 | 'g' ->
3637 gotoy_and_clear_text 0
3639 | 'G' ->
3640 gotopage1 (state.pagecount - 1) 0
3642 | 'n' ->
3643 search state.searchpattern true
3645 | 'p' | 'N' ->
3646 search state.searchpattern false
3648 | 't' ->
3649 begin match state.layout with
3650 | [] -> ()
3651 | l :: _ ->
3652 gotoy_and_clear_text (getpagey l.pageno)
3655 | ' ' ->
3656 begin match List.rev state.layout with
3657 | [] -> ()
3658 | l :: _ ->
3659 let pageno = min (l.pageno+1) (state.pagecount-1) in
3660 gotoy_and_clear_text (getpagey pageno)
3663 | '\127' -> (* del *)
3664 begin match state.layout with
3665 | [] -> ()
3666 | l :: _ ->
3667 let pageno = max 0 (l.pageno-1) in
3668 gotoy_and_clear_text (getpagey pageno)
3671 | '=' ->
3672 showtext ' ' (describe_location ());
3674 | 'w' ->
3675 begin match state.layout with
3676 | [] -> ()
3677 | l :: _ ->
3678 doreshape (l.pagew + state.scrollw) l.pageh;
3679 G.postRedisplay "w"
3682 | '\'' ->
3683 enterbookmarkmode ()
3685 | 'h' ->
3686 enterhelpmode ()
3688 | 'i' ->
3689 enterinfomode ()
3691 | 'm' ->
3692 let ondone s =
3693 match state.layout with
3694 | l :: _ ->
3695 state.bookmarks <-
3696 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
3697 :: state.bookmarks
3698 | _ -> ()
3700 enttext ("bookmark: ", "", None, textentry, ondone)
3702 | '~' ->
3703 quickbookmark ();
3704 showtext ' ' "Quick bookmark added";
3706 | 'z' ->
3707 begin match state.layout with
3708 | l :: _ ->
3709 let rect = getpdimrect l.pagedimno in
3710 let w, h =
3711 if conf.crophack
3712 then
3713 (truncate (1.8 *. (rect.(1) -. rect.(0))),
3714 truncate (1.2 *. (rect.(3) -. rect.(0))))
3715 else
3716 (truncate (rect.(1) -. rect.(0)),
3717 truncate (rect.(3) -. rect.(0)))
3719 let w = truncate ((float w)*.conf.zoom)
3720 and h = truncate ((float h)*.conf.zoom) in
3721 if w != 0 && h != 0
3722 then (
3723 state.anchor <- getanchor ();
3724 doreshape (w + state.scrollw) (h + conf.interpagespace)
3726 G.postRedisplay "z";
3728 | [] -> ()
3731 | '\000' -> (* ctrl-2 *)
3732 let maxw = getmaxw () in
3733 if maxw > 0.0
3734 then setzoom (maxw /. float conf.winw)
3736 | '<' | '>' ->
3737 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
3739 | '[' | ']' ->
3740 conf.colorscale <-
3741 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
3743 G.postRedisplay "brightness";
3745 | 'k' ->
3746 begin match state.mode with
3747 | Birdseye beye -> upbirdseye beye
3748 | _ -> gotoy (clamp (-conf.scrollstep))
3751 | 'j' ->
3752 begin match state.mode with
3753 | Birdseye beye -> downbirdseye beye
3754 | _ -> gotoy (clamp conf.scrollstep)
3757 | 'r' ->
3758 state.anchor <- getanchor ();
3759 opendoc state.path state.password
3761 | 'v' when conf.debug ->
3762 state.rects <- [];
3763 List.iter (fun l ->
3764 match getopaque l.pageno with
3765 | None -> ()
3766 | Some opaque ->
3767 let x0, y0, x1, y1 = pagebbox opaque in
3768 let a,b = float x0, float y0 in
3769 let c,d = float x1, float y0 in
3770 let e,f = float x1, float y1 in
3771 let h,j = float x0, float y1 in
3772 let rect = (a,b,c,d,e,f,h,j) in
3773 debugrect rect;
3774 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
3775 ) state.layout;
3776 G.postRedisplay "v";
3778 | _ ->
3779 vlog "huh? %d %c" key (Char.chr key);
3782 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
3783 match key with
3784 | 27 -> (* escape *)
3785 leavebirdseye beye true
3787 | 12 -> (* ctrl-l *)
3788 let y, h = getpageyh pageno in
3789 let top = (conf.winh - h) / 2 in
3790 gotoy (max 0 (y - top))
3792 | 13 -> (* enter *)
3793 leavebirdseye beye false
3795 | _ ->
3796 viewkeyboard key
3799 let keyboard ~key ~x ~y =
3800 ignore x;
3801 ignore y;
3802 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
3803 then wcmd "interrupt" []
3804 else state.uioh <- state.uioh#key key
3807 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
3808 match key with
3809 | Glut.KEY_UP -> upbirdseye beye
3810 | Glut.KEY_DOWN -> downbirdseye beye
3812 | Glut.KEY_PAGE_UP ->
3813 begin match state.layout with
3814 | l :: _ ->
3815 if l.pagey != 0
3816 then (
3817 state.mode <- Birdseye (
3818 conf, leftx, l.pageno, hooverpageno, anchor
3820 gotopage1 l.pageno 0;
3822 else (
3823 let layout = layout (state.y-conf.winh) conf.winh in
3824 match layout with
3825 | [] -> gotoy (clamp (-conf.winh))
3826 | l :: _ ->
3827 state.mode <- Birdseye (
3828 conf, leftx, l.pageno, hooverpageno, anchor
3830 gotopage1 l.pageno 0
3833 | [] -> gotoy (clamp (-conf.winh))
3834 end;
3836 | Glut.KEY_PAGE_DOWN ->
3837 begin match List.rev state.layout with
3838 | l :: _ ->
3839 let layout = layout (state.y + conf.winh) conf.winh in
3840 begin match layout with
3841 | [] ->
3842 let incr = l.pageh - l.pagevh in
3843 if incr = 0
3844 then (
3845 state.mode <-
3846 Birdseye (
3847 conf, leftx, state.pagecount - 1, hooverpageno, anchor
3849 G.postRedisplay "birdseye pagedown";
3851 else gotoy (clamp (incr + conf.interpagespace*2));
3853 | l :: _ ->
3854 state.mode <-
3855 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
3856 gotopage1 l.pageno 0;
3859 | [] -> gotoy (clamp conf.winh)
3860 end;
3862 | Glut.KEY_HOME ->
3863 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
3864 gotopage1 0 0
3866 | Glut.KEY_END ->
3867 let pageno = state.pagecount - 1 in
3868 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3869 if not (pagevisible state.layout pageno)
3870 then
3871 let h =
3872 match List.rev state.pdims with
3873 | [] -> conf.winh
3874 | (_, _, h, _) :: _ -> h
3876 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
3877 else G.postRedisplay "birdseye end";
3878 | _ -> ()
3881 let setautoscrollspeed step goingdown =
3882 let incr = max 1 ((abs step) / 2) in
3883 let incr = if goingdown then incr else -incr in
3884 let astep = step + incr in
3885 state.autoscroll <- Some astep;
3888 let special ~key ~x ~y =
3889 ignore x;
3890 ignore y;
3891 state.uioh <- state.uioh#special key
3894 let drawpage l =
3895 let color =
3896 match state.mode with
3897 | Textentry _ -> scalecolor 0.4
3898 | View -> scalecolor 1.0
3899 | Birdseye (_, _, pageno, hooverpageno, _) ->
3900 if l.pageno = hooverpageno
3901 then scalecolor 0.9
3902 else (
3903 if l.pageno = pageno
3904 then scalecolor 1.0
3905 else scalecolor 0.8
3908 drawtiles l color;
3909 begin match getopaque l.pageno with
3910 | Some opaque ->
3911 if tileready l l.pagex l.pagey
3912 then
3913 let x = l.pagedispx - l.pagex
3914 and y = l.pagedispy - l.pagey in
3915 postprocess opaque conf.hlinks x y;
3917 | _ -> ()
3918 end;
3921 let scrollph y =
3922 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3923 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3924 let sh = float conf.winh /. sh in
3925 let sh = max sh (float conf.scrollh) in
3927 let percent =
3928 if y = state.maxy
3929 then 1.0
3930 else float y /. float maxy
3932 let position = (float conf.winh -. sh) *. percent in
3934 let position =
3935 if position +. sh > float conf.winh
3936 then float conf.winh -. sh
3937 else position
3939 position, sh;
3942 let scrollpw x =
3943 let winw = conf.winw - state.scrollw - 1 in
3944 let fwinw = float winw in
3945 let sw =
3946 let sw = fwinw /. float state.w in
3947 let sw = fwinw *. sw in
3948 max sw (float conf.scrollh)
3950 let position, sw =
3951 let f = state.w+winw in
3952 let r = float (winw-x) /. float f in
3953 let p = fwinw *. r in
3954 p-.sw/.2., sw
3956 let sw =
3957 if position +. sw > fwinw
3958 then fwinw -. position
3959 else sw
3961 position, sw;
3964 let scrollindicator () =
3965 GlDraw.color (0.64 , 0.64, 0.64);
3966 GlDraw.rect
3967 (float (conf.winw - state.scrollw), 0.)
3968 (float conf.winw, float conf.winh)
3970 GlDraw.rect
3971 (0., float (conf.winh - state.hscrollh))
3972 (float (conf.winw - state.scrollw - 1), float conf.winh)
3974 GlDraw.color (0.0, 0.0, 0.0);
3976 let position, sh = scrollph state.y in
3977 GlDraw.rect
3978 (float (conf.winw - state.scrollw), position)
3979 (float conf.winw, position +. sh)
3981 let position, sw = scrollpw state.x in
3982 GlDraw.rect
3983 (position, float (conf.winh - state.hscrollh))
3984 (position +. sw, float conf.winh)
3988 let pagetranslatepoint l x y =
3989 let dy = y - l.pagedispy in
3990 let y = dy + l.pagey in
3991 let dx = x - l.pagedispx in
3992 let x = dx + l.pagex in
3993 (x, y);
3996 let showsel () =
3997 match state.mstate with
3998 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4001 | Msel ((x0, y0), (x1, y1)) ->
4002 let rec loop = function
4003 | l :: ls ->
4004 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4005 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4006 then
4007 match getopaque l.pageno with
4008 | Some opaque ->
4009 let dx, dy = pagetranslatepoint l 0 0 in
4010 let x0 = x0 + dx
4011 and y0 = y0 + dy
4012 and x1 = x1 + dx
4013 and y1 = y1 + dy in
4014 GlMat.mode `modelview;
4015 GlMat.push ();
4016 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4017 seltext opaque (x0, y0, x1, y1);
4018 GlMat.pop ();
4019 | _ -> ()
4020 else loop ls
4021 | [] -> ()
4023 loop state.layout
4026 let showrects () =
4027 Gl.enable `blend;
4028 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4029 GlDraw.polygon_mode `both `fill;
4030 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4031 List.iter
4032 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4033 List.iter (fun l ->
4034 if l.pageno = pageno
4035 then (
4036 let dx = float (l.pagedispx - l.pagex) in
4037 let dy = float (l.pagedispy - l.pagey) in
4038 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4039 GlDraw.begins `quads;
4041 GlDraw.vertex2 (x0+.dx, y0+.dy);
4042 GlDraw.vertex2 (x1+.dx, y1+.dy);
4043 GlDraw.vertex2 (x2+.dx, y2+.dy);
4044 GlDraw.vertex2 (x3+.dx, y3+.dy);
4046 GlDraw.ends ();
4048 ) state.layout
4049 ) state.rects
4051 Gl.disable `blend;
4054 let display () =
4055 GlClear.color (scalecolor2 conf.bgcolor);
4056 GlClear.clear [`color];
4057 List.iter drawpage state.layout;
4058 showrects ();
4059 showsel ();
4060 scrollindicator ();
4061 state.uioh#display;
4062 begin match state.mstate with
4063 | Mzoomrect ((x0, y0), (x1, y1)) ->
4064 Gl.enable `blend;
4065 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4066 GlDraw.polygon_mode `both `fill;
4067 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4068 GlDraw.rect (float x0, float y0)
4069 (float x1, float y1);
4070 Gl.disable `blend;
4071 | _ -> ()
4072 end;
4073 enttext ();
4074 Glut.swapBuffers ();
4077 let getunder x y =
4078 let rec f = function
4079 | l :: rest ->
4080 begin match getopaque l.pageno with
4081 | Some opaque ->
4082 let x0 = l.pagedispx in
4083 let x1 = x0 + l.pagevw in
4084 let y0 = l.pagedispy in
4085 let y1 = y0 + l.pagevh in
4086 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4087 then
4088 let px, py = pagetranslatepoint l x y in
4089 match whatsunder opaque px py with
4090 | Unone -> f rest
4091 | under -> under
4092 else f rest
4093 | _ ->
4094 f rest
4096 | [] -> Unone
4098 f state.layout
4101 let zoomrect x y x1 y1 =
4102 let x0 = min x x1
4103 and x1 = max x x1
4104 and y0 = min y y1 in
4105 gotoy (state.y + y0);
4106 state.anchor <- getanchor ();
4107 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4108 let margin =
4109 if state.w < conf.winw - state.scrollw
4110 then (conf.winw - state.scrollw - state.w) / 2
4111 else 0
4113 state.x <- (state.x + margin) - x0;
4114 setzoom zoom;
4115 Glut.setCursor Glut.CURSOR_INHERIT;
4116 state.mstate <- Mnone;
4119 let scrollx x =
4120 let winw = conf.winw - state.scrollw - 1 in
4121 let s = float x /. float winw in
4122 let destx = truncate (float (state.w + winw) *. s) in
4123 state.x <- winw - destx;
4124 gotoy_and_clear_text state.y;
4125 state.mstate <- Mscrollx;
4128 let scrolly y =
4129 let s = float y /. float conf.winh in
4130 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4131 gotoy_and_clear_text desty;
4132 state.mstate <- Mscrolly;
4135 let viewmouse button bstate x y =
4136 match button with
4137 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4138 if Glut.getModifiers () land Glut.active_ctrl != 0
4139 then (
4140 match state.mstate with
4141 | Mzoom (oldn, i) ->
4142 if oldn = n
4143 then (
4144 if i = 2
4145 then
4146 let incr =
4147 match n with
4148 | 4 ->
4149 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4150 | _ ->
4151 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4153 let zoom = conf.zoom -. incr in
4154 setzoom zoom;
4155 state.mstate <- Mzoom (n, 0);
4156 else
4157 state.mstate <- Mzoom (n, i+1);
4159 else state.mstate <- Mzoom (n, 0)
4161 | _ -> state.mstate <- Mzoom (n, 0)
4163 else (
4164 match state.autoscroll with
4165 | Some step -> setautoscrollspeed step (n=4)
4166 | None ->
4167 let incr =
4168 if n = 3
4169 then -conf.scrollstep
4170 else conf.scrollstep
4172 let incr = incr * 2 in
4173 let y = clamp incr in
4174 gotoy_and_clear_text y
4177 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4178 if bstate = Glut.DOWN
4179 then (
4180 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4181 state.mstate <- Mpan (x, y)
4183 else
4184 state.mstate <- Mnone
4186 | Glut.RIGHT_BUTTON ->
4187 if bstate = Glut.DOWN
4188 then (
4189 Glut.setCursor Glut.CURSOR_CYCLE;
4190 let p = (x, y) in
4191 state.mstate <- Mzoomrect (p, p)
4193 else (
4194 match state.mstate with
4195 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4196 | _ ->
4197 Glut.setCursor Glut.CURSOR_INHERIT;
4198 state.mstate <- Mnone
4201 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4202 if bstate = Glut.DOWN
4203 then
4204 let position, sh = scrollph state.y in
4205 if y > truncate position && y < truncate (position +. sh)
4206 then state.mstate <- Mscrolly
4207 else scrolly y
4208 else
4209 state.mstate <- Mnone
4211 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4212 if bstate = Glut.DOWN
4213 then
4214 let position, sw = scrollpw state.x in
4215 if x > truncate position && x < truncate (position +. sw)
4216 then state.mstate <- Mscrollx
4217 else scrollx x
4218 else
4219 state.mstate <- Mnone
4221 | Glut.LEFT_BUTTON ->
4222 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4223 begin match dest with
4224 | Ulinkgoto (pageno, top) ->
4225 if pageno >= 0
4226 then (
4227 addnav ();
4228 gotopage1 pageno top;
4231 | Ulinkuri s ->
4232 gotouri s
4234 | Unone when bstate = Glut.DOWN ->
4235 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4236 state.mstate <- Mpan (x, y);
4238 | Unone | Utext _ ->
4239 if bstate = Glut.DOWN
4240 then (
4241 if conf.angle mod 360 = 0
4242 then (
4243 state.mstate <- Msel ((x, y), (x, y));
4244 G.postRedisplay "mouse select";
4247 else (
4248 match state.mstate with
4249 | Mnone -> ()
4251 | Mzoom _ | Mscrollx | Mscrolly ->
4252 state.mstate <- Mnone
4254 | Mzoomrect ((x0, y0), _) ->
4255 zoomrect x0 y0 x y
4257 | Mpan _ ->
4258 Glut.setCursor Glut.CURSOR_INHERIT;
4259 state.mstate <- Mnone
4261 | Msel ((_, y0), (_, y1)) ->
4262 let f l =
4263 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4264 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4265 then
4266 match getopaque l.pageno with
4267 | Some opaque ->
4268 copysel opaque
4269 | _ -> ()
4271 List.iter f state.layout;
4272 copysel ""; (* ugly *)
4273 Glut.setCursor Glut.CURSOR_INHERIT;
4274 state.mstate <- Mnone;
4278 | _ -> ()
4281 let birdseyemouse button bstate x y
4282 (conf, leftx, _, hooverpageno, anchor) =
4283 match button with
4284 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4285 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4286 let rec loop = function
4287 | [] -> ()
4288 | l :: rest ->
4289 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4290 && x > margin && x < margin + l.pagew
4291 then (
4292 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4294 else loop rest
4296 loop state.layout
4297 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4298 | _ -> ()
4301 let mouse bstate button x y =
4302 state.uioh <- state.uioh#button button bstate x y;
4305 let mouse ~button ~state ~x ~y = mouse state button x y;;
4307 let motion ~x ~y =
4308 state.uioh <- state.uioh#motion x y
4311 let pmotion ~x ~y =
4312 state.uioh <- state.uioh#pmotion x y;
4315 let uioh = object
4316 method display = ()
4318 method key key =
4319 begin match state.mode with
4320 | Textentry textentry -> textentrykeyboard key textentry
4321 | Birdseye birdseye -> birdseyekeyboard key birdseye
4322 | View -> viewkeyboard key
4323 end;
4324 state.uioh
4326 method special key =
4327 begin match state.mode with
4328 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4329 togglebirdseye ()
4331 | Birdseye vals ->
4332 birdseyespecial key vals
4334 | View when key = Glut.KEY_F1 ->
4335 enterhelpmode ()
4337 | View ->
4338 begin match state.autoscroll with
4339 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4340 setautoscrollspeed step (key = Glut.KEY_DOWN)
4342 | _ ->
4343 let y =
4344 match key with
4345 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4346 | Glut.KEY_UP ->
4347 if Glut.getModifiers () land Glut.active_ctrl != 0
4348 then
4349 if Glut.getModifiers () land Glut.active_shift != 0
4350 then (setzoom state.prevzoom; state.y)
4351 else clamp (-conf.winh/2)
4352 else clamp (-conf.scrollstep)
4353 | Glut.KEY_DOWN ->
4354 if Glut.getModifiers () land Glut.active_ctrl != 0
4355 then
4356 if Glut.getModifiers () land Glut.active_shift != 0
4357 then (setzoom state.prevzoom; state.y)
4358 else clamp (conf.winh/2)
4359 else clamp (conf.scrollstep)
4360 | Glut.KEY_PAGE_UP ->
4361 if Glut.getModifiers () land Glut.active_ctrl != 0
4362 then
4363 match state.layout with
4364 | [] -> state.y
4365 | l :: _ -> state.y - l.pagey
4366 else
4367 clamp (-conf.winh)
4368 | Glut.KEY_PAGE_DOWN ->
4369 if Glut.getModifiers () land Glut.active_ctrl != 0
4370 then
4371 match List.rev state.layout with
4372 | [] -> state.y
4373 | l :: _ -> getpagey l.pageno
4374 else
4375 clamp conf.winh
4376 | Glut.KEY_HOME ->
4377 addnav ();
4379 | Glut.KEY_END ->
4380 addnav ();
4381 state.maxy - (if conf.maxhfit then conf.winh else 0)
4383 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4384 Glut.getModifiers () land Glut.active_alt != 0 ->
4385 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4387 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4388 let dx =
4389 if Glut.getModifiers () land Glut.active_ctrl != 0
4390 then (conf.winw / 2)
4391 else 10
4393 state.x <- state.x - dx;
4394 state.y
4395 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4396 let dx =
4397 if Glut.getModifiers () land Glut.active_ctrl != 0
4398 then (conf.winw / 2)
4399 else 10
4401 state.x <- state.x + dx;
4402 state.y
4404 | _ -> state.y
4406 gotoy_and_clear_text y
4409 | Textentry te -> textentryspecial key te
4410 end;
4411 state.uioh
4413 method button button bstate x y =
4414 begin match state.mode with
4415 | View -> viewmouse button bstate x y
4416 | Birdseye beye -> birdseyemouse button bstate x y beye
4417 | Textentry _ -> ()
4418 end;
4419 state.uioh
4421 method motion x y =
4422 begin match state.mode with
4423 | Textentry _ -> ()
4424 | View | Birdseye _ ->
4425 match state.mstate with
4426 | Mzoom _ | Mnone -> ()
4428 | Mpan (x0, y0) ->
4429 let dx = x - x0
4430 and dy = y0 - y in
4431 state.mstate <- Mpan (x, y);
4432 if conf.zoom > 1.0 then state.x <- state.x + dx;
4433 let y = clamp dy in
4434 gotoy_and_clear_text y
4436 | Msel (a, _) ->
4437 state.mstate <- Msel (a, (x, y));
4438 G.postRedisplay "motion select";
4440 | Mscrolly ->
4441 let y = min conf.winh (max 0 y) in
4442 scrolly y
4444 | Mscrollx ->
4445 let x = min conf.winw (max 0 x) in
4446 scrollx x
4448 | Mzoomrect (p0, _) ->
4449 state.mstate <- Mzoomrect (p0, (x, y));
4450 G.postRedisplay "motion zoomrect";
4451 end;
4452 state.uioh
4454 method pmotion x y =
4455 begin match state.mode with
4456 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4457 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4458 let rec loop = function
4459 | [] ->
4460 if hooverpageno != -1
4461 then (
4462 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4463 G.postRedisplay "pmotion birdseye no hoover";
4465 | l :: rest ->
4466 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4467 && x > margin && x < margin + l.pagew
4468 then (
4469 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4470 G.postRedisplay "pmotion birdseye hoover";
4472 else loop rest
4474 loop state.layout
4476 | Textentry _ -> ()
4478 | View ->
4479 match state.mstate with
4480 | Mnone ->
4481 begin match getunder x y with
4482 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
4483 | Ulinkuri uri ->
4484 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
4485 Glut.setCursor Glut.CURSOR_INFO
4486 | Ulinkgoto (page, _) ->
4487 if conf.underinfo
4488 then showtext 'p' ("age: " ^ string_of_int (page+1));
4489 Glut.setCursor Glut.CURSOR_INFO
4490 | Utext s ->
4491 if conf.underinfo then showtext 'f' ("ont: " ^ s);
4492 Glut.setCursor Glut.CURSOR_TEXT
4495 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
4497 end;
4498 state.uioh
4500 method infochanged _ = ()
4501 end;;
4503 module Config =
4504 struct
4505 open Parser
4507 let fontpath = ref "";;
4508 let wmclasshack = ref false;;
4510 let unent s =
4511 let l = String.length s in
4512 let b = Buffer.create l in
4513 unent b s 0 l;
4514 Buffer.contents b;
4517 let home =
4519 match platform with
4520 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
4521 | _ -> Sys.getenv "HOME"
4522 with exn ->
4523 prerr_endline
4524 ("Can not determine home directory location: " ^
4525 Printexc.to_string exn);
4529 let config_of c attrs =
4530 let apply c k v =
4532 match k with
4533 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
4534 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
4535 | "case-insensitive-search" -> { c with icase = bool_of_string v }
4536 | "preload" -> { c with preload = bool_of_string v }
4537 | "page-bias" -> { c with pagebias = int_of_string v }
4538 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
4539 | "auto-scroll-step" ->
4540 { c with autoscrollstep = max 0 (int_of_string v) }
4541 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
4542 | "crop-hack" -> { c with crophack = bool_of_string v }
4543 | "throttle" ->
4544 let mw =
4545 match String.lowercase v with
4546 | "true" -> Some infinity
4547 | "false" -> None
4548 | f -> Some (float_of_string f)
4550 { c with maxwait = mw}
4551 | "highlight-links" -> { c with hlinks = bool_of_string v }
4552 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
4553 | "vertical-margin" ->
4554 { c with interpagespace = max 0 (int_of_string v) }
4555 | "zoom" ->
4556 let zoom = float_of_string v /. 100. in
4557 let zoom = max zoom 0.0 in
4558 { c with zoom = zoom }
4559 | "presentation" -> { c with presentation = bool_of_string v }
4560 | "rotation-angle" -> { c with angle = int_of_string v }
4561 | "width" -> { c with winw = max 20 (int_of_string v) }
4562 | "height" -> { c with winh = max 20 (int_of_string v) }
4563 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
4564 | "proportional-display" -> { c with proportional = bool_of_string v }
4565 | "pixmap-cache-size" ->
4566 { c with memlimit = max 2 (int_of_string_with_suffix v) }
4567 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
4568 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
4569 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
4570 | "persistent-location" -> { c with jumpback = bool_of_string v }
4571 | "background-color" -> { c with bgcolor = color_of_string v }
4572 | "scrollbar-in-presentation" ->
4573 { c with scrollbarinpm = bool_of_string v }
4574 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
4575 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
4576 | "memlimit" ->
4577 { c with mumemlimit = max 1024 (int_of_string_with_suffix v) }
4578 | "checkers" -> { c with checkers = bool_of_string v }
4579 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
4580 | "trim-margins" -> { c with trimmargins = bool_of_string v }
4581 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
4582 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
4583 | "uri-launcher" -> { c with urilauncher = unent v }
4584 | "color-space" -> { c with colorspace = colorspace_of_string v }
4585 | "invert-colors" -> { c with invert = bool_of_string v }
4586 | "brightness" -> { c with colorscale = float_of_string v }
4587 | _ -> c
4588 with exn ->
4589 prerr_endline ("Error processing attribute (`" ^
4590 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
4593 let rec fold c = function
4594 | [] -> c
4595 | (k, v) :: rest ->
4596 let c = apply c k v in
4597 fold c rest
4599 fold c attrs;
4602 let fromstring f pos n v d =
4603 try f v
4604 with exn ->
4605 dolog "Error processing attribute (%S=%S) at %d\n%s"
4606 n v pos (Printexc.to_string exn)
4611 let bookmark_of attrs =
4612 let rec fold title page rely = function
4613 | ("title", v) :: rest -> fold v page rely rest
4614 | ("page", v) :: rest -> fold title v rely rest
4615 | ("rely", v) :: rest -> fold title page v rest
4616 | _ :: rest -> fold title page rely rest
4617 | [] -> title, page, rely
4619 fold "invalid" "0" "0" attrs
4622 let doc_of attrs =
4623 let rec fold path page rely pan = function
4624 | ("path", v) :: rest -> fold v page rely pan rest
4625 | ("page", v) :: rest -> fold path v rely pan rest
4626 | ("rely", v) :: rest -> fold path page v pan rest
4627 | ("pan", v) :: rest -> fold path page rely v rest
4628 | _ :: rest -> fold path page rely pan rest
4629 | [] -> path, page, rely, pan
4631 fold "" "0" "0" "0" attrs
4634 let setconf dst src =
4635 dst.scrollbw <- src.scrollbw;
4636 dst.scrollh <- src.scrollh;
4637 dst.icase <- src.icase;
4638 dst.preload <- src.preload;
4639 dst.pagebias <- src.pagebias;
4640 dst.verbose <- src.verbose;
4641 dst.scrollstep <- src.scrollstep;
4642 dst.maxhfit <- src.maxhfit;
4643 dst.crophack <- src.crophack;
4644 dst.autoscrollstep <- src.autoscrollstep;
4645 dst.maxwait <- src.maxwait;
4646 dst.hlinks <- src.hlinks;
4647 dst.underinfo <- src.underinfo;
4648 dst.interpagespace <- src.interpagespace;
4649 dst.zoom <- src.zoom;
4650 dst.presentation <- src.presentation;
4651 dst.angle <- src.angle;
4652 dst.winw <- src.winw;
4653 dst.winh <- src.winh;
4654 dst.savebmarks <- src.savebmarks;
4655 dst.memlimit <- src.memlimit;
4656 dst.proportional <- src.proportional;
4657 dst.texcount <- src.texcount;
4658 dst.sliceheight <- src.sliceheight;
4659 dst.thumbw <- src.thumbw;
4660 dst.jumpback <- src.jumpback;
4661 dst.bgcolor <- src.bgcolor;
4662 dst.scrollbarinpm <- src.scrollbarinpm;
4663 dst.tilew <- src.tilew;
4664 dst.tileh <- src.tileh;
4665 dst.mumemlimit <- src.mumemlimit;
4666 dst.checkers <- src.checkers;
4667 dst.aalevel <- src.aalevel;
4668 dst.trimmargins <- src.trimmargins;
4669 dst.trimfuzz <- src.trimfuzz;
4670 dst.urilauncher <- src.urilauncher;
4671 dst.colorspace <- src.colorspace;
4672 dst.invert <- src.invert;
4673 dst.colorscale <- src.colorscale;
4676 let get s =
4677 let h = Hashtbl.create 10 in
4678 let dc = { defconf with angle = defconf.angle } in
4679 let rec toplevel v t spos _ =
4680 match t with
4681 | Vdata | Vcdata | Vend -> v
4682 | Vopen ("llppconfig", _, closed) ->
4683 if closed
4684 then v
4685 else { v with f = llppconfig }
4686 | Vopen _ ->
4687 error "unexpected subelement at top level" s spos
4688 | Vclose _ -> error "unexpected close at top level" s spos
4690 and llppconfig v t spos _ =
4691 match t with
4692 | Vdata | Vcdata -> v
4693 | Vend -> error "unexpected end of input in llppconfig" s spos
4694 | Vopen ("defaults", attrs, closed) ->
4695 let c = config_of dc attrs in
4696 setconf dc c;
4697 if closed
4698 then v
4699 else { v with f = skip "defaults" (fun () -> v) }
4701 | Vopen ("ui-font", attrs, closed) ->
4702 let rec getsize size = function
4703 | [] -> size
4704 | ("size", v) :: rest ->
4705 let size =
4706 fromstring int_of_string spos "size" v fstate.fontsize in
4707 getsize size rest
4708 | l -> getsize size l
4710 fstate.fontsize <- getsize fstate.fontsize attrs;
4711 if closed
4712 then v
4713 else { v with f = uifont (Buffer.create 10) }
4715 | Vopen ("doc", attrs, closed) ->
4716 let pathent, spage, srely, span = doc_of attrs in
4717 let path = unent pathent
4718 and pageno = fromstring int_of_string spos "page" spage 0
4719 and rely = fromstring float_of_string spos "rely" srely 0.0
4720 and pan = fromstring int_of_string spos "pan" span 0 in
4721 let c = config_of dc attrs in
4722 let anchor = (pageno, rely) in
4723 if closed
4724 then (Hashtbl.add h path (c, [], pan, anchor); v)
4725 else { v with f = doc path pan anchor c [] }
4727 | Vopen _ ->
4728 error "unexpected subelement in llppconfig" s spos
4730 | Vclose "llppconfig" -> { v with f = toplevel }
4731 | Vclose _ -> error "unexpected close in llppconfig" s spos
4733 and uifont b v t spos epos =
4734 match t with
4735 | Vdata | Vcdata ->
4736 Buffer.add_substring b s spos (epos - spos);
4738 | Vopen (_, _, _) ->
4739 error "unexpected subelement in ui-font" s spos
4740 | Vclose "ui-font" ->
4741 if String.length !fontpath = 0
4742 then fontpath := Buffer.contents b;
4743 { v with f = llppconfig }
4744 | Vclose _ -> error "unexpected close in ui-font" s spos
4745 | Vend -> error "unexpected end of input in ui-font" s spos
4747 and doc path pan anchor c bookmarks v t spos _ =
4748 match t with
4749 | Vdata | Vcdata -> v
4750 | Vend -> error "unexpected end of input in doc" s spos
4751 | Vopen ("bookmarks", _, closed) ->
4752 if closed
4753 then v
4754 else { v with f = pbookmarks path pan anchor c bookmarks }
4756 | Vopen (_, _, _) ->
4757 error "unexpected subelement in doc" s spos
4759 | Vclose "doc" ->
4760 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
4761 { v with f = llppconfig }
4763 | Vclose _ -> error "unexpected close in doc" s spos
4765 and pbookmarks path pan anchor c bookmarks v t spos _ =
4766 match t with
4767 | Vdata | Vcdata -> v
4768 | Vend -> error "unexpected end of input in bookmarks" s spos
4769 | Vopen ("item", attrs, closed) ->
4770 let titleent, spage, srely = bookmark_of attrs in
4771 let page = fromstring int_of_string spos "page" spage 0
4772 and rely = fromstring float_of_string spos "rely" srely 0.0 in
4773 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
4774 if closed
4775 then { v with f = pbookmarks path pan anchor c bookmarks }
4776 else
4777 let f () = v in
4778 { v with f = skip "item" f }
4780 | Vopen _ ->
4781 error "unexpected subelement in bookmarks" s spos
4783 | Vclose "bookmarks" ->
4784 { v with f = doc path pan anchor c bookmarks }
4786 | Vclose _ -> error "unexpected close in bookmarks" s spos
4788 and skip tag f v t spos _ =
4789 match t with
4790 | Vdata | Vcdata -> v
4791 | Vend ->
4792 error ("unexpected end of input in skipped " ^ tag) s spos
4793 | Vopen (tag', _, closed) ->
4794 if closed
4795 then v
4796 else
4797 let f' () = { v with f = skip tag f } in
4798 { v with f = skip tag' f' }
4799 | Vclose ctag ->
4800 if tag = ctag
4801 then f ()
4802 else error ("unexpected close in skipped " ^ tag) s spos
4805 parse { f = toplevel; accu = () } s;
4806 h, dc;
4809 let do_load f ic =
4811 let len = in_channel_length ic in
4812 let s = String.create len in
4813 really_input ic s 0 len;
4814 f s;
4815 with
4816 | Parse_error (msg, s, pos) ->
4817 let subs = subs s pos in
4818 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
4819 failwith ("parse error: " ^ s)
4821 | exn ->
4822 failwith ("config load error: " ^ Printexc.to_string exn)
4825 let defconfpath =
4826 let dir =
4828 let dir = Filename.concat home ".config" in
4829 if Sys.is_directory dir then dir else home
4830 with _ -> home
4832 Filename.concat dir "llpp.conf"
4835 let confpath = ref defconfpath;;
4837 let load1 f =
4838 if Sys.file_exists !confpath
4839 then
4840 match
4841 (try Some (open_in_bin !confpath)
4842 with exn ->
4843 prerr_endline
4844 ("Error opening configuation file `" ^ !confpath ^ "': " ^
4845 Printexc.to_string exn);
4846 None
4848 with
4849 | Some ic ->
4850 begin try
4851 f (do_load get ic)
4852 with exn ->
4853 prerr_endline
4854 ("Error loading configuation from `" ^ !confpath ^ "': " ^
4855 Printexc.to_string exn);
4856 end;
4857 close_in ic;
4859 | None -> ()
4860 else
4861 f (Hashtbl.create 0, defconf)
4864 let load () =
4865 let f (h, dc) =
4866 let pc, pb, px, pa =
4868 Hashtbl.find h (Filename.basename state.path)
4869 with Not_found -> dc, [], 0, (0, 0.0)
4871 setconf defconf dc;
4872 setconf conf pc;
4873 state.bookmarks <- pb;
4874 state.x <- px;
4875 state.scrollw <- conf.scrollbw;
4876 if conf.jumpback
4877 then state.anchor <- pa;
4878 cbput state.hists.nav pa;
4880 load1 f
4883 let add_attrs bb always dc c =
4884 let ob s a b =
4885 if always || a != b
4886 then Printf.bprintf bb "\n %s='%b'" s a
4887 and oi s a b =
4888 if always || a != b
4889 then Printf.bprintf bb "\n %s='%d'" s a
4890 and oI s a b =
4891 if always || a != b
4892 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
4893 and oz s a b =
4894 if always || a <> b
4895 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
4896 and oF s a b =
4897 if always || a <> b
4898 then Printf.bprintf bb "\n %s='%f'" s a
4899 and oc s a b =
4900 if always || a <> b
4901 then
4902 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
4903 and oC s a b =
4904 if always || a <> b
4905 then
4906 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
4907 and oR s a b =
4908 if always || a <> b
4909 then
4910 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
4911 and os s a b =
4912 if always || a <> b
4913 then
4914 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
4915 and oW s a b =
4916 if always || a <> b
4917 then
4918 let v =
4919 match a with
4920 | None -> "false"
4921 | Some f ->
4922 if f = infinity
4923 then "true"
4924 else string_of_float f
4926 Printf.bprintf bb "\n %s='%s'" s v
4928 let w, h =
4929 if always
4930 then dc.winw, dc.winh
4931 else
4932 match state.fullscreen with
4933 | Some wh -> wh
4934 | None -> c.winw, c.winh
4936 let zoom, presentation, interpagespace, maxwait =
4937 if always
4938 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
4939 else
4940 match state.mode with
4941 | Birdseye (bc, _, _, _, _) ->
4942 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
4943 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
4945 oi "width" w dc.winw;
4946 oi "height" h dc.winh;
4947 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
4948 oi "scroll-handle-height" c.scrollh dc.scrollh;
4949 ob "case-insensitive-search" c.icase dc.icase;
4950 ob "preload" c.preload dc.preload;
4951 oi "page-bias" c.pagebias dc.pagebias;
4952 oi "scroll-step" c.scrollstep dc.scrollstep;
4953 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
4954 ob "max-height-fit" c.maxhfit dc.maxhfit;
4955 ob "crop-hack" c.crophack dc.crophack;
4956 oW "throttle" maxwait dc.maxwait;
4957 ob "highlight-links" c.hlinks dc.hlinks;
4958 ob "under-cursor-info" c.underinfo dc.underinfo;
4959 oi "vertical-margin" interpagespace dc.interpagespace;
4960 oz "zoom" zoom dc.zoom;
4961 ob "presentation" presentation dc.presentation;
4962 oi "rotation-angle" c.angle dc.angle;
4963 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
4964 ob "proportional-display" c.proportional dc.proportional;
4965 oI "pixmap-cache-size" c.memlimit dc.memlimit;
4966 oi "tex-count" c.texcount dc.texcount;
4967 oi "slice-height" c.sliceheight dc.sliceheight;
4968 oi "thumbnail-width" c.thumbw dc.thumbw;
4969 ob "persistent-location" c.jumpback dc.jumpback;
4970 oc "background-color" c.bgcolor dc.bgcolor;
4971 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
4972 oi "tile-width" c.tilew dc.tilew;
4973 oi "tile-height" c.tileh dc.tileh;
4974 oI "mupdf-memlimit" c.mumemlimit dc.mumemlimit;
4975 ob "checkers" c.checkers dc.checkers;
4976 oi "aalevel" c.aalevel dc.aalevel;
4977 ob "trim-margins" c.trimmargins dc.trimmargins;
4978 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
4979 os "uri-launcher" c.urilauncher dc.urilauncher;
4980 oC "color-space" c.colorspace dc.colorspace;
4981 ob "invert-colors" c.invert dc.invert;
4982 oF "brightness" c.colorscale dc.colorscale;
4983 if always
4984 then ob "wmclass-hack" !wmclasshack false;
4987 let save () =
4988 let uifontsize = fstate.fontsize in
4989 let bb = Buffer.create 32768 in
4990 let f (h, dc) =
4991 let dc = if conf.bedefault then conf else dc in
4992 Buffer.add_string bb "<llppconfig>\n";
4994 if String.length !fontpath > 0
4995 then
4996 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
4997 uifontsize
4998 !fontpath
4999 else (
5000 if uifontsize <> 14
5001 then
5002 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5005 Buffer.add_string bb "<defaults ";
5006 add_attrs bb true dc dc;
5007 Buffer.add_string bb "/>\n";
5009 let adddoc path pan anchor c bookmarks =
5010 if bookmarks == [] && c = dc && anchor = emptyanchor
5011 then ()
5012 else (
5013 Printf.bprintf bb "<doc path='%s'"
5014 (enent path 0 (String.length path));
5016 if anchor <> emptyanchor
5017 then (
5018 let n, y = anchor in
5019 Printf.bprintf bb " page='%d'" n;
5020 if y > 1e-6
5021 then
5022 Printf.bprintf bb " rely='%f'" y
5026 if pan != 0
5027 then Printf.bprintf bb " pan='%d'" pan;
5029 add_attrs bb false dc c;
5031 begin match bookmarks with
5032 | [] -> Buffer.add_string bb "/>\n"
5033 | _ ->
5034 Buffer.add_string bb ">\n<bookmarks>\n";
5035 List.iter (fun (title, _level, (page, rely)) ->
5036 Printf.bprintf bb
5037 "<item title='%s' page='%d'"
5038 (enent title 0 (String.length title))
5039 page
5041 if rely > 1e-6
5042 then
5043 Printf.bprintf bb " rely='%f'" rely
5045 Buffer.add_string bb "/>\n";
5046 ) bookmarks;
5047 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5048 end;
5052 let pan =
5053 match state.mode with
5054 | Birdseye (_, pan, _, _, _) -> pan
5055 | _ -> state.x
5057 let basename = Filename.basename state.path in
5058 adddoc basename pan (getanchor ())
5059 { conf with
5060 autoscrollstep =
5061 match state.autoscroll with
5062 | Some step -> step
5063 | None -> conf.autoscrollstep }
5064 (if conf.savebmarks then state.bookmarks else []);
5066 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5067 if basename <> path
5068 then adddoc path x y c bookmarks
5069 ) h;
5070 Buffer.add_string bb "</llppconfig>";
5072 load1 f;
5073 if Buffer.length bb > 0
5074 then
5076 let tmp = !confpath ^ ".tmp" in
5077 let oc = open_out_bin tmp in
5078 Buffer.output_buffer oc bb;
5079 close_out oc;
5080 Unix.rename tmp !confpath;
5081 with exn ->
5082 prerr_endline
5083 ("error while saving configuration: " ^ Printexc.to_string exn)
5085 end;;
5087 let () =
5088 Arg.parse
5089 (Arg.align
5090 [("-p", Arg.String (fun s -> state.password <- s) ,
5091 "<password> Set password");
5093 ("-f", Arg.String (fun s -> Config.fontpath := s),
5094 "<path> Set path to the user interface font");
5096 ("-c", Arg.String (fun s -> Config.confpath := s),
5097 "<path> Set path to the configuration file");
5099 ("-v", Arg.Unit (fun () ->
5100 Printf.printf
5101 "%s\nconfiguration path: %s\n"
5102 Help.version
5103 Config.defconfpath
5105 exit 0), " Print version and exit");
5108 (fun s -> state.path <- s)
5109 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5111 if String.length state.path = 0
5112 then (prerr_endline "file name missing"; exit 1);
5114 Config.load ();
5116 let _ = Glut.init Sys.argv in
5117 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5118 let () = Glut.initWindowSize conf.winw conf.winh in
5119 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5121 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5122 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5123 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5125 let csock, ssock =
5126 if not is_windows
5127 then
5128 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5129 else
5130 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5131 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5132 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5133 Unix.bind sock addr;
5134 Unix.listen sock 1;
5135 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5136 Unix.connect csock addr;
5137 let ssock, _ = Unix.accept sock in
5138 Unix.close sock;
5139 let opts sock =
5140 Unix.setsockopt sock Unix.TCP_NODELAY true;
5141 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5143 opts ssock;
5144 opts csock;
5145 ssock, csock
5148 let () = Glut.displayFunc display in
5149 let () = Glut.reshapeFunc reshape in
5150 let () = Glut.keyboardFunc keyboard in
5151 let () = Glut.specialFunc special in
5152 let () = Glut.idleFunc (Some idle) in
5153 let () = Glut.mouseFunc mouse in
5154 let () = Glut.motionFunc motion in
5155 let () = Glut.passiveMotionFunc pmotion in
5157 setcheckers conf.checkers;
5158 init ssock (
5159 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5160 conf.texcount, conf.sliceheight, conf.mumemlimit, conf.colorspace,
5161 !Config.wmclasshack, !Config.fontpath
5163 state.csock <- csock;
5164 state.ssock <- ssock;
5165 state.text <- "Opening " ^ state.path;
5166 setaalevel conf.aalevel;
5167 writeopen state.path state.password;
5168 state.uioh <- uioh;
5169 setfontsize fstate.fontsize;
5171 while true do
5173 Glut.mainLoop ();
5174 with
5175 | Glut.BadEnum "key in special_of_int" ->
5176 showtext '!' " LablGlut bug: special key not recognized";
5178 | Quit ->
5179 wcmd "quit" [];
5180 Config.save ();
5181 exit 0
5182 done;