Make space skip past first (not last) visible page
[llpp.git] / main.ml
blob9c8b2ed98e29a44dd06e4348b32b817062f87c37
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and fontpath = string
36 and memsize = int
37 and aalevel = int
38 and irect = (int * int * int * int)
39 and trimparams = (trimmargins * irect)
40 and colorspace = | Rgb | Bgr | Gray
43 type link =
44 | Lnotfound
45 | Lfound of int
46 and linkdir =
47 | LDfirst
48 | LDlast
49 | LDfirstvisible of (int * int * int)
50 | LDleft of int
51 | LDright of int
52 | LDdown of int
53 | LDup of int
56 type pagewithlinks =
57 | Pwlnotfound
58 | Pwl of int
61 type keymap =
62 | KMinsrt of key
63 | KMinsrl of key list
64 | KMmulti of key list * key list
65 and key = int * int
66 and keyhash = (key, keymap) Hashtbl.t
67 and keystate =
68 | KSnone
69 | KSinto of (key list * key list)
72 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
73 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
75 type pipe = (Unix.file_descr * Unix.file_descr);;
77 external init : pipe -> params -> unit = "ml_init";;
78 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
79 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
80 external getpdimrect : int -> float array = "ml_getpdimrect";;
81 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
82 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
83 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
84 external measurestr : int -> string -> float = "ml_measure_string";;
85 external getmaxw : unit -> float = "ml_getmaxw";;
86 external postprocess :
87 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
88 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
89 external platform : unit -> platform = "ml_platform";;
90 external setaalevel : int -> unit = "ml_setaalevel";;
91 external realloctexts : int -> bool = "ml_realloctexts";;
92 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
93 external findlink : opaque -> linkdir -> link = "ml_findlink";;
94 external getlink : opaque -> int -> under = "ml_getlink";;
95 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
96 external getlinkcount : opaque -> int = "ml_getlinkcount";;
97 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
98 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
100 let platform_to_string = function
101 | Punknown -> "unknown"
102 | Plinux -> "Linux"
103 | Posx -> "OSX"
104 | Psun -> "Sun"
105 | Pfreebsd -> "FreeBSD"
106 | Pdragonflybsd -> "DragonflyBSD"
107 | Popenbsd -> "OpenBSD"
108 | Pnetbsd -> "NetBSD"
109 | Pcygwin -> "Cygwin"
112 let platform = platform ();;
114 let popen cmd fda =
115 if platform = Pcygwin
116 then (
117 let sh = "/bin/sh" in
118 let args = [|sh; "-c"; cmd|] in
119 let rec std si so se = function
120 | [] -> si, so, se
121 | (fd, 0) :: rest -> std fd so se rest
122 | (fd, -1) :: rest ->
123 Unix.set_close_on_exec fd;
124 std si so se rest
125 | (_, n) :: _ ->
126 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
128 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
129 ignore (Unix.create_process sh args si so se)
131 else popen cmd fda;
134 type x = int
135 and y = int
136 and tilex = int
137 and tiley = int
138 and tileparams = (x * y * width * height * tilex * tiley)
141 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
143 type mpos = int * int
144 and mstate =
145 | Msel of (mpos * mpos)
146 | Mpan of mpos
147 | Mscrolly | Mscrollx
148 | Mzoom of (int * int)
149 | Mzoomrect of (mpos * mpos)
150 | Mnone
153 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
154 and onkey = string -> int -> te
155 and ondone = string -> unit
156 and histcancel = unit -> unit
157 and onhist = ((histcmd -> string) * histcancel)
158 and histcmd = HCnext | HCprev | HCfirst | HClast
159 and cancelonempty = bool
160 and te =
161 | TEstop
162 | TEdone of string
163 | TEcont of string
164 | TEswitch of textentry
167 type 'a circbuf =
168 { store : 'a array
169 ; mutable rc : int
170 ; mutable wc : int
171 ; mutable len : int
175 let bound v minv maxv =
176 max minv (min maxv v);
179 let cbnew n v =
180 { store = Array.create n v
181 ; rc = 0
182 ; wc = 0
183 ; len = 0
187 let drawstring size x y s =
188 Gl.enable `blend;
189 Gl.enable `texture_2d;
190 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
191 ignore (drawstr size x y s);
192 Gl.disable `blend;
193 Gl.disable `texture_2d;
196 let drawstring1 size x y s =
197 drawstr size x y s;
200 let drawstring2 size x y fmt =
201 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
204 let cbcap b = Array.length b.store;;
206 let cbput b v =
207 let cap = cbcap b in
208 b.store.(b.wc) <- v;
209 b.wc <- (b.wc + 1) mod cap;
210 b.rc <- b.wc;
211 b.len <- min (b.len + 1) cap;
214 let cbempty b = b.len = 0;;
216 let cbgetg b circular dir =
217 if cbempty b
218 then b.store.(0)
219 else
220 let rc = b.rc + dir in
221 let rc =
222 if circular
223 then (
224 if rc = -1
225 then b.len-1
226 else (
227 if rc = b.len
228 then 0
229 else rc
232 else max 0 (min rc (b.len-1))
234 b.rc <- rc;
235 b.store.(rc);
238 let cbget b = cbgetg b false;;
239 let cbgetc b = cbgetg b true;;
241 type page =
242 { pageno : int
243 ; pagedimno : int
244 ; pagew : int
245 ; pageh : int
246 ; pagex : int
247 ; pagey : int
248 ; pagevw : int
249 ; pagevh : int
250 ; pagedispx : int
251 ; pagedispy : int
252 ; pagecol : int
256 let debugl l =
257 dolog "l %d dim=%d {" l.pageno l.pagedimno;
258 dolog " WxH %dx%d" l.pagew l.pageh;
259 dolog " vWxH %dx%d" l.pagevw l.pagevh;
260 dolog " pagex,y %d,%d" l.pagex l.pagey;
261 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
262 dolog " column %d" l.pagecol;
263 dolog "}";
266 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
267 dolog "rect {";
268 dolog " x0,y0=(% f, % f)" x0 y0;
269 dolog " x1,y1=(% f, % f)" x1 y1;
270 dolog " x2,y2=(% f, % f)" x2 y2;
271 dolog " x3,y3=(% f, % f)" x3 y3;
272 dolog "}";
275 type multicolumns = multicol * pagegeom
276 and splitcolumns = columncount * pagegeom
277 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
278 and multicol = columncount * covercount * covercount
279 and pdimno = int
280 and columncount = int
281 and covercount = int;;
283 type conf =
284 { mutable scrollbw : int
285 ; mutable scrollh : int
286 ; mutable icase : bool
287 ; mutable preload : bool
288 ; mutable pagebias : int
289 ; mutable verbose : bool
290 ; mutable debug : bool
291 ; mutable scrollstep : int
292 ; mutable hscrollstep : int
293 ; mutable maxhfit : bool
294 ; mutable crophack : bool
295 ; mutable autoscrollstep : int
296 ; mutable maxwait : float option
297 ; mutable hlinks : bool
298 ; mutable underinfo : bool
299 ; mutable interpagespace : interpagespace
300 ; mutable zoom : float
301 ; mutable presentation : bool
302 ; mutable angle : angle
303 ; mutable winw : int
304 ; mutable winh : int
305 ; mutable savebmarks : bool
306 ; mutable proportional : proportional
307 ; mutable trimmargins : trimmargins
308 ; mutable trimfuzz : irect
309 ; mutable memlimit : memsize
310 ; mutable texcount : texcount
311 ; mutable sliceheight : sliceheight
312 ; mutable thumbw : width
313 ; mutable jumpback : bool
314 ; mutable bgcolor : float * float * float
315 ; mutable bedefault : bool
316 ; mutable scrollbarinpm : bool
317 ; mutable tilew : int
318 ; mutable tileh : int
319 ; mutable mustoresize : memsize
320 ; mutable checkers : bool
321 ; mutable aalevel : int
322 ; mutable urilauncher : string
323 ; mutable pathlauncher : string
324 ; mutable colorspace : colorspace
325 ; mutable invert : bool
326 ; mutable colorscale : float
327 ; mutable redirectstderr : bool
328 ; mutable ghyllscroll : (int * int * int) option
329 ; mutable columns : columns
330 ; mutable beyecolumns : columncount option
331 ; mutable selcmd : string
332 ; mutable updatecurs : bool
333 ; mutable keyhashes : (string * keyhash) list
334 ; mutable hfsize : int
336 and columns =
337 | Csingle
338 | Cmulti of multicolumns
339 | Csplit of splitcolumns
342 type anchor = pageno * top;;
344 type outline = string * int * anchor;;
346 type rect = float * float * float * float * float * float * float * float;;
348 type tile = opaque * pixmapsize * elapsed
349 and elapsed = float;;
350 type pagemapkey = pageno * gen;;
351 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
352 and row = int
353 and col = int;;
355 let emptyanchor = (0, 0.0);;
357 type infochange = | Memused | Docinfo | Pdim;;
359 class type uioh = object
360 method display : unit
361 method key : int -> int -> uioh
362 method button : int -> bool -> int -> int -> int -> uioh
363 method motion : int -> int -> uioh
364 method pmotion : int -> int -> uioh
365 method infochanged : infochange -> unit
366 method scrollpw : (int * float * float)
367 method scrollph : (int * float * float)
368 method modehash : keyhash
369 end;;
371 type mode =
372 | Birdseye of (conf * leftx * pageno * pageno * anchor)
373 | Textentry of (textentry * onleave)
374 | View
375 | LinkNav of linktarget
376 and onleave = leavetextentrystatus -> unit
377 and leavetextentrystatus = | Cancel | Confirm
378 and helpitem = string * int * action
379 and action =
380 | Noaction
381 | Action of (uioh -> uioh)
382 and linktarget =
383 | Ltexact of (pageno * int)
384 | Ltgendir of int
387 let isbirdseye = function Birdseye _ -> true | _ -> false;;
388 let istextentry = function Textentry _ -> true | _ -> false;;
390 type currently =
391 | Idle
392 | Loading of (page * gen)
393 | Tiling of (
394 page * opaque * colorspace * angle * gen * col * row * width * height
396 | Outlining of outline list
399 let emptykeyhash = Hashtbl.create 0;;
400 let nouioh : uioh = object (self)
401 method display = ()
402 method key _ _ = self
403 method button _ _ _ _ _ = self
404 method motion _ _ = self
405 method pmotion _ _ = self
406 method infochanged _ = ()
407 method scrollpw = (0, nan, nan)
408 method scrollph = (0, nan, nan)
409 method modehash = emptykeyhash
410 end;;
412 type state =
413 { mutable sr : Unix.file_descr
414 ; mutable sw : Unix.file_descr
415 ; mutable wsfd : Unix.file_descr
416 ; mutable errfd : Unix.file_descr option
417 ; mutable stderr : Unix.file_descr
418 ; mutable errmsgs : Buffer.t
419 ; mutable newerrmsgs : bool
420 ; mutable w : int
421 ; mutable x : int
422 ; mutable y : int
423 ; mutable scrollw : int
424 ; mutable hscrollh : int
425 ; mutable anchor : anchor
426 ; mutable ranchors : (string * string * anchor) list
427 ; mutable maxy : int
428 ; mutable layout : page list
429 ; pagemap : (pagemapkey, opaque) Hashtbl.t
430 ; tilemap : (tilemapkey, tile) Hashtbl.t
431 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
432 ; mutable pdims : (pageno * width * height * leftx) list
433 ; mutable pagecount : int
434 ; mutable currently : currently
435 ; mutable mstate : mstate
436 ; mutable searchpattern : string
437 ; mutable rects : (pageno * recttype * rect) list
438 ; mutable rects1 : (pageno * recttype * rect) list
439 ; mutable text : string
440 ; mutable fullscreen : (width * height) option
441 ; mutable mode : mode
442 ; mutable uioh : uioh
443 ; mutable outlines : outline array
444 ; mutable bookmarks : outline list
445 ; mutable path : string
446 ; mutable password : string
447 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
448 ; mutable memused : memsize
449 ; mutable gen : gen
450 ; mutable throttle : (page list * int * float) option
451 ; mutable autoscroll : int option
452 ; mutable ghyll : (int option -> unit)
453 ; mutable help : helpitem array
454 ; mutable docinfo : (int * string) list
455 ; mutable texid : GlTex.texture_id option
456 ; hists : hists
457 ; mutable prevzoom : float
458 ; mutable progress : float
459 ; mutable redisplay : bool
460 ; mutable mpos : mpos
461 ; mutable keystate : keystate
462 ; mutable glinks : bool
463 ; mutable prevcolumns : (columns * float) option
465 and hists =
466 { pat : string circbuf
467 ; pag : string circbuf
468 ; nav : anchor circbuf
469 ; sel : string circbuf
473 let defconf =
474 { scrollbw = 7
475 ; scrollh = 12
476 ; icase = true
477 ; preload = true
478 ; pagebias = 0
479 ; verbose = false
480 ; debug = false
481 ; scrollstep = 24
482 ; hscrollstep = 24
483 ; maxhfit = true
484 ; crophack = false
485 ; autoscrollstep = 2
486 ; maxwait = None
487 ; hlinks = false
488 ; underinfo = false
489 ; interpagespace = 2
490 ; zoom = 1.0
491 ; presentation = false
492 ; angle = 0
493 ; winw = 900
494 ; winh = 900
495 ; savebmarks = true
496 ; proportional = true
497 ; trimmargins = false
498 ; trimfuzz = (0,0,0,0)
499 ; memlimit = 32 lsl 20
500 ; texcount = 256
501 ; sliceheight = 24
502 ; thumbw = 76
503 ; jumpback = true
504 ; bgcolor = (0.5, 0.5, 0.5)
505 ; bedefault = false
506 ; scrollbarinpm = true
507 ; tilew = 2048
508 ; tileh = 2048
509 ; mustoresize = 256 lsl 20
510 ; checkers = true
511 ; aalevel = 8
512 ; urilauncher =
513 (match platform with
514 | Plinux | Pfreebsd | Pdragonflybsd
515 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
516 | Posx -> "open \"%s\""
517 | Pcygwin -> "cygstart \"%s\""
518 | Punknown -> "echo %s")
519 ; pathlauncher = "lp \"%s\""
520 ; selcmd =
521 (match platform with
522 | Plinux | Pfreebsd | Pdragonflybsd
523 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
524 | Posx -> "pbcopy"
525 | Pcygwin -> "wsel"
526 | Punknown -> "cat")
527 ; colorspace = Rgb
528 ; invert = false
529 ; colorscale = 1.0
530 ; redirectstderr = false
531 ; ghyllscroll = None
532 ; columns = Csingle
533 ; beyecolumns = None
534 ; updatecurs = false
535 ; hfsize = 12
536 ; keyhashes =
537 let mk n = (n, Hashtbl.create 1) in
538 [ mk "global"
539 ; mk "info"
540 ; mk "help"
541 ; mk "outline"
542 ; mk "listview"
543 ; mk "birdseye"
544 ; mk "textentry"
545 ; mk "links"
546 ; mk "view"
551 let findkeyhash c name =
552 try List.assoc name c.keyhashes
553 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
556 let conf = { defconf with angle = defconf.angle };;
558 type fontstate =
559 { mutable fontsize : int
560 ; mutable wwidth : float
561 ; mutable maxrows : int
565 let fstate =
566 { fontsize = 14
567 ; wwidth = nan
568 ; maxrows = -1
572 let setfontsize n =
573 fstate.fontsize <- n;
574 fstate.wwidth <- measurestr fstate.fontsize "w";
575 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
578 let geturl s =
579 let colonpos = try String.index s ':' with Not_found -> -1 in
580 let len = String.length s in
581 if colonpos >= 0 && colonpos + 3 < len
582 then (
583 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
584 then
585 let schemestartpos =
586 try String.rindex_from s colonpos ' '
587 with Not_found -> -1
589 let scheme =
590 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
592 match scheme with
593 | "http" | "ftp" | "mailto" ->
594 let epos =
595 try String.index_from s colonpos ' '
596 with Not_found -> len
598 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
599 | _ -> ""
600 else ""
602 else ""
605 let gotouri uri =
606 if String.length conf.urilauncher = 0
607 then print_endline uri
608 else (
609 let url = geturl uri in
610 if String.length url = 0
611 then print_endline uri
612 else
613 let re = Str.regexp "%s" in
614 let command = Str.global_replace re url conf.urilauncher in
615 try popen command []
616 with exn ->
617 Printf.eprintf
618 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
619 flush stderr;
623 let version () =
624 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
625 (platform_to_string platform) Sys.word_size Sys.ocaml_version
628 let makehelp () =
629 let strings = version () :: "" :: Help.keys in
630 Array.of_list (
631 List.map (fun s ->
632 let url = geturl s in
633 if String.length url > 0
634 then (s, 0, Action (fun u -> gotouri url; u))
635 else (s, 0, Noaction)
636 ) strings);
639 let noghyll _ = ();;
640 let firstgeomcmds = "", [];;
642 let state =
643 { sr = Unix.stdin
644 ; sw = Unix.stdin
645 ; wsfd = Unix.stdin
646 ; errfd = None
647 ; stderr = Unix.stderr
648 ; errmsgs = Buffer.create 0
649 ; newerrmsgs = false
650 ; x = 0
651 ; y = 0
652 ; w = 0
653 ; scrollw = 0
654 ; hscrollh = 0
655 ; anchor = emptyanchor
656 ; ranchors = []
657 ; layout = []
658 ; maxy = max_int
659 ; tilelru = Queue.create ()
660 ; pagemap = Hashtbl.create 10
661 ; tilemap = Hashtbl.create 10
662 ; pdims = []
663 ; pagecount = 0
664 ; currently = Idle
665 ; mstate = Mnone
666 ; rects = []
667 ; rects1 = []
668 ; text = ""
669 ; mode = View
670 ; fullscreen = None
671 ; searchpattern = ""
672 ; outlines = [||]
673 ; bookmarks = []
674 ; path = ""
675 ; password = ""
676 ; geomcmds = firstgeomcmds
677 ; hists =
678 { nav = cbnew 10 (0, 0.0)
679 ; pat = cbnew 10 ""
680 ; pag = cbnew 10 ""
681 ; sel = cbnew 10 ""
683 ; memused = 0
684 ; gen = 0
685 ; throttle = None
686 ; autoscroll = None
687 ; ghyll = noghyll
688 ; help = makehelp ()
689 ; docinfo = []
690 ; texid = None
691 ; prevzoom = 1.0
692 ; progress = -1.0
693 ; uioh = nouioh
694 ; redisplay = true
695 ; mpos = (-1, -1)
696 ; keystate = KSnone
697 ; glinks = false
698 ; prevcolumns = None
702 let vlog fmt =
703 if conf.verbose
704 then
705 Printf.kprintf prerr_endline fmt
706 else
707 Printf.kprintf ignore fmt
710 let launchpath () =
711 if String.length conf.pathlauncher = 0
712 then print_endline state.path
713 else (
714 let re = Str.regexp "%s" in
715 let command = Str.global_replace re state.path conf.pathlauncher in
716 try popen command []
717 with exn ->
718 Printf.eprintf
719 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
720 flush stderr;
724 module Ne = struct
725 type 'a t = | Res of 'a | Exn of exn;;
727 let pipe () =
728 try Res (Unix.pipe ())
729 with exn -> Exn exn
732 let clo fd f =
733 try Unix.close fd
734 with exn -> f (Printexc.to_string exn)
737 let dup fd =
738 try Res (Unix.dup fd)
739 with exn -> Exn exn
742 let dup2 fd1 fd2 =
743 try Res (Unix.dup2 fd1 fd2)
744 with exn -> Exn exn
746 end;;
748 let redirectstderr () =
749 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
750 if conf.redirectstderr
751 then
752 match Ne.pipe () with
753 | Ne.Exn exn ->
754 dolog "failed to create stderr redirection pipes: %s"
755 (Printexc.to_string exn)
757 | Ne.Res (r, w) ->
758 begin match Ne.dup Unix.stderr with
759 | Ne.Exn exn ->
760 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
761 Ne.clo r (clofail "pipe/r");
762 Ne.clo w (clofail "pipe/w");
764 | Ne.Res dupstderr ->
765 begin match Ne.dup2 w Unix.stderr with
766 | Ne.Exn exn ->
767 dolog "failed to dup2 to stderr: %s"
768 (Printexc.to_string exn);
769 Ne.clo dupstderr (clofail "stderr duplicate");
770 Ne.clo r (clofail "redir pipe/r");
771 Ne.clo w (clofail "redir pipe/w");
773 | Ne.Res () ->
774 state.stderr <- dupstderr;
775 state.errfd <- Some r;
776 end;
778 else (
779 state.newerrmsgs <- false;
780 begin match state.errfd with
781 | Some fd ->
782 begin match Ne.dup2 state.stderr Unix.stderr with
783 | Ne.Exn exn ->
784 dolog "failed to dup2 original stderr: %s"
785 (Printexc.to_string exn)
786 | Ne.Res () ->
787 Ne.clo fd (clofail "dup of stderr");
788 Unix.dup2 state.stderr Unix.stderr;
789 state.errfd <- None;
790 end;
791 | None -> ()
792 end;
793 prerr_string (Buffer.contents state.errmsgs);
794 flush stderr;
795 Buffer.clear state.errmsgs;
799 module G =
800 struct
801 let postRedisplay who =
802 if conf.verbose
803 then prerr_endline ("redisplay for " ^ who);
804 state.redisplay <- true;
806 end;;
808 let getopaque pageno =
809 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
810 with Not_found -> None
813 let putopaque pageno opaque =
814 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
817 let pagetranslatepoint l x y =
818 let dy = y - l.pagedispy in
819 let y = dy + l.pagey in
820 let dx = x - l.pagedispx in
821 let x = dx + l.pagex in
822 (x, y);
825 let getunder x y =
826 let rec f = function
827 | l :: rest ->
828 begin match getopaque l.pageno with
829 | Some opaque ->
830 let x0 = l.pagedispx in
831 let x1 = x0 + l.pagevw in
832 let y0 = l.pagedispy in
833 let y1 = y0 + l.pagevh in
834 if y >= y0 && y <= y1 && x >= x0 && x <= x1
835 then
836 let px, py = pagetranslatepoint l x y in
837 match whatsunder opaque px py with
838 | Unone -> f rest
839 | under -> under
840 else f rest
841 | _ ->
842 f rest
844 | [] -> Unone
846 f state.layout
849 let showtext c s =
850 state.text <- Printf.sprintf "%c%s" c s;
851 G.postRedisplay "showtext";
854 let undertext = function
855 | Unone -> "none"
856 | Ulinkuri s -> s
857 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
858 | Utext s -> "font: " ^ s
859 | Uunexpected s -> "unexpected: " ^ s
860 | Ulaunch s -> "launch: " ^ s
861 | Unamed s -> "named: " ^ s
862 | Uremote (filename, pageno) ->
863 Printf.sprintf "%s: page %d" filename (pageno+1)
866 let updateunder x y =
867 match getunder x y with
868 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
869 | Ulinkuri uri ->
870 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
871 Wsi.setcursor Wsi.CURSOR_INFO
872 | Ulinkgoto (pageno, _) ->
873 if conf.underinfo
874 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
875 Wsi.setcursor Wsi.CURSOR_INFO
876 | Utext s ->
877 if conf.underinfo then showtext 'f' ("ont: " ^ s);
878 Wsi.setcursor Wsi.CURSOR_TEXT
879 | Uunexpected s ->
880 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
881 Wsi.setcursor Wsi.CURSOR_INHERIT
882 | Ulaunch s ->
883 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
884 Wsi.setcursor Wsi.CURSOR_INHERIT
885 | Unamed s ->
886 if conf.underinfo then showtext 'n' ("amed: " ^ s);
887 Wsi.setcursor Wsi.CURSOR_INHERIT
888 | Uremote (filename, pageno) ->
889 if conf.underinfo then showtext 'r'
890 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
891 Wsi.setcursor Wsi.CURSOR_INFO
894 let showlinktype under =
895 if conf.underinfo
896 then
897 match under with
898 | Unone -> ()
899 | under ->
900 let s = undertext under in
901 showtext ' ' s
904 let addchar s c =
905 let b = Buffer.create (String.length s + 1) in
906 Buffer.add_string b s;
907 Buffer.add_char b c;
908 Buffer.contents b;
911 let colorspace_of_string s =
912 match String.lowercase s with
913 | "rgb" -> Rgb
914 | "bgr" -> Bgr
915 | "gray" -> Gray
916 | _ -> failwith "invalid colorspace"
919 let int_of_colorspace = function
920 | Rgb -> 0
921 | Bgr -> 1
922 | Gray -> 2
925 let colorspace_of_int = function
926 | 0 -> Rgb
927 | 1 -> Bgr
928 | 2 -> Gray
929 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
932 let colorspace_to_string = function
933 | Rgb -> "rgb"
934 | Bgr -> "bgr"
935 | Gray -> "gray"
938 let intentry_with_suffix text key =
939 let c =
940 if key >= 32 && key < 127
941 then Char.chr key
942 else '\000'
944 match Char.lowercase c with
945 | '0' .. '9' ->
946 let text = addchar text c in
947 TEcont text
949 | 'k' | 'm' | 'g' ->
950 let text = addchar text c in
951 TEcont text
953 | _ ->
954 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
955 TEcont text
958 let multicolumns_to_string (n, a, b) =
959 if a = 0 && b = 0
960 then Printf.sprintf "%d" n
961 else Printf.sprintf "%d,%d,%d" n a b;
964 let multicolumns_of_string s =
966 (int_of_string s, 0, 0)
967 with _ ->
968 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
971 let readcmd fd =
972 let s = "xxxx" in
973 let n = Unix.read fd s 0 4 in
974 if n != 4 then failwith "incomplete read(len)";
975 let len = 0
976 lor (Char.code s.[0] lsl 24)
977 lor (Char.code s.[1] lsl 16)
978 lor (Char.code s.[2] lsl 8)
979 lor (Char.code s.[3] lsl 0)
981 let s = String.create len in
982 let n = Unix.read fd s 0 len in
983 if n != len then failwith "incomplete read(data)";
987 let btod b = if b then 1 else 0;;
989 let wcmd fmt =
990 let b = Buffer.create 16 in
991 Buffer.add_string b "llll";
992 Printf.kbprintf
993 (fun b ->
994 let s = Buffer.contents b in
995 let n = String.length s in
996 let len = n - 4 in
997 (* dolog "wcmd %S" (String.sub s 4 len); *)
998 s.[0] <- Char.chr ((len lsr 24) land 0xff);
999 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1000 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1001 s.[3] <- Char.chr (len land 0xff);
1002 let n' = Unix.write state.sw s 0 n in
1003 if n' != n then failwith "write failed";
1004 ) b fmt;
1007 let calcips h =
1008 if conf.presentation
1009 then
1010 let d = conf.winh - h in
1011 max 0 ((d + 1) / 2)
1012 else
1013 conf.interpagespace
1016 let calcheight () =
1017 let rec f pn ph pi fh l =
1018 match l with
1019 | (n, _, h, _) :: rest ->
1020 let ips = calcips h in
1021 let fh =
1022 if conf.presentation
1023 then fh+ips
1024 else (
1025 if isbirdseye state.mode && pn = 0
1026 then fh + ips
1027 else fh
1030 let fh = fh + ((n - pn) * (ph + pi)) in
1031 f n h ips fh rest;
1033 | [] ->
1034 let inc =
1035 if conf.presentation || (isbirdseye state.mode && pn = 0)
1036 then 0
1037 else -pi
1039 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
1040 max 0 fh
1042 let fh = f 0 0 0 0 state.pdims in
1046 let calcheight () =
1047 match conf.columns with
1048 | Csingle -> calcheight ()
1049 | Cmulti ((c, _, _), b) ->
1050 let rec loop y h n =
1051 if n < 0
1052 then loop y h (n+1)
1053 else (
1054 if n = Array.length b
1055 then y + h
1056 else
1057 let (_, _, y', (_, _, h', _)) = b.(n) in
1058 let y = min y y'
1059 and h = max h h' in
1060 loop y h (n+1)
1063 loop max_int 0 (((Array.length b - 1) / c) * c)
1064 | Csplit (_, b) ->
1065 if Array.length b > 0
1066 then
1067 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1068 y + h
1069 else 0
1072 let getpageyh pageno =
1073 let rec f pn ph pi y l =
1074 match l with
1075 | (n, _, h, _) :: rest ->
1076 let ips = calcips h in
1077 if n >= pageno
1078 then
1079 let h = if n = pageno then h else ph in
1080 if conf.presentation && n = pageno
1081 then
1082 y + (pageno - pn) * (ph + pi) + pi, h
1083 else
1084 y + (pageno - pn) * (ph + pi), h
1085 else
1086 let y = y + (if conf.presentation then pi else 0) in
1087 let y = y + (n - pn) * (ph + pi) in
1088 f n h ips y rest
1090 | [] ->
1091 y + (pageno - pn) * (ph + pi), ph
1093 f 0 0 0 0 state.pdims
1096 let getpageyh pageno =
1097 match conf.columns with
1098 | Csingle -> getpageyh pageno
1099 | Cmulti (_, b) ->
1100 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1101 y, h
1102 | Csplit (c, b) ->
1103 let n = pageno*c in
1104 let (_, _, y, (_, _, h, _)) = b.(n) in
1105 y, h
1108 let getpagedim pageno =
1109 let rec f ppdim l =
1110 match l with
1111 | (n, _, _, _) as pdim :: rest ->
1112 if n >= pageno
1113 then (if n = pageno then pdim else ppdim)
1114 else f pdim rest
1116 | [] -> ppdim
1118 f (-1, -1, -1, -1) state.pdims
1121 let getpagey pageno = fst (getpageyh pageno);;
1123 let nogeomcmds cmds =
1124 match cmds with
1125 | s, [] -> String.length s = 0
1126 | _ -> false
1129 let layout1 y sh =
1130 let sh = sh - state.hscrollh in
1131 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1132 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1133 match pdims with
1134 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1135 let ips = calcips h in
1136 let yinc =
1137 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1138 then ips
1139 else 0
1141 (w, h, ips, xoff), rest, pdimno + 1, yinc
1142 | _ ->
1143 prev, pdims, pdimno, 0
1145 let dy = dy + yinc in
1146 let py = py + yinc in
1147 if pageno = state.pagecount || dy >= sh
1148 then
1149 accu
1150 else
1151 let vy = y + dy in
1152 if py + h <= vy - yinc
1153 then
1154 let py = py + h + ips in
1155 let dy = max 0 (py - y) in
1156 f ~pageno:(pageno+1)
1157 ~pdimno
1158 ~prev:curr
1161 ~pdims:rest
1162 ~accu
1163 else
1164 let pagey = vy - py in
1165 let pagevh = h - pagey in
1166 let pagevh = min (sh - dy) pagevh in
1167 let off = if yinc > 0 then py - vy else 0 in
1168 let py = py + h + ips in
1169 let pagex, dx =
1170 let xoff = xoff +
1171 if state.w < conf.winw - state.scrollw
1172 then (conf.winw - state.scrollw - state.w) / 2
1173 else 0
1175 let dispx = xoff + state.x in
1176 if dispx < 0
1177 then (-dispx, 0)
1178 else (0, dispx)
1180 let pagevw =
1181 let lw = w - pagex in
1182 min lw (conf.winw - state.scrollw)
1184 let e =
1185 { pageno = pageno
1186 ; pagedimno = pdimno
1187 ; pagew = w
1188 ; pageh = h
1189 ; pagex = pagex
1190 ; pagey = pagey + off
1191 ; pagevw = pagevw
1192 ; pagevh = pagevh - off
1193 ; pagedispx = dx
1194 ; pagedispy = dy + off
1195 ; pagecol = 0
1198 let accu = e :: accu in
1199 f ~pageno:(pageno+1)
1200 ~pdimno
1201 ~prev:curr
1203 ~dy:(dy+pagevh+ips)
1204 ~pdims:rest
1205 ~accu
1207 let accu =
1209 ~pageno:0
1210 ~pdimno:~-1
1211 ~prev:(0,0,0,0)
1212 ~py:0
1213 ~dy:0
1214 ~pdims:state.pdims
1215 ~accu:[]
1217 List.rev accu
1220 let layoutN ((columns, coverA, coverB), b) y sh =
1221 let sh = sh - state.hscrollh in
1222 let rec fold accu n =
1223 if n = Array.length b
1224 then accu
1225 else
1226 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1227 if (vy - y) > sh &&
1228 (n = coverA - 1
1229 || n = state.pagecount - coverB
1230 || (n - coverA) mod columns = columns - 1)
1231 then accu
1232 else
1233 let accu =
1234 if vy + h > y
1235 then
1236 let pagey = max 0 (y - vy) in
1237 let pagedispy = if pagey > 0 then 0 else vy - y in
1238 let pagedispx, pagex =
1239 let pdx =
1240 if n = coverA - 1 || n = state.pagecount - coverB
1241 then state.x + (conf.winw - state.scrollw - w) / 2
1242 else dx + xoff + state.x
1244 if pdx < 0
1245 then 0, -pdx
1246 else pdx, 0
1248 let pagevw =
1249 let vw = conf.winw - state.scrollw - pagedispx in
1250 let pw = w - pagex in
1251 min vw pw
1253 let pagevh = min (h - pagey) (sh - pagedispy) in
1254 if pagevw > 0 && pagevh > 0
1255 then
1256 let e =
1257 { pageno = n
1258 ; pagedimno = pdimno
1259 ; pagew = w
1260 ; pageh = h
1261 ; pagex = pagex
1262 ; pagey = pagey
1263 ; pagevw = pagevw
1264 ; pagevh = pagevh
1265 ; pagedispx = pagedispx
1266 ; pagedispy = pagedispy
1267 ; pagecol = 0
1270 e :: accu
1271 else
1272 accu
1273 else
1274 accu
1276 fold accu (n+1)
1278 List.rev (fold [] 0)
1281 let layoutS (columns, b) y sh =
1282 let sh = sh - state.hscrollh in
1283 let rec fold accu n =
1284 if n = Array.length b
1285 then accu
1286 else
1287 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1288 if (vy - y) > sh
1289 then accu
1290 else
1291 let accu =
1292 if vy + pageh > y
1293 then
1294 let x = xoff + state.x in
1295 let pagey = max 0 (y - vy) in
1296 let pagedispy = if pagey > 0 then 0 else vy - y in
1297 let pagedispx, pagex =
1298 if px = 0
1299 then (
1300 if x < 0
1301 then 0, -x
1302 else x, 0
1304 else (
1305 let px = px - x in
1306 if px < 0
1307 then -px, 0
1308 else 0, px
1311 let pagecolw = pagew/columns in
1312 let pagedispx =
1313 if pagecolw < conf.winw
1314 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1315 else pagedispx
1317 let pagevw =
1318 let vw = conf.winw - pagedispx - state.scrollw in
1319 let pw = pagew - pagex in
1320 min vw pw
1322 let pagevw = min pagevw pagecolw in
1323 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1324 if pagevw > 0 && pagevh > 0
1325 then
1326 let e =
1327 { pageno = n/columns
1328 ; pagedimno = pdimno
1329 ; pagew = pagew
1330 ; pageh = pageh
1331 ; pagex = pagex
1332 ; pagey = pagey
1333 ; pagevw = pagevw
1334 ; pagevh = pagevh
1335 ; pagedispx = pagedispx
1336 ; pagedispy = pagedispy
1337 ; pagecol = n mod columns
1340 e :: accu
1341 else
1342 accu
1343 else
1344 accu
1346 fold accu (n+1)
1348 List.rev (fold [] 0)
1351 let layout y sh =
1352 if nogeomcmds state.geomcmds
1353 then
1354 match conf.columns with
1355 | Csingle -> layout1 y sh
1356 | Cmulti c -> layoutN c y sh
1357 | Csplit s -> layoutS s y sh
1358 else []
1361 let clamp incr =
1362 let y = state.y + incr in
1363 let y = max 0 y in
1364 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1368 let itertiles l f =
1369 let tilex = l.pagex mod conf.tilew in
1370 let tiley = l.pagey mod conf.tileh in
1372 let col = l.pagex / conf.tilew in
1373 let row = l.pagey / conf.tileh in
1375 let rec rowloop row y0 dispy h =
1376 if h = 0
1377 then ()
1378 else (
1379 let dh = conf.tileh - y0 in
1380 let dh = min h dh in
1381 let rec colloop col x0 dispx w =
1382 if w = 0
1383 then ()
1384 else (
1385 let dw = conf.tilew - x0 in
1386 let dw = min w dw in
1388 f col row dispx dispy x0 y0 dw dh;
1389 colloop (col+1) 0 (dispx+dw) (w-dw)
1392 colloop col tilex l.pagedispx l.pagevw;
1393 rowloop (row+1) 0 (dispy+dh) (h-dh)
1396 if l.pagevw > 0 && l.pagevh > 0
1397 then rowloop row tiley l.pagedispy l.pagevh;
1400 let gettileopaque l col row =
1401 let key =
1402 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1404 try Some (Hashtbl.find state.tilemap key)
1405 with Not_found -> None
1408 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1409 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1410 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1413 let drawtiles l color =
1414 GlDraw.color color;
1415 let f col row x y tilex tiley w h =
1416 match gettileopaque l col row with
1417 | Some (opaque, _, t) ->
1418 let params = x, y, w, h, tilex, tiley in
1419 if conf.invert
1420 then (
1421 Gl.enable `blend;
1422 GlFunc.blend_func `zero `one_minus_src_color;
1424 drawtile params opaque;
1425 if conf.invert
1426 then Gl.disable `blend;
1427 if conf.debug
1428 then (
1429 let s = Printf.sprintf
1430 "%d[%d,%d] %f sec"
1431 l.pageno col row t
1433 let w = measurestr fstate.fontsize s in
1434 GlMisc.push_attrib [`current];
1435 GlDraw.color (0.0, 0.0, 0.0);
1436 GlDraw.rect
1437 (float (x-2), float (y-2))
1438 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1439 GlDraw.color (1.0, 1.0, 1.0);
1440 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1441 GlMisc.pop_attrib ();
1444 | _ ->
1445 let w =
1446 let lw = conf.winw - state.scrollw - x in
1447 min lw w
1448 and h =
1449 let lh = conf.winh - y in
1450 min lh h
1452 begin match state.texid with
1453 | Some id ->
1454 Gl.enable `texture_2d;
1455 GlTex.bind_texture `texture_2d id;
1456 let x0 = float x
1457 and y0 = float y
1458 and x1 = float (x+w)
1459 and y1 = float (y+h) in
1461 let tw = float w /. 64.0
1462 and th = float h /. 64.0 in
1463 let tx0 = float tilex /. 64.0
1464 and ty0 = float tiley /. 64.0 in
1465 let tx1 = tx0 +. tw
1466 and ty1 = ty0 +. th in
1467 GlDraw.begins `quads;
1468 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1469 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1470 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1471 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1472 GlDraw.ends ();
1474 Gl.disable `texture_2d;
1475 | None ->
1476 GlDraw.color (1.0, 1.0, 1.0);
1477 GlDraw.rect
1478 (float x, float y)
1479 (float (x+w), float (y+h));
1480 end;
1481 if w > 128 && h > fstate.fontsize + 10
1482 then (
1483 GlDraw.color (0.0, 0.0, 0.0);
1484 let c, r =
1485 if conf.verbose
1486 then (col*conf.tilew, row*conf.tileh)
1487 else col, row
1489 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1491 GlDraw.color color;
1493 itertiles l f
1496 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1498 let tilevisible1 l x y =
1499 let ax0 = l.pagex
1500 and ax1 = l.pagex + l.pagevw
1501 and ay0 = l.pagey
1502 and ay1 = l.pagey + l.pagevh in
1504 let bx0 = x
1505 and by0 = y in
1506 let bx1 = min (bx0 + conf.tilew) l.pagew
1507 and by1 = min (by0 + conf.tileh) l.pageh in
1509 let rx0 = max ax0 bx0
1510 and ry0 = max ay0 by0
1511 and rx1 = min ax1 bx1
1512 and ry1 = min ay1 by1 in
1514 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1515 nonemptyintersection
1518 let tilevisible layout n x y =
1519 let rec findpageinlayout m = function
1520 | l :: rest when l.pageno = n ->
1521 tilevisible1 l x y || (
1522 match conf.columns with
1523 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1524 | _ -> false
1526 | _ :: rest -> findpageinlayout 0 rest
1527 | [] -> false
1529 findpageinlayout 0 layout;
1532 let tileready l x y =
1533 tilevisible1 l x y &&
1534 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1537 let tilepage n p layout =
1538 let rec loop = function
1539 | l :: rest ->
1540 if l.pageno = n
1541 then
1542 let f col row _ _ _ _ _ _ =
1543 if state.currently = Idle
1544 then
1545 match gettileopaque l col row with
1546 | Some _ -> ()
1547 | None ->
1548 let x = col*conf.tilew
1549 and y = row*conf.tileh in
1550 let w =
1551 let w = l.pagew - x in
1552 min w conf.tilew
1554 let h =
1555 let h = l.pageh - y in
1556 min h conf.tileh
1558 wcmd "tile %s %d %d %d %d" p x y w h;
1559 state.currently <-
1560 Tiling (
1561 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1562 conf.tilew, conf.tileh
1565 itertiles l f;
1566 else
1567 loop rest
1569 | [] -> ()
1571 if nogeomcmds state.geomcmds
1572 then loop layout;
1575 let preloadlayout visiblepages =
1576 let presentation = conf.presentation in
1577 let interpagespace = conf.interpagespace in
1578 let maxy = state.maxy in
1579 conf.presentation <- false;
1580 conf.interpagespace <- 0;
1581 state.maxy <- calcheight ();
1582 let y =
1583 match visiblepages with
1584 | [] -> if state.y >= maxy then maxy else 0
1585 | l :: _ -> getpagey l.pageno + l.pagey
1587 let y = if y < conf.winh then 0 else y - conf.winh in
1588 let h = conf.winh*3 in
1589 let pages = layout y h in
1590 conf.presentation <- presentation;
1591 conf.interpagespace <- interpagespace;
1592 state.maxy <- maxy;
1593 pages;
1596 let load pages =
1597 let rec loop pages =
1598 if state.currently != Idle
1599 then ()
1600 else
1601 match pages with
1602 | l :: rest ->
1603 begin match getopaque l.pageno with
1604 | None ->
1605 wcmd "page %d %d" l.pageno l.pagedimno;
1606 state.currently <- Loading (l, state.gen);
1607 | Some opaque ->
1608 tilepage l.pageno opaque pages;
1609 loop rest
1610 end;
1611 | _ -> ()
1613 if nogeomcmds state.geomcmds
1614 then loop pages
1617 let preload pages =
1618 load pages;
1619 if conf.preload && state.currently = Idle
1620 then load (preloadlayout pages);
1623 let layoutready layout =
1624 let rec fold all ls =
1625 all && match ls with
1626 | l :: rest ->
1627 let seen = ref false in
1628 let allvisible = ref true in
1629 let foo col row _ _ _ _ _ _ =
1630 seen := true;
1631 allvisible := !allvisible &&
1632 begin match gettileopaque l col row with
1633 | Some _ -> true
1634 | None -> false
1637 itertiles l foo;
1638 fold (!seen && !allvisible) rest
1639 | [] -> true
1641 let alltilesvisible = fold true layout in
1642 alltilesvisible;
1645 let gotoy y =
1646 let y = bound y 0 state.maxy in
1647 let y, layout, proceed =
1648 match conf.maxwait with
1649 | Some time when state.ghyll == noghyll ->
1650 begin match state.throttle with
1651 | None ->
1652 let layout = layout y conf.winh in
1653 let ready = layoutready layout in
1654 if not ready
1655 then (
1656 load layout;
1657 state.throttle <- Some (layout, y, now ());
1659 else G.postRedisplay "gotoy showall (None)";
1660 y, layout, ready
1661 | Some (_, _, started) ->
1662 let dt = now () -. started in
1663 if dt > time
1664 then (
1665 state.throttle <- None;
1666 let layout = layout y conf.winh in
1667 load layout;
1668 G.postRedisplay "maxwait";
1669 y, layout, true
1671 else -1, [], false
1674 | _ ->
1675 let layout = layout y conf.winh in
1676 if true || layoutready layout
1677 then G.postRedisplay "gotoy ready";
1678 y, layout, true
1680 if proceed
1681 then (
1682 state.y <- y;
1683 state.layout <- layout;
1684 begin match state.mode with
1685 | LinkNav (Ltexact (pageno, linkno)) ->
1686 let rec loop = function
1687 | [] ->
1688 state.mode <- LinkNav (Ltgendir 0)
1689 | l :: _ when l.pageno = pageno ->
1690 begin match getopaque pageno with
1691 | None ->
1692 state.mode <- LinkNav (Ltgendir 0)
1693 | Some opaque ->
1694 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1695 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1696 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1697 then state.mode <- LinkNav (Ltgendir 0)
1699 | _ :: rest -> loop rest
1701 loop layout
1702 | _ -> ()
1703 end;
1704 begin match state.mode with
1705 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1706 if not (pagevisible layout pageno)
1707 then (
1708 match state.layout with
1709 | [] -> ()
1710 | l :: _ ->
1711 state.mode <- Birdseye (
1712 conf, leftx, l.pageno, hooverpageno, anchor
1715 | LinkNav (Ltgendir dir as lt) ->
1716 let linknav =
1717 let rec loop = function
1718 | [] -> lt
1719 | l :: rest ->
1720 match getopaque l.pageno with
1721 | None -> loop rest
1722 | Some opaque ->
1723 let link =
1724 let ld =
1725 if dir = 0
1726 then LDfirstvisible (l.pagex, l.pagey, dir)
1727 else (
1728 if dir > 0 then LDfirst else LDlast
1731 findlink opaque ld
1733 match link with
1734 | Lnotfound -> loop rest
1735 | Lfound n ->
1736 showlinktype (getlink opaque n);
1737 Ltexact (l.pageno, n)
1739 loop state.layout
1741 state.mode <- LinkNav linknav
1742 | _ -> ()
1743 end;
1744 preload layout;
1746 state.ghyll <- noghyll;
1747 if conf.updatecurs
1748 then (
1749 let mx, my = state.mpos in
1750 updateunder mx my;
1754 let conttiling pageno opaque =
1755 tilepage pageno opaque
1756 (if conf.preload then preloadlayout state.layout else state.layout)
1759 let gotoy_and_clear_text y =
1760 if not conf.verbose then state.text <- "";
1761 gotoy y;
1764 let getanchor () =
1765 match state.layout with
1766 | [] -> emptyanchor
1767 | l :: _ ->
1768 let coloff = l.pagecol * l.pageh in
1769 (l.pageno, (float l.pagey +. float coloff) /. float l.pageh)
1772 let getanchory (n, top) =
1773 let y, h = getpageyh n in
1774 y + (truncate (top *. float h));
1777 let gotoanchor anchor =
1778 gotoy (getanchory anchor);
1781 let addnav () =
1782 cbput state.hists.nav (getanchor ());
1785 let getnav dir =
1786 let anchor = cbgetc state.hists.nav dir in
1787 getanchory anchor;
1790 let gotoghyll y =
1791 let scroll f n a b =
1792 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1793 let snake f a b =
1794 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1795 if f < a
1796 then s (float f /. float a)
1797 else (
1798 if f > b
1799 then 1.0 -. s ((float (f-b) /. float (n-b)))
1800 else 1.0
1803 snake f a b
1804 and summa f n a b =
1805 (* courtesy:
1806 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1807 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1808 let iv1 = iv f in
1809 let ins = float a *. iv1
1810 and outs = float (n-b) *. iv1 in
1811 let ones = b - a in
1812 ins +. outs +. float ones
1814 let rec set (_N, _A, _B) y sy =
1815 let sum = summa 1.0 _N _A _B in
1816 let dy = float (y - sy) in
1817 state.ghyll <- (
1818 let rec gf n y1 o =
1819 if n >= _N
1820 then state.ghyll <- noghyll
1821 else
1822 let go n =
1823 let s = scroll n _N _A _B in
1824 let y1 = y1 +. ((s *. dy) /. sum) in
1825 gotoy_and_clear_text (truncate y1);
1826 state.ghyll <- gf (n+1) y1;
1828 match o with
1829 | None -> go n
1830 | Some y' -> set (_N/2, 0, 0) y' state.y
1832 gf 0 (float state.y)
1835 match conf.ghyllscroll with
1836 | None ->
1837 gotoy_and_clear_text y
1838 | Some nab ->
1839 if state.ghyll == noghyll
1840 then set nab y state.y
1841 else state.ghyll (Some y)
1844 let gotopage n top =
1845 let y, h = getpageyh n in
1846 let y = y + (truncate (top *. float h)) in
1847 gotoghyll y
1850 let gotopage1 n top =
1851 let y = getpagey n in
1852 let y = y + top in
1853 gotoghyll y
1856 let invalidate s f =
1857 state.layout <- [];
1858 state.pdims <- [];
1859 state.rects <- [];
1860 state.rects1 <- [];
1861 match state.geomcmds with
1862 | ps, [] when String.length ps = 0 ->
1863 f ();
1864 state.geomcmds <- s, [];
1866 | ps, [] ->
1867 state.geomcmds <- ps, [s, f];
1869 | ps, (s', _) :: rest when s' = s ->
1870 state.geomcmds <- ps, ((s, f) :: rest);
1872 | ps, cmds ->
1873 state.geomcmds <- ps, ((s, f) :: cmds);
1876 let opendoc path password =
1877 state.path <- path;
1878 state.password <- password;
1879 state.gen <- state.gen + 1;
1880 state.docinfo <- [];
1882 setaalevel conf.aalevel;
1883 Wsi.settitle ("llpp " ^ Filename.basename path);
1884 wcmd "open %s\000%s\000" path password;
1885 invalidate "reqlayout"
1886 (fun () ->
1887 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1890 let scalecolor c =
1891 let c = c *. conf.colorscale in
1892 (c, c, c);
1895 let scalecolor2 (r, g, b) =
1896 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1899 let docolumns = function
1900 | Csingle -> ()
1902 | Cmulti ((columns, coverA, coverB), _) ->
1903 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1904 let rec loop pageno pdimno pdim x y rowh pdims =
1905 let rec fixrow m = if m = pageno then () else
1906 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1907 if h < rowh
1908 then (
1909 let y = y + (rowh - h) / 2 in
1910 a.(m) <- (pdimno, x, y, pdim);
1912 fixrow (m+1)
1914 if pageno = state.pagecount
1915 then fixrow (((pageno - 1) / columns) * columns)
1916 else
1917 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1918 match pdims with
1919 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1920 pdimno+1, pdim, rest
1921 | _ ->
1922 pdimno, pdim, pdims
1924 let x, y, rowh' =
1925 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1926 then (
1927 (conf.winw - state.scrollw - w) / 2,
1928 y + rowh + conf.interpagespace, h
1930 else (
1931 if (pageno - coverA) mod columns = 0
1932 then 0, y + rowh + conf.interpagespace, h
1933 else x, y, max rowh h
1936 if pageno > 1 && (pageno - coverA) mod columns = 0
1937 then fixrow (pageno - columns);
1938 a.(pageno) <- (pdimno, x, y, pdim);
1939 let x = x + w + xoff*2 + conf.interpagespace in
1940 loop (pageno+1) pdimno pdim x y rowh' pdims
1942 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1943 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1945 | Csplit (c, _) ->
1946 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1947 let rec loop pageno pdimno pdim y pdims =
1948 if pageno = state.pagecount
1949 then ()
1950 else
1951 let pdimno, ((_, w, h, _) as pdim), pdims =
1952 match pdims with
1953 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1954 pdimno+1, pdim, rest
1955 | _ ->
1956 pdimno, pdim, pdims
1958 let cw = w / c in
1959 let rec loop1 n x y =
1960 if n = c then y else (
1961 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1962 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1965 let y = loop1 0 0 y in
1966 loop (pageno+1) pdimno pdim y pdims
1968 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1969 conf.columns <- Csplit (c, a);
1972 let represent () =
1973 docolumns conf.columns;
1974 state.maxy <- calcheight ();
1975 state.hscrollh <-
1976 if state.w <= conf.winw - state.scrollw
1977 then 0
1978 else state.scrollw
1980 match state.mode with
1981 | Birdseye (_, _, pageno, _, _) ->
1982 let y, h = getpageyh pageno in
1983 let top = (conf.winh - h) / 2 in
1984 gotoy (max 0 (y - top))
1985 | _ -> gotoanchor state.anchor
1988 let reshape w h =
1989 GlDraw.viewport 0 0 w h;
1990 let firsttime = state.geomcmds == firstgeomcmds in
1991 if not firsttime && nogeomcmds state.geomcmds
1992 then state.anchor <- getanchor ();
1994 conf.winw <- w;
1995 let w = truncate (float w *. conf.zoom) - state.scrollw in
1996 let w = max w 2 in
1997 conf.winh <- h;
1998 setfontsize fstate.fontsize;
1999 GlMat.mode `modelview;
2000 GlMat.load_identity ();
2002 GlMat.mode `projection;
2003 GlMat.load_identity ();
2004 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2005 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2006 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2008 let relx =
2009 if conf.zoom <= 1.0
2010 then 0.0
2011 else float state.x /. float state.w
2013 invalidate "geometry"
2014 (fun () ->
2015 state.w <- w;
2016 if not firsttime
2017 then state.x <- truncate (relx *. float w);
2018 let w =
2019 match conf.columns with
2020 | Csingle -> w
2021 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2022 | Csplit (c, _) -> w * c
2024 wcmd "geometry %d %d" w h);
2027 let enttext () =
2028 let len = String.length state.text in
2029 let drawstring s =
2030 let hscrollh =
2031 match state.mode with
2032 | Textentry _
2033 | View ->
2034 let h, _, _ = state.uioh#scrollpw in
2036 | _ -> 0
2038 let rect x w =
2039 GlDraw.rect
2040 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2041 (x+.w, float (conf.winh - hscrollh))
2044 let w = float (conf.winw - state.scrollw - 1) in
2045 if state.progress >= 0.0 && state.progress < 1.0
2046 then (
2047 GlDraw.color (0.3, 0.3, 0.3);
2048 let w1 = w *. state.progress in
2049 rect 0.0 w1;
2050 GlDraw.color (0.0, 0.0, 0.0);
2051 rect w1 (w-.w1)
2053 else (
2054 GlDraw.color (0.0, 0.0, 0.0);
2055 rect 0.0 w;
2058 GlDraw.color (1.0, 1.0, 1.0);
2059 drawstring fstate.fontsize
2060 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2062 let s =
2063 match state.mode with
2064 | Textentry ((prefix, text, _, _, _, _), _) ->
2065 let s =
2066 if len > 0
2067 then
2068 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2069 else
2070 Printf.sprintf "%s%s_" prefix text
2074 | _ -> state.text
2076 let s =
2077 if state.newerrmsgs
2078 then (
2079 if not (istextentry state.mode)
2080 then
2081 let s1 = "(press 'e' to review error messasges)" in
2082 if String.length s > 0 then s ^ " " ^ s1 else s1
2083 else s
2085 else s
2087 if String.length s > 0
2088 then drawstring s
2091 let gctiles () =
2092 let len = Queue.length state.tilelru in
2093 let rec loop qpos =
2094 if state.memused <= conf.memlimit
2095 then ()
2096 else (
2097 if qpos < len
2098 then
2099 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2100 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2101 let (_, pw, ph, _) = getpagedim n in
2103 gen = state.gen
2104 && colorspace = conf.colorspace
2105 && angle = conf.angle
2106 && pagew = pw
2107 && pageh = ph
2108 && (
2109 let layout =
2110 match state.throttle with
2111 | None ->
2112 if conf.preload
2113 then preloadlayout state.layout
2114 else state.layout
2115 | Some (layout, _, _) ->
2116 layout
2118 let x = col*conf.tilew
2119 and y = row*conf.tileh in
2120 tilevisible layout n x y
2122 then Queue.push lruitem state.tilelru
2123 else (
2124 wcmd "freetile %s" p;
2125 state.memused <- state.memused - s;
2126 state.uioh#infochanged Memused;
2127 Hashtbl.remove state.tilemap k;
2129 loop (qpos+1)
2132 loop 0
2135 let flushtiles () =
2136 Queue.iter (fun (k, p, s) ->
2137 wcmd "freetile %s" p;
2138 state.memused <- state.memused - s;
2139 state.uioh#infochanged Memused;
2140 Hashtbl.remove state.tilemap k;
2141 ) state.tilelru;
2142 Queue.clear state.tilelru;
2143 load state.layout;
2146 let logcurrently = function
2147 | Idle -> dolog "Idle"
2148 | Loading (l, gen) ->
2149 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2150 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2151 dolog
2152 "Tiling %d[%d,%d] page=%s cs=%s angle"
2153 l.pageno col row pageopaque
2154 (colorspace_to_string colorspace)
2156 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2157 angle gen conf.angle state.gen
2158 tilew tileh
2159 conf.tilew conf.tileh
2161 | Outlining _ ->
2162 dolog "outlining"
2165 let act cmds =
2166 (* dolog "%S" cmds; *)
2167 let op, args =
2168 let spacepos =
2169 try String.index cmds ' '
2170 with Not_found -> -1
2172 if spacepos = -1
2173 then cmds, ""
2174 else
2175 let l = String.length cmds in
2176 let op = String.sub cmds 0 spacepos in
2177 op, begin
2178 if l - spacepos < 2 then ""
2179 else String.sub cmds (spacepos+1) (l-spacepos-1)
2182 match op with
2183 | "clear" ->
2184 state.uioh#infochanged Pdim;
2185 state.pdims <- [];
2187 | "clearrects" ->
2188 state.rects <- state.rects1;
2189 G.postRedisplay "clearrects";
2191 | "continue" ->
2192 let n =
2193 try Scanf.sscanf args "%u" (fun n -> n)
2194 with exn ->
2195 dolog "error processing 'continue' %S: %s"
2196 cmds (Printexc.to_string exn);
2197 exit 1;
2199 state.pagecount <- n;
2200 begin match state.currently with
2201 | Outlining l ->
2202 state.currently <- Idle;
2203 state.outlines <- Array.of_list (List.rev l)
2204 | _ -> ()
2205 end;
2207 let cur, cmds = state.geomcmds in
2208 if String.length cur = 0
2209 then failwith "umpossible";
2211 begin match List.rev cmds with
2212 | [] ->
2213 state.geomcmds <- "", [];
2214 represent ();
2215 | (s, f) :: rest ->
2216 f ();
2217 state.geomcmds <- s, List.rev rest;
2218 end;
2219 if conf.maxwait = None
2220 then G.postRedisplay "continue";
2222 | "title" ->
2223 Wsi.settitle args
2225 | "msg" ->
2226 showtext ' ' args
2228 | "vmsg" ->
2229 if conf.verbose
2230 then showtext ' ' args
2232 | "progress" ->
2233 let progress, text =
2235 Scanf.sscanf args "%f %n"
2236 (fun f pos ->
2237 f, String.sub args pos (String.length args - pos))
2238 with exn ->
2239 dolog "error processing 'progress' %S: %s"
2240 cmds (Printexc.to_string exn);
2241 exit 1;
2243 state.text <- text;
2244 state.progress <- progress;
2245 G.postRedisplay "progress"
2247 | "firstmatch" ->
2248 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2250 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2251 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2252 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2253 with exn ->
2254 dolog "error processing 'firstmatch' %S: %s"
2255 cmds (Printexc.to_string exn);
2256 exit 1;
2258 let y = (getpagey pageno) + truncate y0 in
2259 addnav ();
2260 gotoy y;
2261 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2263 | "match" ->
2264 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2266 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2267 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2268 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2269 with exn ->
2270 dolog "error processing 'match' %S: %s"
2271 cmds (Printexc.to_string exn);
2272 exit 1;
2274 state.rects1 <-
2275 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2277 | "page" ->
2278 let pageopaque, t =
2280 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2281 with exn ->
2282 dolog "error processing 'page' %S: %s"
2283 cmds (Printexc.to_string exn);
2284 exit 1;
2286 begin match state.currently with
2287 | Loading (l, gen) ->
2288 vlog "page %d took %f sec" l.pageno t;
2289 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2290 begin match state.throttle with
2291 | None ->
2292 let preloadedpages =
2293 if conf.preload
2294 then preloadlayout state.layout
2295 else state.layout
2297 let evict () =
2298 let module IntSet =
2299 Set.Make (struct type t = int let compare = (-) end) in
2300 let set =
2301 List.fold_left (fun s l -> IntSet.add l.pageno s)
2302 IntSet.empty preloadedpages
2304 let evictedpages =
2305 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2306 if not (IntSet.mem pageno set)
2307 then (
2308 wcmd "freepage %s" opaque;
2309 key :: accu
2311 else accu
2312 ) state.pagemap []
2314 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2316 evict ();
2317 state.currently <- Idle;
2318 if gen = state.gen
2319 then (
2320 tilepage l.pageno pageopaque state.layout;
2321 load state.layout;
2322 load preloadedpages;
2323 if pagevisible state.layout l.pageno
2324 && layoutready state.layout
2325 then G.postRedisplay "page";
2328 | Some (layout, _, _) ->
2329 state.currently <- Idle;
2330 tilepage l.pageno pageopaque layout;
2331 load state.layout
2332 end;
2334 | _ ->
2335 dolog "Inconsistent loading state";
2336 logcurrently state.currently;
2337 exit 1
2340 | "tile" ->
2341 let (x, y, opaque, size, t) =
2343 Scanf.sscanf args "%u %u %s %u %f"
2344 (fun x y p size t -> (x, y, p, size, t))
2345 with exn ->
2346 dolog "error processing 'tile' %S: %s"
2347 cmds (Printexc.to_string exn);
2348 exit 1;
2350 begin match state.currently with
2351 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2352 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2354 if tilew != conf.tilew || tileh != conf.tileh
2355 then (
2356 wcmd "freetile %s" opaque;
2357 state.currently <- Idle;
2358 load state.layout;
2360 else (
2361 puttileopaque l col row gen cs angle opaque size t;
2362 state.memused <- state.memused + size;
2363 state.uioh#infochanged Memused;
2364 gctiles ();
2365 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2366 opaque, size) state.tilelru;
2368 let layout =
2369 match state.throttle with
2370 | None -> state.layout
2371 | Some (layout, _, _) -> layout
2374 state.currently <- Idle;
2375 if gen = state.gen
2376 && conf.colorspace = cs
2377 && conf.angle = angle
2378 && tilevisible layout l.pageno x y
2379 then conttiling l.pageno pageopaque;
2381 begin match state.throttle with
2382 | None ->
2383 preload state.layout;
2384 if gen = state.gen
2385 && conf.colorspace = cs
2386 && conf.angle = angle
2387 && tilevisible state.layout l.pageno x y
2388 then G.postRedisplay "tile nothrottle";
2390 | Some (layout, y, _) ->
2391 let ready = layoutready layout in
2392 if ready
2393 then (
2394 state.y <- y;
2395 state.layout <- layout;
2396 state.throttle <- None;
2397 G.postRedisplay "throttle";
2399 else load layout;
2400 end;
2403 | _ ->
2404 dolog "Inconsistent tiling state";
2405 logcurrently state.currently;
2406 exit 1
2409 | "pdim" ->
2410 let pdim =
2412 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2413 with exn ->
2414 dolog "error processing 'pdim' %S: %s"
2415 cmds (Printexc.to_string exn);
2416 exit 1;
2418 state.uioh#infochanged Pdim;
2419 state.pdims <- pdim :: state.pdims
2421 | "o" ->
2422 let (l, n, t, h, pos) =
2424 Scanf.sscanf args "%u %u %d %u %n"
2425 (fun l n t h pos -> l, n, t, h, pos)
2426 with exn ->
2427 dolog "error processing 'o' %S: %s"
2428 cmds (Printexc.to_string exn);
2429 exit 1;
2431 let s = String.sub args pos (String.length args - pos) in
2432 let outline = (s, l, (n, float t /. float h)) in
2433 begin match state.currently with
2434 | Outlining outlines ->
2435 state.currently <- Outlining (outline :: outlines)
2436 | Idle ->
2437 state.currently <- Outlining [outline]
2438 | currently ->
2439 dolog "invalid outlining state";
2440 logcurrently currently
2443 | "info" ->
2444 state.docinfo <- (1, args) :: state.docinfo
2446 | "infoend" ->
2447 state.uioh#infochanged Docinfo;
2448 state.docinfo <- List.rev state.docinfo
2450 | _ ->
2451 dolog "unknown cmd `%S'" cmds
2454 let onhist cb =
2455 let rc = cb.rc in
2456 let action = function
2457 | HCprev -> cbget cb ~-1
2458 | HCnext -> cbget cb 1
2459 | HCfirst -> cbget cb ~-(cb.rc)
2460 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2461 and cancel () = cb.rc <- rc
2462 in (action, cancel)
2465 let search pattern forward =
2466 if String.length pattern > 0
2467 then
2468 let pn, py =
2469 match state.layout with
2470 | [] -> 0, 0
2471 | l :: _ ->
2472 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2474 wcmd "search %d %d %d %d,%s\000"
2475 (btod conf.icase) pn py (btod forward) pattern;
2478 let intentry text key =
2479 let c =
2480 if key >= 32 && key < 127
2481 then Char.chr key
2482 else '\000'
2484 match c with
2485 | '0' .. '9' ->
2486 let text = addchar text c in
2487 TEcont text
2489 | _ ->
2490 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2491 TEcont text
2494 let linknentry text key =
2495 let c =
2496 if key >= 32 && key < 127
2497 then Char.chr key
2498 else '\000'
2500 match c with
2501 | 'a' .. 'z' ->
2502 let text = addchar text c in
2503 TEcont text
2505 | _ ->
2506 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2507 TEcont text
2510 let linkndone f s =
2511 if String.length s > 0
2512 then (
2513 let n =
2514 let l = String.length s in
2515 let rec loop pos n = if pos = l then n else
2516 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2517 loop (pos+1) (n*26 + m)
2518 in loop 0 0
2520 let rec loop n = function
2521 | [] -> ()
2522 | l :: rest ->
2523 match getopaque l.pageno with
2524 | None -> loop n rest
2525 | Some opaque ->
2526 let m = getlinkcount opaque in
2527 if n < m
2528 then (
2529 let under = getlink opaque n in
2530 f under
2532 else loop (n-m) rest
2534 loop n state.layout;
2538 let textentry text key =
2539 if key land 0xff00 = 0xff00
2540 then TEcont text
2541 else TEcont (text ^ Wsi.toutf8 key)
2544 let reqlayout angle proportional =
2545 match state.throttle with
2546 | None ->
2547 if nogeomcmds state.geomcmds
2548 then state.anchor <- getanchor ();
2549 conf.angle <- angle mod 360;
2550 if conf.angle != 0
2551 then (
2552 match state.mode with
2553 | LinkNav _ -> state.mode <- View
2554 | _ -> ()
2556 conf.proportional <- proportional;
2557 invalidate "reqlayout"
2558 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2559 | _ -> ()
2562 let settrim trimmargins trimfuzz =
2563 if nogeomcmds state.geomcmds
2564 then state.anchor <- getanchor ();
2565 conf.trimmargins <- trimmargins;
2566 conf.trimfuzz <- trimfuzz;
2567 let x0, y0, x1, y1 = trimfuzz in
2568 invalidate "settrim"
2569 (fun () ->
2570 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2571 Hashtbl.iter (fun _ opaque ->
2572 wcmd "freepage %s" opaque;
2573 ) state.pagemap;
2574 Hashtbl.clear state.pagemap;
2577 let setzoom zoom =
2578 match state.throttle with
2579 | None ->
2580 let zoom = max 0.01 zoom in
2581 if zoom <> conf.zoom
2582 then (
2583 state.prevzoom <- conf.zoom;
2584 conf.zoom <- zoom;
2585 reshape conf.winw conf.winh;
2586 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2589 | Some (layout, y, started) ->
2590 let time =
2591 match conf.maxwait with
2592 | None -> 0.0
2593 | Some t -> t
2595 let dt = now () -. started in
2596 if dt > time
2597 then (
2598 state.y <- y;
2599 load layout;
2603 let setcolumns mode columns coverA coverB =
2604 state.prevcolumns <- Some (conf.columns, conf.zoom);
2605 if columns < 0
2606 then (
2607 if isbirdseye mode
2608 then showtext '!' "split mode doesn't work in bird's eye"
2609 else (
2610 conf.columns <- Csplit (-columns, [||]);
2611 state.x <- 0;
2612 conf.zoom <- 1.0;
2615 else (
2616 if columns < 2
2617 then (
2618 conf.columns <- Csingle;
2619 state.x <- 0;
2620 setzoom 1.0;
2622 else (
2623 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2624 conf.zoom <- 1.0;
2627 reshape conf.winw conf.winh;
2630 let enterbirdseye () =
2631 let zoom = float conf.thumbw /. float conf.winw in
2632 let birdseyepageno =
2633 let cy = conf.winh / 2 in
2634 let fold = function
2635 | [] -> 0
2636 | l :: rest ->
2637 let rec fold best = function
2638 | [] -> best.pageno
2639 | l :: rest ->
2640 let d = cy - (l.pagedispy + l.pagevh/2)
2641 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2642 if abs d < abs dbest
2643 then fold l rest
2644 else best.pageno
2645 in fold l rest
2647 fold state.layout
2649 state.mode <- Birdseye (
2650 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2652 conf.zoom <- zoom;
2653 conf.presentation <- false;
2654 conf.interpagespace <- 10;
2655 conf.hlinks <- false;
2656 state.x <- 0;
2657 state.mstate <- Mnone;
2658 conf.maxwait <- None;
2659 conf.columns <- (
2660 match conf.beyecolumns with
2661 | Some c ->
2662 conf.zoom <- 1.0;
2663 Cmulti ((c, 0, 0), [||])
2664 | None -> Csingle
2666 Wsi.setcursor Wsi.CURSOR_INHERIT;
2667 if conf.verbose
2668 then
2669 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2670 (100.0*.zoom)
2671 else
2672 state.text <- ""
2674 reshape conf.winw conf.winh;
2677 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2678 state.mode <- View;
2679 conf.zoom <- c.zoom;
2680 conf.presentation <- c.presentation;
2681 conf.interpagespace <- c.interpagespace;
2682 conf.maxwait <- c.maxwait;
2683 conf.hlinks <- c.hlinks;
2684 conf.beyecolumns <- (
2685 match conf.columns with
2686 | Cmulti ((c, _, _), _) -> Some c
2687 | Csingle -> None
2688 | Csplit _ -> failwith "leaving bird's eye split mode"
2690 conf.columns <- (
2691 match c.columns with
2692 | Cmulti (c, _) -> Cmulti (c, [||])
2693 | Csingle -> Csingle
2694 | Csplit (c, _) -> Csplit (c, [||])
2696 state.x <- leftx;
2697 if conf.verbose
2698 then
2699 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2700 (100.0*.conf.zoom)
2702 reshape conf.winw conf.winh;
2703 state.anchor <- if goback then anchor else (pageno, 0.0);
2706 let togglebirdseye () =
2707 match state.mode with
2708 | Birdseye vals -> leavebirdseye vals true
2709 | View -> enterbirdseye ()
2710 | _ -> ()
2713 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2714 let pageno = max 0 (pageno - incr) in
2715 let rec loop = function
2716 | [] -> gotopage1 pageno 0
2717 | l :: _ when l.pageno = pageno ->
2718 if l.pagedispy >= 0 && l.pagey = 0
2719 then G.postRedisplay "upbirdseye"
2720 else gotopage1 pageno 0
2721 | _ :: rest -> loop rest
2723 loop state.layout;
2724 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2727 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2728 let pageno = min (state.pagecount - 1) (pageno + incr) in
2729 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2730 let rec loop = function
2731 | [] ->
2732 let y, h = getpageyh pageno in
2733 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2734 gotoy (clamp dy)
2735 | l :: _ when l.pageno = pageno ->
2736 if l.pagevh != l.pageh
2737 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2738 else G.postRedisplay "downbirdseye"
2739 | _ :: rest -> loop rest
2741 loop state.layout
2744 let optentry mode _ key =
2745 let btos b = if b then "on" else "off" in
2746 if key >= 32 && key < 127
2747 then
2748 let c = Char.chr key in
2749 match c with
2750 | 's' ->
2751 let ondone s =
2752 try conf.scrollstep <- int_of_string s with exc ->
2753 state.text <- Printf.sprintf "bad integer `%s': %s"
2754 s (Printexc.to_string exc)
2756 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2758 | 'A' ->
2759 let ondone s =
2761 conf.autoscrollstep <- int_of_string s;
2762 if state.autoscroll <> None
2763 then state.autoscroll <- Some conf.autoscrollstep
2764 with exc ->
2765 state.text <- Printf.sprintf "bad integer `%s': %s"
2766 s (Printexc.to_string exc)
2768 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2770 | 'C' ->
2771 let ondone s =
2773 let n, a, b = multicolumns_of_string s in
2774 setcolumns mode n a b;
2775 with exc ->
2776 state.text <- Printf.sprintf "bad columns `%s': %s"
2777 s (Printexc.to_string exc)
2779 TEswitch ("columns: ", "", None, textentry, ondone, true)
2781 | 'Z' ->
2782 let ondone s =
2784 let zoom = float (int_of_string s) /. 100.0 in
2785 setzoom zoom
2786 with exc ->
2787 state.text <- Printf.sprintf "bad integer `%s': %s"
2788 s (Printexc.to_string exc)
2790 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2792 | 't' ->
2793 let ondone s =
2795 conf.thumbw <- bound (int_of_string s) 2 4096;
2796 state.text <-
2797 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2798 begin match mode with
2799 | Birdseye beye ->
2800 leavebirdseye beye false;
2801 enterbirdseye ();
2802 | _ -> ();
2804 with exc ->
2805 state.text <- Printf.sprintf "bad integer `%s': %s"
2806 s (Printexc.to_string exc)
2808 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2810 | 'R' ->
2811 let ondone s =
2812 match try
2813 Some (int_of_string s)
2814 with exc ->
2815 state.text <- Printf.sprintf "bad integer `%s': %s"
2816 s (Printexc.to_string exc);
2817 None
2818 with
2819 | Some angle -> reqlayout angle conf.proportional
2820 | None -> ()
2822 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2824 | 'i' ->
2825 conf.icase <- not conf.icase;
2826 TEdone ("case insensitive search " ^ (btos conf.icase))
2828 | 'p' ->
2829 conf.preload <- not conf.preload;
2830 gotoy state.y;
2831 TEdone ("preload " ^ (btos conf.preload))
2833 | 'v' ->
2834 conf.verbose <- not conf.verbose;
2835 TEdone ("verbose " ^ (btos conf.verbose))
2837 | 'd' ->
2838 conf.debug <- not conf.debug;
2839 TEdone ("debug " ^ (btos conf.debug))
2841 | 'h' ->
2842 conf.maxhfit <- not conf.maxhfit;
2843 state.maxy <- calcheight ();
2844 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2846 | 'c' ->
2847 conf.crophack <- not conf.crophack;
2848 TEdone ("crophack " ^ btos conf.crophack)
2850 | 'a' ->
2851 let s =
2852 match conf.maxwait with
2853 | None ->
2854 conf.maxwait <- Some infinity;
2855 "always wait for page to complete"
2856 | Some _ ->
2857 conf.maxwait <- None;
2858 "show placeholder if page is not ready"
2860 TEdone s
2862 | 'f' ->
2863 conf.underinfo <- not conf.underinfo;
2864 TEdone ("underinfo " ^ btos conf.underinfo)
2866 | 'P' ->
2867 conf.savebmarks <- not conf.savebmarks;
2868 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2870 | 'S' ->
2871 let ondone s =
2873 let pageno, py =
2874 match state.layout with
2875 | [] -> 0, 0
2876 | l :: _ ->
2877 l.pageno, l.pagey
2879 conf.interpagespace <- int_of_string s;
2880 docolumns conf.columns;
2881 state.maxy <- calcheight ();
2882 let y = getpagey pageno in
2883 gotoy (y + py)
2884 with exc ->
2885 state.text <- Printf.sprintf "bad integer `%s': %s"
2886 s (Printexc.to_string exc)
2888 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2890 | 'l' ->
2891 reqlayout conf.angle (not conf.proportional);
2892 TEdone ("proportional display " ^ btos conf.proportional)
2894 | 'T' ->
2895 settrim (not conf.trimmargins) conf.trimfuzz;
2896 TEdone ("trim margins " ^ btos conf.trimmargins)
2898 | 'I' ->
2899 conf.invert <- not conf.invert;
2900 TEdone ("invert colors " ^ btos conf.invert)
2902 | 'x' ->
2903 let ondone s =
2904 cbput state.hists.sel s;
2905 conf.selcmd <- s;
2907 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2908 textentry, ondone, true)
2910 | _ ->
2911 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2912 TEstop
2913 else
2914 TEcont state.text
2917 class type lvsource = object
2918 method getitemcount : int
2919 method getitem : int -> (string * int)
2920 method hasaction : int -> bool
2921 method exit :
2922 uioh:uioh ->
2923 cancel:bool ->
2924 active:int ->
2925 first:int ->
2926 pan:int ->
2927 qsearch:string ->
2928 uioh option
2929 method getactive : int
2930 method getfirst : int
2931 method getqsearch : string
2932 method setqsearch : string -> unit
2933 method getpan : int
2934 end;;
2936 class virtual lvsourcebase = object
2937 val mutable m_active = 0
2938 val mutable m_first = 0
2939 val mutable m_qsearch = ""
2940 val mutable m_pan = 0
2941 method getactive = m_active
2942 method getfirst = m_first
2943 method getqsearch = m_qsearch
2944 method getpan = m_pan
2945 method setqsearch s = m_qsearch <- s
2946 end;;
2948 let withoutlastutf8 s =
2949 let len = String.length s in
2950 if len = 0
2951 then s
2952 else
2953 let rec find pos =
2954 if pos = 0
2955 then pos
2956 else
2957 let b = Char.code s.[pos] in
2958 if b land 0b110000 = 0b11000000
2959 then find (pos-1)
2960 else pos-1
2962 let first =
2963 if Char.code s.[len-1] land 0x80 = 0
2964 then len-1
2965 else find (len-1)
2967 String.sub s 0 first;
2970 let textentrykeyboard
2971 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2972 let enttext te =
2973 state.mode <- Textentry (te, onleave);
2974 state.text <- "";
2975 enttext ();
2976 G.postRedisplay "textentrykeyboard enttext";
2978 let histaction cmd =
2979 match opthist with
2980 | None -> ()
2981 | Some (action, _) ->
2982 state.mode <- Textentry (
2983 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2985 G.postRedisplay "textentry histaction"
2987 match key with
2988 | 0xff08 -> (* backspace *)
2989 let s = withoutlastutf8 text in
2990 let len = String.length s in
2991 if cancelonempty && len = 0
2992 then (
2993 onleave Cancel;
2994 G.postRedisplay "textentrykeyboard after cancel";
2996 else (
2997 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3000 | 0xff0d ->
3001 ondone text;
3002 onleave Confirm;
3003 G.postRedisplay "textentrykeyboard after confirm"
3005 | 0xff52 -> histaction HCprev
3006 | 0xff54 -> histaction HCnext
3007 | 0xff50 -> histaction HCfirst
3008 | 0xff57 -> histaction HClast
3010 | 0xff1b -> (* escape*)
3011 if String.length text = 0
3012 then (
3013 begin match opthist with
3014 | None -> ()
3015 | Some (_, onhistcancel) -> onhistcancel ()
3016 end;
3017 onleave Cancel;
3018 state.text <- "";
3019 G.postRedisplay "textentrykeyboard after cancel2"
3021 else (
3022 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3025 | 0xff9f | 0xffff -> () (* delete *)
3027 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3028 begin match onkey text key with
3029 | TEdone text ->
3030 ondone text;
3031 onleave Confirm;
3032 G.postRedisplay "textentrykeyboard after confirm2";
3034 | TEcont text ->
3035 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3037 | TEstop ->
3038 onleave Cancel;
3039 G.postRedisplay "textentrykeyboard after cancel3"
3041 | TEswitch te ->
3042 state.mode <- Textentry (te, onleave);
3043 G.postRedisplay "textentrykeyboard switch";
3044 end;
3046 | _ ->
3047 vlog "unhandled key %s" (Wsi.keyname key)
3050 let firstof first active =
3051 if first > active || abs (first - active) > fstate.maxrows - 1
3052 then max 0 (active - (fstate.maxrows/2))
3053 else first
3056 let calcfirst first active =
3057 if active > first
3058 then
3059 let rows = active - first in
3060 if rows > fstate.maxrows then active - fstate.maxrows else first
3061 else active
3064 let scrollph y maxy =
3065 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3066 let sh = float conf.winh /. sh in
3067 let sh = max sh (float conf.scrollh) in
3069 let percent =
3070 if y = state.maxy
3071 then 1.0
3072 else float y /. float maxy
3074 let position = (float conf.winh -. sh) *. percent in
3076 let position =
3077 if position +. sh > float conf.winh
3078 then float conf.winh -. sh
3079 else position
3081 position, sh;
3084 let coe s = (s :> uioh);;
3086 class listview ~(source:lvsource) ~trusted ~modehash =
3087 object (self)
3088 val m_pan = source#getpan
3089 val m_first = source#getfirst
3090 val m_active = source#getactive
3091 val m_qsearch = source#getqsearch
3092 val m_prev_uioh = state.uioh
3094 method private elemunder y =
3095 let n = y / (fstate.fontsize+1) in
3096 if m_first + n < source#getitemcount
3097 then (
3098 if source#hasaction (m_first + n)
3099 then Some (m_first + n)
3100 else None
3102 else None
3104 method display =
3105 Gl.enable `blend;
3106 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3107 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3108 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3109 GlDraw.color (1., 1., 1.);
3110 Gl.enable `texture_2d;
3111 let fs = fstate.fontsize in
3112 let nfs = fs + 1 in
3113 let ww = fstate.wwidth in
3114 let tabw = 30.0*.ww in
3115 let itemcount = source#getitemcount in
3116 let rec loop row =
3117 if (row - m_first) * nfs > conf.winh
3118 then ()
3119 else (
3120 if row >= 0 && row < itemcount
3121 then (
3122 let (s, level) = source#getitem row in
3123 let y = (row - m_first) * nfs in
3124 let x = 5.0 +. float (level + m_pan) *. ww in
3125 if row = m_active
3126 then (
3127 Gl.disable `texture_2d;
3128 GlDraw.polygon_mode `both `line;
3129 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3130 GlDraw.rect (1., float (y + 1))
3131 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3132 GlDraw.polygon_mode `both `fill;
3133 GlDraw.color (1., 1., 1.);
3134 Gl.enable `texture_2d;
3137 let drawtabularstring s =
3138 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3139 if trusted
3140 then
3141 let tabpos = try String.index s '\t' with Not_found -> -1 in
3142 if tabpos > 0
3143 then
3144 let len = String.length s - tabpos - 1 in
3145 let s1 = String.sub s 0 tabpos
3146 and s2 = String.sub s (tabpos + 1) len in
3147 let nx = drawstr x s1 in
3148 let sw = nx -. x in
3149 let x = x +. (max tabw sw) in
3150 drawstr x s2
3151 else
3152 drawstr x s
3153 else
3154 drawstr x s
3156 let _ = drawtabularstring s in
3157 loop (row+1)
3161 loop m_first;
3162 Gl.disable `blend;
3163 Gl.disable `texture_2d;
3165 method updownlevel incr =
3166 let len = source#getitemcount in
3167 let curlevel =
3168 if m_active >= 0 && m_active < len
3169 then snd (source#getitem m_active)
3170 else -1
3172 let rec flow i =
3173 if i = len then i-1 else if i = -1 then 0 else
3174 let _, l = source#getitem i in
3175 if l != curlevel then i else flow (i+incr)
3177 let active = flow m_active in
3178 let first = calcfirst m_first active in
3179 G.postRedisplay "outline updownlevel";
3180 {< m_active = active; m_first = first >}
3182 method private key1 key mask =
3183 let set1 active first qsearch =
3184 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3186 let search active pattern incr =
3187 let dosearch re =
3188 let rec loop n =
3189 if n >= 0 && n < source#getitemcount
3190 then (
3191 let s, _ = source#getitem n in
3193 (try ignore (Str.search_forward re s 0); true
3194 with Not_found -> false)
3195 then Some n
3196 else loop (n + incr)
3198 else None
3200 loop active
3203 let re = Str.regexp_case_fold pattern in
3204 dosearch re
3205 with Failure s ->
3206 state.text <- s;
3207 None
3209 let itemcount = source#getitemcount in
3210 let find start incr =
3211 let rec find i =
3212 if i = -1 || i = itemcount
3213 then -1
3214 else (
3215 if source#hasaction i
3216 then i
3217 else find (i + incr)
3220 find start
3222 let set active first =
3223 let first = bound first 0 (itemcount - fstate.maxrows) in
3224 state.text <- "";
3225 coe {< m_active = active; m_first = first >}
3227 let navigate incr =
3228 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3229 let active, first =
3230 let incr1 = if incr > 0 then 1 else -1 in
3231 if isvisible m_first m_active
3232 then
3233 let next =
3234 let next = m_active + incr in
3235 let next =
3236 if next < 0 || next >= itemcount
3237 then -1
3238 else find next incr1
3240 if next = -1 || abs (m_active - next) > fstate.maxrows
3241 then -1
3242 else next
3244 if next = -1
3245 then
3246 let first = m_first + incr in
3247 let first = bound first 0 (itemcount - 1) in
3248 let next =
3249 let next = m_active + incr in
3250 let next = bound next 0 (itemcount - 1) in
3251 find next ~-incr1
3253 let active = if next = -1 then m_active else next in
3254 active, first
3255 else
3256 let first = min next m_first in
3257 let first =
3258 if abs (next - first) > fstate.maxrows
3259 then first + incr
3260 else first
3262 next, first
3263 else
3264 let first = m_first + incr in
3265 let first = bound first 0 (itemcount - 1) in
3266 let active =
3267 let next = m_active + incr in
3268 let next = bound next 0 (itemcount - 1) in
3269 let next = find next incr1 in
3270 let active =
3271 if next = -1 || abs (m_active - first) > fstate.maxrows
3272 then (
3273 let active = if m_active = -1 then next else m_active in
3274 active
3276 else next
3278 if isvisible first active
3279 then active
3280 else -1
3282 active, first
3284 G.postRedisplay "listview navigate";
3285 set active first;
3287 match key with
3288 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3289 let incr = if key = 0x72 then -1 else 1 in
3290 let active, first =
3291 match search (m_active + incr) m_qsearch incr with
3292 | None ->
3293 state.text <- m_qsearch ^ " [not found]";
3294 m_active, m_first
3295 | Some active ->
3296 state.text <- m_qsearch;
3297 active, firstof m_first active
3299 G.postRedisplay "listview ctrl-r/s";
3300 set1 active first m_qsearch;
3302 | 0xff08 -> (* backspace *)
3303 if String.length m_qsearch = 0
3304 then coe self
3305 else (
3306 let qsearch = withoutlastutf8 m_qsearch in
3307 let len = String.length qsearch in
3308 if len = 0
3309 then (
3310 state.text <- "";
3311 G.postRedisplay "listview empty qsearch";
3312 set1 m_active m_first "";
3314 else
3315 let active, first =
3316 match search m_active qsearch ~-1 with
3317 | None ->
3318 state.text <- qsearch ^ " [not found]";
3319 m_active, m_first
3320 | Some active ->
3321 state.text <- qsearch;
3322 active, firstof m_first active
3324 G.postRedisplay "listview backspace qsearch";
3325 set1 active first qsearch
3328 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3329 let pattern = m_qsearch ^ Wsi.toutf8 key in
3330 let active, first =
3331 match search m_active pattern 1 with
3332 | None ->
3333 state.text <- pattern ^ " [not found]";
3334 m_active, m_first
3335 | Some active ->
3336 state.text <- pattern;
3337 active, firstof m_first active
3339 G.postRedisplay "listview qsearch add";
3340 set1 active first pattern;
3342 | 0xff1b -> (* escape *)
3343 state.text <- "";
3344 if String.length m_qsearch = 0
3345 then (
3346 G.postRedisplay "list view escape";
3347 begin
3348 match
3349 source#exit (coe self) true m_active m_first m_pan m_qsearch
3350 with
3351 | None -> m_prev_uioh
3352 | Some uioh -> uioh
3355 else (
3356 G.postRedisplay "list view kill qsearch";
3357 source#setqsearch "";
3358 coe {< m_qsearch = "" >}
3361 | 0xff0d -> (* return *)
3362 state.text <- "";
3363 let self = {< m_qsearch = "" >} in
3364 source#setqsearch "";
3365 let opt =
3366 G.postRedisplay "listview enter";
3367 if m_active >= 0 && m_active < source#getitemcount
3368 then (
3369 source#exit (coe self) false m_active m_first m_pan "";
3371 else (
3372 source#exit (coe self) true m_active m_first m_pan "";
3375 begin match opt with
3376 | None -> m_prev_uioh
3377 | Some uioh -> uioh
3380 | 0xff9f | 0xffff -> (* delete *)
3381 coe self
3383 | 0xff52 -> navigate ~-1 (* up *)
3384 | 0xff54 -> navigate 1 (* down *)
3385 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3386 | 0xff56 -> navigate fstate.maxrows (* next *)
3388 | 0xff53 -> (* right *)
3389 state.text <- "";
3390 G.postRedisplay "listview right";
3391 coe {< m_pan = m_pan - 1 >}
3393 | 0xff51 -> (* left *)
3394 state.text <- "";
3395 G.postRedisplay "listview left";
3396 coe {< m_pan = m_pan + 1 >}
3398 | 0xff50 -> (* home *)
3399 let active = find 0 1 in
3400 G.postRedisplay "listview home";
3401 set active 0;
3403 | 0xff57 -> (* end *)
3404 let first = max 0 (itemcount - fstate.maxrows) in
3405 let active = find (itemcount - 1) ~-1 in
3406 G.postRedisplay "listview end";
3407 set active first;
3409 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3410 coe self
3412 | _ ->
3413 dolog "listview unknown key %#x" key; coe self
3415 method key key mask =
3416 match state.mode with
3417 | Textentry te -> textentrykeyboard key mask te; coe self
3418 | _ -> self#key1 key mask
3420 method button button down x y _ =
3421 let opt =
3422 match button with
3423 | 1 when x > conf.winw - conf.scrollbw ->
3424 G.postRedisplay "listview scroll";
3425 if down
3426 then
3427 let _, position, sh = self#scrollph in
3428 if y > truncate position && y < truncate (position +. sh)
3429 then (
3430 state.mstate <- Mscrolly;
3431 Some (coe self)
3433 else
3434 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3435 let first = truncate (s *. float source#getitemcount) in
3436 let first = min source#getitemcount first in
3437 Some (coe {< m_first = first; m_active = first >})
3438 else (
3439 state.mstate <- Mnone;
3440 Some (coe self);
3442 | 1 when not down ->
3443 begin match self#elemunder y with
3444 | Some n ->
3445 G.postRedisplay "listview click";
3446 source#exit
3447 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3448 | _ ->
3449 Some (coe self)
3451 | n when (n == 4 || n == 5) && not down ->
3452 let len = source#getitemcount in
3453 let first =
3454 if n = 5 && m_first + fstate.maxrows >= len
3455 then
3456 m_first
3457 else
3458 let first = m_first + (if n == 4 then -1 else 1) in
3459 bound first 0 (len - 1)
3461 G.postRedisplay "listview wheel";
3462 Some (coe {< m_first = first >})
3463 | n when (n = 6 || n = 7) && not down ->
3464 let inc = m_first + (if n = 7 then -1 else 1) in
3465 G.postRedisplay "listview hwheel";
3466 Some (coe {< m_pan = m_pan + inc >})
3467 | _ ->
3468 Some (coe self)
3470 match opt with
3471 | None -> m_prev_uioh
3472 | Some uioh -> uioh
3474 method motion _ y =
3475 match state.mstate with
3476 | Mscrolly ->
3477 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3478 let first = truncate (s *. float source#getitemcount) in
3479 let first = min source#getitemcount first in
3480 G.postRedisplay "listview motion";
3481 coe {< m_first = first; m_active = first >}
3482 | _ -> coe self
3484 method pmotion x y =
3485 if x < conf.winw - conf.scrollbw
3486 then
3487 let n =
3488 match self#elemunder y with
3489 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3490 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3492 let o =
3493 if n != m_active
3494 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3495 else self
3497 coe o
3498 else (
3499 Wsi.setcursor Wsi.CURSOR_INHERIT;
3500 coe self
3503 method infochanged _ = ()
3505 method scrollpw = (0, 0.0, 0.0)
3506 method scrollph =
3507 let nfs = fstate.fontsize + 1 in
3508 let y = m_first * nfs in
3509 let itemcount = source#getitemcount in
3510 let maxi = max 0 (itemcount - fstate.maxrows) in
3511 let maxy = maxi * nfs in
3512 let p, h = scrollph y maxy in
3513 conf.scrollbw, p, h
3515 method modehash = modehash
3516 end;;
3518 class outlinelistview ~source =
3519 object (self)
3520 inherit listview
3521 ~source:(source :> lvsource)
3522 ~trusted:false
3523 ~modehash:(findkeyhash conf "outline")
3524 as super
3526 method key key mask =
3527 let calcfirst first active =
3528 if active > first
3529 then
3530 let rows = active - first in
3531 let maxrows =
3532 if String.length state.text = 0
3533 then fstate.maxrows
3534 else fstate.maxrows - 2
3536 if rows > maxrows then active - maxrows else first
3537 else active
3539 let navigate incr =
3540 let active = m_active + incr in
3541 let active = bound active 0 (source#getitemcount - 1) in
3542 let first = calcfirst m_first active in
3543 G.postRedisplay "outline navigate";
3544 coe {< m_active = active; m_first = first >}
3546 let ctrl = Wsi.withctrl mask in
3547 match key with
3548 | 110 when ctrl -> (* ctrl-n *)
3549 source#narrow m_qsearch;
3550 G.postRedisplay "outline ctrl-n";
3551 coe {< m_first = 0; m_active = 0 >}
3553 | 117 when ctrl -> (* ctrl-u *)
3554 source#denarrow;
3555 G.postRedisplay "outline ctrl-u";
3556 state.text <- "";
3557 coe {< m_first = 0; m_active = 0 >}
3559 | 108 when ctrl -> (* ctrl-l *)
3560 let first = m_active - (fstate.maxrows / 2) in
3561 G.postRedisplay "outline ctrl-l";
3562 coe {< m_first = first >}
3564 | 0xff9f | 0xffff -> (* delete *)
3565 source#remove m_active;
3566 G.postRedisplay "outline delete";
3567 let active = max 0 (m_active-1) in
3568 coe {< m_first = firstof m_first active;
3569 m_active = active >}
3571 | 0xff52 -> navigate ~-1 (* up *)
3572 | 0xff54 -> navigate 1 (* down *)
3573 | 0xff55 -> (* prior *)
3574 navigate ~-(fstate.maxrows)
3575 | 0xff56 -> (* next *)
3576 navigate fstate.maxrows
3578 | 0xff53 -> (* [ctrl-]right *)
3579 let o =
3580 if ctrl
3581 then (
3582 G.postRedisplay "outline ctrl right";
3583 {< m_pan = m_pan + 1 >}
3585 else self#updownlevel 1
3587 coe o
3589 | 0xff51 -> (* [ctrl-]left *)
3590 let o =
3591 if ctrl
3592 then (
3593 G.postRedisplay "outline ctrl left";
3594 {< m_pan = m_pan - 1 >}
3596 else self#updownlevel ~-1
3598 coe o
3600 | 0xff50 -> (* home *)
3601 G.postRedisplay "outline home";
3602 coe {< m_first = 0; m_active = 0 >}
3604 | 0xff57 -> (* end *)
3605 let active = source#getitemcount - 1 in
3606 let first = max 0 (active - fstate.maxrows) in
3607 G.postRedisplay "outline end";
3608 coe {< m_active = active; m_first = first >}
3610 | _ -> super#key key mask
3613 let outlinesource usebookmarks =
3614 let empty = [||] in
3615 (object
3616 inherit lvsourcebase
3617 val mutable m_items = empty
3618 val mutable m_orig_items = empty
3619 val mutable m_prev_items = empty
3620 val mutable m_narrow_pattern = ""
3621 val mutable m_hadremovals = false
3623 method getitemcount =
3624 Array.length m_items + (if m_hadremovals then 1 else 0)
3626 method getitem n =
3627 if n == Array.length m_items && m_hadremovals
3628 then
3629 ("[Confirm removal]", 0)
3630 else
3631 let s, n, _ = m_items.(n) in
3632 (s, n)
3634 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3635 ignore (uioh, first, qsearch);
3636 let confrimremoval = m_hadremovals && active = Array.length m_items in
3637 let items =
3638 if String.length m_narrow_pattern = 0
3639 then m_orig_items
3640 else m_items
3642 if not cancel
3643 then (
3644 if not confrimremoval
3645 then(
3646 let _, _, anchor = m_items.(active) in
3647 gotoanchor anchor;
3648 m_items <- items;
3650 else (
3651 state.bookmarks <- Array.to_list m_items;
3652 m_orig_items <- m_items;
3655 else m_items <- items;
3656 m_pan <- pan;
3657 None
3659 method hasaction _ = true
3661 method greetmsg =
3662 if Array.length m_items != Array.length m_orig_items
3663 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3664 else ""
3666 method narrow pattern =
3667 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3668 match reopt with
3669 | None -> ()
3670 | Some re ->
3671 let rec loop accu n =
3672 if n = -1
3673 then (
3674 m_narrow_pattern <- pattern;
3675 m_items <- Array.of_list accu
3677 else
3678 let (s, _, _) as o = m_items.(n) in
3679 let accu =
3680 if (try ignore (Str.search_forward re s 0); true
3681 with Not_found -> false)
3682 then o :: accu
3683 else accu
3685 loop accu (n-1)
3687 loop [] (Array.length m_items - 1)
3689 method denarrow =
3690 m_orig_items <- (
3691 if usebookmarks
3692 then Array.of_list state.bookmarks
3693 else state.outlines
3695 m_items <- m_orig_items
3697 method remove m =
3698 if usebookmarks
3699 then
3700 if m >= 0 && m < Array.length m_items
3701 then (
3702 m_hadremovals <- true;
3703 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3704 let n = if n >= m then n+1 else n in
3705 m_items.(n)
3709 method reset anchor items =
3710 m_hadremovals <- false;
3711 if m_orig_items == empty || m_prev_items != items
3712 then (
3713 m_orig_items <- items;
3714 if String.length m_narrow_pattern = 0
3715 then m_items <- items;
3717 m_prev_items <- items;
3718 let rely = getanchory anchor in
3719 let active =
3720 let rec loop n best bestd =
3721 if n = Array.length m_items
3722 then best
3723 else
3724 let (_, _, anchor) = m_items.(n) in
3725 let orely = getanchory anchor in
3726 let d = abs (orely - rely) in
3727 if d < bestd
3728 then loop (n+1) n d
3729 else loop (n+1) best bestd
3731 loop 0 ~-1 max_int
3733 m_active <- active;
3734 m_first <- firstof m_first active
3735 end)
3738 let enterselector usebookmarks =
3739 let source = outlinesource usebookmarks in
3740 fun errmsg ->
3741 let outlines =
3742 if usebookmarks
3743 then Array.of_list state.bookmarks
3744 else state.outlines
3746 if Array.length outlines = 0
3747 then (
3748 showtext ' ' errmsg;
3750 else (
3751 state.text <- source#greetmsg;
3752 Wsi.setcursor Wsi.CURSOR_INHERIT;
3753 let anchor = getanchor () in
3754 source#reset anchor outlines;
3755 state.uioh <- coe (new outlinelistview ~source);
3756 G.postRedisplay "enter selector";
3760 let enteroutlinemode =
3761 let f = enterselector false in
3762 fun ()-> f "Document has no outline";
3765 let enterbookmarkmode =
3766 let f = enterselector true in
3767 fun () -> f "Document has no bookmarks (yet)";
3770 let color_of_string s =
3771 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3772 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3776 let color_to_string (r, g, b) =
3777 let r = truncate (r *. 256.0)
3778 and g = truncate (g *. 256.0)
3779 and b = truncate (b *. 256.0) in
3780 Printf.sprintf "%d/%d/%d" r g b
3783 let irect_of_string s =
3784 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3787 let irect_to_string (x0,y0,x1,y1) =
3788 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3791 let makecheckers () =
3792 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3793 following to say:
3794 converted by Issac Trotts. July 25, 2002 *)
3795 let image_height = 64
3796 and image_width = 64 in
3798 let make_image () =
3799 let image =
3800 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3802 for i = 0 to image_width - 1 do
3803 for j = 0 to image_height - 1 do
3804 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3805 (if (i land 8 ) lxor (j land 8) = 0
3806 then [|255;255;255|] else [|200;200;200|])
3807 done
3808 done;
3809 image
3811 let image = make_image () in
3812 let id = GlTex.gen_texture () in
3813 GlTex.bind_texture `texture_2d id;
3814 GlPix.store (`unpack_alignment 1);
3815 GlTex.image2d image;
3816 List.iter (GlTex.parameter ~target:`texture_2d)
3817 [ `wrap_s `repeat;
3818 `wrap_t `repeat;
3819 `mag_filter `nearest;
3820 `min_filter `nearest ];
3824 let setcheckers enabled =
3825 match state.texid with
3826 | None ->
3827 if enabled then state.texid <- Some (makecheckers ())
3829 | Some texid ->
3830 if not enabled
3831 then (
3832 GlTex.delete_texture texid;
3833 state.texid <- None;
3837 let int_of_string_with_suffix s =
3838 let l = String.length s in
3839 let s1, shift =
3840 if l > 1
3841 then
3842 let suffix = Char.lowercase s.[l-1] in
3843 match suffix with
3844 | 'k' -> String.sub s 0 (l-1), 10
3845 | 'm' -> String.sub s 0 (l-1), 20
3846 | 'g' -> String.sub s 0 (l-1), 30
3847 | _ -> s, 0
3848 else s, 0
3850 let n = int_of_string s1 in
3851 let m = n lsl shift in
3852 if m < 0 || m < n
3853 then raise (Failure "value too large")
3854 else m
3857 let string_with_suffix_of_int n =
3858 if n = 0
3859 then "0"
3860 else
3861 let n, s =
3862 if n land ((1 lsl 20) - 1) = 0
3863 then n lsr 20, "M"
3864 else (
3865 if n land ((1 lsl 10) - 1) = 0
3866 then n lsr 10, "K"
3867 else n, ""
3870 let rec loop s n =
3871 let h = n mod 1000 in
3872 let n = n / 1000 in
3873 if n = 0
3874 then string_of_int h ^ s
3875 else (
3876 let s = Printf.sprintf "_%03d%s" h s in
3877 loop s n
3880 loop "" n ^ s;
3883 let defghyllscroll = (40, 8, 32);;
3884 let ghyllscroll_of_string s =
3885 let (n, a, b) as nab =
3886 if s = "default"
3887 then defghyllscroll
3888 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3890 if n <= a || n <= b || a >= b
3891 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3892 nab;
3895 let ghyllscroll_to_string ((n, a, b) as nab) =
3896 if nab = defghyllscroll
3897 then "default"
3898 else Printf.sprintf "%d,%d,%d" n a b;
3901 let describe_location () =
3902 let f (fn, _) l =
3903 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3905 let fn, ln = List.fold_left f (-1, -1) state.layout in
3906 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3907 let percent =
3908 if maxy <= 0
3909 then 100.
3910 else (100. *. (float state.y /. float maxy))
3912 if fn = ln
3913 then
3914 Printf.sprintf "page %d of %d [%.2f%%]"
3915 (fn+1) state.pagecount percent
3916 else
3917 Printf.sprintf
3918 "pages %d-%d of %d [%.2f%%]"
3919 (fn+1) (ln+1) state.pagecount percent
3922 let enterinfomode =
3923 let btos b = if b then "\xe2\x88\x9a" else "" in
3924 let showextended = ref false in
3925 let leave mode = function
3926 | Confirm -> state.mode <- mode
3927 | Cancel -> state.mode <- mode in
3928 let src =
3929 (object
3930 val mutable m_first_time = true
3931 val mutable m_l = []
3932 val mutable m_a = [||]
3933 val mutable m_prev_uioh = nouioh
3934 val mutable m_prev_mode = View
3936 inherit lvsourcebase
3938 method reset prev_mode prev_uioh =
3939 m_a <- Array.of_list (List.rev m_l);
3940 m_l <- [];
3941 m_prev_mode <- prev_mode;
3942 m_prev_uioh <- prev_uioh;
3943 if m_first_time
3944 then (
3945 let rec loop n =
3946 if n >= Array.length m_a
3947 then ()
3948 else
3949 match m_a.(n) with
3950 | _, _, _, Action _ -> m_active <- n
3951 | _ -> loop (n+1)
3953 loop 0;
3954 m_first_time <- false;
3957 method int name get set =
3958 m_l <-
3959 (name, `int get, 1, Action (
3960 fun u ->
3961 let ondone s =
3962 try set (int_of_string s)
3963 with exn ->
3964 state.text <- Printf.sprintf "bad integer `%s': %s"
3965 s (Printexc.to_string exn)
3967 state.text <- "";
3968 let te = name ^ ": ", "", None, intentry, ondone, true in
3969 state.mode <- Textentry (te, leave m_prev_mode);
3971 )) :: m_l
3973 method int_with_suffix name get set =
3974 m_l <-
3975 (name, `intws get, 1, Action (
3976 fun u ->
3977 let ondone s =
3978 try set (int_of_string_with_suffix s)
3979 with exn ->
3980 state.text <- Printf.sprintf "bad integer `%s': %s"
3981 s (Printexc.to_string exn)
3983 state.text <- "";
3984 let te =
3985 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3987 state.mode <- Textentry (te, leave m_prev_mode);
3989 )) :: m_l
3991 method bool ?(offset=1) ?(btos=btos) name get set =
3992 m_l <-
3993 (name, `bool (btos, get), offset, Action (
3994 fun u ->
3995 let v = get () in
3996 set (not v);
3998 )) :: m_l
4000 method color name get set =
4001 m_l <-
4002 (name, `color get, 1, Action (
4003 fun u ->
4004 let invalid = (nan, nan, nan) in
4005 let ondone s =
4006 let c =
4007 try color_of_string s
4008 with exn ->
4009 state.text <- Printf.sprintf "bad color `%s': %s"
4010 s (Printexc.to_string exn);
4011 invalid
4013 if c <> invalid
4014 then set c;
4016 let te = name ^ ": ", "", None, textentry, ondone, true in
4017 state.text <- color_to_string (get ());
4018 state.mode <- Textentry (te, leave m_prev_mode);
4020 )) :: m_l
4022 method string name get set =
4023 m_l <-
4024 (name, `string get, 1, Action (
4025 fun u ->
4026 let ondone s = set s in
4027 let te = name ^ ": ", "", None, textentry, ondone, true in
4028 state.mode <- Textentry (te, leave m_prev_mode);
4030 )) :: m_l
4032 method colorspace name get set =
4033 m_l <-
4034 (name, `string get, 1, Action (
4035 fun _ ->
4036 let source =
4037 let vals = [| "rgb"; "bgr"; "gray" |] in
4038 (object
4039 inherit lvsourcebase
4041 initializer
4042 m_active <- int_of_colorspace conf.colorspace;
4043 m_first <- 0;
4045 method getitemcount = Array.length vals
4046 method getitem n = (vals.(n), 0)
4047 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4048 ignore (uioh, first, pan, qsearch);
4049 if not cancel then set active;
4050 None
4051 method hasaction _ = true
4052 end)
4054 state.text <- "";
4055 let modehash = findkeyhash conf "info" in
4056 coe (new listview ~source ~trusted:true ~modehash)
4057 )) :: m_l
4059 method caption s offset =
4060 m_l <- (s, `empty, offset, Noaction) :: m_l
4062 method caption2 s f offset =
4063 m_l <- (s, `string f, offset, Noaction) :: m_l
4065 method getitemcount = Array.length m_a
4067 method getitem n =
4068 let tostr = function
4069 | `int f -> string_of_int (f ())
4070 | `intws f -> string_with_suffix_of_int (f ())
4071 | `string f -> f ()
4072 | `color f -> color_to_string (f ())
4073 | `bool (btos, f) -> btos (f ())
4074 | `empty -> ""
4076 let name, t, offset, _ = m_a.(n) in
4077 ((let s = tostr t in
4078 if String.length s > 0
4079 then Printf.sprintf "%s\t%s" name s
4080 else name),
4081 offset)
4083 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4084 let uiohopt =
4085 if not cancel
4086 then (
4087 m_qsearch <- qsearch;
4088 let uioh =
4089 match m_a.(active) with
4090 | _, _, _, Action f -> f uioh
4091 | _ -> uioh
4093 Some uioh
4095 else None
4097 m_active <- active;
4098 m_first <- first;
4099 m_pan <- pan;
4100 uiohopt
4102 method hasaction n =
4103 match m_a.(n) with
4104 | _, _, _, Action _ -> true
4105 | _ -> false
4106 end)
4108 let rec fillsrc prevmode prevuioh =
4109 let sep () = src#caption "" 0 in
4110 let colorp name get set =
4111 src#string name
4112 (fun () -> color_to_string (get ()))
4113 (fun v ->
4115 let c = color_of_string v in
4116 set c
4117 with exn ->
4118 state.text <- Printf.sprintf "bad color `%s': %s"
4119 v (Printexc.to_string exn);
4122 let oldmode = state.mode in
4123 let birdseye = isbirdseye state.mode in
4125 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4127 src#bool "presentation mode"
4128 (fun () -> conf.presentation)
4129 (fun v ->
4130 conf.presentation <- v;
4131 state.anchor <- getanchor ();
4132 represent ());
4134 src#bool "ignore case in searches"
4135 (fun () -> conf.icase)
4136 (fun v -> conf.icase <- v);
4138 src#bool "preload"
4139 (fun () -> conf.preload)
4140 (fun v -> conf.preload <- v);
4142 src#bool "highlight links"
4143 (fun () -> conf.hlinks)
4144 (fun v -> conf.hlinks <- v);
4146 src#bool "under info"
4147 (fun () -> conf.underinfo)
4148 (fun v -> conf.underinfo <- v);
4150 src#bool "persistent bookmarks"
4151 (fun () -> conf.savebmarks)
4152 (fun v -> conf.savebmarks <- v);
4154 src#bool "proportional display"
4155 (fun () -> conf.proportional)
4156 (fun v -> reqlayout conf.angle v);
4158 src#bool "trim margins"
4159 (fun () -> conf.trimmargins)
4160 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4162 src#bool "persistent location"
4163 (fun () -> conf.jumpback)
4164 (fun v -> conf.jumpback <- v);
4166 sep ();
4167 src#int "inter-page space"
4168 (fun () -> conf.interpagespace)
4169 (fun n ->
4170 conf.interpagespace <- n;
4171 docolumns conf.columns;
4172 let pageno, py =
4173 match state.layout with
4174 | [] -> 0, 0
4175 | l :: _ ->
4176 l.pageno, l.pagey
4178 state.maxy <- calcheight ();
4179 let y = getpagey pageno in
4180 gotoy (y + py)
4183 src#int "page bias"
4184 (fun () -> conf.pagebias)
4185 (fun v -> conf.pagebias <- v);
4187 src#int "scroll step"
4188 (fun () -> conf.scrollstep)
4189 (fun n -> conf.scrollstep <- n);
4191 src#int "horizontal scroll step"
4192 (fun () -> conf.hscrollstep)
4193 (fun v -> conf.hscrollstep <- v);
4195 src#int "auto scroll step"
4196 (fun () ->
4197 match state.autoscroll with
4198 | Some step -> step
4199 | _ -> conf.autoscrollstep)
4200 (fun n ->
4201 if state.autoscroll <> None
4202 then state.autoscroll <- Some n;
4203 conf.autoscrollstep <- n);
4205 src#int "zoom"
4206 (fun () -> truncate (conf.zoom *. 100.))
4207 (fun v -> setzoom ((float v) /. 100.));
4209 src#int "rotation"
4210 (fun () -> conf.angle)
4211 (fun v -> reqlayout v conf.proportional);
4213 src#int "scroll bar width"
4214 (fun () -> state.scrollw)
4215 (fun v ->
4216 state.scrollw <- v;
4217 conf.scrollbw <- v;
4218 reshape conf.winw conf.winh;
4221 src#int "scroll handle height"
4222 (fun () -> conf.scrollh)
4223 (fun v -> conf.scrollh <- v;);
4225 src#int "thumbnail width"
4226 (fun () -> conf.thumbw)
4227 (fun v ->
4228 conf.thumbw <- min 4096 v;
4229 match oldmode with
4230 | Birdseye beye ->
4231 leavebirdseye beye false;
4232 enterbirdseye ()
4233 | _ -> ()
4236 let mode = state.mode in
4237 src#string "columns"
4238 (fun () ->
4239 match conf.columns with
4240 | Csingle -> "1"
4241 | Cmulti (multi, _) -> multicolumns_to_string multi
4242 | Csplit (count, _) -> "-" ^ string_of_int count
4244 (fun v ->
4245 let n, a, b = multicolumns_of_string v in
4246 setcolumns mode n a b);
4248 sep ();
4249 src#caption "Presentation mode" 0;
4250 src#bool "scrollbar visible"
4251 (fun () -> conf.scrollbarinpm)
4252 (fun v ->
4253 if v != conf.scrollbarinpm
4254 then (
4255 conf.scrollbarinpm <- v;
4256 if conf.presentation
4257 then (
4258 state.scrollw <- if v then conf.scrollbw else 0;
4259 reshape conf.winw conf.winh;
4264 sep ();
4265 src#caption "Pixmap cache" 0;
4266 src#int_with_suffix "size (advisory)"
4267 (fun () -> conf.memlimit)
4268 (fun v -> conf.memlimit <- v);
4270 src#caption2 "used"
4271 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4272 (string_with_suffix_of_int state.memused)
4273 (Hashtbl.length state.tilemap)) 1;
4275 sep ();
4276 src#caption "Layout" 0;
4277 src#caption2 "Dimension"
4278 (fun () ->
4279 Printf.sprintf "%dx%d (virtual %dx%d)"
4280 conf.winw conf.winh
4281 state.w state.maxy)
4283 if conf.debug
4284 then
4285 src#caption2 "Position" (fun () ->
4286 Printf.sprintf "%dx%d" state.x state.y
4288 else
4289 src#caption2 "Visible" (fun () -> describe_location ()) 1
4292 sep ();
4293 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4294 "Save these parameters as global defaults at exit"
4295 (fun () -> conf.bedefault)
4296 (fun v -> conf.bedefault <- v)
4299 sep ();
4300 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4301 src#bool ~offset:0 ~btos "Extended parameters"
4302 (fun () -> !showextended)
4303 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4304 if !showextended
4305 then (
4306 src#bool "checkers"
4307 (fun () -> conf.checkers)
4308 (fun v -> conf.checkers <- v; setcheckers v);
4309 src#bool "update cursor"
4310 (fun () -> conf.updatecurs)
4311 (fun v -> conf.updatecurs <- v);
4312 src#bool "verbose"
4313 (fun () -> conf.verbose)
4314 (fun v -> conf.verbose <- v);
4315 src#bool "invert colors"
4316 (fun () -> conf.invert)
4317 (fun v -> conf.invert <- v);
4318 src#bool "max fit"
4319 (fun () -> conf.maxhfit)
4320 (fun v -> conf.maxhfit <- v);
4321 src#bool "redirect stderr"
4322 (fun () -> conf.redirectstderr)
4323 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4324 src#string "uri launcher"
4325 (fun () -> conf.urilauncher)
4326 (fun v -> conf.urilauncher <- v);
4327 src#string "path launcher"
4328 (fun () -> conf.pathlauncher)
4329 (fun v -> conf.pathlauncher <- v);
4330 src#string "tile size"
4331 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4332 (fun v ->
4334 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4335 conf.tilew <- max 64 w;
4336 conf.tileh <- max 64 h;
4337 flushtiles ();
4338 with exn ->
4339 state.text <- Printf.sprintf "bad tile size `%s': %s"
4340 v (Printexc.to_string exn));
4341 src#int "texture count"
4342 (fun () -> conf.texcount)
4343 (fun v ->
4344 if realloctexts v
4345 then conf.texcount <- v
4346 else showtext '!' " Failed to set texture count please retry later"
4348 src#int "slice height"
4349 (fun () -> conf.sliceheight)
4350 (fun v ->
4351 conf.sliceheight <- v;
4352 wcmd "sliceh %d" conf.sliceheight;
4354 src#int "anti-aliasing level"
4355 (fun () -> conf.aalevel)
4356 (fun v ->
4357 conf.aalevel <- bound v 0 8;
4358 state.anchor <- getanchor ();
4359 opendoc state.path state.password;
4361 src#int "ui font size"
4362 (fun () -> fstate.fontsize)
4363 (fun v -> setfontsize (bound v 5 100));
4364 src#int "hint font size"
4365 (fun () -> conf.hfsize)
4366 (fun v -> conf.hfsize <- bound v 5 100);
4367 colorp "background color"
4368 (fun () -> conf.bgcolor)
4369 (fun v -> conf.bgcolor <- v);
4370 src#bool "crop hack"
4371 (fun () -> conf.crophack)
4372 (fun v -> conf.crophack <- v);
4373 src#string "trim fuzz"
4374 (fun () -> irect_to_string conf.trimfuzz)
4375 (fun v ->
4377 conf.trimfuzz <- irect_of_string v;
4378 if conf.trimmargins
4379 then settrim true conf.trimfuzz;
4380 with exn ->
4381 state.text <- Printf.sprintf "bad irect `%s': %s"
4382 v (Printexc.to_string exn)
4384 src#string "throttle"
4385 (fun () ->
4386 match conf.maxwait with
4387 | None -> "show place holder if page is not ready"
4388 | Some time ->
4389 if time = infinity
4390 then "wait for page to fully render"
4391 else
4392 "wait " ^ string_of_float time
4393 ^ " seconds before showing placeholder"
4395 (fun v ->
4397 let f = float_of_string v in
4398 if f <= 0.0
4399 then conf.maxwait <- None
4400 else conf.maxwait <- Some f
4401 with exn ->
4402 state.text <- Printf.sprintf "bad time `%s': %s"
4403 v (Printexc.to_string exn)
4405 src#string "ghyll scroll"
4406 (fun () ->
4407 match conf.ghyllscroll with
4408 | None -> ""
4409 | Some nab -> ghyllscroll_to_string nab
4411 (fun v ->
4413 let gs =
4414 if String.length v = 0
4415 then None
4416 else Some (ghyllscroll_of_string v)
4418 conf.ghyllscroll <- gs
4419 with exn ->
4420 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4421 v (Printexc.to_string exn)
4423 src#string "selection command"
4424 (fun () -> conf.selcmd)
4425 (fun v -> conf.selcmd <- v);
4426 src#colorspace "color space"
4427 (fun () -> colorspace_to_string conf.colorspace)
4428 (fun v ->
4429 conf.colorspace <- colorspace_of_int v;
4430 wcmd "cs %d" v;
4431 load state.layout;
4435 sep ();
4436 src#caption "Document" 0;
4437 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4438 src#caption2 "Pages"
4439 (fun () -> string_of_int state.pagecount) 1;
4440 src#caption2 "Dimensions"
4441 (fun () -> string_of_int (List.length state.pdims)) 1;
4442 if conf.trimmargins
4443 then (
4444 sep ();
4445 src#caption "Trimmed margins" 0;
4446 src#caption2 "Dimensions"
4447 (fun () -> string_of_int (List.length state.pdims)) 1;
4450 sep ();
4451 src#caption "OpenGL" 0;
4452 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4453 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4454 src#reset prevmode prevuioh;
4456 fun () ->
4457 state.text <- "";
4458 let prevmode = state.mode
4459 and prevuioh = state.uioh in
4460 fillsrc prevmode prevuioh;
4461 let source = (src :> lvsource) in
4462 let modehash = findkeyhash conf "info" in
4463 state.uioh <- coe (object (self)
4464 inherit listview ~source ~trusted:true ~modehash as super
4465 val mutable m_prevmemused = 0
4466 method infochanged = function
4467 | Memused ->
4468 if m_prevmemused != state.memused
4469 then (
4470 m_prevmemused <- state.memused;
4471 G.postRedisplay "memusedchanged";
4473 | Pdim -> G.postRedisplay "pdimchanged"
4474 | Docinfo -> fillsrc prevmode prevuioh
4476 method key key mask =
4477 if not (Wsi.withctrl mask)
4478 then
4479 match key with
4480 | 0xff51 -> coe (self#updownlevel ~-1)
4481 | 0xff53 -> coe (self#updownlevel 1)
4482 | _ -> super#key key mask
4483 else super#key key mask
4484 end);
4485 G.postRedisplay "info";
4488 let enterhelpmode =
4489 let source =
4490 (object
4491 inherit lvsourcebase
4492 method getitemcount = Array.length state.help
4493 method getitem n =
4494 let s, n, _ = state.help.(n) in
4495 (s, n)
4497 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4498 let optuioh =
4499 if not cancel
4500 then (
4501 m_qsearch <- qsearch;
4502 match state.help.(active) with
4503 | _, _, Action f -> Some (f uioh)
4504 | _ -> Some (uioh)
4506 else None
4508 m_active <- active;
4509 m_first <- first;
4510 m_pan <- pan;
4511 optuioh
4513 method hasaction n =
4514 match state.help.(n) with
4515 | _, _, Action _ -> true
4516 | _ -> false
4518 initializer
4519 m_active <- -1
4520 end)
4521 in fun () ->
4522 let modehash = findkeyhash conf "help" in
4523 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4524 G.postRedisplay "help";
4527 let entermsgsmode =
4528 let msgsource =
4529 let re = Str.regexp "[\r\n]" in
4530 (object
4531 inherit lvsourcebase
4532 val mutable m_items = [||]
4534 method getitemcount = 1 + Array.length m_items
4536 method getitem n =
4537 if n = 0
4538 then "[Clear]", 0
4539 else m_items.(n-1), 0
4541 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4542 ignore uioh;
4543 if not cancel
4544 then (
4545 if active = 0
4546 then Buffer.clear state.errmsgs;
4547 m_qsearch <- qsearch;
4549 m_active <- active;
4550 m_first <- first;
4551 m_pan <- pan;
4552 None
4554 method hasaction n =
4555 n = 0
4557 method reset =
4558 state.newerrmsgs <- false;
4559 let l = Str.split re (Buffer.contents state.errmsgs) in
4560 m_items <- Array.of_list l
4562 initializer
4563 m_active <- 0
4564 end)
4565 in fun () ->
4566 state.text <- "";
4567 msgsource#reset;
4568 let source = (msgsource :> lvsource) in
4569 let modehash = findkeyhash conf "listview" in
4570 state.uioh <- coe (object
4571 inherit listview ~source ~trusted:false ~modehash as super
4572 method display =
4573 if state.newerrmsgs
4574 then msgsource#reset;
4575 super#display
4576 end);
4577 G.postRedisplay "msgs";
4580 let quickbookmark ?title () =
4581 match state.layout with
4582 | [] -> ()
4583 | l :: _ ->
4584 let title =
4585 match title with
4586 | None ->
4587 let sec = Unix.gettimeofday () in
4588 let tm = Unix.localtime sec in
4589 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4590 (l.pageno+1)
4591 tm.Unix.tm_mday
4592 tm.Unix.tm_mon
4593 (tm.Unix.tm_year + 1900)
4594 tm.Unix.tm_hour
4595 tm.Unix.tm_min
4596 | Some title -> title
4598 state.bookmarks <-
4599 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4600 :: state.bookmarks
4603 let doreshape w h =
4604 state.fullscreen <- None;
4605 Wsi.reshape w h;
4608 let setautoscrollspeed step goingdown =
4609 let incr = max 1 ((abs step) / 2) in
4610 let incr = if goingdown then incr else -incr in
4611 let astep = step + incr in
4612 state.autoscroll <- Some astep;
4615 let gotounder = function
4616 | Ulinkgoto (pageno, top) ->
4617 if pageno >= 0
4618 then (
4619 addnav ();
4620 gotopage1 pageno top;
4623 | Ulinkuri s ->
4624 gotouri s
4626 | Uremote (filename, pageno) ->
4627 let path =
4628 if Sys.file_exists filename
4629 then filename
4630 else
4631 let dir = Filename.dirname state.path in
4632 let path = Filename.concat dir filename in
4633 if Sys.file_exists path
4634 then path
4635 else ""
4637 if String.length path > 0
4638 then (
4639 let anchor = getanchor () in
4640 let ranchor = state.path, state.password, anchor in
4641 state.anchor <- (pageno, 0.0);
4642 state.ranchors <- ranchor :: state.ranchors;
4643 opendoc path "";
4645 else showtext '!' ("Could not find " ^ filename)
4647 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4650 let canpan () =
4651 match conf.columns with
4652 | Csplit _ -> true
4653 | _ -> conf.zoom > 1.0
4656 let adjust_brightness () =
4657 if conf.colorscale = 1.0
4658 then GlTex.env (`mode `replace)
4659 else GlTex.env (`mode `modulate);
4662 let viewkeyboard key mask =
4663 let enttext te =
4664 let mode = state.mode in
4665 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4666 state.text <- "";
4667 enttext ();
4668 G.postRedisplay "view:enttext"
4670 let ctrl = Wsi.withctrl mask in
4671 match key with
4672 | 81 -> (* Q *)
4673 exit 0
4675 | 0xff63 -> (* insert *)
4676 if conf.angle mod 360 = 0
4677 then (
4678 state.mode <- LinkNav (Ltgendir 0);
4679 gotoy state.y;
4681 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4683 | 0xff1b | 113 -> (* escape / q *)
4684 begin match state.mstate with
4685 | Mzoomrect _ ->
4686 state.mstate <- Mnone;
4687 Wsi.setcursor Wsi.CURSOR_INHERIT;
4688 G.postRedisplay "kill zoom rect";
4689 | _ ->
4690 match state.ranchors with
4691 | [] -> raise Quit
4692 | (path, password, anchor) :: rest ->
4693 state.ranchors <- rest;
4694 state.anchor <- anchor;
4695 opendoc path password
4696 end;
4698 | 0xff08 -> (* backspace *)
4699 let y = getnav ~-1 in
4700 gotoy_and_clear_text y
4702 | 111 -> (* o *)
4703 enteroutlinemode ()
4705 | 117 -> (* u *)
4706 state.rects <- [];
4707 state.text <- "";
4708 G.postRedisplay "dehighlight";
4710 | 47 | 63 -> (* / ? *)
4711 let ondone isforw s =
4712 cbput state.hists.pat s;
4713 state.searchpattern <- s;
4714 search s isforw
4716 let s = String.create 1 in
4717 s.[0] <- Char.chr key;
4718 enttext (s, "", Some (onhist state.hists.pat),
4719 textentry, ondone (key = 47), true)
4721 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4722 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4723 setzoom (conf.zoom +. incr)
4725 | 43 | 0xffab -> (* + *)
4726 let ondone s =
4727 let n =
4728 try int_of_string s with exc ->
4729 state.text <- Printf.sprintf "bad integer `%s': %s"
4730 s (Printexc.to_string exc);
4731 max_int
4733 if n != max_int
4734 then (
4735 conf.pagebias <- n;
4736 state.text <- "page bias is now " ^ string_of_int n;
4739 enttext ("page bias: ", "", None, intentry, ondone, true)
4741 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4742 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4743 setzoom (max 0.01 (conf.zoom -. decr))
4745 | 45 | 0xffad -> (* - *)
4746 let ondone msg = state.text <- msg in
4747 enttext (
4748 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4749 optentry state.mode, ondone, true
4752 | 48 when ctrl -> (* ctrl-0 *)
4753 setzoom 1.0
4755 | 49 when ctrl -> (* ctrl-1 *)
4756 let cols =
4757 match conf.columns with
4758 | Csingle | Cmulti _ -> 1
4759 | Csplit (n, _) -> n
4761 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4762 if zoom < 1.0
4763 then setzoom zoom
4765 | 0xffc6 -> (* f9 *)
4766 togglebirdseye ()
4768 | 57 when ctrl -> (* ctrl-9 *)
4769 togglebirdseye ()
4771 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4772 when not ctrl -> (* 0..9 *)
4773 let ondone s =
4774 let n =
4775 try int_of_string s with exc ->
4776 state.text <- Printf.sprintf "bad integer `%s': %s"
4777 s (Printexc.to_string exc);
4780 if n >= 0
4781 then (
4782 addnav ();
4783 cbput state.hists.pag (string_of_int n);
4784 gotopage1 (n + conf.pagebias - 1) 0;
4787 let pageentry text key =
4788 match Char.unsafe_chr key with
4789 | 'g' -> TEdone text
4790 | _ -> intentry text key
4792 let text = "x" in text.[0] <- Char.chr key;
4793 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4795 | 98 -> (* b *)
4796 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4797 reshape conf.winw conf.winh;
4799 | 108 -> (* l *)
4800 conf.hlinks <- not conf.hlinks;
4801 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4802 G.postRedisplay "toggle highlightlinks";
4804 | 70 -> (* F *)
4805 state.glinks <- true;
4806 let mode = state.mode in
4807 state.mode <- Textentry (
4808 (":", "", None, linknentry, linkndone (fun under ->
4809 addnav ();
4810 gotounder under
4811 ), false
4812 ), fun _ ->
4813 state.glinks <- false;
4814 state.mode <- mode
4816 state.text <- "";
4817 G.postRedisplay "view:linkent(F)"
4819 | 121 -> (* y *)
4820 state.glinks <- true;
4821 let mode = state.mode in
4822 state.mode <- Textentry (
4823 (":", "", None, linknentry, linkndone (fun under ->
4824 match Ne.pipe () with
4825 | Ne.Exn exn ->
4826 showtext '!' (Printf.sprintf "pipe failed: %s"
4827 (Printexc.to_string exn));
4828 | Ne.Res (r, w) ->
4829 let popened =
4830 try popen conf.selcmd [r, 0; w, -1]; true
4831 with exn ->
4832 showtext '!'
4833 (Printf.sprintf "failed to execute %s: %s"
4834 conf.selcmd (Printexc.to_string exn));
4835 false
4837 let clo cap fd =
4838 Ne.clo fd (fun msg ->
4839 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4842 let s = undertext under in
4843 if popened
4844 then
4845 (try
4846 let l = String.length s in
4847 let n = Unix.write w s 0 l in
4848 if n != l
4849 then
4850 showtext '!'
4851 (Printf.sprintf
4852 "failed to write %d characters to sel pipe, wrote %d"
4855 with exn ->
4856 showtext '!'
4857 (Printf.sprintf "failed to write to sel pipe: %s"
4858 (Printexc.to_string exn)
4861 else dolog "%s" s;
4862 clo "pipe/r" r;
4863 clo "pipe/w" w;
4864 ), false
4866 fun _ ->
4867 state.glinks <- false;
4868 state.mode <- mode
4870 state.text <- "";
4871 G.postRedisplay "view:linkent"
4873 | 97 -> (* a *)
4874 begin match state.autoscroll with
4875 | Some step ->
4876 conf.autoscrollstep <- step;
4877 state.autoscroll <- None
4878 | None ->
4879 if conf.autoscrollstep = 0
4880 then state.autoscroll <- Some 1
4881 else state.autoscroll <- Some conf.autoscrollstep
4884 | 112 when ctrl -> (* ctrl-p *)
4885 launchpath ()
4887 | 80 -> (* P *)
4888 conf.presentation <- not conf.presentation;
4889 if conf.presentation
4890 then (
4891 if not conf.scrollbarinpm
4892 then state.scrollw <- 0;
4894 else
4895 state.scrollw <- conf.scrollbw;
4897 showtext ' ' ("presentation mode " ^
4898 if conf.presentation then "on" else "off");
4899 state.anchor <- getanchor ();
4900 represent ()
4902 | 102 -> (* f *)
4903 begin match state.fullscreen with
4904 | None ->
4905 state.fullscreen <- Some (conf.winw, conf.winh);
4906 Wsi.fullscreen ()
4907 | Some (w, h) ->
4908 state.fullscreen <- None;
4909 doreshape w h
4912 | 103 -> (* g *)
4913 gotoy_and_clear_text 0
4915 | 71 -> (* G *)
4916 gotopage1 (state.pagecount - 1) 0
4918 | 112 | 78 -> (* p|N *)
4919 search state.searchpattern false
4921 | 110 | 0xffc0 -> (* n|F3 *)
4922 search state.searchpattern true
4924 | 116 -> (* t *)
4925 begin match state.layout with
4926 | [] -> ()
4927 | l :: _ ->
4928 gotoy_and_clear_text (getpagey l.pageno)
4931 | 32 -> (* ' ' *)
4932 begin match state.layout with
4933 | [] -> ()
4934 | l :: _ ->
4935 match conf.columns with
4936 | Csingle | Cmulti _ ->
4937 let pageno = min (l.pageno+1) (state.pagecount-1) in
4938 gotoy_and_clear_text (getpagey pageno)
4939 | Csplit (n, _) ->
4940 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4941 then
4942 let pagey, pageh = getpageyh l.pageno in
4943 let pagey = pagey + pageh * l.pagecol in
4944 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4945 gotoy_and_clear_text (pagey + pageh + ips)
4948 | 0xff9f | 0xffff -> (* delete *)
4949 begin match state.layout with
4950 | [] -> ()
4951 | l :: _ ->
4952 match conf.columns with
4953 | Csingle | Cmulti _ ->
4954 let pageno = max 0 (l.pageno-1) in
4955 gotoy_and_clear_text (getpagey pageno)
4956 | Csplit (n, _) ->
4957 let y =
4958 if l.pagecol = 0
4959 then
4960 if l.pageno = 0
4961 then l.pagey
4962 else
4963 let pageno = max 0 (l.pageno-1) in
4964 let pagey, pageh = getpageyh pageno in
4965 pagey + (n-1)*pageh
4966 else
4967 let pagey, pageh = getpageyh l.pageno in
4968 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4970 gotoy_and_clear_text y
4973 | 61 -> (* = *)
4974 showtext ' ' (describe_location ());
4976 | 119 -> (* w *)
4977 begin match state.layout with
4978 | [] -> ()
4979 | l :: _ ->
4980 doreshape (l.pagew + state.scrollw) l.pageh;
4981 G.postRedisplay "w"
4984 | 39 -> (* ' *)
4985 enterbookmarkmode ()
4987 | 104 | 0xffbe -> (* h|F1 *)
4988 enterhelpmode ()
4990 | 105 -> (* i *)
4991 enterinfomode ()
4993 | 101 when conf.redirectstderr -> (* e *)
4994 entermsgsmode ()
4996 | 109 -> (* m *)
4997 let ondone s =
4998 match state.layout with
4999 | l :: _ ->
5000 state.bookmarks <-
5001 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
5002 :: state.bookmarks
5003 | _ -> ()
5005 enttext ("bookmark: ", "", None, textentry, ondone, true)
5007 | 126 -> (* ~ *)
5008 quickbookmark ();
5009 showtext ' ' "Quick bookmark added";
5011 | 122 -> (* z *)
5012 begin match state.layout with
5013 | l :: _ ->
5014 let rect = getpdimrect l.pagedimno in
5015 let w, h =
5016 if conf.crophack
5017 then
5018 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5019 truncate (1.2 *. (rect.(3) -. rect.(0))))
5020 else
5021 (truncate (rect.(1) -. rect.(0)),
5022 truncate (rect.(3) -. rect.(0)))
5024 let w = truncate ((float w)*.conf.zoom)
5025 and h = truncate ((float h)*.conf.zoom) in
5026 if w != 0 && h != 0
5027 then (
5028 state.anchor <- getanchor ();
5029 doreshape (w + state.scrollw) (h + conf.interpagespace)
5031 G.postRedisplay "z";
5033 | [] -> ()
5036 | 50 when ctrl -> (* ctrl-2 *)
5037 let maxw = getmaxw () in
5038 if maxw > 0.0
5039 then setzoom (maxw /. float conf.winw)
5041 | 60 | 62 -> (* < > *)
5042 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5044 | 91 | 93 -> (* [ ] *)
5045 conf.colorscale <-
5046 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5048 adjust_brightness ();
5049 G.postRedisplay "brightness";
5051 | 99 when state.mode = View -> (* c *)
5052 let (c, a, b), z =
5053 match state.prevcolumns with
5054 | None -> (1, 0, 0), 1.0
5055 | Some (columns, z) ->
5056 let cab =
5057 match columns with
5058 | Csplit (c, _) -> -c, 0, 0
5059 | Cmulti ((c, a, b), _) -> c, a, b
5060 | Csingle -> 1, 0, 0
5062 cab, z
5064 setcolumns View c a b;
5065 setzoom z;
5067 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5068 setzoom state.prevzoom
5070 | 107 | 0xff52 -> (* k up *)
5071 begin match state.autoscroll with
5072 | None ->
5073 begin match state.mode with
5074 | Birdseye beye -> upbirdseye 1 beye
5075 | _ ->
5076 if ctrl
5077 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5078 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5080 | Some n ->
5081 setautoscrollspeed n false
5084 | 106 | 0xff54 -> (* j down *)
5085 begin match state.autoscroll with
5086 | None ->
5087 begin match state.mode with
5088 | Birdseye beye -> downbirdseye 1 beye
5089 | _ ->
5090 if ctrl
5091 then gotoy_and_clear_text (clamp (conf.winh/2))
5092 else gotoy_and_clear_text (clamp conf.scrollstep)
5094 | Some n ->
5095 setautoscrollspeed n true
5098 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5099 if canpan ()
5100 then
5101 let dx =
5102 if ctrl
5103 then conf.winw / 2
5104 else 10
5106 let dx = if key = 0xff51 then dx else -dx in
5107 state.x <- state.x + dx;
5108 gotoy_and_clear_text state.y
5109 else (
5110 state.text <- "";
5111 G.postRedisplay "lef/right"
5114 | 0xff55 -> (* prior *)
5115 let y =
5116 if ctrl
5117 then
5118 match state.layout with
5119 | [] -> state.y
5120 | l :: _ -> state.y - l.pagey
5121 else
5122 clamp (-conf.winh)
5124 gotoghyll y
5126 | 0xff56 -> (* next *)
5127 let y =
5128 if ctrl
5129 then
5130 match List.rev state.layout with
5131 | [] -> state.y
5132 | l :: _ -> getpagey l.pageno
5133 else
5134 clamp conf.winh
5136 gotoghyll y
5138 | 0xff50 -> gotoghyll 0
5139 | 0xff57 -> gotoghyll (clamp state.maxy)
5140 | 0xff53 when Wsi.withalt mask ->
5141 gotoghyll (getnav ~-1)
5142 | 0xff51 when Wsi.withalt mask ->
5143 gotoghyll (getnav 1)
5145 | 114 -> (* r *)
5146 state.anchor <- getanchor ();
5147 opendoc state.path state.password
5149 | 118 when conf.debug -> (* v *)
5150 state.rects <- [];
5151 List.iter (fun l ->
5152 match getopaque l.pageno with
5153 | None -> ()
5154 | Some opaque ->
5155 let x0, y0, x1, y1 = pagebbox opaque in
5156 let a,b = float x0, float y0 in
5157 let c,d = float x1, float y0 in
5158 let e,f = float x1, float y1 in
5159 let h,j = float x0, float y1 in
5160 let rect = (a,b,c,d,e,f,h,j) in
5161 debugrect rect;
5162 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5163 ) state.layout;
5164 G.postRedisplay "v";
5166 | _ ->
5167 vlog "huh? %s" (Wsi.keyname key)
5170 let linknavkeyboard key mask linknav =
5171 let getpage pageno =
5172 let rec loop = function
5173 | [] -> None
5174 | l :: _ when l.pageno = pageno -> Some l
5175 | _ :: rest -> loop rest
5176 in loop state.layout
5178 let doexact (pageno, n) =
5179 match getopaque pageno, getpage pageno with
5180 | Some opaque, Some l ->
5181 if key = 0xff0d
5182 then
5183 let under = getlink opaque n in
5184 G.postRedisplay "link gotounder";
5185 gotounder under;
5186 state.mode <- View;
5187 else
5188 let opt, dir =
5189 match key with
5190 | 0xff50 -> (* home *)
5191 Some (findlink opaque LDfirst), -1
5193 | 0xff57 -> (* end *)
5194 Some (findlink opaque LDlast), 1
5196 | 0xff51 -> (* left *)
5197 Some (findlink opaque (LDleft n)), -1
5199 | 0xff53 -> (* right *)
5200 Some (findlink opaque (LDright n)), 1
5202 | 0xff52 -> (* up *)
5203 Some (findlink opaque (LDup n)), -1
5205 | 0xff54 -> (* down *)
5206 Some (findlink opaque (LDdown n)), 1
5208 | _ -> None, 0
5210 let pwl l dir =
5211 begin match findpwl l.pageno dir with
5212 | Pwlnotfound -> ()
5213 | Pwl pageno ->
5214 let notfound dir =
5215 state.mode <- LinkNav (Ltgendir dir);
5216 let y, h = getpageyh pageno in
5217 let y =
5218 if dir < 0
5219 then y + h - conf.winh
5220 else y
5222 gotoy y
5224 begin match getopaque pageno, getpage pageno with
5225 | Some opaque, Some _ ->
5226 let link =
5227 let ld = if dir > 0 then LDfirst else LDlast in
5228 findlink opaque ld
5230 begin match link with
5231 | Lfound m ->
5232 showlinktype (getlink opaque m);
5233 state.mode <- LinkNav (Ltexact (pageno, m));
5234 G.postRedisplay "linknav jpage";
5235 | _ -> notfound dir
5236 end;
5237 | _ -> notfound dir
5238 end;
5239 end;
5241 begin match opt with
5242 | Some Lnotfound -> pwl l dir;
5243 | Some (Lfound m) ->
5244 if m = n
5245 then pwl l dir
5246 else (
5247 let _, y0, _, y1 = getlinkrect opaque m in
5248 if y0 < l.pagey
5249 then gotopage1 l.pageno y0
5250 else (
5251 let d = fstate.fontsize + 1 in
5252 if y1 - l.pagey > l.pagevh - d
5253 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5254 else G.postRedisplay "linknav";
5256 showlinktype (getlink opaque m);
5257 state.mode <- LinkNav (Ltexact (l.pageno, m));
5260 | None -> viewkeyboard key mask
5261 end;
5262 | _ -> viewkeyboard key mask
5264 if key = 0xff63
5265 then (
5266 state.mode <- View;
5267 G.postRedisplay "leave linknav"
5269 else
5270 match linknav with
5271 | Ltgendir _ -> viewkeyboard key mask
5272 | Ltexact exact -> doexact exact
5275 let keyboard key mask =
5276 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5277 then wcmd "interrupt"
5278 else state.uioh <- state.uioh#key key mask
5281 let birdseyekeyboard key mask
5282 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5283 let incr =
5284 match conf.columns with
5285 | Csingle -> 1
5286 | Cmulti ((c, _, _), _) -> c
5287 | Csplit _ -> failwith "bird's eye split mode"
5289 match key with
5290 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5291 let y, h = getpageyh pageno in
5292 let top = (conf.winh - h) / 2 in
5293 gotoy (max 0 (y - top))
5294 | 0xff0d -> leavebirdseye beye false
5295 | 0xff1b -> leavebirdseye beye true (* escape *)
5296 | 0xff52 -> upbirdseye incr beye (* prior *)
5297 | 0xff54 -> downbirdseye incr beye (* next *)
5298 | 0xff51 -> upbirdseye 1 beye (* up *)
5299 | 0xff53 -> downbirdseye 1 beye (* down *)
5301 | 0xff55 ->
5302 begin match state.layout with
5303 | l :: _ ->
5304 if l.pagey != 0
5305 then (
5306 state.mode <- Birdseye (
5307 oconf, leftx, l.pageno, hooverpageno, anchor
5309 gotopage1 l.pageno 0;
5311 else (
5312 let layout = layout (state.y-conf.winh) conf.winh in
5313 match layout with
5314 | [] -> gotoy (clamp (-conf.winh))
5315 | l :: _ ->
5316 state.mode <- Birdseye (
5317 oconf, leftx, l.pageno, hooverpageno, anchor
5319 gotopage1 l.pageno 0
5322 | [] -> gotoy (clamp (-conf.winh))
5323 end;
5325 | 0xff56 ->
5326 begin match List.rev state.layout with
5327 | l :: _ ->
5328 let layout = layout (state.y + conf.winh) conf.winh in
5329 begin match layout with
5330 | [] ->
5331 let incr = l.pageh - l.pagevh in
5332 if incr = 0
5333 then (
5334 state.mode <-
5335 Birdseye (
5336 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5338 G.postRedisplay "birdseye pagedown";
5340 else gotoy (clamp (incr + conf.interpagespace*2));
5342 | l :: _ ->
5343 state.mode <-
5344 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5345 gotopage1 l.pageno 0;
5348 | [] -> gotoy (clamp conf.winh)
5349 end;
5351 | 0xff50 ->
5352 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5353 gotopage1 0 0
5355 | 0xff57 ->
5356 let pageno = state.pagecount - 1 in
5357 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5358 if not (pagevisible state.layout pageno)
5359 then
5360 let h =
5361 match List.rev state.pdims with
5362 | [] -> conf.winh
5363 | (_, _, h, _) :: _ -> h
5365 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5366 else G.postRedisplay "birdseye end";
5367 | _ -> viewkeyboard key mask
5370 let drawpage l linkindexbase =
5371 let color =
5372 match state.mode with
5373 | Textentry _ -> scalecolor 0.4
5374 | LinkNav _
5375 | View -> scalecolor 1.0
5376 | Birdseye (_, _, pageno, hooverpageno, _) ->
5377 if l.pageno = hooverpageno
5378 then scalecolor 0.9
5379 else (
5380 if l.pageno = pageno
5381 then scalecolor 1.0
5382 else scalecolor 0.8
5385 drawtiles l color;
5386 begin match getopaque l.pageno with
5387 | Some opaque ->
5388 if tileready l l.pagex l.pagey
5389 then
5390 let x = l.pagedispx - l.pagex
5391 and y = l.pagedispy - l.pagey in
5392 let hlmask =
5393 match conf.columns with
5394 | Csingle | Cmulti _ ->
5395 (if conf.hlinks then 1 else 0)
5396 + (if state.glinks
5397 && not (isbirdseye state.mode) then 2 else 0)
5398 | _ -> 0
5400 let s =
5401 match state.mode with
5402 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5403 | _ -> ""
5405 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5406 else 0
5408 | _ -> 0
5409 end;
5412 let scrollindicator () =
5413 let sbw, ph, sh = state.uioh#scrollph in
5414 let sbh, pw, sw = state.uioh#scrollpw in
5416 GlDraw.color (0.64, 0.64, 0.64);
5417 GlDraw.rect
5418 (float (conf.winw - sbw), 0.)
5419 (float conf.winw, float conf.winh)
5421 GlDraw.rect
5422 (0., float (conf.winh - sbh))
5423 (float (conf.winw - state.scrollw - 1), float conf.winh)
5425 GlDraw.color (0.0, 0.0, 0.0);
5427 GlDraw.rect
5428 (float (conf.winw - sbw), ph)
5429 (float conf.winw, ph +. sh)
5431 GlDraw.rect
5432 (pw, float (conf.winh - sbh))
5433 (pw +. sw, float conf.winh)
5437 let showsel () =
5438 match state.mstate with
5439 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5442 | Msel ((x0, y0), (x1, y1)) ->
5443 let rec loop = function
5444 | l :: ls ->
5445 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5446 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5447 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5448 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5449 then
5450 match getopaque l.pageno with
5451 | Some opaque ->
5452 let x0, y0 = pagetranslatepoint l x0 y0 in
5453 let x1, y1 = pagetranslatepoint l x1 y1 in
5454 seltext opaque (x0, y0, x1, y1);
5455 | _ -> ()
5456 else loop ls
5457 | [] -> ()
5459 loop state.layout
5462 let showrects rects =
5463 Gl.enable `blend;
5464 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5465 GlDraw.polygon_mode `both `fill;
5466 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5467 List.iter
5468 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5469 List.iter (fun l ->
5470 if l.pageno = pageno
5471 then (
5472 let dx = float (l.pagedispx - l.pagex) in
5473 let dy = float (l.pagedispy - l.pagey) in
5474 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5475 GlDraw.begins `quads;
5477 GlDraw.vertex2 (x0+.dx, y0+.dy);
5478 GlDraw.vertex2 (x1+.dx, y1+.dy);
5479 GlDraw.vertex2 (x2+.dx, y2+.dy);
5480 GlDraw.vertex2 (x3+.dx, y3+.dy);
5482 GlDraw.ends ();
5484 ) state.layout
5485 ) rects
5487 Gl.disable `blend;
5490 let display () =
5491 GlClear.color (scalecolor2 conf.bgcolor);
5492 GlClear.clear [`color];
5493 let rec loop linkindexbase = function
5494 | l :: rest ->
5495 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5496 loop linkindexbase rest
5497 | [] -> ()
5499 loop 0 state.layout;
5500 let rects =
5501 match state.mode with
5502 | LinkNav (Ltexact (pageno, linkno)) ->
5503 begin match getopaque pageno with
5504 | Some opaque ->
5505 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5506 (pageno, 5, (
5507 float x0, float y0,
5508 float x1, float y0,
5509 float x1, float y1,
5510 float x0, float y1)
5511 ) :: state.rects
5512 | None -> state.rects
5514 | _ -> state.rects
5516 showrects rects;
5517 showsel ();
5518 state.uioh#display;
5519 begin match state.mstate with
5520 | Mzoomrect ((x0, y0), (x1, y1)) ->
5521 Gl.enable `blend;
5522 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5523 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5524 GlDraw.rect (float x0, float y0)
5525 (float x1, float y1);
5526 Gl.disable `blend;
5527 | _ -> ()
5528 end;
5529 enttext ();
5530 scrollindicator ();
5531 Wsi.swapb ();
5534 let zoomrect x y x1 y1 =
5535 let x0 = min x x1
5536 and x1 = max x x1
5537 and y0 = min y y1 in
5538 gotoy (state.y + y0);
5539 state.anchor <- getanchor ();
5540 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5541 let margin =
5542 if state.w < conf.winw - state.scrollw
5543 then (conf.winw - state.scrollw - state.w) / 2
5544 else 0
5546 state.x <- (state.x + margin) - x0;
5547 setzoom zoom;
5548 Wsi.setcursor Wsi.CURSOR_INHERIT;
5549 state.mstate <- Mnone;
5552 let scrollx x =
5553 let winw = conf.winw - state.scrollw - 1 in
5554 let s = float x /. float winw in
5555 let destx = truncate (float (state.w + winw) *. s) in
5556 state.x <- winw - destx;
5557 gotoy_and_clear_text state.y;
5558 state.mstate <- Mscrollx;
5561 let scrolly y =
5562 let s = float y /. float conf.winh in
5563 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5564 gotoy_and_clear_text desty;
5565 state.mstate <- Mscrolly;
5568 let viewmouse button down x y mask =
5569 match button with
5570 | n when (n == 4 || n == 5) && not down ->
5571 if Wsi.withctrl mask
5572 then (
5573 match state.mstate with
5574 | Mzoom (oldn, i) ->
5575 if oldn = n
5576 then (
5577 if i = 2
5578 then
5579 let incr =
5580 match n with
5581 | 5 ->
5582 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5583 | _ ->
5584 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5586 let zoom = conf.zoom -. incr in
5587 setzoom zoom;
5588 state.mstate <- Mzoom (n, 0);
5589 else
5590 state.mstate <- Mzoom (n, i+1);
5592 else state.mstate <- Mzoom (n, 0)
5594 | _ -> state.mstate <- Mzoom (n, 0)
5596 else (
5597 match state.autoscroll with
5598 | Some step -> setautoscrollspeed step (n=4)
5599 | None ->
5600 let incr =
5601 if n = 4
5602 then -conf.scrollstep
5603 else conf.scrollstep
5605 let incr = incr * 2 in
5606 let y = clamp incr in
5607 gotoy_and_clear_text y
5610 | n when (n = 6 || n = 7) && not down && canpan () ->
5611 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5612 gotoy_and_clear_text state.y
5614 | 1 when Wsi.withctrl mask ->
5615 if down
5616 then (
5617 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5618 state.mstate <- Mpan (x, y)
5620 else
5621 state.mstate <- Mnone
5623 | 3 ->
5624 if down
5625 then (
5626 Wsi.setcursor Wsi.CURSOR_CYCLE;
5627 let p = (x, y) in
5628 state.mstate <- Mzoomrect (p, p)
5630 else (
5631 match state.mstate with
5632 | Mzoomrect ((x0, y0), _) ->
5633 if abs (x-x0) > 10 && abs (y - y0) > 10
5634 then zoomrect x0 y0 x y
5635 else (
5636 state.mstate <- Mnone;
5637 Wsi.setcursor Wsi.CURSOR_INHERIT;
5638 G.postRedisplay "kill accidental zoom rect";
5640 | _ ->
5641 Wsi.setcursor Wsi.CURSOR_INHERIT;
5642 state.mstate <- Mnone
5645 | 1 when x > conf.winw - state.scrollw ->
5646 if down
5647 then
5648 let _, position, sh = state.uioh#scrollph in
5649 if y > truncate position && y < truncate (position +. sh)
5650 then state.mstate <- Mscrolly
5651 else scrolly y
5652 else
5653 state.mstate <- Mnone
5655 | 1 when y > conf.winh - state.hscrollh ->
5656 if down
5657 then
5658 let _, position, sw = state.uioh#scrollpw in
5659 if x > truncate position && x < truncate (position +. sw)
5660 then state.mstate <- Mscrollx
5661 else scrollx x
5662 else
5663 state.mstate <- Mnone
5665 | 1 ->
5666 let dest = if down then getunder x y else Unone in
5667 begin match dest with
5668 | Ulinkgoto _
5669 | Ulinkuri _
5670 | Uremote _
5671 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5672 gotounder dest
5674 | Unone when down ->
5675 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5676 state.mstate <- Mpan (x, y);
5678 | Unone | Utext _ ->
5679 if down
5680 then (
5681 if conf.angle mod 360 = 0
5682 then (
5683 state.mstate <- Msel ((x, y), (x, y));
5684 G.postRedisplay "mouse select";
5687 else (
5688 match state.mstate with
5689 | Mnone -> ()
5691 | Mzoom _ | Mscrollx | Mscrolly ->
5692 state.mstate <- Mnone
5694 | Mzoomrect ((x0, y0), _) ->
5695 zoomrect x0 y0 x y
5697 | Mpan _ ->
5698 Wsi.setcursor Wsi.CURSOR_INHERIT;
5699 state.mstate <- Mnone
5701 | Msel ((_, y0), (_, y1)) ->
5702 let rec loop = function
5703 | [] -> ()
5704 | l :: rest ->
5705 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5706 || ((y1 >= l.pagedispy
5707 && y1 <= (l.pagedispy + l.pagevh)))
5708 then
5709 match getopaque l.pageno with
5710 | Some opaque ->
5711 begin
5712 match Ne.pipe () with
5713 | Ne.Exn exn ->
5714 showtext '!'
5715 (Printf.sprintf
5716 "can not create sel pipe: %s"
5717 (Printexc.to_string exn));
5718 | Ne.Res (r, w) ->
5719 let doclose what fd =
5720 Ne.clo fd (fun msg ->
5721 dolog "%s close failed: %s" what msg)
5724 popen conf.selcmd [r, 0; w, -1];
5725 copysel w opaque;
5726 doclose "pipe/r" r;
5727 G.postRedisplay "copysel";
5728 with exn ->
5729 dolog "can not execute %S: %s"
5730 conf.selcmd (Printexc.to_string exn);
5731 doclose "pipe/r" r;
5732 doclose "pipe/w" w;
5734 | None -> ()
5735 else loop rest
5737 loop state.layout;
5738 Wsi.setcursor Wsi.CURSOR_INHERIT;
5739 state.mstate <- Mnone;
5743 | _ -> ()
5746 let birdseyemouse button down x y mask
5747 (conf, leftx, _, hooverpageno, anchor) =
5748 match button with
5749 | 1 when down ->
5750 let rec loop = function
5751 | [] -> ()
5752 | l :: rest ->
5753 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5754 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5755 then (
5756 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5758 else loop rest
5760 loop state.layout
5761 | 3 -> ()
5762 | _ -> viewmouse button down x y mask
5765 let mouse button down x y mask =
5766 state.uioh <- state.uioh#button button down x y mask;
5769 let motion ~x ~y =
5770 state.uioh <- state.uioh#motion x y
5773 let pmotion ~x ~y =
5774 state.uioh <- state.uioh#pmotion x y;
5777 let uioh = object
5778 method display = ()
5780 method key key mask =
5781 begin match state.mode with
5782 | Textentry textentry -> textentrykeyboard key mask textentry
5783 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5784 | View -> viewkeyboard key mask
5785 | LinkNav linknav -> linknavkeyboard key mask linknav
5786 end;
5787 state.uioh
5789 method button button bstate x y mask =
5790 begin match state.mode with
5791 | LinkNav _
5792 | View -> viewmouse button bstate x y mask
5793 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5794 | Textentry _ -> ()
5795 end;
5796 state.uioh
5798 method motion x y =
5799 begin match state.mode with
5800 | Textentry _ -> ()
5801 | View | Birdseye _ | LinkNav _ ->
5802 match state.mstate with
5803 | Mzoom _ | Mnone -> ()
5805 | Mpan (x0, y0) ->
5806 let dx = x - x0
5807 and dy = y0 - y in
5808 state.mstate <- Mpan (x, y);
5809 if canpan ()
5810 then state.x <- state.x + dx;
5811 let y = clamp dy in
5812 gotoy_and_clear_text y
5814 | Msel (a, _) ->
5815 state.mstate <- Msel (a, (x, y));
5816 G.postRedisplay "motion select";
5818 | Mscrolly ->
5819 let y = min conf.winh (max 0 y) in
5820 scrolly y
5822 | Mscrollx ->
5823 let x = min conf.winw (max 0 x) in
5824 scrollx x
5826 | Mzoomrect (p0, _) ->
5827 state.mstate <- Mzoomrect (p0, (x, y));
5828 G.postRedisplay "motion zoomrect";
5829 end;
5830 state.uioh
5832 method pmotion x y =
5833 begin match state.mode with
5834 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5835 let rec loop = function
5836 | [] ->
5837 if hooverpageno != -1
5838 then (
5839 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5840 G.postRedisplay "pmotion birdseye no hoover";
5842 | l :: rest ->
5843 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5844 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5845 then (
5846 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5847 G.postRedisplay "pmotion birdseye hoover";
5849 else loop rest
5851 loop state.layout
5853 | Textentry _ -> ()
5855 | LinkNav _
5856 | View ->
5857 match state.mstate with
5858 | Mnone -> updateunder x y
5859 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5861 end;
5862 state.uioh
5864 method infochanged _ = ()
5866 method scrollph =
5867 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5868 let p, h = scrollph state.y maxy in
5869 state.scrollw, p, h
5871 method scrollpw =
5872 let winw = conf.winw - state.scrollw - 1 in
5873 let fwinw = float winw in
5874 let sw =
5875 let sw = fwinw /. float state.w in
5876 let sw = fwinw *. sw in
5877 max sw (float conf.scrollh)
5879 let position, sw =
5880 let f = state.w+winw in
5881 let r = float (winw-state.x) /. float f in
5882 let p = fwinw *. r in
5883 p-.sw/.2., sw
5885 let sw =
5886 if position +. sw > fwinw
5887 then fwinw -. position
5888 else sw
5890 state.hscrollh, position, sw
5892 method modehash =
5893 let modename =
5894 match state.mode with
5895 | LinkNav _ -> "links"
5896 | Textentry _ -> "textentry"
5897 | Birdseye _ -> "birdseye"
5898 | View -> "view"
5900 findkeyhash conf modename
5901 end;;
5903 module Config =
5904 struct
5905 open Parser
5907 let fontpath = ref "";;
5909 module KeyMap =
5910 Map.Make (struct type t = (int * int) let compare = compare end);;
5912 let unent s =
5913 let l = String.length s in
5914 let b = Buffer.create l in
5915 unent b s 0 l;
5916 Buffer.contents b;
5919 let home =
5920 try Sys.getenv "HOME"
5921 with exn ->
5922 prerr_endline
5923 ("Can not determine home directory location: " ^
5924 Printexc.to_string exn);
5928 let modifier_of_string = function
5929 | "alt" -> Wsi.altmask
5930 | "shift" -> Wsi.shiftmask
5931 | "ctrl" | "control" -> Wsi.ctrlmask
5932 | "meta" -> Wsi.metamask
5933 | _ -> 0
5936 let key_of_string =
5937 let r = Str.regexp "-" in
5938 fun s ->
5939 let elems = Str.full_split r s in
5940 let f n k m =
5941 let g s =
5942 let m1 = modifier_of_string s in
5943 if m1 = 0
5944 then (Wsi.namekey s, m)
5945 else (k, m lor m1)
5946 in function
5947 | Str.Delim s when n land 1 = 0 -> g s
5948 | Str.Text s -> g s
5949 | Str.Delim _ -> (k, m)
5951 let rec loop n k m = function
5952 | [] -> (k, m)
5953 | x :: xs ->
5954 let k, m = f n k m x in
5955 loop (n+1) k m xs
5957 loop 0 0 0 elems
5960 let keys_of_string =
5961 let r = Str.regexp "[ \t]" in
5962 fun s ->
5963 let elems = Str.split r s in
5964 List.map key_of_string elems
5967 let copykeyhashes c =
5968 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5971 let config_of c attrs =
5972 let apply c k v =
5974 match k with
5975 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5976 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5977 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5978 | "preload" -> { c with preload = bool_of_string v }
5979 | "page-bias" -> { c with pagebias = int_of_string v }
5980 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5981 | "horizontal-scroll-step" ->
5982 { c with hscrollstep = max (int_of_string v) 1 }
5983 | "auto-scroll-step" ->
5984 { c with autoscrollstep = max 0 (int_of_string v) }
5985 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5986 | "crop-hack" -> { c with crophack = bool_of_string v }
5987 | "throttle" ->
5988 let mw =
5989 match String.lowercase v with
5990 | "true" -> Some infinity
5991 | "false" -> None
5992 | f -> Some (float_of_string f)
5994 { c with maxwait = mw}
5995 | "highlight-links" -> { c with hlinks = bool_of_string v }
5996 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5997 | "vertical-margin" ->
5998 { c with interpagespace = max 0 (int_of_string v) }
5999 | "zoom" ->
6000 let zoom = float_of_string v /. 100. in
6001 let zoom = max zoom 0.0 in
6002 { c with zoom = zoom }
6003 | "presentation" -> { c with presentation = bool_of_string v }
6004 | "rotation-angle" -> { c with angle = int_of_string v }
6005 | "width" -> { c with winw = max 20 (int_of_string v) }
6006 | "height" -> { c with winh = max 20 (int_of_string v) }
6007 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6008 | "proportional-display" -> { c with proportional = bool_of_string v }
6009 | "pixmap-cache-size" ->
6010 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6011 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6012 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6013 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6014 | "persistent-location" -> { c with jumpback = bool_of_string v }
6015 | "background-color" -> { c with bgcolor = color_of_string v }
6016 | "scrollbar-in-presentation" ->
6017 { c with scrollbarinpm = bool_of_string v }
6018 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6019 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6020 | "mupdf-store-size" ->
6021 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6022 | "checkers" -> { c with checkers = bool_of_string v }
6023 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6024 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6025 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6026 | "uri-launcher" -> { c with urilauncher = unent v }
6027 | "path-launcher" -> { c with pathlauncher = unent v }
6028 | "color-space" -> { c with colorspace = colorspace_of_string v }
6029 | "invert-colors" -> { c with invert = bool_of_string v }
6030 | "brightness" -> { c with colorscale = float_of_string v }
6031 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6032 | "ghyllscroll" ->
6033 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6034 | "columns" ->
6035 let (n, _, _) as nab = multicolumns_of_string v in
6036 if n < 0
6037 then { c with columns = Csplit (-n, [||]) }
6038 else { c with columns = Cmulti (nab, [||]) }
6039 | "birds-eye-columns" ->
6040 { c with beyecolumns = Some (max (int_of_string v) 2) }
6041 | "selection-command" -> { c with selcmd = unent v }
6042 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6043 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6044 | _ -> c
6045 with exn ->
6046 prerr_endline ("Error processing attribute (`" ^
6047 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6050 let rec fold c = function
6051 | [] -> c
6052 | (k, v) :: rest ->
6053 let c = apply c k v in
6054 fold c rest
6056 fold { c with keyhashes = copykeyhashes c } attrs;
6059 let fromstring f pos n v d =
6060 try f v
6061 with exn ->
6062 dolog "Error processing attribute (%S=%S) at %d\n%s"
6063 n v pos (Printexc.to_string exn)
6068 let bookmark_of attrs =
6069 let rec fold title page rely = function
6070 | ("title", v) :: rest -> fold v page rely rest
6071 | ("page", v) :: rest -> fold title v rely rest
6072 | ("rely", v) :: rest -> fold title page v rest
6073 | _ :: rest -> fold title page rely rest
6074 | [] -> title, page, rely
6076 fold "invalid" "0" "0" attrs
6079 let doc_of attrs =
6080 let rec fold path page rely pan = function
6081 | ("path", v) :: rest -> fold v page rely pan rest
6082 | ("page", v) :: rest -> fold path v rely pan rest
6083 | ("rely", v) :: rest -> fold path page v pan rest
6084 | ("pan", v) :: rest -> fold path page rely v rest
6085 | _ :: rest -> fold path page rely pan rest
6086 | [] -> path, page, rely, pan
6088 fold "" "0" "0" "0" attrs
6091 let map_of attrs =
6092 let rec fold rs ls = function
6093 | ("out", v) :: rest -> fold v ls rest
6094 | ("in", v) :: rest -> fold rs v rest
6095 | _ :: rest -> fold ls rs rest
6096 | [] -> ls, rs
6098 fold "" "" attrs
6101 let setconf dst src =
6102 dst.scrollbw <- src.scrollbw;
6103 dst.scrollh <- src.scrollh;
6104 dst.icase <- src.icase;
6105 dst.preload <- src.preload;
6106 dst.pagebias <- src.pagebias;
6107 dst.verbose <- src.verbose;
6108 dst.scrollstep <- src.scrollstep;
6109 dst.maxhfit <- src.maxhfit;
6110 dst.crophack <- src.crophack;
6111 dst.autoscrollstep <- src.autoscrollstep;
6112 dst.maxwait <- src.maxwait;
6113 dst.hlinks <- src.hlinks;
6114 dst.underinfo <- src.underinfo;
6115 dst.interpagespace <- src.interpagespace;
6116 dst.zoom <- src.zoom;
6117 dst.presentation <- src.presentation;
6118 dst.angle <- src.angle;
6119 dst.winw <- src.winw;
6120 dst.winh <- src.winh;
6121 dst.savebmarks <- src.savebmarks;
6122 dst.memlimit <- src.memlimit;
6123 dst.proportional <- src.proportional;
6124 dst.texcount <- src.texcount;
6125 dst.sliceheight <- src.sliceheight;
6126 dst.thumbw <- src.thumbw;
6127 dst.jumpback <- src.jumpback;
6128 dst.bgcolor <- src.bgcolor;
6129 dst.scrollbarinpm <- src.scrollbarinpm;
6130 dst.tilew <- src.tilew;
6131 dst.tileh <- src.tileh;
6132 dst.mustoresize <- src.mustoresize;
6133 dst.checkers <- src.checkers;
6134 dst.aalevel <- src.aalevel;
6135 dst.trimmargins <- src.trimmargins;
6136 dst.trimfuzz <- src.trimfuzz;
6137 dst.urilauncher <- src.urilauncher;
6138 dst.colorspace <- src.colorspace;
6139 dst.invert <- src.invert;
6140 dst.colorscale <- src.colorscale;
6141 dst.redirectstderr <- src.redirectstderr;
6142 dst.ghyllscroll <- src.ghyllscroll;
6143 dst.columns <- src.columns;
6144 dst.beyecolumns <- src.beyecolumns;
6145 dst.selcmd <- src.selcmd;
6146 dst.updatecurs <- src.updatecurs;
6147 dst.pathlauncher <- src.pathlauncher;
6148 dst.keyhashes <- copykeyhashes src;
6149 dst.hfsize <- src.hfsize;
6150 dst.hscrollstep <- src.hscrollstep;
6153 let get s =
6154 let h = Hashtbl.create 10 in
6155 let dc = { defconf with angle = defconf.angle } in
6156 let rec toplevel v t spos _ =
6157 match t with
6158 | Vdata | Vcdata | Vend -> v
6159 | Vopen ("llppconfig", _, closed) ->
6160 if closed
6161 then v
6162 else { v with f = llppconfig }
6163 | Vopen _ ->
6164 error "unexpected subelement at top level" s spos
6165 | Vclose _ -> error "unexpected close at top level" s spos
6167 and llppconfig v t spos _ =
6168 match t with
6169 | Vdata | Vcdata -> v
6170 | Vend -> error "unexpected end of input in llppconfig" s spos
6171 | Vopen ("defaults", attrs, closed) ->
6172 let c = config_of dc attrs in
6173 setconf dc c;
6174 if closed
6175 then v
6176 else { v with f = defaults }
6178 | Vopen ("ui-font", attrs, closed) ->
6179 let rec getsize size = function
6180 | [] -> size
6181 | ("size", v) :: rest ->
6182 let size =
6183 fromstring int_of_string spos "size" v fstate.fontsize in
6184 getsize size rest
6185 | l -> getsize size l
6187 fstate.fontsize <- getsize fstate.fontsize attrs;
6188 if closed
6189 then v
6190 else { v with f = uifont (Buffer.create 10) }
6192 | Vopen ("doc", attrs, closed) ->
6193 let pathent, spage, srely, span = doc_of attrs in
6194 let path = unent pathent
6195 and pageno = fromstring int_of_string spos "page" spage 0
6196 and rely = fromstring float_of_string spos "rely" srely 0.0
6197 and pan = fromstring int_of_string spos "pan" span 0 in
6198 let c = config_of dc attrs in
6199 let anchor = (pageno, rely) in
6200 if closed
6201 then (Hashtbl.add h path (c, [], pan, anchor); v)
6202 else { v with f = doc path pan anchor c [] }
6204 | Vopen _ ->
6205 error "unexpected subelement in llppconfig" s spos
6207 | Vclose "llppconfig" -> { v with f = toplevel }
6208 | Vclose _ -> error "unexpected close in llppconfig" s spos
6210 and defaults v t spos _ =
6211 match t with
6212 | Vdata | Vcdata -> v
6213 | Vend -> error "unexpected end of input in defaults" s spos
6214 | Vopen ("keymap", attrs, closed) ->
6215 let modename =
6216 try List.assoc "mode" attrs
6217 with Not_found -> "global" in
6218 if closed
6219 then v
6220 else
6221 let ret keymap =
6222 let h = findkeyhash dc modename in
6223 KeyMap.iter (Hashtbl.replace h) keymap;
6224 defaults
6226 { v with f = pkeymap ret KeyMap.empty }
6228 | Vopen (_, _, _) ->
6229 error "unexpected subelement in defaults" s spos
6231 | Vclose "defaults" ->
6232 { v with f = llppconfig }
6234 | Vclose _ -> error "unexpected close in defaults" s spos
6236 and uifont b v t spos epos =
6237 match t with
6238 | Vdata | Vcdata ->
6239 Buffer.add_substring b s spos (epos - spos);
6241 | Vopen (_, _, _) ->
6242 error "unexpected subelement in ui-font" s spos
6243 | Vclose "ui-font" ->
6244 if String.length !fontpath = 0
6245 then fontpath := Buffer.contents b;
6246 { v with f = llppconfig }
6247 | Vclose _ -> error "unexpected close in ui-font" s spos
6248 | Vend -> error "unexpected end of input in ui-font" s spos
6250 and doc path pan anchor c bookmarks v t spos _ =
6251 match t with
6252 | Vdata | Vcdata -> v
6253 | Vend -> error "unexpected end of input in doc" s spos
6254 | Vopen ("bookmarks", _, closed) ->
6255 if closed
6256 then v
6257 else { v with f = pbookmarks path pan anchor c bookmarks }
6259 | Vopen ("keymap", attrs, closed) ->
6260 let modename =
6261 try List.assoc "mode" attrs
6262 with Not_found -> "global"
6264 if closed
6265 then v
6266 else
6267 let ret keymap =
6268 let h = findkeyhash c modename in
6269 KeyMap.iter (Hashtbl.replace h) keymap;
6270 doc path pan anchor c bookmarks
6272 { v with f = pkeymap ret KeyMap.empty }
6274 | Vopen (_, _, _) ->
6275 error "unexpected subelement in doc" s spos
6277 | Vclose "doc" ->
6278 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6279 { v with f = llppconfig }
6281 | Vclose _ -> error "unexpected close in doc" s spos
6283 and pkeymap ret keymap v t spos _ =
6284 match t with
6285 | Vdata | Vcdata -> v
6286 | Vend -> error "unexpected end of input in keymap" s spos
6287 | Vopen ("map", attrs, closed) ->
6288 let r, l = map_of attrs in
6289 let kss = fromstring keys_of_string spos "in" r [] in
6290 let lss = fromstring keys_of_string spos "out" l [] in
6291 let keymap =
6292 match kss with
6293 | [] -> keymap
6294 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6295 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6297 if closed
6298 then { v with f = pkeymap ret keymap }
6299 else
6300 let f () = v in
6301 { v with f = skip "map" f }
6303 | Vopen _ ->
6304 error "unexpected subelement in keymap" s spos
6306 | Vclose "keymap" ->
6307 { v with f = ret keymap }
6309 | Vclose _ -> error "unexpected close in keymap" s spos
6311 and pbookmarks path pan anchor c bookmarks v t spos _ =
6312 match t with
6313 | Vdata | Vcdata -> v
6314 | Vend -> error "unexpected end of input in bookmarks" s spos
6315 | Vopen ("item", attrs, closed) ->
6316 let titleent, spage, srely = bookmark_of attrs in
6317 let page = fromstring int_of_string spos "page" spage 0
6318 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6319 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6320 if closed
6321 then { v with f = pbookmarks path pan anchor c bookmarks }
6322 else
6323 let f () = v in
6324 { v with f = skip "item" f }
6326 | Vopen _ ->
6327 error "unexpected subelement in bookmarks" s spos
6329 | Vclose "bookmarks" ->
6330 { v with f = doc path pan anchor c bookmarks }
6332 | Vclose _ -> error "unexpected close in bookmarks" s spos
6334 and skip tag f v t spos _ =
6335 match t with
6336 | Vdata | Vcdata -> v
6337 | Vend ->
6338 error ("unexpected end of input in skipped " ^ tag) s spos
6339 | Vopen (tag', _, closed) ->
6340 if closed
6341 then v
6342 else
6343 let f' () = { v with f = skip tag f } in
6344 { v with f = skip tag' f' }
6345 | Vclose ctag ->
6346 if tag = ctag
6347 then f ()
6348 else error ("unexpected close in skipped " ^ tag) s spos
6351 parse { f = toplevel; accu = () } s;
6352 h, dc;
6355 let do_load f ic =
6357 let len = in_channel_length ic in
6358 let s = String.create len in
6359 really_input ic s 0 len;
6360 f s;
6361 with
6362 | Parse_error (msg, s, pos) ->
6363 let subs = subs s pos in
6364 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6365 failwith ("parse error: " ^ s)
6367 | exn ->
6368 failwith ("config load error: " ^ Printexc.to_string exn)
6371 let defconfpath =
6372 let dir =
6374 let dir = Filename.concat home ".config" in
6375 if Sys.is_directory dir then dir else home
6376 with _ -> home
6378 Filename.concat dir "llpp.conf"
6381 let confpath = ref defconfpath;;
6383 let load1 f =
6384 if Sys.file_exists !confpath
6385 then
6386 match
6387 (try Some (open_in_bin !confpath)
6388 with exn ->
6389 prerr_endline
6390 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6391 Printexc.to_string exn);
6392 None
6394 with
6395 | Some ic ->
6396 begin try
6397 f (do_load get ic)
6398 with exn ->
6399 prerr_endline
6400 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6401 Printexc.to_string exn);
6402 end;
6403 close_in ic;
6405 | None -> ()
6406 else
6407 f (Hashtbl.create 0, defconf)
6410 let load () =
6411 let f (h, dc) =
6412 let pc, pb, px, pa =
6414 Hashtbl.find h (Filename.basename state.path)
6415 with Not_found -> dc, [], 0, (0, 0.0)
6417 setconf defconf dc;
6418 setconf conf pc;
6419 state.bookmarks <- pb;
6420 state.x <- px;
6421 state.scrollw <- conf.scrollbw;
6422 if conf.jumpback
6423 then state.anchor <- pa;
6424 cbput state.hists.nav pa;
6426 load1 f
6429 let add_attrs bb always dc c =
6430 let ob s a b =
6431 if always || a != b
6432 then Printf.bprintf bb "\n %s='%b'" s a
6433 and oi s a b =
6434 if always || a != b
6435 then Printf.bprintf bb "\n %s='%d'" s a
6436 and oI s a b =
6437 if always || a != b
6438 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6439 and oz s a b =
6440 if always || a <> b
6441 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6442 and oF s a b =
6443 if always || a <> b
6444 then Printf.bprintf bb "\n %s='%f'" s a
6445 and oc s a b =
6446 if always || a <> b
6447 then
6448 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6449 and oC s a b =
6450 if always || a <> b
6451 then
6452 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6453 and oR s a b =
6454 if always || a <> b
6455 then
6456 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6457 and os s a b =
6458 if always || a <> b
6459 then
6460 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6461 and og s a b =
6462 if always || a <> b
6463 then
6464 match a with
6465 | None -> ()
6466 | Some (_N, _A, _B) ->
6467 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6468 and oW s a b =
6469 if always || a <> b
6470 then
6471 let v =
6472 match a with
6473 | None -> "false"
6474 | Some f ->
6475 if f = infinity
6476 then "true"
6477 else string_of_float f
6479 Printf.bprintf bb "\n %s='%s'" s v
6480 and oco s a b =
6481 if always || a <> b
6482 then
6483 match a with
6484 | Cmulti ((n, a, b), _) when n > 1 ->
6485 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6486 | Csplit (n, _) when n > 1 ->
6487 Printf.bprintf bb "\n %s='%d'" s ~-n
6488 | _ -> ()
6489 and obeco s a b =
6490 if always || a <> b
6491 then
6492 match a with
6493 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6494 | _ -> ()
6496 let w, h =
6497 if always
6498 then dc.winw, dc.winh
6499 else
6500 match state.fullscreen with
6501 | Some wh -> wh
6502 | None -> c.winw, c.winh
6504 let zoom, presentation, interpagespace, maxwait =
6505 if always
6506 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6507 else
6508 match state.mode with
6509 | Birdseye (bc, _, _, _, _) ->
6510 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6511 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6513 oi "width" w dc.winw;
6514 oi "height" h dc.winh;
6515 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6516 oi "scroll-handle-height" c.scrollh dc.scrollh;
6517 ob "case-insensitive-search" c.icase dc.icase;
6518 ob "preload" c.preload dc.preload;
6519 oi "page-bias" c.pagebias dc.pagebias;
6520 oi "scroll-step" c.scrollstep dc.scrollstep;
6521 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6522 ob "max-height-fit" c.maxhfit dc.maxhfit;
6523 ob "crop-hack" c.crophack dc.crophack;
6524 oW "throttle" maxwait dc.maxwait;
6525 ob "highlight-links" c.hlinks dc.hlinks;
6526 ob "under-cursor-info" c.underinfo dc.underinfo;
6527 oi "vertical-margin" interpagespace dc.interpagespace;
6528 oz "zoom" zoom dc.zoom;
6529 ob "presentation" presentation dc.presentation;
6530 oi "rotation-angle" c.angle dc.angle;
6531 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6532 ob "proportional-display" c.proportional dc.proportional;
6533 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6534 oi "tex-count" c.texcount dc.texcount;
6535 oi "slice-height" c.sliceheight dc.sliceheight;
6536 oi "thumbnail-width" c.thumbw dc.thumbw;
6537 ob "persistent-location" c.jumpback dc.jumpback;
6538 oc "background-color" c.bgcolor dc.bgcolor;
6539 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6540 oi "tile-width" c.tilew dc.tilew;
6541 oi "tile-height" c.tileh dc.tileh;
6542 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6543 ob "checkers" c.checkers dc.checkers;
6544 oi "aalevel" c.aalevel dc.aalevel;
6545 ob "trim-margins" c.trimmargins dc.trimmargins;
6546 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6547 os "uri-launcher" c.urilauncher dc.urilauncher;
6548 os "path-launcher" c.pathlauncher dc.pathlauncher;
6549 oC "color-space" c.colorspace dc.colorspace;
6550 ob "invert-colors" c.invert dc.invert;
6551 oF "brightness" c.colorscale dc.colorscale;
6552 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6553 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6554 oco "columns" c.columns dc.columns;
6555 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6556 os "selection-command" c.selcmd dc.selcmd;
6557 ob "update-cursor" c.updatecurs dc.updatecurs;
6558 oi "hint-font-size" c.hfsize dc.hfsize;
6559 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6562 let keymapsbuf always dc c =
6563 let bb = Buffer.create 16 in
6564 let rec loop = function
6565 | [] -> ()
6566 | (modename, h) :: rest ->
6567 let dh = findkeyhash dc modename in
6568 if always || h <> dh
6569 then (
6570 if Hashtbl.length h > 0
6571 then (
6572 if Buffer.length bb > 0
6573 then Buffer.add_char bb '\n';
6574 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6575 Hashtbl.iter (fun i o ->
6576 let isdifferent = always ||
6578 let dO = Hashtbl.find dh i in
6579 dO <> o
6580 with Not_found -> true
6582 if isdifferent
6583 then
6584 let addkm (k, m) =
6585 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6586 if Wsi.withalt m then Buffer.add_string bb "alt-";
6587 if Wsi.withshift m then Buffer.add_string bb "shift-";
6588 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6589 Buffer.add_string bb (Wsi.keyname k);
6591 let addkms l =
6592 let rec loop = function
6593 | [] -> ()
6594 | km :: [] -> addkm km
6595 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6597 loop l
6599 Buffer.add_string bb "<map in='";
6600 addkm i;
6601 match o with
6602 | KMinsrt km ->
6603 Buffer.add_string bb "' out='";
6604 addkm km;
6605 Buffer.add_string bb "'/>\n"
6607 | KMinsrl kms ->
6608 Buffer.add_string bb "' out='";
6609 addkms kms;
6610 Buffer.add_string bb "'/>\n"
6612 | KMmulti (ins, kms) ->
6613 Buffer.add_char bb ' ';
6614 addkms ins;
6615 Buffer.add_string bb "' out='";
6616 addkms kms;
6617 Buffer.add_string bb "'/>\n"
6618 ) h;
6619 Buffer.add_string bb "</keymap>";
6622 loop rest
6624 loop c.keyhashes;
6628 let save () =
6629 let uifontsize = fstate.fontsize in
6630 let bb = Buffer.create 32768 in
6631 let f (h, dc) =
6632 let dc = if conf.bedefault then conf else dc in
6633 Buffer.add_string bb "<llppconfig>\n";
6635 if String.length !fontpath > 0
6636 then
6637 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6638 uifontsize
6639 !fontpath
6640 else (
6641 if uifontsize <> 14
6642 then
6643 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6646 Buffer.add_string bb "<defaults ";
6647 add_attrs bb true dc dc;
6648 let kb = keymapsbuf true dc dc in
6649 if Buffer.length kb > 0
6650 then (
6651 Buffer.add_string bb ">\n";
6652 Buffer.add_buffer bb kb;
6653 Buffer.add_string bb "\n</defaults>\n";
6655 else Buffer.add_string bb "/>\n";
6657 let adddoc path pan anchor c bookmarks =
6658 if bookmarks == [] && c = dc && anchor = emptyanchor
6659 then ()
6660 else (
6661 Printf.bprintf bb "<doc path='%s'"
6662 (enent path 0 (String.length path));
6664 if anchor <> emptyanchor
6665 then (
6666 let n, y = anchor in
6667 Printf.bprintf bb " page='%d'" n;
6668 if y > 1e-6
6669 then
6670 Printf.bprintf bb " rely='%f'" y
6674 if pan != 0
6675 then Printf.bprintf bb " pan='%d'" pan;
6677 add_attrs bb false dc c;
6678 let kb = keymapsbuf false dc c in
6680 begin match bookmarks with
6681 | [] ->
6682 if Buffer.length kb > 0
6683 then (
6684 Buffer.add_string bb ">\n";
6685 Buffer.add_buffer bb kb;
6686 Buffer.add_string bb "\n</doc>\n";
6688 else Buffer.add_string bb "/>\n"
6689 | _ ->
6690 Buffer.add_string bb ">\n<bookmarks>\n";
6691 List.iter (fun (title, _level, (page, rely)) ->
6692 Printf.bprintf bb
6693 "<item title='%s' page='%d'"
6694 (enent title 0 (String.length title))
6695 page
6697 if rely > 1e-6
6698 then
6699 Printf.bprintf bb " rely='%f'" rely
6701 Buffer.add_string bb "/>\n";
6702 ) bookmarks;
6703 Buffer.add_string bb "</bookmarks>";
6704 if Buffer.length kb > 0
6705 then (
6706 Buffer.add_string bb "\n";
6707 Buffer.add_buffer bb kb;
6709 Buffer.add_string bb "\n</doc>\n";
6710 end;
6714 let pan, conf =
6715 match state.mode with
6716 | Birdseye (c, pan, _, _, _) ->
6717 let beyecolumns =
6718 match conf.columns with
6719 | Cmulti ((c, _, _), _) -> Some c
6720 | Csingle -> None
6721 | Csplit _ -> None
6722 and columns =
6723 match c.columns with
6724 | Cmulti (c, _) -> Cmulti (c, [||])
6725 | Csingle -> Csingle
6726 | Csplit _ -> failwith "quit from bird's eye while split"
6728 pan, { c with beyecolumns = beyecolumns; columns = columns }
6729 | _ -> state.x, conf
6731 let basename = Filename.basename state.path in
6732 adddoc basename pan (getanchor ())
6733 { conf with
6734 autoscrollstep =
6735 match state.autoscroll with
6736 | Some step -> step
6737 | None -> conf.autoscrollstep }
6738 (if conf.savebmarks then state.bookmarks else []);
6740 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6741 if basename <> path
6742 then adddoc path x y c bookmarks
6743 ) h;
6744 Buffer.add_string bb "</llppconfig>";
6746 load1 f;
6747 if Buffer.length bb > 0
6748 then
6750 let tmp = !confpath ^ ".tmp" in
6751 let oc = open_out_bin tmp in
6752 Buffer.output_buffer oc bb;
6753 close_out oc;
6754 Unix.rename tmp !confpath;
6755 with exn ->
6756 prerr_endline
6757 ("error while saving configuration: " ^ Printexc.to_string exn)
6759 end;;
6761 let () =
6762 Arg.parse
6763 (Arg.align
6764 [("-p", Arg.String (fun s -> state.password <- s) ,
6765 "<password> Set password");
6767 ("-f", Arg.String (fun s -> Config.fontpath := s),
6768 "<path> Set path to the user interface font");
6770 ("-c", Arg.String (fun s -> Config.confpath := s),
6771 "<path> Set path to the configuration file");
6773 ("-v", Arg.Unit (fun () ->
6774 Printf.printf
6775 "%s\nconfiguration path: %s\n"
6776 (version ())
6777 Config.defconfpath
6779 exit 0), " Print version and exit");
6782 (fun s -> state.path <- s)
6783 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6785 if String.length state.path = 0
6786 then (prerr_endline "file name missing"; exit 1);
6788 Config.load ();
6790 let globalkeyhash = findkeyhash conf "global" in
6791 let wsfd, winw, winh = Wsi.init (object
6792 method expose =
6793 if nogeomcmds state.geomcmds || platform == Posx
6794 then display ()
6795 else (
6796 GlClear.color (scalecolor2 conf.bgcolor);
6797 GlClear.clear [`color];
6799 method display = display ()
6800 method reshape w h = reshape w h
6801 method mouse b d x y m = mouse b d x y m
6802 method motion x y = state.mpos <- (x, y); motion x y
6803 method pmotion x y = state.mpos <- (x, y); pmotion x y
6804 method key k m =
6805 let mascm = m land (
6806 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6807 ) in
6808 match state.keystate with
6809 | KSnone ->
6810 let km = k, mascm in
6811 begin
6812 match
6813 let modehash = state.uioh#modehash in
6814 try Hashtbl.find modehash km
6815 with Not_found ->
6816 try Hashtbl.find globalkeyhash km
6817 with Not_found -> KMinsrt (k, m)
6818 with
6819 | KMinsrt (k, m) -> keyboard k m
6820 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6821 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6823 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6824 List.iter (fun (k, m) -> keyboard k m) insrt;
6825 state.keystate <- KSnone
6826 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6827 state.keystate <- KSinto (keys, insrt)
6828 | _ ->
6829 state.keystate <- KSnone
6831 method enter x y = state.mpos <- (x, y); pmotion x y
6832 method leave = state.mpos <- (-1, -1)
6833 method quit = raise Quit
6834 end) conf.winw conf.winh (platform = Posx) in
6836 state.wsfd <- wsfd;
6838 if not (
6839 List.exists GlMisc.check_extension
6840 [ "GL_ARB_texture_rectangle"
6841 ; "GL_EXT_texture_recangle"
6842 ; "GL_NV_texture_rectangle" ]
6844 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6846 let cr, sw =
6847 match Ne.pipe () with
6848 | Ne.Exn exn ->
6849 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6850 exit 1
6851 | Ne.Res rw -> rw
6852 and sr, cw =
6853 match Ne.pipe () with
6854 | Ne.Exn exn ->
6855 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6856 exit 1
6857 | Ne.Res rw -> rw
6860 cloexec cr;
6861 cloexec sw;
6862 cloexec sr;
6863 cloexec cw;
6865 setcheckers conf.checkers;
6866 redirectstderr ();
6868 init (cr, cw) (
6869 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6870 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6871 !Config.fontpath
6873 state.sr <- sr;
6874 state.sw <- sw;
6875 state.text <- "Opening " ^ state.path;
6876 reshape winw winh;
6877 opendoc state.path state.password;
6878 state.uioh <- uioh;
6880 adjust_brightness ();
6881 let rec loop deadline =
6882 let r =
6883 match state.errfd with
6884 | None -> [state.sr; state.wsfd]
6885 | Some fd -> [state.sr; state.wsfd; fd]
6887 if state.redisplay
6888 then (
6889 state.redisplay <- false;
6890 display ();
6892 let timeout =
6893 let now = now () in
6894 if deadline > now
6895 then (
6896 if deadline = infinity
6897 then ~-.1.0
6898 else max 0.0 (deadline -. now)
6900 else 0.0
6902 let r, _, _ =
6903 try Unix.select r [] [] timeout
6904 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6906 begin match r with
6907 | [] ->
6908 state.ghyll None;
6909 let newdeadline =
6910 if state.ghyll == noghyll
6911 then
6912 match state.autoscroll with
6913 | Some step when step != 0 ->
6914 let y = state.y + step in
6915 let y =
6916 if y < 0
6917 then state.maxy
6918 else if y >= state.maxy then 0 else y
6920 gotoy y;
6921 if state.mode = View
6922 then state.text <- "";
6923 deadline +. 0.01
6924 | _ -> infinity
6925 else deadline +. 0.01
6927 loop newdeadline
6929 | l ->
6930 let rec checkfds = function
6931 | [] -> ()
6932 | fd :: rest when fd = state.sr ->
6933 let cmd = readcmd state.sr in
6934 act cmd;
6935 checkfds rest
6937 | fd :: rest when fd = state.wsfd ->
6938 Wsi.readresp fd;
6939 checkfds rest
6941 | fd :: rest ->
6942 let s = String.create 80 in
6943 let n = Unix.read fd s 0 80 in
6944 if conf.redirectstderr
6945 then (
6946 Buffer.add_substring state.errmsgs s 0 n;
6947 state.newerrmsgs <- true;
6948 state.redisplay <- true;
6950 else (
6951 prerr_string (String.sub s 0 n);
6952 flush stderr;
6954 checkfds rest
6956 checkfds l;
6957 let newdeadline =
6958 let deadline1 =
6959 if deadline = infinity
6960 then now () +. 0.01
6961 else deadline
6963 match state.autoscroll with
6964 | Some step when step != 0 -> deadline1
6965 | _ -> if state.ghyll == noghyll then infinity else deadline1
6967 loop newdeadline
6968 end;
6971 loop infinity;
6972 with Quit ->
6973 Config.save ();