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