Track dimensions/pagecount info properly
[llpp.git] / main.ml
blob30178228b4f32e047cb65ada8107a1700d3135c5
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 src#caption2 "Pages"
3602 (fun () -> string_of_int state.pagecount) 1;
3603 src#caption2 "Dimensions"
3604 (fun () -> string_of_int (List.length state.pdims)) 1;
3605 if conf.trimmargins
3606 then (
3607 sep ();
3608 src#caption "Trimmed margins" 0;
3609 src#caption2 "Dimensions"
3610 (fun () -> string_of_int (List.length state.pdims)) 1;
3613 src#reset prevmode prevuioh;
3615 fun () ->
3616 state.text <- "";
3617 let prevmode = state.mode
3618 and prevuioh = state.uioh in
3619 fillsrc prevmode prevuioh;
3620 let source = (src :> lvsource) in
3621 state.uioh <- coe (object (self)
3622 inherit listview ~source ~trusted:true as super
3623 val mutable m_prevmemused = 0
3624 method infochanged = function
3625 | Memused ->
3626 if m_prevmemused != state.memused
3627 then (
3628 m_prevmemused <- state.memused;
3629 G.postRedisplay "memusedchanged";
3631 | Pdim -> G.postRedisplay "pdimchanged"
3632 | Docinfo -> fillsrc prevmode prevuioh
3634 method special key =
3635 if Glut.getModifiers () land Glut.active_ctrl = 0
3636 then
3637 match key with
3638 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
3639 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
3640 | _ -> super#special key
3641 else super#special key
3642 end);
3643 G.postRedisplay "info";
3646 let enterhelpmode =
3647 let source =
3648 (object
3649 inherit lvsourcebase
3650 method getitemcount = Array.length state.help
3651 method getitem n =
3652 let s, n, _ = state.help.(n) in
3653 (s, n)
3655 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3656 let optuioh =
3657 if not cancel
3658 then (
3659 m_qsearch <- qsearch;
3660 match state.help.(active) with
3661 | _, _, Action f -> Some (f uioh)
3662 | _ -> Some (uioh)
3664 else None
3666 m_active <- active;
3667 m_first <- first;
3668 m_pan <- pan;
3669 optuioh
3671 method hasaction n =
3672 match state.help.(n) with
3673 | _, _, Action _ -> true
3674 | _ -> false
3676 initializer
3677 m_active <- -1
3678 end)
3679 in fun () ->
3680 state.uioh <- coe (new listview ~source ~trusted:true);
3681 G.postRedisplay "help";
3684 let entermsgsmode =
3685 let msgsource =
3686 let re = Str.regexp "[\r\n]" in
3687 (object
3688 inherit lvsourcebase
3689 val mutable m_items = [||]
3691 method getitemcount = 1 + Array.length m_items
3693 method getitem n =
3694 if n = 0
3695 then "[Clear]", 0
3696 else m_items.(n-1), 0
3698 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3699 ignore uioh;
3700 if not cancel
3701 then (
3702 if active = 0
3703 then Buffer.clear state.errmsgs;
3704 m_qsearch <- qsearch;
3706 m_active <- active;
3707 m_first <- first;
3708 m_pan <- pan;
3709 None
3711 method hasaction n =
3712 n = 0
3714 method reset =
3715 state.newerrmsgs <- false;
3716 let l = Str.split re (Buffer.contents state.errmsgs) in
3717 m_items <- Array.of_list l
3719 initializer
3720 m_active <- 0
3721 end)
3722 in fun () ->
3723 state.text <- "";
3724 msgsource#reset;
3725 let source = (msgsource :> lvsource) in
3726 state.uioh <- coe (object
3727 inherit listview ~source ~trusted:false as super
3728 method display =
3729 if state.newerrmsgs
3730 then msgsource#reset;
3731 super#display
3732 end);
3733 G.postRedisplay "msgs";
3736 let quickbookmark ?title () =
3737 match state.layout with
3738 | [] -> ()
3739 | l :: _ ->
3740 let title =
3741 match title with
3742 | None ->
3743 let sec = Unix.gettimeofday () in
3744 let tm = Unix.localtime sec in
3745 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
3746 (l.pageno+1)
3747 tm.Unix.tm_mday
3748 tm.Unix.tm_mon
3749 (tm.Unix.tm_year + 1900)
3750 tm.Unix.tm_hour
3751 tm.Unix.tm_min
3752 | Some title -> title
3754 state.bookmarks <-
3755 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
3756 :: state.bookmarks
3759 let doreshape w h =
3760 state.fullscreen <- None;
3761 Glut.reshapeWindow w h;
3764 let viewkeyboard key =
3765 let enttext te =
3766 let mode = state.mode in
3767 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3768 state.text <- "";
3769 enttext ();
3770 G.postRedisplay "view:enttext"
3772 let c = Char.chr key in
3773 match c with
3774 | '\027' | 'q' -> (* escape *)
3775 begin match state.mstate with
3776 | Mzoomrect _ ->
3777 state.mstate <- Mnone;
3778 Glut.setCursor Glut.CURSOR_INHERIT;
3779 G.postRedisplay "kill zoom rect";
3780 | _ ->
3781 raise Quit
3782 end;
3784 | '\008' -> (* backspace *)
3785 let y = getnav ~-1 in
3786 gotoy_and_clear_text y
3788 | 'o' ->
3789 enteroutlinemode ()
3791 | 'u' ->
3792 state.rects <- [];
3793 state.text <- "";
3794 G.postRedisplay "dehighlight";
3796 | '/' | '?' ->
3797 let ondone isforw s =
3798 cbput state.hists.pat s;
3799 state.searchpattern <- s;
3800 search s isforw
3802 let s = String.create 1 in
3803 s.[0] <- c;
3804 enttext (s, "", Some (onhist state.hists.pat),
3805 textentry, ondone (c ='/'))
3807 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3808 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3809 setzoom (conf.zoom +. incr)
3811 | '+' ->
3812 let ondone s =
3813 let n =
3814 try int_of_string s with exc ->
3815 state.text <- Printf.sprintf "bad integer `%s': %s"
3816 s (Printexc.to_string exc);
3817 max_int
3819 if n != max_int
3820 then (
3821 conf.pagebias <- n;
3822 state.text <- "page bias is now " ^ string_of_int n;
3825 enttext ("page bias: ", "", None, intentry, ondone)
3827 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3828 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3829 setzoom (max 0.01 (conf.zoom -. decr))
3831 | '-' ->
3832 let ondone msg = state.text <- msg in
3833 enttext (
3834 "option [acfhilpstvAPRSZTI]: ", "", None,
3835 optentry state.mode, ondone
3838 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3839 setzoom 1.0
3841 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3842 let zoom = zoomforh conf.winw conf.winh state.scrollw in
3843 if zoom < 1.0
3844 then setzoom zoom
3846 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3847 togglebirdseye ()
3849 | '0' .. '9' ->
3850 let ondone s =
3851 let n =
3852 try int_of_string s with exc ->
3853 state.text <- Printf.sprintf "bad integer `%s': %s"
3854 s (Printexc.to_string exc);
3857 if n >= 0
3858 then (
3859 addnav ();
3860 cbput state.hists.pag (string_of_int n);
3861 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
3864 let pageentry text key =
3865 match Char.unsafe_chr key with
3866 | 'g' -> TEdone text
3867 | _ -> intentry text key
3869 let text = "x" in text.[0] <- c;
3870 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
3872 | 'b' ->
3873 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
3874 reshape conf.winw conf.winh;
3876 | 'l' ->
3877 conf.hlinks <- not conf.hlinks;
3878 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3879 G.postRedisplay "toggle highlightlinks";
3881 | 'a' ->
3882 begin match state.autoscroll with
3883 | Some step ->
3884 conf.autoscrollstep <- step;
3885 state.autoscroll <- None
3886 | None ->
3887 if conf.autoscrollstep = 0
3888 then state.autoscroll <- Some 1
3889 else state.autoscroll <- Some conf.autoscrollstep
3892 | 'P' ->
3893 conf.presentation <- not conf.presentation;
3894 if conf.presentation
3895 then (
3896 if not conf.scrollbarinpm
3897 then state.scrollw <- 0;
3899 else
3900 state.scrollw <- conf.scrollbw;
3902 showtext ' ' ("presentation mode " ^
3903 if conf.presentation then "on" else "off");
3904 state.anchor <- getanchor ();
3905 represent ()
3907 | 'f' ->
3908 begin match state.fullscreen with
3909 | None ->
3910 state.fullscreen <- Some (conf.winw, conf.winh);
3911 Glut.fullScreen ()
3912 | Some (w, h) ->
3913 state.fullscreen <- None;
3914 doreshape w h
3917 | 'g' ->
3918 gotoy_and_clear_text 0
3920 | 'G' ->
3921 gotopage1 (state.pagecount - 1) 0
3923 | 'n' ->
3924 search state.searchpattern true
3926 | 'p' | 'N' ->
3927 search state.searchpattern false
3929 | 't' ->
3930 begin match state.layout with
3931 | [] -> ()
3932 | l :: _ ->
3933 gotoy_and_clear_text (getpagey l.pageno)
3936 | ' ' ->
3937 begin match List.rev state.layout with
3938 | [] -> ()
3939 | l :: _ ->
3940 let pageno = min (l.pageno+1) (state.pagecount-1) in
3941 gotoy_and_clear_text (getpagey pageno)
3944 | '\127' -> (* del *)
3945 begin match state.layout with
3946 | [] -> ()
3947 | l :: _ ->
3948 let pageno = max 0 (l.pageno-1) in
3949 gotoy_and_clear_text (getpagey pageno)
3952 | '=' ->
3953 showtext ' ' (describe_location ());
3955 | 'w' ->
3956 begin match state.layout with
3957 | [] -> ()
3958 | l :: _ ->
3959 doreshape (l.pagew + state.scrollw) l.pageh;
3960 G.postRedisplay "w"
3963 | '\'' ->
3964 enterbookmarkmode ()
3966 | 'h' ->
3967 enterhelpmode ()
3969 | 'i' ->
3970 enterinfomode ()
3972 | 'e' when conf.redirectstderr ->
3973 entermsgsmode ()
3975 | 'm' ->
3976 let ondone s =
3977 match state.layout with
3978 | l :: _ ->
3979 state.bookmarks <-
3980 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
3981 :: state.bookmarks
3982 | _ -> ()
3984 enttext ("bookmark: ", "", None, textentry, ondone)
3986 | '~' ->
3987 quickbookmark ();
3988 showtext ' ' "Quick bookmark added";
3990 | 'z' ->
3991 begin match state.layout with
3992 | l :: _ ->
3993 let rect = getpdimrect l.pagedimno in
3994 let w, h =
3995 if conf.crophack
3996 then
3997 (truncate (1.8 *. (rect.(1) -. rect.(0))),
3998 truncate (1.2 *. (rect.(3) -. rect.(0))))
3999 else
4000 (truncate (rect.(1) -. rect.(0)),
4001 truncate (rect.(3) -. rect.(0)))
4003 let w = truncate ((float w)*.conf.zoom)
4004 and h = truncate ((float h)*.conf.zoom) in
4005 if w != 0 && h != 0
4006 then (
4007 state.anchor <- getanchor ();
4008 doreshape (w + state.scrollw) (h + conf.interpagespace)
4010 G.postRedisplay "z";
4012 | [] -> ()
4015 | '\000' -> (* ctrl-2 *)
4016 let maxw = getmaxw () in
4017 if maxw > 0.0
4018 then setzoom (maxw /. float conf.winw)
4020 | '<' | '>' ->
4021 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
4023 | '[' | ']' ->
4024 conf.colorscale <-
4025 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
4027 G.postRedisplay "brightness";
4029 | 'k' ->
4030 begin match state.mode with
4031 | Birdseye beye -> upbirdseye beye
4032 | _ -> gotoy (clamp (-conf.scrollstep))
4035 | 'j' ->
4036 begin match state.mode with
4037 | Birdseye beye -> downbirdseye beye
4038 | _ -> gotoy (clamp conf.scrollstep)
4041 | 'r' ->
4042 state.anchor <- getanchor ();
4043 opendoc state.path state.password
4045 | 'v' when conf.debug ->
4046 state.rects <- [];
4047 List.iter (fun l ->
4048 match getopaque l.pageno with
4049 | None -> ()
4050 | Some opaque ->
4051 let x0, y0, x1, y1 = pagebbox opaque in
4052 let a,b = float x0, float y0 in
4053 let c,d = float x1, float y0 in
4054 let e,f = float x1, float y1 in
4055 let h,j = float x0, float y1 in
4056 let rect = (a,b,c,d,e,f,h,j) in
4057 debugrect rect;
4058 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4059 ) state.layout;
4060 G.postRedisplay "v";
4062 | _ ->
4063 vlog "huh? %d %c" key (Char.chr key);
4066 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
4067 match key with
4068 | 27 -> (* escape *)
4069 leavebirdseye beye true
4071 | 12 -> (* ctrl-l *)
4072 let y, h = getpageyh pageno in
4073 let top = (conf.winh - h) / 2 in
4074 gotoy (max 0 (y - top))
4076 | 13 -> (* enter *)
4077 leavebirdseye beye false
4079 | _ ->
4080 viewkeyboard key
4083 let keyboard ~key ~x ~y =
4084 ignore x;
4085 ignore y;
4086 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
4087 then wcmd "interrupt" []
4088 else state.uioh <- state.uioh#key key
4091 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
4092 match key with
4093 | Glut.KEY_UP -> upbirdseye beye
4094 | Glut.KEY_DOWN -> downbirdseye beye
4096 | Glut.KEY_PAGE_UP ->
4097 begin match state.layout with
4098 | l :: _ ->
4099 if l.pagey != 0
4100 then (
4101 state.mode <- Birdseye (
4102 conf, leftx, l.pageno, hooverpageno, anchor
4104 gotopage1 l.pageno 0;
4106 else (
4107 let layout = layout (state.y-conf.winh) conf.winh in
4108 match layout with
4109 | [] -> gotoy (clamp (-conf.winh))
4110 | l :: _ ->
4111 state.mode <- Birdseye (
4112 conf, leftx, l.pageno, hooverpageno, anchor
4114 gotopage1 l.pageno 0
4117 | [] -> gotoy (clamp (-conf.winh))
4118 end;
4120 | Glut.KEY_PAGE_DOWN ->
4121 begin match List.rev state.layout with
4122 | l :: _ ->
4123 let layout = layout (state.y + conf.winh) conf.winh in
4124 begin match layout with
4125 | [] ->
4126 let incr = l.pageh - l.pagevh in
4127 if incr = 0
4128 then (
4129 state.mode <-
4130 Birdseye (
4131 conf, leftx, state.pagecount - 1, hooverpageno, anchor
4133 G.postRedisplay "birdseye pagedown";
4135 else gotoy (clamp (incr + conf.interpagespace*2));
4137 | l :: _ ->
4138 state.mode <-
4139 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
4140 gotopage1 l.pageno 0;
4143 | [] -> gotoy (clamp conf.winh)
4144 end;
4146 | Glut.KEY_HOME ->
4147 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
4148 gotopage1 0 0
4150 | Glut.KEY_END ->
4151 let pageno = state.pagecount - 1 in
4152 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
4153 if not (pagevisible state.layout pageno)
4154 then
4155 let h =
4156 match List.rev state.pdims with
4157 | [] -> conf.winh
4158 | (_, _, h, _) :: _ -> h
4160 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4161 else G.postRedisplay "birdseye end";
4162 | _ -> ()
4165 let setautoscrollspeed step goingdown =
4166 let incr = max 1 ((abs step) / 2) in
4167 let incr = if goingdown then incr else -incr in
4168 let astep = step + incr in
4169 state.autoscroll <- Some astep;
4172 let special ~key ~x ~y =
4173 ignore x;
4174 ignore y;
4175 state.uioh <- state.uioh#special key
4178 let drawpage l =
4179 let color =
4180 match state.mode with
4181 | Textentry _ -> scalecolor 0.4
4182 | View -> scalecolor 1.0
4183 | Birdseye (_, _, pageno, hooverpageno, _) ->
4184 if l.pageno = hooverpageno
4185 then scalecolor 0.9
4186 else (
4187 if l.pageno = pageno
4188 then scalecolor 1.0
4189 else scalecolor 0.8
4192 drawtiles l color;
4193 begin match getopaque l.pageno with
4194 | Some opaque ->
4195 if tileready l l.pagex l.pagey
4196 then
4197 let x = l.pagedispx - l.pagex
4198 and y = l.pagedispy - l.pagey in
4199 postprocess opaque conf.hlinks x y;
4201 | _ -> ()
4202 end;
4205 let scrollindicator () =
4206 let sbw, ph, sh = state.uioh#scrollph in
4207 let sbh, pw, sw = state.uioh#scrollpw in
4209 GlDraw.color (0.64, 0.64, 0.64);
4210 GlDraw.rect
4211 (float (conf.winw - sbw), 0.)
4212 (float conf.winw, float conf.winh)
4214 GlDraw.rect
4215 (0., float (conf.winh - sbh))
4216 (float (conf.winw - state.scrollw - 1), float conf.winh)
4218 GlDraw.color (0.0, 0.0, 0.0);
4220 GlDraw.rect
4221 (float (conf.winw - sbw), ph)
4222 (float conf.winw, ph +. sh)
4224 GlDraw.rect
4225 (pw, float (conf.winh - sbh))
4226 (pw +. sw, float conf.winh)
4230 let pagetranslatepoint l x y =
4231 let dy = y - l.pagedispy in
4232 let y = dy + l.pagey in
4233 let dx = x - l.pagedispx in
4234 let x = dx + l.pagex in
4235 (x, y);
4238 let showsel () =
4239 match state.mstate with
4240 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4243 | Msel ((x0, y0), (x1, y1)) ->
4244 let rec loop = function
4245 | l :: ls ->
4246 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4247 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4248 then
4249 match getopaque l.pageno with
4250 | Some opaque ->
4251 let dx, dy = pagetranslatepoint l 0 0 in
4252 let x0 = x0 + dx
4253 and y0 = y0 + dy
4254 and x1 = x1 + dx
4255 and y1 = y1 + dy in
4256 GlMat.mode `modelview;
4257 GlMat.push ();
4258 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4259 seltext opaque (x0, y0, x1, y1);
4260 GlMat.pop ();
4261 | _ -> ()
4262 else loop ls
4263 | [] -> ()
4265 loop state.layout
4268 let showrects () =
4269 Gl.enable `blend;
4270 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4271 GlDraw.polygon_mode `both `fill;
4272 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4273 List.iter
4274 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4275 List.iter (fun l ->
4276 if l.pageno = pageno
4277 then (
4278 let dx = float (l.pagedispx - l.pagex) in
4279 let dy = float (l.pagedispy - l.pagey) in
4280 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4281 GlDraw.begins `quads;
4283 GlDraw.vertex2 (x0+.dx, y0+.dy);
4284 GlDraw.vertex2 (x1+.dx, y1+.dy);
4285 GlDraw.vertex2 (x2+.dx, y2+.dy);
4286 GlDraw.vertex2 (x3+.dx, y3+.dy);
4288 GlDraw.ends ();
4290 ) state.layout
4291 ) state.rects
4293 Gl.disable `blend;
4296 let display () =
4297 GlClear.color (scalecolor2 conf.bgcolor);
4298 GlClear.clear [`color];
4299 List.iter drawpage state.layout;
4300 showrects ();
4301 showsel ();
4302 state.uioh#display;
4303 scrollindicator ();
4304 begin match state.mstate with
4305 | Mzoomrect ((x0, y0), (x1, y1)) ->
4306 Gl.enable `blend;
4307 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4308 GlDraw.polygon_mode `both `fill;
4309 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4310 GlDraw.rect (float x0, float y0)
4311 (float x1, float y1);
4312 Gl.disable `blend;
4313 | _ -> ()
4314 end;
4315 enttext ();
4316 Glut.swapBuffers ();
4319 let getunder x y =
4320 let rec f = function
4321 | l :: rest ->
4322 begin match getopaque l.pageno with
4323 | Some opaque ->
4324 let x0 = l.pagedispx in
4325 let x1 = x0 + l.pagevw in
4326 let y0 = l.pagedispy in
4327 let y1 = y0 + l.pagevh in
4328 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4329 then
4330 let px, py = pagetranslatepoint l x y in
4331 match whatsunder opaque px py with
4332 | Unone -> f rest
4333 | under -> under
4334 else f rest
4335 | _ ->
4336 f rest
4338 | [] -> Unone
4340 f state.layout
4343 let zoomrect x y x1 y1 =
4344 let x0 = min x x1
4345 and x1 = max x x1
4346 and y0 = min y y1 in
4347 gotoy (state.y + y0);
4348 state.anchor <- getanchor ();
4349 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4350 let margin =
4351 if state.w < conf.winw - state.scrollw
4352 then (conf.winw - state.scrollw - state.w) / 2
4353 else 0
4355 state.x <- (state.x + margin) - x0;
4356 setzoom zoom;
4357 Glut.setCursor Glut.CURSOR_INHERIT;
4358 state.mstate <- Mnone;
4361 let scrollx x =
4362 let winw = conf.winw - state.scrollw - 1 in
4363 let s = float x /. float winw in
4364 let destx = truncate (float (state.w + winw) *. s) in
4365 state.x <- winw - destx;
4366 gotoy_and_clear_text state.y;
4367 state.mstate <- Mscrollx;
4370 let scrolly y =
4371 let s = float y /. float conf.winh in
4372 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4373 gotoy_and_clear_text desty;
4374 state.mstate <- Mscrolly;
4377 let viewmouse button bstate x y =
4378 match button with
4379 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4380 if Glut.getModifiers () land Glut.active_ctrl != 0
4381 then (
4382 match state.mstate with
4383 | Mzoom (oldn, i) ->
4384 if oldn = n
4385 then (
4386 if i = 2
4387 then
4388 let incr =
4389 match n with
4390 | 4 ->
4391 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4392 | _ ->
4393 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4395 let zoom = conf.zoom -. incr in
4396 setzoom zoom;
4397 state.mstate <- Mzoom (n, 0);
4398 else
4399 state.mstate <- Mzoom (n, i+1);
4401 else state.mstate <- Mzoom (n, 0)
4403 | _ -> state.mstate <- Mzoom (n, 0)
4405 else (
4406 match state.autoscroll with
4407 | Some step -> setautoscrollspeed step (n=4)
4408 | None ->
4409 let incr =
4410 if n = 3
4411 then -conf.scrollstep
4412 else conf.scrollstep
4414 let incr = incr * 2 in
4415 let y = clamp incr in
4416 gotoy_and_clear_text y
4419 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4420 if bstate = Glut.DOWN
4421 then (
4422 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4423 state.mstate <- Mpan (x, y)
4425 else
4426 state.mstate <- Mnone
4428 | Glut.RIGHT_BUTTON ->
4429 if bstate = Glut.DOWN
4430 then (
4431 Glut.setCursor Glut.CURSOR_CYCLE;
4432 let p = (x, y) in
4433 state.mstate <- Mzoomrect (p, p)
4435 else (
4436 match state.mstate with
4437 | Mzoomrect ((x0, y0), _) ->
4438 if abs (x-x0) > 10 && abs (y - y0) > 10
4439 then zoomrect x0 y0 x y
4440 else (
4441 state.mstate <- Mnone;
4442 Glut.setCursor Glut.CURSOR_INHERIT;
4443 G.postRedisplay "kill accidental zoom rect";
4445 | _ ->
4446 Glut.setCursor Glut.CURSOR_INHERIT;
4447 state.mstate <- Mnone
4450 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4451 if bstate = Glut.DOWN
4452 then
4453 let _, position, sh = state.uioh#scrollph in
4454 if y > truncate position && y < truncate (position +. sh)
4455 then state.mstate <- Mscrolly
4456 else scrolly y
4457 else
4458 state.mstate <- Mnone
4460 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4461 if bstate = Glut.DOWN
4462 then
4463 let _, position, sw = state.uioh#scrollpw in
4464 if x > truncate position && x < truncate (position +. sw)
4465 then state.mstate <- Mscrollx
4466 else scrollx x
4467 else
4468 state.mstate <- Mnone
4470 | Glut.LEFT_BUTTON ->
4471 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4472 begin match dest with
4473 | Ulinkgoto (pageno, top) ->
4474 if pageno >= 0
4475 then (
4476 addnav ();
4477 gotopage1 pageno top;
4480 | Ulinkuri s ->
4481 gotouri s
4483 | Unone when bstate = Glut.DOWN ->
4484 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4485 state.mstate <- Mpan (x, y);
4487 | Unone | Utext _ ->
4488 if bstate = Glut.DOWN
4489 then (
4490 if conf.angle mod 360 = 0
4491 then (
4492 state.mstate <- Msel ((x, y), (x, y));
4493 G.postRedisplay "mouse select";
4496 else (
4497 match state.mstate with
4498 | Mnone -> ()
4500 | Mzoom _ | Mscrollx | Mscrolly ->
4501 state.mstate <- Mnone
4503 | Mzoomrect ((x0, y0), _) ->
4504 zoomrect x0 y0 x y
4506 | Mpan _ ->
4507 Glut.setCursor Glut.CURSOR_INHERIT;
4508 state.mstate <- Mnone
4510 | Msel ((_, y0), (_, y1)) ->
4511 let f l =
4512 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4513 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4514 then
4515 match getopaque l.pageno with
4516 | Some opaque ->
4517 copysel opaque
4518 | _ -> ()
4520 List.iter f state.layout;
4521 copysel ""; (* ugly *)
4522 Glut.setCursor Glut.CURSOR_INHERIT;
4523 state.mstate <- Mnone;
4527 | _ -> ()
4530 let birdseyemouse button bstate x y
4531 (conf, leftx, _, hooverpageno, anchor) =
4532 match button with
4533 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4534 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4535 let rec loop = function
4536 | [] -> ()
4537 | l :: rest ->
4538 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4539 && x > margin && x < margin + l.pagew
4540 then (
4541 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4543 else loop rest
4545 loop state.layout
4546 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4547 | _ -> ()
4550 let mouse bstate button x y =
4551 state.uioh <- state.uioh#button button bstate x y;
4554 let mouse ~button ~state ~x ~y = mouse state button x y;;
4556 let motion ~x ~y =
4557 state.uioh <- state.uioh#motion x y
4560 let pmotion ~x ~y =
4561 state.uioh <- state.uioh#pmotion x y;
4564 let uioh = object
4565 method display = ()
4567 method key key =
4568 begin match state.mode with
4569 | Textentry textentry -> textentrykeyboard key textentry
4570 | Birdseye birdseye -> birdseyekeyboard key birdseye
4571 | View -> viewkeyboard key
4572 end;
4573 state.uioh
4575 method special key =
4576 begin match state.mode with
4577 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4578 togglebirdseye ()
4580 | Birdseye vals ->
4581 birdseyespecial key vals
4583 | View when key = Glut.KEY_F1 ->
4584 enterhelpmode ()
4586 | View ->
4587 begin match state.autoscroll with
4588 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4589 setautoscrollspeed step (key = Glut.KEY_DOWN)
4591 | _ ->
4592 let y =
4593 match key with
4594 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4595 | Glut.KEY_UP ->
4596 if Glut.getModifiers () land Glut.active_ctrl != 0
4597 then
4598 if Glut.getModifiers () land Glut.active_shift != 0
4599 then (setzoom state.prevzoom; state.y)
4600 else clamp (-conf.winh/2)
4601 else clamp (-conf.scrollstep)
4602 | Glut.KEY_DOWN ->
4603 if Glut.getModifiers () land Glut.active_ctrl != 0
4604 then
4605 if Glut.getModifiers () land Glut.active_shift != 0
4606 then (setzoom state.prevzoom; state.y)
4607 else clamp (conf.winh/2)
4608 else clamp (conf.scrollstep)
4609 | Glut.KEY_PAGE_UP ->
4610 if Glut.getModifiers () land Glut.active_ctrl != 0
4611 then
4612 match state.layout with
4613 | [] -> state.y
4614 | l :: _ -> state.y - l.pagey
4615 else
4616 clamp (-conf.winh)
4617 | Glut.KEY_PAGE_DOWN ->
4618 if Glut.getModifiers () land Glut.active_ctrl != 0
4619 then
4620 match List.rev state.layout with
4621 | [] -> state.y
4622 | l :: _ -> getpagey l.pageno
4623 else
4624 clamp conf.winh
4625 | Glut.KEY_HOME ->
4626 addnav ();
4628 | Glut.KEY_END ->
4629 addnav ();
4630 state.maxy - (if conf.maxhfit then conf.winh else 0)
4632 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4633 Glut.getModifiers () land Glut.active_alt != 0 ->
4634 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4636 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4637 let dx =
4638 if Glut.getModifiers () land Glut.active_ctrl != 0
4639 then (conf.winw / 2)
4640 else 10
4642 state.x <- state.x - dx;
4643 state.y
4644 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4645 let dx =
4646 if Glut.getModifiers () land Glut.active_ctrl != 0
4647 then (conf.winw / 2)
4648 else 10
4650 state.x <- state.x + dx;
4651 state.y
4653 | _ -> state.y
4655 gotoy_and_clear_text y
4658 | Textentry te -> textentryspecial key te
4659 end;
4660 state.uioh
4662 method button button bstate x y =
4663 begin match state.mode with
4664 | View -> viewmouse button bstate x y
4665 | Birdseye beye -> birdseyemouse button bstate x y beye
4666 | Textentry _ -> ()
4667 end;
4668 state.uioh
4670 method motion x y =
4671 begin match state.mode with
4672 | Textentry _ -> ()
4673 | View | Birdseye _ ->
4674 match state.mstate with
4675 | Mzoom _ | Mnone -> ()
4677 | Mpan (x0, y0) ->
4678 let dx = x - x0
4679 and dy = y0 - y in
4680 state.mstate <- Mpan (x, y);
4681 if conf.zoom > 1.0 then state.x <- state.x + dx;
4682 let y = clamp dy in
4683 gotoy_and_clear_text y
4685 | Msel (a, _) ->
4686 state.mstate <- Msel (a, (x, y));
4687 G.postRedisplay "motion select";
4689 | Mscrolly ->
4690 let y = min conf.winh (max 0 y) in
4691 scrolly y
4693 | Mscrollx ->
4694 let x = min conf.winw (max 0 x) in
4695 scrollx x
4697 | Mzoomrect (p0, _) ->
4698 state.mstate <- Mzoomrect (p0, (x, y));
4699 G.postRedisplay "motion zoomrect";
4700 end;
4701 state.uioh
4703 method pmotion x y =
4704 begin match state.mode with
4705 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4706 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4707 let rec loop = function
4708 | [] ->
4709 if hooverpageno != -1
4710 then (
4711 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4712 G.postRedisplay "pmotion birdseye no hoover";
4714 | l :: rest ->
4715 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4716 && x > margin && x < margin + l.pagew
4717 then (
4718 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4719 G.postRedisplay "pmotion birdseye hoover";
4721 else loop rest
4723 loop state.layout
4725 | Textentry _ -> ()
4727 | View ->
4728 match state.mstate with
4729 | Mnone ->
4730 begin match getunder x y with
4731 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
4732 | Ulinkuri uri ->
4733 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
4734 Glut.setCursor Glut.CURSOR_INFO
4735 | Ulinkgoto (page, _) ->
4736 if conf.underinfo
4737 then showtext 'p' ("age: " ^ string_of_int (page+1));
4738 Glut.setCursor Glut.CURSOR_INFO
4739 | Utext s ->
4740 if conf.underinfo then showtext 'f' ("ont: " ^ s);
4741 Glut.setCursor Glut.CURSOR_TEXT
4744 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
4746 end;
4747 state.uioh
4749 method infochanged _ = ()
4751 method scrollph =
4752 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
4753 let p, h = scrollph state.y maxy in
4754 state.scrollw, p, h
4756 method scrollpw =
4757 let winw = conf.winw - state.scrollw - 1 in
4758 let fwinw = float winw in
4759 let sw =
4760 let sw = fwinw /. float state.w in
4761 let sw = fwinw *. sw in
4762 max sw (float conf.scrollh)
4764 let position, sw =
4765 let f = state.w+winw in
4766 let r = float (winw-state.x) /. float f in
4767 let p = fwinw *. r in
4768 p-.sw/.2., sw
4770 let sw =
4771 if position +. sw > fwinw
4772 then fwinw -. position
4773 else sw
4775 state.hscrollh, position, sw
4776 end;;
4778 module Config =
4779 struct
4780 open Parser
4782 let fontpath = ref "";;
4783 let wmclasshack = ref false;;
4785 let unent s =
4786 let l = String.length s in
4787 let b = Buffer.create l in
4788 unent b s 0 l;
4789 Buffer.contents b;
4792 let home =
4794 match platform with
4795 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
4796 | _ -> Sys.getenv "HOME"
4797 with exn ->
4798 prerr_endline
4799 ("Can not determine home directory location: " ^
4800 Printexc.to_string exn);
4804 let config_of c attrs =
4805 let apply c k v =
4807 match k with
4808 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
4809 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
4810 | "case-insensitive-search" -> { c with icase = bool_of_string v }
4811 | "preload" -> { c with preload = bool_of_string v }
4812 | "page-bias" -> { c with pagebias = int_of_string v }
4813 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
4814 | "auto-scroll-step" ->
4815 { c with autoscrollstep = max 0 (int_of_string v) }
4816 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
4817 | "crop-hack" -> { c with crophack = bool_of_string v }
4818 | "throttle" ->
4819 let mw =
4820 match String.lowercase v with
4821 | "true" -> Some infinity
4822 | "false" -> None
4823 | f -> Some (float_of_string f)
4825 { c with maxwait = mw}
4826 | "highlight-links" -> { c with hlinks = bool_of_string v }
4827 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
4828 | "vertical-margin" ->
4829 { c with interpagespace = max 0 (int_of_string v) }
4830 | "zoom" ->
4831 let zoom = float_of_string v /. 100. in
4832 let zoom = max zoom 0.0 in
4833 { c with zoom = zoom }
4834 | "presentation" -> { c with presentation = bool_of_string v }
4835 | "rotation-angle" -> { c with angle = int_of_string v }
4836 | "width" -> { c with winw = max 20 (int_of_string v) }
4837 | "height" -> { c with winh = max 20 (int_of_string v) }
4838 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
4839 | "proportional-display" -> { c with proportional = bool_of_string v }
4840 | "pixmap-cache-size" ->
4841 { c with memlimit = max 2 (int_of_string_with_suffix v) }
4842 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
4843 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
4844 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
4845 | "persistent-location" -> { c with jumpback = bool_of_string v }
4846 | "background-color" -> { c with bgcolor = color_of_string v }
4847 | "scrollbar-in-presentation" ->
4848 { c with scrollbarinpm = bool_of_string v }
4849 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
4850 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
4851 | "mupdf-memlimit" ->
4852 { c with mumemlimit = max 1024 (int_of_string_with_suffix v) }
4853 | "checkers" -> { c with checkers = bool_of_string v }
4854 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
4855 | "trim-margins" -> { c with trimmargins = bool_of_string v }
4856 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
4857 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
4858 | "uri-launcher" -> { c with urilauncher = unent v }
4859 | "color-space" -> { c with colorspace = colorspace_of_string v }
4860 | "invert-colors" -> { c with invert = bool_of_string v }
4861 | "brightness" -> { c with colorscale = float_of_string v }
4862 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
4863 | _ -> c
4864 with exn ->
4865 prerr_endline ("Error processing attribute (`" ^
4866 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
4869 let rec fold c = function
4870 | [] -> c
4871 | (k, v) :: rest ->
4872 let c = apply c k v in
4873 fold c rest
4875 fold c attrs;
4878 let fromstring f pos n v d =
4879 try f v
4880 with exn ->
4881 dolog "Error processing attribute (%S=%S) at %d\n%s"
4882 n v pos (Printexc.to_string exn)
4887 let bookmark_of attrs =
4888 let rec fold title page rely = function
4889 | ("title", v) :: rest -> fold v page rely rest
4890 | ("page", v) :: rest -> fold title v rely rest
4891 | ("rely", v) :: rest -> fold title page v rest
4892 | _ :: rest -> fold title page rely rest
4893 | [] -> title, page, rely
4895 fold "invalid" "0" "0" attrs
4898 let doc_of attrs =
4899 let rec fold path page rely pan = function
4900 | ("path", v) :: rest -> fold v page rely pan rest
4901 | ("page", v) :: rest -> fold path v rely pan rest
4902 | ("rely", v) :: rest -> fold path page v pan rest
4903 | ("pan", v) :: rest -> fold path page rely v rest
4904 | _ :: rest -> fold path page rely pan rest
4905 | [] -> path, page, rely, pan
4907 fold "" "0" "0" "0" attrs
4910 let setconf dst src =
4911 dst.scrollbw <- src.scrollbw;
4912 dst.scrollh <- src.scrollh;
4913 dst.icase <- src.icase;
4914 dst.preload <- src.preload;
4915 dst.pagebias <- src.pagebias;
4916 dst.verbose <- src.verbose;
4917 dst.scrollstep <- src.scrollstep;
4918 dst.maxhfit <- src.maxhfit;
4919 dst.crophack <- src.crophack;
4920 dst.autoscrollstep <- src.autoscrollstep;
4921 dst.maxwait <- src.maxwait;
4922 dst.hlinks <- src.hlinks;
4923 dst.underinfo <- src.underinfo;
4924 dst.interpagespace <- src.interpagespace;
4925 dst.zoom <- src.zoom;
4926 dst.presentation <- src.presentation;
4927 dst.angle <- src.angle;
4928 dst.winw <- src.winw;
4929 dst.winh <- src.winh;
4930 dst.savebmarks <- src.savebmarks;
4931 dst.memlimit <- src.memlimit;
4932 dst.proportional <- src.proportional;
4933 dst.texcount <- src.texcount;
4934 dst.sliceheight <- src.sliceheight;
4935 dst.thumbw <- src.thumbw;
4936 dst.jumpback <- src.jumpback;
4937 dst.bgcolor <- src.bgcolor;
4938 dst.scrollbarinpm <- src.scrollbarinpm;
4939 dst.tilew <- src.tilew;
4940 dst.tileh <- src.tileh;
4941 dst.mumemlimit <- src.mumemlimit;
4942 dst.checkers <- src.checkers;
4943 dst.aalevel <- src.aalevel;
4944 dst.trimmargins <- src.trimmargins;
4945 dst.trimfuzz <- src.trimfuzz;
4946 dst.urilauncher <- src.urilauncher;
4947 dst.colorspace <- src.colorspace;
4948 dst.invert <- src.invert;
4949 dst.colorscale <- src.colorscale;
4950 dst.redirectstderr <- src.redirectstderr;
4953 let get s =
4954 let h = Hashtbl.create 10 in
4955 let dc = { defconf with angle = defconf.angle } in
4956 let rec toplevel v t spos _ =
4957 match t with
4958 | Vdata | Vcdata | Vend -> v
4959 | Vopen ("llppconfig", _, closed) ->
4960 if closed
4961 then v
4962 else { v with f = llppconfig }
4963 | Vopen _ ->
4964 error "unexpected subelement at top level" s spos
4965 | Vclose _ -> error "unexpected close at top level" s spos
4967 and llppconfig v t spos _ =
4968 match t with
4969 | Vdata | Vcdata -> v
4970 | Vend -> error "unexpected end of input in llppconfig" s spos
4971 | Vopen ("defaults", attrs, closed) ->
4972 let c = config_of dc attrs in
4973 setconf dc c;
4974 if closed
4975 then v
4976 else { v with f = skip "defaults" (fun () -> v) }
4978 | Vopen ("ui-font", attrs, closed) ->
4979 let rec getsize size = function
4980 | [] -> size
4981 | ("size", v) :: rest ->
4982 let size =
4983 fromstring int_of_string spos "size" v fstate.fontsize in
4984 getsize size rest
4985 | l -> getsize size l
4987 fstate.fontsize <- getsize fstate.fontsize attrs;
4988 if closed
4989 then v
4990 else { v with f = uifont (Buffer.create 10) }
4992 | Vopen ("doc", attrs, closed) ->
4993 let pathent, spage, srely, span = doc_of attrs in
4994 let path = unent pathent
4995 and pageno = fromstring int_of_string spos "page" spage 0
4996 and rely = fromstring float_of_string spos "rely" srely 0.0
4997 and pan = fromstring int_of_string spos "pan" span 0 in
4998 let c = config_of dc attrs in
4999 let anchor = (pageno, rely) in
5000 if closed
5001 then (Hashtbl.add h path (c, [], pan, anchor); v)
5002 else { v with f = doc path pan anchor c [] }
5004 | Vopen _ ->
5005 error "unexpected subelement in llppconfig" s spos
5007 | Vclose "llppconfig" -> { v with f = toplevel }
5008 | Vclose _ -> error "unexpected close in llppconfig" s spos
5010 and uifont b v t spos epos =
5011 match t with
5012 | Vdata | Vcdata ->
5013 Buffer.add_substring b s spos (epos - spos);
5015 | Vopen (_, _, _) ->
5016 error "unexpected subelement in ui-font" s spos
5017 | Vclose "ui-font" ->
5018 if String.length !fontpath = 0
5019 then fontpath := Buffer.contents b;
5020 { v with f = llppconfig }
5021 | Vclose _ -> error "unexpected close in ui-font" s spos
5022 | Vend -> error "unexpected end of input in ui-font" s spos
5024 and doc path pan anchor c bookmarks v t spos _ =
5025 match t with
5026 | Vdata | Vcdata -> v
5027 | Vend -> error "unexpected end of input in doc" s spos
5028 | Vopen ("bookmarks", _, closed) ->
5029 if closed
5030 then v
5031 else { v with f = pbookmarks path pan anchor c bookmarks }
5033 | Vopen (_, _, _) ->
5034 error "unexpected subelement in doc" s spos
5036 | Vclose "doc" ->
5037 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5038 { v with f = llppconfig }
5040 | Vclose _ -> error "unexpected close in doc" s spos
5042 and pbookmarks path pan anchor c bookmarks v t spos _ =
5043 match t with
5044 | Vdata | Vcdata -> v
5045 | Vend -> error "unexpected end of input in bookmarks" s spos
5046 | Vopen ("item", attrs, closed) ->
5047 let titleent, spage, srely = bookmark_of attrs in
5048 let page = fromstring int_of_string spos "page" spage 0
5049 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5050 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5051 if closed
5052 then { v with f = pbookmarks path pan anchor c bookmarks }
5053 else
5054 let f () = v in
5055 { v with f = skip "item" f }
5057 | Vopen _ ->
5058 error "unexpected subelement in bookmarks" s spos
5060 | Vclose "bookmarks" ->
5061 { v with f = doc path pan anchor c bookmarks }
5063 | Vclose _ -> error "unexpected close in bookmarks" s spos
5065 and skip tag f v t spos _ =
5066 match t with
5067 | Vdata | Vcdata -> v
5068 | Vend ->
5069 error ("unexpected end of input in skipped " ^ tag) s spos
5070 | Vopen (tag', _, closed) ->
5071 if closed
5072 then v
5073 else
5074 let f' () = { v with f = skip tag f } in
5075 { v with f = skip tag' f' }
5076 | Vclose ctag ->
5077 if tag = ctag
5078 then f ()
5079 else error ("unexpected close in skipped " ^ tag) s spos
5082 parse { f = toplevel; accu = () } s;
5083 h, dc;
5086 let do_load f ic =
5088 let len = in_channel_length ic in
5089 let s = String.create len in
5090 really_input ic s 0 len;
5091 f s;
5092 with
5093 | Parse_error (msg, s, pos) ->
5094 let subs = subs s pos in
5095 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5096 failwith ("parse error: " ^ s)
5098 | exn ->
5099 failwith ("config load error: " ^ Printexc.to_string exn)
5102 let defconfpath =
5103 let dir =
5105 let dir = Filename.concat home ".config" in
5106 if Sys.is_directory dir then dir else home
5107 with _ -> home
5109 Filename.concat dir "llpp.conf"
5112 let confpath = ref defconfpath;;
5114 let load1 f =
5115 if Sys.file_exists !confpath
5116 then
5117 match
5118 (try Some (open_in_bin !confpath)
5119 with exn ->
5120 prerr_endline
5121 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5122 Printexc.to_string exn);
5123 None
5125 with
5126 | Some ic ->
5127 begin try
5128 f (do_load get ic)
5129 with exn ->
5130 prerr_endline
5131 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5132 Printexc.to_string exn);
5133 end;
5134 close_in ic;
5136 | None -> ()
5137 else
5138 f (Hashtbl.create 0, defconf)
5141 let load () =
5142 let f (h, dc) =
5143 let pc, pb, px, pa =
5145 Hashtbl.find h (Filename.basename state.path)
5146 with Not_found -> dc, [], 0, (0, 0.0)
5148 setconf defconf dc;
5149 setconf conf pc;
5150 state.bookmarks <- pb;
5151 state.x <- px;
5152 state.scrollw <- conf.scrollbw;
5153 if conf.jumpback
5154 then state.anchor <- pa;
5155 cbput state.hists.nav pa;
5157 load1 f
5160 let add_attrs bb always dc c =
5161 let ob s a b =
5162 if always || a != b
5163 then Printf.bprintf bb "\n %s='%b'" s a
5164 and oi s a b =
5165 if always || a != b
5166 then Printf.bprintf bb "\n %s='%d'" s a
5167 and oI s a b =
5168 if always || a != b
5169 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5170 and oz s a b =
5171 if always || a <> b
5172 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5173 and oF s a b =
5174 if always || a <> b
5175 then Printf.bprintf bb "\n %s='%f'" s a
5176 and oc s a b =
5177 if always || a <> b
5178 then
5179 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5180 and oC s a b =
5181 if always || a <> b
5182 then
5183 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5184 and oR s a b =
5185 if always || a <> b
5186 then
5187 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5188 and os s a b =
5189 if always || a <> b
5190 then
5191 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5192 and oW s a b =
5193 if always || a <> b
5194 then
5195 let v =
5196 match a with
5197 | None -> "false"
5198 | Some f ->
5199 if f = infinity
5200 then "true"
5201 else string_of_float f
5203 Printf.bprintf bb "\n %s='%s'" s v
5205 let w, h =
5206 if always
5207 then dc.winw, dc.winh
5208 else
5209 match state.fullscreen with
5210 | Some wh -> wh
5211 | None -> c.winw, c.winh
5213 let zoom, presentation, interpagespace, maxwait =
5214 if always
5215 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5216 else
5217 match state.mode with
5218 | Birdseye (bc, _, _, _, _) ->
5219 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5220 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5222 oi "width" w dc.winw;
5223 oi "height" h dc.winh;
5224 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5225 oi "scroll-handle-height" c.scrollh dc.scrollh;
5226 ob "case-insensitive-search" c.icase dc.icase;
5227 ob "preload" c.preload dc.preload;
5228 oi "page-bias" c.pagebias dc.pagebias;
5229 oi "scroll-step" c.scrollstep dc.scrollstep;
5230 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5231 ob "max-height-fit" c.maxhfit dc.maxhfit;
5232 ob "crop-hack" c.crophack dc.crophack;
5233 oW "throttle" maxwait dc.maxwait;
5234 ob "highlight-links" c.hlinks dc.hlinks;
5235 ob "under-cursor-info" c.underinfo dc.underinfo;
5236 oi "vertical-margin" interpagespace dc.interpagespace;
5237 oz "zoom" zoom dc.zoom;
5238 ob "presentation" presentation dc.presentation;
5239 oi "rotation-angle" c.angle dc.angle;
5240 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5241 ob "proportional-display" c.proportional dc.proportional;
5242 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5243 oi "tex-count" c.texcount dc.texcount;
5244 oi "slice-height" c.sliceheight dc.sliceheight;
5245 oi "thumbnail-width" c.thumbw dc.thumbw;
5246 ob "persistent-location" c.jumpback dc.jumpback;
5247 oc "background-color" c.bgcolor dc.bgcolor;
5248 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5249 oi "tile-width" c.tilew dc.tilew;
5250 oi "tile-height" c.tileh dc.tileh;
5251 oI "mupdf-memlimit" c.mumemlimit dc.mumemlimit;
5252 ob "checkers" c.checkers dc.checkers;
5253 oi "aalevel" c.aalevel dc.aalevel;
5254 ob "trim-margins" c.trimmargins dc.trimmargins;
5255 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5256 os "uri-launcher" c.urilauncher dc.urilauncher;
5257 oC "color-space" c.colorspace dc.colorspace;
5258 ob "invert-colors" c.invert dc.invert;
5259 oF "brightness" c.colorscale dc.colorscale;
5260 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5261 if always
5262 then ob "wmclass-hack" !wmclasshack false;
5265 let save () =
5266 let uifontsize = fstate.fontsize in
5267 let bb = Buffer.create 32768 in
5268 let f (h, dc) =
5269 let dc = if conf.bedefault then conf else dc in
5270 Buffer.add_string bb "<llppconfig>\n";
5272 if String.length !fontpath > 0
5273 then
5274 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5275 uifontsize
5276 !fontpath
5277 else (
5278 if uifontsize <> 14
5279 then
5280 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5283 Buffer.add_string bb "<defaults ";
5284 add_attrs bb true dc dc;
5285 Buffer.add_string bb "/>\n";
5287 let adddoc path pan anchor c bookmarks =
5288 if bookmarks == [] && c = dc && anchor = emptyanchor
5289 then ()
5290 else (
5291 Printf.bprintf bb "<doc path='%s'"
5292 (enent path 0 (String.length path));
5294 if anchor <> emptyanchor
5295 then (
5296 let n, y = anchor in
5297 Printf.bprintf bb " page='%d'" n;
5298 if y > 1e-6
5299 then
5300 Printf.bprintf bb " rely='%f'" y
5304 if pan != 0
5305 then Printf.bprintf bb " pan='%d'" pan;
5307 add_attrs bb false dc c;
5309 begin match bookmarks with
5310 | [] -> Buffer.add_string bb "/>\n"
5311 | _ ->
5312 Buffer.add_string bb ">\n<bookmarks>\n";
5313 List.iter (fun (title, _level, (page, rely)) ->
5314 Printf.bprintf bb
5315 "<item title='%s' page='%d'"
5316 (enent title 0 (String.length title))
5317 page
5319 if rely > 1e-6
5320 then
5321 Printf.bprintf bb " rely='%f'" rely
5323 Buffer.add_string bb "/>\n";
5324 ) bookmarks;
5325 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5326 end;
5330 let pan =
5331 match state.mode with
5332 | Birdseye (_, pan, _, _, _) -> pan
5333 | _ -> state.x
5335 let basename = Filename.basename state.path in
5336 adddoc basename pan (getanchor ())
5337 { conf with
5338 autoscrollstep =
5339 match state.autoscroll with
5340 | Some step -> step
5341 | None -> conf.autoscrollstep }
5342 (if conf.savebmarks then state.bookmarks else []);
5344 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5345 if basename <> path
5346 then adddoc path x y c bookmarks
5347 ) h;
5348 Buffer.add_string bb "</llppconfig>";
5350 load1 f;
5351 if Buffer.length bb > 0
5352 then
5354 let tmp = !confpath ^ ".tmp" in
5355 let oc = open_out_bin tmp in
5356 Buffer.output_buffer oc bb;
5357 close_out oc;
5358 Unix.rename tmp !confpath;
5359 with exn ->
5360 prerr_endline
5361 ("error while saving configuration: " ^ Printexc.to_string exn)
5363 end;;
5365 let () =
5366 Arg.parse
5367 (Arg.align
5368 [("-p", Arg.String (fun s -> state.password <- s) ,
5369 "<password> Set password");
5371 ("-f", Arg.String (fun s -> Config.fontpath := s),
5372 "<path> Set path to the user interface font");
5374 ("-c", Arg.String (fun s -> Config.confpath := s),
5375 "<path> Set path to the configuration file");
5377 ("-v", Arg.Unit (fun () ->
5378 Printf.printf
5379 "%s\nconfiguration path: %s\n"
5380 (version ())
5381 Config.defconfpath
5383 exit 0), " Print version and exit");
5386 (fun s -> state.path <- s)
5387 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5389 if String.length state.path = 0
5390 then (prerr_endline "file name missing"; exit 1);
5392 Config.load ();
5394 let _ = Glut.init Sys.argv in
5395 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5396 let () = Glut.initWindowSize conf.winw conf.winh in
5397 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5399 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5400 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5401 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5403 let csock, ssock =
5404 if not is_windows
5405 then
5406 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5407 else
5408 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5409 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5410 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5411 Unix.bind sock addr;
5412 Unix.listen sock 1;
5413 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5414 Unix.connect csock addr;
5415 let ssock, _ = Unix.accept sock in
5416 Unix.close sock;
5417 let opts sock =
5418 Unix.setsockopt sock Unix.TCP_NODELAY true;
5419 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5421 opts ssock;
5422 opts csock;
5423 ssock, csock
5426 let () = Glut.displayFunc display in
5427 let () = Glut.reshapeFunc reshape in
5428 let () = Glut.keyboardFunc keyboard in
5429 let () = Glut.specialFunc special in
5430 let () = Glut.idleFunc (Some idle) in
5431 let () = Glut.mouseFunc mouse in
5432 let () = Glut.motionFunc motion in
5433 let () = Glut.passiveMotionFunc pmotion in
5435 setcheckers conf.checkers;
5436 init ssock (
5437 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5438 conf.texcount, conf.sliceheight, conf.mumemlimit, conf.colorspace,
5439 !Config.wmclasshack, !Config.fontpath
5441 state.csock <- csock;
5442 state.ssock <- ssock;
5443 state.text <- "Opening " ^ state.path;
5444 setaalevel conf.aalevel;
5445 writeopen state.path state.password;
5446 state.uioh <- uioh;
5447 setfontsize fstate.fontsize;
5449 redirectstderr ();
5451 while true do
5453 Glut.mainLoop ();
5454 with
5455 | Glut.BadEnum "key in special_of_int" ->
5456 showtext '!' " LablGlut bug: special key not recognized";
5458 | Quit ->
5459 wcmd "quit" [];
5460 Config.save ();
5461 exit 0
5463 | exn when conf.redirectstderr ->
5464 let s =
5465 Printf.sprintf "exception %s\n%s"
5466 (Printexc.to_string exn)
5467 (Printexc.get_backtrace ())
5469 ignore (try
5470 Unix.single_write state.stderr s 0 (String.length s);
5471 with _ -> 0);
5472 exit 1
5473 done;