Remove unused scrollcolor
[llpp.git] / main.ml
blob63ed786b7f058633c1fefc59a1093f0d78f50724
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;;
9 let dolog2 fmt = Printf.kprintf print_endline fmt;;
10 let now = Unix.gettimeofday;;
12 exception Quit;;
14 type params = (angle * proportional * trimparams
15 * texcount * sliceheight * memsize
16 * colorspace * wmclasshack * fontpath)
17 and pageno = int
18 and width = int
19 and height = int
20 and leftx = int
21 and opaque = string
22 and recttype = int
23 and pixmapsize = int
24 and angle = int
25 and proportional = bool
26 and trimmargins = bool
27 and interpagespace = int
28 and texcount = int
29 and sliceheight = int
30 and gen = int
31 and top = float
32 and fontpath = string
33 and memsize = int
34 and aalevel = int
35 and wmclasshack = bool
36 and irect = (int * int * int * int)
37 and trimparams = (trimmargins * irect)
38 and colorspace = | Rgb | Bgr | Gray
41 type platform = | Punknown | Plinux | Pwindows | Posx | Psun
42 | Pfreebsd | Pdragonflybsd | Popenbsd | Pmingw | Pcygwin;;
44 external init : Unix.file_descr -> params -> unit = "ml_init";;
45 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
46 external copysel : string -> unit = "ml_copysel";;
47 external getpdimrect : int -> float array = "ml_getpdimrect";;
48 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
49 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
50 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
51 external measurestr : int -> string -> float = "ml_measure_string";;
52 external getmaxw : unit -> float = "ml_getmaxw";;
53 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
54 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
55 external platform : unit -> platform = "ml_platform";;
56 external setaalevel : int -> unit = "ml_setaalevel";;
58 let platform_to_string = function
59 | Punknown -> "unknown"
60 | Plinux -> "Linux"
61 | Pwindows -> "Windows"
62 | Posx -> "OSX"
63 | Psun -> "Sun"
64 | Pfreebsd -> "FreeBSD"
65 | Pdragonflybsd -> "DragonflyBSD"
66 | Popenbsd -> "OpenBSD"
67 | Pcygwin -> "Cygwin"
68 | Pmingw -> "MingW"
71 let platform = platform ();;
73 let is_windows =
74 match platform with
75 | Pwindows | Pmingw -> true
76 | _ -> false
79 type x = int
80 and y = int
81 and tilex = int
82 and tiley = int
83 and tileparams = (x * y * width * height * tilex * tiley)
86 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
88 type mpos = int * int
89 and mstate =
90 | Msel of (mpos * mpos)
91 | Mpan of mpos
92 | Mscrolly | Mscrollx
93 | Mzoom of (int * int)
94 | Mzoomrect of (mpos * mpos)
95 | Mnone
98 type textentry = string * string * onhist option * onkey * ondone
99 and onkey = string -> int -> te
100 and ondone = string -> unit
101 and histcancel = unit -> unit
102 and onhist = ((histcmd -> string) * histcancel)
103 and histcmd = HCnext | HCprev | HCfirst | HClast
104 and te =
105 | TEstop
106 | TEdone of string
107 | TEcont of string
108 | TEswitch of textentry
111 type 'a circbuf =
112 { store : 'a array
113 ; mutable rc : int
114 ; mutable wc : int
115 ; mutable len : int
119 let bound v minv maxv =
120 max minv (min maxv v);
123 let cbnew n v =
124 { store = Array.create n v
125 ; rc = 0
126 ; wc = 0
127 ; len = 0
131 let drawstring size x y s =
132 Gl.enable `blend;
133 Gl.enable `texture_2d;
134 ignore (drawstr size x y s);
135 Gl.disable `blend;
136 Gl.disable `texture_2d;
139 let drawstring1 size x y s =
140 drawstr size x y s;
143 let drawstring2 size x y fmt =
144 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
147 let cbcap b = Array.length b.store;;
149 let cbput b v =
150 let cap = cbcap b in
151 b.store.(b.wc) <- v;
152 b.wc <- (b.wc + 1) mod cap;
153 b.rc <- b.wc;
154 b.len <- min (b.len + 1) cap;
157 let cbempty b = b.len = 0;;
159 let cbgetg b circular dir =
160 if cbempty b
161 then b.store.(0)
162 else
163 let rc = b.rc + dir in
164 let rc =
165 if circular
166 then (
167 if rc = -1
168 then b.len-1
169 else (
170 if rc = b.len
171 then 0
172 else rc
175 else max 0 (min rc (b.len-1))
177 b.rc <- rc;
178 b.store.(rc);
181 let cbget b = cbgetg b false;;
182 let cbgetc b = cbgetg b true;;
184 type page =
185 { pageno : int
186 ; pagedimno : int
187 ; pagew : int
188 ; pageh : int
189 ; pagex : int
190 ; pagey : int
191 ; pagevw : int
192 ; pagevh : int
193 ; pagedispx : int
194 ; pagedispy : int
198 let debugl l =
199 dolog "l %d dim=%d {" l.pageno l.pagedimno;
200 dolog " WxH %dx%d" l.pagew l.pageh;
201 dolog " vWxH %dx%d" l.pagevw l.pagevh;
202 dolog " pagex,y %d,%d" l.pagex l.pagey;
203 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
204 dolog "}";
207 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
208 dolog "rect {";
209 dolog " x0,y0=(% f, % f)" x0 y0;
210 dolog " x1,y1=(% f, % f)" x1 y1;
211 dolog " x2,y2=(% f, % f)" x2 y2;
212 dolog " x3,y3=(% f, % f)" x3 y3;
213 dolog "}";
216 type conf =
217 { mutable scrollbw : int
218 ; mutable scrollh : int
219 ; mutable icase : bool
220 ; mutable preload : bool
221 ; mutable pagebias : int
222 ; mutable verbose : bool
223 ; mutable debug : bool
224 ; mutable scrollstep : int
225 ; mutable maxhfit : bool
226 ; mutable crophack : bool
227 ; mutable autoscrollstep : int
228 ; mutable maxwait : float option
229 ; mutable hlinks : bool
230 ; mutable underinfo : bool
231 ; mutable interpagespace : interpagespace
232 ; mutable zoom : float
233 ; mutable presentation : bool
234 ; mutable angle : angle
235 ; mutable winw : int
236 ; mutable winh : int
237 ; mutable savebmarks : bool
238 ; mutable proportional : proportional
239 ; mutable trimmargins : trimmargins
240 ; mutable trimfuzz : irect
241 ; mutable memlimit : memsize
242 ; mutable texcount : texcount
243 ; mutable sliceheight : sliceheight
244 ; mutable thumbw : width
245 ; mutable jumpback : bool
246 ; mutable bgcolor : float * float * float
247 ; mutable bedefault : bool
248 ; mutable scrollbarinpm : bool
249 ; mutable tilew : int
250 ; mutable tileh : int
251 ; mutable mumemlimit : memsize
252 ; mutable checkers : bool
253 ; mutable aalevel : int
254 ; mutable urilauncher : string
255 ; mutable colorspace : colorspace
256 ; mutable invert : bool
257 ; mutable colorscale : float
258 ; mutable redirectstderr : bool
262 type anchor = pageno * top;;
264 type outline = string * int * anchor;;
266 type rect = float * float * float * float * float * float * float * float;;
268 type tile = opaque * pixmapsize * elapsed
269 and elapsed = float;;
270 type pagemapkey = pageno * gen;;
271 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
272 and row = int
273 and col = int;;
275 let emptyanchor = (0, 0.0);;
277 type infochange = | Memused | Docinfo | Pdim;;
279 class type uioh = object
280 method display : unit
281 method key : int -> uioh
282 method special : Glut.special_key_t -> uioh
283 method button :
284 Glut.button_t -> Glut.mouse_button_state_t -> int -> int -> uioh
285 method motion : int -> int -> uioh
286 method pmotion : int -> int -> uioh
287 method infochanged : infochange -> unit
288 method scrollpw : (int * float * float)
289 method scrollph : (int * float * float)
290 end;;
292 type mode =
293 | Birdseye of (conf * leftx * pageno * pageno * anchor)
294 | Textentry of (textentry * onleave)
295 | View
296 and onleave = leavetextentrystatus -> unit
297 and leavetextentrystatus = | Cancel | Confirm
298 and helpitem = string * int * action
299 and action =
300 | Noaction
301 | Action of (uioh -> uioh)
304 let isbirdseye = function Birdseye _ -> true | _ -> false;;
305 let istextentry = function Textentry _ -> true | _ -> false;;
307 type currently =
308 | Idle
309 | Loading of (page * gen)
310 | Tiling of (
311 page * opaque * colorspace * angle * gen * col * row * width * height
313 | Outlining of outline list
316 let nouioh : uioh = object (self)
317 method display = ()
318 method key _ = self
319 method special _ = self
320 method button _ _ _ _ = self
321 method motion _ _ = self
322 method pmotion _ _ = self
323 method infochanged _ = ()
324 method scrollpw = (0, nan, nan)
325 method scrollph = (0, nan, nan)
326 end;;
328 type state =
329 { mutable csock : Unix.file_descr
330 ; mutable ssock : Unix.file_descr
331 ; mutable errfd : Unix.file_descr
332 ; mutable stderr : Unix.file_descr
333 ; mutable errmsgs : Buffer.t
334 ; mutable newerrmsgs : bool
335 ; mutable w : int
336 ; mutable x : int
337 ; mutable y : int
338 ; mutable scrollw : int
339 ; mutable hscrollh : int
340 ; mutable anchor : anchor
341 ; mutable maxy : int
342 ; mutable layout : page list
343 ; pagemap : (pagemapkey, opaque) Hashtbl.t
344 ; tilemap : (tilemapkey, tile) Hashtbl.t
345 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
346 ; mutable pdims : (pageno * width * height * leftx) list
347 ; mutable pagecount : int
348 ; mutable currently : currently
349 ; mutable mstate : mstate
350 ; mutable searchpattern : string
351 ; mutable rects : (pageno * recttype * rect) list
352 ; mutable rects1 : (pageno * recttype * rect) list
353 ; mutable text : string
354 ; mutable fullscreen : (width * height) option
355 ; mutable mode : mode
356 ; mutable uioh : uioh
357 ; mutable outlines : outline array
358 ; mutable bookmarks : outline list
359 ; mutable path : string
360 ; mutable password : string
361 ; mutable invalidated : int
362 ; mutable memused : memsize
363 ; mutable gen : gen
364 ; mutable throttle : (page list * int * float) option
365 ; mutable autoscroll : int option
366 ; mutable help : helpitem array
367 ; mutable docinfo : (int * string) list
368 ; mutable deadline : float
369 ; mutable texid : GlTex.texture_id option
370 ; hists : hists
371 ; mutable prevzoom : float
372 ; mutable progress : float
374 and hists =
375 { pat : string circbuf
376 ; pag : string circbuf
377 ; nav : anchor circbuf
381 let defconf =
382 { scrollbw = 7
383 ; scrollh = 12
384 ; icase = true
385 ; preload = true
386 ; pagebias = 0
387 ; verbose = false
388 ; debug = false
389 ; scrollstep = 24
390 ; maxhfit = true
391 ; crophack = false
392 ; autoscrollstep = 2
393 ; maxwait = None
394 ; hlinks = false
395 ; underinfo = false
396 ; interpagespace = 2
397 ; zoom = 1.0
398 ; presentation = false
399 ; angle = 0
400 ; winw = 900
401 ; winh = 900
402 ; savebmarks = true
403 ; proportional = true
404 ; trimmargins = false
405 ; trimfuzz = (0,0,0,0)
406 ; memlimit = 32 lsl 20
407 ; texcount = 256
408 ; sliceheight = 24
409 ; thumbw = 76
410 ; jumpback = true
411 ; bgcolor = (0.5, 0.5, 0.5)
412 ; bedefault = false
413 ; scrollbarinpm = true
414 ; tilew = 2048
415 ; tileh = 2048
416 ; mumemlimit = 128 lsl 20
417 ; checkers = true
418 ; aalevel = 8
419 ; urilauncher =
420 (match platform with
421 | Plinux | Pfreebsd | Pdragonflybsd | Popenbsd | Psun -> "xdg-open \"%s\""
422 | Posx -> "open \"%s\""
423 | Pwindows | Pcygwin | Pmingw -> "iexplore \"%s\""
424 | _ -> "")
425 ; colorspace = Rgb
426 ; invert = false
427 ; colorscale = 1.0
428 ; redirectstderr = false
432 let conf = { defconf with angle = defconf.angle };;
434 type fontstate =
435 { mutable fontsize : int
436 ; mutable wwidth : float
437 ; mutable maxrows : int
441 let fstate =
442 { fontsize = 14
443 ; wwidth = nan
444 ; maxrows = -1
448 let setfontsize n =
449 fstate.fontsize <- n;
450 fstate.wwidth <- measurestr fstate.fontsize "w";
451 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
454 let gotouri uri =
455 if String.length conf.urilauncher = 0
456 then print_endline uri
457 else
458 let re = Str.regexp "%s" in
459 let command = Str.global_replace re uri conf.urilauncher in
460 let optic =
461 try Some (Unix.open_process_in command)
462 with exn ->
463 Printf.eprintf
464 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
465 flush stderr;
466 None
468 match optic with
469 | Some ic -> close_in ic
470 | None -> ()
473 let version () =
474 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
475 (platform_to_string platform) Sys.word_size Sys.ocaml_version
478 let makehelp () =
479 let strings = version () :: "" :: Help.keys in
480 Array.of_list (
481 let r = Str.regexp "\\(http://[^ ]+\\)" in
482 List.map (fun s ->
483 if (try Str.search_forward r s 0 with Not_found -> -1) >= 0
484 then
485 let uri = Str.matched_string s in
486 (s, 0, Action (fun u -> gotouri uri; u))
487 else s, 0, Noaction) strings
491 let state =
492 { csock = Unix.stdin
493 ; ssock = Unix.stdin
494 ; errfd = Unix.stdin
495 ; stderr = Unix.stderr
496 ; errmsgs = Buffer.create 0
497 ; newerrmsgs = false
498 ; x = 0
499 ; y = 0
500 ; w = 0
501 ; scrollw = 0
502 ; hscrollh = 0
503 ; anchor = emptyanchor
504 ; layout = []
505 ; maxy = max_int
506 ; tilelru = Queue.create ()
507 ; pagemap = Hashtbl.create 10
508 ; tilemap = Hashtbl.create 10
509 ; pdims = []
510 ; pagecount = 0
511 ; currently = Idle
512 ; mstate = Mnone
513 ; rects = []
514 ; rects1 = []
515 ; text = ""
516 ; mode = View
517 ; fullscreen = None
518 ; searchpattern = ""
519 ; outlines = [||]
520 ; bookmarks = []
521 ; path = ""
522 ; password = ""
523 ; invalidated = 0
524 ; hists =
525 { nav = cbnew 10 (0, 0.0)
526 ; pat = cbnew 1 ""
527 ; pag = cbnew 1 ""
529 ; memused = 0
530 ; gen = 0
531 ; throttle = None
532 ; autoscroll = None
533 ; help = makehelp ()
534 ; docinfo = []
535 ; deadline = nan
536 ; texid = None
537 ; prevzoom = 1.0
538 ; progress = -1.0
539 ; uioh = nouioh
543 let vlog fmt =
544 if conf.verbose
545 then
546 Printf.kprintf prerr_endline fmt
547 else
548 Printf.kprintf ignore fmt
551 let redirectstderr () =
552 if conf.redirectstderr
553 then
554 let rfd, wfd = Unix.pipe () in
555 state.stderr <- Unix.dup Unix.stderr;
556 state.errfd <- rfd;
557 Unix.dup2 wfd Unix.stderr;
558 else (
559 state.newerrmsgs <- false;
560 Unix.dup2 state.stderr Unix.stderr;
561 prerr_string (Buffer.contents state.errmsgs);
562 flush stderr;
563 Buffer.clear state.errmsgs;
567 module G =
568 struct
569 let postRedisplay who =
570 if conf.verbose
571 then prerr_endline ("redisplay for " ^ who);
572 Glut.postRedisplay ();
574 end;;
576 let addchar s c =
577 let b = Buffer.create (String.length s + 1) in
578 Buffer.add_string b s;
579 Buffer.add_char b c;
580 Buffer.contents b;
583 let colorspace_of_string s =
584 match String.lowercase s with
585 | "rgb" -> Rgb
586 | "bgr" -> Bgr
587 | "gray" -> Gray
588 | _ -> failwith "invalid colorspace"
591 let int_of_colorspace = function
592 | Rgb -> 0
593 | Bgr -> 1
594 | Gray -> 2
597 let colorspace_of_int = function
598 | 0 -> Rgb
599 | 1 -> Bgr
600 | 2 -> Gray
601 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
604 let colorspace_to_string = function
605 | Rgb -> "rgb"
606 | Bgr -> "bgr"
607 | Gray -> "gray"
610 let intentry_with_suffix text key =
611 let c = Char.unsafe_chr key in
612 match Char.lowercase c with
613 | '0' .. '9' ->
614 let text = addchar text c in
615 TEcont text
617 | 'k' | 'm' | 'g' ->
618 let text = addchar text c in
619 TEcont text
621 | _ ->
622 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
623 TEcont text
626 let writecmd fd s =
627 let len = String.length s in
628 let n = 4 + len in
629 let b = Buffer.create n in
630 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
631 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
632 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
633 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
634 Buffer.add_string b s;
635 let s' = Buffer.contents b in
636 let n' = Unix.write fd s' 0 n in
637 if n' != n then failwith "write failed";
640 let readcmd fd =
641 let s = "xxxx" in
642 let n = Unix.read fd s 0 4 in
643 if n != 4 then failwith "incomplete read(len)";
644 let len = 0
645 lor (Char.code s.[0] lsl 24)
646 lor (Char.code s.[1] lsl 16)
647 lor (Char.code s.[2] lsl 8)
648 lor (Char.code s.[3] lsl 0)
650 let s = String.create len in
651 let n = Unix.read fd s 0 len in
652 if n != len then failwith "incomplete read(data)";
656 let makecmd s l =
657 let b = Buffer.create 10 in
658 Buffer.add_string b s;
659 let rec combine = function
660 | [] -> b
661 | x :: xs ->
662 Buffer.add_char b ' ';
663 let s =
664 match x with
665 | `b b -> if b then "1" else "0"
666 | `s s -> s
667 | `i i -> string_of_int i
668 | `f f -> string_of_float f
669 | `I f -> string_of_int (truncate f)
671 Buffer.add_string b s;
672 combine xs;
674 combine l;
677 let wcmd s l =
678 let cmd = Buffer.contents (makecmd s l) in
679 writecmd state.csock cmd;
682 let calcips h =
683 if conf.presentation
684 then
685 let d = conf.winh - h in
686 max 0 ((d + 1) / 2)
687 else
688 conf.interpagespace
691 let calcheight () =
692 let rec f pn ph pi fh l =
693 match l with
694 | (n, _, h, _) :: rest ->
695 let ips = calcips h in
696 let fh =
697 if conf.presentation
698 then fh+ips
699 else (
700 if isbirdseye state.mode && pn = 0
701 then fh + ips
702 else fh
705 let fh = fh + ((n - pn) * (ph + pi)) in
706 f n h ips fh rest;
708 | [] ->
709 let inc =
710 if conf.presentation || (isbirdseye state.mode && pn = 0)
711 then 0
712 else -pi
714 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
715 max 0 fh
717 let fh = f 0 0 0 0 state.pdims in
721 let getpageyh pageno =
722 let rec f pn ph pi y l =
723 match l with
724 | (n, _, h, _) :: rest ->
725 let ips = calcips h in
726 if n >= pageno
727 then
728 let h = if n = pageno then h else ph in
729 if conf.presentation && n = pageno
730 then
731 y + (pageno - pn) * (ph + pi) + pi, h
732 else
733 y + (pageno - pn) * (ph + pi), h
734 else
735 let y = y + (if conf.presentation then pi else 0) in
736 let y = y + (n - pn) * (ph + pi) in
737 f n h ips y rest
739 | [] ->
740 y + (pageno - pn) * (ph + pi), ph
742 f 0 0 0 0 state.pdims
745 let getpagedim pageno =
746 let rec f ppdim l =
747 match l with
748 | (n, _, _, _) as pdim :: rest ->
749 if n >= pageno
750 then (if n = pageno then pdim else ppdim)
751 else f pdim rest
753 | [] -> ppdim
755 f (-1, -1, -1, -1) state.pdims
758 let getpagey pageno = fst (getpageyh pageno);;
760 let layout y sh =
761 let sh = sh - state.hscrollh in
762 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
763 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
764 match pdims with
765 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
766 let ips = calcips h in
767 let yinc =
768 if conf.presentation || (isbirdseye state.mode && pageno = 0)
769 then ips
770 else 0
772 (w, h, ips, xoff), rest, pdimno + 1, yinc
773 | _ ->
774 prev, pdims, pdimno, 0
776 let dy = dy + yinc in
777 let py = py + yinc in
778 if pageno = state.pagecount || dy >= sh
779 then
780 accu
781 else
782 let vy = y + dy in
783 if py + h <= vy - yinc
784 then
785 let py = py + h + ips in
786 let dy = max 0 (py - y) in
787 f ~pageno:(pageno+1)
788 ~pdimno
789 ~prev:curr
792 ~pdims:rest
793 ~accu
794 else
795 let pagey = vy - py in
796 let pagevh = h - pagey in
797 let pagevh = min (sh - dy) pagevh in
798 let off = if yinc > 0 then py - vy else 0 in
799 let py = py + h + ips in
800 let pagex, dx =
801 let xoff = xoff +
802 if state.w < conf.winw - state.scrollw
803 then (conf.winw - state.scrollw - state.w) / 2
804 else 0
806 let dispx = xoff + state.x in
807 if dispx < 0
808 then (-dispx, 0)
809 else (0, dispx)
811 let pagevw =
812 let lw = w - pagex in
813 min lw (conf.winw - state.scrollw)
815 let e =
816 { pageno = pageno
817 ; pagedimno = pdimno
818 ; pagew = w
819 ; pageh = h
820 ; pagex = pagex
821 ; pagey = pagey + off
822 ; pagevw = pagevw
823 ; pagevh = pagevh - off
824 ; pagedispx = dx
825 ; pagedispy = dy + off
828 let accu = e :: accu in
829 f ~pageno:(pageno+1)
830 ~pdimno
831 ~prev:curr
833 ~dy:(dy+pagevh+ips)
834 ~pdims:rest
835 ~accu
837 if state.invalidated = 0
838 then (
839 let accu =
841 ~pageno:0
842 ~pdimno:~-1
843 ~prev:(0,0,0,0)
844 ~py:0
845 ~dy:0
846 ~pdims:state.pdims
847 ~accu:[]
849 List.rev accu
851 else
855 let clamp incr =
856 let y = state.y + incr in
857 let y = max 0 y in
858 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
862 let getopaque pageno =
863 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
864 with Not_found -> None
867 let putopaque pageno opaque =
868 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
871 let itertiles l f =
872 let tilex = l.pagex mod conf.tilew in
873 let tiley = l.pagey mod conf.tileh in
875 let col = l.pagex / conf.tilew in
876 let row = l.pagey / conf.tileh in
878 let vw =
879 let a = l.pagew - l.pagex in
880 let b = conf.winw - state.scrollw in
881 min a b
882 and vh = l.pagevh in
884 let rec rowloop row y0 dispy h =
885 if h = 0
886 then ()
887 else (
888 let dh = conf.tileh - y0 in
889 let dh = min h dh in
890 let rec colloop col x0 dispx w =
891 if w = 0
892 then ()
893 else (
894 let dw = conf.tilew - x0 in
895 let dw = min w dw in
897 f col row dispx dispy x0 y0 dw dh;
898 colloop (col+1) 0 (dispx+dw) (w-dw)
901 colloop col tilex l.pagedispx vw;
902 rowloop (row+1) 0 (dispy+dh) (h-dh)
905 if vw > 0 && vh > 0
906 then rowloop row tiley l.pagedispy vh;
909 let gettileopaque l col row =
910 let key =
911 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
913 try Some (Hashtbl.find state.tilemap key)
914 with Not_found -> None
917 let puttileopaque l col row gen colorspace angle opaque size elapsed =
918 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
919 Hashtbl.add state.tilemap key (opaque, size, elapsed)
922 let drawtiles l color =
923 GlDraw.color color;
924 let f col row x y tilex tiley w h =
925 match gettileopaque l col row with
926 | Some (opaque, _, t) ->
927 let params = x, y, w, h, tilex, tiley in
928 if conf.invert
929 then (
930 Gl.enable `blend;
931 GlFunc.blend_func `zero `one_minus_src_color;
933 drawtile params opaque;
934 if conf.invert
935 then Gl.disable `blend;
936 if conf.debug
937 then (
938 let s = Printf.sprintf
939 "%d[%d,%d] %f sec"
940 l.pageno col row t
942 let w = measurestr fstate.fontsize s in
943 GlMisc.push_attrib [`current];
944 GlDraw.color (0.0, 0.0, 0.0);
945 GlDraw.rect
946 (float (x-2), float (y-2))
947 (float (x+2) +. w, float (y + fstate.fontsize + 2));
948 GlDraw.color (1.0, 1.0, 1.0);
949 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
950 GlMisc.pop_attrib ();
953 | _ ->
954 let w =
955 let lw = conf.winw - state.scrollw - x in
956 min lw w
957 and h =
958 let lh = conf.winh - y in
959 min lh h
961 Gl.enable `texture_2d;
962 begin match state.texid with
963 | Some id ->
964 GlTex.bind_texture `texture_2d id;
965 let x0 = float x
966 and y0 = float y
967 and x1 = float (x+w)
968 and y1 = float (y+h) in
970 let tw = float w /. 64.0
971 and th = float h /. 64.0 in
972 let tx0 = float tilex /. 64.0
973 and ty0 = float tiley /. 64.0 in
974 let tx1 = tx0 +. tw
975 and ty1 = ty0 +. th in
976 GlDraw.begins `quads;
977 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
978 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
979 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
980 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
981 GlDraw.ends ();
983 Gl.disable `texture_2d;
984 | None ->
985 GlDraw.color (1.0, 1.0, 1.0);
986 GlDraw.rect
987 (float x, float y)
988 (float (x+w), float (y+h));
989 end;
990 if w > 128 && h > fstate.fontsize + 10
991 then (
992 GlDraw.color (0.0, 0.0, 0.0);
993 let c, r =
994 if conf.verbose
995 then (col*conf.tilew, row*conf.tileh)
996 else col, row
998 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1000 GlDraw.color color;
1002 itertiles l f
1005 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1007 let tilevisible1 l x y =
1008 let ax0 = l.pagex
1009 and ax1 = l.pagex + l.pagevw
1010 and ay0 = l.pagey
1011 and ay1 = l.pagey + l.pagevh in
1013 let bx0 = x
1014 and by0 = y in
1015 let bx1 = min (bx0 + conf.tilew) l.pagew
1016 and by1 = min (by0 + conf.tileh) l.pageh in
1018 let rx0 = max ax0 bx0
1019 and ry0 = max ay0 by0
1020 and rx1 = min ax1 bx1
1021 and ry1 = min ay1 by1 in
1023 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1024 nonemptyintersection
1027 let tilevisible layout n x y =
1028 let rec findpageinlayout = function
1029 | l :: _ when l.pageno = n -> tilevisible1 l x y
1030 | _ :: rest -> findpageinlayout rest
1031 | [] -> false
1033 findpageinlayout layout
1036 let tileready l x y =
1037 tilevisible1 l x y &&
1038 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1041 let tilepage n p layout =
1042 let rec loop = function
1043 | l :: rest ->
1044 if l.pageno = n
1045 then
1046 let f col row _ _ _ _ _ _ =
1047 if state.currently = Idle
1048 then
1049 match gettileopaque l col row with
1050 | Some _ -> ()
1051 | None ->
1052 let x = col*conf.tilew
1053 and y = row*conf.tileh in
1054 let w =
1055 let w = l.pagew - x in
1056 min w conf.tilew
1058 let h =
1059 let h = l.pageh - y in
1060 min h conf.tileh
1062 wcmd "tile"
1063 [`s p
1064 ;`i x
1065 ;`i y
1066 ;`i w
1067 ;`i h
1069 state.currently <-
1070 Tiling (
1071 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1072 conf.tilew, conf.tileh
1075 itertiles l f;
1076 else
1077 loop rest
1079 | [] -> ()
1081 if state.invalidated = 0 then loop layout;
1084 let preloadlayout visiblepages =
1085 let presentation = conf.presentation in
1086 let interpagespace = conf.interpagespace in
1087 let maxy = state.maxy in
1088 conf.presentation <- false;
1089 conf.interpagespace <- 0;
1090 state.maxy <- calcheight ();
1091 let y =
1092 match visiblepages with
1093 | [] -> 0
1094 | l :: _ -> getpagey l.pageno + l.pagey
1096 let y = if y < conf.winh then 0 else y - conf.winh in
1097 let h = state.y - y + conf.winh*3 in
1098 let pages = layout y h in
1099 conf.presentation <- presentation;
1100 conf.interpagespace <- interpagespace;
1101 state.maxy <- maxy;
1102 pages;
1105 let load pages =
1106 let rec loop pages =
1107 if state.currently != Idle
1108 then ()
1109 else
1110 match pages with
1111 | l :: rest ->
1112 begin match getopaque l.pageno with
1113 | None ->
1114 wcmd "page" [`i l.pageno; `i l.pagedimno];
1115 state.currently <- Loading (l, state.gen);
1116 | Some opaque ->
1117 tilepage l.pageno opaque pages;
1118 loop rest
1119 end;
1120 | _ -> ()
1122 if state.invalidated = 0 then loop pages
1125 let preload pages =
1126 load pages;
1127 if conf.preload && state.currently = Idle
1128 then load (preloadlayout pages);
1131 let layoutready layout =
1132 let rec fold all ls =
1133 all && match ls with
1134 | l :: rest ->
1135 let seen = ref false in
1136 let allvisible = ref true in
1137 let foo col row _ _ _ _ _ _ =
1138 seen := true;
1139 allvisible := !allvisible &&
1140 begin match gettileopaque l col row with
1141 | Some _ -> true
1142 | None -> false
1145 itertiles l foo;
1146 fold (!seen && !allvisible) rest
1147 | [] -> true
1149 let alltilesvisible = fold true layout in
1150 alltilesvisible;
1153 let gotoy y =
1154 let y = bound y 0 state.maxy in
1155 let y, layout, proceed =
1156 match conf.maxwait with
1157 | Some time ->
1158 begin match state.throttle with
1159 | None ->
1160 let layout = layout y conf.winh in
1161 let ready = layoutready layout in
1162 if not ready
1163 then (
1164 load layout;
1165 state.throttle <- Some (layout, y, now ());
1167 else G.postRedisplay "gotoy showall (None)";
1168 y, layout, ready
1169 | Some (_, _, started) ->
1170 let dt = now () -. started in
1171 if dt > time
1172 then (
1173 state.throttle <- None;
1174 let layout = layout y conf.winh in
1175 load layout;
1176 G.postRedisplay "maxwait";
1177 y, layout, true
1179 else -1, [], false
1182 | None ->
1183 let layout = layout y conf.winh in
1184 if true || layoutready layout
1185 then G.postRedisplay "gotoy ready";
1186 y, layout, true
1188 if proceed
1189 then (
1190 state.y <- y;
1191 state.layout <- layout;
1192 begin match state.mode with
1193 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1194 if not (pagevisible layout pageno)
1195 then (
1196 match state.layout with
1197 | [] -> ()
1198 | l :: _ ->
1199 state.mode <- Birdseye (
1200 conf, leftx, l.pageno, hooverpageno, anchor
1203 | _ -> ()
1204 end;
1205 preload layout;
1209 let conttiling pageno opaque =
1210 tilepage pageno opaque
1211 (if conf.preload then preloadlayout state.layout else state.layout)
1214 let gotoy_and_clear_text y =
1215 gotoy y;
1216 if not conf.verbose then state.text <- "";
1219 let getanchor () =
1220 match state.layout with
1221 | [] -> emptyanchor
1222 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1225 let getanchory (n, top) =
1226 let y, h = getpageyh n in
1227 y + (truncate (top *. float h));
1230 let gotoanchor anchor =
1231 gotoy (getanchory anchor);
1234 let addnav () =
1235 cbput state.hists.nav (getanchor ());
1238 let getnav dir =
1239 let anchor = cbgetc state.hists.nav dir in
1240 getanchory anchor;
1243 let gotopage n top =
1244 let y, h = getpageyh n in
1245 gotoy_and_clear_text (y + (truncate (top *. float h)));
1248 let gotopage1 n top =
1249 let y = getpagey n in
1250 gotoy_and_clear_text (y + top);
1253 let invalidate () =
1254 state.layout <- [];
1255 state.pdims <- [];
1256 state.rects <- [];
1257 state.rects1 <- [];
1258 state.invalidated <- state.invalidated + 1;
1261 let writeopen path password =
1262 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1265 let opendoc path password =
1266 invalidate ();
1267 state.path <- path;
1268 state.password <- password;
1269 state.gen <- state.gen + 1;
1270 state.docinfo <- [];
1272 setaalevel conf.aalevel;
1273 writeopen path password;
1274 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1275 wcmd "geometry" [`i state.w; `i conf.winh];
1278 let scalecolor c =
1279 let c = c *. conf.colorscale in
1280 (c, c, c);
1283 let scalecolor2 (r, g, b) =
1284 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1287 let represent () =
1288 state.maxy <- calcheight ();
1289 state.hscrollh <-
1290 if state.w <= conf.winw - state.scrollw
1291 then 0
1292 else state.scrollw
1294 match state.mode with
1295 | Birdseye (_, _, pageno, _, _) ->
1296 let y, h = getpageyh pageno in
1297 let top = (conf.winh - h) / 2 in
1298 gotoy (max 0 (y - top))
1299 | _ -> gotoanchor state.anchor
1302 let reshape =
1303 let firsttime = ref true in
1304 fun ~w ~h ->
1305 GlDraw.viewport 0 0 w h;
1306 if state.invalidated = 0 && not !firsttime
1307 then state.anchor <- getanchor ();
1309 firsttime := false;
1310 conf.winw <- w;
1311 let w = truncate (float w *. conf.zoom) - state.scrollw in
1312 let w = max w 2 in
1313 state.w <- w;
1314 conf.winh <- h;
1315 setfontsize fstate.fontsize;
1316 GlMat.mode `modelview;
1317 GlMat.load_identity ();
1319 GlMat.mode `projection;
1320 GlMat.load_identity ();
1321 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1322 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1323 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1325 invalidate ();
1326 wcmd "geometry" [`i w; `i h];
1329 let enttext () =
1330 let len = String.length state.text in
1331 let drawstring s =
1332 let hscrollh =
1333 match state.mode with
1334 | View -> state.hscrollh
1335 | _ -> 0
1337 let rect x w =
1338 GlDraw.rect
1339 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1340 (x+.w, float (conf.winh - hscrollh))
1343 let w = float (conf.winw - state.scrollw - 1) in
1344 if state.progress >= 0.0 && state.progress < 1.0
1345 then (
1346 GlDraw.color (0.3, 0.3, 0.3);
1347 let w1 = w *. state.progress in
1348 rect 0.0 w1;
1349 GlDraw.color (0.0, 0.0, 0.0);
1350 rect w1 (w-.w1)
1352 else (
1353 GlDraw.color (0.0, 0.0, 0.0);
1354 rect 0.0 w;
1357 GlDraw.color (1.0, 1.0, 1.0);
1358 drawstring fstate.fontsize
1359 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1361 let s =
1362 match state.mode with
1363 | Textentry ((prefix, text, _, _, _), _) ->
1364 let s =
1365 if len > 0
1366 then
1367 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1368 else
1369 Printf.sprintf "%s%s_" prefix text
1373 | _ -> state.text
1375 let s =
1376 if state.newerrmsgs
1377 then (
1378 if not (istextentry state.mode)
1379 then
1380 let s1 = "(press 'e' to review error messasges)" in
1381 if String.length s > 0 then s ^ " " ^ s1 else s1
1382 else s
1384 else s
1386 if String.length s > 0
1387 then drawstring s
1390 let showtext c s =
1391 state.text <- Printf.sprintf "%c%s" c s;
1392 G.postRedisplay "showtext";
1395 let gctiles () =
1396 let len = Queue.length state.tilelru in
1397 let rec loop qpos =
1398 if state.memused <= conf.memlimit
1399 then ()
1400 else (
1401 if qpos < len
1402 then
1403 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1404 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1405 let (_, pw, ph, _) = getpagedim n in
1407 gen = state.gen
1408 && colorspace = conf.colorspace
1409 && angle = conf.angle
1410 && pagew = pw
1411 && pageh = ph
1412 && (
1413 let layout =
1414 match state.throttle with
1415 | None ->
1416 if conf.preload
1417 then preloadlayout state.layout
1418 else state.layout
1419 | Some (layout, _, _) ->
1420 layout
1422 let x = col*conf.tilew
1423 and y = row*conf.tileh in
1424 tilevisible layout n x y
1426 then Queue.push lruitem state.tilelru
1427 else (
1428 wcmd "freetile" [`s p];
1429 state.memused <- state.memused - s;
1430 state.uioh#infochanged Memused;
1431 Hashtbl.remove state.tilemap k;
1433 loop (qpos+1)
1436 loop 0
1439 let flushtiles () =
1440 Queue.iter (fun (k, p, s) ->
1441 wcmd "freetile" [`s p];
1442 state.memused <- state.memused - s;
1443 state.uioh#infochanged Memused;
1444 Hashtbl.remove state.tilemap k;
1445 ) state.tilelru;
1446 Queue.clear state.tilelru;
1447 load state.layout;
1450 let logcurrently = function
1451 | Idle -> dolog "Idle"
1452 | Loading (l, gen) ->
1453 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1454 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1455 dolog
1456 "Tiling %d[%d,%d] page=%s cs=%s angle"
1457 l.pageno col row pageopaque
1458 (colorspace_to_string colorspace)
1460 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1461 angle gen conf.angle state.gen
1462 tilew tileh
1463 conf.tilew conf.tileh
1465 | Outlining _ ->
1466 dolog "outlining"
1469 let act cmds =
1470 (* dolog "%S" cmds; *)
1471 let op, args =
1472 let spacepos =
1473 try String.index cmds ' '
1474 with Not_found -> -1
1476 if spacepos = -1
1477 then cmds, ""
1478 else
1479 let l = String.length cmds in
1480 let op = String.sub cmds 0 spacepos in
1481 op, begin
1482 if l - spacepos < 2 then ""
1483 else String.sub cmds (spacepos+1) (l-spacepos-1)
1486 match op with
1487 | "clear" ->
1488 state.uioh#infochanged Pdim;
1489 state.pdims <- [];
1491 | "clearrects" ->
1492 state.rects <- state.rects1;
1493 G.postRedisplay "clearrects";
1495 | "continue" ->
1496 let n =
1497 try Scanf.sscanf args "%u" (fun n -> n)
1498 with exn ->
1499 dolog "error processing 'continue' %S: %s"
1500 cmds (Printexc.to_string exn);
1501 exit 1;
1503 state.pagecount <- n;
1504 state.invalidated <- state.invalidated - 1;
1505 begin match state.currently with
1506 | Outlining l ->
1507 state.currently <- Idle;
1508 state.outlines <- Array.of_list (List.rev l)
1509 | _ -> ()
1510 end;
1511 if state.invalidated = 0
1512 then represent ();
1513 if conf.maxwait = None
1514 then G.postRedisplay "continue";
1516 | "title" ->
1517 Glut.setWindowTitle args
1519 | "msg" ->
1520 showtext ' ' args
1522 | "vmsg" ->
1523 if conf.verbose
1524 then showtext ' ' args
1526 | "progress" ->
1527 let progress, text =
1529 Scanf.sscanf args "%f %n"
1530 (fun f pos ->
1531 f, String.sub args pos (String.length args - pos))
1532 with exn ->
1533 dolog "error processing 'progress' %S: %s"
1534 cmds (Printexc.to_string exn);
1535 exit 1;
1537 state.text <- text;
1538 state.progress <- progress;
1539 G.postRedisplay "progress"
1541 | "firstmatch" ->
1542 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1544 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1545 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1546 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1547 with exn ->
1548 dolog "error processing 'firstmatch' %S: %s"
1549 cmds (Printexc.to_string exn);
1550 exit 1;
1552 let y = (getpagey pageno) + truncate y0 in
1553 addnav ();
1554 gotoy y;
1555 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1557 | "match" ->
1558 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1560 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1561 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1562 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1563 with exn ->
1564 dolog "error processing 'match' %S: %s"
1565 cmds (Printexc.to_string exn);
1566 exit 1;
1568 state.rects1 <-
1569 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1571 | "page" ->
1572 let pageopaque, t =
1574 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1575 with exn ->
1576 dolog "error processing 'page' %S: %s"
1577 cmds (Printexc.to_string exn);
1578 exit 1;
1580 begin match state.currently with
1581 | Loading (l, gen) ->
1582 vlog "page %d took %f sec" l.pageno t;
1583 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1584 begin match state.throttle with
1585 | None ->
1586 let preloadedpages =
1587 if conf.preload
1588 then preloadlayout state.layout
1589 else state.layout
1591 let evict () =
1592 let module IntSet =
1593 Set.Make (struct type t = int let compare = (-) end) in
1594 let set =
1595 List.fold_left (fun s l -> IntSet.add l.pageno s)
1596 IntSet.empty preloadedpages
1598 let evictedpages =
1599 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1600 if not (IntSet.mem pageno set)
1601 then (
1602 wcmd "freepage" [`s opaque];
1603 key :: accu
1605 else accu
1606 ) state.pagemap []
1608 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1610 evict ();
1611 state.currently <- Idle;
1612 if gen = state.gen
1613 then (
1614 tilepage l.pageno pageopaque state.layout;
1615 load state.layout;
1616 load preloadedpages;
1617 if pagevisible state.layout l.pageno
1618 && layoutready state.layout
1619 then G.postRedisplay "page";
1622 | Some (layout, _, _) ->
1623 state.currently <- Idle;
1624 tilepage l.pageno pageopaque layout;
1625 load state.layout
1626 end;
1628 | _ ->
1629 dolog "Inconsistent loading state";
1630 logcurrently state.currently;
1631 raise Quit;
1634 | "tile" ->
1635 let (x, y, opaque, size, t) =
1637 Scanf.sscanf args "%u %u %s %u %f"
1638 (fun x y p size t -> (x, y, p, size, t))
1639 with exn ->
1640 dolog "error processing 'tile' %S: %s"
1641 cmds (Printexc.to_string exn);
1642 exit 1;
1644 begin match state.currently with
1645 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1646 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1648 if tilew != conf.tilew || tileh != conf.tileh
1649 then (
1650 wcmd "freetile" [`s opaque];
1651 state.currently <- Idle;
1652 load state.layout;
1654 else (
1655 puttileopaque l col row gen cs angle opaque size t;
1656 state.memused <- state.memused + size;
1657 state.uioh#infochanged Memused;
1658 gctiles ();
1659 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1660 opaque, size) state.tilelru;
1662 let layout =
1663 match state.throttle with
1664 | None -> state.layout
1665 | Some (layout, _, _) -> layout
1668 state.currently <- Idle;
1669 if gen = state.gen
1670 && conf.colorspace = cs
1671 && conf.angle = angle
1672 && tilevisible layout l.pageno x y
1673 then conttiling l.pageno pageopaque;
1675 begin match state.throttle with
1676 | None ->
1677 preload state.layout;
1678 if gen = state.gen
1679 && conf.colorspace = cs
1680 && conf.angle = angle
1681 && tilevisible state.layout l.pageno x y
1682 then G.postRedisplay "tile nothrottle";
1684 | Some (layout, y, _) ->
1685 let ready = layoutready layout in
1686 if ready
1687 then (
1688 state.y <- y;
1689 state.layout <- layout;
1690 state.throttle <- None;
1691 G.postRedisplay "throttle";
1693 else load layout;
1694 end;
1697 | _ ->
1698 dolog "Inconsistent tiling state";
1699 logcurrently state.currently;
1700 raise Quit;
1703 | "pdim" ->
1704 let pdim =
1706 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1707 with exn ->
1708 dolog "error processing 'pdim' %S: %s"
1709 cmds (Printexc.to_string exn);
1710 exit 1;
1712 state.uioh#infochanged Pdim;
1713 state.pdims <- pdim :: state.pdims
1715 | "o" ->
1716 let (l, n, t, h, pos) =
1718 Scanf.sscanf args "%u %u %d %u %n"
1719 (fun l n t h pos -> l, n, t, h, pos)
1720 with exn ->
1721 dolog "error processing 'o' %S: %s"
1722 cmds (Printexc.to_string exn);
1723 exit 1;
1725 let s = String.sub args pos (String.length args - pos) in
1726 let outline = (s, l, (n, float t /. float h)) in
1727 begin match state.currently with
1728 | Outlining outlines ->
1729 state.currently <- Outlining (outline :: outlines)
1730 | Idle ->
1731 state.currently <- Outlining [outline]
1732 | currently ->
1733 dolog "invalid outlining state";
1734 logcurrently currently
1737 | "info" ->
1738 state.docinfo <- (1, args) :: state.docinfo
1740 | "infoend" ->
1741 state.uioh#infochanged Docinfo;
1742 state.docinfo <- List.rev state.docinfo
1744 | _ ->
1745 dolog "unknown cmd `%S'" cmds
1748 let idle () =
1749 if state.deadline == nan then state.deadline <- now ();
1750 let rec loop delay =
1751 let timeout =
1752 if delay > 0.0
1753 then max 0.0 (state.deadline -. now ())
1754 else 0.0
1756 let r, _, _ = Unix.select [state.csock; state.errfd] [] [] timeout in
1757 begin match r with
1758 | [] ->
1759 begin match state.autoscroll with
1760 | Some step when step != 0 ->
1761 let y = state.y + step in
1762 let y =
1763 if y < 0
1764 then state.maxy
1765 else if y >= state.maxy then 0 else y
1767 gotoy y;
1768 if state.mode = View
1769 then state.text <- "";
1770 state.deadline <- state.deadline +. 0.005;
1772 | _ ->
1773 state.deadline <- state.deadline +. delay;
1774 end;
1776 | l ->
1777 let rec checkfds c = function
1778 | [] -> c
1779 | fd :: rest when fd = state.csock ->
1780 let cmd = readcmd state.csock in
1781 act cmd;
1782 checkfds true rest
1783 | fd :: rest when fd = state.errfd ->
1784 let s = String.create 80 in
1785 let n = Unix.read fd s 0 80 in
1786 if conf.redirectstderr
1787 then (
1788 Buffer.add_substring state.errmsgs s 0 n;
1789 state.newerrmsgs <- true;
1790 Glut.postRedisplay ();
1792 else (
1793 prerr_string (String.sub s 0 n);
1794 flush stderr;
1796 checkfds c rest
1798 | _ ->
1799 failwith "me? fail english? that's unpossible!"
1801 if checkfds false l
1802 then loop 0.0
1803 end;
1804 in loop 0.007
1807 let onhist cb =
1808 let rc = cb.rc in
1809 let action = function
1810 | HCprev -> cbget cb ~-1
1811 | HCnext -> cbget cb 1
1812 | HCfirst -> cbget cb ~-(cb.rc)
1813 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1814 and cancel () = cb.rc <- rc
1815 in (action, cancel)
1818 let search pattern forward =
1819 if String.length pattern > 0
1820 then
1821 let pn, py =
1822 match state.layout with
1823 | [] -> 0, 0
1824 | l :: _ ->
1825 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1827 let cmd =
1828 let b = makecmd "search"
1829 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1831 Buffer.add_char b ',';
1832 Buffer.add_string b pattern;
1833 Buffer.add_char b '\000';
1834 Buffer.contents b;
1836 writecmd state.csock cmd;
1839 let intentry text key =
1840 let c = Char.unsafe_chr key in
1841 match c with
1842 | '0' .. '9' ->
1843 let text = addchar text c in
1844 TEcont text
1846 | _ ->
1847 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1848 TEcont text
1851 let textentry text key =
1852 let c = Char.unsafe_chr key in
1853 match c with
1854 | _ when key >= 32 && key < 127 ->
1855 let text = addchar text c in
1856 TEcont text
1858 | _ ->
1859 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1860 TEcont text
1863 let reqlayout angle proportional =
1864 match state.throttle with
1865 | None ->
1866 if state.invalidated = 0 then state.anchor <- getanchor ();
1867 conf.angle <- angle mod 360;
1868 conf.proportional <- proportional;
1869 invalidate ();
1870 wcmd "reqlayout" [`i conf.angle; `b proportional];
1871 | _ -> ()
1874 let settrim trimmargins trimfuzz =
1875 if state.invalidated = 0 then state.anchor <- getanchor ();
1876 conf.trimmargins <- trimmargins;
1877 conf.trimfuzz <- trimfuzz;
1878 let x0, y0, x1, y1 = trimfuzz in
1879 invalidate ();
1880 wcmd "settrim" [
1881 `b conf.trimmargins;
1882 `i x0;
1883 `i y0;
1884 `i x1;
1885 `i y1;
1887 Hashtbl.iter (fun _ opaque ->
1888 wcmd "freepage" [`s opaque];
1889 ) state.pagemap;
1890 Hashtbl.clear state.pagemap;
1893 let setzoom zoom =
1894 match state.throttle with
1895 | None ->
1896 let zoom = max 0.01 zoom in
1897 if zoom <> conf.zoom
1898 then (
1899 state.prevzoom <- conf.zoom;
1900 let relx =
1901 if zoom <= 1.0
1902 then (state.x <- 0; 0.0)
1903 else float state.x /. float state.w
1905 conf.zoom <- zoom;
1906 reshape conf.winw conf.winh;
1907 if zoom > 1.0
1908 then (
1909 let x = relx *. float state.w in
1910 state.x <- truncate x;
1912 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1915 | Some (layout, y, started) ->
1916 let time =
1917 match conf.maxwait with
1918 | None -> 0.0
1919 | Some t -> t
1921 let dt = now () -. started in
1922 if dt > time
1923 then (
1924 state.y <- y;
1925 load layout;
1929 let enterbirdseye () =
1930 let zoom = float conf.thumbw /. float conf.winw in
1931 let birdseyepageno =
1932 let cy = conf.winh / 2 in
1933 let fold = function
1934 | [] -> 0
1935 | l :: rest ->
1936 let rec fold best = function
1937 | [] -> best.pageno
1938 | l :: rest ->
1939 let d = cy - (l.pagedispy + l.pagevh/2)
1940 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1941 if abs d < abs dbest
1942 then fold l rest
1943 else best.pageno
1944 in fold l rest
1946 fold state.layout
1948 state.mode <- Birdseye (
1949 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1951 conf.zoom <- zoom;
1952 conf.presentation <- false;
1953 conf.interpagespace <- 10;
1954 conf.hlinks <- false;
1955 state.x <- 0;
1956 state.mstate <- Mnone;
1957 conf.maxwait <- None;
1958 Glut.setCursor Glut.CURSOR_INHERIT;
1959 if conf.verbose
1960 then
1961 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1962 (100.0*.zoom)
1963 else
1964 state.text <- ""
1966 reshape conf.winw conf.winh;
1969 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1970 state.mode <- View;
1971 conf.zoom <- c.zoom;
1972 conf.presentation <- c.presentation;
1973 conf.interpagespace <- c.interpagespace;
1974 conf.maxwait <- c.maxwait;
1975 conf.hlinks <- c.hlinks;
1976 state.x <- leftx;
1977 if conf.verbose
1978 then
1979 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1980 (100.0*.conf.zoom)
1982 reshape conf.winw conf.winh;
1983 state.anchor <- if goback then anchor else (pageno, 0.0);
1986 let togglebirdseye () =
1987 match state.mode with
1988 | Birdseye vals -> leavebirdseye vals true
1989 | View -> enterbirdseye ()
1990 | _ -> ()
1993 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1994 let pageno = max 0 (pageno - 1) in
1995 let rec loop = function
1996 | [] -> gotopage1 pageno 0
1997 | l :: _ when l.pageno = pageno ->
1998 if l.pagedispy >= 0 && l.pagey = 0
1999 then G.postRedisplay "upbirdseye"
2000 else gotopage1 pageno 0
2001 | _ :: rest -> loop rest
2003 loop state.layout;
2004 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2007 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
2008 let pageno = min (state.pagecount - 1) (pageno + 1) in
2009 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2010 let rec loop = function
2011 | [] ->
2012 let y, h = getpageyh pageno in
2013 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2014 gotoy (clamp dy)
2015 | l :: _ when l.pageno = pageno ->
2016 if l.pagevh != l.pageh
2017 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2018 else G.postRedisplay "downbirdseye"
2019 | _ :: rest -> loop rest
2021 loop state.layout
2024 let optentry mode _ key =
2025 let btos b = if b then "on" else "off" in
2026 let c = Char.unsafe_chr key in
2027 match c with
2028 | 's' ->
2029 let ondone s =
2030 try conf.scrollstep <- int_of_string s with exc ->
2031 state.text <- Printf.sprintf "bad integer `%s': %s"
2032 s (Printexc.to_string exc)
2034 TEswitch ("scroll step: ", "", None, intentry, ondone)
2036 | 'A' ->
2037 let ondone s =
2039 conf.autoscrollstep <- int_of_string s;
2040 if state.autoscroll <> None
2041 then state.autoscroll <- Some conf.autoscrollstep
2042 with exc ->
2043 state.text <- Printf.sprintf "bad integer `%s': %s"
2044 s (Printexc.to_string exc)
2046 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2048 | 'Z' ->
2049 let ondone s =
2051 let zoom = float (int_of_string s) /. 100.0 in
2052 setzoom zoom
2053 with exc ->
2054 state.text <- Printf.sprintf "bad integer `%s': %s"
2055 s (Printexc.to_string exc)
2057 TEswitch ("zoom: ", "", None, intentry, ondone)
2059 | 't' ->
2060 let ondone s =
2062 conf.thumbw <- bound (int_of_string s) 2 4096;
2063 state.text <-
2064 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2065 begin match mode with
2066 | Birdseye beye ->
2067 leavebirdseye beye false;
2068 enterbirdseye ();
2069 | _ -> ();
2071 with exc ->
2072 state.text <- Printf.sprintf "bad integer `%s': %s"
2073 s (Printexc.to_string exc)
2075 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2077 | 'R' ->
2078 let ondone s =
2079 match try
2080 Some (int_of_string s)
2081 with exc ->
2082 state.text <- Printf.sprintf "bad integer `%s': %s"
2083 s (Printexc.to_string exc);
2084 None
2085 with
2086 | Some angle -> reqlayout angle conf.proportional
2087 | None -> ()
2089 TEswitch ("rotation: ", "", None, intentry, ondone)
2091 | 'i' ->
2092 conf.icase <- not conf.icase;
2093 TEdone ("case insensitive search " ^ (btos conf.icase))
2095 | 'p' ->
2096 conf.preload <- not conf.preload;
2097 gotoy state.y;
2098 TEdone ("preload " ^ (btos conf.preload))
2100 | 'v' ->
2101 conf.verbose <- not conf.verbose;
2102 TEdone ("verbose " ^ (btos conf.verbose))
2104 | 'd' ->
2105 conf.debug <- not conf.debug;
2106 TEdone ("debug " ^ (btos conf.debug))
2108 | 'h' ->
2109 conf.maxhfit <- not conf.maxhfit;
2110 state.maxy <-
2111 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2112 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2114 | 'c' ->
2115 conf.crophack <- not conf.crophack;
2116 TEdone ("crophack " ^ btos conf.crophack)
2118 | 'a' ->
2119 let s =
2120 match conf.maxwait with
2121 | None ->
2122 conf.maxwait <- Some infinity;
2123 "always wait for page to complete"
2124 | Some _ ->
2125 conf.maxwait <- None;
2126 "show placeholder if page is not ready"
2128 TEdone s
2130 | 'f' ->
2131 conf.underinfo <- not conf.underinfo;
2132 TEdone ("underinfo " ^ btos conf.underinfo)
2134 | 'P' ->
2135 conf.savebmarks <- not conf.savebmarks;
2136 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2138 | 'S' ->
2139 let ondone s =
2141 let pageno, py =
2142 match state.layout with
2143 | [] -> 0, 0
2144 | l :: _ ->
2145 l.pageno, l.pagey
2147 conf.interpagespace <- int_of_string s;
2148 state.maxy <- calcheight ();
2149 let y = getpagey pageno in
2150 gotoy (y + py)
2151 with exc ->
2152 state.text <- Printf.sprintf "bad integer `%s': %s"
2153 s (Printexc.to_string exc)
2155 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2157 | 'l' ->
2158 reqlayout conf.angle (not conf.proportional);
2159 TEdone ("proportional display " ^ btos conf.proportional)
2161 | 'T' ->
2162 settrim (not conf.trimmargins) conf.trimfuzz;
2163 TEdone ("trim margins " ^ btos conf.trimmargins)
2165 | 'I' ->
2166 conf.invert <- not conf.invert;
2167 TEdone ("invert colors " ^ btos conf.invert)
2169 | _ ->
2170 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2171 TEstop
2174 class type lvsource = object
2175 method getitemcount : int
2176 method getitem : int -> (string * int)
2177 method hasaction : int -> bool
2178 method exit :
2179 uioh:uioh ->
2180 cancel:bool ->
2181 active:int ->
2182 first:int ->
2183 pan:int ->
2184 qsearch:string ->
2185 uioh option
2186 method getactive : int
2187 method getfirst : int
2188 method getqsearch : string
2189 method setqsearch : string -> unit
2190 method getpan : int
2191 end;;
2193 class virtual lvsourcebase = object
2194 val mutable m_active = 0
2195 val mutable m_first = 0
2196 val mutable m_qsearch = ""
2197 val mutable m_pan = 0
2198 method getactive = m_active
2199 method getfirst = m_first
2200 method getqsearch = m_qsearch
2201 method getpan = m_pan
2202 method setqsearch s = m_qsearch <- s
2203 end;;
2205 let textentryspecial key = function
2206 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2207 let s =
2208 match key with
2209 | Glut.KEY_UP -> action HCprev
2210 | Glut.KEY_DOWN -> action HCnext
2211 | Glut.KEY_HOME -> action HCfirst
2212 | Glut.KEY_END -> action HClast
2213 | _ -> state.text
2215 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2216 G.postRedisplay "special textentry";
2217 | _ -> ()
2220 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2221 let enttext te =
2222 state.mode <- Textentry (te, onleave);
2223 state.text <- "";
2224 enttext ();
2225 G.postRedisplay "textentrykeyboard enttext";
2227 match Char.unsafe_chr key with
2228 | '\008' -> (* backspace *)
2229 let len = String.length text in
2230 if len = 0
2231 then (
2232 onleave Cancel;
2233 G.postRedisplay "textentrykeyboard after cancel";
2235 else (
2236 let s = String.sub text 0 (len - 1) in
2237 enttext (c, s, opthist, onkey, ondone)
2240 | '\r' | '\n' ->
2241 ondone text;
2242 onleave Confirm;
2243 G.postRedisplay "textentrykeyboard after confirm"
2245 | '\007' (* ctrl-g *)
2246 | '\027' -> (* escape *)
2247 if String.length text = 0
2248 then (
2249 begin match opthist with
2250 | None -> ()
2251 | Some (_, onhistcancel) -> onhistcancel ()
2252 end;
2253 onleave Cancel;
2254 state.text <- "";
2255 G.postRedisplay "textentrykeyboard after cancel2"
2257 else (
2258 enttext (c, "", opthist, onkey, ondone)
2261 | '\127' -> () (* delete *)
2263 | _ ->
2264 begin match onkey text key with
2265 | TEdone text ->
2266 ondone text;
2267 onleave Confirm;
2268 G.postRedisplay "textentrykeyboard after confirm2";
2270 | TEcont text ->
2271 enttext (c, text, opthist, onkey, ondone);
2273 | TEstop ->
2274 onleave Cancel;
2275 state.text <- "";
2276 G.postRedisplay "textentrykeyboard after cancel3"
2278 | TEswitch te ->
2279 state.mode <- Textentry (te, onleave);
2280 G.postRedisplay "textentrykeyboard switch";
2281 end;
2284 let firstof first active =
2285 if first > active || abs (first - active) > fstate.maxrows - 1
2286 then max 0 (active - (fstate.maxrows/2))
2287 else first
2290 let calcfirst first active =
2291 if active > first
2292 then
2293 let rows = active - first in
2294 if rows > fstate.maxrows then active - fstate.maxrows else first
2295 else active
2298 let scrollph y maxy =
2299 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2300 let sh = float conf.winh /. sh in
2301 let sh = max sh (float conf.scrollh) in
2303 let percent =
2304 if y = state.maxy
2305 then 1.0
2306 else float y /. float maxy
2308 let position = (float conf.winh -. sh) *. percent in
2310 let position =
2311 if position +. sh > float conf.winh
2312 then float conf.winh -. sh
2313 else position
2315 position, sh;
2318 let coe s = (s :> uioh);;
2320 class listview ~(source:lvsource) ~trusted =
2321 object (self)
2322 val m_pan = source#getpan
2323 val m_first = source#getfirst
2324 val m_active = source#getactive
2325 val m_qsearch = source#getqsearch
2326 val m_prev_uioh = state.uioh
2328 method private elemunder y =
2329 let n = y / (fstate.fontsize+1) in
2330 if m_first + n < source#getitemcount
2331 then (
2332 if source#hasaction (m_first + n)
2333 then Some (m_first + n)
2334 else None
2336 else None
2338 method display =
2339 Gl.enable `blend;
2340 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2341 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2342 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2343 GlDraw.color (1., 1., 1.);
2344 Gl.enable `texture_2d;
2345 let fs = fstate.fontsize in
2346 let nfs = fs + 1 in
2347 let ww = fstate.wwidth in
2348 let tabw = 30.0*.ww in
2349 let rec loop row =
2350 if (row - m_first) * nfs > conf.winh
2351 then ()
2352 else (
2353 if row >= 0 && row < source#getitemcount
2354 then (
2355 let (s, level) = source#getitem row in
2356 let y = (row - m_first) * nfs in
2357 let x = 5.0 +. float (level + m_pan) *. ww in
2358 if row = m_active
2359 then (
2360 Gl.disable `texture_2d;
2361 GlDraw.polygon_mode `both `line;
2362 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2363 GlDraw.rect (1., float (y + 1))
2364 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2365 GlDraw.polygon_mode `both `fill;
2366 GlDraw.color (1., 1., 1.);
2367 Gl.enable `texture_2d;
2370 let drawtabularstring s =
2371 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2372 if trusted
2373 then
2374 let tabpos = try String.index s '\t' with Not_found -> -1 in
2375 if tabpos > 0
2376 then
2377 let len = String.length s - tabpos - 1 in
2378 let s1 = String.sub s 0 tabpos
2379 and s2 = String.sub s (tabpos + 1) len in
2380 let nx = drawstr x s1 in
2381 let sw = nx -. x in
2382 let x = x +. (max tabw sw) in
2383 drawstr x s2
2384 else
2385 drawstr x s
2386 else
2387 drawstr x s
2389 let _ = drawtabularstring s in
2390 loop (row+1)
2394 loop 0;
2395 Gl.disable `blend;
2396 Gl.disable `texture_2d;
2398 method updownlevel incr =
2399 let len = source#getitemcount in
2400 let _, curlevel = source#getitem m_active in
2401 let rec flow i =
2402 if i = len then i-1 else if i = -1 then 0 else
2403 let _, l = source#getitem i in
2404 if l != curlevel then i else flow (i+incr)
2406 let active = flow m_active in
2407 let first = calcfirst m_first active in
2408 G.postRedisplay "special outline updownlevel";
2409 {< m_active = active; m_first = first >}
2411 method private key1 key =
2412 let set active first qsearch =
2413 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2415 let search active pattern incr =
2416 let dosearch re =
2417 let rec loop n =
2418 if n >= 0 && n < source#getitemcount
2419 then (
2420 let s, _ = source#getitem n in
2422 (try ignore (Str.search_forward re s 0); true
2423 with Not_found -> false)
2424 then Some n
2425 else loop (n + incr)
2427 else None
2429 loop active
2432 let re = Str.regexp_case_fold pattern in
2433 dosearch re
2434 with Failure s ->
2435 state.text <- s;
2436 None
2438 match key with
2439 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2440 let incr = if key = 18 then -1 else 1 in
2441 let active, first =
2442 match search (m_active + incr) m_qsearch incr with
2443 | None ->
2444 state.text <- m_qsearch ^ " [not found]";
2445 m_active, m_first
2446 | Some active ->
2447 state.text <- m_qsearch;
2448 active, firstof m_first active
2450 G.postRedisplay "listview ctrl-r/s";
2451 set active first m_qsearch;
2453 | 8 -> (* backspace *)
2454 let len = String.length m_qsearch in
2455 if len = 0
2456 then coe self
2457 else (
2458 if len = 1
2459 then (
2460 state.text <- "";
2461 G.postRedisplay "listview empty qsearch";
2462 set m_active m_first "";
2464 else
2465 let qsearch = String.sub m_qsearch 0 (len - 1) in
2466 let active, first =
2467 match search m_active qsearch ~-1 with
2468 | None ->
2469 state.text <- qsearch ^ " [not found]";
2470 m_active, m_first
2471 | Some active ->
2472 state.text <- qsearch;
2473 active, firstof m_first active
2475 G.postRedisplay "listview backspace qsearch";
2476 set active first qsearch
2479 | _ when key >= 32 && key < 127 ->
2480 let pattern = addchar m_qsearch (Char.chr key) in
2481 let active, first =
2482 match search m_active pattern 1 with
2483 | None ->
2484 state.text <- pattern ^ " [not found]";
2485 m_active, m_first
2486 | Some active ->
2487 state.text <- pattern;
2488 active, firstof m_first active
2490 G.postRedisplay "listview qsearch add";
2491 set active first pattern;
2493 | 27 -> (* escape *)
2494 state.text <- "";
2495 if String.length m_qsearch = 0
2496 then (
2497 G.postRedisplay "list view escape";
2498 begin
2499 match
2500 source#exit (coe self) true m_active m_first m_pan m_qsearch
2501 with
2502 | None -> m_prev_uioh
2503 | Some uioh -> uioh
2506 else (
2507 G.postRedisplay "list view kill qsearch";
2508 source#setqsearch "";
2509 coe {< m_qsearch = "" >}
2512 | 13 -> (* enter *)
2513 state.text <- "";
2514 let self = {< m_qsearch = "" >} in
2515 source#setqsearch "";
2516 let opt =
2517 G.postRedisplay "listview enter";
2518 if m_active >= 0 && m_active < source#getitemcount
2519 then (
2520 source#exit (coe self) false m_active m_first m_pan "";
2522 else (
2523 source#exit (coe self) true m_active m_first m_pan "";
2526 begin match opt with
2527 | None -> m_prev_uioh
2528 | Some uioh -> uioh
2531 | 127 -> (* delete *)
2532 coe self
2534 | _ -> dolog "unknown key %d" key; coe self
2536 method private special1 key =
2537 let itemcount = source#getitemcount in
2538 let find start incr =
2539 let rec find i =
2540 if i = -1 || i = itemcount
2541 then -1
2542 else (
2543 if source#hasaction i
2544 then i
2545 else find (i + incr)
2548 find start
2550 let set active first =
2551 let first = bound first 0 (itemcount - fstate.maxrows) in
2552 state.text <- "";
2553 coe {< m_active = active; m_first = first >}
2555 let navigate incr =
2556 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2557 let active, first =
2558 let incr1 = if incr > 0 then 1 else -1 in
2559 if isvisible m_first m_active
2560 then
2561 let next =
2562 let next = m_active + incr in
2563 let next =
2564 if next < 0 || next >= itemcount
2565 then -1
2566 else find next incr1
2568 if next = -1 || abs (m_active - next) > fstate.maxrows
2569 then -1
2570 else next
2572 if next = -1
2573 then
2574 let first = m_first + incr in
2575 let first = bound first 0 (itemcount - 1) in
2576 let next =
2577 let next = m_active + incr in
2578 let next = bound next 0 (itemcount - 1) in
2579 find next ~-incr1
2581 let active = if next = -1 then m_active else next in
2582 active, first
2583 else
2584 let first = min next m_first in
2585 next, first
2586 else
2587 let first = m_first + incr in
2588 let first = bound first 0 (itemcount - 1) in
2589 let active =
2590 let next = m_active + incr in
2591 let next = bound next 0 (itemcount - 1) in
2592 let next = find next incr1 in
2593 if next = -1 || abs (m_active - first) > fstate.maxrows
2594 then m_active
2595 else next
2597 active, first
2599 G.postRedisplay "listview navigate";
2600 set active first;
2602 begin match key with
2603 | Glut.KEY_UP -> navigate ~-1
2604 | Glut.KEY_DOWN -> navigate 1
2605 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2606 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2608 | Glut.KEY_RIGHT ->
2609 state.text <- "";
2610 G.postRedisplay "listview right";
2611 coe {< m_pan = m_pan - 1 >}
2613 | Glut.KEY_LEFT ->
2614 state.text <- "";
2615 G.postRedisplay "listview left";
2616 coe {< m_pan = m_pan + 1 >}
2618 | Glut.KEY_HOME ->
2619 let active = find 0 1 in
2620 G.postRedisplay "listview home";
2621 set active 0;
2623 | Glut.KEY_END ->
2624 let first = max 0 (itemcount - fstate.maxrows) in
2625 let active = find (itemcount - 1) ~-1 in
2626 G.postRedisplay "listview end";
2627 set active first;
2629 | _ -> coe self
2630 end;
2632 method key key =
2633 match state.mode with
2634 | Textentry te -> textentrykeyboard key te; coe self
2635 | _ -> self#key1 key
2637 method special key =
2638 match state.mode with
2639 | Textentry te -> textentryspecial key te; coe self
2640 | _ -> self#special1 key
2642 method button button bstate x y =
2643 let opt =
2644 match button with
2645 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollbw ->
2646 G.postRedisplay "listview scroll";
2647 if bstate = Glut.DOWN
2648 then
2649 let _, position, sh = self#scrollph in
2650 if y > truncate position && y < truncate (position +. sh)
2651 then (
2652 state.mstate <- Mscrolly;
2653 Some (coe self)
2655 else
2656 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2657 let first = truncate (s *. float source#getitemcount) in
2658 let first = min source#getitemcount first in
2659 Some (coe {< m_first = first; m_active = first >})
2660 else (
2661 state.mstate <- Mnone;
2662 Some (coe self);
2664 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2665 begin match self#elemunder y with
2666 | Some n ->
2667 G.postRedisplay "listview click";
2668 source#exit
2669 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2670 | _ ->
2671 Some (coe self)
2673 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2674 let len = source#getitemcount in
2675 let first =
2676 if m_first + fstate.maxrows >= len
2677 then
2678 m_first
2679 else
2680 let first = m_first + (if n == 3 then -1 else 1) in
2681 bound first 0 (len - 1)
2683 G.postRedisplay "listview wheel";
2684 Some (coe {< m_first = first >})
2685 | _ ->
2686 Some (coe self)
2688 match opt with
2689 | None -> m_prev_uioh
2690 | Some uioh -> uioh
2692 method motion _ y =
2693 match state.mstate with
2694 | Mscrolly ->
2695 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2696 let first = truncate (s *. float source#getitemcount) in
2697 let first = min source#getitemcount first in
2698 G.postRedisplay "listview motion";
2699 coe {< m_first = first; m_active = first >}
2700 | _ -> coe self
2702 method pmotion x y =
2703 if x < conf.winw - conf.scrollbw
2704 then
2705 let n =
2706 match self#elemunder y with
2707 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2708 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2710 let o =
2711 if n != m_active
2712 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2713 else self
2715 coe o
2716 else (
2717 Glut.setCursor Glut.CURSOR_INHERIT;
2718 coe self
2721 method infochanged _ = ()
2723 method scrollpw = (0, 0.0, 0.0)
2724 method scrollph =
2725 let nfs = fstate.fontsize + 1 in
2726 let y = m_first * nfs in
2727 let itemcount = source#getitemcount in
2728 let maxi = max 0 (itemcount - fstate.maxrows) in
2729 let maxy = maxi * nfs in
2730 let p, h = scrollph y maxy in
2731 conf.scrollbw, p, h
2732 end;;
2734 class outlinelistview ~source =
2735 object (self)
2736 inherit listview ~source:(source :> lvsource) ~trusted:false as super
2738 method key key =
2739 match key with
2740 | 14 -> (* ctrl-n *)
2741 source#narrow m_qsearch;
2742 G.postRedisplay "outline ctrl-n";
2743 coe {< m_first = 0; m_active = 0 >}
2745 | 21 -> (* ctrl-u *)
2746 source#denarrow;
2747 G.postRedisplay "outline ctrl-u";
2748 state.text <- "";
2749 coe {< m_first = 0; m_active = 0 >}
2751 | 12 -> (* ctrl-l *)
2752 let first = m_active - (fstate.maxrows / 2) in
2753 G.postRedisplay "outline ctrl-l";
2754 coe {< m_first = first >}
2756 | 127 -> (* delete *)
2757 source#remove m_active;
2758 G.postRedisplay "outline delete";
2759 let active = max 0 (m_active-1) in
2760 coe {< m_first = firstof m_first active;
2761 m_active = active >}
2763 | key -> super#key key
2765 method special key =
2766 let calcfirst first active =
2767 if active > first
2768 then
2769 let rows = active - first in
2770 if rows > fstate.maxrows then active - fstate.maxrows else first
2771 else active
2773 let navigate incr =
2774 let active = m_active + incr in
2775 let active = bound active 0 (source#getitemcount - 1) in
2776 let first = calcfirst m_first active in
2777 G.postRedisplay "special outline navigate";
2778 coe {< m_active = active; m_first = first >}
2780 match key with
2781 | Glut.KEY_UP -> navigate ~-1
2782 | Glut.KEY_DOWN -> navigate 1
2783 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2784 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2786 | Glut.KEY_RIGHT ->
2787 let o =
2788 if Glut.getModifiers () land Glut.active_ctrl != 0
2789 then (
2790 G.postRedisplay "special outline right";
2791 {< m_pan = m_pan + 1 >}
2793 else self#updownlevel 1
2795 coe o
2797 | Glut.KEY_LEFT ->
2798 let o =
2799 if Glut.getModifiers () land Glut.active_ctrl != 0
2800 then (
2801 G.postRedisplay "special outline left";
2802 {< m_pan = m_pan - 1 >}
2804 else self#updownlevel ~-1
2806 coe o
2808 | Glut.KEY_HOME ->
2809 G.postRedisplay "special outline home";
2810 coe {< m_first = 0; m_active = 0 >}
2812 | Glut.KEY_END ->
2813 let active = source#getitemcount - 1 in
2814 let first = max 0 (active - fstate.maxrows) in
2815 G.postRedisplay "special outline end";
2816 coe {< m_active = active; m_first = first >}
2818 | _ -> super#special key
2821 let outlinesource usebookmarks =
2822 let empty = [||] in
2823 (object
2824 inherit lvsourcebase
2825 val mutable m_items = empty
2826 val mutable m_orig_items = empty
2827 val mutable m_prev_items = empty
2828 val mutable m_narrow_pattern = ""
2829 val mutable m_hadremovals = false
2831 method getitemcount =
2832 Array.length m_items + (if m_hadremovals then 1 else 0)
2834 method getitem n =
2835 if n == Array.length m_items && m_hadremovals
2836 then
2837 ("[Confirm removal]", 0)
2838 else
2839 let s, n, _ = m_items.(n) in
2840 (s, n)
2842 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
2843 ignore (uioh, first, pan, qsearch);
2844 let confrimremoval = m_hadremovals && active = Array.length m_items in
2845 let items =
2846 if String.length m_narrow_pattern = 0
2847 then m_orig_items
2848 else m_items
2850 if not cancel
2851 then (
2852 if not confrimremoval
2853 then(
2854 let _, _, anchor = m_items.(active) in
2855 gotoanchor anchor;
2856 m_items <- items;
2858 else (
2859 state.bookmarks <- Array.to_list m_items;
2860 m_orig_items <- m_items;
2863 else m_items <- items;
2864 None
2866 method hasaction _ = true
2868 method greetmsg =
2869 if Array.length m_items != Array.length m_orig_items
2870 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
2871 else ""
2873 method narrow pattern =
2874 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2875 match reopt with
2876 | None -> ()
2877 | Some re ->
2878 let rec loop accu n =
2879 if n = -1
2880 then (
2881 m_narrow_pattern <- pattern;
2882 m_items <- Array.of_list accu
2884 else
2885 let (s, _, _) as o = m_items.(n) in
2886 let accu =
2887 if (try ignore (Str.search_forward re s 0); true
2888 with Not_found -> false)
2889 then o :: accu
2890 else accu
2892 loop accu (n-1)
2894 loop [] (Array.length m_items - 1)
2896 method denarrow =
2897 m_orig_items <- (
2898 if usebookmarks
2899 then Array.of_list state.bookmarks
2900 else state.outlines
2902 m_items <- m_orig_items
2904 method remove m =
2905 if usebookmarks
2906 then
2907 if m >= 0 && m < Array.length m_items
2908 then (
2909 m_hadremovals <- true;
2910 m_items <- Array.init (Array.length m_items - 1) (fun n ->
2911 let n = if n >= m then n+1 else n in
2912 m_items.(n)
2916 method reset pageno items =
2917 m_hadremovals <- false;
2918 if m_orig_items == empty || m_prev_items != items
2919 then (
2920 m_orig_items <- items;
2921 if String.length m_narrow_pattern = 0
2922 then m_items <- items;
2924 m_prev_items <- items;
2925 let active =
2926 let rec loop n best bestd =
2927 if n = Array.length m_items
2928 then best
2929 else
2930 let (_, _, (outlinepageno, _)) = m_items.(n) in
2931 let d = abs (outlinepageno - pageno) in
2932 if d < bestd
2933 then loop (n+1) n d
2934 else loop (n+1) best bestd
2936 loop 0 ~-1 max_int
2938 m_active <- active;
2939 m_first <- firstof m_first active
2940 end)
2943 let enterselector usebookmarks =
2944 let source = outlinesource usebookmarks in
2945 fun errmsg ->
2946 let outlines =
2947 if usebookmarks
2948 then Array.of_list state.bookmarks
2949 else state.outlines
2951 if Array.length outlines = 0
2952 then (
2953 showtext ' ' errmsg;
2955 else (
2956 state.text <- source#greetmsg;
2957 Glut.setCursor Glut.CURSOR_INHERIT;
2958 let pageno =
2959 match state.layout with
2960 | [] -> -1
2961 | {pageno=pageno} :: _ -> pageno
2963 source#reset pageno outlines;
2964 state.uioh <- coe (new outlinelistview ~source);
2965 G.postRedisplay "enter selector";
2969 let enteroutlinemode =
2970 let f = enterselector false in
2971 fun ()-> f "Document has no outline";
2974 let enterbookmarkmode =
2975 let f = enterselector true in
2976 fun () -> f "Document has no bookmarks (yet)";
2979 let color_of_string s =
2980 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
2981 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
2985 let color_to_string (r, g, b) =
2986 let r = truncate (r *. 256.0)
2987 and g = truncate (g *. 256.0)
2988 and b = truncate (b *. 256.0) in
2989 Printf.sprintf "%d/%d/%d" r g b
2992 let irect_of_string s =
2993 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
2996 let irect_to_string (x0,y0,x1,y1) =
2997 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3000 let makecheckers () =
3001 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3002 following to say:
3003 converted by Issac Trotts. July 25, 2002 *)
3004 let image_height = 64
3005 and image_width = 64 in
3007 let make_image () =
3008 let image =
3009 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3011 for i = 0 to image_width - 1 do
3012 for j = 0 to image_height - 1 do
3013 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3014 (if (i land 8 ) lxor (j land 8) = 0
3015 then [|255;255;255|] else [|200;200;200|])
3016 done
3017 done;
3018 image
3020 let image = make_image () in
3021 let id = GlTex.gen_texture () in
3022 GlTex.bind_texture `texture_2d id;
3023 GlPix.store (`unpack_alignment 1);
3024 GlTex.image2d image;
3025 List.iter (GlTex.parameter ~target:`texture_2d)
3026 [ `wrap_s `repeat;
3027 `wrap_t `repeat;
3028 `mag_filter `nearest;
3029 `min_filter `nearest ];
3033 let setcheckers enabled =
3034 match state.texid with
3035 | None ->
3036 if enabled then state.texid <- Some (makecheckers ())
3038 | Some texid ->
3039 if not enabled
3040 then (
3041 GlTex.delete_texture texid;
3042 state.texid <- None;
3046 let int_of_string_with_suffix s =
3047 let l = String.length s in
3048 let s1, shift =
3049 if l > 1
3050 then
3051 let suffix = Char.lowercase s.[l-1] in
3052 match suffix with
3053 | 'k' -> String.sub s 0 (l-1), 10
3054 | 'm' -> String.sub s 0 (l-1), 20
3055 | 'g' -> String.sub s 0 (l-1), 30
3056 | _ -> s, 0
3057 else s, 0
3059 let n = int_of_string s1 in
3060 let m = n lsl shift in
3061 if m < 0 || m < n
3062 then raise (Failure "value too large")
3063 else m
3066 let string_with_suffix_of_int n =
3067 if n = 0
3068 then "0"
3069 else
3070 let n, s =
3071 if n = 0
3072 then 0, ""
3073 else (
3074 if n land ((1 lsl 20) - 1) = 0
3075 then n lsr 20, "M"
3076 else (
3077 if n land ((1 lsl 10) - 1) = 0
3078 then n lsr 10, "K"
3079 else n, ""
3083 let rec loop s n =
3084 let h = n mod 1000 in
3085 let n = n / 1000 in
3086 if n = 0
3087 then string_of_int h ^ s
3088 else (
3089 let s = Printf.sprintf "_%03d%s" h s in
3090 loop s n
3093 loop "" n ^ s;
3096 let describe_location () =
3097 let f (fn, _) l =
3098 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3100 let fn, ln = List.fold_left f (-1, -1) state.layout in
3101 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3102 let percent =
3103 if maxy <= 0
3104 then 100.
3105 else (100. *. (float state.y /. float maxy))
3107 if fn = ln
3108 then
3109 Printf.sprintf "page %d of %d [%.2f%%]"
3110 (fn+1) state.pagecount percent
3111 else
3112 Printf.sprintf
3113 "pages %d-%d of %d [%.2f%%]"
3114 (fn+1) (ln+1) state.pagecount percent
3117 let enterinfomode =
3118 let btos b = if b then "\xe2\x88\x9a" else "" in
3119 let showextended = ref false in
3120 let leave mode = function
3121 | Confirm -> state.mode <- mode
3122 | Cancel -> state.mode <- mode in
3123 let src =
3124 (object
3125 val mutable m_first_time = true
3126 val mutable m_l = []
3127 val mutable m_a = [||]
3128 val mutable m_prev_uioh = nouioh
3129 val mutable m_prev_mode = View
3131 inherit lvsourcebase
3133 method reset prev_mode prev_uioh =
3134 m_a <- Array.of_list (List.rev m_l);
3135 m_l <- [];
3136 m_prev_mode <- prev_mode;
3137 m_prev_uioh <- prev_uioh;
3138 if m_first_time
3139 then (
3140 let rec loop n =
3141 if n >= Array.length m_a
3142 then ()
3143 else
3144 match m_a.(n) with
3145 | _, _, _, Action _ -> m_active <- n
3146 | _ -> loop (n+1)
3148 loop 0;
3149 m_first_time <- false;
3152 method int name get set =
3153 m_l <-
3154 (name, `int get, 1, Action (
3155 fun u ->
3156 let ondone s =
3157 try set (int_of_string s)
3158 with exn ->
3159 state.text <- Printf.sprintf "bad integer `%s': %s"
3160 s (Printexc.to_string exn)
3162 state.text <- "";
3163 let te = name ^ ": ", "", None, intentry, ondone in
3164 state.mode <- Textentry (te, leave m_prev_mode);
3166 )) :: m_l
3168 method int_with_suffix name get set =
3169 m_l <-
3170 (name, `intws get, 1, Action (
3171 fun u ->
3172 let ondone s =
3173 try set (int_of_string_with_suffix s)
3174 with exn ->
3175 state.text <- Printf.sprintf "bad integer `%s': %s"
3176 s (Printexc.to_string exn)
3178 state.text <- "";
3179 let te =
3180 name ^ ": ", "", None, intentry_with_suffix, ondone
3182 state.mode <- Textentry (te, leave m_prev_mode);
3184 )) :: m_l
3186 method bool ?(offset=1) ?(btos=btos) name get set =
3187 m_l <-
3188 (name, `bool (btos, get), offset, Action (
3189 fun u ->
3190 let v = get () in
3191 set (not v);
3193 )) :: m_l
3195 method color name get set =
3196 m_l <-
3197 (name, `color get, 1, Action (
3198 fun u ->
3199 let invalid = (nan, nan, nan) in
3200 let ondone s =
3201 let c =
3202 try color_of_string s
3203 with exn ->
3204 state.text <- Printf.sprintf "bad color `%s': %s"
3205 s (Printexc.to_string exn);
3206 invalid
3208 if c <> invalid
3209 then set c;
3211 let te = name ^ ": ", "", None, textentry, ondone in
3212 state.text <- color_to_string (get ());
3213 state.mode <- Textentry (te, leave m_prev_mode);
3215 )) :: m_l
3217 method string name get set =
3218 m_l <-
3219 (name, `string get, 1, Action (
3220 fun u ->
3221 let ondone s = set s in
3222 let te = name ^ ": ", "", None, textentry, ondone in
3223 state.mode <- Textentry (te, leave m_prev_mode);
3225 )) :: m_l
3227 method colorspace name get set =
3228 m_l <-
3229 (name, `string get, 1, Action (
3230 fun _ ->
3231 let source =
3232 let vals = [| "rgb"; "bgr"; "gray" |] in
3233 (object
3234 inherit lvsourcebase
3236 initializer
3237 m_active <- int_of_colorspace conf.colorspace;
3238 m_first <- 0;
3240 method getitemcount = Array.length vals
3241 method getitem n = (vals.(n), 0)
3242 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3243 ignore (uioh, first, pan, qsearch);
3244 if not cancel then set active;
3245 None
3246 method hasaction _ = true
3247 end)
3249 state.text <- "";
3250 coe (new listview ~source ~trusted:true)
3251 )) :: m_l
3253 method caption s offset =
3254 m_l <- (s, `empty, offset, Noaction) :: m_l
3256 method caption2 s f offset =
3257 m_l <- (s, `string f, offset, Noaction) :: m_l
3259 method getitemcount = Array.length m_a
3261 method getitem n =
3262 let tostr = function
3263 | `int f -> string_of_int (f ())
3264 | `intws f -> string_with_suffix_of_int (f ())
3265 | `string f -> f ()
3266 | `color f -> color_to_string (f ())
3267 | `bool (btos, f) -> btos (f ())
3268 | `empty -> ""
3270 let name, t, offset, _ = m_a.(n) in
3271 ((let s = tostr t in
3272 if String.length s > 0
3273 then Printf.sprintf "%s\t%s" name s
3274 else name),
3275 offset)
3277 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3278 let uiohopt =
3279 if not cancel
3280 then (
3281 m_qsearch <- qsearch;
3282 let uioh =
3283 match m_a.(active) with
3284 | _, _, _, Action f -> f uioh
3285 | _ -> uioh
3287 Some uioh
3289 else None
3291 m_active <- active;
3292 m_first <- first;
3293 m_pan <- pan;
3294 uiohopt
3296 method hasaction n =
3297 match m_a.(n) with
3298 | _, _, _, Action _ -> true
3299 | _ -> false
3300 end)
3302 let rec fillsrc prevmode prevuioh =
3303 let sep () = src#caption "" 0 in
3304 let colorp name get set =
3305 src#string name
3306 (fun () -> color_to_string (get ()))
3307 (fun v ->
3309 let c = color_of_string v in
3310 set c
3311 with exn ->
3312 state.text <- Printf.sprintf "bad color `%s': %s"
3313 v (Printexc.to_string exn);
3316 let oldmode = state.mode in
3317 let birdseye = isbirdseye state.mode in
3319 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3321 src#bool "presentation mode"
3322 (fun () -> conf.presentation)
3323 (fun v ->
3324 conf.presentation <- v;
3325 state.anchor <- getanchor ();
3326 represent ());
3328 src#bool "ignore case in searches"
3329 (fun () -> conf.icase)
3330 (fun v -> conf.icase <- v);
3332 src#bool "preload"
3333 (fun () -> conf.preload)
3334 (fun v -> conf.preload <- v);
3336 src#bool "highlight links"
3337 (fun () -> conf.hlinks)
3338 (fun v -> conf.hlinks <- v);
3340 src#bool "under info"
3341 (fun () -> conf.underinfo)
3342 (fun v -> conf.underinfo <- v);
3344 src#bool "persistent bookmarks"
3345 (fun () -> conf.savebmarks)
3346 (fun v -> conf.savebmarks <- v);
3348 src#bool "proportional display"
3349 (fun () -> conf.proportional)
3350 (fun v -> reqlayout conf.angle v);
3352 src#bool "trim margins"
3353 (fun () -> conf.trimmargins)
3354 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3356 src#bool "persistent location"
3357 (fun () -> conf.jumpback)
3358 (fun v -> conf.jumpback <- v);
3360 sep ();
3361 src#int "vertical margin"
3362 (fun () -> conf.interpagespace)
3363 (fun n ->
3364 conf.interpagespace <- n;
3365 let pageno, py =
3366 match state.layout with
3367 | [] -> 0, 0
3368 | l :: _ ->
3369 l.pageno, l.pagey
3371 state.maxy <- calcheight ();
3372 let y = getpagey pageno in
3373 gotoy (y + py)
3376 src#int "page bias"
3377 (fun () -> conf.pagebias)
3378 (fun v -> conf.pagebias <- v);
3380 src#int "scroll step"
3381 (fun () -> conf.scrollstep)
3382 (fun n -> conf.scrollstep <- n);
3384 src#int "auto scroll step"
3385 (fun () ->
3386 match state.autoscroll with
3387 | Some step -> step
3388 | _ -> conf.autoscrollstep)
3389 (fun n ->
3390 if state.autoscroll <> None
3391 then state.autoscroll <- Some n;
3392 conf.autoscrollstep <- n);
3394 src#int "zoom"
3395 (fun () -> truncate (conf.zoom *. 100.))
3396 (fun v -> setzoom ((float v) /. 100.));
3398 src#int "rotation"
3399 (fun () -> conf.angle)
3400 (fun v -> reqlayout v conf.proportional);
3402 src#int "scroll bar width"
3403 (fun () -> state.scrollw)
3404 (fun v ->
3405 state.scrollw <- v;
3406 conf.scrollbw <- v;
3407 reshape conf.winw conf.winh;
3410 src#int "scroll handle height"
3411 (fun () -> conf.scrollh)
3412 (fun v -> conf.scrollh <- v;);
3414 src#int "thumbnail width"
3415 (fun () -> conf.thumbw)
3416 (fun v ->
3417 conf.thumbw <- min 4096 v;
3418 match oldmode with
3419 | Birdseye beye ->
3420 leavebirdseye beye false;
3421 enterbirdseye ()
3422 | _ -> ()
3425 sep ();
3426 src#caption "Presentation mode" 0;
3427 src#bool "scrollbar visible"
3428 (fun () -> conf.scrollbarinpm)
3429 (fun v ->
3430 if v != conf.scrollbarinpm
3431 then (
3432 conf.scrollbarinpm <- v;
3433 if conf.presentation
3434 then (
3435 state.scrollw <- if v then conf.scrollbw else 0;
3436 reshape conf.winw conf.winh;
3441 sep ();
3442 src#caption "Pixmap cache" 0;
3443 src#int_with_suffix "size (advisory)"
3444 (fun () -> conf.memlimit)
3445 (fun v -> conf.memlimit <- v);
3447 src#caption2 "used"
3448 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3449 (string_with_suffix_of_int state.memused)
3450 (Hashtbl.length state.tilemap)) 1;
3452 sep ();
3453 src#caption "Layout" 0;
3454 src#caption2 "Dimension"
3455 (fun () ->
3456 Printf.sprintf "%dx%d (virtual %dx%d)"
3457 conf.winw conf.winh
3458 state.w state.maxy)
3460 if conf.debug
3461 then
3462 src#caption2 "Position" (fun () ->
3463 Printf.sprintf "%dx%d" state.x state.y
3465 else
3466 src#caption2 "Visible" (fun () -> describe_location ()) 1
3469 sep ();
3470 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3471 "Save these parameters as global defaults at exit"
3472 (fun () -> conf.bedefault)
3473 (fun v -> conf.bedefault <- v)
3476 sep ();
3477 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3478 src#bool ~offset:0 ~btos "Extended parameters"
3479 (fun () -> !showextended)
3480 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3481 if !showextended
3482 then (
3483 src#bool "checkers"
3484 (fun () -> conf.checkers)
3485 (fun v -> conf.checkers <- v; setcheckers v);
3486 src#bool "verbose"
3487 (fun () -> conf.verbose)
3488 (fun v -> conf.verbose <- v);
3489 src#bool "invert colors"
3490 (fun () -> conf.invert)
3491 (fun v -> conf.invert <- v);
3492 src#bool "max fit"
3493 (fun () -> conf.maxhfit)
3494 (fun v -> conf.maxhfit <- v);
3495 src#bool "redirect stderr"
3496 (fun () -> conf.redirectstderr)
3497 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3498 src#string "uri launcher"
3499 (fun () -> conf.urilauncher)
3500 (fun v -> conf.urilauncher <- v);
3501 src#string "tile size"
3502 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3503 (fun v ->
3505 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3506 conf.tileh <- max 64 w;
3507 conf.tilew <- max 64 h;
3508 flushtiles ();
3509 with exn ->
3510 state.text <- Printf.sprintf "bad tile size `%s': %s"
3511 v (Printexc.to_string exn));
3512 src#int "texture count"
3513 (fun () -> conf.texcount)
3514 (fun v ->
3515 conf.texcount <- v;
3516 wcmd "texcount" [`i conf.texcount];
3518 src#int "slice height"
3519 (fun () -> conf.sliceheight)
3520 (fun v ->
3521 conf.sliceheight <- v;
3522 wcmd "sliceh" [`i conf.sliceheight];
3524 src#int "anti-aliasing level"
3525 (fun () -> conf.aalevel)
3526 (fun v ->
3527 conf.aalevel <- bound v 0 8;
3528 state.anchor <- getanchor ();
3529 opendoc state.path state.password;
3531 src#int "ui font size"
3532 (fun () -> fstate.fontsize)
3533 (fun v -> setfontsize (bound v 5 100));
3534 colorp "background color"
3535 (fun () -> conf.bgcolor)
3536 (fun v -> conf.bgcolor <- v);
3537 src#bool "crop hack"
3538 (fun () -> conf.crophack)
3539 (fun v -> conf.crophack <- v);
3540 src#string "trim fuzz"
3541 (fun () -> irect_to_string conf.trimfuzz)
3542 (fun v ->
3544 conf.trimfuzz <- irect_of_string v;
3545 if conf.trimmargins
3546 then settrim true conf.trimfuzz;
3547 with exn ->
3548 state.text <- Printf.sprintf "bad irect `%s': %s"
3549 v (Printexc.to_string exn)
3551 src#string "throttle"
3552 (fun () ->
3553 match conf.maxwait with
3554 | None -> "show place holder if page is not ready"
3555 | Some time ->
3556 if time = infinity
3557 then "wait for page to fully render"
3558 else
3559 "wait " ^ string_of_float time
3560 ^ " seconds before showing placeholder"
3562 (fun v ->
3564 let f = float_of_string v in
3565 if f <= 0.0
3566 then conf.maxwait <- None
3567 else conf.maxwait <- Some f
3568 with exn ->
3569 state.text <- Printf.sprintf "bad time `%s': %s"
3570 v (Printexc.to_string exn)
3572 src#colorspace "color space"
3573 (fun () -> colorspace_to_string conf.colorspace)
3574 (fun v ->
3575 conf.colorspace <- colorspace_of_int v;
3576 wcmd "cs" [`i v];
3577 load state.layout;
3581 sep ();
3582 src#caption "Document" 0;
3583 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3584 if conf.trimmargins
3585 then (
3586 sep ();
3587 src#caption "Trimmed margins" 0;
3588 src#caption2 "Dimensions"
3589 (fun () -> string_of_int (List.length state.pdims)) 1;
3592 src#reset prevmode prevuioh;
3594 fun () ->
3595 state.text <- "";
3596 let prevmode = state.mode
3597 and prevuioh = state.uioh in
3598 fillsrc prevmode prevuioh;
3599 let source = (src :> lvsource) in
3600 state.uioh <- coe (object (self)
3601 inherit listview ~source ~trusted:true as super
3602 val mutable m_prevmemused = 0
3603 method infochanged = function
3604 | Memused ->
3605 if m_prevmemused != state.memused
3606 then (
3607 m_prevmemused <- state.memused;
3608 G.postRedisplay "memusedchanged";
3610 | Pdim -> G.postRedisplay "pdimchanged"
3611 | Docinfo -> fillsrc prevmode prevuioh
3613 method special key =
3614 if Glut.getModifiers () land Glut.active_ctrl = 0
3615 then
3616 match key with
3617 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
3618 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
3619 | _ -> super#special key
3620 else super#special key
3621 end);
3622 G.postRedisplay "info";
3625 let enterhelpmode =
3626 let source =
3627 (object
3628 inherit lvsourcebase
3629 method getitemcount = Array.length state.help
3630 method getitem n =
3631 let s, n, _ = state.help.(n) in
3632 (s, n)
3634 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3635 let optuioh =
3636 if not cancel
3637 then (
3638 m_qsearch <- qsearch;
3639 match state.help.(active) with
3640 | _, _, Action f -> Some (f uioh)
3641 | _ -> Some (uioh)
3643 else None
3645 m_active <- active;
3646 m_first <- first;
3647 m_pan <- pan;
3648 optuioh
3650 method hasaction n =
3651 match state.help.(n) with
3652 | _, _, Action _ -> true
3653 | _ -> false
3655 initializer
3656 m_active <- -1
3657 end)
3658 in fun () ->
3659 state.uioh <- coe (new listview ~source ~trusted:true);
3660 G.postRedisplay "help";
3663 let entermsgsmode =
3664 let msgsource =
3665 let re = Str.regexp "[\r\n]" in
3666 (object
3667 inherit lvsourcebase
3668 val mutable m_items = [||]
3670 method getitemcount = 1 + Array.length m_items
3672 method getitem n =
3673 if n = 0
3674 then "[Clear]", 0
3675 else m_items.(n-1), 0
3677 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3678 ignore uioh;
3679 if not cancel
3680 then (
3681 if active = 0
3682 then Buffer.clear state.errmsgs;
3683 m_qsearch <- qsearch;
3685 m_active <- active;
3686 m_first <- first;
3687 m_pan <- pan;
3688 None
3690 method hasaction n =
3691 n = 0
3693 method reset =
3694 state.newerrmsgs <- false;
3695 let l = Str.split re (Buffer.contents state.errmsgs) in
3696 m_items <- Array.of_list l
3698 initializer
3699 m_active <- 0
3700 end)
3701 in fun () ->
3702 state.text <- "";
3703 msgsource#reset;
3704 let source = (msgsource :> lvsource) in
3705 state.uioh <- coe (object
3706 inherit listview ~source ~trusted:false as super
3707 method display =
3708 if state.newerrmsgs
3709 then msgsource#reset;
3710 super#display
3711 end);
3712 G.postRedisplay "msgs";
3715 let quickbookmark ?title () =
3716 match state.layout with
3717 | [] -> ()
3718 | l :: _ ->
3719 let title =
3720 match title with
3721 | None ->
3722 let sec = Unix.gettimeofday () in
3723 let tm = Unix.localtime sec in
3724 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
3725 (l.pageno+1)
3726 tm.Unix.tm_mday
3727 tm.Unix.tm_mon
3728 (tm.Unix.tm_year + 1900)
3729 tm.Unix.tm_hour
3730 tm.Unix.tm_min
3731 | Some title -> title
3733 state.bookmarks <-
3734 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
3735 :: state.bookmarks
3738 let doreshape w h =
3739 state.fullscreen <- None;
3740 Glut.reshapeWindow w h;
3743 let viewkeyboard key =
3744 let enttext te =
3745 let mode = state.mode in
3746 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3747 state.text <- "";
3748 enttext ();
3749 G.postRedisplay "view:enttext"
3751 let c = Char.chr key in
3752 match c with
3753 | '\027' | 'q' -> (* escape *)
3754 begin match state.mstate with
3755 | Mzoomrect _ ->
3756 state.mstate <- Mnone;
3757 Glut.setCursor Glut.CURSOR_INHERIT;
3758 G.postRedisplay "kill zoom rect";
3759 | _ ->
3760 raise Quit
3761 end;
3763 | '\008' -> (* backspace *)
3764 let y = getnav ~-1 in
3765 gotoy_and_clear_text y
3767 | 'o' ->
3768 enteroutlinemode ()
3770 | 'u' ->
3771 state.rects <- [];
3772 state.text <- "";
3773 G.postRedisplay "dehighlight";
3775 | '/' | '?' ->
3776 let ondone isforw s =
3777 cbput state.hists.pat s;
3778 state.searchpattern <- s;
3779 search s isforw
3781 let s = String.create 1 in
3782 s.[0] <- c;
3783 enttext (s, "", Some (onhist state.hists.pat),
3784 textentry, ondone (c ='/'))
3786 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3787 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3788 setzoom (conf.zoom +. incr)
3790 | '+' ->
3791 let ondone s =
3792 let n =
3793 try int_of_string s with exc ->
3794 state.text <- Printf.sprintf "bad integer `%s': %s"
3795 s (Printexc.to_string exc);
3796 max_int
3798 if n != max_int
3799 then (
3800 conf.pagebias <- n;
3801 state.text <- "page bias is now " ^ string_of_int n;
3804 enttext ("page bias: ", "", None, intentry, ondone)
3806 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3807 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3808 setzoom (max 0.01 (conf.zoom -. decr))
3810 | '-' ->
3811 let ondone msg = state.text <- msg in
3812 enttext (
3813 "option [acfhilpstvAPRSZTI]: ", "", None,
3814 optentry state.mode, ondone
3817 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3818 setzoom 1.0
3820 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3821 let zoom = zoomforh conf.winw conf.winh state.scrollw in
3822 if zoom < 1.0
3823 then setzoom zoom
3825 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3826 togglebirdseye ()
3828 | '0' .. '9' ->
3829 let ondone s =
3830 let n =
3831 try int_of_string s with exc ->
3832 state.text <- Printf.sprintf "bad integer `%s': %s"
3833 s (Printexc.to_string exc);
3836 if n >= 0
3837 then (
3838 addnav ();
3839 cbput state.hists.pag (string_of_int n);
3840 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
3843 let pageentry text key =
3844 match Char.unsafe_chr key with
3845 | 'g' -> TEdone text
3846 | _ -> intentry text key
3848 let text = "x" in text.[0] <- c;
3849 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
3851 | 'b' ->
3852 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
3853 reshape conf.winw conf.winh;
3855 | 'l' ->
3856 conf.hlinks <- not conf.hlinks;
3857 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3858 G.postRedisplay "toggle highlightlinks";
3860 | 'a' ->
3861 begin match state.autoscroll with
3862 | Some step ->
3863 conf.autoscrollstep <- step;
3864 state.autoscroll <- None
3865 | None ->
3866 if conf.autoscrollstep = 0
3867 then state.autoscroll <- Some 1
3868 else state.autoscroll <- Some conf.autoscrollstep
3871 | 'P' ->
3872 conf.presentation <- not conf.presentation;
3873 if conf.presentation
3874 then (
3875 if not conf.scrollbarinpm
3876 then state.scrollw <- 0;
3878 else
3879 state.scrollw <- conf.scrollbw;
3881 showtext ' ' ("presentation mode " ^
3882 if conf.presentation then "on" else "off");
3883 state.anchor <- getanchor ();
3884 represent ()
3886 | 'f' ->
3887 begin match state.fullscreen with
3888 | None ->
3889 state.fullscreen <- Some (conf.winw, conf.winh);
3890 Glut.fullScreen ()
3891 | Some (w, h) ->
3892 state.fullscreen <- None;
3893 doreshape w h
3896 | 'g' ->
3897 gotoy_and_clear_text 0
3899 | 'G' ->
3900 gotopage1 (state.pagecount - 1) 0
3902 | 'n' ->
3903 search state.searchpattern true
3905 | 'p' | 'N' ->
3906 search state.searchpattern false
3908 | 't' ->
3909 begin match state.layout with
3910 | [] -> ()
3911 | l :: _ ->
3912 gotoy_and_clear_text (getpagey l.pageno)
3915 | ' ' ->
3916 begin match List.rev state.layout with
3917 | [] -> ()
3918 | l :: _ ->
3919 let pageno = min (l.pageno+1) (state.pagecount-1) in
3920 gotoy_and_clear_text (getpagey pageno)
3923 | '\127' -> (* del *)
3924 begin match state.layout with
3925 | [] -> ()
3926 | l :: _ ->
3927 let pageno = max 0 (l.pageno-1) in
3928 gotoy_and_clear_text (getpagey pageno)
3931 | '=' ->
3932 showtext ' ' (describe_location ());
3934 | 'w' ->
3935 begin match state.layout with
3936 | [] -> ()
3937 | l :: _ ->
3938 doreshape (l.pagew + state.scrollw) l.pageh;
3939 G.postRedisplay "w"
3942 | '\'' ->
3943 enterbookmarkmode ()
3945 | 'h' ->
3946 enterhelpmode ()
3948 | 'i' ->
3949 enterinfomode ()
3951 | 'e' when conf.redirectstderr ->
3952 entermsgsmode ()
3954 | 'm' ->
3955 let ondone s =
3956 match state.layout with
3957 | l :: _ ->
3958 state.bookmarks <-
3959 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
3960 :: state.bookmarks
3961 | _ -> ()
3963 enttext ("bookmark: ", "", None, textentry, ondone)
3965 | '~' ->
3966 quickbookmark ();
3967 showtext ' ' "Quick bookmark added";
3969 | 'z' ->
3970 begin match state.layout with
3971 | l :: _ ->
3972 let rect = getpdimrect l.pagedimno in
3973 let w, h =
3974 if conf.crophack
3975 then
3976 (truncate (1.8 *. (rect.(1) -. rect.(0))),
3977 truncate (1.2 *. (rect.(3) -. rect.(0))))
3978 else
3979 (truncate (rect.(1) -. rect.(0)),
3980 truncate (rect.(3) -. rect.(0)))
3982 let w = truncate ((float w)*.conf.zoom)
3983 and h = truncate ((float h)*.conf.zoom) in
3984 if w != 0 && h != 0
3985 then (
3986 state.anchor <- getanchor ();
3987 doreshape (w + state.scrollw) (h + conf.interpagespace)
3989 G.postRedisplay "z";
3991 | [] -> ()
3994 | '\000' -> (* ctrl-2 *)
3995 let maxw = getmaxw () in
3996 if maxw > 0.0
3997 then setzoom (maxw /. float conf.winw)
3999 | '<' | '>' ->
4000 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
4002 | '[' | ']' ->
4003 conf.colorscale <-
4004 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
4006 G.postRedisplay "brightness";
4008 | 'k' ->
4009 begin match state.mode with
4010 | Birdseye beye -> upbirdseye beye
4011 | _ -> gotoy (clamp (-conf.scrollstep))
4014 | 'j' ->
4015 begin match state.mode with
4016 | Birdseye beye -> downbirdseye beye
4017 | _ -> gotoy (clamp conf.scrollstep)
4020 | 'r' ->
4021 state.anchor <- getanchor ();
4022 opendoc state.path state.password
4024 | 'v' when conf.debug ->
4025 state.rects <- [];
4026 List.iter (fun l ->
4027 match getopaque l.pageno with
4028 | None -> ()
4029 | Some opaque ->
4030 let x0, y0, x1, y1 = pagebbox opaque in
4031 let a,b = float x0, float y0 in
4032 let c,d = float x1, float y0 in
4033 let e,f = float x1, float y1 in
4034 let h,j = float x0, float y1 in
4035 let rect = (a,b,c,d,e,f,h,j) in
4036 debugrect rect;
4037 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4038 ) state.layout;
4039 G.postRedisplay "v";
4041 | _ ->
4042 vlog "huh? %d %c" key (Char.chr key);
4045 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
4046 match key with
4047 | 27 -> (* escape *)
4048 leavebirdseye beye true
4050 | 12 -> (* ctrl-l *)
4051 let y, h = getpageyh pageno in
4052 let top = (conf.winh - h) / 2 in
4053 gotoy (max 0 (y - top))
4055 | 13 -> (* enter *)
4056 leavebirdseye beye false
4058 | _ ->
4059 viewkeyboard key
4062 let keyboard ~key ~x ~y =
4063 ignore x;
4064 ignore y;
4065 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
4066 then wcmd "interrupt" []
4067 else state.uioh <- state.uioh#key key
4070 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
4071 match key with
4072 | Glut.KEY_UP -> upbirdseye beye
4073 | Glut.KEY_DOWN -> downbirdseye beye
4075 | Glut.KEY_PAGE_UP ->
4076 begin match state.layout with
4077 | l :: _ ->
4078 if l.pagey != 0
4079 then (
4080 state.mode <- Birdseye (
4081 conf, leftx, l.pageno, hooverpageno, anchor
4083 gotopage1 l.pageno 0;
4085 else (
4086 let layout = layout (state.y-conf.winh) conf.winh in
4087 match layout with
4088 | [] -> gotoy (clamp (-conf.winh))
4089 | l :: _ ->
4090 state.mode <- Birdseye (
4091 conf, leftx, l.pageno, hooverpageno, anchor
4093 gotopage1 l.pageno 0
4096 | [] -> gotoy (clamp (-conf.winh))
4097 end;
4099 | Glut.KEY_PAGE_DOWN ->
4100 begin match List.rev state.layout with
4101 | l :: _ ->
4102 let layout = layout (state.y + conf.winh) conf.winh in
4103 begin match layout with
4104 | [] ->
4105 let incr = l.pageh - l.pagevh in
4106 if incr = 0
4107 then (
4108 state.mode <-
4109 Birdseye (
4110 conf, leftx, state.pagecount - 1, hooverpageno, anchor
4112 G.postRedisplay "birdseye pagedown";
4114 else gotoy (clamp (incr + conf.interpagespace*2));
4116 | l :: _ ->
4117 state.mode <-
4118 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
4119 gotopage1 l.pageno 0;
4122 | [] -> gotoy (clamp conf.winh)
4123 end;
4125 | Glut.KEY_HOME ->
4126 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
4127 gotopage1 0 0
4129 | Glut.KEY_END ->
4130 let pageno = state.pagecount - 1 in
4131 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
4132 if not (pagevisible state.layout pageno)
4133 then
4134 let h =
4135 match List.rev state.pdims with
4136 | [] -> conf.winh
4137 | (_, _, h, _) :: _ -> h
4139 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4140 else G.postRedisplay "birdseye end";
4141 | _ -> ()
4144 let setautoscrollspeed step goingdown =
4145 let incr = max 1 ((abs step) / 2) in
4146 let incr = if goingdown then incr else -incr in
4147 let astep = step + incr in
4148 state.autoscroll <- Some astep;
4151 let special ~key ~x ~y =
4152 ignore x;
4153 ignore y;
4154 state.uioh <- state.uioh#special key
4157 let drawpage l =
4158 let color =
4159 match state.mode with
4160 | Textentry _ -> scalecolor 0.4
4161 | View -> scalecolor 1.0
4162 | Birdseye (_, _, pageno, hooverpageno, _) ->
4163 if l.pageno = hooverpageno
4164 then scalecolor 0.9
4165 else (
4166 if l.pageno = pageno
4167 then scalecolor 1.0
4168 else scalecolor 0.8
4171 drawtiles l color;
4172 begin match getopaque l.pageno with
4173 | Some opaque ->
4174 if tileready l l.pagex l.pagey
4175 then
4176 let x = l.pagedispx - l.pagex
4177 and y = l.pagedispy - l.pagey in
4178 postprocess opaque conf.hlinks x y;
4180 | _ -> ()
4181 end;
4184 let scrollindicator () =
4185 let sbw, ph, sh = state.uioh#scrollph in
4186 let sbh, pw, sw = state.uioh#scrollpw in
4188 GlDraw.color (0.64, 0.64, 0.64);
4189 GlDraw.rect
4190 (float (conf.winw - sbw), 0.)
4191 (float conf.winw, float conf.winh)
4193 GlDraw.rect
4194 (0., float (conf.winh - sbh))
4195 (float (conf.winw - state.scrollw - 1), float conf.winh)
4197 GlDraw.color (0.0, 0.0, 0.0);
4199 GlDraw.rect
4200 (float (conf.winw - sbw), ph)
4201 (float conf.winw, ph +. sh)
4203 GlDraw.rect
4204 (pw, float (conf.winh - sbh))
4205 (pw +. sw, float conf.winh)
4209 let pagetranslatepoint l x y =
4210 let dy = y - l.pagedispy in
4211 let y = dy + l.pagey in
4212 let dx = x - l.pagedispx in
4213 let x = dx + l.pagex in
4214 (x, y);
4217 let showsel () =
4218 match state.mstate with
4219 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4222 | Msel ((x0, y0), (x1, y1)) ->
4223 let rec loop = function
4224 | l :: ls ->
4225 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4226 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4227 then
4228 match getopaque l.pageno with
4229 | Some opaque ->
4230 let dx, dy = pagetranslatepoint l 0 0 in
4231 let x0 = x0 + dx
4232 and y0 = y0 + dy
4233 and x1 = x1 + dx
4234 and y1 = y1 + dy in
4235 GlMat.mode `modelview;
4236 GlMat.push ();
4237 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4238 seltext opaque (x0, y0, x1, y1);
4239 GlMat.pop ();
4240 | _ -> ()
4241 else loop ls
4242 | [] -> ()
4244 loop state.layout
4247 let showrects () =
4248 Gl.enable `blend;
4249 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4250 GlDraw.polygon_mode `both `fill;
4251 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4252 List.iter
4253 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4254 List.iter (fun l ->
4255 if l.pageno = pageno
4256 then (
4257 let dx = float (l.pagedispx - l.pagex) in
4258 let dy = float (l.pagedispy - l.pagey) in
4259 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4260 GlDraw.begins `quads;
4262 GlDraw.vertex2 (x0+.dx, y0+.dy);
4263 GlDraw.vertex2 (x1+.dx, y1+.dy);
4264 GlDraw.vertex2 (x2+.dx, y2+.dy);
4265 GlDraw.vertex2 (x3+.dx, y3+.dy);
4267 GlDraw.ends ();
4269 ) state.layout
4270 ) state.rects
4272 Gl.disable `blend;
4275 let display () =
4276 GlClear.color (scalecolor2 conf.bgcolor);
4277 GlClear.clear [`color];
4278 List.iter drawpage state.layout;
4279 showrects ();
4280 showsel ();
4281 state.uioh#display;
4282 scrollindicator ();
4283 begin match state.mstate with
4284 | Mzoomrect ((x0, y0), (x1, y1)) ->
4285 Gl.enable `blend;
4286 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4287 GlDraw.polygon_mode `both `fill;
4288 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4289 GlDraw.rect (float x0, float y0)
4290 (float x1, float y1);
4291 Gl.disable `blend;
4292 | _ -> ()
4293 end;
4294 enttext ();
4295 Glut.swapBuffers ();
4298 let getunder x y =
4299 let rec f = function
4300 | l :: rest ->
4301 begin match getopaque l.pageno with
4302 | Some opaque ->
4303 let x0 = l.pagedispx in
4304 let x1 = x0 + l.pagevw in
4305 let y0 = l.pagedispy in
4306 let y1 = y0 + l.pagevh in
4307 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4308 then
4309 let px, py = pagetranslatepoint l x y in
4310 match whatsunder opaque px py with
4311 | Unone -> f rest
4312 | under -> under
4313 else f rest
4314 | _ ->
4315 f rest
4317 | [] -> Unone
4319 f state.layout
4322 let zoomrect x y x1 y1 =
4323 let x0 = min x x1
4324 and x1 = max x x1
4325 and y0 = min y y1 in
4326 gotoy (state.y + y0);
4327 state.anchor <- getanchor ();
4328 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4329 let margin =
4330 if state.w < conf.winw - state.scrollw
4331 then (conf.winw - state.scrollw - state.w) / 2
4332 else 0
4334 state.x <- (state.x + margin) - x0;
4335 setzoom zoom;
4336 Glut.setCursor Glut.CURSOR_INHERIT;
4337 state.mstate <- Mnone;
4340 let scrollx x =
4341 let winw = conf.winw - state.scrollw - 1 in
4342 let s = float x /. float winw in
4343 let destx = truncate (float (state.w + winw) *. s) in
4344 state.x <- winw - destx;
4345 gotoy_and_clear_text state.y;
4346 state.mstate <- Mscrollx;
4349 let scrolly y =
4350 let s = float y /. float conf.winh in
4351 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4352 gotoy_and_clear_text desty;
4353 state.mstate <- Mscrolly;
4356 let viewmouse button bstate x y =
4357 match button with
4358 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4359 if Glut.getModifiers () land Glut.active_ctrl != 0
4360 then (
4361 match state.mstate with
4362 | Mzoom (oldn, i) ->
4363 if oldn = n
4364 then (
4365 if i = 2
4366 then
4367 let incr =
4368 match n with
4369 | 4 ->
4370 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4371 | _ ->
4372 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4374 let zoom = conf.zoom -. incr in
4375 setzoom zoom;
4376 state.mstate <- Mzoom (n, 0);
4377 else
4378 state.mstate <- Mzoom (n, i+1);
4380 else state.mstate <- Mzoom (n, 0)
4382 | _ -> state.mstate <- Mzoom (n, 0)
4384 else (
4385 match state.autoscroll with
4386 | Some step -> setautoscrollspeed step (n=4)
4387 | None ->
4388 let incr =
4389 if n = 3
4390 then -conf.scrollstep
4391 else conf.scrollstep
4393 let incr = incr * 2 in
4394 let y = clamp incr in
4395 gotoy_and_clear_text y
4398 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4399 if bstate = Glut.DOWN
4400 then (
4401 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4402 state.mstate <- Mpan (x, y)
4404 else
4405 state.mstate <- Mnone
4407 | Glut.RIGHT_BUTTON ->
4408 if bstate = Glut.DOWN
4409 then (
4410 Glut.setCursor Glut.CURSOR_CYCLE;
4411 let p = (x, y) in
4412 state.mstate <- Mzoomrect (p, p)
4414 else (
4415 match state.mstate with
4416 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4417 | _ ->
4418 Glut.setCursor Glut.CURSOR_INHERIT;
4419 state.mstate <- Mnone
4422 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4423 if bstate = Glut.DOWN
4424 then
4425 let _, position, sh = state.uioh#scrollph in
4426 if y > truncate position && y < truncate (position +. sh)
4427 then state.mstate <- Mscrolly
4428 else scrolly y
4429 else
4430 state.mstate <- Mnone
4432 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4433 if bstate = Glut.DOWN
4434 then
4435 let _, position, sw = state.uioh#scrollpw in
4436 if x > truncate position && x < truncate (position +. sw)
4437 then state.mstate <- Mscrollx
4438 else scrollx x
4439 else
4440 state.mstate <- Mnone
4442 | Glut.LEFT_BUTTON ->
4443 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4444 begin match dest with
4445 | Ulinkgoto (pageno, top) ->
4446 if pageno >= 0
4447 then (
4448 addnav ();
4449 gotopage1 pageno top;
4452 | Ulinkuri s ->
4453 gotouri s
4455 | Unone when bstate = Glut.DOWN ->
4456 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4457 state.mstate <- Mpan (x, y);
4459 | Unone | Utext _ ->
4460 if bstate = Glut.DOWN
4461 then (
4462 if conf.angle mod 360 = 0
4463 then (
4464 state.mstate <- Msel ((x, y), (x, y));
4465 G.postRedisplay "mouse select";
4468 else (
4469 match state.mstate with
4470 | Mnone -> ()
4472 | Mzoom _ | Mscrollx | Mscrolly ->
4473 state.mstate <- Mnone
4475 | Mzoomrect ((x0, y0), _) ->
4476 zoomrect x0 y0 x y
4478 | Mpan _ ->
4479 Glut.setCursor Glut.CURSOR_INHERIT;
4480 state.mstate <- Mnone
4482 | Msel ((_, y0), (_, y1)) ->
4483 let f l =
4484 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4485 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4486 then
4487 match getopaque l.pageno with
4488 | Some opaque ->
4489 copysel opaque
4490 | _ -> ()
4492 List.iter f state.layout;
4493 copysel ""; (* ugly *)
4494 Glut.setCursor Glut.CURSOR_INHERIT;
4495 state.mstate <- Mnone;
4499 | _ -> ()
4502 let birdseyemouse button bstate x y
4503 (conf, leftx, _, hooverpageno, anchor) =
4504 match button with
4505 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4506 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4507 let rec loop = function
4508 | [] -> ()
4509 | l :: rest ->
4510 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4511 && x > margin && x < margin + l.pagew
4512 then (
4513 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4515 else loop rest
4517 loop state.layout
4518 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4519 | _ -> ()
4522 let mouse bstate button x y =
4523 state.uioh <- state.uioh#button button bstate x y;
4526 let mouse ~button ~state ~x ~y = mouse state button x y;;
4528 let motion ~x ~y =
4529 state.uioh <- state.uioh#motion x y
4532 let pmotion ~x ~y =
4533 state.uioh <- state.uioh#pmotion x y;
4536 let uioh = object
4537 method display = ()
4539 method key key =
4540 begin match state.mode with
4541 | Textentry textentry -> textentrykeyboard key textentry
4542 | Birdseye birdseye -> birdseyekeyboard key birdseye
4543 | View -> viewkeyboard key
4544 end;
4545 state.uioh
4547 method special key =
4548 begin match state.mode with
4549 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4550 togglebirdseye ()
4552 | Birdseye vals ->
4553 birdseyespecial key vals
4555 | View when key = Glut.KEY_F1 ->
4556 enterhelpmode ()
4558 | View ->
4559 begin match state.autoscroll with
4560 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4561 setautoscrollspeed step (key = Glut.KEY_DOWN)
4563 | _ ->
4564 let y =
4565 match key with
4566 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4567 | Glut.KEY_UP ->
4568 if Glut.getModifiers () land Glut.active_ctrl != 0
4569 then
4570 if Glut.getModifiers () land Glut.active_shift != 0
4571 then (setzoom state.prevzoom; state.y)
4572 else clamp (-conf.winh/2)
4573 else clamp (-conf.scrollstep)
4574 | Glut.KEY_DOWN ->
4575 if Glut.getModifiers () land Glut.active_ctrl != 0
4576 then
4577 if Glut.getModifiers () land Glut.active_shift != 0
4578 then (setzoom state.prevzoom; state.y)
4579 else clamp (conf.winh/2)
4580 else clamp (conf.scrollstep)
4581 | Glut.KEY_PAGE_UP ->
4582 if Glut.getModifiers () land Glut.active_ctrl != 0
4583 then
4584 match state.layout with
4585 | [] -> state.y
4586 | l :: _ -> state.y - l.pagey
4587 else
4588 clamp (-conf.winh)
4589 | Glut.KEY_PAGE_DOWN ->
4590 if Glut.getModifiers () land Glut.active_ctrl != 0
4591 then
4592 match List.rev state.layout with
4593 | [] -> state.y
4594 | l :: _ -> getpagey l.pageno
4595 else
4596 clamp conf.winh
4597 | Glut.KEY_HOME ->
4598 addnav ();
4600 | Glut.KEY_END ->
4601 addnav ();
4602 state.maxy - (if conf.maxhfit then conf.winh else 0)
4604 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4605 Glut.getModifiers () land Glut.active_alt != 0 ->
4606 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4608 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4609 let dx =
4610 if Glut.getModifiers () land Glut.active_ctrl != 0
4611 then (conf.winw / 2)
4612 else 10
4614 state.x <- state.x - dx;
4615 state.y
4616 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4617 let dx =
4618 if Glut.getModifiers () land Glut.active_ctrl != 0
4619 then (conf.winw / 2)
4620 else 10
4622 state.x <- state.x + dx;
4623 state.y
4625 | _ -> state.y
4627 gotoy_and_clear_text y
4630 | Textentry te -> textentryspecial key te
4631 end;
4632 state.uioh
4634 method button button bstate x y =
4635 begin match state.mode with
4636 | View -> viewmouse button bstate x y
4637 | Birdseye beye -> birdseyemouse button bstate x y beye
4638 | Textentry _ -> ()
4639 end;
4640 state.uioh
4642 method motion x y =
4643 begin match state.mode with
4644 | Textentry _ -> ()
4645 | View | Birdseye _ ->
4646 match state.mstate with
4647 | Mzoom _ | Mnone -> ()
4649 | Mpan (x0, y0) ->
4650 let dx = x - x0
4651 and dy = y0 - y in
4652 state.mstate <- Mpan (x, y);
4653 if conf.zoom > 1.0 then state.x <- state.x + dx;
4654 let y = clamp dy in
4655 gotoy_and_clear_text y
4657 | Msel (a, _) ->
4658 state.mstate <- Msel (a, (x, y));
4659 G.postRedisplay "motion select";
4661 | Mscrolly ->
4662 let y = min conf.winh (max 0 y) in
4663 scrolly y
4665 | Mscrollx ->
4666 let x = min conf.winw (max 0 x) in
4667 scrollx x
4669 | Mzoomrect (p0, _) ->
4670 state.mstate <- Mzoomrect (p0, (x, y));
4671 G.postRedisplay "motion zoomrect";
4672 end;
4673 state.uioh
4675 method pmotion x y =
4676 begin match state.mode with
4677 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4678 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4679 let rec loop = function
4680 | [] ->
4681 if hooverpageno != -1
4682 then (
4683 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4684 G.postRedisplay "pmotion birdseye no hoover";
4686 | l :: rest ->
4687 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4688 && x > margin && x < margin + l.pagew
4689 then (
4690 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4691 G.postRedisplay "pmotion birdseye hoover";
4693 else loop rest
4695 loop state.layout
4697 | Textentry _ -> ()
4699 | View ->
4700 match state.mstate with
4701 | Mnone ->
4702 begin match getunder x y with
4703 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
4704 | Ulinkuri uri ->
4705 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
4706 Glut.setCursor Glut.CURSOR_INFO
4707 | Ulinkgoto (page, _) ->
4708 if conf.underinfo
4709 then showtext 'p' ("age: " ^ string_of_int (page+1));
4710 Glut.setCursor Glut.CURSOR_INFO
4711 | Utext s ->
4712 if conf.underinfo then showtext 'f' ("ont: " ^ s);
4713 Glut.setCursor Glut.CURSOR_TEXT
4716 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
4718 end;
4719 state.uioh
4721 method infochanged _ = ()
4723 method scrollph =
4724 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
4725 let p, h = scrollph state.y maxy in
4726 state.scrollw, p, h
4728 method scrollpw =
4729 let winw = conf.winw - state.scrollw - 1 in
4730 let fwinw = float winw in
4731 let sw =
4732 let sw = fwinw /. float state.w in
4733 let sw = fwinw *. sw in
4734 max sw (float conf.scrollh)
4736 let position, sw =
4737 let f = state.w+winw in
4738 let r = float (winw-state.x) /. float f in
4739 let p = fwinw *. r in
4740 p-.sw/.2., sw
4742 let sw =
4743 if position +. sw > fwinw
4744 then fwinw -. position
4745 else sw
4747 state.hscrollh, position, sw
4748 end;;
4750 module Config =
4751 struct
4752 open Parser
4754 let fontpath = ref "";;
4755 let wmclasshack = ref false;;
4757 let unent s =
4758 let l = String.length s in
4759 let b = Buffer.create l in
4760 unent b s 0 l;
4761 Buffer.contents b;
4764 let home =
4766 match platform with
4767 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
4768 | _ -> Sys.getenv "HOME"
4769 with exn ->
4770 prerr_endline
4771 ("Can not determine home directory location: " ^
4772 Printexc.to_string exn);
4776 let config_of c attrs =
4777 let apply c k v =
4779 match k with
4780 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
4781 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
4782 | "case-insensitive-search" -> { c with icase = bool_of_string v }
4783 | "preload" -> { c with preload = bool_of_string v }
4784 | "page-bias" -> { c with pagebias = int_of_string v }
4785 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
4786 | "auto-scroll-step" ->
4787 { c with autoscrollstep = max 0 (int_of_string v) }
4788 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
4789 | "crop-hack" -> { c with crophack = bool_of_string v }
4790 | "throttle" ->
4791 let mw =
4792 match String.lowercase v with
4793 | "true" -> Some infinity
4794 | "false" -> None
4795 | f -> Some (float_of_string f)
4797 { c with maxwait = mw}
4798 | "highlight-links" -> { c with hlinks = bool_of_string v }
4799 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
4800 | "vertical-margin" ->
4801 { c with interpagespace = max 0 (int_of_string v) }
4802 | "zoom" ->
4803 let zoom = float_of_string v /. 100. in
4804 let zoom = max zoom 0.0 in
4805 { c with zoom = zoom }
4806 | "presentation" -> { c with presentation = bool_of_string v }
4807 | "rotation-angle" -> { c with angle = int_of_string v }
4808 | "width" -> { c with winw = max 20 (int_of_string v) }
4809 | "height" -> { c with winh = max 20 (int_of_string v) }
4810 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
4811 | "proportional-display" -> { c with proportional = bool_of_string v }
4812 | "pixmap-cache-size" ->
4813 { c with memlimit = max 2 (int_of_string_with_suffix v) }
4814 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
4815 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
4816 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
4817 | "persistent-location" -> { c with jumpback = bool_of_string v }
4818 | "background-color" -> { c with bgcolor = color_of_string v }
4819 | "scrollbar-in-presentation" ->
4820 { c with scrollbarinpm = bool_of_string v }
4821 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
4822 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
4823 | "mupdf-memlimit" ->
4824 { c with mumemlimit = max 1024 (int_of_string_with_suffix v) }
4825 | "checkers" -> { c with checkers = bool_of_string v }
4826 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
4827 | "trim-margins" -> { c with trimmargins = bool_of_string v }
4828 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
4829 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
4830 | "uri-launcher" -> { c with urilauncher = unent v }
4831 | "color-space" -> { c with colorspace = colorspace_of_string v }
4832 | "invert-colors" -> { c with invert = bool_of_string v }
4833 | "brightness" -> { c with colorscale = float_of_string v }
4834 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
4835 | _ -> c
4836 with exn ->
4837 prerr_endline ("Error processing attribute (`" ^
4838 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
4841 let rec fold c = function
4842 | [] -> c
4843 | (k, v) :: rest ->
4844 let c = apply c k v in
4845 fold c rest
4847 fold c attrs;
4850 let fromstring f pos n v d =
4851 try f v
4852 with exn ->
4853 dolog "Error processing attribute (%S=%S) at %d\n%s"
4854 n v pos (Printexc.to_string exn)
4859 let bookmark_of attrs =
4860 let rec fold title page rely = function
4861 | ("title", v) :: rest -> fold v page rely rest
4862 | ("page", v) :: rest -> fold title v rely rest
4863 | ("rely", v) :: rest -> fold title page v rest
4864 | _ :: rest -> fold title page rely rest
4865 | [] -> title, page, rely
4867 fold "invalid" "0" "0" attrs
4870 let doc_of attrs =
4871 let rec fold path page rely pan = function
4872 | ("path", v) :: rest -> fold v page rely pan rest
4873 | ("page", v) :: rest -> fold path v rely pan rest
4874 | ("rely", v) :: rest -> fold path page v pan rest
4875 | ("pan", v) :: rest -> fold path page rely v rest
4876 | _ :: rest -> fold path page rely pan rest
4877 | [] -> path, page, rely, pan
4879 fold "" "0" "0" "0" attrs
4882 let setconf dst src =
4883 dst.scrollbw <- src.scrollbw;
4884 dst.scrollh <- src.scrollh;
4885 dst.icase <- src.icase;
4886 dst.preload <- src.preload;
4887 dst.pagebias <- src.pagebias;
4888 dst.verbose <- src.verbose;
4889 dst.scrollstep <- src.scrollstep;
4890 dst.maxhfit <- src.maxhfit;
4891 dst.crophack <- src.crophack;
4892 dst.autoscrollstep <- src.autoscrollstep;
4893 dst.maxwait <- src.maxwait;
4894 dst.hlinks <- src.hlinks;
4895 dst.underinfo <- src.underinfo;
4896 dst.interpagespace <- src.interpagespace;
4897 dst.zoom <- src.zoom;
4898 dst.presentation <- src.presentation;
4899 dst.angle <- src.angle;
4900 dst.winw <- src.winw;
4901 dst.winh <- src.winh;
4902 dst.savebmarks <- src.savebmarks;
4903 dst.memlimit <- src.memlimit;
4904 dst.proportional <- src.proportional;
4905 dst.texcount <- src.texcount;
4906 dst.sliceheight <- src.sliceheight;
4907 dst.thumbw <- src.thumbw;
4908 dst.jumpback <- src.jumpback;
4909 dst.bgcolor <- src.bgcolor;
4910 dst.scrollbarinpm <- src.scrollbarinpm;
4911 dst.tilew <- src.tilew;
4912 dst.tileh <- src.tileh;
4913 dst.mumemlimit <- src.mumemlimit;
4914 dst.checkers <- src.checkers;
4915 dst.aalevel <- src.aalevel;
4916 dst.trimmargins <- src.trimmargins;
4917 dst.trimfuzz <- src.trimfuzz;
4918 dst.urilauncher <- src.urilauncher;
4919 dst.colorspace <- src.colorspace;
4920 dst.invert <- src.invert;
4921 dst.colorscale <- src.colorscale;
4922 dst.redirectstderr <- src.redirectstderr;
4925 let get s =
4926 let h = Hashtbl.create 10 in
4927 let dc = { defconf with angle = defconf.angle } in
4928 let rec toplevel v t spos _ =
4929 match t with
4930 | Vdata | Vcdata | Vend -> v
4931 | Vopen ("llppconfig", _, closed) ->
4932 if closed
4933 then v
4934 else { v with f = llppconfig }
4935 | Vopen _ ->
4936 error "unexpected subelement at top level" s spos
4937 | Vclose _ -> error "unexpected close at top level" s spos
4939 and llppconfig v t spos _ =
4940 match t with
4941 | Vdata | Vcdata -> v
4942 | Vend -> error "unexpected end of input in llppconfig" s spos
4943 | Vopen ("defaults", attrs, closed) ->
4944 let c = config_of dc attrs in
4945 setconf dc c;
4946 if closed
4947 then v
4948 else { v with f = skip "defaults" (fun () -> v) }
4950 | Vopen ("ui-font", attrs, closed) ->
4951 let rec getsize size = function
4952 | [] -> size
4953 | ("size", v) :: rest ->
4954 let size =
4955 fromstring int_of_string spos "size" v fstate.fontsize in
4956 getsize size rest
4957 | l -> getsize size l
4959 fstate.fontsize <- getsize fstate.fontsize attrs;
4960 if closed
4961 then v
4962 else { v with f = uifont (Buffer.create 10) }
4964 | Vopen ("doc", attrs, closed) ->
4965 let pathent, spage, srely, span = doc_of attrs in
4966 let path = unent pathent
4967 and pageno = fromstring int_of_string spos "page" spage 0
4968 and rely = fromstring float_of_string spos "rely" srely 0.0
4969 and pan = fromstring int_of_string spos "pan" span 0 in
4970 let c = config_of dc attrs in
4971 let anchor = (pageno, rely) in
4972 if closed
4973 then (Hashtbl.add h path (c, [], pan, anchor); v)
4974 else { v with f = doc path pan anchor c [] }
4976 | Vopen _ ->
4977 error "unexpected subelement in llppconfig" s spos
4979 | Vclose "llppconfig" -> { v with f = toplevel }
4980 | Vclose _ -> error "unexpected close in llppconfig" s spos
4982 and uifont b v t spos epos =
4983 match t with
4984 | Vdata | Vcdata ->
4985 Buffer.add_substring b s spos (epos - spos);
4987 | Vopen (_, _, _) ->
4988 error "unexpected subelement in ui-font" s spos
4989 | Vclose "ui-font" ->
4990 if String.length !fontpath = 0
4991 then fontpath := Buffer.contents b;
4992 { v with f = llppconfig }
4993 | Vclose _ -> error "unexpected close in ui-font" s spos
4994 | Vend -> error "unexpected end of input in ui-font" s spos
4996 and doc path pan anchor c bookmarks v t spos _ =
4997 match t with
4998 | Vdata | Vcdata -> v
4999 | Vend -> error "unexpected end of input in doc" s spos
5000 | Vopen ("bookmarks", _, closed) ->
5001 if closed
5002 then v
5003 else { v with f = pbookmarks path pan anchor c bookmarks }
5005 | Vopen (_, _, _) ->
5006 error "unexpected subelement in doc" s spos
5008 | Vclose "doc" ->
5009 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5010 { v with f = llppconfig }
5012 | Vclose _ -> error "unexpected close in doc" s spos
5014 and pbookmarks path pan anchor c bookmarks v t spos _ =
5015 match t with
5016 | Vdata | Vcdata -> v
5017 | Vend -> error "unexpected end of input in bookmarks" s spos
5018 | Vopen ("item", attrs, closed) ->
5019 let titleent, spage, srely = bookmark_of attrs in
5020 let page = fromstring int_of_string spos "page" spage 0
5021 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5022 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5023 if closed
5024 then { v with f = pbookmarks path pan anchor c bookmarks }
5025 else
5026 let f () = v in
5027 { v with f = skip "item" f }
5029 | Vopen _ ->
5030 error "unexpected subelement in bookmarks" s spos
5032 | Vclose "bookmarks" ->
5033 { v with f = doc path pan anchor c bookmarks }
5035 | Vclose _ -> error "unexpected close in bookmarks" s spos
5037 and skip tag f v t spos _ =
5038 match t with
5039 | Vdata | Vcdata -> v
5040 | Vend ->
5041 error ("unexpected end of input in skipped " ^ tag) s spos
5042 | Vopen (tag', _, closed) ->
5043 if closed
5044 then v
5045 else
5046 let f' () = { v with f = skip tag f } in
5047 { v with f = skip tag' f' }
5048 | Vclose ctag ->
5049 if tag = ctag
5050 then f ()
5051 else error ("unexpected close in skipped " ^ tag) s spos
5054 parse { f = toplevel; accu = () } s;
5055 h, dc;
5058 let do_load f ic =
5060 let len = in_channel_length ic in
5061 let s = String.create len in
5062 really_input ic s 0 len;
5063 f s;
5064 with
5065 | Parse_error (msg, s, pos) ->
5066 let subs = subs s pos in
5067 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5068 failwith ("parse error: " ^ s)
5070 | exn ->
5071 failwith ("config load error: " ^ Printexc.to_string exn)
5074 let defconfpath =
5075 let dir =
5077 let dir = Filename.concat home ".config" in
5078 if Sys.is_directory dir then dir else home
5079 with _ -> home
5081 Filename.concat dir "llpp.conf"
5084 let confpath = ref defconfpath;;
5086 let load1 f =
5087 if Sys.file_exists !confpath
5088 then
5089 match
5090 (try Some (open_in_bin !confpath)
5091 with exn ->
5092 prerr_endline
5093 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5094 Printexc.to_string exn);
5095 None
5097 with
5098 | Some ic ->
5099 begin try
5100 f (do_load get ic)
5101 with exn ->
5102 prerr_endline
5103 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5104 Printexc.to_string exn);
5105 end;
5106 close_in ic;
5108 | None -> ()
5109 else
5110 f (Hashtbl.create 0, defconf)
5113 let load () =
5114 let f (h, dc) =
5115 let pc, pb, px, pa =
5117 Hashtbl.find h (Filename.basename state.path)
5118 with Not_found -> dc, [], 0, (0, 0.0)
5120 setconf defconf dc;
5121 setconf conf pc;
5122 state.bookmarks <- pb;
5123 state.x <- px;
5124 state.scrollw <- conf.scrollbw;
5125 if conf.jumpback
5126 then state.anchor <- pa;
5127 cbput state.hists.nav pa;
5129 load1 f
5132 let add_attrs bb always dc c =
5133 let ob s a b =
5134 if always || a != b
5135 then Printf.bprintf bb "\n %s='%b'" s a
5136 and oi s a b =
5137 if always || a != b
5138 then Printf.bprintf bb "\n %s='%d'" s a
5139 and oI s a b =
5140 if always || a != b
5141 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5142 and oz s a b =
5143 if always || a <> b
5144 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5145 and oF s a b =
5146 if always || a <> b
5147 then Printf.bprintf bb "\n %s='%f'" s a
5148 and oc s a b =
5149 if always || a <> b
5150 then
5151 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5152 and oC s a b =
5153 if always || a <> b
5154 then
5155 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5156 and oR s a b =
5157 if always || a <> b
5158 then
5159 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5160 and os s a b =
5161 if always || a <> b
5162 then
5163 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5164 and oW s a b =
5165 if always || a <> b
5166 then
5167 let v =
5168 match a with
5169 | None -> "false"
5170 | Some f ->
5171 if f = infinity
5172 then "true"
5173 else string_of_float f
5175 Printf.bprintf bb "\n %s='%s'" s v
5177 let w, h =
5178 if always
5179 then dc.winw, dc.winh
5180 else
5181 match state.fullscreen with
5182 | Some wh -> wh
5183 | None -> c.winw, c.winh
5185 let zoom, presentation, interpagespace, maxwait =
5186 if always
5187 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5188 else
5189 match state.mode with
5190 | Birdseye (bc, _, _, _, _) ->
5191 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5192 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5194 oi "width" w dc.winw;
5195 oi "height" h dc.winh;
5196 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5197 oi "scroll-handle-height" c.scrollh dc.scrollh;
5198 ob "case-insensitive-search" c.icase dc.icase;
5199 ob "preload" c.preload dc.preload;
5200 oi "page-bias" c.pagebias dc.pagebias;
5201 oi "scroll-step" c.scrollstep dc.scrollstep;
5202 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5203 ob "max-height-fit" c.maxhfit dc.maxhfit;
5204 ob "crop-hack" c.crophack dc.crophack;
5205 oW "throttle" maxwait dc.maxwait;
5206 ob "highlight-links" c.hlinks dc.hlinks;
5207 ob "under-cursor-info" c.underinfo dc.underinfo;
5208 oi "vertical-margin" interpagespace dc.interpagespace;
5209 oz "zoom" zoom dc.zoom;
5210 ob "presentation" presentation dc.presentation;
5211 oi "rotation-angle" c.angle dc.angle;
5212 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5213 ob "proportional-display" c.proportional dc.proportional;
5214 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5215 oi "tex-count" c.texcount dc.texcount;
5216 oi "slice-height" c.sliceheight dc.sliceheight;
5217 oi "thumbnail-width" c.thumbw dc.thumbw;
5218 ob "persistent-location" c.jumpback dc.jumpback;
5219 oc "background-color" c.bgcolor dc.bgcolor;
5220 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5221 oi "tile-width" c.tilew dc.tilew;
5222 oi "tile-height" c.tileh dc.tileh;
5223 oI "mupdf-memlimit" c.mumemlimit dc.mumemlimit;
5224 ob "checkers" c.checkers dc.checkers;
5225 oi "aalevel" c.aalevel dc.aalevel;
5226 ob "trim-margins" c.trimmargins dc.trimmargins;
5227 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5228 os "uri-launcher" c.urilauncher dc.urilauncher;
5229 oC "color-space" c.colorspace dc.colorspace;
5230 ob "invert-colors" c.invert dc.invert;
5231 oF "brightness" c.colorscale dc.colorscale;
5232 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5233 if always
5234 then ob "wmclass-hack" !wmclasshack false;
5237 let save () =
5238 let uifontsize = fstate.fontsize in
5239 let bb = Buffer.create 32768 in
5240 let f (h, dc) =
5241 let dc = if conf.bedefault then conf else dc in
5242 Buffer.add_string bb "<llppconfig>\n";
5244 if String.length !fontpath > 0
5245 then
5246 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5247 uifontsize
5248 !fontpath
5249 else (
5250 if uifontsize <> 14
5251 then
5252 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5255 Buffer.add_string bb "<defaults ";
5256 add_attrs bb true dc dc;
5257 Buffer.add_string bb "/>\n";
5259 let adddoc path pan anchor c bookmarks =
5260 if bookmarks == [] && c = dc && anchor = emptyanchor
5261 then ()
5262 else (
5263 Printf.bprintf bb "<doc path='%s'"
5264 (enent path 0 (String.length path));
5266 if anchor <> emptyanchor
5267 then (
5268 let n, y = anchor in
5269 Printf.bprintf bb " page='%d'" n;
5270 if y > 1e-6
5271 then
5272 Printf.bprintf bb " rely='%f'" y
5276 if pan != 0
5277 then Printf.bprintf bb " pan='%d'" pan;
5279 add_attrs bb false dc c;
5281 begin match bookmarks with
5282 | [] -> Buffer.add_string bb "/>\n"
5283 | _ ->
5284 Buffer.add_string bb ">\n<bookmarks>\n";
5285 List.iter (fun (title, _level, (page, rely)) ->
5286 Printf.bprintf bb
5287 "<item title='%s' page='%d'"
5288 (enent title 0 (String.length title))
5289 page
5291 if rely > 1e-6
5292 then
5293 Printf.bprintf bb " rely='%f'" rely
5295 Buffer.add_string bb "/>\n";
5296 ) bookmarks;
5297 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5298 end;
5302 let pan =
5303 match state.mode with
5304 | Birdseye (_, pan, _, _, _) -> pan
5305 | _ -> state.x
5307 let basename = Filename.basename state.path in
5308 adddoc basename pan (getanchor ())
5309 { conf with
5310 autoscrollstep =
5311 match state.autoscroll with
5312 | Some step -> step
5313 | None -> conf.autoscrollstep }
5314 (if conf.savebmarks then state.bookmarks else []);
5316 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5317 if basename <> path
5318 then adddoc path x y c bookmarks
5319 ) h;
5320 Buffer.add_string bb "</llppconfig>";
5322 load1 f;
5323 if Buffer.length bb > 0
5324 then
5326 let tmp = !confpath ^ ".tmp" in
5327 let oc = open_out_bin tmp in
5328 Buffer.output_buffer oc bb;
5329 close_out oc;
5330 Unix.rename tmp !confpath;
5331 with exn ->
5332 prerr_endline
5333 ("error while saving configuration: " ^ Printexc.to_string exn)
5335 end;;
5337 let () =
5338 Arg.parse
5339 (Arg.align
5340 [("-p", Arg.String (fun s -> state.password <- s) ,
5341 "<password> Set password");
5343 ("-f", Arg.String (fun s -> Config.fontpath := s),
5344 "<path> Set path to the user interface font");
5346 ("-c", Arg.String (fun s -> Config.confpath := s),
5347 "<path> Set path to the configuration file");
5349 ("-v", Arg.Unit (fun () ->
5350 Printf.printf
5351 "%s\nconfiguration path: %s\n"
5352 (version ())
5353 Config.defconfpath
5355 exit 0), " Print version and exit");
5358 (fun s -> state.path <- s)
5359 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5361 if String.length state.path = 0
5362 then (prerr_endline "file name missing"; exit 1);
5364 Config.load ();
5366 let _ = Glut.init Sys.argv in
5367 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5368 let () = Glut.initWindowSize conf.winw conf.winh in
5369 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5371 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5372 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5373 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5375 let csock, ssock =
5376 if not is_windows
5377 then
5378 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5379 else
5380 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5381 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5382 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5383 Unix.bind sock addr;
5384 Unix.listen sock 1;
5385 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5386 Unix.connect csock addr;
5387 let ssock, _ = Unix.accept sock in
5388 Unix.close sock;
5389 let opts sock =
5390 Unix.setsockopt sock Unix.TCP_NODELAY true;
5391 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5393 opts ssock;
5394 opts csock;
5395 ssock, csock
5398 let () = Glut.displayFunc display in
5399 let () = Glut.reshapeFunc reshape in
5400 let () = Glut.keyboardFunc keyboard in
5401 let () = Glut.specialFunc special in
5402 let () = Glut.idleFunc (Some idle) in
5403 let () = Glut.mouseFunc mouse in
5404 let () = Glut.motionFunc motion in
5405 let () = Glut.passiveMotionFunc pmotion in
5407 setcheckers conf.checkers;
5408 init ssock (
5409 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5410 conf.texcount, conf.sliceheight, conf.mumemlimit, conf.colorspace,
5411 !Config.wmclasshack, !Config.fontpath
5413 state.csock <- csock;
5414 state.ssock <- ssock;
5415 state.text <- "Opening " ^ state.path;
5416 setaalevel conf.aalevel;
5417 writeopen state.path state.password;
5418 state.uioh <- uioh;
5419 setfontsize fstate.fontsize;
5421 redirectstderr ();
5423 while true do
5425 Glut.mainLoop ();
5426 with
5427 | Glut.BadEnum "key in special_of_int" ->
5428 showtext '!' " LablGlut bug: special key not recognized";
5430 | Quit ->
5431 wcmd "quit" [];
5432 Config.save ();
5433 exit 0
5435 | exn when conf.redirectstderr ->
5436 let s =
5437 Printf.sprintf "exception %s\n%s"
5438 (Printexc.to_string exn)
5439 (Printexc.get_backtrace ())
5441 ignore (try
5442 Unix.single_write state.stderr s 0 (String.length s);
5443 with _ -> 0);
5444 exit 1
5445 done;