Usability tweak
[llpp.git] / main.ml
blobfdd31cdff587c84e9f4c4d2fd44168f148882efa
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";;
57 external realloctexts : int -> bool = "ml_realloctexts";;
59 let platform_to_string = function
60 | Punknown -> "unknown"
61 | Plinux -> "Linux"
62 | Pwindows -> "Windows"
63 | Posx -> "OSX"
64 | Psun -> "Sun"
65 | Pfreebsd -> "FreeBSD"
66 | Pdragonflybsd -> "DragonflyBSD"
67 | Popenbsd -> "OpenBSD"
68 | Pcygwin -> "Cygwin"
69 | Pmingw -> "MingW"
72 let platform = platform ();;
74 let is_windows =
75 match platform with
76 | Pwindows | Pmingw -> true
77 | _ -> false
80 type x = int
81 and y = int
82 and tilex = int
83 and tiley = int
84 and tileparams = (x * y * width * height * tilex * tiley)
87 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
89 type mpos = int * int
90 and mstate =
91 | Msel of (mpos * mpos)
92 | Mpan of mpos
93 | Mscrolly | Mscrollx
94 | Mzoom of (int * int)
95 | Mzoomrect of (mpos * mpos)
96 | Mnone
99 type textentry = string * string * onhist option * onkey * ondone
100 and onkey = string -> int -> te
101 and ondone = string -> unit
102 and histcancel = unit -> unit
103 and onhist = ((histcmd -> string) * histcancel)
104 and histcmd = HCnext | HCprev | HCfirst | HClast
105 and te =
106 | TEstop
107 | TEdone of string
108 | TEcont of string
109 | TEswitch of textentry
112 type 'a circbuf =
113 { store : 'a array
114 ; mutable rc : int
115 ; mutable wc : int
116 ; mutable len : int
120 let bound v minv maxv =
121 max minv (min maxv v);
124 let cbnew n v =
125 { store = Array.create n v
126 ; rc = 0
127 ; wc = 0
128 ; len = 0
132 let drawstring size x y s =
133 Gl.enable `blend;
134 Gl.enable `texture_2d;
135 ignore (drawstr size x y s);
136 Gl.disable `blend;
137 Gl.disable `texture_2d;
140 let drawstring1 size x y s =
141 drawstr size x y s;
144 let drawstring2 size x y fmt =
145 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
148 let cbcap b = Array.length b.store;;
150 let cbput b v =
151 let cap = cbcap b in
152 b.store.(b.wc) <- v;
153 b.wc <- (b.wc + 1) mod cap;
154 b.rc <- b.wc;
155 b.len <- min (b.len + 1) cap;
158 let cbempty b = b.len = 0;;
160 let cbgetg b circular dir =
161 if cbempty b
162 then b.store.(0)
163 else
164 let rc = b.rc + dir in
165 let rc =
166 if circular
167 then (
168 if rc = -1
169 then b.len-1
170 else (
171 if rc = b.len
172 then 0
173 else rc
176 else max 0 (min rc (b.len-1))
178 b.rc <- rc;
179 b.store.(rc);
182 let cbget b = cbgetg b false;;
183 let cbgetc b = cbgetg b true;;
185 type page =
186 { pageno : int
187 ; pagedimno : int
188 ; pagew : int
189 ; pageh : int
190 ; pagex : int
191 ; pagey : int
192 ; pagevw : int
193 ; pagevh : int
194 ; pagedispx : int
195 ; pagedispy : int
199 let debugl l =
200 dolog "l %d dim=%d {" l.pageno l.pagedimno;
201 dolog " WxH %dx%d" l.pagew l.pageh;
202 dolog " vWxH %dx%d" l.pagevw l.pagevh;
203 dolog " pagex,y %d,%d" l.pagex l.pagey;
204 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
205 dolog "}";
208 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
209 dolog "rect {";
210 dolog " x0,y0=(% f, % f)" x0 y0;
211 dolog " x1,y1=(% f, % f)" x1 y1;
212 dolog " x2,y2=(% f, % f)" x2 y2;
213 dolog " x3,y3=(% f, % f)" x3 y3;
214 dolog "}";
217 type conf =
218 { mutable scrollbw : int
219 ; mutable scrollh : int
220 ; mutable icase : bool
221 ; mutable preload : bool
222 ; mutable pagebias : int
223 ; mutable verbose : bool
224 ; mutable debug : bool
225 ; mutable scrollstep : int
226 ; mutable maxhfit : bool
227 ; mutable crophack : bool
228 ; mutable autoscrollstep : int
229 ; mutable maxwait : float option
230 ; mutable hlinks : bool
231 ; mutable underinfo : bool
232 ; mutable interpagespace : interpagespace
233 ; mutable zoom : float
234 ; mutable presentation : bool
235 ; mutable angle : angle
236 ; mutable winw : int
237 ; mutable winh : int
238 ; mutable savebmarks : bool
239 ; mutable proportional : proportional
240 ; mutable trimmargins : trimmargins
241 ; mutable trimfuzz : irect
242 ; mutable memlimit : memsize
243 ; mutable texcount : texcount
244 ; mutable sliceheight : sliceheight
245 ; mutable thumbw : width
246 ; mutable jumpback : bool
247 ; mutable bgcolor : float * float * float
248 ; mutable bedefault : bool
249 ; mutable scrollbarinpm : bool
250 ; mutable tilew : int
251 ; mutable tileh : int
252 ; mutable mumemlimit : memsize
253 ; mutable checkers : bool
254 ; mutable aalevel : int
255 ; mutable urilauncher : string
256 ; mutable colorspace : colorspace
257 ; mutable invert : bool
258 ; mutable colorscale : float
259 ; mutable redirectstderr : bool
263 type anchor = pageno * top;;
265 type outline = string * int * anchor;;
267 type rect = float * float * float * float * float * float * float * float;;
269 type tile = opaque * pixmapsize * elapsed
270 and elapsed = float;;
271 type pagemapkey = pageno * gen;;
272 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
273 and row = int
274 and col = int;;
276 let emptyanchor = (0, 0.0);;
278 type infochange = | Memused | Docinfo | Pdim;;
280 class type uioh = object
281 method display : unit
282 method key : int -> uioh
283 method special : Glut.special_key_t -> uioh
284 method button :
285 Glut.button_t -> Glut.mouse_button_state_t -> int -> int -> uioh
286 method motion : int -> int -> uioh
287 method pmotion : int -> int -> uioh
288 method infochanged : infochange -> unit
289 method scrollpw : (int * float * float)
290 method scrollph : (int * float * float)
291 end;;
293 type mode =
294 | Birdseye of (conf * leftx * pageno * pageno * anchor)
295 | Textentry of (textentry * onleave)
296 | View
297 and onleave = leavetextentrystatus -> unit
298 and leavetextentrystatus = | Cancel | Confirm
299 and helpitem = string * int * action
300 and action =
301 | Noaction
302 | Action of (uioh -> uioh)
305 let isbirdseye = function Birdseye _ -> true | _ -> false;;
306 let istextentry = function Textentry _ -> true | _ -> false;;
308 type currently =
309 | Idle
310 | Loading of (page * gen)
311 | Tiling of (
312 page * opaque * colorspace * angle * gen * col * row * width * height
314 | Outlining of outline list
317 let nouioh : uioh = object (self)
318 method display = ()
319 method key _ = self
320 method special _ = self
321 method button _ _ _ _ = self
322 method motion _ _ = self
323 method pmotion _ _ = self
324 method infochanged _ = ()
325 method scrollpw = (0, nan, nan)
326 method scrollph = (0, nan, nan)
327 end;;
329 type state =
330 { mutable csock : Unix.file_descr
331 ; mutable ssock : Unix.file_descr
332 ; mutable errfd : Unix.file_descr option
333 ; mutable stderr : Unix.file_descr
334 ; mutable errmsgs : Buffer.t
335 ; mutable newerrmsgs : bool
336 ; mutable w : int
337 ; mutable x : int
338 ; mutable y : int
339 ; mutable scrollw : int
340 ; mutable hscrollh : int
341 ; mutable anchor : anchor
342 ; mutable maxy : int
343 ; mutable layout : page list
344 ; pagemap : (pagemapkey, opaque) Hashtbl.t
345 ; tilemap : (tilemapkey, tile) Hashtbl.t
346 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
347 ; mutable pdims : (pageno * width * height * leftx) list
348 ; mutable pagecount : int
349 ; mutable currently : currently
350 ; mutable mstate : mstate
351 ; mutable searchpattern : string
352 ; mutable rects : (pageno * recttype * rect) list
353 ; mutable rects1 : (pageno * recttype * rect) list
354 ; mutable text : string
355 ; mutable fullscreen : (width * height) option
356 ; mutable mode : mode
357 ; mutable uioh : uioh
358 ; mutable outlines : outline array
359 ; mutable bookmarks : outline list
360 ; mutable path : string
361 ; mutable password : string
362 ; mutable invalidated : int
363 ; mutable memused : memsize
364 ; mutable gen : gen
365 ; mutable throttle : (page list * int * float) option
366 ; mutable autoscroll : int option
367 ; mutable help : helpitem array
368 ; mutable docinfo : (int * string) list
369 ; mutable deadline : float
370 ; mutable texid : GlTex.texture_id option
371 ; hists : hists
372 ; mutable prevzoom : float
373 ; mutable progress : float
375 and hists =
376 { pat : string circbuf
377 ; pag : string circbuf
378 ; nav : anchor circbuf
382 let defconf =
383 { scrollbw = 7
384 ; scrollh = 12
385 ; icase = true
386 ; preload = true
387 ; pagebias = 0
388 ; verbose = false
389 ; debug = false
390 ; scrollstep = 24
391 ; maxhfit = true
392 ; crophack = false
393 ; autoscrollstep = 2
394 ; maxwait = None
395 ; hlinks = false
396 ; underinfo = false
397 ; interpagespace = 2
398 ; zoom = 1.0
399 ; presentation = false
400 ; angle = 0
401 ; winw = 900
402 ; winh = 900
403 ; savebmarks = true
404 ; proportional = true
405 ; trimmargins = false
406 ; trimfuzz = (0,0,0,0)
407 ; memlimit = 32 lsl 20
408 ; texcount = 256
409 ; sliceheight = 24
410 ; thumbw = 76
411 ; jumpback = true
412 ; bgcolor = (0.5, 0.5, 0.5)
413 ; bedefault = false
414 ; scrollbarinpm = true
415 ; tilew = 2048
416 ; tileh = 2048
417 ; mumemlimit = 128 lsl 20
418 ; checkers = true
419 ; aalevel = 8
420 ; urilauncher =
421 (match platform with
422 | Plinux | Pfreebsd | Pdragonflybsd | Popenbsd | Psun -> "xdg-open \"%s\""
423 | Posx -> "open \"%s\""
424 | Pwindows | Pcygwin | Pmingw -> "iexplore \"%s\""
425 | _ -> "")
426 ; colorspace = Rgb
427 ; invert = false
428 ; colorscale = 1.0
429 ; redirectstderr = false
433 let conf = { defconf with angle = defconf.angle };;
435 type fontstate =
436 { mutable fontsize : int
437 ; mutable wwidth : float
438 ; mutable maxrows : int
442 let fstate =
443 { fontsize = 14
444 ; wwidth = nan
445 ; maxrows = -1
449 let setfontsize n =
450 fstate.fontsize <- n;
451 fstate.wwidth <- measurestr fstate.fontsize "w";
452 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
455 let gotouri uri =
456 if String.length conf.urilauncher = 0
457 then print_endline uri
458 else
459 let re = Str.regexp "%s" in
460 let command = Str.global_replace re uri conf.urilauncher in
461 let optic =
462 try Some (Unix.open_process_in command)
463 with exn ->
464 Printf.eprintf
465 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
466 flush stderr;
467 None
469 match optic with
470 | Some ic -> close_in ic
471 | None -> ()
474 let version () =
475 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
476 (platform_to_string platform) Sys.word_size Sys.ocaml_version
479 let makehelp () =
480 let strings = version () :: "" :: Help.keys in
481 Array.of_list (
482 let r = Str.regexp "\\(http://[^ ]+\\)" in
483 List.map (fun s ->
484 if (try Str.search_forward r s 0 with Not_found -> -1) >= 0
485 then
486 let uri = Str.matched_string s in
487 (s, 0, Action (fun u -> gotouri uri; u))
488 else s, 0, Noaction) strings
492 let state =
493 { csock = Unix.stdin
494 ; ssock = Unix.stdin
495 ; errfd = None
496 ; stderr = Unix.stderr
497 ; errmsgs = Buffer.create 0
498 ; newerrmsgs = false
499 ; x = 0
500 ; y = 0
501 ; w = 0
502 ; scrollw = 0
503 ; hscrollh = 0
504 ; anchor = emptyanchor
505 ; layout = []
506 ; maxy = max_int
507 ; tilelru = Queue.create ()
508 ; pagemap = Hashtbl.create 10
509 ; tilemap = Hashtbl.create 10
510 ; pdims = []
511 ; pagecount = 0
512 ; currently = Idle
513 ; mstate = Mnone
514 ; rects = []
515 ; rects1 = []
516 ; text = ""
517 ; mode = View
518 ; fullscreen = None
519 ; searchpattern = ""
520 ; outlines = [||]
521 ; bookmarks = []
522 ; path = ""
523 ; password = ""
524 ; invalidated = 0
525 ; hists =
526 { nav = cbnew 10 (0, 0.0)
527 ; pat = cbnew 1 ""
528 ; pag = cbnew 1 ""
530 ; memused = 0
531 ; gen = 0
532 ; throttle = None
533 ; autoscroll = None
534 ; help = makehelp ()
535 ; docinfo = []
536 ; deadline = nan
537 ; texid = None
538 ; prevzoom = 1.0
539 ; progress = -1.0
540 ; uioh = nouioh
544 let vlog fmt =
545 if conf.verbose
546 then
547 Printf.kprintf prerr_endline fmt
548 else
549 Printf.kprintf ignore fmt
552 let redirectstderr () =
553 if conf.redirectstderr
554 then
555 let rfd, wfd = Unix.pipe () in
556 state.stderr <- Unix.dup Unix.stderr;
557 state.errfd <- Some rfd;
558 Unix.dup2 wfd Unix.stderr;
559 else (
560 state.newerrmsgs <- false;
561 begin match state.errfd with
562 | Some fd ->
563 Unix.close fd;
564 Unix.dup2 state.stderr Unix.stderr;
565 state.errfd <- None;
566 | None -> ()
567 end;
568 prerr_string (Buffer.contents state.errmsgs);
569 flush stderr;
570 Buffer.clear state.errmsgs;
574 module G =
575 struct
576 let postRedisplay who =
577 if conf.verbose
578 then prerr_endline ("redisplay for " ^ who);
579 Glut.postRedisplay ();
581 end;;
583 let addchar s c =
584 let b = Buffer.create (String.length s + 1) in
585 Buffer.add_string b s;
586 Buffer.add_char b c;
587 Buffer.contents b;
590 let colorspace_of_string s =
591 match String.lowercase s with
592 | "rgb" -> Rgb
593 | "bgr" -> Bgr
594 | "gray" -> Gray
595 | _ -> failwith "invalid colorspace"
598 let int_of_colorspace = function
599 | Rgb -> 0
600 | Bgr -> 1
601 | Gray -> 2
604 let colorspace_of_int = function
605 | 0 -> Rgb
606 | 1 -> Bgr
607 | 2 -> Gray
608 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
611 let colorspace_to_string = function
612 | Rgb -> "rgb"
613 | Bgr -> "bgr"
614 | Gray -> "gray"
617 let intentry_with_suffix text key =
618 let c = Char.unsafe_chr key in
619 match Char.lowercase c with
620 | '0' .. '9' ->
621 let text = addchar text c in
622 TEcont text
624 | 'k' | 'm' | 'g' ->
625 let text = addchar text c in
626 TEcont text
628 | _ ->
629 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
630 TEcont text
633 let writecmd fd s =
634 let len = String.length s in
635 let n = 4 + len in
636 let b = Buffer.create n in
637 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
638 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
639 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
640 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
641 Buffer.add_string b s;
642 let s' = Buffer.contents b in
643 let n' = Unix.write fd s' 0 n in
644 if n' != n then failwith "write failed";
647 let readcmd fd =
648 let s = "xxxx" in
649 let n = Unix.read fd s 0 4 in
650 if n != 4 then failwith "incomplete read(len)";
651 let len = 0
652 lor (Char.code s.[0] lsl 24)
653 lor (Char.code s.[1] lsl 16)
654 lor (Char.code s.[2] lsl 8)
655 lor (Char.code s.[3] lsl 0)
657 let s = String.create len in
658 let n = Unix.read fd s 0 len in
659 if n != len then failwith "incomplete read(data)";
663 let makecmd s l =
664 let b = Buffer.create 10 in
665 Buffer.add_string b s;
666 let rec combine = function
667 | [] -> b
668 | x :: xs ->
669 Buffer.add_char b ' ';
670 let s =
671 match x with
672 | `b b -> if b then "1" else "0"
673 | `s s -> s
674 | `i i -> string_of_int i
675 | `f f -> string_of_float f
676 | `I f -> string_of_int (truncate f)
678 Buffer.add_string b s;
679 combine xs;
681 combine l;
684 let wcmd s l =
685 let cmd = Buffer.contents (makecmd s l) in
686 writecmd state.csock cmd;
689 let calcips h =
690 if conf.presentation
691 then
692 let d = conf.winh - h in
693 max 0 ((d + 1) / 2)
694 else
695 conf.interpagespace
698 let calcheight () =
699 let rec f pn ph pi fh l =
700 match l with
701 | (n, _, h, _) :: rest ->
702 let ips = calcips h in
703 let fh =
704 if conf.presentation
705 then fh+ips
706 else (
707 if isbirdseye state.mode && pn = 0
708 then fh + ips
709 else fh
712 let fh = fh + ((n - pn) * (ph + pi)) in
713 f n h ips fh rest;
715 | [] ->
716 let inc =
717 if conf.presentation || (isbirdseye state.mode && pn = 0)
718 then 0
719 else -pi
721 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
722 max 0 fh
724 let fh = f 0 0 0 0 state.pdims in
728 let getpageyh pageno =
729 let rec f pn ph pi y l =
730 match l with
731 | (n, _, h, _) :: rest ->
732 let ips = calcips h in
733 if n >= pageno
734 then
735 let h = if n = pageno then h else ph in
736 if conf.presentation && n = pageno
737 then
738 y + (pageno - pn) * (ph + pi) + pi, h
739 else
740 y + (pageno - pn) * (ph + pi), h
741 else
742 let y = y + (if conf.presentation then pi else 0) in
743 let y = y + (n - pn) * (ph + pi) in
744 f n h ips y rest
746 | [] ->
747 y + (pageno - pn) * (ph + pi), ph
749 f 0 0 0 0 state.pdims
752 let getpagedim pageno =
753 let rec f ppdim l =
754 match l with
755 | (n, _, _, _) as pdim :: rest ->
756 if n >= pageno
757 then (if n = pageno then pdim else ppdim)
758 else f pdim rest
760 | [] -> ppdim
762 f (-1, -1, -1, -1) state.pdims
765 let getpagey pageno = fst (getpageyh pageno);;
767 let layout y sh =
768 let sh = sh - state.hscrollh in
769 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
770 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
771 match pdims with
772 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
773 let ips = calcips h in
774 let yinc =
775 if conf.presentation || (isbirdseye state.mode && pageno = 0)
776 then ips
777 else 0
779 (w, h, ips, xoff), rest, pdimno + 1, yinc
780 | _ ->
781 prev, pdims, pdimno, 0
783 let dy = dy + yinc in
784 let py = py + yinc in
785 if pageno = state.pagecount || dy >= sh
786 then
787 accu
788 else
789 let vy = y + dy in
790 if py + h <= vy - yinc
791 then
792 let py = py + h + ips in
793 let dy = max 0 (py - y) in
794 f ~pageno:(pageno+1)
795 ~pdimno
796 ~prev:curr
799 ~pdims:rest
800 ~accu
801 else
802 let pagey = vy - py in
803 let pagevh = h - pagey in
804 let pagevh = min (sh - dy) pagevh in
805 let off = if yinc > 0 then py - vy else 0 in
806 let py = py + h + ips in
807 let pagex, dx =
808 let xoff = xoff +
809 if state.w < conf.winw - state.scrollw
810 then (conf.winw - state.scrollw - state.w) / 2
811 else 0
813 let dispx = xoff + state.x in
814 if dispx < 0
815 then (-dispx, 0)
816 else (0, dispx)
818 let pagevw =
819 let lw = w - pagex in
820 min lw (conf.winw - state.scrollw)
822 let e =
823 { pageno = pageno
824 ; pagedimno = pdimno
825 ; pagew = w
826 ; pageh = h
827 ; pagex = pagex
828 ; pagey = pagey + off
829 ; pagevw = pagevw
830 ; pagevh = pagevh - off
831 ; pagedispx = dx
832 ; pagedispy = dy + off
835 let accu = e :: accu in
836 f ~pageno:(pageno+1)
837 ~pdimno
838 ~prev:curr
840 ~dy:(dy+pagevh+ips)
841 ~pdims:rest
842 ~accu
844 if state.invalidated = 0
845 then (
846 let accu =
848 ~pageno:0
849 ~pdimno:~-1
850 ~prev:(0,0,0,0)
851 ~py:0
852 ~dy:0
853 ~pdims:state.pdims
854 ~accu:[]
856 List.rev accu
858 else
862 let clamp incr =
863 let y = state.y + incr in
864 let y = max 0 y in
865 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
869 let getopaque pageno =
870 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
871 with Not_found -> None
874 let putopaque pageno opaque =
875 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
878 let itertiles l f =
879 let tilex = l.pagex mod conf.tilew in
880 let tiley = l.pagey mod conf.tileh in
882 let col = l.pagex / conf.tilew in
883 let row = l.pagey / conf.tileh in
885 let vw =
886 let a = l.pagew - l.pagex in
887 let b = conf.winw - state.scrollw in
888 min a b
889 and vh = l.pagevh in
891 let rec rowloop row y0 dispy h =
892 if h = 0
893 then ()
894 else (
895 let dh = conf.tileh - y0 in
896 let dh = min h dh in
897 let rec colloop col x0 dispx w =
898 if w = 0
899 then ()
900 else (
901 let dw = conf.tilew - x0 in
902 let dw = min w dw in
904 f col row dispx dispy x0 y0 dw dh;
905 colloop (col+1) 0 (dispx+dw) (w-dw)
908 colloop col tilex l.pagedispx vw;
909 rowloop (row+1) 0 (dispy+dh) (h-dh)
912 if vw > 0 && vh > 0
913 then rowloop row tiley l.pagedispy vh;
916 let gettileopaque l col row =
917 let key =
918 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
920 try Some (Hashtbl.find state.tilemap key)
921 with Not_found -> None
924 let puttileopaque l col row gen colorspace angle opaque size elapsed =
925 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
926 Hashtbl.add state.tilemap key (opaque, size, elapsed)
929 let drawtiles l color =
930 GlDraw.color color;
931 let f col row x y tilex tiley w h =
932 match gettileopaque l col row with
933 | Some (opaque, _, t) ->
934 let params = x, y, w, h, tilex, tiley in
935 if conf.invert
936 then (
937 Gl.enable `blend;
938 GlFunc.blend_func `zero `one_minus_src_color;
940 drawtile params opaque;
941 if conf.invert
942 then Gl.disable `blend;
943 if conf.debug
944 then (
945 let s = Printf.sprintf
946 "%d[%d,%d] %f sec"
947 l.pageno col row t
949 let w = measurestr fstate.fontsize s in
950 GlMisc.push_attrib [`current];
951 GlDraw.color (0.0, 0.0, 0.0);
952 GlDraw.rect
953 (float (x-2), float (y-2))
954 (float (x+2) +. w, float (y + fstate.fontsize + 2));
955 GlDraw.color (1.0, 1.0, 1.0);
956 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
957 GlMisc.pop_attrib ();
960 | _ ->
961 let w =
962 let lw = conf.winw - state.scrollw - x in
963 min lw w
964 and h =
965 let lh = conf.winh - y in
966 min lh h
968 Gl.enable `texture_2d;
969 begin match state.texid with
970 | Some id ->
971 GlTex.bind_texture `texture_2d id;
972 let x0 = float x
973 and y0 = float y
974 and x1 = float (x+w)
975 and y1 = float (y+h) in
977 let tw = float w /. 64.0
978 and th = float h /. 64.0 in
979 let tx0 = float tilex /. 64.0
980 and ty0 = float tiley /. 64.0 in
981 let tx1 = tx0 +. tw
982 and ty1 = ty0 +. th in
983 GlDraw.begins `quads;
984 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
985 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
986 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
987 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
988 GlDraw.ends ();
990 Gl.disable `texture_2d;
991 | None ->
992 GlDraw.color (1.0, 1.0, 1.0);
993 GlDraw.rect
994 (float x, float y)
995 (float (x+w), float (y+h));
996 end;
997 if w > 128 && h > fstate.fontsize + 10
998 then (
999 GlDraw.color (0.0, 0.0, 0.0);
1000 let c, r =
1001 if conf.verbose
1002 then (col*conf.tilew, row*conf.tileh)
1003 else col, row
1005 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1007 GlDraw.color color;
1009 itertiles l f
1012 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1014 let tilevisible1 l x y =
1015 let ax0 = l.pagex
1016 and ax1 = l.pagex + l.pagevw
1017 and ay0 = l.pagey
1018 and ay1 = l.pagey + l.pagevh in
1020 let bx0 = x
1021 and by0 = y in
1022 let bx1 = min (bx0 + conf.tilew) l.pagew
1023 and by1 = min (by0 + conf.tileh) l.pageh in
1025 let rx0 = max ax0 bx0
1026 and ry0 = max ay0 by0
1027 and rx1 = min ax1 bx1
1028 and ry1 = min ay1 by1 in
1030 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1031 nonemptyintersection
1034 let tilevisible layout n x y =
1035 let rec findpageinlayout = function
1036 | l :: _ when l.pageno = n -> tilevisible1 l x y
1037 | _ :: rest -> findpageinlayout rest
1038 | [] -> false
1040 findpageinlayout layout
1043 let tileready l x y =
1044 tilevisible1 l x y &&
1045 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1048 let tilepage n p layout =
1049 let rec loop = function
1050 | l :: rest ->
1051 if l.pageno = n
1052 then
1053 let f col row _ _ _ _ _ _ =
1054 if state.currently = Idle
1055 then
1056 match gettileopaque l col row with
1057 | Some _ -> ()
1058 | None ->
1059 let x = col*conf.tilew
1060 and y = row*conf.tileh in
1061 let w =
1062 let w = l.pagew - x in
1063 min w conf.tilew
1065 let h =
1066 let h = l.pageh - y in
1067 min h conf.tileh
1069 wcmd "tile"
1070 [`s p
1071 ;`i x
1072 ;`i y
1073 ;`i w
1074 ;`i h
1076 state.currently <-
1077 Tiling (
1078 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1079 conf.tilew, conf.tileh
1082 itertiles l f;
1083 else
1084 loop rest
1086 | [] -> ()
1088 if state.invalidated = 0 then loop layout;
1091 let preloadlayout visiblepages =
1092 let presentation = conf.presentation in
1093 let interpagespace = conf.interpagespace in
1094 let maxy = state.maxy in
1095 conf.presentation <- false;
1096 conf.interpagespace <- 0;
1097 state.maxy <- calcheight ();
1098 let y =
1099 match visiblepages with
1100 | [] -> 0
1101 | l :: _ -> getpagey l.pageno + l.pagey
1103 let y = if y < conf.winh then 0 else y - conf.winh in
1104 let h = state.y - y + conf.winh*3 in
1105 let pages = layout y h in
1106 conf.presentation <- presentation;
1107 conf.interpagespace <- interpagespace;
1108 state.maxy <- maxy;
1109 pages;
1112 let load pages =
1113 let rec loop pages =
1114 if state.currently != Idle
1115 then ()
1116 else
1117 match pages with
1118 | l :: rest ->
1119 begin match getopaque l.pageno with
1120 | None ->
1121 wcmd "page" [`i l.pageno; `i l.pagedimno];
1122 state.currently <- Loading (l, state.gen);
1123 | Some opaque ->
1124 tilepage l.pageno opaque pages;
1125 loop rest
1126 end;
1127 | _ -> ()
1129 if state.invalidated = 0 then loop pages
1132 let preload pages =
1133 load pages;
1134 if conf.preload && state.currently = Idle
1135 then load (preloadlayout pages);
1138 let layoutready layout =
1139 let rec fold all ls =
1140 all && match ls with
1141 | l :: rest ->
1142 let seen = ref false in
1143 let allvisible = ref true in
1144 let foo col row _ _ _ _ _ _ =
1145 seen := true;
1146 allvisible := !allvisible &&
1147 begin match gettileopaque l col row with
1148 | Some _ -> true
1149 | None -> false
1152 itertiles l foo;
1153 fold (!seen && !allvisible) rest
1154 | [] -> true
1156 let alltilesvisible = fold true layout in
1157 alltilesvisible;
1160 let gotoy y =
1161 let y = bound y 0 state.maxy in
1162 let y, layout, proceed =
1163 match conf.maxwait with
1164 | Some time ->
1165 begin match state.throttle with
1166 | None ->
1167 let layout = layout y conf.winh in
1168 let ready = layoutready layout in
1169 if not ready
1170 then (
1171 load layout;
1172 state.throttle <- Some (layout, y, now ());
1174 else G.postRedisplay "gotoy showall (None)";
1175 y, layout, ready
1176 | Some (_, _, started) ->
1177 let dt = now () -. started in
1178 if dt > time
1179 then (
1180 state.throttle <- None;
1181 let layout = layout y conf.winh in
1182 load layout;
1183 G.postRedisplay "maxwait";
1184 y, layout, true
1186 else -1, [], false
1189 | None ->
1190 let layout = layout y conf.winh in
1191 if true || layoutready layout
1192 then G.postRedisplay "gotoy ready";
1193 y, layout, true
1195 if proceed
1196 then (
1197 state.y <- y;
1198 state.layout <- layout;
1199 begin match state.mode with
1200 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1201 if not (pagevisible layout pageno)
1202 then (
1203 match state.layout with
1204 | [] -> ()
1205 | l :: _ ->
1206 state.mode <- Birdseye (
1207 conf, leftx, l.pageno, hooverpageno, anchor
1210 | _ -> ()
1211 end;
1212 preload layout;
1216 let conttiling pageno opaque =
1217 tilepage pageno opaque
1218 (if conf.preload then preloadlayout state.layout else state.layout)
1221 let gotoy_and_clear_text y =
1222 gotoy y;
1223 if not conf.verbose then state.text <- "";
1226 let getanchor () =
1227 match state.layout with
1228 | [] -> emptyanchor
1229 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1232 let getanchory (n, top) =
1233 let y, h = getpageyh n in
1234 y + (truncate (top *. float h));
1237 let gotoanchor anchor =
1238 gotoy (getanchory anchor);
1241 let addnav () =
1242 cbput state.hists.nav (getanchor ());
1245 let getnav dir =
1246 let anchor = cbgetc state.hists.nav dir in
1247 getanchory anchor;
1250 let gotopage n top =
1251 let y, h = getpageyh n in
1252 gotoy_and_clear_text (y + (truncate (top *. float h)));
1255 let gotopage1 n top =
1256 let y = getpagey n in
1257 gotoy_and_clear_text (y + top);
1260 let invalidate () =
1261 state.layout <- [];
1262 state.pdims <- [];
1263 state.rects <- [];
1264 state.rects1 <- [];
1265 state.invalidated <- state.invalidated + 1;
1268 let writeopen path password =
1269 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1272 let opendoc path password =
1273 invalidate ();
1274 state.path <- path;
1275 state.password <- password;
1276 state.gen <- state.gen + 1;
1277 state.docinfo <- [];
1279 setaalevel conf.aalevel;
1280 writeopen path password;
1281 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1282 wcmd "geometry" [`i state.w; `i conf.winh];
1285 let scalecolor c =
1286 let c = c *. conf.colorscale in
1287 (c, c, c);
1290 let scalecolor2 (r, g, b) =
1291 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1294 let represent () =
1295 state.maxy <- calcheight ();
1296 state.hscrollh <-
1297 if state.w <= conf.winw - state.scrollw
1298 then 0
1299 else state.scrollw
1301 match state.mode with
1302 | Birdseye (_, _, pageno, _, _) ->
1303 let y, h = getpageyh pageno in
1304 let top = (conf.winh - h) / 2 in
1305 gotoy (max 0 (y - top))
1306 | _ -> gotoanchor state.anchor
1309 let reshape =
1310 let firsttime = ref true in
1311 fun ~w ~h ->
1312 GlDraw.viewport 0 0 w h;
1313 if state.invalidated = 0 && not !firsttime
1314 then state.anchor <- getanchor ();
1316 firsttime := false;
1317 conf.winw <- w;
1318 let w = truncate (float w *. conf.zoom) - state.scrollw in
1319 let w = max w 2 in
1320 state.w <- w;
1321 conf.winh <- h;
1322 setfontsize fstate.fontsize;
1323 GlMat.mode `modelview;
1324 GlMat.load_identity ();
1326 GlMat.mode `projection;
1327 GlMat.load_identity ();
1328 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1329 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1330 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1332 invalidate ();
1333 wcmd "geometry" [`i w; `i h];
1336 let enttext () =
1337 let len = String.length state.text in
1338 let drawstring s =
1339 let hscrollh =
1340 match state.mode with
1341 | View -> state.hscrollh
1342 | _ -> 0
1344 let rect x w =
1345 GlDraw.rect
1346 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1347 (x+.w, float (conf.winh - hscrollh))
1350 let w = float (conf.winw - state.scrollw - 1) in
1351 if state.progress >= 0.0 && state.progress < 1.0
1352 then (
1353 GlDraw.color (0.3, 0.3, 0.3);
1354 let w1 = w *. state.progress in
1355 rect 0.0 w1;
1356 GlDraw.color (0.0, 0.0, 0.0);
1357 rect w1 (w-.w1)
1359 else (
1360 GlDraw.color (0.0, 0.0, 0.0);
1361 rect 0.0 w;
1364 GlDraw.color (1.0, 1.0, 1.0);
1365 drawstring fstate.fontsize
1366 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1368 let s =
1369 match state.mode with
1370 | Textentry ((prefix, text, _, _, _), _) ->
1371 let s =
1372 if len > 0
1373 then
1374 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1375 else
1376 Printf.sprintf "%s%s_" prefix text
1380 | _ -> state.text
1382 let s =
1383 if state.newerrmsgs
1384 then (
1385 if not (istextentry state.mode)
1386 then
1387 let s1 = "(press 'e' to review error messasges)" in
1388 if String.length s > 0 then s ^ " " ^ s1 else s1
1389 else s
1391 else s
1393 if String.length s > 0
1394 then drawstring s
1397 let showtext c s =
1398 state.text <- Printf.sprintf "%c%s" c s;
1399 G.postRedisplay "showtext";
1402 let gctiles () =
1403 let len = Queue.length state.tilelru in
1404 let rec loop qpos =
1405 if state.memused <= conf.memlimit
1406 then ()
1407 else (
1408 if qpos < len
1409 then
1410 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1411 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1412 let (_, pw, ph, _) = getpagedim n in
1414 gen = state.gen
1415 && colorspace = conf.colorspace
1416 && angle = conf.angle
1417 && pagew = pw
1418 && pageh = ph
1419 && (
1420 let layout =
1421 match state.throttle with
1422 | None ->
1423 if conf.preload
1424 then preloadlayout state.layout
1425 else state.layout
1426 | Some (layout, _, _) ->
1427 layout
1429 let x = col*conf.tilew
1430 and y = row*conf.tileh in
1431 tilevisible layout n x y
1433 then Queue.push lruitem state.tilelru
1434 else (
1435 wcmd "freetile" [`s p];
1436 state.memused <- state.memused - s;
1437 state.uioh#infochanged Memused;
1438 Hashtbl.remove state.tilemap k;
1440 loop (qpos+1)
1443 loop 0
1446 let flushtiles () =
1447 Queue.iter (fun (k, p, s) ->
1448 wcmd "freetile" [`s p];
1449 state.memused <- state.memused - s;
1450 state.uioh#infochanged Memused;
1451 Hashtbl.remove state.tilemap k;
1452 ) state.tilelru;
1453 Queue.clear state.tilelru;
1454 load state.layout;
1457 let logcurrently = function
1458 | Idle -> dolog "Idle"
1459 | Loading (l, gen) ->
1460 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1461 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1462 dolog
1463 "Tiling %d[%d,%d] page=%s cs=%s angle"
1464 l.pageno col row pageopaque
1465 (colorspace_to_string colorspace)
1467 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1468 angle gen conf.angle state.gen
1469 tilew tileh
1470 conf.tilew conf.tileh
1472 | Outlining _ ->
1473 dolog "outlining"
1476 let act cmds =
1477 (* dolog "%S" cmds; *)
1478 let op, args =
1479 let spacepos =
1480 try String.index cmds ' '
1481 with Not_found -> -1
1483 if spacepos = -1
1484 then cmds, ""
1485 else
1486 let l = String.length cmds in
1487 let op = String.sub cmds 0 spacepos in
1488 op, begin
1489 if l - spacepos < 2 then ""
1490 else String.sub cmds (spacepos+1) (l-spacepos-1)
1493 match op with
1494 | "clear" ->
1495 state.uioh#infochanged Pdim;
1496 state.pdims <- [];
1498 | "clearrects" ->
1499 state.rects <- state.rects1;
1500 G.postRedisplay "clearrects";
1502 | "continue" ->
1503 let n =
1504 try Scanf.sscanf args "%u" (fun n -> n)
1505 with exn ->
1506 dolog "error processing 'continue' %S: %s"
1507 cmds (Printexc.to_string exn);
1508 exit 1;
1510 state.pagecount <- n;
1511 state.invalidated <- state.invalidated - 1;
1512 begin match state.currently with
1513 | Outlining l ->
1514 state.currently <- Idle;
1515 state.outlines <- Array.of_list (List.rev l)
1516 | _ -> ()
1517 end;
1518 if state.invalidated = 0
1519 then represent ();
1520 if conf.maxwait = None
1521 then G.postRedisplay "continue";
1523 | "title" ->
1524 Glut.setWindowTitle args
1526 | "msg" ->
1527 showtext ' ' args
1529 | "vmsg" ->
1530 if conf.verbose
1531 then showtext ' ' args
1533 | "progress" ->
1534 let progress, text =
1536 Scanf.sscanf args "%f %n"
1537 (fun f pos ->
1538 f, String.sub args pos (String.length args - pos))
1539 with exn ->
1540 dolog "error processing 'progress' %S: %s"
1541 cmds (Printexc.to_string exn);
1542 exit 1;
1544 state.text <- text;
1545 state.progress <- progress;
1546 G.postRedisplay "progress"
1548 | "firstmatch" ->
1549 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1551 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1552 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1553 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1554 with exn ->
1555 dolog "error processing 'firstmatch' %S: %s"
1556 cmds (Printexc.to_string exn);
1557 exit 1;
1559 let y = (getpagey pageno) + truncate y0 in
1560 addnav ();
1561 gotoy y;
1562 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1564 | "match" ->
1565 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1567 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1568 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1569 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1570 with exn ->
1571 dolog "error processing 'match' %S: %s"
1572 cmds (Printexc.to_string exn);
1573 exit 1;
1575 state.rects1 <-
1576 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1578 | "page" ->
1579 let pageopaque, t =
1581 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1582 with exn ->
1583 dolog "error processing 'page' %S: %s"
1584 cmds (Printexc.to_string exn);
1585 exit 1;
1587 begin match state.currently with
1588 | Loading (l, gen) ->
1589 vlog "page %d took %f sec" l.pageno t;
1590 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1591 begin match state.throttle with
1592 | None ->
1593 let preloadedpages =
1594 if conf.preload
1595 then preloadlayout state.layout
1596 else state.layout
1598 let evict () =
1599 let module IntSet =
1600 Set.Make (struct type t = int let compare = (-) end) in
1601 let set =
1602 List.fold_left (fun s l -> IntSet.add l.pageno s)
1603 IntSet.empty preloadedpages
1605 let evictedpages =
1606 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1607 if not (IntSet.mem pageno set)
1608 then (
1609 wcmd "freepage" [`s opaque];
1610 key :: accu
1612 else accu
1613 ) state.pagemap []
1615 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1617 evict ();
1618 state.currently <- Idle;
1619 if gen = state.gen
1620 then (
1621 tilepage l.pageno pageopaque state.layout;
1622 load state.layout;
1623 load preloadedpages;
1624 if pagevisible state.layout l.pageno
1625 && layoutready state.layout
1626 then G.postRedisplay "page";
1629 | Some (layout, _, _) ->
1630 state.currently <- Idle;
1631 tilepage l.pageno pageopaque layout;
1632 load state.layout
1633 end;
1635 | _ ->
1636 dolog "Inconsistent loading state";
1637 logcurrently state.currently;
1638 raise Quit;
1641 | "tile" ->
1642 let (x, y, opaque, size, t) =
1644 Scanf.sscanf args "%u %u %s %u %f"
1645 (fun x y p size t -> (x, y, p, size, t))
1646 with exn ->
1647 dolog "error processing 'tile' %S: %s"
1648 cmds (Printexc.to_string exn);
1649 exit 1;
1651 begin match state.currently with
1652 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1653 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1655 if tilew != conf.tilew || tileh != conf.tileh
1656 then (
1657 wcmd "freetile" [`s opaque];
1658 state.currently <- Idle;
1659 load state.layout;
1661 else (
1662 puttileopaque l col row gen cs angle opaque size t;
1663 state.memused <- state.memused + size;
1664 state.uioh#infochanged Memused;
1665 gctiles ();
1666 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1667 opaque, size) state.tilelru;
1669 let layout =
1670 match state.throttle with
1671 | None -> state.layout
1672 | Some (layout, _, _) -> layout
1675 state.currently <- Idle;
1676 if gen = state.gen
1677 && conf.colorspace = cs
1678 && conf.angle = angle
1679 && tilevisible layout l.pageno x y
1680 then conttiling l.pageno pageopaque;
1682 begin match state.throttle with
1683 | None ->
1684 preload state.layout;
1685 if gen = state.gen
1686 && conf.colorspace = cs
1687 && conf.angle = angle
1688 && tilevisible state.layout l.pageno x y
1689 then G.postRedisplay "tile nothrottle";
1691 | Some (layout, y, _) ->
1692 let ready = layoutready layout in
1693 if ready
1694 then (
1695 state.y <- y;
1696 state.layout <- layout;
1697 state.throttle <- None;
1698 G.postRedisplay "throttle";
1700 else load layout;
1701 end;
1704 | _ ->
1705 dolog "Inconsistent tiling state";
1706 logcurrently state.currently;
1707 raise Quit;
1710 | "pdim" ->
1711 let pdim =
1713 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1714 with exn ->
1715 dolog "error processing 'pdim' %S: %s"
1716 cmds (Printexc.to_string exn);
1717 exit 1;
1719 state.uioh#infochanged Pdim;
1720 state.pdims <- pdim :: state.pdims
1722 | "o" ->
1723 let (l, n, t, h, pos) =
1725 Scanf.sscanf args "%u %u %d %u %n"
1726 (fun l n t h pos -> l, n, t, h, pos)
1727 with exn ->
1728 dolog "error processing 'o' %S: %s"
1729 cmds (Printexc.to_string exn);
1730 exit 1;
1732 let s = String.sub args pos (String.length args - pos) in
1733 let outline = (s, l, (n, float t /. float h)) in
1734 begin match state.currently with
1735 | Outlining outlines ->
1736 state.currently <- Outlining (outline :: outlines)
1737 | Idle ->
1738 state.currently <- Outlining [outline]
1739 | currently ->
1740 dolog "invalid outlining state";
1741 logcurrently currently
1744 | "info" ->
1745 state.docinfo <- (1, args) :: state.docinfo
1747 | "infoend" ->
1748 state.uioh#infochanged Docinfo;
1749 state.docinfo <- List.rev state.docinfo
1751 | _ ->
1752 dolog "unknown cmd `%S'" cmds
1755 let idle () =
1756 if state.deadline == nan then state.deadline <- now ();
1757 let r =
1758 match state.errfd with
1759 | None -> [state.csock]
1760 | Some fd -> [state.csock; fd]
1762 let rec loop delay =
1763 let timeout =
1764 if delay > 0.0
1765 then max 0.0 (state.deadline -. now ())
1766 else 0.0
1768 let r, _, _ = Unix.select r [] [] timeout in
1769 begin match r with
1770 | [] ->
1771 begin match state.autoscroll with
1772 | Some step when step != 0 ->
1773 let y = state.y + step in
1774 let y =
1775 if y < 0
1776 then state.maxy
1777 else if y >= state.maxy then 0 else y
1779 gotoy y;
1780 if state.mode = View
1781 then state.text <- "";
1782 state.deadline <- state.deadline +. 0.005;
1784 | _ ->
1785 state.deadline <- state.deadline +. delay;
1786 end;
1788 | l ->
1789 let rec checkfds c = function
1790 | [] -> c
1791 | fd :: rest when fd = state.csock ->
1792 let cmd = readcmd state.csock in
1793 act cmd;
1794 checkfds true rest
1795 | fd :: rest ->
1796 let s = String.create 80 in
1797 let n = Unix.read fd s 0 80 in
1798 if conf.redirectstderr
1799 then (
1800 Buffer.add_substring state.errmsgs s 0 n;
1801 state.newerrmsgs <- true;
1802 Glut.postRedisplay ();
1804 else (
1805 prerr_string (String.sub s 0 n);
1806 flush stderr;
1808 checkfds c rest
1810 if checkfds false l
1811 then loop 0.0
1812 end;
1813 in loop 0.007
1816 let onhist cb =
1817 let rc = cb.rc in
1818 let action = function
1819 | HCprev -> cbget cb ~-1
1820 | HCnext -> cbget cb 1
1821 | HCfirst -> cbget cb ~-(cb.rc)
1822 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1823 and cancel () = cb.rc <- rc
1824 in (action, cancel)
1827 let search pattern forward =
1828 if String.length pattern > 0
1829 then
1830 let pn, py =
1831 match state.layout with
1832 | [] -> 0, 0
1833 | l :: _ ->
1834 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1836 let cmd =
1837 let b = makecmd "search"
1838 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1840 Buffer.add_char b ',';
1841 Buffer.add_string b pattern;
1842 Buffer.add_char b '\000';
1843 Buffer.contents b;
1845 writecmd state.csock cmd;
1848 let intentry text key =
1849 let c = Char.unsafe_chr key in
1850 match c with
1851 | '0' .. '9' ->
1852 let text = addchar text c in
1853 TEcont text
1855 | _ ->
1856 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1857 TEcont text
1860 let textentry text key =
1861 let c = Char.unsafe_chr key in
1862 match c with
1863 | _ when key >= 32 && key < 127 ->
1864 let text = addchar text c in
1865 TEcont text
1867 | _ ->
1868 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1869 TEcont text
1872 let reqlayout angle proportional =
1873 match state.throttle with
1874 | None ->
1875 if state.invalidated = 0 then state.anchor <- getanchor ();
1876 conf.angle <- angle mod 360;
1877 conf.proportional <- proportional;
1878 invalidate ();
1879 wcmd "reqlayout" [`i conf.angle; `b proportional];
1880 | _ -> ()
1883 let settrim trimmargins trimfuzz =
1884 if state.invalidated = 0 then state.anchor <- getanchor ();
1885 conf.trimmargins <- trimmargins;
1886 conf.trimfuzz <- trimfuzz;
1887 let x0, y0, x1, y1 = trimfuzz in
1888 invalidate ();
1889 wcmd "settrim" [
1890 `b conf.trimmargins;
1891 `i x0;
1892 `i y0;
1893 `i x1;
1894 `i y1;
1896 Hashtbl.iter (fun _ opaque ->
1897 wcmd "freepage" [`s opaque];
1898 ) state.pagemap;
1899 Hashtbl.clear state.pagemap;
1902 let setzoom zoom =
1903 match state.throttle with
1904 | None ->
1905 let zoom = max 0.01 zoom in
1906 if zoom <> conf.zoom
1907 then (
1908 state.prevzoom <- conf.zoom;
1909 let relx =
1910 if zoom <= 1.0
1911 then (state.x <- 0; 0.0)
1912 else float state.x /. float state.w
1914 conf.zoom <- zoom;
1915 reshape conf.winw conf.winh;
1916 if zoom > 1.0
1917 then (
1918 let x = relx *. float state.w in
1919 state.x <- truncate x;
1921 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1924 | Some (layout, y, started) ->
1925 let time =
1926 match conf.maxwait with
1927 | None -> 0.0
1928 | Some t -> t
1930 let dt = now () -. started in
1931 if dt > time
1932 then (
1933 state.y <- y;
1934 load layout;
1938 let enterbirdseye () =
1939 let zoom = float conf.thumbw /. float conf.winw in
1940 let birdseyepageno =
1941 let cy = conf.winh / 2 in
1942 let fold = function
1943 | [] -> 0
1944 | l :: rest ->
1945 let rec fold best = function
1946 | [] -> best.pageno
1947 | l :: rest ->
1948 let d = cy - (l.pagedispy + l.pagevh/2)
1949 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1950 if abs d < abs dbest
1951 then fold l rest
1952 else best.pageno
1953 in fold l rest
1955 fold state.layout
1957 state.mode <- Birdseye (
1958 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1960 conf.zoom <- zoom;
1961 conf.presentation <- false;
1962 conf.interpagespace <- 10;
1963 conf.hlinks <- false;
1964 state.x <- 0;
1965 state.mstate <- Mnone;
1966 conf.maxwait <- None;
1967 Glut.setCursor Glut.CURSOR_INHERIT;
1968 if conf.verbose
1969 then
1970 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1971 (100.0*.zoom)
1972 else
1973 state.text <- ""
1975 reshape conf.winw conf.winh;
1978 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1979 state.mode <- View;
1980 conf.zoom <- c.zoom;
1981 conf.presentation <- c.presentation;
1982 conf.interpagespace <- c.interpagespace;
1983 conf.maxwait <- c.maxwait;
1984 conf.hlinks <- c.hlinks;
1985 state.x <- leftx;
1986 if conf.verbose
1987 then
1988 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1989 (100.0*.conf.zoom)
1991 reshape conf.winw conf.winh;
1992 state.anchor <- if goback then anchor else (pageno, 0.0);
1995 let togglebirdseye () =
1996 match state.mode with
1997 | Birdseye vals -> leavebirdseye vals true
1998 | View -> enterbirdseye ()
1999 | _ -> ()
2002 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
2003 let pageno = max 0 (pageno - 1) in
2004 let rec loop = function
2005 | [] -> gotopage1 pageno 0
2006 | l :: _ when l.pageno = pageno ->
2007 if l.pagedispy >= 0 && l.pagey = 0
2008 then G.postRedisplay "upbirdseye"
2009 else gotopage1 pageno 0
2010 | _ :: rest -> loop rest
2012 loop state.layout;
2013 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2016 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
2017 let pageno = min (state.pagecount - 1) (pageno + 1) in
2018 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2019 let rec loop = function
2020 | [] ->
2021 let y, h = getpageyh pageno in
2022 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2023 gotoy (clamp dy)
2024 | l :: _ when l.pageno = pageno ->
2025 if l.pagevh != l.pageh
2026 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2027 else G.postRedisplay "downbirdseye"
2028 | _ :: rest -> loop rest
2030 loop state.layout
2033 let optentry mode _ key =
2034 let btos b = if b then "on" else "off" in
2035 let c = Char.unsafe_chr key in
2036 match c with
2037 | 's' ->
2038 let ondone s =
2039 try conf.scrollstep <- int_of_string s with exc ->
2040 state.text <- Printf.sprintf "bad integer `%s': %s"
2041 s (Printexc.to_string exc)
2043 TEswitch ("scroll step: ", "", None, intentry, ondone)
2045 | 'A' ->
2046 let ondone s =
2048 conf.autoscrollstep <- int_of_string s;
2049 if state.autoscroll <> None
2050 then state.autoscroll <- Some conf.autoscrollstep
2051 with exc ->
2052 state.text <- Printf.sprintf "bad integer `%s': %s"
2053 s (Printexc.to_string exc)
2055 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2057 | 'Z' ->
2058 let ondone s =
2060 let zoom = float (int_of_string s) /. 100.0 in
2061 setzoom zoom
2062 with exc ->
2063 state.text <- Printf.sprintf "bad integer `%s': %s"
2064 s (Printexc.to_string exc)
2066 TEswitch ("zoom: ", "", None, intentry, ondone)
2068 | 't' ->
2069 let ondone s =
2071 conf.thumbw <- bound (int_of_string s) 2 4096;
2072 state.text <-
2073 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2074 begin match mode with
2075 | Birdseye beye ->
2076 leavebirdseye beye false;
2077 enterbirdseye ();
2078 | _ -> ();
2080 with exc ->
2081 state.text <- Printf.sprintf "bad integer `%s': %s"
2082 s (Printexc.to_string exc)
2084 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2086 | 'R' ->
2087 let ondone s =
2088 match try
2089 Some (int_of_string s)
2090 with exc ->
2091 state.text <- Printf.sprintf "bad integer `%s': %s"
2092 s (Printexc.to_string exc);
2093 None
2094 with
2095 | Some angle -> reqlayout angle conf.proportional
2096 | None -> ()
2098 TEswitch ("rotation: ", "", None, intentry, ondone)
2100 | 'i' ->
2101 conf.icase <- not conf.icase;
2102 TEdone ("case insensitive search " ^ (btos conf.icase))
2104 | 'p' ->
2105 conf.preload <- not conf.preload;
2106 gotoy state.y;
2107 TEdone ("preload " ^ (btos conf.preload))
2109 | 'v' ->
2110 conf.verbose <- not conf.verbose;
2111 TEdone ("verbose " ^ (btos conf.verbose))
2113 | 'd' ->
2114 conf.debug <- not conf.debug;
2115 TEdone ("debug " ^ (btos conf.debug))
2117 | 'h' ->
2118 conf.maxhfit <- not conf.maxhfit;
2119 state.maxy <-
2120 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2121 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2123 | 'c' ->
2124 conf.crophack <- not conf.crophack;
2125 TEdone ("crophack " ^ btos conf.crophack)
2127 | 'a' ->
2128 let s =
2129 match conf.maxwait with
2130 | None ->
2131 conf.maxwait <- Some infinity;
2132 "always wait for page to complete"
2133 | Some _ ->
2134 conf.maxwait <- None;
2135 "show placeholder if page is not ready"
2137 TEdone s
2139 | 'f' ->
2140 conf.underinfo <- not conf.underinfo;
2141 TEdone ("underinfo " ^ btos conf.underinfo)
2143 | 'P' ->
2144 conf.savebmarks <- not conf.savebmarks;
2145 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2147 | 'S' ->
2148 let ondone s =
2150 let pageno, py =
2151 match state.layout with
2152 | [] -> 0, 0
2153 | l :: _ ->
2154 l.pageno, l.pagey
2156 conf.interpagespace <- int_of_string s;
2157 state.maxy <- calcheight ();
2158 let y = getpagey pageno in
2159 gotoy (y + py)
2160 with exc ->
2161 state.text <- Printf.sprintf "bad integer `%s': %s"
2162 s (Printexc.to_string exc)
2164 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2166 | 'l' ->
2167 reqlayout conf.angle (not conf.proportional);
2168 TEdone ("proportional display " ^ btos conf.proportional)
2170 | 'T' ->
2171 settrim (not conf.trimmargins) conf.trimfuzz;
2172 TEdone ("trim margins " ^ btos conf.trimmargins)
2174 | 'I' ->
2175 conf.invert <- not conf.invert;
2176 TEdone ("invert colors " ^ btos conf.invert)
2178 | _ ->
2179 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2180 TEstop
2183 class type lvsource = object
2184 method getitemcount : int
2185 method getitem : int -> (string * int)
2186 method hasaction : int -> bool
2187 method exit :
2188 uioh:uioh ->
2189 cancel:bool ->
2190 active:int ->
2191 first:int ->
2192 pan:int ->
2193 qsearch:string ->
2194 uioh option
2195 method getactive : int
2196 method getfirst : int
2197 method getqsearch : string
2198 method setqsearch : string -> unit
2199 method getpan : int
2200 end;;
2202 class virtual lvsourcebase = object
2203 val mutable m_active = 0
2204 val mutable m_first = 0
2205 val mutable m_qsearch = ""
2206 val mutable m_pan = 0
2207 method getactive = m_active
2208 method getfirst = m_first
2209 method getqsearch = m_qsearch
2210 method getpan = m_pan
2211 method setqsearch s = m_qsearch <- s
2212 end;;
2214 let textentryspecial key = function
2215 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2216 let s =
2217 match key with
2218 | Glut.KEY_UP -> action HCprev
2219 | Glut.KEY_DOWN -> action HCnext
2220 | Glut.KEY_HOME -> action HCfirst
2221 | Glut.KEY_END -> action HClast
2222 | _ -> state.text
2224 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2225 G.postRedisplay "special textentry";
2226 | _ -> ()
2229 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2230 let enttext te =
2231 state.mode <- Textentry (te, onleave);
2232 state.text <- "";
2233 enttext ();
2234 G.postRedisplay "textentrykeyboard enttext";
2236 match Char.unsafe_chr key with
2237 | '\008' -> (* backspace *)
2238 let len = String.length text in
2239 if len = 0
2240 then (
2241 onleave Cancel;
2242 G.postRedisplay "textentrykeyboard after cancel";
2244 else (
2245 let s = String.sub text 0 (len - 1) in
2246 enttext (c, s, opthist, onkey, ondone)
2249 | '\r' | '\n' ->
2250 ondone text;
2251 onleave Confirm;
2252 G.postRedisplay "textentrykeyboard after confirm"
2254 | '\007' (* ctrl-g *)
2255 | '\027' -> (* escape *)
2256 if String.length text = 0
2257 then (
2258 begin match opthist with
2259 | None -> ()
2260 | Some (_, onhistcancel) -> onhistcancel ()
2261 end;
2262 onleave Cancel;
2263 state.text <- "";
2264 G.postRedisplay "textentrykeyboard after cancel2"
2266 else (
2267 enttext (c, "", opthist, onkey, ondone)
2270 | '\127' -> () (* delete *)
2272 | _ ->
2273 begin match onkey text key with
2274 | TEdone text ->
2275 ondone text;
2276 onleave Confirm;
2277 G.postRedisplay "textentrykeyboard after confirm2";
2279 | TEcont text ->
2280 enttext (c, text, opthist, onkey, ondone);
2282 | TEstop ->
2283 onleave Cancel;
2284 state.text <- "";
2285 G.postRedisplay "textentrykeyboard after cancel3"
2287 | TEswitch te ->
2288 state.mode <- Textentry (te, onleave);
2289 G.postRedisplay "textentrykeyboard switch";
2290 end;
2293 let firstof first active =
2294 if first > active || abs (first - active) > fstate.maxrows - 1
2295 then max 0 (active - (fstate.maxrows/2))
2296 else first
2299 let calcfirst first active =
2300 if active > first
2301 then
2302 let rows = active - first in
2303 if rows > fstate.maxrows then active - fstate.maxrows else first
2304 else active
2307 let scrollph y maxy =
2308 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2309 let sh = float conf.winh /. sh in
2310 let sh = max sh (float conf.scrollh) in
2312 let percent =
2313 if y = state.maxy
2314 then 1.0
2315 else float y /. float maxy
2317 let position = (float conf.winh -. sh) *. percent in
2319 let position =
2320 if position +. sh > float conf.winh
2321 then float conf.winh -. sh
2322 else position
2324 position, sh;
2327 let coe s = (s :> uioh);;
2329 class listview ~(source:lvsource) ~trusted =
2330 object (self)
2331 val m_pan = source#getpan
2332 val m_first = source#getfirst
2333 val m_active = source#getactive
2334 val m_qsearch = source#getqsearch
2335 val m_prev_uioh = state.uioh
2337 method private elemunder y =
2338 let n = y / (fstate.fontsize+1) in
2339 if m_first + n < source#getitemcount
2340 then (
2341 if source#hasaction (m_first + n)
2342 then Some (m_first + n)
2343 else None
2345 else None
2347 method display =
2348 Gl.enable `blend;
2349 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2350 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2351 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2352 GlDraw.color (1., 1., 1.);
2353 Gl.enable `texture_2d;
2354 let fs = fstate.fontsize in
2355 let nfs = fs + 1 in
2356 let ww = fstate.wwidth in
2357 let tabw = 30.0*.ww in
2358 let itemcount = source#getitemcount in
2359 let rec loop row =
2360 if (row - m_first) * nfs > conf.winh
2361 then ()
2362 else (
2363 if row >= 0 && row < itemcount
2364 then (
2365 let (s, level) = source#getitem row in
2366 let y = (row - m_first) * nfs in
2367 let x = 5.0 +. float (level + m_pan) *. ww in
2368 if row = m_active
2369 then (
2370 Gl.disable `texture_2d;
2371 GlDraw.polygon_mode `both `line;
2372 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2373 GlDraw.rect (1., float (y + 1))
2374 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2375 GlDraw.polygon_mode `both `fill;
2376 GlDraw.color (1., 1., 1.);
2377 Gl.enable `texture_2d;
2380 let drawtabularstring s =
2381 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2382 if trusted
2383 then
2384 let tabpos = try String.index s '\t' with Not_found -> -1 in
2385 if tabpos > 0
2386 then
2387 let len = String.length s - tabpos - 1 in
2388 let s1 = String.sub s 0 tabpos
2389 and s2 = String.sub s (tabpos + 1) len in
2390 let nx = drawstr x s1 in
2391 let sw = nx -. x in
2392 let x = x +. (max tabw sw) in
2393 drawstr x s2
2394 else
2395 drawstr x s
2396 else
2397 drawstr x s
2399 let _ = drawtabularstring s in
2400 loop (row+1)
2404 loop m_first;
2405 Gl.disable `blend;
2406 Gl.disable `texture_2d;
2408 method updownlevel incr =
2409 let len = source#getitemcount in
2410 let _, curlevel = source#getitem m_active in
2411 let rec flow i =
2412 if i = len then i-1 else if i = -1 then 0 else
2413 let _, l = source#getitem i in
2414 if l != curlevel then i else flow (i+incr)
2416 let active = flow m_active in
2417 let first = calcfirst m_first active in
2418 G.postRedisplay "special outline updownlevel";
2419 {< m_active = active; m_first = first >}
2421 method private key1 key =
2422 let set active first qsearch =
2423 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2425 let search active pattern incr =
2426 let dosearch re =
2427 let rec loop n =
2428 if n >= 0 && n < source#getitemcount
2429 then (
2430 let s, _ = source#getitem n in
2432 (try ignore (Str.search_forward re s 0); true
2433 with Not_found -> false)
2434 then Some n
2435 else loop (n + incr)
2437 else None
2439 loop active
2442 let re = Str.regexp_case_fold pattern in
2443 dosearch re
2444 with Failure s ->
2445 state.text <- s;
2446 None
2448 match key with
2449 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2450 let incr = if key = 18 then -1 else 1 in
2451 let active, first =
2452 match search (m_active + incr) m_qsearch incr with
2453 | None ->
2454 state.text <- m_qsearch ^ " [not found]";
2455 m_active, m_first
2456 | Some active ->
2457 state.text <- m_qsearch;
2458 active, firstof m_first active
2460 G.postRedisplay "listview ctrl-r/s";
2461 set active first m_qsearch;
2463 | 8 -> (* backspace *)
2464 let len = String.length m_qsearch in
2465 if len = 0
2466 then coe self
2467 else (
2468 if len = 1
2469 then (
2470 state.text <- "";
2471 G.postRedisplay "listview empty qsearch";
2472 set m_active m_first "";
2474 else
2475 let qsearch = String.sub m_qsearch 0 (len - 1) in
2476 let active, first =
2477 match search m_active qsearch ~-1 with
2478 | None ->
2479 state.text <- qsearch ^ " [not found]";
2480 m_active, m_first
2481 | Some active ->
2482 state.text <- qsearch;
2483 active, firstof m_first active
2485 G.postRedisplay "listview backspace qsearch";
2486 set active first qsearch
2489 | _ when key >= 32 && key < 127 ->
2490 let pattern = addchar m_qsearch (Char.chr key) in
2491 let active, first =
2492 match search m_active pattern 1 with
2493 | None ->
2494 state.text <- pattern ^ " [not found]";
2495 m_active, m_first
2496 | Some active ->
2497 state.text <- pattern;
2498 active, firstof m_first active
2500 G.postRedisplay "listview qsearch add";
2501 set active first pattern;
2503 | 27 -> (* escape *)
2504 state.text <- "";
2505 if String.length m_qsearch = 0
2506 then (
2507 G.postRedisplay "list view escape";
2508 begin
2509 match
2510 source#exit (coe self) true m_active m_first m_pan m_qsearch
2511 with
2512 | None -> m_prev_uioh
2513 | Some uioh -> uioh
2516 else (
2517 G.postRedisplay "list view kill qsearch";
2518 source#setqsearch "";
2519 coe {< m_qsearch = "" >}
2522 | 13 -> (* enter *)
2523 state.text <- "";
2524 let self = {< m_qsearch = "" >} in
2525 source#setqsearch "";
2526 let opt =
2527 G.postRedisplay "listview enter";
2528 if m_active >= 0 && m_active < source#getitemcount
2529 then (
2530 source#exit (coe self) false m_active m_first m_pan "";
2532 else (
2533 source#exit (coe self) true m_active m_first m_pan "";
2536 begin match opt with
2537 | None -> m_prev_uioh
2538 | Some uioh -> uioh
2541 | 127 -> (* delete *)
2542 coe self
2544 | _ -> dolog "unknown key %d" key; coe self
2546 method private special1 key =
2547 let itemcount = source#getitemcount in
2548 let find start incr =
2549 let rec find i =
2550 if i = -1 || i = itemcount
2551 then -1
2552 else (
2553 if source#hasaction i
2554 then i
2555 else find (i + incr)
2558 find start
2560 let set active first =
2561 let first = bound first 0 (itemcount - fstate.maxrows) in
2562 state.text <- "";
2563 coe {< m_active = active; m_first = first >}
2565 let navigate incr =
2566 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2567 let active, first =
2568 let incr1 = if incr > 0 then 1 else -1 in
2569 if isvisible m_first m_active
2570 then
2571 let next =
2572 let next = m_active + incr in
2573 let next =
2574 if next < 0 || next >= itemcount
2575 then -1
2576 else find next incr1
2578 if next = -1 || abs (m_active - next) > fstate.maxrows
2579 then -1
2580 else next
2582 if next = -1
2583 then
2584 let first = m_first + incr in
2585 let first = bound first 0 (itemcount - 1) in
2586 let next =
2587 let next = m_active + incr in
2588 let next = bound next 0 (itemcount - 1) in
2589 find next ~-incr1
2591 let active = if next = -1 then m_active else next in
2592 active, first
2593 else
2594 let first = min next m_first in
2595 next, first
2596 else
2597 let first = m_first + incr in
2598 let first = bound first 0 (itemcount - 1) in
2599 let active =
2600 let next = m_active + incr in
2601 let next = bound next 0 (itemcount - 1) in
2602 let next = find next incr1 in
2603 let active =
2604 if next = -1 || abs (m_active - first) > fstate.maxrows
2605 then (
2606 let active = if m_active = -1 then next else m_active in
2607 active
2609 else next
2611 if isvisible first active
2612 then active
2613 else -1
2615 active, first
2617 G.postRedisplay "listview navigate";
2618 set active first;
2620 begin match key with
2621 | Glut.KEY_UP -> navigate ~-1
2622 | Glut.KEY_DOWN -> navigate 1
2623 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2624 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2626 | Glut.KEY_RIGHT ->
2627 state.text <- "";
2628 G.postRedisplay "listview right";
2629 coe {< m_pan = m_pan - 1 >}
2631 | Glut.KEY_LEFT ->
2632 state.text <- "";
2633 G.postRedisplay "listview left";
2634 coe {< m_pan = m_pan + 1 >}
2636 | Glut.KEY_HOME ->
2637 let active = find 0 1 in
2638 G.postRedisplay "listview home";
2639 set active 0;
2641 | Glut.KEY_END ->
2642 let first = max 0 (itemcount - fstate.maxrows) in
2643 let active = find (itemcount - 1) ~-1 in
2644 G.postRedisplay "listview end";
2645 set active first;
2647 | _ -> coe self
2648 end;
2650 method key key =
2651 match state.mode with
2652 | Textentry te -> textentrykeyboard key te; coe self
2653 | _ -> self#key1 key
2655 method special key =
2656 match state.mode with
2657 | Textentry te -> textentryspecial key te; coe self
2658 | _ -> self#special1 key
2660 method button button bstate x y =
2661 let opt =
2662 match button with
2663 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollbw ->
2664 G.postRedisplay "listview scroll";
2665 if bstate = Glut.DOWN
2666 then
2667 let _, position, sh = self#scrollph in
2668 if y > truncate position && y < truncate (position +. sh)
2669 then (
2670 state.mstate <- Mscrolly;
2671 Some (coe self)
2673 else
2674 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2675 let first = truncate (s *. float source#getitemcount) in
2676 let first = min source#getitemcount first in
2677 Some (coe {< m_first = first; m_active = first >})
2678 else (
2679 state.mstate <- Mnone;
2680 Some (coe self);
2682 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2683 begin match self#elemunder y with
2684 | Some n ->
2685 G.postRedisplay "listview click";
2686 source#exit
2687 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2688 | _ ->
2689 Some (coe self)
2691 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2692 let len = source#getitemcount in
2693 let first =
2694 if n = 4 && m_first + fstate.maxrows >= len
2695 then
2696 m_first
2697 else
2698 let first = m_first + (if n == 3 then -1 else 1) in
2699 bound first 0 (len - 1)
2701 G.postRedisplay "listview wheel";
2702 Some (coe {< m_first = first >})
2703 | _ ->
2704 Some (coe self)
2706 match opt with
2707 | None -> m_prev_uioh
2708 | Some uioh -> uioh
2710 method motion _ y =
2711 match state.mstate with
2712 | Mscrolly ->
2713 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2714 let first = truncate (s *. float source#getitemcount) in
2715 let first = min source#getitemcount first in
2716 G.postRedisplay "listview motion";
2717 coe {< m_first = first; m_active = first >}
2718 | _ -> coe self
2720 method pmotion x y =
2721 if x < conf.winw - conf.scrollbw
2722 then
2723 let n =
2724 match self#elemunder y with
2725 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2726 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2728 let o =
2729 if n != m_active
2730 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2731 else self
2733 coe o
2734 else (
2735 Glut.setCursor Glut.CURSOR_INHERIT;
2736 coe self
2739 method infochanged _ = ()
2741 method scrollpw = (0, 0.0, 0.0)
2742 method scrollph =
2743 let nfs = fstate.fontsize + 1 in
2744 let y = m_first * nfs in
2745 let itemcount = source#getitemcount in
2746 let maxi = max 0 (itemcount - fstate.maxrows) in
2747 let maxy = maxi * nfs in
2748 let p, h = scrollph y maxy in
2749 conf.scrollbw, p, h
2750 end;;
2752 class outlinelistview ~source =
2753 object (self)
2754 inherit listview ~source:(source :> lvsource) ~trusted:false as super
2756 method key key =
2757 match key with
2758 | 14 -> (* ctrl-n *)
2759 source#narrow m_qsearch;
2760 G.postRedisplay "outline ctrl-n";
2761 coe {< m_first = 0; m_active = 0 >}
2763 | 21 -> (* ctrl-u *)
2764 source#denarrow;
2765 G.postRedisplay "outline ctrl-u";
2766 state.text <- "";
2767 coe {< m_first = 0; m_active = 0 >}
2769 | 12 -> (* ctrl-l *)
2770 let first = m_active - (fstate.maxrows / 2) in
2771 G.postRedisplay "outline ctrl-l";
2772 coe {< m_first = first >}
2774 | 127 -> (* delete *)
2775 source#remove m_active;
2776 G.postRedisplay "outline delete";
2777 let active = max 0 (m_active-1) in
2778 coe {< m_first = firstof m_first active;
2779 m_active = active >}
2781 | key -> super#key key
2783 method special key =
2784 let calcfirst first active =
2785 if active > first
2786 then
2787 let rows = active - first in
2788 if rows > fstate.maxrows then active - fstate.maxrows else first
2789 else active
2791 let navigate incr =
2792 let active = m_active + incr in
2793 let active = bound active 0 (source#getitemcount - 1) in
2794 let first = calcfirst m_first active in
2795 G.postRedisplay "special outline navigate";
2796 coe {< m_active = active; m_first = first >}
2798 match key with
2799 | Glut.KEY_UP -> navigate ~-1
2800 | Glut.KEY_DOWN -> navigate 1
2801 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2802 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2804 | Glut.KEY_RIGHT ->
2805 let o =
2806 if Glut.getModifiers () land Glut.active_ctrl != 0
2807 then (
2808 G.postRedisplay "special outline right";
2809 {< m_pan = m_pan + 1 >}
2811 else self#updownlevel 1
2813 coe o
2815 | Glut.KEY_LEFT ->
2816 let o =
2817 if Glut.getModifiers () land Glut.active_ctrl != 0
2818 then (
2819 G.postRedisplay "special outline left";
2820 {< m_pan = m_pan - 1 >}
2822 else self#updownlevel ~-1
2824 coe o
2826 | Glut.KEY_HOME ->
2827 G.postRedisplay "special outline home";
2828 coe {< m_first = 0; m_active = 0 >}
2830 | Glut.KEY_END ->
2831 let active = source#getitemcount - 1 in
2832 let first = max 0 (active - fstate.maxrows) in
2833 G.postRedisplay "special outline end";
2834 coe {< m_active = active; m_first = first >}
2836 | _ -> super#special key
2839 let outlinesource usebookmarks =
2840 let empty = [||] in
2841 (object
2842 inherit lvsourcebase
2843 val mutable m_items = empty
2844 val mutable m_orig_items = empty
2845 val mutable m_prev_items = empty
2846 val mutable m_narrow_pattern = ""
2847 val mutable m_hadremovals = false
2849 method getitemcount =
2850 Array.length m_items + (if m_hadremovals then 1 else 0)
2852 method getitem n =
2853 if n == Array.length m_items && m_hadremovals
2854 then
2855 ("[Confirm removal]", 0)
2856 else
2857 let s, n, _ = m_items.(n) in
2858 (s, n)
2860 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
2861 ignore (uioh, first, pan, qsearch);
2862 let confrimremoval = m_hadremovals && active = Array.length m_items in
2863 let items =
2864 if String.length m_narrow_pattern = 0
2865 then m_orig_items
2866 else m_items
2868 if not cancel
2869 then (
2870 if not confrimremoval
2871 then(
2872 let _, _, anchor = m_items.(active) in
2873 gotoanchor anchor;
2874 m_items <- items;
2876 else (
2877 state.bookmarks <- Array.to_list m_items;
2878 m_orig_items <- m_items;
2881 else m_items <- items;
2882 None
2884 method hasaction _ = true
2886 method greetmsg =
2887 if Array.length m_items != Array.length m_orig_items
2888 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
2889 else ""
2891 method narrow pattern =
2892 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2893 match reopt with
2894 | None -> ()
2895 | Some re ->
2896 let rec loop accu n =
2897 if n = -1
2898 then (
2899 m_narrow_pattern <- pattern;
2900 m_items <- Array.of_list accu
2902 else
2903 let (s, _, _) as o = m_items.(n) in
2904 let accu =
2905 if (try ignore (Str.search_forward re s 0); true
2906 with Not_found -> false)
2907 then o :: accu
2908 else accu
2910 loop accu (n-1)
2912 loop [] (Array.length m_items - 1)
2914 method denarrow =
2915 m_orig_items <- (
2916 if usebookmarks
2917 then Array.of_list state.bookmarks
2918 else state.outlines
2920 m_items <- m_orig_items
2922 method remove m =
2923 if usebookmarks
2924 then
2925 if m >= 0 && m < Array.length m_items
2926 then (
2927 m_hadremovals <- true;
2928 m_items <- Array.init (Array.length m_items - 1) (fun n ->
2929 let n = if n >= m then n+1 else n in
2930 m_items.(n)
2934 method reset anchor items =
2935 m_hadremovals <- false;
2936 if m_orig_items == empty || m_prev_items != items
2937 then (
2938 m_orig_items <- items;
2939 if String.length m_narrow_pattern = 0
2940 then m_items <- items;
2942 m_prev_items <- items;
2943 let rely = getanchory anchor in
2944 let active =
2945 let rec loop n best bestd =
2946 if n = Array.length m_items
2947 then best
2948 else
2949 let (_, _, anchor) = m_items.(n) in
2950 let orely = getanchory anchor in
2951 let d = abs (orely - rely) in
2952 if d < bestd
2953 then loop (n+1) n d
2954 else loop (n+1) best bestd
2956 loop 0 ~-1 max_int
2958 m_active <- active;
2959 m_first <- firstof m_first active
2960 end)
2963 let enterselector usebookmarks =
2964 let source = outlinesource usebookmarks in
2965 fun errmsg ->
2966 let outlines =
2967 if usebookmarks
2968 then Array.of_list state.bookmarks
2969 else state.outlines
2971 if Array.length outlines = 0
2972 then (
2973 showtext ' ' errmsg;
2975 else (
2976 state.text <- source#greetmsg;
2977 Glut.setCursor Glut.CURSOR_INHERIT;
2978 let anchor = getanchor () in
2979 source#reset anchor outlines;
2980 state.uioh <- coe (new outlinelistview ~source);
2981 G.postRedisplay "enter selector";
2985 let enteroutlinemode =
2986 let f = enterselector false in
2987 fun ()-> f "Document has no outline";
2990 let enterbookmarkmode =
2991 let f = enterselector true in
2992 fun () -> f "Document has no bookmarks (yet)";
2995 let color_of_string s =
2996 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
2997 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3001 let color_to_string (r, g, b) =
3002 let r = truncate (r *. 256.0)
3003 and g = truncate (g *. 256.0)
3004 and b = truncate (b *. 256.0) in
3005 Printf.sprintf "%d/%d/%d" r g b
3008 let irect_of_string s =
3009 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3012 let irect_to_string (x0,y0,x1,y1) =
3013 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3016 let makecheckers () =
3017 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3018 following to say:
3019 converted by Issac Trotts. July 25, 2002 *)
3020 let image_height = 64
3021 and image_width = 64 in
3023 let make_image () =
3024 let image =
3025 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3027 for i = 0 to image_width - 1 do
3028 for j = 0 to image_height - 1 do
3029 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3030 (if (i land 8 ) lxor (j land 8) = 0
3031 then [|255;255;255|] else [|200;200;200|])
3032 done
3033 done;
3034 image
3036 let image = make_image () in
3037 let id = GlTex.gen_texture () in
3038 GlTex.bind_texture `texture_2d id;
3039 GlPix.store (`unpack_alignment 1);
3040 GlTex.image2d image;
3041 List.iter (GlTex.parameter ~target:`texture_2d)
3042 [ `wrap_s `repeat;
3043 `wrap_t `repeat;
3044 `mag_filter `nearest;
3045 `min_filter `nearest ];
3049 let setcheckers enabled =
3050 match state.texid with
3051 | None ->
3052 if enabled then state.texid <- Some (makecheckers ())
3054 | Some texid ->
3055 if not enabled
3056 then (
3057 GlTex.delete_texture texid;
3058 state.texid <- None;
3062 let int_of_string_with_suffix s =
3063 let l = String.length s in
3064 let s1, shift =
3065 if l > 1
3066 then
3067 let suffix = Char.lowercase s.[l-1] in
3068 match suffix with
3069 | 'k' -> String.sub s 0 (l-1), 10
3070 | 'm' -> String.sub s 0 (l-1), 20
3071 | 'g' -> String.sub s 0 (l-1), 30
3072 | _ -> s, 0
3073 else s, 0
3075 let n = int_of_string s1 in
3076 let m = n lsl shift in
3077 if m < 0 || m < n
3078 then raise (Failure "value too large")
3079 else m
3082 let string_with_suffix_of_int n =
3083 if n = 0
3084 then "0"
3085 else
3086 let n, s =
3087 if n = 0
3088 then 0, ""
3089 else (
3090 if n land ((1 lsl 20) - 1) = 0
3091 then n lsr 20, "M"
3092 else (
3093 if n land ((1 lsl 10) - 1) = 0
3094 then n lsr 10, "K"
3095 else n, ""
3099 let rec loop s n =
3100 let h = n mod 1000 in
3101 let n = n / 1000 in
3102 if n = 0
3103 then string_of_int h ^ s
3104 else (
3105 let s = Printf.sprintf "_%03d%s" h s in
3106 loop s n
3109 loop "" n ^ s;
3112 let describe_location () =
3113 let f (fn, _) l =
3114 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3116 let fn, ln = List.fold_left f (-1, -1) state.layout in
3117 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3118 let percent =
3119 if maxy <= 0
3120 then 100.
3121 else (100. *. (float state.y /. float maxy))
3123 if fn = ln
3124 then
3125 Printf.sprintf "page %d of %d [%.2f%%]"
3126 (fn+1) state.pagecount percent
3127 else
3128 Printf.sprintf
3129 "pages %d-%d of %d [%.2f%%]"
3130 (fn+1) (ln+1) state.pagecount percent
3133 let enterinfomode =
3134 let btos b = if b then "\xe2\x88\x9a" else "" in
3135 let showextended = ref false in
3136 let leave mode = function
3137 | Confirm -> state.mode <- mode
3138 | Cancel -> state.mode <- mode in
3139 let src =
3140 (object
3141 val mutable m_first_time = true
3142 val mutable m_l = []
3143 val mutable m_a = [||]
3144 val mutable m_prev_uioh = nouioh
3145 val mutable m_prev_mode = View
3147 inherit lvsourcebase
3149 method reset prev_mode prev_uioh =
3150 m_a <- Array.of_list (List.rev m_l);
3151 m_l <- [];
3152 m_prev_mode <- prev_mode;
3153 m_prev_uioh <- prev_uioh;
3154 if m_first_time
3155 then (
3156 let rec loop n =
3157 if n >= Array.length m_a
3158 then ()
3159 else
3160 match m_a.(n) with
3161 | _, _, _, Action _ -> m_active <- n
3162 | _ -> loop (n+1)
3164 loop 0;
3165 m_first_time <- false;
3168 method int name get set =
3169 m_l <-
3170 (name, `int get, 1, Action (
3171 fun u ->
3172 let ondone s =
3173 try set (int_of_string 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 = name ^ ": ", "", None, intentry, ondone in
3180 state.mode <- Textentry (te, leave m_prev_mode);
3182 )) :: m_l
3184 method int_with_suffix name get set =
3185 m_l <-
3186 (name, `intws get, 1, Action (
3187 fun u ->
3188 let ondone s =
3189 try set (int_of_string_with_suffix s)
3190 with exn ->
3191 state.text <- Printf.sprintf "bad integer `%s': %s"
3192 s (Printexc.to_string exn)
3194 state.text <- "";
3195 let te =
3196 name ^ ": ", "", None, intentry_with_suffix, ondone
3198 state.mode <- Textentry (te, leave m_prev_mode);
3200 )) :: m_l
3202 method bool ?(offset=1) ?(btos=btos) name get set =
3203 m_l <-
3204 (name, `bool (btos, get), offset, Action (
3205 fun u ->
3206 let v = get () in
3207 set (not v);
3209 )) :: m_l
3211 method color name get set =
3212 m_l <-
3213 (name, `color get, 1, Action (
3214 fun u ->
3215 let invalid = (nan, nan, nan) in
3216 let ondone s =
3217 let c =
3218 try color_of_string s
3219 with exn ->
3220 state.text <- Printf.sprintf "bad color `%s': %s"
3221 s (Printexc.to_string exn);
3222 invalid
3224 if c <> invalid
3225 then set c;
3227 let te = name ^ ": ", "", None, textentry, ondone in
3228 state.text <- color_to_string (get ());
3229 state.mode <- Textentry (te, leave m_prev_mode);
3231 )) :: m_l
3233 method string name get set =
3234 m_l <-
3235 (name, `string get, 1, Action (
3236 fun u ->
3237 let ondone s = set s in
3238 let te = name ^ ": ", "", None, textentry, ondone in
3239 state.mode <- Textentry (te, leave m_prev_mode);
3241 )) :: m_l
3243 method colorspace name get set =
3244 m_l <-
3245 (name, `string get, 1, Action (
3246 fun _ ->
3247 let source =
3248 let vals = [| "rgb"; "bgr"; "gray" |] in
3249 (object
3250 inherit lvsourcebase
3252 initializer
3253 m_active <- int_of_colorspace conf.colorspace;
3254 m_first <- 0;
3256 method getitemcount = Array.length vals
3257 method getitem n = (vals.(n), 0)
3258 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3259 ignore (uioh, first, pan, qsearch);
3260 if not cancel then set active;
3261 None
3262 method hasaction _ = true
3263 end)
3265 state.text <- "";
3266 coe (new listview ~source ~trusted:true)
3267 )) :: m_l
3269 method caption s offset =
3270 m_l <- (s, `empty, offset, Noaction) :: m_l
3272 method caption2 s f offset =
3273 m_l <- (s, `string f, offset, Noaction) :: m_l
3275 method getitemcount = Array.length m_a
3277 method getitem n =
3278 let tostr = function
3279 | `int f -> string_of_int (f ())
3280 | `intws f -> string_with_suffix_of_int (f ())
3281 | `string f -> f ()
3282 | `color f -> color_to_string (f ())
3283 | `bool (btos, f) -> btos (f ())
3284 | `empty -> ""
3286 let name, t, offset, _ = m_a.(n) in
3287 ((let s = tostr t in
3288 if String.length s > 0
3289 then Printf.sprintf "%s\t%s" name s
3290 else name),
3291 offset)
3293 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3294 let uiohopt =
3295 if not cancel
3296 then (
3297 m_qsearch <- qsearch;
3298 let uioh =
3299 match m_a.(active) with
3300 | _, _, _, Action f -> f uioh
3301 | _ -> uioh
3303 Some uioh
3305 else None
3307 m_active <- active;
3308 m_first <- first;
3309 m_pan <- pan;
3310 uiohopt
3312 method hasaction n =
3313 match m_a.(n) with
3314 | _, _, _, Action _ -> true
3315 | _ -> false
3316 end)
3318 let rec fillsrc prevmode prevuioh =
3319 let sep () = src#caption "" 0 in
3320 let colorp name get set =
3321 src#string name
3322 (fun () -> color_to_string (get ()))
3323 (fun v ->
3325 let c = color_of_string v in
3326 set c
3327 with exn ->
3328 state.text <- Printf.sprintf "bad color `%s': %s"
3329 v (Printexc.to_string exn);
3332 let oldmode = state.mode in
3333 let birdseye = isbirdseye state.mode in
3335 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3337 src#bool "presentation mode"
3338 (fun () -> conf.presentation)
3339 (fun v ->
3340 conf.presentation <- v;
3341 state.anchor <- getanchor ();
3342 represent ());
3344 src#bool "ignore case in searches"
3345 (fun () -> conf.icase)
3346 (fun v -> conf.icase <- v);
3348 src#bool "preload"
3349 (fun () -> conf.preload)
3350 (fun v -> conf.preload <- v);
3352 src#bool "highlight links"
3353 (fun () -> conf.hlinks)
3354 (fun v -> conf.hlinks <- v);
3356 src#bool "under info"
3357 (fun () -> conf.underinfo)
3358 (fun v -> conf.underinfo <- v);
3360 src#bool "persistent bookmarks"
3361 (fun () -> conf.savebmarks)
3362 (fun v -> conf.savebmarks <- v);
3364 src#bool "proportional display"
3365 (fun () -> conf.proportional)
3366 (fun v -> reqlayout conf.angle v);
3368 src#bool "trim margins"
3369 (fun () -> conf.trimmargins)
3370 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3372 src#bool "persistent location"
3373 (fun () -> conf.jumpback)
3374 (fun v -> conf.jumpback <- v);
3376 sep ();
3377 src#int "vertical margin"
3378 (fun () -> conf.interpagespace)
3379 (fun n ->
3380 conf.interpagespace <- n;
3381 let pageno, py =
3382 match state.layout with
3383 | [] -> 0, 0
3384 | l :: _ ->
3385 l.pageno, l.pagey
3387 state.maxy <- calcheight ();
3388 let y = getpagey pageno in
3389 gotoy (y + py)
3392 src#int "page bias"
3393 (fun () -> conf.pagebias)
3394 (fun v -> conf.pagebias <- v);
3396 src#int "scroll step"
3397 (fun () -> conf.scrollstep)
3398 (fun n -> conf.scrollstep <- n);
3400 src#int "auto scroll step"
3401 (fun () ->
3402 match state.autoscroll with
3403 | Some step -> step
3404 | _ -> conf.autoscrollstep)
3405 (fun n ->
3406 if state.autoscroll <> None
3407 then state.autoscroll <- Some n;
3408 conf.autoscrollstep <- n);
3410 src#int "zoom"
3411 (fun () -> truncate (conf.zoom *. 100.))
3412 (fun v -> setzoom ((float v) /. 100.));
3414 src#int "rotation"
3415 (fun () -> conf.angle)
3416 (fun v -> reqlayout v conf.proportional);
3418 src#int "scroll bar width"
3419 (fun () -> state.scrollw)
3420 (fun v ->
3421 state.scrollw <- v;
3422 conf.scrollbw <- v;
3423 reshape conf.winw conf.winh;
3426 src#int "scroll handle height"
3427 (fun () -> conf.scrollh)
3428 (fun v -> conf.scrollh <- v;);
3430 src#int "thumbnail width"
3431 (fun () -> conf.thumbw)
3432 (fun v ->
3433 conf.thumbw <- min 4096 v;
3434 match oldmode with
3435 | Birdseye beye ->
3436 leavebirdseye beye false;
3437 enterbirdseye ()
3438 | _ -> ()
3441 sep ();
3442 src#caption "Presentation mode" 0;
3443 src#bool "scrollbar visible"
3444 (fun () -> conf.scrollbarinpm)
3445 (fun v ->
3446 if v != conf.scrollbarinpm
3447 then (
3448 conf.scrollbarinpm <- v;
3449 if conf.presentation
3450 then (
3451 state.scrollw <- if v then conf.scrollbw else 0;
3452 reshape conf.winw conf.winh;
3457 sep ();
3458 src#caption "Pixmap cache" 0;
3459 src#int_with_suffix "size (advisory)"
3460 (fun () -> conf.memlimit)
3461 (fun v -> conf.memlimit <- v);
3463 src#caption2 "used"
3464 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3465 (string_with_suffix_of_int state.memused)
3466 (Hashtbl.length state.tilemap)) 1;
3468 sep ();
3469 src#caption "Layout" 0;
3470 src#caption2 "Dimension"
3471 (fun () ->
3472 Printf.sprintf "%dx%d (virtual %dx%d)"
3473 conf.winw conf.winh
3474 state.w state.maxy)
3476 if conf.debug
3477 then
3478 src#caption2 "Position" (fun () ->
3479 Printf.sprintf "%dx%d" state.x state.y
3481 else
3482 src#caption2 "Visible" (fun () -> describe_location ()) 1
3485 sep ();
3486 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3487 "Save these parameters as global defaults at exit"
3488 (fun () -> conf.bedefault)
3489 (fun v -> conf.bedefault <- v)
3492 sep ();
3493 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3494 src#bool ~offset:0 ~btos "Extended parameters"
3495 (fun () -> !showextended)
3496 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3497 if !showextended
3498 then (
3499 src#bool "checkers"
3500 (fun () -> conf.checkers)
3501 (fun v -> conf.checkers <- v; setcheckers v);
3502 src#bool "verbose"
3503 (fun () -> conf.verbose)
3504 (fun v -> conf.verbose <- v);
3505 src#bool "invert colors"
3506 (fun () -> conf.invert)
3507 (fun v -> conf.invert <- v);
3508 src#bool "max fit"
3509 (fun () -> conf.maxhfit)
3510 (fun v -> conf.maxhfit <- v);
3511 src#bool "redirect stderr"
3512 (fun () -> conf.redirectstderr)
3513 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3514 src#string "uri launcher"
3515 (fun () -> conf.urilauncher)
3516 (fun v -> conf.urilauncher <- v);
3517 src#string "tile size"
3518 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3519 (fun v ->
3521 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3522 conf.tileh <- max 64 w;
3523 conf.tilew <- max 64 h;
3524 flushtiles ();
3525 with exn ->
3526 state.text <- Printf.sprintf "bad tile size `%s': %s"
3527 v (Printexc.to_string exn));
3528 src#int "texture count"
3529 (fun () -> conf.texcount)
3530 (fun v ->
3531 if realloctexts v
3532 then conf.texcount <- v
3533 else showtext '!' " Failed to set texture count please retry later"
3535 src#int "slice height"
3536 (fun () -> conf.sliceheight)
3537 (fun v ->
3538 conf.sliceheight <- v;
3539 wcmd "sliceh" [`i conf.sliceheight];
3541 src#int "anti-aliasing level"
3542 (fun () -> conf.aalevel)
3543 (fun v ->
3544 conf.aalevel <- bound v 0 8;
3545 state.anchor <- getanchor ();
3546 opendoc state.path state.password;
3548 src#int "ui font size"
3549 (fun () -> fstate.fontsize)
3550 (fun v -> setfontsize (bound v 5 100));
3551 colorp "background color"
3552 (fun () -> conf.bgcolor)
3553 (fun v -> conf.bgcolor <- v);
3554 src#bool "crop hack"
3555 (fun () -> conf.crophack)
3556 (fun v -> conf.crophack <- v);
3557 src#string "trim fuzz"
3558 (fun () -> irect_to_string conf.trimfuzz)
3559 (fun v ->
3561 conf.trimfuzz <- irect_of_string v;
3562 if conf.trimmargins
3563 then settrim true conf.trimfuzz;
3564 with exn ->
3565 state.text <- Printf.sprintf "bad irect `%s': %s"
3566 v (Printexc.to_string exn)
3568 src#string "throttle"
3569 (fun () ->
3570 match conf.maxwait with
3571 | None -> "show place holder if page is not ready"
3572 | Some time ->
3573 if time = infinity
3574 then "wait for page to fully render"
3575 else
3576 "wait " ^ string_of_float time
3577 ^ " seconds before showing placeholder"
3579 (fun v ->
3581 let f = float_of_string v in
3582 if f <= 0.0
3583 then conf.maxwait <- None
3584 else conf.maxwait <- Some f
3585 with exn ->
3586 state.text <- Printf.sprintf "bad time `%s': %s"
3587 v (Printexc.to_string exn)
3589 src#colorspace "color space"
3590 (fun () -> colorspace_to_string conf.colorspace)
3591 (fun v ->
3592 conf.colorspace <- colorspace_of_int v;
3593 wcmd "cs" [`i v];
3594 load state.layout;
3598 sep ();
3599 src#caption "Document" 0;
3600 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3601 if conf.trimmargins
3602 then (
3603 sep ();
3604 src#caption "Trimmed margins" 0;
3605 src#caption2 "Dimensions"
3606 (fun () -> string_of_int (List.length state.pdims)) 1;
3609 src#reset prevmode prevuioh;
3611 fun () ->
3612 state.text <- "";
3613 let prevmode = state.mode
3614 and prevuioh = state.uioh in
3615 fillsrc prevmode prevuioh;
3616 let source = (src :> lvsource) in
3617 state.uioh <- coe (object (self)
3618 inherit listview ~source ~trusted:true as super
3619 val mutable m_prevmemused = 0
3620 method infochanged = function
3621 | Memused ->
3622 if m_prevmemused != state.memused
3623 then (
3624 m_prevmemused <- state.memused;
3625 G.postRedisplay "memusedchanged";
3627 | Pdim -> G.postRedisplay "pdimchanged"
3628 | Docinfo -> fillsrc prevmode prevuioh
3630 method special key =
3631 if Glut.getModifiers () land Glut.active_ctrl = 0
3632 then
3633 match key with
3634 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
3635 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
3636 | _ -> super#special key
3637 else super#special key
3638 end);
3639 G.postRedisplay "info";
3642 let enterhelpmode =
3643 let source =
3644 (object
3645 inherit lvsourcebase
3646 method getitemcount = Array.length state.help
3647 method getitem n =
3648 let s, n, _ = state.help.(n) in
3649 (s, n)
3651 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3652 let optuioh =
3653 if not cancel
3654 then (
3655 m_qsearch <- qsearch;
3656 match state.help.(active) with
3657 | _, _, Action f -> Some (f uioh)
3658 | _ -> Some (uioh)
3660 else None
3662 m_active <- active;
3663 m_first <- first;
3664 m_pan <- pan;
3665 optuioh
3667 method hasaction n =
3668 match state.help.(n) with
3669 | _, _, Action _ -> true
3670 | _ -> false
3672 initializer
3673 m_active <- -1
3674 end)
3675 in fun () ->
3676 state.uioh <- coe (new listview ~source ~trusted:true);
3677 G.postRedisplay "help";
3680 let entermsgsmode =
3681 let msgsource =
3682 let re = Str.regexp "[\r\n]" in
3683 (object
3684 inherit lvsourcebase
3685 val mutable m_items = [||]
3687 method getitemcount = 1 + Array.length m_items
3689 method getitem n =
3690 if n = 0
3691 then "[Clear]", 0
3692 else m_items.(n-1), 0
3694 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3695 ignore uioh;
3696 if not cancel
3697 then (
3698 if active = 0
3699 then Buffer.clear state.errmsgs;
3700 m_qsearch <- qsearch;
3702 m_active <- active;
3703 m_first <- first;
3704 m_pan <- pan;
3705 None
3707 method hasaction n =
3708 n = 0
3710 method reset =
3711 state.newerrmsgs <- false;
3712 let l = Str.split re (Buffer.contents state.errmsgs) in
3713 m_items <- Array.of_list l
3715 initializer
3716 m_active <- 0
3717 end)
3718 in fun () ->
3719 state.text <- "";
3720 msgsource#reset;
3721 let source = (msgsource :> lvsource) in
3722 state.uioh <- coe (object
3723 inherit listview ~source ~trusted:false as super
3724 method display =
3725 if state.newerrmsgs
3726 then msgsource#reset;
3727 super#display
3728 end);
3729 G.postRedisplay "msgs";
3732 let quickbookmark ?title () =
3733 match state.layout with
3734 | [] -> ()
3735 | l :: _ ->
3736 let title =
3737 match title with
3738 | None ->
3739 let sec = Unix.gettimeofday () in
3740 let tm = Unix.localtime sec in
3741 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
3742 (l.pageno+1)
3743 tm.Unix.tm_mday
3744 tm.Unix.tm_mon
3745 (tm.Unix.tm_year + 1900)
3746 tm.Unix.tm_hour
3747 tm.Unix.tm_min
3748 | Some title -> title
3750 state.bookmarks <-
3751 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
3752 :: state.bookmarks
3755 let doreshape w h =
3756 state.fullscreen <- None;
3757 Glut.reshapeWindow w h;
3760 let viewkeyboard key =
3761 let enttext te =
3762 let mode = state.mode in
3763 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3764 state.text <- "";
3765 enttext ();
3766 G.postRedisplay "view:enttext"
3768 let c = Char.chr key in
3769 match c with
3770 | '\027' | 'q' -> (* escape *)
3771 begin match state.mstate with
3772 | Mzoomrect _ ->
3773 state.mstate <- Mnone;
3774 Glut.setCursor Glut.CURSOR_INHERIT;
3775 G.postRedisplay "kill zoom rect";
3776 | _ ->
3777 raise Quit
3778 end;
3780 | '\008' -> (* backspace *)
3781 let y = getnav ~-1 in
3782 gotoy_and_clear_text y
3784 | 'o' ->
3785 enteroutlinemode ()
3787 | 'u' ->
3788 state.rects <- [];
3789 state.text <- "";
3790 G.postRedisplay "dehighlight";
3792 | '/' | '?' ->
3793 let ondone isforw s =
3794 cbput state.hists.pat s;
3795 state.searchpattern <- s;
3796 search s isforw
3798 let s = String.create 1 in
3799 s.[0] <- c;
3800 enttext (s, "", Some (onhist state.hists.pat),
3801 textentry, ondone (c ='/'))
3803 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3804 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3805 setzoom (conf.zoom +. incr)
3807 | '+' ->
3808 let ondone s =
3809 let n =
3810 try int_of_string s with exc ->
3811 state.text <- Printf.sprintf "bad integer `%s': %s"
3812 s (Printexc.to_string exc);
3813 max_int
3815 if n != max_int
3816 then (
3817 conf.pagebias <- n;
3818 state.text <- "page bias is now " ^ string_of_int n;
3821 enttext ("page bias: ", "", None, intentry, ondone)
3823 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3824 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3825 setzoom (max 0.01 (conf.zoom -. decr))
3827 | '-' ->
3828 let ondone msg = state.text <- msg in
3829 enttext (
3830 "option [acfhilpstvAPRSZTI]: ", "", None,
3831 optentry state.mode, ondone
3834 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3835 setzoom 1.0
3837 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3838 let zoom = zoomforh conf.winw conf.winh state.scrollw in
3839 if zoom < 1.0
3840 then setzoom zoom
3842 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3843 togglebirdseye ()
3845 | '0' .. '9' ->
3846 let ondone s =
3847 let n =
3848 try int_of_string s with exc ->
3849 state.text <- Printf.sprintf "bad integer `%s': %s"
3850 s (Printexc.to_string exc);
3853 if n >= 0
3854 then (
3855 addnav ();
3856 cbput state.hists.pag (string_of_int n);
3857 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
3860 let pageentry text key =
3861 match Char.unsafe_chr key with
3862 | 'g' -> TEdone text
3863 | _ -> intentry text key
3865 let text = "x" in text.[0] <- c;
3866 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
3868 | 'b' ->
3869 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
3870 reshape conf.winw conf.winh;
3872 | 'l' ->
3873 conf.hlinks <- not conf.hlinks;
3874 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3875 G.postRedisplay "toggle highlightlinks";
3877 | 'a' ->
3878 begin match state.autoscroll with
3879 | Some step ->
3880 conf.autoscrollstep <- step;
3881 state.autoscroll <- None
3882 | None ->
3883 if conf.autoscrollstep = 0
3884 then state.autoscroll <- Some 1
3885 else state.autoscroll <- Some conf.autoscrollstep
3888 | 'P' ->
3889 conf.presentation <- not conf.presentation;
3890 if conf.presentation
3891 then (
3892 if not conf.scrollbarinpm
3893 then state.scrollw <- 0;
3895 else
3896 state.scrollw <- conf.scrollbw;
3898 showtext ' ' ("presentation mode " ^
3899 if conf.presentation then "on" else "off");
3900 state.anchor <- getanchor ();
3901 represent ()
3903 | 'f' ->
3904 begin match state.fullscreen with
3905 | None ->
3906 state.fullscreen <- Some (conf.winw, conf.winh);
3907 Glut.fullScreen ()
3908 | Some (w, h) ->
3909 state.fullscreen <- None;
3910 doreshape w h
3913 | 'g' ->
3914 gotoy_and_clear_text 0
3916 | 'G' ->
3917 gotopage1 (state.pagecount - 1) 0
3919 | 'n' ->
3920 search state.searchpattern true
3922 | 'p' | 'N' ->
3923 search state.searchpattern false
3925 | 't' ->
3926 begin match state.layout with
3927 | [] -> ()
3928 | l :: _ ->
3929 gotoy_and_clear_text (getpagey l.pageno)
3932 | ' ' ->
3933 begin match List.rev state.layout with
3934 | [] -> ()
3935 | l :: _ ->
3936 let pageno = min (l.pageno+1) (state.pagecount-1) in
3937 gotoy_and_clear_text (getpagey pageno)
3940 | '\127' -> (* del *)
3941 begin match state.layout with
3942 | [] -> ()
3943 | l :: _ ->
3944 let pageno = max 0 (l.pageno-1) in
3945 gotoy_and_clear_text (getpagey pageno)
3948 | '=' ->
3949 showtext ' ' (describe_location ());
3951 | 'w' ->
3952 begin match state.layout with
3953 | [] -> ()
3954 | l :: _ ->
3955 doreshape (l.pagew + state.scrollw) l.pageh;
3956 G.postRedisplay "w"
3959 | '\'' ->
3960 enterbookmarkmode ()
3962 | 'h' ->
3963 enterhelpmode ()
3965 | 'i' ->
3966 enterinfomode ()
3968 | 'e' when conf.redirectstderr ->
3969 entermsgsmode ()
3971 | 'm' ->
3972 let ondone s =
3973 match state.layout with
3974 | l :: _ ->
3975 state.bookmarks <-
3976 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
3977 :: state.bookmarks
3978 | _ -> ()
3980 enttext ("bookmark: ", "", None, textentry, ondone)
3982 | '~' ->
3983 quickbookmark ();
3984 showtext ' ' "Quick bookmark added";
3986 | 'z' ->
3987 begin match state.layout with
3988 | l :: _ ->
3989 let rect = getpdimrect l.pagedimno in
3990 let w, h =
3991 if conf.crophack
3992 then
3993 (truncate (1.8 *. (rect.(1) -. rect.(0))),
3994 truncate (1.2 *. (rect.(3) -. rect.(0))))
3995 else
3996 (truncate (rect.(1) -. rect.(0)),
3997 truncate (rect.(3) -. rect.(0)))
3999 let w = truncate ((float w)*.conf.zoom)
4000 and h = truncate ((float h)*.conf.zoom) in
4001 if w != 0 && h != 0
4002 then (
4003 state.anchor <- getanchor ();
4004 doreshape (w + state.scrollw) (h + conf.interpagespace)
4006 G.postRedisplay "z";
4008 | [] -> ()
4011 | '\000' -> (* ctrl-2 *)
4012 let maxw = getmaxw () in
4013 if maxw > 0.0
4014 then setzoom (maxw /. float conf.winw)
4016 | '<' | '>' ->
4017 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
4019 | '[' | ']' ->
4020 conf.colorscale <-
4021 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
4023 G.postRedisplay "brightness";
4025 | 'k' ->
4026 begin match state.mode with
4027 | Birdseye beye -> upbirdseye beye
4028 | _ -> gotoy (clamp (-conf.scrollstep))
4031 | 'j' ->
4032 begin match state.mode with
4033 | Birdseye beye -> downbirdseye beye
4034 | _ -> gotoy (clamp conf.scrollstep)
4037 | 'r' ->
4038 state.anchor <- getanchor ();
4039 opendoc state.path state.password
4041 | 'v' when conf.debug ->
4042 state.rects <- [];
4043 List.iter (fun l ->
4044 match getopaque l.pageno with
4045 | None -> ()
4046 | Some opaque ->
4047 let x0, y0, x1, y1 = pagebbox opaque in
4048 let a,b = float x0, float y0 in
4049 let c,d = float x1, float y0 in
4050 let e,f = float x1, float y1 in
4051 let h,j = float x0, float y1 in
4052 let rect = (a,b,c,d,e,f,h,j) in
4053 debugrect rect;
4054 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4055 ) state.layout;
4056 G.postRedisplay "v";
4058 | _ ->
4059 vlog "huh? %d %c" key (Char.chr key);
4062 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
4063 match key with
4064 | 27 -> (* escape *)
4065 leavebirdseye beye true
4067 | 12 -> (* ctrl-l *)
4068 let y, h = getpageyh pageno in
4069 let top = (conf.winh - h) / 2 in
4070 gotoy (max 0 (y - top))
4072 | 13 -> (* enter *)
4073 leavebirdseye beye false
4075 | _ ->
4076 viewkeyboard key
4079 let keyboard ~key ~x ~y =
4080 ignore x;
4081 ignore y;
4082 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
4083 then wcmd "interrupt" []
4084 else state.uioh <- state.uioh#key key
4087 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
4088 match key with
4089 | Glut.KEY_UP -> upbirdseye beye
4090 | Glut.KEY_DOWN -> downbirdseye beye
4092 | Glut.KEY_PAGE_UP ->
4093 begin match state.layout with
4094 | l :: _ ->
4095 if l.pagey != 0
4096 then (
4097 state.mode <- Birdseye (
4098 conf, leftx, l.pageno, hooverpageno, anchor
4100 gotopage1 l.pageno 0;
4102 else (
4103 let layout = layout (state.y-conf.winh) conf.winh in
4104 match layout with
4105 | [] -> gotoy (clamp (-conf.winh))
4106 | l :: _ ->
4107 state.mode <- Birdseye (
4108 conf, leftx, l.pageno, hooverpageno, anchor
4110 gotopage1 l.pageno 0
4113 | [] -> gotoy (clamp (-conf.winh))
4114 end;
4116 | Glut.KEY_PAGE_DOWN ->
4117 begin match List.rev state.layout with
4118 | l :: _ ->
4119 let layout = layout (state.y + conf.winh) conf.winh in
4120 begin match layout with
4121 | [] ->
4122 let incr = l.pageh - l.pagevh in
4123 if incr = 0
4124 then (
4125 state.mode <-
4126 Birdseye (
4127 conf, leftx, state.pagecount - 1, hooverpageno, anchor
4129 G.postRedisplay "birdseye pagedown";
4131 else gotoy (clamp (incr + conf.interpagespace*2));
4133 | l :: _ ->
4134 state.mode <-
4135 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
4136 gotopage1 l.pageno 0;
4139 | [] -> gotoy (clamp conf.winh)
4140 end;
4142 | Glut.KEY_HOME ->
4143 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
4144 gotopage1 0 0
4146 | Glut.KEY_END ->
4147 let pageno = state.pagecount - 1 in
4148 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
4149 if not (pagevisible state.layout pageno)
4150 then
4151 let h =
4152 match List.rev state.pdims with
4153 | [] -> conf.winh
4154 | (_, _, h, _) :: _ -> h
4156 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4157 else G.postRedisplay "birdseye end";
4158 | _ -> ()
4161 let setautoscrollspeed step goingdown =
4162 let incr = max 1 ((abs step) / 2) in
4163 let incr = if goingdown then incr else -incr in
4164 let astep = step + incr in
4165 state.autoscroll <- Some astep;
4168 let special ~key ~x ~y =
4169 ignore x;
4170 ignore y;
4171 state.uioh <- state.uioh#special key
4174 let drawpage l =
4175 let color =
4176 match state.mode with
4177 | Textentry _ -> scalecolor 0.4
4178 | View -> scalecolor 1.0
4179 | Birdseye (_, _, pageno, hooverpageno, _) ->
4180 if l.pageno = hooverpageno
4181 then scalecolor 0.9
4182 else (
4183 if l.pageno = pageno
4184 then scalecolor 1.0
4185 else scalecolor 0.8
4188 drawtiles l color;
4189 begin match getopaque l.pageno with
4190 | Some opaque ->
4191 if tileready l l.pagex l.pagey
4192 then
4193 let x = l.pagedispx - l.pagex
4194 and y = l.pagedispy - l.pagey in
4195 postprocess opaque conf.hlinks x y;
4197 | _ -> ()
4198 end;
4201 let scrollindicator () =
4202 let sbw, ph, sh = state.uioh#scrollph in
4203 let sbh, pw, sw = state.uioh#scrollpw in
4205 GlDraw.color (0.64, 0.64, 0.64);
4206 GlDraw.rect
4207 (float (conf.winw - sbw), 0.)
4208 (float conf.winw, float conf.winh)
4210 GlDraw.rect
4211 (0., float (conf.winh - sbh))
4212 (float (conf.winw - state.scrollw - 1), float conf.winh)
4214 GlDraw.color (0.0, 0.0, 0.0);
4216 GlDraw.rect
4217 (float (conf.winw - sbw), ph)
4218 (float conf.winw, ph +. sh)
4220 GlDraw.rect
4221 (pw, float (conf.winh - sbh))
4222 (pw +. sw, float conf.winh)
4226 let pagetranslatepoint l x y =
4227 let dy = y - l.pagedispy in
4228 let y = dy + l.pagey in
4229 let dx = x - l.pagedispx in
4230 let x = dx + l.pagex in
4231 (x, y);
4234 let showsel () =
4235 match state.mstate with
4236 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4239 | Msel ((x0, y0), (x1, y1)) ->
4240 let rec loop = function
4241 | l :: ls ->
4242 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4243 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4244 then
4245 match getopaque l.pageno with
4246 | Some opaque ->
4247 let dx, dy = pagetranslatepoint l 0 0 in
4248 let x0 = x0 + dx
4249 and y0 = y0 + dy
4250 and x1 = x1 + dx
4251 and y1 = y1 + dy in
4252 GlMat.mode `modelview;
4253 GlMat.push ();
4254 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4255 seltext opaque (x0, y0, x1, y1);
4256 GlMat.pop ();
4257 | _ -> ()
4258 else loop ls
4259 | [] -> ()
4261 loop state.layout
4264 let showrects () =
4265 Gl.enable `blend;
4266 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4267 GlDraw.polygon_mode `both `fill;
4268 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4269 List.iter
4270 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4271 List.iter (fun l ->
4272 if l.pageno = pageno
4273 then (
4274 let dx = float (l.pagedispx - l.pagex) in
4275 let dy = float (l.pagedispy - l.pagey) in
4276 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4277 GlDraw.begins `quads;
4279 GlDraw.vertex2 (x0+.dx, y0+.dy);
4280 GlDraw.vertex2 (x1+.dx, y1+.dy);
4281 GlDraw.vertex2 (x2+.dx, y2+.dy);
4282 GlDraw.vertex2 (x3+.dx, y3+.dy);
4284 GlDraw.ends ();
4286 ) state.layout
4287 ) state.rects
4289 Gl.disable `blend;
4292 let display () =
4293 GlClear.color (scalecolor2 conf.bgcolor);
4294 GlClear.clear [`color];
4295 List.iter drawpage state.layout;
4296 showrects ();
4297 showsel ();
4298 state.uioh#display;
4299 scrollindicator ();
4300 begin match state.mstate with
4301 | Mzoomrect ((x0, y0), (x1, y1)) ->
4302 Gl.enable `blend;
4303 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4304 GlDraw.polygon_mode `both `fill;
4305 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4306 GlDraw.rect (float x0, float y0)
4307 (float x1, float y1);
4308 Gl.disable `blend;
4309 | _ -> ()
4310 end;
4311 enttext ();
4312 Glut.swapBuffers ();
4315 let getunder x y =
4316 let rec f = function
4317 | l :: rest ->
4318 begin match getopaque l.pageno with
4319 | Some opaque ->
4320 let x0 = l.pagedispx in
4321 let x1 = x0 + l.pagevw in
4322 let y0 = l.pagedispy in
4323 let y1 = y0 + l.pagevh in
4324 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4325 then
4326 let px, py = pagetranslatepoint l x y in
4327 match whatsunder opaque px py with
4328 | Unone -> f rest
4329 | under -> under
4330 else f rest
4331 | _ ->
4332 f rest
4334 | [] -> Unone
4336 f state.layout
4339 let zoomrect x y x1 y1 =
4340 let x0 = min x x1
4341 and x1 = max x x1
4342 and y0 = min y y1 in
4343 gotoy (state.y + y0);
4344 state.anchor <- getanchor ();
4345 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4346 let margin =
4347 if state.w < conf.winw - state.scrollw
4348 then (conf.winw - state.scrollw - state.w) / 2
4349 else 0
4351 state.x <- (state.x + margin) - x0;
4352 setzoom zoom;
4353 Glut.setCursor Glut.CURSOR_INHERIT;
4354 state.mstate <- Mnone;
4357 let scrollx x =
4358 let winw = conf.winw - state.scrollw - 1 in
4359 let s = float x /. float winw in
4360 let destx = truncate (float (state.w + winw) *. s) in
4361 state.x <- winw - destx;
4362 gotoy_and_clear_text state.y;
4363 state.mstate <- Mscrollx;
4366 let scrolly y =
4367 let s = float y /. float conf.winh in
4368 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4369 gotoy_and_clear_text desty;
4370 state.mstate <- Mscrolly;
4373 let viewmouse button bstate x y =
4374 match button with
4375 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4376 if Glut.getModifiers () land Glut.active_ctrl != 0
4377 then (
4378 match state.mstate with
4379 | Mzoom (oldn, i) ->
4380 if oldn = n
4381 then (
4382 if i = 2
4383 then
4384 let incr =
4385 match n with
4386 | 4 ->
4387 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4388 | _ ->
4389 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4391 let zoom = conf.zoom -. incr in
4392 setzoom zoom;
4393 state.mstate <- Mzoom (n, 0);
4394 else
4395 state.mstate <- Mzoom (n, i+1);
4397 else state.mstate <- Mzoom (n, 0)
4399 | _ -> state.mstate <- Mzoom (n, 0)
4401 else (
4402 match state.autoscroll with
4403 | Some step -> setautoscrollspeed step (n=4)
4404 | None ->
4405 let incr =
4406 if n = 3
4407 then -conf.scrollstep
4408 else conf.scrollstep
4410 let incr = incr * 2 in
4411 let y = clamp incr in
4412 gotoy_and_clear_text y
4415 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4416 if bstate = Glut.DOWN
4417 then (
4418 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4419 state.mstate <- Mpan (x, y)
4421 else
4422 state.mstate <- Mnone
4424 | Glut.RIGHT_BUTTON ->
4425 if bstate = Glut.DOWN
4426 then (
4427 Glut.setCursor Glut.CURSOR_CYCLE;
4428 let p = (x, y) in
4429 state.mstate <- Mzoomrect (p, p)
4431 else (
4432 match state.mstate with
4433 | Mzoomrect ((x0, y0), _) ->
4434 if abs (x-x0) > 10 && abs (y - y0) > 10
4435 then zoomrect x0 y0 x y
4436 else (
4437 state.mstate <- Mnone;
4438 Glut.setCursor Glut.CURSOR_INHERIT;
4439 G.postRedisplay "kill accidental zoom rect";
4441 | _ ->
4442 Glut.setCursor Glut.CURSOR_INHERIT;
4443 state.mstate <- Mnone
4446 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4447 if bstate = Glut.DOWN
4448 then
4449 let _, position, sh = state.uioh#scrollph in
4450 if y > truncate position && y < truncate (position +. sh)
4451 then state.mstate <- Mscrolly
4452 else scrolly y
4453 else
4454 state.mstate <- Mnone
4456 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4457 if bstate = Glut.DOWN
4458 then
4459 let _, position, sw = state.uioh#scrollpw in
4460 if x > truncate position && x < truncate (position +. sw)
4461 then state.mstate <- Mscrollx
4462 else scrollx x
4463 else
4464 state.mstate <- Mnone
4466 | Glut.LEFT_BUTTON ->
4467 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4468 begin match dest with
4469 | Ulinkgoto (pageno, top) ->
4470 if pageno >= 0
4471 then (
4472 addnav ();
4473 gotopage1 pageno top;
4476 | Ulinkuri s ->
4477 gotouri s
4479 | Unone when bstate = Glut.DOWN ->
4480 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4481 state.mstate <- Mpan (x, y);
4483 | Unone | Utext _ ->
4484 if bstate = Glut.DOWN
4485 then (
4486 if conf.angle mod 360 = 0
4487 then (
4488 state.mstate <- Msel ((x, y), (x, y));
4489 G.postRedisplay "mouse select";
4492 else (
4493 match state.mstate with
4494 | Mnone -> ()
4496 | Mzoom _ | Mscrollx | Mscrolly ->
4497 state.mstate <- Mnone
4499 | Mzoomrect ((x0, y0), _) ->
4500 zoomrect x0 y0 x y
4502 | Mpan _ ->
4503 Glut.setCursor Glut.CURSOR_INHERIT;
4504 state.mstate <- Mnone
4506 | Msel ((_, y0), (_, y1)) ->
4507 let f l =
4508 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4509 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4510 then
4511 match getopaque l.pageno with
4512 | Some opaque ->
4513 copysel opaque
4514 | _ -> ()
4516 List.iter f state.layout;
4517 copysel ""; (* ugly *)
4518 Glut.setCursor Glut.CURSOR_INHERIT;
4519 state.mstate <- Mnone;
4523 | _ -> ()
4526 let birdseyemouse button bstate x y
4527 (conf, leftx, _, hooverpageno, anchor) =
4528 match button with
4529 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4530 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4531 let rec loop = function
4532 | [] -> ()
4533 | l :: rest ->
4534 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4535 && x > margin && x < margin + l.pagew
4536 then (
4537 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4539 else loop rest
4541 loop state.layout
4542 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4543 | _ -> ()
4546 let mouse bstate button x y =
4547 state.uioh <- state.uioh#button button bstate x y;
4550 let mouse ~button ~state ~x ~y = mouse state button x y;;
4552 let motion ~x ~y =
4553 state.uioh <- state.uioh#motion x y
4556 let pmotion ~x ~y =
4557 state.uioh <- state.uioh#pmotion x y;
4560 let uioh = object
4561 method display = ()
4563 method key key =
4564 begin match state.mode with
4565 | Textentry textentry -> textentrykeyboard key textentry
4566 | Birdseye birdseye -> birdseyekeyboard key birdseye
4567 | View -> viewkeyboard key
4568 end;
4569 state.uioh
4571 method special key =
4572 begin match state.mode with
4573 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4574 togglebirdseye ()
4576 | Birdseye vals ->
4577 birdseyespecial key vals
4579 | View when key = Glut.KEY_F1 ->
4580 enterhelpmode ()
4582 | View ->
4583 begin match state.autoscroll with
4584 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4585 setautoscrollspeed step (key = Glut.KEY_DOWN)
4587 | _ ->
4588 let y =
4589 match key with
4590 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4591 | Glut.KEY_UP ->
4592 if Glut.getModifiers () land Glut.active_ctrl != 0
4593 then
4594 if Glut.getModifiers () land Glut.active_shift != 0
4595 then (setzoom state.prevzoom; state.y)
4596 else clamp (-conf.winh/2)
4597 else clamp (-conf.scrollstep)
4598 | Glut.KEY_DOWN ->
4599 if Glut.getModifiers () land Glut.active_ctrl != 0
4600 then
4601 if Glut.getModifiers () land Glut.active_shift != 0
4602 then (setzoom state.prevzoom; state.y)
4603 else clamp (conf.winh/2)
4604 else clamp (conf.scrollstep)
4605 | Glut.KEY_PAGE_UP ->
4606 if Glut.getModifiers () land Glut.active_ctrl != 0
4607 then
4608 match state.layout with
4609 | [] -> state.y
4610 | l :: _ -> state.y - l.pagey
4611 else
4612 clamp (-conf.winh)
4613 | Glut.KEY_PAGE_DOWN ->
4614 if Glut.getModifiers () land Glut.active_ctrl != 0
4615 then
4616 match List.rev state.layout with
4617 | [] -> state.y
4618 | l :: _ -> getpagey l.pageno
4619 else
4620 clamp conf.winh
4621 | Glut.KEY_HOME ->
4622 addnav ();
4624 | Glut.KEY_END ->
4625 addnav ();
4626 state.maxy - (if conf.maxhfit then conf.winh else 0)
4628 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4629 Glut.getModifiers () land Glut.active_alt != 0 ->
4630 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4632 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4633 let dx =
4634 if Glut.getModifiers () land Glut.active_ctrl != 0
4635 then (conf.winw / 2)
4636 else 10
4638 state.x <- state.x - dx;
4639 state.y
4640 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4641 let dx =
4642 if Glut.getModifiers () land Glut.active_ctrl != 0
4643 then (conf.winw / 2)
4644 else 10
4646 state.x <- state.x + dx;
4647 state.y
4649 | _ -> state.y
4651 gotoy_and_clear_text y
4654 | Textentry te -> textentryspecial key te
4655 end;
4656 state.uioh
4658 method button button bstate x y =
4659 begin match state.mode with
4660 | View -> viewmouse button bstate x y
4661 | Birdseye beye -> birdseyemouse button bstate x y beye
4662 | Textentry _ -> ()
4663 end;
4664 state.uioh
4666 method motion x y =
4667 begin match state.mode with
4668 | Textentry _ -> ()
4669 | View | Birdseye _ ->
4670 match state.mstate with
4671 | Mzoom _ | Mnone -> ()
4673 | Mpan (x0, y0) ->
4674 let dx = x - x0
4675 and dy = y0 - y in
4676 state.mstate <- Mpan (x, y);
4677 if conf.zoom > 1.0 then state.x <- state.x + dx;
4678 let y = clamp dy in
4679 gotoy_and_clear_text y
4681 | Msel (a, _) ->
4682 state.mstate <- Msel (a, (x, y));
4683 G.postRedisplay "motion select";
4685 | Mscrolly ->
4686 let y = min conf.winh (max 0 y) in
4687 scrolly y
4689 | Mscrollx ->
4690 let x = min conf.winw (max 0 x) in
4691 scrollx x
4693 | Mzoomrect (p0, _) ->
4694 state.mstate <- Mzoomrect (p0, (x, y));
4695 G.postRedisplay "motion zoomrect";
4696 end;
4697 state.uioh
4699 method pmotion x y =
4700 begin match state.mode with
4701 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4702 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4703 let rec loop = function
4704 | [] ->
4705 if hooverpageno != -1
4706 then (
4707 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4708 G.postRedisplay "pmotion birdseye no hoover";
4710 | l :: rest ->
4711 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4712 && x > margin && x < margin + l.pagew
4713 then (
4714 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4715 G.postRedisplay "pmotion birdseye hoover";
4717 else loop rest
4719 loop state.layout
4721 | Textentry _ -> ()
4723 | View ->
4724 match state.mstate with
4725 | Mnone ->
4726 begin match getunder x y with
4727 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
4728 | Ulinkuri uri ->
4729 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
4730 Glut.setCursor Glut.CURSOR_INFO
4731 | Ulinkgoto (page, _) ->
4732 if conf.underinfo
4733 then showtext 'p' ("age: " ^ string_of_int (page+1));
4734 Glut.setCursor Glut.CURSOR_INFO
4735 | Utext s ->
4736 if conf.underinfo then showtext 'f' ("ont: " ^ s);
4737 Glut.setCursor Glut.CURSOR_TEXT
4740 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
4742 end;
4743 state.uioh
4745 method infochanged _ = ()
4747 method scrollph =
4748 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
4749 let p, h = scrollph state.y maxy in
4750 state.scrollw, p, h
4752 method scrollpw =
4753 let winw = conf.winw - state.scrollw - 1 in
4754 let fwinw = float winw in
4755 let sw =
4756 let sw = fwinw /. float state.w in
4757 let sw = fwinw *. sw in
4758 max sw (float conf.scrollh)
4760 let position, sw =
4761 let f = state.w+winw in
4762 let r = float (winw-state.x) /. float f in
4763 let p = fwinw *. r in
4764 p-.sw/.2., sw
4766 let sw =
4767 if position +. sw > fwinw
4768 then fwinw -. position
4769 else sw
4771 state.hscrollh, position, sw
4772 end;;
4774 module Config =
4775 struct
4776 open Parser
4778 let fontpath = ref "";;
4779 let wmclasshack = ref false;;
4781 let unent s =
4782 let l = String.length s in
4783 let b = Buffer.create l in
4784 unent b s 0 l;
4785 Buffer.contents b;
4788 let home =
4790 match platform with
4791 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
4792 | _ -> Sys.getenv "HOME"
4793 with exn ->
4794 prerr_endline
4795 ("Can not determine home directory location: " ^
4796 Printexc.to_string exn);
4800 let config_of c attrs =
4801 let apply c k v =
4803 match k with
4804 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
4805 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
4806 | "case-insensitive-search" -> { c with icase = bool_of_string v }
4807 | "preload" -> { c with preload = bool_of_string v }
4808 | "page-bias" -> { c with pagebias = int_of_string v }
4809 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
4810 | "auto-scroll-step" ->
4811 { c with autoscrollstep = max 0 (int_of_string v) }
4812 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
4813 | "crop-hack" -> { c with crophack = bool_of_string v }
4814 | "throttle" ->
4815 let mw =
4816 match String.lowercase v with
4817 | "true" -> Some infinity
4818 | "false" -> None
4819 | f -> Some (float_of_string f)
4821 { c with maxwait = mw}
4822 | "highlight-links" -> { c with hlinks = bool_of_string v }
4823 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
4824 | "vertical-margin" ->
4825 { c with interpagespace = max 0 (int_of_string v) }
4826 | "zoom" ->
4827 let zoom = float_of_string v /. 100. in
4828 let zoom = max zoom 0.0 in
4829 { c with zoom = zoom }
4830 | "presentation" -> { c with presentation = bool_of_string v }
4831 | "rotation-angle" -> { c with angle = int_of_string v }
4832 | "width" -> { c with winw = max 20 (int_of_string v) }
4833 | "height" -> { c with winh = max 20 (int_of_string v) }
4834 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
4835 | "proportional-display" -> { c with proportional = bool_of_string v }
4836 | "pixmap-cache-size" ->
4837 { c with memlimit = max 2 (int_of_string_with_suffix v) }
4838 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
4839 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
4840 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
4841 | "persistent-location" -> { c with jumpback = bool_of_string v }
4842 | "background-color" -> { c with bgcolor = color_of_string v }
4843 | "scrollbar-in-presentation" ->
4844 { c with scrollbarinpm = bool_of_string v }
4845 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
4846 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
4847 | "mupdf-memlimit" ->
4848 { c with mumemlimit = max 1024 (int_of_string_with_suffix v) }
4849 | "checkers" -> { c with checkers = bool_of_string v }
4850 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
4851 | "trim-margins" -> { c with trimmargins = bool_of_string v }
4852 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
4853 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
4854 | "uri-launcher" -> { c with urilauncher = unent v }
4855 | "color-space" -> { c with colorspace = colorspace_of_string v }
4856 | "invert-colors" -> { c with invert = bool_of_string v }
4857 | "brightness" -> { c with colorscale = float_of_string v }
4858 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
4859 | _ -> c
4860 with exn ->
4861 prerr_endline ("Error processing attribute (`" ^
4862 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
4865 let rec fold c = function
4866 | [] -> c
4867 | (k, v) :: rest ->
4868 let c = apply c k v in
4869 fold c rest
4871 fold c attrs;
4874 let fromstring f pos n v d =
4875 try f v
4876 with exn ->
4877 dolog "Error processing attribute (%S=%S) at %d\n%s"
4878 n v pos (Printexc.to_string exn)
4883 let bookmark_of attrs =
4884 let rec fold title page rely = function
4885 | ("title", v) :: rest -> fold v page rely rest
4886 | ("page", v) :: rest -> fold title v rely rest
4887 | ("rely", v) :: rest -> fold title page v rest
4888 | _ :: rest -> fold title page rely rest
4889 | [] -> title, page, rely
4891 fold "invalid" "0" "0" attrs
4894 let doc_of attrs =
4895 let rec fold path page rely pan = function
4896 | ("path", v) :: rest -> fold v page rely pan rest
4897 | ("page", v) :: rest -> fold path v rely pan rest
4898 | ("rely", v) :: rest -> fold path page v pan rest
4899 | ("pan", v) :: rest -> fold path page rely v rest
4900 | _ :: rest -> fold path page rely pan rest
4901 | [] -> path, page, rely, pan
4903 fold "" "0" "0" "0" attrs
4906 let setconf dst src =
4907 dst.scrollbw <- src.scrollbw;
4908 dst.scrollh <- src.scrollh;
4909 dst.icase <- src.icase;
4910 dst.preload <- src.preload;
4911 dst.pagebias <- src.pagebias;
4912 dst.verbose <- src.verbose;
4913 dst.scrollstep <- src.scrollstep;
4914 dst.maxhfit <- src.maxhfit;
4915 dst.crophack <- src.crophack;
4916 dst.autoscrollstep <- src.autoscrollstep;
4917 dst.maxwait <- src.maxwait;
4918 dst.hlinks <- src.hlinks;
4919 dst.underinfo <- src.underinfo;
4920 dst.interpagespace <- src.interpagespace;
4921 dst.zoom <- src.zoom;
4922 dst.presentation <- src.presentation;
4923 dst.angle <- src.angle;
4924 dst.winw <- src.winw;
4925 dst.winh <- src.winh;
4926 dst.savebmarks <- src.savebmarks;
4927 dst.memlimit <- src.memlimit;
4928 dst.proportional <- src.proportional;
4929 dst.texcount <- src.texcount;
4930 dst.sliceheight <- src.sliceheight;
4931 dst.thumbw <- src.thumbw;
4932 dst.jumpback <- src.jumpback;
4933 dst.bgcolor <- src.bgcolor;
4934 dst.scrollbarinpm <- src.scrollbarinpm;
4935 dst.tilew <- src.tilew;
4936 dst.tileh <- src.tileh;
4937 dst.mumemlimit <- src.mumemlimit;
4938 dst.checkers <- src.checkers;
4939 dst.aalevel <- src.aalevel;
4940 dst.trimmargins <- src.trimmargins;
4941 dst.trimfuzz <- src.trimfuzz;
4942 dst.urilauncher <- src.urilauncher;
4943 dst.colorspace <- src.colorspace;
4944 dst.invert <- src.invert;
4945 dst.colorscale <- src.colorscale;
4946 dst.redirectstderr <- src.redirectstderr;
4949 let get s =
4950 let h = Hashtbl.create 10 in
4951 let dc = { defconf with angle = defconf.angle } in
4952 let rec toplevel v t spos _ =
4953 match t with
4954 | Vdata | Vcdata | Vend -> v
4955 | Vopen ("llppconfig", _, closed) ->
4956 if closed
4957 then v
4958 else { v with f = llppconfig }
4959 | Vopen _ ->
4960 error "unexpected subelement at top level" s spos
4961 | Vclose _ -> error "unexpected close at top level" s spos
4963 and llppconfig v t spos _ =
4964 match t with
4965 | Vdata | Vcdata -> v
4966 | Vend -> error "unexpected end of input in llppconfig" s spos
4967 | Vopen ("defaults", attrs, closed) ->
4968 let c = config_of dc attrs in
4969 setconf dc c;
4970 if closed
4971 then v
4972 else { v with f = skip "defaults" (fun () -> v) }
4974 | Vopen ("ui-font", attrs, closed) ->
4975 let rec getsize size = function
4976 | [] -> size
4977 | ("size", v) :: rest ->
4978 let size =
4979 fromstring int_of_string spos "size" v fstate.fontsize in
4980 getsize size rest
4981 | l -> getsize size l
4983 fstate.fontsize <- getsize fstate.fontsize attrs;
4984 if closed
4985 then v
4986 else { v with f = uifont (Buffer.create 10) }
4988 | Vopen ("doc", attrs, closed) ->
4989 let pathent, spage, srely, span = doc_of attrs in
4990 let path = unent pathent
4991 and pageno = fromstring int_of_string spos "page" spage 0
4992 and rely = fromstring float_of_string spos "rely" srely 0.0
4993 and pan = fromstring int_of_string spos "pan" span 0 in
4994 let c = config_of dc attrs in
4995 let anchor = (pageno, rely) in
4996 if closed
4997 then (Hashtbl.add h path (c, [], pan, anchor); v)
4998 else { v with f = doc path pan anchor c [] }
5000 | Vopen _ ->
5001 error "unexpected subelement in llppconfig" s spos
5003 | Vclose "llppconfig" -> { v with f = toplevel }
5004 | Vclose _ -> error "unexpected close in llppconfig" s spos
5006 and uifont b v t spos epos =
5007 match t with
5008 | Vdata | Vcdata ->
5009 Buffer.add_substring b s spos (epos - spos);
5011 | Vopen (_, _, _) ->
5012 error "unexpected subelement in ui-font" s spos
5013 | Vclose "ui-font" ->
5014 if String.length !fontpath = 0
5015 then fontpath := Buffer.contents b;
5016 { v with f = llppconfig }
5017 | Vclose _ -> error "unexpected close in ui-font" s spos
5018 | Vend -> error "unexpected end of input in ui-font" s spos
5020 and doc path pan anchor c bookmarks v t spos _ =
5021 match t with
5022 | Vdata | Vcdata -> v
5023 | Vend -> error "unexpected end of input in doc" s spos
5024 | Vopen ("bookmarks", _, closed) ->
5025 if closed
5026 then v
5027 else { v with f = pbookmarks path pan anchor c bookmarks }
5029 | Vopen (_, _, _) ->
5030 error "unexpected subelement in doc" s spos
5032 | Vclose "doc" ->
5033 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5034 { v with f = llppconfig }
5036 | Vclose _ -> error "unexpected close in doc" s spos
5038 and pbookmarks path pan anchor c bookmarks v t spos _ =
5039 match t with
5040 | Vdata | Vcdata -> v
5041 | Vend -> error "unexpected end of input in bookmarks" s spos
5042 | Vopen ("item", attrs, closed) ->
5043 let titleent, spage, srely = bookmark_of attrs in
5044 let page = fromstring int_of_string spos "page" spage 0
5045 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5046 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5047 if closed
5048 then { v with f = pbookmarks path pan anchor c bookmarks }
5049 else
5050 let f () = v in
5051 { v with f = skip "item" f }
5053 | Vopen _ ->
5054 error "unexpected subelement in bookmarks" s spos
5056 | Vclose "bookmarks" ->
5057 { v with f = doc path pan anchor c bookmarks }
5059 | Vclose _ -> error "unexpected close in bookmarks" s spos
5061 and skip tag f v t spos _ =
5062 match t with
5063 | Vdata | Vcdata -> v
5064 | Vend ->
5065 error ("unexpected end of input in skipped " ^ tag) s spos
5066 | Vopen (tag', _, closed) ->
5067 if closed
5068 then v
5069 else
5070 let f' () = { v with f = skip tag f } in
5071 { v with f = skip tag' f' }
5072 | Vclose ctag ->
5073 if tag = ctag
5074 then f ()
5075 else error ("unexpected close in skipped " ^ tag) s spos
5078 parse { f = toplevel; accu = () } s;
5079 h, dc;
5082 let do_load f ic =
5084 let len = in_channel_length ic in
5085 let s = String.create len in
5086 really_input ic s 0 len;
5087 f s;
5088 with
5089 | Parse_error (msg, s, pos) ->
5090 let subs = subs s pos in
5091 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5092 failwith ("parse error: " ^ s)
5094 | exn ->
5095 failwith ("config load error: " ^ Printexc.to_string exn)
5098 let defconfpath =
5099 let dir =
5101 let dir = Filename.concat home ".config" in
5102 if Sys.is_directory dir then dir else home
5103 with _ -> home
5105 Filename.concat dir "llpp.conf"
5108 let confpath = ref defconfpath;;
5110 let load1 f =
5111 if Sys.file_exists !confpath
5112 then
5113 match
5114 (try Some (open_in_bin !confpath)
5115 with exn ->
5116 prerr_endline
5117 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5118 Printexc.to_string exn);
5119 None
5121 with
5122 | Some ic ->
5123 begin try
5124 f (do_load get ic)
5125 with exn ->
5126 prerr_endline
5127 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5128 Printexc.to_string exn);
5129 end;
5130 close_in ic;
5132 | None -> ()
5133 else
5134 f (Hashtbl.create 0, defconf)
5137 let load () =
5138 let f (h, dc) =
5139 let pc, pb, px, pa =
5141 Hashtbl.find h (Filename.basename state.path)
5142 with Not_found -> dc, [], 0, (0, 0.0)
5144 setconf defconf dc;
5145 setconf conf pc;
5146 state.bookmarks <- pb;
5147 state.x <- px;
5148 state.scrollw <- conf.scrollbw;
5149 if conf.jumpback
5150 then state.anchor <- pa;
5151 cbput state.hists.nav pa;
5153 load1 f
5156 let add_attrs bb always dc c =
5157 let ob s a b =
5158 if always || a != b
5159 then Printf.bprintf bb "\n %s='%b'" s a
5160 and oi s a b =
5161 if always || a != b
5162 then Printf.bprintf bb "\n %s='%d'" s a
5163 and oI s a b =
5164 if always || a != b
5165 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5166 and oz s a b =
5167 if always || a <> b
5168 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5169 and oF s a b =
5170 if always || a <> b
5171 then Printf.bprintf bb "\n %s='%f'" s a
5172 and oc s a b =
5173 if always || a <> b
5174 then
5175 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5176 and oC s a b =
5177 if always || a <> b
5178 then
5179 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5180 and oR s a b =
5181 if always || a <> b
5182 then
5183 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5184 and os s a b =
5185 if always || a <> b
5186 then
5187 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5188 and oW s a b =
5189 if always || a <> b
5190 then
5191 let v =
5192 match a with
5193 | None -> "false"
5194 | Some f ->
5195 if f = infinity
5196 then "true"
5197 else string_of_float f
5199 Printf.bprintf bb "\n %s='%s'" s v
5201 let w, h =
5202 if always
5203 then dc.winw, dc.winh
5204 else
5205 match state.fullscreen with
5206 | Some wh -> wh
5207 | None -> c.winw, c.winh
5209 let zoom, presentation, interpagespace, maxwait =
5210 if always
5211 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5212 else
5213 match state.mode with
5214 | Birdseye (bc, _, _, _, _) ->
5215 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5216 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5218 oi "width" w dc.winw;
5219 oi "height" h dc.winh;
5220 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5221 oi "scroll-handle-height" c.scrollh dc.scrollh;
5222 ob "case-insensitive-search" c.icase dc.icase;
5223 ob "preload" c.preload dc.preload;
5224 oi "page-bias" c.pagebias dc.pagebias;
5225 oi "scroll-step" c.scrollstep dc.scrollstep;
5226 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5227 ob "max-height-fit" c.maxhfit dc.maxhfit;
5228 ob "crop-hack" c.crophack dc.crophack;
5229 oW "throttle" maxwait dc.maxwait;
5230 ob "highlight-links" c.hlinks dc.hlinks;
5231 ob "under-cursor-info" c.underinfo dc.underinfo;
5232 oi "vertical-margin" interpagespace dc.interpagespace;
5233 oz "zoom" zoom dc.zoom;
5234 ob "presentation" presentation dc.presentation;
5235 oi "rotation-angle" c.angle dc.angle;
5236 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5237 ob "proportional-display" c.proportional dc.proportional;
5238 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5239 oi "tex-count" c.texcount dc.texcount;
5240 oi "slice-height" c.sliceheight dc.sliceheight;
5241 oi "thumbnail-width" c.thumbw dc.thumbw;
5242 ob "persistent-location" c.jumpback dc.jumpback;
5243 oc "background-color" c.bgcolor dc.bgcolor;
5244 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5245 oi "tile-width" c.tilew dc.tilew;
5246 oi "tile-height" c.tileh dc.tileh;
5247 oI "mupdf-memlimit" c.mumemlimit dc.mumemlimit;
5248 ob "checkers" c.checkers dc.checkers;
5249 oi "aalevel" c.aalevel dc.aalevel;
5250 ob "trim-margins" c.trimmargins dc.trimmargins;
5251 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5252 os "uri-launcher" c.urilauncher dc.urilauncher;
5253 oC "color-space" c.colorspace dc.colorspace;
5254 ob "invert-colors" c.invert dc.invert;
5255 oF "brightness" c.colorscale dc.colorscale;
5256 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5257 if always
5258 then ob "wmclass-hack" !wmclasshack false;
5261 let save () =
5262 let uifontsize = fstate.fontsize in
5263 let bb = Buffer.create 32768 in
5264 let f (h, dc) =
5265 let dc = if conf.bedefault then conf else dc in
5266 Buffer.add_string bb "<llppconfig>\n";
5268 if String.length !fontpath > 0
5269 then
5270 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5271 uifontsize
5272 !fontpath
5273 else (
5274 if uifontsize <> 14
5275 then
5276 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5279 Buffer.add_string bb "<defaults ";
5280 add_attrs bb true dc dc;
5281 Buffer.add_string bb "/>\n";
5283 let adddoc path pan anchor c bookmarks =
5284 if bookmarks == [] && c = dc && anchor = emptyanchor
5285 then ()
5286 else (
5287 Printf.bprintf bb "<doc path='%s'"
5288 (enent path 0 (String.length path));
5290 if anchor <> emptyanchor
5291 then (
5292 let n, y = anchor in
5293 Printf.bprintf bb " page='%d'" n;
5294 if y > 1e-6
5295 then
5296 Printf.bprintf bb " rely='%f'" y
5300 if pan != 0
5301 then Printf.bprintf bb " pan='%d'" pan;
5303 add_attrs bb false dc c;
5305 begin match bookmarks with
5306 | [] -> Buffer.add_string bb "/>\n"
5307 | _ ->
5308 Buffer.add_string bb ">\n<bookmarks>\n";
5309 List.iter (fun (title, _level, (page, rely)) ->
5310 Printf.bprintf bb
5311 "<item title='%s' page='%d'"
5312 (enent title 0 (String.length title))
5313 page
5315 if rely > 1e-6
5316 then
5317 Printf.bprintf bb " rely='%f'" rely
5319 Buffer.add_string bb "/>\n";
5320 ) bookmarks;
5321 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5322 end;
5326 let pan =
5327 match state.mode with
5328 | Birdseye (_, pan, _, _, _) -> pan
5329 | _ -> state.x
5331 let basename = Filename.basename state.path in
5332 adddoc basename pan (getanchor ())
5333 { conf with
5334 autoscrollstep =
5335 match state.autoscroll with
5336 | Some step -> step
5337 | None -> conf.autoscrollstep }
5338 (if conf.savebmarks then state.bookmarks else []);
5340 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5341 if basename <> path
5342 then adddoc path x y c bookmarks
5343 ) h;
5344 Buffer.add_string bb "</llppconfig>";
5346 load1 f;
5347 if Buffer.length bb > 0
5348 then
5350 let tmp = !confpath ^ ".tmp" in
5351 let oc = open_out_bin tmp in
5352 Buffer.output_buffer oc bb;
5353 close_out oc;
5354 Unix.rename tmp !confpath;
5355 with exn ->
5356 prerr_endline
5357 ("error while saving configuration: " ^ Printexc.to_string exn)
5359 end;;
5361 let () =
5362 Arg.parse
5363 (Arg.align
5364 [("-p", Arg.String (fun s -> state.password <- s) ,
5365 "<password> Set password");
5367 ("-f", Arg.String (fun s -> Config.fontpath := s),
5368 "<path> Set path to the user interface font");
5370 ("-c", Arg.String (fun s -> Config.confpath := s),
5371 "<path> Set path to the configuration file");
5373 ("-v", Arg.Unit (fun () ->
5374 Printf.printf
5375 "%s\nconfiguration path: %s\n"
5376 (version ())
5377 Config.defconfpath
5379 exit 0), " Print version and exit");
5382 (fun s -> state.path <- s)
5383 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5385 if String.length state.path = 0
5386 then (prerr_endline "file name missing"; exit 1);
5388 Config.load ();
5390 let _ = Glut.init Sys.argv in
5391 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5392 let () = Glut.initWindowSize conf.winw conf.winh in
5393 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5395 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5396 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5397 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5399 let csock, ssock =
5400 if not is_windows
5401 then
5402 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5403 else
5404 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5405 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5406 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5407 Unix.bind sock addr;
5408 Unix.listen sock 1;
5409 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5410 Unix.connect csock addr;
5411 let ssock, _ = Unix.accept sock in
5412 Unix.close sock;
5413 let opts sock =
5414 Unix.setsockopt sock Unix.TCP_NODELAY true;
5415 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5417 opts ssock;
5418 opts csock;
5419 ssock, csock
5422 let () = Glut.displayFunc display in
5423 let () = Glut.reshapeFunc reshape in
5424 let () = Glut.keyboardFunc keyboard in
5425 let () = Glut.specialFunc special in
5426 let () = Glut.idleFunc (Some idle) in
5427 let () = Glut.mouseFunc mouse in
5428 let () = Glut.motionFunc motion in
5429 let () = Glut.passiveMotionFunc pmotion in
5431 setcheckers conf.checkers;
5432 init ssock (
5433 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5434 conf.texcount, conf.sliceheight, conf.mumemlimit, conf.colorspace,
5435 !Config.wmclasshack, !Config.fontpath
5437 state.csock <- csock;
5438 state.ssock <- ssock;
5439 state.text <- "Opening " ^ state.path;
5440 setaalevel conf.aalevel;
5441 writeopen state.path state.password;
5442 state.uioh <- uioh;
5443 setfontsize fstate.fontsize;
5445 redirectstderr ();
5447 while true do
5449 Glut.mainLoop ();
5450 with
5451 | Glut.BadEnum "key in special_of_int" ->
5452 showtext '!' " LablGlut bug: special key not recognized";
5454 | Quit ->
5455 wcmd "quit" [];
5456 Config.save ();
5457 exit 0
5459 | exn when conf.redirectstderr ->
5460 let s =
5461 Printf.sprintf "exception %s\n%s"
5462 (Printexc.to_string exn)
5463 (Printexc.get_backtrace ())
5465 ignore (try
5466 Unix.single_write state.stderr s 0 (String.length s);
5467 with _ -> 0);
5468 exit 1
5469 done;