Remove needless indirection
[llpp.git] / main.ml
blob2ca6d7416a3e8896d1f5f36f1243a72d0dd3fbf0
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 vlog "redisplay for %s" who;
514 Glut.postRedisplay ();
516 end;;
518 let addchar s c =
519 let b = Buffer.create (String.length s + 1) in
520 Buffer.add_string b s;
521 Buffer.add_char b c;
522 Buffer.contents b;
525 let colorspace_of_string s =
526 match String.lowercase s with
527 | "rgb" -> Rgb
528 | "bgr" -> Bgr
529 | "gray" -> Gray
530 | _ -> failwith "invalid colorspace"
533 let int_of_colorspace = function
534 | Rgb -> 0
535 | Bgr -> 1
536 | Gray -> 2
539 let colorspace_of_int = function
540 | 0 -> Rgb
541 | 1 -> Bgr
542 | 2 -> Gray
543 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
546 let colorspace_to_string = function
547 | Rgb -> "rgb"
548 | Bgr -> "bgr"
549 | Gray -> "gray"
552 let intentry_with_suffix text key =
553 let c = Char.unsafe_chr key in
554 match Char.lowercase c with
555 | '0' .. '9' ->
556 let text = addchar text c in
557 TEcont text
559 | 'k' | 'm' | 'g' ->
560 let text = addchar text c in
561 TEcont text
563 | _ ->
564 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
565 TEcont text
568 let writecmd fd s =
569 let len = String.length s in
570 let n = 4 + len in
571 let b = Buffer.create n in
572 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
573 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
574 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
575 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
576 Buffer.add_string b s;
577 let s' = Buffer.contents b in
578 let n' = Unix.write fd s' 0 n in
579 if n' != n then failwith "write failed";
582 let readcmd fd =
583 let s = "xxxx" in
584 let n = Unix.read fd s 0 4 in
585 if n != 4 then failwith "incomplete read(len)";
586 let len = 0
587 lor (Char.code s.[0] lsl 24)
588 lor (Char.code s.[1] lsl 16)
589 lor (Char.code s.[2] lsl 8)
590 lor (Char.code s.[3] lsl 0)
592 let s = String.create len in
593 let n = Unix.read fd s 0 len in
594 if n != len then failwith "incomplete read(data)";
598 let makecmd s l =
599 let b = Buffer.create 10 in
600 Buffer.add_string b s;
601 let rec combine = function
602 | [] -> b
603 | x :: xs ->
604 Buffer.add_char b ' ';
605 let s =
606 match x with
607 | `b b -> if b then "1" else "0"
608 | `s s -> s
609 | `i i -> string_of_int i
610 | `f f -> string_of_float f
611 | `I f -> string_of_int (truncate f)
613 Buffer.add_string b s;
614 combine xs;
616 combine l;
619 let wcmd s l =
620 let cmd = Buffer.contents (makecmd s l) in
621 writecmd state.csock cmd;
624 let calcips h =
625 if conf.presentation
626 then
627 let d = conf.winh - h in
628 max 0 ((d + 1) / 2)
629 else
630 conf.interpagespace
633 let calcheight () =
634 let rec f pn ph pi fh l =
635 match l with
636 | (n, _, h, _) :: rest ->
637 let ips = calcips h in
638 let fh =
639 if conf.presentation
640 then fh+ips
641 else (
642 if isbirdseye state.mode && pn = 0
643 then fh + ips
644 else fh
647 let fh = fh + ((n - pn) * (ph + pi)) in
648 f n h ips fh rest;
650 | [] ->
651 let inc =
652 if conf.presentation || (isbirdseye state.mode && pn = 0)
653 then 0
654 else -pi
656 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
657 max 0 fh
659 let fh = f 0 0 0 0 state.pdims in
663 let getpageyh pageno =
664 let rec f pn ph pi y l =
665 match l with
666 | (n, _, h, _) :: rest ->
667 let ips = calcips h in
668 if n >= pageno
669 then
670 let h = if n = pageno then h else ph in
671 if conf.presentation && n = pageno
672 then
673 y + (pageno - pn) * (ph + pi) + pi, h
674 else
675 y + (pageno - pn) * (ph + pi), h
676 else
677 let y = y + (if conf.presentation then pi else 0) in
678 let y = y + (n - pn) * (ph + pi) in
679 f n h ips y rest
681 | [] ->
682 y + (pageno - pn) * (ph + pi), ph
684 f 0 0 0 0 state.pdims
687 let getpagedim pageno =
688 let rec f ppdim l =
689 match l with
690 | (n, _, _, _) as pdim :: rest ->
691 if n >= pageno
692 then (if n = pageno then pdim else ppdim)
693 else f pdim rest
695 | [] -> ppdim
697 f (-1, -1, -1, -1) state.pdims
700 let getpageh pageno =
701 let _, _, h, _ = getpagedim pageno in
705 let getpagew pageno =
706 let _, w, _, _ = getpagedim pageno in
710 let getpagey pageno = fst (getpageyh pageno);;
712 let layout y sh =
713 let sh = sh - state.hscrollh in
714 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
715 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
716 match pdims with
717 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
718 let ips = calcips h in
719 let yinc =
720 if conf.presentation || (isbirdseye state.mode && pageno = 0)
721 then ips
722 else 0
724 (w, h, ips, xoff), rest, pdimno + 1, yinc
725 | _ ->
726 prev, pdims, pdimno, 0
728 let dy = dy + yinc in
729 let py = py + yinc in
730 if pageno = state.pagecount || dy >= sh
731 then
732 accu
733 else
734 let vy = y + dy in
735 if py + h <= vy - yinc
736 then
737 let py = py + h + ips in
738 let dy = max 0 (py - y) in
739 f ~pageno:(pageno+1)
740 ~pdimno
741 ~prev:curr
744 ~pdims:rest
745 ~accu
746 else
747 let pagey = vy - py in
748 let pagevh = h - pagey in
749 let pagevh = min (sh - dy) pagevh in
750 let off = if yinc > 0 then py - vy else 0 in
751 let py = py + h + ips in
752 let pagex, dx =
753 let xoff = xoff +
754 if state.w < conf.winw - state.scrollw
755 then (conf.winw - state.scrollw - state.w) / 2
756 else 0
758 let dispx = xoff + state.x in
759 if dispx < 0
760 then (-dispx, 0)
761 else (0, dispx)
763 let pagevw =
764 let lw = w - pagex in
765 min lw (conf.winw - state.scrollw)
767 let e =
768 { pageno = pageno
769 ; pagedimno = pdimno
770 ; pagew = w
771 ; pageh = h
772 ; pagex = pagex
773 ; pagey = pagey + off
774 ; pagevw = pagevw
775 ; pagevh = pagevh - off
776 ; pagedispx = dx
777 ; pagedispy = dy + off
780 let accu = e :: accu in
781 f ~pageno:(pageno+1)
782 ~pdimno
783 ~prev:curr
785 ~dy:(dy+pagevh+ips)
786 ~pdims:rest
787 ~accu
789 if state.invalidated = 0
790 then (
791 let accu =
793 ~pageno:0
794 ~pdimno:~-1
795 ~prev:(0,0,0,0)
796 ~py:0
797 ~dy:0
798 ~pdims:state.pdims
799 ~accu:[]
801 List.rev accu
803 else
807 let clamp incr =
808 let y = state.y + incr in
809 let y = max 0 y in
810 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
814 let getopaque pageno =
815 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
816 with Not_found -> None
819 let putopaque pageno opaque =
820 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
823 let itertiles l f =
824 let tilex = l.pagex mod conf.tilew in
825 let tiley = l.pagey mod conf.tileh in
827 let col = l.pagex / conf.tilew in
828 let row = l.pagey / conf.tileh in
830 let vw =
831 let a = l.pagew - l.pagex in
832 let b = conf.winw - state.scrollw in
833 min a b
834 and vh = l.pagevh in
836 let rec rowloop row y0 dispy h =
837 if h = 0
838 then ()
839 else (
840 let dh = conf.tileh - y0 in
841 let dh = min h dh in
842 let rec colloop col x0 dispx w =
843 if w = 0
844 then ()
845 else (
846 let dw = conf.tilew - x0 in
847 let dw = min w dw in
849 f col row dispx dispy x0 y0 dw dh;
850 colloop (col+1) 0 (dispx+dw) (w-dw)
853 colloop col tilex l.pagedispx vw;
854 rowloop (row+1) 0 (dispy+dh) (h-dh)
857 if vw > 0 && vh > 0
858 then rowloop row tiley l.pagedispy vh;
861 let gettileopaque l col row =
862 let key =
863 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
865 try Some (Hashtbl.find state.tilemap key)
866 with Not_found -> None
869 let puttileopaque l col row gen colorspace angle opaque size elapsed =
870 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
871 Hashtbl.add state.tilemap key (opaque, size, elapsed)
874 let drawtiles l color =
875 GlDraw.color color;
876 let f col row x y tilex tiley w h =
877 match gettileopaque l col row with
878 | Some (opaque, _, t) ->
879 let params = x, y, w, h, tilex, tiley in
880 if conf.invert
881 then (
882 Gl.enable `blend;
883 GlFunc.blend_func `zero `one_minus_src_color;
885 drawtile params opaque;
886 if conf.invert
887 then Gl.disable `blend;
888 if conf.debug
889 then (
890 let s = Printf.sprintf
891 "%d[%d,%d] %f sec"
892 l.pageno col row t
894 let w = measurestr !uifontsize s in
895 GlMisc.push_attrib [`current];
896 GlDraw.color (0.0, 0.0, 0.0);
897 GlDraw.rect
898 (float (x-2), float (y-2))
899 (float (x+2) +. w, float (y + !uifontsize + 2));
900 GlDraw.color (1.0, 1.0, 1.0);
901 drawstring !uifontsize x (y + !uifontsize - 1) s;
902 GlMisc.pop_attrib ();
905 | _ ->
906 let w =
907 let lw = conf.winw - state.scrollw - x in
908 min lw w
909 and h =
910 let lh = conf.winh - y in
911 min lh h
913 Gl.enable `texture_2d;
914 begin match state.texid with
915 | Some id ->
916 GlTex.bind_texture `texture_2d id;
917 let x0 = float x
918 and y0 = float y
919 and x1 = float (x+w)
920 and y1 = float (y+h) in
922 let tw = float w /. 64.0
923 and th = float h /. 64.0 in
924 let tx0 = float tilex /. 64.0
925 and ty0 = float tiley /. 64.0 in
926 let tx1 = tx0 +. tw
927 and ty1 = ty0 +. th in
928 GlDraw.begins `quads;
929 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
930 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
931 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
932 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
933 GlDraw.ends ();
935 Gl.disable `texture_2d;
936 | None ->
937 GlDraw.color (1.0, 1.0, 1.0);
938 GlDraw.rect
939 (float x, float y)
940 (float (x+w), float (y+h));
941 end;
942 if w > 128 && h > !uifontsize + 10
943 then (
944 GlDraw.color (0.0, 0.0, 0.0);
945 let c, r =
946 if conf.verbose
947 then (col*conf.tilew, row*conf.tileh)
948 else col, row
950 drawstring2 !uifontsize x y "Loading %d [%d,%d]" l.pageno c r;
952 GlDraw.color color;
954 itertiles l f
957 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
959 let tilevisible1 l x y =
960 let ax0 = l.pagex
961 and ax1 = l.pagex + l.pagevw
962 and ay0 = l.pagey
963 and ay1 = l.pagey + l.pagevh in
965 let bx0 = x
966 and by0 = y in
967 let bx1 = min (bx0 + conf.tilew) l.pagew
968 and by1 = min (by0 + conf.tileh) l.pageh in
970 let rx0 = max ax0 bx0
971 and ry0 = max ay0 by0
972 and rx1 = min ax1 bx1
973 and ry1 = min ay1 by1 in
975 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
976 nonemptyintersection
979 let tilevisible layout n x y =
980 let rec findpageinlayout = function
981 | l :: _ when l.pageno = n -> tilevisible1 l x y
982 | _ :: rest -> findpageinlayout rest
983 | [] -> false
985 findpageinlayout layout
988 let tileready l x y =
989 tilevisible1 l x y &&
990 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
993 let tilepage n p layout =
994 let rec loop = function
995 | l :: rest ->
996 if l.pageno = n
997 then
998 let f col row _ _ _ _ _ _ =
999 if state.currently = Idle
1000 then
1001 match gettileopaque l col row with
1002 | Some _ -> ()
1003 | None ->
1004 let x = col*conf.tilew
1005 and y = row*conf.tileh in
1006 let w =
1007 let w = l.pagew - x in
1008 min w conf.tilew
1010 let h =
1011 let h = l.pageh - y in
1012 min h conf.tileh
1014 wcmd "tile"
1015 [`s p
1016 ;`i x
1017 ;`i y
1018 ;`i w
1019 ;`i h
1021 state.currently <-
1022 Tiling (
1023 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1024 conf.tilew, conf.tileh
1027 itertiles l f;
1028 else
1029 loop rest
1031 | [] -> ()
1033 if state.invalidated = 0 then loop layout;
1036 let preloadlayout visiblepages =
1037 let presentation = conf.presentation in
1038 let interpagespace = conf.interpagespace in
1039 let maxy = state.maxy in
1040 conf.presentation <- false;
1041 conf.interpagespace <- 0;
1042 state.maxy <- calcheight ();
1043 let y =
1044 match visiblepages with
1045 | [] -> 0
1046 | l :: _ -> getpagey l.pageno + l.pagey
1048 let y = if y < conf.winh then 0 else y - conf.winh in
1049 let h = state.y - y + conf.winh*3 in
1050 let pages = layout y h in
1051 conf.presentation <- presentation;
1052 conf.interpagespace <- interpagespace;
1053 state.maxy <- maxy;
1054 pages
1057 let load pages =
1058 let rec loop pages =
1059 if state.currently != Idle
1060 then ()
1061 else
1062 match pages with
1063 | l :: rest ->
1064 begin match getopaque l.pageno with
1065 | None ->
1066 wcmd "page" [`i l.pageno; `i l.pagedimno];
1067 state.currently <- Loading (l, state.gen);
1068 | Some opaque ->
1069 tilepage l.pageno opaque pages;
1070 loop rest
1071 end;
1072 | _ -> ()
1074 if state.invalidated = 0 then loop pages
1077 let preload pages =
1078 load pages;
1079 if conf.preload && state.currently = Idle
1080 then load (preloadlayout pages);
1083 let layoutready layout =
1084 let rec fold all ls =
1085 all && match ls with
1086 | l :: rest ->
1087 let seen = ref false in
1088 let allvisible = ref true in
1089 let foo col row _ _ _ _ _ _ =
1090 seen := true;
1091 allvisible := !allvisible &&
1092 begin match gettileopaque l col row with
1093 | Some _ -> true
1094 | None -> false
1097 itertiles l foo;
1098 fold (!seen && !allvisible) rest
1099 | [] -> true
1101 let alltilesvisible = fold true layout in
1102 alltilesvisible;
1105 let gotoy y =
1106 let y = bound y 0 state.maxy in
1107 let y, layout, proceed =
1108 if conf.showall
1109 then
1110 match state.throttle with
1111 | None ->
1112 let layout = layout y conf.winh in
1113 let ready = layoutready layout in
1114 if not ready
1115 then (
1116 load layout;
1117 state.throttle <- Some (layout, y);
1119 else G.postRedisplay "gotoy showall (None)";
1120 y, layout, ready
1121 | Some _ -> -1, [], false
1122 else
1123 let layout = layout y conf.winh in
1124 if true || layoutready layout
1125 then G.postRedisplay "gotoy ready";
1126 y, layout, true
1128 if proceed
1129 then (
1130 state.y <- y;
1131 state.layout <- layout;
1132 begin match state.mode with
1133 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1134 if not (pagevisible layout pageno)
1135 then (
1136 match state.layout with
1137 | [] -> ()
1138 | l :: _ ->
1139 state.mode <- Birdseye (
1140 conf, leftx, l.pageno, hooverpageno, anchor
1143 | _ -> ()
1144 end;
1145 preload layout;
1149 let conttiling pageno opaque =
1150 tilepage pageno opaque
1151 (if conf.preload then preloadlayout state.layout else state.layout)
1154 let gotoy_and_clear_text y =
1155 gotoy y;
1156 if not conf.verbose then state.text <- "";
1159 let getanchor () =
1160 match state.layout with
1161 | [] -> emptyanchor
1162 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1165 let getanchory (n, top) =
1166 let y, h = getpageyh n in
1167 y + (truncate (top *. float h));
1170 let gotoanchor anchor =
1171 gotoy (getanchory anchor);
1174 let addnav () =
1175 cbput state.hists.nav (getanchor ());
1178 let getnav dir =
1179 let anchor = cbgetc state.hists.nav dir in
1180 getanchory anchor;
1183 let gotopage n top =
1184 let y, h = getpageyh n in
1185 gotoy_and_clear_text (y + (truncate (top *. float h)));
1188 let gotopage1 n top =
1189 let y = getpagey n in
1190 gotoy_and_clear_text (y + top);
1193 let invalidate () =
1194 state.layout <- [];
1195 state.pdims <- [];
1196 state.rects <- [];
1197 state.rects1 <- [];
1198 state.invalidated <- state.invalidated + 1;
1201 let writeopen path password =
1202 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1205 let opendoc path password =
1206 invalidate ();
1207 state.path <- path;
1208 state.password <- password;
1209 state.gen <- state.gen + 1;
1210 state.docinfo <- [];
1212 setaalevel conf.aalevel;
1213 writeopen path password;
1214 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1215 wcmd "geometry" [`i state.w; `i conf.winh];
1218 let scalecolor c =
1219 let c = c *. state.colorscale in
1220 (c, c, c);
1223 let scalecolor2 (r, g, b) =
1224 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
1227 let represent () =
1228 state.maxy <- calcheight ();
1229 state.hscrollh <-
1230 if state.w <= conf.winw - state.scrollw
1231 then 0
1232 else state.scrollw
1234 match state.mode with
1235 | Birdseye (_, _, pageno, _, _) ->
1236 let y, h = getpageyh pageno in
1237 let top = (conf.winh - h) / 2 in
1238 gotoy (max 0 (y - top))
1239 | _ -> gotoanchor state.anchor
1242 let reshape =
1243 let firsttime = ref true in
1244 fun ~w ~h ->
1245 GlDraw.viewport 0 0 w h;
1246 if state.invalidated = 0 && not !firsttime
1247 then state.anchor <- getanchor ();
1249 firsttime := false;
1250 conf.winw <- w;
1251 let w = truncate (float w *. conf.zoom) - state.scrollw in
1252 let w = max w 2 in
1253 state.w <- w;
1254 conf.winh <- h;
1255 GlMat.mode `modelview;
1256 GlMat.load_identity ();
1258 GlMat.mode `projection;
1259 GlMat.load_identity ();
1260 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1261 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1262 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1264 invalidate ();
1265 wcmd "geometry" [`i w; `i h];
1268 let enttext () =
1269 let len = String.length state.text in
1270 let drawstring s =
1271 let hscrollh =
1272 match state.mode with
1273 | View -> state.hscrollh
1274 | _ -> 0
1276 let rect x w =
1277 GlDraw.rect
1278 (x, float (conf.winh - (!uifontsize + 4) - hscrollh))
1279 (x+.w, float (conf.winh - hscrollh))
1282 let w = float (conf.winw - state.scrollw - 1) in
1283 if state.progress >= 0.0 && state.progress < 1.0
1284 then (
1285 GlDraw.color (0.3, 0.3, 0.3);
1286 let w1 = w *. state.progress in
1287 rect 0.0 w1;
1288 GlDraw.color (0.0, 0.0, 0.0);
1289 rect w1 (w-.w1)
1291 else (
1292 GlDraw.color (0.0, 0.0, 0.0);
1293 rect 0.0 w;
1296 GlDraw.color (1.0, 1.0, 1.0);
1297 drawstring !uifontsize
1298 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1300 match state.mode with
1301 | Textentry ((prefix, text, _, _, _), _) ->
1302 let s =
1303 if len > 0
1304 then
1305 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1306 else
1307 Printf.sprintf "%s%s_" prefix text
1309 drawstring s
1311 | _ ->
1312 if len > 0 then drawstring state.text
1315 let showtext c s =
1316 state.text <- Printf.sprintf "%c%s" c s;
1317 G.postRedisplay "showtext";
1320 let gctiles () =
1321 let len = Queue.length state.tilelru in
1322 let rec loop qpos =
1323 if state.memused <= conf.memlimit
1324 then ()
1325 else (
1326 if qpos < len
1327 then
1328 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1329 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1331 gen = state.gen
1332 && colorspace = conf.colorspace
1333 && angle = conf.angle
1334 && pagew = getpagew n
1335 && pageh = getpageh n
1336 && (
1337 let layout =
1338 if conf.preload
1339 then preloadlayout state.layout
1340 else state.layout
1342 let x = col*conf.tilew
1343 and y = row*conf.tileh in
1344 tilevisible layout n x y
1346 then Queue.push lruitem state.tilelru
1347 else (
1348 wcmd "freetile" [`s p];
1349 state.memused <- state.memused - s;
1350 state.uioh#memusedchanged;
1351 Hashtbl.remove state.tilemap k;
1353 loop (qpos+1)
1356 loop 0
1359 let flushtiles () =
1360 Queue.iter (fun (k, p, s) ->
1361 wcmd "freetile" [`s p];
1362 state.memused <- state.memused - s;
1363 state.uioh#memusedchanged;
1364 Hashtbl.remove state.tilemap k;
1365 ) state.tilelru;
1366 Queue.clear state.tilelru;
1367 load state.layout;
1370 let logcurrently = function
1371 | Idle -> dolog "Idle"
1372 | Loading (l, gen) ->
1373 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1374 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1375 dolog
1376 "Tiling %d[%d,%d] page=%s cs=%s angle"
1377 l.pageno col row pageopaque
1378 (colorspace_to_string colorspace)
1380 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1381 angle gen conf.angle state.gen
1382 tilew tileh
1383 conf.tilew conf.tileh
1385 | Outlining _ ->
1386 dolog "outlining"
1389 let act cmds =
1390 (* dolog "%S" cmds; *)
1391 let op, args =
1392 let spacepos =
1393 try String.index cmds ' '
1394 with Not_found -> -1
1396 if spacepos = -1
1397 then cmds, ""
1398 else
1399 let l = String.length cmds in
1400 let op = String.sub cmds 0 spacepos in
1401 op, begin
1402 if l - spacepos < 2 then ""
1403 else String.sub cmds (spacepos+1) (l-spacepos-1)
1406 match op with
1407 | "clear" ->
1408 state.pdims <- [];
1410 | "clearrects" ->
1411 state.rects <- state.rects1;
1412 G.postRedisplay "clearrects";
1414 | "continue" ->
1415 let n = Scanf.sscanf args "%u" (fun n -> n) in
1416 state.pagecount <- n;
1417 state.invalidated <- state.invalidated - 1;
1418 begin match state.currently with
1419 | Outlining l ->
1420 state.currently <- Idle;
1421 state.outlines <- Array.of_list (List.rev l)
1422 | _ -> ()
1423 end;
1424 if state.invalidated = 0
1425 then represent ();
1426 if not conf.showall
1427 then G.postRedisplay "continue";
1429 | "title" ->
1430 Glut.setWindowTitle args
1432 | "msg" ->
1433 showtext ' ' args
1435 | "vmsg" ->
1436 if conf.verbose
1437 then showtext ' ' args
1439 | "progress" ->
1440 let progress, text = Scanf.sscanf args "%f %n"
1441 (fun f pos ->
1442 f, String.sub args pos (String.length args - pos)
1445 state.text <- text;
1446 state.progress <- progress;
1447 G.postRedisplay "progress"
1449 | "firstmatch" ->
1450 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1451 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1452 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1453 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1455 let y = (getpagey pageno) + truncate y0 in
1456 addnav ();
1457 gotoy y;
1458 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1460 | "match" ->
1461 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1462 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1463 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1464 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1466 state.rects1 <-
1467 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1469 | "page" ->
1470 let pageopaque, t = Scanf.sscanf args "%s %f" (fun p t -> p, t) in
1471 begin match state.currently with
1472 | Loading (l, gen) ->
1473 vlog "page %d took %f sec" l.pageno t;
1474 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1475 begin match state.throttle with
1476 | None ->
1477 let preloadedpages =
1478 if conf.preload
1479 then preloadlayout state.layout
1480 else state.layout
1482 let evict () =
1483 let module IntSet =
1484 Set.Make (struct type t = int let compare = (-) end) in
1485 let set =
1486 List.fold_left (fun s l -> IntSet.add l.pageno s)
1487 IntSet.empty preloadedpages
1489 let evictedpages =
1490 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1491 if not (IntSet.mem pageno set)
1492 then (
1493 wcmd "freepage" [`s opaque];
1494 key :: accu
1496 else accu
1497 ) state.pagemap []
1499 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1501 evict ();
1502 state.currently <- Idle;
1503 if gen = state.gen
1504 then (
1505 tilepage l.pageno pageopaque state.layout;
1506 load state.layout;
1507 load preloadedpages;
1508 if pagevisible state.layout l.pageno
1509 && layoutready state.layout
1510 then G.postRedisplay "page";
1513 | Some (layout, _) ->
1514 state.currently <- Idle;
1515 tilepage l.pageno pageopaque layout;
1516 load state.layout
1517 end;
1519 | _ ->
1520 dolog "Inconsistent loading state";
1521 logcurrently state.currently;
1522 raise Quit;
1525 | "tile" ->
1526 let (x, y, opaque, size, t) =
1527 Scanf.sscanf args "%u %u %s %u %f"
1528 (fun x y p size t -> (x, y, p, size, t))
1530 begin match state.currently with
1531 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1532 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1534 if tilew != conf.tilew || tileh != conf.tileh
1535 then (
1536 wcmd "freetile" [`s opaque];
1537 state.currently <- Idle;
1538 load state.layout;
1540 else (
1541 puttileopaque l col row gen cs angle opaque size t;
1542 state.memused <- state.memused + size;
1543 state.uioh#memusedchanged;
1544 gctiles ();
1545 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1546 opaque, size) state.tilelru;
1548 state.currently <- Idle;
1549 if gen = state.gen
1550 && conf.colorspace = cs
1551 && conf.angle = angle
1552 && tilevisible state.layout l.pageno x y
1553 then conttiling l.pageno pageopaque;
1555 begin match state.throttle with
1556 | None ->
1557 preload state.layout;
1558 if gen = state.gen
1559 && conf.colorspace = cs
1560 && conf.angle = angle
1561 && tilevisible state.layout l.pageno x y
1562 then G.postRedisplay "tile nothrottle";
1564 | Some (layout, y) ->
1565 let ready = layoutready layout in
1566 if ready
1567 then (
1568 state.y <- y;
1569 state.layout <- layout;
1570 state.throttle <- None;
1571 G.postRedisplay "throttle";
1573 else load layout;
1574 end;
1577 | _ ->
1578 dolog "Inconsistent tiling state";
1579 logcurrently state.currently;
1580 raise Quit;
1583 | "pdim" ->
1584 let pdim =
1585 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1587 state.pdims <- pdim :: state.pdims
1589 | "o" ->
1590 let (l, n, t, h, pos) =
1591 Scanf.sscanf args "%u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
1593 let s = String.sub args pos (String.length args - pos) in
1594 let outline = (s, l, (n, float t /. float h)) in
1595 begin match state.currently with
1596 | Outlining outlines ->
1597 state.currently <- Outlining (outline :: outlines)
1598 | Idle ->
1599 state.currently <- Outlining [outline]
1600 | currently ->
1601 dolog "invalid outlining state";
1602 logcurrently currently
1605 | "info" ->
1606 state.docinfo <- (1, args) :: state.docinfo
1608 | "infoend" ->
1609 state.docinfo <- List.rev state.docinfo
1611 | _ ->
1612 dolog "unknown cmd `%S'" cmds
1615 let now = Unix.gettimeofday;;
1617 let idle () =
1618 if state.deadline == nan then state.deadline <- now ();
1619 let rec loop delay =
1620 let timeout =
1621 if delay > 0.0
1622 then max 0.0 (state.deadline -. now ())
1623 else 0.0
1625 let r, _, _ = Unix.select [state.csock] [] [] timeout in
1626 begin match r with
1627 | [] ->
1628 begin match state.autoscroll with
1629 | Some step when step != 0 ->
1630 let y = state.y + step in
1631 let y =
1632 if y < 0
1633 then state.maxy
1634 else if y >= state.maxy then 0 else y
1636 gotoy y;
1637 if state.mode = View
1638 then state.text <- "";
1639 state.deadline <- state.deadline +. 0.005;
1641 | _ ->
1642 state.deadline <- state.deadline +. delay;
1643 end;
1645 | _ ->
1646 let cmd = readcmd state.csock in
1647 act cmd;
1648 loop 0.0
1649 end;
1650 in loop 0.007
1653 let onhist cb =
1654 let rc = cb.rc in
1655 let action = function
1656 | HCprev -> cbget cb ~-1
1657 | HCnext -> cbget cb 1
1658 | HCfirst -> cbget cb ~-(cb.rc)
1659 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1660 and cancel () = cb.rc <- rc
1661 in (action, cancel)
1664 let search pattern forward =
1665 if String.length pattern > 0
1666 then
1667 let pn, py =
1668 match state.layout with
1669 | [] -> 0, 0
1670 | l :: _ ->
1671 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1673 let cmd =
1674 let b = makecmd "search"
1675 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1677 Buffer.add_char b ',';
1678 Buffer.add_string b pattern;
1679 Buffer.add_char b '\000';
1680 Buffer.contents b;
1682 writecmd state.csock cmd;
1685 let intentry text key =
1686 let c = Char.unsafe_chr key in
1687 match c with
1688 | '0' .. '9' ->
1689 let text = addchar text c in
1690 TEcont text
1692 | _ ->
1693 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1694 TEcont text
1697 let textentry text key =
1698 let c = Char.unsafe_chr key in
1699 match c with
1700 | _ when key >= 32 && key < 127 ->
1701 let text = addchar text c in
1702 TEcont text
1704 | _ ->
1705 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1706 TEcont text
1709 let reqlayout angle proportional =
1710 match state.throttle with
1711 | None ->
1712 if state.invalidated = 0 then state.anchor <- getanchor ();
1713 conf.angle <- angle mod 360;
1714 conf.proportional <- proportional;
1715 invalidate ();
1716 wcmd "reqlayout" [`i conf.angle; `b proportional];
1717 | _ -> ()
1720 let settrim trimmargins trimfuzz =
1721 if state.invalidated = 0 then state.anchor <- getanchor ();
1722 conf.trimmargins <- trimmargins;
1723 conf.trimfuzz <- trimfuzz;
1724 let x0, y0, x1, y1 = trimfuzz in
1725 invalidate ();
1726 wcmd "settrim" [
1727 `b conf.trimmargins;
1728 `i x0;
1729 `i y0;
1730 `i x1;
1731 `i y1;
1733 Hashtbl.iter (fun _ opaque ->
1734 wcmd "freepage" [`s opaque];
1735 ) state.pagemap;
1736 Hashtbl.clear state.pagemap;
1739 let setzoom zoom =
1740 match state.throttle with
1741 | None ->
1742 let zoom = max 0.01 zoom in
1743 if zoom <> conf.zoom
1744 then (
1745 state.prevzoom <- conf.zoom;
1746 let relx =
1747 if zoom <= 1.0
1748 then (state.x <- 0; 0.0)
1749 else float state.x /. float state.w
1751 conf.zoom <- zoom;
1752 reshape conf.winw conf.winh;
1753 if zoom > 1.0
1754 then (
1755 let x = relx *. float state.w in
1756 state.x <- truncate x;
1758 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1761 | _ -> ()
1764 let enterbirdseye () =
1765 let zoom = float conf.thumbw /. float conf.winw in
1766 let birdseyepageno =
1767 let cy = conf.winh / 2 in
1768 let fold = function
1769 | [] -> 0
1770 | l :: rest ->
1771 let rec fold best = function
1772 | [] -> best.pageno
1773 | l :: rest ->
1774 let d = cy - (l.pagedispy + l.pagevh/2)
1775 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1776 if abs d < abs dbest
1777 then fold l rest
1778 else best.pageno
1779 in fold l rest
1781 fold state.layout
1783 state.mode <- Birdseye (
1784 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1786 conf.zoom <- zoom;
1787 conf.presentation <- false;
1788 conf.interpagespace <- 10;
1789 conf.hlinks <- false;
1790 state.x <- 0;
1791 state.mstate <- Mnone;
1792 conf.showall <- false;
1793 Glut.setCursor Glut.CURSOR_INHERIT;
1794 if conf.verbose
1795 then
1796 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1797 (100.0*.zoom)
1798 else
1799 state.text <- ""
1801 reshape conf.winw conf.winh;
1804 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1805 state.mode <- View;
1806 conf.zoom <- c.zoom;
1807 conf.presentation <- c.presentation;
1808 conf.interpagespace <- c.interpagespace;
1809 conf.showall <- c.showall;
1810 conf.hlinks <- c.hlinks;
1811 state.x <- leftx;
1812 if conf.verbose
1813 then
1814 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1815 (100.0*.conf.zoom)
1817 reshape conf.winw conf.winh;
1818 state.anchor <- if goback then anchor else (pageno, 0.0);
1821 let togglebirdseye () =
1822 match state.mode with
1823 | Birdseye vals -> leavebirdseye vals true
1824 | View -> enterbirdseye ()
1825 | _ -> ()
1828 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1829 let pageno = max 0 (pageno - 1) in
1830 let rec loop = function
1831 | [] -> gotopage1 pageno 0
1832 | l :: _ when l.pageno = pageno ->
1833 if l.pagedispy >= 0 && l.pagey = 0
1834 then G.postRedisplay "upbirdseye"
1835 else gotopage1 pageno 0
1836 | _ :: rest -> loop rest
1838 loop state.layout;
1839 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1842 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1843 let pageno = min (state.pagecount - 1) (pageno + 1) in
1844 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1845 let rec loop = function
1846 | [] ->
1847 let y, h = getpageyh pageno in
1848 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1849 gotoy (clamp dy)
1850 | l :: _ when l.pageno = pageno ->
1851 if l.pagevh != l.pageh
1852 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1853 else G.postRedisplay "downbirdseye"
1854 | _ :: rest -> loop rest
1856 loop state.layout
1859 let optentry mode _ key =
1860 let btos b = if b then "on" else "off" in
1861 let c = Char.unsafe_chr key in
1862 match c with
1863 | 's' ->
1864 let ondone s =
1865 try conf.scrollstep <- int_of_string s with exc ->
1866 state.text <- Printf.sprintf "bad integer `%s': %s"
1867 s (Printexc.to_string exc)
1869 TEswitch ("scroll step: ", "", None, intentry, ondone)
1871 | 'A' ->
1872 let ondone s =
1874 conf.autoscrollstep <- int_of_string s;
1875 if state.autoscroll <> None
1876 then state.autoscroll <- Some conf.autoscrollstep
1877 with exc ->
1878 state.text <- Printf.sprintf "bad integer `%s': %s"
1879 s (Printexc.to_string exc)
1881 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1883 | 'Z' ->
1884 let ondone s =
1886 let zoom = float (int_of_string s) /. 100.0 in
1887 setzoom zoom
1888 with exc ->
1889 state.text <- Printf.sprintf "bad integer `%s': %s"
1890 s (Printexc.to_string exc)
1892 TEswitch ("zoom: ", "", None, intentry, ondone)
1894 | 't' ->
1895 let ondone s =
1897 conf.thumbw <- bound (int_of_string s) 2 4096;
1898 state.text <-
1899 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1900 begin match mode with
1901 | Birdseye beye ->
1902 leavebirdseye beye false;
1903 enterbirdseye ();
1904 | _ -> ();
1906 with exc ->
1907 state.text <- Printf.sprintf "bad integer `%s': %s"
1908 s (Printexc.to_string exc)
1910 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
1912 | 'R' ->
1913 let ondone s =
1914 match try
1915 Some (int_of_string s)
1916 with exc ->
1917 state.text <- Printf.sprintf "bad integer `%s': %s"
1918 s (Printexc.to_string exc);
1919 None
1920 with
1921 | Some angle -> reqlayout angle conf.proportional
1922 | None -> ()
1924 TEswitch ("rotation: ", "", None, intentry, ondone)
1926 | 'i' ->
1927 conf.icase <- not conf.icase;
1928 TEdone ("case insensitive search " ^ (btos conf.icase))
1930 | 'p' ->
1931 conf.preload <- not conf.preload;
1932 gotoy state.y;
1933 TEdone ("preload " ^ (btos conf.preload))
1935 | 'v' ->
1936 conf.verbose <- not conf.verbose;
1937 TEdone ("verbose " ^ (btos conf.verbose))
1939 | 'd' ->
1940 conf.debug <- not conf.debug;
1941 TEdone ("debug " ^ (btos conf.debug))
1943 | 'h' ->
1944 conf.maxhfit <- not conf.maxhfit;
1945 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1946 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1948 | 'c' ->
1949 conf.crophack <- not conf.crophack;
1950 TEdone ("crophack " ^ btos conf.crophack)
1952 | 'a' ->
1953 conf.showall <- not conf.showall;
1954 TEdone ("throttle " ^ btos conf.showall)
1956 | 'f' ->
1957 conf.underinfo <- not conf.underinfo;
1958 TEdone ("underinfo " ^ btos conf.underinfo)
1960 | 'P' ->
1961 conf.savebmarks <- not conf.savebmarks;
1962 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1964 | 'S' ->
1965 let ondone s =
1967 let pageno, py =
1968 match state.layout with
1969 | [] -> 0, 0
1970 | l :: _ ->
1971 l.pageno, l.pagey
1973 conf.interpagespace <- int_of_string s;
1974 state.maxy <- calcheight ();
1975 let y = getpagey pageno in
1976 gotoy (y + py)
1977 with exc ->
1978 state.text <- Printf.sprintf "bad integer `%s': %s"
1979 s (Printexc.to_string exc)
1981 TEswitch ("vertical margin: ", "", None, intentry, ondone)
1983 | 'l' ->
1984 reqlayout conf.angle (not conf.proportional);
1985 TEdone ("proportional display " ^ btos conf.proportional)
1987 | 'T' ->
1988 settrim (not conf.trimmargins) conf.trimfuzz;
1989 TEdone ("trim margins " ^ btos conf.trimmargins)
1991 | 'I' ->
1992 conf.invert <- not conf.invert;
1993 TEdone ("invert colors " ^ btos conf.invert)
1995 | _ ->
1996 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1997 TEstop
2000 let maxoutlinerows () = (conf.winh - !uifontsize - 1) / (!uifontsize + 1);;
2002 class type lvsource = object
2003 method getitemcount : int
2004 method getitem : int -> (string * int)
2005 method hasaction : int -> bool
2006 method exit :
2007 uioh:uioh ->
2008 cancel:bool ->
2009 active:int ->
2010 first:int ->
2011 pan:int ->
2012 qsearch:string ->
2013 uioh option
2014 method getactive : int
2015 method getfirst : int
2016 method getqsearch : string
2017 method setqsearch : string -> unit
2018 method getpan : int
2019 end;;
2021 class virtual lvsourcebase = object
2022 val mutable m_active = 0
2023 val mutable m_first = 0
2024 val mutable m_qsearch = ""
2025 val mutable m_pan = 0
2026 method getactive = m_active
2027 method getfirst = m_first
2028 method getqsearch = m_qsearch
2029 method getpan = m_pan
2030 method setqsearch s = m_qsearch <- s
2031 end;;
2033 let textentryspecial key = function
2034 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2035 let s =
2036 match key with
2037 | Glut.KEY_UP -> action HCprev
2038 | Glut.KEY_DOWN -> action HCnext
2039 | Glut.KEY_HOME -> action HCfirst
2040 | Glut.KEY_END -> action HClast
2041 | _ -> state.text
2043 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2044 G.postRedisplay "special textentry";
2045 | _ -> ()
2048 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2049 let enttext te =
2050 state.mode <- Textentry (te, onleave);
2051 state.text <- "";
2052 enttext ();
2053 G.postRedisplay "textentrykeyboard enttext";
2055 match Char.unsafe_chr key with
2056 | '\008' -> (* backspace *)
2057 let len = String.length text in
2058 if len = 0
2059 then (
2060 onleave Cancel;
2061 G.postRedisplay "textentrykeyboard after cancel";
2063 else (
2064 let s = String.sub text 0 (len - 1) in
2065 enttext (c, s, opthist, onkey, ondone)
2068 | '\r' | '\n' ->
2069 ondone text;
2070 onleave Confirm;
2071 G.postRedisplay "textentrykeyboard after confirm"
2073 | '\007' (* ctrl-g *)
2074 | '\027' -> (* escape *)
2075 if String.length text = 0
2076 then (
2077 begin match opthist with
2078 | None -> ()
2079 | Some (_, onhistcancel) -> onhistcancel ()
2080 end;
2081 onleave Cancel;
2082 state.text <- "";
2083 G.postRedisplay "textentrykeyboard after cancel2"
2085 else (
2086 enttext (c, "", opthist, onkey, ondone)
2089 | '\127' -> () (* delete *)
2091 | _ ->
2092 begin match onkey text key with
2093 | TEdone text ->
2094 ondone text;
2095 onleave Confirm;
2096 G.postRedisplay "textentrykeyboard after confirm2";
2098 | TEcont text ->
2099 enttext (c, text, opthist, onkey, ondone);
2101 | TEstop ->
2102 onleave Cancel;
2103 state.text <- "";
2104 G.postRedisplay "textentrykeyboard after cancel3"
2106 | TEswitch te ->
2107 state.mode <- Textentry (te, onleave);
2108 G.postRedisplay "textentrykeyboard switch";
2109 end;
2112 let firstof first active =
2113 let maxrows = maxoutlinerows () in
2114 if first > active || abs (first - active) > maxrows - 1
2115 then max 0 (active - (maxrows/2))
2116 else first
2119 class listview ~(source:lvsource) ~trusted =
2120 let coe s = (s :> uioh) in
2121 object (self)
2122 val m_pan = source#getpan
2123 val m_first = source#getfirst
2124 val m_active = source#getactive
2125 val m_qsearch = source#getqsearch
2126 val m_prev_uioh = state.uioh
2128 method private elemunder y =
2129 let n = y / (!uifontsize+1) in
2130 if m_first + n < source#getitemcount
2131 then (
2132 if source#hasaction (m_first + n)
2133 then Some (m_first + n)
2134 else None
2136 else None
2138 method display =
2139 Gl.enable `blend;
2140 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2141 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2142 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2143 GlDraw.color (1., 1., 1.);
2144 Gl.enable `texture_2d;
2145 let fs = !uifontsize in
2146 let nfs = fs + 1 in
2147 let wx = measurestr fs "w" in
2148 let tabx = 30.0*.wx +. float (m_pan * fs) in
2149 let rec loop row =
2150 if (row - m_first) * nfs > conf.winh
2151 then ()
2152 else (
2153 if row >= 0 && row < source#getitemcount
2154 then (
2155 let (s, level) = source#getitem row in
2156 let y = (row - m_first) * nfs in
2157 let x = 5 + fs*(max 0 (level+m_pan)) in
2158 if row = m_active
2159 then (
2160 Gl.disable `texture_2d;
2161 GlDraw.polygon_mode `both `line;
2162 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2163 GlDraw.rect (1., float (y + 1))
2164 (float (conf.winw - 1), float (y + fs + 3));
2165 GlDraw.polygon_mode `both `fill;
2166 GlDraw.color (1., 1., 1.);
2167 Gl.enable `texture_2d;
2170 let drawtabularstring x s =
2171 if trusted
2172 then
2173 let tabpos = try String.index s '\t' with Not_found -> -1 in
2174 if tabpos > 0
2175 then
2176 let len = String.length s - tabpos - 1 in
2177 let s1 = String.sub s 0 tabpos
2178 and s2 = String.sub s (tabpos + 1) len in
2179 let xx = wx +. drawstring1 fs x (y + !uifontsize+1) s1 in
2180 let x = truncate (max xx tabx) in
2181 drawstring1 nfs x (y + (!uifontsize+1)) s2
2182 else
2183 drawstring1 fs x (y + nfs) s
2184 else
2185 drawstring1 fs x (y + nfs) s
2187 let _w = drawtabularstring (x + m_pan*nfs) s in
2188 loop (row+1)
2192 loop 0;
2193 Gl.disable `blend;
2194 Gl.disable `texture_2d;
2196 method private key1 key =
2197 let set active first qsearch =
2198 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2200 let search active pattern incr =
2201 let dosearch re =
2202 let rec loop n =
2203 if n >= 0 && n < source#getitemcount
2204 then (
2205 let s, _ = source#getitem n in
2207 (try ignore (Str.search_forward re s 0); true
2208 with Not_found -> false)
2209 then Some n
2210 else loop (n + incr)
2212 else None
2214 loop active
2217 let re = Str.regexp_case_fold pattern in
2218 dosearch re
2219 with Failure s ->
2220 state.text <- s;
2221 None
2223 match key with
2224 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2225 let incr = if key = 18 then -1 else 1 in
2226 let active, first =
2227 match search (m_active + incr) m_qsearch incr with
2228 | None ->
2229 state.text <- m_qsearch ^ " [not found]";
2230 m_active, m_first
2231 | Some active ->
2232 state.text <- m_qsearch;
2233 active, firstof m_first active
2235 G.postRedisplay "listview ctrl-r/s";
2236 set active first m_qsearch;
2238 | 8 -> (* backspace *)
2239 let len = String.length m_qsearch in
2240 if len = 0
2241 then coe self
2242 else (
2243 if len = 1
2244 then (
2245 state.text <- "";
2246 G.postRedisplay "listview empty qsearch";
2247 set m_active m_first "";
2249 else
2250 let qsearch = String.sub m_qsearch 0 (len - 1) in
2251 let active, first =
2252 match search m_active qsearch ~-1 with
2253 | None ->
2254 state.text <- qsearch ^ " [not found]";
2255 m_active, m_first
2256 | Some active ->
2257 state.text <- qsearch;
2258 active, firstof m_first active
2260 G.postRedisplay "listview backspace qsearch";
2261 set active first qsearch
2264 | _ when key >= 32 && key < 127 ->
2265 let pattern = addchar m_qsearch (Char.chr key) in
2266 let active, first =
2267 match search m_active pattern 1 with
2268 | None ->
2269 state.text <- pattern ^ " [not found]";
2270 m_active, m_first
2271 | Some active ->
2272 state.text <- pattern;
2273 active, firstof m_first active
2275 G.postRedisplay "listview qsearch add";
2276 set active first pattern;
2278 | 27 -> (* escape *)
2279 state.text <- "";
2280 if String.length m_qsearch = 0
2281 then (
2282 G.postRedisplay "list view escape";
2283 begin
2284 match
2285 source#exit (coe self) true m_active m_first m_pan m_qsearch
2286 with
2287 | None -> m_prev_uioh
2288 | Some uioh -> uioh
2291 else (
2292 G.postRedisplay "list view kill qsearch";
2293 source#setqsearch "";
2294 coe {< m_qsearch = "" >}
2297 | 13 -> (* enter *)
2298 state.text <- "";
2299 let self = {< m_qsearch = "" >} in
2300 source#setqsearch "";
2301 let opt =
2302 G.postRedisplay "listview enter";
2303 if m_active >= 0 && m_active < source#getitemcount
2304 then (
2305 source#exit (coe self) false m_active m_first m_pan "";
2307 else (
2308 source#exit (coe self) true m_active m_first m_pan "";
2311 begin match opt with
2312 | None -> m_prev_uioh
2313 | Some uioh -> uioh
2316 | 127 -> (* delete *)
2317 coe self
2319 | _ -> dolog "unknown key %d" key; coe self
2321 method private special1 key =
2322 let maxrows = maxoutlinerows () in
2323 let itemcount = source#getitemcount in
2324 let find start incr =
2325 let rec find i =
2326 if i = -1 || i = itemcount
2327 then -1
2328 else (
2329 if source#hasaction i
2330 then i
2331 else find (i + incr)
2334 find start
2336 let set active first =
2337 let first = bound first 0 (itemcount - maxrows) in
2338 state.text <- "";
2339 coe {< m_active = active; m_first = first >}
2341 let navigate incr =
2342 let isvisible first n = n >= first && n - first <= maxrows in
2343 let active, first =
2344 let incr1 = if incr > 0 then 1 else -1 in
2345 if isvisible m_first m_active
2346 then
2347 let next =
2348 let next = m_active + incr in
2349 let next =
2350 if next < 0 || next >= itemcount
2351 then -1
2352 else find next incr1
2354 if next = -1 || abs (m_active - next) > maxrows
2355 then -1
2356 else next
2358 if next = -1
2359 then
2360 let first = m_first + incr in
2361 let first = bound first 0 (itemcount - 1) in
2362 let next =
2363 let next = m_active + incr in
2364 let next = bound next 0 (itemcount - 1) in
2365 find next ~-incr1
2367 let active = if next = -1 then m_active else next in
2368 active, first
2369 else
2370 let first = min next m_first in
2371 next, first
2372 else
2373 let first = m_first + incr in
2374 let first = bound first 0 (itemcount - 1) in
2375 let active =
2376 let next = m_active + incr in
2377 let next = bound next 0 (itemcount - 1) in
2378 let next = find next incr1 in
2379 if next = -1 || abs (m_active - first) > maxrows
2380 then m_active
2381 else next
2383 active, first
2385 G.postRedisplay "listview navigate";
2386 set active first;
2388 begin match key with
2389 | Glut.KEY_UP -> navigate ~-1
2390 | Glut.KEY_DOWN -> navigate 1
2391 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2392 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2394 | Glut.KEY_RIGHT ->
2395 state.text <- "";
2396 G.postRedisplay "listview right";
2397 coe {< m_pan = m_pan - 1 >}
2399 | Glut.KEY_LEFT ->
2400 state.text <- "";
2401 G.postRedisplay "listview left";
2402 coe {< m_pan = m_pan + 1 >}
2404 | Glut.KEY_HOME ->
2405 let active = find 0 1 in
2406 G.postRedisplay "listview home";
2407 set active 0;
2409 | Glut.KEY_END ->
2410 let first = max 0 (itemcount - maxrows) in
2411 let active = find (itemcount - 1) ~-1 in
2412 G.postRedisplay "listview end";
2413 set active first;
2415 | _ -> coe self
2416 end;
2418 method key key =
2419 match state.mode with
2420 | Textentry te -> textentrykeyboard key te; coe self
2421 | _ -> self#key1 key
2423 method special key =
2424 match state.mode with
2425 | Textentry te -> textentryspecial key te; coe self
2426 | _ -> self#special1 key
2428 method button button bstate _ y =
2429 let opt =
2430 match button with
2431 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2432 begin match self#elemunder y with
2433 | Some n ->
2434 G.postRedisplay "listview click";
2435 source#exit (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2436 | _ ->
2437 Some (coe self)
2439 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2440 let len = source#getitemcount in
2441 let first =
2442 if m_first + maxoutlinerows () >= len
2443 then
2444 m_first
2445 else
2446 let first = m_first + (if n == 3 then -1 else 1) in
2447 bound first 0 (len - 1)
2449 G.postRedisplay "listview wheel";
2450 Some (coe {< m_first = first >})
2451 | _ ->
2452 Some (coe self)
2454 match opt with
2455 | None -> m_prev_uioh
2456 | Some uioh -> uioh
2458 method motion _ _ = coe self
2460 method pmotion _ y =
2461 let n =
2462 match self#elemunder y with
2463 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2464 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2466 let o =
2467 if n != m_active
2468 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2469 else self
2471 coe o
2473 method memusedchanged = ()
2474 end;;
2476 class outlinelistview ~source : uioh =
2477 let coe o = (o :> uioh) in
2478 object
2479 inherit listview ~source:(source :> lvsource) ~trusted:false as super
2481 method key key =
2482 match key with
2483 | 14 -> (* ctrl-n *)
2484 source#narrow m_qsearch;
2485 G.postRedisplay "outline ctrl-n";
2486 coe {< m_first = 0; m_active = 0 >}
2488 | 21 -> (* ctrl-u *)
2489 source#denarrow;
2490 G.postRedisplay "outline ctrl-u";
2491 coe {< m_first = 0; m_active = 0 >}
2493 | 12 -> (* ctrl-l *)
2494 let first = m_active - (maxoutlinerows () / 2) in
2495 G.postRedisplay "outline ctrl-l";
2496 coe {< m_first = first >}
2498 | 127 -> (* delete *)
2499 source#remove m_active;
2500 G.postRedisplay "outline delete";
2501 let active = max 0 (m_active-1) in
2502 coe {< m_first = firstof m_first active; m_active = active >}
2504 | key -> super#key key
2506 method special key =
2507 let maxrows = maxoutlinerows () in
2508 let calcfirst first active =
2509 if active > first
2510 then
2511 let rows = active - first in
2512 if rows > maxrows then active - maxrows else first
2513 else active
2515 let navigate incr =
2516 let active = m_active + incr in
2517 let active = bound active 0 (source#getitemcount - 1) in
2518 let first = calcfirst m_first active in
2519 G.postRedisplay "special outline navigate";
2520 coe {< m_active = active; m_first = first >}
2522 let updownlevel incr =
2523 let len = source#getitemcount in
2524 let _, curlevel = source#getitem m_active in
2525 let rec flow i =
2526 if i = len then i-1 else if i = -1 then 0 else
2527 let _, l = source#getitem i in
2528 if l != curlevel then i else flow (i+incr)
2530 let active = flow m_active in
2531 let first = calcfirst m_first active in
2532 G.postRedisplay "special outline updownlevel";
2533 {< m_active = active; m_first = first >}
2535 match key with
2536 | Glut.KEY_UP -> navigate ~-1
2537 | Glut.KEY_DOWN -> navigate 1
2538 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2539 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2541 | Glut.KEY_RIGHT ->
2542 let o =
2543 if Glut.getModifiers () land Glut.active_ctrl != 0
2544 then (
2545 G.postRedisplay "special outline right";
2546 {< m_pan = m_pan + 1 >}
2548 else updownlevel 1
2550 coe o
2552 | Glut.KEY_LEFT ->
2553 let o =
2554 if Glut.getModifiers () land Glut.active_ctrl != 0
2555 then (
2556 G.postRedisplay "special outline left";
2557 {< m_pan = m_pan - 1 >}
2559 else updownlevel ~-1
2561 coe o
2563 | Glut.KEY_HOME ->
2564 G.postRedisplay "special outline home";
2565 coe {< m_first = 0; m_active = 0 >}
2567 | Glut.KEY_END ->
2568 let active = source#getitemcount - 1 in
2569 let first = max 0 (active - maxrows) in
2570 G.postRedisplay "special outline end";
2571 coe {< m_active = active; m_first = first >}
2573 | _ -> super#special key
2576 let outlinesource usebookmarks =
2577 let empty = [||] in
2578 (object
2579 inherit lvsourcebase
2580 val mutable m_items = empty
2581 val mutable m_orig_items = empty
2582 val mutable m_prev_items = empty
2583 val mutable m_narrow_pattern = ""
2584 val mutable m_hadremovals = false
2586 method getitemcount = Array.length m_items + (if m_hadremovals then 1 else 0)
2588 method getitem n =
2589 if n == Array.length m_items && m_hadremovals
2590 then
2591 ("[Confirm removal]", 0)
2592 else
2593 let s, n, _ = m_items.(n) in
2594 (s, n)
2596 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
2597 ignore (uioh, first, pan, qsearch);
2598 let confrimremoval = m_hadremovals && active = Array.length m_items in
2599 let items =
2600 if String.length m_narrow_pattern = 0
2601 then m_orig_items
2602 else m_items
2604 if not cancel
2605 then (
2606 if not confrimremoval
2607 then(
2608 let _, _, anchor = m_items.(active) in
2609 gotoanchor anchor;
2610 m_items <- items;
2612 else (
2613 state.bookmarks <- Array.to_list m_items;
2614 m_orig_items <- m_items;
2617 else m_items <- items;
2618 None
2620 method hasaction _ = true
2622 method greetmsg =
2623 if Array.length m_items != Array.length m_orig_items
2624 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
2625 else ""
2627 method narrow pattern =
2628 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2629 match reopt with
2630 | None -> ()
2631 | Some re ->
2632 let rec loop accu n =
2633 if n = -1
2634 then (
2635 m_narrow_pattern <- pattern;
2636 m_items <- Array.of_list accu
2638 else
2639 let (s, _, _) as o = m_items.(n) in
2640 let accu =
2641 if (try ignore (Str.search_forward re s 0); true
2642 with Not_found -> false)
2643 then o :: accu
2644 else accu
2646 loop accu (n-1)
2648 loop [] (Array.length m_items - 1)
2650 method denarrow =
2651 m_orig_items <- (
2652 if usebookmarks
2653 then Array.of_list state.bookmarks
2654 else state.outlines
2656 m_items <- m_orig_items
2658 method remove m =
2659 if usebookmarks
2660 then
2661 if m >= 0 && m < Array.length m_items
2662 then (
2663 m_hadremovals <- true;
2664 m_items <- Array.init (Array.length m_items - 1) (fun n ->
2665 let n = if n >= m then n+1 else n in
2666 m_items.(n)
2670 method reset pageno items =
2671 m_hadremovals <- false;
2672 if m_orig_items == empty || m_prev_items != items
2673 then (
2674 m_orig_items <- items;
2675 if String.length m_narrow_pattern = 0
2676 then m_items <- items;
2678 m_prev_items <- items;
2679 let active =
2680 let rec loop n best bestd =
2681 if n = Array.length m_items
2682 then best
2683 else
2684 let (_, _, (outlinepageno, _)) = m_items.(n) in
2685 let d = abs (outlinepageno - pageno) in
2686 if d < bestd
2687 then loop (n+1) n d
2688 else loop (n+1) best bestd
2690 loop 0 ~-1 max_int
2692 m_active <- active;
2693 m_first <- firstof m_first active
2694 end)
2697 let enterselector usebookmarks =
2698 let source = outlinesource usebookmarks in
2699 fun errmsg ->
2700 let outlines =
2701 if usebookmarks
2702 then Array.of_list state.bookmarks
2703 else state.outlines
2705 if Array.length outlines = 0
2706 then (
2707 showtext ' ' errmsg;
2709 else (
2710 state.text <- source#greetmsg;
2711 Glut.setCursor Glut.CURSOR_INHERIT;
2712 let pageno =
2713 match state.layout with
2714 | [] -> -1
2715 | {pageno=pageno} :: _ -> pageno
2717 source#reset pageno outlines;
2718 state.uioh <- new outlinelistview ~source;
2719 G.postRedisplay "enter selector";
2723 let enteroutlinemode =
2724 let f = enterselector false in
2725 fun ()-> f "Document has no outline";
2728 let enterbookmarkmode =
2729 let f = enterselector true in
2730 fun () -> f "Document has no bookmarks (yet)";
2733 let color_of_string s =
2734 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
2735 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
2739 let color_to_string (r, g, b) =
2740 let r = truncate (r *. 256.0)
2741 and g = truncate (g *. 256.0)
2742 and b = truncate (b *. 256.0) in
2743 Printf.sprintf "%d/%d/%d" r g b
2746 let irect_of_string s =
2747 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
2750 let irect_to_string (x0,y0,x1,y1) =
2751 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
2754 let makecheckers () =
2755 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
2756 following to say:
2757 converted by Issac Trotts. July 25, 2002 *)
2758 let image_height = 64
2759 and image_width = 64 in
2761 let make_image () =
2762 let image =
2763 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height in
2764 for i = 0 to image_width - 1 do
2765 for j = 0 to image_height - 1 do
2766 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
2767 (if (i land 8 ) lxor (j land 8) = 0
2768 then [|255;255;255|] else [|200;200;200|])
2769 done
2770 done;
2771 image
2773 let image = make_image () in
2774 let id = GlTex.gen_texture () in
2775 GlTex.bind_texture `texture_2d id;
2776 GlPix.store (`unpack_alignment 1);
2777 GlTex.image2d image;
2778 List.iter (GlTex.parameter ~target:`texture_2d)
2779 [ `wrap_s `repeat;
2780 `wrap_t `repeat;
2781 `mag_filter `nearest;
2782 `min_filter `nearest ];
2786 let setcheckers enabled =
2787 match state.texid with
2788 | None ->
2789 if enabled then state.texid <- Some (makecheckers ())
2791 | Some texid ->
2792 if not enabled
2793 then (
2794 GlTex.delete_texture texid;
2795 state.texid <- None;
2799 let int_of_string_with_suffix s =
2800 let l = String.length s in
2801 let s1, shift =
2802 if l > 1
2803 then
2804 let suffix = Char.lowercase s.[l-1] in
2805 match suffix with
2806 | 'k' -> String.sub s 0 (l-1), 10
2807 | 'm' -> String.sub s 0 (l-1), 20
2808 | 'g' -> String.sub s 0 (l-1), 30
2809 | _ -> s, 0
2810 else s, 0
2812 let n = int_of_string s1 in
2813 let m = n lsl shift in
2814 if m < 0 || m < n
2815 then raise (Failure "value too large")
2816 else m
2819 let string_with_suffix_of_int n =
2820 if n = 0
2821 then "0"
2822 else
2823 let n, s =
2824 if n = 0
2825 then 0, ""
2826 else (
2827 if n land ((1 lsl 20) - 1) = 0
2828 then n lsr 20, "M"
2829 else (
2830 if n land ((1 lsl 10) - 1) = 0
2831 then n lsr 10, "K"
2832 else n, ""
2836 let rec loop s n =
2837 let h = n mod 1000 in
2838 let n = n / 1000 in
2839 if n = 0
2840 then string_of_int h ^ s
2841 else (
2842 let s = Printf.sprintf "_%03d%s" h s in
2843 loop s n
2846 loop "" n ^ s;
2849 let describe_location () =
2850 let f (fn, _) l =
2851 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
2853 let fn, ln = List.fold_left f (-1, -1) state.layout in
2854 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2855 let percent =
2856 if maxy <= 0
2857 then 100.
2858 else (100. *. (float state.y /. float maxy))
2860 if fn = ln
2861 then
2862 Printf.sprintf "page %d of %d [%.2f%%]"
2863 (fn+1) state.pagecount percent
2864 else
2865 Printf.sprintf
2866 "pages %d-%d of %d [%.2f%%]"
2867 (fn+1) (ln+1) state.pagecount percent
2870 let rec enterinfomode =
2871 let btos b = if b then "\xe2\x88\x9a" else "" in
2872 let showextended = ref false in
2873 let leave mode = function
2874 | Confirm -> state.mode <- mode
2875 | Cancel -> state.mode <- mode in
2876 let src =
2877 (object
2878 val mutable m_first_time = true
2879 val mutable m_l = []
2880 val mutable m_a = [||]
2881 val mutable m_prev_uioh = nouioh
2882 val mutable m_prev_mode = View
2884 inherit lvsourcebase
2886 method reset prev_mode prev_uioh =
2887 m_a <- Array.of_list (List.rev m_l);
2888 m_l <- [];
2889 m_prev_mode <- prev_mode;
2890 m_prev_uioh <- prev_uioh;
2891 if m_first_time
2892 then (
2893 let rec loop n =
2894 if n >= Array.length m_a
2895 then ()
2896 else
2897 match m_a.(n) with
2898 | _, _, _, Action _ -> m_active <- n
2899 | _ -> loop (n+1)
2901 loop 0;
2902 m_first_time <- false;
2905 method int name get set =
2906 m_l <-
2907 (name, `int get, 1, Action (
2908 fun u ->
2909 let ondone s =
2910 try set (int_of_string s)
2911 with exn ->
2912 state.text <- Printf.sprintf "bad integer `%s': %s"
2913 s (Printexc.to_string exn)
2915 state.text <- "";
2916 let te = name ^ ": ", "", None, intentry, ondone in
2917 state.mode <- Textentry (te, leave m_prev_mode);
2919 )) :: m_l
2921 method int_with_suffix name get set =
2922 m_l <-
2923 (name, `intws get, 1, Action (
2924 fun u ->
2925 let ondone s =
2926 try set (int_of_string_with_suffix s)
2927 with exn ->
2928 state.text <- Printf.sprintf "bad integer `%s': %s"
2929 s (Printexc.to_string exn)
2931 state.text <- "";
2932 let te =
2933 name ^ ": ", "", None, intentry_with_suffix, ondone
2935 state.mode <- Textentry (te, leave m_prev_mode);
2937 )) :: m_l
2939 method bool ?(offset=1) ?(btos=btos) name get set =
2940 m_l <-
2941 (name, `bool (btos, get), offset, Action (
2942 fun u ->
2943 let v = get () in
2944 set (not v);
2946 )) :: m_l
2948 method color name get set =
2949 m_l <-
2950 (name, `color get, 1, Action (
2951 fun u ->
2952 let invalid = (nan, nan, nan) in
2953 let ondone s =
2954 let c =
2955 try color_of_string s
2956 with exn ->
2957 state.text <- Printf.sprintf "bad color `%s': %s"
2958 s (Printexc.to_string exn);
2959 invalid
2961 if c <> invalid
2962 then set c;
2964 let te = name ^ ": ", "", None, textentry, ondone in
2965 state.text <- color_to_string (get ());
2966 state.mode <- Textentry (te, leave m_prev_mode);
2968 )) :: m_l
2970 method string name get set =
2971 m_l <-
2972 (name, `string get, 1, Action (
2973 fun u ->
2974 let ondone s = set s in
2975 let te = name ^ ": ", "", None, textentry, ondone in
2976 state.mode <- Textentry (te, leave m_prev_mode);
2978 )) :: m_l
2980 method colorspace name get set =
2981 m_l <-
2982 (name, `string get, 1, Action (
2983 fun _ ->
2984 let source =
2985 let vals = [| "rgb"; "bgr"; "gray" |] in
2986 (object
2987 inherit lvsourcebase
2989 initializer
2990 m_active <- int_of_colorspace conf.colorspace;
2991 m_first <- 0;
2993 method getitemcount = Array.length vals
2994 method getitem n = (vals.(n), 0)
2995 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
2996 ignore (uioh, first, pan, qsearch);
2997 if not cancel then set active;
2998 None
2999 method hasaction _ = true
3000 end)
3002 state.text <- "";
3003 new listview ~source ~trusted:true
3004 )) :: m_l
3006 method caption s offset =
3007 m_l <- (s, `empty, offset, Noaction) :: m_l
3009 method caption2 s f offset =
3010 m_l <- (s, `string f, offset, Noaction) :: m_l
3012 method getitemcount = Array.length m_a
3014 method getitem n =
3015 let tostr = function
3016 | `int f -> string_of_int (f ())
3017 | `intws f -> string_with_suffix_of_int (f ())
3018 | `string f -> f ()
3019 | `color f -> color_to_string (f ())
3020 | `bool (btos, f) -> btos (f ())
3021 | `empty -> ""
3023 let name, t, offset, _ = m_a.(n) in
3024 ((let s = tostr t in
3025 if String.length s > 0
3026 then Printf.sprintf "%s\t%s" name s
3027 else name),
3028 offset)
3030 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3031 let uiohopt =
3032 if not cancel
3033 then (
3034 m_qsearch <- qsearch;
3035 let uioh =
3036 match m_a.(active) with
3037 | _, _, _, Action f -> f uioh
3038 | _ -> uioh
3040 Some uioh
3042 else None
3044 m_active <- active;
3045 m_first <- first;
3046 m_pan <- pan;
3047 uiohopt
3049 method hasaction n =
3050 match m_a.(n) with
3051 | _, _, _, Action _ -> true
3052 | _ -> false
3053 end)
3055 fun () ->
3056 let sep () = src#caption "" 0 in
3057 let colorp name get set =
3058 src#string name
3059 (fun () -> color_to_string (get ()))
3060 (fun v ->
3062 let c = color_of_string v in
3063 set c
3064 with exn ->
3065 state.text <- Printf.sprintf "bad color `%s': %s"
3066 v (Printexc.to_string exn);
3069 let oldmode = state.mode in
3070 let birdseye = isbirdseye state.mode in
3071 state.text <- "";
3073 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3075 src#bool "presentation mode"
3076 (fun () -> conf.presentation)
3077 (fun v ->
3078 conf.presentation <- v;
3079 state.anchor <- getanchor ();
3080 represent ());
3082 src#bool "ignore case in searches"
3083 (fun () -> conf.icase)
3084 (fun v -> conf.icase <- v);
3086 src#bool "preload"
3087 (fun () -> conf.preload)
3088 (fun v -> conf.preload <- v);
3090 src#bool "throttle"
3091 (fun () -> conf.showall)
3092 (fun v -> conf.showall <- v);
3094 src#bool "highlight links"
3095 (fun () -> conf.hlinks)
3096 (fun v -> conf.hlinks <- v);
3098 src#bool "under info"
3099 (fun () -> conf.underinfo)
3100 (fun v -> conf.underinfo <- v);
3102 src#bool "persistent bookmarks"
3103 (fun () -> conf.savebmarks)
3104 (fun v -> conf.savebmarks <- v);
3106 src#bool "proportional display"
3107 (fun () -> conf.proportional)
3108 (fun v -> reqlayout conf.angle v);
3110 src#bool "trim margins"
3111 (fun () -> conf.trimmargins)
3112 (fun v -> settrim v conf.trimfuzz);
3114 src#bool "persistent location"
3115 (fun () -> conf.jumpback)
3116 (fun v -> conf.jumpback <- v);
3118 sep ();
3119 src#int "vertical margin"
3120 (fun () -> conf.interpagespace)
3121 (fun n ->
3122 conf.interpagespace <- n;
3123 let pageno, py =
3124 match state.layout with
3125 | [] -> 0, 0
3126 | l :: _ ->
3127 l.pageno, l.pagey
3129 state.maxy <- calcheight ();
3130 let y = getpagey pageno in
3131 gotoy (y + py)
3134 src#int "page bias"
3135 (fun () -> conf.pagebias)
3136 (fun v -> conf.pagebias <- v);
3138 src#int "scroll step"
3139 (fun () -> conf.scrollstep)
3140 (fun n -> conf.scrollstep <- n);
3142 src#int "auto scroll step"
3143 (fun () ->
3144 match state.autoscroll with
3145 | Some step -> step
3146 | _ -> conf.autoscrollstep)
3147 (fun n ->
3148 if state.autoscroll <> None
3149 then state.autoscroll <- Some n;
3150 conf.autoscrollstep <- n);
3152 src#int "zoom"
3153 (fun () -> truncate (conf.zoom *. 100.))
3154 (fun v -> setzoom ((float v) /. 100.));
3156 src#int "rotation"
3157 (fun () -> conf.angle)
3158 (fun v -> reqlayout v conf.proportional);
3160 src#int "scroll bar width"
3161 (fun () -> state.scrollw)
3162 (fun v ->
3163 state.scrollw <- v;
3164 conf.scrollbw <- v;
3165 reshape conf.winw conf.winh;
3168 src#int "scroll handle height"
3169 (fun () -> conf.scrollh)
3170 (fun v -> conf.scrollh <- v;);
3172 src#int "thumbnail width"
3173 (fun () -> conf.thumbw)
3174 (fun v ->
3175 conf.thumbw <- min 4096 v;
3176 match oldmode with
3177 | Birdseye beye ->
3178 leavebirdseye beye false;
3179 enterbirdseye ()
3180 | _ -> ()
3183 sep ();
3184 src#caption "Presentation mode" 0;
3185 src#bool "scrollbar visible"
3186 (fun () -> conf.scrollbarinpm)
3187 (fun v ->
3188 if v != conf.scrollbarinpm
3189 then (
3190 conf.scrollbarinpm <- v;
3191 if conf.presentation
3192 then (
3193 state.scrollw <- if v then conf.scrollbw else 0;
3194 reshape conf.winw conf.winh;
3199 sep ();
3200 src#caption "Pixmap cache" 0;
3201 src#int_with_suffix "size (advisory)"
3202 (fun () -> conf.memlimit)
3203 (fun v -> conf.memlimit <- v);
3205 src#caption2 "used"
3206 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3207 (string_with_suffix_of_int state.memused)
3208 (Hashtbl.length state.tilemap)) 1;
3210 sep ();
3211 src#caption "Layout" 0;
3212 src#caption2 "Dimension"
3213 (fun () ->
3214 Printf.sprintf "%dx%d (virtual %dx%d)"
3215 conf.winw conf.winh
3216 state.w state.maxy)
3218 if conf.debug
3219 then
3220 src#caption2 "Position" (fun () ->
3221 Printf.sprintf "%dx%d" state.x state.y
3223 else
3224 src#caption2 "Visible" (fun () -> describe_location ()) 1
3227 sep ();
3228 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3229 "Save these parameters as global defaults at exit"
3230 (fun () -> conf.bedefault)
3231 (fun v -> conf.bedefault <- v)
3234 sep ();
3235 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3236 src#bool ~offset:0 ~btos "Extended parameters"
3237 (fun () -> !showextended)
3238 (fun v -> showextended := v; enterinfomode ());
3239 if !showextended
3240 then (
3241 src#bool "checkers"
3242 (fun () -> conf.checkers)
3243 (fun v -> conf.checkers <- v; setcheckers v);
3244 src#bool "verbose"
3245 (fun () -> conf.verbose)
3246 (fun v -> conf.verbose <- v);
3247 src#bool "invert colors"
3248 (fun () -> conf.invert)
3249 (fun v -> conf.invert <- v);
3250 src#bool "max fit"
3251 (fun () -> conf.maxhfit)
3252 (fun v -> conf.maxhfit <- v);
3253 src#string "uri launcher"
3254 (fun () -> conf.urilauncher)
3255 (fun v -> conf.urilauncher <- v);
3256 src#string "tile size"
3257 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3258 (fun v ->
3260 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3261 conf.tileh <- max 64 w;
3262 conf.tilew <- max 64 h;
3263 flushtiles ();
3264 with exn ->
3265 state.text <- Printf.sprintf "bad tile size `%s': %s"
3266 v (Printexc.to_string exn));
3267 src#int "anti-aliasing level"
3268 (fun () -> conf.aalevel)
3269 (fun v ->
3270 conf.aalevel <- bound v 0 8;
3271 state.anchor <- getanchor ();
3272 opendoc state.path state.password;
3274 src#int "ui font size"
3275 (fun () -> !uifontsize)
3276 (fun v -> uifontsize := bound v 5 100);
3277 colorp "background color"
3278 (fun () -> conf.bgcolor)
3279 (fun v -> conf.bgcolor <- v);
3280 src#bool "crop hack"
3281 (fun () -> conf.crophack)
3282 (fun v -> conf.crophack <- v);
3283 src#string "trim fuzz"
3284 (fun () -> irect_to_string conf.trimfuzz)
3285 (fun v ->
3287 conf.trimfuzz <- irect_of_string v;
3288 if conf.trimmargins
3289 then settrim true conf.trimfuzz;
3290 with exn ->
3291 state.text <- Printf.sprintf "bad irect `%s': %s"
3292 v (Printexc.to_string exn)
3294 src#colorspace "color space"
3295 (fun () -> colorspace_to_string conf.colorspace)
3296 (fun v ->
3297 conf.colorspace <- colorspace_of_int v;
3298 wcmd "cs" [`i v];
3299 load state.layout;
3303 sep ();
3304 src#caption "Document" 0;
3305 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3307 src#reset state.mode state.uioh;
3308 let source = (src :> lvsource) in
3309 state.uioh <- object
3310 inherit listview ~source ~trusted:true
3311 val mutable m_prevmemused = 0
3312 method memusedchanged =
3313 if m_prevmemused != state.memused
3314 then (
3315 m_prevmemused <- state.memused;
3316 G.postRedisplay "memusedchanged";
3318 end;
3319 G.postRedisplay "info";
3322 let enterhelpmode =
3323 let source =
3324 (object
3325 inherit lvsourcebase
3326 method getitemcount = Array.length state.help
3327 method getitem n =
3328 let s, n, _ = state.help.(n) in
3329 (s, n)
3331 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3332 let optuioh =
3333 if not cancel
3334 then (
3335 m_qsearch <- qsearch;
3336 match state.help.(active) with
3337 | _, _, Action f -> Some (f uioh)
3338 | _ -> Some (uioh)
3340 else None
3342 m_active <- active;
3343 m_first <- first;
3344 m_pan <- pan;
3345 optuioh
3347 method hasaction n =
3348 match state.help.(n) with
3349 | _, _, Action _ -> true
3350 | _ -> false
3352 initializer
3353 m_active <- -1
3354 end)
3355 in fun () ->
3356 state.uioh <- new listview ~source ~trusted:true;
3357 G.postRedisplay "help";
3360 let quickbookmark ?title () =
3361 match state.layout with
3362 | [] -> ()
3363 | l :: _ ->
3364 let title =
3365 match title with
3366 | None ->
3367 let sec = Unix.gettimeofday () in
3368 let tm = Unix.localtime sec in
3369 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
3370 (l.pageno+1)
3371 tm.Unix.tm_mday
3372 tm.Unix.tm_mon
3373 (tm.Unix.tm_year + 1900)
3374 tm.Unix.tm_hour
3375 tm.Unix.tm_min
3376 | Some title -> title
3378 state.bookmarks <-
3379 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
3380 :: state.bookmarks
3383 let doreshape w h =
3384 state.fullscreen <- None;
3385 Glut.reshapeWindow w h;
3388 let viewkeyboard key =
3389 let enttext te =
3390 let mode = state.mode in
3391 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3392 state.text <- "";
3393 enttext ();
3394 G.postRedisplay "view:enttext"
3396 let c = Char.chr key in
3397 match c with
3398 | '\027' | 'q' -> (* escape *)
3399 begin match state.mstate with
3400 | Mzoomrect _ ->
3401 state.mstate <- Mnone;
3402 Glut.setCursor Glut.CURSOR_INHERIT;
3403 G.postRedisplay "kill zoom rect";
3404 | _ ->
3405 raise Quit
3406 end;
3408 | '\008' -> (* backspace *)
3409 let y = getnav ~-1 in
3410 gotoy_and_clear_text y
3412 | 'o' ->
3413 enteroutlinemode ()
3415 | 'u' ->
3416 state.rects <- [];
3417 state.text <- "";
3418 G.postRedisplay "dehighlight";
3420 | '/' | '?' ->
3421 let ondone isforw s =
3422 cbput state.hists.pat s;
3423 state.searchpattern <- s;
3424 search s isforw
3426 let s = String.create 1 in
3427 s.[0] <- c;
3428 enttext (s, "", Some (onhist state.hists.pat),
3429 textentry, ondone (c ='/'))
3431 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3432 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3433 setzoom (conf.zoom +. incr)
3435 | '+' ->
3436 let ondone s =
3437 let n =
3438 try int_of_string s with exc ->
3439 state.text <- Printf.sprintf "bad integer `%s': %s"
3440 s (Printexc.to_string exc);
3441 max_int
3443 if n != max_int
3444 then (
3445 conf.pagebias <- n;
3446 state.text <- "page bias is now " ^ string_of_int n;
3449 enttext ("page bias: ", "", None, intentry, ondone)
3451 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3452 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3453 setzoom (max 0.01 (conf.zoom -. decr))
3455 | '-' ->
3456 let ondone msg = state.text <- msg in
3457 enttext (
3458 "option [acfhilpstvAPRSZTI]: ", "", None,
3459 optentry state.mode, ondone
3462 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3463 setzoom 1.0
3465 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3466 let zoom = zoomforh conf.winw conf.winh state.scrollw in
3467 if zoom < 1.0
3468 then setzoom zoom
3470 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3471 togglebirdseye ()
3473 | '0' .. '9' ->
3474 let ondone s =
3475 let n =
3476 try int_of_string s with exc ->
3477 state.text <- Printf.sprintf "bad integer `%s': %s"
3478 s (Printexc.to_string exc);
3481 if n >= 0
3482 then (
3483 addnav ();
3484 cbput state.hists.pag (string_of_int n);
3485 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
3488 let pageentry text key =
3489 match Char.unsafe_chr key with
3490 | 'g' -> TEdone text
3491 | _ -> intentry text key
3493 let text = "x" in text.[0] <- c;
3494 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
3496 | 'b' ->
3497 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
3498 reshape conf.winw conf.winh;
3500 | 'l' ->
3501 conf.hlinks <- not conf.hlinks;
3502 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3503 G.postRedisplay "toggle highlightlinks";
3505 | 'a' ->
3506 begin match state.autoscroll with
3507 | Some step ->
3508 conf.autoscrollstep <- step;
3509 state.autoscroll <- None
3510 | None ->
3511 if conf.autoscrollstep = 0
3512 then state.autoscroll <- Some 1
3513 else state.autoscroll <- Some conf.autoscrollstep
3516 | 'P' ->
3517 conf.presentation <- not conf.presentation;
3518 if conf.presentation
3519 then (
3520 if not conf.scrollbarinpm
3521 then state.scrollw <- 0;
3523 else
3524 state.scrollw <- conf.scrollbw;
3526 showtext ' ' ("presentation mode " ^
3527 if conf.presentation then "on" else "off");
3528 state.anchor <- getanchor ();
3529 represent ()
3531 | 'f' ->
3532 begin match state.fullscreen with
3533 | None ->
3534 state.fullscreen <- Some (conf.winw, conf.winh);
3535 Glut.fullScreen ()
3536 | Some (w, h) ->
3537 state.fullscreen <- None;
3538 doreshape w h
3541 | 'g' ->
3542 gotoy_and_clear_text 0
3544 | 'G' ->
3545 gotopage1 (state.pagecount - 1) 0
3547 | 'n' ->
3548 search state.searchpattern true
3550 | 'p' | 'N' ->
3551 search state.searchpattern false
3553 | 't' ->
3554 begin match state.layout with
3555 | [] -> ()
3556 | l :: _ ->
3557 gotoy_and_clear_text (getpagey l.pageno)
3560 | ' ' ->
3561 begin match List.rev state.layout with
3562 | [] -> ()
3563 | l :: _ ->
3564 let pageno = min (l.pageno+1) (state.pagecount-1) in
3565 gotoy_and_clear_text (getpagey pageno)
3568 | '\127' -> (* del *)
3569 begin match state.layout with
3570 | [] -> ()
3571 | l :: _ ->
3572 let pageno = max 0 (l.pageno-1) in
3573 gotoy_and_clear_text (getpagey pageno)
3576 | '=' ->
3577 showtext ' ' (describe_location ());
3579 | 'w' ->
3580 begin match state.layout with
3581 | [] -> ()
3582 | l :: _ ->
3583 doreshape (l.pagew + state.scrollw) l.pageh;
3584 G.postRedisplay "w"
3587 | '\'' ->
3588 enterbookmarkmode ()
3590 | 'h' ->
3591 enterhelpmode ()
3593 | 'i' ->
3594 enterinfomode ()
3596 | 'm' ->
3597 let ondone s =
3598 match state.layout with
3599 | l :: _ ->
3600 state.bookmarks <-
3601 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
3602 :: state.bookmarks
3603 | _ -> ()
3605 enttext ("bookmark: ", "", None, textentry, ondone)
3607 | '~' ->
3608 quickbookmark ();
3609 showtext ' ' "Quick bookmark added";
3611 | 'z' ->
3612 begin match state.layout with
3613 | l :: _ ->
3614 let rect = getpdimrect l.pagedimno in
3615 let w, h =
3616 if conf.crophack
3617 then
3618 (truncate (1.8 *. (rect.(1) -. rect.(0))),
3619 truncate (1.2 *. (rect.(3) -. rect.(0))))
3620 else
3621 (truncate (rect.(1) -. rect.(0)),
3622 truncate (rect.(3) -. rect.(0)))
3624 let w = truncate ((float w)*.conf.zoom)
3625 and h = truncate ((float h)*.conf.zoom) in
3626 if w != 0 && h != 0
3627 then (
3628 state.anchor <- getanchor ();
3629 doreshape (w + state.scrollw) (h + conf.interpagespace)
3631 G.postRedisplay "z";
3633 | [] -> ()
3636 | '\000' -> (* ctrl-2 *)
3637 let maxw = getmaxw () in
3638 if maxw > 0.0
3639 then setzoom (maxw /. float conf.winw)
3641 | '<' | '>' ->
3642 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
3644 | '[' | ']' ->
3645 state.colorscale <-
3646 bound (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
3648 G.postRedisplay "brightness";
3650 | 'k' ->
3651 begin match state.mode with
3652 | Birdseye beye -> upbirdseye beye
3653 | _ -> gotoy (clamp (-conf.scrollstep))
3656 | 'j' ->
3657 begin match state.mode with
3658 | Birdseye beye -> downbirdseye beye
3659 | _ -> gotoy (clamp conf.scrollstep)
3662 | 'r' ->
3663 state.anchor <- getanchor ();
3664 opendoc state.path state.password
3666 | 'v' when conf.debug ->
3667 state.rects <- [];
3668 List.iter (fun l ->
3669 match getopaque l.pageno with
3670 | None -> ()
3671 | Some opaque ->
3672 let x0, y0, x1, y1 = pagebbox opaque in
3673 let a,b = float x0, float y0 in
3674 let c,d = float x1, float y0 in
3675 let e,f = float x1, float y1 in
3676 let h,j = float x0, float y1 in
3677 let rect = (a,b,c,d,e,f,h,j) in
3678 debugrect rect;
3679 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
3680 ) state.layout;
3681 G.postRedisplay "v";
3683 | _ ->
3684 vlog "huh? %d %c" key (Char.chr key);
3687 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
3688 match key with
3689 | 27 -> (* escape *)
3690 leavebirdseye beye true
3692 | 12 -> (* ctrl-l *)
3693 let y, h = getpageyh pageno in
3694 let top = (conf.winh - h) / 2 in
3695 gotoy (max 0 (y - top))
3697 | 13 -> (* enter *)
3698 leavebirdseye beye false
3700 | _ ->
3701 viewkeyboard key
3704 let keyboard ~key ~x ~y =
3705 ignore x;
3706 ignore y;
3707 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
3708 then wcmd "interrupt" []
3709 else state.uioh <- state.uioh#key key
3712 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
3713 match key with
3714 | Glut.KEY_UP -> upbirdseye beye
3715 | Glut.KEY_DOWN -> downbirdseye beye
3717 | Glut.KEY_PAGE_UP ->
3718 begin match state.layout with
3719 | l :: _ ->
3720 if l.pagey != 0
3721 then (
3722 state.mode <- Birdseye (
3723 conf, leftx, l.pageno, hooverpageno, anchor
3725 gotopage1 l.pageno 0;
3727 else (
3728 let layout = layout (state.y-conf.winh) conf.winh in
3729 match layout with
3730 | [] -> gotoy (clamp (-conf.winh))
3731 | l :: _ ->
3732 state.mode <- Birdseye (
3733 conf, leftx, l.pageno, hooverpageno, anchor
3735 gotopage1 l.pageno 0
3738 | [] -> gotoy (clamp (-conf.winh))
3739 end;
3741 | Glut.KEY_PAGE_DOWN ->
3742 begin match List.rev state.layout with
3743 | l :: _ ->
3744 let layout = layout (state.y + conf.winh) conf.winh in
3745 begin match layout with
3746 | [] ->
3747 let incr = l.pageh - l.pagevh in
3748 if incr = 0
3749 then (
3750 state.mode <-
3751 Birdseye (
3752 conf, leftx, state.pagecount - 1, hooverpageno, anchor
3754 G.postRedisplay "birdseye pagedown";
3756 else gotoy (clamp (incr + conf.interpagespace*2));
3758 | l :: _ ->
3759 state.mode <-
3760 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
3761 gotopage1 l.pageno 0;
3764 | [] -> gotoy (clamp conf.winh)
3765 end;
3767 | Glut.KEY_HOME ->
3768 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
3769 gotopage1 0 0
3771 | Glut.KEY_END ->
3772 let pageno = state.pagecount - 1 in
3773 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3774 if not (pagevisible state.layout pageno)
3775 then
3776 let h =
3777 match List.rev state.pdims with
3778 | [] -> conf.winh
3779 | (_, _, h, _) :: _ -> h
3781 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
3782 else G.postRedisplay "birdseye end";
3783 | _ -> ()
3786 let setautoscrollspeed step goingdown =
3787 let incr = max 1 ((abs step) / 2) in
3788 let incr = if goingdown then incr else -incr in
3789 let astep = step + incr in
3790 state.autoscroll <- Some astep;
3793 let special ~key ~x ~y =
3794 ignore x;
3795 ignore y;
3796 state.uioh <- state.uioh#special key
3799 let drawpage l =
3800 let color =
3801 match state.mode with
3802 | Textentry _ -> scalecolor 0.4
3803 | View -> scalecolor 1.0
3804 | Birdseye (_, _, pageno, hooverpageno, _) ->
3805 if l.pageno = hooverpageno
3806 then scalecolor 0.9
3807 else (
3808 if l.pageno = pageno
3809 then scalecolor 1.0
3810 else scalecolor 0.8
3813 drawtiles l color;
3814 begin match getopaque l.pageno with
3815 | Some opaque ->
3816 if tileready l l.pagex l.pagey
3817 then
3818 let x = l.pagedispx - l.pagex
3819 and y = l.pagedispy - l.pagey in
3820 postprocess opaque conf.hlinks x y;
3822 | _ -> ()
3823 end;
3826 let scrollph y =
3827 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3828 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3829 let sh = float conf.winh /. sh in
3830 let sh = max sh (float conf.scrollh) in
3832 let percent =
3833 if y = state.maxy
3834 then 1.0
3835 else float y /. float maxy
3837 let position = (float conf.winh -. sh) *. percent in
3839 let position =
3840 if position +. sh > float conf.winh
3841 then float conf.winh -. sh
3842 else position
3844 position, sh;
3847 let scrollpw x =
3848 let winw = conf.winw - state.scrollw - 1 in
3849 let fwinw = float winw in
3850 let sw =
3851 let sw = fwinw /. float state.w in
3852 let sw = fwinw *. sw in
3853 max sw (float conf.scrollh)
3855 let position, sw =
3856 let f = state.w+winw in
3857 let r = float (winw-x) /. float f in
3858 let p = fwinw *. r in
3859 p-.sw/.2., sw
3861 let sw =
3862 if position +. sw > fwinw
3863 then fwinw -. position
3864 else sw
3866 position, sw;
3869 let scrollindicator () =
3870 GlDraw.color (0.64 , 0.64, 0.64);
3871 GlDraw.rect
3872 (float (conf.winw - state.scrollw), 0.)
3873 (float conf.winw, float conf.winh)
3875 GlDraw.rect
3876 (0., float (conf.winh - state.hscrollh))
3877 (float (conf.winw - state.scrollw - 1), float conf.winh)
3879 GlDraw.color (0.0, 0.0, 0.0);
3881 let position, sh = scrollph state.y in
3882 GlDraw.rect
3883 (float (conf.winw - state.scrollw), position)
3884 (float conf.winw, position +. sh)
3886 let position, sw = scrollpw state.x in
3887 GlDraw.rect
3888 (position, float (conf.winh - state.hscrollh))
3889 (position +. sw, float conf.winh)
3893 let pagetranslatepoint l x y =
3894 let dy = y - l.pagedispy in
3895 let y = dy + l.pagey in
3896 let dx = x - l.pagedispx in
3897 let x = dx + l.pagex in
3898 (x, y);
3901 let showsel () =
3902 match state.mstate with
3903 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
3906 | Msel ((x0, y0), (x1, y1)) ->
3907 let rec loop = function
3908 | l :: ls ->
3909 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3910 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3911 then
3912 match getopaque l.pageno with
3913 | Some opaque ->
3914 let dx, dy = pagetranslatepoint l 0 0 in
3915 let x0 = x0 + dx
3916 and y0 = y0 + dy
3917 and x1 = x1 + dx
3918 and y1 = y1 + dy in
3919 GlMat.mode `modelview;
3920 GlMat.push ();
3921 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
3922 seltext opaque (x0, y0, x1, y1);
3923 GlMat.pop ();
3924 | _ -> ()
3925 else loop ls
3926 | [] -> ()
3928 loop state.layout
3931 let showrects () =
3932 Gl.enable `blend;
3933 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
3934 GlDraw.polygon_mode `both `fill;
3935 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3936 List.iter
3937 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
3938 List.iter (fun l ->
3939 if l.pageno = pageno
3940 then (
3941 let dx = float (l.pagedispx - l.pagex) in
3942 let dy = float (l.pagedispy - l.pagey) in
3943 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
3944 GlDraw.begins `quads;
3946 GlDraw.vertex2 (x0+.dx, y0+.dy);
3947 GlDraw.vertex2 (x1+.dx, y1+.dy);
3948 GlDraw.vertex2 (x2+.dx, y2+.dy);
3949 GlDraw.vertex2 (x3+.dx, y3+.dy);
3951 GlDraw.ends ();
3953 ) state.layout
3954 ) state.rects
3956 Gl.disable `blend;
3959 let display () =
3960 GlClear.color (scalecolor2 conf.bgcolor);
3961 GlClear.clear [`color];
3962 List.iter drawpage state.layout;
3963 showrects ();
3964 showsel ();
3965 scrollindicator ();
3966 state.uioh#display;
3967 begin match state.mstate with
3968 | Mzoomrect ((x0, y0), (x1, y1)) ->
3969 Gl.enable `blend;
3970 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
3971 GlDraw.polygon_mode `both `fill;
3972 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3973 GlDraw.rect (float x0, float y0)
3974 (float x1, float y1);
3975 Gl.disable `blend;
3976 | _ -> ()
3977 end;
3978 enttext ();
3979 Glut.swapBuffers ();
3982 let getunder x y =
3983 let rec f = function
3984 | l :: rest ->
3985 begin match getopaque l.pageno with
3986 | Some opaque ->
3987 let x0 = l.pagedispx in
3988 let x1 = x0 + l.pagevw in
3989 let y0 = l.pagedispy in
3990 let y1 = y0 + l.pagevh in
3991 if y >= y0 && y <= y1 && x >= x0 && x <= x1
3992 then
3993 let px, py = pagetranslatepoint l x y in
3994 match whatsunder opaque px py with
3995 | Unone -> f rest
3996 | under -> under
3997 else f rest
3998 | _ ->
3999 f rest
4001 | [] -> Unone
4003 f state.layout
4006 let zoomrect x y x1 y1 =
4007 let x0 = min x x1
4008 and x1 = max x x1
4009 and y0 = min y y1 in
4010 gotoy (state.y + y0);
4011 state.anchor <- getanchor ();
4012 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4013 state.x <- state.x - x0;
4014 setzoom zoom;
4015 Glut.setCursor Glut.CURSOR_INHERIT;
4016 state.mstate <- Mnone;
4019 let scrollx x =
4020 let winw = conf.winw - state.scrollw - 1 in
4021 let s = float x /. float winw in
4022 let destx = truncate (float (state.w + winw) *. s) in
4023 state.x <- winw - destx;
4024 gotoy_and_clear_text state.y;
4025 state.mstate <- Mscrollx;
4028 let scrolly y =
4029 let s = float y /. float conf.winh in
4030 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4031 gotoy_and_clear_text desty;
4032 state.mstate <- Mscrolly;
4035 let viewmouse button bstate x y =
4036 match button with
4037 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4038 if Glut.getModifiers () land Glut.active_ctrl != 0
4039 then (
4040 match state.mstate with
4041 | Mzoom (oldn, i) ->
4042 if oldn = n
4043 then (
4044 if i = 2
4045 then
4046 let incr =
4047 match n with
4048 | 4 ->
4049 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4050 | _ ->
4051 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4053 let zoom = conf.zoom -. incr in
4054 setzoom zoom;
4055 state.mstate <- Mzoom (n, 0);
4056 else
4057 state.mstate <- Mzoom (n, i+1);
4059 else state.mstate <- Mzoom (n, 0)
4061 | _ -> state.mstate <- Mzoom (n, 0)
4063 else (
4064 match state.autoscroll with
4065 | Some step -> setautoscrollspeed step (n=4)
4066 | None ->
4067 let incr =
4068 if n = 3
4069 then -conf.scrollstep
4070 else conf.scrollstep
4072 let incr = incr * 2 in
4073 let y = clamp incr in
4074 gotoy_and_clear_text y
4077 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4078 if bstate = Glut.DOWN
4079 then (
4080 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4081 state.mstate <- Mpan (x, y)
4083 else
4084 state.mstate <- Mnone
4086 | Glut.RIGHT_BUTTON ->
4087 if bstate = Glut.DOWN
4088 then (
4089 Glut.setCursor Glut.CURSOR_CYCLE;
4090 let p = (x, y) in
4091 state.mstate <- Mzoomrect (p, p)
4093 else (
4094 match state.mstate with
4095 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4096 | _ ->
4097 Glut.setCursor Glut.CURSOR_INHERIT;
4098 state.mstate <- Mnone
4101 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4102 if bstate = Glut.DOWN
4103 then
4104 let position, sh = scrollph state.y in
4105 if y > truncate position && y < truncate (position +. sh)
4106 then state.mstate <- Mscrolly
4107 else scrolly y
4108 else
4109 state.mstate <- Mnone
4111 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4112 if bstate = Glut.DOWN
4113 then
4114 let position, sw = scrollpw state.x in
4115 if x > truncate position && x < truncate (position +. sw)
4116 then state.mstate <- Mscrollx
4117 else scrollx x
4118 else
4119 state.mstate <- Mnone
4121 | Glut.LEFT_BUTTON ->
4122 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4123 begin match dest with
4124 | Ulinkgoto (pageno, top) ->
4125 if pageno >= 0
4126 then (
4127 addnav ();
4128 gotopage1 pageno top;
4131 | Ulinkuri s ->
4132 gotouri s
4134 | Unone when bstate = Glut.DOWN ->
4135 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4136 state.mstate <- Mpan (x, y);
4138 | Unone | Utext _ ->
4139 if bstate = Glut.DOWN
4140 then (
4141 if conf.angle mod 360 = 0
4142 then (
4143 state.mstate <- Msel ((x, y), (x, y));
4144 G.postRedisplay "mouse select";
4147 else (
4148 match state.mstate with
4149 | Mnone -> ()
4151 | Mzoom _ | Mscrollx | Mscrolly ->
4152 state.mstate <- Mnone
4154 | Mzoomrect ((x0, y0), _) ->
4155 zoomrect x0 y0 x y
4157 | Mpan _ ->
4158 Glut.setCursor Glut.CURSOR_INHERIT;
4159 state.mstate <- Mnone
4161 | Msel ((_, y0), (_, y1)) ->
4162 let f l =
4163 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4164 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4165 then
4166 match getopaque l.pageno with
4167 | Some opaque ->
4168 copysel opaque
4169 | _ -> ()
4171 List.iter f state.layout;
4172 copysel ""; (* ugly *)
4173 Glut.setCursor Glut.CURSOR_INHERIT;
4174 state.mstate <- Mnone;
4178 | _ -> ()
4181 let birdseyemouse button bstate x y
4182 (conf, leftx, _, hooverpageno, anchor) =
4183 match button with
4184 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4185 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4186 let rec loop = function
4187 | [] -> ()
4188 | l :: rest ->
4189 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4190 && x > margin && x < margin + l.pagew
4191 then (
4192 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4194 else loop rest
4196 loop state.layout
4197 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4198 | _ -> ()
4201 let mouse bstate button x y =
4202 state.uioh <- state.uioh#button button bstate x y;
4205 let mouse ~button ~state ~x ~y = mouse state button x y;;
4207 let motion ~x ~y =
4208 state.uioh <- state.uioh#motion x y
4211 let pmotion ~x ~y =
4212 state.uioh <- state.uioh#pmotion x y;
4215 let uioh = object
4216 method display = ()
4218 method key key =
4219 begin match state.mode with
4220 | Textentry textentry -> textentrykeyboard key textentry
4221 | Birdseye birdseye -> birdseyekeyboard key birdseye
4222 | View -> viewkeyboard key
4223 end;
4224 state.uioh
4226 method special key =
4227 begin match state.mode with
4228 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4229 togglebirdseye ()
4231 | Birdseye vals ->
4232 birdseyespecial key vals
4234 | View when key = Glut.KEY_F1 ->
4235 enterhelpmode ()
4237 | View ->
4238 begin match state.autoscroll with
4239 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4240 setautoscrollspeed step (key = Glut.KEY_DOWN)
4242 | _ ->
4243 let y =
4244 match key with
4245 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4246 | Glut.KEY_UP ->
4247 if Glut.getModifiers () land Glut.active_ctrl != 0
4248 then
4249 if Glut.getModifiers () land Glut.active_shift != 0
4250 then (setzoom state.prevzoom; state.y)
4251 else clamp (-conf.winh/2)
4252 else clamp (-conf.scrollstep)
4253 | Glut.KEY_DOWN ->
4254 if Glut.getModifiers () land Glut.active_ctrl != 0
4255 then
4256 if Glut.getModifiers () land Glut.active_shift != 0
4257 then (setzoom state.prevzoom; state.y)
4258 else clamp (conf.winh/2)
4259 else clamp (conf.scrollstep)
4260 | Glut.KEY_PAGE_UP ->
4261 if Glut.getModifiers () land Glut.active_ctrl != 0
4262 then
4263 match state.layout with
4264 | [] -> state.y
4265 | l :: _ -> state.y - l.pagey
4266 else
4267 clamp (-conf.winh)
4268 | Glut.KEY_PAGE_DOWN ->
4269 if Glut.getModifiers () land Glut.active_ctrl != 0
4270 then
4271 match List.rev state.layout with
4272 | [] -> state.y
4273 | l :: _ -> getpagey l.pageno
4274 else
4275 clamp conf.winh
4276 | Glut.KEY_HOME ->
4277 addnav ();
4279 | Glut.KEY_END ->
4280 addnav ();
4281 state.maxy - (if conf.maxhfit then conf.winh else 0)
4283 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4284 Glut.getModifiers () land Glut.active_alt != 0 ->
4285 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4287 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4288 let dx =
4289 if Glut.getModifiers () land Glut.active_ctrl != 0
4290 then (conf.winw / 2)
4291 else 10
4293 state.x <- state.x - dx;
4294 state.y
4295 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4296 let dx =
4297 if Glut.getModifiers () land Glut.active_ctrl != 0
4298 then (conf.winw / 2)
4299 else 10
4301 state.x <- state.x + dx;
4302 state.y
4304 | _ -> state.y
4306 gotoy_and_clear_text y
4309 | Textentry te -> textentryspecial key te
4310 end;
4311 state.uioh
4313 method button button bstate x y =
4314 begin match state.mode with
4315 | View -> viewmouse button bstate x y
4316 | Birdseye beye -> birdseyemouse button bstate x y beye
4317 | Textentry _ -> ()
4318 end;
4319 state.uioh
4321 method motion x y =
4322 begin match state.mode with
4323 | Textentry _ -> ()
4324 | View | Birdseye _ ->
4325 match state.mstate with
4326 | Mzoom _ | Mnone -> ()
4328 | Mpan (x0, y0) ->
4329 let dx = x - x0
4330 and dy = y0 - y in
4331 state.mstate <- Mpan (x, y);
4332 if conf.zoom > 1.0 then state.x <- state.x + dx;
4333 let y = clamp dy in
4334 gotoy_and_clear_text y
4336 | Msel (a, _) ->
4337 state.mstate <- Msel (a, (x, y));
4338 G.postRedisplay "motion select";
4340 | Mscrolly ->
4341 let y = min conf.winh (max 0 y) in
4342 scrolly y
4344 | Mscrollx ->
4345 let x = min conf.winw (max 0 x) in
4346 scrollx x
4348 | Mzoomrect (p0, _) ->
4349 state.mstate <- Mzoomrect (p0, (x, y));
4350 G.postRedisplay "motion zoomrect";
4351 end;
4352 state.uioh
4354 method pmotion x y =
4355 begin match state.mode with
4356 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4357 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4358 let rec loop = function
4359 | [] ->
4360 if hooverpageno != -1
4361 then (
4362 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4363 G.postRedisplay "pmotion birdseye no hoover";
4365 | l :: rest ->
4366 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4367 && x > margin && x < margin + l.pagew
4368 then (
4369 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4370 G.postRedisplay "pmotion birdseye hoover";
4372 else loop rest
4374 loop state.layout
4376 | Textentry _ -> ()
4378 | View ->
4379 match state.mstate with
4380 | Mnone ->
4381 begin match getunder x y with
4382 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
4383 | Ulinkuri uri ->
4384 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
4385 Glut.setCursor Glut.CURSOR_INFO
4386 | Ulinkgoto (page, _) ->
4387 if conf.underinfo
4388 then showtext 'p' ("age: " ^ string_of_int (page+1));
4389 Glut.setCursor Glut.CURSOR_INFO
4390 | Utext s ->
4391 if conf.underinfo then showtext 'f' ("ont: " ^ s);
4392 Glut.setCursor Glut.CURSOR_TEXT
4395 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
4397 end;
4398 state.uioh
4400 method memusedchanged = ()
4401 end;;
4403 module Config =
4404 struct
4405 open Parser
4407 let fontpath = ref "";;
4408 let wmclasshack = ref false;;
4410 let unent s =
4411 let l = String.length s in
4412 let b = Buffer.create l in
4413 unent b s 0 l;
4414 Buffer.contents b;
4417 let home =
4419 match platform with
4420 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
4421 | _ -> Sys.getenv "HOME"
4422 with exn ->
4423 prerr_endline
4424 ("Can not determine home directory location: " ^
4425 Printexc.to_string exn);
4429 let config_of c attrs =
4430 let apply c k v =
4432 match k with
4433 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
4434 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
4435 | "case-insensitive-search" -> { c with icase = bool_of_string v }
4436 | "preload" -> { c with preload = bool_of_string v }
4437 | "page-bias" -> { c with pagebias = int_of_string v }
4438 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
4439 | "auto-scroll-step" ->
4440 { c with autoscrollstep = max 0 (int_of_string v) }
4441 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
4442 | "crop-hack" -> { c with crophack = bool_of_string v }
4443 | "throttle" -> { c with showall = bool_of_string v }
4444 | "highlight-links" -> { c with hlinks = bool_of_string v }
4445 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
4446 | "vertical-margin" ->
4447 { c with interpagespace = max 0 (int_of_string v) }
4448 | "zoom" ->
4449 let zoom = float_of_string v /. 100. in
4450 let zoom = max zoom 0.0 in
4451 { c with zoom = zoom }
4452 | "presentation" -> { c with presentation = bool_of_string v }
4453 | "rotation-angle" -> { c with angle = int_of_string v }
4454 | "width" -> { c with winw = max 20 (int_of_string v) }
4455 | "height" -> { c with winh = max 20 (int_of_string v) }
4456 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
4457 | "proportional-display" -> { c with proportional = bool_of_string v }
4458 | "pixmap-cache-size" ->
4459 { c with memlimit = max 2 (int_of_string_with_suffix v) }
4460 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
4461 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
4462 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
4463 | "persistent-location" -> { c with jumpback = bool_of_string v }
4464 | "background-color" -> { c with bgcolor = color_of_string v }
4465 | "scrollbar-in-presentation" ->
4466 { c with scrollbarinpm = bool_of_string v }
4467 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
4468 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
4469 | "memlimit" ->
4470 { c with mumemlimit = max 1024 (int_of_string_with_suffix v) }
4471 | "checkers" -> { c with checkers = bool_of_string v }
4472 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
4473 | "trim-margins" -> { c with trimmargins = bool_of_string v }
4474 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
4475 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
4476 | "uri-launcher" -> { c with urilauncher = unent v }
4477 | "color-space" -> { c with colorspace = colorspace_of_string v }
4478 | "invert-colors" -> { c with invert = bool_of_string v }
4479 | _ -> c
4480 with exn ->
4481 prerr_endline ("Error processing attribute (`" ^
4482 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
4485 let rec fold c = function
4486 | [] -> c
4487 | (k, v) :: rest ->
4488 let c = apply c k v in
4489 fold c rest
4491 fold c attrs;
4494 let fromstring f pos n v d =
4495 try f v
4496 with exn ->
4497 dolog "Error processing attribute (%S=%S) at %d\n%s"
4498 n v pos (Printexc.to_string exn)
4503 let bookmark_of attrs =
4504 let rec fold title page rely = function
4505 | ("title", v) :: rest -> fold v page rely rest
4506 | ("page", v) :: rest -> fold title v rely rest
4507 | ("rely", v) :: rest -> fold title page v rest
4508 | _ :: rest -> fold title page rely rest
4509 | [] -> title, page, rely
4511 fold "invalid" "0" "0" attrs
4514 let doc_of attrs =
4515 let rec fold path page rely pan = function
4516 | ("path", v) :: rest -> fold v page rely pan rest
4517 | ("page", v) :: rest -> fold path v rely pan rest
4518 | ("rely", v) :: rest -> fold path page v pan rest
4519 | ("pan", v) :: rest -> fold path page rely v rest
4520 | _ :: rest -> fold path page rely pan rest
4521 | [] -> path, page, rely, pan
4523 fold "" "0" "0" "0" attrs
4526 let setconf dst src =
4527 dst.scrollbw <- src.scrollbw;
4528 dst.scrollh <- src.scrollh;
4529 dst.icase <- src.icase;
4530 dst.preload <- src.preload;
4531 dst.pagebias <- src.pagebias;
4532 dst.verbose <- src.verbose;
4533 dst.scrollstep <- src.scrollstep;
4534 dst.maxhfit <- src.maxhfit;
4535 dst.crophack <- src.crophack;
4536 dst.autoscrollstep <- src.autoscrollstep;
4537 dst.showall <- src.showall;
4538 dst.hlinks <- src.hlinks;
4539 dst.underinfo <- src.underinfo;
4540 dst.interpagespace <- src.interpagespace;
4541 dst.zoom <- src.zoom;
4542 dst.presentation <- src.presentation;
4543 dst.angle <- src.angle;
4544 dst.winw <- src.winw;
4545 dst.winh <- src.winh;
4546 dst.savebmarks <- src.savebmarks;
4547 dst.memlimit <- src.memlimit;
4548 dst.proportional <- src.proportional;
4549 dst.texcount <- src.texcount;
4550 dst.sliceheight <- src.sliceheight;
4551 dst.thumbw <- src.thumbw;
4552 dst.jumpback <- src.jumpback;
4553 dst.bgcolor <- src.bgcolor;
4554 dst.scrollbarinpm <- src.scrollbarinpm;
4555 dst.tilew <- src.tilew;
4556 dst.tileh <- src.tileh;
4557 dst.mumemlimit <- src.mumemlimit;
4558 dst.checkers <- src.checkers;
4559 dst.aalevel <- src.aalevel;
4560 dst.trimmargins <- src.trimmargins;
4561 dst.trimfuzz <- src.trimfuzz;
4562 dst.urilauncher <- src.urilauncher;
4563 dst.colorspace <- src.colorspace;
4564 dst.invert <- src.invert;
4567 let get s =
4568 let h = Hashtbl.create 10 in
4569 let dc = { defconf with angle = defconf.angle } in
4570 let rec toplevel v t spos _ =
4571 match t with
4572 | Vdata | Vcdata | Vend -> v
4573 | Vopen ("llppconfig", _, closed) ->
4574 if closed
4575 then v
4576 else { v with f = llppconfig }
4577 | Vopen _ ->
4578 error "unexpected subelement at top level" s spos
4579 | Vclose _ -> error "unexpected close at top level" s spos
4581 and llppconfig v t spos _ =
4582 match t with
4583 | Vdata | Vcdata -> v
4584 | Vend -> error "unexpected end of input in llppconfig" s spos
4585 | Vopen ("defaults", attrs, closed) ->
4586 let c = config_of dc attrs in
4587 setconf dc c;
4588 if closed
4589 then v
4590 else { v with f = skip "defaults" (fun () -> v) }
4592 | Vopen ("ui-font", attrs, closed) ->
4593 let rec getsize size = function
4594 | [] -> size
4595 | ("size", v) :: rest ->
4596 let size =
4597 fromstring int_of_string spos "size" v !uifontsize in
4598 getsize size rest
4599 | l -> getsize size l
4601 uifontsize := getsize !uifontsize attrs;
4602 if closed
4603 then v
4604 else { v with f = uifont (Buffer.create 10) }
4606 | Vopen ("doc", attrs, closed) ->
4607 let pathent, spage, srely, span = doc_of attrs in
4608 let path = unent pathent
4609 and pageno = fromstring int_of_string spos "page" spage 0
4610 and rely = fromstring float_of_string spos "rely" srely 0.0
4611 and pan = fromstring int_of_string spos "pan" span 0 in
4612 let c = config_of dc attrs in
4613 let anchor = (pageno, rely) in
4614 if closed
4615 then (Hashtbl.add h path (c, [], pan, anchor); v)
4616 else { v with f = doc path pan anchor c [] }
4618 | Vopen _ ->
4619 error "unexpected subelement in llppconfig" s spos
4621 | Vclose "llppconfig" -> { v with f = toplevel }
4622 | Vclose _ -> error "unexpected close in llppconfig" s spos
4624 and uifont b v t spos epos =
4625 match t with
4626 | Vdata | Vcdata ->
4627 Buffer.add_substring b s spos (epos - spos);
4629 | Vopen (_, _, _) ->
4630 error "unexpected subelement in ui-font" s spos
4631 | Vclose "ui-font" ->
4632 if String.length !fontpath = 0
4633 then fontpath := Buffer.contents b;
4634 { v with f = llppconfig }
4635 | Vclose _ -> error "unexpected close in ui-font" s spos
4636 | Vend -> error "unexpected end of input in ui-font" s spos
4638 and doc path pan anchor c bookmarks v t spos _ =
4639 match t with
4640 | Vdata | Vcdata -> v
4641 | Vend -> error "unexpected end of input in doc" s spos
4642 | Vopen ("bookmarks", _, closed) ->
4643 if closed
4644 then v
4645 else { v with f = pbookmarks path pan anchor c bookmarks }
4647 | Vopen (_, _, _) ->
4648 error "unexpected subelement in doc" s spos
4650 | Vclose "doc" ->
4651 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
4652 { v with f = llppconfig }
4654 | Vclose _ -> error "unexpected close in doc" s spos
4656 and pbookmarks path pan anchor c bookmarks v t spos _ =
4657 match t with
4658 | Vdata | Vcdata -> v
4659 | Vend -> error "unexpected end of input in bookmarks" s spos
4660 | Vopen ("item", attrs, closed) ->
4661 let titleent, spage, srely = bookmark_of attrs in
4662 let page = fromstring int_of_string spos "page" spage 0
4663 and rely = fromstring float_of_string spos "rely" srely 0.0 in
4664 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
4665 if closed
4666 then { v with f = pbookmarks path pan anchor c bookmarks }
4667 else
4668 let f () = v in
4669 { v with f = skip "item" f }
4671 | Vopen _ ->
4672 error "unexpected subelement in bookmarks" s spos
4674 | Vclose "bookmarks" ->
4675 { v with f = doc path pan anchor c bookmarks }
4677 | Vclose _ -> error "unexpected close in bookmarks" s spos
4679 and skip tag f v t spos _ =
4680 match t with
4681 | Vdata | Vcdata -> v
4682 | Vend ->
4683 error ("unexpected end of input in skipped " ^ tag) s spos
4684 | Vopen (tag', _, closed) ->
4685 if closed
4686 then v
4687 else
4688 let f' () = { v with f = skip tag f } in
4689 { v with f = skip tag' f' }
4690 | Vclose ctag ->
4691 if tag = ctag
4692 then f ()
4693 else error ("unexpected close in skipped " ^ tag) s spos
4696 parse { f = toplevel; accu = () } s;
4697 h, dc;
4700 let do_load f ic =
4702 let len = in_channel_length ic in
4703 let s = String.create len in
4704 really_input ic s 0 len;
4705 f s;
4706 with
4707 | Parse_error (msg, s, pos) ->
4708 let subs = subs s pos in
4709 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
4710 failwith ("parse error: " ^ s)
4712 | exn ->
4713 failwith ("config load error: " ^ Printexc.to_string exn)
4716 let defconfpath =
4717 let dir =
4719 let dir = Filename.concat home ".config" in
4720 if Sys.is_directory dir then dir else home
4721 with _ -> home
4723 Filename.concat dir "llpp.conf"
4726 let confpath = ref defconfpath;;
4728 let load1 f =
4729 if Sys.file_exists !confpath
4730 then
4731 match
4732 (try Some (open_in_bin !confpath)
4733 with exn ->
4734 prerr_endline
4735 ("Error opening configuation file `" ^ !confpath ^ "': " ^
4736 Printexc.to_string exn);
4737 None
4739 with
4740 | Some ic ->
4741 begin try
4742 f (do_load get ic)
4743 with exn ->
4744 prerr_endline
4745 ("Error loading configuation from `" ^ !confpath ^ "': " ^
4746 Printexc.to_string exn);
4747 end;
4748 close_in ic;
4750 | None -> ()
4751 else
4752 f (Hashtbl.create 0, defconf)
4755 let load () =
4756 let f (h, dc) =
4757 let pc, pb, px, pa =
4759 Hashtbl.find h (Filename.basename state.path)
4760 with Not_found -> dc, [], 0, (0, 0.0)
4762 setconf defconf dc;
4763 setconf conf pc;
4764 state.bookmarks <- pb;
4765 state.x <- px;
4766 state.scrollw <- conf.scrollbw;
4767 if conf.jumpback
4768 then state.anchor <- pa;
4769 cbput state.hists.nav pa;
4771 load1 f
4774 let add_attrs bb always dc c =
4775 let ob s a b =
4776 if always || a != b
4777 then Printf.bprintf bb "\n %s='%b'" s a
4778 and oi s a b =
4779 if always || a != b
4780 then Printf.bprintf bb "\n %s='%d'" s a
4781 and oI s a b =
4782 if always || a != b
4783 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
4784 and oz s a b =
4785 if always || a <> b
4786 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
4787 and oc s a b =
4788 if always || a <> b
4789 then
4790 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
4791 and oC s a b =
4792 if always || a <> b
4793 then
4794 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
4795 and oR s a b =
4796 if always || a <> b
4797 then
4798 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
4799 and os s a b =
4800 if always || a <> b
4801 then
4802 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
4804 let w, h =
4805 if always
4806 then dc.winw, dc.winh
4807 else
4808 match state.fullscreen with
4809 | Some wh -> wh
4810 | None -> c.winw, c.winh
4812 let zoom, presentation, interpagespace, showall=
4813 if always
4814 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
4815 else
4816 match state.mode with
4817 | Birdseye (bc, _, _, _, _) ->
4818 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
4819 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
4821 oi "width" w dc.winw;
4822 oi "height" h dc.winh;
4823 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
4824 oi "scroll-handle-height" c.scrollh dc.scrollh;
4825 ob "case-insensitive-search" c.icase dc.icase;
4826 ob "preload" c.preload dc.preload;
4827 oi "page-bias" c.pagebias dc.pagebias;
4828 oi "scroll-step" c.scrollstep dc.scrollstep;
4829 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
4830 ob "max-height-fit" c.maxhfit dc.maxhfit;
4831 ob "crop-hack" c.crophack dc.crophack;
4832 ob "throttle" showall dc.showall;
4833 ob "highlight-links" c.hlinks dc.hlinks;
4834 ob "under-cursor-info" c.underinfo dc.underinfo;
4835 oi "vertical-margin" interpagespace dc.interpagespace;
4836 oz "zoom" zoom dc.zoom;
4837 ob "presentation" presentation dc.presentation;
4838 oi "rotation-angle" c.angle dc.angle;
4839 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
4840 ob "proportional-display" c.proportional dc.proportional;
4841 oI "pixmap-cache-size" c.memlimit dc.memlimit;
4842 oi "tex-count" c.texcount dc.texcount;
4843 oi "slice-height" c.sliceheight dc.sliceheight;
4844 oi "thumbnail-width" c.thumbw dc.thumbw;
4845 ob "persistent-location" c.jumpback dc.jumpback;
4846 oc "background-color" c.bgcolor dc.bgcolor;
4847 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
4848 oi "tile-width" c.tilew dc.tilew;
4849 oi "tile-height" c.tileh dc.tileh;
4850 oI "mupdf-memlimit" c.mumemlimit dc.mumemlimit;
4851 ob "checkers" c.checkers dc.checkers;
4852 oi "aalevel" c.aalevel dc.aalevel;
4853 ob "trim-margins" c.trimmargins dc.trimmargins;
4854 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
4855 os "uri-launcher" c.urilauncher dc.urilauncher;
4856 oC "color-space" c.colorspace dc.colorspace;
4857 ob "invert-colors" c.invert dc.invert;
4858 if always
4859 then ob "wmclass-hack" !wmclasshack false;
4862 let save () =
4863 let uifontsize = !uifontsize in
4864 let bb = Buffer.create 32768 in
4865 let f (h, dc) =
4866 let dc = if conf.bedefault then conf else dc in
4867 Buffer.add_string bb "<llppconfig>\n";
4869 if String.length !fontpath > 0
4870 then
4871 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
4872 uifontsize
4873 !fontpath
4874 else (
4875 if uifontsize <> 14
4876 then
4877 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
4880 Buffer.add_string bb "<defaults ";
4881 add_attrs bb true dc dc;
4882 Buffer.add_string bb "/>\n";
4884 let adddoc path pan anchor c bookmarks =
4885 if bookmarks == [] && c = dc && anchor = emptyanchor
4886 then ()
4887 else (
4888 Printf.bprintf bb "<doc path='%s'"
4889 (enent path 0 (String.length path));
4891 if anchor <> emptyanchor
4892 then (
4893 let n, y = anchor in
4894 Printf.bprintf bb " page='%d'" n;
4895 if y > 1e-6
4896 then
4897 Printf.bprintf bb " rely='%f'" y
4901 if pan != 0
4902 then Printf.bprintf bb " pan='%d'" pan;
4904 add_attrs bb false dc c;
4906 begin match bookmarks with
4907 | [] -> Buffer.add_string bb "/>\n"
4908 | _ ->
4909 Buffer.add_string bb ">\n<bookmarks>\n";
4910 List.iter (fun (title, _level, (page, rely)) ->
4911 Printf.bprintf bb
4912 "<item title='%s' page='%d'"
4913 (enent title 0 (String.length title))
4914 page
4916 if rely > 1e-6
4917 then
4918 Printf.bprintf bb " rely='%f'" rely
4920 Buffer.add_string bb "/>\n";
4921 ) bookmarks;
4922 Buffer.add_string bb "</bookmarks>\n</doc>\n";
4923 end;
4927 let pan =
4928 match state.mode with
4929 | Birdseye (_, pan, _, _, _) -> pan
4930 | _ -> state.x
4932 let basename = Filename.basename state.path in
4933 adddoc basename pan (getanchor ())
4934 { conf with
4935 autoscrollstep =
4936 match state.autoscroll with
4937 | Some step -> step
4938 | None -> conf.autoscrollstep }
4939 (if conf.savebmarks then state.bookmarks else []);
4941 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
4942 if basename <> path
4943 then adddoc path x y c bookmarks
4944 ) h;
4945 Buffer.add_string bb "</llppconfig>";
4947 load1 f;
4948 if Buffer.length bb > 0
4949 then
4951 let tmp = !confpath ^ ".tmp" in
4952 let oc = open_out_bin tmp in
4953 Buffer.output_buffer oc bb;
4954 close_out oc;
4955 Unix.rename tmp !confpath;
4956 with exn ->
4957 prerr_endline
4958 ("error while saving configuration: " ^ Printexc.to_string exn)
4960 end;;
4962 let () =
4963 Arg.parse
4964 (Arg.align
4965 [("-p", Arg.String (fun s -> state.password <- s) ,
4966 "<password> Set password");
4968 ("-f", Arg.String (fun s -> Config.fontpath := s),
4969 "<path> Set path to the user interface font");
4971 ("-c", Arg.String (fun s -> Config.confpath := s),
4972 "<path> Set path to the configuration file");
4974 ("-v", Arg.Unit (fun () ->
4975 Printf.printf
4976 "%s\nconfiguration path: %s\n"
4977 Help.version
4978 Config.defconfpath
4980 exit 0), " Print version and exit");
4983 (fun s -> state.path <- s)
4984 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
4986 if String.length state.path = 0
4987 then (prerr_endline "file name missing"; exit 1);
4989 Config.load ();
4991 let _ = Glut.init Sys.argv in
4992 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
4993 let () = Glut.initWindowSize conf.winw conf.winh in
4994 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
4996 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
4997 || Glut.extensionSupported "GL_EXT_texture_rectangle")
4998 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5000 let csock, ssock =
5001 if not is_windows
5002 then
5003 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5004 else
5005 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5006 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5007 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5008 Unix.bind sock addr;
5009 Unix.listen sock 1;
5010 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5011 Unix.connect csock addr;
5012 let ssock, _ = Unix.accept sock in
5013 Unix.close sock;
5014 let opts sock =
5015 Unix.setsockopt sock Unix.TCP_NODELAY true;
5016 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5018 opts ssock;
5019 opts csock;
5020 ssock, csock
5023 let () = Glut.displayFunc display in
5024 let () = Glut.reshapeFunc reshape in
5025 let () = Glut.keyboardFunc keyboard in
5026 let () = Glut.specialFunc special in
5027 let () = Glut.idleFunc (Some idle) in
5028 let () = Glut.mouseFunc mouse in
5029 let () = Glut.motionFunc motion in
5030 let () = Glut.passiveMotionFunc pmotion in
5032 setcheckers conf.checkers;
5033 init ssock (
5034 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5035 conf.texcount, conf.sliceheight, conf.mumemlimit, conf.colorspace,
5036 !Config.wmclasshack, !Config.fontpath
5038 state.csock <- csock;
5039 state.ssock <- ssock;
5040 state.text <- "Opening " ^ state.path;
5041 setaalevel conf.aalevel;
5042 writeopen state.path state.password;
5043 state.uioh <- uioh;
5045 while true do
5047 Glut.mainLoop ();
5048 with
5049 | Glut.BadEnum "key in special_of_int" ->
5050 showtext '!' " LablGlut bug: special key not recognized";
5052 | Quit ->
5053 wcmd "quit" [];
5054 Config.save ();
5055 exit 0
5056 done;