Add information about trimmed margins
[llpp.git] / main.ml
blob37a7c02932ac50e1bfdfd3ca16414cab403a6e9d
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;;
10 exception Quit;;
12 type params = (angle * proportional * trimparams
13 * texcount * sliceheight * memsize
14 * colorspace * wmclasshack * fontpath)
15 and pageno = int
16 and width = int
17 and height = int
18 and leftx = int
19 and opaque = string
20 and recttype = int
21 and pixmapsize = int
22 and angle = int
23 and proportional = bool
24 and trimmargins = bool
25 and interpagespace = int
26 and texcount = int
27 and sliceheight = int
28 and gen = int
29 and top = float
30 and fontpath = string
31 and memsize = int
32 and aalevel = int
33 and wmclasshack = bool
34 and irect = (int * int * int * int)
35 and trimparams = (trimmargins * irect)
36 and colorspace = | Rgb | Bgr | Gray
39 type platform = | Punknown | Plinux | Pwindows | Posx | Psun
40 | Pfreebsd | Pdragonflybsd | Popenbsd | Pmingw | Pcygwin;;
42 external init : Unix.file_descr -> params -> unit = "ml_init";;
43 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
44 external copysel : string -> unit = "ml_copysel";;
45 external getpdimrect : int -> float array = "ml_getpdimrect";;
46 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
47 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
48 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
49 external measurestr : int -> string -> float = "ml_measure_string";;
50 external getmaxw : unit -> float = "ml_getmaxw";;
51 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
52 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
53 external platform : unit -> platform = "ml_platform";;
54 external setaalevel : int -> unit = "ml_setaalevel";;
56 let platform_to_string = function
57 | Punknown -> "unknown"
58 | Plinux -> "Linux"
59 | Pwindows -> "Windows"
60 | Posx -> "OSX"
61 | Psun -> "Sun"
62 | Pfreebsd -> "FreeBSD"
63 | Pdragonflybsd -> "DragonflyBSD"
64 | Popenbsd -> "OpenBSD"
65 | Pcygwin -> "Cygwin"
66 | Pmingw -> "MingW"
69 let platform = platform ();;
71 let is_windows =
72 match platform with
73 | Pwindows | Pmingw -> true
74 | _ -> false
77 type x = int
78 and y = int
79 and tilex = int
80 and tiley = int
81 and tileparams = (x * y * width * height * tilex * tiley)
84 external drawtile : tileparams -> string -> unit = "ml_drawtile";;
86 type mpos = int * int
87 and mstate =
88 | Msel of (mpos * mpos)
89 | Mpan of mpos
90 | Mscrolly | Mscrollx
91 | Mzoom of (int * int)
92 | Mzoomrect of (mpos * mpos)
93 | Mnone
96 type textentry = string * string * onhist option * onkey * ondone
97 and onkey = string -> int -> te
98 and ondone = string -> unit
99 and histcancel = unit -> unit
100 and onhist = ((histcmd -> string) * histcancel)
101 and histcmd = HCnext | HCprev | HCfirst | HClast
102 and te =
103 | TEstop
104 | TEdone of string
105 | TEcont of string
106 | TEswitch of textentry
109 type 'a circbuf =
110 { store : 'a array
111 ; mutable rc : int
112 ; mutable wc : int
113 ; mutable len : int
117 let bound v minv maxv =
118 max minv (min maxv v);
121 let cbnew n v =
122 { store = Array.create n v
123 ; rc = 0
124 ; wc = 0
125 ; len = 0
129 let drawstring size x y s =
130 Gl.enable `blend;
131 Gl.enable `texture_2d;
132 ignore (drawstr size x y s);
133 Gl.disable `blend;
134 Gl.disable `texture_2d;
137 let drawstring1 size x y s =
138 drawstr size x y s;
141 let drawstring2 size x y fmt =
142 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
145 let cbcap b = Array.length b.store;;
147 let cbput b v =
148 let cap = cbcap b in
149 b.store.(b.wc) <- v;
150 b.wc <- (b.wc + 1) mod cap;
151 b.rc <- b.wc;
152 b.len <- min (b.len + 1) cap;
155 let cbempty b = b.len = 0;;
157 let cbgetg b circular dir =
158 if cbempty b
159 then b.store.(0)
160 else
161 let rc = b.rc + dir in
162 let rc =
163 if circular
164 then (
165 if rc = -1
166 then b.len-1
167 else (
168 if rc = b.len
169 then 0
170 else rc
173 else max 0 (min rc (b.len-1))
175 b.rc <- rc;
176 b.store.(rc);
179 let cbget b = cbgetg b false;;
180 let cbgetc b = cbgetg b true;;
182 type page =
183 { pageno : int
184 ; pagedimno : int
185 ; pagew : int
186 ; pageh : int
187 ; pagex : int
188 ; pagey : int
189 ; pagevw : int
190 ; pagevh : int
191 ; pagedispx : int
192 ; pagedispy : int
196 let debugl l =
197 dolog "l %d dim=%d {" l.pageno l.pagedimno;
198 dolog " WxH %dx%d" l.pagew l.pageh;
199 dolog " vWxH %dx%d" l.pagevw l.pagevh;
200 dolog " pagex,y %d,%d" l.pagex l.pagey;
201 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
202 dolog "}";
205 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
206 dolog "rect {";
207 dolog " x0,y0=(% f, % f)" x0 y0;
208 dolog " x1,y1=(% f, % f)" x1 y1;
209 dolog " x2,y2=(% f, % f)" x2 y2;
210 dolog " x3,y3=(% f, % f)" x3 y3;
211 dolog "}";
214 type conf =
215 { mutable scrollbw : int
216 ; mutable scrollh : int
217 ; mutable icase : bool
218 ; mutable preload : bool
219 ; mutable pagebias : int
220 ; mutable verbose : bool
221 ; mutable debug : bool
222 ; mutable scrollstep : int
223 ; mutable maxhfit : bool
224 ; mutable crophack : bool
225 ; mutable autoscrollstep : int
226 ; mutable showall : bool
227 ; mutable hlinks : bool
228 ; mutable underinfo : bool
229 ; mutable interpagespace : interpagespace
230 ; mutable zoom : float
231 ; mutable presentation : bool
232 ; mutable angle : angle
233 ; mutable winw : int
234 ; mutable winh : int
235 ; mutable savebmarks : bool
236 ; mutable proportional : proportional
237 ; mutable trimmargins : trimmargins
238 ; mutable trimfuzz : irect
239 ; mutable memlimit : memsize
240 ; mutable texcount : texcount
241 ; mutable sliceheight : sliceheight
242 ; mutable thumbw : width
243 ; mutable jumpback : bool
244 ; mutable bgcolor : float * float * float
245 ; mutable bedefault : bool
246 ; mutable scrollbarinpm : bool
247 ; mutable tilew : int
248 ; mutable tileh : int
249 ; mutable mumemlimit : memsize
250 ; mutable checkers : bool
251 ; mutable aalevel : int
252 ; mutable urilauncher : string
253 ; mutable colorspace : colorspace
254 ; mutable invert : bool
258 type anchor = pageno * top;;
260 type outline = string * int * anchor;;
262 type rect = float * float * float * float * float * float * float * float;;
264 type tile = opaque * pixmapsize * elapsed
265 and elapsed = float;;
266 type pagemapkey = pageno * gen;;
267 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
268 and row = int
269 and col = int;;
271 let emptyanchor = (0, 0.0);;
273 class type uioh = object
274 method display : unit
275 method key : int -> uioh
276 method special : Glut.special_key_t -> uioh
277 method button :
278 Glut.button_t -> Glut.mouse_button_state_t -> int -> int -> uioh
279 method motion : int -> int -> uioh
280 method pmotion : int -> int -> uioh
281 method memusedchanged : unit
282 end;;
284 type mode =
285 | Birdseye of (conf * leftx * pageno * pageno * anchor)
286 | Textentry of (textentry * onleave)
287 | View
288 and onleave = leavetextentrystatus -> unit
289 and leavetextentrystatus = | Cancel | Confirm
290 and helpitem = string * int * action
291 and action =
292 | Noaction
293 | Action of (uioh -> uioh)
296 let isbirdseye = function Birdseye _ -> true | _ -> false;;
297 let istextentry = function Textentry _ -> true | _ -> false;;
299 type currently =
300 | Idle
301 | Loading of (page * gen)
302 | Tiling of (
303 page * opaque * colorspace * angle * gen * col * row * width * height
305 | Outlining of outline list
308 let nouioh : uioh = object (self)
309 method display = ()
310 method key _ = self
311 method special _ = self
312 method button _ _ _ _ = self
313 method motion _ _ = self
314 method pmotion _ _ = self
315 method memusedchanged = ()
316 end;;
318 type state =
319 { mutable csock : Unix.file_descr
320 ; mutable ssock : Unix.file_descr
321 ; mutable w : int
322 ; mutable x : int
323 ; mutable y : int
324 ; mutable scrollw : int
325 ; mutable hscrollh : int
326 ; mutable anchor : anchor
327 ; mutable maxy : int
328 ; mutable layout : page list
329 ; pagemap : (pagemapkey, opaque) Hashtbl.t
330 ; tilemap : (tilemapkey, tile) Hashtbl.t
331 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
332 ; mutable pdims : (pageno * width * height * leftx) list
333 ; mutable pagecount : int
334 ; mutable currently : currently
335 ; mutable mstate : mstate
336 ; mutable searchpattern : string
337 ; mutable rects : (pageno * recttype * rect) list
338 ; mutable rects1 : (pageno * recttype * rect) list
339 ; mutable text : string
340 ; mutable fullscreen : (width * height) option
341 ; mutable mode : mode
342 ; mutable uioh : uioh
343 ; mutable outlines : outline array
344 ; mutable bookmarks : outline list
345 ; mutable path : string
346 ; mutable password : string
347 ; mutable invalidated : int
348 ; mutable colorscale : float
349 ; mutable memused : memsize
350 ; mutable gen : gen
351 ; mutable throttle : (page list * int) option
352 ; mutable autoscroll : int option
353 ; mutable help : helpitem array
354 ; mutable docinfo : (int * string) list
355 ; mutable deadline : float
356 ; mutable texid : GlTex.texture_id option
357 ; hists : hists
358 ; mutable prevzoom : float
359 ; mutable progress : float
361 and hists =
362 { pat : string circbuf
363 ; pag : string circbuf
364 ; nav : anchor circbuf
368 let defconf =
369 { scrollbw = 7
370 ; scrollh = 12
371 ; icase = true
372 ; preload = true
373 ; pagebias = 0
374 ; verbose = false
375 ; debug = false
376 ; scrollstep = 24
377 ; maxhfit = true
378 ; crophack = false
379 ; autoscrollstep = 2
380 ; showall = false
381 ; hlinks = false
382 ; underinfo = false
383 ; interpagespace = 2
384 ; zoom = 1.0
385 ; presentation = false
386 ; angle = 0
387 ; winw = 900
388 ; winh = 900
389 ; savebmarks = true
390 ; proportional = true
391 ; trimmargins = false
392 ; trimfuzz = (0,0,0,0)
393 ; memlimit = 32 lsl 20
394 ; texcount = 256
395 ; sliceheight = 24
396 ; thumbw = 76
397 ; jumpback = true
398 ; bgcolor = (0.5, 0.5, 0.5)
399 ; bedefault = false
400 ; scrollbarinpm = true
401 ; tilew = 2048
402 ; tileh = 2048
403 ; mumemlimit = 128 lsl 20
404 ; checkers = true
405 ; aalevel = 8
406 ; urilauncher =
407 (match platform with
408 | Plinux | Pfreebsd | Pdragonflybsd | Popenbsd | Psun -> "xdg-open \"%s\""
409 | Posx -> "open \"%s\""
410 | Pwindows | Pcygwin | Pmingw -> "iexplore \"%s\""
411 | _ -> "")
412 ; colorspace = Rgb
413 ; invert = false
417 let conf = { defconf with angle = defconf.angle };;
419 let uifontsize = ref 14;;
421 let gotouri uri =
422 if String.length conf.urilauncher = 0
423 then print_endline uri
424 else
425 let re = Str.regexp "%s" in
426 let command = Str.global_replace re uri conf.urilauncher in
427 let optic =
428 try Some (Unix.open_process_in command)
429 with exn ->
430 Printf.eprintf
431 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
432 flush stderr;
433 None
435 match optic with
436 | Some ic -> close_in ic
437 | None -> ()
440 let makehelp () =
441 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
442 Array.of_list (
443 let r = Str.regexp "\\(http://[^ ]+\\)" in
444 List.map (fun s ->
445 if (try Str.search_forward r s 0 with Not_found -> -1) >= 0
446 then
447 let uri = Str.matched_string s in
448 (s, 0, Action (fun u -> gotouri uri; u))
449 else s, 0, Noaction) strings
453 let state =
454 { csock = Unix.stdin
455 ; ssock = Unix.stdin
456 ; x = 0
457 ; y = 0
458 ; w = 0
459 ; scrollw = 0
460 ; hscrollh = 0
461 ; anchor = emptyanchor
462 ; layout = []
463 ; maxy = max_int
464 ; tilelru = Queue.create ()
465 ; pagemap = Hashtbl.create 10
466 ; tilemap = Hashtbl.create 10
467 ; pdims = []
468 ; pagecount = 0
469 ; currently = Idle
470 ; mstate = Mnone
471 ; rects = []
472 ; rects1 = []
473 ; text = ""
474 ; mode = View
475 ; fullscreen = None
476 ; searchpattern = ""
477 ; outlines = [||]
478 ; bookmarks = []
479 ; path = ""
480 ; password = ""
481 ; invalidated = 0
482 ; hists =
483 { nav = cbnew 10 (0, 0.0)
484 ; pat = cbnew 1 ""
485 ; pag = cbnew 1 ""
487 ; colorscale = 1.0
488 ; memused = 0
489 ; gen = 0
490 ; throttle = None
491 ; autoscroll = None
492 ; help = makehelp ()
493 ; docinfo = []
494 ; deadline = nan
495 ; texid = None
496 ; prevzoom = 1.0
497 ; progress = -1.0
498 ; uioh = nouioh
502 let vlog fmt =
503 if conf.verbose
504 then
505 Printf.kprintf prerr_endline fmt
506 else
507 Printf.kprintf ignore fmt
510 module G =
511 struct
512 let postRedisplay who =
513 if conf.verbose
514 then prerr_endline ("redisplay for " ^ who);
515 Glut.postRedisplay ();
517 end;;
519 let addchar s c =
520 let b = Buffer.create (String.length s + 1) in
521 Buffer.add_string b s;
522 Buffer.add_char b c;
523 Buffer.contents b;
526 let colorspace_of_string s =
527 match String.lowercase s with
528 | "rgb" -> Rgb
529 | "bgr" -> Bgr
530 | "gray" -> Gray
531 | _ -> failwith "invalid colorspace"
534 let int_of_colorspace = function
535 | Rgb -> 0
536 | Bgr -> 1
537 | Gray -> 2
540 let colorspace_of_int = function
541 | 0 -> Rgb
542 | 1 -> Bgr
543 | 2 -> Gray
544 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
547 let colorspace_to_string = function
548 | Rgb -> "rgb"
549 | Bgr -> "bgr"
550 | Gray -> "gray"
553 let intentry_with_suffix text key =
554 let c = Char.unsafe_chr key in
555 match Char.lowercase c with
556 | '0' .. '9' ->
557 let text = addchar text c in
558 TEcont text
560 | 'k' | 'm' | 'g' ->
561 let text = addchar text c in
562 TEcont text
564 | _ ->
565 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
566 TEcont text
569 let writecmd fd s =
570 let len = String.length s in
571 let n = 4 + len in
572 let b = Buffer.create n in
573 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
574 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
575 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
576 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
577 Buffer.add_string b s;
578 let s' = Buffer.contents b in
579 let n' = Unix.write fd s' 0 n in
580 if n' != n then failwith "write failed";
583 let readcmd fd =
584 let s = "xxxx" in
585 let n = Unix.read fd s 0 4 in
586 if n != 4 then failwith "incomplete read(len)";
587 let len = 0
588 lor (Char.code s.[0] lsl 24)
589 lor (Char.code s.[1] lsl 16)
590 lor (Char.code s.[2] lsl 8)
591 lor (Char.code s.[3] lsl 0)
593 let s = String.create len in
594 let n = Unix.read fd s 0 len in
595 if n != len then failwith "incomplete read(data)";
599 let makecmd s l =
600 let b = Buffer.create 10 in
601 Buffer.add_string b s;
602 let rec combine = function
603 | [] -> b
604 | x :: xs ->
605 Buffer.add_char b ' ';
606 let s =
607 match x with
608 | `b b -> if b then "1" else "0"
609 | `s s -> s
610 | `i i -> string_of_int i
611 | `f f -> string_of_float f
612 | `I f -> string_of_int (truncate f)
614 Buffer.add_string b s;
615 combine xs;
617 combine l;
620 let wcmd s l =
621 let cmd = Buffer.contents (makecmd s l) in
622 writecmd state.csock cmd;
625 let calcips h =
626 if conf.presentation
627 then
628 let d = conf.winh - h in
629 max 0 ((d + 1) / 2)
630 else
631 conf.interpagespace
634 let calcheight () =
635 let rec f pn ph pi fh l =
636 match l with
637 | (n, _, h, _) :: rest ->
638 let ips = calcips h in
639 let fh =
640 if conf.presentation
641 then fh+ips
642 else (
643 if isbirdseye state.mode && pn = 0
644 then fh + ips
645 else fh
648 let fh = fh + ((n - pn) * (ph + pi)) in
649 f n h ips fh rest;
651 | [] ->
652 let inc =
653 if conf.presentation || (isbirdseye state.mode && pn = 0)
654 then 0
655 else -pi
657 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
658 max 0 fh
660 let fh = f 0 0 0 0 state.pdims in
664 let getpageyh pageno =
665 let rec f pn ph pi y l =
666 match l with
667 | (n, _, h, _) :: rest ->
668 let ips = calcips h in
669 if n >= pageno
670 then
671 let h = if n = pageno then h else ph in
672 if conf.presentation && n = pageno
673 then
674 y + (pageno - pn) * (ph + pi) + pi, h
675 else
676 y + (pageno - pn) * (ph + pi), h
677 else
678 let y = y + (if conf.presentation then pi else 0) in
679 let y = y + (n - pn) * (ph + pi) in
680 f n h ips y rest
682 | [] ->
683 y + (pageno - pn) * (ph + pi), ph
685 f 0 0 0 0 state.pdims
688 let getpagedim pageno =
689 let rec f ppdim l =
690 match l with
691 | (n, _, _, _) as pdim :: rest ->
692 if n >= pageno
693 then (if n = pageno then pdim else ppdim)
694 else f pdim rest
696 | [] -> ppdim
698 f (-1, -1, -1, -1) state.pdims
701 let getpageh pageno =
702 let _, _, h, _ = getpagedim pageno in
706 let getpagew pageno =
707 let _, w, _, _ = getpagedim pageno in
711 let getpagey pageno = fst (getpageyh pageno);;
713 let layout y sh =
714 let sh = sh - state.hscrollh in
715 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
716 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
717 match pdims with
718 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
719 let ips = calcips h in
720 let yinc =
721 if conf.presentation || (isbirdseye state.mode && pageno = 0)
722 then ips
723 else 0
725 (w, h, ips, xoff), rest, pdimno + 1, yinc
726 | _ ->
727 prev, pdims, pdimno, 0
729 let dy = dy + yinc in
730 let py = py + yinc in
731 if pageno = state.pagecount || dy >= sh
732 then
733 accu
734 else
735 let vy = y + dy in
736 if py + h <= vy - yinc
737 then
738 let py = py + h + ips in
739 let dy = max 0 (py - y) in
740 f ~pageno:(pageno+1)
741 ~pdimno
742 ~prev:curr
745 ~pdims:rest
746 ~accu
747 else
748 let pagey = vy - py in
749 let pagevh = h - pagey in
750 let pagevh = min (sh - dy) pagevh in
751 let off = if yinc > 0 then py - vy else 0 in
752 let py = py + h + ips in
753 let pagex, dx =
754 let xoff = xoff +
755 if state.w < conf.winw - state.scrollw
756 then (conf.winw - state.scrollw - state.w) / 2
757 else 0
759 let dispx = xoff + state.x in
760 if dispx < 0
761 then (-dispx, 0)
762 else (0, dispx)
764 let pagevw =
765 let lw = w - pagex in
766 min lw (conf.winw - state.scrollw)
768 let e =
769 { pageno = pageno
770 ; pagedimno = pdimno
771 ; pagew = w
772 ; pageh = h
773 ; pagex = pagex
774 ; pagey = pagey + off
775 ; pagevw = pagevw
776 ; pagevh = pagevh - off
777 ; pagedispx = dx
778 ; pagedispy = dy + off
781 let accu = e :: accu in
782 f ~pageno:(pageno+1)
783 ~pdimno
784 ~prev:curr
786 ~dy:(dy+pagevh+ips)
787 ~pdims:rest
788 ~accu
790 if state.invalidated = 0
791 then (
792 let accu =
794 ~pageno:0
795 ~pdimno:~-1
796 ~prev:(0,0,0,0)
797 ~py:0
798 ~dy:0
799 ~pdims:state.pdims
800 ~accu:[]
802 List.rev accu
804 else
808 let clamp incr =
809 let y = state.y + incr in
810 let y = max 0 y in
811 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
815 let getopaque pageno =
816 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
817 with Not_found -> None
820 let putopaque pageno opaque =
821 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
824 let itertiles l f =
825 let tilex = l.pagex mod conf.tilew in
826 let tiley = l.pagey mod conf.tileh in
828 let col = l.pagex / conf.tilew in
829 let row = l.pagey / conf.tileh in
831 let vw =
832 let a = l.pagew - l.pagex in
833 let b = conf.winw - state.scrollw in
834 min a b
835 and vh = l.pagevh in
837 let rec rowloop row y0 dispy h =
838 if h = 0
839 then ()
840 else (
841 let dh = conf.tileh - y0 in
842 let dh = min h dh in
843 let rec colloop col x0 dispx w =
844 if w = 0
845 then ()
846 else (
847 let dw = conf.tilew - x0 in
848 let dw = min w dw in
850 f col row dispx dispy x0 y0 dw dh;
851 colloop (col+1) 0 (dispx+dw) (w-dw)
854 colloop col tilex l.pagedispx vw;
855 rowloop (row+1) 0 (dispy+dh) (h-dh)
858 if vw > 0 && vh > 0
859 then rowloop row tiley l.pagedispy vh;
862 let gettileopaque l col row =
863 let key =
864 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
866 try Some (Hashtbl.find state.tilemap key)
867 with Not_found -> None
870 let puttileopaque l col row gen colorspace angle opaque size elapsed =
871 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
872 Hashtbl.add state.tilemap key (opaque, size, elapsed)
875 let drawtiles l color =
876 GlDraw.color color;
877 let f col row x y tilex tiley w h =
878 match gettileopaque l col row with
879 | Some (opaque, _, t) ->
880 let params = x, y, w, h, tilex, tiley in
881 if conf.invert
882 then (
883 Gl.enable `blend;
884 GlFunc.blend_func `zero `one_minus_src_color;
886 drawtile params opaque;
887 if conf.invert
888 then Gl.disable `blend;
889 if conf.debug
890 then (
891 let s = Printf.sprintf
892 "%d[%d,%d] %f sec"
893 l.pageno col row t
895 let w = measurestr !uifontsize s in
896 GlMisc.push_attrib [`current];
897 GlDraw.color (0.0, 0.0, 0.0);
898 GlDraw.rect
899 (float (x-2), float (y-2))
900 (float (x+2) +. w, float (y + !uifontsize + 2));
901 GlDraw.color (1.0, 1.0, 1.0);
902 drawstring !uifontsize x (y + !uifontsize - 1) s;
903 GlMisc.pop_attrib ();
906 | _ ->
907 let w =
908 let lw = conf.winw - state.scrollw - x in
909 min lw w
910 and h =
911 let lh = conf.winh - y in
912 min lh h
914 Gl.enable `texture_2d;
915 begin match state.texid with
916 | Some id ->
917 GlTex.bind_texture `texture_2d id;
918 let x0 = float x
919 and y0 = float y
920 and x1 = float (x+w)
921 and y1 = float (y+h) in
923 let tw = float w /. 64.0
924 and th = float h /. 64.0 in
925 let tx0 = float tilex /. 64.0
926 and ty0 = float tiley /. 64.0 in
927 let tx1 = tx0 +. tw
928 and ty1 = ty0 +. th in
929 GlDraw.begins `quads;
930 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
931 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
932 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
933 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
934 GlDraw.ends ();
936 Gl.disable `texture_2d;
937 | None ->
938 GlDraw.color (1.0, 1.0, 1.0);
939 GlDraw.rect
940 (float x, float y)
941 (float (x+w), float (y+h));
942 end;
943 if w > 128 && h > !uifontsize + 10
944 then (
945 GlDraw.color (0.0, 0.0, 0.0);
946 let c, r =
947 if conf.verbose
948 then (col*conf.tilew, row*conf.tileh)
949 else col, row
951 drawstring2 !uifontsize x y "Loading %d [%d,%d]" l.pageno c r;
953 GlDraw.color color;
955 itertiles l f
958 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
960 let tilevisible1 l x y =
961 let ax0 = l.pagex
962 and ax1 = l.pagex + l.pagevw
963 and ay0 = l.pagey
964 and ay1 = l.pagey + l.pagevh in
966 let bx0 = x
967 and by0 = y in
968 let bx1 = min (bx0 + conf.tilew) l.pagew
969 and by1 = min (by0 + conf.tileh) l.pageh in
971 let rx0 = max ax0 bx0
972 and ry0 = max ay0 by0
973 and rx1 = min ax1 bx1
974 and ry1 = min ay1 by1 in
976 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
977 nonemptyintersection
980 let tilevisible layout n x y =
981 let rec findpageinlayout = function
982 | l :: _ when l.pageno = n -> tilevisible1 l x y
983 | _ :: rest -> findpageinlayout rest
984 | [] -> false
986 findpageinlayout layout
989 let tileready l x y =
990 tilevisible1 l x y &&
991 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
994 let tilepage n p layout =
995 let rec loop = function
996 | l :: rest ->
997 if l.pageno = n
998 then
999 let f col row _ _ _ _ _ _ =
1000 if state.currently = Idle
1001 then
1002 match gettileopaque l col row with
1003 | Some _ -> ()
1004 | None ->
1005 let x = col*conf.tilew
1006 and y = row*conf.tileh in
1007 let w =
1008 let w = l.pagew - x in
1009 min w conf.tilew
1011 let h =
1012 let h = l.pageh - y in
1013 min h conf.tileh
1015 wcmd "tile"
1016 [`s p
1017 ;`i x
1018 ;`i y
1019 ;`i w
1020 ;`i h
1022 state.currently <-
1023 Tiling (
1024 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1025 conf.tilew, conf.tileh
1028 itertiles l f;
1029 else
1030 loop rest
1032 | [] -> ()
1034 if state.invalidated = 0 then loop layout;
1037 let preloadlayout visiblepages =
1038 let presentation = conf.presentation in
1039 let interpagespace = conf.interpagespace in
1040 let maxy = state.maxy in
1041 conf.presentation <- false;
1042 conf.interpagespace <- 0;
1043 state.maxy <- calcheight ();
1044 let y =
1045 match visiblepages with
1046 | [] -> 0
1047 | l :: _ -> getpagey l.pageno + l.pagey
1049 let y = if y < conf.winh then 0 else y - conf.winh in
1050 let h = state.y - y + conf.winh*3 in
1051 let pages = layout y h in
1052 conf.presentation <- presentation;
1053 conf.interpagespace <- interpagespace;
1054 state.maxy <- maxy;
1055 pages
1058 let load pages =
1059 let rec loop pages =
1060 if state.currently != Idle
1061 then ()
1062 else
1063 match pages with
1064 | l :: rest ->
1065 begin match getopaque l.pageno with
1066 | None ->
1067 wcmd "page" [`i l.pageno; `i l.pagedimno];
1068 state.currently <- Loading (l, state.gen);
1069 | Some opaque ->
1070 tilepage l.pageno opaque pages;
1071 loop rest
1072 end;
1073 | _ -> ()
1075 if state.invalidated = 0 then loop pages
1078 let preload pages =
1079 load pages;
1080 if conf.preload && state.currently = Idle
1081 then load (preloadlayout pages);
1084 let layoutready layout =
1085 let rec fold all ls =
1086 all && match ls with
1087 | l :: rest ->
1088 let seen = ref false in
1089 let allvisible = ref true in
1090 let foo col row _ _ _ _ _ _ =
1091 seen := true;
1092 allvisible := !allvisible &&
1093 begin match gettileopaque l col row with
1094 | Some _ -> true
1095 | None -> false
1098 itertiles l foo;
1099 fold (!seen && !allvisible) rest
1100 | [] -> true
1102 let alltilesvisible = fold true layout in
1103 alltilesvisible;
1106 let gotoy y =
1107 let y = bound y 0 state.maxy in
1108 let y, layout, proceed =
1109 if conf.showall
1110 then
1111 match state.throttle with
1112 | None ->
1113 let layout = layout y conf.winh in
1114 let ready = layoutready layout in
1115 if not ready
1116 then (
1117 load layout;
1118 state.throttle <- Some (layout, y);
1120 else G.postRedisplay "gotoy showall (None)";
1121 y, layout, ready
1122 | Some _ -> -1, [], false
1123 else
1124 let layout = layout y conf.winh in
1125 if true || layoutready layout
1126 then G.postRedisplay "gotoy ready";
1127 y, layout, true
1129 if proceed
1130 then (
1131 state.y <- y;
1132 state.layout <- layout;
1133 begin match state.mode with
1134 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1135 if not (pagevisible layout pageno)
1136 then (
1137 match state.layout with
1138 | [] -> ()
1139 | l :: _ ->
1140 state.mode <- Birdseye (
1141 conf, leftx, l.pageno, hooverpageno, anchor
1144 | _ -> ()
1145 end;
1146 preload layout;
1150 let conttiling pageno opaque =
1151 tilepage pageno opaque
1152 (if conf.preload then preloadlayout state.layout else state.layout)
1155 let gotoy_and_clear_text y =
1156 gotoy y;
1157 if not conf.verbose then state.text <- "";
1160 let getanchor () =
1161 match state.layout with
1162 | [] -> emptyanchor
1163 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1166 let getanchory (n, top) =
1167 let y, h = getpageyh n in
1168 y + (truncate (top *. float h));
1171 let gotoanchor anchor =
1172 gotoy (getanchory anchor);
1175 let addnav () =
1176 cbput state.hists.nav (getanchor ());
1179 let getnav dir =
1180 let anchor = cbgetc state.hists.nav dir in
1181 getanchory anchor;
1184 let gotopage n top =
1185 let y, h = getpageyh n in
1186 gotoy_and_clear_text (y + (truncate (top *. float h)));
1189 let gotopage1 n top =
1190 let y = getpagey n in
1191 gotoy_and_clear_text (y + top);
1194 let invalidate () =
1195 state.layout <- [];
1196 state.pdims <- [];
1197 state.rects <- [];
1198 state.rects1 <- [];
1199 state.invalidated <- state.invalidated + 1;
1202 let writeopen path password =
1203 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1206 let opendoc path password =
1207 invalidate ();
1208 state.path <- path;
1209 state.password <- password;
1210 state.gen <- state.gen + 1;
1211 state.docinfo <- [];
1213 setaalevel conf.aalevel;
1214 writeopen path password;
1215 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1216 wcmd "geometry" [`i state.w; `i conf.winh];
1219 let scalecolor c =
1220 let c = c *. state.colorscale in
1221 (c, c, c);
1224 let scalecolor2 (r, g, b) =
1225 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
1228 let represent () =
1229 state.maxy <- calcheight ();
1230 state.hscrollh <-
1231 if state.w <= conf.winw - state.scrollw
1232 then 0
1233 else state.scrollw
1235 match state.mode with
1236 | Birdseye (_, _, pageno, _, _) ->
1237 let y, h = getpageyh pageno in
1238 let top = (conf.winh - h) / 2 in
1239 gotoy (max 0 (y - top))
1240 | _ -> gotoanchor state.anchor
1243 let reshape =
1244 let firsttime = ref true in
1245 fun ~w ~h ->
1246 GlDraw.viewport 0 0 w h;
1247 if state.invalidated = 0 && not !firsttime
1248 then state.anchor <- getanchor ();
1250 firsttime := false;
1251 conf.winw <- w;
1252 let w = truncate (float w *. conf.zoom) - state.scrollw in
1253 let w = max w 2 in
1254 state.w <- w;
1255 conf.winh <- h;
1256 GlMat.mode `modelview;
1257 GlMat.load_identity ();
1259 GlMat.mode `projection;
1260 GlMat.load_identity ();
1261 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1262 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1263 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1265 invalidate ();
1266 wcmd "geometry" [`i w; `i h];
1269 let enttext () =
1270 let len = String.length state.text in
1271 let drawstring s =
1272 let hscrollh =
1273 match state.mode with
1274 | View -> state.hscrollh
1275 | _ -> 0
1277 let rect x w =
1278 GlDraw.rect
1279 (x, float (conf.winh - (!uifontsize + 4) - hscrollh))
1280 (x+.w, float (conf.winh - hscrollh))
1283 let w = float (conf.winw - state.scrollw - 1) in
1284 if state.progress >= 0.0 && state.progress < 1.0
1285 then (
1286 GlDraw.color (0.3, 0.3, 0.3);
1287 let w1 = w *. state.progress in
1288 rect 0.0 w1;
1289 GlDraw.color (0.0, 0.0, 0.0);
1290 rect w1 (w-.w1)
1292 else (
1293 GlDraw.color (0.0, 0.0, 0.0);
1294 rect 0.0 w;
1297 GlDraw.color (1.0, 1.0, 1.0);
1298 drawstring !uifontsize
1299 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1301 match state.mode with
1302 | Textentry ((prefix, text, _, _, _), _) ->
1303 let s =
1304 if len > 0
1305 then
1306 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1307 else
1308 Printf.sprintf "%s%s_" prefix text
1310 drawstring s
1312 | _ ->
1313 if len > 0 then drawstring state.text
1316 let showtext c s =
1317 state.text <- Printf.sprintf "%c%s" c s;
1318 G.postRedisplay "showtext";
1321 let gctiles () =
1322 let len = Queue.length state.tilelru in
1323 let rec loop qpos =
1324 if state.memused <= conf.memlimit
1325 then ()
1326 else (
1327 if qpos < len
1328 then
1329 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1330 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1332 gen = state.gen
1333 && colorspace = conf.colorspace
1334 && angle = conf.angle
1335 && pagew = getpagew n
1336 && pageh = getpageh n
1337 && (
1338 let layout =
1339 if conf.preload
1340 then preloadlayout state.layout
1341 else state.layout
1343 let x = col*conf.tilew
1344 and y = row*conf.tileh in
1345 tilevisible layout n x y
1347 then Queue.push lruitem state.tilelru
1348 else (
1349 wcmd "freetile" [`s p];
1350 state.memused <- state.memused - s;
1351 state.uioh#memusedchanged;
1352 Hashtbl.remove state.tilemap k;
1354 loop (qpos+1)
1357 loop 0
1360 let flushtiles () =
1361 Queue.iter (fun (k, p, s) ->
1362 wcmd "freetile" [`s p];
1363 state.memused <- state.memused - s;
1364 state.uioh#memusedchanged;
1365 Hashtbl.remove state.tilemap k;
1366 ) state.tilelru;
1367 Queue.clear state.tilelru;
1368 load state.layout;
1371 let logcurrently = function
1372 | Idle -> dolog "Idle"
1373 | Loading (l, gen) ->
1374 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1375 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1376 dolog
1377 "Tiling %d[%d,%d] page=%s cs=%s angle"
1378 l.pageno col row pageopaque
1379 (colorspace_to_string colorspace)
1381 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1382 angle gen conf.angle state.gen
1383 tilew tileh
1384 conf.tilew conf.tileh
1386 | Outlining _ ->
1387 dolog "outlining"
1390 let act cmds =
1391 (* dolog "%S" cmds; *)
1392 let op, args =
1393 let spacepos =
1394 try String.index cmds ' '
1395 with Not_found -> -1
1397 if spacepos = -1
1398 then cmds, ""
1399 else
1400 let l = String.length cmds in
1401 let op = String.sub cmds 0 spacepos in
1402 op, begin
1403 if l - spacepos < 2 then ""
1404 else String.sub cmds (spacepos+1) (l-spacepos-1)
1407 match op with
1408 | "clear" ->
1409 state.pdims <- [];
1411 | "clearrects" ->
1412 state.rects <- state.rects1;
1413 G.postRedisplay "clearrects";
1415 | "continue" ->
1416 let n = Scanf.sscanf args "%u" (fun n -> n) in
1417 state.pagecount <- n;
1418 state.invalidated <- state.invalidated - 1;
1419 begin match state.currently with
1420 | Outlining l ->
1421 state.currently <- Idle;
1422 state.outlines <- Array.of_list (List.rev l)
1423 | _ -> ()
1424 end;
1425 if state.invalidated = 0
1426 then represent ();
1427 if not conf.showall
1428 then G.postRedisplay "continue";
1430 | "title" ->
1431 Glut.setWindowTitle args
1433 | "msg" ->
1434 showtext ' ' args
1436 | "vmsg" ->
1437 if conf.verbose
1438 then showtext ' ' args
1440 | "progress" ->
1441 let progress, text = Scanf.sscanf args "%f %n"
1442 (fun f pos ->
1443 f, String.sub args pos (String.length args - pos)
1446 state.text <- text;
1447 state.progress <- progress;
1448 G.postRedisplay "progress"
1450 | "firstmatch" ->
1451 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1452 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1453 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1454 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1456 let y = (getpagey pageno) + truncate y0 in
1457 addnav ();
1458 gotoy y;
1459 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1461 | "match" ->
1462 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1463 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1464 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1465 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1467 state.rects1 <-
1468 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1470 | "page" ->
1471 let pageopaque, t = Scanf.sscanf args "%s %f" (fun p t -> p, t) in
1472 begin match state.currently with
1473 | Loading (l, gen) ->
1474 vlog "page %d took %f sec" l.pageno t;
1475 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1476 begin match state.throttle with
1477 | None ->
1478 let preloadedpages =
1479 if conf.preload
1480 then preloadlayout state.layout
1481 else state.layout
1483 let evict () =
1484 let module IntSet =
1485 Set.Make (struct type t = int let compare = (-) end) in
1486 let set =
1487 List.fold_left (fun s l -> IntSet.add l.pageno s)
1488 IntSet.empty preloadedpages
1490 let evictedpages =
1491 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1492 if not (IntSet.mem pageno set)
1493 then (
1494 wcmd "freepage" [`s opaque];
1495 key :: accu
1497 else accu
1498 ) state.pagemap []
1500 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1502 evict ();
1503 state.currently <- Idle;
1504 if gen = state.gen
1505 then (
1506 tilepage l.pageno pageopaque state.layout;
1507 load state.layout;
1508 load preloadedpages;
1509 if pagevisible state.layout l.pageno
1510 && layoutready state.layout
1511 then G.postRedisplay "page";
1514 | Some (layout, _) ->
1515 state.currently <- Idle;
1516 tilepage l.pageno pageopaque layout;
1517 load state.layout
1518 end;
1520 | _ ->
1521 dolog "Inconsistent loading state";
1522 logcurrently state.currently;
1523 raise Quit;
1526 | "tile" ->
1527 let (x, y, opaque, size, t) =
1528 Scanf.sscanf args "%u %u %s %u %f"
1529 (fun x y p size t -> (x, y, p, size, t))
1531 begin match state.currently with
1532 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1533 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1535 if tilew != conf.tilew || tileh != conf.tileh
1536 then (
1537 wcmd "freetile" [`s opaque];
1538 state.currently <- Idle;
1539 load state.layout;
1541 else (
1542 puttileopaque l col row gen cs angle opaque size t;
1543 state.memused <- state.memused + size;
1544 state.uioh#memusedchanged;
1545 gctiles ();
1546 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1547 opaque, size) state.tilelru;
1549 state.currently <- Idle;
1550 if gen = state.gen
1551 && conf.colorspace = cs
1552 && conf.angle = angle
1553 && tilevisible state.layout l.pageno x y
1554 then conttiling l.pageno pageopaque;
1556 begin match state.throttle with
1557 | None ->
1558 preload state.layout;
1559 if gen = state.gen
1560 && conf.colorspace = cs
1561 && conf.angle = angle
1562 && tilevisible state.layout l.pageno x y
1563 then G.postRedisplay "tile nothrottle";
1565 | Some (layout, y) ->
1566 let ready = layoutready layout in
1567 if ready
1568 then (
1569 state.y <- y;
1570 state.layout <- layout;
1571 state.throttle <- None;
1572 G.postRedisplay "throttle";
1574 else load layout;
1575 end;
1578 | _ ->
1579 dolog "Inconsistent tiling state";
1580 logcurrently state.currently;
1581 raise Quit;
1584 | "pdim" ->
1585 let pdim =
1586 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1588 state.pdims <- pdim :: state.pdims
1590 | "o" ->
1591 let (l, n, t, h, pos) =
1592 Scanf.sscanf args "%u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
1594 let s = String.sub args pos (String.length args - pos) in
1595 let outline = (s, l, (n, float t /. float h)) in
1596 begin match state.currently with
1597 | Outlining outlines ->
1598 state.currently <- Outlining (outline :: outlines)
1599 | Idle ->
1600 state.currently <- Outlining [outline]
1601 | currently ->
1602 dolog "invalid outlining state";
1603 logcurrently currently
1606 | "info" ->
1607 state.docinfo <- (1, args) :: state.docinfo
1609 | "infoend" ->
1610 state.docinfo <- List.rev state.docinfo
1612 | _ ->
1613 dolog "unknown cmd `%S'" cmds
1616 let now = Unix.gettimeofday;;
1618 let idle () =
1619 if state.deadline == nan then state.deadline <- now ();
1620 let rec loop delay =
1621 let timeout =
1622 if delay > 0.0
1623 then max 0.0 (state.deadline -. now ())
1624 else 0.0
1626 let r, _, _ = Unix.select [state.csock] [] [] timeout in
1627 begin match r with
1628 | [] ->
1629 begin match state.autoscroll with
1630 | Some step when step != 0 ->
1631 let y = state.y + step in
1632 let y =
1633 if y < 0
1634 then state.maxy
1635 else if y >= state.maxy then 0 else y
1637 gotoy y;
1638 if state.mode = View
1639 then state.text <- "";
1640 state.deadline <- state.deadline +. 0.005;
1642 | _ ->
1643 state.deadline <- state.deadline +. delay;
1644 end;
1646 | _ ->
1647 let cmd = readcmd state.csock in
1648 act cmd;
1649 loop 0.0
1650 end;
1651 in loop 0.007
1654 let onhist cb =
1655 let rc = cb.rc in
1656 let action = function
1657 | HCprev -> cbget cb ~-1
1658 | HCnext -> cbget cb 1
1659 | HCfirst -> cbget cb ~-(cb.rc)
1660 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1661 and cancel () = cb.rc <- rc
1662 in (action, cancel)
1665 let search pattern forward =
1666 if String.length pattern > 0
1667 then
1668 let pn, py =
1669 match state.layout with
1670 | [] -> 0, 0
1671 | l :: _ ->
1672 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1674 let cmd =
1675 let b = makecmd "search"
1676 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1678 Buffer.add_char b ',';
1679 Buffer.add_string b pattern;
1680 Buffer.add_char b '\000';
1681 Buffer.contents b;
1683 writecmd state.csock cmd;
1686 let intentry text key =
1687 let c = Char.unsafe_chr key in
1688 match c with
1689 | '0' .. '9' ->
1690 let text = addchar text c in
1691 TEcont text
1693 | _ ->
1694 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1695 TEcont text
1698 let textentry text key =
1699 let c = Char.unsafe_chr key in
1700 match c with
1701 | _ when key >= 32 && key < 127 ->
1702 let text = addchar text c in
1703 TEcont text
1705 | _ ->
1706 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1707 TEcont text
1710 let reqlayout angle proportional =
1711 match state.throttle with
1712 | None ->
1713 if state.invalidated = 0 then state.anchor <- getanchor ();
1714 conf.angle <- angle mod 360;
1715 conf.proportional <- proportional;
1716 invalidate ();
1717 wcmd "reqlayout" [`i conf.angle; `b proportional];
1718 | _ -> ()
1721 let settrim trimmargins trimfuzz =
1722 if state.invalidated = 0 then state.anchor <- getanchor ();
1723 conf.trimmargins <- trimmargins;
1724 conf.trimfuzz <- trimfuzz;
1725 let x0, y0, x1, y1 = trimfuzz in
1726 invalidate ();
1727 wcmd "settrim" [
1728 `b conf.trimmargins;
1729 `i x0;
1730 `i y0;
1731 `i x1;
1732 `i y1;
1734 Hashtbl.iter (fun _ opaque ->
1735 wcmd "freepage" [`s opaque];
1736 ) state.pagemap;
1737 Hashtbl.clear state.pagemap;
1740 let setzoom zoom =
1741 match state.throttle with
1742 | None ->
1743 let zoom = max 0.01 zoom in
1744 if zoom <> conf.zoom
1745 then (
1746 state.prevzoom <- conf.zoom;
1747 let relx =
1748 if zoom <= 1.0
1749 then (state.x <- 0; 0.0)
1750 else float state.x /. float state.w
1752 conf.zoom <- zoom;
1753 reshape conf.winw conf.winh;
1754 if zoom > 1.0
1755 then (
1756 let x = relx *. float state.w in
1757 state.x <- truncate x;
1759 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1762 | _ -> ()
1765 let enterbirdseye () =
1766 let zoom = float conf.thumbw /. float conf.winw in
1767 let birdseyepageno =
1768 let cy = conf.winh / 2 in
1769 let fold = function
1770 | [] -> 0
1771 | l :: rest ->
1772 let rec fold best = function
1773 | [] -> best.pageno
1774 | l :: rest ->
1775 let d = cy - (l.pagedispy + l.pagevh/2)
1776 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1777 if abs d < abs dbest
1778 then fold l rest
1779 else best.pageno
1780 in fold l rest
1782 fold state.layout
1784 state.mode <- Birdseye (
1785 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1787 conf.zoom <- zoom;
1788 conf.presentation <- false;
1789 conf.interpagespace <- 10;
1790 conf.hlinks <- false;
1791 state.x <- 0;
1792 state.mstate <- Mnone;
1793 conf.showall <- false;
1794 Glut.setCursor Glut.CURSOR_INHERIT;
1795 if conf.verbose
1796 then
1797 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1798 (100.0*.zoom)
1799 else
1800 state.text <- ""
1802 reshape conf.winw conf.winh;
1805 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1806 state.mode <- View;
1807 conf.zoom <- c.zoom;
1808 conf.presentation <- c.presentation;
1809 conf.interpagespace <- c.interpagespace;
1810 conf.showall <- c.showall;
1811 conf.hlinks <- c.hlinks;
1812 state.x <- leftx;
1813 if conf.verbose
1814 then
1815 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1816 (100.0*.conf.zoom)
1818 reshape conf.winw conf.winh;
1819 state.anchor <- if goback then anchor else (pageno, 0.0);
1822 let togglebirdseye () =
1823 match state.mode with
1824 | Birdseye vals -> leavebirdseye vals true
1825 | View -> enterbirdseye ()
1826 | _ -> ()
1829 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1830 let pageno = max 0 (pageno - 1) in
1831 let rec loop = function
1832 | [] -> gotopage1 pageno 0
1833 | l :: _ when l.pageno = pageno ->
1834 if l.pagedispy >= 0 && l.pagey = 0
1835 then G.postRedisplay "upbirdseye"
1836 else gotopage1 pageno 0
1837 | _ :: rest -> loop rest
1839 loop state.layout;
1840 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1843 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1844 let pageno = min (state.pagecount - 1) (pageno + 1) in
1845 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1846 let rec loop = function
1847 | [] ->
1848 let y, h = getpageyh pageno in
1849 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1850 gotoy (clamp dy)
1851 | l :: _ when l.pageno = pageno ->
1852 if l.pagevh != l.pageh
1853 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1854 else G.postRedisplay "downbirdseye"
1855 | _ :: rest -> loop rest
1857 loop state.layout
1860 let optentry mode _ key =
1861 let btos b = if b then "on" else "off" in
1862 let c = Char.unsafe_chr key in
1863 match c with
1864 | 's' ->
1865 let ondone s =
1866 try conf.scrollstep <- int_of_string s with exc ->
1867 state.text <- Printf.sprintf "bad integer `%s': %s"
1868 s (Printexc.to_string exc)
1870 TEswitch ("scroll step: ", "", None, intentry, ondone)
1872 | 'A' ->
1873 let ondone s =
1875 conf.autoscrollstep <- int_of_string s;
1876 if state.autoscroll <> None
1877 then state.autoscroll <- Some conf.autoscrollstep
1878 with exc ->
1879 state.text <- Printf.sprintf "bad integer `%s': %s"
1880 s (Printexc.to_string exc)
1882 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1884 | 'Z' ->
1885 let ondone s =
1887 let zoom = float (int_of_string s) /. 100.0 in
1888 setzoom zoom
1889 with exc ->
1890 state.text <- Printf.sprintf "bad integer `%s': %s"
1891 s (Printexc.to_string exc)
1893 TEswitch ("zoom: ", "", None, intentry, ondone)
1895 | 't' ->
1896 let ondone s =
1898 conf.thumbw <- bound (int_of_string s) 2 4096;
1899 state.text <-
1900 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1901 begin match mode with
1902 | Birdseye beye ->
1903 leavebirdseye beye false;
1904 enterbirdseye ();
1905 | _ -> ();
1907 with exc ->
1908 state.text <- Printf.sprintf "bad integer `%s': %s"
1909 s (Printexc.to_string exc)
1911 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
1913 | 'R' ->
1914 let ondone s =
1915 match try
1916 Some (int_of_string s)
1917 with exc ->
1918 state.text <- Printf.sprintf "bad integer `%s': %s"
1919 s (Printexc.to_string exc);
1920 None
1921 with
1922 | Some angle -> reqlayout angle conf.proportional
1923 | None -> ()
1925 TEswitch ("rotation: ", "", None, intentry, ondone)
1927 | 'i' ->
1928 conf.icase <- not conf.icase;
1929 TEdone ("case insensitive search " ^ (btos conf.icase))
1931 | 'p' ->
1932 conf.preload <- not conf.preload;
1933 gotoy state.y;
1934 TEdone ("preload " ^ (btos conf.preload))
1936 | 'v' ->
1937 conf.verbose <- not conf.verbose;
1938 TEdone ("verbose " ^ (btos conf.verbose))
1940 | 'd' ->
1941 conf.debug <- not conf.debug;
1942 TEdone ("debug " ^ (btos conf.debug))
1944 | 'h' ->
1945 conf.maxhfit <- not conf.maxhfit;
1946 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1947 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1949 | 'c' ->
1950 conf.crophack <- not conf.crophack;
1951 TEdone ("crophack " ^ btos conf.crophack)
1953 | 'a' ->
1954 conf.showall <- not conf.showall;
1955 TEdone ("throttle " ^ btos conf.showall)
1957 | 'f' ->
1958 conf.underinfo <- not conf.underinfo;
1959 TEdone ("underinfo " ^ btos conf.underinfo)
1961 | 'P' ->
1962 conf.savebmarks <- not conf.savebmarks;
1963 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1965 | 'S' ->
1966 let ondone s =
1968 let pageno, py =
1969 match state.layout with
1970 | [] -> 0, 0
1971 | l :: _ ->
1972 l.pageno, l.pagey
1974 conf.interpagespace <- int_of_string s;
1975 state.maxy <- calcheight ();
1976 let y = getpagey pageno in
1977 gotoy (y + py)
1978 with exc ->
1979 state.text <- Printf.sprintf "bad integer `%s': %s"
1980 s (Printexc.to_string exc)
1982 TEswitch ("vertical margin: ", "", None, intentry, ondone)
1984 | 'l' ->
1985 reqlayout conf.angle (not conf.proportional);
1986 TEdone ("proportional display " ^ btos conf.proportional)
1988 | 'T' ->
1989 settrim (not conf.trimmargins) conf.trimfuzz;
1990 TEdone ("trim margins " ^ btos conf.trimmargins)
1992 | 'I' ->
1993 conf.invert <- not conf.invert;
1994 TEdone ("invert colors " ^ btos conf.invert)
1996 | _ ->
1997 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1998 TEstop
2001 let maxoutlinerows () = (conf.winh - !uifontsize - 1) / (!uifontsize + 1);;
2003 class type lvsource = object
2004 method getitemcount : int
2005 method getitem : int -> (string * int)
2006 method hasaction : int -> bool
2007 method exit :
2008 uioh:uioh ->
2009 cancel:bool ->
2010 active:int ->
2011 first:int ->
2012 pan:int ->
2013 qsearch:string ->
2014 uioh option
2015 method getactive : int
2016 method getfirst : int
2017 method getqsearch : string
2018 method setqsearch : string -> unit
2019 method getpan : int
2020 end;;
2022 class virtual lvsourcebase = object
2023 val mutable m_active = 0
2024 val mutable m_first = 0
2025 val mutable m_qsearch = ""
2026 val mutable m_pan = 0
2027 method getactive = m_active
2028 method getfirst = m_first
2029 method getqsearch = m_qsearch
2030 method getpan = m_pan
2031 method setqsearch s = m_qsearch <- s
2032 end;;
2034 let textentryspecial key = function
2035 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2036 let s =
2037 match key with
2038 | Glut.KEY_UP -> action HCprev
2039 | Glut.KEY_DOWN -> action HCnext
2040 | Glut.KEY_HOME -> action HCfirst
2041 | Glut.KEY_END -> action HClast
2042 | _ -> state.text
2044 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2045 G.postRedisplay "special textentry";
2046 | _ -> ()
2049 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2050 let enttext te =
2051 state.mode <- Textentry (te, onleave);
2052 state.text <- "";
2053 enttext ();
2054 G.postRedisplay "textentrykeyboard enttext";
2056 match Char.unsafe_chr key with
2057 | '\008' -> (* backspace *)
2058 let len = String.length text in
2059 if len = 0
2060 then (
2061 onleave Cancel;
2062 G.postRedisplay "textentrykeyboard after cancel";
2064 else (
2065 let s = String.sub text 0 (len - 1) in
2066 enttext (c, s, opthist, onkey, ondone)
2069 | '\r' | '\n' ->
2070 ondone text;
2071 onleave Confirm;
2072 G.postRedisplay "textentrykeyboard after confirm"
2074 | '\007' (* ctrl-g *)
2075 | '\027' -> (* escape *)
2076 if String.length text = 0
2077 then (
2078 begin match opthist with
2079 | None -> ()
2080 | Some (_, onhistcancel) -> onhistcancel ()
2081 end;
2082 onleave Cancel;
2083 state.text <- "";
2084 G.postRedisplay "textentrykeyboard after cancel2"
2086 else (
2087 enttext (c, "", opthist, onkey, ondone)
2090 | '\127' -> () (* delete *)
2092 | _ ->
2093 begin match onkey text key with
2094 | TEdone text ->
2095 ondone text;
2096 onleave Confirm;
2097 G.postRedisplay "textentrykeyboard after confirm2";
2099 | TEcont text ->
2100 enttext (c, text, opthist, onkey, ondone);
2102 | TEstop ->
2103 onleave Cancel;
2104 state.text <- "";
2105 G.postRedisplay "textentrykeyboard after cancel3"
2107 | TEswitch te ->
2108 state.mode <- Textentry (te, onleave);
2109 G.postRedisplay "textentrykeyboard switch";
2110 end;
2113 let firstof first active =
2114 let maxrows = maxoutlinerows () in
2115 if first > active || abs (first - active) > maxrows - 1
2116 then max 0 (active - (maxrows/2))
2117 else first
2120 class listview ~(source:lvsource) ~trusted =
2121 let coe s = (s :> uioh) in
2122 object (self)
2123 val m_pan = source#getpan
2124 val m_first = source#getfirst
2125 val m_active = source#getactive
2126 val m_qsearch = source#getqsearch
2127 val m_prev_uioh = state.uioh
2129 method private elemunder y =
2130 let n = y / (!uifontsize+1) in
2131 if m_first + n < source#getitemcount
2132 then (
2133 if source#hasaction (m_first + n)
2134 then Some (m_first + n)
2135 else None
2137 else None
2139 method display =
2140 Gl.enable `blend;
2141 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2142 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2143 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2144 GlDraw.color (1., 1., 1.);
2145 Gl.enable `texture_2d;
2146 let fs = !uifontsize in
2147 let nfs = fs + 1 in
2148 let ww = measurestr fs "w" in
2149 let tabw = 30.0*.ww in
2150 let rec loop row =
2151 if (row - m_first) * nfs > conf.winh
2152 then ()
2153 else (
2154 if row >= 0 && row < source#getitemcount
2155 then (
2156 let (s, level) = source#getitem row in
2157 let y = (row - m_first) * nfs in
2158 let x = 5.0 +. float (level + m_pan) *. ww in
2159 if row = m_active
2160 then (
2161 Gl.disable `texture_2d;
2162 GlDraw.polygon_mode `both `line;
2163 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2164 GlDraw.rect (1., float (y + 1))
2165 (float (conf.winw - 1), float (y + fs + 3));
2166 GlDraw.polygon_mode `both `fill;
2167 GlDraw.color (1., 1., 1.);
2168 Gl.enable `texture_2d;
2171 let drawtabularstring s =
2172 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2173 if trusted
2174 then
2175 let tabpos = try String.index s '\t' with Not_found -> -1 in
2176 if tabpos > 0
2177 then
2178 let len = String.length s - tabpos - 1 in
2179 let s1 = String.sub s 0 tabpos
2180 and s2 = String.sub s (tabpos + 1) len in
2181 let nx = drawstr x s1 in
2182 let sw = nx -. x in
2183 let x = x +. (max tabw sw) in
2184 drawstr x s2
2185 else
2186 drawstr x s
2187 else
2188 drawstr x s
2190 let _ = drawtabularstring s in
2191 loop (row+1)
2195 loop 0;
2196 Gl.disable `blend;
2197 Gl.disable `texture_2d;
2199 method private key1 key =
2200 let set active first qsearch =
2201 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2203 let search active pattern incr =
2204 let dosearch re =
2205 let rec loop n =
2206 if n >= 0 && n < source#getitemcount
2207 then (
2208 let s, _ = source#getitem n in
2210 (try ignore (Str.search_forward re s 0); true
2211 with Not_found -> false)
2212 then Some n
2213 else loop (n + incr)
2215 else None
2217 loop active
2220 let re = Str.regexp_case_fold pattern in
2221 dosearch re
2222 with Failure s ->
2223 state.text <- s;
2224 None
2226 match key with
2227 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2228 let incr = if key = 18 then -1 else 1 in
2229 let active, first =
2230 match search (m_active + incr) m_qsearch incr with
2231 | None ->
2232 state.text <- m_qsearch ^ " [not found]";
2233 m_active, m_first
2234 | Some active ->
2235 state.text <- m_qsearch;
2236 active, firstof m_first active
2238 G.postRedisplay "listview ctrl-r/s";
2239 set active first m_qsearch;
2241 | 8 -> (* backspace *)
2242 let len = String.length m_qsearch in
2243 if len = 0
2244 then coe self
2245 else (
2246 if len = 1
2247 then (
2248 state.text <- "";
2249 G.postRedisplay "listview empty qsearch";
2250 set m_active m_first "";
2252 else
2253 let qsearch = String.sub m_qsearch 0 (len - 1) in
2254 let active, first =
2255 match search m_active qsearch ~-1 with
2256 | None ->
2257 state.text <- qsearch ^ " [not found]";
2258 m_active, m_first
2259 | Some active ->
2260 state.text <- qsearch;
2261 active, firstof m_first active
2263 G.postRedisplay "listview backspace qsearch";
2264 set active first qsearch
2267 | _ when key >= 32 && key < 127 ->
2268 let pattern = addchar m_qsearch (Char.chr key) in
2269 let active, first =
2270 match search m_active pattern 1 with
2271 | None ->
2272 state.text <- pattern ^ " [not found]";
2273 m_active, m_first
2274 | Some active ->
2275 state.text <- pattern;
2276 active, firstof m_first active
2278 G.postRedisplay "listview qsearch add";
2279 set active first pattern;
2281 | 27 -> (* escape *)
2282 state.text <- "";
2283 if String.length m_qsearch = 0
2284 then (
2285 G.postRedisplay "list view escape";
2286 begin
2287 match
2288 source#exit (coe self) true m_active m_first m_pan m_qsearch
2289 with
2290 | None -> m_prev_uioh
2291 | Some uioh -> uioh
2294 else (
2295 G.postRedisplay "list view kill qsearch";
2296 source#setqsearch "";
2297 coe {< m_qsearch = "" >}
2300 | 13 -> (* enter *)
2301 state.text <- "";
2302 let self = {< m_qsearch = "" >} in
2303 source#setqsearch "";
2304 let opt =
2305 G.postRedisplay "listview enter";
2306 if m_active >= 0 && m_active < source#getitemcount
2307 then (
2308 source#exit (coe self) false m_active m_first m_pan "";
2310 else (
2311 source#exit (coe self) true m_active m_first m_pan "";
2314 begin match opt with
2315 | None -> m_prev_uioh
2316 | Some uioh -> uioh
2319 | 127 -> (* delete *)
2320 coe self
2322 | _ -> dolog "unknown key %d" key; coe self
2324 method private special1 key =
2325 let maxrows = maxoutlinerows () in
2326 let itemcount = source#getitemcount in
2327 let find start incr =
2328 let rec find i =
2329 if i = -1 || i = itemcount
2330 then -1
2331 else (
2332 if source#hasaction i
2333 then i
2334 else find (i + incr)
2337 find start
2339 let set active first =
2340 let first = bound first 0 (itemcount - maxrows) in
2341 state.text <- "";
2342 coe {< m_active = active; m_first = first >}
2344 let navigate incr =
2345 let isvisible first n = n >= first && n - first <= maxrows in
2346 let active, first =
2347 let incr1 = if incr > 0 then 1 else -1 in
2348 if isvisible m_first m_active
2349 then
2350 let next =
2351 let next = m_active + incr in
2352 let next =
2353 if next < 0 || next >= itemcount
2354 then -1
2355 else find next incr1
2357 if next = -1 || abs (m_active - next) > maxrows
2358 then -1
2359 else next
2361 if next = -1
2362 then
2363 let first = m_first + incr in
2364 let first = bound first 0 (itemcount - 1) in
2365 let next =
2366 let next = m_active + incr in
2367 let next = bound next 0 (itemcount - 1) in
2368 find next ~-incr1
2370 let active = if next = -1 then m_active else next in
2371 active, first
2372 else
2373 let first = min next m_first in
2374 next, first
2375 else
2376 let first = m_first + incr in
2377 let first = bound first 0 (itemcount - 1) in
2378 let active =
2379 let next = m_active + incr in
2380 let next = bound next 0 (itemcount - 1) in
2381 let next = find next incr1 in
2382 if next = -1 || abs (m_active - first) > maxrows
2383 then m_active
2384 else next
2386 active, first
2388 G.postRedisplay "listview navigate";
2389 set active first;
2391 begin match key with
2392 | Glut.KEY_UP -> navigate ~-1
2393 | Glut.KEY_DOWN -> navigate 1
2394 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2395 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2397 | Glut.KEY_RIGHT ->
2398 state.text <- "";
2399 G.postRedisplay "listview right";
2400 coe {< m_pan = m_pan - 1 >}
2402 | Glut.KEY_LEFT ->
2403 state.text <- "";
2404 G.postRedisplay "listview left";
2405 coe {< m_pan = m_pan + 1 >}
2407 | Glut.KEY_HOME ->
2408 let active = find 0 1 in
2409 G.postRedisplay "listview home";
2410 set active 0;
2412 | Glut.KEY_END ->
2413 let first = max 0 (itemcount - maxrows) in
2414 let active = find (itemcount - 1) ~-1 in
2415 G.postRedisplay "listview end";
2416 set active first;
2418 | _ -> coe self
2419 end;
2421 method key key =
2422 match state.mode with
2423 | Textentry te -> textentrykeyboard key te; coe self
2424 | _ -> self#key1 key
2426 method special key =
2427 match state.mode with
2428 | Textentry te -> textentryspecial key te; coe self
2429 | _ -> self#special1 key
2431 method button button bstate _ y =
2432 let opt =
2433 match button with
2434 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2435 begin match self#elemunder y with
2436 | Some n ->
2437 G.postRedisplay "listview click";
2438 source#exit (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2439 | _ ->
2440 Some (coe self)
2442 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2443 let len = source#getitemcount in
2444 let first =
2445 if m_first + maxoutlinerows () >= len
2446 then
2447 m_first
2448 else
2449 let first = m_first + (if n == 3 then -1 else 1) in
2450 bound first 0 (len - 1)
2452 G.postRedisplay "listview wheel";
2453 Some (coe {< m_first = first >})
2454 | _ ->
2455 Some (coe self)
2457 match opt with
2458 | None -> m_prev_uioh
2459 | Some uioh -> uioh
2461 method motion _ _ = coe self
2463 method pmotion _ y =
2464 let n =
2465 match self#elemunder y with
2466 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2467 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2469 let o =
2470 if n != m_active
2471 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2472 else self
2474 coe o
2476 method memusedchanged = ()
2477 end;;
2479 class outlinelistview ~source : uioh =
2480 let coe o = (o :> uioh) in
2481 object
2482 inherit listview ~source:(source :> lvsource) ~trusted:false as super
2484 method key key =
2485 match key with
2486 | 14 -> (* ctrl-n *)
2487 source#narrow m_qsearch;
2488 G.postRedisplay "outline ctrl-n";
2489 coe {< m_first = 0; m_active = 0 >}
2491 | 21 -> (* ctrl-u *)
2492 source#denarrow;
2493 G.postRedisplay "outline ctrl-u";
2494 coe {< m_first = 0; m_active = 0 >}
2496 | 12 -> (* ctrl-l *)
2497 let first = m_active - (maxoutlinerows () / 2) in
2498 G.postRedisplay "outline ctrl-l";
2499 coe {< m_first = first >}
2501 | 127 -> (* delete *)
2502 source#remove m_active;
2503 G.postRedisplay "outline delete";
2504 let active = max 0 (m_active-1) in
2505 coe {< m_first = firstof m_first active; m_active = active >}
2507 | key -> super#key key
2509 method special key =
2510 let maxrows = maxoutlinerows () in
2511 let calcfirst first active =
2512 if active > first
2513 then
2514 let rows = active - first in
2515 if rows > maxrows then active - maxrows else first
2516 else active
2518 let navigate incr =
2519 let active = m_active + incr in
2520 let active = bound active 0 (source#getitemcount - 1) in
2521 let first = calcfirst m_first active in
2522 G.postRedisplay "special outline navigate";
2523 coe {< m_active = active; m_first = first >}
2525 let updownlevel incr =
2526 let len = source#getitemcount in
2527 let _, curlevel = source#getitem m_active in
2528 let rec flow i =
2529 if i = len then i-1 else if i = -1 then 0 else
2530 let _, l = source#getitem i in
2531 if l != curlevel then i else flow (i+incr)
2533 let active = flow m_active in
2534 let first = calcfirst m_first active in
2535 G.postRedisplay "special outline updownlevel";
2536 {< m_active = active; m_first = first >}
2538 match key with
2539 | Glut.KEY_UP -> navigate ~-1
2540 | Glut.KEY_DOWN -> navigate 1
2541 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2542 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2544 | Glut.KEY_RIGHT ->
2545 let o =
2546 if Glut.getModifiers () land Glut.active_ctrl != 0
2547 then (
2548 G.postRedisplay "special outline right";
2549 {< m_pan = m_pan + 1 >}
2551 else updownlevel 1
2553 coe o
2555 | Glut.KEY_LEFT ->
2556 let o =
2557 if Glut.getModifiers () land Glut.active_ctrl != 0
2558 then (
2559 G.postRedisplay "special outline left";
2560 {< m_pan = m_pan - 1 >}
2562 else updownlevel ~-1
2564 coe o
2566 | Glut.KEY_HOME ->
2567 G.postRedisplay "special outline home";
2568 coe {< m_first = 0; m_active = 0 >}
2570 | Glut.KEY_END ->
2571 let active = source#getitemcount - 1 in
2572 let first = max 0 (active - maxrows) in
2573 G.postRedisplay "special outline end";
2574 coe {< m_active = active; m_first = first >}
2576 | _ -> super#special key
2579 let outlinesource usebookmarks =
2580 let empty = [||] in
2581 (object
2582 inherit lvsourcebase
2583 val mutable m_items = empty
2584 val mutable m_orig_items = empty
2585 val mutable m_prev_items = empty
2586 val mutable m_narrow_pattern = ""
2587 val mutable m_hadremovals = false
2589 method getitemcount = Array.length m_items + (if m_hadremovals then 1 else 0)
2591 method getitem n =
2592 if n == Array.length m_items && m_hadremovals
2593 then
2594 ("[Confirm removal]", 0)
2595 else
2596 let s, n, _ = m_items.(n) in
2597 (s, n)
2599 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
2600 ignore (uioh, first, pan, qsearch);
2601 let confrimremoval = m_hadremovals && active = Array.length m_items in
2602 let items =
2603 if String.length m_narrow_pattern = 0
2604 then m_orig_items
2605 else m_items
2607 if not cancel
2608 then (
2609 if not confrimremoval
2610 then(
2611 let _, _, anchor = m_items.(active) in
2612 gotoanchor anchor;
2613 m_items <- items;
2615 else (
2616 state.bookmarks <- Array.to_list m_items;
2617 m_orig_items <- m_items;
2620 else m_items <- items;
2621 None
2623 method hasaction _ = true
2625 method greetmsg =
2626 if Array.length m_items != Array.length m_orig_items
2627 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
2628 else ""
2630 method narrow pattern =
2631 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2632 match reopt with
2633 | None -> ()
2634 | Some re ->
2635 let rec loop accu n =
2636 if n = -1
2637 then (
2638 m_narrow_pattern <- pattern;
2639 m_items <- Array.of_list accu
2641 else
2642 let (s, _, _) as o = m_items.(n) in
2643 let accu =
2644 if (try ignore (Str.search_forward re s 0); true
2645 with Not_found -> false)
2646 then o :: accu
2647 else accu
2649 loop accu (n-1)
2651 loop [] (Array.length m_items - 1)
2653 method denarrow =
2654 m_orig_items <- (
2655 if usebookmarks
2656 then Array.of_list state.bookmarks
2657 else state.outlines
2659 m_items <- m_orig_items
2661 method remove m =
2662 if usebookmarks
2663 then
2664 if m >= 0 && m < Array.length m_items
2665 then (
2666 m_hadremovals <- true;
2667 m_items <- Array.init (Array.length m_items - 1) (fun n ->
2668 let n = if n >= m then n+1 else n in
2669 m_items.(n)
2673 method reset pageno items =
2674 m_hadremovals <- false;
2675 if m_orig_items == empty || m_prev_items != items
2676 then (
2677 m_orig_items <- items;
2678 if String.length m_narrow_pattern = 0
2679 then m_items <- items;
2681 m_prev_items <- items;
2682 let active =
2683 let rec loop n best bestd =
2684 if n = Array.length m_items
2685 then best
2686 else
2687 let (_, _, (outlinepageno, _)) = m_items.(n) in
2688 let d = abs (outlinepageno - pageno) in
2689 if d < bestd
2690 then loop (n+1) n d
2691 else loop (n+1) best bestd
2693 loop 0 ~-1 max_int
2695 m_active <- active;
2696 m_first <- firstof m_first active
2697 end)
2700 let enterselector usebookmarks =
2701 let source = outlinesource usebookmarks in
2702 fun errmsg ->
2703 let outlines =
2704 if usebookmarks
2705 then Array.of_list state.bookmarks
2706 else state.outlines
2708 if Array.length outlines = 0
2709 then (
2710 showtext ' ' errmsg;
2712 else (
2713 state.text <- source#greetmsg;
2714 Glut.setCursor Glut.CURSOR_INHERIT;
2715 let pageno =
2716 match state.layout with
2717 | [] -> -1
2718 | {pageno=pageno} :: _ -> pageno
2720 source#reset pageno outlines;
2721 state.uioh <- new outlinelistview ~source;
2722 G.postRedisplay "enter selector";
2726 let enteroutlinemode =
2727 let f = enterselector false in
2728 fun ()-> f "Document has no outline";
2731 let enterbookmarkmode =
2732 let f = enterselector true in
2733 fun () -> f "Document has no bookmarks (yet)";
2736 let color_of_string s =
2737 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
2738 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
2742 let color_to_string (r, g, b) =
2743 let r = truncate (r *. 256.0)
2744 and g = truncate (g *. 256.0)
2745 and b = truncate (b *. 256.0) in
2746 Printf.sprintf "%d/%d/%d" r g b
2749 let irect_of_string s =
2750 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
2753 let irect_to_string (x0,y0,x1,y1) =
2754 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
2757 let makecheckers () =
2758 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
2759 following to say:
2760 converted by Issac Trotts. July 25, 2002 *)
2761 let image_height = 64
2762 and image_width = 64 in
2764 let make_image () =
2765 let image =
2766 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height in
2767 for i = 0 to image_width - 1 do
2768 for j = 0 to image_height - 1 do
2769 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
2770 (if (i land 8 ) lxor (j land 8) = 0
2771 then [|255;255;255|] else [|200;200;200|])
2772 done
2773 done;
2774 image
2776 let image = make_image () in
2777 let id = GlTex.gen_texture () in
2778 GlTex.bind_texture `texture_2d id;
2779 GlPix.store (`unpack_alignment 1);
2780 GlTex.image2d image;
2781 List.iter (GlTex.parameter ~target:`texture_2d)
2782 [ `wrap_s `repeat;
2783 `wrap_t `repeat;
2784 `mag_filter `nearest;
2785 `min_filter `nearest ];
2789 let setcheckers enabled =
2790 match state.texid with
2791 | None ->
2792 if enabled then state.texid <- Some (makecheckers ())
2794 | Some texid ->
2795 if not enabled
2796 then (
2797 GlTex.delete_texture texid;
2798 state.texid <- None;
2802 let int_of_string_with_suffix s =
2803 let l = String.length s in
2804 let s1, shift =
2805 if l > 1
2806 then
2807 let suffix = Char.lowercase s.[l-1] in
2808 match suffix with
2809 | 'k' -> String.sub s 0 (l-1), 10
2810 | 'm' -> String.sub s 0 (l-1), 20
2811 | 'g' -> String.sub s 0 (l-1), 30
2812 | _ -> s, 0
2813 else s, 0
2815 let n = int_of_string s1 in
2816 let m = n lsl shift in
2817 if m < 0 || m < n
2818 then raise (Failure "value too large")
2819 else m
2822 let string_with_suffix_of_int n =
2823 if n = 0
2824 then "0"
2825 else
2826 let n, s =
2827 if n = 0
2828 then 0, ""
2829 else (
2830 if n land ((1 lsl 20) - 1) = 0
2831 then n lsr 20, "M"
2832 else (
2833 if n land ((1 lsl 10) - 1) = 0
2834 then n lsr 10, "K"
2835 else n, ""
2839 let rec loop s n =
2840 let h = n mod 1000 in
2841 let n = n / 1000 in
2842 if n = 0
2843 then string_of_int h ^ s
2844 else (
2845 let s = Printf.sprintf "_%03d%s" h s in
2846 loop s n
2849 loop "" n ^ s;
2852 let describe_location () =
2853 let f (fn, _) l =
2854 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
2856 let fn, ln = List.fold_left f (-1, -1) state.layout in
2857 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2858 let percent =
2859 if maxy <= 0
2860 then 100.
2861 else (100. *. (float state.y /. float maxy))
2863 if fn = ln
2864 then
2865 Printf.sprintf "page %d of %d [%.2f%%]"
2866 (fn+1) state.pagecount percent
2867 else
2868 Printf.sprintf
2869 "pages %d-%d of %d [%.2f%%]"
2870 (fn+1) (ln+1) state.pagecount percent
2873 let rec enterinfomode =
2874 let btos b = if b then "\xe2\x88\x9a" else "" in
2875 let showextended = ref false in
2876 let leave mode = function
2877 | Confirm -> state.mode <- mode
2878 | Cancel -> state.mode <- mode in
2879 let src =
2880 (object
2881 val mutable m_first_time = true
2882 val mutable m_l = []
2883 val mutable m_a = [||]
2884 val mutable m_prev_uioh = nouioh
2885 val mutable m_prev_mode = View
2887 inherit lvsourcebase
2889 method reset prev_mode prev_uioh =
2890 m_a <- Array.of_list (List.rev m_l);
2891 m_l <- [];
2892 m_prev_mode <- prev_mode;
2893 m_prev_uioh <- prev_uioh;
2894 if m_first_time
2895 then (
2896 let rec loop n =
2897 if n >= Array.length m_a
2898 then ()
2899 else
2900 match m_a.(n) with
2901 | _, _, _, Action _ -> m_active <- n
2902 | _ -> loop (n+1)
2904 loop 0;
2905 m_first_time <- false;
2908 method int name get set =
2909 m_l <-
2910 (name, `int get, 1, Action (
2911 fun u ->
2912 let ondone s =
2913 try set (int_of_string s)
2914 with exn ->
2915 state.text <- Printf.sprintf "bad integer `%s': %s"
2916 s (Printexc.to_string exn)
2918 state.text <- "";
2919 let te = name ^ ": ", "", None, intentry, ondone in
2920 state.mode <- Textentry (te, leave m_prev_mode);
2922 )) :: m_l
2924 method int_with_suffix name get set =
2925 m_l <-
2926 (name, `intws get, 1, Action (
2927 fun u ->
2928 let ondone s =
2929 try set (int_of_string_with_suffix s)
2930 with exn ->
2931 state.text <- Printf.sprintf "bad integer `%s': %s"
2932 s (Printexc.to_string exn)
2934 state.text <- "";
2935 let te =
2936 name ^ ": ", "", None, intentry_with_suffix, ondone
2938 state.mode <- Textentry (te, leave m_prev_mode);
2940 )) :: m_l
2942 method bool ?(offset=1) ?(btos=btos) name get set =
2943 m_l <-
2944 (name, `bool (btos, get), offset, Action (
2945 fun u ->
2946 let v = get () in
2947 set (not v);
2949 )) :: m_l
2951 method color name get set =
2952 m_l <-
2953 (name, `color get, 1, Action (
2954 fun u ->
2955 let invalid = (nan, nan, nan) in
2956 let ondone s =
2957 let c =
2958 try color_of_string s
2959 with exn ->
2960 state.text <- Printf.sprintf "bad color `%s': %s"
2961 s (Printexc.to_string exn);
2962 invalid
2964 if c <> invalid
2965 then set c;
2967 let te = name ^ ": ", "", None, textentry, ondone in
2968 state.text <- color_to_string (get ());
2969 state.mode <- Textentry (te, leave m_prev_mode);
2971 )) :: m_l
2973 method string name get set =
2974 m_l <-
2975 (name, `string get, 1, Action (
2976 fun u ->
2977 let ondone s = set s in
2978 let te = name ^ ": ", "", None, textentry, ondone in
2979 state.mode <- Textentry (te, leave m_prev_mode);
2981 )) :: m_l
2983 method colorspace name get set =
2984 m_l <-
2985 (name, `string get, 1, Action (
2986 fun _ ->
2987 let source =
2988 let vals = [| "rgb"; "bgr"; "gray" |] in
2989 (object
2990 inherit lvsourcebase
2992 initializer
2993 m_active <- int_of_colorspace conf.colorspace;
2994 m_first <- 0;
2996 method getitemcount = Array.length vals
2997 method getitem n = (vals.(n), 0)
2998 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
2999 ignore (uioh, first, pan, qsearch);
3000 if not cancel then set active;
3001 None
3002 method hasaction _ = true
3003 end)
3005 state.text <- "";
3006 new listview ~source ~trusted:true
3007 )) :: m_l
3009 method caption s offset =
3010 m_l <- (s, `empty, offset, Noaction) :: m_l
3012 method caption2 s f offset =
3013 m_l <- (s, `string f, offset, Noaction) :: m_l
3015 method getitemcount = Array.length m_a
3017 method getitem n =
3018 let tostr = function
3019 | `int f -> string_of_int (f ())
3020 | `intws f -> string_with_suffix_of_int (f ())
3021 | `string f -> f ()
3022 | `color f -> color_to_string (f ())
3023 | `bool (btos, f) -> btos (f ())
3024 | `empty -> ""
3026 let name, t, offset, _ = m_a.(n) in
3027 ((let s = tostr t in
3028 if String.length s > 0
3029 then Printf.sprintf "%s\t%s" name s
3030 else name),
3031 offset)
3033 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3034 let uiohopt =
3035 if not cancel
3036 then (
3037 m_qsearch <- qsearch;
3038 let uioh =
3039 match m_a.(active) with
3040 | _, _, _, Action f -> f uioh
3041 | _ -> uioh
3043 Some uioh
3045 else None
3047 m_active <- active;
3048 m_first <- first;
3049 m_pan <- pan;
3050 uiohopt
3052 method hasaction n =
3053 match m_a.(n) with
3054 | _, _, _, Action _ -> true
3055 | _ -> false
3056 end)
3058 fun () ->
3059 let sep () = src#caption "" 0 in
3060 let colorp name get set =
3061 src#string name
3062 (fun () -> color_to_string (get ()))
3063 (fun v ->
3065 let c = color_of_string v in
3066 set c
3067 with exn ->
3068 state.text <- Printf.sprintf "bad color `%s': %s"
3069 v (Printexc.to_string exn);
3072 let oldmode = state.mode in
3073 let birdseye = isbirdseye state.mode in
3074 state.text <- "";
3076 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3078 src#bool "presentation mode"
3079 (fun () -> conf.presentation)
3080 (fun v ->
3081 conf.presentation <- v;
3082 state.anchor <- getanchor ();
3083 represent ());
3085 src#bool "ignore case in searches"
3086 (fun () -> conf.icase)
3087 (fun v -> conf.icase <- v);
3089 src#bool "preload"
3090 (fun () -> conf.preload)
3091 (fun v -> conf.preload <- v);
3093 src#bool "throttle"
3094 (fun () -> conf.showall)
3095 (fun v -> conf.showall <- v);
3097 src#bool "highlight links"
3098 (fun () -> conf.hlinks)
3099 (fun v -> conf.hlinks <- v);
3101 src#bool "under info"
3102 (fun () -> conf.underinfo)
3103 (fun v -> conf.underinfo <- v);
3105 src#bool "persistent bookmarks"
3106 (fun () -> conf.savebmarks)
3107 (fun v -> conf.savebmarks <- v);
3109 src#bool "proportional display"
3110 (fun () -> conf.proportional)
3111 (fun v -> reqlayout conf.angle v);
3113 src#bool "trim margins"
3114 (fun () -> conf.trimmargins)
3115 (fun v -> settrim v conf.trimfuzz);
3117 src#bool "persistent location"
3118 (fun () -> conf.jumpback)
3119 (fun v -> conf.jumpback <- v);
3121 sep ();
3122 src#int "vertical margin"
3123 (fun () -> conf.interpagespace)
3124 (fun n ->
3125 conf.interpagespace <- n;
3126 let pageno, py =
3127 match state.layout with
3128 | [] -> 0, 0
3129 | l :: _ ->
3130 l.pageno, l.pagey
3132 state.maxy <- calcheight ();
3133 let y = getpagey pageno in
3134 gotoy (y + py)
3137 src#int "page bias"
3138 (fun () -> conf.pagebias)
3139 (fun v -> conf.pagebias <- v);
3141 src#int "scroll step"
3142 (fun () -> conf.scrollstep)
3143 (fun n -> conf.scrollstep <- n);
3145 src#int "auto scroll step"
3146 (fun () ->
3147 match state.autoscroll with
3148 | Some step -> step
3149 | _ -> conf.autoscrollstep)
3150 (fun n ->
3151 if state.autoscroll <> None
3152 then state.autoscroll <- Some n;
3153 conf.autoscrollstep <- n);
3155 src#int "zoom"
3156 (fun () -> truncate (conf.zoom *. 100.))
3157 (fun v -> setzoom ((float v) /. 100.));
3159 src#int "rotation"
3160 (fun () -> conf.angle)
3161 (fun v -> reqlayout v conf.proportional);
3163 src#int "scroll bar width"
3164 (fun () -> state.scrollw)
3165 (fun v ->
3166 state.scrollw <- v;
3167 conf.scrollbw <- v;
3168 reshape conf.winw conf.winh;
3171 src#int "scroll handle height"
3172 (fun () -> conf.scrollh)
3173 (fun v -> conf.scrollh <- v;);
3175 src#int "thumbnail width"
3176 (fun () -> conf.thumbw)
3177 (fun v ->
3178 conf.thumbw <- min 4096 v;
3179 match oldmode with
3180 | Birdseye beye ->
3181 leavebirdseye beye false;
3182 enterbirdseye ()
3183 | _ -> ()
3186 sep ();
3187 src#caption "Presentation mode" 0;
3188 src#bool "scrollbar visible"
3189 (fun () -> conf.scrollbarinpm)
3190 (fun v ->
3191 if v != conf.scrollbarinpm
3192 then (
3193 conf.scrollbarinpm <- v;
3194 if conf.presentation
3195 then (
3196 state.scrollw <- if v then conf.scrollbw else 0;
3197 reshape conf.winw conf.winh;
3202 sep ();
3203 src#caption "Pixmap cache" 0;
3204 src#int_with_suffix "size (advisory)"
3205 (fun () -> conf.memlimit)
3206 (fun v -> conf.memlimit <- v);
3208 src#caption2 "used"
3209 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3210 (string_with_suffix_of_int state.memused)
3211 (Hashtbl.length state.tilemap)) 1;
3213 sep ();
3214 src#caption "Layout" 0;
3215 src#caption2 "Dimension"
3216 (fun () ->
3217 Printf.sprintf "%dx%d (virtual %dx%d)"
3218 conf.winw conf.winh
3219 state.w state.maxy)
3221 if conf.debug
3222 then
3223 src#caption2 "Position" (fun () ->
3224 Printf.sprintf "%dx%d" state.x state.y
3226 else
3227 src#caption2 "Visible" (fun () -> describe_location ()) 1
3230 sep ();
3231 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3232 "Save these parameters as global defaults at exit"
3233 (fun () -> conf.bedefault)
3234 (fun v -> conf.bedefault <- v)
3237 sep ();
3238 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3239 src#bool ~offset:0 ~btos "Extended parameters"
3240 (fun () -> !showextended)
3241 (fun v -> showextended := v; enterinfomode ());
3242 if !showextended
3243 then (
3244 src#bool "checkers"
3245 (fun () -> conf.checkers)
3246 (fun v -> conf.checkers <- v; setcheckers v);
3247 src#bool "verbose"
3248 (fun () -> conf.verbose)
3249 (fun v -> conf.verbose <- v);
3250 src#bool "invert colors"
3251 (fun () -> conf.invert)
3252 (fun v -> conf.invert <- v);
3253 src#bool "max fit"
3254 (fun () -> conf.maxhfit)
3255 (fun v -> conf.maxhfit <- v);
3256 src#string "uri launcher"
3257 (fun () -> conf.urilauncher)
3258 (fun v -> conf.urilauncher <- v);
3259 src#string "tile size"
3260 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3261 (fun v ->
3263 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3264 conf.tileh <- max 64 w;
3265 conf.tilew <- max 64 h;
3266 flushtiles ();
3267 with exn ->
3268 state.text <- Printf.sprintf "bad tile size `%s': %s"
3269 v (Printexc.to_string exn));
3270 src#int "anti-aliasing level"
3271 (fun () -> conf.aalevel)
3272 (fun v ->
3273 conf.aalevel <- bound v 0 8;
3274 state.anchor <- getanchor ();
3275 opendoc state.path state.password;
3277 src#int "ui font size"
3278 (fun () -> !uifontsize)
3279 (fun v -> uifontsize := bound v 5 100);
3280 colorp "background color"
3281 (fun () -> conf.bgcolor)
3282 (fun v -> conf.bgcolor <- v);
3283 src#bool "crop hack"
3284 (fun () -> conf.crophack)
3285 (fun v -> conf.crophack <- v);
3286 src#string "trim fuzz"
3287 (fun () -> irect_to_string conf.trimfuzz)
3288 (fun v ->
3290 conf.trimfuzz <- irect_of_string v;
3291 if conf.trimmargins
3292 then settrim true conf.trimfuzz;
3293 with exn ->
3294 state.text <- Printf.sprintf "bad irect `%s': %s"
3295 v (Printexc.to_string exn)
3297 src#colorspace "color space"
3298 (fun () -> colorspace_to_string conf.colorspace)
3299 (fun v ->
3300 conf.colorspace <- colorspace_of_int v;
3301 wcmd "cs" [`i v];
3302 load state.layout;
3306 sep ();
3307 src#caption "Document" 0;
3308 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3309 if conf.trimmargins
3310 then (
3311 src#caption "Trimmed margins" 0;
3312 src#caption ("Dimensions\t" ^ string_of_int (List.length state.pdims)) 1;
3315 src#reset state.mode state.uioh;
3316 let source = (src :> lvsource) in
3317 state.uioh <- object
3318 inherit listview ~source ~trusted:true
3319 val mutable m_prevmemused = 0
3320 method memusedchanged =
3321 if m_prevmemused != state.memused
3322 then (
3323 m_prevmemused <- state.memused;
3324 G.postRedisplay "memusedchanged";
3326 end;
3327 G.postRedisplay "info";
3330 let enterhelpmode =
3331 let source =
3332 (object
3333 inherit lvsourcebase
3334 method getitemcount = Array.length state.help
3335 method getitem n =
3336 let s, n, _ = state.help.(n) in
3337 (s, n)
3339 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3340 let optuioh =
3341 if not cancel
3342 then (
3343 m_qsearch <- qsearch;
3344 match state.help.(active) with
3345 | _, _, Action f -> Some (f uioh)
3346 | _ -> Some (uioh)
3348 else None
3350 m_active <- active;
3351 m_first <- first;
3352 m_pan <- pan;
3353 optuioh
3355 method hasaction n =
3356 match state.help.(n) with
3357 | _, _, Action _ -> true
3358 | _ -> false
3360 initializer
3361 m_active <- -1
3362 end)
3363 in fun () ->
3364 state.uioh <- new listview ~source ~trusted:true;
3365 G.postRedisplay "help";
3368 let quickbookmark ?title () =
3369 match state.layout with
3370 | [] -> ()
3371 | l :: _ ->
3372 let title =
3373 match title with
3374 | None ->
3375 let sec = Unix.gettimeofday () in
3376 let tm = Unix.localtime sec in
3377 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
3378 (l.pageno+1)
3379 tm.Unix.tm_mday
3380 tm.Unix.tm_mon
3381 (tm.Unix.tm_year + 1900)
3382 tm.Unix.tm_hour
3383 tm.Unix.tm_min
3384 | Some title -> title
3386 state.bookmarks <-
3387 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
3388 :: state.bookmarks
3391 let doreshape w h =
3392 state.fullscreen <- None;
3393 Glut.reshapeWindow w h;
3396 let viewkeyboard key =
3397 let enttext te =
3398 let mode = state.mode in
3399 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3400 state.text <- "";
3401 enttext ();
3402 G.postRedisplay "view:enttext"
3404 let c = Char.chr key in
3405 match c with
3406 | '\027' | 'q' -> (* escape *)
3407 begin match state.mstate with
3408 | Mzoomrect _ ->
3409 state.mstate <- Mnone;
3410 Glut.setCursor Glut.CURSOR_INHERIT;
3411 G.postRedisplay "kill zoom rect";
3412 | _ ->
3413 raise Quit
3414 end;
3416 | '\008' -> (* backspace *)
3417 let y = getnav ~-1 in
3418 gotoy_and_clear_text y
3420 | 'o' ->
3421 enteroutlinemode ()
3423 | 'u' ->
3424 state.rects <- [];
3425 state.text <- "";
3426 G.postRedisplay "dehighlight";
3428 | '/' | '?' ->
3429 let ondone isforw s =
3430 cbput state.hists.pat s;
3431 state.searchpattern <- s;
3432 search s isforw
3434 let s = String.create 1 in
3435 s.[0] <- c;
3436 enttext (s, "", Some (onhist state.hists.pat),
3437 textentry, ondone (c ='/'))
3439 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3440 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3441 setzoom (conf.zoom +. incr)
3443 | '+' ->
3444 let ondone s =
3445 let n =
3446 try int_of_string s with exc ->
3447 state.text <- Printf.sprintf "bad integer `%s': %s"
3448 s (Printexc.to_string exc);
3449 max_int
3451 if n != max_int
3452 then (
3453 conf.pagebias <- n;
3454 state.text <- "page bias is now " ^ string_of_int n;
3457 enttext ("page bias: ", "", None, intentry, ondone)
3459 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3460 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3461 setzoom (max 0.01 (conf.zoom -. decr))
3463 | '-' ->
3464 let ondone msg = state.text <- msg in
3465 enttext (
3466 "option [acfhilpstvAPRSZTI]: ", "", None,
3467 optentry state.mode, ondone
3470 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3471 setzoom 1.0
3473 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3474 let zoom = zoomforh conf.winw conf.winh state.scrollw in
3475 if zoom < 1.0
3476 then setzoom zoom
3478 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3479 togglebirdseye ()
3481 | '0' .. '9' ->
3482 let ondone s =
3483 let n =
3484 try int_of_string s with exc ->
3485 state.text <- Printf.sprintf "bad integer `%s': %s"
3486 s (Printexc.to_string exc);
3489 if n >= 0
3490 then (
3491 addnav ();
3492 cbput state.hists.pag (string_of_int n);
3493 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
3496 let pageentry text key =
3497 match Char.unsafe_chr key with
3498 | 'g' -> TEdone text
3499 | _ -> intentry text key
3501 let text = "x" in text.[0] <- c;
3502 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
3504 | 'b' ->
3505 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
3506 reshape conf.winw conf.winh;
3508 | 'l' ->
3509 conf.hlinks <- not conf.hlinks;
3510 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3511 G.postRedisplay "toggle highlightlinks";
3513 | 'a' ->
3514 begin match state.autoscroll with
3515 | Some step ->
3516 conf.autoscrollstep <- step;
3517 state.autoscroll <- None
3518 | None ->
3519 if conf.autoscrollstep = 0
3520 then state.autoscroll <- Some 1
3521 else state.autoscroll <- Some conf.autoscrollstep
3524 | 'P' ->
3525 conf.presentation <- not conf.presentation;
3526 if conf.presentation
3527 then (
3528 if not conf.scrollbarinpm
3529 then state.scrollw <- 0;
3531 else
3532 state.scrollw <- conf.scrollbw;
3534 showtext ' ' ("presentation mode " ^
3535 if conf.presentation then "on" else "off");
3536 state.anchor <- getanchor ();
3537 represent ()
3539 | 'f' ->
3540 begin match state.fullscreen with
3541 | None ->
3542 state.fullscreen <- Some (conf.winw, conf.winh);
3543 Glut.fullScreen ()
3544 | Some (w, h) ->
3545 state.fullscreen <- None;
3546 doreshape w h
3549 | 'g' ->
3550 gotoy_and_clear_text 0
3552 | 'G' ->
3553 gotopage1 (state.pagecount - 1) 0
3555 | 'n' ->
3556 search state.searchpattern true
3558 | 'p' | 'N' ->
3559 search state.searchpattern false
3561 | 't' ->
3562 begin match state.layout with
3563 | [] -> ()
3564 | l :: _ ->
3565 gotoy_and_clear_text (getpagey l.pageno)
3568 | ' ' ->
3569 begin match List.rev state.layout with
3570 | [] -> ()
3571 | l :: _ ->
3572 let pageno = min (l.pageno+1) (state.pagecount-1) in
3573 gotoy_and_clear_text (getpagey pageno)
3576 | '\127' -> (* del *)
3577 begin match state.layout with
3578 | [] -> ()
3579 | l :: _ ->
3580 let pageno = max 0 (l.pageno-1) in
3581 gotoy_and_clear_text (getpagey pageno)
3584 | '=' ->
3585 showtext ' ' (describe_location ());
3587 | 'w' ->
3588 begin match state.layout with
3589 | [] -> ()
3590 | l :: _ ->
3591 doreshape (l.pagew + state.scrollw) l.pageh;
3592 G.postRedisplay "w"
3595 | '\'' ->
3596 enterbookmarkmode ()
3598 | 'h' ->
3599 enterhelpmode ()
3601 | 'i' ->
3602 enterinfomode ()
3604 | 'm' ->
3605 let ondone s =
3606 match state.layout with
3607 | l :: _ ->
3608 state.bookmarks <-
3609 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
3610 :: state.bookmarks
3611 | _ -> ()
3613 enttext ("bookmark: ", "", None, textentry, ondone)
3615 | '~' ->
3616 quickbookmark ();
3617 showtext ' ' "Quick bookmark added";
3619 | 'z' ->
3620 begin match state.layout with
3621 | l :: _ ->
3622 let rect = getpdimrect l.pagedimno in
3623 let w, h =
3624 if conf.crophack
3625 then
3626 (truncate (1.8 *. (rect.(1) -. rect.(0))),
3627 truncate (1.2 *. (rect.(3) -. rect.(0))))
3628 else
3629 (truncate (rect.(1) -. rect.(0)),
3630 truncate (rect.(3) -. rect.(0)))
3632 let w = truncate ((float w)*.conf.zoom)
3633 and h = truncate ((float h)*.conf.zoom) in
3634 if w != 0 && h != 0
3635 then (
3636 state.anchor <- getanchor ();
3637 doreshape (w + state.scrollw) (h + conf.interpagespace)
3639 G.postRedisplay "z";
3641 | [] -> ()
3644 | '\000' -> (* ctrl-2 *)
3645 let maxw = getmaxw () in
3646 if maxw > 0.0
3647 then setzoom (maxw /. float conf.winw)
3649 | '<' | '>' ->
3650 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
3652 | '[' | ']' ->
3653 state.colorscale <-
3654 bound (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
3656 G.postRedisplay "brightness";
3658 | 'k' ->
3659 begin match state.mode with
3660 | Birdseye beye -> upbirdseye beye
3661 | _ -> gotoy (clamp (-conf.scrollstep))
3664 | 'j' ->
3665 begin match state.mode with
3666 | Birdseye beye -> downbirdseye beye
3667 | _ -> gotoy (clamp conf.scrollstep)
3670 | 'r' ->
3671 state.anchor <- getanchor ();
3672 opendoc state.path state.password
3674 | 'v' when conf.debug ->
3675 state.rects <- [];
3676 List.iter (fun l ->
3677 match getopaque l.pageno with
3678 | None -> ()
3679 | Some opaque ->
3680 let x0, y0, x1, y1 = pagebbox opaque in
3681 let a,b = float x0, float y0 in
3682 let c,d = float x1, float y0 in
3683 let e,f = float x1, float y1 in
3684 let h,j = float x0, float y1 in
3685 let rect = (a,b,c,d,e,f,h,j) in
3686 debugrect rect;
3687 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
3688 ) state.layout;
3689 G.postRedisplay "v";
3691 | _ ->
3692 vlog "huh? %d %c" key (Char.chr key);
3695 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
3696 match key with
3697 | 27 -> (* escape *)
3698 leavebirdseye beye true
3700 | 12 -> (* ctrl-l *)
3701 let y, h = getpageyh pageno in
3702 let top = (conf.winh - h) / 2 in
3703 gotoy (max 0 (y - top))
3705 | 13 -> (* enter *)
3706 leavebirdseye beye false
3708 | _ ->
3709 viewkeyboard key
3712 let keyboard ~key ~x ~y =
3713 ignore x;
3714 ignore y;
3715 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
3716 then wcmd "interrupt" []
3717 else state.uioh <- state.uioh#key key
3720 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
3721 match key with
3722 | Glut.KEY_UP -> upbirdseye beye
3723 | Glut.KEY_DOWN -> downbirdseye beye
3725 | Glut.KEY_PAGE_UP ->
3726 begin match state.layout with
3727 | l :: _ ->
3728 if l.pagey != 0
3729 then (
3730 state.mode <- Birdseye (
3731 conf, leftx, l.pageno, hooverpageno, anchor
3733 gotopage1 l.pageno 0;
3735 else (
3736 let layout = layout (state.y-conf.winh) conf.winh in
3737 match layout with
3738 | [] -> gotoy (clamp (-conf.winh))
3739 | l :: _ ->
3740 state.mode <- Birdseye (
3741 conf, leftx, l.pageno, hooverpageno, anchor
3743 gotopage1 l.pageno 0
3746 | [] -> gotoy (clamp (-conf.winh))
3747 end;
3749 | Glut.KEY_PAGE_DOWN ->
3750 begin match List.rev state.layout with
3751 | l :: _ ->
3752 let layout = layout (state.y + conf.winh) conf.winh in
3753 begin match layout with
3754 | [] ->
3755 let incr = l.pageh - l.pagevh in
3756 if incr = 0
3757 then (
3758 state.mode <-
3759 Birdseye (
3760 conf, leftx, state.pagecount - 1, hooverpageno, anchor
3762 G.postRedisplay "birdseye pagedown";
3764 else gotoy (clamp (incr + conf.interpagespace*2));
3766 | l :: _ ->
3767 state.mode <-
3768 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
3769 gotopage1 l.pageno 0;
3772 | [] -> gotoy (clamp conf.winh)
3773 end;
3775 | Glut.KEY_HOME ->
3776 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
3777 gotopage1 0 0
3779 | Glut.KEY_END ->
3780 let pageno = state.pagecount - 1 in
3781 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3782 if not (pagevisible state.layout pageno)
3783 then
3784 let h =
3785 match List.rev state.pdims with
3786 | [] -> conf.winh
3787 | (_, _, h, _) :: _ -> h
3789 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
3790 else G.postRedisplay "birdseye end";
3791 | _ -> ()
3794 let setautoscrollspeed step goingdown =
3795 let incr = max 1 ((abs step) / 2) in
3796 let incr = if goingdown then incr else -incr in
3797 let astep = step + incr in
3798 state.autoscroll <- Some astep;
3801 let special ~key ~x ~y =
3802 ignore x;
3803 ignore y;
3804 state.uioh <- state.uioh#special key
3807 let drawpage l =
3808 let color =
3809 match state.mode with
3810 | Textentry _ -> scalecolor 0.4
3811 | View -> scalecolor 1.0
3812 | Birdseye (_, _, pageno, hooverpageno, _) ->
3813 if l.pageno = hooverpageno
3814 then scalecolor 0.9
3815 else (
3816 if l.pageno = pageno
3817 then scalecolor 1.0
3818 else scalecolor 0.8
3821 drawtiles l color;
3822 begin match getopaque l.pageno with
3823 | Some opaque ->
3824 if tileready l l.pagex l.pagey
3825 then
3826 let x = l.pagedispx - l.pagex
3827 and y = l.pagedispy - l.pagey in
3828 postprocess opaque conf.hlinks x y;
3830 | _ -> ()
3831 end;
3834 let scrollph y =
3835 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3836 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3837 let sh = float conf.winh /. sh in
3838 let sh = max sh (float conf.scrollh) in
3840 let percent =
3841 if y = state.maxy
3842 then 1.0
3843 else float y /. float maxy
3845 let position = (float conf.winh -. sh) *. percent in
3847 let position =
3848 if position +. sh > float conf.winh
3849 then float conf.winh -. sh
3850 else position
3852 position, sh;
3855 let scrollpw x =
3856 let winw = conf.winw - state.scrollw - 1 in
3857 let fwinw = float winw in
3858 let sw =
3859 let sw = fwinw /. float state.w in
3860 let sw = fwinw *. sw in
3861 max sw (float conf.scrollh)
3863 let position, sw =
3864 let f = state.w+winw in
3865 let r = float (winw-x) /. float f in
3866 let p = fwinw *. r in
3867 p-.sw/.2., sw
3869 let sw =
3870 if position +. sw > fwinw
3871 then fwinw -. position
3872 else sw
3874 position, sw;
3877 let scrollindicator () =
3878 GlDraw.color (0.64 , 0.64, 0.64);
3879 GlDraw.rect
3880 (float (conf.winw - state.scrollw), 0.)
3881 (float conf.winw, float conf.winh)
3883 GlDraw.rect
3884 (0., float (conf.winh - state.hscrollh))
3885 (float (conf.winw - state.scrollw - 1), float conf.winh)
3887 GlDraw.color (0.0, 0.0, 0.0);
3889 let position, sh = scrollph state.y in
3890 GlDraw.rect
3891 (float (conf.winw - state.scrollw), position)
3892 (float conf.winw, position +. sh)
3894 let position, sw = scrollpw state.x in
3895 GlDraw.rect
3896 (position, float (conf.winh - state.hscrollh))
3897 (position +. sw, float conf.winh)
3901 let pagetranslatepoint l x y =
3902 let dy = y - l.pagedispy in
3903 let y = dy + l.pagey in
3904 let dx = x - l.pagedispx in
3905 let x = dx + l.pagex in
3906 (x, y);
3909 let showsel () =
3910 match state.mstate with
3911 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
3914 | Msel ((x0, y0), (x1, y1)) ->
3915 let rec loop = function
3916 | l :: ls ->
3917 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3918 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3919 then
3920 match getopaque l.pageno with
3921 | Some opaque ->
3922 let dx, dy = pagetranslatepoint l 0 0 in
3923 let x0 = x0 + dx
3924 and y0 = y0 + dy
3925 and x1 = x1 + dx
3926 and y1 = y1 + dy in
3927 GlMat.mode `modelview;
3928 GlMat.push ();
3929 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
3930 seltext opaque (x0, y0, x1, y1);
3931 GlMat.pop ();
3932 | _ -> ()
3933 else loop ls
3934 | [] -> ()
3936 loop state.layout
3939 let showrects () =
3940 Gl.enable `blend;
3941 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
3942 GlDraw.polygon_mode `both `fill;
3943 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3944 List.iter
3945 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
3946 List.iter (fun l ->
3947 if l.pageno = pageno
3948 then (
3949 let dx = float (l.pagedispx - l.pagex) in
3950 let dy = float (l.pagedispy - l.pagey) in
3951 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
3952 GlDraw.begins `quads;
3954 GlDraw.vertex2 (x0+.dx, y0+.dy);
3955 GlDraw.vertex2 (x1+.dx, y1+.dy);
3956 GlDraw.vertex2 (x2+.dx, y2+.dy);
3957 GlDraw.vertex2 (x3+.dx, y3+.dy);
3959 GlDraw.ends ();
3961 ) state.layout
3962 ) state.rects
3964 Gl.disable `blend;
3967 let display () =
3968 GlClear.color (scalecolor2 conf.bgcolor);
3969 GlClear.clear [`color];
3970 List.iter drawpage state.layout;
3971 showrects ();
3972 showsel ();
3973 scrollindicator ();
3974 state.uioh#display;
3975 begin match state.mstate with
3976 | Mzoomrect ((x0, y0), (x1, y1)) ->
3977 Gl.enable `blend;
3978 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
3979 GlDraw.polygon_mode `both `fill;
3980 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3981 GlDraw.rect (float x0, float y0)
3982 (float x1, float y1);
3983 Gl.disable `blend;
3984 | _ -> ()
3985 end;
3986 enttext ();
3987 Glut.swapBuffers ();
3990 let getunder x y =
3991 let rec f = function
3992 | l :: rest ->
3993 begin match getopaque l.pageno with
3994 | Some opaque ->
3995 let x0 = l.pagedispx in
3996 let x1 = x0 + l.pagevw in
3997 let y0 = l.pagedispy in
3998 let y1 = y0 + l.pagevh in
3999 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4000 then
4001 let px, py = pagetranslatepoint l x y in
4002 match whatsunder opaque px py with
4003 | Unone -> f rest
4004 | under -> under
4005 else f rest
4006 | _ ->
4007 f rest
4009 | [] -> Unone
4011 f state.layout
4014 let zoomrect x y x1 y1 =
4015 let x0 = min x x1
4016 and x1 = max x x1
4017 and y0 = min y y1 in
4018 gotoy (state.y + y0);
4019 state.anchor <- getanchor ();
4020 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4021 state.x <- state.x - x0;
4022 setzoom zoom;
4023 Glut.setCursor Glut.CURSOR_INHERIT;
4024 state.mstate <- Mnone;
4027 let scrollx x =
4028 let winw = conf.winw - state.scrollw - 1 in
4029 let s = float x /. float winw in
4030 let destx = truncate (float (state.w + winw) *. s) in
4031 state.x <- winw - destx;
4032 gotoy_and_clear_text state.y;
4033 state.mstate <- Mscrollx;
4036 let scrolly y =
4037 let s = float y /. float conf.winh in
4038 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4039 gotoy_and_clear_text desty;
4040 state.mstate <- Mscrolly;
4043 let viewmouse button bstate x y =
4044 match button with
4045 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4046 if Glut.getModifiers () land Glut.active_ctrl != 0
4047 then (
4048 match state.mstate with
4049 | Mzoom (oldn, i) ->
4050 if oldn = n
4051 then (
4052 if i = 2
4053 then
4054 let incr =
4055 match n with
4056 | 4 ->
4057 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4058 | _ ->
4059 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4061 let zoom = conf.zoom -. incr in
4062 setzoom zoom;
4063 state.mstate <- Mzoom (n, 0);
4064 else
4065 state.mstate <- Mzoom (n, i+1);
4067 else state.mstate <- Mzoom (n, 0)
4069 | _ -> state.mstate <- Mzoom (n, 0)
4071 else (
4072 match state.autoscroll with
4073 | Some step -> setautoscrollspeed step (n=4)
4074 | None ->
4075 let incr =
4076 if n = 3
4077 then -conf.scrollstep
4078 else conf.scrollstep
4080 let incr = incr * 2 in
4081 let y = clamp incr in
4082 gotoy_and_clear_text y
4085 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4086 if bstate = Glut.DOWN
4087 then (
4088 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4089 state.mstate <- Mpan (x, y)
4091 else
4092 state.mstate <- Mnone
4094 | Glut.RIGHT_BUTTON ->
4095 if bstate = Glut.DOWN
4096 then (
4097 Glut.setCursor Glut.CURSOR_CYCLE;
4098 let p = (x, y) in
4099 state.mstate <- Mzoomrect (p, p)
4101 else (
4102 match state.mstate with
4103 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4104 | _ ->
4105 Glut.setCursor Glut.CURSOR_INHERIT;
4106 state.mstate <- Mnone
4109 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4110 if bstate = Glut.DOWN
4111 then
4112 let position, sh = scrollph state.y in
4113 if y > truncate position && y < truncate (position +. sh)
4114 then state.mstate <- Mscrolly
4115 else scrolly y
4116 else
4117 state.mstate <- Mnone
4119 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4120 if bstate = Glut.DOWN
4121 then
4122 let position, sw = scrollpw state.x in
4123 if x > truncate position && x < truncate (position +. sw)
4124 then state.mstate <- Mscrollx
4125 else scrollx x
4126 else
4127 state.mstate <- Mnone
4129 | Glut.LEFT_BUTTON ->
4130 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4131 begin match dest with
4132 | Ulinkgoto (pageno, top) ->
4133 if pageno >= 0
4134 then (
4135 addnav ();
4136 gotopage1 pageno top;
4139 | Ulinkuri s ->
4140 gotouri s
4142 | Unone when bstate = Glut.DOWN ->
4143 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4144 state.mstate <- Mpan (x, y);
4146 | Unone | Utext _ ->
4147 if bstate = Glut.DOWN
4148 then (
4149 if conf.angle mod 360 = 0
4150 then (
4151 state.mstate <- Msel ((x, y), (x, y));
4152 G.postRedisplay "mouse select";
4155 else (
4156 match state.mstate with
4157 | Mnone -> ()
4159 | Mzoom _ | Mscrollx | Mscrolly ->
4160 state.mstate <- Mnone
4162 | Mzoomrect ((x0, y0), _) ->
4163 zoomrect x0 y0 x y
4165 | Mpan _ ->
4166 Glut.setCursor Glut.CURSOR_INHERIT;
4167 state.mstate <- Mnone
4169 | Msel ((_, y0), (_, y1)) ->
4170 let f l =
4171 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4172 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4173 then
4174 match getopaque l.pageno with
4175 | Some opaque ->
4176 copysel opaque
4177 | _ -> ()
4179 List.iter f state.layout;
4180 copysel ""; (* ugly *)
4181 Glut.setCursor Glut.CURSOR_INHERIT;
4182 state.mstate <- Mnone;
4186 | _ -> ()
4189 let birdseyemouse button bstate x y
4190 (conf, leftx, _, hooverpageno, anchor) =
4191 match button with
4192 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4193 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4194 let rec loop = function
4195 | [] -> ()
4196 | l :: rest ->
4197 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4198 && x > margin && x < margin + l.pagew
4199 then (
4200 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4202 else loop rest
4204 loop state.layout
4205 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4206 | _ -> ()
4209 let mouse bstate button x y =
4210 state.uioh <- state.uioh#button button bstate x y;
4213 let mouse ~button ~state ~x ~y = mouse state button x y;;
4215 let motion ~x ~y =
4216 state.uioh <- state.uioh#motion x y
4219 let pmotion ~x ~y =
4220 state.uioh <- state.uioh#pmotion x y;
4223 let uioh = object
4224 method display = ()
4226 method key key =
4227 begin match state.mode with
4228 | Textentry textentry -> textentrykeyboard key textentry
4229 | Birdseye birdseye -> birdseyekeyboard key birdseye
4230 | View -> viewkeyboard key
4231 end;
4232 state.uioh
4234 method special key =
4235 begin match state.mode with
4236 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4237 togglebirdseye ()
4239 | Birdseye vals ->
4240 birdseyespecial key vals
4242 | View when key = Glut.KEY_F1 ->
4243 enterhelpmode ()
4245 | View ->
4246 begin match state.autoscroll with
4247 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4248 setautoscrollspeed step (key = Glut.KEY_DOWN)
4250 | _ ->
4251 let y =
4252 match key with
4253 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4254 | Glut.KEY_UP ->
4255 if Glut.getModifiers () land Glut.active_ctrl != 0
4256 then
4257 if Glut.getModifiers () land Glut.active_shift != 0
4258 then (setzoom state.prevzoom; state.y)
4259 else clamp (-conf.winh/2)
4260 else clamp (-conf.scrollstep)
4261 | Glut.KEY_DOWN ->
4262 if Glut.getModifiers () land Glut.active_ctrl != 0
4263 then
4264 if Glut.getModifiers () land Glut.active_shift != 0
4265 then (setzoom state.prevzoom; state.y)
4266 else clamp (conf.winh/2)
4267 else clamp (conf.scrollstep)
4268 | Glut.KEY_PAGE_UP ->
4269 if Glut.getModifiers () land Glut.active_ctrl != 0
4270 then
4271 match state.layout with
4272 | [] -> state.y
4273 | l :: _ -> state.y - l.pagey
4274 else
4275 clamp (-conf.winh)
4276 | Glut.KEY_PAGE_DOWN ->
4277 if Glut.getModifiers () land Glut.active_ctrl != 0
4278 then
4279 match List.rev state.layout with
4280 | [] -> state.y
4281 | l :: _ -> getpagey l.pageno
4282 else
4283 clamp conf.winh
4284 | Glut.KEY_HOME ->
4285 addnav ();
4287 | Glut.KEY_END ->
4288 addnav ();
4289 state.maxy - (if conf.maxhfit then conf.winh else 0)
4291 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4292 Glut.getModifiers () land Glut.active_alt != 0 ->
4293 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4295 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4296 let dx =
4297 if Glut.getModifiers () land Glut.active_ctrl != 0
4298 then (conf.winw / 2)
4299 else 10
4301 state.x <- state.x - dx;
4302 state.y
4303 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4304 let dx =
4305 if Glut.getModifiers () land Glut.active_ctrl != 0
4306 then (conf.winw / 2)
4307 else 10
4309 state.x <- state.x + dx;
4310 state.y
4312 | _ -> state.y
4314 gotoy_and_clear_text y
4317 | Textentry te -> textentryspecial key te
4318 end;
4319 state.uioh
4321 method button button bstate x y =
4322 begin match state.mode with
4323 | View -> viewmouse button bstate x y
4324 | Birdseye beye -> birdseyemouse button bstate x y beye
4325 | Textentry _ -> ()
4326 end;
4327 state.uioh
4329 method motion x y =
4330 begin match state.mode with
4331 | Textentry _ -> ()
4332 | View | Birdseye _ ->
4333 match state.mstate with
4334 | Mzoom _ | Mnone -> ()
4336 | Mpan (x0, y0) ->
4337 let dx = x - x0
4338 and dy = y0 - y in
4339 state.mstate <- Mpan (x, y);
4340 if conf.zoom > 1.0 then state.x <- state.x + dx;
4341 let y = clamp dy in
4342 gotoy_and_clear_text y
4344 | Msel (a, _) ->
4345 state.mstate <- Msel (a, (x, y));
4346 G.postRedisplay "motion select";
4348 | Mscrolly ->
4349 let y = min conf.winh (max 0 y) in
4350 scrolly y
4352 | Mscrollx ->
4353 let x = min conf.winw (max 0 x) in
4354 scrollx x
4356 | Mzoomrect (p0, _) ->
4357 state.mstate <- Mzoomrect (p0, (x, y));
4358 G.postRedisplay "motion zoomrect";
4359 end;
4360 state.uioh
4362 method pmotion x y =
4363 begin match state.mode with
4364 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4365 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4366 let rec loop = function
4367 | [] ->
4368 if hooverpageno != -1
4369 then (
4370 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4371 G.postRedisplay "pmotion birdseye no hoover";
4373 | l :: rest ->
4374 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4375 && x > margin && x < margin + l.pagew
4376 then (
4377 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4378 G.postRedisplay "pmotion birdseye hoover";
4380 else loop rest
4382 loop state.layout
4384 | Textentry _ -> ()
4386 | View ->
4387 match state.mstate with
4388 | Mnone ->
4389 begin match getunder x y with
4390 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
4391 | Ulinkuri uri ->
4392 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
4393 Glut.setCursor Glut.CURSOR_INFO
4394 | Ulinkgoto (page, _) ->
4395 if conf.underinfo
4396 then showtext 'p' ("age: " ^ string_of_int (page+1));
4397 Glut.setCursor Glut.CURSOR_INFO
4398 | Utext s ->
4399 if conf.underinfo then showtext 'f' ("ont: " ^ s);
4400 Glut.setCursor Glut.CURSOR_TEXT
4403 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
4405 end;
4406 state.uioh
4408 method memusedchanged = ()
4409 end;;
4411 module Config =
4412 struct
4413 open Parser
4415 let fontpath = ref "";;
4416 let wmclasshack = ref false;;
4418 let unent s =
4419 let l = String.length s in
4420 let b = Buffer.create l in
4421 unent b s 0 l;
4422 Buffer.contents b;
4425 let home =
4427 match platform with
4428 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
4429 | _ -> Sys.getenv "HOME"
4430 with exn ->
4431 prerr_endline
4432 ("Can not determine home directory location: " ^
4433 Printexc.to_string exn);
4437 let config_of c attrs =
4438 let apply c k v =
4440 match k with
4441 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
4442 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
4443 | "case-insensitive-search" -> { c with icase = bool_of_string v }
4444 | "preload" -> { c with preload = bool_of_string v }
4445 | "page-bias" -> { c with pagebias = int_of_string v }
4446 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
4447 | "auto-scroll-step" ->
4448 { c with autoscrollstep = max 0 (int_of_string v) }
4449 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
4450 | "crop-hack" -> { c with crophack = bool_of_string v }
4451 | "throttle" -> { c with showall = bool_of_string v }
4452 | "highlight-links" -> { c with hlinks = bool_of_string v }
4453 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
4454 | "vertical-margin" ->
4455 { c with interpagespace = max 0 (int_of_string v) }
4456 | "zoom" ->
4457 let zoom = float_of_string v /. 100. in
4458 let zoom = max zoom 0.0 in
4459 { c with zoom = zoom }
4460 | "presentation" -> { c with presentation = bool_of_string v }
4461 | "rotation-angle" -> { c with angle = int_of_string v }
4462 | "width" -> { c with winw = max 20 (int_of_string v) }
4463 | "height" -> { c with winh = max 20 (int_of_string v) }
4464 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
4465 | "proportional-display" -> { c with proportional = bool_of_string v }
4466 | "pixmap-cache-size" ->
4467 { c with memlimit = max 2 (int_of_string_with_suffix v) }
4468 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
4469 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
4470 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
4471 | "persistent-location" -> { c with jumpback = bool_of_string v }
4472 | "background-color" -> { c with bgcolor = color_of_string v }
4473 | "scrollbar-in-presentation" ->
4474 { c with scrollbarinpm = bool_of_string v }
4475 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
4476 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
4477 | "memlimit" ->
4478 { c with mumemlimit = max 1024 (int_of_string_with_suffix v) }
4479 | "checkers" -> { c with checkers = bool_of_string v }
4480 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
4481 | "trim-margins" -> { c with trimmargins = bool_of_string v }
4482 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
4483 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
4484 | "uri-launcher" -> { c with urilauncher = unent v }
4485 | "color-space" -> { c with colorspace = colorspace_of_string v }
4486 | "invert-colors" -> { c with invert = bool_of_string v }
4487 | _ -> c
4488 with exn ->
4489 prerr_endline ("Error processing attribute (`" ^
4490 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
4493 let rec fold c = function
4494 | [] -> c
4495 | (k, v) :: rest ->
4496 let c = apply c k v in
4497 fold c rest
4499 fold c attrs;
4502 let fromstring f pos n v d =
4503 try f v
4504 with exn ->
4505 dolog "Error processing attribute (%S=%S) at %d\n%s"
4506 n v pos (Printexc.to_string exn)
4511 let bookmark_of attrs =
4512 let rec fold title page rely = function
4513 | ("title", v) :: rest -> fold v page rely rest
4514 | ("page", v) :: rest -> fold title v rely rest
4515 | ("rely", v) :: rest -> fold title page v rest
4516 | _ :: rest -> fold title page rely rest
4517 | [] -> title, page, rely
4519 fold "invalid" "0" "0" attrs
4522 let doc_of attrs =
4523 let rec fold path page rely pan = function
4524 | ("path", v) :: rest -> fold v page rely pan rest
4525 | ("page", v) :: rest -> fold path v rely pan rest
4526 | ("rely", v) :: rest -> fold path page v pan rest
4527 | ("pan", v) :: rest -> fold path page rely v rest
4528 | _ :: rest -> fold path page rely pan rest
4529 | [] -> path, page, rely, pan
4531 fold "" "0" "0" "0" attrs
4534 let setconf dst src =
4535 dst.scrollbw <- src.scrollbw;
4536 dst.scrollh <- src.scrollh;
4537 dst.icase <- src.icase;
4538 dst.preload <- src.preload;
4539 dst.pagebias <- src.pagebias;
4540 dst.verbose <- src.verbose;
4541 dst.scrollstep <- src.scrollstep;
4542 dst.maxhfit <- src.maxhfit;
4543 dst.crophack <- src.crophack;
4544 dst.autoscrollstep <- src.autoscrollstep;
4545 dst.showall <- src.showall;
4546 dst.hlinks <- src.hlinks;
4547 dst.underinfo <- src.underinfo;
4548 dst.interpagespace <- src.interpagespace;
4549 dst.zoom <- src.zoom;
4550 dst.presentation <- src.presentation;
4551 dst.angle <- src.angle;
4552 dst.winw <- src.winw;
4553 dst.winh <- src.winh;
4554 dst.savebmarks <- src.savebmarks;
4555 dst.memlimit <- src.memlimit;
4556 dst.proportional <- src.proportional;
4557 dst.texcount <- src.texcount;
4558 dst.sliceheight <- src.sliceheight;
4559 dst.thumbw <- src.thumbw;
4560 dst.jumpback <- src.jumpback;
4561 dst.bgcolor <- src.bgcolor;
4562 dst.scrollbarinpm <- src.scrollbarinpm;
4563 dst.tilew <- src.tilew;
4564 dst.tileh <- src.tileh;
4565 dst.mumemlimit <- src.mumemlimit;
4566 dst.checkers <- src.checkers;
4567 dst.aalevel <- src.aalevel;
4568 dst.trimmargins <- src.trimmargins;
4569 dst.trimfuzz <- src.trimfuzz;
4570 dst.urilauncher <- src.urilauncher;
4571 dst.colorspace <- src.colorspace;
4572 dst.invert <- src.invert;
4575 let get s =
4576 let h = Hashtbl.create 10 in
4577 let dc = { defconf with angle = defconf.angle } in
4578 let rec toplevel v t spos _ =
4579 match t with
4580 | Vdata | Vcdata | Vend -> v
4581 | Vopen ("llppconfig", _, closed) ->
4582 if closed
4583 then v
4584 else { v with f = llppconfig }
4585 | Vopen _ ->
4586 error "unexpected subelement at top level" s spos
4587 | Vclose _ -> error "unexpected close at top level" s spos
4589 and llppconfig v t spos _ =
4590 match t with
4591 | Vdata | Vcdata -> v
4592 | Vend -> error "unexpected end of input in llppconfig" s spos
4593 | Vopen ("defaults", attrs, closed) ->
4594 let c = config_of dc attrs in
4595 setconf dc c;
4596 if closed
4597 then v
4598 else { v with f = skip "defaults" (fun () -> v) }
4600 | Vopen ("ui-font", attrs, closed) ->
4601 let rec getsize size = function
4602 | [] -> size
4603 | ("size", v) :: rest ->
4604 let size =
4605 fromstring int_of_string spos "size" v !uifontsize in
4606 getsize size rest
4607 | l -> getsize size l
4609 uifontsize := getsize !uifontsize attrs;
4610 if closed
4611 then v
4612 else { v with f = uifont (Buffer.create 10) }
4614 | Vopen ("doc", attrs, closed) ->
4615 let pathent, spage, srely, span = doc_of attrs in
4616 let path = unent pathent
4617 and pageno = fromstring int_of_string spos "page" spage 0
4618 and rely = fromstring float_of_string spos "rely" srely 0.0
4619 and pan = fromstring int_of_string spos "pan" span 0 in
4620 let c = config_of dc attrs in
4621 let anchor = (pageno, rely) in
4622 if closed
4623 then (Hashtbl.add h path (c, [], pan, anchor); v)
4624 else { v with f = doc path pan anchor c [] }
4626 | Vopen _ ->
4627 error "unexpected subelement in llppconfig" s spos
4629 | Vclose "llppconfig" -> { v with f = toplevel }
4630 | Vclose _ -> error "unexpected close in llppconfig" s spos
4632 and uifont b v t spos epos =
4633 match t with
4634 | Vdata | Vcdata ->
4635 Buffer.add_substring b s spos (epos - spos);
4637 | Vopen (_, _, _) ->
4638 error "unexpected subelement in ui-font" s spos
4639 | Vclose "ui-font" ->
4640 if String.length !fontpath = 0
4641 then fontpath := Buffer.contents b;
4642 { v with f = llppconfig }
4643 | Vclose _ -> error "unexpected close in ui-font" s spos
4644 | Vend -> error "unexpected end of input in ui-font" s spos
4646 and doc path pan anchor c bookmarks v t spos _ =
4647 match t with
4648 | Vdata | Vcdata -> v
4649 | Vend -> error "unexpected end of input in doc" s spos
4650 | Vopen ("bookmarks", _, closed) ->
4651 if closed
4652 then v
4653 else { v with f = pbookmarks path pan anchor c bookmarks }
4655 | Vopen (_, _, _) ->
4656 error "unexpected subelement in doc" s spos
4658 | Vclose "doc" ->
4659 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
4660 { v with f = llppconfig }
4662 | Vclose _ -> error "unexpected close in doc" s spos
4664 and pbookmarks path pan anchor c bookmarks v t spos _ =
4665 match t with
4666 | Vdata | Vcdata -> v
4667 | Vend -> error "unexpected end of input in bookmarks" s spos
4668 | Vopen ("item", attrs, closed) ->
4669 let titleent, spage, srely = bookmark_of attrs in
4670 let page = fromstring int_of_string spos "page" spage 0
4671 and rely = fromstring float_of_string spos "rely" srely 0.0 in
4672 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
4673 if closed
4674 then { v with f = pbookmarks path pan anchor c bookmarks }
4675 else
4676 let f () = v in
4677 { v with f = skip "item" f }
4679 | Vopen _ ->
4680 error "unexpected subelement in bookmarks" s spos
4682 | Vclose "bookmarks" ->
4683 { v with f = doc path pan anchor c bookmarks }
4685 | Vclose _ -> error "unexpected close in bookmarks" s spos
4687 and skip tag f v t spos _ =
4688 match t with
4689 | Vdata | Vcdata -> v
4690 | Vend ->
4691 error ("unexpected end of input in skipped " ^ tag) s spos
4692 | Vopen (tag', _, closed) ->
4693 if closed
4694 then v
4695 else
4696 let f' () = { v with f = skip tag f } in
4697 { v with f = skip tag' f' }
4698 | Vclose ctag ->
4699 if tag = ctag
4700 then f ()
4701 else error ("unexpected close in skipped " ^ tag) s spos
4704 parse { f = toplevel; accu = () } s;
4705 h, dc;
4708 let do_load f ic =
4710 let len = in_channel_length ic in
4711 let s = String.create len in
4712 really_input ic s 0 len;
4713 f s;
4714 with
4715 | Parse_error (msg, s, pos) ->
4716 let subs = subs s pos in
4717 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
4718 failwith ("parse error: " ^ s)
4720 | exn ->
4721 failwith ("config load error: " ^ Printexc.to_string exn)
4724 let defconfpath =
4725 let dir =
4727 let dir = Filename.concat home ".config" in
4728 if Sys.is_directory dir then dir else home
4729 with _ -> home
4731 Filename.concat dir "llpp.conf"
4734 let confpath = ref defconfpath;;
4736 let load1 f =
4737 if Sys.file_exists !confpath
4738 then
4739 match
4740 (try Some (open_in_bin !confpath)
4741 with exn ->
4742 prerr_endline
4743 ("Error opening configuation file `" ^ !confpath ^ "': " ^
4744 Printexc.to_string exn);
4745 None
4747 with
4748 | Some ic ->
4749 begin try
4750 f (do_load get ic)
4751 with exn ->
4752 prerr_endline
4753 ("Error loading configuation from `" ^ !confpath ^ "': " ^
4754 Printexc.to_string exn);
4755 end;
4756 close_in ic;
4758 | None -> ()
4759 else
4760 f (Hashtbl.create 0, defconf)
4763 let load () =
4764 let f (h, dc) =
4765 let pc, pb, px, pa =
4767 Hashtbl.find h (Filename.basename state.path)
4768 with Not_found -> dc, [], 0, (0, 0.0)
4770 setconf defconf dc;
4771 setconf conf pc;
4772 state.bookmarks <- pb;
4773 state.x <- px;
4774 state.scrollw <- conf.scrollbw;
4775 if conf.jumpback
4776 then state.anchor <- pa;
4777 cbput state.hists.nav pa;
4779 load1 f
4782 let add_attrs bb always dc c =
4783 let ob s a b =
4784 if always || a != b
4785 then Printf.bprintf bb "\n %s='%b'" s a
4786 and oi s a b =
4787 if always || a != b
4788 then Printf.bprintf bb "\n %s='%d'" s a
4789 and oI s a b =
4790 if always || a != b
4791 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
4792 and oz s a b =
4793 if always || a <> b
4794 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
4795 and oc s a b =
4796 if always || a <> b
4797 then
4798 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
4799 and oC s a b =
4800 if always || a <> b
4801 then
4802 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
4803 and oR s a b =
4804 if always || a <> b
4805 then
4806 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
4807 and os s a b =
4808 if always || a <> b
4809 then
4810 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
4812 let w, h =
4813 if always
4814 then dc.winw, dc.winh
4815 else
4816 match state.fullscreen with
4817 | Some wh -> wh
4818 | None -> c.winw, c.winh
4820 let zoom, presentation, interpagespace, showall=
4821 if always
4822 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
4823 else
4824 match state.mode with
4825 | Birdseye (bc, _, _, _, _) ->
4826 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
4827 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
4829 oi "width" w dc.winw;
4830 oi "height" h dc.winh;
4831 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
4832 oi "scroll-handle-height" c.scrollh dc.scrollh;
4833 ob "case-insensitive-search" c.icase dc.icase;
4834 ob "preload" c.preload dc.preload;
4835 oi "page-bias" c.pagebias dc.pagebias;
4836 oi "scroll-step" c.scrollstep dc.scrollstep;
4837 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
4838 ob "max-height-fit" c.maxhfit dc.maxhfit;
4839 ob "crop-hack" c.crophack dc.crophack;
4840 ob "throttle" showall dc.showall;
4841 ob "highlight-links" c.hlinks dc.hlinks;
4842 ob "under-cursor-info" c.underinfo dc.underinfo;
4843 oi "vertical-margin" interpagespace dc.interpagespace;
4844 oz "zoom" zoom dc.zoom;
4845 ob "presentation" presentation dc.presentation;
4846 oi "rotation-angle" c.angle dc.angle;
4847 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
4848 ob "proportional-display" c.proportional dc.proportional;
4849 oI "pixmap-cache-size" c.memlimit dc.memlimit;
4850 oi "tex-count" c.texcount dc.texcount;
4851 oi "slice-height" c.sliceheight dc.sliceheight;
4852 oi "thumbnail-width" c.thumbw dc.thumbw;
4853 ob "persistent-location" c.jumpback dc.jumpback;
4854 oc "background-color" c.bgcolor dc.bgcolor;
4855 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
4856 oi "tile-width" c.tilew dc.tilew;
4857 oi "tile-height" c.tileh dc.tileh;
4858 oI "mupdf-memlimit" c.mumemlimit dc.mumemlimit;
4859 ob "checkers" c.checkers dc.checkers;
4860 oi "aalevel" c.aalevel dc.aalevel;
4861 ob "trim-margins" c.trimmargins dc.trimmargins;
4862 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
4863 os "uri-launcher" c.urilauncher dc.urilauncher;
4864 oC "color-space" c.colorspace dc.colorspace;
4865 ob "invert-colors" c.invert dc.invert;
4866 if always
4867 then ob "wmclass-hack" !wmclasshack false;
4870 let save () =
4871 let uifontsize = !uifontsize in
4872 let bb = Buffer.create 32768 in
4873 let f (h, dc) =
4874 let dc = if conf.bedefault then conf else dc in
4875 Buffer.add_string bb "<llppconfig>\n";
4877 if String.length !fontpath > 0
4878 then
4879 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
4880 uifontsize
4881 !fontpath
4882 else (
4883 if uifontsize <> 14
4884 then
4885 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
4888 Buffer.add_string bb "<defaults ";
4889 add_attrs bb true dc dc;
4890 Buffer.add_string bb "/>\n";
4892 let adddoc path pan anchor c bookmarks =
4893 if bookmarks == [] && c = dc && anchor = emptyanchor
4894 then ()
4895 else (
4896 Printf.bprintf bb "<doc path='%s'"
4897 (enent path 0 (String.length path));
4899 if anchor <> emptyanchor
4900 then (
4901 let n, y = anchor in
4902 Printf.bprintf bb " page='%d'" n;
4903 if y > 1e-6
4904 then
4905 Printf.bprintf bb " rely='%f'" y
4909 if pan != 0
4910 then Printf.bprintf bb " pan='%d'" pan;
4912 add_attrs bb false dc c;
4914 begin match bookmarks with
4915 | [] -> Buffer.add_string bb "/>\n"
4916 | _ ->
4917 Buffer.add_string bb ">\n<bookmarks>\n";
4918 List.iter (fun (title, _level, (page, rely)) ->
4919 Printf.bprintf bb
4920 "<item title='%s' page='%d'"
4921 (enent title 0 (String.length title))
4922 page
4924 if rely > 1e-6
4925 then
4926 Printf.bprintf bb " rely='%f'" rely
4928 Buffer.add_string bb "/>\n";
4929 ) bookmarks;
4930 Buffer.add_string bb "</bookmarks>\n</doc>\n";
4931 end;
4935 let pan =
4936 match state.mode with
4937 | Birdseye (_, pan, _, _, _) -> pan
4938 | _ -> state.x
4940 let basename = Filename.basename state.path in
4941 adddoc basename pan (getanchor ())
4942 { conf with
4943 autoscrollstep =
4944 match state.autoscroll with
4945 | Some step -> step
4946 | None -> conf.autoscrollstep }
4947 (if conf.savebmarks then state.bookmarks else []);
4949 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
4950 if basename <> path
4951 then adddoc path x y c bookmarks
4952 ) h;
4953 Buffer.add_string bb "</llppconfig>";
4955 load1 f;
4956 if Buffer.length bb > 0
4957 then
4959 let tmp = !confpath ^ ".tmp" in
4960 let oc = open_out_bin tmp in
4961 Buffer.output_buffer oc bb;
4962 close_out oc;
4963 Unix.rename tmp !confpath;
4964 with exn ->
4965 prerr_endline
4966 ("error while saving configuration: " ^ Printexc.to_string exn)
4968 end;;
4970 let () =
4971 Arg.parse
4972 (Arg.align
4973 [("-p", Arg.String (fun s -> state.password <- s) ,
4974 "<password> Set password");
4976 ("-f", Arg.String (fun s -> Config.fontpath := s),
4977 "<path> Set path to the user interface font");
4979 ("-c", Arg.String (fun s -> Config.confpath := s),
4980 "<path> Set path to the configuration file");
4982 ("-v", Arg.Unit (fun () ->
4983 Printf.printf
4984 "%s\nconfiguration path: %s\n"
4985 Help.version
4986 Config.defconfpath
4988 exit 0), " Print version and exit");
4991 (fun s -> state.path <- s)
4992 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
4994 if String.length state.path = 0
4995 then (prerr_endline "file name missing"; exit 1);
4997 Config.load ();
4999 let _ = Glut.init Sys.argv in
5000 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5001 let () = Glut.initWindowSize conf.winw conf.winh in
5002 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5004 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5005 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5006 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5008 let csock, ssock =
5009 if not is_windows
5010 then
5011 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5012 else
5013 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5014 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5015 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5016 Unix.bind sock addr;
5017 Unix.listen sock 1;
5018 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5019 Unix.connect csock addr;
5020 let ssock, _ = Unix.accept sock in
5021 Unix.close sock;
5022 let opts sock =
5023 Unix.setsockopt sock Unix.TCP_NODELAY true;
5024 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5026 opts ssock;
5027 opts csock;
5028 ssock, csock
5031 let () = Glut.displayFunc display in
5032 let () = Glut.reshapeFunc reshape in
5033 let () = Glut.keyboardFunc keyboard in
5034 let () = Glut.specialFunc special in
5035 let () = Glut.idleFunc (Some idle) in
5036 let () = Glut.mouseFunc mouse in
5037 let () = Glut.motionFunc motion in
5038 let () = Glut.passiveMotionFunc pmotion in
5040 setcheckers conf.checkers;
5041 init ssock (
5042 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5043 conf.texcount, conf.sliceheight, conf.mumemlimit, conf.colorspace,
5044 !Config.wmclasshack, !Config.fontpath
5046 state.csock <- csock;
5047 state.ssock <- ssock;
5048 state.text <- "Opening " ^ state.path;
5049 setaalevel conf.aalevel;
5050 writeopen state.path state.password;
5051 state.uioh <- uioh;
5053 while true do
5055 Glut.mainLoop ();
5056 with
5057 | Glut.BadEnum "key in special_of_int" ->
5058 showtext '!' " LablGlut bug: special key not recognized";
5060 | Quit ->
5061 wcmd "quit" [];
5062 Config.save ();
5063 exit 0
5064 done;