Do info tracking properly
[llpp.git] / main.ml
blobe31a864a3c770f00bd9232b41016db14f28fcdc5
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 type infochange = | Memused | Docinfo | Pdim;;
275 class type uioh = object
276 method display : unit
277 method key : int -> uioh
278 method special : Glut.special_key_t -> uioh
279 method button :
280 Glut.button_t -> Glut.mouse_button_state_t -> int -> int -> uioh
281 method motion : int -> int -> uioh
282 method pmotion : int -> int -> uioh
283 method infochanged : infochange -> unit
284 end;;
286 type mode =
287 | Birdseye of (conf * leftx * pageno * pageno * anchor)
288 | Textentry of (textentry * onleave)
289 | View
290 and onleave = leavetextentrystatus -> unit
291 and leavetextentrystatus = | Cancel | Confirm
292 and helpitem = string * int * action
293 and action =
294 | Noaction
295 | Action of (uioh -> uioh)
298 let isbirdseye = function Birdseye _ -> true | _ -> false;;
299 let istextentry = function Textentry _ -> true | _ -> false;;
301 type currently =
302 | Idle
303 | Loading of (page * gen)
304 | Tiling of (
305 page * opaque * colorspace * angle * gen * col * row * width * height
307 | Outlining of outline list
310 let nouioh : uioh = object (self)
311 method display = ()
312 method key _ = self
313 method special _ = self
314 method button _ _ _ _ = self
315 method motion _ _ = self
316 method pmotion _ _ = self
317 method infochanged _ = ()
318 end;;
320 type state =
321 { mutable csock : Unix.file_descr
322 ; mutable ssock : Unix.file_descr
323 ; mutable w : int
324 ; mutable x : int
325 ; mutable y : int
326 ; mutable scrollw : int
327 ; mutable hscrollh : int
328 ; mutable anchor : anchor
329 ; mutable maxy : int
330 ; mutable layout : page list
331 ; pagemap : (pagemapkey, opaque) Hashtbl.t
332 ; tilemap : (tilemapkey, tile) Hashtbl.t
333 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
334 ; mutable pdims : (pageno * width * height * leftx) list
335 ; mutable pagecount : int
336 ; mutable currently : currently
337 ; mutable mstate : mstate
338 ; mutable searchpattern : string
339 ; mutable rects : (pageno * recttype * rect) list
340 ; mutable rects1 : (pageno * recttype * rect) list
341 ; mutable text : string
342 ; mutable fullscreen : (width * height) option
343 ; mutable mode : mode
344 ; mutable uioh : uioh
345 ; mutable outlines : outline array
346 ; mutable bookmarks : outline list
347 ; mutable path : string
348 ; mutable password : string
349 ; mutable invalidated : int
350 ; mutable colorscale : float
351 ; mutable memused : memsize
352 ; mutable gen : gen
353 ; mutable throttle : (page list * int) option
354 ; mutable autoscroll : int option
355 ; mutable help : helpitem array
356 ; mutable docinfo : (int * string) list
357 ; mutable deadline : float
358 ; mutable texid : GlTex.texture_id option
359 ; hists : hists
360 ; mutable prevzoom : float
361 ; mutable progress : float
363 and hists =
364 { pat : string circbuf
365 ; pag : string circbuf
366 ; nav : anchor circbuf
370 let defconf =
371 { scrollbw = 7
372 ; scrollh = 12
373 ; icase = true
374 ; preload = true
375 ; pagebias = 0
376 ; verbose = false
377 ; debug = false
378 ; scrollstep = 24
379 ; maxhfit = true
380 ; crophack = false
381 ; autoscrollstep = 2
382 ; showall = false
383 ; hlinks = false
384 ; underinfo = false
385 ; interpagespace = 2
386 ; zoom = 1.0
387 ; presentation = false
388 ; angle = 0
389 ; winw = 900
390 ; winh = 900
391 ; savebmarks = true
392 ; proportional = true
393 ; trimmargins = false
394 ; trimfuzz = (0,0,0,0)
395 ; memlimit = 32 lsl 20
396 ; texcount = 256
397 ; sliceheight = 24
398 ; thumbw = 76
399 ; jumpback = true
400 ; bgcolor = (0.5, 0.5, 0.5)
401 ; bedefault = false
402 ; scrollbarinpm = true
403 ; tilew = 2048
404 ; tileh = 2048
405 ; mumemlimit = 128 lsl 20
406 ; checkers = true
407 ; aalevel = 8
408 ; urilauncher =
409 (match platform with
410 | Plinux | Pfreebsd | Pdragonflybsd | Popenbsd | Psun -> "xdg-open \"%s\""
411 | Posx -> "open \"%s\""
412 | Pwindows | Pcygwin | Pmingw -> "iexplore \"%s\""
413 | _ -> "")
414 ; colorspace = Rgb
415 ; invert = false
419 let conf = { defconf with angle = defconf.angle };;
421 let uifontsize = ref 14;;
423 let gotouri uri =
424 if String.length conf.urilauncher = 0
425 then print_endline uri
426 else
427 let re = Str.regexp "%s" in
428 let command = Str.global_replace re uri conf.urilauncher in
429 let optic =
430 try Some (Unix.open_process_in command)
431 with exn ->
432 Printf.eprintf
433 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
434 flush stderr;
435 None
437 match optic with
438 | Some ic -> close_in ic
439 | None -> ()
442 let makehelp () =
443 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
444 Array.of_list (
445 let r = Str.regexp "\\(http://[^ ]+\\)" in
446 List.map (fun s ->
447 if (try Str.search_forward r s 0 with Not_found -> -1) >= 0
448 then
449 let uri = Str.matched_string s in
450 (s, 0, Action (fun u -> gotouri uri; u))
451 else s, 0, Noaction) strings
455 let state =
456 { csock = Unix.stdin
457 ; ssock = Unix.stdin
458 ; x = 0
459 ; y = 0
460 ; w = 0
461 ; scrollw = 0
462 ; hscrollh = 0
463 ; anchor = emptyanchor
464 ; layout = []
465 ; maxy = max_int
466 ; tilelru = Queue.create ()
467 ; pagemap = Hashtbl.create 10
468 ; tilemap = Hashtbl.create 10
469 ; pdims = []
470 ; pagecount = 0
471 ; currently = Idle
472 ; mstate = Mnone
473 ; rects = []
474 ; rects1 = []
475 ; text = ""
476 ; mode = View
477 ; fullscreen = None
478 ; searchpattern = ""
479 ; outlines = [||]
480 ; bookmarks = []
481 ; path = ""
482 ; password = ""
483 ; invalidated = 0
484 ; hists =
485 { nav = cbnew 10 (0, 0.0)
486 ; pat = cbnew 1 ""
487 ; pag = cbnew 1 ""
489 ; colorscale = 1.0
490 ; memused = 0
491 ; gen = 0
492 ; throttle = None
493 ; autoscroll = None
494 ; help = makehelp ()
495 ; docinfo = []
496 ; deadline = nan
497 ; texid = None
498 ; prevzoom = 1.0
499 ; progress = -1.0
500 ; uioh = nouioh
504 let vlog fmt =
505 if conf.verbose
506 then
507 Printf.kprintf prerr_endline fmt
508 else
509 Printf.kprintf ignore fmt
512 module G =
513 struct
514 let postRedisplay who =
515 if conf.verbose
516 then prerr_endline ("redisplay for " ^ who);
517 Glut.postRedisplay ();
519 end;;
521 let addchar s c =
522 let b = Buffer.create (String.length s + 1) in
523 Buffer.add_string b s;
524 Buffer.add_char b c;
525 Buffer.contents b;
528 let colorspace_of_string s =
529 match String.lowercase s with
530 | "rgb" -> Rgb
531 | "bgr" -> Bgr
532 | "gray" -> Gray
533 | _ -> failwith "invalid colorspace"
536 let int_of_colorspace = function
537 | Rgb -> 0
538 | Bgr -> 1
539 | Gray -> 2
542 let colorspace_of_int = function
543 | 0 -> Rgb
544 | 1 -> Bgr
545 | 2 -> Gray
546 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
549 let colorspace_to_string = function
550 | Rgb -> "rgb"
551 | Bgr -> "bgr"
552 | Gray -> "gray"
555 let intentry_with_suffix text key =
556 let c = Char.unsafe_chr key in
557 match Char.lowercase c with
558 | '0' .. '9' ->
559 let text = addchar text c in
560 TEcont text
562 | 'k' | 'm' | 'g' ->
563 let text = addchar text c in
564 TEcont text
566 | _ ->
567 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
568 TEcont text
571 let writecmd fd s =
572 let len = String.length s in
573 let n = 4 + len in
574 let b = Buffer.create n in
575 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
576 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
577 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
578 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
579 Buffer.add_string b s;
580 let s' = Buffer.contents b in
581 let n' = Unix.write fd s' 0 n in
582 if n' != n then failwith "write failed";
585 let readcmd fd =
586 let s = "xxxx" in
587 let n = Unix.read fd s 0 4 in
588 if n != 4 then failwith "incomplete read(len)";
589 let len = 0
590 lor (Char.code s.[0] lsl 24)
591 lor (Char.code s.[1] lsl 16)
592 lor (Char.code s.[2] lsl 8)
593 lor (Char.code s.[3] lsl 0)
595 let s = String.create len in
596 let n = Unix.read fd s 0 len in
597 if n != len then failwith "incomplete read(data)";
601 let makecmd s l =
602 let b = Buffer.create 10 in
603 Buffer.add_string b s;
604 let rec combine = function
605 | [] -> b
606 | x :: xs ->
607 Buffer.add_char b ' ';
608 let s =
609 match x with
610 | `b b -> if b then "1" else "0"
611 | `s s -> s
612 | `i i -> string_of_int i
613 | `f f -> string_of_float f
614 | `I f -> string_of_int (truncate f)
616 Buffer.add_string b s;
617 combine xs;
619 combine l;
622 let wcmd s l =
623 let cmd = Buffer.contents (makecmd s l) in
624 writecmd state.csock cmd;
627 let calcips h =
628 if conf.presentation
629 then
630 let d = conf.winh - h in
631 max 0 ((d + 1) / 2)
632 else
633 conf.interpagespace
636 let calcheight () =
637 let rec f pn ph pi fh l =
638 match l with
639 | (n, _, h, _) :: rest ->
640 let ips = calcips h in
641 let fh =
642 if conf.presentation
643 then fh+ips
644 else (
645 if isbirdseye state.mode && pn = 0
646 then fh + ips
647 else fh
650 let fh = fh + ((n - pn) * (ph + pi)) in
651 f n h ips fh rest;
653 | [] ->
654 let inc =
655 if conf.presentation || (isbirdseye state.mode && pn = 0)
656 then 0
657 else -pi
659 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
660 max 0 fh
662 let fh = f 0 0 0 0 state.pdims in
666 let getpageyh pageno =
667 let rec f pn ph pi y l =
668 match l with
669 | (n, _, h, _) :: rest ->
670 let ips = calcips h in
671 if n >= pageno
672 then
673 let h = if n = pageno then h else ph in
674 if conf.presentation && n = pageno
675 then
676 y + (pageno - pn) * (ph + pi) + pi, h
677 else
678 y + (pageno - pn) * (ph + pi), h
679 else
680 let y = y + (if conf.presentation then pi else 0) in
681 let y = y + (n - pn) * (ph + pi) in
682 f n h ips y rest
684 | [] ->
685 y + (pageno - pn) * (ph + pi), ph
687 f 0 0 0 0 state.pdims
690 let getpagedim pageno =
691 let rec f ppdim l =
692 match l with
693 | (n, _, _, _) as pdim :: rest ->
694 if n >= pageno
695 then (if n = pageno then pdim else ppdim)
696 else f pdim rest
698 | [] -> ppdim
700 f (-1, -1, -1, -1) state.pdims
703 let getpageh pageno =
704 let _, _, h, _ = getpagedim pageno in
708 let getpagew pageno =
709 let _, w, _, _ = getpagedim pageno in
713 let getpagey pageno = fst (getpageyh pageno);;
715 let layout y sh =
716 let sh = sh - state.hscrollh in
717 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
718 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
719 match pdims with
720 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
721 let ips = calcips h in
722 let yinc =
723 if conf.presentation || (isbirdseye state.mode && pageno = 0)
724 then ips
725 else 0
727 (w, h, ips, xoff), rest, pdimno + 1, yinc
728 | _ ->
729 prev, pdims, pdimno, 0
731 let dy = dy + yinc in
732 let py = py + yinc in
733 if pageno = state.pagecount || dy >= sh
734 then
735 accu
736 else
737 let vy = y + dy in
738 if py + h <= vy - yinc
739 then
740 let py = py + h + ips in
741 let dy = max 0 (py - y) in
742 f ~pageno:(pageno+1)
743 ~pdimno
744 ~prev:curr
747 ~pdims:rest
748 ~accu
749 else
750 let pagey = vy - py in
751 let pagevh = h - pagey in
752 let pagevh = min (sh - dy) pagevh in
753 let off = if yinc > 0 then py - vy else 0 in
754 let py = py + h + ips in
755 let pagex, dx =
756 let xoff = xoff +
757 if state.w < conf.winw - state.scrollw
758 then (conf.winw - state.scrollw - state.w) / 2
759 else 0
761 let dispx = xoff + state.x in
762 if dispx < 0
763 then (-dispx, 0)
764 else (0, dispx)
766 let pagevw =
767 let lw = w - pagex in
768 min lw (conf.winw - state.scrollw)
770 let e =
771 { pageno = pageno
772 ; pagedimno = pdimno
773 ; pagew = w
774 ; pageh = h
775 ; pagex = pagex
776 ; pagey = pagey + off
777 ; pagevw = pagevw
778 ; pagevh = pagevh - off
779 ; pagedispx = dx
780 ; pagedispy = dy + off
783 let accu = e :: accu in
784 f ~pageno:(pageno+1)
785 ~pdimno
786 ~prev:curr
788 ~dy:(dy+pagevh+ips)
789 ~pdims:rest
790 ~accu
792 if state.invalidated = 0
793 then (
794 let accu =
796 ~pageno:0
797 ~pdimno:~-1
798 ~prev:(0,0,0,0)
799 ~py:0
800 ~dy:0
801 ~pdims:state.pdims
802 ~accu:[]
804 List.rev accu
806 else
810 let clamp incr =
811 let y = state.y + incr in
812 let y = max 0 y in
813 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
817 let getopaque pageno =
818 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
819 with Not_found -> None
822 let putopaque pageno opaque =
823 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
826 let itertiles l f =
827 let tilex = l.pagex mod conf.tilew in
828 let tiley = l.pagey mod conf.tileh in
830 let col = l.pagex / conf.tilew in
831 let row = l.pagey / conf.tileh in
833 let vw =
834 let a = l.pagew - l.pagex in
835 let b = conf.winw - state.scrollw in
836 min a b
837 and vh = l.pagevh in
839 let rec rowloop row y0 dispy h =
840 if h = 0
841 then ()
842 else (
843 let dh = conf.tileh - y0 in
844 let dh = min h dh in
845 let rec colloop col x0 dispx w =
846 if w = 0
847 then ()
848 else (
849 let dw = conf.tilew - x0 in
850 let dw = min w dw in
852 f col row dispx dispy x0 y0 dw dh;
853 colloop (col+1) 0 (dispx+dw) (w-dw)
856 colloop col tilex l.pagedispx vw;
857 rowloop (row+1) 0 (dispy+dh) (h-dh)
860 if vw > 0 && vh > 0
861 then rowloop row tiley l.pagedispy vh;
864 let gettileopaque l col row =
865 let key =
866 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
868 try Some (Hashtbl.find state.tilemap key)
869 with Not_found -> None
872 let puttileopaque l col row gen colorspace angle opaque size elapsed =
873 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
874 Hashtbl.add state.tilemap key (opaque, size, elapsed)
877 let drawtiles l color =
878 GlDraw.color color;
879 let f col row x y tilex tiley w h =
880 match gettileopaque l col row with
881 | Some (opaque, _, t) ->
882 let params = x, y, w, h, tilex, tiley in
883 if conf.invert
884 then (
885 Gl.enable `blend;
886 GlFunc.blend_func `zero `one_minus_src_color;
888 drawtile params opaque;
889 if conf.invert
890 then Gl.disable `blend;
891 if conf.debug
892 then (
893 let s = Printf.sprintf
894 "%d[%d,%d] %f sec"
895 l.pageno col row t
897 let w = measurestr !uifontsize s in
898 GlMisc.push_attrib [`current];
899 GlDraw.color (0.0, 0.0, 0.0);
900 GlDraw.rect
901 (float (x-2), float (y-2))
902 (float (x+2) +. w, float (y + !uifontsize + 2));
903 GlDraw.color (1.0, 1.0, 1.0);
904 drawstring !uifontsize x (y + !uifontsize - 1) s;
905 GlMisc.pop_attrib ();
908 | _ ->
909 let w =
910 let lw = conf.winw - state.scrollw - x in
911 min lw w
912 and h =
913 let lh = conf.winh - y in
914 min lh h
916 Gl.enable `texture_2d;
917 begin match state.texid with
918 | Some id ->
919 GlTex.bind_texture `texture_2d id;
920 let x0 = float x
921 and y0 = float y
922 and x1 = float (x+w)
923 and y1 = float (y+h) in
925 let tw = float w /. 64.0
926 and th = float h /. 64.0 in
927 let tx0 = float tilex /. 64.0
928 and ty0 = float tiley /. 64.0 in
929 let tx1 = tx0 +. tw
930 and ty1 = ty0 +. th in
931 GlDraw.begins `quads;
932 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
933 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
934 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
935 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
936 GlDraw.ends ();
938 Gl.disable `texture_2d;
939 | None ->
940 GlDraw.color (1.0, 1.0, 1.0);
941 GlDraw.rect
942 (float x, float y)
943 (float (x+w), float (y+h));
944 end;
945 if w > 128 && h > !uifontsize + 10
946 then (
947 GlDraw.color (0.0, 0.0, 0.0);
948 let c, r =
949 if conf.verbose
950 then (col*conf.tilew, row*conf.tileh)
951 else col, row
953 drawstring2 !uifontsize x y "Loading %d [%d,%d]" l.pageno c r;
955 GlDraw.color color;
957 itertiles l f
960 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
962 let tilevisible1 l x y =
963 let ax0 = l.pagex
964 and ax1 = l.pagex + l.pagevw
965 and ay0 = l.pagey
966 and ay1 = l.pagey + l.pagevh in
968 let bx0 = x
969 and by0 = y in
970 let bx1 = min (bx0 + conf.tilew) l.pagew
971 and by1 = min (by0 + conf.tileh) l.pageh in
973 let rx0 = max ax0 bx0
974 and ry0 = max ay0 by0
975 and rx1 = min ax1 bx1
976 and ry1 = min ay1 by1 in
978 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
979 nonemptyintersection
982 let tilevisible layout n x y =
983 let rec findpageinlayout = function
984 | l :: _ when l.pageno = n -> tilevisible1 l x y
985 | _ :: rest -> findpageinlayout rest
986 | [] -> false
988 findpageinlayout layout
991 let tileready l x y =
992 tilevisible1 l x y &&
993 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
996 let tilepage n p layout =
997 let rec loop = function
998 | l :: rest ->
999 if l.pageno = n
1000 then
1001 let f col row _ _ _ _ _ _ =
1002 if state.currently = Idle
1003 then
1004 match gettileopaque l col row with
1005 | Some _ -> ()
1006 | None ->
1007 let x = col*conf.tilew
1008 and y = row*conf.tileh in
1009 let w =
1010 let w = l.pagew - x in
1011 min w conf.tilew
1013 let h =
1014 let h = l.pageh - y in
1015 min h conf.tileh
1017 wcmd "tile"
1018 [`s p
1019 ;`i x
1020 ;`i y
1021 ;`i w
1022 ;`i h
1024 state.currently <-
1025 Tiling (
1026 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1027 conf.tilew, conf.tileh
1030 itertiles l f;
1031 else
1032 loop rest
1034 | [] -> ()
1036 if state.invalidated = 0 then loop layout;
1039 let preloadlayout visiblepages =
1040 let presentation = conf.presentation in
1041 let interpagespace = conf.interpagespace in
1042 let maxy = state.maxy in
1043 conf.presentation <- false;
1044 conf.interpagespace <- 0;
1045 state.maxy <- calcheight ();
1046 let y =
1047 match visiblepages with
1048 | [] -> 0
1049 | l :: _ -> getpagey l.pageno + l.pagey
1051 let y = if y < conf.winh then 0 else y - conf.winh in
1052 let h = state.y - y + conf.winh*3 in
1053 let pages = layout y h in
1054 conf.presentation <- presentation;
1055 conf.interpagespace <- interpagespace;
1056 state.maxy <- maxy;
1057 pages
1060 let load pages =
1061 let rec loop pages =
1062 if state.currently != Idle
1063 then ()
1064 else
1065 match pages with
1066 | l :: rest ->
1067 begin match getopaque l.pageno with
1068 | None ->
1069 wcmd "page" [`i l.pageno; `i l.pagedimno];
1070 state.currently <- Loading (l, state.gen);
1071 | Some opaque ->
1072 tilepage l.pageno opaque pages;
1073 loop rest
1074 end;
1075 | _ -> ()
1077 if state.invalidated = 0 then loop pages
1080 let preload pages =
1081 load pages;
1082 if conf.preload && state.currently = Idle
1083 then load (preloadlayout pages);
1086 let layoutready layout =
1087 let rec fold all ls =
1088 all && match ls with
1089 | l :: rest ->
1090 let seen = ref false in
1091 let allvisible = ref true in
1092 let foo col row _ _ _ _ _ _ =
1093 seen := true;
1094 allvisible := !allvisible &&
1095 begin match gettileopaque l col row with
1096 | Some _ -> true
1097 | None -> false
1100 itertiles l foo;
1101 fold (!seen && !allvisible) rest
1102 | [] -> true
1104 let alltilesvisible = fold true layout in
1105 alltilesvisible;
1108 let gotoy y =
1109 let y = bound y 0 state.maxy in
1110 let y, layout, proceed =
1111 if conf.showall
1112 then
1113 match state.throttle with
1114 | None ->
1115 let layout = layout y conf.winh in
1116 let ready = layoutready layout in
1117 if not ready
1118 then (
1119 load layout;
1120 state.throttle <- Some (layout, y);
1122 else G.postRedisplay "gotoy showall (None)";
1123 y, layout, ready
1124 | Some _ -> -1, [], false
1125 else
1126 let layout = layout y conf.winh in
1127 if true || layoutready layout
1128 then G.postRedisplay "gotoy ready";
1129 y, layout, true
1131 if proceed
1132 then (
1133 state.y <- y;
1134 state.layout <- layout;
1135 begin match state.mode with
1136 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1137 if not (pagevisible layout pageno)
1138 then (
1139 match state.layout with
1140 | [] -> ()
1141 | l :: _ ->
1142 state.mode <- Birdseye (
1143 conf, leftx, l.pageno, hooverpageno, anchor
1146 | _ -> ()
1147 end;
1148 preload layout;
1152 let conttiling pageno opaque =
1153 tilepage pageno opaque
1154 (if conf.preload then preloadlayout state.layout else state.layout)
1157 let gotoy_and_clear_text y =
1158 gotoy y;
1159 if not conf.verbose then state.text <- "";
1162 let getanchor () =
1163 match state.layout with
1164 | [] -> emptyanchor
1165 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1168 let getanchory (n, top) =
1169 let y, h = getpageyh n in
1170 y + (truncate (top *. float h));
1173 let gotoanchor anchor =
1174 gotoy (getanchory anchor);
1177 let addnav () =
1178 cbput state.hists.nav (getanchor ());
1181 let getnav dir =
1182 let anchor = cbgetc state.hists.nav dir in
1183 getanchory anchor;
1186 let gotopage n top =
1187 let y, h = getpageyh n in
1188 gotoy_and_clear_text (y + (truncate (top *. float h)));
1191 let gotopage1 n top =
1192 let y = getpagey n in
1193 gotoy_and_clear_text (y + top);
1196 let invalidate () =
1197 state.layout <- [];
1198 state.pdims <- [];
1199 state.rects <- [];
1200 state.rects1 <- [];
1201 state.invalidated <- state.invalidated + 1;
1204 let writeopen path password =
1205 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1208 let opendoc path password =
1209 invalidate ();
1210 state.path <- path;
1211 state.password <- password;
1212 state.gen <- state.gen + 1;
1213 state.docinfo <- [];
1215 setaalevel conf.aalevel;
1216 writeopen path password;
1217 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1218 wcmd "geometry" [`i state.w; `i conf.winh];
1221 let scalecolor c =
1222 let c = c *. state.colorscale in
1223 (c, c, c);
1226 let scalecolor2 (r, g, b) =
1227 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
1230 let represent () =
1231 state.maxy <- calcheight ();
1232 state.hscrollh <-
1233 if state.w <= conf.winw - state.scrollw
1234 then 0
1235 else state.scrollw
1237 match state.mode with
1238 | Birdseye (_, _, pageno, _, _) ->
1239 let y, h = getpageyh pageno in
1240 let top = (conf.winh - h) / 2 in
1241 gotoy (max 0 (y - top))
1242 | _ -> gotoanchor state.anchor
1245 let reshape =
1246 let firsttime = ref true in
1247 fun ~w ~h ->
1248 GlDraw.viewport 0 0 w h;
1249 if state.invalidated = 0 && not !firsttime
1250 then state.anchor <- getanchor ();
1252 firsttime := false;
1253 conf.winw <- w;
1254 let w = truncate (float w *. conf.zoom) - state.scrollw in
1255 let w = max w 2 in
1256 state.w <- w;
1257 conf.winh <- h;
1258 GlMat.mode `modelview;
1259 GlMat.load_identity ();
1261 GlMat.mode `projection;
1262 GlMat.load_identity ();
1263 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1264 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1265 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1267 invalidate ();
1268 wcmd "geometry" [`i w; `i h];
1271 let enttext () =
1272 let len = String.length state.text in
1273 let drawstring s =
1274 let hscrollh =
1275 match state.mode with
1276 | View -> state.hscrollh
1277 | _ -> 0
1279 let rect x w =
1280 GlDraw.rect
1281 (x, float (conf.winh - (!uifontsize + 4) - hscrollh))
1282 (x+.w, float (conf.winh - hscrollh))
1285 let w = float (conf.winw - state.scrollw - 1) in
1286 if state.progress >= 0.0 && state.progress < 1.0
1287 then (
1288 GlDraw.color (0.3, 0.3, 0.3);
1289 let w1 = w *. state.progress in
1290 rect 0.0 w1;
1291 GlDraw.color (0.0, 0.0, 0.0);
1292 rect w1 (w-.w1)
1294 else (
1295 GlDraw.color (0.0, 0.0, 0.0);
1296 rect 0.0 w;
1299 GlDraw.color (1.0, 1.0, 1.0);
1300 drawstring !uifontsize
1301 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1303 match state.mode with
1304 | Textentry ((prefix, text, _, _, _), _) ->
1305 let s =
1306 if len > 0
1307 then
1308 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1309 else
1310 Printf.sprintf "%s%s_" prefix text
1312 drawstring s
1314 | _ ->
1315 if len > 0 then drawstring state.text
1318 let showtext c s =
1319 state.text <- Printf.sprintf "%c%s" c s;
1320 G.postRedisplay "showtext";
1323 let gctiles () =
1324 let len = Queue.length state.tilelru in
1325 let rec loop qpos =
1326 if state.memused <= conf.memlimit
1327 then ()
1328 else (
1329 if qpos < len
1330 then
1331 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1332 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1334 gen = state.gen
1335 && colorspace = conf.colorspace
1336 && angle = conf.angle
1337 && pagew = getpagew n
1338 && pageh = getpageh n
1339 && (
1340 let layout =
1341 if conf.preload
1342 then preloadlayout state.layout
1343 else state.layout
1345 let x = col*conf.tilew
1346 and y = row*conf.tileh in
1347 tilevisible layout n x y
1349 then Queue.push lruitem state.tilelru
1350 else (
1351 wcmd "freetile" [`s p];
1352 state.memused <- state.memused - s;
1353 state.uioh#infochanged Memused;
1354 Hashtbl.remove state.tilemap k;
1356 loop (qpos+1)
1359 loop 0
1362 let flushtiles () =
1363 Queue.iter (fun (k, p, s) ->
1364 wcmd "freetile" [`s p];
1365 state.memused <- state.memused - s;
1366 state.uioh#infochanged Memused;
1367 Hashtbl.remove state.tilemap k;
1368 ) state.tilelru;
1369 Queue.clear state.tilelru;
1370 load state.layout;
1373 let logcurrently = function
1374 | Idle -> dolog "Idle"
1375 | Loading (l, gen) ->
1376 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1377 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1378 dolog
1379 "Tiling %d[%d,%d] page=%s cs=%s angle"
1380 l.pageno col row pageopaque
1381 (colorspace_to_string colorspace)
1383 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1384 angle gen conf.angle state.gen
1385 tilew tileh
1386 conf.tilew conf.tileh
1388 | Outlining _ ->
1389 dolog "outlining"
1392 let act cmds =
1393 (* dolog "%S" cmds; *)
1394 let op, args =
1395 let spacepos =
1396 try String.index cmds ' '
1397 with Not_found -> -1
1399 if spacepos = -1
1400 then cmds, ""
1401 else
1402 let l = String.length cmds in
1403 let op = String.sub cmds 0 spacepos in
1404 op, begin
1405 if l - spacepos < 2 then ""
1406 else String.sub cmds (spacepos+1) (l-spacepos-1)
1409 match op with
1410 | "clear" ->
1411 state.uioh#infochanged Pdim;
1412 state.pdims <- [];
1414 | "clearrects" ->
1415 state.rects <- state.rects1;
1416 G.postRedisplay "clearrects";
1418 | "continue" ->
1419 let n = Scanf.sscanf args "%u" (fun n -> n) in
1420 state.pagecount <- n;
1421 state.invalidated <- state.invalidated - 1;
1422 begin match state.currently with
1423 | Outlining l ->
1424 state.currently <- Idle;
1425 state.outlines <- Array.of_list (List.rev l)
1426 | _ -> ()
1427 end;
1428 if state.invalidated = 0
1429 then represent ();
1430 if not conf.showall
1431 then G.postRedisplay "continue";
1433 | "title" ->
1434 Glut.setWindowTitle args
1436 | "msg" ->
1437 showtext ' ' args
1439 | "vmsg" ->
1440 if conf.verbose
1441 then showtext ' ' args
1443 | "progress" ->
1444 let progress, text = Scanf.sscanf args "%f %n"
1445 (fun f pos ->
1446 f, String.sub args pos (String.length args - pos)
1449 state.text <- text;
1450 state.progress <- progress;
1451 G.postRedisplay "progress"
1453 | "firstmatch" ->
1454 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1455 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1456 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1457 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1459 let y = (getpagey pageno) + truncate y0 in
1460 addnav ();
1461 gotoy y;
1462 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1464 | "match" ->
1465 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1466 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1467 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1468 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1470 state.rects1 <-
1471 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1473 | "page" ->
1474 let pageopaque, t = Scanf.sscanf args "%s %f" (fun p t -> p, t) in
1475 begin match state.currently with
1476 | Loading (l, gen) ->
1477 vlog "page %d took %f sec" l.pageno t;
1478 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1479 begin match state.throttle with
1480 | None ->
1481 let preloadedpages =
1482 if conf.preload
1483 then preloadlayout state.layout
1484 else state.layout
1486 let evict () =
1487 let module IntSet =
1488 Set.Make (struct type t = int let compare = (-) end) in
1489 let set =
1490 List.fold_left (fun s l -> IntSet.add l.pageno s)
1491 IntSet.empty preloadedpages
1493 let evictedpages =
1494 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1495 if not (IntSet.mem pageno set)
1496 then (
1497 wcmd "freepage" [`s opaque];
1498 key :: accu
1500 else accu
1501 ) state.pagemap []
1503 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1505 evict ();
1506 state.currently <- Idle;
1507 if gen = state.gen
1508 then (
1509 tilepage l.pageno pageopaque state.layout;
1510 load state.layout;
1511 load preloadedpages;
1512 if pagevisible state.layout l.pageno
1513 && layoutready state.layout
1514 then G.postRedisplay "page";
1517 | Some (layout, _) ->
1518 state.currently <- Idle;
1519 tilepage l.pageno pageopaque layout;
1520 load state.layout
1521 end;
1523 | _ ->
1524 dolog "Inconsistent loading state";
1525 logcurrently state.currently;
1526 raise Quit;
1529 | "tile" ->
1530 let (x, y, opaque, size, t) =
1531 Scanf.sscanf args "%u %u %s %u %f"
1532 (fun x y p size t -> (x, y, p, size, t))
1534 begin match state.currently with
1535 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1536 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1538 if tilew != conf.tilew || tileh != conf.tileh
1539 then (
1540 wcmd "freetile" [`s opaque];
1541 state.currently <- Idle;
1542 load state.layout;
1544 else (
1545 puttileopaque l col row gen cs angle opaque size t;
1546 state.memused <- state.memused + size;
1547 state.uioh#infochanged Memused;
1548 gctiles ();
1549 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1550 opaque, size) state.tilelru;
1552 state.currently <- Idle;
1553 if gen = state.gen
1554 && conf.colorspace = cs
1555 && conf.angle = angle
1556 && tilevisible state.layout l.pageno x y
1557 then conttiling l.pageno pageopaque;
1559 begin match state.throttle with
1560 | None ->
1561 preload state.layout;
1562 if gen = state.gen
1563 && conf.colorspace = cs
1564 && conf.angle = angle
1565 && tilevisible state.layout l.pageno x y
1566 then G.postRedisplay "tile nothrottle";
1568 | Some (layout, y) ->
1569 let ready = layoutready layout in
1570 if ready
1571 then (
1572 state.y <- y;
1573 state.layout <- layout;
1574 state.throttle <- None;
1575 G.postRedisplay "throttle";
1577 else load layout;
1578 end;
1581 | _ ->
1582 dolog "Inconsistent tiling state";
1583 logcurrently state.currently;
1584 raise Quit;
1587 | "pdim" ->
1588 let pdim =
1589 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1591 state.uioh#infochanged Pdim;
1592 state.pdims <- pdim :: state.pdims
1594 | "o" ->
1595 let (l, n, t, h, pos) =
1596 Scanf.sscanf args "%u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
1598 let s = String.sub args pos (String.length args - pos) in
1599 let outline = (s, l, (n, float t /. float h)) in
1600 begin match state.currently with
1601 | Outlining outlines ->
1602 state.currently <- Outlining (outline :: outlines)
1603 | Idle ->
1604 state.currently <- Outlining [outline]
1605 | currently ->
1606 dolog "invalid outlining state";
1607 logcurrently currently
1610 | "info" ->
1611 state.docinfo <- (1, args) :: state.docinfo
1613 | "infoend" ->
1614 state.uioh#infochanged Docinfo;
1615 state.docinfo <- List.rev state.docinfo
1617 | _ ->
1618 dolog "unknown cmd `%S'" cmds
1621 let now = Unix.gettimeofday;;
1623 let idle () =
1624 if state.deadline == nan then state.deadline <- now ();
1625 let rec loop delay =
1626 let timeout =
1627 if delay > 0.0
1628 then max 0.0 (state.deadline -. now ())
1629 else 0.0
1631 let r, _, _ = Unix.select [state.csock] [] [] timeout in
1632 begin match r with
1633 | [] ->
1634 begin match state.autoscroll with
1635 | Some step when step != 0 ->
1636 let y = state.y + step in
1637 let y =
1638 if y < 0
1639 then state.maxy
1640 else if y >= state.maxy then 0 else y
1642 gotoy y;
1643 if state.mode = View
1644 then state.text <- "";
1645 state.deadline <- state.deadline +. 0.005;
1647 | _ ->
1648 state.deadline <- state.deadline +. delay;
1649 end;
1651 | _ ->
1652 let cmd = readcmd state.csock in
1653 act cmd;
1654 loop 0.0
1655 end;
1656 in loop 0.007
1659 let onhist cb =
1660 let rc = cb.rc in
1661 let action = function
1662 | HCprev -> cbget cb ~-1
1663 | HCnext -> cbget cb 1
1664 | HCfirst -> cbget cb ~-(cb.rc)
1665 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1666 and cancel () = cb.rc <- rc
1667 in (action, cancel)
1670 let search pattern forward =
1671 if String.length pattern > 0
1672 then
1673 let pn, py =
1674 match state.layout with
1675 | [] -> 0, 0
1676 | l :: _ ->
1677 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1679 let cmd =
1680 let b = makecmd "search"
1681 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1683 Buffer.add_char b ',';
1684 Buffer.add_string b pattern;
1685 Buffer.add_char b '\000';
1686 Buffer.contents b;
1688 writecmd state.csock cmd;
1691 let intentry text key =
1692 let c = Char.unsafe_chr key in
1693 match c with
1694 | '0' .. '9' ->
1695 let text = addchar text c in
1696 TEcont text
1698 | _ ->
1699 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1700 TEcont text
1703 let textentry text key =
1704 let c = Char.unsafe_chr key in
1705 match c with
1706 | _ when key >= 32 && key < 127 ->
1707 let text = addchar text c in
1708 TEcont text
1710 | _ ->
1711 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1712 TEcont text
1715 let reqlayout angle proportional =
1716 match state.throttle with
1717 | None ->
1718 if state.invalidated = 0 then state.anchor <- getanchor ();
1719 conf.angle <- angle mod 360;
1720 conf.proportional <- proportional;
1721 invalidate ();
1722 wcmd "reqlayout" [`i conf.angle; `b proportional];
1723 | _ -> ()
1726 let settrim trimmargins trimfuzz =
1727 if state.invalidated = 0 then state.anchor <- getanchor ();
1728 conf.trimmargins <- trimmargins;
1729 conf.trimfuzz <- trimfuzz;
1730 let x0, y0, x1, y1 = trimfuzz in
1731 invalidate ();
1732 wcmd "settrim" [
1733 `b conf.trimmargins;
1734 `i x0;
1735 `i y0;
1736 `i x1;
1737 `i y1;
1739 Hashtbl.iter (fun _ opaque ->
1740 wcmd "freepage" [`s opaque];
1741 ) state.pagemap;
1742 Hashtbl.clear state.pagemap;
1745 let setzoom zoom =
1746 match state.throttle with
1747 | None ->
1748 let zoom = max 0.01 zoom in
1749 if zoom <> conf.zoom
1750 then (
1751 state.prevzoom <- conf.zoom;
1752 let relx =
1753 if zoom <= 1.0
1754 then (state.x <- 0; 0.0)
1755 else float state.x /. float state.w
1757 conf.zoom <- zoom;
1758 reshape conf.winw conf.winh;
1759 if zoom > 1.0
1760 then (
1761 let x = relx *. float state.w in
1762 state.x <- truncate x;
1764 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1767 | _ -> ()
1770 let enterbirdseye () =
1771 let zoom = float conf.thumbw /. float conf.winw in
1772 let birdseyepageno =
1773 let cy = conf.winh / 2 in
1774 let fold = function
1775 | [] -> 0
1776 | l :: rest ->
1777 let rec fold best = function
1778 | [] -> best.pageno
1779 | l :: rest ->
1780 let d = cy - (l.pagedispy + l.pagevh/2)
1781 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1782 if abs d < abs dbest
1783 then fold l rest
1784 else best.pageno
1785 in fold l rest
1787 fold state.layout
1789 state.mode <- Birdseye (
1790 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1792 conf.zoom <- zoom;
1793 conf.presentation <- false;
1794 conf.interpagespace <- 10;
1795 conf.hlinks <- false;
1796 state.x <- 0;
1797 state.mstate <- Mnone;
1798 conf.showall <- false;
1799 Glut.setCursor Glut.CURSOR_INHERIT;
1800 if conf.verbose
1801 then
1802 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1803 (100.0*.zoom)
1804 else
1805 state.text <- ""
1807 reshape conf.winw conf.winh;
1810 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1811 state.mode <- View;
1812 conf.zoom <- c.zoom;
1813 conf.presentation <- c.presentation;
1814 conf.interpagespace <- c.interpagespace;
1815 conf.showall <- c.showall;
1816 conf.hlinks <- c.hlinks;
1817 state.x <- leftx;
1818 if conf.verbose
1819 then
1820 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1821 (100.0*.conf.zoom)
1823 reshape conf.winw conf.winh;
1824 state.anchor <- if goback then anchor else (pageno, 0.0);
1827 let togglebirdseye () =
1828 match state.mode with
1829 | Birdseye vals -> leavebirdseye vals true
1830 | View -> enterbirdseye ()
1831 | _ -> ()
1834 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1835 let pageno = max 0 (pageno - 1) in
1836 let rec loop = function
1837 | [] -> gotopage1 pageno 0
1838 | l :: _ when l.pageno = pageno ->
1839 if l.pagedispy >= 0 && l.pagey = 0
1840 then G.postRedisplay "upbirdseye"
1841 else gotopage1 pageno 0
1842 | _ :: rest -> loop rest
1844 loop state.layout;
1845 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1848 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1849 let pageno = min (state.pagecount - 1) (pageno + 1) in
1850 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1851 let rec loop = function
1852 | [] ->
1853 let y, h = getpageyh pageno in
1854 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1855 gotoy (clamp dy)
1856 | l :: _ when l.pageno = pageno ->
1857 if l.pagevh != l.pageh
1858 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1859 else G.postRedisplay "downbirdseye"
1860 | _ :: rest -> loop rest
1862 loop state.layout
1865 let optentry mode _ key =
1866 let btos b = if b then "on" else "off" in
1867 let c = Char.unsafe_chr key in
1868 match c with
1869 | 's' ->
1870 let ondone s =
1871 try conf.scrollstep <- int_of_string s with exc ->
1872 state.text <- Printf.sprintf "bad integer `%s': %s"
1873 s (Printexc.to_string exc)
1875 TEswitch ("scroll step: ", "", None, intentry, ondone)
1877 | 'A' ->
1878 let ondone s =
1880 conf.autoscrollstep <- int_of_string s;
1881 if state.autoscroll <> None
1882 then state.autoscroll <- Some conf.autoscrollstep
1883 with exc ->
1884 state.text <- Printf.sprintf "bad integer `%s': %s"
1885 s (Printexc.to_string exc)
1887 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1889 | 'Z' ->
1890 let ondone s =
1892 let zoom = float (int_of_string s) /. 100.0 in
1893 setzoom zoom
1894 with exc ->
1895 state.text <- Printf.sprintf "bad integer `%s': %s"
1896 s (Printexc.to_string exc)
1898 TEswitch ("zoom: ", "", None, intentry, ondone)
1900 | 't' ->
1901 let ondone s =
1903 conf.thumbw <- bound (int_of_string s) 2 4096;
1904 state.text <-
1905 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1906 begin match mode with
1907 | Birdseye beye ->
1908 leavebirdseye beye false;
1909 enterbirdseye ();
1910 | _ -> ();
1912 with exc ->
1913 state.text <- Printf.sprintf "bad integer `%s': %s"
1914 s (Printexc.to_string exc)
1916 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
1918 | 'R' ->
1919 let ondone s =
1920 match try
1921 Some (int_of_string s)
1922 with exc ->
1923 state.text <- Printf.sprintf "bad integer `%s': %s"
1924 s (Printexc.to_string exc);
1925 None
1926 with
1927 | Some angle -> reqlayout angle conf.proportional
1928 | None -> ()
1930 TEswitch ("rotation: ", "", None, intentry, ondone)
1932 | 'i' ->
1933 conf.icase <- not conf.icase;
1934 TEdone ("case insensitive search " ^ (btos conf.icase))
1936 | 'p' ->
1937 conf.preload <- not conf.preload;
1938 gotoy state.y;
1939 TEdone ("preload " ^ (btos conf.preload))
1941 | 'v' ->
1942 conf.verbose <- not conf.verbose;
1943 TEdone ("verbose " ^ (btos conf.verbose))
1945 | 'd' ->
1946 conf.debug <- not conf.debug;
1947 TEdone ("debug " ^ (btos conf.debug))
1949 | 'h' ->
1950 conf.maxhfit <- not conf.maxhfit;
1951 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1952 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1954 | 'c' ->
1955 conf.crophack <- not conf.crophack;
1956 TEdone ("crophack " ^ btos conf.crophack)
1958 | 'a' ->
1959 conf.showall <- not conf.showall;
1960 TEdone ("throttle " ^ btos conf.showall)
1962 | 'f' ->
1963 conf.underinfo <- not conf.underinfo;
1964 TEdone ("underinfo " ^ btos conf.underinfo)
1966 | 'P' ->
1967 conf.savebmarks <- not conf.savebmarks;
1968 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1970 | 'S' ->
1971 let ondone s =
1973 let pageno, py =
1974 match state.layout with
1975 | [] -> 0, 0
1976 | l :: _ ->
1977 l.pageno, l.pagey
1979 conf.interpagespace <- int_of_string s;
1980 state.maxy <- calcheight ();
1981 let y = getpagey pageno in
1982 gotoy (y + py)
1983 with exc ->
1984 state.text <- Printf.sprintf "bad integer `%s': %s"
1985 s (Printexc.to_string exc)
1987 TEswitch ("vertical margin: ", "", None, intentry, ondone)
1989 | 'l' ->
1990 reqlayout conf.angle (not conf.proportional);
1991 TEdone ("proportional display " ^ btos conf.proportional)
1993 | 'T' ->
1994 settrim (not conf.trimmargins) conf.trimfuzz;
1995 TEdone ("trim margins " ^ btos conf.trimmargins)
1997 | 'I' ->
1998 conf.invert <- not conf.invert;
1999 TEdone ("invert colors " ^ btos conf.invert)
2001 | _ ->
2002 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2003 TEstop
2006 let maxoutlinerows () = (conf.winh - !uifontsize - 1) / (!uifontsize + 1);;
2008 class type lvsource = object
2009 method getitemcount : int
2010 method getitem : int -> (string * int)
2011 method hasaction : int -> bool
2012 method exit :
2013 uioh:uioh ->
2014 cancel:bool ->
2015 active:int ->
2016 first:int ->
2017 pan:int ->
2018 qsearch:string ->
2019 uioh option
2020 method getactive : int
2021 method getfirst : int
2022 method getqsearch : string
2023 method setqsearch : string -> unit
2024 method getpan : int
2025 end;;
2027 class virtual lvsourcebase = object
2028 val mutable m_active = 0
2029 val mutable m_first = 0
2030 val mutable m_qsearch = ""
2031 val mutable m_pan = 0
2032 method getactive = m_active
2033 method getfirst = m_first
2034 method getqsearch = m_qsearch
2035 method getpan = m_pan
2036 method setqsearch s = m_qsearch <- s
2037 end;;
2039 let textentryspecial key = function
2040 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2041 let s =
2042 match key with
2043 | Glut.KEY_UP -> action HCprev
2044 | Glut.KEY_DOWN -> action HCnext
2045 | Glut.KEY_HOME -> action HCfirst
2046 | Glut.KEY_END -> action HClast
2047 | _ -> state.text
2049 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2050 G.postRedisplay "special textentry";
2051 | _ -> ()
2054 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2055 let enttext te =
2056 state.mode <- Textentry (te, onleave);
2057 state.text <- "";
2058 enttext ();
2059 G.postRedisplay "textentrykeyboard enttext";
2061 match Char.unsafe_chr key with
2062 | '\008' -> (* backspace *)
2063 let len = String.length text in
2064 if len = 0
2065 then (
2066 onleave Cancel;
2067 G.postRedisplay "textentrykeyboard after cancel";
2069 else (
2070 let s = String.sub text 0 (len - 1) in
2071 enttext (c, s, opthist, onkey, ondone)
2074 | '\r' | '\n' ->
2075 ondone text;
2076 onleave Confirm;
2077 G.postRedisplay "textentrykeyboard after confirm"
2079 | '\007' (* ctrl-g *)
2080 | '\027' -> (* escape *)
2081 if String.length text = 0
2082 then (
2083 begin match opthist with
2084 | None -> ()
2085 | Some (_, onhistcancel) -> onhistcancel ()
2086 end;
2087 onleave Cancel;
2088 state.text <- "";
2089 G.postRedisplay "textentrykeyboard after cancel2"
2091 else (
2092 enttext (c, "", opthist, onkey, ondone)
2095 | '\127' -> () (* delete *)
2097 | _ ->
2098 begin match onkey text key with
2099 | TEdone text ->
2100 ondone text;
2101 onleave Confirm;
2102 G.postRedisplay "textentrykeyboard after confirm2";
2104 | TEcont text ->
2105 enttext (c, text, opthist, onkey, ondone);
2107 | TEstop ->
2108 onleave Cancel;
2109 state.text <- "";
2110 G.postRedisplay "textentrykeyboard after cancel3"
2112 | TEswitch te ->
2113 state.mode <- Textentry (te, onleave);
2114 G.postRedisplay "textentrykeyboard switch";
2115 end;
2118 let firstof first active =
2119 let maxrows = maxoutlinerows () in
2120 if first > active || abs (first - active) > maxrows - 1
2121 then max 0 (active - (maxrows/2))
2122 else first
2125 class listview ~(source:lvsource) ~trusted =
2126 let coe s = (s :> uioh) in
2127 object (self)
2128 val m_pan = source#getpan
2129 val m_first = source#getfirst
2130 val m_active = source#getactive
2131 val m_qsearch = source#getqsearch
2132 val m_prev_uioh = state.uioh
2134 method private elemunder y =
2135 let n = y / (!uifontsize+1) in
2136 if m_first + n < source#getitemcount
2137 then (
2138 if source#hasaction (m_first + n)
2139 then Some (m_first + n)
2140 else None
2142 else None
2144 method display =
2145 Gl.enable `blend;
2146 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2147 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2148 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2149 GlDraw.color (1., 1., 1.);
2150 Gl.enable `texture_2d;
2151 let fs = !uifontsize in
2152 let nfs = fs + 1 in
2153 let ww = measurestr fs "w" in
2154 let tabw = 30.0*.ww in
2155 let rec loop row =
2156 if (row - m_first) * nfs > conf.winh
2157 then ()
2158 else (
2159 if row >= 0 && row < source#getitemcount
2160 then (
2161 let (s, level) = source#getitem row in
2162 let y = (row - m_first) * nfs in
2163 let x = 5.0 +. float (level + m_pan) *. ww in
2164 if row = m_active
2165 then (
2166 Gl.disable `texture_2d;
2167 GlDraw.polygon_mode `both `line;
2168 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2169 GlDraw.rect (1., float (y + 1))
2170 (float (conf.winw - 1), float (y + fs + 3));
2171 GlDraw.polygon_mode `both `fill;
2172 GlDraw.color (1., 1., 1.);
2173 Gl.enable `texture_2d;
2176 let drawtabularstring s =
2177 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2178 if trusted
2179 then
2180 let tabpos = try String.index s '\t' with Not_found -> -1 in
2181 if tabpos > 0
2182 then
2183 let len = String.length s - tabpos - 1 in
2184 let s1 = String.sub s 0 tabpos
2185 and s2 = String.sub s (tabpos + 1) len in
2186 let nx = drawstr x s1 in
2187 let sw = nx -. x in
2188 let x = x +. (max tabw sw) in
2189 drawstr x s2
2190 else
2191 drawstr x s
2192 else
2193 drawstr x s
2195 let _ = drawtabularstring s in
2196 loop (row+1)
2200 loop 0;
2201 Gl.disable `blend;
2202 Gl.disable `texture_2d;
2204 method private key1 key =
2205 let set active first qsearch =
2206 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2208 let search active pattern incr =
2209 let dosearch re =
2210 let rec loop n =
2211 if n >= 0 && n < source#getitemcount
2212 then (
2213 let s, _ = source#getitem n in
2215 (try ignore (Str.search_forward re s 0); true
2216 with Not_found -> false)
2217 then Some n
2218 else loop (n + incr)
2220 else None
2222 loop active
2225 let re = Str.regexp_case_fold pattern in
2226 dosearch re
2227 with Failure s ->
2228 state.text <- s;
2229 None
2231 match key with
2232 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2233 let incr = if key = 18 then -1 else 1 in
2234 let active, first =
2235 match search (m_active + incr) m_qsearch incr with
2236 | None ->
2237 state.text <- m_qsearch ^ " [not found]";
2238 m_active, m_first
2239 | Some active ->
2240 state.text <- m_qsearch;
2241 active, firstof m_first active
2243 G.postRedisplay "listview ctrl-r/s";
2244 set active first m_qsearch;
2246 | 8 -> (* backspace *)
2247 let len = String.length m_qsearch in
2248 if len = 0
2249 then coe self
2250 else (
2251 if len = 1
2252 then (
2253 state.text <- "";
2254 G.postRedisplay "listview empty qsearch";
2255 set m_active m_first "";
2257 else
2258 let qsearch = String.sub m_qsearch 0 (len - 1) in
2259 let active, first =
2260 match search m_active qsearch ~-1 with
2261 | None ->
2262 state.text <- qsearch ^ " [not found]";
2263 m_active, m_first
2264 | Some active ->
2265 state.text <- qsearch;
2266 active, firstof m_first active
2268 G.postRedisplay "listview backspace qsearch";
2269 set active first qsearch
2272 | _ when key >= 32 && key < 127 ->
2273 let pattern = addchar m_qsearch (Char.chr key) in
2274 let active, first =
2275 match search m_active pattern 1 with
2276 | None ->
2277 state.text <- pattern ^ " [not found]";
2278 m_active, m_first
2279 | Some active ->
2280 state.text <- pattern;
2281 active, firstof m_first active
2283 G.postRedisplay "listview qsearch add";
2284 set active first pattern;
2286 | 27 -> (* escape *)
2287 state.text <- "";
2288 if String.length m_qsearch = 0
2289 then (
2290 G.postRedisplay "list view escape";
2291 begin
2292 match
2293 source#exit (coe self) true m_active m_first m_pan m_qsearch
2294 with
2295 | None -> m_prev_uioh
2296 | Some uioh -> uioh
2299 else (
2300 G.postRedisplay "list view kill qsearch";
2301 source#setqsearch "";
2302 coe {< m_qsearch = "" >}
2305 | 13 -> (* enter *)
2306 state.text <- "";
2307 let self = {< m_qsearch = "" >} in
2308 source#setqsearch "";
2309 let opt =
2310 G.postRedisplay "listview enter";
2311 if m_active >= 0 && m_active < source#getitemcount
2312 then (
2313 source#exit (coe self) false m_active m_first m_pan "";
2315 else (
2316 source#exit (coe self) true m_active m_first m_pan "";
2319 begin match opt with
2320 | None -> m_prev_uioh
2321 | Some uioh -> uioh
2324 | 127 -> (* delete *)
2325 coe self
2327 | _ -> dolog "unknown key %d" key; coe self
2329 method private special1 key =
2330 let maxrows = maxoutlinerows () in
2331 let itemcount = source#getitemcount in
2332 let find start incr =
2333 let rec find i =
2334 if i = -1 || i = itemcount
2335 then -1
2336 else (
2337 if source#hasaction i
2338 then i
2339 else find (i + incr)
2342 find start
2344 let set active first =
2345 let first = bound first 0 (itemcount - maxrows) in
2346 state.text <- "";
2347 coe {< m_active = active; m_first = first >}
2349 let navigate incr =
2350 let isvisible first n = n >= first && n - first <= maxrows in
2351 let active, first =
2352 let incr1 = if incr > 0 then 1 else -1 in
2353 if isvisible m_first m_active
2354 then
2355 let next =
2356 let next = m_active + incr in
2357 let next =
2358 if next < 0 || next >= itemcount
2359 then -1
2360 else find next incr1
2362 if next = -1 || abs (m_active - next) > maxrows
2363 then -1
2364 else next
2366 if next = -1
2367 then
2368 let first = m_first + incr in
2369 let first = bound first 0 (itemcount - 1) in
2370 let next =
2371 let next = m_active + incr in
2372 let next = bound next 0 (itemcount - 1) in
2373 find next ~-incr1
2375 let active = if next = -1 then m_active else next in
2376 active, first
2377 else
2378 let first = min next m_first in
2379 next, first
2380 else
2381 let first = m_first + incr in
2382 let first = bound first 0 (itemcount - 1) in
2383 let active =
2384 let next = m_active + incr in
2385 let next = bound next 0 (itemcount - 1) in
2386 let next = find next incr1 in
2387 if next = -1 || abs (m_active - first) > maxrows
2388 then m_active
2389 else next
2391 active, first
2393 G.postRedisplay "listview navigate";
2394 set active first;
2396 begin match key with
2397 | Glut.KEY_UP -> navigate ~-1
2398 | Glut.KEY_DOWN -> navigate 1
2399 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2400 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2402 | Glut.KEY_RIGHT ->
2403 state.text <- "";
2404 G.postRedisplay "listview right";
2405 coe {< m_pan = m_pan - 1 >}
2407 | Glut.KEY_LEFT ->
2408 state.text <- "";
2409 G.postRedisplay "listview left";
2410 coe {< m_pan = m_pan + 1 >}
2412 | Glut.KEY_HOME ->
2413 let active = find 0 1 in
2414 G.postRedisplay "listview home";
2415 set active 0;
2417 | Glut.KEY_END ->
2418 let first = max 0 (itemcount - maxrows) in
2419 let active = find (itemcount - 1) ~-1 in
2420 G.postRedisplay "listview end";
2421 set active first;
2423 | _ -> coe self
2424 end;
2426 method key key =
2427 match state.mode with
2428 | Textentry te -> textentrykeyboard key te; coe self
2429 | _ -> self#key1 key
2431 method special key =
2432 match state.mode with
2433 | Textentry te -> textentryspecial key te; coe self
2434 | _ -> self#special1 key
2436 method button button bstate _ y =
2437 let opt =
2438 match button with
2439 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2440 begin match self#elemunder y with
2441 | Some n ->
2442 G.postRedisplay "listview click";
2443 source#exit (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2444 | _ ->
2445 Some (coe self)
2447 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2448 let len = source#getitemcount in
2449 let first =
2450 if m_first + maxoutlinerows () >= len
2451 then
2452 m_first
2453 else
2454 let first = m_first + (if n == 3 then -1 else 1) in
2455 bound first 0 (len - 1)
2457 G.postRedisplay "listview wheel";
2458 Some (coe {< m_first = first >})
2459 | _ ->
2460 Some (coe self)
2462 match opt with
2463 | None -> m_prev_uioh
2464 | Some uioh -> uioh
2466 method motion _ _ = coe self
2468 method pmotion _ y =
2469 let n =
2470 match self#elemunder y with
2471 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2472 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2474 let o =
2475 if n != m_active
2476 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2477 else self
2479 coe o
2481 method infochanged _ = ()
2482 end;;
2484 class outlinelistview ~source : uioh =
2485 let coe o = (o :> uioh) in
2486 object
2487 inherit listview ~source:(source :> lvsource) ~trusted:false as super
2489 method key key =
2490 match key with
2491 | 14 -> (* ctrl-n *)
2492 source#narrow m_qsearch;
2493 G.postRedisplay "outline ctrl-n";
2494 coe {< m_first = 0; m_active = 0 >}
2496 | 21 -> (* ctrl-u *)
2497 source#denarrow;
2498 G.postRedisplay "outline ctrl-u";
2499 coe {< m_first = 0; m_active = 0 >}
2501 | 12 -> (* ctrl-l *)
2502 let first = m_active - (maxoutlinerows () / 2) in
2503 G.postRedisplay "outline ctrl-l";
2504 coe {< m_first = first >}
2506 | 127 -> (* delete *)
2507 source#remove m_active;
2508 G.postRedisplay "outline delete";
2509 let active = max 0 (m_active-1) in
2510 coe {< m_first = firstof m_first active; m_active = active >}
2512 | key -> super#key key
2514 method special key =
2515 let maxrows = maxoutlinerows () in
2516 let calcfirst first active =
2517 if active > first
2518 then
2519 let rows = active - first in
2520 if rows > maxrows then active - maxrows else first
2521 else active
2523 let navigate incr =
2524 let active = m_active + incr in
2525 let active = bound active 0 (source#getitemcount - 1) in
2526 let first = calcfirst m_first active in
2527 G.postRedisplay "special outline navigate";
2528 coe {< m_active = active; m_first = first >}
2530 let updownlevel incr =
2531 let len = source#getitemcount in
2532 let _, curlevel = source#getitem m_active in
2533 let rec flow i =
2534 if i = len then i-1 else if i = -1 then 0 else
2535 let _, l = source#getitem i in
2536 if l != curlevel then i else flow (i+incr)
2538 let active = flow m_active in
2539 let first = calcfirst m_first active in
2540 G.postRedisplay "special outline updownlevel";
2541 {< m_active = active; m_first = first >}
2543 match key with
2544 | Glut.KEY_UP -> navigate ~-1
2545 | Glut.KEY_DOWN -> navigate 1
2546 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2547 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2549 | Glut.KEY_RIGHT ->
2550 let o =
2551 if Glut.getModifiers () land Glut.active_ctrl != 0
2552 then (
2553 G.postRedisplay "special outline right";
2554 {< m_pan = m_pan + 1 >}
2556 else updownlevel 1
2558 coe o
2560 | Glut.KEY_LEFT ->
2561 let o =
2562 if Glut.getModifiers () land Glut.active_ctrl != 0
2563 then (
2564 G.postRedisplay "special outline left";
2565 {< m_pan = m_pan - 1 >}
2567 else updownlevel ~-1
2569 coe o
2571 | Glut.KEY_HOME ->
2572 G.postRedisplay "special outline home";
2573 coe {< m_first = 0; m_active = 0 >}
2575 | Glut.KEY_END ->
2576 let active = source#getitemcount - 1 in
2577 let first = max 0 (active - maxrows) in
2578 G.postRedisplay "special outline end";
2579 coe {< m_active = active; m_first = first >}
2581 | _ -> super#special key
2584 let outlinesource usebookmarks =
2585 let empty = [||] in
2586 (object
2587 inherit lvsourcebase
2588 val mutable m_items = empty
2589 val mutable m_orig_items = empty
2590 val mutable m_prev_items = empty
2591 val mutable m_narrow_pattern = ""
2592 val mutable m_hadremovals = false
2594 method getitemcount = Array.length m_items + (if m_hadremovals then 1 else 0)
2596 method getitem n =
2597 if n == Array.length m_items && m_hadremovals
2598 then
2599 ("[Confirm removal]", 0)
2600 else
2601 let s, n, _ = m_items.(n) in
2602 (s, n)
2604 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
2605 ignore (uioh, first, pan, qsearch);
2606 let confrimremoval = m_hadremovals && active = Array.length m_items in
2607 let items =
2608 if String.length m_narrow_pattern = 0
2609 then m_orig_items
2610 else m_items
2612 if not cancel
2613 then (
2614 if not confrimremoval
2615 then(
2616 let _, _, anchor = m_items.(active) in
2617 gotoanchor anchor;
2618 m_items <- items;
2620 else (
2621 state.bookmarks <- Array.to_list m_items;
2622 m_orig_items <- m_items;
2625 else m_items <- items;
2626 None
2628 method hasaction _ = true
2630 method greetmsg =
2631 if Array.length m_items != Array.length m_orig_items
2632 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
2633 else ""
2635 method narrow pattern =
2636 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2637 match reopt with
2638 | None -> ()
2639 | Some re ->
2640 let rec loop accu n =
2641 if n = -1
2642 then (
2643 m_narrow_pattern <- pattern;
2644 m_items <- Array.of_list accu
2646 else
2647 let (s, _, _) as o = m_items.(n) in
2648 let accu =
2649 if (try ignore (Str.search_forward re s 0); true
2650 with Not_found -> false)
2651 then o :: accu
2652 else accu
2654 loop accu (n-1)
2656 loop [] (Array.length m_items - 1)
2658 method denarrow =
2659 m_orig_items <- (
2660 if usebookmarks
2661 then Array.of_list state.bookmarks
2662 else state.outlines
2664 m_items <- m_orig_items
2666 method remove m =
2667 if usebookmarks
2668 then
2669 if m >= 0 && m < Array.length m_items
2670 then (
2671 m_hadremovals <- true;
2672 m_items <- Array.init (Array.length m_items - 1) (fun n ->
2673 let n = if n >= m then n+1 else n in
2674 m_items.(n)
2678 method reset pageno items =
2679 m_hadremovals <- false;
2680 if m_orig_items == empty || m_prev_items != items
2681 then (
2682 m_orig_items <- items;
2683 if String.length m_narrow_pattern = 0
2684 then m_items <- items;
2686 m_prev_items <- items;
2687 let active =
2688 let rec loop n best bestd =
2689 if n = Array.length m_items
2690 then best
2691 else
2692 let (_, _, (outlinepageno, _)) = m_items.(n) in
2693 let d = abs (outlinepageno - pageno) in
2694 if d < bestd
2695 then loop (n+1) n d
2696 else loop (n+1) best bestd
2698 loop 0 ~-1 max_int
2700 m_active <- active;
2701 m_first <- firstof m_first active
2702 end)
2705 let enterselector usebookmarks =
2706 let source = outlinesource usebookmarks in
2707 fun errmsg ->
2708 let outlines =
2709 if usebookmarks
2710 then Array.of_list state.bookmarks
2711 else state.outlines
2713 if Array.length outlines = 0
2714 then (
2715 showtext ' ' errmsg;
2717 else (
2718 state.text <- source#greetmsg;
2719 Glut.setCursor Glut.CURSOR_INHERIT;
2720 let pageno =
2721 match state.layout with
2722 | [] -> -1
2723 | {pageno=pageno} :: _ -> pageno
2725 source#reset pageno outlines;
2726 state.uioh <- new outlinelistview ~source;
2727 G.postRedisplay "enter selector";
2731 let enteroutlinemode =
2732 let f = enterselector false in
2733 fun ()-> f "Document has no outline";
2736 let enterbookmarkmode =
2737 let f = enterselector true in
2738 fun () -> f "Document has no bookmarks (yet)";
2741 let color_of_string s =
2742 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
2743 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
2747 let color_to_string (r, g, b) =
2748 let r = truncate (r *. 256.0)
2749 and g = truncate (g *. 256.0)
2750 and b = truncate (b *. 256.0) in
2751 Printf.sprintf "%d/%d/%d" r g b
2754 let irect_of_string s =
2755 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
2758 let irect_to_string (x0,y0,x1,y1) =
2759 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
2762 let makecheckers () =
2763 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
2764 following to say:
2765 converted by Issac Trotts. July 25, 2002 *)
2766 let image_height = 64
2767 and image_width = 64 in
2769 let make_image () =
2770 let image =
2771 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height in
2772 for i = 0 to image_width - 1 do
2773 for j = 0 to image_height - 1 do
2774 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
2775 (if (i land 8 ) lxor (j land 8) = 0
2776 then [|255;255;255|] else [|200;200;200|])
2777 done
2778 done;
2779 image
2781 let image = make_image () in
2782 let id = GlTex.gen_texture () in
2783 GlTex.bind_texture `texture_2d id;
2784 GlPix.store (`unpack_alignment 1);
2785 GlTex.image2d image;
2786 List.iter (GlTex.parameter ~target:`texture_2d)
2787 [ `wrap_s `repeat;
2788 `wrap_t `repeat;
2789 `mag_filter `nearest;
2790 `min_filter `nearest ];
2794 let setcheckers enabled =
2795 match state.texid with
2796 | None ->
2797 if enabled then state.texid <- Some (makecheckers ())
2799 | Some texid ->
2800 if not enabled
2801 then (
2802 GlTex.delete_texture texid;
2803 state.texid <- None;
2807 let int_of_string_with_suffix s =
2808 let l = String.length s in
2809 let s1, shift =
2810 if l > 1
2811 then
2812 let suffix = Char.lowercase s.[l-1] in
2813 match suffix with
2814 | 'k' -> String.sub s 0 (l-1), 10
2815 | 'm' -> String.sub s 0 (l-1), 20
2816 | 'g' -> String.sub s 0 (l-1), 30
2817 | _ -> s, 0
2818 else s, 0
2820 let n = int_of_string s1 in
2821 let m = n lsl shift in
2822 if m < 0 || m < n
2823 then raise (Failure "value too large")
2824 else m
2827 let string_with_suffix_of_int n =
2828 if n = 0
2829 then "0"
2830 else
2831 let n, s =
2832 if n = 0
2833 then 0, ""
2834 else (
2835 if n land ((1 lsl 20) - 1) = 0
2836 then n lsr 20, "M"
2837 else (
2838 if n land ((1 lsl 10) - 1) = 0
2839 then n lsr 10, "K"
2840 else n, ""
2844 let rec loop s n =
2845 let h = n mod 1000 in
2846 let n = n / 1000 in
2847 if n = 0
2848 then string_of_int h ^ s
2849 else (
2850 let s = Printf.sprintf "_%03d%s" h s in
2851 loop s n
2854 loop "" n ^ s;
2857 let describe_location () =
2858 let f (fn, _) l =
2859 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
2861 let fn, ln = List.fold_left f (-1, -1) state.layout in
2862 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2863 let percent =
2864 if maxy <= 0
2865 then 100.
2866 else (100. *. (float state.y /. float maxy))
2868 if fn = ln
2869 then
2870 Printf.sprintf "page %d of %d [%.2f%%]"
2871 (fn+1) state.pagecount percent
2872 else
2873 Printf.sprintf
2874 "pages %d-%d of %d [%.2f%%]"
2875 (fn+1) (ln+1) state.pagecount percent
2878 let enterinfomode =
2879 let btos b = if b then "\xe2\x88\x9a" else "" in
2880 let showextended = ref false in
2881 let leave mode = function
2882 | Confirm -> state.mode <- mode
2883 | Cancel -> state.mode <- mode in
2884 let src =
2885 (object
2886 val mutable m_first_time = true
2887 val mutable m_l = []
2888 val mutable m_a = [||]
2889 val mutable m_prev_uioh = nouioh
2890 val mutable m_prev_mode = View
2892 inherit lvsourcebase
2894 method reset prev_mode prev_uioh =
2895 m_a <- Array.of_list (List.rev m_l);
2896 m_l <- [];
2897 m_prev_mode <- prev_mode;
2898 m_prev_uioh <- prev_uioh;
2899 if m_first_time
2900 then (
2901 let rec loop n =
2902 if n >= Array.length m_a
2903 then ()
2904 else
2905 match m_a.(n) with
2906 | _, _, _, Action _ -> m_active <- n
2907 | _ -> loop (n+1)
2909 loop 0;
2910 m_first_time <- false;
2913 method int name get set =
2914 m_l <-
2915 (name, `int get, 1, Action (
2916 fun u ->
2917 let ondone s =
2918 try set (int_of_string s)
2919 with exn ->
2920 state.text <- Printf.sprintf "bad integer `%s': %s"
2921 s (Printexc.to_string exn)
2923 state.text <- "";
2924 let te = name ^ ": ", "", None, intentry, ondone in
2925 state.mode <- Textentry (te, leave m_prev_mode);
2927 )) :: m_l
2929 method int_with_suffix name get set =
2930 m_l <-
2931 (name, `intws get, 1, Action (
2932 fun u ->
2933 let ondone s =
2934 try set (int_of_string_with_suffix s)
2935 with exn ->
2936 state.text <- Printf.sprintf "bad integer `%s': %s"
2937 s (Printexc.to_string exn)
2939 state.text <- "";
2940 let te =
2941 name ^ ": ", "", None, intentry_with_suffix, ondone
2943 state.mode <- Textentry (te, leave m_prev_mode);
2945 )) :: m_l
2947 method bool ?(offset=1) ?(btos=btos) name get set =
2948 m_l <-
2949 (name, `bool (btos, get), offset, Action (
2950 fun u ->
2951 let v = get () in
2952 set (not v);
2954 )) :: m_l
2956 method color name get set =
2957 m_l <-
2958 (name, `color get, 1, Action (
2959 fun u ->
2960 let invalid = (nan, nan, nan) in
2961 let ondone s =
2962 let c =
2963 try color_of_string s
2964 with exn ->
2965 state.text <- Printf.sprintf "bad color `%s': %s"
2966 s (Printexc.to_string exn);
2967 invalid
2969 if c <> invalid
2970 then set c;
2972 let te = name ^ ": ", "", None, textentry, ondone in
2973 state.text <- color_to_string (get ());
2974 state.mode <- Textentry (te, leave m_prev_mode);
2976 )) :: m_l
2978 method string name get set =
2979 m_l <-
2980 (name, `string get, 1, Action (
2981 fun u ->
2982 let ondone s = set s in
2983 let te = name ^ ": ", "", None, textentry, ondone in
2984 state.mode <- Textentry (te, leave m_prev_mode);
2986 )) :: m_l
2988 method colorspace name get set =
2989 m_l <-
2990 (name, `string get, 1, Action (
2991 fun _ ->
2992 let source =
2993 let vals = [| "rgb"; "bgr"; "gray" |] in
2994 (object
2995 inherit lvsourcebase
2997 initializer
2998 m_active <- int_of_colorspace conf.colorspace;
2999 m_first <- 0;
3001 method getitemcount = Array.length vals
3002 method getitem n = (vals.(n), 0)
3003 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3004 ignore (uioh, first, pan, qsearch);
3005 if not cancel then set active;
3006 None
3007 method hasaction _ = true
3008 end)
3010 state.text <- "";
3011 new listview ~source ~trusted:true
3012 )) :: m_l
3014 method caption s offset =
3015 m_l <- (s, `empty, offset, Noaction) :: m_l
3017 method caption2 s f offset =
3018 m_l <- (s, `string f, offset, Noaction) :: m_l
3020 method getitemcount = Array.length m_a
3022 method getitem n =
3023 let tostr = function
3024 | `int f -> string_of_int (f ())
3025 | `intws f -> string_with_suffix_of_int (f ())
3026 | `string f -> f ()
3027 | `color f -> color_to_string (f ())
3028 | `bool (btos, f) -> btos (f ())
3029 | `empty -> ""
3031 let name, t, offset, _ = m_a.(n) in
3032 ((let s = tostr t in
3033 if String.length s > 0
3034 then Printf.sprintf "%s\t%s" name s
3035 else name),
3036 offset)
3038 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3039 let uiohopt =
3040 if not cancel
3041 then (
3042 m_qsearch <- qsearch;
3043 let uioh =
3044 match m_a.(active) with
3045 | _, _, _, Action f -> f uioh
3046 | _ -> uioh
3048 Some uioh
3050 else None
3052 m_active <- active;
3053 m_first <- first;
3054 m_pan <- pan;
3055 uiohopt
3057 method hasaction n =
3058 match m_a.(n) with
3059 | _, _, _, Action _ -> true
3060 | _ -> false
3061 end)
3063 let rec fillsrc () =
3064 let sep () = src#caption "" 0 in
3065 let colorp name get set =
3066 src#string name
3067 (fun () -> color_to_string (get ()))
3068 (fun v ->
3070 let c = color_of_string v in
3071 set c
3072 with exn ->
3073 state.text <- Printf.sprintf "bad color `%s': %s"
3074 v (Printexc.to_string exn);
3077 let oldmode = state.mode in
3078 let birdseye = isbirdseye state.mode in
3080 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3082 src#bool "presentation mode"
3083 (fun () -> conf.presentation)
3084 (fun v ->
3085 conf.presentation <- v;
3086 state.anchor <- getanchor ();
3087 represent ());
3089 src#bool "ignore case in searches"
3090 (fun () -> conf.icase)
3091 (fun v -> conf.icase <- v);
3093 src#bool "preload"
3094 (fun () -> conf.preload)
3095 (fun v -> conf.preload <- v);
3097 src#bool "throttle"
3098 (fun () -> conf.showall)
3099 (fun v -> conf.showall <- v);
3101 src#bool "highlight links"
3102 (fun () -> conf.hlinks)
3103 (fun v -> conf.hlinks <- v);
3105 src#bool "under info"
3106 (fun () -> conf.underinfo)
3107 (fun v -> conf.underinfo <- v);
3109 src#bool "persistent bookmarks"
3110 (fun () -> conf.savebmarks)
3111 (fun v -> conf.savebmarks <- v);
3113 src#bool "proportional display"
3114 (fun () -> conf.proportional)
3115 (fun v -> reqlayout conf.angle v);
3117 src#bool "trim margins"
3118 (fun () -> conf.trimmargins)
3119 (fun v -> settrim v conf.trimfuzz; fillsrc ());
3121 src#bool "persistent location"
3122 (fun () -> conf.jumpback)
3123 (fun v -> conf.jumpback <- v);
3125 sep ();
3126 src#int "vertical margin"
3127 (fun () -> conf.interpagespace)
3128 (fun n ->
3129 conf.interpagespace <- n;
3130 let pageno, py =
3131 match state.layout with
3132 | [] -> 0, 0
3133 | l :: _ ->
3134 l.pageno, l.pagey
3136 state.maxy <- calcheight ();
3137 let y = getpagey pageno in
3138 gotoy (y + py)
3141 src#int "page bias"
3142 (fun () -> conf.pagebias)
3143 (fun v -> conf.pagebias <- v);
3145 src#int "scroll step"
3146 (fun () -> conf.scrollstep)
3147 (fun n -> conf.scrollstep <- n);
3149 src#int "auto scroll step"
3150 (fun () ->
3151 match state.autoscroll with
3152 | Some step -> step
3153 | _ -> conf.autoscrollstep)
3154 (fun n ->
3155 if state.autoscroll <> None
3156 then state.autoscroll <- Some n;
3157 conf.autoscrollstep <- n);
3159 src#int "zoom"
3160 (fun () -> truncate (conf.zoom *. 100.))
3161 (fun v -> setzoom ((float v) /. 100.));
3163 src#int "rotation"
3164 (fun () -> conf.angle)
3165 (fun v -> reqlayout v conf.proportional);
3167 src#int "scroll bar width"
3168 (fun () -> state.scrollw)
3169 (fun v ->
3170 state.scrollw <- v;
3171 conf.scrollbw <- v;
3172 reshape conf.winw conf.winh;
3175 src#int "scroll handle height"
3176 (fun () -> conf.scrollh)
3177 (fun v -> conf.scrollh <- v;);
3179 src#int "thumbnail width"
3180 (fun () -> conf.thumbw)
3181 (fun v ->
3182 conf.thumbw <- min 4096 v;
3183 match oldmode with
3184 | Birdseye beye ->
3185 leavebirdseye beye false;
3186 enterbirdseye ()
3187 | _ -> ()
3190 sep ();
3191 src#caption "Presentation mode" 0;
3192 src#bool "scrollbar visible"
3193 (fun () -> conf.scrollbarinpm)
3194 (fun v ->
3195 if v != conf.scrollbarinpm
3196 then (
3197 conf.scrollbarinpm <- v;
3198 if conf.presentation
3199 then (
3200 state.scrollw <- if v then conf.scrollbw else 0;
3201 reshape conf.winw conf.winh;
3206 sep ();
3207 src#caption "Pixmap cache" 0;
3208 src#int_with_suffix "size (advisory)"
3209 (fun () -> conf.memlimit)
3210 (fun v -> conf.memlimit <- v);
3212 src#caption2 "used"
3213 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3214 (string_with_suffix_of_int state.memused)
3215 (Hashtbl.length state.tilemap)) 1;
3217 sep ();
3218 src#caption "Layout" 0;
3219 src#caption2 "Dimension"
3220 (fun () ->
3221 Printf.sprintf "%dx%d (virtual %dx%d)"
3222 conf.winw conf.winh
3223 state.w state.maxy)
3225 if conf.debug
3226 then
3227 src#caption2 "Position" (fun () ->
3228 Printf.sprintf "%dx%d" state.x state.y
3230 else
3231 src#caption2 "Visible" (fun () -> describe_location ()) 1
3234 sep ();
3235 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3236 "Save these parameters as global defaults at exit"
3237 (fun () -> conf.bedefault)
3238 (fun v -> conf.bedefault <- v)
3241 sep ();
3242 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3243 src#bool ~offset:0 ~btos "Extended parameters"
3244 (fun () -> !showextended)
3245 (fun v -> showextended := v; fillsrc ());
3246 if !showextended
3247 then (
3248 src#bool "checkers"
3249 (fun () -> conf.checkers)
3250 (fun v -> conf.checkers <- v; setcheckers v);
3251 src#bool "verbose"
3252 (fun () -> conf.verbose)
3253 (fun v -> conf.verbose <- v);
3254 src#bool "invert colors"
3255 (fun () -> conf.invert)
3256 (fun v -> conf.invert <- v);
3257 src#bool "max fit"
3258 (fun () -> conf.maxhfit)
3259 (fun v -> conf.maxhfit <- v);
3260 src#string "uri launcher"
3261 (fun () -> conf.urilauncher)
3262 (fun v -> conf.urilauncher <- v);
3263 src#string "tile size"
3264 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3265 (fun v ->
3267 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3268 conf.tileh <- max 64 w;
3269 conf.tilew <- max 64 h;
3270 flushtiles ();
3271 with exn ->
3272 state.text <- Printf.sprintf "bad tile size `%s': %s"
3273 v (Printexc.to_string exn));
3274 src#int "anti-aliasing level"
3275 (fun () -> conf.aalevel)
3276 (fun v ->
3277 conf.aalevel <- bound v 0 8;
3278 state.anchor <- getanchor ();
3279 opendoc state.path state.password;
3281 src#int "ui font size"
3282 (fun () -> !uifontsize)
3283 (fun v -> uifontsize := bound v 5 100);
3284 colorp "background color"
3285 (fun () -> conf.bgcolor)
3286 (fun v -> conf.bgcolor <- v);
3287 src#bool "crop hack"
3288 (fun () -> conf.crophack)
3289 (fun v -> conf.crophack <- v);
3290 src#string "trim fuzz"
3291 (fun () -> irect_to_string conf.trimfuzz)
3292 (fun v ->
3294 conf.trimfuzz <- irect_of_string v;
3295 if conf.trimmargins
3296 then settrim true conf.trimfuzz;
3297 with exn ->
3298 state.text <- Printf.sprintf "bad irect `%s': %s"
3299 v (Printexc.to_string exn)
3301 src#colorspace "color space"
3302 (fun () -> colorspace_to_string conf.colorspace)
3303 (fun v ->
3304 conf.colorspace <- colorspace_of_int v;
3305 wcmd "cs" [`i v];
3306 load state.layout;
3310 sep ();
3311 src#caption "Document" 0;
3312 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3313 if conf.trimmargins
3314 then (
3315 sep ();
3316 src#caption "Trimmed margins" 0;
3317 src#caption2 "Dimensions"
3318 (fun () -> string_of_int (List.length state.pdims)) 1;
3321 src#reset state.mode state.uioh;
3323 fun () ->
3324 state.text <- "";
3325 fillsrc ();
3326 let source = (src :> lvsource) in
3327 state.uioh <- object
3328 inherit listview ~source ~trusted:true
3329 val mutable m_prevmemused = 0
3330 method infochanged = function
3331 | Memused ->
3332 if m_prevmemused != state.memused
3333 then (
3334 m_prevmemused <- state.memused;
3335 G.postRedisplay "memusedchanged";
3337 | Pdim -> G.postRedisplay "pdimchanged"
3338 | Docinfo -> fillsrc ()
3339 end;
3340 G.postRedisplay "info";
3343 let enterhelpmode =
3344 let source =
3345 (object
3346 inherit lvsourcebase
3347 method getitemcount = Array.length state.help
3348 method getitem n =
3349 let s, n, _ = state.help.(n) in
3350 (s, n)
3352 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3353 let optuioh =
3354 if not cancel
3355 then (
3356 m_qsearch <- qsearch;
3357 match state.help.(active) with
3358 | _, _, Action f -> Some (f uioh)
3359 | _ -> Some (uioh)
3361 else None
3363 m_active <- active;
3364 m_first <- first;
3365 m_pan <- pan;
3366 optuioh
3368 method hasaction n =
3369 match state.help.(n) with
3370 | _, _, Action _ -> true
3371 | _ -> false
3373 initializer
3374 m_active <- -1
3375 end)
3376 in fun () ->
3377 state.uioh <- new listview ~source ~trusted:true;
3378 G.postRedisplay "help";
3381 let quickbookmark ?title () =
3382 match state.layout with
3383 | [] -> ()
3384 | l :: _ ->
3385 let title =
3386 match title with
3387 | None ->
3388 let sec = Unix.gettimeofday () in
3389 let tm = Unix.localtime sec in
3390 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
3391 (l.pageno+1)
3392 tm.Unix.tm_mday
3393 tm.Unix.tm_mon
3394 (tm.Unix.tm_year + 1900)
3395 tm.Unix.tm_hour
3396 tm.Unix.tm_min
3397 | Some title -> title
3399 state.bookmarks <-
3400 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
3401 :: state.bookmarks
3404 let doreshape w h =
3405 state.fullscreen <- None;
3406 Glut.reshapeWindow w h;
3409 let viewkeyboard key =
3410 let enttext te =
3411 let mode = state.mode in
3412 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3413 state.text <- "";
3414 enttext ();
3415 G.postRedisplay "view:enttext"
3417 let c = Char.chr key in
3418 match c with
3419 | '\027' | 'q' -> (* escape *)
3420 begin match state.mstate with
3421 | Mzoomrect _ ->
3422 state.mstate <- Mnone;
3423 Glut.setCursor Glut.CURSOR_INHERIT;
3424 G.postRedisplay "kill zoom rect";
3425 | _ ->
3426 raise Quit
3427 end;
3429 | '\008' -> (* backspace *)
3430 let y = getnav ~-1 in
3431 gotoy_and_clear_text y
3433 | 'o' ->
3434 enteroutlinemode ()
3436 | 'u' ->
3437 state.rects <- [];
3438 state.text <- "";
3439 G.postRedisplay "dehighlight";
3441 | '/' | '?' ->
3442 let ondone isforw s =
3443 cbput state.hists.pat s;
3444 state.searchpattern <- s;
3445 search s isforw
3447 let s = String.create 1 in
3448 s.[0] <- c;
3449 enttext (s, "", Some (onhist state.hists.pat),
3450 textentry, ondone (c ='/'))
3452 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3453 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3454 setzoom (conf.zoom +. incr)
3456 | '+' ->
3457 let ondone s =
3458 let n =
3459 try int_of_string s with exc ->
3460 state.text <- Printf.sprintf "bad integer `%s': %s"
3461 s (Printexc.to_string exc);
3462 max_int
3464 if n != max_int
3465 then (
3466 conf.pagebias <- n;
3467 state.text <- "page bias is now " ^ string_of_int n;
3470 enttext ("page bias: ", "", None, intentry, ondone)
3472 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3473 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3474 setzoom (max 0.01 (conf.zoom -. decr))
3476 | '-' ->
3477 let ondone msg = state.text <- msg in
3478 enttext (
3479 "option [acfhilpstvAPRSZTI]: ", "", None,
3480 optentry state.mode, ondone
3483 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3484 setzoom 1.0
3486 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3487 let zoom = zoomforh conf.winw conf.winh state.scrollw in
3488 if zoom < 1.0
3489 then setzoom zoom
3491 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3492 togglebirdseye ()
3494 | '0' .. '9' ->
3495 let ondone s =
3496 let n =
3497 try int_of_string s with exc ->
3498 state.text <- Printf.sprintf "bad integer `%s': %s"
3499 s (Printexc.to_string exc);
3502 if n >= 0
3503 then (
3504 addnav ();
3505 cbput state.hists.pag (string_of_int n);
3506 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
3509 let pageentry text key =
3510 match Char.unsafe_chr key with
3511 | 'g' -> TEdone text
3512 | _ -> intentry text key
3514 let text = "x" in text.[0] <- c;
3515 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
3517 | 'b' ->
3518 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
3519 reshape conf.winw conf.winh;
3521 | 'l' ->
3522 conf.hlinks <- not conf.hlinks;
3523 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3524 G.postRedisplay "toggle highlightlinks";
3526 | 'a' ->
3527 begin match state.autoscroll with
3528 | Some step ->
3529 conf.autoscrollstep <- step;
3530 state.autoscroll <- None
3531 | None ->
3532 if conf.autoscrollstep = 0
3533 then state.autoscroll <- Some 1
3534 else state.autoscroll <- Some conf.autoscrollstep
3537 | 'P' ->
3538 conf.presentation <- not conf.presentation;
3539 if conf.presentation
3540 then (
3541 if not conf.scrollbarinpm
3542 then state.scrollw <- 0;
3544 else
3545 state.scrollw <- conf.scrollbw;
3547 showtext ' ' ("presentation mode " ^
3548 if conf.presentation then "on" else "off");
3549 state.anchor <- getanchor ();
3550 represent ()
3552 | 'f' ->
3553 begin match state.fullscreen with
3554 | None ->
3555 state.fullscreen <- Some (conf.winw, conf.winh);
3556 Glut.fullScreen ()
3557 | Some (w, h) ->
3558 state.fullscreen <- None;
3559 doreshape w h
3562 | 'g' ->
3563 gotoy_and_clear_text 0
3565 | 'G' ->
3566 gotopage1 (state.pagecount - 1) 0
3568 | 'n' ->
3569 search state.searchpattern true
3571 | 'p' | 'N' ->
3572 search state.searchpattern false
3574 | 't' ->
3575 begin match state.layout with
3576 | [] -> ()
3577 | l :: _ ->
3578 gotoy_and_clear_text (getpagey l.pageno)
3581 | ' ' ->
3582 begin match List.rev state.layout with
3583 | [] -> ()
3584 | l :: _ ->
3585 let pageno = min (l.pageno+1) (state.pagecount-1) in
3586 gotoy_and_clear_text (getpagey pageno)
3589 | '\127' -> (* del *)
3590 begin match state.layout with
3591 | [] -> ()
3592 | l :: _ ->
3593 let pageno = max 0 (l.pageno-1) in
3594 gotoy_and_clear_text (getpagey pageno)
3597 | '=' ->
3598 showtext ' ' (describe_location ());
3600 | 'w' ->
3601 begin match state.layout with
3602 | [] -> ()
3603 | l :: _ ->
3604 doreshape (l.pagew + state.scrollw) l.pageh;
3605 G.postRedisplay "w"
3608 | '\'' ->
3609 enterbookmarkmode ()
3611 | 'h' ->
3612 enterhelpmode ()
3614 | 'i' ->
3615 enterinfomode ()
3617 | 'm' ->
3618 let ondone s =
3619 match state.layout with
3620 | l :: _ ->
3621 state.bookmarks <-
3622 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
3623 :: state.bookmarks
3624 | _ -> ()
3626 enttext ("bookmark: ", "", None, textentry, ondone)
3628 | '~' ->
3629 quickbookmark ();
3630 showtext ' ' "Quick bookmark added";
3632 | 'z' ->
3633 begin match state.layout with
3634 | l :: _ ->
3635 let rect = getpdimrect l.pagedimno in
3636 let w, h =
3637 if conf.crophack
3638 then
3639 (truncate (1.8 *. (rect.(1) -. rect.(0))),
3640 truncate (1.2 *. (rect.(3) -. rect.(0))))
3641 else
3642 (truncate (rect.(1) -. rect.(0)),
3643 truncate (rect.(3) -. rect.(0)))
3645 let w = truncate ((float w)*.conf.zoom)
3646 and h = truncate ((float h)*.conf.zoom) in
3647 if w != 0 && h != 0
3648 then (
3649 state.anchor <- getanchor ();
3650 doreshape (w + state.scrollw) (h + conf.interpagespace)
3652 G.postRedisplay "z";
3654 | [] -> ()
3657 | '\000' -> (* ctrl-2 *)
3658 let maxw = getmaxw () in
3659 if maxw > 0.0
3660 then setzoom (maxw /. float conf.winw)
3662 | '<' | '>' ->
3663 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
3665 | '[' | ']' ->
3666 state.colorscale <-
3667 bound (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
3669 G.postRedisplay "brightness";
3671 | 'k' ->
3672 begin match state.mode with
3673 | Birdseye beye -> upbirdseye beye
3674 | _ -> gotoy (clamp (-conf.scrollstep))
3677 | 'j' ->
3678 begin match state.mode with
3679 | Birdseye beye -> downbirdseye beye
3680 | _ -> gotoy (clamp conf.scrollstep)
3683 | 'r' ->
3684 state.anchor <- getanchor ();
3685 opendoc state.path state.password
3687 | 'v' when conf.debug ->
3688 state.rects <- [];
3689 List.iter (fun l ->
3690 match getopaque l.pageno with
3691 | None -> ()
3692 | Some opaque ->
3693 let x0, y0, x1, y1 = pagebbox opaque in
3694 let a,b = float x0, float y0 in
3695 let c,d = float x1, float y0 in
3696 let e,f = float x1, float y1 in
3697 let h,j = float x0, float y1 in
3698 let rect = (a,b,c,d,e,f,h,j) in
3699 debugrect rect;
3700 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
3701 ) state.layout;
3702 G.postRedisplay "v";
3704 | _ ->
3705 vlog "huh? %d %c" key (Char.chr key);
3708 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
3709 match key with
3710 | 27 -> (* escape *)
3711 leavebirdseye beye true
3713 | 12 -> (* ctrl-l *)
3714 let y, h = getpageyh pageno in
3715 let top = (conf.winh - h) / 2 in
3716 gotoy (max 0 (y - top))
3718 | 13 -> (* enter *)
3719 leavebirdseye beye false
3721 | _ ->
3722 viewkeyboard key
3725 let keyboard ~key ~x ~y =
3726 ignore x;
3727 ignore y;
3728 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
3729 then wcmd "interrupt" []
3730 else state.uioh <- state.uioh#key key
3733 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
3734 match key with
3735 | Glut.KEY_UP -> upbirdseye beye
3736 | Glut.KEY_DOWN -> downbirdseye beye
3738 | Glut.KEY_PAGE_UP ->
3739 begin match state.layout with
3740 | l :: _ ->
3741 if l.pagey != 0
3742 then (
3743 state.mode <- Birdseye (
3744 conf, leftx, l.pageno, hooverpageno, anchor
3746 gotopage1 l.pageno 0;
3748 else (
3749 let layout = layout (state.y-conf.winh) conf.winh in
3750 match layout with
3751 | [] -> gotoy (clamp (-conf.winh))
3752 | l :: _ ->
3753 state.mode <- Birdseye (
3754 conf, leftx, l.pageno, hooverpageno, anchor
3756 gotopage1 l.pageno 0
3759 | [] -> gotoy (clamp (-conf.winh))
3760 end;
3762 | Glut.KEY_PAGE_DOWN ->
3763 begin match List.rev state.layout with
3764 | l :: _ ->
3765 let layout = layout (state.y + conf.winh) conf.winh in
3766 begin match layout with
3767 | [] ->
3768 let incr = l.pageh - l.pagevh in
3769 if incr = 0
3770 then (
3771 state.mode <-
3772 Birdseye (
3773 conf, leftx, state.pagecount - 1, hooverpageno, anchor
3775 G.postRedisplay "birdseye pagedown";
3777 else gotoy (clamp (incr + conf.interpagespace*2));
3779 | l :: _ ->
3780 state.mode <-
3781 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
3782 gotopage1 l.pageno 0;
3785 | [] -> gotoy (clamp conf.winh)
3786 end;
3788 | Glut.KEY_HOME ->
3789 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
3790 gotopage1 0 0
3792 | Glut.KEY_END ->
3793 let pageno = state.pagecount - 1 in
3794 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3795 if not (pagevisible state.layout pageno)
3796 then
3797 let h =
3798 match List.rev state.pdims with
3799 | [] -> conf.winh
3800 | (_, _, h, _) :: _ -> h
3802 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
3803 else G.postRedisplay "birdseye end";
3804 | _ -> ()
3807 let setautoscrollspeed step goingdown =
3808 let incr = max 1 ((abs step) / 2) in
3809 let incr = if goingdown then incr else -incr in
3810 let astep = step + incr in
3811 state.autoscroll <- Some astep;
3814 let special ~key ~x ~y =
3815 ignore x;
3816 ignore y;
3817 state.uioh <- state.uioh#special key
3820 let drawpage l =
3821 let color =
3822 match state.mode with
3823 | Textentry _ -> scalecolor 0.4
3824 | View -> scalecolor 1.0
3825 | Birdseye (_, _, pageno, hooverpageno, _) ->
3826 if l.pageno = hooverpageno
3827 then scalecolor 0.9
3828 else (
3829 if l.pageno = pageno
3830 then scalecolor 1.0
3831 else scalecolor 0.8
3834 drawtiles l color;
3835 begin match getopaque l.pageno with
3836 | Some opaque ->
3837 if tileready l l.pagex l.pagey
3838 then
3839 let x = l.pagedispx - l.pagex
3840 and y = l.pagedispy - l.pagey in
3841 postprocess opaque conf.hlinks x y;
3843 | _ -> ()
3844 end;
3847 let scrollph y =
3848 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3849 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3850 let sh = float conf.winh /. sh in
3851 let sh = max sh (float conf.scrollh) in
3853 let percent =
3854 if y = state.maxy
3855 then 1.0
3856 else float y /. float maxy
3858 let position = (float conf.winh -. sh) *. percent in
3860 let position =
3861 if position +. sh > float conf.winh
3862 then float conf.winh -. sh
3863 else position
3865 position, sh;
3868 let scrollpw x =
3869 let winw = conf.winw - state.scrollw - 1 in
3870 let fwinw = float winw in
3871 let sw =
3872 let sw = fwinw /. float state.w in
3873 let sw = fwinw *. sw in
3874 max sw (float conf.scrollh)
3876 let position, sw =
3877 let f = state.w+winw in
3878 let r = float (winw-x) /. float f in
3879 let p = fwinw *. r in
3880 p-.sw/.2., sw
3882 let sw =
3883 if position +. sw > fwinw
3884 then fwinw -. position
3885 else sw
3887 position, sw;
3890 let scrollindicator () =
3891 GlDraw.color (0.64 , 0.64, 0.64);
3892 GlDraw.rect
3893 (float (conf.winw - state.scrollw), 0.)
3894 (float conf.winw, float conf.winh)
3896 GlDraw.rect
3897 (0., float (conf.winh - state.hscrollh))
3898 (float (conf.winw - state.scrollw - 1), float conf.winh)
3900 GlDraw.color (0.0, 0.0, 0.0);
3902 let position, sh = scrollph state.y in
3903 GlDraw.rect
3904 (float (conf.winw - state.scrollw), position)
3905 (float conf.winw, position +. sh)
3907 let position, sw = scrollpw state.x in
3908 GlDraw.rect
3909 (position, float (conf.winh - state.hscrollh))
3910 (position +. sw, float conf.winh)
3914 let pagetranslatepoint l x y =
3915 let dy = y - l.pagedispy in
3916 let y = dy + l.pagey in
3917 let dx = x - l.pagedispx in
3918 let x = dx + l.pagex in
3919 (x, y);
3922 let showsel () =
3923 match state.mstate with
3924 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
3927 | Msel ((x0, y0), (x1, y1)) ->
3928 let rec loop = function
3929 | l :: ls ->
3930 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3931 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3932 then
3933 match getopaque l.pageno with
3934 | Some opaque ->
3935 let dx, dy = pagetranslatepoint l 0 0 in
3936 let x0 = x0 + dx
3937 and y0 = y0 + dy
3938 and x1 = x1 + dx
3939 and y1 = y1 + dy in
3940 GlMat.mode `modelview;
3941 GlMat.push ();
3942 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
3943 seltext opaque (x0, y0, x1, y1);
3944 GlMat.pop ();
3945 | _ -> ()
3946 else loop ls
3947 | [] -> ()
3949 loop state.layout
3952 let showrects () =
3953 Gl.enable `blend;
3954 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
3955 GlDraw.polygon_mode `both `fill;
3956 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3957 List.iter
3958 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
3959 List.iter (fun l ->
3960 if l.pageno = pageno
3961 then (
3962 let dx = float (l.pagedispx - l.pagex) in
3963 let dy = float (l.pagedispy - l.pagey) in
3964 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
3965 GlDraw.begins `quads;
3967 GlDraw.vertex2 (x0+.dx, y0+.dy);
3968 GlDraw.vertex2 (x1+.dx, y1+.dy);
3969 GlDraw.vertex2 (x2+.dx, y2+.dy);
3970 GlDraw.vertex2 (x3+.dx, y3+.dy);
3972 GlDraw.ends ();
3974 ) state.layout
3975 ) state.rects
3977 Gl.disable `blend;
3980 let display () =
3981 GlClear.color (scalecolor2 conf.bgcolor);
3982 GlClear.clear [`color];
3983 List.iter drawpage state.layout;
3984 showrects ();
3985 showsel ();
3986 scrollindicator ();
3987 state.uioh#display;
3988 begin match state.mstate with
3989 | Mzoomrect ((x0, y0), (x1, y1)) ->
3990 Gl.enable `blend;
3991 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
3992 GlDraw.polygon_mode `both `fill;
3993 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3994 GlDraw.rect (float x0, float y0)
3995 (float x1, float y1);
3996 Gl.disable `blend;
3997 | _ -> ()
3998 end;
3999 enttext ();
4000 Glut.swapBuffers ();
4003 let getunder x y =
4004 let rec f = function
4005 | l :: rest ->
4006 begin match getopaque l.pageno with
4007 | Some opaque ->
4008 let x0 = l.pagedispx in
4009 let x1 = x0 + l.pagevw in
4010 let y0 = l.pagedispy in
4011 let y1 = y0 + l.pagevh in
4012 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4013 then
4014 let px, py = pagetranslatepoint l x y in
4015 match whatsunder opaque px py with
4016 | Unone -> f rest
4017 | under -> under
4018 else f rest
4019 | _ ->
4020 f rest
4022 | [] -> Unone
4024 f state.layout
4027 let zoomrect x y x1 y1 =
4028 let x0 = min x x1
4029 and x1 = max x x1
4030 and y0 = min y y1 in
4031 gotoy (state.y + y0);
4032 state.anchor <- getanchor ();
4033 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4034 state.x <- state.x - x0;
4035 setzoom zoom;
4036 Glut.setCursor Glut.CURSOR_INHERIT;
4037 state.mstate <- Mnone;
4040 let scrollx x =
4041 let winw = conf.winw - state.scrollw - 1 in
4042 let s = float x /. float winw in
4043 let destx = truncate (float (state.w + winw) *. s) in
4044 state.x <- winw - destx;
4045 gotoy_and_clear_text state.y;
4046 state.mstate <- Mscrollx;
4049 let scrolly y =
4050 let s = float y /. float conf.winh in
4051 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4052 gotoy_and_clear_text desty;
4053 state.mstate <- Mscrolly;
4056 let viewmouse button bstate x y =
4057 match button with
4058 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4059 if Glut.getModifiers () land Glut.active_ctrl != 0
4060 then (
4061 match state.mstate with
4062 | Mzoom (oldn, i) ->
4063 if oldn = n
4064 then (
4065 if i = 2
4066 then
4067 let incr =
4068 match n with
4069 | 4 ->
4070 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4071 | _ ->
4072 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4074 let zoom = conf.zoom -. incr in
4075 setzoom zoom;
4076 state.mstate <- Mzoom (n, 0);
4077 else
4078 state.mstate <- Mzoom (n, i+1);
4080 else state.mstate <- Mzoom (n, 0)
4082 | _ -> state.mstate <- Mzoom (n, 0)
4084 else (
4085 match state.autoscroll with
4086 | Some step -> setautoscrollspeed step (n=4)
4087 | None ->
4088 let incr =
4089 if n = 3
4090 then -conf.scrollstep
4091 else conf.scrollstep
4093 let incr = incr * 2 in
4094 let y = clamp incr in
4095 gotoy_and_clear_text y
4098 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4099 if bstate = Glut.DOWN
4100 then (
4101 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4102 state.mstate <- Mpan (x, y)
4104 else
4105 state.mstate <- Mnone
4107 | Glut.RIGHT_BUTTON ->
4108 if bstate = Glut.DOWN
4109 then (
4110 Glut.setCursor Glut.CURSOR_CYCLE;
4111 let p = (x, y) in
4112 state.mstate <- Mzoomrect (p, p)
4114 else (
4115 match state.mstate with
4116 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4117 | _ ->
4118 Glut.setCursor Glut.CURSOR_INHERIT;
4119 state.mstate <- Mnone
4122 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4123 if bstate = Glut.DOWN
4124 then
4125 let position, sh = scrollph state.y in
4126 if y > truncate position && y < truncate (position +. sh)
4127 then state.mstate <- Mscrolly
4128 else scrolly y
4129 else
4130 state.mstate <- Mnone
4132 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4133 if bstate = Glut.DOWN
4134 then
4135 let position, sw = scrollpw state.x in
4136 if x > truncate position && x < truncate (position +. sw)
4137 then state.mstate <- Mscrollx
4138 else scrollx x
4139 else
4140 state.mstate <- Mnone
4142 | Glut.LEFT_BUTTON ->
4143 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4144 begin match dest with
4145 | Ulinkgoto (pageno, top) ->
4146 if pageno >= 0
4147 then (
4148 addnav ();
4149 gotopage1 pageno top;
4152 | Ulinkuri s ->
4153 gotouri s
4155 | Unone when bstate = Glut.DOWN ->
4156 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4157 state.mstate <- Mpan (x, y);
4159 | Unone | Utext _ ->
4160 if bstate = Glut.DOWN
4161 then (
4162 if conf.angle mod 360 = 0
4163 then (
4164 state.mstate <- Msel ((x, y), (x, y));
4165 G.postRedisplay "mouse select";
4168 else (
4169 match state.mstate with
4170 | Mnone -> ()
4172 | Mzoom _ | Mscrollx | Mscrolly ->
4173 state.mstate <- Mnone
4175 | Mzoomrect ((x0, y0), _) ->
4176 zoomrect x0 y0 x y
4178 | Mpan _ ->
4179 Glut.setCursor Glut.CURSOR_INHERIT;
4180 state.mstate <- Mnone
4182 | Msel ((_, y0), (_, y1)) ->
4183 let f l =
4184 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4185 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4186 then
4187 match getopaque l.pageno with
4188 | Some opaque ->
4189 copysel opaque
4190 | _ -> ()
4192 List.iter f state.layout;
4193 copysel ""; (* ugly *)
4194 Glut.setCursor Glut.CURSOR_INHERIT;
4195 state.mstate <- Mnone;
4199 | _ -> ()
4202 let birdseyemouse button bstate x y
4203 (conf, leftx, _, hooverpageno, anchor) =
4204 match button with
4205 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4206 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4207 let rec loop = function
4208 | [] -> ()
4209 | l :: rest ->
4210 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4211 && x > margin && x < margin + l.pagew
4212 then (
4213 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4215 else loop rest
4217 loop state.layout
4218 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4219 | _ -> ()
4222 let mouse bstate button x y =
4223 state.uioh <- state.uioh#button button bstate x y;
4226 let mouse ~button ~state ~x ~y = mouse state button x y;;
4228 let motion ~x ~y =
4229 state.uioh <- state.uioh#motion x y
4232 let pmotion ~x ~y =
4233 state.uioh <- state.uioh#pmotion x y;
4236 let uioh = object
4237 method display = ()
4239 method key key =
4240 begin match state.mode with
4241 | Textentry textentry -> textentrykeyboard key textentry
4242 | Birdseye birdseye -> birdseyekeyboard key birdseye
4243 | View -> viewkeyboard key
4244 end;
4245 state.uioh
4247 method special key =
4248 begin match state.mode with
4249 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4250 togglebirdseye ()
4252 | Birdseye vals ->
4253 birdseyespecial key vals
4255 | View when key = Glut.KEY_F1 ->
4256 enterhelpmode ()
4258 | View ->
4259 begin match state.autoscroll with
4260 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4261 setautoscrollspeed step (key = Glut.KEY_DOWN)
4263 | _ ->
4264 let y =
4265 match key with
4266 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4267 | Glut.KEY_UP ->
4268 if Glut.getModifiers () land Glut.active_ctrl != 0
4269 then
4270 if Glut.getModifiers () land Glut.active_shift != 0
4271 then (setzoom state.prevzoom; state.y)
4272 else clamp (-conf.winh/2)
4273 else clamp (-conf.scrollstep)
4274 | Glut.KEY_DOWN ->
4275 if Glut.getModifiers () land Glut.active_ctrl != 0
4276 then
4277 if Glut.getModifiers () land Glut.active_shift != 0
4278 then (setzoom state.prevzoom; state.y)
4279 else clamp (conf.winh/2)
4280 else clamp (conf.scrollstep)
4281 | Glut.KEY_PAGE_UP ->
4282 if Glut.getModifiers () land Glut.active_ctrl != 0
4283 then
4284 match state.layout with
4285 | [] -> state.y
4286 | l :: _ -> state.y - l.pagey
4287 else
4288 clamp (-conf.winh)
4289 | Glut.KEY_PAGE_DOWN ->
4290 if Glut.getModifiers () land Glut.active_ctrl != 0
4291 then
4292 match List.rev state.layout with
4293 | [] -> state.y
4294 | l :: _ -> getpagey l.pageno
4295 else
4296 clamp conf.winh
4297 | Glut.KEY_HOME ->
4298 addnav ();
4300 | Glut.KEY_END ->
4301 addnav ();
4302 state.maxy - (if conf.maxhfit then conf.winh else 0)
4304 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4305 Glut.getModifiers () land Glut.active_alt != 0 ->
4306 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4308 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4309 let dx =
4310 if Glut.getModifiers () land Glut.active_ctrl != 0
4311 then (conf.winw / 2)
4312 else 10
4314 state.x <- state.x - dx;
4315 state.y
4316 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4317 let dx =
4318 if Glut.getModifiers () land Glut.active_ctrl != 0
4319 then (conf.winw / 2)
4320 else 10
4322 state.x <- state.x + dx;
4323 state.y
4325 | _ -> state.y
4327 gotoy_and_clear_text y
4330 | Textentry te -> textentryspecial key te
4331 end;
4332 state.uioh
4334 method button button bstate x y =
4335 begin match state.mode with
4336 | View -> viewmouse button bstate x y
4337 | Birdseye beye -> birdseyemouse button bstate x y beye
4338 | Textentry _ -> ()
4339 end;
4340 state.uioh
4342 method motion x y =
4343 begin match state.mode with
4344 | Textentry _ -> ()
4345 | View | Birdseye _ ->
4346 match state.mstate with
4347 | Mzoom _ | Mnone -> ()
4349 | Mpan (x0, y0) ->
4350 let dx = x - x0
4351 and dy = y0 - y in
4352 state.mstate <- Mpan (x, y);
4353 if conf.zoom > 1.0 then state.x <- state.x + dx;
4354 let y = clamp dy in
4355 gotoy_and_clear_text y
4357 | Msel (a, _) ->
4358 state.mstate <- Msel (a, (x, y));
4359 G.postRedisplay "motion select";
4361 | Mscrolly ->
4362 let y = min conf.winh (max 0 y) in
4363 scrolly y
4365 | Mscrollx ->
4366 let x = min conf.winw (max 0 x) in
4367 scrollx x
4369 | Mzoomrect (p0, _) ->
4370 state.mstate <- Mzoomrect (p0, (x, y));
4371 G.postRedisplay "motion zoomrect";
4372 end;
4373 state.uioh
4375 method pmotion x y =
4376 begin match state.mode with
4377 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4378 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4379 let rec loop = function
4380 | [] ->
4381 if hooverpageno != -1
4382 then (
4383 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4384 G.postRedisplay "pmotion birdseye no hoover";
4386 | l :: rest ->
4387 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4388 && x > margin && x < margin + l.pagew
4389 then (
4390 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4391 G.postRedisplay "pmotion birdseye hoover";
4393 else loop rest
4395 loop state.layout
4397 | Textentry _ -> ()
4399 | View ->
4400 match state.mstate with
4401 | Mnone ->
4402 begin match getunder x y with
4403 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
4404 | Ulinkuri uri ->
4405 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
4406 Glut.setCursor Glut.CURSOR_INFO
4407 | Ulinkgoto (page, _) ->
4408 if conf.underinfo
4409 then showtext 'p' ("age: " ^ string_of_int (page+1));
4410 Glut.setCursor Glut.CURSOR_INFO
4411 | Utext s ->
4412 if conf.underinfo then showtext 'f' ("ont: " ^ s);
4413 Glut.setCursor Glut.CURSOR_TEXT
4416 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
4418 end;
4419 state.uioh
4421 method infochanged _ = ()
4422 end;;
4424 module Config =
4425 struct
4426 open Parser
4428 let fontpath = ref "";;
4429 let wmclasshack = ref false;;
4431 let unent s =
4432 let l = String.length s in
4433 let b = Buffer.create l in
4434 unent b s 0 l;
4435 Buffer.contents b;
4438 let home =
4440 match platform with
4441 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
4442 | _ -> Sys.getenv "HOME"
4443 with exn ->
4444 prerr_endline
4445 ("Can not determine home directory location: " ^
4446 Printexc.to_string exn);
4450 let config_of c attrs =
4451 let apply c k v =
4453 match k with
4454 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
4455 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
4456 | "case-insensitive-search" -> { c with icase = bool_of_string v }
4457 | "preload" -> { c with preload = bool_of_string v }
4458 | "page-bias" -> { c with pagebias = int_of_string v }
4459 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
4460 | "auto-scroll-step" ->
4461 { c with autoscrollstep = max 0 (int_of_string v) }
4462 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
4463 | "crop-hack" -> { c with crophack = bool_of_string v }
4464 | "throttle" -> { c with showall = bool_of_string v }
4465 | "highlight-links" -> { c with hlinks = bool_of_string v }
4466 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
4467 | "vertical-margin" ->
4468 { c with interpagespace = max 0 (int_of_string v) }
4469 | "zoom" ->
4470 let zoom = float_of_string v /. 100. in
4471 let zoom = max zoom 0.0 in
4472 { c with zoom = zoom }
4473 | "presentation" -> { c with presentation = bool_of_string v }
4474 | "rotation-angle" -> { c with angle = int_of_string v }
4475 | "width" -> { c with winw = max 20 (int_of_string v) }
4476 | "height" -> { c with winh = max 20 (int_of_string v) }
4477 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
4478 | "proportional-display" -> { c with proportional = bool_of_string v }
4479 | "pixmap-cache-size" ->
4480 { c with memlimit = max 2 (int_of_string_with_suffix v) }
4481 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
4482 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
4483 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
4484 | "persistent-location" -> { c with jumpback = bool_of_string v }
4485 | "background-color" -> { c with bgcolor = color_of_string v }
4486 | "scrollbar-in-presentation" ->
4487 { c with scrollbarinpm = bool_of_string v }
4488 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
4489 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
4490 | "memlimit" ->
4491 { c with mumemlimit = max 1024 (int_of_string_with_suffix v) }
4492 | "checkers" -> { c with checkers = bool_of_string v }
4493 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
4494 | "trim-margins" -> { c with trimmargins = bool_of_string v }
4495 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
4496 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
4497 | "uri-launcher" -> { c with urilauncher = unent v }
4498 | "color-space" -> { c with colorspace = colorspace_of_string v }
4499 | "invert-colors" -> { c with invert = bool_of_string v }
4500 | _ -> c
4501 with exn ->
4502 prerr_endline ("Error processing attribute (`" ^
4503 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
4506 let rec fold c = function
4507 | [] -> c
4508 | (k, v) :: rest ->
4509 let c = apply c k v in
4510 fold c rest
4512 fold c attrs;
4515 let fromstring f pos n v d =
4516 try f v
4517 with exn ->
4518 dolog "Error processing attribute (%S=%S) at %d\n%s"
4519 n v pos (Printexc.to_string exn)
4524 let bookmark_of attrs =
4525 let rec fold title page rely = function
4526 | ("title", v) :: rest -> fold v page rely rest
4527 | ("page", v) :: rest -> fold title v rely rest
4528 | ("rely", v) :: rest -> fold title page v rest
4529 | _ :: rest -> fold title page rely rest
4530 | [] -> title, page, rely
4532 fold "invalid" "0" "0" attrs
4535 let doc_of attrs =
4536 let rec fold path page rely pan = function
4537 | ("path", v) :: rest -> fold v page rely pan rest
4538 | ("page", v) :: rest -> fold path v rely pan rest
4539 | ("rely", v) :: rest -> fold path page v pan rest
4540 | ("pan", v) :: rest -> fold path page rely v rest
4541 | _ :: rest -> fold path page rely pan rest
4542 | [] -> path, page, rely, pan
4544 fold "" "0" "0" "0" attrs
4547 let setconf dst src =
4548 dst.scrollbw <- src.scrollbw;
4549 dst.scrollh <- src.scrollh;
4550 dst.icase <- src.icase;
4551 dst.preload <- src.preload;
4552 dst.pagebias <- src.pagebias;
4553 dst.verbose <- src.verbose;
4554 dst.scrollstep <- src.scrollstep;
4555 dst.maxhfit <- src.maxhfit;
4556 dst.crophack <- src.crophack;
4557 dst.autoscrollstep <- src.autoscrollstep;
4558 dst.showall <- src.showall;
4559 dst.hlinks <- src.hlinks;
4560 dst.underinfo <- src.underinfo;
4561 dst.interpagespace <- src.interpagespace;
4562 dst.zoom <- src.zoom;
4563 dst.presentation <- src.presentation;
4564 dst.angle <- src.angle;
4565 dst.winw <- src.winw;
4566 dst.winh <- src.winh;
4567 dst.savebmarks <- src.savebmarks;
4568 dst.memlimit <- src.memlimit;
4569 dst.proportional <- src.proportional;
4570 dst.texcount <- src.texcount;
4571 dst.sliceheight <- src.sliceheight;
4572 dst.thumbw <- src.thumbw;
4573 dst.jumpback <- src.jumpback;
4574 dst.bgcolor <- src.bgcolor;
4575 dst.scrollbarinpm <- src.scrollbarinpm;
4576 dst.tilew <- src.tilew;
4577 dst.tileh <- src.tileh;
4578 dst.mumemlimit <- src.mumemlimit;
4579 dst.checkers <- src.checkers;
4580 dst.aalevel <- src.aalevel;
4581 dst.trimmargins <- src.trimmargins;
4582 dst.trimfuzz <- src.trimfuzz;
4583 dst.urilauncher <- src.urilauncher;
4584 dst.colorspace <- src.colorspace;
4585 dst.invert <- src.invert;
4588 let get s =
4589 let h = Hashtbl.create 10 in
4590 let dc = { defconf with angle = defconf.angle } in
4591 let rec toplevel v t spos _ =
4592 match t with
4593 | Vdata | Vcdata | Vend -> v
4594 | Vopen ("llppconfig", _, closed) ->
4595 if closed
4596 then v
4597 else { v with f = llppconfig }
4598 | Vopen _ ->
4599 error "unexpected subelement at top level" s spos
4600 | Vclose _ -> error "unexpected close at top level" s spos
4602 and llppconfig v t spos _ =
4603 match t with
4604 | Vdata | Vcdata -> v
4605 | Vend -> error "unexpected end of input in llppconfig" s spos
4606 | Vopen ("defaults", attrs, closed) ->
4607 let c = config_of dc attrs in
4608 setconf dc c;
4609 if closed
4610 then v
4611 else { v with f = skip "defaults" (fun () -> v) }
4613 | Vopen ("ui-font", attrs, closed) ->
4614 let rec getsize size = function
4615 | [] -> size
4616 | ("size", v) :: rest ->
4617 let size =
4618 fromstring int_of_string spos "size" v !uifontsize in
4619 getsize size rest
4620 | l -> getsize size l
4622 uifontsize := getsize !uifontsize attrs;
4623 if closed
4624 then v
4625 else { v with f = uifont (Buffer.create 10) }
4627 | Vopen ("doc", attrs, closed) ->
4628 let pathent, spage, srely, span = doc_of attrs in
4629 let path = unent pathent
4630 and pageno = fromstring int_of_string spos "page" spage 0
4631 and rely = fromstring float_of_string spos "rely" srely 0.0
4632 and pan = fromstring int_of_string spos "pan" span 0 in
4633 let c = config_of dc attrs in
4634 let anchor = (pageno, rely) in
4635 if closed
4636 then (Hashtbl.add h path (c, [], pan, anchor); v)
4637 else { v with f = doc path pan anchor c [] }
4639 | Vopen _ ->
4640 error "unexpected subelement in llppconfig" s spos
4642 | Vclose "llppconfig" -> { v with f = toplevel }
4643 | Vclose _ -> error "unexpected close in llppconfig" s spos
4645 and uifont b v t spos epos =
4646 match t with
4647 | Vdata | Vcdata ->
4648 Buffer.add_substring b s spos (epos - spos);
4650 | Vopen (_, _, _) ->
4651 error "unexpected subelement in ui-font" s spos
4652 | Vclose "ui-font" ->
4653 if String.length !fontpath = 0
4654 then fontpath := Buffer.contents b;
4655 { v with f = llppconfig }
4656 | Vclose _ -> error "unexpected close in ui-font" s spos
4657 | Vend -> error "unexpected end of input in ui-font" s spos
4659 and doc path pan anchor c bookmarks v t spos _ =
4660 match t with
4661 | Vdata | Vcdata -> v
4662 | Vend -> error "unexpected end of input in doc" s spos
4663 | Vopen ("bookmarks", _, closed) ->
4664 if closed
4665 then v
4666 else { v with f = pbookmarks path pan anchor c bookmarks }
4668 | Vopen (_, _, _) ->
4669 error "unexpected subelement in doc" s spos
4671 | Vclose "doc" ->
4672 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
4673 { v with f = llppconfig }
4675 | Vclose _ -> error "unexpected close in doc" s spos
4677 and pbookmarks path pan anchor c bookmarks v t spos _ =
4678 match t with
4679 | Vdata | Vcdata -> v
4680 | Vend -> error "unexpected end of input in bookmarks" s spos
4681 | Vopen ("item", attrs, closed) ->
4682 let titleent, spage, srely = bookmark_of attrs in
4683 let page = fromstring int_of_string spos "page" spage 0
4684 and rely = fromstring float_of_string spos "rely" srely 0.0 in
4685 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
4686 if closed
4687 then { v with f = pbookmarks path pan anchor c bookmarks }
4688 else
4689 let f () = v in
4690 { v with f = skip "item" f }
4692 | Vopen _ ->
4693 error "unexpected subelement in bookmarks" s spos
4695 | Vclose "bookmarks" ->
4696 { v with f = doc path pan anchor c bookmarks }
4698 | Vclose _ -> error "unexpected close in bookmarks" s spos
4700 and skip tag f v t spos _ =
4701 match t with
4702 | Vdata | Vcdata -> v
4703 | Vend ->
4704 error ("unexpected end of input in skipped " ^ tag) s spos
4705 | Vopen (tag', _, closed) ->
4706 if closed
4707 then v
4708 else
4709 let f' () = { v with f = skip tag f } in
4710 { v with f = skip tag' f' }
4711 | Vclose ctag ->
4712 if tag = ctag
4713 then f ()
4714 else error ("unexpected close in skipped " ^ tag) s spos
4717 parse { f = toplevel; accu = () } s;
4718 h, dc;
4721 let do_load f ic =
4723 let len = in_channel_length ic in
4724 let s = String.create len in
4725 really_input ic s 0 len;
4726 f s;
4727 with
4728 | Parse_error (msg, s, pos) ->
4729 let subs = subs s pos in
4730 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
4731 failwith ("parse error: " ^ s)
4733 | exn ->
4734 failwith ("config load error: " ^ Printexc.to_string exn)
4737 let defconfpath =
4738 let dir =
4740 let dir = Filename.concat home ".config" in
4741 if Sys.is_directory dir then dir else home
4742 with _ -> home
4744 Filename.concat dir "llpp.conf"
4747 let confpath = ref defconfpath;;
4749 let load1 f =
4750 if Sys.file_exists !confpath
4751 then
4752 match
4753 (try Some (open_in_bin !confpath)
4754 with exn ->
4755 prerr_endline
4756 ("Error opening configuation file `" ^ !confpath ^ "': " ^
4757 Printexc.to_string exn);
4758 None
4760 with
4761 | Some ic ->
4762 begin try
4763 f (do_load get ic)
4764 with exn ->
4765 prerr_endline
4766 ("Error loading configuation from `" ^ !confpath ^ "': " ^
4767 Printexc.to_string exn);
4768 end;
4769 close_in ic;
4771 | None -> ()
4772 else
4773 f (Hashtbl.create 0, defconf)
4776 let load () =
4777 let f (h, dc) =
4778 let pc, pb, px, pa =
4780 Hashtbl.find h (Filename.basename state.path)
4781 with Not_found -> dc, [], 0, (0, 0.0)
4783 setconf defconf dc;
4784 setconf conf pc;
4785 state.bookmarks <- pb;
4786 state.x <- px;
4787 state.scrollw <- conf.scrollbw;
4788 if conf.jumpback
4789 then state.anchor <- pa;
4790 cbput state.hists.nav pa;
4792 load1 f
4795 let add_attrs bb always dc c =
4796 let ob s a b =
4797 if always || a != b
4798 then Printf.bprintf bb "\n %s='%b'" s a
4799 and oi s a b =
4800 if always || a != b
4801 then Printf.bprintf bb "\n %s='%d'" s a
4802 and oI s a b =
4803 if always || a != b
4804 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
4805 and oz s a b =
4806 if always || a <> b
4807 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
4808 and oc s a b =
4809 if always || a <> b
4810 then
4811 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
4812 and oC s a b =
4813 if always || a <> b
4814 then
4815 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
4816 and oR s a b =
4817 if always || a <> b
4818 then
4819 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
4820 and os s a b =
4821 if always || a <> b
4822 then
4823 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
4825 let w, h =
4826 if always
4827 then dc.winw, dc.winh
4828 else
4829 match state.fullscreen with
4830 | Some wh -> wh
4831 | None -> c.winw, c.winh
4833 let zoom, presentation, interpagespace, showall=
4834 if always
4835 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
4836 else
4837 match state.mode with
4838 | Birdseye (bc, _, _, _, _) ->
4839 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
4840 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
4842 oi "width" w dc.winw;
4843 oi "height" h dc.winh;
4844 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
4845 oi "scroll-handle-height" c.scrollh dc.scrollh;
4846 ob "case-insensitive-search" c.icase dc.icase;
4847 ob "preload" c.preload dc.preload;
4848 oi "page-bias" c.pagebias dc.pagebias;
4849 oi "scroll-step" c.scrollstep dc.scrollstep;
4850 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
4851 ob "max-height-fit" c.maxhfit dc.maxhfit;
4852 ob "crop-hack" c.crophack dc.crophack;
4853 ob "throttle" showall dc.showall;
4854 ob "highlight-links" c.hlinks dc.hlinks;
4855 ob "under-cursor-info" c.underinfo dc.underinfo;
4856 oi "vertical-margin" interpagespace dc.interpagespace;
4857 oz "zoom" zoom dc.zoom;
4858 ob "presentation" presentation dc.presentation;
4859 oi "rotation-angle" c.angle dc.angle;
4860 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
4861 ob "proportional-display" c.proportional dc.proportional;
4862 oI "pixmap-cache-size" c.memlimit dc.memlimit;
4863 oi "tex-count" c.texcount dc.texcount;
4864 oi "slice-height" c.sliceheight dc.sliceheight;
4865 oi "thumbnail-width" c.thumbw dc.thumbw;
4866 ob "persistent-location" c.jumpback dc.jumpback;
4867 oc "background-color" c.bgcolor dc.bgcolor;
4868 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
4869 oi "tile-width" c.tilew dc.tilew;
4870 oi "tile-height" c.tileh dc.tileh;
4871 oI "mupdf-memlimit" c.mumemlimit dc.mumemlimit;
4872 ob "checkers" c.checkers dc.checkers;
4873 oi "aalevel" c.aalevel dc.aalevel;
4874 ob "trim-margins" c.trimmargins dc.trimmargins;
4875 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
4876 os "uri-launcher" c.urilauncher dc.urilauncher;
4877 oC "color-space" c.colorspace dc.colorspace;
4878 ob "invert-colors" c.invert dc.invert;
4879 if always
4880 then ob "wmclass-hack" !wmclasshack false;
4883 let save () =
4884 let uifontsize = !uifontsize in
4885 let bb = Buffer.create 32768 in
4886 let f (h, dc) =
4887 let dc = if conf.bedefault then conf else dc in
4888 Buffer.add_string bb "<llppconfig>\n";
4890 if String.length !fontpath > 0
4891 then
4892 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
4893 uifontsize
4894 !fontpath
4895 else (
4896 if uifontsize <> 14
4897 then
4898 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
4901 Buffer.add_string bb "<defaults ";
4902 add_attrs bb true dc dc;
4903 Buffer.add_string bb "/>\n";
4905 let adddoc path pan anchor c bookmarks =
4906 if bookmarks == [] && c = dc && anchor = emptyanchor
4907 then ()
4908 else (
4909 Printf.bprintf bb "<doc path='%s'"
4910 (enent path 0 (String.length path));
4912 if anchor <> emptyanchor
4913 then (
4914 let n, y = anchor in
4915 Printf.bprintf bb " page='%d'" n;
4916 if y > 1e-6
4917 then
4918 Printf.bprintf bb " rely='%f'" y
4922 if pan != 0
4923 then Printf.bprintf bb " pan='%d'" pan;
4925 add_attrs bb false dc c;
4927 begin match bookmarks with
4928 | [] -> Buffer.add_string bb "/>\n"
4929 | _ ->
4930 Buffer.add_string bb ">\n<bookmarks>\n";
4931 List.iter (fun (title, _level, (page, rely)) ->
4932 Printf.bprintf bb
4933 "<item title='%s' page='%d'"
4934 (enent title 0 (String.length title))
4935 page
4937 if rely > 1e-6
4938 then
4939 Printf.bprintf bb " rely='%f'" rely
4941 Buffer.add_string bb "/>\n";
4942 ) bookmarks;
4943 Buffer.add_string bb "</bookmarks>\n</doc>\n";
4944 end;
4948 let pan =
4949 match state.mode with
4950 | Birdseye (_, pan, _, _, _) -> pan
4951 | _ -> state.x
4953 let basename = Filename.basename state.path in
4954 adddoc basename pan (getanchor ())
4955 { conf with
4956 autoscrollstep =
4957 match state.autoscroll with
4958 | Some step -> step
4959 | None -> conf.autoscrollstep }
4960 (if conf.savebmarks then state.bookmarks else []);
4962 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
4963 if basename <> path
4964 then adddoc path x y c bookmarks
4965 ) h;
4966 Buffer.add_string bb "</llppconfig>";
4968 load1 f;
4969 if Buffer.length bb > 0
4970 then
4972 let tmp = !confpath ^ ".tmp" in
4973 let oc = open_out_bin tmp in
4974 Buffer.output_buffer oc bb;
4975 close_out oc;
4976 Unix.rename tmp !confpath;
4977 with exn ->
4978 prerr_endline
4979 ("error while saving configuration: " ^ Printexc.to_string exn)
4981 end;;
4983 let () =
4984 Arg.parse
4985 (Arg.align
4986 [("-p", Arg.String (fun s -> state.password <- s) ,
4987 "<password> Set password");
4989 ("-f", Arg.String (fun s -> Config.fontpath := s),
4990 "<path> Set path to the user interface font");
4992 ("-c", Arg.String (fun s -> Config.confpath := s),
4993 "<path> Set path to the configuration file");
4995 ("-v", Arg.Unit (fun () ->
4996 Printf.printf
4997 "%s\nconfiguration path: %s\n"
4998 Help.version
4999 Config.defconfpath
5001 exit 0), " Print version and exit");
5004 (fun s -> state.path <- s)
5005 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5007 if String.length state.path = 0
5008 then (prerr_endline "file name missing"; exit 1);
5010 Config.load ();
5012 let _ = Glut.init Sys.argv in
5013 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5014 let () = Glut.initWindowSize conf.winw conf.winh in
5015 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5017 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5018 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5019 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5021 let csock, ssock =
5022 if not is_windows
5023 then
5024 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5025 else
5026 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5027 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5028 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5029 Unix.bind sock addr;
5030 Unix.listen sock 1;
5031 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5032 Unix.connect csock addr;
5033 let ssock, _ = Unix.accept sock in
5034 Unix.close sock;
5035 let opts sock =
5036 Unix.setsockopt sock Unix.TCP_NODELAY true;
5037 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5039 opts ssock;
5040 opts csock;
5041 ssock, csock
5044 let () = Glut.displayFunc display in
5045 let () = Glut.reshapeFunc reshape in
5046 let () = Glut.keyboardFunc keyboard in
5047 let () = Glut.specialFunc special in
5048 let () = Glut.idleFunc (Some idle) in
5049 let () = Glut.mouseFunc mouse in
5050 let () = Glut.motionFunc motion in
5051 let () = Glut.passiveMotionFunc pmotion in
5053 setcheckers conf.checkers;
5054 init ssock (
5055 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5056 conf.texcount, conf.sliceheight, conf.mumemlimit, conf.colorspace,
5057 !Config.wmclasshack, !Config.fontpath
5059 state.csock <- csock;
5060 state.ssock <- ssock;
5061 state.text <- "Opening " ^ state.path;
5062 setaalevel conf.aalevel;
5063 writeopen state.path state.password;
5064 state.uioh <- uioh;
5066 while true do
5068 Glut.mainLoop ();
5069 with
5070 | Glut.BadEnum "key in special_of_int" ->
5071 showtext '!' " LablGlut bug: special key not recognized";
5073 | Quit ->
5074 wcmd "quit" [];
5075 Config.save ();
5076 exit 0
5077 done;