Track more info changes
[llpp.git] / main.ml
blob62744e9b03903e60b5c9b4206a6a822d5b226e70
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 rec enterinfomode =
2879 let btos b = if b then "\xe2\x88\x9a" else "" in
2880 let showextended = ref false in
2881 let reenter () =
2882 state.uioh <- state.uioh#key 27;
2883 enterinfomode ();
2885 let leave mode = function
2886 | Confirm -> state.mode <- mode
2887 | Cancel -> state.mode <- mode in
2888 let src =
2889 (object
2890 val mutable m_first_time = true
2891 val mutable m_l = []
2892 val mutable m_a = [||]
2893 val mutable m_prev_uioh = nouioh
2894 val mutable m_prev_mode = View
2896 inherit lvsourcebase
2898 method reset prev_mode prev_uioh =
2899 m_a <- Array.of_list (List.rev m_l);
2900 m_l <- [];
2901 m_prev_mode <- prev_mode;
2902 m_prev_uioh <- prev_uioh;
2903 if m_first_time
2904 then (
2905 let rec loop n =
2906 if n >= Array.length m_a
2907 then ()
2908 else
2909 match m_a.(n) with
2910 | _, _, _, Action _ -> m_active <- n
2911 | _ -> loop (n+1)
2913 loop 0;
2914 m_first_time <- false;
2917 method int name get set =
2918 m_l <-
2919 (name, `int get, 1, Action (
2920 fun u ->
2921 let ondone s =
2922 try set (int_of_string s)
2923 with exn ->
2924 state.text <- Printf.sprintf "bad integer `%s': %s"
2925 s (Printexc.to_string exn)
2927 state.text <- "";
2928 let te = name ^ ": ", "", None, intentry, ondone in
2929 state.mode <- Textentry (te, leave m_prev_mode);
2931 )) :: m_l
2933 method int_with_suffix name get set =
2934 m_l <-
2935 (name, `intws get, 1, Action (
2936 fun u ->
2937 let ondone s =
2938 try set (int_of_string_with_suffix s)
2939 with exn ->
2940 state.text <- Printf.sprintf "bad integer `%s': %s"
2941 s (Printexc.to_string exn)
2943 state.text <- "";
2944 let te =
2945 name ^ ": ", "", None, intentry_with_suffix, ondone
2947 state.mode <- Textentry (te, leave m_prev_mode);
2949 )) :: m_l
2951 method bool ?(offset=1) ?(btos=btos) name get set =
2952 m_l <-
2953 (name, `bool (btos, get), offset, Action (
2954 fun u ->
2955 let v = get () in
2956 set (not v);
2958 )) :: m_l
2960 method color name get set =
2961 m_l <-
2962 (name, `color get, 1, Action (
2963 fun u ->
2964 let invalid = (nan, nan, nan) in
2965 let ondone s =
2966 let c =
2967 try color_of_string s
2968 with exn ->
2969 state.text <- Printf.sprintf "bad color `%s': %s"
2970 s (Printexc.to_string exn);
2971 invalid
2973 if c <> invalid
2974 then set c;
2976 let te = name ^ ": ", "", None, textentry, ondone in
2977 state.text <- color_to_string (get ());
2978 state.mode <- Textentry (te, leave m_prev_mode);
2980 )) :: m_l
2982 method string name get set =
2983 m_l <-
2984 (name, `string get, 1, Action (
2985 fun u ->
2986 let ondone s = set s in
2987 let te = name ^ ": ", "", None, textentry, ondone in
2988 state.mode <- Textentry (te, leave m_prev_mode);
2990 )) :: m_l
2992 method colorspace name get set =
2993 m_l <-
2994 (name, `string get, 1, Action (
2995 fun _ ->
2996 let source =
2997 let vals = [| "rgb"; "bgr"; "gray" |] in
2998 (object
2999 inherit lvsourcebase
3001 initializer
3002 m_active <- int_of_colorspace conf.colorspace;
3003 m_first <- 0;
3005 method getitemcount = Array.length vals
3006 method getitem n = (vals.(n), 0)
3007 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3008 ignore (uioh, first, pan, qsearch);
3009 if not cancel then set active;
3010 None
3011 method hasaction _ = true
3012 end)
3014 state.text <- "";
3015 new listview ~source ~trusted:true
3016 )) :: m_l
3018 method caption s offset =
3019 m_l <- (s, `empty, offset, Noaction) :: m_l
3021 method caption2 s f offset =
3022 m_l <- (s, `string f, offset, Noaction) :: m_l
3024 method getitemcount = Array.length m_a
3026 method getitem n =
3027 let tostr = function
3028 | `int f -> string_of_int (f ())
3029 | `intws f -> string_with_suffix_of_int (f ())
3030 | `string f -> f ()
3031 | `color f -> color_to_string (f ())
3032 | `bool (btos, f) -> btos (f ())
3033 | `empty -> ""
3035 let name, t, offset, _ = m_a.(n) in
3036 ((let s = tostr t in
3037 if String.length s > 0
3038 then Printf.sprintf "%s\t%s" name s
3039 else name),
3040 offset)
3042 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3043 let uiohopt =
3044 if not cancel
3045 then (
3046 m_qsearch <- qsearch;
3047 let uioh =
3048 match m_a.(active) with
3049 | _, _, _, Action f -> f uioh
3050 | _ -> uioh
3052 Some uioh
3054 else None
3056 m_active <- active;
3057 m_first <- first;
3058 m_pan <- pan;
3059 uiohopt
3061 method hasaction n =
3062 match m_a.(n) with
3063 | _, _, _, Action _ -> true
3064 | _ -> false
3065 end)
3067 fun () ->
3068 let sep () = src#caption "" 0 in
3069 let colorp name get set =
3070 src#string name
3071 (fun () -> color_to_string (get ()))
3072 (fun v ->
3074 let c = color_of_string v in
3075 set c
3076 with exn ->
3077 state.text <- Printf.sprintf "bad color `%s': %s"
3078 v (Printexc.to_string exn);
3081 let oldmode = state.mode in
3082 let birdseye = isbirdseye state.mode in
3083 state.text <- "";
3085 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3087 src#bool "presentation mode"
3088 (fun () -> conf.presentation)
3089 (fun v ->
3090 conf.presentation <- v;
3091 state.anchor <- getanchor ();
3092 represent ());
3094 src#bool "ignore case in searches"
3095 (fun () -> conf.icase)
3096 (fun v -> conf.icase <- v);
3098 src#bool "preload"
3099 (fun () -> conf.preload)
3100 (fun v -> conf.preload <- v);
3102 src#bool "throttle"
3103 (fun () -> conf.showall)
3104 (fun v -> conf.showall <- v);
3106 src#bool "highlight links"
3107 (fun () -> conf.hlinks)
3108 (fun v -> conf.hlinks <- v);
3110 src#bool "under info"
3111 (fun () -> conf.underinfo)
3112 (fun v -> conf.underinfo <- v);
3114 src#bool "persistent bookmarks"
3115 (fun () -> conf.savebmarks)
3116 (fun v -> conf.savebmarks <- v);
3118 src#bool "proportional display"
3119 (fun () -> conf.proportional)
3120 (fun v -> reqlayout conf.angle v);
3122 src#bool "trim margins"
3123 (fun () -> conf.trimmargins)
3124 (fun v -> settrim v conf.trimfuzz; reenter ());
3126 src#bool "persistent location"
3127 (fun () -> conf.jumpback)
3128 (fun v -> conf.jumpback <- v);
3130 sep ();
3131 src#int "vertical margin"
3132 (fun () -> conf.interpagespace)
3133 (fun n ->
3134 conf.interpagespace <- n;
3135 let pageno, py =
3136 match state.layout with
3137 | [] -> 0, 0
3138 | l :: _ ->
3139 l.pageno, l.pagey
3141 state.maxy <- calcheight ();
3142 let y = getpagey pageno in
3143 gotoy (y + py)
3146 src#int "page bias"
3147 (fun () -> conf.pagebias)
3148 (fun v -> conf.pagebias <- v);
3150 src#int "scroll step"
3151 (fun () -> conf.scrollstep)
3152 (fun n -> conf.scrollstep <- n);
3154 src#int "auto scroll step"
3155 (fun () ->
3156 match state.autoscroll with
3157 | Some step -> step
3158 | _ -> conf.autoscrollstep)
3159 (fun n ->
3160 if state.autoscroll <> None
3161 then state.autoscroll <- Some n;
3162 conf.autoscrollstep <- n);
3164 src#int "zoom"
3165 (fun () -> truncate (conf.zoom *. 100.))
3166 (fun v -> setzoom ((float v) /. 100.));
3168 src#int "rotation"
3169 (fun () -> conf.angle)
3170 (fun v -> reqlayout v conf.proportional);
3172 src#int "scroll bar width"
3173 (fun () -> state.scrollw)
3174 (fun v ->
3175 state.scrollw <- v;
3176 conf.scrollbw <- v;
3177 reshape conf.winw conf.winh;
3180 src#int "scroll handle height"
3181 (fun () -> conf.scrollh)
3182 (fun v -> conf.scrollh <- v;);
3184 src#int "thumbnail width"
3185 (fun () -> conf.thumbw)
3186 (fun v ->
3187 conf.thumbw <- min 4096 v;
3188 match oldmode with
3189 | Birdseye beye ->
3190 leavebirdseye beye false;
3191 enterbirdseye ()
3192 | _ -> ()
3195 sep ();
3196 src#caption "Presentation mode" 0;
3197 src#bool "scrollbar visible"
3198 (fun () -> conf.scrollbarinpm)
3199 (fun v ->
3200 if v != conf.scrollbarinpm
3201 then (
3202 conf.scrollbarinpm <- v;
3203 if conf.presentation
3204 then (
3205 state.scrollw <- if v then conf.scrollbw else 0;
3206 reshape conf.winw conf.winh;
3211 sep ();
3212 src#caption "Pixmap cache" 0;
3213 src#int_with_suffix "size (advisory)"
3214 (fun () -> conf.memlimit)
3215 (fun v -> conf.memlimit <- v);
3217 src#caption2 "used"
3218 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3219 (string_with_suffix_of_int state.memused)
3220 (Hashtbl.length state.tilemap)) 1;
3222 sep ();
3223 src#caption "Layout" 0;
3224 src#caption2 "Dimension"
3225 (fun () ->
3226 Printf.sprintf "%dx%d (virtual %dx%d)"
3227 conf.winw conf.winh
3228 state.w state.maxy)
3230 if conf.debug
3231 then
3232 src#caption2 "Position" (fun () ->
3233 Printf.sprintf "%dx%d" state.x state.y
3235 else
3236 src#caption2 "Visible" (fun () -> describe_location ()) 1
3239 sep ();
3240 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3241 "Save these parameters as global defaults at exit"
3242 (fun () -> conf.bedefault)
3243 (fun v -> conf.bedefault <- v)
3246 sep ();
3247 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3248 src#bool ~offset:0 ~btos "Extended parameters"
3249 (fun () -> !showextended)
3250 (fun v -> showextended := v; reenter ());
3251 if !showextended
3252 then (
3253 src#bool "checkers"
3254 (fun () -> conf.checkers)
3255 (fun v -> conf.checkers <- v; setcheckers v);
3256 src#bool "verbose"
3257 (fun () -> conf.verbose)
3258 (fun v -> conf.verbose <- v);
3259 src#bool "invert colors"
3260 (fun () -> conf.invert)
3261 (fun v -> conf.invert <- v);
3262 src#bool "max fit"
3263 (fun () -> conf.maxhfit)
3264 (fun v -> conf.maxhfit <- v);
3265 src#string "uri launcher"
3266 (fun () -> conf.urilauncher)
3267 (fun v -> conf.urilauncher <- v);
3268 src#string "tile size"
3269 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3270 (fun v ->
3272 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3273 conf.tileh <- max 64 w;
3274 conf.tilew <- max 64 h;
3275 flushtiles ();
3276 with exn ->
3277 state.text <- Printf.sprintf "bad tile size `%s': %s"
3278 v (Printexc.to_string exn));
3279 src#int "anti-aliasing level"
3280 (fun () -> conf.aalevel)
3281 (fun v ->
3282 conf.aalevel <- bound v 0 8;
3283 state.anchor <- getanchor ();
3284 opendoc state.path state.password;
3286 src#int "ui font size"
3287 (fun () -> !uifontsize)
3288 (fun v -> uifontsize := bound v 5 100);
3289 colorp "background color"
3290 (fun () -> conf.bgcolor)
3291 (fun v -> conf.bgcolor <- v);
3292 src#bool "crop hack"
3293 (fun () -> conf.crophack)
3294 (fun v -> conf.crophack <- v);
3295 src#string "trim fuzz"
3296 (fun () -> irect_to_string conf.trimfuzz)
3297 (fun v ->
3299 conf.trimfuzz <- irect_of_string v;
3300 if conf.trimmargins
3301 then settrim true conf.trimfuzz;
3302 with exn ->
3303 state.text <- Printf.sprintf "bad irect `%s': %s"
3304 v (Printexc.to_string exn)
3306 src#colorspace "color space"
3307 (fun () -> colorspace_to_string conf.colorspace)
3308 (fun v ->
3309 conf.colorspace <- colorspace_of_int v;
3310 wcmd "cs" [`i v];
3311 load state.layout;
3315 sep ();
3316 src#caption "Document" 0;
3317 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3318 if conf.trimmargins
3319 then (
3320 sep ();
3321 src#caption "Trimmed margins" 0;
3322 src#caption2 "Dimensions"
3323 (fun () -> string_of_int (List.length state.pdims)) 1;
3326 src#reset state.mode state.uioh;
3327 let source = (src :> lvsource) in
3328 state.uioh <- object
3329 inherit listview ~source ~trusted:true
3330 val mutable m_prevmemused = 0
3331 method infochanged = function
3332 | Memused ->
3333 if m_prevmemused != state.memused
3334 then (
3335 m_prevmemused <- state.memused;
3336 G.postRedisplay "memusedchanged";
3338 | Pdim -> G.postRedisplay "pdimchanged"
3339 | Docinfo -> reenter ()
3340 end;
3341 G.postRedisplay "info";
3344 let enterhelpmode =
3345 let source =
3346 (object
3347 inherit lvsourcebase
3348 method getitemcount = Array.length state.help
3349 method getitem n =
3350 let s, n, _ = state.help.(n) in
3351 (s, n)
3353 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3354 let optuioh =
3355 if not cancel
3356 then (
3357 m_qsearch <- qsearch;
3358 match state.help.(active) with
3359 | _, _, Action f -> Some (f uioh)
3360 | _ -> Some (uioh)
3362 else None
3364 m_active <- active;
3365 m_first <- first;
3366 m_pan <- pan;
3367 optuioh
3369 method hasaction n =
3370 match state.help.(n) with
3371 | _, _, Action _ -> true
3372 | _ -> false
3374 initializer
3375 m_active <- -1
3376 end)
3377 in fun () ->
3378 state.uioh <- new listview ~source ~trusted:true;
3379 G.postRedisplay "help";
3382 let quickbookmark ?title () =
3383 match state.layout with
3384 | [] -> ()
3385 | l :: _ ->
3386 let title =
3387 match title with
3388 | None ->
3389 let sec = Unix.gettimeofday () in
3390 let tm = Unix.localtime sec in
3391 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
3392 (l.pageno+1)
3393 tm.Unix.tm_mday
3394 tm.Unix.tm_mon
3395 (tm.Unix.tm_year + 1900)
3396 tm.Unix.tm_hour
3397 tm.Unix.tm_min
3398 | Some title -> title
3400 state.bookmarks <-
3401 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
3402 :: state.bookmarks
3405 let doreshape w h =
3406 state.fullscreen <- None;
3407 Glut.reshapeWindow w h;
3410 let viewkeyboard key =
3411 let enttext te =
3412 let mode = state.mode in
3413 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3414 state.text <- "";
3415 enttext ();
3416 G.postRedisplay "view:enttext"
3418 let c = Char.chr key in
3419 match c with
3420 | '\027' | 'q' -> (* escape *)
3421 begin match state.mstate with
3422 | Mzoomrect _ ->
3423 state.mstate <- Mnone;
3424 Glut.setCursor Glut.CURSOR_INHERIT;
3425 G.postRedisplay "kill zoom rect";
3426 | _ ->
3427 raise Quit
3428 end;
3430 | '\008' -> (* backspace *)
3431 let y = getnav ~-1 in
3432 gotoy_and_clear_text y
3434 | 'o' ->
3435 enteroutlinemode ()
3437 | 'u' ->
3438 state.rects <- [];
3439 state.text <- "";
3440 G.postRedisplay "dehighlight";
3442 | '/' | '?' ->
3443 let ondone isforw s =
3444 cbput state.hists.pat s;
3445 state.searchpattern <- s;
3446 search s isforw
3448 let s = String.create 1 in
3449 s.[0] <- c;
3450 enttext (s, "", Some (onhist state.hists.pat),
3451 textentry, ondone (c ='/'))
3453 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3454 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3455 setzoom (conf.zoom +. incr)
3457 | '+' ->
3458 let ondone s =
3459 let n =
3460 try int_of_string s with exc ->
3461 state.text <- Printf.sprintf "bad integer `%s': %s"
3462 s (Printexc.to_string exc);
3463 max_int
3465 if n != max_int
3466 then (
3467 conf.pagebias <- n;
3468 state.text <- "page bias is now " ^ string_of_int n;
3471 enttext ("page bias: ", "", None, intentry, ondone)
3473 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3474 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3475 setzoom (max 0.01 (conf.zoom -. decr))
3477 | '-' ->
3478 let ondone msg = state.text <- msg in
3479 enttext (
3480 "option [acfhilpstvAPRSZTI]: ", "", None,
3481 optentry state.mode, ondone
3484 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3485 setzoom 1.0
3487 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3488 let zoom = zoomforh conf.winw conf.winh state.scrollw in
3489 if zoom < 1.0
3490 then setzoom zoom
3492 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3493 togglebirdseye ()
3495 | '0' .. '9' ->
3496 let ondone s =
3497 let n =
3498 try int_of_string s with exc ->
3499 state.text <- Printf.sprintf "bad integer `%s': %s"
3500 s (Printexc.to_string exc);
3503 if n >= 0
3504 then (
3505 addnav ();
3506 cbput state.hists.pag (string_of_int n);
3507 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
3510 let pageentry text key =
3511 match Char.unsafe_chr key with
3512 | 'g' -> TEdone text
3513 | _ -> intentry text key
3515 let text = "x" in text.[0] <- c;
3516 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
3518 | 'b' ->
3519 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
3520 reshape conf.winw conf.winh;
3522 | 'l' ->
3523 conf.hlinks <- not conf.hlinks;
3524 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3525 G.postRedisplay "toggle highlightlinks";
3527 | 'a' ->
3528 begin match state.autoscroll with
3529 | Some step ->
3530 conf.autoscrollstep <- step;
3531 state.autoscroll <- None
3532 | None ->
3533 if conf.autoscrollstep = 0
3534 then state.autoscroll <- Some 1
3535 else state.autoscroll <- Some conf.autoscrollstep
3538 | 'P' ->
3539 conf.presentation <- not conf.presentation;
3540 if conf.presentation
3541 then (
3542 if not conf.scrollbarinpm
3543 then state.scrollw <- 0;
3545 else
3546 state.scrollw <- conf.scrollbw;
3548 showtext ' ' ("presentation mode " ^
3549 if conf.presentation then "on" else "off");
3550 state.anchor <- getanchor ();
3551 represent ()
3553 | 'f' ->
3554 begin match state.fullscreen with
3555 | None ->
3556 state.fullscreen <- Some (conf.winw, conf.winh);
3557 Glut.fullScreen ()
3558 | Some (w, h) ->
3559 state.fullscreen <- None;
3560 doreshape w h
3563 | 'g' ->
3564 gotoy_and_clear_text 0
3566 | 'G' ->
3567 gotopage1 (state.pagecount - 1) 0
3569 | 'n' ->
3570 search state.searchpattern true
3572 | 'p' | 'N' ->
3573 search state.searchpattern false
3575 | 't' ->
3576 begin match state.layout with
3577 | [] -> ()
3578 | l :: _ ->
3579 gotoy_and_clear_text (getpagey l.pageno)
3582 | ' ' ->
3583 begin match List.rev state.layout with
3584 | [] -> ()
3585 | l :: _ ->
3586 let pageno = min (l.pageno+1) (state.pagecount-1) in
3587 gotoy_and_clear_text (getpagey pageno)
3590 | '\127' -> (* del *)
3591 begin match state.layout with
3592 | [] -> ()
3593 | l :: _ ->
3594 let pageno = max 0 (l.pageno-1) in
3595 gotoy_and_clear_text (getpagey pageno)
3598 | '=' ->
3599 showtext ' ' (describe_location ());
3601 | 'w' ->
3602 begin match state.layout with
3603 | [] -> ()
3604 | l :: _ ->
3605 doreshape (l.pagew + state.scrollw) l.pageh;
3606 G.postRedisplay "w"
3609 | '\'' ->
3610 enterbookmarkmode ()
3612 | 'h' ->
3613 enterhelpmode ()
3615 | 'i' ->
3616 enterinfomode ()
3618 | 'm' ->
3619 let ondone s =
3620 match state.layout with
3621 | l :: _ ->
3622 state.bookmarks <-
3623 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
3624 :: state.bookmarks
3625 | _ -> ()
3627 enttext ("bookmark: ", "", None, textentry, ondone)
3629 | '~' ->
3630 quickbookmark ();
3631 showtext ' ' "Quick bookmark added";
3633 | 'z' ->
3634 begin match state.layout with
3635 | l :: _ ->
3636 let rect = getpdimrect l.pagedimno in
3637 let w, h =
3638 if conf.crophack
3639 then
3640 (truncate (1.8 *. (rect.(1) -. rect.(0))),
3641 truncate (1.2 *. (rect.(3) -. rect.(0))))
3642 else
3643 (truncate (rect.(1) -. rect.(0)),
3644 truncate (rect.(3) -. rect.(0)))
3646 let w = truncate ((float w)*.conf.zoom)
3647 and h = truncate ((float h)*.conf.zoom) in
3648 if w != 0 && h != 0
3649 then (
3650 state.anchor <- getanchor ();
3651 doreshape (w + state.scrollw) (h + conf.interpagespace)
3653 G.postRedisplay "z";
3655 | [] -> ()
3658 | '\000' -> (* ctrl-2 *)
3659 let maxw = getmaxw () in
3660 if maxw > 0.0
3661 then setzoom (maxw /. float conf.winw)
3663 | '<' | '>' ->
3664 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
3666 | '[' | ']' ->
3667 state.colorscale <-
3668 bound (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
3670 G.postRedisplay "brightness";
3672 | 'k' ->
3673 begin match state.mode with
3674 | Birdseye beye -> upbirdseye beye
3675 | _ -> gotoy (clamp (-conf.scrollstep))
3678 | 'j' ->
3679 begin match state.mode with
3680 | Birdseye beye -> downbirdseye beye
3681 | _ -> gotoy (clamp conf.scrollstep)
3684 | 'r' ->
3685 state.anchor <- getanchor ();
3686 opendoc state.path state.password
3688 | 'v' when conf.debug ->
3689 state.rects <- [];
3690 List.iter (fun l ->
3691 match getopaque l.pageno with
3692 | None -> ()
3693 | Some opaque ->
3694 let x0, y0, x1, y1 = pagebbox opaque in
3695 let a,b = float x0, float y0 in
3696 let c,d = float x1, float y0 in
3697 let e,f = float x1, float y1 in
3698 let h,j = float x0, float y1 in
3699 let rect = (a,b,c,d,e,f,h,j) in
3700 debugrect rect;
3701 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
3702 ) state.layout;
3703 G.postRedisplay "v";
3705 | _ ->
3706 vlog "huh? %d %c" key (Char.chr key);
3709 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
3710 match key with
3711 | 27 -> (* escape *)
3712 leavebirdseye beye true
3714 | 12 -> (* ctrl-l *)
3715 let y, h = getpageyh pageno in
3716 let top = (conf.winh - h) / 2 in
3717 gotoy (max 0 (y - top))
3719 | 13 -> (* enter *)
3720 leavebirdseye beye false
3722 | _ ->
3723 viewkeyboard key
3726 let keyboard ~key ~x ~y =
3727 ignore x;
3728 ignore y;
3729 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
3730 then wcmd "interrupt" []
3731 else state.uioh <- state.uioh#key key
3734 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
3735 match key with
3736 | Glut.KEY_UP -> upbirdseye beye
3737 | Glut.KEY_DOWN -> downbirdseye beye
3739 | Glut.KEY_PAGE_UP ->
3740 begin match state.layout with
3741 | l :: _ ->
3742 if l.pagey != 0
3743 then (
3744 state.mode <- Birdseye (
3745 conf, leftx, l.pageno, hooverpageno, anchor
3747 gotopage1 l.pageno 0;
3749 else (
3750 let layout = layout (state.y-conf.winh) conf.winh in
3751 match layout with
3752 | [] -> gotoy (clamp (-conf.winh))
3753 | l :: _ ->
3754 state.mode <- Birdseye (
3755 conf, leftx, l.pageno, hooverpageno, anchor
3757 gotopage1 l.pageno 0
3760 | [] -> gotoy (clamp (-conf.winh))
3761 end;
3763 | Glut.KEY_PAGE_DOWN ->
3764 begin match List.rev state.layout with
3765 | l :: _ ->
3766 let layout = layout (state.y + conf.winh) conf.winh in
3767 begin match layout with
3768 | [] ->
3769 let incr = l.pageh - l.pagevh in
3770 if incr = 0
3771 then (
3772 state.mode <-
3773 Birdseye (
3774 conf, leftx, state.pagecount - 1, hooverpageno, anchor
3776 G.postRedisplay "birdseye pagedown";
3778 else gotoy (clamp (incr + conf.interpagespace*2));
3780 | l :: _ ->
3781 state.mode <-
3782 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
3783 gotopage1 l.pageno 0;
3786 | [] -> gotoy (clamp conf.winh)
3787 end;
3789 | Glut.KEY_HOME ->
3790 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
3791 gotopage1 0 0
3793 | Glut.KEY_END ->
3794 let pageno = state.pagecount - 1 in
3795 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3796 if not (pagevisible state.layout pageno)
3797 then
3798 let h =
3799 match List.rev state.pdims with
3800 | [] -> conf.winh
3801 | (_, _, h, _) :: _ -> h
3803 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
3804 else G.postRedisplay "birdseye end";
3805 | _ -> ()
3808 let setautoscrollspeed step goingdown =
3809 let incr = max 1 ((abs step) / 2) in
3810 let incr = if goingdown then incr else -incr in
3811 let astep = step + incr in
3812 state.autoscroll <- Some astep;
3815 let special ~key ~x ~y =
3816 ignore x;
3817 ignore y;
3818 state.uioh <- state.uioh#special key
3821 let drawpage l =
3822 let color =
3823 match state.mode with
3824 | Textentry _ -> scalecolor 0.4
3825 | View -> scalecolor 1.0
3826 | Birdseye (_, _, pageno, hooverpageno, _) ->
3827 if l.pageno = hooverpageno
3828 then scalecolor 0.9
3829 else (
3830 if l.pageno = pageno
3831 then scalecolor 1.0
3832 else scalecolor 0.8
3835 drawtiles l color;
3836 begin match getopaque l.pageno with
3837 | Some opaque ->
3838 if tileready l l.pagex l.pagey
3839 then
3840 let x = l.pagedispx - l.pagex
3841 and y = l.pagedispy - l.pagey in
3842 postprocess opaque conf.hlinks x y;
3844 | _ -> ()
3845 end;
3848 let scrollph y =
3849 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3850 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3851 let sh = float conf.winh /. sh in
3852 let sh = max sh (float conf.scrollh) in
3854 let percent =
3855 if y = state.maxy
3856 then 1.0
3857 else float y /. float maxy
3859 let position = (float conf.winh -. sh) *. percent in
3861 let position =
3862 if position +. sh > float conf.winh
3863 then float conf.winh -. sh
3864 else position
3866 position, sh;
3869 let scrollpw x =
3870 let winw = conf.winw - state.scrollw - 1 in
3871 let fwinw = float winw in
3872 let sw =
3873 let sw = fwinw /. float state.w in
3874 let sw = fwinw *. sw in
3875 max sw (float conf.scrollh)
3877 let position, sw =
3878 let f = state.w+winw in
3879 let r = float (winw-x) /. float f in
3880 let p = fwinw *. r in
3881 p-.sw/.2., sw
3883 let sw =
3884 if position +. sw > fwinw
3885 then fwinw -. position
3886 else sw
3888 position, sw;
3891 let scrollindicator () =
3892 GlDraw.color (0.64 , 0.64, 0.64);
3893 GlDraw.rect
3894 (float (conf.winw - state.scrollw), 0.)
3895 (float conf.winw, float conf.winh)
3897 GlDraw.rect
3898 (0., float (conf.winh - state.hscrollh))
3899 (float (conf.winw - state.scrollw - 1), float conf.winh)
3901 GlDraw.color (0.0, 0.0, 0.0);
3903 let position, sh = scrollph state.y in
3904 GlDraw.rect
3905 (float (conf.winw - state.scrollw), position)
3906 (float conf.winw, position +. sh)
3908 let position, sw = scrollpw state.x in
3909 GlDraw.rect
3910 (position, float (conf.winh - state.hscrollh))
3911 (position +. sw, float conf.winh)
3915 let pagetranslatepoint l x y =
3916 let dy = y - l.pagedispy in
3917 let y = dy + l.pagey in
3918 let dx = x - l.pagedispx in
3919 let x = dx + l.pagex in
3920 (x, y);
3923 let showsel () =
3924 match state.mstate with
3925 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
3928 | Msel ((x0, y0), (x1, y1)) ->
3929 let rec loop = function
3930 | l :: ls ->
3931 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3932 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3933 then
3934 match getopaque l.pageno with
3935 | Some opaque ->
3936 let dx, dy = pagetranslatepoint l 0 0 in
3937 let x0 = x0 + dx
3938 and y0 = y0 + dy
3939 and x1 = x1 + dx
3940 and y1 = y1 + dy in
3941 GlMat.mode `modelview;
3942 GlMat.push ();
3943 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
3944 seltext opaque (x0, y0, x1, y1);
3945 GlMat.pop ();
3946 | _ -> ()
3947 else loop ls
3948 | [] -> ()
3950 loop state.layout
3953 let showrects () =
3954 Gl.enable `blend;
3955 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
3956 GlDraw.polygon_mode `both `fill;
3957 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3958 List.iter
3959 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
3960 List.iter (fun l ->
3961 if l.pageno = pageno
3962 then (
3963 let dx = float (l.pagedispx - l.pagex) in
3964 let dy = float (l.pagedispy - l.pagey) in
3965 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
3966 GlDraw.begins `quads;
3968 GlDraw.vertex2 (x0+.dx, y0+.dy);
3969 GlDraw.vertex2 (x1+.dx, y1+.dy);
3970 GlDraw.vertex2 (x2+.dx, y2+.dy);
3971 GlDraw.vertex2 (x3+.dx, y3+.dy);
3973 GlDraw.ends ();
3975 ) state.layout
3976 ) state.rects
3978 Gl.disable `blend;
3981 let display () =
3982 GlClear.color (scalecolor2 conf.bgcolor);
3983 GlClear.clear [`color];
3984 List.iter drawpage state.layout;
3985 showrects ();
3986 showsel ();
3987 scrollindicator ();
3988 state.uioh#display;
3989 begin match state.mstate with
3990 | Mzoomrect ((x0, y0), (x1, y1)) ->
3991 Gl.enable `blend;
3992 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
3993 GlDraw.polygon_mode `both `fill;
3994 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3995 GlDraw.rect (float x0, float y0)
3996 (float x1, float y1);
3997 Gl.disable `blend;
3998 | _ -> ()
3999 end;
4000 enttext ();
4001 Glut.swapBuffers ();
4004 let getunder x y =
4005 let rec f = function
4006 | l :: rest ->
4007 begin match getopaque l.pageno with
4008 | Some opaque ->
4009 let x0 = l.pagedispx in
4010 let x1 = x0 + l.pagevw in
4011 let y0 = l.pagedispy in
4012 let y1 = y0 + l.pagevh in
4013 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4014 then
4015 let px, py = pagetranslatepoint l x y in
4016 match whatsunder opaque px py with
4017 | Unone -> f rest
4018 | under -> under
4019 else f rest
4020 | _ ->
4021 f rest
4023 | [] -> Unone
4025 f state.layout
4028 let zoomrect x y x1 y1 =
4029 let x0 = min x x1
4030 and x1 = max x x1
4031 and y0 = min y y1 in
4032 gotoy (state.y + y0);
4033 state.anchor <- getanchor ();
4034 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4035 state.x <- state.x - x0;
4036 setzoom zoom;
4037 Glut.setCursor Glut.CURSOR_INHERIT;
4038 state.mstate <- Mnone;
4041 let scrollx x =
4042 let winw = conf.winw - state.scrollw - 1 in
4043 let s = float x /. float winw in
4044 let destx = truncate (float (state.w + winw) *. s) in
4045 state.x <- winw - destx;
4046 gotoy_and_clear_text state.y;
4047 state.mstate <- Mscrollx;
4050 let scrolly y =
4051 let s = float y /. float conf.winh in
4052 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4053 gotoy_and_clear_text desty;
4054 state.mstate <- Mscrolly;
4057 let viewmouse button bstate x y =
4058 match button with
4059 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4060 if Glut.getModifiers () land Glut.active_ctrl != 0
4061 then (
4062 match state.mstate with
4063 | Mzoom (oldn, i) ->
4064 if oldn = n
4065 then (
4066 if i = 2
4067 then
4068 let incr =
4069 match n with
4070 | 4 ->
4071 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4072 | _ ->
4073 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4075 let zoom = conf.zoom -. incr in
4076 setzoom zoom;
4077 state.mstate <- Mzoom (n, 0);
4078 else
4079 state.mstate <- Mzoom (n, i+1);
4081 else state.mstate <- Mzoom (n, 0)
4083 | _ -> state.mstate <- Mzoom (n, 0)
4085 else (
4086 match state.autoscroll with
4087 | Some step -> setautoscrollspeed step (n=4)
4088 | None ->
4089 let incr =
4090 if n = 3
4091 then -conf.scrollstep
4092 else conf.scrollstep
4094 let incr = incr * 2 in
4095 let y = clamp incr in
4096 gotoy_and_clear_text y
4099 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4100 if bstate = Glut.DOWN
4101 then (
4102 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4103 state.mstate <- Mpan (x, y)
4105 else
4106 state.mstate <- Mnone
4108 | Glut.RIGHT_BUTTON ->
4109 if bstate = Glut.DOWN
4110 then (
4111 Glut.setCursor Glut.CURSOR_CYCLE;
4112 let p = (x, y) in
4113 state.mstate <- Mzoomrect (p, p)
4115 else (
4116 match state.mstate with
4117 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4118 | _ ->
4119 Glut.setCursor Glut.CURSOR_INHERIT;
4120 state.mstate <- Mnone
4123 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4124 if bstate = Glut.DOWN
4125 then
4126 let position, sh = scrollph state.y in
4127 if y > truncate position && y < truncate (position +. sh)
4128 then state.mstate <- Mscrolly
4129 else scrolly y
4130 else
4131 state.mstate <- Mnone
4133 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4134 if bstate = Glut.DOWN
4135 then
4136 let position, sw = scrollpw state.x in
4137 if x > truncate position && x < truncate (position +. sw)
4138 then state.mstate <- Mscrollx
4139 else scrollx x
4140 else
4141 state.mstate <- Mnone
4143 | Glut.LEFT_BUTTON ->
4144 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4145 begin match dest with
4146 | Ulinkgoto (pageno, top) ->
4147 if pageno >= 0
4148 then (
4149 addnav ();
4150 gotopage1 pageno top;
4153 | Ulinkuri s ->
4154 gotouri s
4156 | Unone when bstate = Glut.DOWN ->
4157 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4158 state.mstate <- Mpan (x, y);
4160 | Unone | Utext _ ->
4161 if bstate = Glut.DOWN
4162 then (
4163 if conf.angle mod 360 = 0
4164 then (
4165 state.mstate <- Msel ((x, y), (x, y));
4166 G.postRedisplay "mouse select";
4169 else (
4170 match state.mstate with
4171 | Mnone -> ()
4173 | Mzoom _ | Mscrollx | Mscrolly ->
4174 state.mstate <- Mnone
4176 | Mzoomrect ((x0, y0), _) ->
4177 zoomrect x0 y0 x y
4179 | Mpan _ ->
4180 Glut.setCursor Glut.CURSOR_INHERIT;
4181 state.mstate <- Mnone
4183 | Msel ((_, y0), (_, y1)) ->
4184 let f l =
4185 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4186 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4187 then
4188 match getopaque l.pageno with
4189 | Some opaque ->
4190 copysel opaque
4191 | _ -> ()
4193 List.iter f state.layout;
4194 copysel ""; (* ugly *)
4195 Glut.setCursor Glut.CURSOR_INHERIT;
4196 state.mstate <- Mnone;
4200 | _ -> ()
4203 let birdseyemouse button bstate x y
4204 (conf, leftx, _, hooverpageno, anchor) =
4205 match button with
4206 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4207 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4208 let rec loop = function
4209 | [] -> ()
4210 | l :: rest ->
4211 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4212 && x > margin && x < margin + l.pagew
4213 then (
4214 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4216 else loop rest
4218 loop state.layout
4219 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4220 | _ -> ()
4223 let mouse bstate button x y =
4224 state.uioh <- state.uioh#button button bstate x y;
4227 let mouse ~button ~state ~x ~y = mouse state button x y;;
4229 let motion ~x ~y =
4230 state.uioh <- state.uioh#motion x y
4233 let pmotion ~x ~y =
4234 state.uioh <- state.uioh#pmotion x y;
4237 let uioh = object
4238 method display = ()
4240 method key key =
4241 begin match state.mode with
4242 | Textentry textentry -> textentrykeyboard key textentry
4243 | Birdseye birdseye -> birdseyekeyboard key birdseye
4244 | View -> viewkeyboard key
4245 end;
4246 state.uioh
4248 method special key =
4249 begin match state.mode with
4250 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4251 togglebirdseye ()
4253 | Birdseye vals ->
4254 birdseyespecial key vals
4256 | View when key = Glut.KEY_F1 ->
4257 enterhelpmode ()
4259 | View ->
4260 begin match state.autoscroll with
4261 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4262 setautoscrollspeed step (key = Glut.KEY_DOWN)
4264 | _ ->
4265 let y =
4266 match key with
4267 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4268 | Glut.KEY_UP ->
4269 if Glut.getModifiers () land Glut.active_ctrl != 0
4270 then
4271 if Glut.getModifiers () land Glut.active_shift != 0
4272 then (setzoom state.prevzoom; state.y)
4273 else clamp (-conf.winh/2)
4274 else clamp (-conf.scrollstep)
4275 | Glut.KEY_DOWN ->
4276 if Glut.getModifiers () land Glut.active_ctrl != 0
4277 then
4278 if Glut.getModifiers () land Glut.active_shift != 0
4279 then (setzoom state.prevzoom; state.y)
4280 else clamp (conf.winh/2)
4281 else clamp (conf.scrollstep)
4282 | Glut.KEY_PAGE_UP ->
4283 if Glut.getModifiers () land Glut.active_ctrl != 0
4284 then
4285 match state.layout with
4286 | [] -> state.y
4287 | l :: _ -> state.y - l.pagey
4288 else
4289 clamp (-conf.winh)
4290 | Glut.KEY_PAGE_DOWN ->
4291 if Glut.getModifiers () land Glut.active_ctrl != 0
4292 then
4293 match List.rev state.layout with
4294 | [] -> state.y
4295 | l :: _ -> getpagey l.pageno
4296 else
4297 clamp conf.winh
4298 | Glut.KEY_HOME ->
4299 addnav ();
4301 | Glut.KEY_END ->
4302 addnav ();
4303 state.maxy - (if conf.maxhfit then conf.winh else 0)
4305 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4306 Glut.getModifiers () land Glut.active_alt != 0 ->
4307 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4309 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4310 let dx =
4311 if Glut.getModifiers () land Glut.active_ctrl != 0
4312 then (conf.winw / 2)
4313 else 10
4315 state.x <- state.x - dx;
4316 state.y
4317 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4318 let dx =
4319 if Glut.getModifiers () land Glut.active_ctrl != 0
4320 then (conf.winw / 2)
4321 else 10
4323 state.x <- state.x + dx;
4324 state.y
4326 | _ -> state.y
4328 gotoy_and_clear_text y
4331 | Textentry te -> textentryspecial key te
4332 end;
4333 state.uioh
4335 method button button bstate x y =
4336 begin match state.mode with
4337 | View -> viewmouse button bstate x y
4338 | Birdseye beye -> birdseyemouse button bstate x y beye
4339 | Textentry _ -> ()
4340 end;
4341 state.uioh
4343 method motion x y =
4344 begin match state.mode with
4345 | Textentry _ -> ()
4346 | View | Birdseye _ ->
4347 match state.mstate with
4348 | Mzoom _ | Mnone -> ()
4350 | Mpan (x0, y0) ->
4351 let dx = x - x0
4352 and dy = y0 - y in
4353 state.mstate <- Mpan (x, y);
4354 if conf.zoom > 1.0 then state.x <- state.x + dx;
4355 let y = clamp dy in
4356 gotoy_and_clear_text y
4358 | Msel (a, _) ->
4359 state.mstate <- Msel (a, (x, y));
4360 G.postRedisplay "motion select";
4362 | Mscrolly ->
4363 let y = min conf.winh (max 0 y) in
4364 scrolly y
4366 | Mscrollx ->
4367 let x = min conf.winw (max 0 x) in
4368 scrollx x
4370 | Mzoomrect (p0, _) ->
4371 state.mstate <- Mzoomrect (p0, (x, y));
4372 G.postRedisplay "motion zoomrect";
4373 end;
4374 state.uioh
4376 method pmotion x y =
4377 begin match state.mode with
4378 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4379 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4380 let rec loop = function
4381 | [] ->
4382 if hooverpageno != -1
4383 then (
4384 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4385 G.postRedisplay "pmotion birdseye no hoover";
4387 | l :: rest ->
4388 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4389 && x > margin && x < margin + l.pagew
4390 then (
4391 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4392 G.postRedisplay "pmotion birdseye hoover";
4394 else loop rest
4396 loop state.layout
4398 | Textentry _ -> ()
4400 | View ->
4401 match state.mstate with
4402 | Mnone ->
4403 begin match getunder x y with
4404 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
4405 | Ulinkuri uri ->
4406 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
4407 Glut.setCursor Glut.CURSOR_INFO
4408 | Ulinkgoto (page, _) ->
4409 if conf.underinfo
4410 then showtext 'p' ("age: " ^ string_of_int (page+1));
4411 Glut.setCursor Glut.CURSOR_INFO
4412 | Utext s ->
4413 if conf.underinfo then showtext 'f' ("ont: " ^ s);
4414 Glut.setCursor Glut.CURSOR_TEXT
4417 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
4419 end;
4420 state.uioh
4422 method infochanged _ = ()
4423 end;;
4425 module Config =
4426 struct
4427 open Parser
4429 let fontpath = ref "";;
4430 let wmclasshack = ref false;;
4432 let unent s =
4433 let l = String.length s in
4434 let b = Buffer.create l in
4435 unent b s 0 l;
4436 Buffer.contents b;
4439 let home =
4441 match platform with
4442 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
4443 | _ -> Sys.getenv "HOME"
4444 with exn ->
4445 prerr_endline
4446 ("Can not determine home directory location: " ^
4447 Printexc.to_string exn);
4451 let config_of c attrs =
4452 let apply c k v =
4454 match k with
4455 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
4456 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
4457 | "case-insensitive-search" -> { c with icase = bool_of_string v }
4458 | "preload" -> { c with preload = bool_of_string v }
4459 | "page-bias" -> { c with pagebias = int_of_string v }
4460 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
4461 | "auto-scroll-step" ->
4462 { c with autoscrollstep = max 0 (int_of_string v) }
4463 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
4464 | "crop-hack" -> { c with crophack = bool_of_string v }
4465 | "throttle" -> { c with showall = bool_of_string v }
4466 | "highlight-links" -> { c with hlinks = bool_of_string v }
4467 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
4468 | "vertical-margin" ->
4469 { c with interpagespace = max 0 (int_of_string v) }
4470 | "zoom" ->
4471 let zoom = float_of_string v /. 100. in
4472 let zoom = max zoom 0.0 in
4473 { c with zoom = zoom }
4474 | "presentation" -> { c with presentation = bool_of_string v }
4475 | "rotation-angle" -> { c with angle = int_of_string v }
4476 | "width" -> { c with winw = max 20 (int_of_string v) }
4477 | "height" -> { c with winh = max 20 (int_of_string v) }
4478 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
4479 | "proportional-display" -> { c with proportional = bool_of_string v }
4480 | "pixmap-cache-size" ->
4481 { c with memlimit = max 2 (int_of_string_with_suffix v) }
4482 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
4483 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
4484 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
4485 | "persistent-location" -> { c with jumpback = bool_of_string v }
4486 | "background-color" -> { c with bgcolor = color_of_string v }
4487 | "scrollbar-in-presentation" ->
4488 { c with scrollbarinpm = bool_of_string v }
4489 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
4490 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
4491 | "memlimit" ->
4492 { c with mumemlimit = max 1024 (int_of_string_with_suffix v) }
4493 | "checkers" -> { c with checkers = bool_of_string v }
4494 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
4495 | "trim-margins" -> { c with trimmargins = bool_of_string v }
4496 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
4497 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
4498 | "uri-launcher" -> { c with urilauncher = unent v }
4499 | "color-space" -> { c with colorspace = colorspace_of_string v }
4500 | "invert-colors" -> { c with invert = bool_of_string v }
4501 | _ -> c
4502 with exn ->
4503 prerr_endline ("Error processing attribute (`" ^
4504 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
4507 let rec fold c = function
4508 | [] -> c
4509 | (k, v) :: rest ->
4510 let c = apply c k v in
4511 fold c rest
4513 fold c attrs;
4516 let fromstring f pos n v d =
4517 try f v
4518 with exn ->
4519 dolog "Error processing attribute (%S=%S) at %d\n%s"
4520 n v pos (Printexc.to_string exn)
4525 let bookmark_of attrs =
4526 let rec fold title page rely = function
4527 | ("title", v) :: rest -> fold v page rely rest
4528 | ("page", v) :: rest -> fold title v rely rest
4529 | ("rely", v) :: rest -> fold title page v rest
4530 | _ :: rest -> fold title page rely rest
4531 | [] -> title, page, rely
4533 fold "invalid" "0" "0" attrs
4536 let doc_of attrs =
4537 let rec fold path page rely pan = function
4538 | ("path", v) :: rest -> fold v page rely pan rest
4539 | ("page", v) :: rest -> fold path v rely pan rest
4540 | ("rely", v) :: rest -> fold path page v pan rest
4541 | ("pan", v) :: rest -> fold path page rely v rest
4542 | _ :: rest -> fold path page rely pan rest
4543 | [] -> path, page, rely, pan
4545 fold "" "0" "0" "0" attrs
4548 let setconf dst src =
4549 dst.scrollbw <- src.scrollbw;
4550 dst.scrollh <- src.scrollh;
4551 dst.icase <- src.icase;
4552 dst.preload <- src.preload;
4553 dst.pagebias <- src.pagebias;
4554 dst.verbose <- src.verbose;
4555 dst.scrollstep <- src.scrollstep;
4556 dst.maxhfit <- src.maxhfit;
4557 dst.crophack <- src.crophack;
4558 dst.autoscrollstep <- src.autoscrollstep;
4559 dst.showall <- src.showall;
4560 dst.hlinks <- src.hlinks;
4561 dst.underinfo <- src.underinfo;
4562 dst.interpagespace <- src.interpagespace;
4563 dst.zoom <- src.zoom;
4564 dst.presentation <- src.presentation;
4565 dst.angle <- src.angle;
4566 dst.winw <- src.winw;
4567 dst.winh <- src.winh;
4568 dst.savebmarks <- src.savebmarks;
4569 dst.memlimit <- src.memlimit;
4570 dst.proportional <- src.proportional;
4571 dst.texcount <- src.texcount;
4572 dst.sliceheight <- src.sliceheight;
4573 dst.thumbw <- src.thumbw;
4574 dst.jumpback <- src.jumpback;
4575 dst.bgcolor <- src.bgcolor;
4576 dst.scrollbarinpm <- src.scrollbarinpm;
4577 dst.tilew <- src.tilew;
4578 dst.tileh <- src.tileh;
4579 dst.mumemlimit <- src.mumemlimit;
4580 dst.checkers <- src.checkers;
4581 dst.aalevel <- src.aalevel;
4582 dst.trimmargins <- src.trimmargins;
4583 dst.trimfuzz <- src.trimfuzz;
4584 dst.urilauncher <- src.urilauncher;
4585 dst.colorspace <- src.colorspace;
4586 dst.invert <- src.invert;
4589 let get s =
4590 let h = Hashtbl.create 10 in
4591 let dc = { defconf with angle = defconf.angle } in
4592 let rec toplevel v t spos _ =
4593 match t with
4594 | Vdata | Vcdata | Vend -> v
4595 | Vopen ("llppconfig", _, closed) ->
4596 if closed
4597 then v
4598 else { v with f = llppconfig }
4599 | Vopen _ ->
4600 error "unexpected subelement at top level" s spos
4601 | Vclose _ -> error "unexpected close at top level" s spos
4603 and llppconfig v t spos _ =
4604 match t with
4605 | Vdata | Vcdata -> v
4606 | Vend -> error "unexpected end of input in llppconfig" s spos
4607 | Vopen ("defaults", attrs, closed) ->
4608 let c = config_of dc attrs in
4609 setconf dc c;
4610 if closed
4611 then v
4612 else { v with f = skip "defaults" (fun () -> v) }
4614 | Vopen ("ui-font", attrs, closed) ->
4615 let rec getsize size = function
4616 | [] -> size
4617 | ("size", v) :: rest ->
4618 let size =
4619 fromstring int_of_string spos "size" v !uifontsize in
4620 getsize size rest
4621 | l -> getsize size l
4623 uifontsize := getsize !uifontsize attrs;
4624 if closed
4625 then v
4626 else { v with f = uifont (Buffer.create 10) }
4628 | Vopen ("doc", attrs, closed) ->
4629 let pathent, spage, srely, span = doc_of attrs in
4630 let path = unent pathent
4631 and pageno = fromstring int_of_string spos "page" spage 0
4632 and rely = fromstring float_of_string spos "rely" srely 0.0
4633 and pan = fromstring int_of_string spos "pan" span 0 in
4634 let c = config_of dc attrs in
4635 let anchor = (pageno, rely) in
4636 if closed
4637 then (Hashtbl.add h path (c, [], pan, anchor); v)
4638 else { v with f = doc path pan anchor c [] }
4640 | Vopen _ ->
4641 error "unexpected subelement in llppconfig" s spos
4643 | Vclose "llppconfig" -> { v with f = toplevel }
4644 | Vclose _ -> error "unexpected close in llppconfig" s spos
4646 and uifont b v t spos epos =
4647 match t with
4648 | Vdata | Vcdata ->
4649 Buffer.add_substring b s spos (epos - spos);
4651 | Vopen (_, _, _) ->
4652 error "unexpected subelement in ui-font" s spos
4653 | Vclose "ui-font" ->
4654 if String.length !fontpath = 0
4655 then fontpath := Buffer.contents b;
4656 { v with f = llppconfig }
4657 | Vclose _ -> error "unexpected close in ui-font" s spos
4658 | Vend -> error "unexpected end of input in ui-font" s spos
4660 and doc path pan anchor c bookmarks v t spos _ =
4661 match t with
4662 | Vdata | Vcdata -> v
4663 | Vend -> error "unexpected end of input in doc" s spos
4664 | Vopen ("bookmarks", _, closed) ->
4665 if closed
4666 then v
4667 else { v with f = pbookmarks path pan anchor c bookmarks }
4669 | Vopen (_, _, _) ->
4670 error "unexpected subelement in doc" s spos
4672 | Vclose "doc" ->
4673 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
4674 { v with f = llppconfig }
4676 | Vclose _ -> error "unexpected close in doc" s spos
4678 and pbookmarks path pan anchor c bookmarks v t spos _ =
4679 match t with
4680 | Vdata | Vcdata -> v
4681 | Vend -> error "unexpected end of input in bookmarks" s spos
4682 | Vopen ("item", attrs, closed) ->
4683 let titleent, spage, srely = bookmark_of attrs in
4684 let page = fromstring int_of_string spos "page" spage 0
4685 and rely = fromstring float_of_string spos "rely" srely 0.0 in
4686 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
4687 if closed
4688 then { v with f = pbookmarks path pan anchor c bookmarks }
4689 else
4690 let f () = v in
4691 { v with f = skip "item" f }
4693 | Vopen _ ->
4694 error "unexpected subelement in bookmarks" s spos
4696 | Vclose "bookmarks" ->
4697 { v with f = doc path pan anchor c bookmarks }
4699 | Vclose _ -> error "unexpected close in bookmarks" s spos
4701 and skip tag f v t spos _ =
4702 match t with
4703 | Vdata | Vcdata -> v
4704 | Vend ->
4705 error ("unexpected end of input in skipped " ^ tag) s spos
4706 | Vopen (tag', _, closed) ->
4707 if closed
4708 then v
4709 else
4710 let f' () = { v with f = skip tag f } in
4711 { v with f = skip tag' f' }
4712 | Vclose ctag ->
4713 if tag = ctag
4714 then f ()
4715 else error ("unexpected close in skipped " ^ tag) s spos
4718 parse { f = toplevel; accu = () } s;
4719 h, dc;
4722 let do_load f ic =
4724 let len = in_channel_length ic in
4725 let s = String.create len in
4726 really_input ic s 0 len;
4727 f s;
4728 with
4729 | Parse_error (msg, s, pos) ->
4730 let subs = subs s pos in
4731 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
4732 failwith ("parse error: " ^ s)
4734 | exn ->
4735 failwith ("config load error: " ^ Printexc.to_string exn)
4738 let defconfpath =
4739 let dir =
4741 let dir = Filename.concat home ".config" in
4742 if Sys.is_directory dir then dir else home
4743 with _ -> home
4745 Filename.concat dir "llpp.conf"
4748 let confpath = ref defconfpath;;
4750 let load1 f =
4751 if Sys.file_exists !confpath
4752 then
4753 match
4754 (try Some (open_in_bin !confpath)
4755 with exn ->
4756 prerr_endline
4757 ("Error opening configuation file `" ^ !confpath ^ "': " ^
4758 Printexc.to_string exn);
4759 None
4761 with
4762 | Some ic ->
4763 begin try
4764 f (do_load get ic)
4765 with exn ->
4766 prerr_endline
4767 ("Error loading configuation from `" ^ !confpath ^ "': " ^
4768 Printexc.to_string exn);
4769 end;
4770 close_in ic;
4772 | None -> ()
4773 else
4774 f (Hashtbl.create 0, defconf)
4777 let load () =
4778 let f (h, dc) =
4779 let pc, pb, px, pa =
4781 Hashtbl.find h (Filename.basename state.path)
4782 with Not_found -> dc, [], 0, (0, 0.0)
4784 setconf defconf dc;
4785 setconf conf pc;
4786 state.bookmarks <- pb;
4787 state.x <- px;
4788 state.scrollw <- conf.scrollbw;
4789 if conf.jumpback
4790 then state.anchor <- pa;
4791 cbput state.hists.nav pa;
4793 load1 f
4796 let add_attrs bb always dc c =
4797 let ob s a b =
4798 if always || a != b
4799 then Printf.bprintf bb "\n %s='%b'" s a
4800 and oi s a b =
4801 if always || a != b
4802 then Printf.bprintf bb "\n %s='%d'" s a
4803 and oI s a b =
4804 if always || a != b
4805 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
4806 and oz s a b =
4807 if always || a <> b
4808 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
4809 and oc s a b =
4810 if always || a <> b
4811 then
4812 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
4813 and oC s a b =
4814 if always || a <> b
4815 then
4816 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
4817 and oR s a b =
4818 if always || a <> b
4819 then
4820 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
4821 and os s a b =
4822 if always || a <> b
4823 then
4824 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
4826 let w, h =
4827 if always
4828 then dc.winw, dc.winh
4829 else
4830 match state.fullscreen with
4831 | Some wh -> wh
4832 | None -> c.winw, c.winh
4834 let zoom, presentation, interpagespace, showall=
4835 if always
4836 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
4837 else
4838 match state.mode with
4839 | Birdseye (bc, _, _, _, _) ->
4840 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
4841 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
4843 oi "width" w dc.winw;
4844 oi "height" h dc.winh;
4845 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
4846 oi "scroll-handle-height" c.scrollh dc.scrollh;
4847 ob "case-insensitive-search" c.icase dc.icase;
4848 ob "preload" c.preload dc.preload;
4849 oi "page-bias" c.pagebias dc.pagebias;
4850 oi "scroll-step" c.scrollstep dc.scrollstep;
4851 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
4852 ob "max-height-fit" c.maxhfit dc.maxhfit;
4853 ob "crop-hack" c.crophack dc.crophack;
4854 ob "throttle" showall dc.showall;
4855 ob "highlight-links" c.hlinks dc.hlinks;
4856 ob "under-cursor-info" c.underinfo dc.underinfo;
4857 oi "vertical-margin" interpagespace dc.interpagespace;
4858 oz "zoom" zoom dc.zoom;
4859 ob "presentation" presentation dc.presentation;
4860 oi "rotation-angle" c.angle dc.angle;
4861 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
4862 ob "proportional-display" c.proportional dc.proportional;
4863 oI "pixmap-cache-size" c.memlimit dc.memlimit;
4864 oi "tex-count" c.texcount dc.texcount;
4865 oi "slice-height" c.sliceheight dc.sliceheight;
4866 oi "thumbnail-width" c.thumbw dc.thumbw;
4867 ob "persistent-location" c.jumpback dc.jumpback;
4868 oc "background-color" c.bgcolor dc.bgcolor;
4869 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
4870 oi "tile-width" c.tilew dc.tilew;
4871 oi "tile-height" c.tileh dc.tileh;
4872 oI "mupdf-memlimit" c.mumemlimit dc.mumemlimit;
4873 ob "checkers" c.checkers dc.checkers;
4874 oi "aalevel" c.aalevel dc.aalevel;
4875 ob "trim-margins" c.trimmargins dc.trimmargins;
4876 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
4877 os "uri-launcher" c.urilauncher dc.urilauncher;
4878 oC "color-space" c.colorspace dc.colorspace;
4879 ob "invert-colors" c.invert dc.invert;
4880 if always
4881 then ob "wmclass-hack" !wmclasshack false;
4884 let save () =
4885 let uifontsize = !uifontsize in
4886 let bb = Buffer.create 32768 in
4887 let f (h, dc) =
4888 let dc = if conf.bedefault then conf else dc in
4889 Buffer.add_string bb "<llppconfig>\n";
4891 if String.length !fontpath > 0
4892 then
4893 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
4894 uifontsize
4895 !fontpath
4896 else (
4897 if uifontsize <> 14
4898 then
4899 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
4902 Buffer.add_string bb "<defaults ";
4903 add_attrs bb true dc dc;
4904 Buffer.add_string bb "/>\n";
4906 let adddoc path pan anchor c bookmarks =
4907 if bookmarks == [] && c = dc && anchor = emptyanchor
4908 then ()
4909 else (
4910 Printf.bprintf bb "<doc path='%s'"
4911 (enent path 0 (String.length path));
4913 if anchor <> emptyanchor
4914 then (
4915 let n, y = anchor in
4916 Printf.bprintf bb " page='%d'" n;
4917 if y > 1e-6
4918 then
4919 Printf.bprintf bb " rely='%f'" y
4923 if pan != 0
4924 then Printf.bprintf bb " pan='%d'" pan;
4926 add_attrs bb false dc c;
4928 begin match bookmarks with
4929 | [] -> Buffer.add_string bb "/>\n"
4930 | _ ->
4931 Buffer.add_string bb ">\n<bookmarks>\n";
4932 List.iter (fun (title, _level, (page, rely)) ->
4933 Printf.bprintf bb
4934 "<item title='%s' page='%d'"
4935 (enent title 0 (String.length title))
4936 page
4938 if rely > 1e-6
4939 then
4940 Printf.bprintf bb " rely='%f'" rely
4942 Buffer.add_string bb "/>\n";
4943 ) bookmarks;
4944 Buffer.add_string bb "</bookmarks>\n</doc>\n";
4945 end;
4949 let pan =
4950 match state.mode with
4951 | Birdseye (_, pan, _, _, _) -> pan
4952 | _ -> state.x
4954 let basename = Filename.basename state.path in
4955 adddoc basename pan (getanchor ())
4956 { conf with
4957 autoscrollstep =
4958 match state.autoscroll with
4959 | Some step -> step
4960 | None -> conf.autoscrollstep }
4961 (if conf.savebmarks then state.bookmarks else []);
4963 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
4964 if basename <> path
4965 then adddoc path x y c bookmarks
4966 ) h;
4967 Buffer.add_string bb "</llppconfig>";
4969 load1 f;
4970 if Buffer.length bb > 0
4971 then
4973 let tmp = !confpath ^ ".tmp" in
4974 let oc = open_out_bin tmp in
4975 Buffer.output_buffer oc bb;
4976 close_out oc;
4977 Unix.rename tmp !confpath;
4978 with exn ->
4979 prerr_endline
4980 ("error while saving configuration: " ^ Printexc.to_string exn)
4982 end;;
4984 let () =
4985 Arg.parse
4986 (Arg.align
4987 [("-p", Arg.String (fun s -> state.password <- s) ,
4988 "<password> Set password");
4990 ("-f", Arg.String (fun s -> Config.fontpath := s),
4991 "<path> Set path to the user interface font");
4993 ("-c", Arg.String (fun s -> Config.confpath := s),
4994 "<path> Set path to the configuration file");
4996 ("-v", Arg.Unit (fun () ->
4997 Printf.printf
4998 "%s\nconfiguration path: %s\n"
4999 Help.version
5000 Config.defconfpath
5002 exit 0), " Print version and exit");
5005 (fun s -> state.path <- s)
5006 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5008 if String.length state.path = 0
5009 then (prerr_endline "file name missing"; exit 1);
5011 Config.load ();
5013 let _ = Glut.init Sys.argv in
5014 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5015 let () = Glut.initWindowSize conf.winw conf.winh in
5016 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5018 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5019 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5020 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5022 let csock, ssock =
5023 if not is_windows
5024 then
5025 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5026 else
5027 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5028 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5029 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5030 Unix.bind sock addr;
5031 Unix.listen sock 1;
5032 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5033 Unix.connect csock addr;
5034 let ssock, _ = Unix.accept sock in
5035 Unix.close sock;
5036 let opts sock =
5037 Unix.setsockopt sock Unix.TCP_NODELAY true;
5038 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5040 opts ssock;
5041 opts csock;
5042 ssock, csock
5045 let () = Glut.displayFunc display in
5046 let () = Glut.reshapeFunc reshape in
5047 let () = Glut.keyboardFunc keyboard in
5048 let () = Glut.specialFunc special in
5049 let () = Glut.idleFunc (Some idle) in
5050 let () = Glut.mouseFunc mouse in
5051 let () = Glut.motionFunc motion in
5052 let () = Glut.passiveMotionFunc pmotion in
5054 setcheckers conf.checkers;
5055 init ssock (
5056 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5057 conf.texcount, conf.sliceheight, conf.mumemlimit, conf.colorspace,
5058 !Config.wmclasshack, !Config.fontpath
5060 state.csock <- csock;
5061 state.ssock <- ssock;
5062 state.text <- "Opening " ^ state.path;
5063 setaalevel conf.aalevel;
5064 writeopen state.path state.password;
5065 state.uioh <- uioh;
5067 while true do
5069 Glut.mainLoop ();
5070 with
5071 | Glut.BadEnum "key in special_of_int" ->
5072 showtext '!' " LablGlut bug: special key not recognized";
5074 | Quit ->
5075 wcmd "quit" [];
5076 Config.save ();
5077 exit 0
5078 done;