Add -C
[llpp.git] / main.ml
blob242c8136d3702abc7f1fdd1dceeed0b5a1b9e0d6
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
9 let now = Unix.gettimeofday;;
11 exception Quit;;
13 type params = (angle * proportional * trimparams
14 * texcount * sliceheight * memsize
15 * colorspace * wmclasshack * fontpath)
16 and pageno = int
17 and width = int
18 and height = int
19 and leftx = int
20 and opaque = string
21 and recttype = int
22 and pixmapsize = int
23 and angle = int
24 and proportional = bool
25 and trimmargins = bool
26 and interpagespace = int
27 and texcount = int
28 and sliceheight = int
29 and gen = int
30 and top = float
31 and fontpath = string
32 and memsize = int
33 and aalevel = int
34 and wmclasshack = bool
35 and irect = (int * int * int * int)
36 and trimparams = (trimmargins * irect)
37 and colorspace = | Rgb | Bgr | Gray
40 type platform = | Punknown | Plinux | Pwindows | Posx | Psun
41 | Pfreebsd | Pdragonflybsd | Popenbsd | Pmingw | Pcygwin;;
43 external init : Unix.file_descr -> params -> unit = "ml_init";;
44 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
45 external copysel : string -> unit = "ml_copysel";;
46 external getpdimrect : int -> float array = "ml_getpdimrect";;
47 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
48 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
49 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
50 external measurestr : int -> string -> float = "ml_measure_string";;
51 external getmaxw : unit -> float = "ml_getmaxw";;
52 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
53 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
54 external platform : unit -> platform = "ml_platform";;
55 external setaalevel : int -> unit = "ml_setaalevel";;
56 external realloctexts : int -> bool = "ml_realloctexts";;
58 let platform_to_string = function
59 | Punknown -> "unknown"
60 | Plinux -> "Linux"
61 | Pwindows -> "Windows"
62 | Posx -> "OSX"
63 | Psun -> "Sun"
64 | Pfreebsd -> "FreeBSD"
65 | Pdragonflybsd -> "DragonflyBSD"
66 | Popenbsd -> "OpenBSD"
67 | Pcygwin -> "Cygwin"
68 | Pmingw -> "MingW"
71 let platform = platform ();;
73 let is_windows =
74 match platform with
75 | Pwindows | Pmingw -> true
76 | _ -> false
79 type x = int
80 and y = int
81 and tilex = int
82 and tiley = int
83 and tileparams = (x * y * width * height * tilex * tiley)
86 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
88 type mpos = int * int
89 and mstate =
90 | Msel of (mpos * mpos)
91 | Mpan of mpos
92 | Mscrolly | Mscrollx
93 | Mzoom of (int * int)
94 | Mzoomrect of (mpos * mpos)
95 | Mnone
98 type textentry = string * string * onhist option * onkey * ondone
99 and onkey = string -> int -> te
100 and ondone = string -> unit
101 and histcancel = unit -> unit
102 and onhist = ((histcmd -> string) * histcancel)
103 and histcmd = HCnext | HCprev | HCfirst | HClast
104 and te =
105 | TEstop
106 | TEdone of string
107 | TEcont of string
108 | TEswitch of textentry
111 type 'a circbuf =
112 { store : 'a array
113 ; mutable rc : int
114 ; mutable wc : int
115 ; mutable len : int
119 let bound v minv maxv =
120 max minv (min maxv v);
123 let cbnew n v =
124 { store = Array.create n v
125 ; rc = 0
126 ; wc = 0
127 ; len = 0
131 let drawstring size x y s =
132 Gl.enable `blend;
133 Gl.enable `texture_2d;
134 ignore (drawstr size x y s);
135 Gl.disable `blend;
136 Gl.disable `texture_2d;
139 let drawstring1 size x y s =
140 drawstr size x y s;
143 let drawstring2 size x y fmt =
144 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
147 let cbcap b = Array.length b.store;;
149 let cbput b v =
150 let cap = cbcap b in
151 b.store.(b.wc) <- v;
152 b.wc <- (b.wc + 1) mod cap;
153 b.rc <- b.wc;
154 b.len <- min (b.len + 1) cap;
157 let cbempty b = b.len = 0;;
159 let cbgetg b circular dir =
160 if cbempty b
161 then b.store.(0)
162 else
163 let rc = b.rc + dir in
164 let rc =
165 if circular
166 then (
167 if rc = -1
168 then b.len-1
169 else (
170 if rc = b.len
171 then 0
172 else rc
175 else max 0 (min rc (b.len-1))
177 b.rc <- rc;
178 b.store.(rc);
181 let cbget b = cbgetg b false;;
182 let cbgetc b = cbgetg b true;;
184 type page =
185 { pageno : int
186 ; pagedimno : int
187 ; pagew : int
188 ; pageh : int
189 ; pagex : int
190 ; pagey : int
191 ; pagevw : int
192 ; pagevh : int
193 ; pagedispx : int
194 ; pagedispy : int
198 let debugl l =
199 dolog "l %d dim=%d {" l.pageno l.pagedimno;
200 dolog " WxH %dx%d" l.pagew l.pageh;
201 dolog " vWxH %dx%d" l.pagevw l.pagevh;
202 dolog " pagex,y %d,%d" l.pagex l.pagey;
203 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
204 dolog "}";
207 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
208 dolog "rect {";
209 dolog " x0,y0=(% f, % f)" x0 y0;
210 dolog " x1,y1=(% f, % f)" x1 y1;
211 dolog " x2,y2=(% f, % f)" x2 y2;
212 dolog " x3,y3=(% f, % f)" x3 y3;
213 dolog "}";
216 type columns =
217 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
218 and multicol = columncount * covercount * covercount
219 and pdimno = int
220 and columncount = int
221 and covercount = int;;
223 type conf =
224 { mutable scrollbw : int
225 ; mutable scrollh : int
226 ; mutable icase : bool
227 ; mutable preload : bool
228 ; mutable pagebias : int
229 ; mutable verbose : bool
230 ; mutable debug : bool
231 ; mutable scrollstep : int
232 ; mutable maxhfit : bool
233 ; mutable crophack : bool
234 ; mutable autoscrollstep : int
235 ; mutable maxwait : float option
236 ; mutable hlinks : bool
237 ; mutable underinfo : bool
238 ; mutable interpagespace : interpagespace
239 ; mutable zoom : float
240 ; mutable presentation : bool
241 ; mutable angle : angle
242 ; mutable winw : int
243 ; mutable winh : int
244 ; mutable savebmarks : bool
245 ; mutable proportional : proportional
246 ; mutable trimmargins : trimmargins
247 ; mutable trimfuzz : irect
248 ; mutable memlimit : memsize
249 ; mutable texcount : texcount
250 ; mutable sliceheight : sliceheight
251 ; mutable thumbw : width
252 ; mutable jumpback : bool
253 ; mutable bgcolor : float * float * float
254 ; mutable bedefault : bool
255 ; mutable scrollbarinpm : bool
256 ; mutable tilew : int
257 ; mutable tileh : int
258 ; mutable mustoresize : memsize
259 ; mutable checkers : bool
260 ; mutable aalevel : int
261 ; mutable urilauncher : string
262 ; mutable colorspace : colorspace
263 ; mutable invert : bool
264 ; mutable colorscale : float
265 ; mutable redirectstderr : bool
266 ; mutable ghyllscroll : (int * int * int) option
267 ; mutable columns : columns option
268 ; mutable beyecolumns : columncount option
272 type anchor = pageno * top;;
274 type outline = string * int * anchor;;
276 type rect = float * float * float * float * float * float * float * float;;
278 type tile = opaque * pixmapsize * elapsed
279 and elapsed = float;;
280 type pagemapkey = pageno * gen;;
281 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
282 and row = int
283 and col = int;;
285 let emptyanchor = (0, 0.0);;
287 type infochange = | Memused | Docinfo | Pdim;;
289 class type uioh = object
290 method display : unit
291 method key : int -> uioh
292 method special : Glut.special_key_t -> uioh
293 method button :
294 Glut.button_t -> Glut.mouse_button_state_t -> int -> int -> uioh
295 method motion : int -> int -> uioh
296 method pmotion : int -> int -> uioh
297 method infochanged : infochange -> unit
298 method scrollpw : (int * float * float)
299 method scrollph : (int * float * float)
300 end;;
302 type mode =
303 | Birdseye of (conf * leftx * pageno * pageno * anchor)
304 | Textentry of (textentry * onleave)
305 | View
306 and onleave = leavetextentrystatus -> unit
307 and leavetextentrystatus = | Cancel | Confirm
308 and helpitem = string * int * action
309 and action =
310 | Noaction
311 | Action of (uioh -> uioh)
314 let isbirdseye = function Birdseye _ -> true | _ -> false;;
315 let istextentry = function Textentry _ -> true | _ -> false;;
317 type currently =
318 | Idle
319 | Loading of (page * gen)
320 | Tiling of (
321 page * opaque * colorspace * angle * gen * col * row * width * height
323 | Outlining of outline list
326 let nouioh : uioh = object (self)
327 method display = ()
328 method key _ = self
329 method special _ = self
330 method button _ _ _ _ = self
331 method motion _ _ = self
332 method pmotion _ _ = self
333 method infochanged _ = ()
334 method scrollpw = (0, nan, nan)
335 method scrollph = (0, nan, nan)
336 end;;
338 type state =
339 { mutable csock : Unix.file_descr
340 ; mutable ssock : Unix.file_descr
341 ; mutable errfd : Unix.file_descr option
342 ; mutable stderr : Unix.file_descr
343 ; mutable errmsgs : Buffer.t
344 ; mutable newerrmsgs : bool
345 ; mutable w : int
346 ; mutable x : int
347 ; mutable y : int
348 ; mutable scrollw : int
349 ; mutable hscrollh : int
350 ; mutable anchor : anchor
351 ; mutable maxy : int
352 ; mutable layout : page list
353 ; pagemap : (pagemapkey, opaque) Hashtbl.t
354 ; tilemap : (tilemapkey, tile) Hashtbl.t
355 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
356 ; mutable pdims : (pageno * width * height * leftx) list
357 ; mutable pagecount : int
358 ; mutable currently : currently
359 ; mutable mstate : mstate
360 ; mutable searchpattern : string
361 ; mutable rects : (pageno * recttype * rect) list
362 ; mutable rects1 : (pageno * recttype * rect) list
363 ; mutable text : string
364 ; mutable fullscreen : (width * height) option
365 ; mutable mode : mode
366 ; mutable uioh : uioh
367 ; mutable outlines : outline array
368 ; mutable bookmarks : outline list
369 ; mutable path : string
370 ; mutable password : string
371 ; mutable invalidated : int
372 ; mutable memused : memsize
373 ; mutable gen : gen
374 ; mutable throttle : (page list * int * float) option
375 ; mutable autoscroll : int option
376 ; mutable ghyll : int option -> unit
377 ; mutable help : helpitem array
378 ; mutable docinfo : (int * string) list
379 ; mutable deadline : float
380 ; mutable texid : GlTex.texture_id option
381 ; hists : hists
382 ; mutable prevzoom : float
383 ; mutable progress : float
385 and hists =
386 { pat : string circbuf
387 ; pag : string circbuf
388 ; nav : anchor circbuf
392 let defconf =
393 { scrollbw = 7
394 ; scrollh = 12
395 ; icase = true
396 ; preload = true
397 ; pagebias = 0
398 ; verbose = false
399 ; debug = false
400 ; scrollstep = 24
401 ; maxhfit = true
402 ; crophack = false
403 ; autoscrollstep = 2
404 ; maxwait = None
405 ; hlinks = false
406 ; underinfo = false
407 ; interpagespace = 2
408 ; zoom = 1.0
409 ; presentation = false
410 ; angle = 0
411 ; winw = 900
412 ; winh = 900
413 ; savebmarks = true
414 ; proportional = true
415 ; trimmargins = false
416 ; trimfuzz = (0,0,0,0)
417 ; memlimit = 32 lsl 20
418 ; texcount = 256
419 ; sliceheight = 24
420 ; thumbw = 76
421 ; jumpback = true
422 ; bgcolor = (0.5, 0.5, 0.5)
423 ; bedefault = false
424 ; scrollbarinpm = true
425 ; tilew = 2048
426 ; tileh = 2048
427 ; mustoresize = 128 lsl 20
428 ; checkers = true
429 ; aalevel = 8
430 ; urilauncher =
431 (match platform with
432 | Plinux | Pfreebsd | Pdragonflybsd | Popenbsd | Psun -> "xdg-open \"%s\""
433 | Posx -> "open \"%s\""
434 | Pwindows | Pcygwin | Pmingw -> "iexplore \"%s\""
435 | _ -> "")
436 ; colorspace = Rgb
437 ; invert = false
438 ; colorscale = 1.0
439 ; redirectstderr = false
440 ; ghyllscroll = None
441 ; columns = None
442 ; beyecolumns = None
446 let conf = { defconf with angle = defconf.angle };;
448 type fontstate =
449 { mutable fontsize : int
450 ; mutable wwidth : float
451 ; mutable maxrows : int
455 let fstate =
456 { fontsize = 14
457 ; wwidth = nan
458 ; maxrows = -1
462 let setfontsize n =
463 fstate.fontsize <- n;
464 fstate.wwidth <- measurestr fstate.fontsize "w";
465 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
468 let gotouri uri =
469 if String.length conf.urilauncher = 0
470 then print_endline uri
471 else
472 let re = Str.regexp "%s" in
473 let command = Str.global_replace re uri conf.urilauncher in
474 let optic =
475 try Some (Unix.open_process_in command)
476 with exn ->
477 Printf.eprintf
478 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
479 flush stderr;
480 None
482 match optic with
483 | Some ic -> close_in ic
484 | None -> ()
487 let version () =
488 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
489 (platform_to_string platform) Sys.word_size Sys.ocaml_version
492 let makehelp () =
493 let strings = version () :: "" :: Help.keys in
494 Array.of_list (
495 let r = Str.regexp "\\(http://[^ ]+\\)" in
496 List.map (fun s ->
497 if (try Str.search_forward r s 0 with Not_found -> -1) >= 0
498 then
499 let uri = Str.matched_string s in
500 (s, 0, Action (fun u -> gotouri uri; u))
501 else s, 0, Noaction) strings
505 let noghyll _ = ();;
507 let state =
508 { csock = Unix.stdin
509 ; ssock = Unix.stdin
510 ; errfd = None
511 ; stderr = Unix.stderr
512 ; errmsgs = Buffer.create 0
513 ; newerrmsgs = false
514 ; x = 0
515 ; y = 0
516 ; w = 0
517 ; scrollw = 0
518 ; hscrollh = 0
519 ; anchor = emptyanchor
520 ; layout = []
521 ; maxy = max_int
522 ; tilelru = Queue.create ()
523 ; pagemap = Hashtbl.create 10
524 ; tilemap = Hashtbl.create 10
525 ; pdims = []
526 ; pagecount = 0
527 ; currently = Idle
528 ; mstate = Mnone
529 ; rects = []
530 ; rects1 = []
531 ; text = ""
532 ; mode = View
533 ; fullscreen = None
534 ; searchpattern = ""
535 ; outlines = [||]
536 ; bookmarks = []
537 ; path = ""
538 ; password = ""
539 ; invalidated = 0
540 ; hists =
541 { nav = cbnew 10 (0, 0.0)
542 ; pat = cbnew 1 ""
543 ; pag = cbnew 1 ""
545 ; memused = 0
546 ; gen = 0
547 ; throttle = None
548 ; autoscroll = None
549 ; ghyll = noghyll
550 ; help = makehelp ()
551 ; docinfo = []
552 ; deadline = nan
553 ; texid = None
554 ; prevzoom = 1.0
555 ; progress = -1.0
556 ; uioh = nouioh
560 let vlog fmt =
561 if conf.verbose
562 then
563 Printf.kprintf prerr_endline fmt
564 else
565 Printf.kprintf ignore fmt
568 let redirectstderr () =
569 if conf.redirectstderr
570 then
571 let rfd, wfd = Unix.pipe () in
572 state.stderr <- Unix.dup Unix.stderr;
573 state.errfd <- Some rfd;
574 Unix.dup2 wfd Unix.stderr;
575 else (
576 state.newerrmsgs <- false;
577 begin match state.errfd with
578 | Some fd ->
579 Unix.close fd;
580 Unix.dup2 state.stderr Unix.stderr;
581 state.errfd <- None;
582 | None -> ()
583 end;
584 prerr_string (Buffer.contents state.errmsgs);
585 flush stderr;
586 Buffer.clear state.errmsgs;
590 module G =
591 struct
592 let postRedisplay who =
593 if conf.verbose
594 then prerr_endline ("redisplay for " ^ who);
595 Glut.postRedisplay ();
597 end;;
599 let addchar s c =
600 let b = Buffer.create (String.length s + 1) in
601 Buffer.add_string b s;
602 Buffer.add_char b c;
603 Buffer.contents b;
606 let colorspace_of_string s =
607 match String.lowercase s with
608 | "rgb" -> Rgb
609 | "bgr" -> Bgr
610 | "gray" -> Gray
611 | _ -> failwith "invalid colorspace"
614 let int_of_colorspace = function
615 | Rgb -> 0
616 | Bgr -> 1
617 | Gray -> 2
620 let colorspace_of_int = function
621 | 0 -> Rgb
622 | 1 -> Bgr
623 | 2 -> Gray
624 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
627 let colorspace_to_string = function
628 | Rgb -> "rgb"
629 | Bgr -> "bgr"
630 | Gray -> "gray"
633 let intentry_with_suffix text key =
634 let c = Char.unsafe_chr key in
635 match Char.lowercase c with
636 | '0' .. '9' ->
637 let text = addchar text c in
638 TEcont text
640 | 'k' | 'm' | 'g' ->
641 let text = addchar text c in
642 TEcont text
644 | _ ->
645 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
646 TEcont text
649 let columns_to_string (n, a, b) =
650 if a = 0 && b = 0
651 then Printf.sprintf "%d" n
652 else Printf.sprintf "%d,%d,%d" n a b;
655 let columns_of_string s =
657 (int_of_string s, 0, 0)
658 with _ ->
659 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
662 let writecmd fd s =
663 let len = String.length s in
664 let n = 4 + len in
665 let b = Buffer.create n in
666 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
667 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
668 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
669 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
670 Buffer.add_string b s;
671 let s' = Buffer.contents b in
672 let n' = Unix.write fd s' 0 n in
673 if n' != n then failwith "write failed";
676 let readcmd fd =
677 let s = "xxxx" in
678 let n = Unix.read fd s 0 4 in
679 if n != 4 then failwith "incomplete read(len)";
680 let len = 0
681 lor (Char.code s.[0] lsl 24)
682 lor (Char.code s.[1] lsl 16)
683 lor (Char.code s.[2] lsl 8)
684 lor (Char.code s.[3] lsl 0)
686 let s = String.create len in
687 let n = Unix.read fd s 0 len in
688 if n != len then failwith "incomplete read(data)";
692 let makecmd s l =
693 let b = Buffer.create 10 in
694 Buffer.add_string b s;
695 let rec combine = function
696 | [] -> b
697 | x :: xs ->
698 Buffer.add_char b ' ';
699 let s =
700 match x with
701 | `b b -> if b then "1" else "0"
702 | `s s -> s
703 | `i i -> string_of_int i
704 | `f f -> string_of_float f
705 | `I f -> string_of_int (truncate f)
707 Buffer.add_string b s;
708 combine xs;
710 combine l;
713 let wcmd s l =
714 let cmd = Buffer.contents (makecmd s l) in
715 writecmd state.csock cmd;
718 let calcips h =
719 if conf.presentation
720 then
721 let d = conf.winh - h in
722 max 0 ((d + 1) / 2)
723 else
724 conf.interpagespace
727 let calcheight () =
728 let rec f pn ph pi fh l =
729 match l with
730 | (n, _, h, _) :: rest ->
731 let ips = calcips h in
732 let fh =
733 if conf.presentation
734 then fh+ips
735 else (
736 if isbirdseye state.mode && pn = 0
737 then fh + ips
738 else fh
741 let fh = fh + ((n - pn) * (ph + pi)) in
742 f n h ips fh rest;
744 | [] ->
745 let inc =
746 if conf.presentation || (isbirdseye state.mode && pn = 0)
747 then 0
748 else -pi
750 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
751 max 0 fh
753 let fh = f 0 0 0 0 state.pdims in
757 let calcheight () =
758 match conf.columns with
759 | None -> calcheight ()
760 | Some (_, b) ->
761 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
762 y + h
765 let getpageyh pageno =
766 let rec f pn ph pi y l =
767 match l with
768 | (n, _, h, _) :: rest ->
769 let ips = calcips h in
770 if n >= pageno
771 then
772 let h = if n = pageno then h else ph in
773 if conf.presentation && n = pageno
774 then
775 y + (pageno - pn) * (ph + pi) + pi, h
776 else
777 y + (pageno - pn) * (ph + pi), h
778 else
779 let y = y + (if conf.presentation then pi else 0) in
780 let y = y + (n - pn) * (ph + pi) in
781 f n h ips y rest
783 | [] ->
784 y + (pageno - pn) * (ph + pi), ph
786 f 0 0 0 0 state.pdims
789 let getpageyh pageno =
790 match conf.columns with
791 | None -> getpageyh pageno
792 | Some (_, b) ->
793 let (_, _, y, (_, _, h, _)) = b.(pageno) in
794 y, h
797 let getpagedim pageno =
798 let rec f ppdim l =
799 match l with
800 | (n, _, _, _) as pdim :: rest ->
801 if n >= pageno
802 then (if n = pageno then pdim else ppdim)
803 else f pdim rest
805 | [] -> ppdim
807 f (-1, -1, -1, -1) state.pdims
810 let getpagey pageno = fst (getpageyh pageno);;
812 let layout1 y sh =
813 let sh = sh - state.hscrollh in
814 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
815 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
816 match pdims with
817 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
818 let ips = calcips h in
819 let yinc =
820 if conf.presentation || (isbirdseye state.mode && pageno = 0)
821 then ips
822 else 0
824 (w, h, ips, xoff), rest, pdimno + 1, yinc
825 | _ ->
826 prev, pdims, pdimno, 0
828 let dy = dy + yinc in
829 let py = py + yinc in
830 if pageno = state.pagecount || dy >= sh
831 then
832 accu
833 else
834 let vy = y + dy in
835 if py + h <= vy - yinc
836 then
837 let py = py + h + ips in
838 let dy = max 0 (py - y) in
839 f ~pageno:(pageno+1)
840 ~pdimno
841 ~prev:curr
844 ~pdims:rest
845 ~accu
846 else
847 let pagey = vy - py in
848 let pagevh = h - pagey in
849 let pagevh = min (sh - dy) pagevh in
850 let off = if yinc > 0 then py - vy else 0 in
851 let py = py + h + ips in
852 let pagex, dx =
853 let xoff = xoff +
854 if state.w < conf.winw - state.scrollw
855 then (conf.winw - state.scrollw - state.w) / 2
856 else 0
858 let dispx = xoff + state.x in
859 if dispx < 0
860 then (-dispx, 0)
861 else (0, dispx)
863 let pagevw =
864 let lw = w - pagex in
865 min lw (conf.winw - state.scrollw)
867 let e =
868 { pageno = pageno
869 ; pagedimno = pdimno
870 ; pagew = w
871 ; pageh = h
872 ; pagex = pagex
873 ; pagey = pagey + off
874 ; pagevw = pagevw
875 ; pagevh = pagevh - off
876 ; pagedispx = dx
877 ; pagedispy = dy + off
880 let accu = e :: accu in
881 f ~pageno:(pageno+1)
882 ~pdimno
883 ~prev:curr
885 ~dy:(dy+pagevh+ips)
886 ~pdims:rest
887 ~accu
889 if state.invalidated = 0
890 then (
891 let accu =
893 ~pageno:0
894 ~pdimno:~-1
895 ~prev:(0,0,0,0)
896 ~py:0
897 ~dy:0
898 ~pdims:state.pdims
899 ~accu:[]
901 List.rev accu
903 else
907 let layoutN (_, b) y sh =
908 let sh = sh - state.hscrollh in
909 let rec fold accu n =
910 if n = Array.length b
911 then accu
912 else
913 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
914 if (vy - y) > sh
915 then accu
916 else
917 let accu =
918 if vy + h > y
919 then
920 let pagey = max 0 (y - vy) in
921 let e =
922 { pageno = n
923 ; pagedimno = pdimno
924 ; pagew = w
925 ; pageh = h
926 ; pagex = 0
927 ; pagey = pagey
928 ; pagevw = w
929 ; pagevh = h - pagey
930 ; pagedispx = dx + xoff + state.x
931 ; pagedispy = if pagey > 0 then 0 else vy - y
934 e :: accu
935 else
936 accu
938 fold accu (n+1)
940 if state.invalidated = 0
941 then List.rev (fold [] 0)
942 else []
945 let layout y sh =
946 match conf.columns with
947 | None -> layout1 y sh
948 | Some c -> layoutN c y sh
951 let clamp incr =
952 let y = state.y + incr in
953 let y = max 0 y in
954 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
958 let getopaque pageno =
959 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
960 with Not_found -> None
963 let putopaque pageno opaque =
964 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
967 let itertiles l f =
968 let tilex = l.pagex mod conf.tilew in
969 let tiley = l.pagey mod conf.tileh in
971 let col = l.pagex / conf.tilew in
972 let row = l.pagey / conf.tileh in
974 let vw =
975 let a = l.pagew - l.pagex in
976 let b = conf.winw - state.scrollw in
977 min a b
978 and vh = l.pagevh in
980 let rec rowloop row y0 dispy h =
981 if h = 0
982 then ()
983 else (
984 let dh = conf.tileh - y0 in
985 let dh = min h dh in
986 let rec colloop col x0 dispx w =
987 if w = 0
988 then ()
989 else (
990 let dw = conf.tilew - x0 in
991 let dw = min w dw in
993 f col row dispx dispy x0 y0 dw dh;
994 colloop (col+1) 0 (dispx+dw) (w-dw)
997 colloop col tilex l.pagedispx vw;
998 rowloop (row+1) 0 (dispy+dh) (h-dh)
1001 if vw > 0 && vh > 0
1002 then rowloop row tiley l.pagedispy vh;
1005 let gettileopaque l col row =
1006 let key =
1007 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1009 try Some (Hashtbl.find state.tilemap key)
1010 with Not_found -> None
1013 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1014 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1015 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1018 let drawtiles l color =
1019 GlDraw.color color;
1020 let f col row x y tilex tiley w h =
1021 match gettileopaque l col row with
1022 | Some (opaque, _, t) ->
1023 let params = x, y, w, h, tilex, tiley in
1024 if conf.invert
1025 then (
1026 Gl.enable `blend;
1027 GlFunc.blend_func `zero `one_minus_src_color;
1029 drawtile params opaque;
1030 if conf.invert
1031 then Gl.disable `blend;
1032 if conf.debug
1033 then (
1034 let s = Printf.sprintf
1035 "%d[%d,%d] %f sec"
1036 l.pageno col row t
1038 let w = measurestr fstate.fontsize s in
1039 GlMisc.push_attrib [`current];
1040 GlDraw.color (0.0, 0.0, 0.0);
1041 GlDraw.rect
1042 (float (x-2), float (y-2))
1043 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1044 GlDraw.color (1.0, 1.0, 1.0);
1045 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1046 GlMisc.pop_attrib ();
1049 | _ ->
1050 let w =
1051 let lw = conf.winw - state.scrollw - x in
1052 min lw w
1053 and h =
1054 let lh = conf.winh - y in
1055 min lh h
1057 Gl.enable `texture_2d;
1058 begin match state.texid with
1059 | Some id ->
1060 GlTex.bind_texture `texture_2d id;
1061 let x0 = float x
1062 and y0 = float y
1063 and x1 = float (x+w)
1064 and y1 = float (y+h) in
1066 let tw = float w /. 64.0
1067 and th = float h /. 64.0 in
1068 let tx0 = float tilex /. 64.0
1069 and ty0 = float tiley /. 64.0 in
1070 let tx1 = tx0 +. tw
1071 and ty1 = ty0 +. th in
1072 GlDraw.begins `quads;
1073 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1074 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1075 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1076 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1077 GlDraw.ends ();
1079 Gl.disable `texture_2d;
1080 | None ->
1081 GlDraw.color (1.0, 1.0, 1.0);
1082 GlDraw.rect
1083 (float x, float y)
1084 (float (x+w), float (y+h));
1085 end;
1086 if w > 128 && h > fstate.fontsize + 10
1087 then (
1088 GlDraw.color (0.0, 0.0, 0.0);
1089 let c, r =
1090 if conf.verbose
1091 then (col*conf.tilew, row*conf.tileh)
1092 else col, row
1094 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1096 GlDraw.color color;
1098 itertiles l f
1101 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1103 let tilevisible1 l x y =
1104 let ax0 = l.pagex
1105 and ax1 = l.pagex + l.pagevw
1106 and ay0 = l.pagey
1107 and ay1 = l.pagey + l.pagevh in
1109 let bx0 = x
1110 and by0 = y in
1111 let bx1 = min (bx0 + conf.tilew) l.pagew
1112 and by1 = min (by0 + conf.tileh) l.pageh in
1114 let rx0 = max ax0 bx0
1115 and ry0 = max ay0 by0
1116 and rx1 = min ax1 bx1
1117 and ry1 = min ay1 by1 in
1119 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1120 nonemptyintersection
1123 let tilevisible layout n x y =
1124 let rec findpageinlayout = function
1125 | l :: _ when l.pageno = n -> tilevisible1 l x y
1126 | _ :: rest -> findpageinlayout rest
1127 | [] -> false
1129 findpageinlayout layout
1132 let tileready l x y =
1133 tilevisible1 l x y &&
1134 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1137 let tilepage n p layout =
1138 let rec loop = function
1139 | l :: rest ->
1140 if l.pageno = n
1141 then
1142 let f col row _ _ _ _ _ _ =
1143 if state.currently = Idle
1144 then
1145 match gettileopaque l col row with
1146 | Some _ -> ()
1147 | None ->
1148 let x = col*conf.tilew
1149 and y = row*conf.tileh in
1150 let w =
1151 let w = l.pagew - x in
1152 min w conf.tilew
1154 let h =
1155 let h = l.pageh - y in
1156 min h conf.tileh
1158 wcmd "tile"
1159 [`s p
1160 ;`i x
1161 ;`i y
1162 ;`i w
1163 ;`i h
1165 state.currently <-
1166 Tiling (
1167 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1168 conf.tilew, conf.tileh
1171 itertiles l f;
1172 else
1173 loop rest
1175 | [] -> ()
1177 if state.invalidated = 0 then loop layout;
1180 let preloadlayout visiblepages =
1181 let presentation = conf.presentation in
1182 let interpagespace = conf.interpagespace in
1183 let maxy = state.maxy in
1184 conf.presentation <- false;
1185 conf.interpagespace <- 0;
1186 state.maxy <- calcheight ();
1187 let y =
1188 match visiblepages with
1189 | [] -> 0
1190 | l :: _ -> getpagey l.pageno + l.pagey
1192 let y = if y < conf.winh then 0 else y - conf.winh in
1193 let h = state.y - y + conf.winh*3 in
1194 let pages = layout y h in
1195 conf.presentation <- presentation;
1196 conf.interpagespace <- interpagespace;
1197 state.maxy <- maxy;
1198 pages;
1201 let load pages =
1202 let rec loop pages =
1203 if state.currently != Idle
1204 then ()
1205 else
1206 match pages with
1207 | l :: rest ->
1208 begin match getopaque l.pageno with
1209 | None ->
1210 wcmd "page" [`i l.pageno; `i l.pagedimno];
1211 state.currently <- Loading (l, state.gen);
1212 | Some opaque ->
1213 tilepage l.pageno opaque pages;
1214 loop rest
1215 end;
1216 | _ -> ()
1218 if state.invalidated = 0 then loop pages
1221 let preload pages =
1222 load pages;
1223 if conf.preload && state.currently = Idle
1224 then load (preloadlayout pages);
1227 let layoutready layout =
1228 let rec fold all ls =
1229 all && match ls with
1230 | l :: rest ->
1231 let seen = ref false in
1232 let allvisible = ref true in
1233 let foo col row _ _ _ _ _ _ =
1234 seen := true;
1235 allvisible := !allvisible &&
1236 begin match gettileopaque l col row with
1237 | Some _ -> true
1238 | None -> false
1241 itertiles l foo;
1242 fold (!seen && !allvisible) rest
1243 | [] -> true
1245 let alltilesvisible = fold true layout in
1246 alltilesvisible;
1249 let gotoy y =
1250 let y = bound y 0 state.maxy in
1251 let y, layout, proceed =
1252 match conf.maxwait with
1253 | Some time when state.ghyll == noghyll ->
1254 begin match state.throttle with
1255 | None ->
1256 let layout = layout y conf.winh in
1257 let ready = layoutready layout in
1258 if not ready
1259 then (
1260 load layout;
1261 state.throttle <- Some (layout, y, now ());
1263 else G.postRedisplay "gotoy showall (None)";
1264 y, layout, ready
1265 | Some (_, _, started) ->
1266 let dt = now () -. started in
1267 if dt > time
1268 then (
1269 state.throttle <- None;
1270 let layout = layout y conf.winh in
1271 load layout;
1272 G.postRedisplay "maxwait";
1273 y, layout, true
1275 else -1, [], false
1278 | _ ->
1279 let layout = layout y conf.winh in
1280 if true || layoutready layout
1281 then G.postRedisplay "gotoy ready";
1282 y, layout, true
1284 if proceed
1285 then (
1286 state.y <- y;
1287 state.layout <- layout;
1288 begin match state.mode with
1289 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1290 if not (pagevisible layout pageno)
1291 then (
1292 match state.layout with
1293 | [] -> ()
1294 | l :: _ ->
1295 state.mode <- Birdseye (
1296 conf, leftx, l.pageno, hooverpageno, anchor
1299 | _ -> ()
1300 end;
1301 preload layout;
1303 state.ghyll <- noghyll;
1306 let conttiling pageno opaque =
1307 tilepage pageno opaque
1308 (if conf.preload then preloadlayout state.layout else state.layout)
1311 let gotoy_and_clear_text y =
1312 gotoy y;
1313 if not conf.verbose then state.text <- "";
1316 let getanchor () =
1317 match state.layout with
1318 | [] -> emptyanchor
1319 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1322 let getanchory (n, top) =
1323 let y, h = getpageyh n in
1324 y + (truncate (top *. float h));
1327 let gotoanchor anchor =
1328 gotoy (getanchory anchor);
1331 let addnav () =
1332 cbput state.hists.nav (getanchor ());
1335 let getnav dir =
1336 let anchor = cbgetc state.hists.nav dir in
1337 getanchory anchor;
1340 let gotoghyll y =
1341 let rec scroll f n a b =
1342 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1343 let snake f a b =
1344 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1345 if f < a
1346 then s (float f /. float a)
1347 else (
1348 if f > b
1349 then 1.0 -. s ((float (f-b) /. float (n-b)))
1350 else 1.0
1353 snake f a b
1354 and summa f n a b =
1355 (* courtesy:
1356 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1357 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1358 let iv1 = iv f in
1359 let ins = float a *. iv1
1360 and outs = float (n-b) *. iv1 in
1361 let ones = b - a in
1362 ins +. outs +. float ones
1364 let rec set (_N, _A, _B) y sy =
1365 let sum = summa 1.0 _N _A _B in
1366 let dy = float (y - sy) in
1367 state.ghyll <- (
1368 let rec gf n y1 o =
1369 if n >= _N
1370 then state.ghyll <- noghyll
1371 else
1372 let go n =
1373 let s = scroll n _N _A _B in
1374 let y1 = y1 +. ((s *. dy) /. sum) in
1375 gotoy_and_clear_text (truncate y1);
1376 state.ghyll <- gf (n+1) y1;
1378 match o with
1379 | None -> go n
1380 | Some y' -> set (_N/2, 0, 0) y' state.y
1382 gf 0 (float state.y)
1385 match conf.ghyllscroll with
1386 | None ->
1387 gotoy_and_clear_text y
1388 | Some nab ->
1389 if state.ghyll == noghyll
1390 then set nab y state.y
1391 else state.ghyll (Some y)
1394 let gotopage n top =
1395 let y, h = getpageyh n in
1396 let y = y + (truncate (top *. float h)) in
1397 gotoghyll y
1400 let gotopage1 n top =
1401 let y = getpagey n in
1402 let y = y + top in
1403 gotoghyll y
1406 let invalidate () =
1407 state.layout <- [];
1408 state.pdims <- [];
1409 state.rects <- [];
1410 state.rects1 <- [];
1411 state.invalidated <- state.invalidated + 1;
1414 let writeopen path password =
1415 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1418 let opendoc path password =
1419 invalidate ();
1420 state.path <- path;
1421 state.password <- password;
1422 state.gen <- state.gen + 1;
1423 state.docinfo <- [];
1425 setaalevel conf.aalevel;
1426 writeopen path password;
1427 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1428 wcmd "geometry" [`i state.w; `i conf.winh];
1431 let scalecolor c =
1432 let c = c *. conf.colorscale in
1433 (c, c, c);
1436 let scalecolor2 (r, g, b) =
1437 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1440 let represent () =
1441 let docolumns = function
1442 | None -> ()
1443 | Some ((columns, coverA, coverB), _) ->
1444 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1445 let rec loop pageno pdimno pdim x y rowh pdims =
1446 if pageno = state.pagecount
1447 then ()
1448 else
1449 let pdimno, ((_, w, h, _) as pdim), pdims =
1450 match pdims with
1451 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1452 pdimno+1, pdim, rest
1453 | _ ->
1454 pdimno, pdim, pdims
1456 let x, y, rowh =
1457 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1458 then (
1459 (conf.winw - state.scrollw - w) / 2,
1460 y + rowh + conf.interpagespace, h
1462 else (
1463 if (pageno - coverA) mod columns = 0
1464 then 0, y + rowh + conf.interpagespace, h
1465 else x + w + conf.interpagespace, y, max rowh h
1468 a.(pageno) <- (pdimno, x, y, pdim);
1469 loop (pageno+1) pdimno pdim x y rowh pdims
1471 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1472 let rec fix n rowh = if n = Array.length a then () else
1473 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(n) in
1474 let rowh =
1475 if n mod columns = 0
1476 then h
1477 else max h rowh
1479 if h < rowh
1480 then (
1481 let y = y + (rowh - h) / 2 in
1482 a.(n) <- (pdimno, x, y, pdim);
1484 fix (n+1) rowh
1486 fix 0 0;
1487 conf.columns <- Some ((columns, coverA, coverB), a);
1489 docolumns conf.columns;
1490 state.maxy <- calcheight ();
1491 state.hscrollh <-
1492 if state.w <= conf.winw - state.scrollw
1493 then 0
1494 else state.scrollw
1496 match state.mode with
1497 | Birdseye (_, _, pageno, _, _) ->
1498 let y, h = getpageyh pageno in
1499 let top = (conf.winh - h) / 2 in
1500 gotoy (max 0 (y - top))
1501 | _ -> gotoanchor state.anchor
1504 let reshape =
1505 let firsttime = ref true in
1506 fun ~w ~h ->
1507 GlDraw.viewport 0 0 w h;
1508 if state.invalidated = 0 && not !firsttime
1509 then state.anchor <- getanchor ();
1511 firsttime := false;
1512 conf.winw <- w;
1513 let w = truncate (float w *. conf.zoom) - state.scrollw in
1514 let w = max w 2 in
1515 state.w <- w;
1516 conf.winh <- h;
1517 setfontsize fstate.fontsize;
1518 GlMat.mode `modelview;
1519 GlMat.load_identity ();
1521 GlMat.mode `projection;
1522 GlMat.load_identity ();
1523 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1524 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1525 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1527 let w =
1528 match conf.columns with
1529 | None -> w
1530 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1532 invalidate ();
1533 wcmd "geometry" [`i w; `i h];
1536 let enttext () =
1537 let len = String.length state.text in
1538 let drawstring s =
1539 let hscrollh =
1540 match state.mode with
1541 | View -> state.hscrollh
1542 | _ -> 0
1544 let rect x w =
1545 GlDraw.rect
1546 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1547 (x+.w, float (conf.winh - hscrollh))
1550 let w = float (conf.winw - state.scrollw - 1) in
1551 if state.progress >= 0.0 && state.progress < 1.0
1552 then (
1553 GlDraw.color (0.3, 0.3, 0.3);
1554 let w1 = w *. state.progress in
1555 rect 0.0 w1;
1556 GlDraw.color (0.0, 0.0, 0.0);
1557 rect w1 (w-.w1)
1559 else (
1560 GlDraw.color (0.0, 0.0, 0.0);
1561 rect 0.0 w;
1564 GlDraw.color (1.0, 1.0, 1.0);
1565 drawstring fstate.fontsize
1566 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1568 let s =
1569 match state.mode with
1570 | Textentry ((prefix, text, _, _, _), _) ->
1571 let s =
1572 if len > 0
1573 then
1574 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1575 else
1576 Printf.sprintf "%s%s_" prefix text
1580 | _ -> state.text
1582 let s =
1583 if state.newerrmsgs
1584 then (
1585 if not (istextentry state.mode)
1586 then
1587 let s1 = "(press 'e' to review error messasges)" in
1588 if String.length s > 0 then s ^ " " ^ s1 else s1
1589 else s
1591 else s
1593 if String.length s > 0
1594 then drawstring s
1597 let showtext c s =
1598 state.text <- Printf.sprintf "%c%s" c s;
1599 G.postRedisplay "showtext";
1602 let gctiles () =
1603 let len = Queue.length state.tilelru in
1604 let rec loop qpos =
1605 if state.memused <= conf.memlimit
1606 then ()
1607 else (
1608 if qpos < len
1609 then
1610 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1611 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1612 let (_, pw, ph, _) = getpagedim n in
1614 gen = state.gen
1615 && colorspace = conf.colorspace
1616 && angle = conf.angle
1617 && pagew = pw
1618 && pageh = ph
1619 && (
1620 let layout =
1621 match state.throttle with
1622 | None ->
1623 if conf.preload
1624 then preloadlayout state.layout
1625 else state.layout
1626 | Some (layout, _, _) ->
1627 layout
1629 let x = col*conf.tilew
1630 and y = row*conf.tileh in
1631 tilevisible layout n x y
1633 then Queue.push lruitem state.tilelru
1634 else (
1635 wcmd "freetile" [`s p];
1636 state.memused <- state.memused - s;
1637 state.uioh#infochanged Memused;
1638 Hashtbl.remove state.tilemap k;
1640 loop (qpos+1)
1643 loop 0
1646 let flushtiles () =
1647 Queue.iter (fun (k, p, s) ->
1648 wcmd "freetile" [`s p];
1649 state.memused <- state.memused - s;
1650 state.uioh#infochanged Memused;
1651 Hashtbl.remove state.tilemap k;
1652 ) state.tilelru;
1653 Queue.clear state.tilelru;
1654 load state.layout;
1657 let logcurrently = function
1658 | Idle -> dolog "Idle"
1659 | Loading (l, gen) ->
1660 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1661 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1662 dolog
1663 "Tiling %d[%d,%d] page=%s cs=%s angle"
1664 l.pageno col row pageopaque
1665 (colorspace_to_string colorspace)
1667 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1668 angle gen conf.angle state.gen
1669 tilew tileh
1670 conf.tilew conf.tileh
1672 | Outlining _ ->
1673 dolog "outlining"
1676 let act cmds =
1677 (* dolog "%S" cmds; *)
1678 let op, args =
1679 let spacepos =
1680 try String.index cmds ' '
1681 with Not_found -> -1
1683 if spacepos = -1
1684 then cmds, ""
1685 else
1686 let l = String.length cmds in
1687 let op = String.sub cmds 0 spacepos in
1688 op, begin
1689 if l - spacepos < 2 then ""
1690 else String.sub cmds (spacepos+1) (l-spacepos-1)
1693 match op with
1694 | "clear" ->
1695 state.uioh#infochanged Pdim;
1696 state.pdims <- [];
1698 | "clearrects" ->
1699 state.rects <- state.rects1;
1700 G.postRedisplay "clearrects";
1702 | "continue" ->
1703 let n =
1704 try Scanf.sscanf args "%u" (fun n -> n)
1705 with exn ->
1706 dolog "error processing 'continue' %S: %s"
1707 cmds (Printexc.to_string exn);
1708 exit 1;
1710 state.pagecount <- n;
1711 state.invalidated <- state.invalidated - 1;
1712 begin match state.currently with
1713 | Outlining l ->
1714 state.currently <- Idle;
1715 state.outlines <- Array.of_list (List.rev l)
1716 | _ -> ()
1717 end;
1718 if state.invalidated = 0
1719 then represent ();
1720 if conf.maxwait = None
1721 then G.postRedisplay "continue";
1723 | "title" ->
1724 Glut.setWindowTitle args
1726 | "msg" ->
1727 showtext ' ' args
1729 | "vmsg" ->
1730 if conf.verbose
1731 then showtext ' ' args
1733 | "progress" ->
1734 let progress, text =
1736 Scanf.sscanf args "%f %n"
1737 (fun f pos ->
1738 f, String.sub args pos (String.length args - pos))
1739 with exn ->
1740 dolog "error processing 'progress' %S: %s"
1741 cmds (Printexc.to_string exn);
1742 exit 1;
1744 state.text <- text;
1745 state.progress <- progress;
1746 G.postRedisplay "progress"
1748 | "firstmatch" ->
1749 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1751 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1752 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1753 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1754 with exn ->
1755 dolog "error processing 'firstmatch' %S: %s"
1756 cmds (Printexc.to_string exn);
1757 exit 1;
1759 let y = (getpagey pageno) + truncate y0 in
1760 addnav ();
1761 gotoy y;
1762 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1764 | "match" ->
1765 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1767 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1768 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1769 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1770 with exn ->
1771 dolog "error processing 'match' %S: %s"
1772 cmds (Printexc.to_string exn);
1773 exit 1;
1775 state.rects1 <-
1776 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1778 | "page" ->
1779 let pageopaque, t =
1781 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1782 with exn ->
1783 dolog "error processing 'page' %S: %s"
1784 cmds (Printexc.to_string exn);
1785 exit 1;
1787 begin match state.currently with
1788 | Loading (l, gen) ->
1789 vlog "page %d took %f sec" l.pageno t;
1790 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1791 begin match state.throttle with
1792 | None ->
1793 let preloadedpages =
1794 if conf.preload
1795 then preloadlayout state.layout
1796 else state.layout
1798 let evict () =
1799 let module IntSet =
1800 Set.Make (struct type t = int let compare = (-) end) in
1801 let set =
1802 List.fold_left (fun s l -> IntSet.add l.pageno s)
1803 IntSet.empty preloadedpages
1805 let evictedpages =
1806 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1807 if not (IntSet.mem pageno set)
1808 then (
1809 wcmd "freepage" [`s opaque];
1810 key :: accu
1812 else accu
1813 ) state.pagemap []
1815 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1817 evict ();
1818 state.currently <- Idle;
1819 if gen = state.gen
1820 then (
1821 tilepage l.pageno pageopaque state.layout;
1822 load state.layout;
1823 load preloadedpages;
1824 if pagevisible state.layout l.pageno
1825 && layoutready state.layout
1826 then G.postRedisplay "page";
1829 | Some (layout, _, _) ->
1830 state.currently <- Idle;
1831 tilepage l.pageno pageopaque layout;
1832 load state.layout
1833 end;
1835 | _ ->
1836 dolog "Inconsistent loading state";
1837 logcurrently state.currently;
1838 raise Quit;
1841 | "tile" ->
1842 let (x, y, opaque, size, t) =
1844 Scanf.sscanf args "%u %u %s %u %f"
1845 (fun x y p size t -> (x, y, p, size, t))
1846 with exn ->
1847 dolog "error processing 'tile' %S: %s"
1848 cmds (Printexc.to_string exn);
1849 exit 1;
1851 begin match state.currently with
1852 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1853 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1855 if tilew != conf.tilew || tileh != conf.tileh
1856 then (
1857 wcmd "freetile" [`s opaque];
1858 state.currently <- Idle;
1859 load state.layout;
1861 else (
1862 puttileopaque l col row gen cs angle opaque size t;
1863 state.memused <- state.memused + size;
1864 state.uioh#infochanged Memused;
1865 gctiles ();
1866 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1867 opaque, size) state.tilelru;
1869 let layout =
1870 match state.throttle with
1871 | None -> state.layout
1872 | Some (layout, _, _) -> layout
1875 state.currently <- Idle;
1876 if gen = state.gen
1877 && conf.colorspace = cs
1878 && conf.angle = angle
1879 && tilevisible layout l.pageno x y
1880 then conttiling l.pageno pageopaque;
1882 begin match state.throttle with
1883 | None ->
1884 preload state.layout;
1885 if gen = state.gen
1886 && conf.colorspace = cs
1887 && conf.angle = angle
1888 && tilevisible state.layout l.pageno x y
1889 then G.postRedisplay "tile nothrottle";
1891 | Some (layout, y, _) ->
1892 let ready = layoutready layout in
1893 if ready
1894 then (
1895 state.y <- y;
1896 state.layout <- layout;
1897 state.throttle <- None;
1898 G.postRedisplay "throttle";
1900 else load layout;
1901 end;
1904 | _ ->
1905 dolog "Inconsistent tiling state";
1906 logcurrently state.currently;
1907 raise Quit;
1910 | "pdim" ->
1911 let pdim =
1913 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1914 with exn ->
1915 dolog "error processing 'pdim' %S: %s"
1916 cmds (Printexc.to_string exn);
1917 exit 1;
1919 state.uioh#infochanged Pdim;
1920 state.pdims <- pdim :: state.pdims
1922 | "o" ->
1923 let (l, n, t, h, pos) =
1925 Scanf.sscanf args "%u %u %d %u %n"
1926 (fun l n t h pos -> l, n, t, h, pos)
1927 with exn ->
1928 dolog "error processing 'o' %S: %s"
1929 cmds (Printexc.to_string exn);
1930 exit 1;
1932 let s = String.sub args pos (String.length args - pos) in
1933 let outline = (s, l, (n, float t /. float h)) in
1934 begin match state.currently with
1935 | Outlining outlines ->
1936 state.currently <- Outlining (outline :: outlines)
1937 | Idle ->
1938 state.currently <- Outlining [outline]
1939 | currently ->
1940 dolog "invalid outlining state";
1941 logcurrently currently
1944 | "info" ->
1945 state.docinfo <- (1, args) :: state.docinfo
1947 | "infoend" ->
1948 state.uioh#infochanged Docinfo;
1949 state.docinfo <- List.rev state.docinfo
1951 | _ ->
1952 dolog "unknown cmd `%S'" cmds
1955 let idle () =
1956 if state.deadline == nan then state.deadline <- now ();
1957 let r =
1958 match state.errfd with
1959 | None -> [state.csock]
1960 | Some fd -> [state.csock; fd]
1962 let rec loop delay =
1963 let deadline =
1964 if state.ghyll == noghyll
1965 then state.deadline
1966 else now () +. 0.02
1968 let timeout =
1969 if delay > 0.0
1970 then max 0.0 (deadline -. now ())
1971 else 0.0
1973 let r, _, _ = Unix.select r [] [] timeout in
1974 begin match r with
1975 | [] ->
1976 state.ghyll None;
1977 begin match state.autoscroll with
1978 | Some step when step != 0 ->
1979 let y = state.y + step in
1980 let y =
1981 if y < 0
1982 then state.maxy
1983 else if y >= state.maxy then 0 else y
1985 gotoy y;
1986 if state.mode = View
1987 then state.text <- "";
1988 state.deadline <- state.deadline +. 0.005;
1990 | _ ->
1991 state.deadline <- state.deadline +. delay;
1992 end;
1994 | l ->
1995 let rec checkfds c = function
1996 | [] -> c
1997 | fd :: rest when fd = state.csock ->
1998 let cmd = readcmd state.csock in
1999 act cmd;
2000 checkfds true rest
2001 | fd :: rest ->
2002 let s = String.create 80 in
2003 let n = Unix.read fd s 0 80 in
2004 if conf.redirectstderr
2005 then (
2006 Buffer.add_substring state.errmsgs s 0 n;
2007 state.newerrmsgs <- true;
2008 Glut.postRedisplay ();
2010 else (
2011 prerr_string (String.sub s 0 n);
2012 flush stderr;
2014 checkfds c rest
2016 if checkfds false l
2017 then loop 0.0
2018 end;
2019 in loop 0.007
2022 let onhist cb =
2023 let rc = cb.rc in
2024 let action = function
2025 | HCprev -> cbget cb ~-1
2026 | HCnext -> cbget cb 1
2027 | HCfirst -> cbget cb ~-(cb.rc)
2028 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2029 and cancel () = cb.rc <- rc
2030 in (action, cancel)
2033 let search pattern forward =
2034 if String.length pattern > 0
2035 then
2036 let pn, py =
2037 match state.layout with
2038 | [] -> 0, 0
2039 | l :: _ ->
2040 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2042 let cmd =
2043 let b = makecmd "search"
2044 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
2046 Buffer.add_char b ',';
2047 Buffer.add_string b pattern;
2048 Buffer.add_char b '\000';
2049 Buffer.contents b;
2051 writecmd state.csock cmd;
2054 let intentry text key =
2055 let c = Char.unsafe_chr key in
2056 match c with
2057 | '0' .. '9' ->
2058 let text = addchar text c in
2059 TEcont text
2061 | _ ->
2062 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2063 TEcont text
2066 let textentry text key =
2067 let c = Char.unsafe_chr key in
2068 match c with
2069 | _ when key >= 32 && key < 127 ->
2070 let text = addchar text c in
2071 TEcont text
2073 | _ ->
2074 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
2075 TEcont text
2078 let reqlayout angle proportional =
2079 match state.throttle with
2080 | None ->
2081 if state.invalidated = 0 then state.anchor <- getanchor ();
2082 conf.angle <- angle mod 360;
2083 conf.proportional <- proportional;
2084 invalidate ();
2085 wcmd "reqlayout" [`i conf.angle; `b proportional];
2086 | _ -> ()
2089 let settrim trimmargins trimfuzz =
2090 if state.invalidated = 0 then state.anchor <- getanchor ();
2091 conf.trimmargins <- trimmargins;
2092 conf.trimfuzz <- trimfuzz;
2093 let x0, y0, x1, y1 = trimfuzz in
2094 invalidate ();
2095 wcmd "settrim" [
2096 `b conf.trimmargins;
2097 `i x0;
2098 `i y0;
2099 `i x1;
2100 `i y1;
2102 Hashtbl.iter (fun _ opaque ->
2103 wcmd "freepage" [`s opaque];
2104 ) state.pagemap;
2105 Hashtbl.clear state.pagemap;
2108 let setzoom zoom =
2109 match state.throttle with
2110 | None ->
2111 let zoom = max 0.01 zoom in
2112 if zoom <> conf.zoom
2113 then (
2114 state.prevzoom <- conf.zoom;
2115 let relx =
2116 if zoom <= 1.0
2117 then (state.x <- 0; 0.0)
2118 else float state.x /. float state.w
2120 conf.zoom <- zoom;
2121 reshape conf.winw conf.winh;
2122 if zoom > 1.0
2123 then (
2124 let x = relx *. float state.w in
2125 state.x <- truncate x;
2127 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2130 | Some (layout, y, started) ->
2131 let time =
2132 match conf.maxwait with
2133 | None -> 0.0
2134 | Some t -> t
2136 let dt = now () -. started in
2137 if dt > time
2138 then (
2139 state.y <- y;
2140 load layout;
2144 let setcolumns columns coverA coverB =
2145 if columns < 2
2146 then (
2147 conf.columns <- None;
2148 state.x <- 0;
2149 setzoom 1.0;
2151 else (
2152 conf.columns <- Some ((columns, coverA, coverB), [||]);
2153 conf.zoom <- 1.0;
2155 reshape conf.winw conf.winh;
2158 let enterbirdseye () =
2159 let zoom = float conf.thumbw /. float conf.winw in
2160 let birdseyepageno =
2161 let cy = conf.winh / 2 in
2162 let fold = function
2163 | [] -> 0
2164 | l :: rest ->
2165 let rec fold best = function
2166 | [] -> best.pageno
2167 | l :: rest ->
2168 let d = cy - (l.pagedispy + l.pagevh/2)
2169 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2170 if abs d < abs dbest
2171 then fold l rest
2172 else best.pageno
2173 in fold l rest
2175 fold state.layout
2177 state.mode <- Birdseye (
2178 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2180 conf.zoom <- zoom;
2181 conf.presentation <- false;
2182 conf.interpagespace <- 10;
2183 conf.hlinks <- false;
2184 state.x <- 0;
2185 state.mstate <- Mnone;
2186 conf.maxwait <- None;
2187 conf.columns <- (
2188 match conf.beyecolumns with
2189 | Some c ->
2190 conf.zoom <- 1.0;
2191 Some ((c, 0, 0), [||])
2192 | None -> None
2194 Glut.setCursor Glut.CURSOR_INHERIT;
2195 if conf.verbose
2196 then
2197 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2198 (100.0*.zoom)
2199 else
2200 state.text <- ""
2202 reshape conf.winw conf.winh;
2205 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2206 state.mode <- View;
2207 conf.zoom <- c.zoom;
2208 conf.presentation <- c.presentation;
2209 conf.interpagespace <- c.interpagespace;
2210 conf.maxwait <- c.maxwait;
2211 conf.hlinks <- c.hlinks;
2212 conf.beyecolumns <- (
2213 match conf.columns with
2214 | Some ((c, _, _), _) -> Some c
2215 | None -> None
2217 conf.columns <- (
2218 match c.columns with
2219 | Some (c, _) -> Some (c, [||])
2220 | None -> None
2222 state.x <- leftx;
2223 if conf.verbose
2224 then
2225 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2226 (100.0*.conf.zoom)
2228 reshape conf.winw conf.winh;
2229 state.anchor <- if goback then anchor else (pageno, 0.0);
2232 let togglebirdseye () =
2233 match state.mode with
2234 | Birdseye vals -> leavebirdseye vals true
2235 | View -> enterbirdseye ()
2236 | _ -> ()
2239 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2240 let pageno = max 0 (pageno - incr) in
2241 let rec loop = function
2242 | [] -> gotopage1 pageno 0
2243 | l :: _ when l.pageno = pageno ->
2244 if l.pagedispy >= 0 && l.pagey = 0
2245 then G.postRedisplay "upbirdseye"
2246 else gotopage1 pageno 0
2247 | _ :: rest -> loop rest
2249 loop state.layout;
2250 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2253 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2254 let pageno = min (state.pagecount - 1) (pageno + incr) in
2255 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2256 let rec loop = function
2257 | [] ->
2258 let y, h = getpageyh pageno in
2259 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2260 gotoy (clamp dy)
2261 | l :: _ when l.pageno = pageno ->
2262 if l.pagevh != l.pageh
2263 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2264 else G.postRedisplay "downbirdseye"
2265 | _ :: rest -> loop rest
2267 loop state.layout
2270 let optentry mode _ key =
2271 let btos b = if b then "on" else "off" in
2272 let c = Char.unsafe_chr key in
2273 match c with
2274 | 's' ->
2275 let ondone s =
2276 try conf.scrollstep <- int_of_string s with exc ->
2277 state.text <- Printf.sprintf "bad integer `%s': %s"
2278 s (Printexc.to_string exc)
2280 TEswitch ("scroll step: ", "", None, intentry, ondone)
2282 | 'A' ->
2283 let ondone s =
2285 conf.autoscrollstep <- int_of_string s;
2286 if state.autoscroll <> None
2287 then state.autoscroll <- Some conf.autoscrollstep
2288 with exc ->
2289 state.text <- Printf.sprintf "bad integer `%s': %s"
2290 s (Printexc.to_string exc)
2292 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2294 | 'C' ->
2295 let ondone s =
2297 let n, a, b = columns_of_string s in
2298 setcolumns n a b;
2299 with exc ->
2300 state.text <- Printf.sprintf "bad columns `%s': %s"
2301 s (Printexc.to_string exc)
2303 TEswitch ("columns: ", "", None, textentry, ondone)
2305 | 'Z' ->
2306 let ondone s =
2308 let zoom = float (int_of_string s) /. 100.0 in
2309 setzoom zoom
2310 with exc ->
2311 state.text <- Printf.sprintf "bad integer `%s': %s"
2312 s (Printexc.to_string exc)
2314 TEswitch ("zoom: ", "", None, intentry, ondone)
2316 | 't' ->
2317 let ondone s =
2319 conf.thumbw <- bound (int_of_string s) 2 4096;
2320 state.text <-
2321 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2322 begin match mode with
2323 | Birdseye beye ->
2324 leavebirdseye beye false;
2325 enterbirdseye ();
2326 | _ -> ();
2328 with exc ->
2329 state.text <- Printf.sprintf "bad integer `%s': %s"
2330 s (Printexc.to_string exc)
2332 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2334 | 'R' ->
2335 let ondone s =
2336 match try
2337 Some (int_of_string s)
2338 with exc ->
2339 state.text <- Printf.sprintf "bad integer `%s': %s"
2340 s (Printexc.to_string exc);
2341 None
2342 with
2343 | Some angle -> reqlayout angle conf.proportional
2344 | None -> ()
2346 TEswitch ("rotation: ", "", None, intentry, ondone)
2348 | 'i' ->
2349 conf.icase <- not conf.icase;
2350 TEdone ("case insensitive search " ^ (btos conf.icase))
2352 | 'p' ->
2353 conf.preload <- not conf.preload;
2354 gotoy state.y;
2355 TEdone ("preload " ^ (btos conf.preload))
2357 | 'v' ->
2358 conf.verbose <- not conf.verbose;
2359 TEdone ("verbose " ^ (btos conf.verbose))
2361 | 'd' ->
2362 conf.debug <- not conf.debug;
2363 TEdone ("debug " ^ (btos conf.debug))
2365 | 'h' ->
2366 conf.maxhfit <- not conf.maxhfit;
2367 state.maxy <-
2368 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2369 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2371 | 'c' ->
2372 conf.crophack <- not conf.crophack;
2373 TEdone ("crophack " ^ btos conf.crophack)
2375 | 'a' ->
2376 let s =
2377 match conf.maxwait with
2378 | None ->
2379 conf.maxwait <- Some infinity;
2380 "always wait for page to complete"
2381 | Some _ ->
2382 conf.maxwait <- None;
2383 "show placeholder if page is not ready"
2385 TEdone s
2387 | 'f' ->
2388 conf.underinfo <- not conf.underinfo;
2389 TEdone ("underinfo " ^ btos conf.underinfo)
2391 | 'P' ->
2392 conf.savebmarks <- not conf.savebmarks;
2393 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2395 | 'S' ->
2396 let ondone s =
2398 let pageno, py =
2399 match state.layout with
2400 | [] -> 0, 0
2401 | l :: _ ->
2402 l.pageno, l.pagey
2404 conf.interpagespace <- int_of_string s;
2405 state.maxy <- calcheight ();
2406 let y = getpagey pageno in
2407 gotoy (y + py)
2408 with exc ->
2409 state.text <- Printf.sprintf "bad integer `%s': %s"
2410 s (Printexc.to_string exc)
2412 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2414 | 'l' ->
2415 reqlayout conf.angle (not conf.proportional);
2416 TEdone ("proportional display " ^ btos conf.proportional)
2418 | 'T' ->
2419 settrim (not conf.trimmargins) conf.trimfuzz;
2420 TEdone ("trim margins " ^ btos conf.trimmargins)
2422 | 'I' ->
2423 conf.invert <- not conf.invert;
2424 TEdone ("invert colors " ^ btos conf.invert)
2426 | _ ->
2427 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2428 TEstop
2431 class type lvsource = object
2432 method getitemcount : int
2433 method getitem : int -> (string * int)
2434 method hasaction : int -> bool
2435 method exit :
2436 uioh:uioh ->
2437 cancel:bool ->
2438 active:int ->
2439 first:int ->
2440 pan:int ->
2441 qsearch:string ->
2442 uioh option
2443 method getactive : int
2444 method getfirst : int
2445 method getqsearch : string
2446 method setqsearch : string -> unit
2447 method getpan : int
2448 end;;
2450 class virtual lvsourcebase = object
2451 val mutable m_active = 0
2452 val mutable m_first = 0
2453 val mutable m_qsearch = ""
2454 val mutable m_pan = 0
2455 method getactive = m_active
2456 method getfirst = m_first
2457 method getqsearch = m_qsearch
2458 method getpan = m_pan
2459 method setqsearch s = m_qsearch <- s
2460 end;;
2462 let textentryspecial key = function
2463 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2464 let s =
2465 match key with
2466 | Glut.KEY_UP -> action HCprev
2467 | Glut.KEY_DOWN -> action HCnext
2468 | Glut.KEY_HOME -> action HCfirst
2469 | Glut.KEY_END -> action HClast
2470 | _ -> state.text
2472 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2473 G.postRedisplay "special textentry";
2474 | _ -> ()
2477 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2478 let enttext te =
2479 state.mode <- Textentry (te, onleave);
2480 state.text <- "";
2481 enttext ();
2482 G.postRedisplay "textentrykeyboard enttext";
2484 match Char.unsafe_chr key with
2485 | '\008' -> (* backspace *)
2486 let len = String.length text in
2487 if len = 0
2488 then (
2489 onleave Cancel;
2490 G.postRedisplay "textentrykeyboard after cancel";
2492 else (
2493 let s = String.sub text 0 (len - 1) in
2494 enttext (c, s, opthist, onkey, ondone)
2497 | '\r' | '\n' ->
2498 ondone text;
2499 onleave Confirm;
2500 G.postRedisplay "textentrykeyboard after confirm"
2502 | '\007' (* ctrl-g *)
2503 | '\027' -> (* escape *)
2504 if String.length text = 0
2505 then (
2506 begin match opthist with
2507 | None -> ()
2508 | Some (_, onhistcancel) -> onhistcancel ()
2509 end;
2510 onleave Cancel;
2511 state.text <- "";
2512 G.postRedisplay "textentrykeyboard after cancel2"
2514 else (
2515 enttext (c, "", opthist, onkey, ondone)
2518 | '\127' -> () (* delete *)
2520 | _ ->
2521 begin match onkey text key with
2522 | TEdone text ->
2523 ondone text;
2524 onleave Confirm;
2525 G.postRedisplay "textentrykeyboard after confirm2";
2527 | TEcont text ->
2528 enttext (c, text, opthist, onkey, ondone);
2530 | TEstop ->
2531 onleave Cancel;
2532 G.postRedisplay "textentrykeyboard after cancel3"
2534 | TEswitch te ->
2535 state.mode <- Textentry (te, onleave);
2536 G.postRedisplay "textentrykeyboard switch";
2537 end;
2540 let firstof first active =
2541 if first > active || abs (first - active) > fstate.maxrows - 1
2542 then max 0 (active - (fstate.maxrows/2))
2543 else first
2546 let calcfirst first active =
2547 if active > first
2548 then
2549 let rows = active - first in
2550 if rows > fstate.maxrows then active - fstate.maxrows else first
2551 else active
2554 let scrollph y maxy =
2555 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2556 let sh = float conf.winh /. sh in
2557 let sh = max sh (float conf.scrollh) in
2559 let percent =
2560 if y = state.maxy
2561 then 1.0
2562 else float y /. float maxy
2564 let position = (float conf.winh -. sh) *. percent in
2566 let position =
2567 if position +. sh > float conf.winh
2568 then float conf.winh -. sh
2569 else position
2571 position, sh;
2574 let coe s = (s :> uioh);;
2576 class listview ~(source:lvsource) ~trusted =
2577 object (self)
2578 val m_pan = source#getpan
2579 val m_first = source#getfirst
2580 val m_active = source#getactive
2581 val m_qsearch = source#getqsearch
2582 val m_prev_uioh = state.uioh
2584 method private elemunder y =
2585 let n = y / (fstate.fontsize+1) in
2586 if m_first + n < source#getitemcount
2587 then (
2588 if source#hasaction (m_first + n)
2589 then Some (m_first + n)
2590 else None
2592 else None
2594 method display =
2595 Gl.enable `blend;
2596 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2597 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2598 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2599 GlDraw.color (1., 1., 1.);
2600 Gl.enable `texture_2d;
2601 let fs = fstate.fontsize in
2602 let nfs = fs + 1 in
2603 let ww = fstate.wwidth in
2604 let tabw = 30.0*.ww in
2605 let itemcount = source#getitemcount in
2606 let rec loop row =
2607 if (row - m_first) * nfs > conf.winh
2608 then ()
2609 else (
2610 if row >= 0 && row < itemcount
2611 then (
2612 let (s, level) = source#getitem row in
2613 let y = (row - m_first) * nfs in
2614 let x = 5.0 +. float (level + m_pan) *. ww in
2615 if row = m_active
2616 then (
2617 Gl.disable `texture_2d;
2618 GlDraw.polygon_mode `both `line;
2619 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2620 GlDraw.rect (1., float (y + 1))
2621 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2622 GlDraw.polygon_mode `both `fill;
2623 GlDraw.color (1., 1., 1.);
2624 Gl.enable `texture_2d;
2627 let drawtabularstring s =
2628 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2629 if trusted
2630 then
2631 let tabpos = try String.index s '\t' with Not_found -> -1 in
2632 if tabpos > 0
2633 then
2634 let len = String.length s - tabpos - 1 in
2635 let s1 = String.sub s 0 tabpos
2636 and s2 = String.sub s (tabpos + 1) len in
2637 let nx = drawstr x s1 in
2638 let sw = nx -. x in
2639 let x = x +. (max tabw sw) in
2640 drawstr x s2
2641 else
2642 drawstr x s
2643 else
2644 drawstr x s
2646 let _ = drawtabularstring s in
2647 loop (row+1)
2651 loop m_first;
2652 Gl.disable `blend;
2653 Gl.disable `texture_2d;
2655 method updownlevel incr =
2656 let len = source#getitemcount in
2657 let curlevel =
2658 if m_active >= 0 && m_active < len
2659 then snd (source#getitem m_active)
2660 else -1
2662 let rec flow i =
2663 if i = len then i-1 else if i = -1 then 0 else
2664 let _, l = source#getitem i in
2665 if l != curlevel then i else flow (i+incr)
2667 let active = flow m_active in
2668 let first = calcfirst m_first active in
2669 G.postRedisplay "special outline updownlevel";
2670 {< m_active = active; m_first = first >}
2672 method private key1 key =
2673 let set active first qsearch =
2674 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2676 let search active pattern incr =
2677 let dosearch re =
2678 let rec loop n =
2679 if n >= 0 && n < source#getitemcount
2680 then (
2681 let s, _ = source#getitem n in
2683 (try ignore (Str.search_forward re s 0); true
2684 with Not_found -> false)
2685 then Some n
2686 else loop (n + incr)
2688 else None
2690 loop active
2693 let re = Str.regexp_case_fold pattern in
2694 dosearch re
2695 with Failure s ->
2696 state.text <- s;
2697 None
2699 match key with
2700 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2701 let incr = if key = 18 then -1 else 1 in
2702 let active, first =
2703 match search (m_active + incr) m_qsearch incr with
2704 | None ->
2705 state.text <- m_qsearch ^ " [not found]";
2706 m_active, m_first
2707 | Some active ->
2708 state.text <- m_qsearch;
2709 active, firstof m_first active
2711 G.postRedisplay "listview ctrl-r/s";
2712 set active first m_qsearch;
2714 | 8 -> (* backspace *)
2715 let len = String.length m_qsearch in
2716 if len = 0
2717 then coe self
2718 else (
2719 if len = 1
2720 then (
2721 state.text <- "";
2722 G.postRedisplay "listview empty qsearch";
2723 set m_active m_first "";
2725 else
2726 let qsearch = String.sub m_qsearch 0 (len - 1) in
2727 let active, first =
2728 match search m_active qsearch ~-1 with
2729 | None ->
2730 state.text <- qsearch ^ " [not found]";
2731 m_active, m_first
2732 | Some active ->
2733 state.text <- qsearch;
2734 active, firstof m_first active
2736 G.postRedisplay "listview backspace qsearch";
2737 set active first qsearch
2740 | _ when key >= 32 && key < 127 ->
2741 let pattern = addchar m_qsearch (Char.chr key) in
2742 let active, first =
2743 match search m_active pattern 1 with
2744 | None ->
2745 state.text <- pattern ^ " [not found]";
2746 m_active, m_first
2747 | Some active ->
2748 state.text <- pattern;
2749 active, firstof m_first active
2751 G.postRedisplay "listview qsearch add";
2752 set active first pattern;
2754 | 27 -> (* escape *)
2755 state.text <- "";
2756 if String.length m_qsearch = 0
2757 then (
2758 G.postRedisplay "list view escape";
2759 begin
2760 match
2761 source#exit (coe self) true m_active m_first m_pan m_qsearch
2762 with
2763 | None -> m_prev_uioh
2764 | Some uioh -> uioh
2767 else (
2768 G.postRedisplay "list view kill qsearch";
2769 source#setqsearch "";
2770 coe {< m_qsearch = "" >}
2773 | 13 -> (* enter *)
2774 state.text <- "";
2775 let self = {< m_qsearch = "" >} in
2776 source#setqsearch "";
2777 let opt =
2778 G.postRedisplay "listview enter";
2779 if m_active >= 0 && m_active < source#getitemcount
2780 then (
2781 source#exit (coe self) false m_active m_first m_pan "";
2783 else (
2784 source#exit (coe self) true m_active m_first m_pan "";
2787 begin match opt with
2788 | None -> m_prev_uioh
2789 | Some uioh -> uioh
2792 | 127 -> (* delete *)
2793 coe self
2795 | _ -> dolog "unknown key %d" key; coe self
2797 method private special1 key =
2798 let itemcount = source#getitemcount in
2799 let find start incr =
2800 let rec find i =
2801 if i = -1 || i = itemcount
2802 then -1
2803 else (
2804 if source#hasaction i
2805 then i
2806 else find (i + incr)
2809 find start
2811 let set active first =
2812 let first = bound first 0 (itemcount - fstate.maxrows) in
2813 state.text <- "";
2814 coe {< m_active = active; m_first = first >}
2816 let navigate incr =
2817 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2818 let active, first =
2819 let incr1 = if incr > 0 then 1 else -1 in
2820 if isvisible m_first m_active
2821 then
2822 let next =
2823 let next = m_active + incr in
2824 let next =
2825 if next < 0 || next >= itemcount
2826 then -1
2827 else find next incr1
2829 if next = -1 || abs (m_active - next) > fstate.maxrows
2830 then -1
2831 else next
2833 if next = -1
2834 then
2835 let first = m_first + incr in
2836 let first = bound first 0 (itemcount - 1) in
2837 let next =
2838 let next = m_active + incr in
2839 let next = bound next 0 (itemcount - 1) in
2840 find next ~-incr1
2842 let active = if next = -1 then m_active else next in
2843 active, first
2844 else
2845 let first = min next m_first in
2846 let first =
2847 if abs (next - first) > fstate.maxrows
2848 then first + incr
2849 else first
2851 next, first
2852 else
2853 let first = m_first + incr in
2854 let first = bound first 0 (itemcount - 1) in
2855 let active =
2856 let next = m_active + incr in
2857 let next = bound next 0 (itemcount - 1) in
2858 let next = find next incr1 in
2859 let active =
2860 if next = -1 || abs (m_active - first) > fstate.maxrows
2861 then (
2862 let active = if m_active = -1 then next else m_active in
2863 active
2865 else next
2867 if isvisible first active
2868 then active
2869 else -1
2871 active, first
2873 G.postRedisplay "listview navigate";
2874 set active first;
2876 begin match key with
2877 | Glut.KEY_UP -> navigate ~-1
2878 | Glut.KEY_DOWN -> navigate 1
2879 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2880 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2882 | Glut.KEY_RIGHT ->
2883 state.text <- "";
2884 G.postRedisplay "listview right";
2885 coe {< m_pan = m_pan - 1 >}
2887 | Glut.KEY_LEFT ->
2888 state.text <- "";
2889 G.postRedisplay "listview left";
2890 coe {< m_pan = m_pan + 1 >}
2892 | Glut.KEY_HOME ->
2893 let active = find 0 1 in
2894 G.postRedisplay "listview home";
2895 set active 0;
2897 | Glut.KEY_END ->
2898 let first = max 0 (itemcount - fstate.maxrows) in
2899 let active = find (itemcount - 1) ~-1 in
2900 G.postRedisplay "listview end";
2901 set active first;
2903 | _ -> coe self
2904 end;
2906 method key key =
2907 match state.mode with
2908 | Textentry te -> textentrykeyboard key te; coe self
2909 | _ -> self#key1 key
2911 method special key =
2912 match state.mode with
2913 | Textentry te -> textentryspecial key te; coe self
2914 | _ -> self#special1 key
2916 method button button bstate x y =
2917 let opt =
2918 match button with
2919 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollbw ->
2920 G.postRedisplay "listview scroll";
2921 if bstate = Glut.DOWN
2922 then
2923 let _, position, sh = self#scrollph in
2924 if y > truncate position && y < truncate (position +. sh)
2925 then (
2926 state.mstate <- Mscrolly;
2927 Some (coe self)
2929 else
2930 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2931 let first = truncate (s *. float source#getitemcount) in
2932 let first = min source#getitemcount first in
2933 Some (coe {< m_first = first; m_active = first >})
2934 else (
2935 state.mstate <- Mnone;
2936 Some (coe self);
2938 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2939 begin match self#elemunder y with
2940 | Some n ->
2941 G.postRedisplay "listview click";
2942 source#exit
2943 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2944 | _ ->
2945 Some (coe self)
2947 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2948 let len = source#getitemcount in
2949 let first =
2950 if n = 4 && m_first + fstate.maxrows >= len
2951 then
2952 m_first
2953 else
2954 let first = m_first + (if n == 3 then -1 else 1) in
2955 bound first 0 (len - 1)
2957 G.postRedisplay "listview wheel";
2958 Some (coe {< m_first = first >})
2959 | _ ->
2960 Some (coe self)
2962 match opt with
2963 | None -> m_prev_uioh
2964 | Some uioh -> uioh
2966 method motion _ y =
2967 match state.mstate with
2968 | Mscrolly ->
2969 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2970 let first = truncate (s *. float source#getitemcount) in
2971 let first = min source#getitemcount first in
2972 G.postRedisplay "listview motion";
2973 coe {< m_first = first; m_active = first >}
2974 | _ -> coe self
2976 method pmotion x y =
2977 if x < conf.winw - conf.scrollbw
2978 then
2979 let n =
2980 match self#elemunder y with
2981 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2982 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2984 let o =
2985 if n != m_active
2986 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2987 else self
2989 coe o
2990 else (
2991 Glut.setCursor Glut.CURSOR_INHERIT;
2992 coe self
2995 method infochanged _ = ()
2997 method scrollpw = (0, 0.0, 0.0)
2998 method scrollph =
2999 let nfs = fstate.fontsize + 1 in
3000 let y = m_first * nfs in
3001 let itemcount = source#getitemcount in
3002 let maxi = max 0 (itemcount - fstate.maxrows) in
3003 let maxy = maxi * nfs in
3004 let p, h = scrollph y maxy in
3005 conf.scrollbw, p, h
3006 end;;
3008 class outlinelistview ~source =
3009 object (self)
3010 inherit listview ~source:(source :> lvsource) ~trusted:false as super
3012 method key key =
3013 match key with
3014 | 14 -> (* ctrl-n *)
3015 source#narrow m_qsearch;
3016 G.postRedisplay "outline ctrl-n";
3017 coe {< m_first = 0; m_active = 0 >}
3019 | 21 -> (* ctrl-u *)
3020 source#denarrow;
3021 G.postRedisplay "outline ctrl-u";
3022 state.text <- "";
3023 coe {< m_first = 0; m_active = 0 >}
3025 | 12 -> (* ctrl-l *)
3026 let first = m_active - (fstate.maxrows / 2) in
3027 G.postRedisplay "outline ctrl-l";
3028 coe {< m_first = first >}
3030 | 127 -> (* delete *)
3031 source#remove m_active;
3032 G.postRedisplay "outline delete";
3033 let active = max 0 (m_active-1) in
3034 coe {< m_first = firstof m_first active;
3035 m_active = active >}
3037 | key -> super#key key
3039 method special key =
3040 let calcfirst first active =
3041 if active > first
3042 then
3043 let rows = active - first in
3044 if rows > fstate.maxrows then active - fstate.maxrows else first
3045 else active
3047 let navigate incr =
3048 let active = m_active + incr in
3049 let active = bound active 0 (source#getitemcount - 1) in
3050 let first = calcfirst m_first active in
3051 G.postRedisplay "special outline navigate";
3052 coe {< m_active = active; m_first = first >}
3054 match key with
3055 | Glut.KEY_UP -> navigate ~-1
3056 | Glut.KEY_DOWN -> navigate 1
3057 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
3058 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
3060 | Glut.KEY_RIGHT ->
3061 let o =
3062 if Glut.getModifiers () land Glut.active_ctrl != 0
3063 then (
3064 G.postRedisplay "special outline right";
3065 {< m_pan = m_pan + 1 >}
3067 else self#updownlevel 1
3069 coe o
3071 | Glut.KEY_LEFT ->
3072 let o =
3073 if Glut.getModifiers () land Glut.active_ctrl != 0
3074 then (
3075 G.postRedisplay "special outline left";
3076 {< m_pan = m_pan - 1 >}
3078 else self#updownlevel ~-1
3080 coe o
3082 | Glut.KEY_HOME ->
3083 G.postRedisplay "special outline home";
3084 coe {< m_first = 0; m_active = 0 >}
3086 | Glut.KEY_END ->
3087 let active = source#getitemcount - 1 in
3088 let first = max 0 (active - fstate.maxrows) in
3089 G.postRedisplay "special outline end";
3090 coe {< m_active = active; m_first = first >}
3092 | _ -> super#special key
3095 let outlinesource usebookmarks =
3096 let empty = [||] in
3097 (object
3098 inherit lvsourcebase
3099 val mutable m_items = empty
3100 val mutable m_orig_items = empty
3101 val mutable m_prev_items = empty
3102 val mutable m_narrow_pattern = ""
3103 val mutable m_hadremovals = false
3105 method getitemcount =
3106 Array.length m_items + (if m_hadremovals then 1 else 0)
3108 method getitem n =
3109 if n == Array.length m_items && m_hadremovals
3110 then
3111 ("[Confirm removal]", 0)
3112 else
3113 let s, n, _ = m_items.(n) in
3114 (s, n)
3116 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3117 ignore (uioh, first, pan, qsearch);
3118 let confrimremoval = m_hadremovals && active = Array.length m_items in
3119 let items =
3120 if String.length m_narrow_pattern = 0
3121 then m_orig_items
3122 else m_items
3124 if not cancel
3125 then (
3126 if not confrimremoval
3127 then(
3128 let _, _, anchor = m_items.(active) in
3129 gotoanchor anchor;
3130 m_items <- items;
3132 else (
3133 state.bookmarks <- Array.to_list m_items;
3134 m_orig_items <- m_items;
3137 else m_items <- items;
3138 None
3140 method hasaction _ = true
3142 method greetmsg =
3143 if Array.length m_items != Array.length m_orig_items
3144 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3145 else ""
3147 method narrow pattern =
3148 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3149 match reopt with
3150 | None -> ()
3151 | Some re ->
3152 let rec loop accu n =
3153 if n = -1
3154 then (
3155 m_narrow_pattern <- pattern;
3156 m_items <- Array.of_list accu
3158 else
3159 let (s, _, _) as o = m_items.(n) in
3160 let accu =
3161 if (try ignore (Str.search_forward re s 0); true
3162 with Not_found -> false)
3163 then o :: accu
3164 else accu
3166 loop accu (n-1)
3168 loop [] (Array.length m_items - 1)
3170 method denarrow =
3171 m_orig_items <- (
3172 if usebookmarks
3173 then Array.of_list state.bookmarks
3174 else state.outlines
3176 m_items <- m_orig_items
3178 method remove m =
3179 if usebookmarks
3180 then
3181 if m >= 0 && m < Array.length m_items
3182 then (
3183 m_hadremovals <- true;
3184 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3185 let n = if n >= m then n+1 else n in
3186 m_items.(n)
3190 method reset anchor items =
3191 m_hadremovals <- false;
3192 if m_orig_items == empty || m_prev_items != items
3193 then (
3194 m_orig_items <- items;
3195 if String.length m_narrow_pattern = 0
3196 then m_items <- items;
3198 m_prev_items <- items;
3199 let rely = getanchory anchor in
3200 let active =
3201 let rec loop n best bestd =
3202 if n = Array.length m_items
3203 then best
3204 else
3205 let (_, _, anchor) = m_items.(n) in
3206 let orely = getanchory anchor in
3207 let d = abs (orely - rely) in
3208 if d < bestd
3209 then loop (n+1) n d
3210 else loop (n+1) best bestd
3212 loop 0 ~-1 max_int
3214 m_active <- active;
3215 m_first <- firstof m_first active
3216 end)
3219 let enterselector usebookmarks =
3220 let source = outlinesource usebookmarks in
3221 fun errmsg ->
3222 let outlines =
3223 if usebookmarks
3224 then Array.of_list state.bookmarks
3225 else state.outlines
3227 if Array.length outlines = 0
3228 then (
3229 showtext ' ' errmsg;
3231 else (
3232 state.text <- source#greetmsg;
3233 Glut.setCursor Glut.CURSOR_INHERIT;
3234 let anchor = getanchor () in
3235 source#reset anchor outlines;
3236 state.uioh <- coe (new outlinelistview ~source);
3237 G.postRedisplay "enter selector";
3241 let enteroutlinemode =
3242 let f = enterselector false in
3243 fun ()-> f "Document has no outline";
3246 let enterbookmarkmode =
3247 let f = enterselector true in
3248 fun () -> f "Document has no bookmarks (yet)";
3251 let color_of_string s =
3252 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3253 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3257 let color_to_string (r, g, b) =
3258 let r = truncate (r *. 256.0)
3259 and g = truncate (g *. 256.0)
3260 and b = truncate (b *. 256.0) in
3261 Printf.sprintf "%d/%d/%d" r g b
3264 let irect_of_string s =
3265 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3268 let irect_to_string (x0,y0,x1,y1) =
3269 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3272 let makecheckers () =
3273 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3274 following to say:
3275 converted by Issac Trotts. July 25, 2002 *)
3276 let image_height = 64
3277 and image_width = 64 in
3279 let make_image () =
3280 let image =
3281 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3283 for i = 0 to image_width - 1 do
3284 for j = 0 to image_height - 1 do
3285 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3286 (if (i land 8 ) lxor (j land 8) = 0
3287 then [|255;255;255|] else [|200;200;200|])
3288 done
3289 done;
3290 image
3292 let image = make_image () in
3293 let id = GlTex.gen_texture () in
3294 GlTex.bind_texture `texture_2d id;
3295 GlPix.store (`unpack_alignment 1);
3296 GlTex.image2d image;
3297 List.iter (GlTex.parameter ~target:`texture_2d)
3298 [ `wrap_s `repeat;
3299 `wrap_t `repeat;
3300 `mag_filter `nearest;
3301 `min_filter `nearest ];
3305 let setcheckers enabled =
3306 match state.texid with
3307 | None ->
3308 if enabled then state.texid <- Some (makecheckers ())
3310 | Some texid ->
3311 if not enabled
3312 then (
3313 GlTex.delete_texture texid;
3314 state.texid <- None;
3318 let int_of_string_with_suffix s =
3319 let l = String.length s in
3320 let s1, shift =
3321 if l > 1
3322 then
3323 let suffix = Char.lowercase s.[l-1] in
3324 match suffix with
3325 | 'k' -> String.sub s 0 (l-1), 10
3326 | 'm' -> String.sub s 0 (l-1), 20
3327 | 'g' -> String.sub s 0 (l-1), 30
3328 | _ -> s, 0
3329 else s, 0
3331 let n = int_of_string s1 in
3332 let m = n lsl shift in
3333 if m < 0 || m < n
3334 then raise (Failure "value too large")
3335 else m
3338 let string_with_suffix_of_int n =
3339 if n = 0
3340 then "0"
3341 else
3342 let n, s =
3343 if n = 0
3344 then 0, ""
3345 else (
3346 if n land ((1 lsl 20) - 1) = 0
3347 then n lsr 20, "M"
3348 else (
3349 if n land ((1 lsl 10) - 1) = 0
3350 then n lsr 10, "K"
3351 else n, ""
3355 let rec loop s n =
3356 let h = n mod 1000 in
3357 let n = n / 1000 in
3358 if n = 0
3359 then string_of_int h ^ s
3360 else (
3361 let s = Printf.sprintf "_%03d%s" h s in
3362 loop s n
3365 loop "" n ^ s;
3368 let defghyllscroll = (40, 8, 32);;
3369 let ghyllscroll_of_string s =
3370 let (n, a, b) as nab =
3371 if s = "default"
3372 then defghyllscroll
3373 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3375 if n <= a || n <= b || a >= b
3376 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3377 nab;
3380 let ghyllscroll_to_string ((n, a, b) as nab) =
3381 if nab = defghyllscroll
3382 then "default"
3383 else Printf.sprintf "%d,%d,%d" n a b;
3386 let describe_location () =
3387 let f (fn, _) l =
3388 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3390 let fn, ln = List.fold_left f (-1, -1) state.layout in
3391 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3392 let percent =
3393 if maxy <= 0
3394 then 100.
3395 else (100. *. (float state.y /. float maxy))
3397 if fn = ln
3398 then
3399 Printf.sprintf "page %d of %d [%.2f%%]"
3400 (fn+1) state.pagecount percent
3401 else
3402 Printf.sprintf
3403 "pages %d-%d of %d [%.2f%%]"
3404 (fn+1) (ln+1) state.pagecount percent
3407 let enterinfomode =
3408 let btos b = if b then "\xe2\x88\x9a" else "" in
3409 let showextended = ref false in
3410 let leave mode = function
3411 | Confirm -> state.mode <- mode
3412 | Cancel -> state.mode <- mode in
3413 let src =
3414 (object
3415 val mutable m_first_time = true
3416 val mutable m_l = []
3417 val mutable m_a = [||]
3418 val mutable m_prev_uioh = nouioh
3419 val mutable m_prev_mode = View
3421 inherit lvsourcebase
3423 method reset prev_mode prev_uioh =
3424 m_a <- Array.of_list (List.rev m_l);
3425 m_l <- [];
3426 m_prev_mode <- prev_mode;
3427 m_prev_uioh <- prev_uioh;
3428 if m_first_time
3429 then (
3430 let rec loop n =
3431 if n >= Array.length m_a
3432 then ()
3433 else
3434 match m_a.(n) with
3435 | _, _, _, Action _ -> m_active <- n
3436 | _ -> loop (n+1)
3438 loop 0;
3439 m_first_time <- false;
3442 method int name get set =
3443 m_l <-
3444 (name, `int get, 1, Action (
3445 fun u ->
3446 let ondone s =
3447 try set (int_of_string s)
3448 with exn ->
3449 state.text <- Printf.sprintf "bad integer `%s': %s"
3450 s (Printexc.to_string exn)
3452 state.text <- "";
3453 let te = name ^ ": ", "", None, intentry, ondone in
3454 state.mode <- Textentry (te, leave m_prev_mode);
3456 )) :: m_l
3458 method int_with_suffix name get set =
3459 m_l <-
3460 (name, `intws get, 1, Action (
3461 fun u ->
3462 let ondone s =
3463 try set (int_of_string_with_suffix s)
3464 with exn ->
3465 state.text <- Printf.sprintf "bad integer `%s': %s"
3466 s (Printexc.to_string exn)
3468 state.text <- "";
3469 let te =
3470 name ^ ": ", "", None, intentry_with_suffix, ondone
3472 state.mode <- Textentry (te, leave m_prev_mode);
3474 )) :: m_l
3476 method bool ?(offset=1) ?(btos=btos) name get set =
3477 m_l <-
3478 (name, `bool (btos, get), offset, Action (
3479 fun u ->
3480 let v = get () in
3481 set (not v);
3483 )) :: m_l
3485 method color name get set =
3486 m_l <-
3487 (name, `color get, 1, Action (
3488 fun u ->
3489 let invalid = (nan, nan, nan) in
3490 let ondone s =
3491 let c =
3492 try color_of_string s
3493 with exn ->
3494 state.text <- Printf.sprintf "bad color `%s': %s"
3495 s (Printexc.to_string exn);
3496 invalid
3498 if c <> invalid
3499 then set c;
3501 let te = name ^ ": ", "", None, textentry, ondone in
3502 state.text <- color_to_string (get ());
3503 state.mode <- Textentry (te, leave m_prev_mode);
3505 )) :: m_l
3507 method string name get set =
3508 m_l <-
3509 (name, `string get, 1, Action (
3510 fun u ->
3511 let ondone s = set s in
3512 let te = name ^ ": ", "", None, textentry, ondone in
3513 state.mode <- Textentry (te, leave m_prev_mode);
3515 )) :: m_l
3517 method colorspace name get set =
3518 m_l <-
3519 (name, `string get, 1, Action (
3520 fun _ ->
3521 let source =
3522 let vals = [| "rgb"; "bgr"; "gray" |] in
3523 (object
3524 inherit lvsourcebase
3526 initializer
3527 m_active <- int_of_colorspace conf.colorspace;
3528 m_first <- 0;
3530 method getitemcount = Array.length vals
3531 method getitem n = (vals.(n), 0)
3532 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3533 ignore (uioh, first, pan, qsearch);
3534 if not cancel then set active;
3535 None
3536 method hasaction _ = true
3537 end)
3539 state.text <- "";
3540 coe (new listview ~source ~trusted:true)
3541 )) :: m_l
3543 method caption s offset =
3544 m_l <- (s, `empty, offset, Noaction) :: m_l
3546 method caption2 s f offset =
3547 m_l <- (s, `string f, offset, Noaction) :: m_l
3549 method getitemcount = Array.length m_a
3551 method getitem n =
3552 let tostr = function
3553 | `int f -> string_of_int (f ())
3554 | `intws f -> string_with_suffix_of_int (f ())
3555 | `string f -> f ()
3556 | `color f -> color_to_string (f ())
3557 | `bool (btos, f) -> btos (f ())
3558 | `empty -> ""
3560 let name, t, offset, _ = m_a.(n) in
3561 ((let s = tostr t in
3562 if String.length s > 0
3563 then Printf.sprintf "%s\t%s" name s
3564 else name),
3565 offset)
3567 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3568 let uiohopt =
3569 if not cancel
3570 then (
3571 m_qsearch <- qsearch;
3572 let uioh =
3573 match m_a.(active) with
3574 | _, _, _, Action f -> f uioh
3575 | _ -> uioh
3577 Some uioh
3579 else None
3581 m_active <- active;
3582 m_first <- first;
3583 m_pan <- pan;
3584 uiohopt
3586 method hasaction n =
3587 match m_a.(n) with
3588 | _, _, _, Action _ -> true
3589 | _ -> false
3590 end)
3592 let rec fillsrc prevmode prevuioh =
3593 let sep () = src#caption "" 0 in
3594 let colorp name get set =
3595 src#string name
3596 (fun () -> color_to_string (get ()))
3597 (fun v ->
3599 let c = color_of_string v in
3600 set c
3601 with exn ->
3602 state.text <- Printf.sprintf "bad color `%s': %s"
3603 v (Printexc.to_string exn);
3606 let oldmode = state.mode in
3607 let birdseye = isbirdseye state.mode in
3609 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3611 src#bool "presentation mode"
3612 (fun () -> conf.presentation)
3613 (fun v ->
3614 conf.presentation <- v;
3615 state.anchor <- getanchor ();
3616 represent ());
3618 src#bool "ignore case in searches"
3619 (fun () -> conf.icase)
3620 (fun v -> conf.icase <- v);
3622 src#bool "preload"
3623 (fun () -> conf.preload)
3624 (fun v -> conf.preload <- v);
3626 src#bool "highlight links"
3627 (fun () -> conf.hlinks)
3628 (fun v -> conf.hlinks <- v);
3630 src#bool "under info"
3631 (fun () -> conf.underinfo)
3632 (fun v -> conf.underinfo <- v);
3634 src#bool "persistent bookmarks"
3635 (fun () -> conf.savebmarks)
3636 (fun v -> conf.savebmarks <- v);
3638 src#bool "proportional display"
3639 (fun () -> conf.proportional)
3640 (fun v -> reqlayout conf.angle v);
3642 src#bool "trim margins"
3643 (fun () -> conf.trimmargins)
3644 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3646 src#bool "persistent location"
3647 (fun () -> conf.jumpback)
3648 (fun v -> conf.jumpback <- v);
3650 sep ();
3651 src#int "inter-page space"
3652 (fun () -> conf.interpagespace)
3653 (fun n ->
3654 conf.interpagespace <- n;
3655 let pageno, py =
3656 match state.layout with
3657 | [] -> 0, 0
3658 | l :: _ ->
3659 l.pageno, l.pagey
3661 state.maxy <- calcheight ();
3662 let y = getpagey pageno in
3663 gotoy (y + py)
3666 src#int "page bias"
3667 (fun () -> conf.pagebias)
3668 (fun v -> conf.pagebias <- v);
3670 src#int "scroll step"
3671 (fun () -> conf.scrollstep)
3672 (fun n -> conf.scrollstep <- n);
3674 src#int "auto scroll step"
3675 (fun () ->
3676 match state.autoscroll with
3677 | Some step -> step
3678 | _ -> conf.autoscrollstep)
3679 (fun n ->
3680 if state.autoscroll <> None
3681 then state.autoscroll <- Some n;
3682 conf.autoscrollstep <- n);
3684 src#int "zoom"
3685 (fun () -> truncate (conf.zoom *. 100.))
3686 (fun v -> setzoom ((float v) /. 100.));
3688 src#int "rotation"
3689 (fun () -> conf.angle)
3690 (fun v -> reqlayout v conf.proportional);
3692 src#int "scroll bar width"
3693 (fun () -> state.scrollw)
3694 (fun v ->
3695 state.scrollw <- v;
3696 conf.scrollbw <- v;
3697 reshape conf.winw conf.winh;
3700 src#int "scroll handle height"
3701 (fun () -> conf.scrollh)
3702 (fun v -> conf.scrollh <- v;);
3704 src#int "thumbnail width"
3705 (fun () -> conf.thumbw)
3706 (fun v ->
3707 conf.thumbw <- min 4096 v;
3708 match oldmode with
3709 | Birdseye beye ->
3710 leavebirdseye beye false;
3711 enterbirdseye ()
3712 | _ -> ()
3715 src#string "columns"
3716 (fun () ->
3717 match conf.columns with
3718 | None -> "1"
3719 | Some (multicol, _) -> columns_to_string multicol)
3720 (fun v ->
3721 let n, a, b = columns_of_string v in
3722 setcolumns n a b);
3724 sep ();
3725 src#caption "Presentation mode" 0;
3726 src#bool "scrollbar visible"
3727 (fun () -> conf.scrollbarinpm)
3728 (fun v ->
3729 if v != conf.scrollbarinpm
3730 then (
3731 conf.scrollbarinpm <- v;
3732 if conf.presentation
3733 then (
3734 state.scrollw <- if v then conf.scrollbw else 0;
3735 reshape conf.winw conf.winh;
3740 sep ();
3741 src#caption "Pixmap cache" 0;
3742 src#int_with_suffix "size (advisory)"
3743 (fun () -> conf.memlimit)
3744 (fun v -> conf.memlimit <- v);
3746 src#caption2 "used"
3747 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3748 (string_with_suffix_of_int state.memused)
3749 (Hashtbl.length state.tilemap)) 1;
3751 sep ();
3752 src#caption "Layout" 0;
3753 src#caption2 "Dimension"
3754 (fun () ->
3755 Printf.sprintf "%dx%d (virtual %dx%d)"
3756 conf.winw conf.winh
3757 state.w state.maxy)
3759 if conf.debug
3760 then
3761 src#caption2 "Position" (fun () ->
3762 Printf.sprintf "%dx%d" state.x state.y
3764 else
3765 src#caption2 "Visible" (fun () -> describe_location ()) 1
3768 sep ();
3769 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3770 "Save these parameters as global defaults at exit"
3771 (fun () -> conf.bedefault)
3772 (fun v -> conf.bedefault <- v)
3775 sep ();
3776 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3777 src#bool ~offset:0 ~btos "Extended parameters"
3778 (fun () -> !showextended)
3779 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3780 if !showextended
3781 then (
3782 src#bool "checkers"
3783 (fun () -> conf.checkers)
3784 (fun v -> conf.checkers <- v; setcheckers v);
3785 src#bool "verbose"
3786 (fun () -> conf.verbose)
3787 (fun v -> conf.verbose <- v);
3788 src#bool "invert colors"
3789 (fun () -> conf.invert)
3790 (fun v -> conf.invert <- v);
3791 src#bool "max fit"
3792 (fun () -> conf.maxhfit)
3793 (fun v -> conf.maxhfit <- v);
3794 src#bool "redirect stderr"
3795 (fun () -> conf.redirectstderr)
3796 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3797 src#string "uri launcher"
3798 (fun () -> conf.urilauncher)
3799 (fun v -> conf.urilauncher <- v);
3800 src#string "tile size"
3801 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3802 (fun v ->
3804 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3805 conf.tileh <- max 64 w;
3806 conf.tilew <- max 64 h;
3807 flushtiles ();
3808 with exn ->
3809 state.text <- Printf.sprintf "bad tile size `%s': %s"
3810 v (Printexc.to_string exn));
3811 src#int "texture count"
3812 (fun () -> conf.texcount)
3813 (fun v ->
3814 if realloctexts v
3815 then conf.texcount <- v
3816 else showtext '!' " Failed to set texture count please retry later"
3818 src#int "slice height"
3819 (fun () -> conf.sliceheight)
3820 (fun v ->
3821 conf.sliceheight <- v;
3822 wcmd "sliceh" [`i conf.sliceheight];
3824 src#int "anti-aliasing level"
3825 (fun () -> conf.aalevel)
3826 (fun v ->
3827 conf.aalevel <- bound v 0 8;
3828 state.anchor <- getanchor ();
3829 opendoc state.path state.password;
3831 src#int "ui font size"
3832 (fun () -> fstate.fontsize)
3833 (fun v -> setfontsize (bound v 5 100));
3834 colorp "background color"
3835 (fun () -> conf.bgcolor)
3836 (fun v -> conf.bgcolor <- v);
3837 src#bool "crop hack"
3838 (fun () -> conf.crophack)
3839 (fun v -> conf.crophack <- v);
3840 src#string "trim fuzz"
3841 (fun () -> irect_to_string conf.trimfuzz)
3842 (fun v ->
3844 conf.trimfuzz <- irect_of_string v;
3845 if conf.trimmargins
3846 then settrim true conf.trimfuzz;
3847 with exn ->
3848 state.text <- Printf.sprintf "bad irect `%s': %s"
3849 v (Printexc.to_string exn)
3851 src#string "throttle"
3852 (fun () ->
3853 match conf.maxwait with
3854 | None -> "show place holder if page is not ready"
3855 | Some time ->
3856 if time = infinity
3857 then "wait for page to fully render"
3858 else
3859 "wait " ^ string_of_float time
3860 ^ " seconds before showing placeholder"
3862 (fun v ->
3864 let f = float_of_string v in
3865 if f <= 0.0
3866 then conf.maxwait <- None
3867 else conf.maxwait <- Some f
3868 with exn ->
3869 state.text <- Printf.sprintf "bad time `%s': %s"
3870 v (Printexc.to_string exn)
3872 src#string "ghyll scroll"
3873 (fun () ->
3874 match conf.ghyllscroll with
3875 | None -> ""
3876 | Some nab -> ghyllscroll_to_string nab
3878 (fun v ->
3880 let gs =
3881 if String.length v = 0
3882 then None
3883 else Some (ghyllscroll_of_string v)
3885 conf.ghyllscroll <- gs
3886 with exn ->
3887 state.text <- Printf.sprintf "bad ghyll `%s': %s"
3888 v (Printexc.to_string exn)
3890 src#colorspace "color space"
3891 (fun () -> colorspace_to_string conf.colorspace)
3892 (fun v ->
3893 conf.colorspace <- colorspace_of_int v;
3894 wcmd "cs" [`i v];
3895 load state.layout;
3899 sep ();
3900 src#caption "Document" 0;
3901 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3902 src#caption2 "Pages"
3903 (fun () -> string_of_int state.pagecount) 1;
3904 src#caption2 "Dimensions"
3905 (fun () -> string_of_int (List.length state.pdims)) 1;
3906 if conf.trimmargins
3907 then (
3908 sep ();
3909 src#caption "Trimmed margins" 0;
3910 src#caption2 "Dimensions"
3911 (fun () -> string_of_int (List.length state.pdims)) 1;
3914 src#reset prevmode prevuioh;
3916 fun () ->
3917 state.text <- "";
3918 let prevmode = state.mode
3919 and prevuioh = state.uioh in
3920 fillsrc prevmode prevuioh;
3921 let source = (src :> lvsource) in
3922 state.uioh <- coe (object (self)
3923 inherit listview ~source ~trusted:true as super
3924 val mutable m_prevmemused = 0
3925 method infochanged = function
3926 | Memused ->
3927 if m_prevmemused != state.memused
3928 then (
3929 m_prevmemused <- state.memused;
3930 G.postRedisplay "memusedchanged";
3932 | Pdim -> G.postRedisplay "pdimchanged"
3933 | Docinfo -> fillsrc prevmode prevuioh
3935 method special key =
3936 if Glut.getModifiers () land Glut.active_ctrl = 0
3937 then
3938 match key with
3939 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
3940 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
3941 | _ -> super#special key
3942 else super#special key
3943 end);
3944 G.postRedisplay "info";
3947 let enterhelpmode =
3948 let source =
3949 (object
3950 inherit lvsourcebase
3951 method getitemcount = Array.length state.help
3952 method getitem n =
3953 let s, n, _ = state.help.(n) in
3954 (s, n)
3956 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3957 let optuioh =
3958 if not cancel
3959 then (
3960 m_qsearch <- qsearch;
3961 match state.help.(active) with
3962 | _, _, Action f -> Some (f uioh)
3963 | _ -> Some (uioh)
3965 else None
3967 m_active <- active;
3968 m_first <- first;
3969 m_pan <- pan;
3970 optuioh
3972 method hasaction n =
3973 match state.help.(n) with
3974 | _, _, Action _ -> true
3975 | _ -> false
3977 initializer
3978 m_active <- -1
3979 end)
3980 in fun () ->
3981 state.uioh <- coe (new listview ~source ~trusted:true);
3982 G.postRedisplay "help";
3985 let entermsgsmode =
3986 let msgsource =
3987 let re = Str.regexp "[\r\n]" in
3988 (object
3989 inherit lvsourcebase
3990 val mutable m_items = [||]
3992 method getitemcount = 1 + Array.length m_items
3994 method getitem n =
3995 if n = 0
3996 then "[Clear]", 0
3997 else m_items.(n-1), 0
3999 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4000 ignore uioh;
4001 if not cancel
4002 then (
4003 if active = 0
4004 then Buffer.clear state.errmsgs;
4005 m_qsearch <- qsearch;
4007 m_active <- active;
4008 m_first <- first;
4009 m_pan <- pan;
4010 None
4012 method hasaction n =
4013 n = 0
4015 method reset =
4016 state.newerrmsgs <- false;
4017 let l = Str.split re (Buffer.contents state.errmsgs) in
4018 m_items <- Array.of_list l
4020 initializer
4021 m_active <- 0
4022 end)
4023 in fun () ->
4024 state.text <- "";
4025 msgsource#reset;
4026 let source = (msgsource :> lvsource) in
4027 state.uioh <- coe (object
4028 inherit listview ~source ~trusted:false as super
4029 method display =
4030 if state.newerrmsgs
4031 then msgsource#reset;
4032 super#display
4033 end);
4034 G.postRedisplay "msgs";
4037 let quickbookmark ?title () =
4038 match state.layout with
4039 | [] -> ()
4040 | l :: _ ->
4041 let title =
4042 match title with
4043 | None ->
4044 let sec = Unix.gettimeofday () in
4045 let tm = Unix.localtime sec in
4046 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4047 (l.pageno+1)
4048 tm.Unix.tm_mday
4049 tm.Unix.tm_mon
4050 (tm.Unix.tm_year + 1900)
4051 tm.Unix.tm_hour
4052 tm.Unix.tm_min
4053 | Some title -> title
4055 state.bookmarks <-
4056 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4057 :: state.bookmarks
4060 let doreshape w h =
4061 state.fullscreen <- None;
4062 Glut.reshapeWindow w h;
4065 let viewkeyboard key =
4066 let enttext te =
4067 let mode = state.mode in
4068 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4069 state.text <- "";
4070 enttext ();
4071 G.postRedisplay "view:enttext"
4073 let c = Char.chr key in
4074 match c with
4075 | '\027' | 'q' -> (* escape *)
4076 begin match state.mstate with
4077 | Mzoomrect _ ->
4078 state.mstate <- Mnone;
4079 Glut.setCursor Glut.CURSOR_INHERIT;
4080 G.postRedisplay "kill zoom rect";
4081 | _ ->
4082 raise Quit
4083 end;
4085 | '\008' -> (* backspace *)
4086 let y = getnav ~-1 in
4087 gotoy_and_clear_text y
4089 | 'o' ->
4090 enteroutlinemode ()
4092 | 'u' ->
4093 state.rects <- [];
4094 state.text <- "";
4095 G.postRedisplay "dehighlight";
4097 | '/' | '?' ->
4098 let ondone isforw s =
4099 cbput state.hists.pat s;
4100 state.searchpattern <- s;
4101 search s isforw
4103 let s = String.create 1 in
4104 s.[0] <- c;
4105 enttext (s, "", Some (onhist state.hists.pat),
4106 textentry, ondone (c ='/'))
4108 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4109 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4110 setzoom (conf.zoom +. incr)
4112 | '+' ->
4113 let ondone s =
4114 let n =
4115 try int_of_string s with exc ->
4116 state.text <- Printf.sprintf "bad integer `%s': %s"
4117 s (Printexc.to_string exc);
4118 max_int
4120 if n != max_int
4121 then (
4122 conf.pagebias <- n;
4123 state.text <- "page bias is now " ^ string_of_int n;
4126 enttext ("page bias: ", "", None, intentry, ondone)
4128 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4129 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4130 setzoom (max 0.01 (conf.zoom -. decr))
4132 | '-' ->
4133 let ondone msg = state.text <- msg in
4134 enttext (
4135 "option [acfhilpstvACPRSZTI]: ", "", None,
4136 optentry state.mode, ondone
4139 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4140 setzoom 1.0
4142 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4143 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4144 if zoom < 1.0
4145 then setzoom zoom
4147 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4148 togglebirdseye ()
4150 | '0' .. '9' ->
4151 let ondone s =
4152 let n =
4153 try int_of_string s with exc ->
4154 state.text <- Printf.sprintf "bad integer `%s': %s"
4155 s (Printexc.to_string exc);
4158 if n >= 0
4159 then (
4160 addnav ();
4161 cbput state.hists.pag (string_of_int n);
4162 gotopage1 (n + conf.pagebias - 1) 0;
4165 let pageentry text key =
4166 match Char.unsafe_chr key with
4167 | 'g' -> TEdone text
4168 | _ -> intentry text key
4170 let text = "x" in text.[0] <- c;
4171 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4173 | 'b' ->
4174 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4175 reshape conf.winw conf.winh;
4177 | 'l' ->
4178 conf.hlinks <- not conf.hlinks;
4179 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4180 G.postRedisplay "toggle highlightlinks";
4182 | 'a' ->
4183 begin match state.autoscroll with
4184 | Some step ->
4185 conf.autoscrollstep <- step;
4186 state.autoscroll <- None
4187 | None ->
4188 if conf.autoscrollstep = 0
4189 then state.autoscroll <- Some 1
4190 else state.autoscroll <- Some conf.autoscrollstep
4193 | 'P' ->
4194 conf.presentation <- not conf.presentation;
4195 if conf.presentation
4196 then (
4197 if not conf.scrollbarinpm
4198 then state.scrollw <- 0;
4200 else
4201 state.scrollw <- conf.scrollbw;
4203 showtext ' ' ("presentation mode " ^
4204 if conf.presentation then "on" else "off");
4205 state.anchor <- getanchor ();
4206 represent ()
4208 | 'f' ->
4209 begin match state.fullscreen with
4210 | None ->
4211 state.fullscreen <- Some (conf.winw, conf.winh);
4212 Glut.fullScreen ()
4213 | Some (w, h) ->
4214 state.fullscreen <- None;
4215 doreshape w h
4218 | 'g' ->
4219 gotoy_and_clear_text 0
4221 | 'G' ->
4222 gotopage1 (state.pagecount - 1) 0
4224 | 'n' ->
4225 search state.searchpattern true
4227 | 'p' | 'N' ->
4228 search state.searchpattern false
4230 | 't' ->
4231 begin match state.layout with
4232 | [] -> ()
4233 | l :: _ ->
4234 gotoy_and_clear_text (getpagey l.pageno)
4237 | ' ' ->
4238 begin match List.rev state.layout with
4239 | [] -> ()
4240 | l :: _ ->
4241 let pageno = min (l.pageno+1) (state.pagecount-1) in
4242 gotoy_and_clear_text (getpagey pageno)
4245 | '\127' -> (* del *)
4246 begin match state.layout with
4247 | [] -> ()
4248 | l :: _ ->
4249 let pageno = max 0 (l.pageno-1) in
4250 gotoy_and_clear_text (getpagey pageno)
4253 | '=' ->
4254 showtext ' ' (describe_location ());
4256 | 'w' ->
4257 begin match state.layout with
4258 | [] -> ()
4259 | l :: _ ->
4260 doreshape (l.pagew + state.scrollw) l.pageh;
4261 G.postRedisplay "w"
4264 | '\'' ->
4265 enterbookmarkmode ()
4267 | 'h' ->
4268 enterhelpmode ()
4270 | 'i' ->
4271 enterinfomode ()
4273 | 'e' when conf.redirectstderr ->
4274 entermsgsmode ()
4276 | 'm' ->
4277 let ondone s =
4278 match state.layout with
4279 | l :: _ ->
4280 state.bookmarks <-
4281 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4282 :: state.bookmarks
4283 | _ -> ()
4285 enttext ("bookmark: ", "", None, textentry, ondone)
4287 | '~' ->
4288 quickbookmark ();
4289 showtext ' ' "Quick bookmark added";
4291 | 'z' ->
4292 begin match state.layout with
4293 | l :: _ ->
4294 let rect = getpdimrect l.pagedimno in
4295 let w, h =
4296 if conf.crophack
4297 then
4298 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4299 truncate (1.2 *. (rect.(3) -. rect.(0))))
4300 else
4301 (truncate (rect.(1) -. rect.(0)),
4302 truncate (rect.(3) -. rect.(0)))
4304 let w = truncate ((float w)*.conf.zoom)
4305 and h = truncate ((float h)*.conf.zoom) in
4306 if w != 0 && h != 0
4307 then (
4308 state.anchor <- getanchor ();
4309 doreshape (w + state.scrollw) (h + conf.interpagespace)
4311 G.postRedisplay "z";
4313 | [] -> ()
4316 | '\000' -> (* ctrl-2 *)
4317 let maxw = getmaxw () in
4318 if maxw > 0.0
4319 then setzoom (maxw /. float conf.winw)
4321 | '<' | '>' ->
4322 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
4324 | '[' | ']' ->
4325 conf.colorscale <-
4326 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
4328 G.postRedisplay "brightness";
4330 | 'k' ->
4331 begin match state.mode with
4332 | Birdseye beye -> upbirdseye 1 beye
4333 | _ -> gotoy (clamp (-conf.scrollstep))
4336 | 'j' ->
4337 begin match state.mode with
4338 | Birdseye beye -> downbirdseye 1 beye
4339 | _ -> gotoy (clamp conf.scrollstep)
4342 | 'r' ->
4343 state.anchor <- getanchor ();
4344 opendoc state.path state.password
4346 | 'v' when not conf.debug ->
4347 List.iter debugl state.layout;
4349 | 'v' when conf.debug ->
4350 state.rects <- [];
4351 List.iter (fun l ->
4352 match getopaque l.pageno with
4353 | None -> ()
4354 | Some opaque ->
4355 let x0, y0, x1, y1 = pagebbox opaque in
4356 let a,b = float x0, float y0 in
4357 let c,d = float x1, float y0 in
4358 let e,f = float x1, float y1 in
4359 let h,j = float x0, float y1 in
4360 let rect = (a,b,c,d,e,f,h,j) in
4361 debugrect rect;
4362 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4363 ) state.layout;
4364 G.postRedisplay "v";
4366 | _ ->
4367 vlog "huh? %d %c" key (Char.chr key);
4370 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
4371 match key with
4372 | 27 -> (* escape *)
4373 leavebirdseye beye true
4375 | 12 -> (* ctrl-l *)
4376 let y, h = getpageyh pageno in
4377 let top = (conf.winh - h) / 2 in
4378 gotoy (max 0 (y - top))
4380 | 13 -> (* enter *)
4381 leavebirdseye beye false
4383 | _ ->
4384 viewkeyboard key
4387 let keyboard ~key ~x ~y =
4388 ignore x;
4389 ignore y;
4390 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
4391 then wcmd "interrupt" []
4392 else state.uioh <- state.uioh#key key
4395 let birdseyespecial key ((oconf, leftx, _, hooverpageno, anchor) as beye) =
4396 let incr =
4397 match conf.columns with
4398 | None -> 1
4399 | Some ((c, _, _), _) -> c
4401 match key with
4402 | Glut.KEY_UP -> upbirdseye incr beye
4403 | Glut.KEY_DOWN -> downbirdseye incr beye
4404 | Glut.KEY_LEFT -> upbirdseye 1 beye
4405 | Glut.KEY_RIGHT -> downbirdseye 1 beye
4407 | Glut.KEY_PAGE_UP ->
4408 begin match state.layout with
4409 | l :: _ ->
4410 if l.pagey != 0
4411 then (
4412 state.mode <- Birdseye (
4413 oconf, leftx, l.pageno, hooverpageno, anchor
4415 gotopage1 l.pageno 0;
4417 else (
4418 let layout = layout (state.y-conf.winh) conf.winh in
4419 match layout with
4420 | [] -> gotoy (clamp (-conf.winh))
4421 | l :: _ ->
4422 state.mode <- Birdseye (
4423 oconf, leftx, l.pageno, hooverpageno, anchor
4425 gotopage1 l.pageno 0
4428 | [] -> gotoy (clamp (-conf.winh))
4429 end;
4431 | Glut.KEY_PAGE_DOWN ->
4432 begin match List.rev state.layout with
4433 | l :: _ ->
4434 let layout = layout (state.y + conf.winh) conf.winh in
4435 begin match layout with
4436 | [] ->
4437 let incr = l.pageh - l.pagevh in
4438 if incr = 0
4439 then (
4440 state.mode <-
4441 Birdseye (
4442 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4444 G.postRedisplay "birdseye pagedown";
4446 else gotoy (clamp (incr + conf.interpagespace*2));
4448 | l :: _ ->
4449 state.mode <-
4450 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4451 gotopage1 l.pageno 0;
4454 | [] -> gotoy (clamp conf.winh)
4455 end;
4457 | Glut.KEY_HOME ->
4458 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4459 gotopage1 0 0
4461 | Glut.KEY_END ->
4462 let pageno = state.pagecount - 1 in
4463 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4464 if not (pagevisible state.layout pageno)
4465 then
4466 let h =
4467 match List.rev state.pdims with
4468 | [] -> conf.winh
4469 | (_, _, h, _) :: _ -> h
4471 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4472 else G.postRedisplay "birdseye end";
4473 | _ -> ()
4476 let setautoscrollspeed step goingdown =
4477 let incr = max 1 ((abs step) / 2) in
4478 let incr = if goingdown then incr else -incr in
4479 let astep = step + incr in
4480 state.autoscroll <- Some astep;
4483 let special ~key ~x ~y =
4484 ignore x;
4485 ignore y;
4486 state.uioh <- state.uioh#special key
4489 let drawpage l =
4490 let color =
4491 match state.mode with
4492 | Textentry _ -> scalecolor 0.4
4493 | View -> scalecolor 1.0
4494 | Birdseye (_, _, pageno, hooverpageno, _) ->
4495 if l.pageno = hooverpageno
4496 then scalecolor 0.9
4497 else (
4498 if l.pageno = pageno
4499 then scalecolor 1.0
4500 else scalecolor 0.8
4503 drawtiles l color;
4504 begin match getopaque l.pageno with
4505 | Some opaque ->
4506 if tileready l l.pagex l.pagey
4507 then
4508 let x = l.pagedispx - l.pagex
4509 and y = l.pagedispy - l.pagey in
4510 postprocess opaque conf.hlinks x y;
4512 | _ -> ()
4513 end;
4516 let scrollindicator () =
4517 let sbw, ph, sh = state.uioh#scrollph in
4518 let sbh, pw, sw = state.uioh#scrollpw in
4520 GlDraw.color (0.64, 0.64, 0.64);
4521 GlDraw.rect
4522 (float (conf.winw - sbw), 0.)
4523 (float conf.winw, float conf.winh)
4525 GlDraw.rect
4526 (0., float (conf.winh - sbh))
4527 (float (conf.winw - state.scrollw - 1), float conf.winh)
4529 GlDraw.color (0.0, 0.0, 0.0);
4531 GlDraw.rect
4532 (float (conf.winw - sbw), ph)
4533 (float conf.winw, ph +. sh)
4535 GlDraw.rect
4536 (pw, float (conf.winh - sbh))
4537 (pw +. sw, float conf.winh)
4541 let pagetranslatepoint l x y =
4542 let dy = y - l.pagedispy in
4543 let y = dy + l.pagey in
4544 let dx = x - l.pagedispx in
4545 let x = dx + l.pagex in
4546 (x, y);
4549 let showsel () =
4550 match state.mstate with
4551 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4554 | Msel ((x0, y0), (x1, y1)) ->
4555 let rec loop = function
4556 | l :: ls ->
4557 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4558 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4559 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4560 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4561 then
4562 match getopaque l.pageno with
4563 | Some opaque ->
4564 let dx, dy = pagetranslatepoint l 0 0 in
4565 let x0 = x0 + dx
4566 and y0 = y0 + dy
4567 and x1 = x1 + dx
4568 and y1 = y1 + dy in
4569 GlMat.mode `modelview;
4570 GlMat.push ();
4571 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4572 seltext opaque (x0, y0, x1, y1);
4573 GlMat.pop ();
4574 | _ -> ()
4575 else loop ls
4576 | [] -> ()
4578 loop state.layout
4581 let showrects () =
4582 Gl.enable `blend;
4583 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4584 GlDraw.polygon_mode `both `fill;
4585 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4586 List.iter
4587 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4588 List.iter (fun l ->
4589 if l.pageno = pageno
4590 then (
4591 let dx = float (l.pagedispx - l.pagex) in
4592 let dy = float (l.pagedispy - l.pagey) in
4593 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4594 GlDraw.begins `quads;
4596 GlDraw.vertex2 (x0+.dx, y0+.dy);
4597 GlDraw.vertex2 (x1+.dx, y1+.dy);
4598 GlDraw.vertex2 (x2+.dx, y2+.dy);
4599 GlDraw.vertex2 (x3+.dx, y3+.dy);
4601 GlDraw.ends ();
4603 ) state.layout
4604 ) state.rects
4606 Gl.disable `blend;
4609 let display () =
4610 GlClear.color (scalecolor2 conf.bgcolor);
4611 GlClear.clear [`color];
4612 List.iter drawpage state.layout;
4613 showrects ();
4614 showsel ();
4615 state.uioh#display;
4616 scrollindicator ();
4617 begin match state.mstate with
4618 | Mzoomrect ((x0, y0), (x1, y1)) ->
4619 Gl.enable `blend;
4620 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4621 GlDraw.polygon_mode `both `fill;
4622 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4623 GlDraw.rect (float x0, float y0)
4624 (float x1, float y1);
4625 Gl.disable `blend;
4626 | _ -> ()
4627 end;
4628 enttext ();
4629 Glut.swapBuffers ();
4632 let getunder x y =
4633 let rec f = function
4634 | l :: rest ->
4635 begin match getopaque l.pageno with
4636 | Some opaque ->
4637 let x0 = l.pagedispx in
4638 let x1 = x0 + l.pagevw in
4639 let y0 = l.pagedispy in
4640 let y1 = y0 + l.pagevh in
4641 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4642 then
4643 let px, py = pagetranslatepoint l x y in
4644 match whatsunder opaque px py with
4645 | Unone -> f rest
4646 | under -> under
4647 else f rest
4648 | _ ->
4649 f rest
4651 | [] -> Unone
4653 f state.layout
4656 let zoomrect x y x1 y1 =
4657 let x0 = min x x1
4658 and x1 = max x x1
4659 and y0 = min y y1 in
4660 gotoy (state.y + y0);
4661 state.anchor <- getanchor ();
4662 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4663 let margin =
4664 if state.w < conf.winw - state.scrollw
4665 then (conf.winw - state.scrollw - state.w) / 2
4666 else 0
4668 state.x <- (state.x + margin) - x0;
4669 setzoom zoom;
4670 Glut.setCursor Glut.CURSOR_INHERIT;
4671 state.mstate <- Mnone;
4674 let scrollx x =
4675 let winw = conf.winw - state.scrollw - 1 in
4676 let s = float x /. float winw in
4677 let destx = truncate (float (state.w + winw) *. s) in
4678 state.x <- winw - destx;
4679 gotoy_and_clear_text state.y;
4680 state.mstate <- Mscrollx;
4683 let scrolly y =
4684 let s = float y /. float conf.winh in
4685 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4686 gotoy_and_clear_text desty;
4687 state.mstate <- Mscrolly;
4690 let viewmouse button bstate x y =
4691 match button with
4692 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4693 if Glut.getModifiers () land Glut.active_ctrl != 0
4694 then (
4695 match state.mstate with
4696 | Mzoom (oldn, i) ->
4697 if oldn = n
4698 then (
4699 if i = 2
4700 then
4701 let incr =
4702 match n with
4703 | 4 ->
4704 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4705 | _ ->
4706 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4708 let zoom = conf.zoom -. incr in
4709 setzoom zoom;
4710 state.mstate <- Mzoom (n, 0);
4711 else
4712 state.mstate <- Mzoom (n, i+1);
4714 else state.mstate <- Mzoom (n, 0)
4716 | _ -> state.mstate <- Mzoom (n, 0)
4718 else (
4719 match state.autoscroll with
4720 | Some step -> setautoscrollspeed step (n=4)
4721 | None ->
4722 let incr =
4723 if n = 3
4724 then -conf.scrollstep
4725 else conf.scrollstep
4727 let incr = incr * 2 in
4728 let y = clamp incr in
4729 gotoy_and_clear_text y
4732 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4733 if bstate = Glut.DOWN
4734 then (
4735 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4736 state.mstate <- Mpan (x, y)
4738 else
4739 state.mstate <- Mnone
4741 | Glut.RIGHT_BUTTON ->
4742 if bstate = Glut.DOWN
4743 then (
4744 Glut.setCursor Glut.CURSOR_CYCLE;
4745 let p = (x, y) in
4746 state.mstate <- Mzoomrect (p, p)
4748 else (
4749 match state.mstate with
4750 | Mzoomrect ((x0, y0), _) ->
4751 if abs (x-x0) > 10 && abs (y - y0) > 10
4752 then zoomrect x0 y0 x y
4753 else (
4754 state.mstate <- Mnone;
4755 Glut.setCursor Glut.CURSOR_INHERIT;
4756 G.postRedisplay "kill accidental zoom rect";
4758 | _ ->
4759 Glut.setCursor Glut.CURSOR_INHERIT;
4760 state.mstate <- Mnone
4763 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4764 if bstate = Glut.DOWN
4765 then
4766 let _, position, sh = state.uioh#scrollph in
4767 if y > truncate position && y < truncate (position +. sh)
4768 then state.mstate <- Mscrolly
4769 else scrolly y
4770 else
4771 state.mstate <- Mnone
4773 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4774 if bstate = Glut.DOWN
4775 then
4776 let _, position, sw = state.uioh#scrollpw in
4777 if x > truncate position && x < truncate (position +. sw)
4778 then state.mstate <- Mscrollx
4779 else scrollx x
4780 else
4781 state.mstate <- Mnone
4783 | Glut.LEFT_BUTTON ->
4784 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4785 begin match dest with
4786 | Ulinkgoto (pageno, top) ->
4787 if pageno >= 0
4788 then (
4789 addnav ();
4790 gotopage1 pageno top;
4793 | Ulinkuri s ->
4794 gotouri s
4796 | Unone when bstate = Glut.DOWN ->
4797 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4798 state.mstate <- Mpan (x, y);
4800 | Unone | Utext _ ->
4801 if bstate = Glut.DOWN
4802 then (
4803 if conf.angle mod 360 = 0
4804 then (
4805 state.mstate <- Msel ((x, y), (x, y));
4806 G.postRedisplay "mouse select";
4809 else (
4810 match state.mstate with
4811 | Mnone -> ()
4813 | Mzoom _ | Mscrollx | Mscrolly ->
4814 state.mstate <- Mnone
4816 | Mzoomrect ((x0, y0), _) ->
4817 zoomrect x0 y0 x y
4819 | Mpan _ ->
4820 Glut.setCursor Glut.CURSOR_INHERIT;
4821 state.mstate <- Mnone
4823 | Msel ((_, y0), (_, y1)) ->
4824 let f l =
4825 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4826 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4827 then
4828 match getopaque l.pageno with
4829 | Some opaque ->
4830 copysel opaque
4831 | _ -> ()
4833 List.iter f state.layout;
4834 copysel ""; (* ugly *)
4835 Glut.setCursor Glut.CURSOR_INHERIT;
4836 state.mstate <- Mnone;
4840 | _ -> ()
4843 let birdseyemouse button bstate x y
4844 (conf, leftx, _, hooverpageno, anchor) =
4845 match button with
4846 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4847 let rec loop = function
4848 | [] -> ()
4849 | l :: rest ->
4850 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4851 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4852 then (
4853 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4855 else loop rest
4857 loop state.layout
4858 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4859 | _ -> ()
4862 let mouse bstate button x y =
4863 state.uioh <- state.uioh#button button bstate x y;
4866 let mouse ~button ~state ~x ~y = mouse state button x y;;
4868 let motion ~x ~y =
4869 state.uioh <- state.uioh#motion x y
4872 let pmotion ~x ~y =
4873 state.uioh <- state.uioh#pmotion x y;
4876 let uioh = object
4877 method display = ()
4879 method key key =
4880 begin match state.mode with
4881 | Textentry textentry -> textentrykeyboard key textentry
4882 | Birdseye birdseye -> birdseyekeyboard key birdseye
4883 | View -> viewkeyboard key
4884 end;
4885 state.uioh
4887 method special key =
4888 begin match state.mode with
4889 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4890 togglebirdseye ()
4892 | Birdseye vals ->
4893 birdseyespecial key vals
4895 | View when key = Glut.KEY_F1 ->
4896 enterhelpmode ()
4898 | View ->
4899 begin match state.autoscroll with
4900 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4901 setautoscrollspeed step (key = Glut.KEY_DOWN)
4903 | _ ->
4904 let y =
4905 match key with
4906 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4907 | Glut.KEY_UP ->
4908 if Glut.getModifiers () land Glut.active_ctrl != 0
4909 then
4910 if Glut.getModifiers () land Glut.active_shift != 0
4911 then (setzoom state.prevzoom; state.y)
4912 else clamp (-conf.winh/2)
4913 else clamp (-conf.scrollstep)
4914 | Glut.KEY_DOWN ->
4915 if Glut.getModifiers () land Glut.active_ctrl != 0
4916 then
4917 if Glut.getModifiers () land Glut.active_shift != 0
4918 then (setzoom state.prevzoom; state.y)
4919 else clamp (conf.winh/2)
4920 else clamp (conf.scrollstep)
4921 | Glut.KEY_PAGE_UP ->
4922 if Glut.getModifiers () land Glut.active_ctrl != 0
4923 then
4924 match state.layout with
4925 | [] -> state.y
4926 | l :: _ -> state.y - l.pagey
4927 else
4928 clamp (-conf.winh)
4929 | Glut.KEY_PAGE_DOWN ->
4930 if Glut.getModifiers () land Glut.active_ctrl != 0
4931 then
4932 match List.rev state.layout with
4933 | [] -> state.y
4934 | l :: _ -> getpagey l.pageno
4935 else
4936 clamp conf.winh
4937 | Glut.KEY_HOME ->
4938 addnav ();
4940 | Glut.KEY_END ->
4941 addnav ();
4942 state.maxy - (if conf.maxhfit then conf.winh else 0)
4944 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4945 Glut.getModifiers () land Glut.active_alt != 0 ->
4946 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4948 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4949 let dx =
4950 if Glut.getModifiers () land Glut.active_ctrl != 0
4951 then (conf.winw / 2)
4952 else 10
4954 state.x <- state.x - dx;
4955 state.y
4956 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4957 let dx =
4958 if Glut.getModifiers () land Glut.active_ctrl != 0
4959 then (conf.winw / 2)
4960 else 10
4962 state.x <- state.x + dx;
4963 state.y
4965 | _ -> state.y
4967 if abs (state.y - y) > conf.scrollstep*2
4968 then gotoghyll y
4969 else gotoy_and_clear_text y
4972 | Textentry te -> textentryspecial key te
4973 end;
4974 state.uioh
4976 method button button bstate x y =
4977 begin match state.mode with
4978 | View -> viewmouse button bstate x y
4979 | Birdseye beye -> birdseyemouse button bstate x y beye
4980 | Textentry _ -> ()
4981 end;
4982 state.uioh
4984 method motion x y =
4985 begin match state.mode with
4986 | Textentry _ -> ()
4987 | View | Birdseye _ ->
4988 match state.mstate with
4989 | Mzoom _ | Mnone -> ()
4991 | Mpan (x0, y0) ->
4992 let dx = x - x0
4993 and dy = y0 - y in
4994 state.mstate <- Mpan (x, y);
4995 if conf.zoom > 1.0 then state.x <- state.x + dx;
4996 let y = clamp dy in
4997 gotoy_and_clear_text y
4999 | Msel (a, _) ->
5000 state.mstate <- Msel (a, (x, y));
5001 G.postRedisplay "motion select";
5003 | Mscrolly ->
5004 let y = min conf.winh (max 0 y) in
5005 scrolly y
5007 | Mscrollx ->
5008 let x = min conf.winw (max 0 x) in
5009 scrollx x
5011 | Mzoomrect (p0, _) ->
5012 state.mstate <- Mzoomrect (p0, (x, y));
5013 G.postRedisplay "motion zoomrect";
5014 end;
5015 state.uioh
5017 method pmotion x y =
5018 begin match state.mode with
5019 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5020 let rec loop = function
5021 | [] ->
5022 if hooverpageno != -1
5023 then (
5024 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5025 G.postRedisplay "pmotion birdseye no hoover";
5027 | l :: rest ->
5028 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5029 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5030 then (
5031 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5032 G.postRedisplay "pmotion birdseye hoover";
5034 else loop rest
5036 loop state.layout
5038 | Textentry _ -> ()
5040 | View ->
5041 match state.mstate with
5042 | Mnone ->
5043 begin match getunder x y with
5044 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
5045 | Ulinkuri uri ->
5046 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
5047 Glut.setCursor Glut.CURSOR_INFO
5048 | Ulinkgoto (page, _) ->
5049 if conf.underinfo
5050 then showtext 'p' ("age: " ^ string_of_int (page+1));
5051 Glut.setCursor Glut.CURSOR_INFO
5052 | Utext s ->
5053 if conf.underinfo then showtext 'f' ("ont: " ^ s);
5054 Glut.setCursor Glut.CURSOR_TEXT
5057 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5059 end;
5060 state.uioh
5062 method infochanged _ = ()
5064 method scrollph =
5065 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5066 let p, h = scrollph state.y maxy in
5067 state.scrollw, p, h
5069 method scrollpw =
5070 let winw = conf.winw - state.scrollw - 1 in
5071 let fwinw = float winw in
5072 let sw =
5073 let sw = fwinw /. float state.w in
5074 let sw = fwinw *. sw in
5075 max sw (float conf.scrollh)
5077 let position, sw =
5078 let f = state.w+winw in
5079 let r = float (winw-state.x) /. float f in
5080 let p = fwinw *. r in
5081 p-.sw/.2., sw
5083 let sw =
5084 if position +. sw > fwinw
5085 then fwinw -. position
5086 else sw
5088 state.hscrollh, position, sw
5089 end;;
5091 module Config =
5092 struct
5093 open Parser
5095 let fontpath = ref "";;
5096 let wmclasshack = ref false;;
5098 let unent s =
5099 let l = String.length s in
5100 let b = Buffer.create l in
5101 unent b s 0 l;
5102 Buffer.contents b;
5105 let home =
5107 match platform with
5108 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
5109 | _ -> Sys.getenv "HOME"
5110 with exn ->
5111 prerr_endline
5112 ("Can not determine home directory location: " ^
5113 Printexc.to_string exn);
5117 let config_of c attrs =
5118 let apply c k v =
5120 match k with
5121 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5122 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5123 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5124 | "preload" -> { c with preload = bool_of_string v }
5125 | "page-bias" -> { c with pagebias = int_of_string v }
5126 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5127 | "auto-scroll-step" ->
5128 { c with autoscrollstep = max 0 (int_of_string v) }
5129 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5130 | "crop-hack" -> { c with crophack = bool_of_string v }
5131 | "throttle" ->
5132 let mw =
5133 match String.lowercase v with
5134 | "true" -> Some infinity
5135 | "false" -> None
5136 | f -> Some (float_of_string f)
5138 { c with maxwait = mw}
5139 | "highlight-links" -> { c with hlinks = bool_of_string v }
5140 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5141 | "vertical-margin" ->
5142 { c with interpagespace = max 0 (int_of_string v) }
5143 | "zoom" ->
5144 let zoom = float_of_string v /. 100. in
5145 let zoom = max zoom 0.0 in
5146 { c with zoom = zoom }
5147 | "presentation" -> { c with presentation = bool_of_string v }
5148 | "rotation-angle" -> { c with angle = int_of_string v }
5149 | "width" -> { c with winw = max 20 (int_of_string v) }
5150 | "height" -> { c with winh = max 20 (int_of_string v) }
5151 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5152 | "proportional-display" -> { c with proportional = bool_of_string v }
5153 | "pixmap-cache-size" ->
5154 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5155 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5156 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5157 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5158 | "persistent-location" -> { c with jumpback = bool_of_string v }
5159 | "background-color" -> { c with bgcolor = color_of_string v }
5160 | "scrollbar-in-presentation" ->
5161 { c with scrollbarinpm = bool_of_string v }
5162 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5163 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5164 | "mupdf-store-size" ->
5165 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5166 | "checkers" -> { c with checkers = bool_of_string v }
5167 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5168 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5169 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5170 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
5171 | "uri-launcher" -> { c with urilauncher = unent v }
5172 | "color-space" -> { c with colorspace = colorspace_of_string v }
5173 | "invert-colors" -> { c with invert = bool_of_string v }
5174 | "brightness" -> { c with colorscale = float_of_string v }
5175 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5176 | "ghyllscroll" ->
5177 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5178 | "columns" ->
5179 let nab = columns_of_string v in
5180 { c with columns = Some (nab, [||]) }
5181 | "birds-eye-columns" ->
5182 { c with beyecolumns = Some (max (int_of_string v) 2) }
5183 | _ -> c
5184 with exn ->
5185 prerr_endline ("Error processing attribute (`" ^
5186 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5189 let rec fold c = function
5190 | [] -> c
5191 | (k, v) :: rest ->
5192 let c = apply c k v in
5193 fold c rest
5195 fold c attrs;
5198 let fromstring f pos n v d =
5199 try f v
5200 with exn ->
5201 dolog "Error processing attribute (%S=%S) at %d\n%s"
5202 n v pos (Printexc.to_string exn)
5207 let bookmark_of attrs =
5208 let rec fold title page rely = function
5209 | ("title", v) :: rest -> fold v page rely rest
5210 | ("page", v) :: rest -> fold title v rely rest
5211 | ("rely", v) :: rest -> fold title page v rest
5212 | _ :: rest -> fold title page rely rest
5213 | [] -> title, page, rely
5215 fold "invalid" "0" "0" attrs
5218 let doc_of attrs =
5219 let rec fold path page rely pan = function
5220 | ("path", v) :: rest -> fold v page rely pan rest
5221 | ("page", v) :: rest -> fold path v rely pan rest
5222 | ("rely", v) :: rest -> fold path page v pan rest
5223 | ("pan", v) :: rest -> fold path page rely v rest
5224 | _ :: rest -> fold path page rely pan rest
5225 | [] -> path, page, rely, pan
5227 fold "" "0" "0" "0" attrs
5230 let setconf dst src =
5231 dst.scrollbw <- src.scrollbw;
5232 dst.scrollh <- src.scrollh;
5233 dst.icase <- src.icase;
5234 dst.preload <- src.preload;
5235 dst.pagebias <- src.pagebias;
5236 dst.verbose <- src.verbose;
5237 dst.scrollstep <- src.scrollstep;
5238 dst.maxhfit <- src.maxhfit;
5239 dst.crophack <- src.crophack;
5240 dst.autoscrollstep <- src.autoscrollstep;
5241 dst.maxwait <- src.maxwait;
5242 dst.hlinks <- src.hlinks;
5243 dst.underinfo <- src.underinfo;
5244 dst.interpagespace <- src.interpagespace;
5245 dst.zoom <- src.zoom;
5246 dst.presentation <- src.presentation;
5247 dst.angle <- src.angle;
5248 dst.winw <- src.winw;
5249 dst.winh <- src.winh;
5250 dst.savebmarks <- src.savebmarks;
5251 dst.memlimit <- src.memlimit;
5252 dst.proportional <- src.proportional;
5253 dst.texcount <- src.texcount;
5254 dst.sliceheight <- src.sliceheight;
5255 dst.thumbw <- src.thumbw;
5256 dst.jumpback <- src.jumpback;
5257 dst.bgcolor <- src.bgcolor;
5258 dst.scrollbarinpm <- src.scrollbarinpm;
5259 dst.tilew <- src.tilew;
5260 dst.tileh <- src.tileh;
5261 dst.mustoresize <- src.mustoresize;
5262 dst.checkers <- src.checkers;
5263 dst.aalevel <- src.aalevel;
5264 dst.trimmargins <- src.trimmargins;
5265 dst.trimfuzz <- src.trimfuzz;
5266 dst.urilauncher <- src.urilauncher;
5267 dst.colorspace <- src.colorspace;
5268 dst.invert <- src.invert;
5269 dst.colorscale <- src.colorscale;
5270 dst.redirectstderr <- src.redirectstderr;
5271 dst.ghyllscroll <- src.ghyllscroll;
5272 dst.columns <- src.columns;
5273 dst.beyecolumns <- src.beyecolumns;
5276 let get s =
5277 let h = Hashtbl.create 10 in
5278 let dc = { defconf with angle = defconf.angle } in
5279 let rec toplevel v t spos _ =
5280 match t with
5281 | Vdata | Vcdata | Vend -> v
5282 | Vopen ("llppconfig", _, closed) ->
5283 if closed
5284 then v
5285 else { v with f = llppconfig }
5286 | Vopen _ ->
5287 error "unexpected subelement at top level" s spos
5288 | Vclose _ -> error "unexpected close at top level" s spos
5290 and llppconfig v t spos _ =
5291 match t with
5292 | Vdata | Vcdata -> v
5293 | Vend -> error "unexpected end of input in llppconfig" s spos
5294 | Vopen ("defaults", attrs, closed) ->
5295 let c = config_of dc attrs in
5296 setconf dc c;
5297 if closed
5298 then v
5299 else { v with f = skip "defaults" (fun () -> v) }
5301 | Vopen ("ui-font", attrs, closed) ->
5302 let rec getsize size = function
5303 | [] -> size
5304 | ("size", v) :: rest ->
5305 let size =
5306 fromstring int_of_string spos "size" v fstate.fontsize in
5307 getsize size rest
5308 | l -> getsize size l
5310 fstate.fontsize <- getsize fstate.fontsize attrs;
5311 if closed
5312 then v
5313 else { v with f = uifont (Buffer.create 10) }
5315 | Vopen ("doc", attrs, closed) ->
5316 let pathent, spage, srely, span = doc_of attrs in
5317 let path = unent pathent
5318 and pageno = fromstring int_of_string spos "page" spage 0
5319 and rely = fromstring float_of_string spos "rely" srely 0.0
5320 and pan = fromstring int_of_string spos "pan" span 0 in
5321 let c = config_of dc attrs in
5322 let anchor = (pageno, rely) in
5323 if closed
5324 then (Hashtbl.add h path (c, [], pan, anchor); v)
5325 else { v with f = doc path pan anchor c [] }
5327 | Vopen _ ->
5328 error "unexpected subelement in llppconfig" s spos
5330 | Vclose "llppconfig" -> { v with f = toplevel }
5331 | Vclose _ -> error "unexpected close in llppconfig" s spos
5333 and uifont b v t spos epos =
5334 match t with
5335 | Vdata | Vcdata ->
5336 Buffer.add_substring b s spos (epos - spos);
5338 | Vopen (_, _, _) ->
5339 error "unexpected subelement in ui-font" s spos
5340 | Vclose "ui-font" ->
5341 if String.length !fontpath = 0
5342 then fontpath := Buffer.contents b;
5343 { v with f = llppconfig }
5344 | Vclose _ -> error "unexpected close in ui-font" s spos
5345 | Vend -> error "unexpected end of input in ui-font" s spos
5347 and doc path pan anchor c bookmarks v t spos _ =
5348 match t with
5349 | Vdata | Vcdata -> v
5350 | Vend -> error "unexpected end of input in doc" s spos
5351 | Vopen ("bookmarks", _, closed) ->
5352 if closed
5353 then v
5354 else { v with f = pbookmarks path pan anchor c bookmarks }
5356 | Vopen (_, _, _) ->
5357 error "unexpected subelement in doc" s spos
5359 | Vclose "doc" ->
5360 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5361 { v with f = llppconfig }
5363 | Vclose _ -> error "unexpected close in doc" s spos
5365 and pbookmarks path pan anchor c bookmarks v t spos _ =
5366 match t with
5367 | Vdata | Vcdata -> v
5368 | Vend -> error "unexpected end of input in bookmarks" s spos
5369 | Vopen ("item", attrs, closed) ->
5370 let titleent, spage, srely = bookmark_of attrs in
5371 let page = fromstring int_of_string spos "page" spage 0
5372 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5373 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5374 if closed
5375 then { v with f = pbookmarks path pan anchor c bookmarks }
5376 else
5377 let f () = v in
5378 { v with f = skip "item" f }
5380 | Vopen _ ->
5381 error "unexpected subelement in bookmarks" s spos
5383 | Vclose "bookmarks" ->
5384 { v with f = doc path pan anchor c bookmarks }
5386 | Vclose _ -> error "unexpected close in bookmarks" s spos
5388 and skip tag f v t spos _ =
5389 match t with
5390 | Vdata | Vcdata -> v
5391 | Vend ->
5392 error ("unexpected end of input in skipped " ^ tag) s spos
5393 | Vopen (tag', _, closed) ->
5394 if closed
5395 then v
5396 else
5397 let f' () = { v with f = skip tag f } in
5398 { v with f = skip tag' f' }
5399 | Vclose ctag ->
5400 if tag = ctag
5401 then f ()
5402 else error ("unexpected close in skipped " ^ tag) s spos
5405 parse { f = toplevel; accu = () } s;
5406 h, dc;
5409 let do_load f ic =
5411 let len = in_channel_length ic in
5412 let s = String.create len in
5413 really_input ic s 0 len;
5414 f s;
5415 with
5416 | Parse_error (msg, s, pos) ->
5417 let subs = subs s pos in
5418 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5419 failwith ("parse error: " ^ s)
5421 | exn ->
5422 failwith ("config load error: " ^ Printexc.to_string exn)
5425 let defconfpath =
5426 let dir =
5428 let dir = Filename.concat home ".config" in
5429 if Sys.is_directory dir then dir else home
5430 with _ -> home
5432 Filename.concat dir "llpp.conf"
5435 let confpath = ref defconfpath;;
5437 let load1 f =
5438 if Sys.file_exists !confpath
5439 then
5440 match
5441 (try Some (open_in_bin !confpath)
5442 with exn ->
5443 prerr_endline
5444 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5445 Printexc.to_string exn);
5446 None
5448 with
5449 | Some ic ->
5450 begin try
5451 f (do_load get ic)
5452 with exn ->
5453 prerr_endline
5454 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5455 Printexc.to_string exn);
5456 end;
5457 close_in ic;
5459 | None -> ()
5460 else
5461 f (Hashtbl.create 0, defconf)
5464 let load () =
5465 let f (h, dc) =
5466 let pc, pb, px, pa =
5468 Hashtbl.find h (Filename.basename state.path)
5469 with Not_found -> dc, [], 0, (0, 0.0)
5471 setconf defconf dc;
5472 setconf conf pc;
5473 state.bookmarks <- pb;
5474 state.x <- px;
5475 state.scrollw <- conf.scrollbw;
5476 if conf.jumpback
5477 then state.anchor <- pa;
5478 cbput state.hists.nav pa;
5480 load1 f
5483 let add_attrs bb always dc c =
5484 let ob s a b =
5485 if always || a != b
5486 then Printf.bprintf bb "\n %s='%b'" s a
5487 and oi s a b =
5488 if always || a != b
5489 then Printf.bprintf bb "\n %s='%d'" s a
5490 and oI s a b =
5491 if always || a != b
5492 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5493 and oz s a b =
5494 if always || a <> b
5495 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5496 and oF s a b =
5497 if always || a <> b
5498 then Printf.bprintf bb "\n %s='%f'" s a
5499 and oc s a b =
5500 if always || a <> b
5501 then
5502 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5503 and oC s a b =
5504 if always || a <> b
5505 then
5506 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5507 and oR s a b =
5508 if always || a <> b
5509 then
5510 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5511 and os s a b =
5512 if always || a <> b
5513 then
5514 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5515 and og s a b =
5516 if always || a <> b
5517 then
5518 match a with
5519 | None -> ()
5520 | Some (_N, _A, _B) ->
5521 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5522 and oW s a b =
5523 if always || a <> b
5524 then
5525 let v =
5526 match a with
5527 | None -> "false"
5528 | Some f ->
5529 if f = infinity
5530 then "true"
5531 else string_of_float f
5533 Printf.bprintf bb "\n %s='%s'" s v
5534 and oco s a b =
5535 if always || a <> b
5536 then
5537 match a with
5538 | Some ((n, a, b), _) when n > 1 ->
5539 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5540 | _ -> ()
5541 and obeco s a b =
5542 if always || a <> b
5543 then
5544 match a with
5545 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5546 | _ -> ()
5548 let w, h =
5549 if always
5550 then dc.winw, dc.winh
5551 else
5552 match state.fullscreen with
5553 | Some wh -> wh
5554 | None -> c.winw, c.winh
5556 let zoom, presentation, interpagespace, maxwait =
5557 if always
5558 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5559 else
5560 match state.mode with
5561 | Birdseye (bc, _, _, _, _) ->
5562 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5563 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5565 oi "width" w dc.winw;
5566 oi "height" h dc.winh;
5567 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5568 oi "scroll-handle-height" c.scrollh dc.scrollh;
5569 ob "case-insensitive-search" c.icase dc.icase;
5570 ob "preload" c.preload dc.preload;
5571 oi "page-bias" c.pagebias dc.pagebias;
5572 oi "scroll-step" c.scrollstep dc.scrollstep;
5573 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5574 ob "max-height-fit" c.maxhfit dc.maxhfit;
5575 ob "crop-hack" c.crophack dc.crophack;
5576 oW "throttle" maxwait dc.maxwait;
5577 ob "highlight-links" c.hlinks dc.hlinks;
5578 ob "under-cursor-info" c.underinfo dc.underinfo;
5579 oi "vertical-margin" interpagespace dc.interpagespace;
5580 oz "zoom" zoom dc.zoom;
5581 ob "presentation" presentation dc.presentation;
5582 oi "rotation-angle" c.angle dc.angle;
5583 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5584 ob "proportional-display" c.proportional dc.proportional;
5585 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5586 oi "tex-count" c.texcount dc.texcount;
5587 oi "slice-height" c.sliceheight dc.sliceheight;
5588 oi "thumbnail-width" c.thumbw dc.thumbw;
5589 ob "persistent-location" c.jumpback dc.jumpback;
5590 oc "background-color" c.bgcolor dc.bgcolor;
5591 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5592 oi "tile-width" c.tilew dc.tilew;
5593 oi "tile-height" c.tileh dc.tileh;
5594 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5595 ob "checkers" c.checkers dc.checkers;
5596 oi "aalevel" c.aalevel dc.aalevel;
5597 ob "trim-margins" c.trimmargins dc.trimmargins;
5598 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5599 os "uri-launcher" c.urilauncher dc.urilauncher;
5600 oC "color-space" c.colorspace dc.colorspace;
5601 ob "invert-colors" c.invert dc.invert;
5602 oF "brightness" c.colorscale dc.colorscale;
5603 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5604 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
5605 oco "columns" c.columns dc.columns;
5606 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
5607 if always
5608 then ob "wmclass-hack" !wmclasshack false;
5611 let save () =
5612 let uifontsize = fstate.fontsize in
5613 let bb = Buffer.create 32768 in
5614 let f (h, dc) =
5615 let dc = if conf.bedefault then conf else dc in
5616 Buffer.add_string bb "<llppconfig>\n";
5618 if String.length !fontpath > 0
5619 then
5620 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5621 uifontsize
5622 !fontpath
5623 else (
5624 if uifontsize <> 14
5625 then
5626 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5629 Buffer.add_string bb "<defaults ";
5630 add_attrs bb true dc dc;
5631 Buffer.add_string bb "/>\n";
5633 let adddoc path pan anchor c bookmarks =
5634 if bookmarks == [] && c = dc && anchor = emptyanchor
5635 then ()
5636 else (
5637 Printf.bprintf bb "<doc path='%s'"
5638 (enent path 0 (String.length path));
5640 if anchor <> emptyanchor
5641 then (
5642 let n, y = anchor in
5643 Printf.bprintf bb " page='%d'" n;
5644 if y > 1e-6
5645 then
5646 Printf.bprintf bb " rely='%f'" y
5650 if pan != 0
5651 then Printf.bprintf bb " pan='%d'" pan;
5653 add_attrs bb false dc c;
5655 begin match bookmarks with
5656 | [] -> Buffer.add_string bb "/>\n"
5657 | _ ->
5658 Buffer.add_string bb ">\n<bookmarks>\n";
5659 List.iter (fun (title, _level, (page, rely)) ->
5660 Printf.bprintf bb
5661 "<item title='%s' page='%d'"
5662 (enent title 0 (String.length title))
5663 page
5665 if rely > 1e-6
5666 then
5667 Printf.bprintf bb " rely='%f'" rely
5669 Buffer.add_string bb "/>\n";
5670 ) bookmarks;
5671 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5672 end;
5676 let pan =
5677 match state.mode with
5678 | Birdseye (_, pan, _, _, _) -> pan
5679 | _ -> state.x
5681 let basename = Filename.basename state.path in
5682 adddoc basename pan (getanchor ())
5683 { conf with
5684 autoscrollstep =
5685 match state.autoscroll with
5686 | Some step -> step
5687 | None -> conf.autoscrollstep }
5688 (if conf.savebmarks then state.bookmarks else []);
5690 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5691 if basename <> path
5692 then adddoc path x y c bookmarks
5693 ) h;
5694 Buffer.add_string bb "</llppconfig>";
5696 load1 f;
5697 if Buffer.length bb > 0
5698 then
5700 let tmp = !confpath ^ ".tmp" in
5701 let oc = open_out_bin tmp in
5702 Buffer.output_buffer oc bb;
5703 close_out oc;
5704 Unix.rename tmp !confpath;
5705 with exn ->
5706 prerr_endline
5707 ("error while saving configuration: " ^ Printexc.to_string exn)
5709 end;;
5711 let () =
5712 Arg.parse
5713 (Arg.align
5714 [("-p", Arg.String (fun s -> state.password <- s) ,
5715 "<password> Set password");
5717 ("-f", Arg.String (fun s -> Config.fontpath := s),
5718 "<path> Set path to the user interface font");
5720 ("-c", Arg.String (fun s -> Config.confpath := s),
5721 "<path> Set path to the configuration file");
5723 ("-v", Arg.Unit (fun () ->
5724 Printf.printf
5725 "%s\nconfiguration path: %s\n"
5726 (version ())
5727 Config.defconfpath
5729 exit 0), " Print version and exit");
5732 (fun s -> state.path <- s)
5733 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5735 if String.length state.path = 0
5736 then (prerr_endline "file name missing"; exit 1);
5738 Config.load ();
5740 let _ = Glut.init Sys.argv in
5741 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5742 let () = Glut.initWindowSize conf.winw conf.winh in
5743 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5745 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5746 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5747 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5749 let csock, ssock =
5750 if not is_windows
5751 then
5752 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5753 else
5754 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5755 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5756 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5757 Unix.bind sock addr;
5758 Unix.listen sock 1;
5759 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5760 Unix.connect csock addr;
5761 let ssock, _ = Unix.accept sock in
5762 Unix.close sock;
5763 let opts sock =
5764 Unix.setsockopt sock Unix.TCP_NODELAY true;
5765 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5767 opts ssock;
5768 opts csock;
5769 ssock, csock
5772 let () = Glut.displayFunc display in
5773 let () = Glut.reshapeFunc reshape in
5774 let () = Glut.keyboardFunc keyboard in
5775 let () = Glut.specialFunc special in
5776 let () = Glut.idleFunc (Some idle) in
5777 let () = Glut.mouseFunc mouse in
5778 let () = Glut.motionFunc motion in
5779 let () = Glut.passiveMotionFunc pmotion in
5781 setcheckers conf.checkers;
5782 init ssock (
5783 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5784 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5785 !Config.wmclasshack, !Config.fontpath
5787 state.csock <- csock;
5788 state.ssock <- ssock;
5789 state.text <- "Opening " ^ state.path;
5790 setaalevel conf.aalevel;
5791 writeopen state.path state.password;
5792 state.uioh <- uioh;
5793 setfontsize fstate.fontsize;
5795 redirectstderr ();
5797 while true do
5799 Glut.mainLoop ();
5800 with
5801 | Glut.BadEnum "key in special_of_int" ->
5802 showtext '!' " LablGlut bug: special key not recognized";
5804 | Quit ->
5805 wcmd "quit" [];
5806 Config.save ();
5807 exit 0
5809 | exn when conf.redirectstderr ->
5810 let s =
5811 Printf.sprintf "exception %s\n%s"
5812 (Printexc.to_string exn)
5813 (Printexc.get_backtrace ())
5815 ignore (try
5816 Unix.single_write state.stderr s 0 (String.length s);
5817 with _ -> 0);
5818 exit 1
5819 done;