Make hint font size user configurable
[llpp.git] / main.ml
blob9f6ac10aec7e5a7d99afff6e602bc6f66b2b2b95
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 -> 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
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 te =
160 | TEstop
161 | TEdone of string
162 | TEcont of string
163 | TEswitch of textentry
166 type 'a circbuf =
167 { store : 'a array
168 ; mutable rc : int
169 ; mutable wc : int
170 ; mutable len : int
174 let bound v minv maxv =
175 max minv (min maxv v);
178 let cbnew n v =
179 { store = Array.create n v
180 ; rc = 0
181 ; wc = 0
182 ; len = 0
186 let drawstring size x y s =
187 Gl.enable `blend;
188 Gl.enable `texture_2d;
189 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
190 ignore (drawstr size x y s);
191 Gl.disable `blend;
192 Gl.disable `texture_2d;
195 let drawstring1 size x y s =
196 drawstr size x y s;
199 let drawstring2 size x y fmt =
200 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
203 let cbcap b = Array.length b.store;;
205 let cbput b v =
206 let cap = cbcap b in
207 b.store.(b.wc) <- v;
208 b.wc <- (b.wc + 1) mod cap;
209 b.rc <- b.wc;
210 b.len <- min (b.len + 1) cap;
213 let cbempty b = b.len = 0;;
215 let cbgetg b circular dir =
216 if cbempty b
217 then b.store.(0)
218 else
219 let rc = b.rc + dir in
220 let rc =
221 if circular
222 then (
223 if rc = -1
224 then b.len-1
225 else (
226 if rc = b.len
227 then 0
228 else rc
231 else max 0 (min rc (b.len-1))
233 b.rc <- rc;
234 b.store.(rc);
237 let cbget b = cbgetg b false;;
238 let cbgetc b = cbgetg b true;;
240 type page =
241 { pageno : int
242 ; pagedimno : int
243 ; pagew : int
244 ; pageh : int
245 ; pagex : int
246 ; pagey : int
247 ; pagevw : int
248 ; pagevh : int
249 ; pagedispx : int
250 ; pagedispy : int
251 ; pagecol : int
255 let debugl l =
256 dolog "l %d dim=%d {" l.pageno l.pagedimno;
257 dolog " WxH %dx%d" l.pagew l.pageh;
258 dolog " vWxH %dx%d" l.pagevw l.pagevh;
259 dolog " pagex,y %d,%d" l.pagex l.pagey;
260 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
261 dolog " column %d" l.pagecol;
262 dolog "}";
265 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
266 dolog "rect {";
267 dolog " x0,y0=(% f, % f)" x0 y0;
268 dolog " x1,y1=(% f, % f)" x1 y1;
269 dolog " x2,y2=(% f, % f)" x2 y2;
270 dolog " x3,y3=(% f, % f)" x3 y3;
271 dolog "}";
274 type multicolumns = multicol * pagegeom
275 and splitcolumns = columncount * pagegeom
276 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
277 and multicol = columncount * covercount * covercount
278 and pdimno = int
279 and columncount = int
280 and covercount = int;;
282 type conf =
283 { mutable scrollbw : int
284 ; mutable scrollh : int
285 ; mutable icase : bool
286 ; mutable preload : bool
287 ; mutable pagebias : int
288 ; mutable verbose : bool
289 ; mutable debug : bool
290 ; mutable scrollstep : int
291 ; mutable maxhfit : bool
292 ; mutable crophack : bool
293 ; mutable autoscrollstep : int
294 ; mutable maxwait : float option
295 ; mutable hlinks : bool
296 ; mutable underinfo : bool
297 ; mutable interpagespace : interpagespace
298 ; mutable zoom : float
299 ; mutable presentation : bool
300 ; mutable angle : angle
301 ; mutable winw : int
302 ; mutable winh : int
303 ; mutable savebmarks : bool
304 ; mutable proportional : proportional
305 ; mutable trimmargins : trimmargins
306 ; mutable trimfuzz : irect
307 ; mutable memlimit : memsize
308 ; mutable texcount : texcount
309 ; mutable sliceheight : sliceheight
310 ; mutable thumbw : width
311 ; mutable jumpback : bool
312 ; mutable bgcolor : float * float * float
313 ; mutable bedefault : bool
314 ; mutable scrollbarinpm : bool
315 ; mutable tilew : int
316 ; mutable tileh : int
317 ; mutable mustoresize : memsize
318 ; mutable checkers : bool
319 ; mutable aalevel : int
320 ; mutable urilauncher : string
321 ; mutable pathlauncher : string
322 ; mutable colorspace : colorspace
323 ; mutable invert : bool
324 ; mutable colorscale : float
325 ; mutable redirectstderr : bool
326 ; mutable ghyllscroll : (int * int * int) option
327 ; mutable columns : columns
328 ; mutable beyecolumns : columncount option
329 ; mutable selcmd : string
330 ; mutable updatecurs : bool
331 ; mutable keyhashes : (string * keyhash) list
332 ; mutable hfsize : int
334 and columns =
335 | Csingle
336 | Cmulti of multicolumns
337 | Csplit of splitcolumns
340 type anchor = pageno * top;;
342 type outline = string * int * anchor;;
344 type rect = float * float * float * float * float * float * float * float;;
346 type tile = opaque * pixmapsize * elapsed
347 and elapsed = float;;
348 type pagemapkey = pageno * gen;;
349 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
350 and row = int
351 and col = int;;
353 let emptyanchor = (0, 0.0);;
355 type infochange = | Memused | Docinfo | Pdim;;
357 class type uioh = object
358 method display : unit
359 method key : int -> int -> uioh
360 method button : int -> bool -> int -> int -> int -> uioh
361 method motion : int -> int -> uioh
362 method pmotion : int -> int -> uioh
363 method infochanged : infochange -> unit
364 method scrollpw : (int * float * float)
365 method scrollph : (int * float * float)
366 method modehash : keyhash
367 end;;
369 type mode =
370 | Birdseye of (conf * leftx * pageno * pageno * anchor)
371 | Textentry of (textentry * onleave)
372 | View
373 | LinkNav of linktarget
374 and onleave = leavetextentrystatus -> unit
375 and leavetextentrystatus = | Cancel | Confirm
376 and helpitem = string * int * action
377 and action =
378 | Noaction
379 | Action of (uioh -> uioh)
380 and linktarget =
381 | Ltexact of (pageno * int)
382 | Ltgendir of int
385 let isbirdseye = function Birdseye _ -> true | _ -> false;;
386 let istextentry = function Textentry _ -> true | _ -> false;;
388 type currently =
389 | Idle
390 | Loading of (page * gen)
391 | Tiling of (
392 page * opaque * colorspace * angle * gen * col * row * width * height
394 | Outlining of outline list
397 let emptykeyhash = Hashtbl.create 0;;
398 let nouioh : uioh = object (self)
399 method display = ()
400 method key _ _ = self
401 method button _ _ _ _ _ = self
402 method motion _ _ = self
403 method pmotion _ _ = self
404 method infochanged _ = ()
405 method scrollpw = (0, nan, nan)
406 method scrollph = (0, nan, nan)
407 method modehash = emptykeyhash
408 end;;
410 type state =
411 { mutable sr : Unix.file_descr
412 ; mutable sw : Unix.file_descr
413 ; mutable wsfd : Unix.file_descr
414 ; mutable errfd : Unix.file_descr option
415 ; mutable stderr : Unix.file_descr
416 ; mutable errmsgs : Buffer.t
417 ; mutable newerrmsgs : bool
418 ; mutable w : int
419 ; mutable x : int
420 ; mutable y : int
421 ; mutable scrollw : int
422 ; mutable hscrollh : int
423 ; mutable anchor : anchor
424 ; mutable ranchors : (string * string * anchor) list
425 ; mutable maxy : int
426 ; mutable layout : page list
427 ; pagemap : (pagemapkey, opaque) Hashtbl.t
428 ; tilemap : (tilemapkey, tile) Hashtbl.t
429 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
430 ; mutable pdims : (pageno * width * height * leftx) list
431 ; mutable pagecount : int
432 ; mutable currently : currently
433 ; mutable mstate : mstate
434 ; mutable searchpattern : string
435 ; mutable rects : (pageno * recttype * rect) list
436 ; mutable rects1 : (pageno * recttype * rect) list
437 ; mutable text : string
438 ; mutable fullscreen : (width * height) option
439 ; mutable mode : mode
440 ; mutable uioh : uioh
441 ; mutable outlines : outline array
442 ; mutable bookmarks : outline list
443 ; mutable path : string
444 ; mutable password : string
445 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
446 ; mutable memused : memsize
447 ; mutable gen : gen
448 ; mutable throttle : (page list * int * float) option
449 ; mutable autoscroll : int option
450 ; mutable ghyll : (int option -> unit)
451 ; mutable help : helpitem array
452 ; mutable docinfo : (int * string) list
453 ; mutable texid : GlTex.texture_id option
454 ; hists : hists
455 ; mutable prevzoom : float
456 ; mutable progress : float
457 ; mutable redisplay : bool
458 ; mutable mpos : mpos
459 ; mutable keystate : keystate
460 ; mutable glinks : bool
462 and hists =
463 { pat : string circbuf
464 ; pag : string circbuf
465 ; nav : anchor circbuf
466 ; sel : string circbuf
470 let defconf =
471 { scrollbw = 7
472 ; scrollh = 12
473 ; icase = true
474 ; preload = true
475 ; pagebias = 0
476 ; verbose = false
477 ; debug = false
478 ; scrollstep = 24
479 ; maxhfit = true
480 ; crophack = false
481 ; autoscrollstep = 2
482 ; maxwait = None
483 ; hlinks = false
484 ; underinfo = false
485 ; interpagespace = 2
486 ; zoom = 1.0
487 ; presentation = false
488 ; angle = 0
489 ; winw = 900
490 ; winh = 900
491 ; savebmarks = true
492 ; proportional = true
493 ; trimmargins = false
494 ; trimfuzz = (0,0,0,0)
495 ; memlimit = 32 lsl 20
496 ; texcount = 256
497 ; sliceheight = 24
498 ; thumbw = 76
499 ; jumpback = true
500 ; bgcolor = (0.5, 0.5, 0.5)
501 ; bedefault = false
502 ; scrollbarinpm = true
503 ; tilew = 2048
504 ; tileh = 2048
505 ; mustoresize = 256 lsl 20
506 ; checkers = true
507 ; aalevel = 8
508 ; urilauncher =
509 (match platform with
510 | Plinux | Pfreebsd | Pdragonflybsd
511 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
512 | Posx -> "open \"%s\""
513 | Pcygwin -> "cygstart \"%s\""
514 | Punknown -> "echo %s")
515 ; pathlauncher = "lp \"%s\""
516 ; selcmd =
517 (match platform with
518 | Plinux | Pfreebsd | Pdragonflybsd
519 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
520 | Posx -> "pbcopy"
521 | Pcygwin -> "wsel"
522 | Punknown -> "cat")
523 ; colorspace = Rgb
524 ; invert = false
525 ; colorscale = 1.0
526 ; redirectstderr = false
527 ; ghyllscroll = None
528 ; columns = Csingle
529 ; beyecolumns = None
530 ; updatecurs = false
531 ; hfsize = 12
532 ; keyhashes =
533 let mk n = (n, Hashtbl.create 1) in
534 [ mk "global"
535 ; mk "info"
536 ; mk "help"
537 ; mk "outline"
538 ; mk "listview"
539 ; mk "birdseye"
540 ; mk "textentry"
541 ; mk "links"
546 let findkeyhash c name =
547 try List.assoc name c.keyhashes
548 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
551 let conf = { defconf with angle = defconf.angle };;
553 type fontstate =
554 { mutable fontsize : int
555 ; mutable wwidth : float
556 ; mutable maxrows : int
560 let fstate =
561 { fontsize = 14
562 ; wwidth = nan
563 ; maxrows = -1
567 let setfontsize n =
568 fstate.fontsize <- n;
569 fstate.wwidth <- measurestr fstate.fontsize "w";
570 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
573 let geturl s =
574 let colonpos = try String.index s ':' with Not_found -> -1 in
575 let len = String.length s in
576 if colonpos >= 0 && colonpos + 3 < len
577 then (
578 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
579 then
580 let schemestartpos =
581 try String.rindex_from s colonpos ' '
582 with Not_found -> -1
584 let scheme =
585 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
587 match scheme with
588 | "http" | "ftp" | "mailto" ->
589 let epos =
590 try String.index_from s colonpos ' '
591 with Not_found -> len
593 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
594 | _ -> ""
595 else ""
597 else ""
600 let gotouri uri =
601 if String.length conf.urilauncher = 0
602 then print_endline uri
603 else (
604 let url = geturl uri in
605 if String.length url = 0
606 then print_endline uri
607 else
608 let re = Str.regexp "%s" in
609 let command = Str.global_replace re url conf.urilauncher in
610 try popen command []
611 with exn ->
612 Printf.eprintf
613 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
614 flush stderr;
618 let version () =
619 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
620 (platform_to_string platform) Sys.word_size Sys.ocaml_version
623 let makehelp () =
624 let strings = version () :: "" :: Help.keys in
625 Array.of_list (
626 List.map (fun s ->
627 let url = geturl s in
628 if String.length url > 0
629 then (s, 0, Action (fun u -> gotouri url; u))
630 else (s, 0, Noaction)
631 ) strings);
634 let noghyll _ = ();;
635 let firstgeomcmds = "", [];;
637 let state =
638 { sr = Unix.stdin
639 ; sw = Unix.stdin
640 ; wsfd = Unix.stdin
641 ; errfd = None
642 ; stderr = Unix.stderr
643 ; errmsgs = Buffer.create 0
644 ; newerrmsgs = false
645 ; x = 0
646 ; y = 0
647 ; w = 0
648 ; scrollw = 0
649 ; hscrollh = 0
650 ; anchor = emptyanchor
651 ; ranchors = []
652 ; layout = []
653 ; maxy = max_int
654 ; tilelru = Queue.create ()
655 ; pagemap = Hashtbl.create 10
656 ; tilemap = Hashtbl.create 10
657 ; pdims = []
658 ; pagecount = 0
659 ; currently = Idle
660 ; mstate = Mnone
661 ; rects = []
662 ; rects1 = []
663 ; text = ""
664 ; mode = View
665 ; fullscreen = None
666 ; searchpattern = ""
667 ; outlines = [||]
668 ; bookmarks = []
669 ; path = ""
670 ; password = ""
671 ; geomcmds = firstgeomcmds
672 ; hists =
673 { nav = cbnew 10 (0, 0.0)
674 ; pat = cbnew 10 ""
675 ; pag = cbnew 10 ""
676 ; sel = cbnew 10 ""
678 ; memused = 0
679 ; gen = 0
680 ; throttle = None
681 ; autoscroll = None
682 ; ghyll = noghyll
683 ; help = makehelp ()
684 ; docinfo = []
685 ; texid = None
686 ; prevzoom = 1.0
687 ; progress = -1.0
688 ; uioh = nouioh
689 ; redisplay = true
690 ; mpos = (-1, -1)
691 ; keystate = KSnone
692 ; glinks = false
696 let vlog fmt =
697 if conf.verbose
698 then
699 Printf.kprintf prerr_endline fmt
700 else
701 Printf.kprintf ignore fmt
704 let launchpath () =
705 if String.length conf.pathlauncher = 0
706 then print_endline state.path
707 else (
708 let re = Str.regexp "%s" in
709 let command = Str.global_replace re state.path conf.pathlauncher in
710 try popen command []
711 with exn ->
712 Printf.eprintf
713 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
714 flush stderr;
718 module Ne = struct
719 type 'a t = | Res of 'a | Exn of exn;;
721 let pipe () =
722 try Res (Unix.pipe ())
723 with exn -> Exn exn
726 let clo fd f =
727 try Unix.close fd
728 with exn -> f (Printexc.to_string exn)
731 let dup fd =
732 try Res (Unix.dup fd)
733 with exn -> Exn exn
736 let dup2 fd1 fd2 =
737 try Res (Unix.dup2 fd1 fd2)
738 with exn -> Exn exn
740 end;;
742 let redirectstderr () =
743 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
744 if conf.redirectstderr
745 then
746 match Ne.pipe () with
747 | Ne.Exn exn ->
748 dolog "failed to create stderr redirection pipes: %s"
749 (Printexc.to_string exn)
751 | Ne.Res (r, w) ->
752 begin match Ne.dup Unix.stderr with
753 | Ne.Exn exn ->
754 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
755 Ne.clo r (clofail "pipe/r");
756 Ne.clo w (clofail "pipe/w");
758 | Ne.Res dupstderr ->
759 begin match Ne.dup2 w Unix.stderr with
760 | Ne.Exn exn ->
761 dolog "failed to dup2 to stderr: %s"
762 (Printexc.to_string exn);
763 Ne.clo dupstderr (clofail "stderr duplicate");
764 Ne.clo r (clofail "redir pipe/r");
765 Ne.clo w (clofail "redir pipe/w");
767 | Ne.Res () ->
768 state.stderr <- dupstderr;
769 state.errfd <- Some r;
770 end;
772 else (
773 state.newerrmsgs <- false;
774 begin match state.errfd with
775 | Some fd ->
776 begin match Ne.dup2 state.stderr Unix.stderr with
777 | Ne.Exn exn ->
778 dolog "failed to dup2 original stderr: %s"
779 (Printexc.to_string exn)
780 | Ne.Res () ->
781 Ne.clo fd (clofail "dup of stderr");
782 Unix.dup2 state.stderr Unix.stderr;
783 state.errfd <- None;
784 end;
785 | None -> ()
786 end;
787 prerr_string (Buffer.contents state.errmsgs);
788 flush stderr;
789 Buffer.clear state.errmsgs;
793 module G =
794 struct
795 let postRedisplay who =
796 if conf.verbose
797 then prerr_endline ("redisplay for " ^ who);
798 state.redisplay <- true;
800 end;;
802 let getopaque pageno =
803 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
804 with Not_found -> None
807 let putopaque pageno opaque =
808 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
811 let pagetranslatepoint l x y =
812 let dy = y - l.pagedispy in
813 let y = dy + l.pagey in
814 let dx = x - l.pagedispx in
815 let x = dx + l.pagex in
816 (x, y);
819 let getunder x y =
820 let rec f = function
821 | l :: rest ->
822 begin match getopaque l.pageno with
823 | Some opaque ->
824 let x0 = l.pagedispx in
825 let x1 = x0 + l.pagevw in
826 let y0 = l.pagedispy in
827 let y1 = y0 + l.pagevh in
828 if y >= y0 && y <= y1 && x >= x0 && x <= x1
829 then
830 let px, py = pagetranslatepoint l x y in
831 match whatsunder opaque px py with
832 | Unone -> f rest
833 | under -> under
834 else f rest
835 | _ ->
836 f rest
838 | [] -> Unone
840 f state.layout
843 let showtext c s =
844 state.text <- Printf.sprintf "%c%s" c s;
845 G.postRedisplay "showtext";
848 let undertext = function
849 | Unone -> "none"
850 | Ulinkuri s -> s
851 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
852 | Utext s -> "font: " ^ s
853 | Uunexpected s -> "unexpected: " ^ s
854 | Ulaunch s -> "launch: " ^ s
855 | Unamed s -> "named: " ^ s
856 | Uremote (filename, pageno) ->
857 Printf.sprintf "%s: page %d" filename (pageno+1)
860 let updateunder x y =
861 match getunder x y with
862 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
863 | Ulinkuri uri ->
864 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
865 Wsi.setcursor Wsi.CURSOR_INFO
866 | Ulinkgoto (pageno, _) ->
867 if conf.underinfo
868 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
869 Wsi.setcursor Wsi.CURSOR_INFO
870 | Utext s ->
871 if conf.underinfo then showtext 'f' ("ont: " ^ s);
872 Wsi.setcursor Wsi.CURSOR_TEXT
873 | Uunexpected s ->
874 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
875 Wsi.setcursor Wsi.CURSOR_INHERIT
876 | Ulaunch s ->
877 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
878 Wsi.setcursor Wsi.CURSOR_INHERIT
879 | Unamed s ->
880 if conf.underinfo then showtext 'n' ("amed: " ^ s);
881 Wsi.setcursor Wsi.CURSOR_INHERIT
882 | Uremote (filename, pageno) ->
883 if conf.underinfo then showtext 'r'
884 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
885 Wsi.setcursor Wsi.CURSOR_INFO
888 let showlinktype under =
889 if conf.underinfo
890 then
891 match under with
892 | Unone -> ()
893 | under ->
894 let s = undertext under in
895 showtext ' ' s
898 let addchar s c =
899 let b = Buffer.create (String.length s + 1) in
900 Buffer.add_string b s;
901 Buffer.add_char b c;
902 Buffer.contents b;
905 let colorspace_of_string s =
906 match String.lowercase s with
907 | "rgb" -> Rgb
908 | "bgr" -> Bgr
909 | "gray" -> Gray
910 | _ -> failwith "invalid colorspace"
913 let int_of_colorspace = function
914 | Rgb -> 0
915 | Bgr -> 1
916 | Gray -> 2
919 let colorspace_of_int = function
920 | 0 -> Rgb
921 | 1 -> Bgr
922 | 2 -> Gray
923 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
926 let colorspace_to_string = function
927 | Rgb -> "rgb"
928 | Bgr -> "bgr"
929 | Gray -> "gray"
932 let intentry_with_suffix text key =
933 let c =
934 if key >= 32 && key < 127
935 then Char.chr key
936 else '\000'
938 match Char.lowercase c with
939 | '0' .. '9' ->
940 let text = addchar text c in
941 TEcont text
943 | 'k' | 'm' | 'g' ->
944 let text = addchar text c in
945 TEcont text
947 | _ ->
948 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
949 TEcont text
952 let multicolumns_to_string (n, a, b) =
953 if a = 0 && b = 0
954 then Printf.sprintf "%d" n
955 else Printf.sprintf "%d,%d,%d" n a b;
958 let multicolumns_of_string s =
960 (int_of_string s, 0, 0)
961 with _ ->
962 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
965 let readcmd fd =
966 let s = "xxxx" in
967 let n = Unix.read fd s 0 4 in
968 if n != 4 then failwith "incomplete read(len)";
969 let len = 0
970 lor (Char.code s.[0] lsl 24)
971 lor (Char.code s.[1] lsl 16)
972 lor (Char.code s.[2] lsl 8)
973 lor (Char.code s.[3] lsl 0)
975 let s = String.create len in
976 let n = Unix.read fd s 0 len in
977 if n != len then failwith "incomplete read(data)";
981 let btod b = if b then 1 else 0;;
983 let wcmd fmt =
984 let b = Buffer.create 16 in
985 Buffer.add_string b "llll";
986 Printf.kbprintf
987 (fun b ->
988 let s = Buffer.contents b in
989 let n = String.length s in
990 let len = n - 4 in
991 (* dolog "wcmd %S" (String.sub s 4 len); *)
992 s.[0] <- Char.chr ((len lsr 24) land 0xff);
993 s.[1] <- Char.chr ((len lsr 16) land 0xff);
994 s.[2] <- Char.chr ((len lsr 8) land 0xff);
995 s.[3] <- Char.chr (len land 0xff);
996 let n' = Unix.write state.sw s 0 n in
997 if n' != n then failwith "write failed";
998 ) b fmt;
1001 let calcips h =
1002 if conf.presentation
1003 then
1004 let d = conf.winh - h in
1005 max 0 ((d + 1) / 2)
1006 else
1007 conf.interpagespace
1010 let calcheight () =
1011 let rec f pn ph pi fh l =
1012 match l with
1013 | (n, _, h, _) :: rest ->
1014 let ips = calcips h in
1015 let fh =
1016 if conf.presentation
1017 then fh+ips
1018 else (
1019 if isbirdseye state.mode && pn = 0
1020 then fh + ips
1021 else fh
1024 let fh = fh + ((n - pn) * (ph + pi)) in
1025 f n h ips fh rest;
1027 | [] ->
1028 let inc =
1029 if conf.presentation || (isbirdseye state.mode && pn = 0)
1030 then 0
1031 else -pi
1033 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
1034 max 0 fh
1036 let fh = f 0 0 0 0 state.pdims in
1040 let calcheight () =
1041 match conf.columns with
1042 | Csingle -> calcheight ()
1043 | Cmulti ((c, _, _), b) ->
1044 let rec loop y h n =
1045 if n < 0
1046 then loop y h (n+1)
1047 else (
1048 if n = Array.length b
1049 then y + h
1050 else
1051 let (_, _, y', (_, _, h', _)) = b.(n) in
1052 let y = min y y'
1053 and h = max h h' in
1054 loop y h (n+1)
1057 loop max_int 0 (((Array.length b - 1) / c) * c)
1058 | Csplit (_, b) ->
1059 if Array.length b > 0
1060 then
1061 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1062 y + h
1063 else 0
1066 let getpageyh pageno =
1067 let rec f pn ph pi y l =
1068 match l with
1069 | (n, _, h, _) :: rest ->
1070 let ips = calcips h in
1071 if n >= pageno
1072 then
1073 let h = if n = pageno then h else ph in
1074 if conf.presentation && n = pageno
1075 then
1076 y + (pageno - pn) * (ph + pi) + pi, h
1077 else
1078 y + (pageno - pn) * (ph + pi), h
1079 else
1080 let y = y + (if conf.presentation then pi else 0) in
1081 let y = y + (n - pn) * (ph + pi) in
1082 f n h ips y rest
1084 | [] ->
1085 y + (pageno - pn) * (ph + pi), ph
1087 f 0 0 0 0 state.pdims
1090 let getpageyh pageno =
1091 match conf.columns with
1092 | Csingle -> getpageyh pageno
1093 | Cmulti (_, b) ->
1094 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1095 y, h
1096 | Csplit (c, b) ->
1097 let n = pageno*c in
1098 let (_, _, y, (_, _, h, _)) = b.(n) in
1099 y, h
1102 let getpagedim pageno =
1103 let rec f ppdim l =
1104 match l with
1105 | (n, _, _, _) as pdim :: rest ->
1106 if n >= pageno
1107 then (if n = pageno then pdim else ppdim)
1108 else f pdim rest
1110 | [] -> ppdim
1112 f (-1, -1, -1, -1) state.pdims
1115 let getpagey pageno = fst (getpageyh pageno);;
1117 let nogeomcmds cmds =
1118 match cmds with
1119 | s, [] -> String.length s = 0
1120 | _ -> false
1123 let layout1 y sh =
1124 let sh = sh - state.hscrollh in
1125 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1126 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1127 match pdims with
1128 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1129 let ips = calcips h in
1130 let yinc =
1131 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1132 then ips
1133 else 0
1135 (w, h, ips, xoff), rest, pdimno + 1, yinc
1136 | _ ->
1137 prev, pdims, pdimno, 0
1139 let dy = dy + yinc in
1140 let py = py + yinc in
1141 if pageno = state.pagecount || dy >= sh
1142 then
1143 accu
1144 else
1145 let vy = y + dy in
1146 if py + h <= vy - yinc
1147 then
1148 let py = py + h + ips in
1149 let dy = max 0 (py - y) in
1150 f ~pageno:(pageno+1)
1151 ~pdimno
1152 ~prev:curr
1155 ~pdims:rest
1156 ~accu
1157 else
1158 let pagey = vy - py in
1159 let pagevh = h - pagey in
1160 let pagevh = min (sh - dy) pagevh in
1161 let off = if yinc > 0 then py - vy else 0 in
1162 let py = py + h + ips in
1163 let pagex, dx =
1164 let xoff = xoff +
1165 if state.w < conf.winw - state.scrollw
1166 then (conf.winw - state.scrollw - state.w) / 2
1167 else 0
1169 let dispx = xoff + state.x in
1170 if dispx < 0
1171 then (-dispx, 0)
1172 else (0, dispx)
1174 let pagevw =
1175 let lw = w - pagex in
1176 min lw (conf.winw - state.scrollw)
1178 let e =
1179 { pageno = pageno
1180 ; pagedimno = pdimno
1181 ; pagew = w
1182 ; pageh = h
1183 ; pagex = pagex
1184 ; pagey = pagey + off
1185 ; pagevw = pagevw
1186 ; pagevh = pagevh - off
1187 ; pagedispx = dx
1188 ; pagedispy = dy + off
1189 ; pagecol = 0
1192 let accu = e :: accu in
1193 f ~pageno:(pageno+1)
1194 ~pdimno
1195 ~prev:curr
1197 ~dy:(dy+pagevh+ips)
1198 ~pdims:rest
1199 ~accu
1201 let accu =
1203 ~pageno:0
1204 ~pdimno:~-1
1205 ~prev:(0,0,0,0)
1206 ~py:0
1207 ~dy:0
1208 ~pdims:state.pdims
1209 ~accu:[]
1211 List.rev accu
1214 let layoutN ((columns, coverA, coverB), b) y sh =
1215 let sh = sh - state.hscrollh in
1216 let rec fold accu n =
1217 if n = Array.length b
1218 then accu
1219 else
1220 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1221 if (vy - y) > sh &&
1222 (n = coverA - 1
1223 || n = state.pagecount - coverB
1224 || (n - coverA) mod columns = columns - 1)
1225 then accu
1226 else
1227 let accu =
1228 if vy + h > y
1229 then
1230 let pagey = max 0 (y - vy) in
1231 let pagedispy = if pagey > 0 then 0 else vy - y in
1232 let pagedispx, pagex =
1233 let pdx =
1234 if n = coverA - 1 || n = state.pagecount - coverB
1235 then state.x + (conf.winw - state.scrollw - w) / 2
1236 else dx + xoff + state.x
1238 if pdx < 0
1239 then 0, -pdx
1240 else pdx, 0
1242 let pagevw =
1243 let vw = conf.winw - state.scrollw - pagedispx in
1244 let pw = w - pagex in
1245 min vw pw
1247 let pagevh = min (h - pagey) (sh - pagedispy) in
1248 if pagevw > 0 && pagevh > 0
1249 then
1250 let e =
1251 { pageno = n
1252 ; pagedimno = pdimno
1253 ; pagew = w
1254 ; pageh = h
1255 ; pagex = pagex
1256 ; pagey = pagey
1257 ; pagevw = pagevw
1258 ; pagevh = pagevh
1259 ; pagedispx = pagedispx
1260 ; pagedispy = pagedispy
1261 ; pagecol = 0
1264 e :: accu
1265 else
1266 accu
1267 else
1268 accu
1270 fold accu (n+1)
1272 List.rev (fold [] 0)
1275 let layoutS (columns, b) y sh =
1276 let sh = sh - state.hscrollh in
1277 let rec fold accu n =
1278 if n = Array.length b
1279 then accu
1280 else
1281 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1282 if (vy - y) > sh
1283 then accu
1284 else
1285 let accu =
1286 if vy + pageh > y
1287 then
1288 let x = xoff + state.x in
1289 let pagey = max 0 (y - vy) in
1290 let pagedispy = if pagey > 0 then 0 else vy - y in
1291 let pagedispx, pagex =
1292 if px = 0
1293 then (
1294 if x < 0
1295 then 0, -x
1296 else x, 0
1298 else (
1299 let px = px - x in
1300 if px < 0
1301 then -px, 0
1302 else 0, px
1305 let pagevw =
1306 let vw = conf.winw - pagedispx - state.scrollw in
1307 let pw = pagew - pagex in
1308 min vw pw
1310 let pagevw = min pagevw (pagew/columns) in
1311 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1312 if pagevw > 0 && pagevh > 0
1313 then
1314 let e =
1315 { pageno = n/columns
1316 ; pagedimno = pdimno
1317 ; pagew = pagew
1318 ; pageh = pageh
1319 ; pagex = pagex
1320 ; pagey = pagey
1321 ; pagevw = pagevw
1322 ; pagevh = pagevh
1323 ; pagedispx = pagedispx
1324 ; pagedispy = pagedispy
1325 ; pagecol = n mod columns
1328 e :: accu
1329 else
1330 accu
1331 else
1332 accu
1334 fold accu (n+1)
1336 List.rev (fold [] 0)
1339 let layout y sh =
1340 if nogeomcmds state.geomcmds
1341 then
1342 match conf.columns with
1343 | Csingle -> layout1 y sh
1344 | Cmulti c -> layoutN c y sh
1345 | Csplit s -> layoutS s y sh
1346 else []
1349 let clamp incr =
1350 let y = state.y + incr in
1351 let y = max 0 y in
1352 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1356 let itertiles l f =
1357 let tilex = l.pagex mod conf.tilew in
1358 let tiley = l.pagey mod conf.tileh in
1360 let col = l.pagex / conf.tilew in
1361 let row = l.pagey / conf.tileh in
1363 let rec rowloop row y0 dispy h =
1364 if h = 0
1365 then ()
1366 else (
1367 let dh = conf.tileh - y0 in
1368 let dh = min h dh in
1369 let rec colloop col x0 dispx w =
1370 if w = 0
1371 then ()
1372 else (
1373 let dw = conf.tilew - x0 in
1374 let dw = min w dw in
1376 f col row dispx dispy x0 y0 dw dh;
1377 colloop (col+1) 0 (dispx+dw) (w-dw)
1380 colloop col tilex l.pagedispx l.pagevw;
1381 rowloop (row+1) 0 (dispy+dh) (h-dh)
1384 if l.pagevw > 0 && l.pagevh > 0
1385 then rowloop row tiley l.pagedispy l.pagevh;
1388 let gettileopaque l col row =
1389 let key =
1390 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1392 try Some (Hashtbl.find state.tilemap key)
1393 with Not_found -> None
1396 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1397 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1398 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1401 let drawtiles l color =
1402 GlDraw.color color;
1403 let f col row x y tilex tiley w h =
1404 match gettileopaque l col row with
1405 | Some (opaque, _, t) ->
1406 let params = x, y, w, h, tilex, tiley in
1407 if conf.invert
1408 then (
1409 Gl.enable `blend;
1410 GlFunc.blend_func `zero `one_minus_src_color;
1412 drawtile params opaque;
1413 if conf.invert
1414 then Gl.disable `blend;
1415 if conf.debug
1416 then (
1417 let s = Printf.sprintf
1418 "%d[%d,%d] %f sec"
1419 l.pageno col row t
1421 let w = measurestr fstate.fontsize s in
1422 GlMisc.push_attrib [`current];
1423 GlDraw.color (0.0, 0.0, 0.0);
1424 GlDraw.rect
1425 (float (x-2), float (y-2))
1426 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1427 GlDraw.color (1.0, 1.0, 1.0);
1428 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1429 GlMisc.pop_attrib ();
1432 | _ ->
1433 let w =
1434 let lw = conf.winw - state.scrollw - x in
1435 min lw w
1436 and h =
1437 let lh = conf.winh - y in
1438 min lh h
1440 Gl.enable `texture_2d;
1441 begin match state.texid with
1442 | Some id ->
1443 GlTex.bind_texture `texture_2d id;
1444 let x0 = float x
1445 and y0 = float y
1446 and x1 = float (x+w)
1447 and y1 = float (y+h) in
1449 let tw = float w /. 64.0
1450 and th = float h /. 64.0 in
1451 let tx0 = float tilex /. 64.0
1452 and ty0 = float tiley /. 64.0 in
1453 let tx1 = tx0 +. tw
1454 and ty1 = ty0 +. th in
1455 GlDraw.begins `quads;
1456 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1457 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1458 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1459 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1460 GlDraw.ends ();
1462 Gl.disable `texture_2d;
1463 | None ->
1464 GlDraw.color (1.0, 1.0, 1.0);
1465 GlDraw.rect
1466 (float x, float y)
1467 (float (x+w), float (y+h));
1468 end;
1469 if w > 128 && h > fstate.fontsize + 10
1470 then (
1471 GlDraw.color (0.0, 0.0, 0.0);
1472 let c, r =
1473 if conf.verbose
1474 then (col*conf.tilew, row*conf.tileh)
1475 else col, row
1477 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1479 GlDraw.color color;
1481 itertiles l f
1484 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1486 let tilevisible1 l x y =
1487 let ax0 = l.pagex
1488 and ax1 = l.pagex + l.pagevw
1489 and ay0 = l.pagey
1490 and ay1 = l.pagey + l.pagevh in
1492 let bx0 = x
1493 and by0 = y in
1494 let bx1 = min (bx0 + conf.tilew) l.pagew
1495 and by1 = min (by0 + conf.tileh) l.pageh in
1497 let rx0 = max ax0 bx0
1498 and ry0 = max ay0 by0
1499 and rx1 = min ax1 bx1
1500 and ry1 = min ay1 by1 in
1502 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1503 nonemptyintersection
1506 let tilevisible layout n x y =
1507 let rec findpageinlayout m = function
1508 | l :: rest when l.pageno = n ->
1509 tilevisible1 l x y || (
1510 match conf.columns with
1511 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1512 | _ -> false
1514 | _ :: rest -> findpageinlayout 0 rest
1515 | [] -> false
1517 findpageinlayout 0 layout;
1520 let tileready l x y =
1521 tilevisible1 l x y &&
1522 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1525 let tilepage n p layout =
1526 let rec loop = function
1527 | l :: rest ->
1528 if l.pageno = n
1529 then
1530 let f col row _ _ _ _ _ _ =
1531 if state.currently = Idle
1532 then
1533 match gettileopaque l col row with
1534 | Some _ -> ()
1535 | None ->
1536 let x = col*conf.tilew
1537 and y = row*conf.tileh in
1538 let w =
1539 let w = l.pagew - x in
1540 min w conf.tilew
1542 let h =
1543 let h = l.pageh - y in
1544 min h conf.tileh
1546 wcmd "tile %s %d %d %d %d" p x y w h;
1547 state.currently <-
1548 Tiling (
1549 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1550 conf.tilew, conf.tileh
1553 itertiles l f;
1554 else
1555 loop rest
1557 | [] -> ()
1559 if nogeomcmds state.geomcmds
1560 then loop layout;
1563 let preloadlayout visiblepages =
1564 let presentation = conf.presentation in
1565 let interpagespace = conf.interpagespace in
1566 let maxy = state.maxy in
1567 conf.presentation <- false;
1568 conf.interpagespace <- 0;
1569 state.maxy <- calcheight ();
1570 let y =
1571 match visiblepages with
1572 | [] -> if state.y >= maxy then maxy else 0
1573 | l :: _ -> getpagey l.pageno + l.pagey
1575 let y = if y < conf.winh then 0 else y - conf.winh in
1576 let h = state.y - y + conf.winh*3 in
1577 let pages = layout y h in
1578 conf.presentation <- presentation;
1579 conf.interpagespace <- interpagespace;
1580 state.maxy <- maxy;
1581 pages;
1584 let load pages =
1585 let rec loop pages =
1586 if state.currently != Idle
1587 then ()
1588 else
1589 match pages with
1590 | l :: rest ->
1591 begin match getopaque l.pageno with
1592 | None ->
1593 wcmd "page %d %d" l.pageno l.pagedimno;
1594 state.currently <- Loading (l, state.gen);
1595 | Some opaque ->
1596 tilepage l.pageno opaque pages;
1597 loop rest
1598 end;
1599 | _ -> ()
1601 if nogeomcmds state.geomcmds
1602 then loop pages
1605 let preload pages =
1606 load pages;
1607 if conf.preload && state.currently = Idle
1608 then load (preloadlayout pages);
1611 let layoutready layout =
1612 let rec fold all ls =
1613 all && match ls with
1614 | l :: rest ->
1615 let seen = ref false in
1616 let allvisible = ref true in
1617 let foo col row _ _ _ _ _ _ =
1618 seen := true;
1619 allvisible := !allvisible &&
1620 begin match gettileopaque l col row with
1621 | Some _ -> true
1622 | None -> false
1625 itertiles l foo;
1626 fold (!seen && !allvisible) rest
1627 | [] -> true
1629 let alltilesvisible = fold true layout in
1630 alltilesvisible;
1633 let gotoy y =
1634 let y = bound y 0 state.maxy in
1635 let y, layout, proceed =
1636 match conf.maxwait with
1637 | Some time when state.ghyll == noghyll ->
1638 begin match state.throttle with
1639 | None ->
1640 let layout = layout y conf.winh in
1641 let ready = layoutready layout in
1642 if not ready
1643 then (
1644 load layout;
1645 state.throttle <- Some (layout, y, now ());
1647 else G.postRedisplay "gotoy showall (None)";
1648 y, layout, ready
1649 | Some (_, _, started) ->
1650 let dt = now () -. started in
1651 if dt > time
1652 then (
1653 state.throttle <- None;
1654 let layout = layout y conf.winh in
1655 load layout;
1656 G.postRedisplay "maxwait";
1657 y, layout, true
1659 else -1, [], false
1662 | _ ->
1663 let layout = layout y conf.winh in
1664 if true || layoutready layout
1665 then G.postRedisplay "gotoy ready";
1666 y, layout, true
1668 if proceed
1669 then (
1670 state.y <- y;
1671 state.layout <- layout;
1672 begin match state.mode with
1673 | LinkNav (Ltexact (pageno, linkno)) ->
1674 let rec loop = function
1675 | [] ->
1676 state.mode <- LinkNav (Ltgendir 0)
1677 | l :: _ when l.pageno = pageno ->
1678 begin match getopaque pageno with
1679 | None ->
1680 state.mode <- LinkNav (Ltgendir 0)
1681 | Some opaque ->
1682 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1683 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1684 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1685 then state.mode <- LinkNav (Ltgendir 0)
1687 | _ :: rest -> loop rest
1689 loop layout
1690 | _ -> ()
1691 end;
1692 begin match state.mode with
1693 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1694 if not (pagevisible layout pageno)
1695 then (
1696 match state.layout with
1697 | [] -> ()
1698 | l :: _ ->
1699 state.mode <- Birdseye (
1700 conf, leftx, l.pageno, hooverpageno, anchor
1703 | LinkNav (Ltgendir dir as lt) ->
1704 let linknav =
1705 let rec loop = function
1706 | [] -> lt
1707 | l :: rest ->
1708 match getopaque l.pageno with
1709 | None -> loop rest
1710 | Some opaque ->
1711 let link =
1712 let ld =
1713 if dir = 0
1714 then LDfirstvisible (l.pagex, l.pagey, dir)
1715 else (
1716 if dir > 0 then LDfirst else LDlast
1719 findlink opaque ld
1721 match link with
1722 | Lnotfound -> loop rest
1723 | Lfound n ->
1724 showlinktype (getlink opaque n);
1725 Ltexact (l.pageno, n)
1727 loop state.layout
1729 state.mode <- LinkNav linknav
1730 | _ -> ()
1731 end;
1732 preload layout;
1734 state.ghyll <- noghyll;
1735 if conf.updatecurs
1736 then (
1737 let mx, my = state.mpos in
1738 updateunder mx my;
1742 let conttiling pageno opaque =
1743 tilepage pageno opaque
1744 (if conf.preload then preloadlayout state.layout else state.layout)
1747 let gotoy_and_clear_text y =
1748 if not conf.verbose then state.text <- "";
1749 gotoy y;
1752 let getanchor () =
1753 match state.layout with
1754 | [] -> emptyanchor
1755 | l :: _ ->
1756 let coloff = l.pagecol * l.pageh in
1757 (l.pageno, (float l.pagey +. float coloff) /. float l.pageh)
1760 let getanchory (n, top) =
1761 let y, h = getpageyh n in
1762 y + (truncate (top *. float h));
1765 let gotoanchor anchor =
1766 gotoy (getanchory anchor);
1769 let addnav () =
1770 cbput state.hists.nav (getanchor ());
1773 let getnav dir =
1774 let anchor = cbgetc state.hists.nav dir in
1775 getanchory anchor;
1778 let gotoghyll y =
1779 let rec scroll f n a b =
1780 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1781 let snake f a b =
1782 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1783 if f < a
1784 then s (float f /. float a)
1785 else (
1786 if f > b
1787 then 1.0 -. s ((float (f-b) /. float (n-b)))
1788 else 1.0
1791 snake f a b
1792 and summa f n a b =
1793 (* courtesy:
1794 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1795 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1796 let iv1 = iv f in
1797 let ins = float a *. iv1
1798 and outs = float (n-b) *. iv1 in
1799 let ones = b - a in
1800 ins +. outs +. float ones
1802 let rec set (_N, _A, _B) y sy =
1803 let sum = summa 1.0 _N _A _B in
1804 let dy = float (y - sy) in
1805 state.ghyll <- (
1806 let rec gf n y1 o =
1807 if n >= _N
1808 then state.ghyll <- noghyll
1809 else
1810 let go n =
1811 let s = scroll n _N _A _B in
1812 let y1 = y1 +. ((s *. dy) /. sum) in
1813 gotoy_and_clear_text (truncate y1);
1814 state.ghyll <- gf (n+1) y1;
1816 match o with
1817 | None -> go n
1818 | Some y' -> set (_N/2, 0, 0) y' state.y
1820 gf 0 (float state.y)
1823 match conf.ghyllscroll with
1824 | None ->
1825 gotoy_and_clear_text y
1826 | Some nab ->
1827 if state.ghyll == noghyll
1828 then set nab y state.y
1829 else state.ghyll (Some y)
1832 let gotopage n top =
1833 let y, h = getpageyh n in
1834 let y = y + (truncate (top *. float h)) in
1835 gotoghyll y
1838 let gotopage1 n top =
1839 let y = getpagey n in
1840 let y = y + top in
1841 gotoghyll y
1844 let invalidate s f =
1845 state.layout <- [];
1846 state.pdims <- [];
1847 state.rects <- [];
1848 state.rects1 <- [];
1849 match state.geomcmds with
1850 | ps, [] when String.length ps = 0 ->
1851 f ();
1852 state.geomcmds <- s, [];
1854 | ps, [] ->
1855 state.geomcmds <- ps, [s, f];
1857 | ps, (s', _) :: rest when s' = s ->
1858 state.geomcmds <- ps, ((s, f) :: rest);
1860 | ps, cmds ->
1861 state.geomcmds <- ps, ((s, f) :: cmds);
1864 let opendoc path password =
1865 state.path <- path;
1866 state.password <- password;
1867 state.gen <- state.gen + 1;
1868 state.docinfo <- [];
1870 setaalevel conf.aalevel;
1871 Wsi.settitle ("llpp " ^ Filename.basename path);
1872 wcmd "open %s\000%s\000" path password;
1873 invalidate "reqlayout"
1874 (fun () ->
1875 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1878 let scalecolor c =
1879 let c = c *. conf.colorscale in
1880 (c, c, c);
1883 let scalecolor2 (r, g, b) =
1884 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1887 let docolumns = function
1888 | Csingle -> ()
1890 | Cmulti ((columns, coverA, coverB), _) ->
1891 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1892 let rec loop pageno pdimno pdim x y rowh pdims =
1893 let rec fixrow m = if m = pageno then () else
1894 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1895 if h < rowh
1896 then (
1897 let y = y + (rowh - h) / 2 in
1898 a.(m) <- (pdimno, x, y, pdim);
1900 fixrow (m+1)
1902 if pageno = state.pagecount
1903 then fixrow (((pageno - 1) / columns) * columns)
1904 else
1905 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1906 match pdims with
1907 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1908 pdimno+1, pdim, rest
1909 | _ ->
1910 pdimno, pdim, pdims
1912 let x, y, rowh' =
1913 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1914 then (
1915 (conf.winw - state.scrollw - w) / 2,
1916 y + rowh + conf.interpagespace, h
1918 else (
1919 if (pageno - coverA) mod columns = 0
1920 then 0, y + rowh + conf.interpagespace, h
1921 else x, y, max rowh h
1924 if pageno > 1 && (pageno - coverA) mod columns = 0
1925 then fixrow (pageno - columns);
1926 a.(pageno) <- (pdimno, x, y, pdim);
1927 let x = x + w + xoff*2 + conf.interpagespace in
1928 loop (pageno+1) pdimno pdim x y rowh' pdims
1930 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1931 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1933 | Csplit (c, _) ->
1934 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1935 let rec loop pageno pdimno pdim y pdims =
1936 if pageno = state.pagecount
1937 then ()
1938 else
1939 let pdimno, ((_, w, h, _) as pdim), pdims =
1940 match pdims with
1941 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1942 pdimno+1, pdim, rest
1943 | _ ->
1944 pdimno, pdim, pdims
1946 let cw = w / c in
1947 let rec loop1 n x y =
1948 if n = c then y else (
1949 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1950 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1953 let y = loop1 0 0 y in
1954 loop (pageno+1) pdimno pdim y pdims
1956 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1957 conf.columns <- Csplit (c, a);
1960 let represent () =
1961 docolumns conf.columns;
1962 state.maxy <- calcheight ();
1963 state.hscrollh <-
1964 if state.w <= conf.winw - state.scrollw
1965 then 0
1966 else state.scrollw
1968 match state.mode with
1969 | Birdseye (_, _, pageno, _, _) ->
1970 let y, h = getpageyh pageno in
1971 let top = (conf.winh - h) / 2 in
1972 gotoy (max 0 (y - top))
1973 | _ -> gotoanchor state.anchor
1976 let reshape w h =
1977 GlDraw.viewport 0 0 w h;
1978 let firsttime = state.geomcmds == firstgeomcmds in
1979 if not firsttime && nogeomcmds state.geomcmds
1980 then state.anchor <- getanchor ();
1982 conf.winw <- w;
1983 let w = truncate (float w *. conf.zoom) - state.scrollw in
1984 let w = max w 2 in
1985 conf.winh <- h;
1986 setfontsize fstate.fontsize;
1987 GlMat.mode `modelview;
1988 GlMat.load_identity ();
1990 GlMat.mode `projection;
1991 GlMat.load_identity ();
1992 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1993 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1994 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1996 let relx =
1997 if conf.zoom <= 1.0
1998 then 0.0
1999 else float state.x /. float state.w
2001 invalidate "geometry"
2002 (fun () ->
2003 state.w <- w;
2004 if not firsttime
2005 then state.x <- truncate (relx *. float w);
2006 let w =
2007 match conf.columns with
2008 | Csingle -> w
2009 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2010 | Csplit (c, _) -> w * c
2012 wcmd "geometry %d %d" w h);
2015 let enttext () =
2016 let len = String.length state.text in
2017 let drawstring s =
2018 let hscrollh =
2019 match state.mode with
2020 | Textentry _
2021 | View ->
2022 let h, _, _ = state.uioh#scrollpw in
2024 | _ -> 0
2026 let rect x w =
2027 GlDraw.rect
2028 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2029 (x+.w, float (conf.winh - hscrollh))
2032 let w = float (conf.winw - state.scrollw - 1) in
2033 if state.progress >= 0.0 && state.progress < 1.0
2034 then (
2035 GlDraw.color (0.3, 0.3, 0.3);
2036 let w1 = w *. state.progress in
2037 rect 0.0 w1;
2038 GlDraw.color (0.0, 0.0, 0.0);
2039 rect w1 (w-.w1)
2041 else (
2042 GlDraw.color (0.0, 0.0, 0.0);
2043 rect 0.0 w;
2046 GlDraw.color (1.0, 1.0, 1.0);
2047 drawstring fstate.fontsize
2048 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2050 let s =
2051 match state.mode with
2052 | Textentry ((prefix, text, _, _, _), _) ->
2053 let s =
2054 if len > 0
2055 then
2056 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2057 else
2058 Printf.sprintf "%s%s_" prefix text
2062 | _ -> state.text
2064 let s =
2065 if state.newerrmsgs
2066 then (
2067 if not (istextentry state.mode)
2068 then
2069 let s1 = "(press 'e' to review error messasges)" in
2070 if String.length s > 0 then s ^ " " ^ s1 else s1
2071 else s
2073 else s
2075 if String.length s > 0
2076 then drawstring s
2079 let gctiles () =
2080 let len = Queue.length state.tilelru in
2081 let rec loop qpos =
2082 if state.memused <= conf.memlimit
2083 then ()
2084 else (
2085 if qpos < len
2086 then
2087 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2088 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2089 let (_, pw, ph, _) = getpagedim n in
2091 gen = state.gen
2092 && colorspace = conf.colorspace
2093 && angle = conf.angle
2094 && pagew = pw
2095 && pageh = ph
2096 && (
2097 let layout =
2098 match state.throttle with
2099 | None ->
2100 if conf.preload
2101 then preloadlayout state.layout
2102 else state.layout
2103 | Some (layout, _, _) ->
2104 layout
2106 let x = col*conf.tilew
2107 and y = row*conf.tileh in
2108 tilevisible layout n x y
2110 then Queue.push lruitem state.tilelru
2111 else (
2112 wcmd "freetile %s" p;
2113 state.memused <- state.memused - s;
2114 state.uioh#infochanged Memused;
2115 Hashtbl.remove state.tilemap k;
2117 loop (qpos+1)
2120 loop 0
2123 let flushtiles () =
2124 Queue.iter (fun (k, p, s) ->
2125 wcmd "freetile %s" p;
2126 state.memused <- state.memused - s;
2127 state.uioh#infochanged Memused;
2128 Hashtbl.remove state.tilemap k;
2129 ) state.tilelru;
2130 Queue.clear state.tilelru;
2131 load state.layout;
2134 let logcurrently = function
2135 | Idle -> dolog "Idle"
2136 | Loading (l, gen) ->
2137 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2138 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2139 dolog
2140 "Tiling %d[%d,%d] page=%s cs=%s angle"
2141 l.pageno col row pageopaque
2142 (colorspace_to_string colorspace)
2144 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2145 angle gen conf.angle state.gen
2146 tilew tileh
2147 conf.tilew conf.tileh
2149 | Outlining _ ->
2150 dolog "outlining"
2153 let act cmds =
2154 (* dolog "%S" cmds; *)
2155 let op, args =
2156 let spacepos =
2157 try String.index cmds ' '
2158 with Not_found -> -1
2160 if spacepos = -1
2161 then cmds, ""
2162 else
2163 let l = String.length cmds in
2164 let op = String.sub cmds 0 spacepos in
2165 op, begin
2166 if l - spacepos < 2 then ""
2167 else String.sub cmds (spacepos+1) (l-spacepos-1)
2170 match op with
2171 | "clear" ->
2172 state.uioh#infochanged Pdim;
2173 state.pdims <- [];
2175 | "clearrects" ->
2176 state.rects <- state.rects1;
2177 G.postRedisplay "clearrects";
2179 | "continue" ->
2180 let n =
2181 try Scanf.sscanf args "%u" (fun n -> n)
2182 with exn ->
2183 dolog "error processing 'continue' %S: %s"
2184 cmds (Printexc.to_string exn);
2185 exit 1;
2187 state.pagecount <- n;
2188 begin match state.currently with
2189 | Outlining l ->
2190 state.currently <- Idle;
2191 state.outlines <- Array.of_list (List.rev l)
2192 | _ -> ()
2193 end;
2195 let cur, cmds = state.geomcmds in
2196 if String.length cur = 0
2197 then failwith "umpossible";
2199 begin match List.rev cmds with
2200 | [] ->
2201 state.geomcmds <- "", [];
2202 represent ();
2203 | (s, f) :: rest ->
2204 f ();
2205 state.geomcmds <- s, List.rev rest;
2206 end;
2207 if conf.maxwait = None
2208 then G.postRedisplay "continue";
2210 | "title" ->
2211 Wsi.settitle args
2213 | "msg" ->
2214 showtext ' ' args
2216 | "vmsg" ->
2217 if conf.verbose
2218 then showtext ' ' args
2220 | "progress" ->
2221 let progress, text =
2223 Scanf.sscanf args "%f %n"
2224 (fun f pos ->
2225 f, String.sub args pos (String.length args - pos))
2226 with exn ->
2227 dolog "error processing 'progress' %S: %s"
2228 cmds (Printexc.to_string exn);
2229 exit 1;
2231 state.text <- text;
2232 state.progress <- progress;
2233 G.postRedisplay "progress"
2235 | "firstmatch" ->
2236 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2238 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2239 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2240 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2241 with exn ->
2242 dolog "error processing 'firstmatch' %S: %s"
2243 cmds (Printexc.to_string exn);
2244 exit 1;
2246 let y = (getpagey pageno) + truncate y0 in
2247 addnav ();
2248 gotoy y;
2249 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2251 | "match" ->
2252 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2254 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2255 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2256 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2257 with exn ->
2258 dolog "error processing 'match' %S: %s"
2259 cmds (Printexc.to_string exn);
2260 exit 1;
2262 state.rects1 <-
2263 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2265 | "page" ->
2266 let pageopaque, t =
2268 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2269 with exn ->
2270 dolog "error processing 'page' %S: %s"
2271 cmds (Printexc.to_string exn);
2272 exit 1;
2274 begin match state.currently with
2275 | Loading (l, gen) ->
2276 vlog "page %d took %f sec" l.pageno t;
2277 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2278 begin match state.throttle with
2279 | None ->
2280 let preloadedpages =
2281 if conf.preload
2282 then preloadlayout state.layout
2283 else state.layout
2285 let evict () =
2286 let module IntSet =
2287 Set.Make (struct type t = int let compare = (-) end) in
2288 let set =
2289 List.fold_left (fun s l -> IntSet.add l.pageno s)
2290 IntSet.empty preloadedpages
2292 let evictedpages =
2293 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2294 if not (IntSet.mem pageno set)
2295 then (
2296 wcmd "freepage %s" opaque;
2297 key :: accu
2299 else accu
2300 ) state.pagemap []
2302 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2304 evict ();
2305 state.currently <- Idle;
2306 if gen = state.gen
2307 then (
2308 tilepage l.pageno pageopaque state.layout;
2309 load state.layout;
2310 load preloadedpages;
2311 if pagevisible state.layout l.pageno
2312 && layoutready state.layout
2313 then G.postRedisplay "page";
2316 | Some (layout, _, _) ->
2317 state.currently <- Idle;
2318 tilepage l.pageno pageopaque layout;
2319 load state.layout
2320 end;
2322 | _ ->
2323 dolog "Inconsistent loading state";
2324 logcurrently state.currently;
2325 exit 1
2328 | "tile" ->
2329 let (x, y, opaque, size, t) =
2331 Scanf.sscanf args "%u %u %s %u %f"
2332 (fun x y p size t -> (x, y, p, size, t))
2333 with exn ->
2334 dolog "error processing 'tile' %S: %s"
2335 cmds (Printexc.to_string exn);
2336 exit 1;
2338 begin match state.currently with
2339 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2340 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2342 if tilew != conf.tilew || tileh != conf.tileh
2343 then (
2344 wcmd "freetile %s" opaque;
2345 state.currently <- Idle;
2346 load state.layout;
2348 else (
2349 puttileopaque l col row gen cs angle opaque size t;
2350 state.memused <- state.memused + size;
2351 state.uioh#infochanged Memused;
2352 gctiles ();
2353 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2354 opaque, size) state.tilelru;
2356 let layout =
2357 match state.throttle with
2358 | None -> state.layout
2359 | Some (layout, _, _) -> layout
2362 state.currently <- Idle;
2363 if gen = state.gen
2364 && conf.colorspace = cs
2365 && conf.angle = angle
2366 && tilevisible layout l.pageno x y
2367 then conttiling l.pageno pageopaque;
2369 begin match state.throttle with
2370 | None ->
2371 preload state.layout;
2372 if gen = state.gen
2373 && conf.colorspace = cs
2374 && conf.angle = angle
2375 && tilevisible state.layout l.pageno x y
2376 then G.postRedisplay "tile nothrottle";
2378 | Some (layout, y, _) ->
2379 let ready = layoutready layout in
2380 if ready
2381 then (
2382 state.y <- y;
2383 state.layout <- layout;
2384 state.throttle <- None;
2385 G.postRedisplay "throttle";
2387 else load layout;
2388 end;
2391 | _ ->
2392 dolog "Inconsistent tiling state";
2393 logcurrently state.currently;
2394 exit 1
2397 | "pdim" ->
2398 let pdim =
2400 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2401 with exn ->
2402 dolog "error processing 'pdim' %S: %s"
2403 cmds (Printexc.to_string exn);
2404 exit 1;
2406 state.uioh#infochanged Pdim;
2407 state.pdims <- pdim :: state.pdims
2409 | "o" ->
2410 let (l, n, t, h, pos) =
2412 Scanf.sscanf args "%u %u %d %u %n"
2413 (fun l n t h pos -> l, n, t, h, pos)
2414 with exn ->
2415 dolog "error processing 'o' %S: %s"
2416 cmds (Printexc.to_string exn);
2417 exit 1;
2419 let s = String.sub args pos (String.length args - pos) in
2420 let outline = (s, l, (n, float t /. float h)) in
2421 begin match state.currently with
2422 | Outlining outlines ->
2423 state.currently <- Outlining (outline :: outlines)
2424 | Idle ->
2425 state.currently <- Outlining [outline]
2426 | currently ->
2427 dolog "invalid outlining state";
2428 logcurrently currently
2431 | "info" ->
2432 state.docinfo <- (1, args) :: state.docinfo
2434 | "infoend" ->
2435 state.uioh#infochanged Docinfo;
2436 state.docinfo <- List.rev state.docinfo
2438 | _ ->
2439 dolog "unknown cmd `%S'" cmds
2442 let onhist cb =
2443 let rc = cb.rc in
2444 let action = function
2445 | HCprev -> cbget cb ~-1
2446 | HCnext -> cbget cb 1
2447 | HCfirst -> cbget cb ~-(cb.rc)
2448 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2449 and cancel () = cb.rc <- rc
2450 in (action, cancel)
2453 let search pattern forward =
2454 if String.length pattern > 0
2455 then
2456 let pn, py =
2457 match state.layout with
2458 | [] -> 0, 0
2459 | l :: _ ->
2460 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2462 wcmd "search %d %d %d %d,%s\000"
2463 (btod conf.icase) pn py (btod forward) pattern;
2466 let intentry text key =
2467 let c =
2468 if key >= 32 && key < 127
2469 then Char.chr key
2470 else '\000'
2472 match c with
2473 | '0' .. '9' ->
2474 let text = addchar text c in
2475 TEcont text
2477 | _ ->
2478 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2479 TEcont text
2482 let linknentry text key =
2483 let c =
2484 if key >= 32 && key < 127
2485 then Char.chr key
2486 else '\000'
2488 match c with
2489 | 'a' .. 'z' ->
2490 let text = addchar text c in
2491 TEcont text
2493 | _ ->
2494 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2495 TEcont text
2498 let linkndone f s =
2499 if String.length s > 0
2500 then (
2501 let n =
2502 let l = String.length s in
2503 let rec loop pos n = if pos = l then n else
2504 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2505 loop (pos+1) (n*26 + m)
2506 in loop 0 0
2508 let rec loop n = function
2509 | [] -> ()
2510 | l :: rest ->
2511 match getopaque l.pageno with
2512 | None -> loop n rest
2513 | Some opaque ->
2514 let m = getlinkcount opaque in
2515 if n < m
2516 then (
2517 let under = getlink opaque n in
2518 f under
2520 else loop (n-m) rest
2522 loop n state.layout;
2526 let textentry text key =
2527 if key land 0xff00 = 0xff00
2528 then TEcont text
2529 else TEcont (text ^ Wsi.toutf8 key)
2532 let reqlayout angle proportional =
2533 match state.throttle with
2534 | None ->
2535 if nogeomcmds state.geomcmds
2536 then state.anchor <- getanchor ();
2537 conf.angle <- angle mod 360;
2538 if conf.angle != 0
2539 then (
2540 match state.mode with
2541 | LinkNav _ -> state.mode <- View
2542 | _ -> ()
2544 conf.proportional <- proportional;
2545 invalidate "reqlayout"
2546 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2547 | _ -> ()
2550 let settrim trimmargins trimfuzz =
2551 if nogeomcmds state.geomcmds
2552 then state.anchor <- getanchor ();
2553 conf.trimmargins <- trimmargins;
2554 conf.trimfuzz <- trimfuzz;
2555 let x0, y0, x1, y1 = trimfuzz in
2556 invalidate "settrim"
2557 (fun () ->
2558 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2559 Hashtbl.iter (fun _ opaque ->
2560 wcmd "freepage %s" opaque;
2561 ) state.pagemap;
2562 Hashtbl.clear state.pagemap;
2565 let setzoom zoom =
2566 match state.throttle with
2567 | None ->
2568 let zoom = max 0.01 zoom in
2569 if zoom <> conf.zoom
2570 then (
2571 state.prevzoom <- conf.zoom;
2572 conf.zoom <- zoom;
2573 reshape conf.winw conf.winh;
2574 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2577 | Some (layout, y, started) ->
2578 let time =
2579 match conf.maxwait with
2580 | None -> 0.0
2581 | Some t -> t
2583 let dt = now () -. started in
2584 if dt > time
2585 then (
2586 state.y <- y;
2587 load layout;
2591 let setcolumns mode columns coverA coverB =
2592 if columns < 0
2593 then (
2594 if isbirdseye mode
2595 then showtext '!' "split mode doesn't work in bird's eye"
2596 else (
2597 conf.columns <- Csplit (-columns, [||]);
2598 state.x <- 0;
2599 conf.zoom <- 1.0;
2602 else (
2603 if columns < 2
2604 then (
2605 conf.columns <- Csingle;
2606 state.x <- 0;
2607 setzoom 1.0;
2609 else (
2610 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2611 conf.zoom <- 1.0;
2614 reshape conf.winw conf.winh;
2617 let enterbirdseye () =
2618 let zoom = float conf.thumbw /. float conf.winw in
2619 let birdseyepageno =
2620 let cy = conf.winh / 2 in
2621 let fold = function
2622 | [] -> 0
2623 | l :: rest ->
2624 let rec fold best = function
2625 | [] -> best.pageno
2626 | l :: rest ->
2627 let d = cy - (l.pagedispy + l.pagevh/2)
2628 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2629 if abs d < abs dbest
2630 then fold l rest
2631 else best.pageno
2632 in fold l rest
2634 fold state.layout
2636 state.mode <- Birdseye (
2637 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2639 conf.zoom <- zoom;
2640 conf.presentation <- false;
2641 conf.interpagespace <- 10;
2642 conf.hlinks <- false;
2643 state.x <- 0;
2644 state.mstate <- Mnone;
2645 conf.maxwait <- None;
2646 conf.columns <- (
2647 match conf.beyecolumns with
2648 | Some c ->
2649 conf.zoom <- 1.0;
2650 Cmulti ((c, 0, 0), [||])
2651 | None -> Csingle
2653 Wsi.setcursor Wsi.CURSOR_INHERIT;
2654 if conf.verbose
2655 then
2656 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2657 (100.0*.zoom)
2658 else
2659 state.text <- ""
2661 reshape conf.winw conf.winh;
2664 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2665 state.mode <- View;
2666 conf.zoom <- c.zoom;
2667 conf.presentation <- c.presentation;
2668 conf.interpagespace <- c.interpagespace;
2669 conf.maxwait <- c.maxwait;
2670 conf.hlinks <- c.hlinks;
2671 conf.beyecolumns <- (
2672 match conf.columns with
2673 | Cmulti ((c, _, _), _) -> Some c
2674 | Csingle -> None
2675 | Csplit _ -> assert false
2677 conf.columns <- (
2678 match c.columns with
2679 | Cmulti (c, _) -> Cmulti (c, [||])
2680 | Csingle -> Csingle
2681 | Csplit _ -> failwith "leaving bird's eye split mode"
2683 state.x <- leftx;
2684 if conf.verbose
2685 then
2686 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2687 (100.0*.conf.zoom)
2689 reshape conf.winw conf.winh;
2690 state.anchor <- if goback then anchor else (pageno, 0.0);
2693 let togglebirdseye () =
2694 match state.mode with
2695 | Birdseye vals -> leavebirdseye vals true
2696 | View -> enterbirdseye ()
2697 | _ -> ()
2700 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2701 let pageno = max 0 (pageno - incr) in
2702 let rec loop = function
2703 | [] -> gotopage1 pageno 0
2704 | l :: _ when l.pageno = pageno ->
2705 if l.pagedispy >= 0 && l.pagey = 0
2706 then G.postRedisplay "upbirdseye"
2707 else gotopage1 pageno 0
2708 | _ :: rest -> loop rest
2710 loop state.layout;
2711 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2714 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2715 let pageno = min (state.pagecount - 1) (pageno + incr) in
2716 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2717 let rec loop = function
2718 | [] ->
2719 let y, h = getpageyh pageno in
2720 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2721 gotoy (clamp dy)
2722 | l :: _ when l.pageno = pageno ->
2723 if l.pagevh != l.pageh
2724 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2725 else G.postRedisplay "downbirdseye"
2726 | _ :: rest -> loop rest
2728 loop state.layout
2731 let optentry mode _ key =
2732 let btos b = if b then "on" else "off" in
2733 if key >= 32 && key < 127
2734 then
2735 let c = Char.chr key in
2736 match c with
2737 | 's' ->
2738 let ondone s =
2739 try conf.scrollstep <- int_of_string s with exc ->
2740 state.text <- Printf.sprintf "bad integer `%s': %s"
2741 s (Printexc.to_string exc)
2743 TEswitch ("scroll step: ", "", None, intentry, ondone)
2745 | 'A' ->
2746 let ondone s =
2748 conf.autoscrollstep <- int_of_string s;
2749 if state.autoscroll <> None
2750 then state.autoscroll <- Some conf.autoscrollstep
2751 with exc ->
2752 state.text <- Printf.sprintf "bad integer `%s': %s"
2753 s (Printexc.to_string exc)
2755 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2757 | 'C' ->
2758 let mode = state.mode in
2759 let ondone s =
2761 let n, a, b = multicolumns_of_string s in
2762 setcolumns mode n a b;
2763 with exc ->
2764 state.text <- Printf.sprintf "bad columns `%s': %s"
2765 s (Printexc.to_string exc)
2767 TEswitch ("columns: ", "", None, textentry, ondone)
2769 | 'Z' ->
2770 let ondone s =
2772 let zoom = float (int_of_string s) /. 100.0 in
2773 setzoom zoom
2774 with exc ->
2775 state.text <- Printf.sprintf "bad integer `%s': %s"
2776 s (Printexc.to_string exc)
2778 TEswitch ("zoom: ", "", None, intentry, ondone)
2780 | 't' ->
2781 let ondone s =
2783 conf.thumbw <- bound (int_of_string s) 2 4096;
2784 state.text <-
2785 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2786 begin match mode with
2787 | Birdseye beye ->
2788 leavebirdseye beye false;
2789 enterbirdseye ();
2790 | _ -> ();
2792 with exc ->
2793 state.text <- Printf.sprintf "bad integer `%s': %s"
2794 s (Printexc.to_string exc)
2796 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2798 | 'R' ->
2799 let ondone s =
2800 match try
2801 Some (int_of_string s)
2802 with exc ->
2803 state.text <- Printf.sprintf "bad integer `%s': %s"
2804 s (Printexc.to_string exc);
2805 None
2806 with
2807 | Some angle -> reqlayout angle conf.proportional
2808 | None -> ()
2810 TEswitch ("rotation: ", "", None, intentry, ondone)
2812 | 'i' ->
2813 conf.icase <- not conf.icase;
2814 TEdone ("case insensitive search " ^ (btos conf.icase))
2816 | 'p' ->
2817 conf.preload <- not conf.preload;
2818 gotoy state.y;
2819 TEdone ("preload " ^ (btos conf.preload))
2821 | 'v' ->
2822 conf.verbose <- not conf.verbose;
2823 TEdone ("verbose " ^ (btos conf.verbose))
2825 | 'd' ->
2826 conf.debug <- not conf.debug;
2827 TEdone ("debug " ^ (btos conf.debug))
2829 | 'h' ->
2830 conf.maxhfit <- not conf.maxhfit;
2831 state.maxy <- calcheight ();
2832 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2834 | 'c' ->
2835 conf.crophack <- not conf.crophack;
2836 TEdone ("crophack " ^ btos conf.crophack)
2838 | 'a' ->
2839 let s =
2840 match conf.maxwait with
2841 | None ->
2842 conf.maxwait <- Some infinity;
2843 "always wait for page to complete"
2844 | Some _ ->
2845 conf.maxwait <- None;
2846 "show placeholder if page is not ready"
2848 TEdone s
2850 | 'f' ->
2851 conf.underinfo <- not conf.underinfo;
2852 TEdone ("underinfo " ^ btos conf.underinfo)
2854 | 'P' ->
2855 conf.savebmarks <- not conf.savebmarks;
2856 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2858 | 'S' ->
2859 let ondone s =
2861 let pageno, py =
2862 match state.layout with
2863 | [] -> 0, 0
2864 | l :: _ ->
2865 l.pageno, l.pagey
2867 conf.interpagespace <- int_of_string s;
2868 docolumns conf.columns;
2869 state.maxy <- calcheight ();
2870 let y = getpagey pageno in
2871 gotoy (y + py)
2872 with exc ->
2873 state.text <- Printf.sprintf "bad integer `%s': %s"
2874 s (Printexc.to_string exc)
2876 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2878 | 'l' ->
2879 reqlayout conf.angle (not conf.proportional);
2880 TEdone ("proportional display " ^ btos conf.proportional)
2882 | 'T' ->
2883 settrim (not conf.trimmargins) conf.trimfuzz;
2884 TEdone ("trim margins " ^ btos conf.trimmargins)
2886 | 'I' ->
2887 conf.invert <- not conf.invert;
2888 TEdone ("invert colors " ^ btos conf.invert)
2890 | 'x' ->
2891 let ondone s =
2892 cbput state.hists.sel s;
2893 conf.selcmd <- s;
2895 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2896 textentry, ondone)
2898 | _ ->
2899 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2900 TEstop
2901 else
2902 TEcont state.text
2905 class type lvsource = object
2906 method getitemcount : int
2907 method getitem : int -> (string * int)
2908 method hasaction : int -> bool
2909 method exit :
2910 uioh:uioh ->
2911 cancel:bool ->
2912 active:int ->
2913 first:int ->
2914 pan:int ->
2915 qsearch:string ->
2916 uioh option
2917 method getactive : int
2918 method getfirst : int
2919 method getqsearch : string
2920 method setqsearch : string -> unit
2921 method getpan : int
2922 end;;
2924 class virtual lvsourcebase = object
2925 val mutable m_active = 0
2926 val mutable m_first = 0
2927 val mutable m_qsearch = ""
2928 val mutable m_pan = 0
2929 method getactive = m_active
2930 method getfirst = m_first
2931 method getqsearch = m_qsearch
2932 method getpan = m_pan
2933 method setqsearch s = m_qsearch <- s
2934 end;;
2936 let withoutlastutf8 s =
2937 let len = String.length s in
2938 if len = 0
2939 then s
2940 else
2941 let rec find pos =
2942 if pos = 0
2943 then pos
2944 else
2945 let b = Char.code s.[pos] in
2946 if b land 0b110000 = 0b11000000
2947 then find (pos-1)
2948 else pos-1
2950 let first =
2951 if Char.code s.[len-1] land 0x80 = 0
2952 then len-1
2953 else find (len-1)
2955 String.sub s 0 first;
2958 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2959 let enttext te =
2960 state.mode <- Textentry (te, onleave);
2961 state.text <- "";
2962 enttext ();
2963 G.postRedisplay "textentrykeyboard enttext";
2965 let histaction cmd =
2966 match opthist with
2967 | None -> ()
2968 | Some (action, _) ->
2969 state.mode <- Textentry (
2970 (c, action cmd, opthist, onkey, ondone), onleave
2972 G.postRedisplay "textentry histaction"
2974 match key with
2975 | 0xff08 -> (* backspace *)
2976 let s = withoutlastutf8 text in
2977 let len = String.length s in
2978 if len = 0
2979 then (
2980 onleave Cancel;
2981 G.postRedisplay "textentrykeyboard after cancel";
2983 else (
2984 enttext (c, s, opthist, onkey, ondone)
2987 | 0xff0d ->
2988 ondone text;
2989 onleave Confirm;
2990 G.postRedisplay "textentrykeyboard after confirm"
2992 | 0xff52 -> histaction HCprev
2993 | 0xff54 -> histaction HCnext
2994 | 0xff50 -> histaction HCfirst
2995 | 0xff57 -> histaction HClast
2997 | 0xff1b -> (* escape*)
2998 if String.length text = 0
2999 then (
3000 begin match opthist with
3001 | None -> ()
3002 | Some (_, onhistcancel) -> onhistcancel ()
3003 end;
3004 onleave Cancel;
3005 state.text <- "";
3006 G.postRedisplay "textentrykeyboard after cancel2"
3008 else (
3009 enttext (c, "", opthist, onkey, ondone)
3012 | 0xff9f | 0xffff -> () (* delete *)
3014 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3015 begin match onkey text key with
3016 | TEdone text ->
3017 ondone text;
3018 onleave Confirm;
3019 G.postRedisplay "textentrykeyboard after confirm2";
3021 | TEcont text ->
3022 enttext (c, text, opthist, onkey, ondone);
3024 | TEstop ->
3025 onleave Cancel;
3026 G.postRedisplay "textentrykeyboard after cancel3"
3028 | TEswitch te ->
3029 state.mode <- Textentry (te, onleave);
3030 G.postRedisplay "textentrykeyboard switch";
3031 end;
3033 | _ ->
3034 vlog "unhandled key %s" (Wsi.keyname key)
3037 let firstof first active =
3038 if first > active || abs (first - active) > fstate.maxrows - 1
3039 then max 0 (active - (fstate.maxrows/2))
3040 else first
3043 let calcfirst first active =
3044 if active > first
3045 then
3046 let rows = active - first in
3047 if rows > fstate.maxrows then active - fstate.maxrows else first
3048 else active
3051 let scrollph y maxy =
3052 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3053 let sh = float conf.winh /. sh in
3054 let sh = max sh (float conf.scrollh) in
3056 let percent =
3057 if y = state.maxy
3058 then 1.0
3059 else float y /. float maxy
3061 let position = (float conf.winh -. sh) *. percent in
3063 let position =
3064 if position +. sh > float conf.winh
3065 then float conf.winh -. sh
3066 else position
3068 position, sh;
3071 let coe s = (s :> uioh);;
3073 class listview ~(source:lvsource) ~trusted ~modehash =
3074 object (self)
3075 val m_pan = source#getpan
3076 val m_first = source#getfirst
3077 val m_active = source#getactive
3078 val m_qsearch = source#getqsearch
3079 val m_prev_uioh = state.uioh
3081 method private elemunder y =
3082 let n = y / (fstate.fontsize+1) in
3083 if m_first + n < source#getitemcount
3084 then (
3085 if source#hasaction (m_first + n)
3086 then Some (m_first + n)
3087 else None
3089 else None
3091 method display =
3092 Gl.enable `blend;
3093 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3094 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3095 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3096 GlDraw.color (1., 1., 1.);
3097 Gl.enable `texture_2d;
3098 let fs = fstate.fontsize in
3099 let nfs = fs + 1 in
3100 let ww = fstate.wwidth in
3101 let tabw = 30.0*.ww in
3102 let itemcount = source#getitemcount in
3103 let rec loop row =
3104 if (row - m_first) * nfs > conf.winh
3105 then ()
3106 else (
3107 if row >= 0 && row < itemcount
3108 then (
3109 let (s, level) = source#getitem row in
3110 let y = (row - m_first) * nfs in
3111 let x = 5.0 +. float (level + m_pan) *. ww in
3112 if row = m_active
3113 then (
3114 Gl.disable `texture_2d;
3115 GlDraw.polygon_mode `both `line;
3116 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3117 GlDraw.rect (1., float (y + 1))
3118 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3119 GlDraw.polygon_mode `both `fill;
3120 GlDraw.color (1., 1., 1.);
3121 Gl.enable `texture_2d;
3124 let drawtabularstring s =
3125 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3126 if trusted
3127 then
3128 let tabpos = try String.index s '\t' with Not_found -> -1 in
3129 if tabpos > 0
3130 then
3131 let len = String.length s - tabpos - 1 in
3132 let s1 = String.sub s 0 tabpos
3133 and s2 = String.sub s (tabpos + 1) len in
3134 let nx = drawstr x s1 in
3135 let sw = nx -. x in
3136 let x = x +. (max tabw sw) in
3137 drawstr x s2
3138 else
3139 drawstr x s
3140 else
3141 drawstr x s
3143 let _ = drawtabularstring s in
3144 loop (row+1)
3148 loop m_first;
3149 Gl.disable `blend;
3150 Gl.disable `texture_2d;
3152 method updownlevel incr =
3153 let len = source#getitemcount in
3154 let curlevel =
3155 if m_active >= 0 && m_active < len
3156 then snd (source#getitem m_active)
3157 else -1
3159 let rec flow i =
3160 if i = len then i-1 else if i = -1 then 0 else
3161 let _, l = source#getitem i in
3162 if l != curlevel then i else flow (i+incr)
3164 let active = flow m_active in
3165 let first = calcfirst m_first active in
3166 G.postRedisplay "outline updownlevel";
3167 {< m_active = active; m_first = first >}
3169 method private key1 key mask =
3170 let set1 active first qsearch =
3171 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3173 let search active pattern incr =
3174 let dosearch re =
3175 let rec loop n =
3176 if n >= 0 && n < source#getitemcount
3177 then (
3178 let s, _ = source#getitem n in
3180 (try ignore (Str.search_forward re s 0); true
3181 with Not_found -> false)
3182 then Some n
3183 else loop (n + incr)
3185 else None
3187 loop active
3190 let re = Str.regexp_case_fold pattern in
3191 dosearch re
3192 with Failure s ->
3193 state.text <- s;
3194 None
3196 let itemcount = source#getitemcount in
3197 let find start incr =
3198 let rec find i =
3199 if i = -1 || i = itemcount
3200 then -1
3201 else (
3202 if source#hasaction i
3203 then i
3204 else find (i + incr)
3207 find start
3209 let set active first =
3210 let first = bound first 0 (itemcount - fstate.maxrows) in
3211 state.text <- "";
3212 coe {< m_active = active; m_first = first >}
3214 let navigate incr =
3215 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3216 let active, first =
3217 let incr1 = if incr > 0 then 1 else -1 in
3218 if isvisible m_first m_active
3219 then
3220 let next =
3221 let next = m_active + incr in
3222 let next =
3223 if next < 0 || next >= itemcount
3224 then -1
3225 else find next incr1
3227 if next = -1 || abs (m_active - next) > fstate.maxrows
3228 then -1
3229 else next
3231 if next = -1
3232 then
3233 let first = m_first + incr in
3234 let first = bound first 0 (itemcount - 1) in
3235 let next =
3236 let next = m_active + incr in
3237 let next = bound next 0 (itemcount - 1) in
3238 find next ~-incr1
3240 let active = if next = -1 then m_active else next in
3241 active, first
3242 else
3243 let first = min next m_first in
3244 let first =
3245 if abs (next - first) > fstate.maxrows
3246 then first + incr
3247 else first
3249 next, first
3250 else
3251 let first = m_first + incr in
3252 let first = bound first 0 (itemcount - 1) in
3253 let active =
3254 let next = m_active + incr in
3255 let next = bound next 0 (itemcount - 1) in
3256 let next = find next incr1 in
3257 let active =
3258 if next = -1 || abs (m_active - first) > fstate.maxrows
3259 then (
3260 let active = if m_active = -1 then next else m_active in
3261 active
3263 else next
3265 if isvisible first active
3266 then active
3267 else -1
3269 active, first
3271 G.postRedisplay "listview navigate";
3272 set active first;
3274 match key with
3275 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3276 let incr = if key = 0x72 then -1 else 1 in
3277 let active, first =
3278 match search (m_active + incr) m_qsearch incr with
3279 | None ->
3280 state.text <- m_qsearch ^ " [not found]";
3281 m_active, m_first
3282 | Some active ->
3283 state.text <- m_qsearch;
3284 active, firstof m_first active
3286 G.postRedisplay "listview ctrl-r/s";
3287 set1 active first m_qsearch;
3289 | 0xff08 -> (* backspace *)
3290 if String.length m_qsearch = 0
3291 then coe self
3292 else (
3293 let qsearch = withoutlastutf8 m_qsearch in
3294 let len = String.length qsearch in
3295 if len = 0
3296 then (
3297 state.text <- "";
3298 G.postRedisplay "listview empty qsearch";
3299 set1 m_active m_first "";
3301 else
3302 let active, first =
3303 match search m_active qsearch ~-1 with
3304 | None ->
3305 state.text <- qsearch ^ " [not found]";
3306 m_active, m_first
3307 | Some active ->
3308 state.text <- qsearch;
3309 active, firstof m_first active
3311 G.postRedisplay "listview backspace qsearch";
3312 set1 active first qsearch
3315 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3316 let pattern = m_qsearch ^ Wsi.toutf8 key in
3317 let active, first =
3318 match search m_active pattern 1 with
3319 | None ->
3320 state.text <- pattern ^ " [not found]";
3321 m_active, m_first
3322 | Some active ->
3323 state.text <- pattern;
3324 active, firstof m_first active
3326 G.postRedisplay "listview qsearch add";
3327 set1 active first pattern;
3329 | 0xff1b -> (* escape *)
3330 state.text <- "";
3331 if String.length m_qsearch = 0
3332 then (
3333 G.postRedisplay "list view escape";
3334 begin
3335 match
3336 source#exit (coe self) true m_active m_first m_pan m_qsearch
3337 with
3338 | None -> m_prev_uioh
3339 | Some uioh -> uioh
3342 else (
3343 G.postRedisplay "list view kill qsearch";
3344 source#setqsearch "";
3345 coe {< m_qsearch = "" >}
3348 | 0xff0d -> (* return *)
3349 state.text <- "";
3350 let self = {< m_qsearch = "" >} in
3351 source#setqsearch "";
3352 let opt =
3353 G.postRedisplay "listview enter";
3354 if m_active >= 0 && m_active < source#getitemcount
3355 then (
3356 source#exit (coe self) false m_active m_first m_pan "";
3358 else (
3359 source#exit (coe self) true m_active m_first m_pan "";
3362 begin match opt with
3363 | None -> m_prev_uioh
3364 | Some uioh -> uioh
3367 | 0xff9f | 0xffff -> (* delete *)
3368 coe self
3370 | 0xff52 -> navigate ~-1 (* up *)
3371 | 0xff54 -> navigate 1 (* down *)
3372 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3373 | 0xff56 -> navigate fstate.maxrows (* next *)
3375 | 0xff53 -> (* right *)
3376 state.text <- "";
3377 G.postRedisplay "listview right";
3378 coe {< m_pan = m_pan - 1 >}
3380 | 0xff51 -> (* left *)
3381 state.text <- "";
3382 G.postRedisplay "listview left";
3383 coe {< m_pan = m_pan + 1 >}
3385 | 0xff50 -> (* home *)
3386 let active = find 0 1 in
3387 G.postRedisplay "listview home";
3388 set active 0;
3390 | 0xff57 -> (* end *)
3391 let first = max 0 (itemcount - fstate.maxrows) in
3392 let active = find (itemcount - 1) ~-1 in
3393 G.postRedisplay "listview end";
3394 set active first;
3396 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3397 coe self
3399 | _ ->
3400 dolog "listview unknown key %#x" key; coe self
3402 method key key mask =
3403 match state.mode with
3404 | Textentry te -> textentrykeyboard key mask te; coe self
3405 | _ -> self#key1 key mask
3407 method button button down x y _ =
3408 let opt =
3409 match button with
3410 | 1 when x > conf.winw - conf.scrollbw ->
3411 G.postRedisplay "listview scroll";
3412 if down
3413 then
3414 let _, position, sh = self#scrollph in
3415 if y > truncate position && y < truncate (position +. sh)
3416 then (
3417 state.mstate <- Mscrolly;
3418 Some (coe self)
3420 else
3421 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3422 let first = truncate (s *. float source#getitemcount) in
3423 let first = min source#getitemcount first in
3424 Some (coe {< m_first = first; m_active = first >})
3425 else (
3426 state.mstate <- Mnone;
3427 Some (coe self);
3429 | 1 when not down ->
3430 begin match self#elemunder y with
3431 | Some n ->
3432 G.postRedisplay "listview click";
3433 source#exit
3434 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3435 | _ ->
3436 Some (coe self)
3438 | n when (n == 4 || n == 5) && not down ->
3439 let len = source#getitemcount in
3440 let first =
3441 if n = 5 && m_first + fstate.maxrows >= len
3442 then
3443 m_first
3444 else
3445 let first = m_first + (if n == 4 then -1 else 1) in
3446 bound first 0 (len - 1)
3448 G.postRedisplay "listview wheel";
3449 Some (coe {< m_first = first >})
3450 | _ ->
3451 Some (coe self)
3453 match opt with
3454 | None -> m_prev_uioh
3455 | Some uioh -> uioh
3457 method motion _ y =
3458 match state.mstate with
3459 | Mscrolly ->
3460 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3461 let first = truncate (s *. float source#getitemcount) in
3462 let first = min source#getitemcount first in
3463 G.postRedisplay "listview motion";
3464 coe {< m_first = first; m_active = first >}
3465 | _ -> coe self
3467 method pmotion x y =
3468 if x < conf.winw - conf.scrollbw
3469 then
3470 let n =
3471 match self#elemunder y with
3472 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3473 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3475 let o =
3476 if n != m_active
3477 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3478 else self
3480 coe o
3481 else (
3482 Wsi.setcursor Wsi.CURSOR_INHERIT;
3483 coe self
3486 method infochanged _ = ()
3488 method scrollpw = (0, 0.0, 0.0)
3489 method scrollph =
3490 let nfs = fstate.fontsize + 1 in
3491 let y = m_first * nfs in
3492 let itemcount = source#getitemcount in
3493 let maxi = max 0 (itemcount - fstate.maxrows) in
3494 let maxy = maxi * nfs in
3495 let p, h = scrollph y maxy in
3496 conf.scrollbw, p, h
3498 method modehash = modehash
3499 end;;
3501 class outlinelistview ~source =
3502 object (self)
3503 inherit listview
3504 ~source:(source :> lvsource)
3505 ~trusted:false
3506 ~modehash:(findkeyhash conf "outline")
3507 as super
3509 method key key mask =
3510 let calcfirst first active =
3511 if active > first
3512 then
3513 let rows = active - first in
3514 if rows > fstate.maxrows then active - fstate.maxrows else first
3515 else active
3517 let navigate incr =
3518 let active = m_active + incr in
3519 let active = bound active 0 (source#getitemcount - 1) in
3520 let first = calcfirst m_first active in
3521 G.postRedisplay "outline navigate";
3522 coe {< m_active = active; m_first = first >}
3524 let ctrl = Wsi.withctrl mask in
3525 match key with
3526 | 110 when ctrl -> (* ctrl-n *)
3527 source#narrow m_qsearch;
3528 G.postRedisplay "outline ctrl-n";
3529 coe {< m_first = 0; m_active = 0 >}
3531 | 117 when ctrl -> (* ctrl-u *)
3532 source#denarrow;
3533 G.postRedisplay "outline ctrl-u";
3534 state.text <- "";
3535 coe {< m_first = 0; m_active = 0 >}
3537 | 108 when ctrl -> (* ctrl-l *)
3538 let first = m_active - (fstate.maxrows / 2) in
3539 G.postRedisplay "outline ctrl-l";
3540 coe {< m_first = first >}
3542 | 0xff9f | 0xffff -> (* delete *)
3543 source#remove m_active;
3544 G.postRedisplay "outline delete";
3545 let active = max 0 (m_active-1) in
3546 coe {< m_first = firstof m_first active;
3547 m_active = active >}
3549 | 0xff52 -> navigate ~-1 (* up *)
3550 | 0xff54 -> navigate 1 (* down *)
3551 | 0xff55 -> (* prior *)
3552 navigate ~-(fstate.maxrows)
3553 | 0xff56 -> (* next *)
3554 navigate fstate.maxrows
3556 | 0xff53 -> (* [ctrl-]right *)
3557 let o =
3558 if ctrl
3559 then (
3560 G.postRedisplay "outline ctrl right";
3561 {< m_pan = m_pan + 1 >}
3563 else self#updownlevel 1
3565 coe o
3567 | 0xff51 -> (* [ctrl-]left *)
3568 let o =
3569 if ctrl
3570 then (
3571 G.postRedisplay "outline ctrl left";
3572 {< m_pan = m_pan - 1 >}
3574 else self#updownlevel ~-1
3576 coe o
3578 | 0xff50 -> (* home *)
3579 G.postRedisplay "outline home";
3580 coe {< m_first = 0; m_active = 0 >}
3582 | 0xff57 -> (* end *)
3583 let active = source#getitemcount - 1 in
3584 let first = max 0 (active - fstate.maxrows) in
3585 G.postRedisplay "outline end";
3586 coe {< m_active = active; m_first = first >}
3588 | _ -> super#key key mask
3591 let outlinesource usebookmarks =
3592 let empty = [||] in
3593 (object
3594 inherit lvsourcebase
3595 val mutable m_items = empty
3596 val mutable m_orig_items = empty
3597 val mutable m_prev_items = empty
3598 val mutable m_narrow_pattern = ""
3599 val mutable m_hadremovals = false
3601 method getitemcount =
3602 Array.length m_items + (if m_hadremovals then 1 else 0)
3604 method getitem n =
3605 if n == Array.length m_items && m_hadremovals
3606 then
3607 ("[Confirm removal]", 0)
3608 else
3609 let s, n, _ = m_items.(n) in
3610 (s, n)
3612 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3613 ignore (uioh, first, qsearch);
3614 let confrimremoval = m_hadremovals && active = Array.length m_items in
3615 let items =
3616 if String.length m_narrow_pattern = 0
3617 then m_orig_items
3618 else m_items
3620 if not cancel
3621 then (
3622 if not confrimremoval
3623 then(
3624 let _, _, anchor = m_items.(active) in
3625 gotoanchor anchor;
3626 m_items <- items;
3628 else (
3629 state.bookmarks <- Array.to_list m_items;
3630 m_orig_items <- m_items;
3633 else m_items <- items;
3634 m_pan <- pan;
3635 None
3637 method hasaction _ = true
3639 method greetmsg =
3640 if Array.length m_items != Array.length m_orig_items
3641 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3642 else ""
3644 method narrow pattern =
3645 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3646 match reopt with
3647 | None -> ()
3648 | Some re ->
3649 let rec loop accu n =
3650 if n = -1
3651 then (
3652 m_narrow_pattern <- pattern;
3653 m_items <- Array.of_list accu
3655 else
3656 let (s, _, _) as o = m_items.(n) in
3657 let accu =
3658 if (try ignore (Str.search_forward re s 0); true
3659 with Not_found -> false)
3660 then o :: accu
3661 else accu
3663 loop accu (n-1)
3665 loop [] (Array.length m_items - 1)
3667 method denarrow =
3668 m_orig_items <- (
3669 if usebookmarks
3670 then Array.of_list state.bookmarks
3671 else state.outlines
3673 m_items <- m_orig_items
3675 method remove m =
3676 if usebookmarks
3677 then
3678 if m >= 0 && m < Array.length m_items
3679 then (
3680 m_hadremovals <- true;
3681 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3682 let n = if n >= m then n+1 else n in
3683 m_items.(n)
3687 method reset anchor items =
3688 m_hadremovals <- false;
3689 if m_orig_items == empty || m_prev_items != items
3690 then (
3691 m_orig_items <- items;
3692 if String.length m_narrow_pattern = 0
3693 then m_items <- items;
3695 m_prev_items <- items;
3696 let rely = getanchory anchor in
3697 let active =
3698 let rec loop n best bestd =
3699 if n = Array.length m_items
3700 then best
3701 else
3702 let (_, _, anchor) = m_items.(n) in
3703 let orely = getanchory anchor in
3704 let d = abs (orely - rely) in
3705 if d < bestd
3706 then loop (n+1) n d
3707 else loop (n+1) best bestd
3709 loop 0 ~-1 max_int
3711 m_active <- active;
3712 m_first <- firstof m_first active
3713 end)
3716 let enterselector usebookmarks =
3717 let source = outlinesource usebookmarks in
3718 fun errmsg ->
3719 let outlines =
3720 if usebookmarks
3721 then Array.of_list state.bookmarks
3722 else state.outlines
3724 if Array.length outlines = 0
3725 then (
3726 showtext ' ' errmsg;
3728 else (
3729 state.text <- source#greetmsg;
3730 Wsi.setcursor Wsi.CURSOR_INHERIT;
3731 let anchor = getanchor () in
3732 source#reset anchor outlines;
3733 state.uioh <- coe (new outlinelistview ~source);
3734 G.postRedisplay "enter selector";
3738 let enteroutlinemode =
3739 let f = enterselector false in
3740 fun ()-> f "Document has no outline";
3743 let enterbookmarkmode =
3744 let f = enterselector true in
3745 fun () -> f "Document has no bookmarks (yet)";
3748 let color_of_string s =
3749 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3750 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3754 let color_to_string (r, g, b) =
3755 let r = truncate (r *. 256.0)
3756 and g = truncate (g *. 256.0)
3757 and b = truncate (b *. 256.0) in
3758 Printf.sprintf "%d/%d/%d" r g b
3761 let irect_of_string s =
3762 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3765 let irect_to_string (x0,y0,x1,y1) =
3766 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3769 let makecheckers () =
3770 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3771 following to say:
3772 converted by Issac Trotts. July 25, 2002 *)
3773 let image_height = 64
3774 and image_width = 64 in
3776 let make_image () =
3777 let image =
3778 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3780 for i = 0 to image_width - 1 do
3781 for j = 0 to image_height - 1 do
3782 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3783 (if (i land 8 ) lxor (j land 8) = 0
3784 then [|255;255;255|] else [|200;200;200|])
3785 done
3786 done;
3787 image
3789 let image = make_image () in
3790 let id = GlTex.gen_texture () in
3791 GlTex.bind_texture `texture_2d id;
3792 GlPix.store (`unpack_alignment 1);
3793 GlTex.image2d image;
3794 List.iter (GlTex.parameter ~target:`texture_2d)
3795 [ `wrap_s `repeat;
3796 `wrap_t `repeat;
3797 `mag_filter `nearest;
3798 `min_filter `nearest ];
3802 let setcheckers enabled =
3803 match state.texid with
3804 | None ->
3805 if enabled then state.texid <- Some (makecheckers ())
3807 | Some texid ->
3808 if not enabled
3809 then (
3810 GlTex.delete_texture texid;
3811 state.texid <- None;
3815 let int_of_string_with_suffix s =
3816 let l = String.length s in
3817 let s1, shift =
3818 if l > 1
3819 then
3820 let suffix = Char.lowercase s.[l-1] in
3821 match suffix with
3822 | 'k' -> String.sub s 0 (l-1), 10
3823 | 'm' -> String.sub s 0 (l-1), 20
3824 | 'g' -> String.sub s 0 (l-1), 30
3825 | _ -> s, 0
3826 else s, 0
3828 let n = int_of_string s1 in
3829 let m = n lsl shift in
3830 if m < 0 || m < n
3831 then raise (Failure "value too large")
3832 else m
3835 let string_with_suffix_of_int n =
3836 if n = 0
3837 then "0"
3838 else
3839 let n, s =
3840 if n = 0
3841 then 0, ""
3842 else (
3843 if n land ((1 lsl 20) - 1) = 0
3844 then n lsr 20, "M"
3845 else (
3846 if n land ((1 lsl 10) - 1) = 0
3847 then n lsr 10, "K"
3848 else n, ""
3852 let rec loop s n =
3853 let h = n mod 1000 in
3854 let n = n / 1000 in
3855 if n = 0
3856 then string_of_int h ^ s
3857 else (
3858 let s = Printf.sprintf "_%03d%s" h s in
3859 loop s n
3862 loop "" n ^ s;
3865 let defghyllscroll = (40, 8, 32);;
3866 let ghyllscroll_of_string s =
3867 let (n, a, b) as nab =
3868 if s = "default"
3869 then defghyllscroll
3870 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3872 if n <= a || n <= b || a >= b
3873 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3874 nab;
3877 let ghyllscroll_to_string ((n, a, b) as nab) =
3878 if nab = defghyllscroll
3879 then "default"
3880 else Printf.sprintf "%d,%d,%d" n a b;
3883 let describe_location () =
3884 let f (fn, _) l =
3885 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3887 let fn, ln = List.fold_left f (-1, -1) state.layout in
3888 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3889 let percent =
3890 if maxy <= 0
3891 then 100.
3892 else (100. *. (float state.y /. float maxy))
3894 if fn = ln
3895 then
3896 Printf.sprintf "page %d of %d [%.2f%%]"
3897 (fn+1) state.pagecount percent
3898 else
3899 Printf.sprintf
3900 "pages %d-%d of %d [%.2f%%]"
3901 (fn+1) (ln+1) state.pagecount percent
3904 let enterinfomode =
3905 let btos b = if b then "\xe2\x88\x9a" else "" in
3906 let showextended = ref false in
3907 let leave mode = function
3908 | Confirm -> state.mode <- mode
3909 | Cancel -> state.mode <- mode in
3910 let src =
3911 (object
3912 val mutable m_first_time = true
3913 val mutable m_l = []
3914 val mutable m_a = [||]
3915 val mutable m_prev_uioh = nouioh
3916 val mutable m_prev_mode = View
3918 inherit lvsourcebase
3920 method reset prev_mode prev_uioh =
3921 m_a <- Array.of_list (List.rev m_l);
3922 m_l <- [];
3923 m_prev_mode <- prev_mode;
3924 m_prev_uioh <- prev_uioh;
3925 if m_first_time
3926 then (
3927 let rec loop n =
3928 if n >= Array.length m_a
3929 then ()
3930 else
3931 match m_a.(n) with
3932 | _, _, _, Action _ -> m_active <- n
3933 | _ -> loop (n+1)
3935 loop 0;
3936 m_first_time <- false;
3939 method int name get set =
3940 m_l <-
3941 (name, `int get, 1, Action (
3942 fun u ->
3943 let ondone s =
3944 try set (int_of_string s)
3945 with exn ->
3946 state.text <- Printf.sprintf "bad integer `%s': %s"
3947 s (Printexc.to_string exn)
3949 state.text <- "";
3950 let te = name ^ ": ", "", None, intentry, ondone in
3951 state.mode <- Textentry (te, leave m_prev_mode);
3953 )) :: m_l
3955 method int_with_suffix name get set =
3956 m_l <-
3957 (name, `intws get, 1, Action (
3958 fun u ->
3959 let ondone s =
3960 try set (int_of_string_with_suffix s)
3961 with exn ->
3962 state.text <- Printf.sprintf "bad integer `%s': %s"
3963 s (Printexc.to_string exn)
3965 state.text <- "";
3966 let te =
3967 name ^ ": ", "", None, intentry_with_suffix, ondone
3969 state.mode <- Textentry (te, leave m_prev_mode);
3971 )) :: m_l
3973 method bool ?(offset=1) ?(btos=btos) name get set =
3974 m_l <-
3975 (name, `bool (btos, get), offset, Action (
3976 fun u ->
3977 let v = get () in
3978 set (not v);
3980 )) :: m_l
3982 method color name get set =
3983 m_l <-
3984 (name, `color get, 1, Action (
3985 fun u ->
3986 let invalid = (nan, nan, nan) in
3987 let ondone s =
3988 let c =
3989 try color_of_string s
3990 with exn ->
3991 state.text <- Printf.sprintf "bad color `%s': %s"
3992 s (Printexc.to_string exn);
3993 invalid
3995 if c <> invalid
3996 then set c;
3998 let te = name ^ ": ", "", None, textentry, ondone in
3999 state.text <- color_to_string (get ());
4000 state.mode <- Textentry (te, leave m_prev_mode);
4002 )) :: m_l
4004 method string name get set =
4005 m_l <-
4006 (name, `string get, 1, Action (
4007 fun u ->
4008 let ondone s = set s in
4009 let te = name ^ ": ", "", None, textentry, ondone in
4010 state.mode <- Textentry (te, leave m_prev_mode);
4012 )) :: m_l
4014 method colorspace name get set =
4015 m_l <-
4016 (name, `string get, 1, Action (
4017 fun _ ->
4018 let source =
4019 let vals = [| "rgb"; "bgr"; "gray" |] in
4020 (object
4021 inherit lvsourcebase
4023 initializer
4024 m_active <- int_of_colorspace conf.colorspace;
4025 m_first <- 0;
4027 method getitemcount = Array.length vals
4028 method getitem n = (vals.(n), 0)
4029 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4030 ignore (uioh, first, pan, qsearch);
4031 if not cancel then set active;
4032 None
4033 method hasaction _ = true
4034 end)
4036 state.text <- "";
4037 let modehash = findkeyhash conf "info" in
4038 coe (new listview ~source ~trusted:true ~modehash)
4039 )) :: m_l
4041 method caption s offset =
4042 m_l <- (s, `empty, offset, Noaction) :: m_l
4044 method caption2 s f offset =
4045 m_l <- (s, `string f, offset, Noaction) :: m_l
4047 method getitemcount = Array.length m_a
4049 method getitem n =
4050 let tostr = function
4051 | `int f -> string_of_int (f ())
4052 | `intws f -> string_with_suffix_of_int (f ())
4053 | `string f -> f ()
4054 | `color f -> color_to_string (f ())
4055 | `bool (btos, f) -> btos (f ())
4056 | `empty -> ""
4058 let name, t, offset, _ = m_a.(n) in
4059 ((let s = tostr t in
4060 if String.length s > 0
4061 then Printf.sprintf "%s\t%s" name s
4062 else name),
4063 offset)
4065 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4066 let uiohopt =
4067 if not cancel
4068 then (
4069 m_qsearch <- qsearch;
4070 let uioh =
4071 match m_a.(active) with
4072 | _, _, _, Action f -> f uioh
4073 | _ -> uioh
4075 Some uioh
4077 else None
4079 m_active <- active;
4080 m_first <- first;
4081 m_pan <- pan;
4082 uiohopt
4084 method hasaction n =
4085 match m_a.(n) with
4086 | _, _, _, Action _ -> true
4087 | _ -> false
4088 end)
4090 let rec fillsrc prevmode prevuioh =
4091 let sep () = src#caption "" 0 in
4092 let colorp name get set =
4093 src#string name
4094 (fun () -> color_to_string (get ()))
4095 (fun v ->
4097 let c = color_of_string v in
4098 set c
4099 with exn ->
4100 state.text <- Printf.sprintf "bad color `%s': %s"
4101 v (Printexc.to_string exn);
4104 let oldmode = state.mode in
4105 let birdseye = isbirdseye state.mode in
4107 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4109 src#bool "presentation mode"
4110 (fun () -> conf.presentation)
4111 (fun v ->
4112 conf.presentation <- v;
4113 state.anchor <- getanchor ();
4114 represent ());
4116 src#bool "ignore case in searches"
4117 (fun () -> conf.icase)
4118 (fun v -> conf.icase <- v);
4120 src#bool "preload"
4121 (fun () -> conf.preload)
4122 (fun v -> conf.preload <- v);
4124 src#bool "highlight links"
4125 (fun () -> conf.hlinks)
4126 (fun v -> conf.hlinks <- v);
4128 src#bool "under info"
4129 (fun () -> conf.underinfo)
4130 (fun v -> conf.underinfo <- v);
4132 src#bool "persistent bookmarks"
4133 (fun () -> conf.savebmarks)
4134 (fun v -> conf.savebmarks <- v);
4136 src#bool "proportional display"
4137 (fun () -> conf.proportional)
4138 (fun v -> reqlayout conf.angle v);
4140 src#bool "trim margins"
4141 (fun () -> conf.trimmargins)
4142 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4144 src#bool "persistent location"
4145 (fun () -> conf.jumpback)
4146 (fun v -> conf.jumpback <- v);
4148 sep ();
4149 src#int "inter-page space"
4150 (fun () -> conf.interpagespace)
4151 (fun n ->
4152 conf.interpagespace <- n;
4153 docolumns conf.columns;
4154 let pageno, py =
4155 match state.layout with
4156 | [] -> 0, 0
4157 | l :: _ ->
4158 l.pageno, l.pagey
4160 state.maxy <- calcheight ();
4161 let y = getpagey pageno in
4162 gotoy (y + py)
4165 src#int "page bias"
4166 (fun () -> conf.pagebias)
4167 (fun v -> conf.pagebias <- v);
4169 src#int "scroll step"
4170 (fun () -> conf.scrollstep)
4171 (fun n -> conf.scrollstep <- n);
4173 src#int "auto scroll step"
4174 (fun () ->
4175 match state.autoscroll with
4176 | Some step -> step
4177 | _ -> conf.autoscrollstep)
4178 (fun n ->
4179 if state.autoscroll <> None
4180 then state.autoscroll <- Some n;
4181 conf.autoscrollstep <- n);
4183 src#int "zoom"
4184 (fun () -> truncate (conf.zoom *. 100.))
4185 (fun v -> setzoom ((float v) /. 100.));
4187 src#int "rotation"
4188 (fun () -> conf.angle)
4189 (fun v -> reqlayout v conf.proportional);
4191 src#int "scroll bar width"
4192 (fun () -> state.scrollw)
4193 (fun v ->
4194 state.scrollw <- v;
4195 conf.scrollbw <- v;
4196 reshape conf.winw conf.winh;
4199 src#int "scroll handle height"
4200 (fun () -> conf.scrollh)
4201 (fun v -> conf.scrollh <- v;);
4203 src#int "thumbnail width"
4204 (fun () -> conf.thumbw)
4205 (fun v ->
4206 conf.thumbw <- min 4096 v;
4207 match oldmode with
4208 | Birdseye beye ->
4209 leavebirdseye beye false;
4210 enterbirdseye ()
4211 | _ -> ()
4214 let mode = state.mode in
4215 src#string "columns"
4216 (fun () ->
4217 match conf.columns with
4218 | Csingle -> "1"
4219 | Cmulti (multi, _) -> multicolumns_to_string multi
4220 | Csplit (count, _) -> "-" ^ string_of_int count
4222 (fun v ->
4223 let n, a, b = multicolumns_of_string v in
4224 setcolumns mode n a b);
4226 sep ();
4227 src#caption "Presentation mode" 0;
4228 src#bool "scrollbar visible"
4229 (fun () -> conf.scrollbarinpm)
4230 (fun v ->
4231 if v != conf.scrollbarinpm
4232 then (
4233 conf.scrollbarinpm <- v;
4234 if conf.presentation
4235 then (
4236 state.scrollw <- if v then conf.scrollbw else 0;
4237 reshape conf.winw conf.winh;
4242 sep ();
4243 src#caption "Pixmap cache" 0;
4244 src#int_with_suffix "size (advisory)"
4245 (fun () -> conf.memlimit)
4246 (fun v -> conf.memlimit <- v);
4248 src#caption2 "used"
4249 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4250 (string_with_suffix_of_int state.memused)
4251 (Hashtbl.length state.tilemap)) 1;
4253 sep ();
4254 src#caption "Layout" 0;
4255 src#caption2 "Dimension"
4256 (fun () ->
4257 Printf.sprintf "%dx%d (virtual %dx%d)"
4258 conf.winw conf.winh
4259 state.w state.maxy)
4261 if conf.debug
4262 then
4263 src#caption2 "Position" (fun () ->
4264 Printf.sprintf "%dx%d" state.x state.y
4266 else
4267 src#caption2 "Visible" (fun () -> describe_location ()) 1
4270 sep ();
4271 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4272 "Save these parameters as global defaults at exit"
4273 (fun () -> conf.bedefault)
4274 (fun v -> conf.bedefault <- v)
4277 sep ();
4278 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4279 src#bool ~offset:0 ~btos "Extended parameters"
4280 (fun () -> !showextended)
4281 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4282 if !showextended
4283 then (
4284 src#bool "checkers"
4285 (fun () -> conf.checkers)
4286 (fun v -> conf.checkers <- v; setcheckers v);
4287 src#bool "update cursor"
4288 (fun () -> conf.updatecurs)
4289 (fun v -> conf.updatecurs <- v);
4290 src#bool "verbose"
4291 (fun () -> conf.verbose)
4292 (fun v -> conf.verbose <- v);
4293 src#bool "invert colors"
4294 (fun () -> conf.invert)
4295 (fun v -> conf.invert <- v);
4296 src#bool "max fit"
4297 (fun () -> conf.maxhfit)
4298 (fun v -> conf.maxhfit <- v);
4299 src#bool "redirect stderr"
4300 (fun () -> conf.redirectstderr)
4301 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4302 src#string "uri launcher"
4303 (fun () -> conf.urilauncher)
4304 (fun v -> conf.urilauncher <- v);
4305 src#string "path launcher"
4306 (fun () -> conf.pathlauncher)
4307 (fun v -> conf.pathlauncher <- v);
4308 src#string "tile size"
4309 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4310 (fun v ->
4312 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4313 conf.tilew <- max 64 w;
4314 conf.tileh <- max 64 h;
4315 flushtiles ();
4316 with exn ->
4317 state.text <- Printf.sprintf "bad tile size `%s': %s"
4318 v (Printexc.to_string exn));
4319 src#int "texture count"
4320 (fun () -> conf.texcount)
4321 (fun v ->
4322 if realloctexts v
4323 then conf.texcount <- v
4324 else showtext '!' " Failed to set texture count please retry later"
4326 src#int "slice height"
4327 (fun () -> conf.sliceheight)
4328 (fun v ->
4329 conf.sliceheight <- v;
4330 wcmd "sliceh %d" conf.sliceheight;
4332 src#int "anti-aliasing level"
4333 (fun () -> conf.aalevel)
4334 (fun v ->
4335 conf.aalevel <- bound v 0 8;
4336 state.anchor <- getanchor ();
4337 opendoc state.path state.password;
4339 src#int "ui font size"
4340 (fun () -> fstate.fontsize)
4341 (fun v -> setfontsize (bound v 5 100));
4342 src#int "hint font size"
4343 (fun () -> conf.hfsize)
4344 (fun v -> conf.hfsize <- bound v 5 100);
4345 colorp "background color"
4346 (fun () -> conf.bgcolor)
4347 (fun v -> conf.bgcolor <- v);
4348 src#bool "crop hack"
4349 (fun () -> conf.crophack)
4350 (fun v -> conf.crophack <- v);
4351 src#string "trim fuzz"
4352 (fun () -> irect_to_string conf.trimfuzz)
4353 (fun v ->
4355 conf.trimfuzz <- irect_of_string v;
4356 if conf.trimmargins
4357 then settrim true conf.trimfuzz;
4358 with exn ->
4359 state.text <- Printf.sprintf "bad irect `%s': %s"
4360 v (Printexc.to_string exn)
4362 src#string "throttle"
4363 (fun () ->
4364 match conf.maxwait with
4365 | None -> "show place holder if page is not ready"
4366 | Some time ->
4367 if time = infinity
4368 then "wait for page to fully render"
4369 else
4370 "wait " ^ string_of_float time
4371 ^ " seconds before showing placeholder"
4373 (fun v ->
4375 let f = float_of_string v in
4376 if f <= 0.0
4377 then conf.maxwait <- None
4378 else conf.maxwait <- Some f
4379 with exn ->
4380 state.text <- Printf.sprintf "bad time `%s': %s"
4381 v (Printexc.to_string exn)
4383 src#string "ghyll scroll"
4384 (fun () ->
4385 match conf.ghyllscroll with
4386 | None -> ""
4387 | Some nab -> ghyllscroll_to_string nab
4389 (fun v ->
4391 let gs =
4392 if String.length v = 0
4393 then None
4394 else Some (ghyllscroll_of_string v)
4396 conf.ghyllscroll <- gs
4397 with exn ->
4398 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4399 v (Printexc.to_string exn)
4401 src#string "selection command"
4402 (fun () -> conf.selcmd)
4403 (fun v -> conf.selcmd <- v);
4404 src#colorspace "color space"
4405 (fun () -> colorspace_to_string conf.colorspace)
4406 (fun v ->
4407 conf.colorspace <- colorspace_of_int v;
4408 wcmd "cs %d" v;
4409 load state.layout;
4413 sep ();
4414 src#caption "Document" 0;
4415 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4416 src#caption2 "Pages"
4417 (fun () -> string_of_int state.pagecount) 1;
4418 src#caption2 "Dimensions"
4419 (fun () -> string_of_int (List.length state.pdims)) 1;
4420 if conf.trimmargins
4421 then (
4422 sep ();
4423 src#caption "Trimmed margins" 0;
4424 src#caption2 "Dimensions"
4425 (fun () -> string_of_int (List.length state.pdims)) 1;
4428 src#reset prevmode prevuioh;
4430 fun () ->
4431 state.text <- "";
4432 let prevmode = state.mode
4433 and prevuioh = state.uioh in
4434 fillsrc prevmode prevuioh;
4435 let source = (src :> lvsource) in
4436 let modehash = findkeyhash conf "info" in
4437 state.uioh <- coe (object (self)
4438 inherit listview ~source ~trusted:true ~modehash as super
4439 val mutable m_prevmemused = 0
4440 method infochanged = function
4441 | Memused ->
4442 if m_prevmemused != state.memused
4443 then (
4444 m_prevmemused <- state.memused;
4445 G.postRedisplay "memusedchanged";
4447 | Pdim -> G.postRedisplay "pdimchanged"
4448 | Docinfo -> fillsrc prevmode prevuioh
4450 method key key mask =
4451 if not (Wsi.withctrl mask)
4452 then
4453 match key with
4454 | 0xff51 -> coe (self#updownlevel ~-1)
4455 | 0xff53 -> coe (self#updownlevel 1)
4456 | _ -> super#key key mask
4457 else super#key key mask
4458 end);
4459 G.postRedisplay "info";
4462 let enterhelpmode =
4463 let source =
4464 (object
4465 inherit lvsourcebase
4466 method getitemcount = Array.length state.help
4467 method getitem n =
4468 let s, n, _ = state.help.(n) in
4469 (s, n)
4471 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4472 let optuioh =
4473 if not cancel
4474 then (
4475 m_qsearch <- qsearch;
4476 match state.help.(active) with
4477 | _, _, Action f -> Some (f uioh)
4478 | _ -> Some (uioh)
4480 else None
4482 m_active <- active;
4483 m_first <- first;
4484 m_pan <- pan;
4485 optuioh
4487 method hasaction n =
4488 match state.help.(n) with
4489 | _, _, Action _ -> true
4490 | _ -> false
4492 initializer
4493 m_active <- -1
4494 end)
4495 in fun () ->
4496 let modehash = findkeyhash conf "help" in
4497 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4498 G.postRedisplay "help";
4501 let entermsgsmode =
4502 let msgsource =
4503 let re = Str.regexp "[\r\n]" in
4504 (object
4505 inherit lvsourcebase
4506 val mutable m_items = [||]
4508 method getitemcount = 1 + Array.length m_items
4510 method getitem n =
4511 if n = 0
4512 then "[Clear]", 0
4513 else m_items.(n-1), 0
4515 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4516 ignore uioh;
4517 if not cancel
4518 then (
4519 if active = 0
4520 then Buffer.clear state.errmsgs;
4521 m_qsearch <- qsearch;
4523 m_active <- active;
4524 m_first <- first;
4525 m_pan <- pan;
4526 None
4528 method hasaction n =
4529 n = 0
4531 method reset =
4532 state.newerrmsgs <- false;
4533 let l = Str.split re (Buffer.contents state.errmsgs) in
4534 m_items <- Array.of_list l
4536 initializer
4537 m_active <- 0
4538 end)
4539 in fun () ->
4540 state.text <- "";
4541 msgsource#reset;
4542 let source = (msgsource :> lvsource) in
4543 let modehash = findkeyhash conf "listview" in
4544 state.uioh <- coe (object
4545 inherit listview ~source ~trusted:false ~modehash as super
4546 method display =
4547 if state.newerrmsgs
4548 then msgsource#reset;
4549 super#display
4550 end);
4551 G.postRedisplay "msgs";
4554 let quickbookmark ?title () =
4555 match state.layout with
4556 | [] -> ()
4557 | l :: _ ->
4558 let title =
4559 match title with
4560 | None ->
4561 let sec = Unix.gettimeofday () in
4562 let tm = Unix.localtime sec in
4563 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4564 (l.pageno+1)
4565 tm.Unix.tm_mday
4566 tm.Unix.tm_mon
4567 (tm.Unix.tm_year + 1900)
4568 tm.Unix.tm_hour
4569 tm.Unix.tm_min
4570 | Some title -> title
4572 state.bookmarks <-
4573 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4574 :: state.bookmarks
4577 let doreshape w h =
4578 state.fullscreen <- None;
4579 Wsi.reshape w h;
4582 let setautoscrollspeed step goingdown =
4583 let incr = max 1 ((abs step) / 2) in
4584 let incr = if goingdown then incr else -incr in
4585 let astep = step + incr in
4586 state.autoscroll <- Some astep;
4589 let gotounder = function
4590 | Ulinkgoto (pageno, top) ->
4591 if pageno >= 0
4592 then (
4593 addnav ();
4594 gotopage1 pageno top;
4597 | Ulinkuri s ->
4598 gotouri s
4600 | Uremote (filename, pageno) ->
4601 let path =
4602 if Sys.file_exists filename
4603 then filename
4604 else
4605 let dir = Filename.dirname state.path in
4606 let path = Filename.concat dir filename in
4607 if Sys.file_exists path
4608 then path
4609 else ""
4611 if String.length path > 0
4612 then (
4613 let anchor = getanchor () in
4614 let ranchor = state.path, state.password, anchor in
4615 state.anchor <- (pageno, 0.0);
4616 state.ranchors <- ranchor :: state.ranchors;
4617 opendoc path "";
4619 else showtext '!' ("Could not find " ^ filename)
4621 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4624 let canpan () =
4625 match conf.columns with
4626 | Csplit _ -> true
4627 | _ -> conf.zoom > 1.0
4630 let viewkeyboard key mask =
4631 let enttext te =
4632 let mode = state.mode in
4633 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4634 state.text <- "";
4635 enttext ();
4636 G.postRedisplay "view:enttext"
4638 let ctrl = Wsi.withctrl mask in
4639 match key with
4640 | 81 -> (* Q *)
4641 exit 0
4643 | 0xff63 -> (* insert *)
4644 if conf.angle mod 360 = 0
4645 then (
4646 state.mode <- LinkNav (Ltgendir 0);
4647 gotoy state.y;
4649 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4651 | 0xff1b | 113 -> (* escape / q *)
4652 begin match state.mstate with
4653 | Mzoomrect _ ->
4654 state.mstate <- Mnone;
4655 Wsi.setcursor Wsi.CURSOR_INHERIT;
4656 G.postRedisplay "kill zoom rect";
4657 | _ ->
4658 match state.ranchors with
4659 | [] -> raise Quit
4660 | (path, password, anchor) :: rest ->
4661 state.ranchors <- rest;
4662 state.anchor <- anchor;
4663 opendoc path password
4664 end;
4666 | 0xff08 -> (* backspace *)
4667 let y = getnav ~-1 in
4668 gotoy_and_clear_text y
4670 | 111 -> (* o *)
4671 enteroutlinemode ()
4673 | 117 -> (* u *)
4674 state.rects <- [];
4675 state.text <- "";
4676 G.postRedisplay "dehighlight";
4678 | 47 | 63 -> (* / ? *)
4679 let ondone isforw s =
4680 cbput state.hists.pat s;
4681 state.searchpattern <- s;
4682 search s isforw
4684 let s = String.create 1 in
4685 s.[0] <- Char.chr key;
4686 enttext (s, "", Some (onhist state.hists.pat),
4687 textentry, ondone (key = 47))
4689 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4690 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4691 setzoom (conf.zoom +. incr)
4693 | 43 | 0xffab -> (* + *)
4694 let ondone s =
4695 let n =
4696 try int_of_string s with exc ->
4697 state.text <- Printf.sprintf "bad integer `%s': %s"
4698 s (Printexc.to_string exc);
4699 max_int
4701 if n != max_int
4702 then (
4703 conf.pagebias <- n;
4704 state.text <- "page bias is now " ^ string_of_int n;
4707 enttext ("page bias: ", "", None, intentry, ondone)
4709 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4710 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4711 setzoom (max 0.01 (conf.zoom -. decr))
4713 | 45 | 0xffad -> (* - *)
4714 let ondone msg = state.text <- msg in
4715 enttext (
4716 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4717 optentry state.mode, ondone
4720 | 48 when ctrl -> (* ctrl-0 *)
4721 setzoom 1.0
4723 | 49 when ctrl -> (* 1 *)
4724 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4725 if zoom < 1.0
4726 then setzoom zoom
4728 | 0xffc6 -> (* f9 *)
4729 togglebirdseye ()
4731 | 57 when ctrl -> (* ctrl-9 *)
4732 togglebirdseye ()
4734 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4735 when not ctrl -> (* 0..9 *)
4736 let ondone s =
4737 let n =
4738 try int_of_string s with exc ->
4739 state.text <- Printf.sprintf "bad integer `%s': %s"
4740 s (Printexc.to_string exc);
4743 if n >= 0
4744 then (
4745 addnav ();
4746 cbput state.hists.pag (string_of_int n);
4747 gotopage1 (n + conf.pagebias - 1) 0;
4750 let pageentry text key =
4751 match Char.unsafe_chr key with
4752 | 'g' -> TEdone text
4753 | _ -> intentry text key
4755 let text = "x" in text.[0] <- Char.chr key;
4756 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4758 | 98 -> (* b *)
4759 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4760 reshape conf.winw conf.winh;
4762 | 108 -> (* l *)
4763 conf.hlinks <- not conf.hlinks;
4764 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4765 G.postRedisplay "toggle highlightlinks";
4767 | 70 -> (* F *)
4768 state.glinks <- true;
4769 let mode = state.mode in
4770 state.mode <- Textentry (
4771 (":", "", None, linknentry, linkndone (fun under ->
4772 addnav ();
4773 gotounder under
4775 ), fun _ ->
4776 state.glinks <- false;
4777 state.mode <- mode
4779 state.text <- "";
4780 G.postRedisplay "view:linkent(F)"
4782 | 121 -> (* y *)
4783 state.glinks <- true;
4784 let mode = state.mode in
4785 state.mode <- Textentry (
4786 (":", "", None, linknentry, linkndone (fun under ->
4787 match Ne.pipe () with
4788 | Ne.Exn exn ->
4789 showtext '!' (Printf.sprintf "pipe failed: %s"
4790 (Printexc.to_string exn));
4791 | Ne.Res (r, w) ->
4792 let popened =
4793 try popen conf.selcmd [r, 0; w, -1]; true
4794 with exn ->
4795 showtext '!'
4796 (Printf.sprintf "failed to execute %s: %s"
4797 conf.selcmd (Printexc.to_string exn));
4798 false
4800 let clo cap fd =
4801 Ne.clo fd (fun msg ->
4802 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4805 let s = undertext under in
4806 if popened
4807 then
4808 (try
4809 let l = String.length s in
4810 let n = Unix.write w s 0 l in
4811 if n != l
4812 then
4813 showtext '!'
4814 (Printf.sprintf
4815 "failed to write %d characters to sel pipe, wrote %d"
4818 with exn ->
4819 showtext '!'
4820 (Printf.sprintf "failed to write to sel pipe: %s"
4821 (Printexc.to_string exn)
4824 else dolog "%s" s;
4825 clo "pipe/r" r;
4826 clo "pipe/w" w;
4829 fun _ ->
4830 state.glinks <- false;
4831 state.mode <- mode
4833 state.text <- "";
4834 G.postRedisplay "view:linkent"
4836 | 97 -> (* a *)
4837 begin match state.autoscroll with
4838 | Some step ->
4839 conf.autoscrollstep <- step;
4840 state.autoscroll <- None
4841 | None ->
4842 if conf.autoscrollstep = 0
4843 then state.autoscroll <- Some 1
4844 else state.autoscroll <- Some conf.autoscrollstep
4847 | 112 when ctrl -> (* ctrl-p *)
4848 launchpath ()
4850 | 80 -> (* P *)
4851 conf.presentation <- not conf.presentation;
4852 if conf.presentation
4853 then (
4854 if not conf.scrollbarinpm
4855 then state.scrollw <- 0;
4857 else
4858 state.scrollw <- conf.scrollbw;
4860 showtext ' ' ("presentation mode " ^
4861 if conf.presentation then "on" else "off");
4862 state.anchor <- getanchor ();
4863 represent ()
4865 | 102 -> (* f *)
4866 begin match state.fullscreen with
4867 | None ->
4868 state.fullscreen <- Some (conf.winw, conf.winh);
4869 Wsi.fullscreen ()
4870 | Some (w, h) ->
4871 state.fullscreen <- None;
4872 doreshape w h
4875 | 103 -> (* g *)
4876 gotoy_and_clear_text 0
4878 | 71 -> (* G *)
4879 gotopage1 (state.pagecount - 1) 0
4881 | 112 | 78 -> (* p|N *)
4882 search state.searchpattern false
4884 | 110 | 0xffc0 -> (* n|F3 *)
4885 search state.searchpattern true
4887 | 116 -> (* t *)
4888 begin match state.layout with
4889 | [] -> ()
4890 | l :: _ ->
4891 gotoy_and_clear_text (getpagey l.pageno)
4894 | 32 -> (* ' ' *)
4895 begin match List.rev state.layout with
4896 | [] -> ()
4897 | l :: _ ->
4898 let pageno = min (l.pageno+1) (state.pagecount-1) in
4899 gotoy_and_clear_text (getpagey pageno)
4902 | 0xff9f | 0xffff -> (* delete *)
4903 begin match state.layout with
4904 | [] -> ()
4905 | l :: _ ->
4906 let pageno = max 0 (l.pageno-1) in
4907 gotoy_and_clear_text (getpagey pageno)
4910 | 61 -> (* = *)
4911 showtext ' ' (describe_location ());
4913 | 119 -> (* w *)
4914 begin match state.layout with
4915 | [] -> ()
4916 | l :: _ ->
4917 doreshape (l.pagew + state.scrollw) l.pageh;
4918 G.postRedisplay "w"
4921 | 39 -> (* ' *)
4922 enterbookmarkmode ()
4924 | 104 | 0xffbe -> (* h|F1 *)
4925 enterhelpmode ()
4927 | 105 -> (* i *)
4928 enterinfomode ()
4930 | 101 when conf.redirectstderr -> (* e *)
4931 entermsgsmode ()
4933 | 109 -> (* m *)
4934 let ondone s =
4935 match state.layout with
4936 | l :: _ ->
4937 state.bookmarks <-
4938 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4939 :: state.bookmarks
4940 | _ -> ()
4942 enttext ("bookmark: ", "", None, textentry, ondone)
4944 | 126 -> (* ~ *)
4945 quickbookmark ();
4946 showtext ' ' "Quick bookmark added";
4948 | 122 -> (* z *)
4949 begin match state.layout with
4950 | l :: _ ->
4951 let rect = getpdimrect l.pagedimno in
4952 let w, h =
4953 if conf.crophack
4954 then
4955 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4956 truncate (1.2 *. (rect.(3) -. rect.(0))))
4957 else
4958 (truncate (rect.(1) -. rect.(0)),
4959 truncate (rect.(3) -. rect.(0)))
4961 let w = truncate ((float w)*.conf.zoom)
4962 and h = truncate ((float h)*.conf.zoom) in
4963 if w != 0 && h != 0
4964 then (
4965 state.anchor <- getanchor ();
4966 doreshape (w + state.scrollw) (h + conf.interpagespace)
4968 G.postRedisplay "z";
4970 | [] -> ()
4973 | 50 when ctrl -> (* ctrl-2 *)
4974 let maxw = getmaxw () in
4975 if maxw > 0.0
4976 then setzoom (maxw /. float conf.winw)
4978 | 60 | 62 -> (* < > *)
4979 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4981 | 91 | 93 -> (* [ ] *)
4982 conf.colorscale <-
4983 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4985 G.postRedisplay "brightness";
4987 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4988 setzoom state.prevzoom
4990 | 107 | 0xff52 -> (* k up *)
4991 begin match state.autoscroll with
4992 | None ->
4993 begin match state.mode with
4994 | Birdseye beye -> upbirdseye 1 beye
4995 | _ ->
4996 if ctrl
4997 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4998 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5000 | Some n ->
5001 setautoscrollspeed n false
5004 | 106 | 0xff54 -> (* j down *)
5005 begin match state.autoscroll with
5006 | None ->
5007 begin match state.mode with
5008 | Birdseye beye -> downbirdseye 1 beye
5009 | _ ->
5010 if ctrl
5011 then gotoy_and_clear_text (clamp (conf.winh/2))
5012 else gotoy_and_clear_text (clamp conf.scrollstep)
5014 | Some n ->
5015 setautoscrollspeed n true
5018 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5019 if canpan ()
5020 then
5021 let dx =
5022 if ctrl
5023 then conf.winw / 2
5024 else 10
5026 let dx = if key = 0xff51 then dx else -dx in
5027 state.x <- state.x + dx;
5028 gotoy_and_clear_text state.y
5029 else (
5030 state.text <- "";
5031 G.postRedisplay "lef/right"
5034 | 0xff55 -> (* prior *)
5035 let y =
5036 if ctrl
5037 then
5038 match state.layout with
5039 | [] -> state.y
5040 | l :: _ -> state.y - l.pagey
5041 else
5042 clamp (-conf.winh)
5044 gotoghyll y
5046 | 0xff56 -> (* next *)
5047 let y =
5048 if ctrl
5049 then
5050 match List.rev state.layout with
5051 | [] -> state.y
5052 | l :: _ -> getpagey l.pageno
5053 else
5054 clamp conf.winh
5056 gotoghyll y
5058 | 0xff50 -> gotoghyll 0
5059 | 0xff57 -> gotoghyll (clamp state.maxy)
5060 | 0xff53 when Wsi.withalt mask ->
5061 gotoghyll (getnav ~-1)
5062 | 0xff51 when Wsi.withalt mask ->
5063 gotoghyll (getnav 1)
5065 | 114 -> (* r *)
5066 state.anchor <- getanchor ();
5067 opendoc state.path state.password
5069 | 118 when conf.debug -> (* v *)
5070 state.rects <- [];
5071 List.iter (fun l ->
5072 match getopaque l.pageno with
5073 | None -> ()
5074 | Some opaque ->
5075 let x0, y0, x1, y1 = pagebbox opaque in
5076 let a,b = float x0, float y0 in
5077 let c,d = float x1, float y0 in
5078 let e,f = float x1, float y1 in
5079 let h,j = float x0, float y1 in
5080 let rect = (a,b,c,d,e,f,h,j) in
5081 debugrect rect;
5082 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5083 ) state.layout;
5084 G.postRedisplay "v";
5086 | _ ->
5087 vlog "huh? %s" (Wsi.keyname key)
5090 let linknavkeyboard key mask linknav =
5091 let getpage pageno =
5092 let rec loop = function
5093 | [] -> None
5094 | l :: _ when l.pageno = pageno -> Some l
5095 | _ :: rest -> loop rest
5096 in loop state.layout
5098 let doexact (pageno, n) =
5099 match getopaque pageno, getpage pageno with
5100 | Some opaque, Some l ->
5101 if key = 0xff0d
5102 then
5103 let under = getlink opaque n in
5104 G.postRedisplay "link gotounder";
5105 gotounder under;
5106 state.mode <- View;
5107 else
5108 let opt, dir =
5109 match key with
5110 | 0xff50 -> (* home *)
5111 Some (findlink opaque LDfirst), -1
5113 | 0xff57 -> (* end *)
5114 Some (findlink opaque LDlast), 1
5116 | 0xff51 -> (* left *)
5117 Some (findlink opaque (LDleft n)), -1
5119 | 0xff53 -> (* right *)
5120 Some (findlink opaque (LDright n)), 1
5122 | 0xff52 -> (* up *)
5123 Some (findlink opaque (LDup n)), -1
5125 | 0xff54 -> (* down *)
5126 Some (findlink opaque (LDdown n)), 1
5128 | _ -> None, 0
5130 let pwl l dir =
5131 begin match findpwl l.pageno dir with
5132 | Pwlnotfound -> ()
5133 | Pwl pageno ->
5134 let notfound dir =
5135 state.mode <- LinkNav (Ltgendir dir);
5136 let y, h = getpageyh pageno in
5137 let y =
5138 if dir < 0
5139 then y + h - conf.winh
5140 else y
5142 gotoy y
5144 begin match getopaque pageno, getpage pageno with
5145 | Some opaque, Some _ ->
5146 let link =
5147 let ld = if dir > 0 then LDfirst else LDlast in
5148 findlink opaque ld
5150 begin match link with
5151 | Lfound m ->
5152 showlinktype (getlink opaque m);
5153 state.mode <- LinkNav (Ltexact (pageno, m));
5154 G.postRedisplay "linknav jpage";
5155 | _ -> notfound dir
5156 end;
5157 | _ -> notfound dir
5158 end;
5159 end;
5161 begin match opt with
5162 | Some Lnotfound -> pwl l dir;
5163 | Some (Lfound m) ->
5164 if m = n
5165 then pwl l dir
5166 else (
5167 let _, y0, _, y1 = getlinkrect opaque m in
5168 if y0 < l.pagey
5169 then gotopage1 l.pageno y0
5170 else (
5171 let d = fstate.fontsize + 1 in
5172 if y1 - l.pagey > l.pagevh - d
5173 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5174 else G.postRedisplay "linknav";
5176 showlinktype (getlink opaque m);
5177 state.mode <- LinkNav (Ltexact (l.pageno, m));
5180 | None -> viewkeyboard key mask
5181 end;
5182 | _ -> viewkeyboard key mask
5184 if key = 0xff63
5185 then (
5186 state.mode <- View;
5187 G.postRedisplay "leave linknav"
5189 else
5190 match linknav with
5191 | Ltgendir _ -> viewkeyboard key mask
5192 | Ltexact exact -> doexact exact
5195 let keyboard key mask =
5196 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5197 then wcmd "interrupt"
5198 else state.uioh <- state.uioh#key key mask
5201 let birdseyekeyboard key mask
5202 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5203 let incr =
5204 match conf.columns with
5205 | Csingle -> 1
5206 | Cmulti ((c, _, _), _) -> c
5207 | Csplit _ -> failwith "bird's eye split mode"
5209 match key with
5210 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5211 let y, h = getpageyh pageno in
5212 let top = (conf.winh - h) / 2 in
5213 gotoy (max 0 (y - top))
5214 | 0xff0d -> leavebirdseye beye false
5215 | 0xff1b -> leavebirdseye beye true (* escape *)
5216 | 0xff52 -> upbirdseye incr beye (* prior *)
5217 | 0xff54 -> downbirdseye incr beye (* next *)
5218 | 0xff51 -> upbirdseye 1 beye (* up *)
5219 | 0xff53 -> downbirdseye 1 beye (* down *)
5221 | 0xff55 ->
5222 begin match state.layout with
5223 | l :: _ ->
5224 if l.pagey != 0
5225 then (
5226 state.mode <- Birdseye (
5227 oconf, leftx, l.pageno, hooverpageno, anchor
5229 gotopage1 l.pageno 0;
5231 else (
5232 let layout = layout (state.y-conf.winh) conf.winh in
5233 match layout with
5234 | [] -> gotoy (clamp (-conf.winh))
5235 | l :: _ ->
5236 state.mode <- Birdseye (
5237 oconf, leftx, l.pageno, hooverpageno, anchor
5239 gotopage1 l.pageno 0
5242 | [] -> gotoy (clamp (-conf.winh))
5243 end;
5245 | 0xff56 ->
5246 begin match List.rev state.layout with
5247 | l :: _ ->
5248 let layout = layout (state.y + conf.winh) conf.winh in
5249 begin match layout with
5250 | [] ->
5251 let incr = l.pageh - l.pagevh in
5252 if incr = 0
5253 then (
5254 state.mode <-
5255 Birdseye (
5256 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5258 G.postRedisplay "birdseye pagedown";
5260 else gotoy (clamp (incr + conf.interpagespace*2));
5262 | l :: _ ->
5263 state.mode <-
5264 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5265 gotopage1 l.pageno 0;
5268 | [] -> gotoy (clamp conf.winh)
5269 end;
5271 | 0xff50 ->
5272 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5273 gotopage1 0 0
5275 | 0xff57 ->
5276 let pageno = state.pagecount - 1 in
5277 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5278 if not (pagevisible state.layout pageno)
5279 then
5280 let h =
5281 match List.rev state.pdims with
5282 | [] -> conf.winh
5283 | (_, _, h, _) :: _ -> h
5285 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5286 else G.postRedisplay "birdseye end";
5287 | _ -> viewkeyboard key mask
5290 let drawpage l linkindexbase =
5291 let color =
5292 match state.mode with
5293 | Textentry _ -> scalecolor 0.4
5294 | LinkNav _
5295 | View -> scalecolor 1.0
5296 | Birdseye (_, _, pageno, hooverpageno, _) ->
5297 if l.pageno = hooverpageno
5298 then scalecolor 0.9
5299 else (
5300 if l.pageno = pageno
5301 then scalecolor 1.0
5302 else scalecolor 0.8
5305 drawtiles l color;
5306 begin match getopaque l.pageno with
5307 | Some opaque ->
5308 if tileready l l.pagex l.pagey
5309 then
5310 let x = l.pagedispx - l.pagex
5311 and y = l.pagedispy - l.pagey in
5312 let hlmask = (if conf.hlinks then 1 else 0)
5313 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5315 let s =
5316 match state.mode with
5317 | Textentry ((_, s, _, _, _), _) when state.glinks -> s
5318 | _ -> ""
5320 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5321 else 0
5323 | _ -> 0
5324 end;
5327 let scrollindicator () =
5328 let sbw, ph, sh = state.uioh#scrollph in
5329 let sbh, pw, sw = state.uioh#scrollpw in
5331 GlDraw.color (0.64, 0.64, 0.64);
5332 GlDraw.rect
5333 (float (conf.winw - sbw), 0.)
5334 (float conf.winw, float conf.winh)
5336 GlDraw.rect
5337 (0., float (conf.winh - sbh))
5338 (float (conf.winw - state.scrollw - 1), float conf.winh)
5340 GlDraw.color (0.0, 0.0, 0.0);
5342 GlDraw.rect
5343 (float (conf.winw - sbw), ph)
5344 (float conf.winw, ph +. sh)
5346 GlDraw.rect
5347 (pw, float (conf.winh - sbh))
5348 (pw +. sw, float conf.winh)
5352 let showsel () =
5353 match state.mstate with
5354 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5357 | Msel ((x0, y0), (x1, y1)) ->
5358 let rec loop = function
5359 | l :: ls ->
5360 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5361 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5362 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5363 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5364 then
5365 match getopaque l.pageno with
5366 | Some opaque ->
5367 let x0, y0 = pagetranslatepoint l x0 y0 in
5368 let x1, y1 = pagetranslatepoint l x1 y1 in
5369 seltext opaque (x0, y0, x1, y1);
5370 | _ -> ()
5371 else loop ls
5372 | [] -> ()
5374 loop state.layout
5377 let showrects rects =
5378 Gl.enable `blend;
5379 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5380 GlDraw.polygon_mode `both `fill;
5381 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5382 List.iter
5383 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5384 List.iter (fun l ->
5385 if l.pageno = pageno
5386 then (
5387 let dx = float (l.pagedispx - l.pagex) in
5388 let dy = float (l.pagedispy - l.pagey) in
5389 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5390 GlDraw.begins `quads;
5392 GlDraw.vertex2 (x0+.dx, y0+.dy);
5393 GlDraw.vertex2 (x1+.dx, y1+.dy);
5394 GlDraw.vertex2 (x2+.dx, y2+.dy);
5395 GlDraw.vertex2 (x3+.dx, y3+.dy);
5397 GlDraw.ends ();
5399 ) state.layout
5400 ) rects
5402 Gl.disable `blend;
5405 let display () =
5406 GlClear.color (scalecolor2 conf.bgcolor);
5407 GlClear.clear [`color];
5408 let rec loop linkindexbase = function
5409 | l :: rest ->
5410 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5411 loop linkindexbase rest
5412 | [] -> ()
5414 loop 0 state.layout;
5415 let rects =
5416 match state.mode with
5417 | LinkNav (Ltexact (pageno, linkno)) ->
5418 begin match getopaque pageno with
5419 | Some opaque ->
5420 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5421 (pageno, 5, (
5422 float x0, float y0,
5423 float x1, float y0,
5424 float x1, float y1,
5425 float x0, float y1)
5426 ) :: state.rects
5427 | None -> state.rects
5429 | _ -> state.rects
5431 showrects rects;
5432 showsel ();
5433 state.uioh#display;
5434 begin match state.mstate with
5435 | Mzoomrect ((x0, y0), (x1, y1)) ->
5436 Gl.enable `blend;
5437 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5438 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5439 GlDraw.rect (float x0, float y0)
5440 (float x1, float y1);
5441 Gl.disable `blend;
5442 | _ -> ()
5443 end;
5444 enttext ();
5445 scrollindicator ();
5446 Wsi.swapb ();
5449 let zoomrect x y x1 y1 =
5450 let x0 = min x x1
5451 and x1 = max x x1
5452 and y0 = min y y1 in
5453 gotoy (state.y + y0);
5454 state.anchor <- getanchor ();
5455 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5456 let margin =
5457 if state.w < conf.winw - state.scrollw
5458 then (conf.winw - state.scrollw - state.w) / 2
5459 else 0
5461 state.x <- (state.x + margin) - x0;
5462 setzoom zoom;
5463 Wsi.setcursor Wsi.CURSOR_INHERIT;
5464 state.mstate <- Mnone;
5467 let scrollx x =
5468 let winw = conf.winw - state.scrollw - 1 in
5469 let s = float x /. float winw in
5470 let destx = truncate (float (state.w + winw) *. s) in
5471 state.x <- winw - destx;
5472 gotoy_and_clear_text state.y;
5473 state.mstate <- Mscrollx;
5476 let scrolly y =
5477 let s = float y /. float conf.winh in
5478 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5479 gotoy_and_clear_text desty;
5480 state.mstate <- Mscrolly;
5483 let viewmouse button down x y mask =
5484 match button with
5485 | n when (n == 4 || n == 5) && not down ->
5486 if Wsi.withctrl mask
5487 then (
5488 match state.mstate with
5489 | Mzoom (oldn, i) ->
5490 if oldn = n
5491 then (
5492 if i = 2
5493 then
5494 let incr =
5495 match n with
5496 | 5 ->
5497 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5498 | _ ->
5499 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5501 let zoom = conf.zoom -. incr in
5502 setzoom zoom;
5503 state.mstate <- Mzoom (n, 0);
5504 else
5505 state.mstate <- Mzoom (n, i+1);
5507 else state.mstate <- Mzoom (n, 0)
5509 | _ -> state.mstate <- Mzoom (n, 0)
5511 else (
5512 match state.autoscroll with
5513 | Some step -> setautoscrollspeed step (n=4)
5514 | None ->
5515 let incr =
5516 if n = 4
5517 then -conf.scrollstep
5518 else conf.scrollstep
5520 let incr = incr * 2 in
5521 let y = clamp incr in
5522 gotoy_and_clear_text y
5525 | 1 when Wsi.withctrl mask ->
5526 if down
5527 then (
5528 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5529 state.mstate <- Mpan (x, y)
5531 else
5532 state.mstate <- Mnone
5534 | 3 ->
5535 if down
5536 then (
5537 Wsi.setcursor Wsi.CURSOR_CYCLE;
5538 let p = (x, y) in
5539 state.mstate <- Mzoomrect (p, p)
5541 else (
5542 match state.mstate with
5543 | Mzoomrect ((x0, y0), _) ->
5544 if abs (x-x0) > 10 && abs (y - y0) > 10
5545 then zoomrect x0 y0 x y
5546 else (
5547 state.mstate <- Mnone;
5548 Wsi.setcursor Wsi.CURSOR_INHERIT;
5549 G.postRedisplay "kill accidental zoom rect";
5551 | _ ->
5552 Wsi.setcursor Wsi.CURSOR_INHERIT;
5553 state.mstate <- Mnone
5556 | 1 when x > conf.winw - state.scrollw ->
5557 if down
5558 then
5559 let _, position, sh = state.uioh#scrollph in
5560 if y > truncate position && y < truncate (position +. sh)
5561 then state.mstate <- Mscrolly
5562 else scrolly y
5563 else
5564 state.mstate <- Mnone
5566 | 1 when y > conf.winh - state.hscrollh ->
5567 if down
5568 then
5569 let _, position, sw = state.uioh#scrollpw in
5570 if x > truncate position && x < truncate (position +. sw)
5571 then state.mstate <- Mscrollx
5572 else scrollx x
5573 else
5574 state.mstate <- Mnone
5576 | 1 ->
5577 let dest = if down then getunder x y else Unone in
5578 begin match dest with
5579 | Ulinkgoto _
5580 | Ulinkuri _
5581 | Uremote _
5582 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5583 gotounder dest
5585 | Unone when down ->
5586 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5587 state.mstate <- Mpan (x, y);
5589 | Unone | Utext _ ->
5590 if down
5591 then (
5592 if conf.angle mod 360 = 0
5593 then (
5594 state.mstate <- Msel ((x, y), (x, y));
5595 G.postRedisplay "mouse select";
5598 else (
5599 match state.mstate with
5600 | Mnone -> ()
5602 | Mzoom _ | Mscrollx | Mscrolly ->
5603 state.mstate <- Mnone
5605 | Mzoomrect ((x0, y0), _) ->
5606 zoomrect x0 y0 x y
5608 | Mpan _ ->
5609 Wsi.setcursor Wsi.CURSOR_INHERIT;
5610 state.mstate <- Mnone
5612 | Msel ((_, y0), (_, y1)) ->
5613 let rec loop = function
5614 | [] -> ()
5615 | l :: rest ->
5616 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5617 || ((y1 >= l.pagedispy
5618 && y1 <= (l.pagedispy + l.pagevh)))
5619 then
5620 match getopaque l.pageno with
5621 | Some opaque ->
5622 begin
5623 match Ne.pipe () with
5624 | Ne.Exn exn ->
5625 showtext '!'
5626 (Printf.sprintf
5627 "can not create sel pipe: %s"
5628 (Printexc.to_string exn));
5629 | Ne.Res (r, w) ->
5630 let doclose what fd =
5631 Ne.clo fd (fun msg ->
5632 dolog "%s close failed: %s" what msg)
5635 popen conf.selcmd [r, 0; w, -1];
5636 copysel w opaque;
5637 doclose "pipe/r" r;
5638 G.postRedisplay "copysel";
5639 with exn ->
5640 dolog "can not exectute %S: %s"
5641 conf.selcmd (Printexc.to_string exn);
5642 doclose "pipe/r" r;
5643 doclose "pipe/w" w;
5645 | None -> ()
5646 else loop rest
5648 loop state.layout;
5649 Wsi.setcursor Wsi.CURSOR_INHERIT;
5650 state.mstate <- Mnone;
5654 | _ -> ()
5657 let birdseyemouse button down x y mask
5658 (conf, leftx, _, hooverpageno, anchor) =
5659 match button with
5660 | 1 when down ->
5661 let rec loop = function
5662 | [] -> ()
5663 | l :: rest ->
5664 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5665 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5666 then (
5667 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5669 else loop rest
5671 loop state.layout
5672 | 3 -> ()
5673 | _ -> viewmouse button down x y mask
5676 let mouse button down x y mask =
5677 state.uioh <- state.uioh#button button down x y mask;
5680 let motion ~x ~y =
5681 state.uioh <- state.uioh#motion x y
5684 let pmotion ~x ~y =
5685 state.uioh <- state.uioh#pmotion x y;
5688 let uioh = object
5689 method display = ()
5691 method key key mask =
5692 begin match state.mode with
5693 | Textentry textentry -> textentrykeyboard key mask textentry
5694 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5695 | View -> viewkeyboard key mask
5696 | LinkNav linknav -> linknavkeyboard key mask linknav
5697 end;
5698 state.uioh
5700 method button button bstate x y mask =
5701 begin match state.mode with
5702 | LinkNav _
5703 | View -> viewmouse button bstate x y mask
5704 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5705 | Textentry _ -> ()
5706 end;
5707 state.uioh
5709 method motion x y =
5710 begin match state.mode with
5711 | Textentry _ -> ()
5712 | View | Birdseye _ | LinkNav _ ->
5713 match state.mstate with
5714 | Mzoom _ | Mnone -> ()
5716 | Mpan (x0, y0) ->
5717 let dx = x - x0
5718 and dy = y0 - y in
5719 state.mstate <- Mpan (x, y);
5720 if canpan ()
5721 then state.x <- state.x + dx;
5722 let y = clamp dy in
5723 gotoy_and_clear_text y
5725 | Msel (a, _) ->
5726 state.mstate <- Msel (a, (x, y));
5727 G.postRedisplay "motion select";
5729 | Mscrolly ->
5730 let y = min conf.winh (max 0 y) in
5731 scrolly y
5733 | Mscrollx ->
5734 let x = min conf.winw (max 0 x) in
5735 scrollx x
5737 | Mzoomrect (p0, _) ->
5738 state.mstate <- Mzoomrect (p0, (x, y));
5739 G.postRedisplay "motion zoomrect";
5740 end;
5741 state.uioh
5743 method pmotion x y =
5744 begin match state.mode with
5745 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5746 let rec loop = function
5747 | [] ->
5748 if hooverpageno != -1
5749 then (
5750 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5751 G.postRedisplay "pmotion birdseye no hoover";
5753 | l :: rest ->
5754 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5755 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5756 then (
5757 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5758 G.postRedisplay "pmotion birdseye hoover";
5760 else loop rest
5762 loop state.layout
5764 | Textentry _ -> ()
5766 | LinkNav _
5767 | View ->
5768 match state.mstate with
5769 | Mnone -> updateunder x y
5770 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5772 end;
5773 state.uioh
5775 method infochanged _ = ()
5777 method scrollph =
5778 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5779 let p, h = scrollph state.y maxy in
5780 state.scrollw, p, h
5782 method scrollpw =
5783 let winw = conf.winw - state.scrollw - 1 in
5784 let fwinw = float winw in
5785 let sw =
5786 let sw = fwinw /. float state.w in
5787 let sw = fwinw *. sw in
5788 max sw (float conf.scrollh)
5790 let position, sw =
5791 let f = state.w+winw in
5792 let r = float (winw-state.x) /. float f in
5793 let p = fwinw *. r in
5794 p-.sw/.2., sw
5796 let sw =
5797 if position +. sw > fwinw
5798 then fwinw -. position
5799 else sw
5801 state.hscrollh, position, sw
5803 method modehash =
5804 let modename =
5805 match state.mode with
5806 | LinkNav _ -> "links"
5807 | Textentry _ -> "textentry"
5808 | Birdseye _ -> "birdseye"
5809 | View -> "global"
5811 findkeyhash conf modename
5812 end;;
5814 module Config =
5815 struct
5816 open Parser
5818 let fontpath = ref "";;
5820 module KeyMap =
5821 Map.Make (struct type t = (int * int) let compare = compare end);;
5823 let unent s =
5824 let l = String.length s in
5825 let b = Buffer.create l in
5826 unent b s 0 l;
5827 Buffer.contents b;
5830 let home =
5831 try Sys.getenv "HOME"
5832 with exn ->
5833 prerr_endline
5834 ("Can not determine home directory location: " ^
5835 Printexc.to_string exn);
5839 let modifier_of_string = function
5840 | "alt" -> Wsi.altmask
5841 | "shift" -> Wsi.shiftmask
5842 | "ctrl" | "control" -> Wsi.ctrlmask
5843 | "meta" -> Wsi.metamask
5844 | _ -> 0
5847 let key_of_string =
5848 let r = Str.regexp "-" in
5849 fun s ->
5850 let elems = Str.full_split r s in
5851 let f n k m =
5852 let g s =
5853 let m1 = modifier_of_string s in
5854 if m1 = 0
5855 then (Wsi.namekey s, m)
5856 else (k, m lor m1)
5857 in function
5858 | Str.Delim s when n land 1 = 0 -> g s
5859 | Str.Text s -> g s
5860 | Str.Delim _ -> (k, m)
5862 let rec loop n k m = function
5863 | [] -> (k, m)
5864 | x :: xs ->
5865 let k, m = f n k m x in
5866 loop (n+1) k m xs
5868 loop 0 0 0 elems
5871 let keys_of_string =
5872 let r = Str.regexp "[ \t]" in
5873 fun s ->
5874 let elems = Str.split r s in
5875 List.map key_of_string elems
5878 let copykeyhashes c =
5879 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5882 let config_of c attrs =
5883 let apply c k v =
5885 match k with
5886 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5887 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5888 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5889 | "preload" -> { c with preload = bool_of_string v }
5890 | "page-bias" -> { c with pagebias = int_of_string v }
5891 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5892 | "auto-scroll-step" ->
5893 { c with autoscrollstep = max 0 (int_of_string v) }
5894 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5895 | "crop-hack" -> { c with crophack = bool_of_string v }
5896 | "throttle" ->
5897 let mw =
5898 match String.lowercase v with
5899 | "true" -> Some infinity
5900 | "false" -> None
5901 | f -> Some (float_of_string f)
5903 { c with maxwait = mw}
5904 | "highlight-links" -> { c with hlinks = bool_of_string v }
5905 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5906 | "vertical-margin" ->
5907 { c with interpagespace = max 0 (int_of_string v) }
5908 | "zoom" ->
5909 let zoom = float_of_string v /. 100. in
5910 let zoom = max zoom 0.0 in
5911 { c with zoom = zoom }
5912 | "presentation" -> { c with presentation = bool_of_string v }
5913 | "rotation-angle" -> { c with angle = int_of_string v }
5914 | "width" -> { c with winw = max 20 (int_of_string v) }
5915 | "height" -> { c with winh = max 20 (int_of_string v) }
5916 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5917 | "proportional-display" -> { c with proportional = bool_of_string v }
5918 | "pixmap-cache-size" ->
5919 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5920 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5921 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5922 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5923 | "persistent-location" -> { c with jumpback = bool_of_string v }
5924 | "background-color" -> { c with bgcolor = color_of_string v }
5925 | "scrollbar-in-presentation" ->
5926 { c with scrollbarinpm = bool_of_string v }
5927 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5928 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5929 | "mupdf-store-size" ->
5930 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5931 | "checkers" -> { c with checkers = bool_of_string v }
5932 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5933 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5934 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5935 | "uri-launcher" -> { c with urilauncher = unent v }
5936 | "path-launcher" -> { c with pathlauncher = unent v }
5937 | "color-space" -> { c with colorspace = colorspace_of_string v }
5938 | "invert-colors" -> { c with invert = bool_of_string v }
5939 | "brightness" -> { c with colorscale = float_of_string v }
5940 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5941 | "ghyllscroll" ->
5942 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5943 | "columns" ->
5944 let (n, _, _) as nab = multicolumns_of_string v in
5945 if n < 0
5946 then { c with columns = Csplit (-n, [||]) }
5947 else { c with columns = Cmulti (nab, [||]) }
5948 | "birds-eye-columns" ->
5949 { c with beyecolumns = Some (max (int_of_string v) 2) }
5950 | "selection-command" -> { c with selcmd = unent v }
5951 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5952 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
5953 | _ -> c
5954 with exn ->
5955 prerr_endline ("Error processing attribute (`" ^
5956 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5959 let rec fold c = function
5960 | [] -> c
5961 | (k, v) :: rest ->
5962 let c = apply c k v in
5963 fold c rest
5965 fold { c with keyhashes = copykeyhashes c } attrs;
5968 let fromstring f pos n v d =
5969 try f v
5970 with exn ->
5971 dolog "Error processing attribute (%S=%S) at %d\n%s"
5972 n v pos (Printexc.to_string exn)
5977 let bookmark_of attrs =
5978 let rec fold title page rely = function
5979 | ("title", v) :: rest -> fold v page rely rest
5980 | ("page", v) :: rest -> fold title v rely rest
5981 | ("rely", v) :: rest -> fold title page v rest
5982 | _ :: rest -> fold title page rely rest
5983 | [] -> title, page, rely
5985 fold "invalid" "0" "0" attrs
5988 let doc_of attrs =
5989 let rec fold path page rely pan = function
5990 | ("path", v) :: rest -> fold v page rely pan rest
5991 | ("page", v) :: rest -> fold path v rely pan rest
5992 | ("rely", v) :: rest -> fold path page v pan rest
5993 | ("pan", v) :: rest -> fold path page rely v rest
5994 | _ :: rest -> fold path page rely pan rest
5995 | [] -> path, page, rely, pan
5997 fold "" "0" "0" "0" attrs
6000 let map_of attrs =
6001 let rec fold rs ls = function
6002 | ("out", v) :: rest -> fold v ls rest
6003 | ("in", v) :: rest -> fold rs v rest
6004 | _ :: rest -> fold ls rs rest
6005 | [] -> ls, rs
6007 fold "" "" attrs
6010 let setconf dst src =
6011 dst.scrollbw <- src.scrollbw;
6012 dst.scrollh <- src.scrollh;
6013 dst.icase <- src.icase;
6014 dst.preload <- src.preload;
6015 dst.pagebias <- src.pagebias;
6016 dst.verbose <- src.verbose;
6017 dst.scrollstep <- src.scrollstep;
6018 dst.maxhfit <- src.maxhfit;
6019 dst.crophack <- src.crophack;
6020 dst.autoscrollstep <- src.autoscrollstep;
6021 dst.maxwait <- src.maxwait;
6022 dst.hlinks <- src.hlinks;
6023 dst.underinfo <- src.underinfo;
6024 dst.interpagespace <- src.interpagespace;
6025 dst.zoom <- src.zoom;
6026 dst.presentation <- src.presentation;
6027 dst.angle <- src.angle;
6028 dst.winw <- src.winw;
6029 dst.winh <- src.winh;
6030 dst.savebmarks <- src.savebmarks;
6031 dst.memlimit <- src.memlimit;
6032 dst.proportional <- src.proportional;
6033 dst.texcount <- src.texcount;
6034 dst.sliceheight <- src.sliceheight;
6035 dst.thumbw <- src.thumbw;
6036 dst.jumpback <- src.jumpback;
6037 dst.bgcolor <- src.bgcolor;
6038 dst.scrollbarinpm <- src.scrollbarinpm;
6039 dst.tilew <- src.tilew;
6040 dst.tileh <- src.tileh;
6041 dst.mustoresize <- src.mustoresize;
6042 dst.checkers <- src.checkers;
6043 dst.aalevel <- src.aalevel;
6044 dst.trimmargins <- src.trimmargins;
6045 dst.trimfuzz <- src.trimfuzz;
6046 dst.urilauncher <- src.urilauncher;
6047 dst.colorspace <- src.colorspace;
6048 dst.invert <- src.invert;
6049 dst.colorscale <- src.colorscale;
6050 dst.redirectstderr <- src.redirectstderr;
6051 dst.ghyllscroll <- src.ghyllscroll;
6052 dst.columns <- src.columns;
6053 dst.beyecolumns <- src.beyecolumns;
6054 dst.selcmd <- src.selcmd;
6055 dst.updatecurs <- src.updatecurs;
6056 dst.pathlauncher <- src.pathlauncher;
6057 dst.keyhashes <- copykeyhashes src;
6058 dst.hfsize <- src.hfsize;
6061 let get s =
6062 let h = Hashtbl.create 10 in
6063 let dc = { defconf with angle = defconf.angle } in
6064 let rec toplevel v t spos _ =
6065 match t with
6066 | Vdata | Vcdata | Vend -> v
6067 | Vopen ("llppconfig", _, closed) ->
6068 if closed
6069 then v
6070 else { v with f = llppconfig }
6071 | Vopen _ ->
6072 error "unexpected subelement at top level" s spos
6073 | Vclose _ -> error "unexpected close at top level" s spos
6075 and llppconfig v t spos _ =
6076 match t with
6077 | Vdata | Vcdata -> v
6078 | Vend -> error "unexpected end of input in llppconfig" s spos
6079 | Vopen ("defaults", attrs, closed) ->
6080 let c = config_of dc attrs in
6081 setconf dc c;
6082 if closed
6083 then v
6084 else { v with f = defaults }
6086 | Vopen ("ui-font", attrs, closed) ->
6087 let rec getsize size = function
6088 | [] -> size
6089 | ("size", v) :: rest ->
6090 let size =
6091 fromstring int_of_string spos "size" v fstate.fontsize in
6092 getsize size rest
6093 | l -> getsize size l
6095 fstate.fontsize <- getsize fstate.fontsize attrs;
6096 if closed
6097 then v
6098 else { v with f = uifont (Buffer.create 10) }
6100 | Vopen ("doc", attrs, closed) ->
6101 let pathent, spage, srely, span = doc_of attrs in
6102 let path = unent pathent
6103 and pageno = fromstring int_of_string spos "page" spage 0
6104 and rely = fromstring float_of_string spos "rely" srely 0.0
6105 and pan = fromstring int_of_string spos "pan" span 0 in
6106 let c = config_of dc attrs in
6107 let anchor = (pageno, rely) in
6108 if closed
6109 then (Hashtbl.add h path (c, [], pan, anchor); v)
6110 else { v with f = doc path pan anchor c [] }
6112 | Vopen _ ->
6113 error "unexpected subelement in llppconfig" s spos
6115 | Vclose "llppconfig" -> { v with f = toplevel }
6116 | Vclose _ -> error "unexpected close in llppconfig" s spos
6118 and defaults v t spos _ =
6119 match t with
6120 | Vdata | Vcdata -> v
6121 | Vend -> error "unexpected end of input in defaults" s spos
6122 | Vopen ("keymap", attrs, closed) ->
6123 let modename =
6124 try List.assoc "mode" attrs
6125 with Not_found -> "global" in
6126 if closed
6127 then v
6128 else
6129 let ret keymap =
6130 let h = findkeyhash dc modename in
6131 KeyMap.iter (Hashtbl.replace h) keymap;
6132 defaults
6134 { v with f = pkeymap ret KeyMap.empty }
6136 | Vopen (_, _, _) ->
6137 error "unexpected subelement in defaults" s spos
6139 | Vclose "defaults" ->
6140 { v with f = llppconfig }
6142 | Vclose _ -> error "unexpected close in defaults" s spos
6144 and uifont b v t spos epos =
6145 match t with
6146 | Vdata | Vcdata ->
6147 Buffer.add_substring b s spos (epos - spos);
6149 | Vopen (_, _, _) ->
6150 error "unexpected subelement in ui-font" s spos
6151 | Vclose "ui-font" ->
6152 if String.length !fontpath = 0
6153 then fontpath := Buffer.contents b;
6154 { v with f = llppconfig }
6155 | Vclose _ -> error "unexpected close in ui-font" s spos
6156 | Vend -> error "unexpected end of input in ui-font" s spos
6158 and doc path pan anchor c bookmarks v t spos _ =
6159 match t with
6160 | Vdata | Vcdata -> v
6161 | Vend -> error "unexpected end of input in doc" s spos
6162 | Vopen ("bookmarks", _, closed) ->
6163 if closed
6164 then v
6165 else { v with f = pbookmarks path pan anchor c bookmarks }
6167 | Vopen ("keymap", attrs, closed) ->
6168 let modename =
6169 try List.assoc "mode" attrs
6170 with Not_found -> "global"
6172 if closed
6173 then v
6174 else
6175 let ret keymap =
6176 let h = findkeyhash c modename in
6177 KeyMap.iter (Hashtbl.replace h) keymap;
6178 doc path pan anchor c bookmarks
6180 { v with f = pkeymap ret KeyMap.empty }
6182 | Vopen (_, _, _) ->
6183 error "unexpected subelement in doc" s spos
6185 | Vclose "doc" ->
6186 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6187 { v with f = llppconfig }
6189 | Vclose _ -> error "unexpected close in doc" s spos
6191 and pkeymap ret keymap v t spos _ =
6192 match t with
6193 | Vdata | Vcdata -> v
6194 | Vend -> error "unexpected end of input in keymap" s spos
6195 | Vopen ("map", attrs, closed) ->
6196 let r, l = map_of attrs in
6197 let kss = fromstring keys_of_string spos "in" r [] in
6198 let lss = fromstring keys_of_string spos "out" l [] in
6199 let keymap =
6200 match kss with
6201 | [] -> keymap
6202 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6203 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6205 if closed
6206 then { v with f = pkeymap ret keymap }
6207 else
6208 let f () = v in
6209 { v with f = skip "map" f }
6211 | Vopen _ ->
6212 error "unexpected subelement in keymap" s spos
6214 | Vclose "keymap" ->
6215 { v with f = ret keymap }
6217 | Vclose _ -> error "unexpected close in keymap" s spos
6219 and pbookmarks path pan anchor c bookmarks v t spos _ =
6220 match t with
6221 | Vdata | Vcdata -> v
6222 | Vend -> error "unexpected end of input in bookmarks" s spos
6223 | Vopen ("item", attrs, closed) ->
6224 let titleent, spage, srely = bookmark_of attrs in
6225 let page = fromstring int_of_string spos "page" spage 0
6226 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6227 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6228 if closed
6229 then { v with f = pbookmarks path pan anchor c bookmarks }
6230 else
6231 let f () = v in
6232 { v with f = skip "item" f }
6234 | Vopen _ ->
6235 error "unexpected subelement in bookmarks" s spos
6237 | Vclose "bookmarks" ->
6238 { v with f = doc path pan anchor c bookmarks }
6240 | Vclose _ -> error "unexpected close in bookmarks" s spos
6242 and skip tag f v t spos _ =
6243 match t with
6244 | Vdata | Vcdata -> v
6245 | Vend ->
6246 error ("unexpected end of input in skipped " ^ tag) s spos
6247 | Vopen (tag', _, closed) ->
6248 if closed
6249 then v
6250 else
6251 let f' () = { v with f = skip tag f } in
6252 { v with f = skip tag' f' }
6253 | Vclose ctag ->
6254 if tag = ctag
6255 then f ()
6256 else error ("unexpected close in skipped " ^ tag) s spos
6259 parse { f = toplevel; accu = () } s;
6260 h, dc;
6263 let do_load f ic =
6265 let len = in_channel_length ic in
6266 let s = String.create len in
6267 really_input ic s 0 len;
6268 f s;
6269 with
6270 | Parse_error (msg, s, pos) ->
6271 let subs = subs s pos in
6272 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6273 failwith ("parse error: " ^ s)
6275 | exn ->
6276 failwith ("config load error: " ^ Printexc.to_string exn)
6279 let defconfpath =
6280 let dir =
6282 let dir = Filename.concat home ".config" in
6283 if Sys.is_directory dir then dir else home
6284 with _ -> home
6286 Filename.concat dir "llpp.conf"
6289 let confpath = ref defconfpath;;
6291 let load1 f =
6292 if Sys.file_exists !confpath
6293 then
6294 match
6295 (try Some (open_in_bin !confpath)
6296 with exn ->
6297 prerr_endline
6298 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6299 Printexc.to_string exn);
6300 None
6302 with
6303 | Some ic ->
6304 begin try
6305 f (do_load get ic)
6306 with exn ->
6307 prerr_endline
6308 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6309 Printexc.to_string exn);
6310 end;
6311 close_in ic;
6313 | None -> ()
6314 else
6315 f (Hashtbl.create 0, defconf)
6318 let load () =
6319 let f (h, dc) =
6320 let pc, pb, px, pa =
6322 Hashtbl.find h (Filename.basename state.path)
6323 with Not_found -> dc, [], 0, (0, 0.0)
6325 setconf defconf dc;
6326 setconf conf pc;
6327 state.bookmarks <- pb;
6328 state.x <- px;
6329 state.scrollw <- conf.scrollbw;
6330 if conf.jumpback
6331 then state.anchor <- pa;
6332 cbput state.hists.nav pa;
6334 load1 f
6337 let add_attrs bb always dc c =
6338 let ob s a b =
6339 if always || a != b
6340 then Printf.bprintf bb "\n %s='%b'" s a
6341 and oi s a b =
6342 if always || a != b
6343 then Printf.bprintf bb "\n %s='%d'" s a
6344 and oI s a b =
6345 if always || a != b
6346 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6347 and oz s a b =
6348 if always || a <> b
6349 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6350 and oF s a b =
6351 if always || a <> b
6352 then Printf.bprintf bb "\n %s='%f'" s a
6353 and oc s a b =
6354 if always || a <> b
6355 then
6356 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6357 and oC s a b =
6358 if always || a <> b
6359 then
6360 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6361 and oR s a b =
6362 if always || a <> b
6363 then
6364 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6365 and os s a b =
6366 if always || a <> b
6367 then
6368 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6369 and og s a b =
6370 if always || a <> b
6371 then
6372 match a with
6373 | None -> ()
6374 | Some (_N, _A, _B) ->
6375 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6376 and oW s a b =
6377 if always || a <> b
6378 then
6379 let v =
6380 match a with
6381 | None -> "false"
6382 | Some f ->
6383 if f = infinity
6384 then "true"
6385 else string_of_float f
6387 Printf.bprintf bb "\n %s='%s'" s v
6388 and oco s a b =
6389 if always || a <> b
6390 then
6391 match a with
6392 | Cmulti ((n, a, b), _) when n > 1 ->
6393 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6394 | Csplit (n, _) when n > 1 ->
6395 Printf.bprintf bb "\n %s='%d'" s ~-n
6396 | _ -> ()
6397 and obeco s a b =
6398 if always || a <> b
6399 then
6400 match a with
6401 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6402 | _ -> ()
6404 let w, h =
6405 if always
6406 then dc.winw, dc.winh
6407 else
6408 match state.fullscreen with
6409 | Some wh -> wh
6410 | None -> c.winw, c.winh
6412 let zoom, presentation, interpagespace, maxwait =
6413 if always
6414 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6415 else
6416 match state.mode with
6417 | Birdseye (bc, _, _, _, _) ->
6418 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6419 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6421 oi "width" w dc.winw;
6422 oi "height" h dc.winh;
6423 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6424 oi "scroll-handle-height" c.scrollh dc.scrollh;
6425 ob "case-insensitive-search" c.icase dc.icase;
6426 ob "preload" c.preload dc.preload;
6427 oi "page-bias" c.pagebias dc.pagebias;
6428 oi "scroll-step" c.scrollstep dc.scrollstep;
6429 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6430 ob "max-height-fit" c.maxhfit dc.maxhfit;
6431 ob "crop-hack" c.crophack dc.crophack;
6432 oW "throttle" maxwait dc.maxwait;
6433 ob "highlight-links" c.hlinks dc.hlinks;
6434 ob "under-cursor-info" c.underinfo dc.underinfo;
6435 oi "vertical-margin" interpagespace dc.interpagespace;
6436 oz "zoom" zoom dc.zoom;
6437 ob "presentation" presentation dc.presentation;
6438 oi "rotation-angle" c.angle dc.angle;
6439 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6440 ob "proportional-display" c.proportional dc.proportional;
6441 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6442 oi "tex-count" c.texcount dc.texcount;
6443 oi "slice-height" c.sliceheight dc.sliceheight;
6444 oi "thumbnail-width" c.thumbw dc.thumbw;
6445 ob "persistent-location" c.jumpback dc.jumpback;
6446 oc "background-color" c.bgcolor dc.bgcolor;
6447 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6448 oi "tile-width" c.tilew dc.tilew;
6449 oi "tile-height" c.tileh dc.tileh;
6450 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6451 ob "checkers" c.checkers dc.checkers;
6452 oi "aalevel" c.aalevel dc.aalevel;
6453 ob "trim-margins" c.trimmargins dc.trimmargins;
6454 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6455 os "uri-launcher" c.urilauncher dc.urilauncher;
6456 os "path-launcher" c.pathlauncher dc.pathlauncher;
6457 oC "color-space" c.colorspace dc.colorspace;
6458 ob "invert-colors" c.invert dc.invert;
6459 oF "brightness" c.colorscale dc.colorscale;
6460 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6461 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6462 oco "columns" c.columns dc.columns;
6463 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6464 os "selection-command" c.selcmd dc.selcmd;
6465 ob "update-cursor" c.updatecurs dc.updatecurs;
6466 oi "hint-font-size" c.hfsize dc.hfsize;
6469 let keymapsbuf always dc c =
6470 let bb = Buffer.create 16 in
6471 let rec loop = function
6472 | [] -> ()
6473 | (modename, h) :: rest ->
6474 let dh = findkeyhash dc modename in
6475 if always || h <> dh
6476 then (
6477 if Hashtbl.length h > 0
6478 then (
6479 if Buffer.length bb > 0
6480 then Buffer.add_char bb '\n';
6481 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6482 Hashtbl.iter (fun i o ->
6483 let isdifferent = always ||
6485 let dO = Hashtbl.find dh i in
6486 dO <> o
6487 with Not_found -> true
6489 if isdifferent
6490 then
6491 let addkm (k, m) =
6492 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6493 if Wsi.withalt m then Buffer.add_string bb "alt-";
6494 if Wsi.withshift m then Buffer.add_string bb "shift-";
6495 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6496 Buffer.add_string bb (Wsi.keyname k);
6498 let addkms l =
6499 let rec loop = function
6500 | [] -> ()
6501 | km :: [] -> addkm km
6502 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6504 loop l
6506 Buffer.add_string bb "<map in='";
6507 addkm i;
6508 match o with
6509 | KMinsrt km ->
6510 Buffer.add_string bb "' out='";
6511 addkm km;
6512 Buffer.add_string bb "'/>\n"
6514 | KMinsrl kms ->
6515 Buffer.add_string bb "' out='";
6516 addkms kms;
6517 Buffer.add_string bb "'/>\n"
6519 | KMmulti (ins, kms) ->
6520 Buffer.add_char bb ' ';
6521 addkms ins;
6522 Buffer.add_string bb "' out='";
6523 addkms kms;
6524 Buffer.add_string bb "'/>\n"
6525 ) h;
6526 Buffer.add_string bb "</keymap>";
6529 loop rest
6531 loop c.keyhashes;
6535 let save () =
6536 let uifontsize = fstate.fontsize in
6537 let bb = Buffer.create 32768 in
6538 let f (h, dc) =
6539 let dc = if conf.bedefault then conf else dc in
6540 Buffer.add_string bb "<llppconfig>\n";
6542 if String.length !fontpath > 0
6543 then
6544 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6545 uifontsize
6546 !fontpath
6547 else (
6548 if uifontsize <> 14
6549 then
6550 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6553 Buffer.add_string bb "<defaults ";
6554 add_attrs bb true dc dc;
6555 let kb = keymapsbuf true dc dc in
6556 if Buffer.length kb > 0
6557 then (
6558 Buffer.add_string bb ">\n";
6559 Buffer.add_buffer bb kb;
6560 Buffer.add_string bb "\n</defaults>\n";
6562 else Buffer.add_string bb "/>\n";
6564 let adddoc path pan anchor c bookmarks =
6565 if bookmarks == [] && c = dc && anchor = emptyanchor
6566 then ()
6567 else (
6568 Printf.bprintf bb "<doc path='%s'"
6569 (enent path 0 (String.length path));
6571 if anchor <> emptyanchor
6572 then (
6573 let n, y = anchor in
6574 Printf.bprintf bb " page='%d'" n;
6575 if y > 1e-6
6576 then
6577 Printf.bprintf bb " rely='%f'" y
6581 if pan != 0
6582 then Printf.bprintf bb " pan='%d'" pan;
6584 add_attrs bb false dc c;
6585 let kb = keymapsbuf false dc c in
6587 begin match bookmarks with
6588 | [] ->
6589 if Buffer.length kb > 0
6590 then (
6591 Buffer.add_string bb ">\n";
6592 Buffer.add_buffer bb kb;
6593 Buffer.add_string bb "\n</doc>\n";
6595 else Buffer.add_string bb "/>\n"
6596 | _ ->
6597 Buffer.add_string bb ">\n<bookmarks>\n";
6598 List.iter (fun (title, _level, (page, rely)) ->
6599 Printf.bprintf bb
6600 "<item title='%s' page='%d'"
6601 (enent title 0 (String.length title))
6602 page
6604 if rely > 1e-6
6605 then
6606 Printf.bprintf bb " rely='%f'" rely
6608 Buffer.add_string bb "/>\n";
6609 ) bookmarks;
6610 Buffer.add_string bb "</bookmarks>";
6611 if Buffer.length kb > 0
6612 then (
6613 Buffer.add_string bb "\n";
6614 Buffer.add_buffer bb kb;
6616 Buffer.add_string bb "\n</doc>\n";
6617 end;
6621 let pan, conf =
6622 match state.mode with
6623 | Birdseye (c, pan, _, _, _) ->
6624 let beyecolumns =
6625 match conf.columns with
6626 | Cmulti ((c, _, _), _) -> Some c
6627 | Csingle -> None
6628 | Csplit _ -> None
6629 and columns =
6630 match c.columns with
6631 | Cmulti (c, _) -> Cmulti (c, [||])
6632 | Csingle -> Csingle
6633 | Csplit _ -> failwith "quit from bird's eye while split"
6635 pan, { c with beyecolumns = beyecolumns; columns = columns }
6636 | _ -> state.x, conf
6638 let basename = Filename.basename state.path in
6639 adddoc basename pan (getanchor ())
6640 { conf with
6641 autoscrollstep =
6642 match state.autoscroll with
6643 | Some step -> step
6644 | None -> conf.autoscrollstep }
6645 (if conf.savebmarks then state.bookmarks else []);
6647 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6648 if basename <> path
6649 then adddoc path x y c bookmarks
6650 ) h;
6651 Buffer.add_string bb "</llppconfig>";
6653 load1 f;
6654 if Buffer.length bb > 0
6655 then
6657 let tmp = !confpath ^ ".tmp" in
6658 let oc = open_out_bin tmp in
6659 Buffer.output_buffer oc bb;
6660 close_out oc;
6661 Unix.rename tmp !confpath;
6662 with exn ->
6663 prerr_endline
6664 ("error while saving configuration: " ^ Printexc.to_string exn)
6666 end;;
6668 let () =
6669 Arg.parse
6670 (Arg.align
6671 [("-p", Arg.String (fun s -> state.password <- s) ,
6672 "<password> Set password");
6674 ("-f", Arg.String (fun s -> Config.fontpath := s),
6675 "<path> Set path to the user interface font");
6677 ("-c", Arg.String (fun s -> Config.confpath := s),
6678 "<path> Set path to the configuration file");
6680 ("-v", Arg.Unit (fun () ->
6681 Printf.printf
6682 "%s\nconfiguration path: %s\n"
6683 (version ())
6684 Config.defconfpath
6686 exit 0), " Print version and exit");
6689 (fun s -> state.path <- s)
6690 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6692 if String.length state.path = 0
6693 then (prerr_endline "file name missing"; exit 1);
6695 Config.load ();
6697 let globalkeyhash = findkeyhash conf "global" in
6698 let wsfd, winw, winh = Wsi.init (object
6699 method expose =
6700 if nogeomcmds state.geomcmds || platform == Posx
6701 then display ()
6702 else (
6703 GlFunc.draw_buffer `front;
6704 GlClear.color (scalecolor2 conf.bgcolor);
6705 GlClear.clear [`color];
6706 GlFunc.draw_buffer `back;
6708 method display = display ()
6709 method reshape w h = reshape w h
6710 method mouse b d x y m = mouse b d x y m
6711 method motion x y = state.mpos <- (x, y); motion x y
6712 method pmotion x y = state.mpos <- (x, y); pmotion x y
6713 method key k m =
6714 let mascm = m land (
6715 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6716 ) in
6717 match state.keystate with
6718 | KSnone ->
6719 let km = k, mascm in
6720 begin
6721 match
6722 try Hashtbl.find globalkeyhash km
6723 with Not_found ->
6724 let modehash = state.uioh#modehash in
6725 try Hashtbl.find modehash km
6726 with Not_found -> KMinsrt (k, m)
6727 with
6728 | KMinsrt (k, m) -> keyboard k m
6729 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6730 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6732 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6733 List.iter (fun (k, m) -> keyboard k m) insrt;
6734 state.keystate <- KSnone
6735 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6736 state.keystate <- KSinto (keys, insrt)
6737 | _ ->
6738 state.keystate <- KSnone
6740 method enter x y = state.mpos <- (x, y); pmotion x y
6741 method leave = state.mpos <- (-1, -1)
6742 method quit = raise Quit
6743 end) conf.winw conf.winh (platform = Posx) in
6745 state.wsfd <- wsfd;
6747 if not (
6748 List.exists GlMisc.check_extension
6749 [ "GL_ARB_texture_rectangle"
6750 ; "GL_EXT_texture_recangle"
6751 ; "GL_NV_texture_rectangle" ]
6753 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6755 let cr, sw =
6756 match Ne.pipe () with
6757 | Ne.Exn exn ->
6758 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6759 exit 1
6760 | Ne.Res rw -> rw
6761 and sr, cw =
6762 match Ne.pipe () with
6763 | Ne.Exn exn ->
6764 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6765 exit 1
6766 | Ne.Res rw -> rw
6769 cloexec cr;
6770 cloexec sw;
6771 cloexec sr;
6772 cloexec cw;
6774 setcheckers conf.checkers;
6775 redirectstderr ();
6777 init (cr, cw) (
6778 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6779 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6780 !Config.fontpath
6782 state.sr <- sr;
6783 state.sw <- sw;
6784 state.text <- "Opening " ^ state.path;
6785 reshape winw winh;
6786 opendoc state.path state.password;
6787 state.uioh <- uioh;
6789 let rec loop deadline =
6790 let r =
6791 match state.errfd with
6792 | None -> [state.sr; state.wsfd]
6793 | Some fd -> [state.sr; state.wsfd; fd]
6795 if state.redisplay
6796 then (
6797 state.redisplay <- false;
6798 display ();
6800 let timeout =
6801 let now = now () in
6802 if deadline > now
6803 then (
6804 if deadline = infinity
6805 then ~-.1.0
6806 else max 0.0 (deadline -. now)
6808 else 0.0
6810 let r, _, _ =
6811 try Unix.select r [] [] timeout
6812 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6814 begin match r with
6815 | [] ->
6816 state.ghyll None;
6817 let newdeadline =
6818 if state.ghyll == noghyll
6819 then
6820 match state.autoscroll with
6821 | Some step when step != 0 ->
6822 let y = state.y + step in
6823 let y =
6824 if y < 0
6825 then state.maxy
6826 else if y >= state.maxy then 0 else y
6828 gotoy y;
6829 if state.mode = View
6830 then state.text <- "";
6831 deadline +. 0.01
6832 | _ -> infinity
6833 else deadline +. 0.01
6835 loop newdeadline
6837 | l ->
6838 let rec checkfds = function
6839 | [] -> ()
6840 | fd :: rest when fd = state.sr ->
6841 let cmd = readcmd state.sr in
6842 act cmd;
6843 checkfds rest
6845 | fd :: rest when fd = state.wsfd ->
6846 Wsi.readresp fd;
6847 checkfds rest
6849 | fd :: rest ->
6850 let s = String.create 80 in
6851 let n = Unix.read fd s 0 80 in
6852 if conf.redirectstderr
6853 then (
6854 Buffer.add_substring state.errmsgs s 0 n;
6855 state.newerrmsgs <- true;
6856 state.redisplay <- true;
6858 else (
6859 prerr_string (String.sub s 0 n);
6860 flush stderr;
6862 checkfds rest
6864 checkfds l;
6865 let newdeadline =
6866 let deadline1 =
6867 if deadline = infinity
6868 then now () +. 0.01
6869 else deadline
6871 match state.autoscroll with
6872 | Some step when step != 0 -> deadline1
6873 | _ -> if state.ghyll == noghyll then infinity else deadline1
6875 loop newdeadline
6876 end;
6879 loop infinity;
6880 with Quit ->
6881 Config.save ();
6882 exit 0;