Refactor a bit
[llpp.git] / main.ml
blob9252d3f962de3a56bbafaa7706afcc640ed75f12
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 | Uunexpected of string
7 | Ulaunch of string
8 | Unamed of string
9 | Uremote of string
10 and facename = string;;
12 let dolog fmt = Printf.kprintf prerr_endline fmt;;
13 let now = Unix.gettimeofday;;
15 exception Quit;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * wmclasshack * fontpath)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and fontpath = string
36 and memsize = int
37 and aalevel = int
38 and wmclasshack = bool
39 and irect = (int * int * int * int)
40 and trimparams = (trimmargins * irect)
41 and colorspace = | Rgb | Bgr | Gray
44 type platform = | Punknown | Plinux | Pwindows | Posx | Psun
45 | Pfreebsd | Pdragonflybsd | Popenbsd | Pnetbsd
46 | Pmingw | Pcygwin;;
48 external init : Unix.file_descr -> params -> unit = "ml_init";;
49 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
50 external copysel : string -> unit = "ml_copysel";;
51 external getpdimrect : int -> float array = "ml_getpdimrect";;
52 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
53 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
54 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
55 external measurestr : int -> string -> float = "ml_measure_string";;
56 external getmaxw : unit -> float = "ml_getmaxw";;
57 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
58 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
59 external platform : unit -> platform = "ml_platform";;
60 external setaalevel : int -> unit = "ml_setaalevel";;
61 external realloctexts : int -> bool = "ml_realloctexts";;
63 let platform_to_string = function
64 | Punknown -> "unknown"
65 | Plinux -> "Linux"
66 | Pwindows -> "Windows"
67 | Posx -> "OSX"
68 | Psun -> "Sun"
69 | Pfreebsd -> "FreeBSD"
70 | Pdragonflybsd -> "DragonflyBSD"
71 | Popenbsd -> "OpenBSD"
72 | Pnetbsd -> "NetBSD"
73 | Pcygwin -> "Cygwin"
74 | Pmingw -> "MingW"
77 let platform = platform ();;
79 let is_windows =
80 match platform with
81 | Pwindows | Pmingw -> true
82 | _ -> false
85 type x = int
86 and y = int
87 and tilex = int
88 and tiley = int
89 and tileparams = (x * y * width * height * tilex * tiley)
92 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
94 type mpos = int * int
95 and mstate =
96 | Msel of (mpos * mpos)
97 | Mpan of mpos
98 | Mscrolly | Mscrollx
99 | Mzoom of (int * int)
100 | Mzoomrect of (mpos * mpos)
101 | Mnone
104 type textentry = string * string * onhist option * onkey * ondone
105 and onkey = string -> int -> te
106 and ondone = string -> unit
107 and histcancel = unit -> unit
108 and onhist = ((histcmd -> string) * histcancel)
109 and histcmd = HCnext | HCprev | HCfirst | HClast
110 and te =
111 | TEstop
112 | TEdone of string
113 | TEcont of string
114 | TEswitch of textentry
117 type 'a circbuf =
118 { store : 'a array
119 ; mutable rc : int
120 ; mutable wc : int
121 ; mutable len : int
125 let bound v minv maxv =
126 max minv (min maxv v);
129 let cbnew n v =
130 { store = Array.create n v
131 ; rc = 0
132 ; wc = 0
133 ; len = 0
137 let drawstring size x y s =
138 Gl.enable `blend;
139 Gl.enable `texture_2d;
140 ignore (drawstr size x y s);
141 Gl.disable `blend;
142 Gl.disable `texture_2d;
145 let drawstring1 size x y s =
146 drawstr size x y s;
149 let drawstring2 size x y fmt =
150 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
153 let cbcap b = Array.length b.store;;
155 let cbput b v =
156 let cap = cbcap b in
157 b.store.(b.wc) <- v;
158 b.wc <- (b.wc + 1) mod cap;
159 b.rc <- b.wc;
160 b.len <- min (b.len + 1) cap;
163 let cbempty b = b.len = 0;;
165 let cbgetg b circular dir =
166 if cbempty b
167 then b.store.(0)
168 else
169 let rc = b.rc + dir in
170 let rc =
171 if circular
172 then (
173 if rc = -1
174 then b.len-1
175 else (
176 if rc = b.len
177 then 0
178 else rc
181 else max 0 (min rc (b.len-1))
183 b.rc <- rc;
184 b.store.(rc);
187 let cbget b = cbgetg b false;;
188 let cbgetc b = cbgetg b true;;
190 type page =
191 { pageno : int
192 ; pagedimno : int
193 ; pagew : int
194 ; pageh : int
195 ; pagex : int
196 ; pagey : int
197 ; pagevw : int
198 ; pagevh : int
199 ; pagedispx : int
200 ; pagedispy : int
204 let debugl l =
205 dolog "l %d dim=%d {" l.pageno l.pagedimno;
206 dolog " WxH %dx%d" l.pagew l.pageh;
207 dolog " vWxH %dx%d" l.pagevw l.pagevh;
208 dolog " pagex,y %d,%d" l.pagex l.pagey;
209 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
210 dolog "}";
213 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
214 dolog "rect {";
215 dolog " x0,y0=(% f, % f)" x0 y0;
216 dolog " x1,y1=(% f, % f)" x1 y1;
217 dolog " x2,y2=(% f, % f)" x2 y2;
218 dolog " x3,y3=(% f, % f)" x3 y3;
219 dolog "}";
222 type columns =
223 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
224 and multicol = columncount * covercount * covercount
225 and pdimno = int
226 and columncount = int
227 and covercount = int;;
229 type conf =
230 { mutable scrollbw : int
231 ; mutable scrollh : int
232 ; mutable icase : bool
233 ; mutable preload : bool
234 ; mutable pagebias : int
235 ; mutable verbose : bool
236 ; mutable debug : bool
237 ; mutable scrollstep : int
238 ; mutable maxhfit : bool
239 ; mutable crophack : bool
240 ; mutable autoscrollstep : int
241 ; mutable maxwait : float option
242 ; mutable hlinks : bool
243 ; mutable underinfo : bool
244 ; mutable interpagespace : interpagespace
245 ; mutable zoom : float
246 ; mutable presentation : bool
247 ; mutable angle : angle
248 ; mutable winw : int
249 ; mutable winh : int
250 ; mutable savebmarks : bool
251 ; mutable proportional : proportional
252 ; mutable trimmargins : trimmargins
253 ; mutable trimfuzz : irect
254 ; mutable memlimit : memsize
255 ; mutable texcount : texcount
256 ; mutable sliceheight : sliceheight
257 ; mutable thumbw : width
258 ; mutable jumpback : bool
259 ; mutable bgcolor : float * float * float
260 ; mutable bedefault : bool
261 ; mutable scrollbarinpm : bool
262 ; mutable tilew : int
263 ; mutable tileh : int
264 ; mutable mustoresize : memsize
265 ; mutable checkers : bool
266 ; mutable aalevel : int
267 ; mutable urilauncher : string
268 ; mutable colorspace : colorspace
269 ; mutable invert : bool
270 ; mutable colorscale : float
271 ; mutable redirectstderr : bool
272 ; mutable ghyllscroll : (int * int * int) option
273 ; mutable columns : columns option
274 ; mutable beyecolumns : columncount option
278 type anchor = pageno * top;;
280 type outline = string * int * anchor;;
282 type rect = float * float * float * float * float * float * float * float;;
284 type tile = opaque * pixmapsize * elapsed
285 and elapsed = float;;
286 type pagemapkey = pageno * gen;;
287 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
288 and row = int
289 and col = int;;
291 let emptyanchor = (0, 0.0);;
293 type infochange = | Memused | Docinfo | Pdim;;
295 class type uioh = object
296 method display : unit
297 method key : int -> uioh
298 method special : Glut.special_key_t -> uioh
299 method button :
300 Glut.button_t -> Glut.mouse_button_state_t -> int -> int -> uioh
301 method motion : int -> int -> uioh
302 method pmotion : int -> int -> uioh
303 method infochanged : infochange -> unit
304 method scrollpw : (int * float * float)
305 method scrollph : (int * float * float)
306 end;;
308 type mode =
309 | Birdseye of (conf * leftx * pageno * pageno * anchor)
310 | Textentry of (textentry * onleave)
311 | View
312 and onleave = leavetextentrystatus -> unit
313 and leavetextentrystatus = | Cancel | Confirm
314 and helpitem = string * int * action
315 and action =
316 | Noaction
317 | Action of (uioh -> uioh)
320 let isbirdseye = function Birdseye _ -> true | _ -> false;;
321 let istextentry = function Textentry _ -> true | _ -> false;;
323 type currently =
324 | Idle
325 | Loading of (page * gen)
326 | Tiling of (
327 page * opaque * colorspace * angle * gen * col * row * width * height
329 | Outlining of outline list
332 let nouioh : uioh = object (self)
333 method display = ()
334 method key _ = self
335 method special _ = self
336 method button _ _ _ _ = self
337 method motion _ _ = self
338 method pmotion _ _ = self
339 method infochanged _ = ()
340 method scrollpw = (0, nan, nan)
341 method scrollph = (0, nan, nan)
342 end;;
344 type state =
345 { mutable csock : Unix.file_descr
346 ; mutable ssock : Unix.file_descr
347 ; mutable errfd : Unix.file_descr option
348 ; mutable stderr : Unix.file_descr
349 ; mutable errmsgs : Buffer.t
350 ; mutable newerrmsgs : bool
351 ; mutable w : int
352 ; mutable x : int
353 ; mutable y : int
354 ; mutable scrollw : int
355 ; mutable hscrollh : int
356 ; mutable anchor : anchor
357 ; mutable maxy : int
358 ; mutable layout : page list
359 ; pagemap : (pagemapkey, opaque) Hashtbl.t
360 ; tilemap : (tilemapkey, tile) Hashtbl.t
361 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
362 ; mutable pdims : (pageno * width * height * leftx) list
363 ; mutable pagecount : int
364 ; mutable currently : currently
365 ; mutable mstate : mstate
366 ; mutable searchpattern : string
367 ; mutable rects : (pageno * recttype * rect) list
368 ; mutable rects1 : (pageno * recttype * rect) list
369 ; mutable text : string
370 ; mutable fullscreen : (width * height) option
371 ; mutable mode : mode
372 ; mutable uioh : uioh
373 ; mutable outlines : outline array
374 ; mutable bookmarks : outline list
375 ; mutable path : string
376 ; mutable password : string
377 ; mutable invalidated : int
378 ; mutable memused : memsize
379 ; mutable gen : gen
380 ; mutable throttle : (page list * int * float) option
381 ; mutable autoscroll : int option
382 ; mutable ghyll : int option -> unit
383 ; mutable help : helpitem array
384 ; mutable docinfo : (int * string) list
385 ; mutable deadline : float
386 ; mutable texid : GlTex.texture_id option
387 ; hists : hists
388 ; mutable prevzoom : float
389 ; mutable progress : float
391 and hists =
392 { pat : string circbuf
393 ; pag : string circbuf
394 ; nav : anchor circbuf
398 let defconf =
399 { scrollbw = 7
400 ; scrollh = 12
401 ; icase = true
402 ; preload = true
403 ; pagebias = 0
404 ; verbose = false
405 ; debug = false
406 ; scrollstep = 24
407 ; maxhfit = true
408 ; crophack = false
409 ; autoscrollstep = 2
410 ; maxwait = None
411 ; hlinks = false
412 ; underinfo = false
413 ; interpagespace = 2
414 ; zoom = 1.0
415 ; presentation = false
416 ; angle = 0
417 ; winw = 900
418 ; winh = 900
419 ; savebmarks = true
420 ; proportional = true
421 ; trimmargins = false
422 ; trimfuzz = (0,0,0,0)
423 ; memlimit = 32 lsl 20
424 ; texcount = 256
425 ; sliceheight = 24
426 ; thumbw = 76
427 ; jumpback = true
428 ; bgcolor = (0.5, 0.5, 0.5)
429 ; bedefault = false
430 ; scrollbarinpm = true
431 ; tilew = 2048
432 ; tileh = 2048
433 ; mustoresize = 128 lsl 20
434 ; checkers = true
435 ; aalevel = 8
436 ; urilauncher =
437 (match platform with
438 | Plinux | Pfreebsd | Pdragonflybsd | Popenbsd | Psun -> "xdg-open \"%s\""
439 | Posx -> "open \"%s\""
440 | Pwindows | Pcygwin | Pmingw -> "start %s"
441 | _ -> "")
442 ; colorspace = Rgb
443 ; invert = false
444 ; colorscale = 1.0
445 ; redirectstderr = false
446 ; ghyllscroll = None
447 ; columns = None
448 ; beyecolumns = None
452 let conf = { defconf with angle = defconf.angle };;
454 type fontstate =
455 { mutable fontsize : int
456 ; mutable wwidth : float
457 ; mutable maxrows : int
461 let fstate =
462 { fontsize = 14
463 ; wwidth = nan
464 ; maxrows = -1
468 let setfontsize n =
469 fstate.fontsize <- n;
470 fstate.wwidth <- measurestr fstate.fontsize "w";
471 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
474 let geturl s =
475 let colonpos = try String.index s ':' with Not_found -> -1 in
476 let len = String.length s in
477 if colonpos >= 0 && colonpos + 3 < len
478 then (
479 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
480 then
481 let schemestartpos =
482 try String.rindex_from s colonpos ' '
483 with Not_found -> -1
485 let scheme =
486 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
488 match scheme with
489 | "http" | "ftp" | "mailto" ->
490 let epos =
491 try String.index_from s colonpos ' '
492 with Not_found -> len
494 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
495 | _ -> ""
496 else ""
498 else ""
501 let popen =
502 let shell, farg =
503 if is_windows
504 then (try Sys.getenv "COMSPEC" with Not_found -> "cmd"), "/c"
505 else "/bin/sh", "-c"
507 fun s ->
508 let args = [|shell; farg; s|] in
509 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
512 let gotouri uri =
513 if String.length conf.urilauncher = 0
514 then print_endline uri
515 else (
516 let url = geturl uri in
517 if String.length url = 0
518 then print_endline uri
519 else
520 let re = Str.regexp "%s" in
521 let command = Str.global_replace re url conf.urilauncher in
522 try popen command
523 with exn ->
524 Printf.eprintf
525 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
526 flush stderr;
530 let version () =
531 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
532 (platform_to_string platform) Sys.word_size Sys.ocaml_version
535 let makehelp () =
536 let strings = version () :: "" :: Help.keys in
537 Array.of_list (
538 List.map (fun s ->
539 let url = geturl s in
540 if String.length url > 0
541 then (s, 0, Action (fun u -> gotouri url; u))
542 else (s, 0, Noaction)
543 ) strings);
546 let noghyll _ = ();;
548 let state =
549 { csock = Unix.stdin
550 ; ssock = Unix.stdin
551 ; errfd = None
552 ; stderr = Unix.stderr
553 ; errmsgs = Buffer.create 0
554 ; newerrmsgs = false
555 ; x = 0
556 ; y = 0
557 ; w = 0
558 ; scrollw = 0
559 ; hscrollh = 0
560 ; anchor = emptyanchor
561 ; layout = []
562 ; maxy = max_int
563 ; tilelru = Queue.create ()
564 ; pagemap = Hashtbl.create 10
565 ; tilemap = Hashtbl.create 10
566 ; pdims = []
567 ; pagecount = 0
568 ; currently = Idle
569 ; mstate = Mnone
570 ; rects = []
571 ; rects1 = []
572 ; text = ""
573 ; mode = View
574 ; fullscreen = None
575 ; searchpattern = ""
576 ; outlines = [||]
577 ; bookmarks = []
578 ; path = ""
579 ; password = ""
580 ; invalidated = 0
581 ; hists =
582 { nav = cbnew 10 (0, 0.0)
583 ; pat = cbnew 1 ""
584 ; pag = cbnew 1 ""
586 ; memused = 0
587 ; gen = 0
588 ; throttle = None
589 ; autoscroll = None
590 ; ghyll = noghyll
591 ; help = makehelp ()
592 ; docinfo = []
593 ; deadline = nan
594 ; texid = None
595 ; prevzoom = 1.0
596 ; progress = -1.0
597 ; uioh = nouioh
601 let vlog fmt =
602 if conf.verbose
603 then
604 Printf.kprintf prerr_endline fmt
605 else
606 Printf.kprintf ignore fmt
609 let redirectstderr () =
610 if conf.redirectstderr
611 then
612 let rfd, wfd = Unix.pipe () in
613 state.stderr <- Unix.dup Unix.stderr;
614 state.errfd <- Some rfd;
615 Unix.dup2 wfd Unix.stderr;
616 else (
617 state.newerrmsgs <- false;
618 begin match state.errfd with
619 | Some fd ->
620 Unix.close fd;
621 Unix.dup2 state.stderr Unix.stderr;
622 state.errfd <- None;
623 | None -> ()
624 end;
625 prerr_string (Buffer.contents state.errmsgs);
626 flush stderr;
627 Buffer.clear state.errmsgs;
631 module G =
632 struct
633 let postRedisplay who =
634 if conf.verbose
635 then prerr_endline ("redisplay for " ^ who);
636 Glut.postRedisplay ();
638 end;;
640 let addchar s c =
641 let b = Buffer.create (String.length s + 1) in
642 Buffer.add_string b s;
643 Buffer.add_char b c;
644 Buffer.contents b;
647 let colorspace_of_string s =
648 match String.lowercase s with
649 | "rgb" -> Rgb
650 | "bgr" -> Bgr
651 | "gray" -> Gray
652 | _ -> failwith "invalid colorspace"
655 let int_of_colorspace = function
656 | Rgb -> 0
657 | Bgr -> 1
658 | Gray -> 2
661 let colorspace_of_int = function
662 | 0 -> Rgb
663 | 1 -> Bgr
664 | 2 -> Gray
665 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
668 let colorspace_to_string = function
669 | Rgb -> "rgb"
670 | Bgr -> "bgr"
671 | Gray -> "gray"
674 let intentry_with_suffix text key =
675 let c = Char.unsafe_chr key in
676 match Char.lowercase c with
677 | '0' .. '9' ->
678 let text = addchar text c in
679 TEcont text
681 | 'k' | 'm' | 'g' ->
682 let text = addchar text c in
683 TEcont text
685 | _ ->
686 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
687 TEcont text
690 let columns_to_string (n, a, b) =
691 if a = 0 && b = 0
692 then Printf.sprintf "%d" n
693 else Printf.sprintf "%d,%d,%d" n a b;
696 let columns_of_string s =
698 (int_of_string s, 0, 0)
699 with _ ->
700 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
703 let writecmd fd s =
704 let len = String.length s in
705 let n = 4 + len in
706 let b = Buffer.create n in
707 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
708 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
709 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
710 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
711 Buffer.add_string b s;
712 let s' = Buffer.contents b in
713 let n' = Unix.write fd s' 0 n in
714 if n' != n then failwith "write failed";
717 let readcmd fd =
718 let s = "xxxx" in
719 let n = Unix.read fd s 0 4 in
720 if n != 4 then failwith "incomplete read(len)";
721 let len = 0
722 lor (Char.code s.[0] lsl 24)
723 lor (Char.code s.[1] lsl 16)
724 lor (Char.code s.[2] lsl 8)
725 lor (Char.code s.[3] lsl 0)
727 let s = String.create len in
728 let n =
729 if is_windows
730 then
731 let rec loop n =
732 if n = 10
733 then failwith "EWOULDBLOCK encountered 10 times"
734 else
736 Unix.read fd s 0 len
737 with Unix.Unix_error (Unix.EWOULDBLOCK, _, _) ->
738 let _, _, _ = Unix.select [fd] [] [] 0.01 in
739 loop (n+1)
740 in loop 0
741 else
742 Unix.read fd s 0 len
744 if n != len then failwith "incomplete read(data)";
748 let makecmd s l =
749 let b = Buffer.create 10 in
750 Buffer.add_string b s;
751 let rec combine = function
752 | [] -> b
753 | x :: xs ->
754 Buffer.add_char b ' ';
755 let s =
756 match x with
757 | `b b -> if b then "1" else "0"
758 | `s s -> s
759 | `i i -> string_of_int i
760 | `f f -> string_of_float f
761 | `I f -> string_of_int (truncate f)
763 Buffer.add_string b s;
764 combine xs;
766 combine l;
769 let wcmd s l =
770 let cmd = Buffer.contents (makecmd s l) in
771 writecmd state.csock cmd;
774 let calcips h =
775 if conf.presentation
776 then
777 let d = conf.winh - h in
778 max 0 ((d + 1) / 2)
779 else
780 conf.interpagespace
783 let calcheight () =
784 let rec f pn ph pi fh l =
785 match l with
786 | (n, _, h, _) :: rest ->
787 let ips = calcips h in
788 let fh =
789 if conf.presentation
790 then fh+ips
791 else (
792 if isbirdseye state.mode && pn = 0
793 then fh + ips
794 else fh
797 let fh = fh + ((n - pn) * (ph + pi)) in
798 f n h ips fh rest;
800 | [] ->
801 let inc =
802 if conf.presentation || (isbirdseye state.mode && pn = 0)
803 then 0
804 else -pi
806 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
807 max 0 fh
809 let fh = f 0 0 0 0 state.pdims in
813 let calcheight () =
814 match conf.columns with
815 | None -> calcheight ()
816 | Some (_, b) ->
817 if Array.length b > 0
818 then
819 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
820 y + h
821 else 0
824 let getpageyh pageno =
825 let rec f pn ph pi y l =
826 match l with
827 | (n, _, h, _) :: rest ->
828 let ips = calcips h in
829 if n >= pageno
830 then
831 let h = if n = pageno then h else ph in
832 if conf.presentation && n = pageno
833 then
834 y + (pageno - pn) * (ph + pi) + pi, h
835 else
836 y + (pageno - pn) * (ph + pi), h
837 else
838 let y = y + (if conf.presentation then pi else 0) in
839 let y = y + (n - pn) * (ph + pi) in
840 f n h ips y rest
842 | [] ->
843 y + (pageno - pn) * (ph + pi), ph
845 f 0 0 0 0 state.pdims
848 let getpageyh pageno =
849 match conf.columns with
850 | None -> getpageyh pageno
851 | Some (_, b) ->
852 let (_, _, y, (_, _, h, _)) = b.(pageno) in
853 y, h
856 let getpagedim pageno =
857 let rec f ppdim l =
858 match l with
859 | (n, _, _, _) as pdim :: rest ->
860 if n >= pageno
861 then (if n = pageno then pdim else ppdim)
862 else f pdim rest
864 | [] -> ppdim
866 f (-1, -1, -1, -1) state.pdims
869 let getpagey pageno = fst (getpageyh pageno);;
871 let layout1 y sh =
872 let sh = sh - state.hscrollh in
873 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
874 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
875 match pdims with
876 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
877 let ips = calcips h in
878 let yinc =
879 if conf.presentation || (isbirdseye state.mode && pageno = 0)
880 then ips
881 else 0
883 (w, h, ips, xoff), rest, pdimno + 1, yinc
884 | _ ->
885 prev, pdims, pdimno, 0
887 let dy = dy + yinc in
888 let py = py + yinc in
889 if pageno = state.pagecount || dy >= sh
890 then
891 accu
892 else
893 let vy = y + dy in
894 if py + h <= vy - yinc
895 then
896 let py = py + h + ips in
897 let dy = max 0 (py - y) in
898 f ~pageno:(pageno+1)
899 ~pdimno
900 ~prev:curr
903 ~pdims:rest
904 ~accu
905 else
906 let pagey = vy - py in
907 let pagevh = h - pagey in
908 let pagevh = min (sh - dy) pagevh in
909 let off = if yinc > 0 then py - vy else 0 in
910 let py = py + h + ips in
911 let pagex, dx =
912 let xoff = xoff +
913 if state.w < conf.winw - state.scrollw
914 then (conf.winw - state.scrollw - state.w) / 2
915 else 0
917 let dispx = xoff + state.x in
918 if dispx < 0
919 then (-dispx, 0)
920 else (0, dispx)
922 let pagevw =
923 let lw = w - pagex in
924 min lw (conf.winw - state.scrollw)
926 let e =
927 { pageno = pageno
928 ; pagedimno = pdimno
929 ; pagew = w
930 ; pageh = h
931 ; pagex = pagex
932 ; pagey = pagey + off
933 ; pagevw = pagevw
934 ; pagevh = pagevh - off
935 ; pagedispx = dx
936 ; pagedispy = dy + off
939 let accu = e :: accu in
940 f ~pageno:(pageno+1)
941 ~pdimno
942 ~prev:curr
944 ~dy:(dy+pagevh+ips)
945 ~pdims:rest
946 ~accu
948 if state.invalidated = 0
949 then (
950 let accu =
952 ~pageno:0
953 ~pdimno:~-1
954 ~prev:(0,0,0,0)
955 ~py:0
956 ~dy:0
957 ~pdims:state.pdims
958 ~accu:[]
960 List.rev accu
962 else
966 let layoutN ((columns, coverA, coverB), b) y sh =
967 let sh = sh - state.hscrollh in
968 let rec fold accu n =
969 if n = Array.length b
970 then accu
971 else
972 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
973 if (vy - y) > sh &&
974 (n = coverA - 1
975 || n = state.pagecount - coverB
976 || (n - coverA) mod columns = columns - 1)
977 then accu
978 else
979 let accu =
980 if vy + h > y
981 then
982 let pagey = max 0 (y - vy) in
983 let pagedispy = if pagey > 0 then 0 else vy - y in
984 let pagedispx, pagex, pagevw =
985 let pdx =
986 if n = coverA - 1 || n = state.pagecount - coverB
987 then state.x + (conf.winw - state.scrollw - w) / 2
988 else dx + xoff + state.x
990 if pdx < 0
991 then 0, -pdx, w + pdx
992 else pdx, 0, min (conf.winw - state.scrollw) w
994 let pagevh = min (h - pagey) (sh - pagedispy) in
995 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
996 then
997 let e =
998 { pageno = n
999 ; pagedimno = pdimno
1000 ; pagew = w
1001 ; pageh = h
1002 ; pagex = pagex
1003 ; pagey = pagey
1004 ; pagevw = pagevw
1005 ; pagevh = pagevh
1006 ; pagedispx = pagedispx
1007 ; pagedispy = pagedispy
1010 e :: accu
1011 else
1012 accu
1013 else
1014 accu
1016 fold accu (n+1)
1018 if state.invalidated = 0
1019 then List.rev (fold [] 0)
1020 else []
1023 let layout y sh =
1024 match conf.columns with
1025 | None -> layout1 y sh
1026 | Some c -> layoutN c y sh
1029 let clamp incr =
1030 let y = state.y + incr in
1031 let y = max 0 y in
1032 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1036 let getopaque pageno =
1037 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
1038 with Not_found -> None
1041 let putopaque pageno opaque =
1042 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
1045 let itertiles l f =
1046 let tilex = l.pagex mod conf.tilew in
1047 let tiley = l.pagey mod conf.tileh in
1049 let col = l.pagex / conf.tilew in
1050 let row = l.pagey / conf.tileh in
1052 let vw =
1053 let a = l.pagew - l.pagex in
1054 let b = conf.winw - state.scrollw in
1055 min a b
1056 and vh = l.pagevh in
1058 let rec rowloop row y0 dispy h =
1059 if h = 0
1060 then ()
1061 else (
1062 let dh = conf.tileh - y0 in
1063 let dh = min h dh in
1064 let rec colloop col x0 dispx w =
1065 if w = 0
1066 then ()
1067 else (
1068 let dw = conf.tilew - x0 in
1069 let dw = min w dw in
1071 f col row dispx dispy x0 y0 dw dh;
1072 colloop (col+1) 0 (dispx+dw) (w-dw)
1075 colloop col tilex l.pagedispx vw;
1076 rowloop (row+1) 0 (dispy+dh) (h-dh)
1079 if vw > 0 && vh > 0
1080 then rowloop row tiley l.pagedispy vh;
1083 let gettileopaque l col row =
1084 let key =
1085 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1087 try Some (Hashtbl.find state.tilemap key)
1088 with Not_found -> None
1091 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1092 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1093 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1096 let drawtiles l color =
1097 GlDraw.color color;
1098 let f col row x y tilex tiley w h =
1099 match gettileopaque l col row with
1100 | Some (opaque, _, t) ->
1101 let params = x, y, w, h, tilex, tiley in
1102 if conf.invert
1103 then (
1104 Gl.enable `blend;
1105 GlFunc.blend_func `zero `one_minus_src_color;
1107 drawtile params opaque;
1108 if conf.invert
1109 then Gl.disable `blend;
1110 if conf.debug
1111 then (
1112 let s = Printf.sprintf
1113 "%d[%d,%d] %f sec"
1114 l.pageno col row t
1116 let w = measurestr fstate.fontsize s in
1117 GlMisc.push_attrib [`current];
1118 GlDraw.color (0.0, 0.0, 0.0);
1119 GlDraw.rect
1120 (float (x-2), float (y-2))
1121 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1122 GlDraw.color (1.0, 1.0, 1.0);
1123 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1124 GlMisc.pop_attrib ();
1127 | _ ->
1128 let w =
1129 let lw = conf.winw - state.scrollw - x in
1130 min lw w
1131 and h =
1132 let lh = conf.winh - y in
1133 min lh h
1135 Gl.enable `texture_2d;
1136 begin match state.texid with
1137 | Some id ->
1138 GlTex.bind_texture `texture_2d id;
1139 let x0 = float x
1140 and y0 = float y
1141 and x1 = float (x+w)
1142 and y1 = float (y+h) in
1144 let tw = float w /. 64.0
1145 and th = float h /. 64.0 in
1146 let tx0 = float tilex /. 64.0
1147 and ty0 = float tiley /. 64.0 in
1148 let tx1 = tx0 +. tw
1149 and ty1 = ty0 +. th in
1150 GlDraw.begins `quads;
1151 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1152 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1153 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1154 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1155 GlDraw.ends ();
1157 Gl.disable `texture_2d;
1158 | None ->
1159 GlDraw.color (1.0, 1.0, 1.0);
1160 GlDraw.rect
1161 (float x, float y)
1162 (float (x+w), float (y+h));
1163 end;
1164 if w > 128 && h > fstate.fontsize + 10
1165 then (
1166 GlDraw.color (0.0, 0.0, 0.0);
1167 let c, r =
1168 if conf.verbose
1169 then (col*conf.tilew, row*conf.tileh)
1170 else col, row
1172 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1174 GlDraw.color color;
1176 itertiles l f
1179 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1181 let tilevisible1 l x y =
1182 let ax0 = l.pagex
1183 and ax1 = l.pagex + l.pagevw
1184 and ay0 = l.pagey
1185 and ay1 = l.pagey + l.pagevh in
1187 let bx0 = x
1188 and by0 = y in
1189 let bx1 = min (bx0 + conf.tilew) l.pagew
1190 and by1 = min (by0 + conf.tileh) l.pageh in
1192 let rx0 = max ax0 bx0
1193 and ry0 = max ay0 by0
1194 and rx1 = min ax1 bx1
1195 and ry1 = min ay1 by1 in
1197 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1198 nonemptyintersection
1201 let tilevisible layout n x y =
1202 let rec findpageinlayout = function
1203 | l :: _ when l.pageno = n -> tilevisible1 l x y
1204 | _ :: rest -> findpageinlayout rest
1205 | [] -> false
1207 findpageinlayout layout
1210 let tileready l x y =
1211 tilevisible1 l x y &&
1212 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1215 let tilepage n p layout =
1216 let rec loop = function
1217 | l :: rest ->
1218 if l.pageno = n
1219 then
1220 let f col row _ _ _ _ _ _ =
1221 if state.currently = Idle
1222 then
1223 match gettileopaque l col row with
1224 | Some _ -> ()
1225 | None ->
1226 let x = col*conf.tilew
1227 and y = row*conf.tileh in
1228 let w =
1229 let w = l.pagew - x in
1230 min w conf.tilew
1232 let h =
1233 let h = l.pageh - y in
1234 min h conf.tileh
1236 wcmd "tile"
1237 [`s p
1238 ;`i x
1239 ;`i y
1240 ;`i w
1241 ;`i h
1243 state.currently <-
1244 Tiling (
1245 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1246 conf.tilew, conf.tileh
1249 itertiles l f;
1250 else
1251 loop rest
1253 | [] -> ()
1255 if state.invalidated = 0 then loop layout;
1258 let preloadlayout visiblepages =
1259 let presentation = conf.presentation in
1260 let interpagespace = conf.interpagespace in
1261 let maxy = state.maxy in
1262 conf.presentation <- false;
1263 conf.interpagespace <- 0;
1264 state.maxy <- calcheight ();
1265 let y =
1266 match visiblepages with
1267 | [] -> 0
1268 | l :: _ -> getpagey l.pageno + l.pagey
1270 let y = if y < conf.winh then 0 else y - conf.winh in
1271 let h = state.y - y + conf.winh*3 in
1272 let pages = layout y h in
1273 conf.presentation <- presentation;
1274 conf.interpagespace <- interpagespace;
1275 state.maxy <- maxy;
1276 pages;
1279 let load pages =
1280 let rec loop pages =
1281 if state.currently != Idle
1282 then ()
1283 else
1284 match pages with
1285 | l :: rest ->
1286 begin match getopaque l.pageno with
1287 | None ->
1288 wcmd "page" [`i l.pageno; `i l.pagedimno];
1289 state.currently <- Loading (l, state.gen);
1290 | Some opaque ->
1291 tilepage l.pageno opaque pages;
1292 loop rest
1293 end;
1294 | _ -> ()
1296 if state.invalidated = 0 then loop pages
1299 let preload pages =
1300 load pages;
1301 if conf.preload && state.currently = Idle
1302 then load (preloadlayout pages);
1305 let layoutready layout =
1306 let rec fold all ls =
1307 all && match ls with
1308 | l :: rest ->
1309 let seen = ref false in
1310 let allvisible = ref true in
1311 let foo col row _ _ _ _ _ _ =
1312 seen := true;
1313 allvisible := !allvisible &&
1314 begin match gettileopaque l col row with
1315 | Some _ -> true
1316 | None -> false
1319 itertiles l foo;
1320 fold (!seen && !allvisible) rest
1321 | [] -> true
1323 let alltilesvisible = fold true layout in
1324 alltilesvisible;
1327 let gotoy y =
1328 let y = bound y 0 state.maxy in
1329 let y, layout, proceed =
1330 match conf.maxwait with
1331 | Some time when state.ghyll == noghyll ->
1332 begin match state.throttle with
1333 | None ->
1334 let layout = layout y conf.winh in
1335 let ready = layoutready layout in
1336 if not ready
1337 then (
1338 load layout;
1339 state.throttle <- Some (layout, y, now ());
1341 else G.postRedisplay "gotoy showall (None)";
1342 y, layout, ready
1343 | Some (_, _, started) ->
1344 let dt = now () -. started in
1345 if dt > time
1346 then (
1347 state.throttle <- None;
1348 let layout = layout y conf.winh in
1349 load layout;
1350 G.postRedisplay "maxwait";
1351 y, layout, true
1353 else -1, [], false
1356 | _ ->
1357 let layout = layout y conf.winh in
1358 if true || layoutready layout
1359 then G.postRedisplay "gotoy ready";
1360 y, layout, true
1362 if proceed
1363 then (
1364 state.y <- y;
1365 state.layout <- layout;
1366 begin match state.mode with
1367 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1368 if not (pagevisible layout pageno)
1369 then (
1370 match state.layout with
1371 | [] -> ()
1372 | l :: _ ->
1373 state.mode <- Birdseye (
1374 conf, leftx, l.pageno, hooverpageno, anchor
1377 | _ -> ()
1378 end;
1379 preload layout;
1381 state.ghyll <- noghyll;
1384 let conttiling pageno opaque =
1385 tilepage pageno opaque
1386 (if conf.preload then preloadlayout state.layout else state.layout)
1389 let gotoy_and_clear_text y =
1390 gotoy y;
1391 if not conf.verbose then state.text <- "";
1394 let getanchor () =
1395 match state.layout with
1396 | [] -> emptyanchor
1397 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1400 let getanchory (n, top) =
1401 let y, h = getpageyh n in
1402 y + (truncate (top *. float h));
1405 let gotoanchor anchor =
1406 gotoy (getanchory anchor);
1409 let addnav () =
1410 cbput state.hists.nav (getanchor ());
1413 let getnav dir =
1414 let anchor = cbgetc state.hists.nav dir in
1415 getanchory anchor;
1418 let gotoghyll y =
1419 let rec scroll f n a b =
1420 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1421 let snake f a b =
1422 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1423 if f < a
1424 then s (float f /. float a)
1425 else (
1426 if f > b
1427 then 1.0 -. s ((float (f-b) /. float (n-b)))
1428 else 1.0
1431 snake f a b
1432 and summa f n a b =
1433 (* courtesy:
1434 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1435 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1436 let iv1 = iv f in
1437 let ins = float a *. iv1
1438 and outs = float (n-b) *. iv1 in
1439 let ones = b - a in
1440 ins +. outs +. float ones
1442 let rec set (_N, _A, _B) y sy =
1443 let sum = summa 1.0 _N _A _B in
1444 let dy = float (y - sy) in
1445 state.ghyll <- (
1446 let rec gf n y1 o =
1447 if n >= _N
1448 then state.ghyll <- noghyll
1449 else
1450 let go n =
1451 let s = scroll n _N _A _B in
1452 let y1 = y1 +. ((s *. dy) /. sum) in
1453 gotoy_and_clear_text (truncate y1);
1454 state.ghyll <- gf (n+1) y1;
1456 match o with
1457 | None -> go n
1458 | Some y' -> set (_N/2, 0, 0) y' state.y
1460 gf 0 (float state.y)
1463 match conf.ghyllscroll with
1464 | None ->
1465 gotoy_and_clear_text y
1466 | Some nab ->
1467 if state.ghyll == noghyll
1468 then set nab y state.y
1469 else state.ghyll (Some y)
1472 let gotopage n top =
1473 let y, h = getpageyh n in
1474 let y = y + (truncate (top *. float h)) in
1475 gotoghyll y
1478 let gotopage1 n top =
1479 let y = getpagey n in
1480 let y = y + top in
1481 gotoghyll y
1484 let invalidate () =
1485 state.layout <- [];
1486 state.pdims <- [];
1487 state.rects <- [];
1488 state.rects1 <- [];
1489 state.invalidated <- state.invalidated + 1;
1492 let writeopen path password =
1493 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1496 let opendoc path password =
1497 invalidate ();
1498 state.path <- path;
1499 state.password <- password;
1500 state.gen <- state.gen + 1;
1501 state.docinfo <- [];
1503 setaalevel conf.aalevel;
1504 writeopen path password;
1505 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1506 wcmd "geometry" [`i state.w; `i conf.winh];
1509 let scalecolor c =
1510 let c = c *. conf.colorscale in
1511 (c, c, c);
1514 let scalecolor2 (r, g, b) =
1515 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1518 let represent () =
1519 let docolumns = function
1520 | None -> ()
1521 | Some ((columns, coverA, coverB), _) ->
1522 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1523 let rec loop pageno pdimno pdim x y rowh pdims =
1524 if pageno = state.pagecount
1525 then ()
1526 else
1527 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1528 match pdims with
1529 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1530 pdimno+1, pdim, rest
1531 | _ ->
1532 pdimno, pdim, pdims
1534 let x, y, rowh' =
1535 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1536 then (
1537 (conf.winw - state.scrollw - w) / 2,
1538 y + rowh + conf.interpagespace, h
1540 else (
1541 if (pageno - coverA) mod columns = 0
1542 then 0, y + rowh + conf.interpagespace, h
1543 else x, y, max rowh h
1546 let rec fixrow m = if m = pageno then () else
1547 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1548 if h < rowh
1549 then (
1550 let y = y + (rowh - h) / 2 in
1551 a.(m) <- (pdimno, x, y, pdim);
1553 fixrow (m+1)
1555 if pageno > 1 && (pageno - coverA) mod columns = 0
1556 then fixrow (pageno - columns);
1557 a.(pageno) <- (pdimno, x, y, pdim);
1558 let x = x + w + xoff*2 + conf.interpagespace in
1559 loop (pageno+1) pdimno pdim x y rowh' pdims
1561 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1562 conf.columns <- Some ((columns, coverA, coverB), a);
1564 docolumns conf.columns;
1565 state.maxy <- calcheight ();
1566 state.hscrollh <-
1567 if state.w <= conf.winw - state.scrollw
1568 then 0
1569 else state.scrollw
1571 match state.mode with
1572 | Birdseye (_, _, pageno, _, _) ->
1573 let y, h = getpageyh pageno in
1574 let top = (conf.winh - h) / 2 in
1575 gotoy (max 0 (y - top))
1576 | _ -> gotoanchor state.anchor
1579 let reshape =
1580 let firsttime = ref true in
1581 fun ~w ~h ->
1582 GlDraw.viewport 0 0 w h;
1583 if state.invalidated = 0 && not !firsttime
1584 then state.anchor <- getanchor ();
1586 firsttime := false;
1587 conf.winw <- w;
1588 let w = truncate (float w *. conf.zoom) - state.scrollw in
1589 let w = max w 2 in
1590 state.w <- w;
1591 conf.winh <- h;
1592 setfontsize fstate.fontsize;
1593 GlMat.mode `modelview;
1594 GlMat.load_identity ();
1596 GlMat.mode `projection;
1597 GlMat.load_identity ();
1598 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1599 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1600 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1602 let w =
1603 match conf.columns with
1604 | None -> w
1605 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1607 invalidate ();
1608 wcmd "geometry" [`i w; `i h];
1611 let enttext () =
1612 let len = String.length state.text in
1613 let drawstring s =
1614 let hscrollh =
1615 match state.mode with
1616 | View -> state.hscrollh
1617 | _ -> 0
1619 let rect x w =
1620 GlDraw.rect
1621 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1622 (x+.w, float (conf.winh - hscrollh))
1625 let w = float (conf.winw - state.scrollw - 1) in
1626 if state.progress >= 0.0 && state.progress < 1.0
1627 then (
1628 GlDraw.color (0.3, 0.3, 0.3);
1629 let w1 = w *. state.progress in
1630 rect 0.0 w1;
1631 GlDraw.color (0.0, 0.0, 0.0);
1632 rect w1 (w-.w1)
1634 else (
1635 GlDraw.color (0.0, 0.0, 0.0);
1636 rect 0.0 w;
1639 GlDraw.color (1.0, 1.0, 1.0);
1640 drawstring fstate.fontsize
1641 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1643 let s =
1644 match state.mode with
1645 | Textentry ((prefix, text, _, _, _), _) ->
1646 let s =
1647 if len > 0
1648 then
1649 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1650 else
1651 Printf.sprintf "%s%s_" prefix text
1655 | _ -> state.text
1657 let s =
1658 if state.newerrmsgs
1659 then (
1660 if not (istextentry state.mode)
1661 then
1662 let s1 = "(press 'e' to review error messasges)" in
1663 if String.length s > 0 then s ^ " " ^ s1 else s1
1664 else s
1666 else s
1668 if String.length s > 0
1669 then drawstring s
1672 let showtext c s =
1673 state.text <- Printf.sprintf "%c%s" c s;
1674 G.postRedisplay "showtext";
1677 let gctiles () =
1678 let len = Queue.length state.tilelru in
1679 let rec loop qpos =
1680 if state.memused <= conf.memlimit
1681 then ()
1682 else (
1683 if qpos < len
1684 then
1685 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1686 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1687 let (_, pw, ph, _) = getpagedim n in
1689 gen = state.gen
1690 && colorspace = conf.colorspace
1691 && angle = conf.angle
1692 && pagew = pw
1693 && pageh = ph
1694 && (
1695 let layout =
1696 match state.throttle with
1697 | None ->
1698 if conf.preload
1699 then preloadlayout state.layout
1700 else state.layout
1701 | Some (layout, _, _) ->
1702 layout
1704 let x = col*conf.tilew
1705 and y = row*conf.tileh in
1706 tilevisible layout n x y
1708 then Queue.push lruitem state.tilelru
1709 else (
1710 wcmd "freetile" [`s p];
1711 state.memused <- state.memused - s;
1712 state.uioh#infochanged Memused;
1713 Hashtbl.remove state.tilemap k;
1715 loop (qpos+1)
1718 loop 0
1721 let flushtiles () =
1722 Queue.iter (fun (k, p, s) ->
1723 wcmd "freetile" [`s p];
1724 state.memused <- state.memused - s;
1725 state.uioh#infochanged Memused;
1726 Hashtbl.remove state.tilemap k;
1727 ) state.tilelru;
1728 Queue.clear state.tilelru;
1729 load state.layout;
1732 let logcurrently = function
1733 | Idle -> dolog "Idle"
1734 | Loading (l, gen) ->
1735 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1736 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1737 dolog
1738 "Tiling %d[%d,%d] page=%s cs=%s angle"
1739 l.pageno col row pageopaque
1740 (colorspace_to_string colorspace)
1742 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1743 angle gen conf.angle state.gen
1744 tilew tileh
1745 conf.tilew conf.tileh
1747 | Outlining _ ->
1748 dolog "outlining"
1751 let act cmds =
1752 (* dolog "%S" cmds; *)
1753 let op, args =
1754 let spacepos =
1755 try String.index cmds ' '
1756 with Not_found -> -1
1758 if spacepos = -1
1759 then cmds, ""
1760 else
1761 let l = String.length cmds in
1762 let op = String.sub cmds 0 spacepos in
1763 op, begin
1764 if l - spacepos < 2 then ""
1765 else String.sub cmds (spacepos+1) (l-spacepos-1)
1768 match op with
1769 | "clear" ->
1770 state.uioh#infochanged Pdim;
1771 state.pdims <- [];
1773 | "clearrects" ->
1774 state.rects <- state.rects1;
1775 G.postRedisplay "clearrects";
1777 | "continue" ->
1778 let n =
1779 try Scanf.sscanf args "%u" (fun n -> n)
1780 with exn ->
1781 dolog "error processing 'continue' %S: %s"
1782 cmds (Printexc.to_string exn);
1783 exit 1;
1785 state.pagecount <- n;
1786 state.invalidated <- state.invalidated - 1;
1787 begin match state.currently with
1788 | Outlining l ->
1789 state.currently <- Idle;
1790 state.outlines <- Array.of_list (List.rev l)
1791 | _ -> ()
1792 end;
1793 if state.invalidated = 0
1794 then represent ();
1795 if conf.maxwait = None
1796 then G.postRedisplay "continue";
1798 | "title" ->
1799 Glut.setWindowTitle args
1801 | "msg" ->
1802 showtext ' ' args
1804 | "vmsg" ->
1805 if conf.verbose
1806 then showtext ' ' args
1808 | "progress" ->
1809 let progress, text =
1811 Scanf.sscanf args "%f %n"
1812 (fun f pos ->
1813 f, String.sub args pos (String.length args - pos))
1814 with exn ->
1815 dolog "error processing 'progress' %S: %s"
1816 cmds (Printexc.to_string exn);
1817 exit 1;
1819 state.text <- text;
1820 state.progress <- progress;
1821 G.postRedisplay "progress"
1823 | "firstmatch" ->
1824 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1826 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1827 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1828 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1829 with exn ->
1830 dolog "error processing 'firstmatch' %S: %s"
1831 cmds (Printexc.to_string exn);
1832 exit 1;
1834 let y = (getpagey pageno) + truncate y0 in
1835 addnav ();
1836 gotoy y;
1837 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1839 | "match" ->
1840 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1842 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1843 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1844 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1845 with exn ->
1846 dolog "error processing 'match' %S: %s"
1847 cmds (Printexc.to_string exn);
1848 exit 1;
1850 state.rects1 <-
1851 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1853 | "page" ->
1854 let pageopaque, t =
1856 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1857 with exn ->
1858 dolog "error processing 'page' %S: %s"
1859 cmds (Printexc.to_string exn);
1860 exit 1;
1862 begin match state.currently with
1863 | Loading (l, gen) ->
1864 vlog "page %d took %f sec" l.pageno t;
1865 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1866 begin match state.throttle with
1867 | None ->
1868 let preloadedpages =
1869 if conf.preload
1870 then preloadlayout state.layout
1871 else state.layout
1873 let evict () =
1874 let module IntSet =
1875 Set.Make (struct type t = int let compare = (-) end) in
1876 let set =
1877 List.fold_left (fun s l -> IntSet.add l.pageno s)
1878 IntSet.empty preloadedpages
1880 let evictedpages =
1881 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1882 if not (IntSet.mem pageno set)
1883 then (
1884 wcmd "freepage" [`s opaque];
1885 key :: accu
1887 else accu
1888 ) state.pagemap []
1890 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1892 evict ();
1893 state.currently <- Idle;
1894 if gen = state.gen
1895 then (
1896 tilepage l.pageno pageopaque state.layout;
1897 load state.layout;
1898 load preloadedpages;
1899 if pagevisible state.layout l.pageno
1900 && layoutready state.layout
1901 then G.postRedisplay "page";
1904 | Some (layout, _, _) ->
1905 state.currently <- Idle;
1906 tilepage l.pageno pageopaque layout;
1907 load state.layout
1908 end;
1910 | _ ->
1911 dolog "Inconsistent loading state";
1912 logcurrently state.currently;
1913 raise Quit;
1916 | "tile" ->
1917 let (x, y, opaque, size, t) =
1919 Scanf.sscanf args "%u %u %s %u %f"
1920 (fun x y p size t -> (x, y, p, size, t))
1921 with exn ->
1922 dolog "error processing 'tile' %S: %s"
1923 cmds (Printexc.to_string exn);
1924 exit 1;
1926 begin match state.currently with
1927 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1928 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1930 if tilew != conf.tilew || tileh != conf.tileh
1931 then (
1932 wcmd "freetile" [`s opaque];
1933 state.currently <- Idle;
1934 load state.layout;
1936 else (
1937 puttileopaque l col row gen cs angle opaque size t;
1938 state.memused <- state.memused + size;
1939 state.uioh#infochanged Memused;
1940 gctiles ();
1941 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1942 opaque, size) state.tilelru;
1944 let layout =
1945 match state.throttle with
1946 | None -> state.layout
1947 | Some (layout, _, _) -> layout
1950 state.currently <- Idle;
1951 if gen = state.gen
1952 && conf.colorspace = cs
1953 && conf.angle = angle
1954 && tilevisible layout l.pageno x y
1955 then conttiling l.pageno pageopaque;
1957 begin match state.throttle with
1958 | None ->
1959 preload state.layout;
1960 if gen = state.gen
1961 && conf.colorspace = cs
1962 && conf.angle = angle
1963 && tilevisible state.layout l.pageno x y
1964 then G.postRedisplay "tile nothrottle";
1966 | Some (layout, y, _) ->
1967 let ready = layoutready layout in
1968 if ready
1969 then (
1970 state.y <- y;
1971 state.layout <- layout;
1972 state.throttle <- None;
1973 G.postRedisplay "throttle";
1975 else load layout;
1976 end;
1979 | _ ->
1980 dolog "Inconsistent tiling state";
1981 logcurrently state.currently;
1982 raise Quit;
1985 | "pdim" ->
1986 let pdim =
1988 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1989 with exn ->
1990 dolog "error processing 'pdim' %S: %s"
1991 cmds (Printexc.to_string exn);
1992 exit 1;
1994 state.uioh#infochanged Pdim;
1995 state.pdims <- pdim :: state.pdims
1997 | "o" ->
1998 let (l, n, t, h, pos) =
2000 Scanf.sscanf args "%u %u %d %u %n"
2001 (fun l n t h pos -> l, n, t, h, pos)
2002 with exn ->
2003 dolog "error processing 'o' %S: %s"
2004 cmds (Printexc.to_string exn);
2005 exit 1;
2007 let s = String.sub args pos (String.length args - pos) in
2008 let outline = (s, l, (n, float t /. float h)) in
2009 begin match state.currently with
2010 | Outlining outlines ->
2011 state.currently <- Outlining (outline :: outlines)
2012 | Idle ->
2013 state.currently <- Outlining [outline]
2014 | currently ->
2015 dolog "invalid outlining state";
2016 logcurrently currently
2019 | "info" ->
2020 state.docinfo <- (1, args) :: state.docinfo
2022 | "infoend" ->
2023 state.uioh#infochanged Docinfo;
2024 state.docinfo <- List.rev state.docinfo
2026 | _ ->
2027 dolog "unknown cmd `%S'" cmds
2030 let idle () =
2031 if state.deadline == nan then state.deadline <- now ();
2032 let r =
2033 match state.errfd with
2034 | None -> [state.csock]
2035 | Some fd -> [state.csock; fd]
2037 let rec loop delay =
2038 let deadline =
2039 if state.ghyll == noghyll
2040 then state.deadline
2041 else now () +. 0.02
2043 let timeout =
2044 if delay > 0.0
2045 then max 0.0 (deadline -. now ())
2046 else 0.0
2048 let r, _, _ =
2049 try Unix.select r [] [] timeout
2050 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [] ,[]
2052 begin match r with
2053 | [] ->
2054 state.ghyll None;
2055 begin match state.autoscroll with
2056 | Some step when step != 0 ->
2057 let y = state.y + step in
2058 let y =
2059 if y < 0
2060 then state.maxy
2061 else if y >= state.maxy then 0 else y
2063 gotoy y;
2064 if state.mode = View
2065 then state.text <- "";
2066 state.deadline <- state.deadline +. 0.005;
2068 | _ ->
2069 state.deadline <- state.deadline +. delay;
2070 end;
2072 | l ->
2073 let rec checkfds c = function
2074 | [] -> c
2075 | fd :: rest when fd = state.csock ->
2076 let cmd = readcmd state.csock in
2077 act cmd;
2078 checkfds true rest
2079 | fd :: rest ->
2080 let s = String.create 80 in
2081 let n = Unix.read fd s 0 80 in
2082 if conf.redirectstderr
2083 then (
2084 Buffer.add_substring state.errmsgs s 0 n;
2085 state.newerrmsgs <- true;
2086 Glut.postRedisplay ();
2088 else (
2089 prerr_string (String.sub s 0 n);
2090 flush stderr;
2092 checkfds c rest
2094 if checkfds false l
2095 then loop 0.0
2096 end;
2097 in loop 0.007
2100 let onhist cb =
2101 let rc = cb.rc in
2102 let action = function
2103 | HCprev -> cbget cb ~-1
2104 | HCnext -> cbget cb 1
2105 | HCfirst -> cbget cb ~-(cb.rc)
2106 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2107 and cancel () = cb.rc <- rc
2108 in (action, cancel)
2111 let search pattern forward =
2112 if String.length pattern > 0
2113 then
2114 let pn, py =
2115 match state.layout with
2116 | [] -> 0, 0
2117 | l :: _ ->
2118 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2120 let cmd =
2121 let b = makecmd "search"
2122 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
2124 Buffer.add_char b ',';
2125 Buffer.add_string b pattern;
2126 Buffer.add_char b '\000';
2127 Buffer.contents b;
2129 writecmd state.csock cmd;
2132 let intentry text key =
2133 let c = Char.unsafe_chr key in
2134 match c with
2135 | '0' .. '9' ->
2136 let text = addchar text c in
2137 TEcont text
2139 | _ ->
2140 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2141 TEcont text
2144 let textentry text key =
2145 let c = Char.unsafe_chr key in
2146 match c with
2147 | _ when key >= 32 && key < 127 ->
2148 let text = addchar text c in
2149 TEcont text
2151 | _ ->
2152 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
2153 TEcont text
2156 let reqlayout angle proportional =
2157 match state.throttle with
2158 | None ->
2159 if state.invalidated = 0 then state.anchor <- getanchor ();
2160 conf.angle <- angle mod 360;
2161 conf.proportional <- proportional;
2162 invalidate ();
2163 wcmd "reqlayout" [`i conf.angle; `b proportional];
2164 | _ -> ()
2167 let settrim trimmargins trimfuzz =
2168 if state.invalidated = 0 then state.anchor <- getanchor ();
2169 conf.trimmargins <- trimmargins;
2170 conf.trimfuzz <- trimfuzz;
2171 let x0, y0, x1, y1 = trimfuzz in
2172 invalidate ();
2173 wcmd "settrim" [
2174 `b conf.trimmargins;
2175 `i x0;
2176 `i y0;
2177 `i x1;
2178 `i y1;
2180 Hashtbl.iter (fun _ opaque ->
2181 wcmd "freepage" [`s opaque];
2182 ) state.pagemap;
2183 Hashtbl.clear state.pagemap;
2186 let setzoom zoom =
2187 match state.throttle with
2188 | None ->
2189 let zoom = max 0.01 zoom in
2190 if zoom <> conf.zoom
2191 then (
2192 state.prevzoom <- conf.zoom;
2193 let relx =
2194 if zoom <= 1.0
2195 then (state.x <- 0; 0.0)
2196 else float state.x /. float state.w
2198 conf.zoom <- zoom;
2199 reshape conf.winw conf.winh;
2200 if zoom > 1.0
2201 then (
2202 let x = relx *. float state.w in
2203 state.x <- truncate x;
2205 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2208 | Some (layout, y, started) ->
2209 let time =
2210 match conf.maxwait with
2211 | None -> 0.0
2212 | Some t -> t
2214 let dt = now () -. started in
2215 if dt > time
2216 then (
2217 state.y <- y;
2218 load layout;
2222 let setcolumns columns coverA coverB =
2223 if columns < 2
2224 then (
2225 conf.columns <- None;
2226 state.x <- 0;
2227 setzoom 1.0;
2229 else (
2230 conf.columns <- Some ((columns, coverA, coverB), [||]);
2231 conf.zoom <- 1.0;
2233 reshape conf.winw conf.winh;
2236 let enterbirdseye () =
2237 let zoom = float conf.thumbw /. float conf.winw in
2238 let birdseyepageno =
2239 let cy = conf.winh / 2 in
2240 let fold = function
2241 | [] -> 0
2242 | l :: rest ->
2243 let rec fold best = function
2244 | [] -> best.pageno
2245 | l :: rest ->
2246 let d = cy - (l.pagedispy + l.pagevh/2)
2247 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2248 if abs d < abs dbest
2249 then fold l rest
2250 else best.pageno
2251 in fold l rest
2253 fold state.layout
2255 state.mode <- Birdseye (
2256 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2258 conf.zoom <- zoom;
2259 conf.presentation <- false;
2260 conf.interpagespace <- 10;
2261 conf.hlinks <- false;
2262 state.x <- 0;
2263 state.mstate <- Mnone;
2264 conf.maxwait <- None;
2265 conf.columns <- (
2266 match conf.beyecolumns with
2267 | Some c ->
2268 conf.zoom <- 1.0;
2269 Some ((c, 0, 0), [||])
2270 | None -> None
2272 Glut.setCursor Glut.CURSOR_INHERIT;
2273 if conf.verbose
2274 then
2275 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2276 (100.0*.zoom)
2277 else
2278 state.text <- ""
2280 reshape conf.winw conf.winh;
2283 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2284 state.mode <- View;
2285 conf.zoom <- c.zoom;
2286 conf.presentation <- c.presentation;
2287 conf.interpagespace <- c.interpagespace;
2288 conf.maxwait <- c.maxwait;
2289 conf.hlinks <- c.hlinks;
2290 conf.beyecolumns <- (
2291 match conf.columns with
2292 | Some ((c, _, _), _) -> Some c
2293 | None -> None
2295 conf.columns <- (
2296 match c.columns with
2297 | Some (c, _) -> Some (c, [||])
2298 | None -> None
2300 state.x <- leftx;
2301 if conf.verbose
2302 then
2303 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2304 (100.0*.conf.zoom)
2306 reshape conf.winw conf.winh;
2307 state.anchor <- if goback then anchor else (pageno, 0.0);
2310 let togglebirdseye () =
2311 match state.mode with
2312 | Birdseye vals -> leavebirdseye vals true
2313 | View -> enterbirdseye ()
2314 | _ -> ()
2317 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2318 let pageno = max 0 (pageno - incr) in
2319 let rec loop = function
2320 | [] -> gotopage1 pageno 0
2321 | l :: _ when l.pageno = pageno ->
2322 if l.pagedispy >= 0 && l.pagey = 0
2323 then G.postRedisplay "upbirdseye"
2324 else gotopage1 pageno 0
2325 | _ :: rest -> loop rest
2327 loop state.layout;
2328 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2331 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2332 let pageno = min (state.pagecount - 1) (pageno + incr) in
2333 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2334 let rec loop = function
2335 | [] ->
2336 let y, h = getpageyh pageno in
2337 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2338 gotoy (clamp dy)
2339 | l :: _ when l.pageno = pageno ->
2340 if l.pagevh != l.pageh
2341 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2342 else G.postRedisplay "downbirdseye"
2343 | _ :: rest -> loop rest
2345 loop state.layout
2348 let optentry mode _ key =
2349 let btos b = if b then "on" else "off" in
2350 let c = Char.unsafe_chr key in
2351 match c with
2352 | 's' ->
2353 let ondone s =
2354 try conf.scrollstep <- int_of_string s with exc ->
2355 state.text <- Printf.sprintf "bad integer `%s': %s"
2356 s (Printexc.to_string exc)
2358 TEswitch ("scroll step: ", "", None, intentry, ondone)
2360 | 'A' ->
2361 let ondone s =
2363 conf.autoscrollstep <- int_of_string s;
2364 if state.autoscroll <> None
2365 then state.autoscroll <- Some conf.autoscrollstep
2366 with exc ->
2367 state.text <- Printf.sprintf "bad integer `%s': %s"
2368 s (Printexc.to_string exc)
2370 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2372 | 'C' ->
2373 let ondone s =
2375 let n, a, b = columns_of_string s in
2376 setcolumns n a b;
2377 with exc ->
2378 state.text <- Printf.sprintf "bad columns `%s': %s"
2379 s (Printexc.to_string exc)
2381 TEswitch ("columns: ", "", None, textentry, ondone)
2383 | 'Z' ->
2384 let ondone s =
2386 let zoom = float (int_of_string s) /. 100.0 in
2387 setzoom zoom
2388 with exc ->
2389 state.text <- Printf.sprintf "bad integer `%s': %s"
2390 s (Printexc.to_string exc)
2392 TEswitch ("zoom: ", "", None, intentry, ondone)
2394 | 't' ->
2395 let ondone s =
2397 conf.thumbw <- bound (int_of_string s) 2 4096;
2398 state.text <-
2399 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2400 begin match mode with
2401 | Birdseye beye ->
2402 leavebirdseye beye false;
2403 enterbirdseye ();
2404 | _ -> ();
2406 with exc ->
2407 state.text <- Printf.sprintf "bad integer `%s': %s"
2408 s (Printexc.to_string exc)
2410 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2412 | 'R' ->
2413 let ondone s =
2414 match try
2415 Some (int_of_string s)
2416 with exc ->
2417 state.text <- Printf.sprintf "bad integer `%s': %s"
2418 s (Printexc.to_string exc);
2419 None
2420 with
2421 | Some angle -> reqlayout angle conf.proportional
2422 | None -> ()
2424 TEswitch ("rotation: ", "", None, intentry, ondone)
2426 | 'i' ->
2427 conf.icase <- not conf.icase;
2428 TEdone ("case insensitive search " ^ (btos conf.icase))
2430 | 'p' ->
2431 conf.preload <- not conf.preload;
2432 gotoy state.y;
2433 TEdone ("preload " ^ (btos conf.preload))
2435 | 'v' ->
2436 conf.verbose <- not conf.verbose;
2437 TEdone ("verbose " ^ (btos conf.verbose))
2439 | 'd' ->
2440 conf.debug <- not conf.debug;
2441 TEdone ("debug " ^ (btos conf.debug))
2443 | 'h' ->
2444 conf.maxhfit <- not conf.maxhfit;
2445 state.maxy <-
2446 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2447 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2449 | 'c' ->
2450 conf.crophack <- not conf.crophack;
2451 TEdone ("crophack " ^ btos conf.crophack)
2453 | 'a' ->
2454 let s =
2455 match conf.maxwait with
2456 | None ->
2457 conf.maxwait <- Some infinity;
2458 "always wait for page to complete"
2459 | Some _ ->
2460 conf.maxwait <- None;
2461 "show placeholder if page is not ready"
2463 TEdone s
2465 | 'f' ->
2466 conf.underinfo <- not conf.underinfo;
2467 TEdone ("underinfo " ^ btos conf.underinfo)
2469 | 'P' ->
2470 conf.savebmarks <- not conf.savebmarks;
2471 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2473 | 'S' ->
2474 let ondone s =
2476 let pageno, py =
2477 match state.layout with
2478 | [] -> 0, 0
2479 | l :: _ ->
2480 l.pageno, l.pagey
2482 conf.interpagespace <- int_of_string s;
2483 state.maxy <- calcheight ();
2484 let y = getpagey pageno in
2485 gotoy (y + py)
2486 with exc ->
2487 state.text <- Printf.sprintf "bad integer `%s': %s"
2488 s (Printexc.to_string exc)
2490 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2492 | 'l' ->
2493 reqlayout conf.angle (not conf.proportional);
2494 TEdone ("proportional display " ^ btos conf.proportional)
2496 | 'T' ->
2497 settrim (not conf.trimmargins) conf.trimfuzz;
2498 TEdone ("trim margins " ^ btos conf.trimmargins)
2500 | 'I' ->
2501 conf.invert <- not conf.invert;
2502 TEdone ("invert colors " ^ btos conf.invert)
2504 | _ ->
2505 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2506 TEstop
2509 class type lvsource = object
2510 method getitemcount : int
2511 method getitem : int -> (string * int)
2512 method hasaction : int -> bool
2513 method exit :
2514 uioh:uioh ->
2515 cancel:bool ->
2516 active:int ->
2517 first:int ->
2518 pan:int ->
2519 qsearch:string ->
2520 uioh option
2521 method getactive : int
2522 method getfirst : int
2523 method getqsearch : string
2524 method setqsearch : string -> unit
2525 method getpan : int
2526 end;;
2528 class virtual lvsourcebase = object
2529 val mutable m_active = 0
2530 val mutable m_first = 0
2531 val mutable m_qsearch = ""
2532 val mutable m_pan = 0
2533 method getactive = m_active
2534 method getfirst = m_first
2535 method getqsearch = m_qsearch
2536 method getpan = m_pan
2537 method setqsearch s = m_qsearch <- s
2538 end;;
2540 let textentryspecial key = function
2541 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2542 let s =
2543 match key with
2544 | Glut.KEY_UP -> action HCprev
2545 | Glut.KEY_DOWN -> action HCnext
2546 | Glut.KEY_HOME -> action HCfirst
2547 | Glut.KEY_END -> action HClast
2548 | _ -> state.text
2550 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2551 G.postRedisplay "special textentry";
2552 | _ -> ()
2555 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2556 let enttext te =
2557 state.mode <- Textentry (te, onleave);
2558 state.text <- "";
2559 enttext ();
2560 G.postRedisplay "textentrykeyboard enttext";
2562 match Char.unsafe_chr key with
2563 | '\008' -> (* backspace *)
2564 let len = String.length text in
2565 if len = 0
2566 then (
2567 onleave Cancel;
2568 G.postRedisplay "textentrykeyboard after cancel";
2570 else (
2571 let s = String.sub text 0 (len - 1) in
2572 enttext (c, s, opthist, onkey, ondone)
2575 | '\r' | '\n' ->
2576 ondone text;
2577 onleave Confirm;
2578 G.postRedisplay "textentrykeyboard after confirm"
2580 | '\007' (* ctrl-g *)
2581 | '\027' -> (* escape *)
2582 if String.length text = 0
2583 then (
2584 begin match opthist with
2585 | None -> ()
2586 | Some (_, onhistcancel) -> onhistcancel ()
2587 end;
2588 onleave Cancel;
2589 state.text <- "";
2590 G.postRedisplay "textentrykeyboard after cancel2"
2592 else (
2593 enttext (c, "", opthist, onkey, ondone)
2596 | '\127' -> () (* delete *)
2598 | _ ->
2599 begin match onkey text key with
2600 | TEdone text ->
2601 ondone text;
2602 onleave Confirm;
2603 G.postRedisplay "textentrykeyboard after confirm2";
2605 | TEcont text ->
2606 enttext (c, text, opthist, onkey, ondone);
2608 | TEstop ->
2609 onleave Cancel;
2610 G.postRedisplay "textentrykeyboard after cancel3"
2612 | TEswitch te ->
2613 state.mode <- Textentry (te, onleave);
2614 G.postRedisplay "textentrykeyboard switch";
2615 end;
2618 let firstof first active =
2619 if first > active || abs (first - active) > fstate.maxrows - 1
2620 then max 0 (active - (fstate.maxrows/2))
2621 else first
2624 let calcfirst first active =
2625 if active > first
2626 then
2627 let rows = active - first in
2628 if rows > fstate.maxrows then active - fstate.maxrows else first
2629 else active
2632 let scrollph y maxy =
2633 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2634 let sh = float conf.winh /. sh in
2635 let sh = max sh (float conf.scrollh) in
2637 let percent =
2638 if y = state.maxy
2639 then 1.0
2640 else float y /. float maxy
2642 let position = (float conf.winh -. sh) *. percent in
2644 let position =
2645 if position +. sh > float conf.winh
2646 then float conf.winh -. sh
2647 else position
2649 position, sh;
2652 let coe s = (s :> uioh);;
2654 class listview ~(source:lvsource) ~trusted =
2655 object (self)
2656 val m_pan = source#getpan
2657 val m_first = source#getfirst
2658 val m_active = source#getactive
2659 val m_qsearch = source#getqsearch
2660 val m_prev_uioh = state.uioh
2662 method private elemunder y =
2663 let n = y / (fstate.fontsize+1) in
2664 if m_first + n < source#getitemcount
2665 then (
2666 if source#hasaction (m_first + n)
2667 then Some (m_first + n)
2668 else None
2670 else None
2672 method display =
2673 Gl.enable `blend;
2674 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2675 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2676 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2677 GlDraw.color (1., 1., 1.);
2678 Gl.enable `texture_2d;
2679 let fs = fstate.fontsize in
2680 let nfs = fs + 1 in
2681 let ww = fstate.wwidth in
2682 let tabw = 30.0*.ww in
2683 let itemcount = source#getitemcount in
2684 let rec loop row =
2685 if (row - m_first) * nfs > conf.winh
2686 then ()
2687 else (
2688 if row >= 0 && row < itemcount
2689 then (
2690 let (s, level) = source#getitem row in
2691 let y = (row - m_first) * nfs in
2692 let x = 5.0 +. float (level + m_pan) *. ww in
2693 if row = m_active
2694 then (
2695 Gl.disable `texture_2d;
2696 GlDraw.polygon_mode `both `line;
2697 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2698 GlDraw.rect (1., float (y + 1))
2699 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2700 GlDraw.polygon_mode `both `fill;
2701 GlDraw.color (1., 1., 1.);
2702 Gl.enable `texture_2d;
2705 let drawtabularstring s =
2706 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2707 if trusted
2708 then
2709 let tabpos = try String.index s '\t' with Not_found -> -1 in
2710 if tabpos > 0
2711 then
2712 let len = String.length s - tabpos - 1 in
2713 let s1 = String.sub s 0 tabpos
2714 and s2 = String.sub s (tabpos + 1) len in
2715 let nx = drawstr x s1 in
2716 let sw = nx -. x in
2717 let x = x +. (max tabw sw) in
2718 drawstr x s2
2719 else
2720 drawstr x s
2721 else
2722 drawstr x s
2724 let _ = drawtabularstring s in
2725 loop (row+1)
2729 loop m_first;
2730 Gl.disable `blend;
2731 Gl.disable `texture_2d;
2733 method updownlevel incr =
2734 let len = source#getitemcount in
2735 let curlevel =
2736 if m_active >= 0 && m_active < len
2737 then snd (source#getitem m_active)
2738 else -1
2740 let rec flow i =
2741 if i = len then i-1 else if i = -1 then 0 else
2742 let _, l = source#getitem i in
2743 if l != curlevel then i else flow (i+incr)
2745 let active = flow m_active in
2746 let first = calcfirst m_first active in
2747 G.postRedisplay "special outline updownlevel";
2748 {< m_active = active; m_first = first >}
2750 method private key1 key =
2751 let set active first qsearch =
2752 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2754 let search active pattern incr =
2755 let dosearch re =
2756 let rec loop n =
2757 if n >= 0 && n < source#getitemcount
2758 then (
2759 let s, _ = source#getitem n in
2761 (try ignore (Str.search_forward re s 0); true
2762 with Not_found -> false)
2763 then Some n
2764 else loop (n + incr)
2766 else None
2768 loop active
2771 let re = Str.regexp_case_fold pattern in
2772 dosearch re
2773 with Failure s ->
2774 state.text <- s;
2775 None
2777 match key with
2778 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2779 let incr = if key = 18 then -1 else 1 in
2780 let active, first =
2781 match search (m_active + incr) m_qsearch incr with
2782 | None ->
2783 state.text <- m_qsearch ^ " [not found]";
2784 m_active, m_first
2785 | Some active ->
2786 state.text <- m_qsearch;
2787 active, firstof m_first active
2789 G.postRedisplay "listview ctrl-r/s";
2790 set active first m_qsearch;
2792 | 8 -> (* backspace *)
2793 let len = String.length m_qsearch in
2794 if len = 0
2795 then coe self
2796 else (
2797 if len = 1
2798 then (
2799 state.text <- "";
2800 G.postRedisplay "listview empty qsearch";
2801 set m_active m_first "";
2803 else
2804 let qsearch = String.sub m_qsearch 0 (len - 1) in
2805 let active, first =
2806 match search m_active qsearch ~-1 with
2807 | None ->
2808 state.text <- qsearch ^ " [not found]";
2809 m_active, m_first
2810 | Some active ->
2811 state.text <- qsearch;
2812 active, firstof m_first active
2814 G.postRedisplay "listview backspace qsearch";
2815 set active first qsearch
2818 | _ when key >= 32 && key < 127 ->
2819 let pattern = addchar m_qsearch (Char.chr key) in
2820 let active, first =
2821 match search m_active pattern 1 with
2822 | None ->
2823 state.text <- pattern ^ " [not found]";
2824 m_active, m_first
2825 | Some active ->
2826 state.text <- pattern;
2827 active, firstof m_first active
2829 G.postRedisplay "listview qsearch add";
2830 set active first pattern;
2832 | 27 -> (* escape *)
2833 state.text <- "";
2834 if String.length m_qsearch = 0
2835 then (
2836 G.postRedisplay "list view escape";
2837 begin
2838 match
2839 source#exit (coe self) true m_active m_first m_pan m_qsearch
2840 with
2841 | None -> m_prev_uioh
2842 | Some uioh -> uioh
2845 else (
2846 G.postRedisplay "list view kill qsearch";
2847 source#setqsearch "";
2848 coe {< m_qsearch = "" >}
2851 | 13 -> (* enter *)
2852 state.text <- "";
2853 let self = {< m_qsearch = "" >} in
2854 source#setqsearch "";
2855 let opt =
2856 G.postRedisplay "listview enter";
2857 if m_active >= 0 && m_active < source#getitemcount
2858 then (
2859 source#exit (coe self) false m_active m_first m_pan "";
2861 else (
2862 source#exit (coe self) true m_active m_first m_pan "";
2865 begin match opt with
2866 | None -> m_prev_uioh
2867 | Some uioh -> uioh
2870 | 127 -> (* delete *)
2871 coe self
2873 | _ -> dolog "unknown key %d" key; coe self
2875 method private special1 key =
2876 let itemcount = source#getitemcount in
2877 let find start incr =
2878 let rec find i =
2879 if i = -1 || i = itemcount
2880 then -1
2881 else (
2882 if source#hasaction i
2883 then i
2884 else find (i + incr)
2887 find start
2889 let set active first =
2890 let first = bound first 0 (itemcount - fstate.maxrows) in
2891 state.text <- "";
2892 coe {< m_active = active; m_first = first >}
2894 let navigate incr =
2895 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2896 let active, first =
2897 let incr1 = if incr > 0 then 1 else -1 in
2898 if isvisible m_first m_active
2899 then
2900 let next =
2901 let next = m_active + incr in
2902 let next =
2903 if next < 0 || next >= itemcount
2904 then -1
2905 else find next incr1
2907 if next = -1 || abs (m_active - next) > fstate.maxrows
2908 then -1
2909 else next
2911 if next = -1
2912 then
2913 let first = m_first + incr in
2914 let first = bound first 0 (itemcount - 1) in
2915 let next =
2916 let next = m_active + incr in
2917 let next = bound next 0 (itemcount - 1) in
2918 find next ~-incr1
2920 let active = if next = -1 then m_active else next in
2921 active, first
2922 else
2923 let first = min next m_first in
2924 let first =
2925 if abs (next - first) > fstate.maxrows
2926 then first + incr
2927 else first
2929 next, first
2930 else
2931 let first = m_first + incr in
2932 let first = bound first 0 (itemcount - 1) in
2933 let active =
2934 let next = m_active + incr in
2935 let next = bound next 0 (itemcount - 1) in
2936 let next = find next incr1 in
2937 let active =
2938 if next = -1 || abs (m_active - first) > fstate.maxrows
2939 then (
2940 let active = if m_active = -1 then next else m_active in
2941 active
2943 else next
2945 if isvisible first active
2946 then active
2947 else -1
2949 active, first
2951 G.postRedisplay "listview navigate";
2952 set active first;
2954 begin match key with
2955 | Glut.KEY_UP -> navigate ~-1
2956 | Glut.KEY_DOWN -> navigate 1
2957 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2958 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2960 | Glut.KEY_RIGHT ->
2961 state.text <- "";
2962 G.postRedisplay "listview right";
2963 coe {< m_pan = m_pan - 1 >}
2965 | Glut.KEY_LEFT ->
2966 state.text <- "";
2967 G.postRedisplay "listview left";
2968 coe {< m_pan = m_pan + 1 >}
2970 | Glut.KEY_HOME ->
2971 let active = find 0 1 in
2972 G.postRedisplay "listview home";
2973 set active 0;
2975 | Glut.KEY_END ->
2976 let first = max 0 (itemcount - fstate.maxrows) in
2977 let active = find (itemcount - 1) ~-1 in
2978 G.postRedisplay "listview end";
2979 set active first;
2981 | _ -> coe self
2982 end;
2984 method key key =
2985 match state.mode with
2986 | Textentry te -> textentrykeyboard key te; coe self
2987 | _ -> self#key1 key
2989 method special key =
2990 match state.mode with
2991 | Textentry te -> textentryspecial key te; coe self
2992 | _ -> self#special1 key
2994 method button button bstate x y =
2995 let opt =
2996 match button with
2997 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollbw ->
2998 G.postRedisplay "listview scroll";
2999 if bstate = Glut.DOWN
3000 then
3001 let _, position, sh = self#scrollph in
3002 if y > truncate position && y < truncate (position +. sh)
3003 then (
3004 state.mstate <- Mscrolly;
3005 Some (coe self)
3007 else
3008 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3009 let first = truncate (s *. float source#getitemcount) in
3010 let first = min source#getitemcount first in
3011 Some (coe {< m_first = first; m_active = first >})
3012 else (
3013 state.mstate <- Mnone;
3014 Some (coe self);
3016 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3017 begin match self#elemunder y with
3018 | Some n ->
3019 G.postRedisplay "listview click";
3020 source#exit
3021 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3022 | _ ->
3023 Some (coe self)
3025 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
3026 let len = source#getitemcount in
3027 let first =
3028 if n = 4 && m_first + fstate.maxrows >= len
3029 then
3030 m_first
3031 else
3032 let first = m_first + (if n == 3 then -1 else 1) in
3033 bound first 0 (len - 1)
3035 G.postRedisplay "listview wheel";
3036 Some (coe {< m_first = first >})
3037 | _ ->
3038 Some (coe self)
3040 match opt with
3041 | None -> m_prev_uioh
3042 | Some uioh -> uioh
3044 method motion _ y =
3045 match state.mstate with
3046 | Mscrolly ->
3047 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3048 let first = truncate (s *. float source#getitemcount) in
3049 let first = min source#getitemcount first in
3050 G.postRedisplay "listview motion";
3051 coe {< m_first = first; m_active = first >}
3052 | _ -> coe self
3054 method pmotion x y =
3055 if x < conf.winw - conf.scrollbw
3056 then
3057 let n =
3058 match self#elemunder y with
3059 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
3060 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
3062 let o =
3063 if n != m_active
3064 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3065 else self
3067 coe o
3068 else (
3069 Glut.setCursor Glut.CURSOR_INHERIT;
3070 coe self
3073 method infochanged _ = ()
3075 method scrollpw = (0, 0.0, 0.0)
3076 method scrollph =
3077 let nfs = fstate.fontsize + 1 in
3078 let y = m_first * nfs in
3079 let itemcount = source#getitemcount in
3080 let maxi = max 0 (itemcount - fstate.maxrows) in
3081 let maxy = maxi * nfs in
3082 let p, h = scrollph y maxy in
3083 conf.scrollbw, p, h
3084 end;;
3086 class outlinelistview ~source =
3087 object (self)
3088 inherit listview ~source:(source :> lvsource) ~trusted:false as super
3090 method key key =
3091 match key with
3092 | 14 -> (* ctrl-n *)
3093 source#narrow m_qsearch;
3094 G.postRedisplay "outline ctrl-n";
3095 coe {< m_first = 0; m_active = 0 >}
3097 | 21 -> (* ctrl-u *)
3098 source#denarrow;
3099 G.postRedisplay "outline ctrl-u";
3100 state.text <- "";
3101 coe {< m_first = 0; m_active = 0 >}
3103 | 12 -> (* ctrl-l *)
3104 let first = m_active - (fstate.maxrows / 2) in
3105 G.postRedisplay "outline ctrl-l";
3106 coe {< m_first = first >}
3108 | 127 -> (* delete *)
3109 source#remove m_active;
3110 G.postRedisplay "outline delete";
3111 let active = max 0 (m_active-1) in
3112 coe {< m_first = firstof m_first active;
3113 m_active = active >}
3115 | key -> super#key key
3117 method special key =
3118 let calcfirst first active =
3119 if active > first
3120 then
3121 let rows = active - first in
3122 if rows > fstate.maxrows then active - fstate.maxrows else first
3123 else active
3125 let navigate incr =
3126 let active = m_active + incr in
3127 let active = bound active 0 (source#getitemcount - 1) in
3128 let first = calcfirst m_first active in
3129 G.postRedisplay "special outline navigate";
3130 coe {< m_active = active; m_first = first >}
3132 match key with
3133 | Glut.KEY_UP -> navigate ~-1
3134 | Glut.KEY_DOWN -> navigate 1
3135 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
3136 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
3138 | Glut.KEY_RIGHT ->
3139 let o =
3140 if Glut.getModifiers () land Glut.active_ctrl != 0
3141 then (
3142 G.postRedisplay "special outline right";
3143 {< m_pan = m_pan + 1 >}
3145 else self#updownlevel 1
3147 coe o
3149 | Glut.KEY_LEFT ->
3150 let o =
3151 if Glut.getModifiers () land Glut.active_ctrl != 0
3152 then (
3153 G.postRedisplay "special outline left";
3154 {< m_pan = m_pan - 1 >}
3156 else self#updownlevel ~-1
3158 coe o
3160 | Glut.KEY_HOME ->
3161 G.postRedisplay "special outline home";
3162 coe {< m_first = 0; m_active = 0 >}
3164 | Glut.KEY_END ->
3165 let active = source#getitemcount - 1 in
3166 let first = max 0 (active - fstate.maxrows) in
3167 G.postRedisplay "special outline end";
3168 coe {< m_active = active; m_first = first >}
3170 | _ -> super#special key
3173 let outlinesource usebookmarks =
3174 let empty = [||] in
3175 (object
3176 inherit lvsourcebase
3177 val mutable m_items = empty
3178 val mutable m_orig_items = empty
3179 val mutable m_prev_items = empty
3180 val mutable m_narrow_pattern = ""
3181 val mutable m_hadremovals = false
3183 method getitemcount =
3184 Array.length m_items + (if m_hadremovals then 1 else 0)
3186 method getitem n =
3187 if n == Array.length m_items && m_hadremovals
3188 then
3189 ("[Confirm removal]", 0)
3190 else
3191 let s, n, _ = m_items.(n) in
3192 (s, n)
3194 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3195 ignore (uioh, first, qsearch);
3196 let confrimremoval = m_hadremovals && active = Array.length m_items in
3197 let items =
3198 if String.length m_narrow_pattern = 0
3199 then m_orig_items
3200 else m_items
3202 if not cancel
3203 then (
3204 if not confrimremoval
3205 then(
3206 let _, _, anchor = m_items.(active) in
3207 gotoanchor anchor;
3208 m_items <- items;
3210 else (
3211 state.bookmarks <- Array.to_list m_items;
3212 m_orig_items <- m_items;
3215 else m_items <- items;
3216 m_pan <- pan;
3217 None
3219 method hasaction _ = true
3221 method greetmsg =
3222 if Array.length m_items != Array.length m_orig_items
3223 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3224 else ""
3226 method narrow pattern =
3227 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3228 match reopt with
3229 | None -> ()
3230 | Some re ->
3231 let rec loop accu n =
3232 if n = -1
3233 then (
3234 m_narrow_pattern <- pattern;
3235 m_items <- Array.of_list accu
3237 else
3238 let (s, _, _) as o = m_items.(n) in
3239 let accu =
3240 if (try ignore (Str.search_forward re s 0); true
3241 with Not_found -> false)
3242 then o :: accu
3243 else accu
3245 loop accu (n-1)
3247 loop [] (Array.length m_items - 1)
3249 method denarrow =
3250 m_orig_items <- (
3251 if usebookmarks
3252 then Array.of_list state.bookmarks
3253 else state.outlines
3255 m_items <- m_orig_items
3257 method remove m =
3258 if usebookmarks
3259 then
3260 if m >= 0 && m < Array.length m_items
3261 then (
3262 m_hadremovals <- true;
3263 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3264 let n = if n >= m then n+1 else n in
3265 m_items.(n)
3269 method reset anchor items =
3270 m_hadremovals <- false;
3271 if m_orig_items == empty || m_prev_items != items
3272 then (
3273 m_orig_items <- items;
3274 if String.length m_narrow_pattern = 0
3275 then m_items <- items;
3277 m_prev_items <- items;
3278 let rely = getanchory anchor in
3279 let active =
3280 let rec loop n best bestd =
3281 if n = Array.length m_items
3282 then best
3283 else
3284 let (_, _, anchor) = m_items.(n) in
3285 let orely = getanchory anchor in
3286 let d = abs (orely - rely) in
3287 if d < bestd
3288 then loop (n+1) n d
3289 else loop (n+1) best bestd
3291 loop 0 ~-1 max_int
3293 m_active <- active;
3294 m_first <- firstof m_first active
3295 end)
3298 let enterselector usebookmarks =
3299 let source = outlinesource usebookmarks in
3300 fun errmsg ->
3301 let outlines =
3302 if usebookmarks
3303 then Array.of_list state.bookmarks
3304 else state.outlines
3306 if Array.length outlines = 0
3307 then (
3308 showtext ' ' errmsg;
3310 else (
3311 state.text <- source#greetmsg;
3312 Glut.setCursor Glut.CURSOR_INHERIT;
3313 let anchor = getanchor () in
3314 source#reset anchor outlines;
3315 state.uioh <- coe (new outlinelistview ~source);
3316 G.postRedisplay "enter selector";
3320 let enteroutlinemode =
3321 let f = enterselector false in
3322 fun ()-> f "Document has no outline";
3325 let enterbookmarkmode =
3326 let f = enterselector true in
3327 fun () -> f "Document has no bookmarks (yet)";
3330 let color_of_string s =
3331 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3332 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3336 let color_to_string (r, g, b) =
3337 let r = truncate (r *. 256.0)
3338 and g = truncate (g *. 256.0)
3339 and b = truncate (b *. 256.0) in
3340 Printf.sprintf "%d/%d/%d" r g b
3343 let irect_of_string s =
3344 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3347 let irect_to_string (x0,y0,x1,y1) =
3348 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3351 let makecheckers () =
3352 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3353 following to say:
3354 converted by Issac Trotts. July 25, 2002 *)
3355 let image_height = 64
3356 and image_width = 64 in
3358 let make_image () =
3359 let image =
3360 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3362 for i = 0 to image_width - 1 do
3363 for j = 0 to image_height - 1 do
3364 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3365 (if (i land 8 ) lxor (j land 8) = 0
3366 then [|255;255;255|] else [|200;200;200|])
3367 done
3368 done;
3369 image
3371 let image = make_image () in
3372 let id = GlTex.gen_texture () in
3373 GlTex.bind_texture `texture_2d id;
3374 GlPix.store (`unpack_alignment 1);
3375 GlTex.image2d image;
3376 List.iter (GlTex.parameter ~target:`texture_2d)
3377 [ `wrap_s `repeat;
3378 `wrap_t `repeat;
3379 `mag_filter `nearest;
3380 `min_filter `nearest ];
3384 let setcheckers enabled =
3385 match state.texid with
3386 | None ->
3387 if enabled then state.texid <- Some (makecheckers ())
3389 | Some texid ->
3390 if not enabled
3391 then (
3392 GlTex.delete_texture texid;
3393 state.texid <- None;
3397 let int_of_string_with_suffix s =
3398 let l = String.length s in
3399 let s1, shift =
3400 if l > 1
3401 then
3402 let suffix = Char.lowercase s.[l-1] in
3403 match suffix with
3404 | 'k' -> String.sub s 0 (l-1), 10
3405 | 'm' -> String.sub s 0 (l-1), 20
3406 | 'g' -> String.sub s 0 (l-1), 30
3407 | _ -> s, 0
3408 else s, 0
3410 let n = int_of_string s1 in
3411 let m = n lsl shift in
3412 if m < 0 || m < n
3413 then raise (Failure "value too large")
3414 else m
3417 let string_with_suffix_of_int n =
3418 if n = 0
3419 then "0"
3420 else
3421 let n, s =
3422 if n = 0
3423 then 0, ""
3424 else (
3425 if n land ((1 lsl 20) - 1) = 0
3426 then n lsr 20, "M"
3427 else (
3428 if n land ((1 lsl 10) - 1) = 0
3429 then n lsr 10, "K"
3430 else n, ""
3434 let rec loop s n =
3435 let h = n mod 1000 in
3436 let n = n / 1000 in
3437 if n = 0
3438 then string_of_int h ^ s
3439 else (
3440 let s = Printf.sprintf "_%03d%s" h s in
3441 loop s n
3444 loop "" n ^ s;
3447 let defghyllscroll = (40, 8, 32);;
3448 let ghyllscroll_of_string s =
3449 let (n, a, b) as nab =
3450 if s = "default"
3451 then defghyllscroll
3452 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3454 if n <= a || n <= b || a >= b
3455 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3456 nab;
3459 let ghyllscroll_to_string ((n, a, b) as nab) =
3460 if nab = defghyllscroll
3461 then "default"
3462 else Printf.sprintf "%d,%d,%d" n a b;
3465 let describe_location () =
3466 let f (fn, _) l =
3467 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3469 let fn, ln = List.fold_left f (-1, -1) state.layout in
3470 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3471 let percent =
3472 if maxy <= 0
3473 then 100.
3474 else (100. *. (float state.y /. float maxy))
3476 if fn = ln
3477 then
3478 Printf.sprintf "page %d of %d [%.2f%%]"
3479 (fn+1) state.pagecount percent
3480 else
3481 Printf.sprintf
3482 "pages %d-%d of %d [%.2f%%]"
3483 (fn+1) (ln+1) state.pagecount percent
3486 let enterinfomode =
3487 let btos b = if b then "\xe2\x88\x9a" else "" in
3488 let showextended = ref false in
3489 let leave mode = function
3490 | Confirm -> state.mode <- mode
3491 | Cancel -> state.mode <- mode in
3492 let src =
3493 (object
3494 val mutable m_first_time = true
3495 val mutable m_l = []
3496 val mutable m_a = [||]
3497 val mutable m_prev_uioh = nouioh
3498 val mutable m_prev_mode = View
3500 inherit lvsourcebase
3502 method reset prev_mode prev_uioh =
3503 m_a <- Array.of_list (List.rev m_l);
3504 m_l <- [];
3505 m_prev_mode <- prev_mode;
3506 m_prev_uioh <- prev_uioh;
3507 if m_first_time
3508 then (
3509 let rec loop n =
3510 if n >= Array.length m_a
3511 then ()
3512 else
3513 match m_a.(n) with
3514 | _, _, _, Action _ -> m_active <- n
3515 | _ -> loop (n+1)
3517 loop 0;
3518 m_first_time <- false;
3521 method int name get set =
3522 m_l <-
3523 (name, `int get, 1, Action (
3524 fun u ->
3525 let ondone s =
3526 try set (int_of_string s)
3527 with exn ->
3528 state.text <- Printf.sprintf "bad integer `%s': %s"
3529 s (Printexc.to_string exn)
3531 state.text <- "";
3532 let te = name ^ ": ", "", None, intentry, ondone in
3533 state.mode <- Textentry (te, leave m_prev_mode);
3535 )) :: m_l
3537 method int_with_suffix name get set =
3538 m_l <-
3539 (name, `intws get, 1, Action (
3540 fun u ->
3541 let ondone s =
3542 try set (int_of_string_with_suffix s)
3543 with exn ->
3544 state.text <- Printf.sprintf "bad integer `%s': %s"
3545 s (Printexc.to_string exn)
3547 state.text <- "";
3548 let te =
3549 name ^ ": ", "", None, intentry_with_suffix, ondone
3551 state.mode <- Textentry (te, leave m_prev_mode);
3553 )) :: m_l
3555 method bool ?(offset=1) ?(btos=btos) name get set =
3556 m_l <-
3557 (name, `bool (btos, get), offset, Action (
3558 fun u ->
3559 let v = get () in
3560 set (not v);
3562 )) :: m_l
3564 method color name get set =
3565 m_l <-
3566 (name, `color get, 1, Action (
3567 fun u ->
3568 let invalid = (nan, nan, nan) in
3569 let ondone s =
3570 let c =
3571 try color_of_string s
3572 with exn ->
3573 state.text <- Printf.sprintf "bad color `%s': %s"
3574 s (Printexc.to_string exn);
3575 invalid
3577 if c <> invalid
3578 then set c;
3580 let te = name ^ ": ", "", None, textentry, ondone in
3581 state.text <- color_to_string (get ());
3582 state.mode <- Textentry (te, leave m_prev_mode);
3584 )) :: m_l
3586 method string name get set =
3587 m_l <-
3588 (name, `string get, 1, Action (
3589 fun u ->
3590 let ondone s = set s in
3591 let te = name ^ ": ", "", None, textentry, ondone in
3592 state.mode <- Textentry (te, leave m_prev_mode);
3594 )) :: m_l
3596 method colorspace name get set =
3597 m_l <-
3598 (name, `string get, 1, Action (
3599 fun _ ->
3600 let source =
3601 let vals = [| "rgb"; "bgr"; "gray" |] in
3602 (object
3603 inherit lvsourcebase
3605 initializer
3606 m_active <- int_of_colorspace conf.colorspace;
3607 m_first <- 0;
3609 method getitemcount = Array.length vals
3610 method getitem n = (vals.(n), 0)
3611 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3612 ignore (uioh, first, pan, qsearch);
3613 if not cancel then set active;
3614 None
3615 method hasaction _ = true
3616 end)
3618 state.text <- "";
3619 coe (new listview ~source ~trusted:true)
3620 )) :: m_l
3622 method caption s offset =
3623 m_l <- (s, `empty, offset, Noaction) :: m_l
3625 method caption2 s f offset =
3626 m_l <- (s, `string f, offset, Noaction) :: m_l
3628 method getitemcount = Array.length m_a
3630 method getitem n =
3631 let tostr = function
3632 | `int f -> string_of_int (f ())
3633 | `intws f -> string_with_suffix_of_int (f ())
3634 | `string f -> f ()
3635 | `color f -> color_to_string (f ())
3636 | `bool (btos, f) -> btos (f ())
3637 | `empty -> ""
3639 let name, t, offset, _ = m_a.(n) in
3640 ((let s = tostr t in
3641 if String.length s > 0
3642 then Printf.sprintf "%s\t%s" name s
3643 else name),
3644 offset)
3646 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3647 let uiohopt =
3648 if not cancel
3649 then (
3650 m_qsearch <- qsearch;
3651 let uioh =
3652 match m_a.(active) with
3653 | _, _, _, Action f -> f uioh
3654 | _ -> uioh
3656 Some uioh
3658 else None
3660 m_active <- active;
3661 m_first <- first;
3662 m_pan <- pan;
3663 uiohopt
3665 method hasaction n =
3666 match m_a.(n) with
3667 | _, _, _, Action _ -> true
3668 | _ -> false
3669 end)
3671 let rec fillsrc prevmode prevuioh =
3672 let sep () = src#caption "" 0 in
3673 let colorp name get set =
3674 src#string name
3675 (fun () -> color_to_string (get ()))
3676 (fun v ->
3678 let c = color_of_string v in
3679 set c
3680 with exn ->
3681 state.text <- Printf.sprintf "bad color `%s': %s"
3682 v (Printexc.to_string exn);
3685 let oldmode = state.mode in
3686 let birdseye = isbirdseye state.mode in
3688 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3690 src#bool "presentation mode"
3691 (fun () -> conf.presentation)
3692 (fun v ->
3693 conf.presentation <- v;
3694 state.anchor <- getanchor ();
3695 represent ());
3697 src#bool "ignore case in searches"
3698 (fun () -> conf.icase)
3699 (fun v -> conf.icase <- v);
3701 src#bool "preload"
3702 (fun () -> conf.preload)
3703 (fun v -> conf.preload <- v);
3705 src#bool "highlight links"
3706 (fun () -> conf.hlinks)
3707 (fun v -> conf.hlinks <- v);
3709 src#bool "under info"
3710 (fun () -> conf.underinfo)
3711 (fun v -> conf.underinfo <- v);
3713 src#bool "persistent bookmarks"
3714 (fun () -> conf.savebmarks)
3715 (fun v -> conf.savebmarks <- v);
3717 src#bool "proportional display"
3718 (fun () -> conf.proportional)
3719 (fun v -> reqlayout conf.angle v);
3721 src#bool "trim margins"
3722 (fun () -> conf.trimmargins)
3723 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3725 src#bool "persistent location"
3726 (fun () -> conf.jumpback)
3727 (fun v -> conf.jumpback <- v);
3729 sep ();
3730 src#int "inter-page space"
3731 (fun () -> conf.interpagespace)
3732 (fun n ->
3733 conf.interpagespace <- n;
3734 let pageno, py =
3735 match state.layout with
3736 | [] -> 0, 0
3737 | l :: _ ->
3738 l.pageno, l.pagey
3740 state.maxy <- calcheight ();
3741 let y = getpagey pageno in
3742 gotoy (y + py)
3745 src#int "page bias"
3746 (fun () -> conf.pagebias)
3747 (fun v -> conf.pagebias <- v);
3749 src#int "scroll step"
3750 (fun () -> conf.scrollstep)
3751 (fun n -> conf.scrollstep <- n);
3753 src#int "auto scroll step"
3754 (fun () ->
3755 match state.autoscroll with
3756 | Some step -> step
3757 | _ -> conf.autoscrollstep)
3758 (fun n ->
3759 if state.autoscroll <> None
3760 then state.autoscroll <- Some n;
3761 conf.autoscrollstep <- n);
3763 src#int "zoom"
3764 (fun () -> truncate (conf.zoom *. 100.))
3765 (fun v -> setzoom ((float v) /. 100.));
3767 src#int "rotation"
3768 (fun () -> conf.angle)
3769 (fun v -> reqlayout v conf.proportional);
3771 src#int "scroll bar width"
3772 (fun () -> state.scrollw)
3773 (fun v ->
3774 state.scrollw <- v;
3775 conf.scrollbw <- v;
3776 reshape conf.winw conf.winh;
3779 src#int "scroll handle height"
3780 (fun () -> conf.scrollh)
3781 (fun v -> conf.scrollh <- v;);
3783 src#int "thumbnail width"
3784 (fun () -> conf.thumbw)
3785 (fun v ->
3786 conf.thumbw <- min 4096 v;
3787 match oldmode with
3788 | Birdseye beye ->
3789 leavebirdseye beye false;
3790 enterbirdseye ()
3791 | _ -> ()
3794 src#string "columns"
3795 (fun () ->
3796 match conf.columns with
3797 | None -> "1"
3798 | Some (multicol, _) -> columns_to_string multicol)
3799 (fun v ->
3800 let n, a, b = columns_of_string v in
3801 setcolumns n a b);
3803 sep ();
3804 src#caption "Presentation mode" 0;
3805 src#bool "scrollbar visible"
3806 (fun () -> conf.scrollbarinpm)
3807 (fun v ->
3808 if v != conf.scrollbarinpm
3809 then (
3810 conf.scrollbarinpm <- v;
3811 if conf.presentation
3812 then (
3813 state.scrollw <- if v then conf.scrollbw else 0;
3814 reshape conf.winw conf.winh;
3819 sep ();
3820 src#caption "Pixmap cache" 0;
3821 src#int_with_suffix "size (advisory)"
3822 (fun () -> conf.memlimit)
3823 (fun v -> conf.memlimit <- v);
3825 src#caption2 "used"
3826 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3827 (string_with_suffix_of_int state.memused)
3828 (Hashtbl.length state.tilemap)) 1;
3830 sep ();
3831 src#caption "Layout" 0;
3832 src#caption2 "Dimension"
3833 (fun () ->
3834 Printf.sprintf "%dx%d (virtual %dx%d)"
3835 conf.winw conf.winh
3836 state.w state.maxy)
3838 if conf.debug
3839 then
3840 src#caption2 "Position" (fun () ->
3841 Printf.sprintf "%dx%d" state.x state.y
3843 else
3844 src#caption2 "Visible" (fun () -> describe_location ()) 1
3847 sep ();
3848 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3849 "Save these parameters as global defaults at exit"
3850 (fun () -> conf.bedefault)
3851 (fun v -> conf.bedefault <- v)
3854 sep ();
3855 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3856 src#bool ~offset:0 ~btos "Extended parameters"
3857 (fun () -> !showextended)
3858 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3859 if !showextended
3860 then (
3861 src#bool "checkers"
3862 (fun () -> conf.checkers)
3863 (fun v -> conf.checkers <- v; setcheckers v);
3864 src#bool "verbose"
3865 (fun () -> conf.verbose)
3866 (fun v -> conf.verbose <- v);
3867 src#bool "invert colors"
3868 (fun () -> conf.invert)
3869 (fun v -> conf.invert <- v);
3870 src#bool "max fit"
3871 (fun () -> conf.maxhfit)
3872 (fun v -> conf.maxhfit <- v);
3873 src#bool "redirect stderr"
3874 (fun () -> conf.redirectstderr)
3875 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3876 src#string "uri launcher"
3877 (fun () -> conf.urilauncher)
3878 (fun v -> conf.urilauncher <- v);
3879 src#string "tile size"
3880 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3881 (fun v ->
3883 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3884 conf.tileh <- max 64 w;
3885 conf.tilew <- max 64 h;
3886 flushtiles ();
3887 with exn ->
3888 state.text <- Printf.sprintf "bad tile size `%s': %s"
3889 v (Printexc.to_string exn));
3890 src#int "texture count"
3891 (fun () -> conf.texcount)
3892 (fun v ->
3893 if realloctexts v
3894 then conf.texcount <- v
3895 else showtext '!' " Failed to set texture count please retry later"
3897 src#int "slice height"
3898 (fun () -> conf.sliceheight)
3899 (fun v ->
3900 conf.sliceheight <- v;
3901 wcmd "sliceh" [`i conf.sliceheight];
3903 src#int "anti-aliasing level"
3904 (fun () -> conf.aalevel)
3905 (fun v ->
3906 conf.aalevel <- bound v 0 8;
3907 state.anchor <- getanchor ();
3908 opendoc state.path state.password;
3910 src#int "ui font size"
3911 (fun () -> fstate.fontsize)
3912 (fun v -> setfontsize (bound v 5 100));
3913 colorp "background color"
3914 (fun () -> conf.bgcolor)
3915 (fun v -> conf.bgcolor <- v);
3916 src#bool "crop hack"
3917 (fun () -> conf.crophack)
3918 (fun v -> conf.crophack <- v);
3919 src#string "trim fuzz"
3920 (fun () -> irect_to_string conf.trimfuzz)
3921 (fun v ->
3923 conf.trimfuzz <- irect_of_string v;
3924 if conf.trimmargins
3925 then settrim true conf.trimfuzz;
3926 with exn ->
3927 state.text <- Printf.sprintf "bad irect `%s': %s"
3928 v (Printexc.to_string exn)
3930 src#string "throttle"
3931 (fun () ->
3932 match conf.maxwait with
3933 | None -> "show place holder if page is not ready"
3934 | Some time ->
3935 if time = infinity
3936 then "wait for page to fully render"
3937 else
3938 "wait " ^ string_of_float time
3939 ^ " seconds before showing placeholder"
3941 (fun v ->
3943 let f = float_of_string v in
3944 if f <= 0.0
3945 then conf.maxwait <- None
3946 else conf.maxwait <- Some f
3947 with exn ->
3948 state.text <- Printf.sprintf "bad time `%s': %s"
3949 v (Printexc.to_string exn)
3951 src#string "ghyll scroll"
3952 (fun () ->
3953 match conf.ghyllscroll with
3954 | None -> ""
3955 | Some nab -> ghyllscroll_to_string nab
3957 (fun v ->
3959 let gs =
3960 if String.length v = 0
3961 then None
3962 else Some (ghyllscroll_of_string v)
3964 conf.ghyllscroll <- gs
3965 with exn ->
3966 state.text <- Printf.sprintf "bad ghyll `%s': %s"
3967 v (Printexc.to_string exn)
3969 src#colorspace "color space"
3970 (fun () -> colorspace_to_string conf.colorspace)
3971 (fun v ->
3972 conf.colorspace <- colorspace_of_int v;
3973 wcmd "cs" [`i v];
3974 load state.layout;
3978 sep ();
3979 src#caption "Document" 0;
3980 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3981 src#caption2 "Pages"
3982 (fun () -> string_of_int state.pagecount) 1;
3983 src#caption2 "Dimensions"
3984 (fun () -> string_of_int (List.length state.pdims)) 1;
3985 if conf.trimmargins
3986 then (
3987 sep ();
3988 src#caption "Trimmed margins" 0;
3989 src#caption2 "Dimensions"
3990 (fun () -> string_of_int (List.length state.pdims)) 1;
3993 src#reset prevmode prevuioh;
3995 fun () ->
3996 state.text <- "";
3997 let prevmode = state.mode
3998 and prevuioh = state.uioh in
3999 fillsrc prevmode prevuioh;
4000 let source = (src :> lvsource) in
4001 state.uioh <- coe (object (self)
4002 inherit listview ~source ~trusted:true as super
4003 val mutable m_prevmemused = 0
4004 method infochanged = function
4005 | Memused ->
4006 if m_prevmemused != state.memused
4007 then (
4008 m_prevmemused <- state.memused;
4009 G.postRedisplay "memusedchanged";
4011 | Pdim -> G.postRedisplay "pdimchanged"
4012 | Docinfo -> fillsrc prevmode prevuioh
4014 method special key =
4015 if Glut.getModifiers () land Glut.active_ctrl = 0
4016 then
4017 match key with
4018 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
4019 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
4020 | _ -> super#special key
4021 else super#special key
4022 end);
4023 G.postRedisplay "info";
4026 let enterhelpmode =
4027 let source =
4028 (object
4029 inherit lvsourcebase
4030 method getitemcount = Array.length state.help
4031 method getitem n =
4032 let s, n, _ = state.help.(n) in
4033 (s, n)
4035 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4036 let optuioh =
4037 if not cancel
4038 then (
4039 m_qsearch <- qsearch;
4040 match state.help.(active) with
4041 | _, _, Action f -> Some (f uioh)
4042 | _ -> Some (uioh)
4044 else None
4046 m_active <- active;
4047 m_first <- first;
4048 m_pan <- pan;
4049 optuioh
4051 method hasaction n =
4052 match state.help.(n) with
4053 | _, _, Action _ -> true
4054 | _ -> false
4056 initializer
4057 m_active <- -1
4058 end)
4059 in fun () ->
4060 state.uioh <- coe (new listview ~source ~trusted:true);
4061 G.postRedisplay "help";
4064 let entermsgsmode =
4065 let msgsource =
4066 let re = Str.regexp "[\r\n]" in
4067 (object
4068 inherit lvsourcebase
4069 val mutable m_items = [||]
4071 method getitemcount = 1 + Array.length m_items
4073 method getitem n =
4074 if n = 0
4075 then "[Clear]", 0
4076 else m_items.(n-1), 0
4078 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4079 ignore uioh;
4080 if not cancel
4081 then (
4082 if active = 0
4083 then Buffer.clear state.errmsgs;
4084 m_qsearch <- qsearch;
4086 m_active <- active;
4087 m_first <- first;
4088 m_pan <- pan;
4089 None
4091 method hasaction n =
4092 n = 0
4094 method reset =
4095 state.newerrmsgs <- false;
4096 let l = Str.split re (Buffer.contents state.errmsgs) in
4097 m_items <- Array.of_list l
4099 initializer
4100 m_active <- 0
4101 end)
4102 in fun () ->
4103 state.text <- "";
4104 msgsource#reset;
4105 let source = (msgsource :> lvsource) in
4106 state.uioh <- coe (object
4107 inherit listview ~source ~trusted:false as super
4108 method display =
4109 if state.newerrmsgs
4110 then msgsource#reset;
4111 super#display
4112 end);
4113 G.postRedisplay "msgs";
4116 let quickbookmark ?title () =
4117 match state.layout with
4118 | [] -> ()
4119 | l :: _ ->
4120 let title =
4121 match title with
4122 | None ->
4123 let sec = Unix.gettimeofday () in
4124 let tm = Unix.localtime sec in
4125 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4126 (l.pageno+1)
4127 tm.Unix.tm_mday
4128 tm.Unix.tm_mon
4129 (tm.Unix.tm_year + 1900)
4130 tm.Unix.tm_hour
4131 tm.Unix.tm_min
4132 | Some title -> title
4134 state.bookmarks <-
4135 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4136 :: state.bookmarks
4139 let doreshape w h =
4140 state.fullscreen <- None;
4141 Glut.reshapeWindow w h;
4144 let viewkeyboard key =
4145 let enttext te =
4146 let mode = state.mode in
4147 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4148 state.text <- "";
4149 enttext ();
4150 G.postRedisplay "view:enttext"
4152 let c = Char.chr key in
4153 match c with
4154 | '\027' | 'q' -> (* escape *)
4155 begin match state.mstate with
4156 | Mzoomrect _ ->
4157 state.mstate <- Mnone;
4158 Glut.setCursor Glut.CURSOR_INHERIT;
4159 G.postRedisplay "kill zoom rect";
4160 | _ ->
4161 raise Quit
4162 end;
4164 | '\008' -> (* backspace *)
4165 let y = getnav ~-1 in
4166 gotoy_and_clear_text y
4168 | 'o' ->
4169 enteroutlinemode ()
4171 | 'u' ->
4172 state.rects <- [];
4173 state.text <- "";
4174 G.postRedisplay "dehighlight";
4176 | '/' | '?' ->
4177 let ondone isforw s =
4178 cbput state.hists.pat s;
4179 state.searchpattern <- s;
4180 search s isforw
4182 let s = String.create 1 in
4183 s.[0] <- c;
4184 enttext (s, "", Some (onhist state.hists.pat),
4185 textentry, ondone (c ='/'))
4187 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4188 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4189 setzoom (conf.zoom +. incr)
4191 | '+' ->
4192 let ondone s =
4193 let n =
4194 try int_of_string s with exc ->
4195 state.text <- Printf.sprintf "bad integer `%s': %s"
4196 s (Printexc.to_string exc);
4197 max_int
4199 if n != max_int
4200 then (
4201 conf.pagebias <- n;
4202 state.text <- "page bias is now " ^ string_of_int n;
4205 enttext ("page bias: ", "", None, intentry, ondone)
4207 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4208 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4209 setzoom (max 0.01 (conf.zoom -. decr))
4211 | '-' ->
4212 let ondone msg = state.text <- msg in
4213 enttext (
4214 "option [acfhilpstvACPRSZTI]: ", "", None,
4215 optentry state.mode, ondone
4218 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4219 setzoom 1.0
4221 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4222 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4223 if zoom < 1.0
4224 then setzoom zoom
4226 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4227 togglebirdseye ()
4229 | '0' .. '9' ->
4230 let ondone s =
4231 let n =
4232 try int_of_string s with exc ->
4233 state.text <- Printf.sprintf "bad integer `%s': %s"
4234 s (Printexc.to_string exc);
4237 if n >= 0
4238 then (
4239 addnav ();
4240 cbput state.hists.pag (string_of_int n);
4241 gotopage1 (n + conf.pagebias - 1) 0;
4244 let pageentry text key =
4245 match Char.unsafe_chr key with
4246 | 'g' -> TEdone text
4247 | _ -> intentry text key
4249 let text = "x" in text.[0] <- c;
4250 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4252 | 'b' ->
4253 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4254 reshape conf.winw conf.winh;
4256 | 'l' ->
4257 conf.hlinks <- not conf.hlinks;
4258 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4259 G.postRedisplay "toggle highlightlinks";
4261 | 'a' ->
4262 begin match state.autoscroll with
4263 | Some step ->
4264 conf.autoscrollstep <- step;
4265 state.autoscroll <- None
4266 | None ->
4267 if conf.autoscrollstep = 0
4268 then state.autoscroll <- Some 1
4269 else state.autoscroll <- Some conf.autoscrollstep
4272 | 'P' ->
4273 conf.presentation <- not conf.presentation;
4274 if conf.presentation
4275 then (
4276 if not conf.scrollbarinpm
4277 then state.scrollw <- 0;
4279 else
4280 state.scrollw <- conf.scrollbw;
4282 showtext ' ' ("presentation mode " ^
4283 if conf.presentation then "on" else "off");
4284 state.anchor <- getanchor ();
4285 represent ()
4287 | 'f' ->
4288 begin match state.fullscreen with
4289 | None ->
4290 state.fullscreen <- Some (conf.winw, conf.winh);
4291 Glut.fullScreen ()
4292 | Some (w, h) ->
4293 state.fullscreen <- None;
4294 doreshape w h
4297 | 'g' ->
4298 gotoy_and_clear_text 0
4300 | 'G' ->
4301 gotopage1 (state.pagecount - 1) 0
4303 | 'n' ->
4304 search state.searchpattern true
4306 | 'p' | 'N' ->
4307 search state.searchpattern false
4309 | 't' ->
4310 begin match state.layout with
4311 | [] -> ()
4312 | l :: _ ->
4313 gotoy_and_clear_text (getpagey l.pageno)
4316 | ' ' ->
4317 begin match List.rev state.layout with
4318 | [] -> ()
4319 | l :: _ ->
4320 let pageno = min (l.pageno+1) (state.pagecount-1) in
4321 gotoy_and_clear_text (getpagey pageno)
4324 | '\127' -> (* del *)
4325 begin match state.layout with
4326 | [] -> ()
4327 | l :: _ ->
4328 let pageno = max 0 (l.pageno-1) in
4329 gotoy_and_clear_text (getpagey pageno)
4332 | '=' ->
4333 showtext ' ' (describe_location ());
4335 | 'w' ->
4336 begin match state.layout with
4337 | [] -> ()
4338 | l :: _ ->
4339 doreshape (l.pagew + state.scrollw) l.pageh;
4340 G.postRedisplay "w"
4343 | '\'' ->
4344 enterbookmarkmode ()
4346 | 'h' ->
4347 enterhelpmode ()
4349 | 'i' ->
4350 enterinfomode ()
4352 | 'e' when conf.redirectstderr ->
4353 entermsgsmode ()
4355 | 'm' ->
4356 let ondone s =
4357 match state.layout with
4358 | l :: _ ->
4359 state.bookmarks <-
4360 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4361 :: state.bookmarks
4362 | _ -> ()
4364 enttext ("bookmark: ", "", None, textentry, ondone)
4366 | '~' ->
4367 quickbookmark ();
4368 showtext ' ' "Quick bookmark added";
4370 | 'z' ->
4371 begin match state.layout with
4372 | l :: _ ->
4373 let rect = getpdimrect l.pagedimno in
4374 let w, h =
4375 if conf.crophack
4376 then
4377 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4378 truncate (1.2 *. (rect.(3) -. rect.(0))))
4379 else
4380 (truncate (rect.(1) -. rect.(0)),
4381 truncate (rect.(3) -. rect.(0)))
4383 let w = truncate ((float w)*.conf.zoom)
4384 and h = truncate ((float h)*.conf.zoom) in
4385 if w != 0 && h != 0
4386 then (
4387 state.anchor <- getanchor ();
4388 doreshape (w + state.scrollw) (h + conf.interpagespace)
4390 G.postRedisplay "z";
4392 | [] -> ()
4395 | '\000' -> (* ctrl-2 *)
4396 let maxw = getmaxw () in
4397 if maxw > 0.0
4398 then setzoom (maxw /. float conf.winw)
4400 | '<' | '>' ->
4401 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
4403 | '[' | ']' ->
4404 conf.colorscale <-
4405 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
4407 G.postRedisplay "brightness";
4409 | 'k' ->
4410 begin match state.mode with
4411 | Birdseye beye -> upbirdseye 1 beye
4412 | _ -> gotoy (clamp (-conf.scrollstep))
4415 | 'j' ->
4416 begin match state.mode with
4417 | Birdseye beye -> downbirdseye 1 beye
4418 | _ -> gotoy (clamp conf.scrollstep)
4421 | 'r' ->
4422 state.anchor <- getanchor ();
4423 opendoc state.path state.password
4425 | 'v' when not conf.debug ->
4426 List.iter debugl state.layout;
4428 | 'v' when conf.debug ->
4429 state.rects <- [];
4430 List.iter (fun l ->
4431 match getopaque l.pageno with
4432 | None -> ()
4433 | Some opaque ->
4434 let x0, y0, x1, y1 = pagebbox opaque in
4435 let a,b = float x0, float y0 in
4436 let c,d = float x1, float y0 in
4437 let e,f = float x1, float y1 in
4438 let h,j = float x0, float y1 in
4439 let rect = (a,b,c,d,e,f,h,j) in
4440 debugrect rect;
4441 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4442 ) state.layout;
4443 G.postRedisplay "v";
4445 | _ ->
4446 vlog "huh? %d %c" key (Char.chr key);
4449 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
4450 match key with
4451 | 27 -> (* escape *)
4452 leavebirdseye beye true
4454 | 12 -> (* ctrl-l *)
4455 let y, h = getpageyh pageno in
4456 let top = (conf.winh - h) / 2 in
4457 gotoy (max 0 (y - top))
4459 | 13 -> (* enter *)
4460 leavebirdseye beye false
4462 | _ ->
4463 viewkeyboard key
4466 let keyboard ~key ~x ~y =
4467 ignore x;
4468 ignore y;
4469 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
4470 then wcmd "interrupt" []
4471 else state.uioh <- state.uioh#key key
4474 let birdseyespecial key ((oconf, leftx, _, hooverpageno, anchor) as beye) =
4475 let incr =
4476 match conf.columns with
4477 | None -> 1
4478 | Some ((c, _, _), _) -> c
4480 match key with
4481 | Glut.KEY_UP -> upbirdseye incr beye
4482 | Glut.KEY_DOWN -> downbirdseye incr beye
4483 | Glut.KEY_LEFT -> upbirdseye 1 beye
4484 | Glut.KEY_RIGHT -> downbirdseye 1 beye
4486 | Glut.KEY_PAGE_UP ->
4487 begin match state.layout with
4488 | l :: _ ->
4489 if l.pagey != 0
4490 then (
4491 state.mode <- Birdseye (
4492 oconf, leftx, l.pageno, hooverpageno, anchor
4494 gotopage1 l.pageno 0;
4496 else (
4497 let layout = layout (state.y-conf.winh) conf.winh in
4498 match layout with
4499 | [] -> gotoy (clamp (-conf.winh))
4500 | l :: _ ->
4501 state.mode <- Birdseye (
4502 oconf, leftx, l.pageno, hooverpageno, anchor
4504 gotopage1 l.pageno 0
4507 | [] -> gotoy (clamp (-conf.winh))
4508 end;
4510 | Glut.KEY_PAGE_DOWN ->
4511 begin match List.rev state.layout with
4512 | l :: _ ->
4513 let layout = layout (state.y + conf.winh) conf.winh in
4514 begin match layout with
4515 | [] ->
4516 let incr = l.pageh - l.pagevh in
4517 if incr = 0
4518 then (
4519 state.mode <-
4520 Birdseye (
4521 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4523 G.postRedisplay "birdseye pagedown";
4525 else gotoy (clamp (incr + conf.interpagespace*2));
4527 | l :: _ ->
4528 state.mode <-
4529 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4530 gotopage1 l.pageno 0;
4533 | [] -> gotoy (clamp conf.winh)
4534 end;
4536 | Glut.KEY_HOME ->
4537 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4538 gotopage1 0 0
4540 | Glut.KEY_END ->
4541 let pageno = state.pagecount - 1 in
4542 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4543 if not (pagevisible state.layout pageno)
4544 then
4545 let h =
4546 match List.rev state.pdims with
4547 | [] -> conf.winh
4548 | (_, _, h, _) :: _ -> h
4550 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4551 else G.postRedisplay "birdseye end";
4552 | _ -> ()
4555 let setautoscrollspeed step goingdown =
4556 let incr = max 1 ((abs step) / 2) in
4557 let incr = if goingdown then incr else -incr in
4558 let astep = step + incr in
4559 state.autoscroll <- Some astep;
4562 let special ~key ~x ~y =
4563 ignore x;
4564 ignore y;
4565 state.uioh <- state.uioh#special key
4568 let drawpage l =
4569 let color =
4570 match state.mode with
4571 | Textentry _ -> scalecolor 0.4
4572 | View -> scalecolor 1.0
4573 | Birdseye (_, _, pageno, hooverpageno, _) ->
4574 if l.pageno = hooverpageno
4575 then scalecolor 0.9
4576 else (
4577 if l.pageno = pageno
4578 then scalecolor 1.0
4579 else scalecolor 0.8
4582 drawtiles l color;
4583 begin match getopaque l.pageno with
4584 | Some opaque ->
4585 if tileready l l.pagex l.pagey
4586 then
4587 let x = l.pagedispx - l.pagex
4588 and y = l.pagedispy - l.pagey in
4589 postprocess opaque conf.hlinks x y;
4591 | _ -> ()
4592 end;
4595 let scrollindicator () =
4596 let sbw, ph, sh = state.uioh#scrollph in
4597 let sbh, pw, sw = state.uioh#scrollpw in
4599 GlDraw.color (0.64, 0.64, 0.64);
4600 GlDraw.rect
4601 (float (conf.winw - sbw), 0.)
4602 (float conf.winw, float conf.winh)
4604 GlDraw.rect
4605 (0., float (conf.winh - sbh))
4606 (float (conf.winw - state.scrollw - 1), float conf.winh)
4608 GlDraw.color (0.0, 0.0, 0.0);
4610 GlDraw.rect
4611 (float (conf.winw - sbw), ph)
4612 (float conf.winw, ph +. sh)
4614 GlDraw.rect
4615 (pw, float (conf.winh - sbh))
4616 (pw +. sw, float conf.winh)
4620 let pagetranslatepoint l x y =
4621 let dy = y - l.pagedispy in
4622 let y = dy + l.pagey in
4623 let dx = x - l.pagedispx in
4624 let x = dx + l.pagex in
4625 (x, y);
4628 let showsel () =
4629 match state.mstate with
4630 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4633 | Msel ((x0, y0), (x1, y1)) ->
4634 let rec loop = function
4635 | l :: ls ->
4636 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4637 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4638 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4639 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4640 then
4641 match getopaque l.pageno with
4642 | Some opaque ->
4643 let dx, dy = pagetranslatepoint l 0 0 in
4644 let x0 = x0 + dx
4645 and y0 = y0 + dy
4646 and x1 = x1 + dx
4647 and y1 = y1 + dy in
4648 GlMat.mode `modelview;
4649 GlMat.push ();
4650 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4651 seltext opaque (x0, y0, x1, y1);
4652 GlMat.pop ();
4653 | _ -> ()
4654 else loop ls
4655 | [] -> ()
4657 loop state.layout
4660 let showrects () =
4661 Gl.enable `blend;
4662 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4663 GlDraw.polygon_mode `both `fill;
4664 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4665 List.iter
4666 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4667 List.iter (fun l ->
4668 if l.pageno = pageno
4669 then (
4670 let dx = float (l.pagedispx - l.pagex) in
4671 let dy = float (l.pagedispy - l.pagey) in
4672 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4673 GlDraw.begins `quads;
4675 GlDraw.vertex2 (x0+.dx, y0+.dy);
4676 GlDraw.vertex2 (x1+.dx, y1+.dy);
4677 GlDraw.vertex2 (x2+.dx, y2+.dy);
4678 GlDraw.vertex2 (x3+.dx, y3+.dy);
4680 GlDraw.ends ();
4682 ) state.layout
4683 ) state.rects
4685 Gl.disable `blend;
4688 let display () =
4689 GlClear.color (scalecolor2 conf.bgcolor);
4690 GlClear.clear [`color];
4691 List.iter drawpage state.layout;
4692 showrects ();
4693 showsel ();
4694 state.uioh#display;
4695 scrollindicator ();
4696 begin match state.mstate with
4697 | Mzoomrect ((x0, y0), (x1, y1)) ->
4698 Gl.enable `blend;
4699 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4700 GlDraw.polygon_mode `both `fill;
4701 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4702 GlDraw.rect (float x0, float y0)
4703 (float x1, float y1);
4704 Gl.disable `blend;
4705 | _ -> ()
4706 end;
4707 enttext ();
4708 Glut.swapBuffers ();
4711 let getunder x y =
4712 let rec f = function
4713 | l :: rest ->
4714 begin match getopaque l.pageno with
4715 | Some opaque ->
4716 let x0 = l.pagedispx in
4717 let x1 = x0 + l.pagevw in
4718 let y0 = l.pagedispy in
4719 let y1 = y0 + l.pagevh in
4720 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4721 then
4722 let px, py = pagetranslatepoint l x y in
4723 match whatsunder opaque px py with
4724 | Unone -> f rest
4725 | under -> under
4726 else f rest
4727 | _ ->
4728 f rest
4730 | [] -> Unone
4732 f state.layout
4735 let zoomrect x y x1 y1 =
4736 let x0 = min x x1
4737 and x1 = max x x1
4738 and y0 = min y y1 in
4739 gotoy (state.y + y0);
4740 state.anchor <- getanchor ();
4741 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4742 let margin =
4743 if state.w < conf.winw - state.scrollw
4744 then (conf.winw - state.scrollw - state.w) / 2
4745 else 0
4747 state.x <- (state.x + margin) - x0;
4748 setzoom zoom;
4749 Glut.setCursor Glut.CURSOR_INHERIT;
4750 state.mstate <- Mnone;
4753 let scrollx x =
4754 let winw = conf.winw - state.scrollw - 1 in
4755 let s = float x /. float winw in
4756 let destx = truncate (float (state.w + winw) *. s) in
4757 state.x <- winw - destx;
4758 gotoy_and_clear_text state.y;
4759 state.mstate <- Mscrollx;
4762 let scrolly y =
4763 let s = float y /. float conf.winh in
4764 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4765 gotoy_and_clear_text desty;
4766 state.mstate <- Mscrolly;
4769 let viewmouse button bstate x y =
4770 match button with
4771 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4772 if Glut.getModifiers () land Glut.active_ctrl != 0
4773 then (
4774 match state.mstate with
4775 | Mzoom (oldn, i) ->
4776 if oldn = n
4777 then (
4778 if i = 2
4779 then
4780 let incr =
4781 match n with
4782 | 4 ->
4783 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4784 | _ ->
4785 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4787 let zoom = conf.zoom -. incr in
4788 setzoom zoom;
4789 state.mstate <- Mzoom (n, 0);
4790 else
4791 state.mstate <- Mzoom (n, i+1);
4793 else state.mstate <- Mzoom (n, 0)
4795 | _ -> state.mstate <- Mzoom (n, 0)
4797 else (
4798 match state.autoscroll with
4799 | Some step -> setautoscrollspeed step (n=4)
4800 | None ->
4801 let incr =
4802 if n = 3
4803 then -conf.scrollstep
4804 else conf.scrollstep
4806 let incr = incr * 2 in
4807 let y = clamp incr in
4808 gotoy_and_clear_text y
4811 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4812 if bstate = Glut.DOWN
4813 then (
4814 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4815 state.mstate <- Mpan (x, y)
4817 else
4818 state.mstate <- Mnone
4820 | Glut.RIGHT_BUTTON ->
4821 if bstate = Glut.DOWN
4822 then (
4823 Glut.setCursor Glut.CURSOR_CYCLE;
4824 let p = (x, y) in
4825 state.mstate <- Mzoomrect (p, p)
4827 else (
4828 match state.mstate with
4829 | Mzoomrect ((x0, y0), _) ->
4830 if abs (x-x0) > 10 && abs (y - y0) > 10
4831 then zoomrect x0 y0 x y
4832 else (
4833 state.mstate <- Mnone;
4834 Glut.setCursor Glut.CURSOR_INHERIT;
4835 G.postRedisplay "kill accidental zoom rect";
4837 | _ ->
4838 Glut.setCursor Glut.CURSOR_INHERIT;
4839 state.mstate <- Mnone
4842 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4843 if bstate = Glut.DOWN
4844 then
4845 let _, position, sh = state.uioh#scrollph in
4846 if y > truncate position && y < truncate (position +. sh)
4847 then state.mstate <- Mscrolly
4848 else scrolly y
4849 else
4850 state.mstate <- Mnone
4852 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4853 if bstate = Glut.DOWN
4854 then
4855 let _, position, sw = state.uioh#scrollpw in
4856 if x > truncate position && x < truncate (position +. sw)
4857 then state.mstate <- Mscrollx
4858 else scrollx x
4859 else
4860 state.mstate <- Mnone
4862 | Glut.LEFT_BUTTON ->
4863 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4864 begin match dest with
4865 | Ulinkgoto (pageno, top) ->
4866 if pageno >= 0
4867 then (
4868 addnav ();
4869 gotopage1 pageno top;
4872 | Ulinkuri s ->
4873 gotouri s
4875 | Uremote _ | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
4877 | Unone when bstate = Glut.DOWN ->
4878 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4879 state.mstate <- Mpan (x, y);
4881 | Unone | Utext _ ->
4882 if bstate = Glut.DOWN
4883 then (
4884 if conf.angle mod 360 = 0
4885 then (
4886 state.mstate <- Msel ((x, y), (x, y));
4887 G.postRedisplay "mouse select";
4890 else (
4891 match state.mstate with
4892 | Mnone -> ()
4894 | Mzoom _ | Mscrollx | Mscrolly ->
4895 state.mstate <- Mnone
4897 | Mzoomrect ((x0, y0), _) ->
4898 zoomrect x0 y0 x y
4900 | Mpan _ ->
4901 Glut.setCursor Glut.CURSOR_INHERIT;
4902 state.mstate <- Mnone
4904 | Msel ((_, y0), (_, y1)) ->
4905 let f l =
4906 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4907 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4908 then
4909 match getopaque l.pageno with
4910 | Some opaque ->
4911 copysel opaque
4912 | _ -> ()
4914 List.iter f state.layout;
4915 copysel ""; (* ugly *)
4916 Glut.setCursor Glut.CURSOR_INHERIT;
4917 state.mstate <- Mnone;
4921 | _ -> ()
4924 let birdseyemouse button bstate x y
4925 (conf, leftx, _, hooverpageno, anchor) =
4926 match button with
4927 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4928 let rec loop = function
4929 | [] -> ()
4930 | l :: rest ->
4931 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4932 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4933 then (
4934 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4936 else loop rest
4938 loop state.layout
4939 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4940 | _ -> ()
4943 let mouse bstate button x y =
4944 state.uioh <- state.uioh#button button bstate x y;
4947 let mouse ~button ~state ~x ~y = mouse state button x y;;
4949 let motion ~x ~y =
4950 state.uioh <- state.uioh#motion x y
4953 let pmotion ~x ~y =
4954 state.uioh <- state.uioh#pmotion x y;
4957 let uioh = object
4958 method display = ()
4960 method key key =
4961 begin match state.mode with
4962 | Textentry textentry -> textentrykeyboard key textentry
4963 | Birdseye birdseye -> birdseyekeyboard key birdseye
4964 | View -> viewkeyboard key
4965 end;
4966 state.uioh
4968 method special key =
4969 begin match state.mode with
4970 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4971 togglebirdseye ()
4973 | Birdseye vals ->
4974 birdseyespecial key vals
4976 | View when key = Glut.KEY_F1 ->
4977 enterhelpmode ()
4979 | View ->
4980 begin match state.autoscroll with
4981 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4982 setautoscrollspeed step (key = Glut.KEY_DOWN)
4984 | _ ->
4985 let y =
4986 match key with
4987 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4988 | Glut.KEY_UP ->
4989 if Glut.getModifiers () land Glut.active_ctrl != 0
4990 then
4991 if Glut.getModifiers () land Glut.active_shift != 0
4992 then (setzoom state.prevzoom; state.y)
4993 else clamp (-conf.winh/2)
4994 else clamp (-conf.scrollstep)
4995 | Glut.KEY_DOWN ->
4996 if Glut.getModifiers () land Glut.active_ctrl != 0
4997 then
4998 if Glut.getModifiers () land Glut.active_shift != 0
4999 then (setzoom state.prevzoom; state.y)
5000 else clamp (conf.winh/2)
5001 else clamp (conf.scrollstep)
5002 | Glut.KEY_PAGE_UP ->
5003 if Glut.getModifiers () land Glut.active_ctrl != 0
5004 then
5005 match state.layout with
5006 | [] -> state.y
5007 | l :: _ -> state.y - l.pagey
5008 else
5009 clamp (-conf.winh)
5010 | Glut.KEY_PAGE_DOWN ->
5011 if Glut.getModifiers () land Glut.active_ctrl != 0
5012 then
5013 match List.rev state.layout with
5014 | [] -> state.y
5015 | l :: _ -> getpagey l.pageno
5016 else
5017 clamp conf.winh
5018 | Glut.KEY_HOME ->
5019 addnav ();
5021 | Glut.KEY_END ->
5022 addnav ();
5023 state.maxy - (if conf.maxhfit then conf.winh else 0)
5025 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
5026 Glut.getModifiers () land Glut.active_alt != 0 ->
5027 getnav (if key = Glut.KEY_LEFT then 1 else -1)
5029 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
5030 let dx =
5031 if Glut.getModifiers () land Glut.active_ctrl != 0
5032 then (conf.winw / 2)
5033 else 10
5035 state.x <- state.x - dx;
5036 state.y
5037 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
5038 let dx =
5039 if Glut.getModifiers () land Glut.active_ctrl != 0
5040 then (conf.winw / 2)
5041 else 10
5043 state.x <- state.x + dx;
5044 state.y
5046 | _ -> state.y
5048 if abs (state.y - y) > conf.scrollstep*2
5049 then gotoghyll y
5050 else gotoy_and_clear_text y
5053 | Textentry te -> textentryspecial key te
5054 end;
5055 state.uioh
5057 method button button bstate x y =
5058 begin match state.mode with
5059 | View -> viewmouse button bstate x y
5060 | Birdseye beye -> birdseyemouse button bstate x y beye
5061 | Textentry _ -> ()
5062 end;
5063 state.uioh
5065 method motion x y =
5066 begin match state.mode with
5067 | Textentry _ -> ()
5068 | View | Birdseye _ ->
5069 match state.mstate with
5070 | Mzoom _ | Mnone -> ()
5072 | Mpan (x0, y0) ->
5073 let dx = x - x0
5074 and dy = y0 - y in
5075 state.mstate <- Mpan (x, y);
5076 if conf.zoom > 1.0 then state.x <- state.x + dx;
5077 let y = clamp dy in
5078 gotoy_and_clear_text y
5080 | Msel (a, _) ->
5081 state.mstate <- Msel (a, (x, y));
5082 G.postRedisplay "motion select";
5084 | Mscrolly ->
5085 let y = min conf.winh (max 0 y) in
5086 scrolly y
5088 | Mscrollx ->
5089 let x = min conf.winw (max 0 x) in
5090 scrollx x
5092 | Mzoomrect (p0, _) ->
5093 state.mstate <- Mzoomrect (p0, (x, y));
5094 G.postRedisplay "motion zoomrect";
5095 end;
5096 state.uioh
5098 method pmotion x y =
5099 begin match state.mode with
5100 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5101 let rec loop = function
5102 | [] ->
5103 if hooverpageno != -1
5104 then (
5105 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5106 G.postRedisplay "pmotion birdseye no hoover";
5108 | l :: rest ->
5109 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5110 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5111 then (
5112 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5113 G.postRedisplay "pmotion birdseye hoover";
5115 else loop rest
5117 loop state.layout
5119 | Textentry _ -> ()
5121 | View ->
5122 match state.mstate with
5123 | Mnone ->
5124 begin match getunder x y with
5125 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
5126 | Ulinkuri uri ->
5127 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
5128 Glut.setCursor Glut.CURSOR_INFO
5129 | Ulinkgoto (page, _) ->
5130 if conf.underinfo
5131 then showtext 'p' ("age: " ^ string_of_int (page+1));
5132 Glut.setCursor Glut.CURSOR_INFO
5133 | Utext s ->
5134 if conf.underinfo then showtext 'f' ("ont: " ^ s);
5135 Glut.setCursor Glut.CURSOR_TEXT
5136 | Uunexpected s ->
5137 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
5138 Glut.setCursor Glut.CURSOR_INHERIT
5139 | Ulaunch s ->
5140 if conf.underinfo then showtext 'l' ("launch: " ^ s);
5141 Glut.setCursor Glut.CURSOR_INHERIT
5142 | Unamed s ->
5143 if conf.underinfo then showtext 'n' ("named: " ^ s);
5144 Glut.setCursor Glut.CURSOR_INHERIT
5145 | Uremote s ->
5146 if conf.underinfo then showtext 'r' ("emote: " ^ s);
5147 Glut.setCursor Glut.CURSOR_INHERIT
5150 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5152 end;
5153 state.uioh
5155 method infochanged _ = ()
5157 method scrollph =
5158 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5159 let p, h = scrollph state.y maxy in
5160 state.scrollw, p, h
5162 method scrollpw =
5163 let winw = conf.winw - state.scrollw - 1 in
5164 let fwinw = float winw in
5165 let sw =
5166 let sw = fwinw /. float state.w in
5167 let sw = fwinw *. sw in
5168 max sw (float conf.scrollh)
5170 let position, sw =
5171 let f = state.w+winw in
5172 let r = float (winw-state.x) /. float f in
5173 let p = fwinw *. r in
5174 p-.sw/.2., sw
5176 let sw =
5177 if position +. sw > fwinw
5178 then fwinw -. position
5179 else sw
5181 state.hscrollh, position, sw
5182 end;;
5184 module Config =
5185 struct
5186 open Parser
5188 let fontpath = ref "";;
5189 let wmclasshack = ref false;;
5191 let unent s =
5192 let l = String.length s in
5193 let b = Buffer.create l in
5194 unent b s 0 l;
5195 Buffer.contents b;
5198 let home =
5200 match platform with
5201 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
5202 | _ -> Sys.getenv "HOME"
5203 with exn ->
5204 prerr_endline
5205 ("Can not determine home directory location: " ^
5206 Printexc.to_string exn);
5210 let config_of c attrs =
5211 let apply c k v =
5213 match k with
5214 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5215 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5216 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5217 | "preload" -> { c with preload = bool_of_string v }
5218 | "page-bias" -> { c with pagebias = int_of_string v }
5219 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5220 | "auto-scroll-step" ->
5221 { c with autoscrollstep = max 0 (int_of_string v) }
5222 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5223 | "crop-hack" -> { c with crophack = bool_of_string v }
5224 | "throttle" ->
5225 let mw =
5226 match String.lowercase v with
5227 | "true" -> Some infinity
5228 | "false" -> None
5229 | f -> Some (float_of_string f)
5231 { c with maxwait = mw}
5232 | "highlight-links" -> { c with hlinks = bool_of_string v }
5233 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5234 | "vertical-margin" ->
5235 { c with interpagespace = max 0 (int_of_string v) }
5236 | "zoom" ->
5237 let zoom = float_of_string v /. 100. in
5238 let zoom = max zoom 0.0 in
5239 { c with zoom = zoom }
5240 | "presentation" -> { c with presentation = bool_of_string v }
5241 | "rotation-angle" -> { c with angle = int_of_string v }
5242 | "width" -> { c with winw = max 20 (int_of_string v) }
5243 | "height" -> { c with winh = max 20 (int_of_string v) }
5244 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5245 | "proportional-display" -> { c with proportional = bool_of_string v }
5246 | "pixmap-cache-size" ->
5247 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5248 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5249 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5250 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5251 | "persistent-location" -> { c with jumpback = bool_of_string v }
5252 | "background-color" -> { c with bgcolor = color_of_string v }
5253 | "scrollbar-in-presentation" ->
5254 { c with scrollbarinpm = bool_of_string v }
5255 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5256 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5257 | "mupdf-store-size" ->
5258 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5259 | "checkers" -> { c with checkers = bool_of_string v }
5260 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5261 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5262 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5263 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
5264 | "uri-launcher" -> { c with urilauncher = unent v }
5265 | "color-space" -> { c with colorspace = colorspace_of_string v }
5266 | "invert-colors" -> { c with invert = bool_of_string v }
5267 | "brightness" -> { c with colorscale = float_of_string v }
5268 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5269 | "ghyllscroll" ->
5270 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5271 | "columns" ->
5272 let nab = columns_of_string v in
5273 { c with columns = Some (nab, [||]) }
5274 | "birds-eye-columns" ->
5275 { c with beyecolumns = Some (max (int_of_string v) 2) }
5276 | _ -> c
5277 with exn ->
5278 prerr_endline ("Error processing attribute (`" ^
5279 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5282 let rec fold c = function
5283 | [] -> c
5284 | (k, v) :: rest ->
5285 let c = apply c k v in
5286 fold c rest
5288 fold c attrs;
5291 let fromstring f pos n v d =
5292 try f v
5293 with exn ->
5294 dolog "Error processing attribute (%S=%S) at %d\n%s"
5295 n v pos (Printexc.to_string exn)
5300 let bookmark_of attrs =
5301 let rec fold title page rely = function
5302 | ("title", v) :: rest -> fold v page rely rest
5303 | ("page", v) :: rest -> fold title v rely rest
5304 | ("rely", v) :: rest -> fold title page v rest
5305 | _ :: rest -> fold title page rely rest
5306 | [] -> title, page, rely
5308 fold "invalid" "0" "0" attrs
5311 let doc_of attrs =
5312 let rec fold path page rely pan = function
5313 | ("path", v) :: rest -> fold v page rely pan rest
5314 | ("page", v) :: rest -> fold path v rely pan rest
5315 | ("rely", v) :: rest -> fold path page v pan rest
5316 | ("pan", v) :: rest -> fold path page rely v rest
5317 | _ :: rest -> fold path page rely pan rest
5318 | [] -> path, page, rely, pan
5320 fold "" "0" "0" "0" attrs
5323 let setconf dst src =
5324 dst.scrollbw <- src.scrollbw;
5325 dst.scrollh <- src.scrollh;
5326 dst.icase <- src.icase;
5327 dst.preload <- src.preload;
5328 dst.pagebias <- src.pagebias;
5329 dst.verbose <- src.verbose;
5330 dst.scrollstep <- src.scrollstep;
5331 dst.maxhfit <- src.maxhfit;
5332 dst.crophack <- src.crophack;
5333 dst.autoscrollstep <- src.autoscrollstep;
5334 dst.maxwait <- src.maxwait;
5335 dst.hlinks <- src.hlinks;
5336 dst.underinfo <- src.underinfo;
5337 dst.interpagespace <- src.interpagespace;
5338 dst.zoom <- src.zoom;
5339 dst.presentation <- src.presentation;
5340 dst.angle <- src.angle;
5341 dst.winw <- src.winw;
5342 dst.winh <- src.winh;
5343 dst.savebmarks <- src.savebmarks;
5344 dst.memlimit <- src.memlimit;
5345 dst.proportional <- src.proportional;
5346 dst.texcount <- src.texcount;
5347 dst.sliceheight <- src.sliceheight;
5348 dst.thumbw <- src.thumbw;
5349 dst.jumpback <- src.jumpback;
5350 dst.bgcolor <- src.bgcolor;
5351 dst.scrollbarinpm <- src.scrollbarinpm;
5352 dst.tilew <- src.tilew;
5353 dst.tileh <- src.tileh;
5354 dst.mustoresize <- src.mustoresize;
5355 dst.checkers <- src.checkers;
5356 dst.aalevel <- src.aalevel;
5357 dst.trimmargins <- src.trimmargins;
5358 dst.trimfuzz <- src.trimfuzz;
5359 dst.urilauncher <- src.urilauncher;
5360 dst.colorspace <- src.colorspace;
5361 dst.invert <- src.invert;
5362 dst.colorscale <- src.colorscale;
5363 dst.redirectstderr <- src.redirectstderr;
5364 dst.ghyllscroll <- src.ghyllscroll;
5365 dst.columns <- src.columns;
5366 dst.beyecolumns <- src.beyecolumns;
5369 let get s =
5370 let h = Hashtbl.create 10 in
5371 let dc = { defconf with angle = defconf.angle } in
5372 let rec toplevel v t spos _ =
5373 match t with
5374 | Vdata | Vcdata | Vend -> v
5375 | Vopen ("llppconfig", _, closed) ->
5376 if closed
5377 then v
5378 else { v with f = llppconfig }
5379 | Vopen _ ->
5380 error "unexpected subelement at top level" s spos
5381 | Vclose _ -> error "unexpected close at top level" s spos
5383 and llppconfig v t spos _ =
5384 match t with
5385 | Vdata | Vcdata -> v
5386 | Vend -> error "unexpected end of input in llppconfig" s spos
5387 | Vopen ("defaults", attrs, closed) ->
5388 let c = config_of dc attrs in
5389 setconf dc c;
5390 if closed
5391 then v
5392 else { v with f = skip "defaults" (fun () -> v) }
5394 | Vopen ("ui-font", attrs, closed) ->
5395 let rec getsize size = function
5396 | [] -> size
5397 | ("size", v) :: rest ->
5398 let size =
5399 fromstring int_of_string spos "size" v fstate.fontsize in
5400 getsize size rest
5401 | l -> getsize size l
5403 fstate.fontsize <- getsize fstate.fontsize attrs;
5404 if closed
5405 then v
5406 else { v with f = uifont (Buffer.create 10) }
5408 | Vopen ("doc", attrs, closed) ->
5409 let pathent, spage, srely, span = doc_of attrs in
5410 let path = unent pathent
5411 and pageno = fromstring int_of_string spos "page" spage 0
5412 and rely = fromstring float_of_string spos "rely" srely 0.0
5413 and pan = fromstring int_of_string spos "pan" span 0 in
5414 let c = config_of dc attrs in
5415 let anchor = (pageno, rely) in
5416 if closed
5417 then (Hashtbl.add h path (c, [], pan, anchor); v)
5418 else { v with f = doc path pan anchor c [] }
5420 | Vopen _ ->
5421 error "unexpected subelement in llppconfig" s spos
5423 | Vclose "llppconfig" -> { v with f = toplevel }
5424 | Vclose _ -> error "unexpected close in llppconfig" s spos
5426 and uifont b v t spos epos =
5427 match t with
5428 | Vdata | Vcdata ->
5429 Buffer.add_substring b s spos (epos - spos);
5431 | Vopen (_, _, _) ->
5432 error "unexpected subelement in ui-font" s spos
5433 | Vclose "ui-font" ->
5434 if String.length !fontpath = 0
5435 then fontpath := Buffer.contents b;
5436 { v with f = llppconfig }
5437 | Vclose _ -> error "unexpected close in ui-font" s spos
5438 | Vend -> error "unexpected end of input in ui-font" s spos
5440 and doc path pan anchor c bookmarks v t spos _ =
5441 match t with
5442 | Vdata | Vcdata -> v
5443 | Vend -> error "unexpected end of input in doc" s spos
5444 | Vopen ("bookmarks", _, closed) ->
5445 if closed
5446 then v
5447 else { v with f = pbookmarks path pan anchor c bookmarks }
5449 | Vopen (_, _, _) ->
5450 error "unexpected subelement in doc" s spos
5452 | Vclose "doc" ->
5453 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5454 { v with f = llppconfig }
5456 | Vclose _ -> error "unexpected close in doc" s spos
5458 and pbookmarks path pan anchor c bookmarks v t spos _ =
5459 match t with
5460 | Vdata | Vcdata -> v
5461 | Vend -> error "unexpected end of input in bookmarks" s spos
5462 | Vopen ("item", attrs, closed) ->
5463 let titleent, spage, srely = bookmark_of attrs in
5464 let page = fromstring int_of_string spos "page" spage 0
5465 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5466 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5467 if closed
5468 then { v with f = pbookmarks path pan anchor c bookmarks }
5469 else
5470 let f () = v in
5471 { v with f = skip "item" f }
5473 | Vopen _ ->
5474 error "unexpected subelement in bookmarks" s spos
5476 | Vclose "bookmarks" ->
5477 { v with f = doc path pan anchor c bookmarks }
5479 | Vclose _ -> error "unexpected close in bookmarks" s spos
5481 and skip tag f v t spos _ =
5482 match t with
5483 | Vdata | Vcdata -> v
5484 | Vend ->
5485 error ("unexpected end of input in skipped " ^ tag) s spos
5486 | Vopen (tag', _, closed) ->
5487 if closed
5488 then v
5489 else
5490 let f' () = { v with f = skip tag f } in
5491 { v with f = skip tag' f' }
5492 | Vclose ctag ->
5493 if tag = ctag
5494 then f ()
5495 else error ("unexpected close in skipped " ^ tag) s spos
5498 parse { f = toplevel; accu = () } s;
5499 h, dc;
5502 let do_load f ic =
5504 let len = in_channel_length ic in
5505 let s = String.create len in
5506 really_input ic s 0 len;
5507 f s;
5508 with
5509 | Parse_error (msg, s, pos) ->
5510 let subs = subs s pos in
5511 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5512 failwith ("parse error: " ^ s)
5514 | exn ->
5515 failwith ("config load error: " ^ Printexc.to_string exn)
5518 let defconfpath =
5519 let dir =
5521 let dir = Filename.concat home ".config" in
5522 if Sys.is_directory dir then dir else home
5523 with _ -> home
5525 Filename.concat dir "llpp.conf"
5528 let confpath = ref defconfpath;;
5530 let load1 f =
5531 if Sys.file_exists !confpath
5532 then
5533 match
5534 (try Some (open_in_bin !confpath)
5535 with exn ->
5536 prerr_endline
5537 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5538 Printexc.to_string exn);
5539 None
5541 with
5542 | Some ic ->
5543 begin try
5544 f (do_load get ic)
5545 with exn ->
5546 prerr_endline
5547 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5548 Printexc.to_string exn);
5549 end;
5550 close_in ic;
5552 | None -> ()
5553 else
5554 f (Hashtbl.create 0, defconf)
5557 let load () =
5558 let f (h, dc) =
5559 let pc, pb, px, pa =
5561 Hashtbl.find h (Filename.basename state.path)
5562 with Not_found -> dc, [], 0, (0, 0.0)
5564 setconf defconf dc;
5565 setconf conf pc;
5566 state.bookmarks <- pb;
5567 state.x <- px;
5568 state.scrollw <- conf.scrollbw;
5569 if conf.jumpback
5570 then state.anchor <- pa;
5571 cbput state.hists.nav pa;
5573 load1 f
5576 let add_attrs bb always dc c =
5577 let ob s a b =
5578 if always || a != b
5579 then Printf.bprintf bb "\n %s='%b'" s a
5580 and oi s a b =
5581 if always || a != b
5582 then Printf.bprintf bb "\n %s='%d'" s a
5583 and oI s a b =
5584 if always || a != b
5585 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5586 and oz s a b =
5587 if always || a <> b
5588 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5589 and oF s a b =
5590 if always || a <> b
5591 then Printf.bprintf bb "\n %s='%f'" s a
5592 and oc s a b =
5593 if always || a <> b
5594 then
5595 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5596 and oC s a b =
5597 if always || a <> b
5598 then
5599 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5600 and oR s a b =
5601 if always || a <> b
5602 then
5603 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5604 and os s a b =
5605 if always || a <> b
5606 then
5607 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5608 and og s a b =
5609 if always || a <> b
5610 then
5611 match a with
5612 | None -> ()
5613 | Some (_N, _A, _B) ->
5614 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5615 and oW s a b =
5616 if always || a <> b
5617 then
5618 let v =
5619 match a with
5620 | None -> "false"
5621 | Some f ->
5622 if f = infinity
5623 then "true"
5624 else string_of_float f
5626 Printf.bprintf bb "\n %s='%s'" s v
5627 and oco s a b =
5628 if always || a <> b
5629 then
5630 match a with
5631 | Some ((n, a, b), _) when n > 1 ->
5632 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5633 | _ -> ()
5634 and obeco s a b =
5635 if always || a <> b
5636 then
5637 match a with
5638 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5639 | _ -> ()
5641 let w, h =
5642 if always
5643 then dc.winw, dc.winh
5644 else
5645 match state.fullscreen with
5646 | Some wh -> wh
5647 | None -> c.winw, c.winh
5649 let zoom, presentation, interpagespace, maxwait =
5650 if always
5651 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5652 else
5653 match state.mode with
5654 | Birdseye (bc, _, _, _, _) ->
5655 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5656 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5658 oi "width" w dc.winw;
5659 oi "height" h dc.winh;
5660 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5661 oi "scroll-handle-height" c.scrollh dc.scrollh;
5662 ob "case-insensitive-search" c.icase dc.icase;
5663 ob "preload" c.preload dc.preload;
5664 oi "page-bias" c.pagebias dc.pagebias;
5665 oi "scroll-step" c.scrollstep dc.scrollstep;
5666 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5667 ob "max-height-fit" c.maxhfit dc.maxhfit;
5668 ob "crop-hack" c.crophack dc.crophack;
5669 oW "throttle" maxwait dc.maxwait;
5670 ob "highlight-links" c.hlinks dc.hlinks;
5671 ob "under-cursor-info" c.underinfo dc.underinfo;
5672 oi "vertical-margin" interpagespace dc.interpagespace;
5673 oz "zoom" zoom dc.zoom;
5674 ob "presentation" presentation dc.presentation;
5675 oi "rotation-angle" c.angle dc.angle;
5676 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5677 ob "proportional-display" c.proportional dc.proportional;
5678 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5679 oi "tex-count" c.texcount dc.texcount;
5680 oi "slice-height" c.sliceheight dc.sliceheight;
5681 oi "thumbnail-width" c.thumbw dc.thumbw;
5682 ob "persistent-location" c.jumpback dc.jumpback;
5683 oc "background-color" c.bgcolor dc.bgcolor;
5684 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5685 oi "tile-width" c.tilew dc.tilew;
5686 oi "tile-height" c.tileh dc.tileh;
5687 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5688 ob "checkers" c.checkers dc.checkers;
5689 oi "aalevel" c.aalevel dc.aalevel;
5690 ob "trim-margins" c.trimmargins dc.trimmargins;
5691 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5692 os "uri-launcher" c.urilauncher dc.urilauncher;
5693 oC "color-space" c.colorspace dc.colorspace;
5694 ob "invert-colors" c.invert dc.invert;
5695 oF "brightness" c.colorscale dc.colorscale;
5696 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5697 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
5698 oco "columns" c.columns dc.columns;
5699 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
5700 if always
5701 then ob "wmclass-hack" !wmclasshack false;
5704 let save () =
5705 let uifontsize = fstate.fontsize in
5706 let bb = Buffer.create 32768 in
5707 let f (h, dc) =
5708 let dc = if conf.bedefault then conf else dc in
5709 Buffer.add_string bb "<llppconfig>\n";
5711 if String.length !fontpath > 0
5712 then
5713 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5714 uifontsize
5715 !fontpath
5716 else (
5717 if uifontsize <> 14
5718 then
5719 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5722 Buffer.add_string bb "<defaults ";
5723 add_attrs bb true dc dc;
5724 Buffer.add_string bb "/>\n";
5726 let adddoc path pan anchor c bookmarks =
5727 if bookmarks == [] && c = dc && anchor = emptyanchor
5728 then ()
5729 else (
5730 Printf.bprintf bb "<doc path='%s'"
5731 (enent path 0 (String.length path));
5733 if anchor <> emptyanchor
5734 then (
5735 let n, y = anchor in
5736 Printf.bprintf bb " page='%d'" n;
5737 if y > 1e-6
5738 then
5739 Printf.bprintf bb " rely='%f'" y
5743 if pan != 0
5744 then Printf.bprintf bb " pan='%d'" pan;
5746 add_attrs bb false dc c;
5748 begin match bookmarks with
5749 | [] -> Buffer.add_string bb "/>\n"
5750 | _ ->
5751 Buffer.add_string bb ">\n<bookmarks>\n";
5752 List.iter (fun (title, _level, (page, rely)) ->
5753 Printf.bprintf bb
5754 "<item title='%s' page='%d'"
5755 (enent title 0 (String.length title))
5756 page
5758 if rely > 1e-6
5759 then
5760 Printf.bprintf bb " rely='%f'" rely
5762 Buffer.add_string bb "/>\n";
5763 ) bookmarks;
5764 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5765 end;
5769 let pan, conf =
5770 match state.mode with
5771 | Birdseye (c, pan, _, _, _) ->
5772 let beyecolumns =
5773 match conf.columns with
5774 | Some ((c, _, _), _) -> Some c
5775 | None -> None
5776 and columns =
5777 match c.columns with
5778 | Some (c, _) -> Some (c, [||])
5779 | None -> None
5781 pan, { c with beyecolumns = beyecolumns; columns = columns }
5782 | _ -> state.x, conf
5784 let basename = Filename.basename state.path in
5785 adddoc basename pan (getanchor ())
5786 { conf with
5787 autoscrollstep =
5788 match state.autoscroll with
5789 | Some step -> step
5790 | None -> conf.autoscrollstep }
5791 (if conf.savebmarks then state.bookmarks else []);
5793 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5794 if basename <> path
5795 then adddoc path x y c bookmarks
5796 ) h;
5797 Buffer.add_string bb "</llppconfig>";
5799 load1 f;
5800 if Buffer.length bb > 0
5801 then
5803 let tmp = !confpath ^ ".tmp" in
5804 let oc = open_out_bin tmp in
5805 Buffer.output_buffer oc bb;
5806 close_out oc;
5807 Unix.rename tmp !confpath;
5808 with exn ->
5809 prerr_endline
5810 ("error while saving configuration: " ^ Printexc.to_string exn)
5812 end;;
5814 let () =
5815 Arg.parse
5816 (Arg.align
5817 [("-p", Arg.String (fun s -> state.password <- s) ,
5818 "<password> Set password");
5820 ("-f", Arg.String (fun s -> Config.fontpath := s),
5821 "<path> Set path to the user interface font");
5823 ("-c", Arg.String (fun s -> Config.confpath := s),
5824 "<path> Set path to the configuration file");
5826 ("-v", Arg.Unit (fun () ->
5827 Printf.printf
5828 "%s\nconfiguration path: %s\n"
5829 (version ())
5830 Config.defconfpath
5832 exit 0), " Print version and exit");
5835 (fun s -> state.path <- s)
5836 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5838 if String.length state.path = 0
5839 then (prerr_endline "file name missing"; exit 1);
5841 Config.load ();
5843 let _ = Glut.init Sys.argv in
5844 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5845 let () = Glut.initWindowSize conf.winw conf.winh in
5846 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5848 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5849 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5850 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5852 let csock, ssock =
5853 if not is_windows
5854 then
5855 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5856 else
5857 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5858 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5859 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5860 Unix.bind sock addr;
5861 Unix.listen sock 1;
5862 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5863 Unix.connect csock addr;
5864 let ssock, _ = Unix.accept sock in
5865 Unix.close sock;
5866 let opts sock =
5867 Unix.setsockopt sock Unix.TCP_NODELAY true;
5868 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5870 opts ssock;
5871 opts csock;
5872 ssock, csock
5875 let () = Glut.displayFunc display in
5876 let () = Glut.reshapeFunc reshape in
5877 let () = Glut.keyboardFunc keyboard in
5878 let () = Glut.specialFunc special in
5879 let () = Glut.idleFunc (Some idle) in
5880 let () = Glut.mouseFunc mouse in
5881 let () = Glut.motionFunc motion in
5882 let () = Glut.passiveMotionFunc pmotion in
5884 setcheckers conf.checkers;
5885 init ssock (
5886 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5887 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5888 !Config.wmclasshack, !Config.fontpath
5890 state.csock <- csock;
5891 state.ssock <- ssock;
5892 state.text <- "Opening " ^ state.path;
5893 setaalevel conf.aalevel;
5894 writeopen state.path state.password;
5895 state.uioh <- uioh;
5896 setfontsize fstate.fontsize;
5898 redirectstderr ();
5900 while true do
5902 Glut.mainLoop ();
5903 with
5904 | Glut.BadEnum "key in special_of_int" ->
5905 showtext '!' " LablGlut bug: special key not recognized";
5907 | Quit ->
5908 wcmd "quit" [];
5909 Config.save ();
5910 exit 0
5912 | exn when conf.redirectstderr ->
5913 let s =
5914 Printf.sprintf "exception %s\n%s"
5915 (Printexc.to_string exn)
5916 (Printexc.get_backtrace ())
5918 ignore (try
5919 Unix.single_write state.stderr s 0 (String.length s);
5920 with _ -> 0);
5921 exit 1
5922 done;