Fix config saving when exiting from bird's eye mode
[llpp.git] / main.ml
blobcecfe8ed6a4e62f449c89317efadc31400aab316
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 * trimcachepath)
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 trimcachepath = string
37 and memsize = int
38 and aalevel = int
39 and irect = (int * int * int * int)
40 and trimparams = (trimmargins * irect)
41 and colorspace = | Rgb | Bgr | Gray
44 type link =
45 | Lnotfound
46 | Lfound of int
47 and linkdir =
48 | LDfirst
49 | LDlast
50 | LDfirstvisible of (int * int * int)
51 | LDleft of int
52 | LDright of int
53 | LDdown of int
54 | LDup of int
57 type pagewithlinks =
58 | Pwlnotfound
59 | Pwl of int
62 type keymap =
63 | KMinsrt of key
64 | KMinsrl of key list
65 | KMmulti of key list * key list
66 and key = int * int
67 and keyhash = (key, keymap) Hashtbl.t
68 and keystate =
69 | KSnone
70 | KSinto of (key list * key list)
73 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
74 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
76 type pipe = (Unix.file_descr * Unix.file_descr);;
78 external init : pipe -> params -> unit = "ml_init";;
79 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
80 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
81 external getpdimrect : int -> float array = "ml_getpdimrect";;
82 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
83 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
84 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
85 external measurestr : int -> string -> float = "ml_measure_string";;
86 external getmaxw : unit -> float = "ml_getmaxw";;
87 external postprocess :
88 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
89 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
90 external platform : unit -> platform = "ml_platform";;
91 external setaalevel : int -> unit = "ml_setaalevel";;
92 external realloctexts : int -> bool = "ml_realloctexts";;
93 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
94 external findlink : opaque -> linkdir -> link = "ml_findlink";;
95 external getlink : opaque -> int -> under = "ml_getlink";;
96 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
97 external getlinkcount : opaque -> int = "ml_getlinkcount";;
98 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
99 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
101 let platform_to_string = function
102 | Punknown -> "unknown"
103 | Plinux -> "Linux"
104 | Posx -> "OSX"
105 | Psun -> "Sun"
106 | Pfreebsd -> "FreeBSD"
107 | Pdragonflybsd -> "DragonflyBSD"
108 | Popenbsd -> "OpenBSD"
109 | Pnetbsd -> "NetBSD"
110 | Pcygwin -> "Cygwin"
113 let platform = platform ();;
115 let popen cmd fda =
116 if platform = Pcygwin
117 then (
118 let sh = "/bin/sh" in
119 let args = [|sh; "-c"; cmd|] in
120 let rec std si so se = function
121 | [] -> si, so, se
122 | (fd, 0) :: rest -> std fd so se rest
123 | (fd, -1) :: rest ->
124 Unix.set_close_on_exec fd;
125 std si so se rest
126 | (_, n) :: _ ->
127 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
129 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
130 ignore (Unix.create_process sh args si so se)
132 else popen cmd fda;
135 type x = int
136 and y = int
137 and tilex = int
138 and tiley = int
139 and tileparams = (x * y * width * height * tilex * tiley)
142 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
144 type mpos = int * int
145 and mstate =
146 | Msel of (mpos * mpos)
147 | Mpan of mpos
148 | Mscrolly | Mscrollx
149 | Mzoom of (int * int)
150 | Mzoomrect of (mpos * mpos)
151 | Mnone
154 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
155 and onkey = string -> int -> te
156 and ondone = string -> unit
157 and histcancel = unit -> unit
158 and onhist = ((histcmd -> string) * histcancel)
159 and histcmd = HCnext | HCprev | HCfirst | HClast
160 and cancelonempty = bool
161 and te =
162 | TEstop
163 | TEdone of string
164 | TEcont of string
165 | TEswitch of textentry
168 type 'a circbuf =
169 { store : 'a array
170 ; mutable rc : int
171 ; mutable wc : int
172 ; mutable len : int
176 let bound v minv maxv =
177 max minv (min maxv v);
180 let cbnew n v =
181 { store = Array.create n v
182 ; rc = 0
183 ; wc = 0
184 ; len = 0
188 let drawstring size x y s =
189 Gl.enable `blend;
190 Gl.enable `texture_2d;
191 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
192 ignore (drawstr size x y s);
193 Gl.disable `blend;
194 Gl.disable `texture_2d;
197 let drawstring1 size x y s =
198 drawstr size x y s;
201 let drawstring2 size x y fmt =
202 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
205 let cbcap b = Array.length b.store;;
207 let cbput b v =
208 let cap = cbcap b in
209 b.store.(b.wc) <- v;
210 b.wc <- (b.wc + 1) mod cap;
211 b.rc <- b.wc;
212 b.len <- min (b.len + 1) cap;
215 let cbempty b = b.len = 0;;
217 let cbgetg b circular dir =
218 if cbempty b
219 then b.store.(0)
220 else
221 let rc = b.rc + dir in
222 let rc =
223 if circular
224 then (
225 if rc = -1
226 then b.len-1
227 else (
228 if rc = b.len
229 then 0
230 else rc
233 else max 0 (min rc (b.len-1))
235 b.rc <- rc;
236 b.store.(rc);
239 let cbget b = cbgetg b false;;
240 let cbgetc b = cbgetg b true;;
242 type page =
243 { pageno : int
244 ; pagedimno : int
245 ; pagew : int
246 ; pageh : int
247 ; pagex : int
248 ; pagey : int
249 ; pagevw : int
250 ; pagevh : int
251 ; pagedispx : int
252 ; pagedispy : int
253 ; pagecol : int
257 let debugl l =
258 dolog "l %d dim=%d {" l.pageno l.pagedimno;
259 dolog " WxH %dx%d" l.pagew l.pageh;
260 dolog " vWxH %dx%d" l.pagevw l.pagevh;
261 dolog " pagex,y %d,%d" l.pagex l.pagey;
262 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
263 dolog " column %d" l.pagecol;
264 dolog "}";
267 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
268 dolog "rect {";
269 dolog " x0,y0=(% f, % f)" x0 y0;
270 dolog " x1,y1=(% f, % f)" x1 y1;
271 dolog " x2,y2=(% f, % f)" x2 y2;
272 dolog " x3,y3=(% f, % f)" x3 y3;
273 dolog "}";
276 type multicolumns = multicol * pagegeom
277 and singlecolumn = pagegeom
278 and splitcolumns = columncount * pagegeom
279 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
280 and multicol = columncount * covercount * covercount
281 and pdimno = int
282 and columncount = int
283 and covercount = int;;
285 type conf =
286 { mutable scrollbw : int
287 ; mutable scrollh : int
288 ; mutable icase : bool
289 ; mutable preload : bool
290 ; mutable pagebias : int
291 ; mutable verbose : bool
292 ; mutable debug : bool
293 ; mutable scrollstep : int
294 ; mutable hscrollstep : int
295 ; mutable maxhfit : bool
296 ; mutable crophack : bool
297 ; mutable autoscrollstep : int
298 ; mutable maxwait : float option
299 ; mutable hlinks : bool
300 ; mutable underinfo : bool
301 ; mutable interpagespace : interpagespace
302 ; mutable zoom : float
303 ; mutable presentation : bool
304 ; mutable angle : angle
305 ; mutable winw : int
306 ; mutable winh : int
307 ; mutable savebmarks : bool
308 ; mutable proportional : proportional
309 ; mutable trimmargins : trimmargins
310 ; mutable trimfuzz : irect
311 ; mutable memlimit : memsize
312 ; mutable texcount : texcount
313 ; mutable sliceheight : sliceheight
314 ; mutable thumbw : width
315 ; mutable jumpback : bool
316 ; mutable bgcolor : float * float * float
317 ; mutable bedefault : bool
318 ; mutable scrollbarinpm : bool
319 ; mutable tilew : int
320 ; mutable tileh : int
321 ; mutable mustoresize : memsize
322 ; mutable checkers : bool
323 ; mutable aalevel : int
324 ; mutable urilauncher : string
325 ; mutable pathlauncher : string
326 ; mutable colorspace : colorspace
327 ; mutable invert : bool
328 ; mutable colorscale : float
329 ; mutable redirectstderr : bool
330 ; mutable ghyllscroll : (int * int * int) option
331 ; mutable columns : columns
332 ; mutable beyecolumns : columncount option
333 ; mutable selcmd : string
334 ; mutable updatecurs : bool
335 ; mutable keyhashes : (string * keyhash) list
336 ; mutable hfsize : int
337 ; mutable pgscale : float
339 and columns =
340 | Csingle of singlecolumn
341 | Cmulti of multicolumns
342 | Csplit of splitcolumns
345 type anchor = pageno * top;;
347 type outline = string * int * anchor;;
349 type rect = float * float * float * float * float * float * float * float;;
351 type tile = opaque * pixmapsize * elapsed
352 and elapsed = float;;
353 type pagemapkey = pageno * gen;;
354 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
355 and row = int
356 and col = int;;
358 let emptyanchor = (0, 0.0);;
360 type infochange = | Memused | Docinfo | Pdim;;
362 class type uioh = object
363 method display : unit
364 method key : int -> int -> uioh
365 method button : int -> bool -> int -> int -> int -> uioh
366 method motion : int -> int -> uioh
367 method pmotion : int -> int -> uioh
368 method infochanged : infochange -> unit
369 method scrollpw : (int * float * float)
370 method scrollph : (int * float * float)
371 method modehash : keyhash
372 end;;
374 type mode =
375 | Birdseye of (conf * leftx * pageno * pageno * anchor)
376 | Textentry of (textentry * onleave)
377 | View
378 | LinkNav of linktarget
379 and onleave = leavetextentrystatus -> unit
380 and leavetextentrystatus = | Cancel | Confirm
381 and helpitem = string * int * action
382 and action =
383 | Noaction
384 | Action of (uioh -> uioh)
385 and linktarget =
386 | Ltexact of (pageno * int)
387 | Ltgendir of int
390 let isbirdseye = function Birdseye _ -> true | _ -> false;;
391 let istextentry = function Textentry _ -> true | _ -> false;;
393 type currently =
394 | Idle
395 | Loading of (page * gen)
396 | Tiling of (
397 page * opaque * colorspace * angle * gen * col * row * width * height
399 | Outlining of outline list
402 let emptykeyhash = Hashtbl.create 0;;
403 let nouioh : uioh = object (self)
404 method display = ()
405 method key _ _ = self
406 method button _ _ _ _ _ = self
407 method motion _ _ = self
408 method pmotion _ _ = self
409 method infochanged _ = ()
410 method scrollpw = (0, nan, nan)
411 method scrollph = (0, nan, nan)
412 method modehash = emptykeyhash
413 end;;
415 type state =
416 { mutable sr : Unix.file_descr
417 ; mutable sw : Unix.file_descr
418 ; mutable wsfd : Unix.file_descr
419 ; mutable errfd : Unix.file_descr option
420 ; mutable stderr : Unix.file_descr
421 ; mutable errmsgs : Buffer.t
422 ; mutable newerrmsgs : bool
423 ; mutable w : int
424 ; mutable x : int
425 ; mutable y : int
426 ; mutable scrollw : int
427 ; mutable hscrollh : int
428 ; mutable anchor : anchor
429 ; mutable ranchors : (string * string * anchor) list
430 ; mutable maxy : int
431 ; mutable layout : page list
432 ; pagemap : (pagemapkey, opaque) Hashtbl.t
433 ; tilemap : (tilemapkey, tile) Hashtbl.t
434 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
435 ; mutable pdims : (pageno * width * height * leftx) list
436 ; mutable pagecount : int
437 ; mutable currently : currently
438 ; mutable mstate : mstate
439 ; mutable searchpattern : string
440 ; mutable rects : (pageno * recttype * rect) list
441 ; mutable rects1 : (pageno * recttype * rect) list
442 ; mutable text : string
443 ; mutable fullscreen : (width * height) option
444 ; mutable mode : mode
445 ; mutable uioh : uioh
446 ; mutable outlines : outline array
447 ; mutable bookmarks : outline list
448 ; mutable path : string
449 ; mutable password : string
450 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
451 ; mutable memused : memsize
452 ; mutable gen : gen
453 ; mutable throttle : (page list * int * float) option
454 ; mutable autoscroll : int option
455 ; mutable ghyll : (int option -> unit)
456 ; mutable help : helpitem array
457 ; mutable docinfo : (int * string) list
458 ; mutable texid : GlTex.texture_id option
459 ; hists : hists
460 ; mutable prevzoom : float
461 ; mutable progress : float
462 ; mutable redisplay : bool
463 ; mutable mpos : mpos
464 ; mutable keystate : keystate
465 ; mutable glinks : bool
466 ; mutable prevcolumns : (columns * float) option
468 and hists =
469 { pat : string circbuf
470 ; pag : string circbuf
471 ; nav : anchor circbuf
472 ; sel : string circbuf
476 let defconf =
477 { scrollbw = 7
478 ; scrollh = 12
479 ; icase = true
480 ; preload = true
481 ; pagebias = 0
482 ; verbose = false
483 ; debug = false
484 ; scrollstep = 24
485 ; hscrollstep = 24
486 ; maxhfit = true
487 ; crophack = false
488 ; autoscrollstep = 2
489 ; maxwait = None
490 ; hlinks = false
491 ; underinfo = false
492 ; interpagespace = 2
493 ; zoom = 1.0
494 ; presentation = false
495 ; angle = 0
496 ; winw = 900
497 ; winh = 900
498 ; savebmarks = true
499 ; proportional = true
500 ; trimmargins = false
501 ; trimfuzz = (0,0,0,0)
502 ; memlimit = 32 lsl 20
503 ; texcount = 256
504 ; sliceheight = 24
505 ; thumbw = 76
506 ; jumpback = true
507 ; bgcolor = (0.5, 0.5, 0.5)
508 ; bedefault = false
509 ; scrollbarinpm = true
510 ; tilew = 2048
511 ; tileh = 2048
512 ; mustoresize = 256 lsl 20
513 ; checkers = true
514 ; aalevel = 8
515 ; urilauncher =
516 (match platform with
517 | Plinux | Pfreebsd | Pdragonflybsd
518 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
519 | Posx -> "open \"%s\""
520 | Pcygwin -> "cygstart \"%s\""
521 | Punknown -> "echo %s")
522 ; pathlauncher = "lp \"%s\""
523 ; selcmd =
524 (match platform with
525 | Plinux | Pfreebsd | Pdragonflybsd
526 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
527 | Posx -> "pbcopy"
528 | Pcygwin -> "wsel"
529 | Punknown -> "cat")
530 ; colorspace = Rgb
531 ; invert = false
532 ; colorscale = 1.0
533 ; redirectstderr = false
534 ; ghyllscroll = None
535 ; columns = Csingle [||]
536 ; beyecolumns = None
537 ; updatecurs = false
538 ; hfsize = 12
539 ; pgscale = 1.0
540 ; keyhashes =
541 let mk n = (n, Hashtbl.create 1) in
542 [ mk "global"
543 ; mk "info"
544 ; mk "help"
545 ; mk "outline"
546 ; mk "listview"
547 ; mk "birdseye"
548 ; mk "textentry"
549 ; mk "links"
550 ; mk "view"
555 let findkeyhash c name =
556 try List.assoc name c.keyhashes
557 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
560 let conf = { defconf with angle = defconf.angle };;
562 let pgscale h = truncate (float h *. conf.pgscale);;
564 type fontstate =
565 { mutable fontsize : int
566 ; mutable wwidth : float
567 ; mutable maxrows : int
571 let fstate =
572 { fontsize = 14
573 ; wwidth = nan
574 ; maxrows = -1
578 let setfontsize n =
579 fstate.fontsize <- n;
580 fstate.wwidth <- measurestr fstate.fontsize "w";
581 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
584 let geturl s =
585 let colonpos = try String.index s ':' with Not_found -> -1 in
586 let len = String.length s in
587 if colonpos >= 0 && colonpos + 3 < len
588 then (
589 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
590 then
591 let schemestartpos =
592 try String.rindex_from s colonpos ' '
593 with Not_found -> -1
595 let scheme =
596 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
598 match scheme with
599 | "http" | "ftp" | "mailto" ->
600 let epos =
601 try String.index_from s colonpos ' '
602 with Not_found -> len
604 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
605 | _ -> ""
606 else ""
608 else ""
611 let gotouri uri =
612 if String.length conf.urilauncher = 0
613 then print_endline uri
614 else (
615 let url = geturl uri in
616 if String.length url = 0
617 then print_endline uri
618 else
619 let re = Str.regexp "%s" in
620 let command = Str.global_replace re url conf.urilauncher in
621 try popen command []
622 with exn ->
623 Printf.eprintf
624 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
625 flush stderr;
629 let version () =
630 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
631 (platform_to_string platform) Sys.word_size Sys.ocaml_version
634 let makehelp () =
635 let strings = version () :: "" :: Help.keys in
636 Array.of_list (
637 List.map (fun s ->
638 let url = geturl s in
639 if String.length url > 0
640 then (s, 0, Action (fun u -> gotouri url; u))
641 else (s, 0, Noaction)
642 ) strings);
645 let noghyll _ = ();;
646 let firstgeomcmds = "", [];;
648 let state =
649 { sr = Unix.stdin
650 ; sw = Unix.stdin
651 ; wsfd = Unix.stdin
652 ; errfd = None
653 ; stderr = Unix.stderr
654 ; errmsgs = Buffer.create 0
655 ; newerrmsgs = false
656 ; x = 0
657 ; y = 0
658 ; w = 0
659 ; scrollw = 0
660 ; hscrollh = 0
661 ; anchor = emptyanchor
662 ; ranchors = []
663 ; layout = []
664 ; maxy = max_int
665 ; tilelru = Queue.create ()
666 ; pagemap = Hashtbl.create 10
667 ; tilemap = Hashtbl.create 10
668 ; pdims = []
669 ; pagecount = 0
670 ; currently = Idle
671 ; mstate = Mnone
672 ; rects = []
673 ; rects1 = []
674 ; text = ""
675 ; mode = View
676 ; fullscreen = None
677 ; searchpattern = ""
678 ; outlines = [||]
679 ; bookmarks = []
680 ; path = ""
681 ; password = ""
682 ; geomcmds = firstgeomcmds
683 ; hists =
684 { nav = cbnew 10 (0, 0.0)
685 ; pat = cbnew 10 ""
686 ; pag = cbnew 10 ""
687 ; sel = cbnew 10 ""
689 ; memused = 0
690 ; gen = 0
691 ; throttle = None
692 ; autoscroll = None
693 ; ghyll = noghyll
694 ; help = makehelp ()
695 ; docinfo = []
696 ; texid = None
697 ; prevzoom = 1.0
698 ; progress = -1.0
699 ; uioh = nouioh
700 ; redisplay = true
701 ; mpos = (-1, -1)
702 ; keystate = KSnone
703 ; glinks = false
704 ; prevcolumns = None
708 let vlog fmt =
709 if conf.verbose
710 then
711 Printf.kprintf prerr_endline fmt
712 else
713 Printf.kprintf ignore fmt
716 let launchpath () =
717 if String.length conf.pathlauncher = 0
718 then print_endline state.path
719 else (
720 let re = Str.regexp "%s" in
721 let command = Str.global_replace re state.path conf.pathlauncher in
722 try popen command []
723 with exn ->
724 Printf.eprintf
725 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
726 flush stderr;
730 module Ne = struct
731 type 'a t = | Res of 'a | Exn of exn;;
733 let pipe () =
734 try Res (Unix.pipe ())
735 with exn -> Exn exn
738 let clo fd f =
739 try Unix.close fd
740 with exn -> f (Printexc.to_string exn)
743 let dup fd =
744 try Res (Unix.dup fd)
745 with exn -> Exn exn
748 let dup2 fd1 fd2 =
749 try Res (Unix.dup2 fd1 fd2)
750 with exn -> Exn exn
752 end;;
754 let redirectstderr () =
755 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
756 if conf.redirectstderr
757 then
758 match Ne.pipe () with
759 | Ne.Exn exn ->
760 dolog "failed to create stderr redirection pipes: %s"
761 (Printexc.to_string exn)
763 | Ne.Res (r, w) ->
764 begin match Ne.dup Unix.stderr with
765 | Ne.Exn exn ->
766 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
767 Ne.clo r (clofail "pipe/r");
768 Ne.clo w (clofail "pipe/w");
770 | Ne.Res dupstderr ->
771 begin match Ne.dup2 w Unix.stderr with
772 | Ne.Exn exn ->
773 dolog "failed to dup2 to stderr: %s"
774 (Printexc.to_string exn);
775 Ne.clo dupstderr (clofail "stderr duplicate");
776 Ne.clo r (clofail "redir pipe/r");
777 Ne.clo w (clofail "redir pipe/w");
779 | Ne.Res () ->
780 state.stderr <- dupstderr;
781 state.errfd <- Some r;
782 end;
784 else (
785 state.newerrmsgs <- false;
786 begin match state.errfd with
787 | Some fd ->
788 begin match Ne.dup2 state.stderr Unix.stderr with
789 | Ne.Exn exn ->
790 dolog "failed to dup2 original stderr: %s"
791 (Printexc.to_string exn)
792 | Ne.Res () ->
793 Ne.clo fd (clofail "dup of stderr");
794 Unix.dup2 state.stderr Unix.stderr;
795 state.errfd <- None;
796 end;
797 | None -> ()
798 end;
799 prerr_string (Buffer.contents state.errmsgs);
800 flush stderr;
801 Buffer.clear state.errmsgs;
805 module G =
806 struct
807 let postRedisplay who =
808 if conf.verbose
809 then prerr_endline ("redisplay for " ^ who);
810 state.redisplay <- true;
812 end;;
814 let getopaque pageno =
815 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
816 with Not_found -> None
819 let putopaque pageno opaque =
820 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
823 let pagetranslatepoint l x y =
824 let dy = y - l.pagedispy in
825 let y = dy + l.pagey in
826 let dx = x - l.pagedispx in
827 let x = dx + l.pagex in
828 (x, y);
831 let getunder x y =
832 let rec f = function
833 | l :: rest ->
834 begin match getopaque l.pageno with
835 | Some opaque ->
836 let x0 = l.pagedispx in
837 let x1 = x0 + l.pagevw in
838 let y0 = l.pagedispy in
839 let y1 = y0 + l.pagevh in
840 if y >= y0 && y <= y1 && x >= x0 && x <= x1
841 then
842 let px, py = pagetranslatepoint l x y in
843 match whatsunder opaque px py with
844 | Unone -> f rest
845 | under -> under
846 else f rest
847 | _ ->
848 f rest
850 | [] -> Unone
852 f state.layout
855 let showtext c s =
856 state.text <- Printf.sprintf "%c%s" c s;
857 G.postRedisplay "showtext";
860 let undertext = function
861 | Unone -> "none"
862 | Ulinkuri s -> s
863 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
864 | Utext s -> "font: " ^ s
865 | Uunexpected s -> "unexpected: " ^ s
866 | Ulaunch s -> "launch: " ^ s
867 | Unamed s -> "named: " ^ s
868 | Uremote (filename, pageno) ->
869 Printf.sprintf "%s: page %d" filename (pageno+1)
872 let updateunder x y =
873 match getunder x y with
874 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
875 | Ulinkuri uri ->
876 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
877 Wsi.setcursor Wsi.CURSOR_INFO
878 | Ulinkgoto (pageno, _) ->
879 if conf.underinfo
880 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
881 Wsi.setcursor Wsi.CURSOR_INFO
882 | Utext s ->
883 if conf.underinfo then showtext 'f' ("ont: " ^ s);
884 Wsi.setcursor Wsi.CURSOR_TEXT
885 | Uunexpected s ->
886 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
887 Wsi.setcursor Wsi.CURSOR_INHERIT
888 | Ulaunch s ->
889 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
890 Wsi.setcursor Wsi.CURSOR_INHERIT
891 | Unamed s ->
892 if conf.underinfo then showtext 'n' ("amed: " ^ s);
893 Wsi.setcursor Wsi.CURSOR_INHERIT
894 | Uremote (filename, pageno) ->
895 if conf.underinfo then showtext 'r'
896 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
897 Wsi.setcursor Wsi.CURSOR_INFO
900 let showlinktype under =
901 if conf.underinfo
902 then
903 match under with
904 | Unone -> ()
905 | under ->
906 let s = undertext under in
907 showtext ' ' s
910 let addchar s c =
911 let b = Buffer.create (String.length s + 1) in
912 Buffer.add_string b s;
913 Buffer.add_char b c;
914 Buffer.contents b;
917 let colorspace_of_string s =
918 match String.lowercase s with
919 | "rgb" -> Rgb
920 | "bgr" -> Bgr
921 | "gray" -> Gray
922 | _ -> failwith "invalid colorspace"
925 let int_of_colorspace = function
926 | Rgb -> 0
927 | Bgr -> 1
928 | Gray -> 2
931 let colorspace_of_int = function
932 | 0 -> Rgb
933 | 1 -> Bgr
934 | 2 -> Gray
935 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
938 let colorspace_to_string = function
939 | Rgb -> "rgb"
940 | Bgr -> "bgr"
941 | Gray -> "gray"
944 let intentry_with_suffix text key =
945 let c =
946 if key >= 32 && key < 127
947 then Char.chr key
948 else '\000'
950 match Char.lowercase c with
951 | '0' .. '9' ->
952 let text = addchar text c in
953 TEcont text
955 | 'k' | 'm' | 'g' ->
956 let text = addchar text c in
957 TEcont text
959 | _ ->
960 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
961 TEcont text
964 let multicolumns_to_string (n, a, b) =
965 if a = 0 && b = 0
966 then Printf.sprintf "%d" n
967 else Printf.sprintf "%d,%d,%d" n a b;
970 let multicolumns_of_string s =
972 (int_of_string s, 0, 0)
973 with _ ->
974 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
977 let readcmd fd =
978 let s = "xxxx" in
979 let n = Unix.read fd s 0 4 in
980 if n != 4 then failwith "incomplete read(len)";
981 let len = 0
982 lor (Char.code s.[0] lsl 24)
983 lor (Char.code s.[1] lsl 16)
984 lor (Char.code s.[2] lsl 8)
985 lor (Char.code s.[3] lsl 0)
987 let s = String.create len in
988 let n = Unix.read fd s 0 len in
989 if n != len then failwith "incomplete read(data)";
993 let btod b = if b then 1 else 0;;
995 let wcmd fmt =
996 let b = Buffer.create 16 in
997 Buffer.add_string b "llll";
998 Printf.kbprintf
999 (fun b ->
1000 let s = Buffer.contents b in
1001 let n = String.length s in
1002 let len = n - 4 in
1003 (* dolog "wcmd %S" (String.sub s 4 len); *)
1004 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1005 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1006 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1007 s.[3] <- Char.chr (len land 0xff);
1008 let n' = Unix.write state.sw s 0 n in
1009 if n' != n then failwith "write failed";
1010 ) b fmt;
1013 let calcips h =
1014 if conf.presentation
1015 then
1016 let d = conf.winh - h in
1017 max conf.interpagespace ((d + 1) / 2)
1018 else
1019 conf.interpagespace
1022 let calcheight () =
1023 match conf.columns with
1024 | Cmulti ((c, _, _), b) ->
1025 let rec loop y h n =
1026 if n < 0
1027 then loop y h (n+1)
1028 else (
1029 if n = Array.length b
1030 then y + h
1031 else
1032 let (_, _, y', (_, _, h', _)) = b.(n) in
1033 let y = min y y'
1034 and h = max h h' in
1035 loop y h (n+1)
1038 loop max_int 0 (((Array.length b - 1) / c) * c)
1039 | Csingle b ->
1040 if Array.length b > 0
1041 then
1042 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1043 y + h + (if conf.presentation then calcips h else 0)
1044 else 0
1045 | Csplit (_, b) ->
1046 if Array.length b > 0
1047 then
1048 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1049 y + h
1050 else 0
1053 let getpageyh pageno =
1054 let pageno = bound pageno 0 state.pagecount in
1055 match conf.columns with
1056 | Csingle b ->
1057 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1058 let y =
1059 if conf.presentation
1060 then y - calcips h
1061 else y
1063 y, h
1064 | Cmulti (_, b) ->
1065 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1066 y, h
1067 | Csplit (c, b) ->
1068 let n = pageno*c in
1069 let (_, _, y, (_, _, h, _)) = b.(n) in
1070 y, h
1073 let getpagedim pageno =
1074 let rec f ppdim l =
1075 match l with
1076 | (n, _, _, _) as pdim :: rest ->
1077 if n >= pageno
1078 then (if n = pageno then pdim else ppdim)
1079 else f pdim rest
1081 | [] -> ppdim
1083 f (-1, -1, -1, -1) state.pdims
1086 let getpagey pageno = fst (getpageyh pageno);;
1088 let nogeomcmds cmds =
1089 match cmds with
1090 | s, [] -> String.length s = 0
1091 | _ -> false
1094 let layoutN ((columns, coverA, coverB), b) y sh =
1095 let sh = sh - state.hscrollh in
1096 let rec fold accu n =
1097 if n = Array.length b
1098 then accu
1099 else
1100 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1101 if (vy - y) > sh &&
1102 (n = coverA - 1
1103 || n = state.pagecount - coverB
1104 || (n - coverA) mod columns = columns - 1)
1105 then accu
1106 else
1107 let accu =
1108 if vy + h > y
1109 then
1110 let pagey = max 0 (y - vy) in
1111 let pagedispy = if pagey > 0 then 0 else vy - y in
1112 let pagedispx, pagex =
1113 let pdx =
1114 if n = coverA - 1 || n = state.pagecount - coverB
1115 then state.x + (conf.winw - state.scrollw - w) / 2
1116 else dx + xoff + state.x
1118 if pdx < 0
1119 then 0, -pdx
1120 else pdx, 0
1122 let pagevw =
1123 let vw = conf.winw - state.scrollw - pagedispx in
1124 let pw = w - pagex in
1125 min vw pw
1127 let pagevh = min (h - pagey) (sh - pagedispy) in
1128 if pagevw > 0 && pagevh > 0
1129 then
1130 let e =
1131 { pageno = n
1132 ; pagedimno = pdimno
1133 ; pagew = w
1134 ; pageh = h
1135 ; pagex = pagex
1136 ; pagey = pagey
1137 ; pagevw = pagevw
1138 ; pagevh = pagevh
1139 ; pagedispx = pagedispx
1140 ; pagedispy = pagedispy
1141 ; pagecol = 0
1144 e :: accu
1145 else
1146 accu
1147 else
1148 accu
1150 fold accu (n+1)
1152 List.rev (fold [] 0)
1155 let layoutS (columns, b) y sh =
1156 let sh = sh - state.hscrollh in
1157 let rec fold accu n =
1158 if n = Array.length b
1159 then accu
1160 else
1161 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1162 if (vy - y) > sh
1163 then accu
1164 else
1165 let accu =
1166 if vy + pageh > y
1167 then
1168 let x = xoff + state.x in
1169 let pagey = max 0 (y - vy) in
1170 let pagedispy = if pagey > 0 then 0 else vy - y in
1171 let pagedispx, pagex =
1172 if px = 0
1173 then (
1174 if x < 0
1175 then 0, -x
1176 else x, 0
1178 else (
1179 let px = px - x in
1180 if px < 0
1181 then -px, 0
1182 else 0, px
1185 let pagecolw = pagew/columns in
1186 let pagedispx =
1187 if pagecolw < conf.winw
1188 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1189 else pagedispx
1191 let pagevw =
1192 let vw = conf.winw - pagedispx - state.scrollw in
1193 let pw = pagew - pagex in
1194 min vw pw
1196 let pagevw = min pagevw pagecolw in
1197 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1198 if pagevw > 0 && pagevh > 0
1199 then
1200 let e =
1201 { pageno = n/columns
1202 ; pagedimno = pdimno
1203 ; pagew = pagew
1204 ; pageh = pageh
1205 ; pagex = pagex
1206 ; pagey = pagey
1207 ; pagevw = pagevw
1208 ; pagevh = pagevh
1209 ; pagedispx = pagedispx
1210 ; pagedispy = pagedispy
1211 ; pagecol = n mod columns
1214 e :: accu
1215 else
1216 accu
1217 else
1218 accu
1220 fold accu (n+1)
1222 List.rev (fold [] 0)
1225 let layout y sh =
1226 if nogeomcmds state.geomcmds
1227 then
1228 match conf.columns with
1229 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1230 | Cmulti c -> layoutN c y sh
1231 | Csplit s -> layoutS s y sh
1232 else []
1235 let clamp incr =
1236 let y = state.y + incr in
1237 let y = max 0 y in
1238 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1242 let itertiles l f =
1243 let tilex = l.pagex mod conf.tilew in
1244 let tiley = l.pagey mod conf.tileh in
1246 let col = l.pagex / conf.tilew in
1247 let row = l.pagey / conf.tileh in
1249 let rec rowloop row y0 dispy h =
1250 if h = 0
1251 then ()
1252 else (
1253 let dh = conf.tileh - y0 in
1254 let dh = min h dh in
1255 let rec colloop col x0 dispx w =
1256 if w = 0
1257 then ()
1258 else (
1259 let dw = conf.tilew - x0 in
1260 let dw = min w dw in
1262 f col row dispx dispy x0 y0 dw dh;
1263 colloop (col+1) 0 (dispx+dw) (w-dw)
1266 colloop col tilex l.pagedispx l.pagevw;
1267 rowloop (row+1) 0 (dispy+dh) (h-dh)
1270 if l.pagevw > 0 && l.pagevh > 0
1271 then rowloop row tiley l.pagedispy l.pagevh;
1274 let gettileopaque l col row =
1275 let key =
1276 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1278 try Some (Hashtbl.find state.tilemap key)
1279 with Not_found -> None
1282 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1283 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1284 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1287 let drawtiles l color =
1288 GlDraw.color color;
1289 let f col row x y tilex tiley w h =
1290 match gettileopaque l col row with
1291 | Some (opaque, _, t) ->
1292 let params = x, y, w, h, tilex, tiley in
1293 if conf.invert
1294 then (
1295 Gl.enable `blend;
1296 GlFunc.blend_func `zero `one_minus_src_color;
1298 drawtile params opaque;
1299 if conf.invert
1300 then Gl.disable `blend;
1301 if conf.debug
1302 then (
1303 let s = Printf.sprintf
1304 "%d[%d,%d] %f sec"
1305 l.pageno col row t
1307 let w = measurestr fstate.fontsize s in
1308 GlMisc.push_attrib [`current];
1309 GlDraw.color (0.0, 0.0, 0.0);
1310 GlDraw.rect
1311 (float (x-2), float (y-2))
1312 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1313 GlDraw.color (1.0, 1.0, 1.0);
1314 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1315 GlMisc.pop_attrib ();
1318 | _ ->
1319 let w =
1320 let lw = conf.winw - state.scrollw - x in
1321 min lw w
1322 and h =
1323 let lh = conf.winh - y in
1324 min lh h
1326 begin match state.texid with
1327 | Some id ->
1328 Gl.enable `texture_2d;
1329 GlTex.bind_texture `texture_2d id;
1330 let x0 = float x
1331 and y0 = float y
1332 and x1 = float (x+w)
1333 and y1 = float (y+h) in
1335 let tw = float w /. 64.0
1336 and th = float h /. 64.0 in
1337 let tx0 = float tilex /. 64.0
1338 and ty0 = float tiley /. 64.0 in
1339 let tx1 = tx0 +. tw
1340 and ty1 = ty0 +. th in
1341 GlDraw.begins `quads;
1342 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1343 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1344 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1345 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1346 GlDraw.ends ();
1348 Gl.disable `texture_2d;
1349 | None ->
1350 GlDraw.color (1.0, 1.0, 1.0);
1351 GlDraw.rect
1352 (float x, float y)
1353 (float (x+w), float (y+h));
1354 end;
1355 if w > 128 && h > fstate.fontsize + 10
1356 then (
1357 GlDraw.color (0.0, 0.0, 0.0);
1358 let c, r =
1359 if conf.verbose
1360 then (col*conf.tilew, row*conf.tileh)
1361 else col, row
1363 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1365 GlDraw.color color;
1367 itertiles l f
1370 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1372 let tilevisible1 l x y =
1373 let ax0 = l.pagex
1374 and ax1 = l.pagex + l.pagevw
1375 and ay0 = l.pagey
1376 and ay1 = l.pagey + l.pagevh in
1378 let bx0 = x
1379 and by0 = y in
1380 let bx1 = min (bx0 + conf.tilew) l.pagew
1381 and by1 = min (by0 + conf.tileh) l.pageh in
1383 let rx0 = max ax0 bx0
1384 and ry0 = max ay0 by0
1385 and rx1 = min ax1 bx1
1386 and ry1 = min ay1 by1 in
1388 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1389 nonemptyintersection
1392 let tilevisible layout n x y =
1393 let rec findpageinlayout m = function
1394 | l :: rest when l.pageno = n ->
1395 tilevisible1 l x y || (
1396 match conf.columns with
1397 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1398 | _ -> false
1400 | _ :: rest -> findpageinlayout 0 rest
1401 | [] -> false
1403 findpageinlayout 0 layout;
1406 let tileready l x y =
1407 tilevisible1 l x y &&
1408 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1411 let tilepage n p layout =
1412 let rec loop = function
1413 | l :: rest ->
1414 if l.pageno = n
1415 then
1416 let f col row _ _ _ _ _ _ =
1417 if state.currently = Idle
1418 then
1419 match gettileopaque l col row with
1420 | Some _ -> ()
1421 | None ->
1422 let x = col*conf.tilew
1423 and y = row*conf.tileh in
1424 let w =
1425 let w = l.pagew - x in
1426 min w conf.tilew
1428 let h =
1429 let h = l.pageh - y in
1430 min h conf.tileh
1432 wcmd "tile %s %d %d %d %d" p x y w h;
1433 state.currently <-
1434 Tiling (
1435 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1436 conf.tilew, conf.tileh
1439 itertiles l f;
1440 else
1441 loop rest
1443 | [] -> ()
1445 if nogeomcmds state.geomcmds
1446 then loop layout;
1449 let preloadlayout visiblepages =
1450 let presentation = conf.presentation in
1451 let interpagespace = conf.interpagespace in
1452 conf.presentation <- false;
1453 conf.interpagespace <- 0;
1454 let y =
1455 match visiblepages with
1456 | [] -> if state.y >= state.maxy then state.maxy else 0
1457 | l :: _ -> getpagey l.pageno + (l.pagey - min 0 l.pagedispy)
1459 let y = if y < conf.winh then 0 else y - conf.winh in
1460 let h = conf.winh*3 in
1461 let pages = layout y h in
1462 conf.presentation <- presentation;
1463 conf.interpagespace <- interpagespace;
1464 pages;
1467 let load pages =
1468 let rec loop pages =
1469 if state.currently != Idle
1470 then ()
1471 else
1472 match pages with
1473 | l :: rest ->
1474 begin match getopaque l.pageno with
1475 | None ->
1476 wcmd "page %d %d" l.pageno l.pagedimno;
1477 state.currently <- Loading (l, state.gen);
1478 | Some opaque ->
1479 tilepage l.pageno opaque pages;
1480 loop rest
1481 end;
1482 | _ -> ()
1484 if nogeomcmds state.geomcmds
1485 then loop pages
1488 let preload pages =
1489 load pages;
1490 if conf.preload && state.currently = Idle
1491 then load (preloadlayout pages);
1494 let layoutready layout =
1495 let rec fold all ls =
1496 all && match ls with
1497 | l :: rest ->
1498 let seen = ref false in
1499 let allvisible = ref true in
1500 let foo col row _ _ _ _ _ _ =
1501 seen := true;
1502 allvisible := !allvisible &&
1503 begin match gettileopaque l col row with
1504 | Some _ -> true
1505 | None -> false
1508 itertiles l foo;
1509 fold (!seen && !allvisible) rest
1510 | [] -> true
1512 let alltilesvisible = fold true layout in
1513 alltilesvisible;
1516 let gotoy y =
1517 let y = bound y 0 state.maxy in
1518 let y, layout, proceed =
1519 match conf.maxwait with
1520 | Some time when state.ghyll == noghyll ->
1521 begin match state.throttle with
1522 | None ->
1523 let layout = layout y conf.winh in
1524 let ready = layoutready layout in
1525 if not ready
1526 then (
1527 load layout;
1528 state.throttle <- Some (layout, y, now ());
1530 else G.postRedisplay "gotoy showall (None)";
1531 y, layout, ready
1532 | Some (_, _, started) ->
1533 let dt = now () -. started in
1534 if dt > time
1535 then (
1536 state.throttle <- None;
1537 let layout = layout y conf.winh in
1538 load layout;
1539 G.postRedisplay "maxwait";
1540 y, layout, true
1542 else -1, [], false
1545 | _ ->
1546 let layout = layout y conf.winh in
1547 if true || layoutready layout
1548 then G.postRedisplay "gotoy ready";
1549 y, layout, true
1551 if proceed
1552 then (
1553 state.y <- y;
1554 state.layout <- layout;
1555 begin match state.mode with
1556 | LinkNav (Ltexact (pageno, linkno)) ->
1557 let rec loop = function
1558 | [] ->
1559 state.mode <- LinkNav (Ltgendir 0)
1560 | l :: _ when l.pageno = pageno ->
1561 begin match getopaque pageno with
1562 | None ->
1563 state.mode <- LinkNav (Ltgendir 0)
1564 | Some opaque ->
1565 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1566 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1567 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1568 then state.mode <- LinkNav (Ltgendir 0)
1570 | _ :: rest -> loop rest
1572 loop layout
1573 | _ -> ()
1574 end;
1575 begin match state.mode with
1576 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1577 if not (pagevisible layout pageno)
1578 then (
1579 match state.layout with
1580 | [] -> ()
1581 | l :: _ ->
1582 state.mode <- Birdseye (
1583 conf, leftx, l.pageno, hooverpageno, anchor
1586 | LinkNav (Ltgendir dir as lt) ->
1587 let linknav =
1588 let rec loop = function
1589 | [] -> lt
1590 | l :: rest ->
1591 match getopaque l.pageno with
1592 | None -> loop rest
1593 | Some opaque ->
1594 let link =
1595 let ld =
1596 if dir = 0
1597 then LDfirstvisible (l.pagex, l.pagey, dir)
1598 else (
1599 if dir > 0 then LDfirst else LDlast
1602 findlink opaque ld
1604 match link with
1605 | Lnotfound -> loop rest
1606 | Lfound n ->
1607 showlinktype (getlink opaque n);
1608 Ltexact (l.pageno, n)
1610 loop state.layout
1612 state.mode <- LinkNav linknav
1613 | _ -> ()
1614 end;
1615 preload layout;
1617 state.ghyll <- noghyll;
1618 if conf.updatecurs
1619 then (
1620 let mx, my = state.mpos in
1621 updateunder mx my;
1625 let conttiling pageno opaque =
1626 tilepage pageno opaque
1627 (if conf.preload then preloadlayout state.layout else state.layout)
1630 let gotoy_and_clear_text y =
1631 if not conf.verbose then state.text <- "";
1632 gotoy y;
1635 let getanchor () =
1636 match state.layout with
1637 | [] -> emptyanchor
1638 | l :: _ ->
1639 let coloff = l.pagecol * l.pageh in
1640 (l.pageno,
1641 (float (l.pagey - l.pagedispy) +. float coloff) /. float l.pageh)
1644 let getanchory (n, top) =
1645 let y, h = getpageyh n in
1646 y + (truncate (top *. float h));
1649 let gotoanchor anchor =
1650 gotoy (getanchory anchor);
1653 let addnav () =
1654 cbput state.hists.nav (getanchor ());
1657 let getnav dir =
1658 let anchor = cbgetc state.hists.nav dir in
1659 getanchory anchor;
1662 let gotoghyll y =
1663 let scroll f n a b =
1664 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1665 let snake f a b =
1666 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1667 if f < a
1668 then s (float f /. float a)
1669 else (
1670 if f > b
1671 then 1.0 -. s ((float (f-b) /. float (n-b)))
1672 else 1.0
1675 snake f a b
1676 and summa f n a b =
1677 (* courtesy:
1678 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1679 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1680 let iv1 = iv f in
1681 let ins = float a *. iv1
1682 and outs = float (n-b) *. iv1 in
1683 let ones = b - a in
1684 ins +. outs +. float ones
1686 let rec set (_N, _A, _B) y sy =
1687 let sum = summa 1.0 _N _A _B in
1688 let dy = float (y - sy) in
1689 state.ghyll <- (
1690 let rec gf n y1 o =
1691 if n >= _N
1692 then state.ghyll <- noghyll
1693 else
1694 let go n =
1695 let s = scroll n _N _A _B in
1696 let y1 = y1 +. ((s *. dy) /. sum) in
1697 gotoy_and_clear_text (truncate y1);
1698 state.ghyll <- gf (n+1) y1;
1700 match o with
1701 | None -> go n
1702 | Some y' -> set (_N/2, 0, 0) y' state.y
1704 gf 0 (float state.y)
1707 match conf.ghyllscroll with
1708 | None ->
1709 gotoy_and_clear_text y
1710 | Some nab ->
1711 if state.ghyll == noghyll
1712 then set nab y state.y
1713 else state.ghyll (Some y)
1716 let gotopage n top =
1717 let y, h = getpageyh n in
1718 let y = y + (truncate (top *. float h)) in
1719 gotoghyll y
1722 let gotopage1 n top =
1723 let y = getpagey n in
1724 let y = y + top in
1725 gotoghyll y
1728 let invalidate s f =
1729 state.layout <- [];
1730 state.pdims <- [];
1731 state.rects <- [];
1732 state.rects1 <- [];
1733 match state.geomcmds with
1734 | ps, [] when String.length ps = 0 ->
1735 f ();
1736 state.geomcmds <- s, [];
1738 | ps, [] ->
1739 state.geomcmds <- ps, [s, f];
1741 | ps, (s', _) :: rest when s' = s ->
1742 state.geomcmds <- ps, ((s, f) :: rest);
1744 | ps, cmds ->
1745 state.geomcmds <- ps, ((s, f) :: cmds);
1748 let opendoc path password =
1749 state.path <- path;
1750 state.password <- password;
1751 state.gen <- state.gen + 1;
1752 state.docinfo <- [];
1754 setaalevel conf.aalevel;
1755 Wsi.settitle ("llpp " ^ Filename.basename path);
1756 wcmd "open %s\000%s\000" path password;
1757 invalidate "reqlayout"
1758 (fun () ->
1759 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1762 let scalecolor c =
1763 let c = c *. conf.colorscale in
1764 (c, c, c);
1767 let scalecolor2 (r, g, b) =
1768 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1771 let docolumns = function
1772 | Csingle _ ->
1773 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1774 let rec loop pageno pdimno pdim y ph pdims =
1775 if pageno = state.pagecount
1776 then ()
1777 else
1778 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1779 match pdims with
1780 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1781 pdimno+1, pdim, rest
1782 | _ ->
1783 pdimno, pdim, pdims
1785 let x =
1786 if isbirdseye state.mode
1787 then (conf.winw - state.scrollw - w) / 2 - xoff
1788 else 0
1790 let y = y +
1791 (if conf.presentation
1792 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1793 else (if pageno = 0 then 0 else calcips h)
1796 a.(pageno) <- (pdimno, x, y, pdim);
1797 loop (pageno+1) pdimno pdim (y + h) h pdims
1799 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1800 conf.columns <- Csingle a;
1802 | Cmulti ((columns, coverA, coverB), _) ->
1803 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1804 let rec loop pageno pdimno pdim x y rowh pdims =
1805 let rec fixrow m = if m = pageno then () else
1806 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1807 if h < rowh
1808 then (
1809 let y = y + (rowh - h) / 2 in
1810 a.(m) <- (pdimno, x, y, pdim);
1812 fixrow (m+1)
1814 if pageno = state.pagecount
1815 then fixrow (((pageno - 1) / columns) * columns)
1816 else
1817 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1818 match pdims with
1819 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1820 pdimno+1, pdim, rest
1821 | _ ->
1822 pdimno, pdim, pdims
1824 let x, y, rowh' =
1825 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1826 then (
1827 (conf.winw - state.scrollw - w) / 2,
1828 y + rowh + conf.interpagespace, h
1830 else (
1831 if (pageno - coverA) mod columns = 0
1832 then 0, y + rowh + conf.interpagespace, h
1833 else x, y, max rowh h
1836 if pageno > 1 && (pageno - coverA) mod columns = 0
1837 then fixrow (pageno - columns);
1838 a.(pageno) <- (pdimno, x, y, pdim);
1839 let x = x + w + xoff*2 + conf.interpagespace in
1840 loop (pageno+1) pdimno pdim x y rowh' pdims
1842 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1843 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1845 | Csplit (c, _) ->
1846 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1847 let rec loop pageno pdimno pdim y pdims =
1848 if pageno = state.pagecount
1849 then ()
1850 else
1851 let pdimno, ((_, w, h, _) as pdim), pdims =
1852 match pdims with
1853 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1854 pdimno+1, pdim, rest
1855 | _ ->
1856 pdimno, pdim, pdims
1858 let cw = w / c in
1859 let rec loop1 n x y =
1860 if n = c then y else (
1861 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1862 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1865 let y = loop1 0 0 y in
1866 loop (pageno+1) pdimno pdim y pdims
1868 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1869 conf.columns <- Csplit (c, a);
1872 let represent () =
1873 docolumns conf.columns;
1874 state.maxy <- calcheight ();
1875 state.hscrollh <-
1876 if state.w <= conf.winw - state.scrollw
1877 then 0
1878 else state.scrollw
1880 match state.mode with
1881 | Birdseye (_, _, pageno, _, _) ->
1882 let y, h = getpageyh pageno in
1883 let top = (conf.winh - h) / 2 in
1884 gotoy (max 0 (y - top))
1885 | _ -> gotoanchor state.anchor
1888 let reshape w h =
1889 GlDraw.viewport 0 0 w h;
1890 let firsttime = state.geomcmds == firstgeomcmds in
1891 if not firsttime && nogeomcmds state.geomcmds
1892 then state.anchor <- getanchor ();
1894 conf.winw <- w;
1895 let w = truncate (float w *. conf.zoom) - state.scrollw in
1896 let w = max w 2 in
1897 conf.winh <- h;
1898 setfontsize fstate.fontsize;
1899 GlMat.mode `modelview;
1900 GlMat.load_identity ();
1902 GlMat.mode `projection;
1903 GlMat.load_identity ();
1904 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1905 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1906 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1908 let relx =
1909 if conf.zoom <= 1.0
1910 then 0.0
1911 else float state.x /. float state.w
1913 invalidate "geometry"
1914 (fun () ->
1915 state.w <- w;
1916 if not firsttime
1917 then state.x <- truncate (relx *. float w);
1918 let w =
1919 match conf.columns with
1920 | Csingle _ -> w
1921 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1922 | Csplit (c, _) -> w * c
1924 wcmd "geometry %d %d" w h);
1927 let enttext () =
1928 let len = String.length state.text in
1929 let drawstring s =
1930 let hscrollh =
1931 match state.mode with
1932 | Textentry _
1933 | View ->
1934 let h, _, _ = state.uioh#scrollpw in
1936 | _ -> 0
1938 let rect x w =
1939 GlDraw.rect
1940 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1941 (x+.w, float (conf.winh - hscrollh))
1944 let w = float (conf.winw - state.scrollw - 1) in
1945 if state.progress >= 0.0 && state.progress < 1.0
1946 then (
1947 GlDraw.color (0.3, 0.3, 0.3);
1948 let w1 = w *. state.progress in
1949 rect 0.0 w1;
1950 GlDraw.color (0.0, 0.0, 0.0);
1951 rect w1 (w-.w1)
1953 else (
1954 GlDraw.color (0.0, 0.0, 0.0);
1955 rect 0.0 w;
1958 GlDraw.color (1.0, 1.0, 1.0);
1959 drawstring fstate.fontsize
1960 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1962 let s =
1963 match state.mode with
1964 | Textentry ((prefix, text, _, _, _, _), _) ->
1965 let s =
1966 if len > 0
1967 then
1968 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1969 else
1970 Printf.sprintf "%s%s_" prefix text
1974 | _ -> state.text
1976 let s =
1977 if state.newerrmsgs
1978 then (
1979 if not (istextentry state.mode)
1980 then
1981 let s1 = "(press 'e' to review error messasges)" in
1982 if String.length s > 0 then s ^ " " ^ s1 else s1
1983 else s
1985 else s
1987 if String.length s > 0
1988 then drawstring s
1991 let gctiles () =
1992 let len = Queue.length state.tilelru in
1993 let layout = lazy (
1994 match state.throttle with
1995 | None ->
1996 if conf.preload
1997 then preloadlayout state.layout
1998 else state.layout
1999 | Some (layout, _, _) ->
2000 layout
2001 ) in
2002 let rec loop qpos =
2003 if state.memused <= conf.memlimit
2004 then ()
2005 else (
2006 if qpos < len
2007 then
2008 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2009 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2010 let (_, pw, ph, _) = getpagedim n in
2012 gen = state.gen
2013 && colorspace = conf.colorspace
2014 && angle = conf.angle
2015 && pagew = pw
2016 && pageh = ph
2017 && (
2018 let x = col*conf.tilew
2019 and y = row*conf.tileh in
2020 tilevisible (Lazy.force_val layout) n x y
2022 then Queue.push lruitem state.tilelru
2023 else (
2024 wcmd "freetile %s" p;
2025 state.memused <- state.memused - s;
2026 state.uioh#infochanged Memused;
2027 Hashtbl.remove state.tilemap k;
2029 loop (qpos+1)
2032 loop 0
2035 let flushtiles () =
2036 Queue.iter (fun (k, p, s) ->
2037 wcmd "freetile %s" p;
2038 state.memused <- state.memused - s;
2039 state.uioh#infochanged Memused;
2040 Hashtbl.remove state.tilemap k;
2041 ) state.tilelru;
2042 Queue.clear state.tilelru;
2043 load state.layout;
2046 let logcurrently = function
2047 | Idle -> dolog "Idle"
2048 | Loading (l, gen) ->
2049 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2050 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2051 dolog
2052 "Tiling %d[%d,%d] page=%s cs=%s angle"
2053 l.pageno col row pageopaque
2054 (colorspace_to_string colorspace)
2056 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2057 angle gen conf.angle state.gen
2058 tilew tileh
2059 conf.tilew conf.tileh
2061 | Outlining _ ->
2062 dolog "outlining"
2065 let act cmds =
2066 (* dolog "%S" cmds; *)
2067 let op, args =
2068 let spacepos =
2069 try String.index cmds ' '
2070 with Not_found -> -1
2072 if spacepos = -1
2073 then cmds, ""
2074 else
2075 let l = String.length cmds in
2076 let op = String.sub cmds 0 spacepos in
2077 op, begin
2078 if l - spacepos < 2 then ""
2079 else String.sub cmds (spacepos+1) (l-spacepos-1)
2082 match op with
2083 | "clear" ->
2084 state.uioh#infochanged Pdim;
2085 state.pdims <- [];
2087 | "clearrects" ->
2088 state.rects <- state.rects1;
2089 G.postRedisplay "clearrects";
2091 | "continue" ->
2092 let n =
2093 try Scanf.sscanf args "%u" (fun n -> n)
2094 with exn ->
2095 dolog "error processing 'continue' %S: %s"
2096 cmds (Printexc.to_string exn);
2097 exit 1;
2099 state.pagecount <- n;
2100 begin match state.currently with
2101 | Outlining l ->
2102 state.currently <- Idle;
2103 state.outlines <- Array.of_list (List.rev l)
2104 | _ -> ()
2105 end;
2107 let cur, cmds = state.geomcmds in
2108 if String.length cur = 0
2109 then failwith "umpossible";
2111 begin match List.rev cmds with
2112 | [] ->
2113 state.geomcmds <- "", [];
2114 represent ();
2115 | (s, f) :: rest ->
2116 f ();
2117 state.geomcmds <- s, List.rev rest;
2118 end;
2119 if conf.maxwait = None
2120 then G.postRedisplay "continue";
2122 | "title" ->
2123 Wsi.settitle args
2125 | "msg" ->
2126 showtext ' ' args
2128 | "vmsg" ->
2129 if conf.verbose
2130 then showtext ' ' args
2132 | "progress" ->
2133 let progress, text =
2135 Scanf.sscanf args "%f %n"
2136 (fun f pos ->
2137 f, String.sub args pos (String.length args - pos))
2138 with exn ->
2139 dolog "error processing 'progress' %S: %s"
2140 cmds (Printexc.to_string exn);
2141 exit 1;
2143 state.text <- text;
2144 state.progress <- progress;
2145 G.postRedisplay "progress"
2147 | "firstmatch" ->
2148 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2150 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2151 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2152 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2153 with exn ->
2154 dolog "error processing 'firstmatch' %S: %s"
2155 cmds (Printexc.to_string exn);
2156 exit 1;
2158 let y = (getpagey pageno) + truncate y0 in
2159 addnav ();
2160 gotoy y;
2161 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2163 | "match" ->
2164 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2166 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2167 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2168 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2169 with exn ->
2170 dolog "error processing 'match' %S: %s"
2171 cmds (Printexc.to_string exn);
2172 exit 1;
2174 state.rects1 <-
2175 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2177 | "page" ->
2178 let pageopaque, t =
2180 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2181 with exn ->
2182 dolog "error processing 'page' %S: %s"
2183 cmds (Printexc.to_string exn);
2184 exit 1;
2186 begin match state.currently with
2187 | Loading (l, gen) ->
2188 vlog "page %d took %f sec" l.pageno t;
2189 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2190 begin match state.throttle with
2191 | None ->
2192 let preloadedpages =
2193 if conf.preload
2194 then preloadlayout state.layout
2195 else state.layout
2197 let evict () =
2198 let module IntSet =
2199 Set.Make (struct type t = int let compare = (-) end) in
2200 let set =
2201 List.fold_left (fun s l -> IntSet.add l.pageno s)
2202 IntSet.empty preloadedpages
2204 let evictedpages =
2205 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2206 if not (IntSet.mem pageno set)
2207 then (
2208 wcmd "freepage %s" opaque;
2209 key :: accu
2211 else accu
2212 ) state.pagemap []
2214 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2216 evict ();
2217 state.currently <- Idle;
2218 if gen = state.gen
2219 then (
2220 tilepage l.pageno pageopaque state.layout;
2221 load state.layout;
2222 load preloadedpages;
2223 if pagevisible state.layout l.pageno
2224 && layoutready state.layout
2225 then G.postRedisplay "page";
2228 | Some (layout, _, _) ->
2229 state.currently <- Idle;
2230 tilepage l.pageno pageopaque layout;
2231 load state.layout
2232 end;
2234 | _ ->
2235 dolog "Inconsistent loading state";
2236 logcurrently state.currently;
2237 exit 1
2240 | "tile" ->
2241 let (x, y, opaque, size, t) =
2243 Scanf.sscanf args "%u %u %s %u %f"
2244 (fun x y p size t -> (x, y, p, size, t))
2245 with exn ->
2246 dolog "error processing 'tile' %S: %s"
2247 cmds (Printexc.to_string exn);
2248 exit 1;
2250 begin match state.currently with
2251 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2252 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2254 if tilew != conf.tilew || tileh != conf.tileh
2255 then (
2256 wcmd "freetile %s" opaque;
2257 state.currently <- Idle;
2258 load state.layout;
2260 else (
2261 puttileopaque l col row gen cs angle opaque size t;
2262 state.memused <- state.memused + size;
2263 state.uioh#infochanged Memused;
2264 gctiles ();
2265 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2266 opaque, size) state.tilelru;
2268 let layout =
2269 match state.throttle with
2270 | None -> state.layout
2271 | Some (layout, _, _) -> layout
2274 state.currently <- Idle;
2275 if gen = state.gen
2276 && conf.colorspace = cs
2277 && conf.angle = angle
2278 && tilevisible layout l.pageno x y
2279 then conttiling l.pageno pageopaque;
2281 begin match state.throttle with
2282 | None ->
2283 preload state.layout;
2284 if gen = state.gen
2285 && conf.colorspace = cs
2286 && conf.angle = angle
2287 && tilevisible state.layout l.pageno x y
2288 then G.postRedisplay "tile nothrottle";
2290 | Some (layout, y, _) ->
2291 let ready = layoutready layout in
2292 if ready
2293 then (
2294 state.y <- y;
2295 state.layout <- layout;
2296 state.throttle <- None;
2297 G.postRedisplay "throttle";
2299 else load layout;
2300 end;
2303 | _ ->
2304 dolog "Inconsistent tiling state";
2305 logcurrently state.currently;
2306 exit 1
2309 | "pdim" ->
2310 let pdim =
2312 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2313 with exn ->
2314 dolog "error processing 'pdim' %S: %s"
2315 cmds (Printexc.to_string exn);
2316 exit 1;
2318 state.uioh#infochanged Pdim;
2319 state.pdims <- pdim :: state.pdims
2321 | "o" ->
2322 let (l, n, t, h, pos) =
2324 Scanf.sscanf args "%u %u %d %u %n"
2325 (fun l n t h pos -> l, n, t, h, pos)
2326 with exn ->
2327 dolog "error processing 'o' %S: %s"
2328 cmds (Printexc.to_string exn);
2329 exit 1;
2331 let s = String.sub args pos (String.length args - pos) in
2332 let outline = (s, l, (n, float t /. float h)) in
2333 begin match state.currently with
2334 | Outlining outlines ->
2335 state.currently <- Outlining (outline :: outlines)
2336 | Idle ->
2337 state.currently <- Outlining [outline]
2338 | currently ->
2339 dolog "invalid outlining state";
2340 logcurrently currently
2343 | "info" ->
2344 state.docinfo <- (1, args) :: state.docinfo
2346 | "infoend" ->
2347 state.uioh#infochanged Docinfo;
2348 state.docinfo <- List.rev state.docinfo
2350 | _ ->
2351 dolog "unknown cmd `%S'" cmds
2354 let onhist cb =
2355 let rc = cb.rc in
2356 let action = function
2357 | HCprev -> cbget cb ~-1
2358 | HCnext -> cbget cb 1
2359 | HCfirst -> cbget cb ~-(cb.rc)
2360 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2361 and cancel () = cb.rc <- rc
2362 in (action, cancel)
2365 let search pattern forward =
2366 if String.length pattern > 0
2367 then
2368 let pn, py =
2369 match state.layout with
2370 | [] -> 0, 0
2371 | l :: _ ->
2372 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2374 wcmd "search %d %d %d %d,%s\000"
2375 (btod conf.icase) pn py (btod forward) pattern;
2378 let intentry text key =
2379 let c =
2380 if key >= 32 && key < 127
2381 then Char.chr key
2382 else '\000'
2384 match c with
2385 | '0' .. '9' ->
2386 let text = addchar text c in
2387 TEcont text
2389 | _ ->
2390 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2391 TEcont text
2394 let linknentry text key =
2395 let c =
2396 if key >= 32 && key < 127
2397 then Char.chr key
2398 else '\000'
2400 match c with
2401 | 'a' .. 'z' ->
2402 let text = addchar text c in
2403 TEcont text
2405 | _ ->
2406 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2407 TEcont text
2410 let linkndone f s =
2411 if String.length s > 0
2412 then (
2413 let n =
2414 let l = String.length s in
2415 let rec loop pos n = if pos = l then n else
2416 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2417 loop (pos+1) (n*26 + m)
2418 in loop 0 0
2420 let rec loop n = function
2421 | [] -> ()
2422 | l :: rest ->
2423 match getopaque l.pageno with
2424 | None -> loop n rest
2425 | Some opaque ->
2426 let m = getlinkcount opaque in
2427 if n < m
2428 then (
2429 let under = getlink opaque n in
2430 f under
2432 else loop (n-m) rest
2434 loop n state.layout;
2438 let textentry text key =
2439 if key land 0xff00 = 0xff00
2440 then TEcont text
2441 else TEcont (text ^ Wsi.toutf8 key)
2444 let reqlayout angle proportional =
2445 match state.throttle with
2446 | None ->
2447 if nogeomcmds state.geomcmds
2448 then state.anchor <- getanchor ();
2449 conf.angle <- angle mod 360;
2450 if conf.angle != 0
2451 then (
2452 match state.mode with
2453 | LinkNav _ -> state.mode <- View
2454 | _ -> ()
2456 conf.proportional <- proportional;
2457 invalidate "reqlayout"
2458 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2459 | _ -> ()
2462 let settrim trimmargins trimfuzz =
2463 if nogeomcmds state.geomcmds
2464 then state.anchor <- getanchor ();
2465 conf.trimmargins <- trimmargins;
2466 conf.trimfuzz <- trimfuzz;
2467 let x0, y0, x1, y1 = trimfuzz in
2468 invalidate "settrim"
2469 (fun () ->
2470 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2471 Hashtbl.iter (fun _ opaque ->
2472 wcmd "freepage %s" opaque;
2473 ) state.pagemap;
2474 Hashtbl.clear state.pagemap;
2477 let setzoom zoom =
2478 match state.throttle with
2479 | None ->
2480 let zoom = max 0.01 zoom in
2481 if zoom <> conf.zoom
2482 then (
2483 state.prevzoom <- conf.zoom;
2484 conf.zoom <- zoom;
2485 reshape conf.winw conf.winh;
2486 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2489 | Some (layout, y, started) ->
2490 let time =
2491 match conf.maxwait with
2492 | None -> 0.0
2493 | Some t -> t
2495 let dt = now () -. started in
2496 if dt > time
2497 then (
2498 state.y <- y;
2499 load layout;
2503 let setcolumns mode columns coverA coverB =
2504 state.prevcolumns <- Some (conf.columns, conf.zoom);
2505 if columns < 0
2506 then (
2507 if isbirdseye mode
2508 then showtext '!' "split mode doesn't work in bird's eye"
2509 else (
2510 conf.columns <- Csplit (-columns, [||]);
2511 state.x <- 0;
2512 conf.zoom <- 1.0;
2515 else (
2516 if columns < 2
2517 then (
2518 conf.columns <- Csingle [||];
2519 state.x <- 0;
2520 setzoom 1.0;
2522 else (
2523 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2524 conf.zoom <- 1.0;
2527 reshape conf.winw conf.winh;
2530 let enterbirdseye () =
2531 let zoom = float conf.thumbw /. float conf.winw in
2532 let birdseyepageno =
2533 let cy = conf.winh / 2 in
2534 let fold = function
2535 | [] -> 0
2536 | l :: rest ->
2537 let rec fold best = function
2538 | [] -> best.pageno
2539 | l :: rest ->
2540 let d = cy - (l.pagedispy + l.pagevh/2)
2541 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2542 if abs d < abs dbest
2543 then fold l rest
2544 else best.pageno
2545 in fold l rest
2547 fold state.layout
2549 state.mode <- Birdseye (
2550 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2552 conf.zoom <- zoom;
2553 conf.presentation <- false;
2554 conf.interpagespace <- 10;
2555 conf.hlinks <- false;
2556 state.x <- 0;
2557 state.mstate <- Mnone;
2558 conf.maxwait <- None;
2559 conf.columns <- (
2560 match conf.beyecolumns with
2561 | Some c ->
2562 conf.zoom <- 1.0;
2563 Cmulti ((c, 0, 0), [||])
2564 | None -> Csingle [||]
2566 Wsi.setcursor Wsi.CURSOR_INHERIT;
2567 if conf.verbose
2568 then
2569 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2570 (100.0*.zoom)
2571 else
2572 state.text <- ""
2574 reshape conf.winw conf.winh;
2577 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2578 state.mode <- View;
2579 conf.zoom <- c.zoom;
2580 conf.presentation <- c.presentation;
2581 conf.interpagespace <- c.interpagespace;
2582 conf.maxwait <- c.maxwait;
2583 conf.hlinks <- c.hlinks;
2584 conf.beyecolumns <- (
2585 match conf.columns with
2586 | Cmulti ((c, _, _), _) -> Some c
2587 | Csingle _ -> None
2588 | Csplit _ -> failwith "leaving bird's eye split mode"
2590 conf.columns <- (
2591 match c.columns with
2592 | Cmulti (c, _) -> Cmulti (c, [||])
2593 | Csingle _ -> Csingle [||]
2594 | Csplit (c, _) -> Csplit (c, [||])
2596 state.x <- leftx;
2597 if conf.verbose
2598 then
2599 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2600 (100.0*.conf.zoom)
2602 reshape conf.winw conf.winh;
2603 state.anchor <- if goback then anchor else (pageno, 0.0);
2606 let togglebirdseye () =
2607 match state.mode with
2608 | Birdseye vals -> leavebirdseye vals true
2609 | View -> enterbirdseye ()
2610 | _ -> ()
2613 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2614 let pageno = max 0 (pageno - incr) in
2615 let rec loop = function
2616 | [] -> gotopage1 pageno 0
2617 | l :: _ when l.pageno = pageno ->
2618 if l.pagedispy >= 0 && l.pagey = 0
2619 then G.postRedisplay "upbirdseye"
2620 else gotopage1 pageno 0
2621 | _ :: rest -> loop rest
2623 loop state.layout;
2624 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2627 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2628 let pageno = min (state.pagecount - 1) (pageno + incr) in
2629 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2630 let rec loop = function
2631 | [] ->
2632 let y, h = getpageyh pageno in
2633 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2634 gotoy (clamp dy)
2635 | l :: _ when l.pageno = pageno ->
2636 if l.pagevh != l.pageh
2637 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2638 else G.postRedisplay "downbirdseye"
2639 | _ :: rest -> loop rest
2641 loop state.layout
2644 let optentry mode _ key =
2645 let btos b = if b then "on" else "off" in
2646 if key >= 32 && key < 127
2647 then
2648 let c = Char.chr key in
2649 match c with
2650 | 's' ->
2651 let ondone s =
2652 try conf.scrollstep <- int_of_string s with exc ->
2653 state.text <- Printf.sprintf "bad integer `%s': %s"
2654 s (Printexc.to_string exc)
2656 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2658 | 'A' ->
2659 let ondone s =
2661 conf.autoscrollstep <- int_of_string s;
2662 if state.autoscroll <> None
2663 then state.autoscroll <- Some conf.autoscrollstep
2664 with exc ->
2665 state.text <- Printf.sprintf "bad integer `%s': %s"
2666 s (Printexc.to_string exc)
2668 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2670 | 'C' ->
2671 let ondone s =
2673 let n, a, b = multicolumns_of_string s in
2674 setcolumns mode n a b;
2675 with exc ->
2676 state.text <- Printf.sprintf "bad columns `%s': %s"
2677 s (Printexc.to_string exc)
2679 TEswitch ("columns: ", "", None, textentry, ondone, true)
2681 | 'Z' ->
2682 let ondone s =
2684 let zoom = float (int_of_string s) /. 100.0 in
2685 setzoom zoom
2686 with exc ->
2687 state.text <- Printf.sprintf "bad integer `%s': %s"
2688 s (Printexc.to_string exc)
2690 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2692 | 't' ->
2693 let ondone s =
2695 conf.thumbw <- bound (int_of_string s) 2 4096;
2696 state.text <-
2697 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2698 begin match mode with
2699 | Birdseye beye ->
2700 leavebirdseye beye false;
2701 enterbirdseye ();
2702 | _ -> ();
2704 with exc ->
2705 state.text <- Printf.sprintf "bad integer `%s': %s"
2706 s (Printexc.to_string exc)
2708 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2710 | 'R' ->
2711 let ondone s =
2712 match try
2713 Some (int_of_string s)
2714 with exc ->
2715 state.text <- Printf.sprintf "bad integer `%s': %s"
2716 s (Printexc.to_string exc);
2717 None
2718 with
2719 | Some angle -> reqlayout angle conf.proportional
2720 | None -> ()
2722 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2724 | 'i' ->
2725 conf.icase <- not conf.icase;
2726 TEdone ("case insensitive search " ^ (btos conf.icase))
2728 | 'p' ->
2729 conf.preload <- not conf.preload;
2730 gotoy state.y;
2731 TEdone ("preload " ^ (btos conf.preload))
2733 | 'v' ->
2734 conf.verbose <- not conf.verbose;
2735 TEdone ("verbose " ^ (btos conf.verbose))
2737 | 'd' ->
2738 conf.debug <- not conf.debug;
2739 TEdone ("debug " ^ (btos conf.debug))
2741 | 'h' ->
2742 conf.maxhfit <- not conf.maxhfit;
2743 state.maxy <- calcheight ();
2744 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2746 | 'c' ->
2747 conf.crophack <- not conf.crophack;
2748 TEdone ("crophack " ^ btos conf.crophack)
2750 | 'a' ->
2751 let s =
2752 match conf.maxwait with
2753 | None ->
2754 conf.maxwait <- Some infinity;
2755 "always wait for page to complete"
2756 | Some _ ->
2757 conf.maxwait <- None;
2758 "show placeholder if page is not ready"
2760 TEdone s
2762 | 'f' ->
2763 conf.underinfo <- not conf.underinfo;
2764 TEdone ("underinfo " ^ btos conf.underinfo)
2766 | 'P' ->
2767 conf.savebmarks <- not conf.savebmarks;
2768 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2770 | 'S' ->
2771 let ondone s =
2773 let pageno, py =
2774 match state.layout with
2775 | [] -> 0, 0
2776 | l :: _ ->
2777 l.pageno, l.pagey
2779 conf.interpagespace <- int_of_string s;
2780 docolumns conf.columns;
2781 state.maxy <- calcheight ();
2782 let y = getpagey pageno in
2783 gotoy (y + py)
2784 with exc ->
2785 state.text <- Printf.sprintf "bad integer `%s': %s"
2786 s (Printexc.to_string exc)
2788 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2790 | 'l' ->
2791 reqlayout conf.angle (not conf.proportional);
2792 TEdone ("proportional display " ^ btos conf.proportional)
2794 | 'T' ->
2795 settrim (not conf.trimmargins) conf.trimfuzz;
2796 TEdone ("trim margins " ^ btos conf.trimmargins)
2798 | 'I' ->
2799 conf.invert <- not conf.invert;
2800 TEdone ("invert colors " ^ btos conf.invert)
2802 | 'x' ->
2803 let ondone s =
2804 cbput state.hists.sel s;
2805 conf.selcmd <- s;
2807 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2808 textentry, ondone, true)
2810 | _ ->
2811 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2812 TEstop
2813 else
2814 TEcont state.text
2817 class type lvsource = object
2818 method getitemcount : int
2819 method getitem : int -> (string * int)
2820 method hasaction : int -> bool
2821 method exit :
2822 uioh:uioh ->
2823 cancel:bool ->
2824 active:int ->
2825 first:int ->
2826 pan:int ->
2827 qsearch:string ->
2828 uioh option
2829 method getactive : int
2830 method getfirst : int
2831 method getqsearch : string
2832 method setqsearch : string -> unit
2833 method getpan : int
2834 end;;
2836 class virtual lvsourcebase = object
2837 val mutable m_active = 0
2838 val mutable m_first = 0
2839 val mutable m_qsearch = ""
2840 val mutable m_pan = 0
2841 method getactive = m_active
2842 method getfirst = m_first
2843 method getqsearch = m_qsearch
2844 method getpan = m_pan
2845 method setqsearch s = m_qsearch <- s
2846 end;;
2848 let withoutlastutf8 s =
2849 let len = String.length s in
2850 if len = 0
2851 then s
2852 else
2853 let rec find pos =
2854 if pos = 0
2855 then pos
2856 else
2857 let b = Char.code s.[pos] in
2858 if b land 0b110000 = 0b11000000
2859 then find (pos-1)
2860 else pos-1
2862 let first =
2863 if Char.code s.[len-1] land 0x80 = 0
2864 then len-1
2865 else find (len-1)
2867 String.sub s 0 first;
2870 let textentrykeyboard
2871 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2872 let enttext te =
2873 state.mode <- Textentry (te, onleave);
2874 state.text <- "";
2875 enttext ();
2876 G.postRedisplay "textentrykeyboard enttext";
2878 let histaction cmd =
2879 match opthist with
2880 | None -> ()
2881 | Some (action, _) ->
2882 state.mode <- Textentry (
2883 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2885 G.postRedisplay "textentry histaction"
2887 match key with
2888 | 0xff08 -> (* backspace *)
2889 let s = withoutlastutf8 text in
2890 let len = String.length s in
2891 if cancelonempty && len = 0
2892 then (
2893 onleave Cancel;
2894 G.postRedisplay "textentrykeyboard after cancel";
2896 else (
2897 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2900 | 0xff0d ->
2901 ondone text;
2902 onleave Confirm;
2903 G.postRedisplay "textentrykeyboard after confirm"
2905 | 0xff52 -> histaction HCprev
2906 | 0xff54 -> histaction HCnext
2907 | 0xff50 -> histaction HCfirst
2908 | 0xff57 -> histaction HClast
2910 | 0xff1b -> (* escape*)
2911 if String.length text = 0
2912 then (
2913 begin match opthist with
2914 | None -> ()
2915 | Some (_, onhistcancel) -> onhistcancel ()
2916 end;
2917 onleave Cancel;
2918 state.text <- "";
2919 G.postRedisplay "textentrykeyboard after cancel2"
2921 else (
2922 enttext (c, "", opthist, onkey, ondone, cancelonempty)
2925 | 0xff9f | 0xffff -> () (* delete *)
2927 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2928 begin match onkey text key with
2929 | TEdone text ->
2930 ondone text;
2931 onleave Confirm;
2932 G.postRedisplay "textentrykeyboard after confirm2";
2934 | TEcont text ->
2935 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2937 | TEstop ->
2938 onleave Cancel;
2939 G.postRedisplay "textentrykeyboard after cancel3"
2941 | TEswitch te ->
2942 state.mode <- Textentry (te, onleave);
2943 G.postRedisplay "textentrykeyboard switch";
2944 end;
2946 | _ ->
2947 vlog "unhandled key %s" (Wsi.keyname key)
2950 let firstof first active =
2951 if first > active || abs (first - active) > fstate.maxrows - 1
2952 then max 0 (active - (fstate.maxrows/2))
2953 else first
2956 let calcfirst first active =
2957 if active > first
2958 then
2959 let rows = active - first in
2960 if rows > fstate.maxrows then active - fstate.maxrows else first
2961 else active
2964 let scrollph y maxy =
2965 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2966 let sh = float conf.winh /. sh in
2967 let sh = max sh (float conf.scrollh) in
2969 let percent =
2970 if y = state.maxy
2971 then 1.0
2972 else float y /. float maxy
2974 let position = (float conf.winh -. sh) *. percent in
2976 let position =
2977 if position +. sh > float conf.winh
2978 then float conf.winh -. sh
2979 else position
2981 position, sh;
2984 let coe s = (s :> uioh);;
2986 class listview ~(source:lvsource) ~trusted ~modehash =
2987 object (self)
2988 val m_pan = source#getpan
2989 val m_first = source#getfirst
2990 val m_active = source#getactive
2991 val m_qsearch = source#getqsearch
2992 val m_prev_uioh = state.uioh
2994 method private elemunder y =
2995 let n = y / (fstate.fontsize+1) in
2996 if m_first + n < source#getitemcount
2997 then (
2998 if source#hasaction (m_first + n)
2999 then Some (m_first + n)
3000 else None
3002 else None
3004 method display =
3005 Gl.enable `blend;
3006 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3007 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3008 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3009 GlDraw.color (1., 1., 1.);
3010 Gl.enable `texture_2d;
3011 let fs = fstate.fontsize in
3012 let nfs = fs + 1 in
3013 let ww = fstate.wwidth in
3014 let tabw = 30.0*.ww in
3015 let itemcount = source#getitemcount in
3016 let rec loop row =
3017 if (row - m_first) * nfs > conf.winh
3018 then ()
3019 else (
3020 if row >= 0 && row < itemcount
3021 then (
3022 let (s, level) = source#getitem row in
3023 let y = (row - m_first) * nfs in
3024 let x = 5.0 +. float (level + m_pan) *. ww in
3025 if row = m_active
3026 then (
3027 Gl.disable `texture_2d;
3028 GlDraw.polygon_mode `both `line;
3029 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3030 GlDraw.rect (1., float (y + 1))
3031 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3032 GlDraw.polygon_mode `both `fill;
3033 GlDraw.color (1., 1., 1.);
3034 Gl.enable `texture_2d;
3037 let drawtabularstring s =
3038 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3039 if trusted
3040 then
3041 let tabpos = try String.index s '\t' with Not_found -> -1 in
3042 if tabpos > 0
3043 then
3044 let len = String.length s - tabpos - 1 in
3045 let s1 = String.sub s 0 tabpos
3046 and s2 = String.sub s (tabpos + 1) len in
3047 let nx = drawstr x s1 in
3048 let sw = nx -. x in
3049 let x = x +. (max tabw sw) in
3050 drawstr x s2
3051 else
3052 drawstr x s
3053 else
3054 drawstr x s
3056 let _ = drawtabularstring s in
3057 loop (row+1)
3061 loop m_first;
3062 Gl.disable `blend;
3063 Gl.disable `texture_2d;
3065 method updownlevel incr =
3066 let len = source#getitemcount in
3067 let curlevel =
3068 if m_active >= 0 && m_active < len
3069 then snd (source#getitem m_active)
3070 else -1
3072 let rec flow i =
3073 if i = len then i-1 else if i = -1 then 0 else
3074 let _, l = source#getitem i in
3075 if l != curlevel then i else flow (i+incr)
3077 let active = flow m_active in
3078 let first = calcfirst m_first active in
3079 G.postRedisplay "outline updownlevel";
3080 {< m_active = active; m_first = first >}
3082 method private key1 key mask =
3083 let set1 active first qsearch =
3084 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3086 let search active pattern incr =
3087 let dosearch re =
3088 let rec loop n =
3089 if n >= 0 && n < source#getitemcount
3090 then (
3091 let s, _ = source#getitem n in
3093 (try ignore (Str.search_forward re s 0); true
3094 with Not_found -> false)
3095 then Some n
3096 else loop (n + incr)
3098 else None
3100 loop active
3103 let re = Str.regexp_case_fold pattern in
3104 dosearch re
3105 with Failure s ->
3106 state.text <- s;
3107 None
3109 let itemcount = source#getitemcount in
3110 let find start incr =
3111 let rec find i =
3112 if i = -1 || i = itemcount
3113 then -1
3114 else (
3115 if source#hasaction i
3116 then i
3117 else find (i + incr)
3120 find start
3122 let set active first =
3123 let first = bound first 0 (itemcount - fstate.maxrows) in
3124 state.text <- "";
3125 coe {< m_active = active; m_first = first >}
3127 let navigate incr =
3128 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3129 let active, first =
3130 let incr1 = if incr > 0 then 1 else -1 in
3131 if isvisible m_first m_active
3132 then
3133 let next =
3134 let next = m_active + incr in
3135 let next =
3136 if next < 0 || next >= itemcount
3137 then -1
3138 else find next incr1
3140 if next = -1 || abs (m_active - next) > fstate.maxrows
3141 then -1
3142 else next
3144 if next = -1
3145 then
3146 let first = m_first + incr in
3147 let first = bound first 0 (itemcount - 1) in
3148 let next =
3149 let next = m_active + incr in
3150 let next = bound next 0 (itemcount - 1) in
3151 find next ~-incr1
3153 let active = if next = -1 then m_active else next in
3154 active, first
3155 else
3156 let first = min next m_first in
3157 let first =
3158 if abs (next - first) > fstate.maxrows
3159 then first + incr
3160 else first
3162 next, first
3163 else
3164 let first = m_first + incr in
3165 let first = bound first 0 (itemcount - 1) in
3166 let active =
3167 let next = m_active + incr in
3168 let next = bound next 0 (itemcount - 1) in
3169 let next = find next incr1 in
3170 let active =
3171 if next = -1 || abs (m_active - first) > fstate.maxrows
3172 then (
3173 let active = if m_active = -1 then next else m_active in
3174 active
3176 else next
3178 if isvisible first active
3179 then active
3180 else -1
3182 active, first
3184 G.postRedisplay "listview navigate";
3185 set active first;
3187 match key with
3188 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3189 let incr = if key = 0x72 then -1 else 1 in
3190 let active, first =
3191 match search (m_active + incr) m_qsearch incr with
3192 | None ->
3193 state.text <- m_qsearch ^ " [not found]";
3194 m_active, m_first
3195 | Some active ->
3196 state.text <- m_qsearch;
3197 active, firstof m_first active
3199 G.postRedisplay "listview ctrl-r/s";
3200 set1 active first m_qsearch;
3202 | 0xff08 -> (* backspace *)
3203 if String.length m_qsearch = 0
3204 then coe self
3205 else (
3206 let qsearch = withoutlastutf8 m_qsearch in
3207 let len = String.length qsearch in
3208 if len = 0
3209 then (
3210 state.text <- "";
3211 G.postRedisplay "listview empty qsearch";
3212 set1 m_active m_first "";
3214 else
3215 let active, first =
3216 match search m_active qsearch ~-1 with
3217 | None ->
3218 state.text <- qsearch ^ " [not found]";
3219 m_active, m_first
3220 | Some active ->
3221 state.text <- qsearch;
3222 active, firstof m_first active
3224 G.postRedisplay "listview backspace qsearch";
3225 set1 active first qsearch
3228 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3229 let pattern = m_qsearch ^ Wsi.toutf8 key in
3230 let active, first =
3231 match search m_active pattern 1 with
3232 | None ->
3233 state.text <- pattern ^ " [not found]";
3234 m_active, m_first
3235 | Some active ->
3236 state.text <- pattern;
3237 active, firstof m_first active
3239 G.postRedisplay "listview qsearch add";
3240 set1 active first pattern;
3242 | 0xff1b -> (* escape *)
3243 state.text <- "";
3244 if String.length m_qsearch = 0
3245 then (
3246 G.postRedisplay "list view escape";
3247 begin
3248 match
3249 source#exit (coe self) true m_active m_first m_pan m_qsearch
3250 with
3251 | None -> m_prev_uioh
3252 | Some uioh -> uioh
3255 else (
3256 G.postRedisplay "list view kill qsearch";
3257 source#setqsearch "";
3258 coe {< m_qsearch = "" >}
3261 | 0xff0d -> (* return *)
3262 state.text <- "";
3263 let self = {< m_qsearch = "" >} in
3264 source#setqsearch "";
3265 let opt =
3266 G.postRedisplay "listview enter";
3267 if m_active >= 0 && m_active < source#getitemcount
3268 then (
3269 source#exit (coe self) false m_active m_first m_pan "";
3271 else (
3272 source#exit (coe self) true m_active m_first m_pan "";
3275 begin match opt with
3276 | None -> m_prev_uioh
3277 | Some uioh -> uioh
3280 | 0xff9f | 0xffff -> (* delete *)
3281 coe self
3283 | 0xff52 -> navigate ~-1 (* up *)
3284 | 0xff54 -> navigate 1 (* down *)
3285 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3286 | 0xff56 -> navigate fstate.maxrows (* next *)
3288 | 0xff53 -> (* right *)
3289 state.text <- "";
3290 G.postRedisplay "listview right";
3291 coe {< m_pan = m_pan - 1 >}
3293 | 0xff51 -> (* left *)
3294 state.text <- "";
3295 G.postRedisplay "listview left";
3296 coe {< m_pan = m_pan + 1 >}
3298 | 0xff50 -> (* home *)
3299 let active = find 0 1 in
3300 G.postRedisplay "listview home";
3301 set active 0;
3303 | 0xff57 -> (* end *)
3304 let first = max 0 (itemcount - fstate.maxrows) in
3305 let active = find (itemcount - 1) ~-1 in
3306 G.postRedisplay "listview end";
3307 set active first;
3309 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3310 coe self
3312 | _ ->
3313 dolog "listview unknown key %#x" key; coe self
3315 method key key mask =
3316 match state.mode with
3317 | Textentry te -> textentrykeyboard key mask te; coe self
3318 | _ -> self#key1 key mask
3320 method button button down x y _ =
3321 let opt =
3322 match button with
3323 | 1 when x > conf.winw - conf.scrollbw ->
3324 G.postRedisplay "listview scroll";
3325 if down
3326 then
3327 let _, position, sh = self#scrollph in
3328 if y > truncate position && y < truncate (position +. sh)
3329 then (
3330 state.mstate <- Mscrolly;
3331 Some (coe self)
3333 else
3334 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3335 let first = truncate (s *. float source#getitemcount) in
3336 let first = min source#getitemcount first in
3337 Some (coe {< m_first = first; m_active = first >})
3338 else (
3339 state.mstate <- Mnone;
3340 Some (coe self);
3342 | 1 when not down ->
3343 begin match self#elemunder y with
3344 | Some n ->
3345 G.postRedisplay "listview click";
3346 source#exit
3347 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3348 | _ ->
3349 Some (coe self)
3351 | n when (n == 4 || n == 5) && not down ->
3352 let len = source#getitemcount in
3353 let first =
3354 if n = 5 && m_first + fstate.maxrows >= len
3355 then
3356 m_first
3357 else
3358 let first = m_first + (if n == 4 then -1 else 1) in
3359 bound first 0 (len - 1)
3361 G.postRedisplay "listview wheel";
3362 Some (coe {< m_first = first >})
3363 | n when (n = 6 || n = 7) && not down ->
3364 let inc = m_first + (if n = 7 then -1 else 1) in
3365 G.postRedisplay "listview hwheel";
3366 Some (coe {< m_pan = m_pan + inc >})
3367 | _ ->
3368 Some (coe self)
3370 match opt with
3371 | None -> m_prev_uioh
3372 | Some uioh -> uioh
3374 method motion _ y =
3375 match state.mstate with
3376 | Mscrolly ->
3377 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3378 let first = truncate (s *. float source#getitemcount) in
3379 let first = min source#getitemcount first in
3380 G.postRedisplay "listview motion";
3381 coe {< m_first = first; m_active = first >}
3382 | _ -> coe self
3384 method pmotion x y =
3385 if x < conf.winw - conf.scrollbw
3386 then
3387 let n =
3388 match self#elemunder y with
3389 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3390 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3392 let o =
3393 if n != m_active
3394 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3395 else self
3397 coe o
3398 else (
3399 Wsi.setcursor Wsi.CURSOR_INHERIT;
3400 coe self
3403 method infochanged _ = ()
3405 method scrollpw = (0, 0.0, 0.0)
3406 method scrollph =
3407 let nfs = fstate.fontsize + 1 in
3408 let y = m_first * nfs in
3409 let itemcount = source#getitemcount in
3410 let maxi = max 0 (itemcount - fstate.maxrows) in
3411 let maxy = maxi * nfs in
3412 let p, h = scrollph y maxy in
3413 conf.scrollbw, p, h
3415 method modehash = modehash
3416 end;;
3418 class outlinelistview ~source =
3419 object (self)
3420 inherit listview
3421 ~source:(source :> lvsource)
3422 ~trusted:false
3423 ~modehash:(findkeyhash conf "outline")
3424 as super
3426 method key key mask =
3427 let calcfirst first active =
3428 if active > first
3429 then
3430 let rows = active - first in
3431 let maxrows =
3432 if String.length state.text = 0
3433 then fstate.maxrows
3434 else fstate.maxrows - 2
3436 if rows > maxrows then active - maxrows else first
3437 else active
3439 let navigate incr =
3440 let active = m_active + incr in
3441 let active = bound active 0 (source#getitemcount - 1) in
3442 let first = calcfirst m_first active in
3443 G.postRedisplay "outline navigate";
3444 coe {< m_active = active; m_first = first >}
3446 let ctrl = Wsi.withctrl mask in
3447 match key with
3448 | 110 when ctrl -> (* ctrl-n *)
3449 source#narrow m_qsearch;
3450 G.postRedisplay "outline ctrl-n";
3451 coe {< m_first = 0; m_active = 0 >}
3453 | 117 when ctrl -> (* ctrl-u *)
3454 source#denarrow;
3455 G.postRedisplay "outline ctrl-u";
3456 state.text <- "";
3457 coe {< m_first = 0; m_active = 0 >}
3459 | 108 when ctrl -> (* ctrl-l *)
3460 let first = m_active - (fstate.maxrows / 2) in
3461 G.postRedisplay "outline ctrl-l";
3462 coe {< m_first = first >}
3464 | 0xff9f | 0xffff -> (* delete *)
3465 source#remove m_active;
3466 G.postRedisplay "outline delete";
3467 let active = max 0 (m_active-1) in
3468 coe {< m_first = firstof m_first active;
3469 m_active = active >}
3471 | 0xff52 -> navigate ~-1 (* up *)
3472 | 0xff54 -> navigate 1 (* down *)
3473 | 0xff55 -> (* prior *)
3474 navigate ~-(fstate.maxrows)
3475 | 0xff56 -> (* next *)
3476 navigate fstate.maxrows
3478 | 0xff53 -> (* [ctrl-]right *)
3479 let o =
3480 if ctrl
3481 then (
3482 G.postRedisplay "outline ctrl right";
3483 {< m_pan = m_pan + 1 >}
3485 else self#updownlevel 1
3487 coe o
3489 | 0xff51 -> (* [ctrl-]left *)
3490 let o =
3491 if ctrl
3492 then (
3493 G.postRedisplay "outline ctrl left";
3494 {< m_pan = m_pan - 1 >}
3496 else self#updownlevel ~-1
3498 coe o
3500 | 0xff50 -> (* home *)
3501 G.postRedisplay "outline home";
3502 coe {< m_first = 0; m_active = 0 >}
3504 | 0xff57 -> (* end *)
3505 let active = source#getitemcount - 1 in
3506 let first = max 0 (active - fstate.maxrows) in
3507 G.postRedisplay "outline end";
3508 coe {< m_active = active; m_first = first >}
3510 | _ -> super#key key mask
3513 let outlinesource usebookmarks =
3514 let empty = [||] in
3515 (object
3516 inherit lvsourcebase
3517 val mutable m_items = empty
3518 val mutable m_orig_items = empty
3519 val mutable m_prev_items = empty
3520 val mutable m_narrow_pattern = ""
3521 val mutable m_hadremovals = false
3523 method getitemcount =
3524 Array.length m_items + (if m_hadremovals then 1 else 0)
3526 method getitem n =
3527 if n == Array.length m_items && m_hadremovals
3528 then
3529 ("[Confirm removal]", 0)
3530 else
3531 let s, n, _ = m_items.(n) in
3532 (s, n)
3534 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3535 ignore (uioh, first, qsearch);
3536 let confrimremoval = m_hadremovals && active = Array.length m_items in
3537 let items =
3538 if String.length m_narrow_pattern = 0
3539 then m_orig_items
3540 else m_items
3542 if not cancel
3543 then (
3544 if not confrimremoval
3545 then(
3546 let _, _, anchor = m_items.(active) in
3547 gotoanchor anchor;
3548 m_items <- items;
3550 else (
3551 state.bookmarks <- Array.to_list m_items;
3552 m_orig_items <- m_items;
3555 else m_items <- items;
3556 m_pan <- pan;
3557 None
3559 method hasaction _ = true
3561 method greetmsg =
3562 if Array.length m_items != Array.length m_orig_items
3563 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3564 else ""
3566 method narrow pattern =
3567 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3568 match reopt with
3569 | None -> ()
3570 | Some re ->
3571 let rec loop accu n =
3572 if n = -1
3573 then (
3574 m_narrow_pattern <- pattern;
3575 m_items <- Array.of_list accu
3577 else
3578 let (s, _, _) as o = m_items.(n) in
3579 let accu =
3580 if (try ignore (Str.search_forward re s 0); true
3581 with Not_found -> false)
3582 then o :: accu
3583 else accu
3585 loop accu (n-1)
3587 loop [] (Array.length m_items - 1)
3589 method denarrow =
3590 m_orig_items <- (
3591 if usebookmarks
3592 then Array.of_list state.bookmarks
3593 else state.outlines
3595 m_items <- m_orig_items
3597 method remove m =
3598 if usebookmarks
3599 then
3600 if m >= 0 && m < Array.length m_items
3601 then (
3602 m_hadremovals <- true;
3603 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3604 let n = if n >= m then n+1 else n in
3605 m_items.(n)
3609 method reset anchor items =
3610 m_hadremovals <- false;
3611 if m_orig_items == empty || m_prev_items != items
3612 then (
3613 m_orig_items <- items;
3614 if String.length m_narrow_pattern = 0
3615 then m_items <- items;
3617 m_prev_items <- items;
3618 let rely = getanchory anchor in
3619 let active =
3620 let rec loop n best bestd =
3621 if n = Array.length m_items
3622 then best
3623 else
3624 let (_, _, anchor) = m_items.(n) in
3625 let orely = getanchory anchor in
3626 let d = abs (orely - rely) in
3627 if d < bestd
3628 then loop (n+1) n d
3629 else loop (n+1) best bestd
3631 loop 0 ~-1 max_int
3633 m_active <- active;
3634 m_first <- firstof m_first active
3635 end)
3638 let enterselector usebookmarks =
3639 let source = outlinesource usebookmarks in
3640 fun errmsg ->
3641 let outlines =
3642 if usebookmarks
3643 then Array.of_list state.bookmarks
3644 else state.outlines
3646 if Array.length outlines = 0
3647 then (
3648 showtext ' ' errmsg;
3650 else (
3651 state.text <- source#greetmsg;
3652 Wsi.setcursor Wsi.CURSOR_INHERIT;
3653 let anchor = getanchor () in
3654 source#reset anchor outlines;
3655 state.uioh <- coe (new outlinelistview ~source);
3656 G.postRedisplay "enter selector";
3660 let enteroutlinemode =
3661 let f = enterselector false in
3662 fun ()-> f "Document has no outline";
3665 let enterbookmarkmode =
3666 let f = enterselector true in
3667 fun () -> f "Document has no bookmarks (yet)";
3670 let color_of_string s =
3671 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3672 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3676 let color_to_string (r, g, b) =
3677 let r = truncate (r *. 256.0)
3678 and g = truncate (g *. 256.0)
3679 and b = truncate (b *. 256.0) in
3680 Printf.sprintf "%d/%d/%d" r g b
3683 let irect_of_string s =
3684 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3687 let irect_to_string (x0,y0,x1,y1) =
3688 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3691 let makecheckers () =
3692 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3693 following to say:
3694 converted by Issac Trotts. July 25, 2002 *)
3695 let image_height = 64
3696 and image_width = 64 in
3698 let make_image () =
3699 let image =
3700 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3702 for i = 0 to image_width - 1 do
3703 for j = 0 to image_height - 1 do
3704 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3705 (if (i land 8 ) lxor (j land 8) = 0
3706 then [|255;255;255|] else [|200;200;200|])
3707 done
3708 done;
3709 image
3711 let image = make_image () in
3712 let id = GlTex.gen_texture () in
3713 GlTex.bind_texture `texture_2d id;
3714 GlPix.store (`unpack_alignment 1);
3715 GlTex.image2d image;
3716 List.iter (GlTex.parameter ~target:`texture_2d)
3717 [ `wrap_s `repeat;
3718 `wrap_t `repeat;
3719 `mag_filter `nearest;
3720 `min_filter `nearest ];
3724 let setcheckers enabled =
3725 match state.texid with
3726 | None ->
3727 if enabled then state.texid <- Some (makecheckers ())
3729 | Some texid ->
3730 if not enabled
3731 then (
3732 GlTex.delete_texture texid;
3733 state.texid <- None;
3737 let int_of_string_with_suffix s =
3738 let l = String.length s in
3739 let s1, shift =
3740 if l > 1
3741 then
3742 let suffix = Char.lowercase s.[l-1] in
3743 match suffix with
3744 | 'k' -> String.sub s 0 (l-1), 10
3745 | 'm' -> String.sub s 0 (l-1), 20
3746 | 'g' -> String.sub s 0 (l-1), 30
3747 | _ -> s, 0
3748 else s, 0
3750 let n = int_of_string s1 in
3751 let m = n lsl shift in
3752 if m < 0 || m < n
3753 then raise (Failure "value too large")
3754 else m
3757 let string_with_suffix_of_int n =
3758 if n = 0
3759 then "0"
3760 else
3761 let n, s =
3762 if n land ((1 lsl 20) - 1) = 0
3763 then n lsr 20, "M"
3764 else (
3765 if n land ((1 lsl 10) - 1) = 0
3766 then n lsr 10, "K"
3767 else n, ""
3770 let rec loop s n =
3771 let h = n mod 1000 in
3772 let n = n / 1000 in
3773 if n = 0
3774 then string_of_int h ^ s
3775 else (
3776 let s = Printf.sprintf "_%03d%s" h s in
3777 loop s n
3780 loop "" n ^ s;
3783 let defghyllscroll = (40, 8, 32);;
3784 let ghyllscroll_of_string s =
3785 let (n, a, b) as nab =
3786 if s = "default"
3787 then defghyllscroll
3788 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3790 if n <= a || n <= b || a >= b
3791 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3792 nab;
3795 let ghyllscroll_to_string ((n, a, b) as nab) =
3796 if nab = defghyllscroll
3797 then "default"
3798 else Printf.sprintf "%d,%d,%d" n a b;
3801 let describe_location () =
3802 let f (fn, _) l =
3803 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3805 let fn, ln = List.fold_left f (-1, -1) state.layout in
3806 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3807 let percent =
3808 if maxy <= 0
3809 then 100.
3810 else (100. *. (float state.y /. float maxy))
3812 if fn = ln
3813 then
3814 Printf.sprintf "page %d of %d [%.2f%%]"
3815 (fn+1) state.pagecount percent
3816 else
3817 Printf.sprintf
3818 "pages %d-%d of %d [%.2f%%]"
3819 (fn+1) (ln+1) state.pagecount percent
3822 let enterinfomode =
3823 let btos b = if b then "\xe2\x88\x9a" else "" in
3824 let showextended = ref false in
3825 let leave mode = function
3826 | Confirm -> state.mode <- mode
3827 | Cancel -> state.mode <- mode in
3828 let src =
3829 (object
3830 val mutable m_first_time = true
3831 val mutable m_l = []
3832 val mutable m_a = [||]
3833 val mutable m_prev_uioh = nouioh
3834 val mutable m_prev_mode = View
3836 inherit lvsourcebase
3838 method reset prev_mode prev_uioh =
3839 m_a <- Array.of_list (List.rev m_l);
3840 m_l <- [];
3841 m_prev_mode <- prev_mode;
3842 m_prev_uioh <- prev_uioh;
3843 if m_first_time
3844 then (
3845 let rec loop n =
3846 if n >= Array.length m_a
3847 then ()
3848 else
3849 match m_a.(n) with
3850 | _, _, _, Action _ -> m_active <- n
3851 | _ -> loop (n+1)
3853 loop 0;
3854 m_first_time <- false;
3857 method int name get set =
3858 m_l <-
3859 (name, `int get, 1, Action (
3860 fun u ->
3861 let ondone s =
3862 try set (int_of_string s)
3863 with exn ->
3864 state.text <- Printf.sprintf "bad integer `%s': %s"
3865 s (Printexc.to_string exn)
3867 state.text <- "";
3868 let te = name ^ ": ", "", None, intentry, ondone, true in
3869 state.mode <- Textentry (te, leave m_prev_mode);
3871 )) :: m_l
3873 method int_with_suffix name get set =
3874 m_l <-
3875 (name, `intws get, 1, Action (
3876 fun u ->
3877 let ondone s =
3878 try set (int_of_string_with_suffix s)
3879 with exn ->
3880 state.text <- Printf.sprintf "bad integer `%s': %s"
3881 s (Printexc.to_string exn)
3883 state.text <- "";
3884 let te =
3885 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3887 state.mode <- Textentry (te, leave m_prev_mode);
3889 )) :: m_l
3891 method bool ?(offset=1) ?(btos=btos) name get set =
3892 m_l <-
3893 (name, `bool (btos, get), offset, Action (
3894 fun u ->
3895 let v = get () in
3896 set (not v);
3898 )) :: m_l
3900 method color name get set =
3901 m_l <-
3902 (name, `color get, 1, Action (
3903 fun u ->
3904 let invalid = (nan, nan, nan) in
3905 let ondone s =
3906 let c =
3907 try color_of_string s
3908 with exn ->
3909 state.text <- Printf.sprintf "bad color `%s': %s"
3910 s (Printexc.to_string exn);
3911 invalid
3913 if c <> invalid
3914 then set c;
3916 let te = name ^ ": ", "", None, textentry, ondone, true in
3917 state.text <- color_to_string (get ());
3918 state.mode <- Textentry (te, leave m_prev_mode);
3920 )) :: m_l
3922 method string name get set =
3923 m_l <-
3924 (name, `string get, 1, Action (
3925 fun u ->
3926 let ondone s = set s in
3927 let te = name ^ ": ", "", None, textentry, ondone, true in
3928 state.mode <- Textentry (te, leave m_prev_mode);
3930 )) :: m_l
3932 method colorspace name get set =
3933 m_l <-
3934 (name, `string get, 1, Action (
3935 fun _ ->
3936 let source =
3937 let vals = [| "rgb"; "bgr"; "gray" |] in
3938 (object
3939 inherit lvsourcebase
3941 initializer
3942 m_active <- int_of_colorspace conf.colorspace;
3943 m_first <- 0;
3945 method getitemcount = Array.length vals
3946 method getitem n = (vals.(n), 0)
3947 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3948 ignore (uioh, first, pan, qsearch);
3949 if not cancel then set active;
3950 None
3951 method hasaction _ = true
3952 end)
3954 state.text <- "";
3955 let modehash = findkeyhash conf "info" in
3956 coe (new listview ~source ~trusted:true ~modehash)
3957 )) :: m_l
3959 method caption s offset =
3960 m_l <- (s, `empty, offset, Noaction) :: m_l
3962 method caption2 s f offset =
3963 m_l <- (s, `string f, offset, Noaction) :: m_l
3965 method getitemcount = Array.length m_a
3967 method getitem n =
3968 let tostr = function
3969 | `int f -> string_of_int (f ())
3970 | `intws f -> string_with_suffix_of_int (f ())
3971 | `string f -> f ()
3972 | `color f -> color_to_string (f ())
3973 | `bool (btos, f) -> btos (f ())
3974 | `empty -> ""
3976 let name, t, offset, _ = m_a.(n) in
3977 ((let s = tostr t in
3978 if String.length s > 0
3979 then Printf.sprintf "%s\t%s" name s
3980 else name),
3981 offset)
3983 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3984 let uiohopt =
3985 if not cancel
3986 then (
3987 m_qsearch <- qsearch;
3988 let uioh =
3989 match m_a.(active) with
3990 | _, _, _, Action f -> f uioh
3991 | _ -> uioh
3993 Some uioh
3995 else None
3997 m_active <- active;
3998 m_first <- first;
3999 m_pan <- pan;
4000 uiohopt
4002 method hasaction n =
4003 match m_a.(n) with
4004 | _, _, _, Action _ -> true
4005 | _ -> false
4006 end)
4008 let rec fillsrc prevmode prevuioh =
4009 let sep () = src#caption "" 0 in
4010 let colorp name get set =
4011 src#string name
4012 (fun () -> color_to_string (get ()))
4013 (fun v ->
4015 let c = color_of_string v in
4016 set c
4017 with exn ->
4018 state.text <- Printf.sprintf "bad color `%s': %s"
4019 v (Printexc.to_string exn);
4022 let oldmode = state.mode in
4023 let birdseye = isbirdseye state.mode in
4025 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4027 src#bool "presentation mode"
4028 (fun () -> conf.presentation)
4029 (fun v ->
4030 conf.presentation <- v;
4031 state.anchor <- getanchor ();
4032 represent ());
4034 src#bool "ignore case in searches"
4035 (fun () -> conf.icase)
4036 (fun v -> conf.icase <- v);
4038 src#bool "preload"
4039 (fun () -> conf.preload)
4040 (fun v -> conf.preload <- v);
4042 src#bool "highlight links"
4043 (fun () -> conf.hlinks)
4044 (fun v -> conf.hlinks <- v);
4046 src#bool "under info"
4047 (fun () -> conf.underinfo)
4048 (fun v -> conf.underinfo <- v);
4050 src#bool "persistent bookmarks"
4051 (fun () -> conf.savebmarks)
4052 (fun v -> conf.savebmarks <- v);
4054 src#bool "proportional display"
4055 (fun () -> conf.proportional)
4056 (fun v -> reqlayout conf.angle v);
4058 src#bool "trim margins"
4059 (fun () -> conf.trimmargins)
4060 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4062 src#bool "persistent location"
4063 (fun () -> conf.jumpback)
4064 (fun v -> conf.jumpback <- v);
4066 sep ();
4067 src#int "inter-page space"
4068 (fun () -> conf.interpagespace)
4069 (fun n ->
4070 conf.interpagespace <- n;
4071 docolumns conf.columns;
4072 let pageno, py =
4073 match state.layout with
4074 | [] -> 0, 0
4075 | l :: _ ->
4076 l.pageno, l.pagey
4078 state.maxy <- calcheight ();
4079 let y = getpagey pageno in
4080 gotoy (y + py)
4083 src#int "page bias"
4084 (fun () -> conf.pagebias)
4085 (fun v -> conf.pagebias <- v);
4087 src#int "scroll step"
4088 (fun () -> conf.scrollstep)
4089 (fun n -> conf.scrollstep <- n);
4091 src#int "horizontal scroll step"
4092 (fun () -> conf.hscrollstep)
4093 (fun v -> conf.hscrollstep <- v);
4095 src#int "auto scroll step"
4096 (fun () ->
4097 match state.autoscroll with
4098 | Some step -> step
4099 | _ -> conf.autoscrollstep)
4100 (fun n ->
4101 if state.autoscroll <> None
4102 then state.autoscroll <- Some n;
4103 conf.autoscrollstep <- n);
4105 src#int "zoom"
4106 (fun () -> truncate (conf.zoom *. 100.))
4107 (fun v -> setzoom ((float v) /. 100.));
4109 src#int "rotation"
4110 (fun () -> conf.angle)
4111 (fun v -> reqlayout v conf.proportional);
4113 src#int "scroll bar width"
4114 (fun () -> state.scrollw)
4115 (fun v ->
4116 state.scrollw <- v;
4117 conf.scrollbw <- v;
4118 reshape conf.winw conf.winh;
4121 src#int "scroll handle height"
4122 (fun () -> conf.scrollh)
4123 (fun v -> conf.scrollh <- v;);
4125 src#int "thumbnail width"
4126 (fun () -> conf.thumbw)
4127 (fun v ->
4128 conf.thumbw <- min 4096 v;
4129 match oldmode with
4130 | Birdseye beye ->
4131 leavebirdseye beye false;
4132 enterbirdseye ()
4133 | _ -> ()
4136 let mode = state.mode in
4137 src#string "columns"
4138 (fun () ->
4139 match conf.columns with
4140 | Csingle _ -> "1"
4141 | Cmulti (multi, _) -> multicolumns_to_string multi
4142 | Csplit (count, _) -> "-" ^ string_of_int count
4144 (fun v ->
4145 let n, a, b = multicolumns_of_string v in
4146 setcolumns mode n a b);
4148 sep ();
4149 src#caption "Presentation mode" 0;
4150 src#bool "scrollbar visible"
4151 (fun () -> conf.scrollbarinpm)
4152 (fun v ->
4153 if v != conf.scrollbarinpm
4154 then (
4155 conf.scrollbarinpm <- v;
4156 if conf.presentation
4157 then (
4158 state.scrollw <- if v then conf.scrollbw else 0;
4159 reshape conf.winw conf.winh;
4164 sep ();
4165 src#caption "Pixmap cache" 0;
4166 src#int_with_suffix "size (advisory)"
4167 (fun () -> conf.memlimit)
4168 (fun v -> conf.memlimit <- v);
4170 src#caption2 "used"
4171 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4172 (string_with_suffix_of_int state.memused)
4173 (Hashtbl.length state.tilemap)) 1;
4175 sep ();
4176 src#caption "Layout" 0;
4177 src#caption2 "Dimension"
4178 (fun () ->
4179 Printf.sprintf "%dx%d (virtual %dx%d) [pos %d]"
4180 conf.winw conf.winh
4181 state.w state.maxy state.y)
4183 if conf.debug
4184 then
4185 src#caption2 "Position" (fun () ->
4186 Printf.sprintf "%dx%d" state.x state.y
4188 else
4189 src#caption2 "Visible" (fun () -> describe_location ()) 1
4192 sep ();
4193 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4194 "Save these parameters as global defaults at exit"
4195 (fun () -> conf.bedefault)
4196 (fun v -> conf.bedefault <- v)
4199 sep ();
4200 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4201 src#bool ~offset:0 ~btos "Extended parameters"
4202 (fun () -> !showextended)
4203 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4204 if !showextended
4205 then (
4206 src#bool "checkers"
4207 (fun () -> conf.checkers)
4208 (fun v -> conf.checkers <- v; setcheckers v);
4209 src#bool "update cursor"
4210 (fun () -> conf.updatecurs)
4211 (fun v -> conf.updatecurs <- v);
4212 src#bool "verbose"
4213 (fun () -> conf.verbose)
4214 (fun v -> conf.verbose <- v);
4215 src#bool "invert colors"
4216 (fun () -> conf.invert)
4217 (fun v -> conf.invert <- v);
4218 src#bool "max fit"
4219 (fun () -> conf.maxhfit)
4220 (fun v -> conf.maxhfit <- v);
4221 src#bool "redirect stderr"
4222 (fun () -> conf.redirectstderr)
4223 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4224 src#string "uri launcher"
4225 (fun () -> conf.urilauncher)
4226 (fun v -> conf.urilauncher <- v);
4227 src#string "path launcher"
4228 (fun () -> conf.pathlauncher)
4229 (fun v -> conf.pathlauncher <- v);
4230 src#string "tile size"
4231 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4232 (fun v ->
4234 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4235 conf.tilew <- max 64 w;
4236 conf.tileh <- max 64 h;
4237 flushtiles ();
4238 with exn ->
4239 state.text <- Printf.sprintf "bad tile size `%s': %s"
4240 v (Printexc.to_string exn));
4241 src#int "texture count"
4242 (fun () -> conf.texcount)
4243 (fun v ->
4244 if realloctexts v
4245 then conf.texcount <- v
4246 else showtext '!' " Failed to set texture count please retry later"
4248 src#int "slice height"
4249 (fun () -> conf.sliceheight)
4250 (fun v ->
4251 conf.sliceheight <- v;
4252 wcmd "sliceh %d" conf.sliceheight;
4254 src#int "anti-aliasing level"
4255 (fun () -> conf.aalevel)
4256 (fun v ->
4257 conf.aalevel <- bound v 0 8;
4258 state.anchor <- getanchor ();
4259 opendoc state.path state.password;
4261 src#string "page scroll scaling factor"
4262 (fun () -> string_of_float conf.pgscale)
4263 (fun v ->
4265 let s = float_of_string v in
4266 conf.pgscale <- s
4267 with exn ->
4268 state.text <- Printf.sprintf
4269 "bad page scroll scaling factor `%s': %s"
4270 v (Printexc.to_string exn)
4273 src#int "ui font size"
4274 (fun () -> fstate.fontsize)
4275 (fun v -> setfontsize (bound v 5 100));
4276 src#int "hint font size"
4277 (fun () -> conf.hfsize)
4278 (fun v -> conf.hfsize <- bound v 5 100);
4279 colorp "background color"
4280 (fun () -> conf.bgcolor)
4281 (fun v -> conf.bgcolor <- v);
4282 src#bool "crop hack"
4283 (fun () -> conf.crophack)
4284 (fun v -> conf.crophack <- v);
4285 src#string "trim fuzz"
4286 (fun () -> irect_to_string conf.trimfuzz)
4287 (fun v ->
4289 conf.trimfuzz <- irect_of_string v;
4290 if conf.trimmargins
4291 then settrim true conf.trimfuzz;
4292 with exn ->
4293 state.text <- Printf.sprintf "bad irect `%s': %s"
4294 v (Printexc.to_string exn)
4296 src#string "throttle"
4297 (fun () ->
4298 match conf.maxwait with
4299 | None -> "show place holder if page is not ready"
4300 | Some time ->
4301 if time = infinity
4302 then "wait for page to fully render"
4303 else
4304 "wait " ^ string_of_float time
4305 ^ " seconds before showing placeholder"
4307 (fun v ->
4309 let f = float_of_string v in
4310 if f <= 0.0
4311 then conf.maxwait <- None
4312 else conf.maxwait <- Some f
4313 with exn ->
4314 state.text <- Printf.sprintf "bad time `%s': %s"
4315 v (Printexc.to_string exn)
4317 src#string "ghyll scroll"
4318 (fun () ->
4319 match conf.ghyllscroll with
4320 | None -> ""
4321 | Some nab -> ghyllscroll_to_string nab
4323 (fun v ->
4325 let gs =
4326 if String.length v = 0
4327 then None
4328 else Some (ghyllscroll_of_string v)
4330 conf.ghyllscroll <- gs
4331 with exn ->
4332 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4333 v (Printexc.to_string exn)
4335 src#string "selection command"
4336 (fun () -> conf.selcmd)
4337 (fun v -> conf.selcmd <- v);
4338 src#colorspace "color space"
4339 (fun () -> colorspace_to_string conf.colorspace)
4340 (fun v ->
4341 conf.colorspace <- colorspace_of_int v;
4342 wcmd "cs %d" v;
4343 load state.layout;
4347 sep ();
4348 src#caption "Document" 0;
4349 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4350 src#caption2 "Pages"
4351 (fun () -> string_of_int state.pagecount) 1;
4352 src#caption2 "Dimensions"
4353 (fun () -> string_of_int (List.length state.pdims)) 1;
4354 if conf.trimmargins
4355 then (
4356 sep ();
4357 src#caption "Trimmed margins" 0;
4358 src#caption2 "Dimensions"
4359 (fun () -> string_of_int (List.length state.pdims)) 1;
4362 sep ();
4363 src#caption "OpenGL" 0;
4364 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4365 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4366 src#reset prevmode prevuioh;
4368 fun () ->
4369 state.text <- "";
4370 let prevmode = state.mode
4371 and prevuioh = state.uioh in
4372 fillsrc prevmode prevuioh;
4373 let source = (src :> lvsource) in
4374 let modehash = findkeyhash conf "info" in
4375 state.uioh <- coe (object (self)
4376 inherit listview ~source ~trusted:true ~modehash as super
4377 val mutable m_prevmemused = 0
4378 method infochanged = function
4379 | Memused ->
4380 if m_prevmemused != state.memused
4381 then (
4382 m_prevmemused <- state.memused;
4383 G.postRedisplay "memusedchanged";
4385 | Pdim -> G.postRedisplay "pdimchanged"
4386 | Docinfo -> fillsrc prevmode prevuioh
4388 method key key mask =
4389 if not (Wsi.withctrl mask)
4390 then
4391 match key with
4392 | 0xff51 -> coe (self#updownlevel ~-1)
4393 | 0xff53 -> coe (self#updownlevel 1)
4394 | _ -> super#key key mask
4395 else super#key key mask
4396 end);
4397 G.postRedisplay "info";
4400 let enterhelpmode =
4401 let source =
4402 (object
4403 inherit lvsourcebase
4404 method getitemcount = Array.length state.help
4405 method getitem n =
4406 let s, n, _ = state.help.(n) in
4407 (s, n)
4409 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4410 let optuioh =
4411 if not cancel
4412 then (
4413 m_qsearch <- qsearch;
4414 match state.help.(active) with
4415 | _, _, Action f -> Some (f uioh)
4416 | _ -> Some (uioh)
4418 else None
4420 m_active <- active;
4421 m_first <- first;
4422 m_pan <- pan;
4423 optuioh
4425 method hasaction n =
4426 match state.help.(n) with
4427 | _, _, Action _ -> true
4428 | _ -> false
4430 initializer
4431 m_active <- -1
4432 end)
4433 in fun () ->
4434 let modehash = findkeyhash conf "help" in
4435 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4436 G.postRedisplay "help";
4439 let entermsgsmode =
4440 let msgsource =
4441 let re = Str.regexp "[\r\n]" in
4442 (object
4443 inherit lvsourcebase
4444 val mutable m_items = [||]
4446 method getitemcount = 1 + Array.length m_items
4448 method getitem n =
4449 if n = 0
4450 then "[Clear]", 0
4451 else m_items.(n-1), 0
4453 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4454 ignore uioh;
4455 if not cancel
4456 then (
4457 if active = 0
4458 then Buffer.clear state.errmsgs;
4459 m_qsearch <- qsearch;
4461 m_active <- active;
4462 m_first <- first;
4463 m_pan <- pan;
4464 None
4466 method hasaction n =
4467 n = 0
4469 method reset =
4470 state.newerrmsgs <- false;
4471 let l = Str.split re (Buffer.contents state.errmsgs) in
4472 m_items <- Array.of_list l
4474 initializer
4475 m_active <- 0
4476 end)
4477 in fun () ->
4478 state.text <- "";
4479 msgsource#reset;
4480 let source = (msgsource :> lvsource) in
4481 let modehash = findkeyhash conf "listview" in
4482 state.uioh <- coe (object
4483 inherit listview ~source ~trusted:false ~modehash as super
4484 method display =
4485 if state.newerrmsgs
4486 then msgsource#reset;
4487 super#display
4488 end);
4489 G.postRedisplay "msgs";
4492 let quickbookmark ?title () =
4493 match state.layout with
4494 | [] -> ()
4495 | l :: _ ->
4496 let title =
4497 match title with
4498 | None ->
4499 let sec = Unix.gettimeofday () in
4500 let tm = Unix.localtime sec in
4501 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4502 (l.pageno+1)
4503 tm.Unix.tm_mday
4504 tm.Unix.tm_mon
4505 (tm.Unix.tm_year + 1900)
4506 tm.Unix.tm_hour
4507 tm.Unix.tm_min
4508 | Some title -> title
4510 state.bookmarks <-
4511 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4512 :: state.bookmarks
4515 let doreshape w h =
4516 state.fullscreen <- None;
4517 Wsi.reshape w h;
4520 let setautoscrollspeed step goingdown =
4521 let incr = max 1 ((abs step) / 2) in
4522 let incr = if goingdown then incr else -incr in
4523 let astep = step + incr in
4524 state.autoscroll <- Some astep;
4527 let gotounder = function
4528 | Ulinkgoto (pageno, top) ->
4529 if pageno >= 0
4530 then (
4531 addnav ();
4532 gotopage1 pageno top;
4535 | Ulinkuri s ->
4536 gotouri s
4538 | Uremote (filename, pageno) ->
4539 let path =
4540 if Sys.file_exists filename
4541 then filename
4542 else
4543 let dir = Filename.dirname state.path in
4544 let path = Filename.concat dir filename in
4545 if Sys.file_exists path
4546 then path
4547 else ""
4549 if String.length path > 0
4550 then (
4551 let anchor = getanchor () in
4552 let ranchor = state.path, state.password, anchor in
4553 state.anchor <- (pageno, 0.0);
4554 state.ranchors <- ranchor :: state.ranchors;
4555 opendoc path "";
4557 else showtext '!' ("Could not find " ^ filename)
4559 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4562 let canpan () =
4563 match conf.columns with
4564 | Csplit _ -> true
4565 | _ -> conf.zoom > 1.0
4568 let viewkeyboard key mask =
4569 let enttext te =
4570 let mode = state.mode in
4571 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4572 state.text <- "";
4573 enttext ();
4574 G.postRedisplay "view:enttext"
4576 let ctrl = Wsi.withctrl mask in
4577 match key with
4578 | 81 -> (* Q *)
4579 exit 0
4581 | 0xff63 -> (* insert *)
4582 if conf.angle mod 360 = 0
4583 then (
4584 state.mode <- LinkNav (Ltgendir 0);
4585 gotoy state.y;
4587 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4589 | 0xff1b | 113 -> (* escape / q *)
4590 begin match state.mstate with
4591 | Mzoomrect _ ->
4592 state.mstate <- Mnone;
4593 Wsi.setcursor Wsi.CURSOR_INHERIT;
4594 G.postRedisplay "kill zoom rect";
4595 | _ ->
4596 match state.ranchors with
4597 | [] -> raise Quit
4598 | (path, password, anchor) :: rest ->
4599 state.ranchors <- rest;
4600 state.anchor <- anchor;
4601 opendoc path password
4602 end;
4604 | 0xff08 -> (* backspace *)
4605 let y = getnav ~-1 in
4606 gotoy_and_clear_text y
4608 | 111 -> (* o *)
4609 enteroutlinemode ()
4611 | 117 -> (* u *)
4612 state.rects <- [];
4613 state.text <- "";
4614 G.postRedisplay "dehighlight";
4616 | 47 | 63 -> (* / ? *)
4617 let ondone isforw s =
4618 cbput state.hists.pat s;
4619 state.searchpattern <- s;
4620 search s isforw
4622 let s = String.create 1 in
4623 s.[0] <- Char.chr key;
4624 enttext (s, "", Some (onhist state.hists.pat),
4625 textentry, ondone (key = 47), true)
4627 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-=*)
4628 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4629 setzoom (conf.zoom +. incr)
4631 | 43 | 0xffab -> (* + *)
4632 let ondone s =
4633 let n =
4634 try int_of_string s with exc ->
4635 state.text <- Printf.sprintf "bad integer `%s': %s"
4636 s (Printexc.to_string exc);
4637 max_int
4639 if n != max_int
4640 then (
4641 conf.pagebias <- n;
4642 state.text <- "page bias is now " ^ string_of_int n;
4645 enttext ("page bias: ", "", None, intentry, ondone, true)
4647 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4648 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4649 setzoom (max 0.01 (conf.zoom -. decr))
4651 | 45 | 0xffad -> (* - *)
4652 let ondone msg = state.text <- msg in
4653 enttext (
4654 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4655 optentry state.mode, ondone, true
4658 | 48 when ctrl -> (* ctrl-0 *)
4659 setzoom 1.0
4661 | 49 when ctrl -> (* ctrl-1 *)
4662 let cols =
4663 match conf.columns with
4664 | Csingle _ | Cmulti _ -> 1
4665 | Csplit (n, _) -> n
4667 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4668 if zoom < 1.0
4669 then setzoom zoom
4671 | 0xffc6 -> (* f9 *)
4672 togglebirdseye ()
4674 | 57 when ctrl -> (* ctrl-9 *)
4675 togglebirdseye ()
4677 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4678 when not ctrl -> (* 0..9 *)
4679 let ondone s =
4680 let n =
4681 try int_of_string s with exc ->
4682 state.text <- Printf.sprintf "bad integer `%s': %s"
4683 s (Printexc.to_string exc);
4686 if n >= 0
4687 then (
4688 addnav ();
4689 cbput state.hists.pag (string_of_int n);
4690 gotopage1 (n + conf.pagebias - 1) 0;
4693 let pageentry text key =
4694 match Char.unsafe_chr key with
4695 | 'g' -> TEdone text
4696 | _ -> intentry text key
4698 let text = "x" in text.[0] <- Char.chr key;
4699 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4701 | 98 -> (* b *)
4702 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4703 reshape conf.winw conf.winh;
4705 | 108 -> (* l *)
4706 conf.hlinks <- not conf.hlinks;
4707 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4708 G.postRedisplay "toggle highlightlinks";
4710 | 70 -> (* F *)
4711 state.glinks <- true;
4712 let mode = state.mode in
4713 state.mode <- Textentry (
4714 (":", "", None, linknentry, linkndone (fun under ->
4715 addnav ();
4716 gotounder under
4717 ), false
4718 ), fun _ ->
4719 state.glinks <- false;
4720 state.mode <- mode
4722 state.text <- "";
4723 G.postRedisplay "view:linkent(F)"
4725 | 121 -> (* y *)
4726 state.glinks <- true;
4727 let mode = state.mode in
4728 state.mode <- Textentry (
4729 (":", "", None, linknentry, linkndone (fun under ->
4730 match Ne.pipe () with
4731 | Ne.Exn exn ->
4732 showtext '!' (Printf.sprintf "pipe failed: %s"
4733 (Printexc.to_string exn));
4734 | Ne.Res (r, w) ->
4735 let popened =
4736 try popen conf.selcmd [r, 0; w, -1]; true
4737 with exn ->
4738 showtext '!'
4739 (Printf.sprintf "failed to execute %s: %s"
4740 conf.selcmd (Printexc.to_string exn));
4741 false
4743 let clo cap fd =
4744 Ne.clo fd (fun msg ->
4745 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4748 let s = undertext under in
4749 if popened
4750 then
4751 (try
4752 let l = String.length s in
4753 let n = Unix.write w s 0 l in
4754 if n != l
4755 then
4756 showtext '!'
4757 (Printf.sprintf
4758 "failed to write %d characters to sel pipe, wrote %d"
4761 with exn ->
4762 showtext '!'
4763 (Printf.sprintf "failed to write to sel pipe: %s"
4764 (Printexc.to_string exn)
4767 else dolog "%s" s;
4768 clo "pipe/r" r;
4769 clo "pipe/w" w;
4770 ), false
4772 fun _ ->
4773 state.glinks <- false;
4774 state.mode <- mode
4776 state.text <- "";
4777 G.postRedisplay "view:linkent"
4779 | 97 -> (* a *)
4780 begin match state.autoscroll with
4781 | Some step ->
4782 conf.autoscrollstep <- step;
4783 state.autoscroll <- None
4784 | None ->
4785 if conf.autoscrollstep = 0
4786 then state.autoscroll <- Some 1
4787 else state.autoscroll <- Some conf.autoscrollstep
4790 | 112 when ctrl -> (* ctrl-p *)
4791 launchpath ()
4793 | 80 -> (* P *)
4794 conf.presentation <- not conf.presentation;
4795 if conf.presentation
4796 then (
4797 if not conf.scrollbarinpm
4798 then state.scrollw <- 0;
4800 else
4801 state.scrollw <- conf.scrollbw;
4803 showtext ' ' ("presentation mode " ^
4804 if conf.presentation then "on" else "off");
4805 state.anchor <- getanchor ();
4806 represent ()
4808 | 102 -> (* f *)
4809 begin match state.fullscreen with
4810 | None ->
4811 state.fullscreen <- Some (conf.winw, conf.winh);
4812 Wsi.fullscreen ()
4813 | Some (w, h) ->
4814 state.fullscreen <- None;
4815 doreshape w h
4818 | 103 -> (* g *)
4819 gotoy_and_clear_text 0
4821 | 71 -> (* G *)
4822 gotopage1 (state.pagecount - 1) 0
4824 | 112 | 78 -> (* p|N *)
4825 search state.searchpattern false
4827 | 110 | 0xffc0 -> (* n|F3 *)
4828 search state.searchpattern true
4830 | 116 -> (* t *)
4831 begin match state.layout with
4832 | [] -> ()
4833 | l :: _ ->
4834 gotoy_and_clear_text (getpagey l.pageno)
4837 | 32 -> (* space *)
4838 begin match state.layout with
4839 | [] -> ()
4840 | l :: rest ->
4841 match conf.columns with
4842 | Csingle _ | Cmulti _ ->
4843 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4844 then
4845 let y = clamp (pgscale conf.winh) in
4846 gotoy_and_clear_text y
4847 else
4848 let pageno = min (l.pageno+1) (state.pagecount-1) in
4849 gotoy_and_clear_text (getpagey pageno)
4850 | Csplit (n, _) ->
4851 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4852 then
4853 let pagey, pageh = getpageyh l.pageno in
4854 let pagey = pagey + pageh * l.pagecol in
4855 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4856 gotoy_and_clear_text (pagey + pageh + ips)
4859 | 0xff9f | 0xffff -> (* delete *)
4860 begin match state.layout with
4861 | [] -> ()
4862 | l :: _ ->
4863 match conf.columns with
4864 | Csingle _ | Cmulti _ ->
4865 if conf.presentation && (l.pagey != 0 || l.pagedispy < 0)
4866 then
4867 gotoy_and_clear_text (clamp (pgscale ~-(conf.winh)))
4868 else
4869 let pageno = max 0 (l.pageno-1) in
4870 gotoy_and_clear_text (getpagey pageno)
4871 | Csplit (n, _) ->
4872 let y =
4873 if l.pagecol = 0
4874 then
4875 if l.pageno = 0
4876 then l.pagey
4877 else
4878 let pageno = max 0 (l.pageno-1) in
4879 let pagey, pageh = getpageyh pageno in
4880 pagey + (n-1)*pageh
4881 else
4882 let pagey, pageh = getpageyh l.pageno in
4883 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4885 gotoy_and_clear_text y
4888 | 61 -> (* = *)
4889 showtext ' ' (describe_location ());
4891 | 119 -> (* w *)
4892 begin match state.layout with
4893 | [] -> ()
4894 | l :: _ ->
4895 doreshape (l.pagew + state.scrollw) l.pageh;
4896 G.postRedisplay "w"
4899 | 39 -> (* ' *)
4900 enterbookmarkmode ()
4902 | 104 | 0xffbe -> (* h|F1 *)
4903 enterhelpmode ()
4905 | 105 -> (* i *)
4906 enterinfomode ()
4908 | 101 when conf.redirectstderr -> (* e *)
4909 entermsgsmode ()
4911 | 109 -> (* m *)
4912 let ondone s =
4913 match state.layout with
4914 | l :: _ ->
4915 state.bookmarks <-
4916 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4917 :: state.bookmarks
4918 | _ -> ()
4920 enttext ("bookmark: ", "", None, textentry, ondone, true)
4922 | 126 -> (* ~ *)
4923 quickbookmark ();
4924 showtext ' ' "Quick bookmark added";
4926 | 122 -> (* z *)
4927 begin match state.layout with
4928 | l :: _ ->
4929 let rect = getpdimrect l.pagedimno in
4930 let w, h =
4931 if conf.crophack
4932 then
4933 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4934 truncate (1.2 *. (rect.(3) -. rect.(0))))
4935 else
4936 (truncate (rect.(1) -. rect.(0)),
4937 truncate (rect.(3) -. rect.(0)))
4939 let w = truncate ((float w)*.conf.zoom)
4940 and h = truncate ((float h)*.conf.zoom) in
4941 if w != 0 && h != 0
4942 then (
4943 state.anchor <- getanchor ();
4944 doreshape (w + state.scrollw) (h + conf.interpagespace)
4946 G.postRedisplay "z";
4948 | [] -> ()
4951 | 50 when ctrl -> (* ctrl-2 *)
4952 let maxw = getmaxw () in
4953 if maxw > 0.0
4954 then setzoom (maxw /. float conf.winw)
4956 | 60 | 62 -> (* < > *)
4957 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4959 | 91 | 93 -> (* [ ] *)
4960 conf.colorscale <-
4961 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4963 G.postRedisplay "brightness";
4965 | 99 when state.mode = View -> (* c *)
4966 let (c, a, b), z =
4967 match state.prevcolumns with
4968 | None -> (1, 0, 0), 1.0
4969 | Some (columns, z) ->
4970 let cab =
4971 match columns with
4972 | Csplit (c, _) -> -c, 0, 0
4973 | Cmulti ((c, a, b), _) -> c, a, b
4974 | Csingle _ -> 1, 0, 0
4976 cab, z
4978 setcolumns View c a b;
4979 setzoom z;
4981 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4982 setzoom state.prevzoom
4984 | 107 | 0xff52 -> (* k up *)
4985 begin match state.autoscroll with
4986 | None ->
4987 begin match state.mode with
4988 | Birdseye beye -> upbirdseye 1 beye
4989 | _ ->
4990 if ctrl
4991 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4992 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4994 | Some n ->
4995 setautoscrollspeed n false
4998 | 106 | 0xff54 -> (* j down *)
4999 begin match state.autoscroll with
5000 | None ->
5001 begin match state.mode with
5002 | Birdseye beye -> downbirdseye 1 beye
5003 | _ ->
5004 if ctrl
5005 then gotoy_and_clear_text (clamp (conf.winh/2))
5006 else gotoy_and_clear_text (clamp conf.scrollstep)
5008 | Some n ->
5009 setautoscrollspeed n true
5012 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5013 if canpan ()
5014 then
5015 let dx =
5016 if ctrl
5017 then conf.winw / 2
5018 else 10
5020 let dx = if key = 0xff51 then dx else -dx in
5021 state.x <- state.x + dx;
5022 gotoy_and_clear_text state.y
5023 else (
5024 state.text <- "";
5025 G.postRedisplay "lef/right"
5028 | 0xff55 -> (* prior *)
5029 let y =
5030 if ctrl
5031 then
5032 match state.layout with
5033 | [] -> state.y
5034 | l :: _ -> state.y - l.pagey
5035 else
5036 clamp (pgscale (-conf.winh))
5038 gotoghyll y
5040 | 0xff56 -> (* next *)
5041 let y =
5042 if ctrl
5043 then
5044 match List.rev state.layout with
5045 | [] -> state.y
5046 | l :: _ -> getpagey l.pageno
5047 else
5048 clamp (pgscale conf.winh)
5050 gotoghyll y
5052 | 0xff50 -> gotoghyll 0
5053 | 0xff57 -> gotoghyll (clamp state.maxy)
5054 | 0xff53 when Wsi.withalt mask ->
5055 gotoghyll (getnav ~-1)
5056 | 0xff51 when Wsi.withalt mask ->
5057 gotoghyll (getnav 1)
5059 | 114 -> (* r *)
5060 state.anchor <- getanchor ();
5061 opendoc state.path state.password
5063 | 118 when conf.debug -> (* v *)
5064 state.rects <- [];
5065 List.iter (fun l ->
5066 match getopaque l.pageno with
5067 | None -> ()
5068 | Some opaque ->
5069 let x0, y0, x1, y1 = pagebbox opaque in
5070 let a,b = float x0, float y0 in
5071 let c,d = float x1, float y0 in
5072 let e,f = float x1, float y1 in
5073 let h,j = float x0, float y1 in
5074 let rect = (a,b,c,d,e,f,h,j) in
5075 debugrect rect;
5076 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5077 ) state.layout;
5078 G.postRedisplay "v";
5080 | _ ->
5081 vlog "huh? %s" (Wsi.keyname key)
5084 let linknavkeyboard key mask linknav =
5085 let getpage pageno =
5086 let rec loop = function
5087 | [] -> None
5088 | l :: _ when l.pageno = pageno -> Some l
5089 | _ :: rest -> loop rest
5090 in loop state.layout
5092 let doexact (pageno, n) =
5093 match getopaque pageno, getpage pageno with
5094 | Some opaque, Some l ->
5095 if key = 0xff0d
5096 then
5097 let under = getlink opaque n in
5098 G.postRedisplay "link gotounder";
5099 gotounder under;
5100 state.mode <- View;
5101 else
5102 let opt, dir =
5103 match key with
5104 | 0xff50 -> (* home *)
5105 Some (findlink opaque LDfirst), -1
5107 | 0xff57 -> (* end *)
5108 Some (findlink opaque LDlast), 1
5110 | 0xff51 -> (* left *)
5111 Some (findlink opaque (LDleft n)), -1
5113 | 0xff53 -> (* right *)
5114 Some (findlink opaque (LDright n)), 1
5116 | 0xff52 -> (* up *)
5117 Some (findlink opaque (LDup n)), -1
5119 | 0xff54 -> (* down *)
5120 Some (findlink opaque (LDdown n)), 1
5122 | _ -> None, 0
5124 let pwl l dir =
5125 begin match findpwl l.pageno dir with
5126 | Pwlnotfound -> ()
5127 | Pwl pageno ->
5128 let notfound dir =
5129 state.mode <- LinkNav (Ltgendir dir);
5130 let y, h = getpageyh pageno in
5131 let y =
5132 if dir < 0
5133 then y + h - conf.winh
5134 else y
5136 gotoy y
5138 begin match getopaque pageno, getpage pageno with
5139 | Some opaque, Some _ ->
5140 let link =
5141 let ld = if dir > 0 then LDfirst else LDlast in
5142 findlink opaque ld
5144 begin match link with
5145 | Lfound m ->
5146 showlinktype (getlink opaque m);
5147 state.mode <- LinkNav (Ltexact (pageno, m));
5148 G.postRedisplay "linknav jpage";
5149 | _ -> notfound dir
5150 end;
5151 | _ -> notfound dir
5152 end;
5153 end;
5155 begin match opt with
5156 | Some Lnotfound -> pwl l dir;
5157 | Some (Lfound m) ->
5158 if m = n
5159 then pwl l dir
5160 else (
5161 let _, y0, _, y1 = getlinkrect opaque m in
5162 if y0 < l.pagey
5163 then gotopage1 l.pageno y0
5164 else (
5165 let d = fstate.fontsize + 1 in
5166 if y1 - l.pagey > l.pagevh - d
5167 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5168 else G.postRedisplay "linknav";
5170 showlinktype (getlink opaque m);
5171 state.mode <- LinkNav (Ltexact (l.pageno, m));
5174 | None -> viewkeyboard key mask
5175 end;
5176 | _ -> viewkeyboard key mask
5178 if key = 0xff63
5179 then (
5180 state.mode <- View;
5181 G.postRedisplay "leave linknav"
5183 else
5184 match linknav with
5185 | Ltgendir _ -> viewkeyboard key mask
5186 | Ltexact exact -> doexact exact
5189 let keyboard key mask =
5190 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5191 then wcmd "interrupt"
5192 else state.uioh <- state.uioh#key key mask
5195 let birdseyekeyboard key mask
5196 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5197 let incr =
5198 match conf.columns with
5199 | Csingle _ -> 1
5200 | Cmulti ((c, _, _), _) -> c
5201 | Csplit _ -> failwith "bird's eye split mode"
5203 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5204 match key with
5205 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5206 let y, h = getpageyh pageno in
5207 let top = (conf.winh - h) / 2 in
5208 gotoy (max 0 (y - top))
5209 | 0xff0d -> leavebirdseye beye false
5210 | 0xff1b -> leavebirdseye beye true (* escape *)
5211 | 0xff52 -> upbirdseye incr beye (* up *)
5212 | 0xff54 -> downbirdseye incr beye (* down *)
5213 | 0xff51 -> upbirdseye 1 beye (* left *)
5214 | 0xff53 -> downbirdseye 1 beye (* right *)
5216 | 0xff55 -> (* prior *)
5217 begin match state.layout with
5218 | l :: _ ->
5219 if l.pagey != 0
5220 then (
5221 state.mode <- Birdseye (
5222 oconf, leftx, l.pageno, hooverpageno, anchor
5224 gotopage1 l.pageno 0;
5226 else (
5227 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5228 match layout with
5229 | [] -> gotoy (clamp (-conf.winh))
5230 | l :: _ ->
5231 state.mode <- Birdseye (
5232 oconf, leftx, l.pageno, hooverpageno, anchor
5234 gotopage1 l.pageno 0
5237 | [] -> gotoy (clamp (-conf.winh))
5238 end;
5240 | 0xff56 -> (* next *)
5241 begin match List.rev state.layout with
5242 | l :: _ ->
5243 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5244 begin match layout with
5245 | [] ->
5246 let incr = l.pageh - l.pagevh in
5247 if incr = 0
5248 then (
5249 state.mode <-
5250 Birdseye (
5251 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5253 G.postRedisplay "birdseye pagedown";
5255 else gotoy (clamp (incr + conf.interpagespace*2));
5257 | l :: _ ->
5258 state.mode <-
5259 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5260 gotopage1 l.pageno 0;
5263 | [] -> gotoy (clamp conf.winh)
5264 end;
5266 | 0xff50 -> (* home *)
5267 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5268 gotopage1 0 0
5270 | 0xff57 -> (* end *)
5271 let pageno = state.pagecount - 1 in
5272 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5273 if not (pagevisible state.layout pageno)
5274 then
5275 let h =
5276 match List.rev state.pdims with
5277 | [] -> conf.winh
5278 | (_, _, h, _) :: _ -> h
5280 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5281 else G.postRedisplay "birdseye end";
5282 | _ -> viewkeyboard key mask
5285 let drawpage l linkindexbase =
5286 let color =
5287 match state.mode with
5288 | Textentry _ -> scalecolor 0.4
5289 | LinkNav _
5290 | View -> scalecolor 1.0
5291 | Birdseye (_, _, pageno, hooverpageno, _) ->
5292 if l.pageno = hooverpageno
5293 then scalecolor 0.9
5294 else (
5295 if l.pageno = pageno
5296 then scalecolor 1.0
5297 else scalecolor 0.8
5300 drawtiles l color;
5301 begin match getopaque l.pageno with
5302 | Some opaque ->
5303 if tileready l l.pagex l.pagey
5304 then
5305 let x = l.pagedispx - l.pagex
5306 and y = l.pagedispy - l.pagey in
5307 let hlmask =
5308 match conf.columns with
5309 | Csingle _ | Cmulti _ ->
5310 (if conf.hlinks then 1 else 0)
5311 + (if state.glinks
5312 && not (isbirdseye state.mode) then 2 else 0)
5313 | _ -> 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 | n when (n = 6 || n = 7) && not down && canpan () ->
5526 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5527 gotoy_and_clear_text state.y
5529 | 1 when Wsi.withctrl mask ->
5530 if down
5531 then (
5532 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5533 state.mstate <- Mpan (x, y)
5535 else
5536 state.mstate <- Mnone
5538 | 3 ->
5539 if down
5540 then (
5541 Wsi.setcursor Wsi.CURSOR_CYCLE;
5542 let p = (x, y) in
5543 state.mstate <- Mzoomrect (p, p)
5545 else (
5546 match state.mstate with
5547 | Mzoomrect ((x0, y0), _) ->
5548 if abs (x-x0) > 10 && abs (y - y0) > 10
5549 then zoomrect x0 y0 x y
5550 else (
5551 state.mstate <- Mnone;
5552 Wsi.setcursor Wsi.CURSOR_INHERIT;
5553 G.postRedisplay "kill accidental zoom rect";
5555 | _ ->
5556 Wsi.setcursor Wsi.CURSOR_INHERIT;
5557 state.mstate <- Mnone
5560 | 1 when x > conf.winw - state.scrollw ->
5561 if down
5562 then
5563 let _, position, sh = state.uioh#scrollph in
5564 if y > truncate position && y < truncate (position +. sh)
5565 then state.mstate <- Mscrolly
5566 else scrolly y
5567 else
5568 state.mstate <- Mnone
5570 | 1 when y > conf.winh - state.hscrollh ->
5571 if down
5572 then
5573 let _, position, sw = state.uioh#scrollpw in
5574 if x > truncate position && x < truncate (position +. sw)
5575 then state.mstate <- Mscrollx
5576 else scrollx x
5577 else
5578 state.mstate <- Mnone
5580 | 1 ->
5581 let dest = if down then getunder x y else Unone in
5582 begin match dest with
5583 | Ulinkgoto _
5584 | Ulinkuri _
5585 | Uremote _
5586 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5587 gotounder dest
5589 | Unone when down ->
5590 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5591 state.mstate <- Mpan (x, y);
5593 | Unone | Utext _ ->
5594 if down
5595 then (
5596 if conf.angle mod 360 = 0
5597 then (
5598 state.mstate <- Msel ((x, y), (x, y));
5599 G.postRedisplay "mouse select";
5602 else (
5603 match state.mstate with
5604 | Mnone -> ()
5606 | Mzoom _ | Mscrollx | Mscrolly ->
5607 state.mstate <- Mnone
5609 | Mzoomrect ((x0, y0), _) ->
5610 zoomrect x0 y0 x y
5612 | Mpan _ ->
5613 Wsi.setcursor Wsi.CURSOR_INHERIT;
5614 state.mstate <- Mnone
5616 | Msel ((_, y0), (_, y1)) ->
5617 let rec loop = function
5618 | [] -> ()
5619 | l :: rest ->
5620 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5621 || ((y1 >= l.pagedispy
5622 && y1 <= (l.pagedispy + l.pagevh)))
5623 then
5624 match getopaque l.pageno with
5625 | Some opaque ->
5626 begin
5627 match Ne.pipe () with
5628 | Ne.Exn exn ->
5629 showtext '!'
5630 (Printf.sprintf
5631 "can not create sel pipe: %s"
5632 (Printexc.to_string exn));
5633 | Ne.Res (r, w) ->
5634 let doclose what fd =
5635 Ne.clo fd (fun msg ->
5636 dolog "%s close failed: %s" what msg)
5639 popen conf.selcmd [r, 0; w, -1];
5640 copysel w opaque;
5641 doclose "pipe/r" r;
5642 G.postRedisplay "copysel";
5643 with exn ->
5644 dolog "can not execute %S: %s"
5645 conf.selcmd (Printexc.to_string exn);
5646 doclose "pipe/r" r;
5647 doclose "pipe/w" w;
5649 | None -> ()
5650 else loop rest
5652 loop state.layout;
5653 Wsi.setcursor Wsi.CURSOR_INHERIT;
5654 state.mstate <- Mnone;
5658 | _ -> ()
5661 let birdseyemouse button down x y mask
5662 (conf, leftx, _, hooverpageno, anchor) =
5663 match button with
5664 | 1 when down ->
5665 let rec loop = function
5666 | [] -> ()
5667 | l :: rest ->
5668 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5669 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5670 then (
5671 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5673 else loop rest
5675 loop state.layout
5676 | 3 -> ()
5677 | _ -> viewmouse button down x y mask
5680 let mouse button down x y mask =
5681 state.uioh <- state.uioh#button button down x y mask;
5684 let motion ~x ~y =
5685 state.uioh <- state.uioh#motion x y
5688 let pmotion ~x ~y =
5689 state.uioh <- state.uioh#pmotion x y;
5692 let uioh = object
5693 method display = ()
5695 method key key mask =
5696 begin match state.mode with
5697 | Textentry textentry -> textentrykeyboard key mask textentry
5698 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5699 | View -> viewkeyboard key mask
5700 | LinkNav linknav -> linknavkeyboard key mask linknav
5701 end;
5702 state.uioh
5704 method button button bstate x y mask =
5705 begin match state.mode with
5706 | LinkNav _
5707 | View -> viewmouse button bstate x y mask
5708 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5709 | Textentry _ -> ()
5710 end;
5711 state.uioh
5713 method motion x y =
5714 begin match state.mode with
5715 | Textentry _ -> ()
5716 | View | Birdseye _ | LinkNav _ ->
5717 match state.mstate with
5718 | Mzoom _ | Mnone -> ()
5720 | Mpan (x0, y0) ->
5721 let dx = x - x0
5722 and dy = y0 - y in
5723 state.mstate <- Mpan (x, y);
5724 if canpan ()
5725 then state.x <- state.x + dx;
5726 let y = clamp dy in
5727 gotoy_and_clear_text y
5729 | Msel (a, _) ->
5730 state.mstate <- Msel (a, (x, y));
5731 G.postRedisplay "motion select";
5733 | Mscrolly ->
5734 let y = min conf.winh (max 0 y) in
5735 scrolly y
5737 | Mscrollx ->
5738 let x = min conf.winw (max 0 x) in
5739 scrollx x
5741 | Mzoomrect (p0, _) ->
5742 state.mstate <- Mzoomrect (p0, (x, y));
5743 G.postRedisplay "motion zoomrect";
5744 end;
5745 state.uioh
5747 method pmotion x y =
5748 begin match state.mode with
5749 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5750 let rec loop = function
5751 | [] ->
5752 if hooverpageno != -1
5753 then (
5754 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5755 G.postRedisplay "pmotion birdseye no hoover";
5757 | l :: rest ->
5758 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5759 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5760 then (
5761 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5762 G.postRedisplay "pmotion birdseye hoover";
5764 else loop rest
5766 loop state.layout
5768 | Textentry _ -> ()
5770 | LinkNav _
5771 | View ->
5772 match state.mstate with
5773 | Mnone -> updateunder x y
5774 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5776 end;
5777 state.uioh
5779 method infochanged _ = ()
5781 method scrollph =
5782 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5783 let p, h = scrollph state.y maxy in
5784 state.scrollw, p, h
5786 method scrollpw =
5787 let winw = conf.winw - state.scrollw - 1 in
5788 let fwinw = float winw in
5789 let sw =
5790 let sw = fwinw /. float state.w in
5791 let sw = fwinw *. sw in
5792 max sw (float conf.scrollh)
5794 let position, sw =
5795 let f = state.w+winw in
5796 let r = float (winw-state.x) /. float f in
5797 let p = fwinw *. r in
5798 p-.sw/.2., sw
5800 let sw =
5801 if position +. sw > fwinw
5802 then fwinw -. position
5803 else sw
5805 state.hscrollh, position, sw
5807 method modehash =
5808 let modename =
5809 match state.mode with
5810 | LinkNav _ -> "links"
5811 | Textentry _ -> "textentry"
5812 | Birdseye _ -> "birdseye"
5813 | View -> "view"
5815 findkeyhash conf modename
5816 end;;
5818 module Config =
5819 struct
5820 open Parser
5822 let fontpath = ref "";;
5824 module KeyMap =
5825 Map.Make (struct type t = (int * int) let compare = compare end);;
5827 let unent s =
5828 let l = String.length s in
5829 let b = Buffer.create l in
5830 unent b s 0 l;
5831 Buffer.contents b;
5834 let home =
5835 try Sys.getenv "HOME"
5836 with exn ->
5837 prerr_endline
5838 ("Can not determine home directory location: " ^
5839 Printexc.to_string exn);
5843 let modifier_of_string = function
5844 | "alt" -> Wsi.altmask
5845 | "shift" -> Wsi.shiftmask
5846 | "ctrl" | "control" -> Wsi.ctrlmask
5847 | "meta" -> Wsi.metamask
5848 | _ -> 0
5851 let key_of_string =
5852 let r = Str.regexp "-" in
5853 fun s ->
5854 let elems = Str.full_split r s in
5855 let f n k m =
5856 let g s =
5857 let m1 = modifier_of_string s in
5858 if m1 = 0
5859 then (Wsi.namekey s, m)
5860 else (k, m lor m1)
5861 in function
5862 | Str.Delim s when n land 1 = 0 -> g s
5863 | Str.Text s -> g s
5864 | Str.Delim _ -> (k, m)
5866 let rec loop n k m = function
5867 | [] -> (k, m)
5868 | x :: xs ->
5869 let k, m = f n k m x in
5870 loop (n+1) k m xs
5872 loop 0 0 0 elems
5875 let keys_of_string =
5876 let r = Str.regexp "[ \t]" in
5877 fun s ->
5878 let elems = Str.split r s in
5879 List.map key_of_string elems
5882 let copykeyhashes c =
5883 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5886 let config_of c attrs =
5887 let apply c k v =
5889 match k with
5890 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5891 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5892 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5893 | "preload" -> { c with preload = bool_of_string v }
5894 | "page-bias" -> { c with pagebias = int_of_string v }
5895 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5896 | "horizontal-scroll-step" ->
5897 { c with hscrollstep = max (int_of_string v) 1 }
5898 | "auto-scroll-step" ->
5899 { c with autoscrollstep = max 0 (int_of_string v) }
5900 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5901 | "crop-hack" -> { c with crophack = bool_of_string v }
5902 | "throttle" ->
5903 let mw =
5904 match String.lowercase v with
5905 | "true" -> Some infinity
5906 | "false" -> None
5907 | f -> Some (float_of_string f)
5909 { c with maxwait = mw}
5910 | "highlight-links" -> { c with hlinks = bool_of_string v }
5911 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5912 | "vertical-margin" ->
5913 { c with interpagespace = max 0 (int_of_string v) }
5914 | "zoom" ->
5915 let zoom = float_of_string v /. 100. in
5916 let zoom = max zoom 0.0 in
5917 { c with zoom = zoom }
5918 | "presentation" -> { c with presentation = bool_of_string v }
5919 | "rotation-angle" -> { c with angle = int_of_string v }
5920 | "width" -> { c with winw = max 20 (int_of_string v) }
5921 | "height" -> { c with winh = max 20 (int_of_string v) }
5922 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5923 | "proportional-display" -> { c with proportional = bool_of_string v }
5924 | "pixmap-cache-size" ->
5925 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5926 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5927 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5928 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5929 | "persistent-location" -> { c with jumpback = bool_of_string v }
5930 | "background-color" -> { c with bgcolor = color_of_string v }
5931 | "scrollbar-in-presentation" ->
5932 { c with scrollbarinpm = bool_of_string v }
5933 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5934 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5935 | "mupdf-store-size" ->
5936 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5937 | "checkers" -> { c with checkers = bool_of_string v }
5938 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5939 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5940 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5941 | "uri-launcher" -> { c with urilauncher = unent v }
5942 | "path-launcher" -> { c with pathlauncher = unent v }
5943 | "color-space" -> { c with colorspace = colorspace_of_string v }
5944 | "invert-colors" -> { c with invert = bool_of_string v }
5945 | "brightness" -> { c with colorscale = float_of_string v }
5946 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5947 | "ghyllscroll" ->
5948 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5949 | "columns" ->
5950 let (n, _, _) as nab = multicolumns_of_string v in
5951 if n < 0
5952 then { c with columns = Csplit (-n, [||]) }
5953 else { c with columns = Cmulti (nab, [||]) }
5954 | "birds-eye-columns" ->
5955 { c with beyecolumns = Some (max (int_of_string v) 2) }
5956 | "selection-command" -> { c with selcmd = unent v }
5957 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5958 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
5959 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
5960 | _ -> c
5961 with exn ->
5962 prerr_endline ("Error processing attribute (`" ^
5963 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5966 let rec fold c = function
5967 | [] -> c
5968 | (k, v) :: rest ->
5969 let c = apply c k v in
5970 fold c rest
5972 fold { c with keyhashes = copykeyhashes c } attrs;
5975 let fromstring f pos n v d =
5976 try f v
5977 with exn ->
5978 dolog "Error processing attribute (%S=%S) at %d\n%s"
5979 n v pos (Printexc.to_string exn)
5984 let bookmark_of attrs =
5985 let rec fold title page rely = function
5986 | ("title", v) :: rest -> fold v page rely rest
5987 | ("page", v) :: rest -> fold title v rely rest
5988 | ("rely", v) :: rest -> fold title page v rest
5989 | _ :: rest -> fold title page rely rest
5990 | [] -> title, page, rely
5992 fold "invalid" "0" "0" attrs
5995 let doc_of attrs =
5996 let rec fold path page rely pan = function
5997 | ("path", v) :: rest -> fold v page rely pan rest
5998 | ("page", v) :: rest -> fold path v rely pan rest
5999 | ("rely", v) :: rest -> fold path page v pan rest
6000 | ("pan", v) :: rest -> fold path page rely v rest
6001 | _ :: rest -> fold path page rely pan rest
6002 | [] -> path, page, rely, pan
6004 fold "" "0" "0" "0" attrs
6007 let map_of attrs =
6008 let rec fold rs ls = function
6009 | ("out", v) :: rest -> fold v ls rest
6010 | ("in", v) :: rest -> fold rs v rest
6011 | _ :: rest -> fold ls rs rest
6012 | [] -> ls, rs
6014 fold "" "" attrs
6017 let setconf dst src =
6018 dst.scrollbw <- src.scrollbw;
6019 dst.scrollh <- src.scrollh;
6020 dst.icase <- src.icase;
6021 dst.preload <- src.preload;
6022 dst.pagebias <- src.pagebias;
6023 dst.verbose <- src.verbose;
6024 dst.scrollstep <- src.scrollstep;
6025 dst.maxhfit <- src.maxhfit;
6026 dst.crophack <- src.crophack;
6027 dst.autoscrollstep <- src.autoscrollstep;
6028 dst.maxwait <- src.maxwait;
6029 dst.hlinks <- src.hlinks;
6030 dst.underinfo <- src.underinfo;
6031 dst.interpagespace <- src.interpagespace;
6032 dst.zoom <- src.zoom;
6033 dst.presentation <- src.presentation;
6034 dst.angle <- src.angle;
6035 dst.winw <- src.winw;
6036 dst.winh <- src.winh;
6037 dst.savebmarks <- src.savebmarks;
6038 dst.memlimit <- src.memlimit;
6039 dst.proportional <- src.proportional;
6040 dst.texcount <- src.texcount;
6041 dst.sliceheight <- src.sliceheight;
6042 dst.thumbw <- src.thumbw;
6043 dst.jumpback <- src.jumpback;
6044 dst.bgcolor <- src.bgcolor;
6045 dst.scrollbarinpm <- src.scrollbarinpm;
6046 dst.tilew <- src.tilew;
6047 dst.tileh <- src.tileh;
6048 dst.mustoresize <- src.mustoresize;
6049 dst.checkers <- src.checkers;
6050 dst.aalevel <- src.aalevel;
6051 dst.trimmargins <- src.trimmargins;
6052 dst.trimfuzz <- src.trimfuzz;
6053 dst.urilauncher <- src.urilauncher;
6054 dst.colorspace <- src.colorspace;
6055 dst.invert <- src.invert;
6056 dst.colorscale <- src.colorscale;
6057 dst.redirectstderr <- src.redirectstderr;
6058 dst.ghyllscroll <- src.ghyllscroll;
6059 dst.columns <- src.columns;
6060 dst.beyecolumns <- src.beyecolumns;
6061 dst.selcmd <- src.selcmd;
6062 dst.updatecurs <- src.updatecurs;
6063 dst.pathlauncher <- src.pathlauncher;
6064 dst.keyhashes <- copykeyhashes src;
6065 dst.hfsize <- src.hfsize;
6066 dst.hscrollstep <- src.hscrollstep;
6067 dst.pgscale <- src.pgscale;
6070 let get s =
6071 let h = Hashtbl.create 10 in
6072 let dc = { defconf with angle = defconf.angle } in
6073 let rec toplevel v t spos _ =
6074 match t with
6075 | Vdata | Vcdata | Vend -> v
6076 | Vopen ("llppconfig", _, closed) ->
6077 if closed
6078 then v
6079 else { v with f = llppconfig }
6080 | Vopen _ ->
6081 error "unexpected subelement at top level" s spos
6082 | Vclose _ -> error "unexpected close at top level" s spos
6084 and llppconfig v t spos _ =
6085 match t with
6086 | Vdata | Vcdata -> v
6087 | Vend -> error "unexpected end of input in llppconfig" s spos
6088 | Vopen ("defaults", attrs, closed) ->
6089 let c = config_of dc attrs in
6090 setconf dc c;
6091 if closed
6092 then v
6093 else { v with f = defaults }
6095 | Vopen ("ui-font", attrs, closed) ->
6096 let rec getsize size = function
6097 | [] -> size
6098 | ("size", v) :: rest ->
6099 let size =
6100 fromstring int_of_string spos "size" v fstate.fontsize in
6101 getsize size rest
6102 | l -> getsize size l
6104 fstate.fontsize <- getsize fstate.fontsize attrs;
6105 if closed
6106 then v
6107 else { v with f = uifont (Buffer.create 10) }
6109 | Vopen ("doc", attrs, closed) ->
6110 let pathent, spage, srely, span = doc_of attrs in
6111 let path = unent pathent
6112 and pageno = fromstring int_of_string spos "page" spage 0
6113 and rely = fromstring float_of_string spos "rely" srely 0.0
6114 and pan = fromstring int_of_string spos "pan" span 0 in
6115 let c = config_of dc attrs in
6116 let anchor = (pageno, rely) in
6117 if closed
6118 then (Hashtbl.add h path (c, [], pan, anchor); v)
6119 else { v with f = doc path pan anchor c [] }
6121 | Vopen _ ->
6122 error "unexpected subelement in llppconfig" s spos
6124 | Vclose "llppconfig" -> { v with f = toplevel }
6125 | Vclose _ -> error "unexpected close in llppconfig" s spos
6127 and defaults v t spos _ =
6128 match t with
6129 | Vdata | Vcdata -> v
6130 | Vend -> error "unexpected end of input in defaults" s spos
6131 | Vopen ("keymap", attrs, closed) ->
6132 let modename =
6133 try List.assoc "mode" attrs
6134 with Not_found -> "global" in
6135 if closed
6136 then v
6137 else
6138 let ret keymap =
6139 let h = findkeyhash dc modename in
6140 KeyMap.iter (Hashtbl.replace h) keymap;
6141 defaults
6143 { v with f = pkeymap ret KeyMap.empty }
6145 | Vopen (_, _, _) ->
6146 error "unexpected subelement in defaults" s spos
6148 | Vclose "defaults" ->
6149 { v with f = llppconfig }
6151 | Vclose _ -> error "unexpected close in defaults" s spos
6153 and uifont b v t spos epos =
6154 match t with
6155 | Vdata | Vcdata ->
6156 Buffer.add_substring b s spos (epos - spos);
6158 | Vopen (_, _, _) ->
6159 error "unexpected subelement in ui-font" s spos
6160 | Vclose "ui-font" ->
6161 if String.length !fontpath = 0
6162 then fontpath := Buffer.contents b;
6163 { v with f = llppconfig }
6164 | Vclose _ -> error "unexpected close in ui-font" s spos
6165 | Vend -> error "unexpected end of input in ui-font" s spos
6167 and doc path pan anchor c bookmarks v t spos _ =
6168 match t with
6169 | Vdata | Vcdata -> v
6170 | Vend -> error "unexpected end of input in doc" s spos
6171 | Vopen ("bookmarks", _, closed) ->
6172 if closed
6173 then v
6174 else { v with f = pbookmarks path pan anchor c bookmarks }
6176 | Vopen ("keymap", attrs, closed) ->
6177 let modename =
6178 try List.assoc "mode" attrs
6179 with Not_found -> "global"
6181 if closed
6182 then v
6183 else
6184 let ret keymap =
6185 let h = findkeyhash c modename in
6186 KeyMap.iter (Hashtbl.replace h) keymap;
6187 doc path pan anchor c bookmarks
6189 { v with f = pkeymap ret KeyMap.empty }
6191 | Vopen (_, _, _) ->
6192 error "unexpected subelement in doc" s spos
6194 | Vclose "doc" ->
6195 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6196 { v with f = llppconfig }
6198 | Vclose _ -> error "unexpected close in doc" s spos
6200 and pkeymap ret keymap v t spos _ =
6201 match t with
6202 | Vdata | Vcdata -> v
6203 | Vend -> error "unexpected end of input in keymap" s spos
6204 | Vopen ("map", attrs, closed) ->
6205 let r, l = map_of attrs in
6206 let kss = fromstring keys_of_string spos "in" r [] in
6207 let lss = fromstring keys_of_string spos "out" l [] in
6208 let keymap =
6209 match kss with
6210 | [] -> keymap
6211 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6212 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6214 if closed
6215 then { v with f = pkeymap ret keymap }
6216 else
6217 let f () = v in
6218 { v with f = skip "map" f }
6220 | Vopen _ ->
6221 error "unexpected subelement in keymap" s spos
6223 | Vclose "keymap" ->
6224 { v with f = ret keymap }
6226 | Vclose _ -> error "unexpected close in keymap" s spos
6228 and pbookmarks path pan anchor c bookmarks v t spos _ =
6229 match t with
6230 | Vdata | Vcdata -> v
6231 | Vend -> error "unexpected end of input in bookmarks" s spos
6232 | Vopen ("item", attrs, closed) ->
6233 let titleent, spage, srely = bookmark_of attrs in
6234 let page = fromstring int_of_string spos "page" spage 0
6235 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6236 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6237 if closed
6238 then { v with f = pbookmarks path pan anchor c bookmarks }
6239 else
6240 let f () = v in
6241 { v with f = skip "item" f }
6243 | Vopen _ ->
6244 error "unexpected subelement in bookmarks" s spos
6246 | Vclose "bookmarks" ->
6247 { v with f = doc path pan anchor c bookmarks }
6249 | Vclose _ -> error "unexpected close in bookmarks" s spos
6251 and skip tag f v t spos _ =
6252 match t with
6253 | Vdata | Vcdata -> v
6254 | Vend ->
6255 error ("unexpected end of input in skipped " ^ tag) s spos
6256 | Vopen (tag', _, closed) ->
6257 if closed
6258 then v
6259 else
6260 let f' () = { v with f = skip tag f } in
6261 { v with f = skip tag' f' }
6262 | Vclose ctag ->
6263 if tag = ctag
6264 then f ()
6265 else error ("unexpected close in skipped " ^ tag) s spos
6268 parse { f = toplevel; accu = () } s;
6269 h, dc;
6272 let do_load f ic =
6274 let len = in_channel_length ic in
6275 let s = String.create len in
6276 really_input ic s 0 len;
6277 f s;
6278 with
6279 | Parse_error (msg, s, pos) ->
6280 let subs = subs s pos in
6281 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6282 failwith ("parse error: " ^ s)
6284 | exn ->
6285 failwith ("config load error: " ^ Printexc.to_string exn)
6288 let defconfpath =
6289 let dir =
6291 let dir = Filename.concat home ".config" in
6292 if Sys.is_directory dir then dir else home
6293 with _ -> home
6295 Filename.concat dir "llpp.conf"
6298 let confpath = ref defconfpath;;
6300 let load1 f =
6301 if Sys.file_exists !confpath
6302 then
6303 match
6304 (try Some (open_in_bin !confpath)
6305 with exn ->
6306 prerr_endline
6307 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6308 Printexc.to_string exn);
6309 None
6311 with
6312 | Some ic ->
6313 begin try
6314 f (do_load get ic)
6315 with exn ->
6316 prerr_endline
6317 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6318 Printexc.to_string exn);
6319 end;
6320 close_in ic;
6322 | None -> ()
6323 else
6324 f (Hashtbl.create 0, defconf)
6327 let load () =
6328 let f (h, dc) =
6329 let pc, pb, px, pa =
6331 Hashtbl.find h (Filename.basename state.path)
6332 with Not_found -> dc, [], 0, (0, 0.0)
6334 setconf defconf dc;
6335 setconf conf pc;
6336 state.bookmarks <- pb;
6337 state.x <- px;
6338 state.scrollw <- conf.scrollbw;
6339 if conf.jumpback
6340 then state.anchor <- pa;
6341 cbput state.hists.nav pa;
6343 load1 f
6346 let add_attrs bb always dc c =
6347 let ob s a b =
6348 if always || a != b
6349 then Printf.bprintf bb "\n %s='%b'" s a
6350 and oi s a b =
6351 if always || a != b
6352 then Printf.bprintf bb "\n %s='%d'" s a
6353 and oI s a b =
6354 if always || a != b
6355 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6356 and oz s a b =
6357 if always || a <> b
6358 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6359 and oF s a b =
6360 if always || a <> b
6361 then Printf.bprintf bb "\n %s='%f'" s a
6362 and oc s a b =
6363 if always || a <> b
6364 then
6365 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6366 and oC s a b =
6367 if always || a <> b
6368 then
6369 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6370 and oR s a b =
6371 if always || a <> b
6372 then
6373 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6374 and os s a b =
6375 if always || a <> b
6376 then
6377 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6378 and og s a b =
6379 if always || a <> b
6380 then
6381 match a with
6382 | None -> ()
6383 | Some (_N, _A, _B) ->
6384 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6385 and oW s a b =
6386 if always || a <> b
6387 then
6388 let v =
6389 match a with
6390 | None -> "false"
6391 | Some f ->
6392 if f = infinity
6393 then "true"
6394 else string_of_float f
6396 Printf.bprintf bb "\n %s='%s'" s v
6397 and oco s a b =
6398 if always || a <> b
6399 then
6400 match a with
6401 | Cmulti ((n, a, b), _) when n > 1 ->
6402 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6403 | Csplit (n, _) when n > 1 ->
6404 Printf.bprintf bb "\n %s='%d'" s ~-n
6405 | _ -> ()
6406 and obeco s a b =
6407 if always || a <> b
6408 then
6409 match a with
6410 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6411 | _ -> ()
6413 let w, h =
6414 if always
6415 then dc.winw, dc.winh
6416 else
6417 match state.fullscreen with
6418 | Some wh -> wh
6419 | None -> c.winw, c.winh
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" c.maxwait dc.maxwait;
6433 ob "highlight-links" c.hlinks dc.hlinks;
6434 ob "under-cursor-info" c.underinfo dc.underinfo;
6435 oi "vertical-margin" c.interpagespace dc.interpagespace;
6436 oz "zoom" c.zoom dc.zoom;
6437 ob "presentation" c.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;
6467 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6468 oF "page-scroll-scale" c.pgscale dc.pgscale;
6471 let keymapsbuf always dc c =
6472 let bb = Buffer.create 16 in
6473 let rec loop = function
6474 | [] -> ()
6475 | (modename, h) :: rest ->
6476 let dh = findkeyhash dc modename in
6477 if always || h <> dh
6478 then (
6479 if Hashtbl.length h > 0
6480 then (
6481 if Buffer.length bb > 0
6482 then Buffer.add_char bb '\n';
6483 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6484 Hashtbl.iter (fun i o ->
6485 let isdifferent = always ||
6487 let dO = Hashtbl.find dh i in
6488 dO <> o
6489 with Not_found -> true
6491 if isdifferent
6492 then
6493 let addkm (k, m) =
6494 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6495 if Wsi.withalt m then Buffer.add_string bb "alt-";
6496 if Wsi.withshift m then Buffer.add_string bb "shift-";
6497 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6498 Buffer.add_string bb (Wsi.keyname k);
6500 let addkms l =
6501 let rec loop = function
6502 | [] -> ()
6503 | km :: [] -> addkm km
6504 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6506 loop l
6508 Buffer.add_string bb "<map in='";
6509 addkm i;
6510 match o with
6511 | KMinsrt km ->
6512 Buffer.add_string bb "' out='";
6513 addkm km;
6514 Buffer.add_string bb "'/>\n"
6516 | KMinsrl kms ->
6517 Buffer.add_string bb "' out='";
6518 addkms kms;
6519 Buffer.add_string bb "'/>\n"
6521 | KMmulti (ins, kms) ->
6522 Buffer.add_char bb ' ';
6523 addkms ins;
6524 Buffer.add_string bb "' out='";
6525 addkms kms;
6526 Buffer.add_string bb "'/>\n"
6527 ) h;
6528 Buffer.add_string bb "</keymap>";
6531 loop rest
6533 loop c.keyhashes;
6537 let save () =
6538 let uifontsize = fstate.fontsize in
6539 let bb = Buffer.create 32768 in
6540 let f (h, dc) =
6541 let dc = if conf.bedefault then conf else dc in
6542 Buffer.add_string bb "<llppconfig>\n";
6544 if String.length !fontpath > 0
6545 then
6546 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6547 uifontsize
6548 !fontpath
6549 else (
6550 if uifontsize <> 14
6551 then
6552 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6555 Buffer.add_string bb "<defaults ";
6556 add_attrs bb true dc dc;
6557 let kb = keymapsbuf true dc dc in
6558 if Buffer.length kb > 0
6559 then (
6560 Buffer.add_string bb ">\n";
6561 Buffer.add_buffer bb kb;
6562 Buffer.add_string bb "\n</defaults>\n";
6564 else Buffer.add_string bb "/>\n";
6566 let adddoc path pan anchor c bookmarks =
6567 if bookmarks == [] && c = dc && anchor = emptyanchor
6568 then ()
6569 else (
6570 Printf.bprintf bb "<doc path='%s'"
6571 (enent path 0 (String.length path));
6573 if anchor <> emptyanchor
6574 then (
6575 let n, y = anchor in
6576 Printf.bprintf bb " page='%d'" n;
6577 if y > 1e-6
6578 then
6579 Printf.bprintf bb " rely='%f'" y
6583 if pan != 0
6584 then Printf.bprintf bb " pan='%d'" pan;
6586 add_attrs bb false dc c;
6587 let kb = keymapsbuf false dc c in
6589 begin match bookmarks with
6590 | [] ->
6591 if Buffer.length kb > 0
6592 then (
6593 Buffer.add_string bb ">\n";
6594 Buffer.add_buffer bb kb;
6595 Buffer.add_string bb "\n</doc>\n";
6597 else Buffer.add_string bb "/>\n"
6598 | _ ->
6599 Buffer.add_string bb ">\n<bookmarks>\n";
6600 List.iter (fun (title, _level, (page, rely)) ->
6601 Printf.bprintf bb
6602 "<item title='%s' page='%d'"
6603 (enent title 0 (String.length title))
6604 page
6606 if rely > 1e-6
6607 then
6608 Printf.bprintf bb " rely='%f'" rely
6610 Buffer.add_string bb "/>\n";
6611 ) bookmarks;
6612 Buffer.add_string bb "</bookmarks>";
6613 if Buffer.length kb > 0
6614 then (
6615 Buffer.add_string bb "\n";
6616 Buffer.add_buffer bb kb;
6618 Buffer.add_string bb "\n</doc>\n";
6619 end;
6623 let pan, conf =
6624 match state.mode with
6625 | Birdseye (c, pan, _, _, _) ->
6626 let beyecolumns =
6627 match conf.columns with
6628 | Cmulti ((c, _, _), _) -> Some c
6629 | Csingle _ -> None
6630 | Csplit _ -> None
6631 and columns =
6632 match c.columns with
6633 | Cmulti (c, _) -> Cmulti (c, [||])
6634 | Csingle _ -> Csingle [||]
6635 | Csplit _ -> failwith "quit from bird's eye while split"
6637 pan, { c with beyecolumns = beyecolumns; columns = columns }
6638 | _ -> state.x, conf
6640 let basename = Filename.basename state.path in
6641 adddoc basename pan (getanchor ())
6642 (let conf =
6643 let autoscrollstep =
6644 match state.autoscroll with
6645 | Some step -> step
6646 | None -> conf.autoscrollstep
6648 match state.mode with
6649 | Birdseye (bc, _, _, _, _) ->
6650 { conf with
6651 zoom = bc.zoom;
6652 presentation = bc.presentation;
6653 interpagespace = bc.interpagespace;
6654 maxwait = bc.maxwait;
6655 autoscrollstep = autoscrollstep }
6656 | _ -> { conf with autoscrollstep = autoscrollstep }
6657 in conf)
6658 (if conf.savebmarks then state.bookmarks else []);
6660 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6661 if basename <> path
6662 then adddoc path x y c bookmarks
6663 ) h;
6664 Buffer.add_string bb "</llppconfig>";
6666 load1 f;
6667 if Buffer.length bb > 0
6668 then
6670 let tmp = !confpath ^ ".tmp" in
6671 let oc = open_out_bin tmp in
6672 Buffer.output_buffer oc bb;
6673 close_out oc;
6674 Unix.rename tmp !confpath;
6675 with exn ->
6676 prerr_endline
6677 ("error while saving configuration: " ^ Printexc.to_string exn)
6679 end;;
6681 let () =
6682 let trimcachepath = ref "" in
6683 Arg.parse
6684 (Arg.align
6685 [("-p", Arg.String (fun s -> state.password <- s) ,
6686 "<password> Set password");
6688 ("-f", Arg.String (fun s -> Config.fontpath := s),
6689 "<path> Set path to the user interface font");
6691 ("-c", Arg.String (fun s -> Config.confpath := s),
6692 "<path> Set path to the configuration file");
6694 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6695 "<path> Set path to the trim cache file");
6697 ("-v", Arg.Unit (fun () ->
6698 Printf.printf
6699 "%s\nconfiguration path: %s\n"
6700 (version ())
6701 Config.defconfpath
6703 exit 0), " Print version and exit");
6706 (fun s -> state.path <- s)
6707 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6709 if String.length state.path = 0
6710 then (prerr_endline "file name missing"; exit 1);
6712 Config.load ();
6714 let globalkeyhash = findkeyhash conf "global" in
6715 let wsfd, winw, winh = Wsi.init (object
6716 method expose =
6717 if nogeomcmds state.geomcmds || platform == Posx
6718 then display ()
6719 else (
6720 GlClear.color (scalecolor2 conf.bgcolor);
6721 GlClear.clear [`color];
6723 method display = display ()
6724 method reshape w h = reshape w h
6725 method mouse b d x y m = mouse b d x y m
6726 method motion x y = state.mpos <- (x, y); motion x y
6727 method pmotion x y = state.mpos <- (x, y); pmotion x y
6728 method key k m =
6729 let mascm = m land (
6730 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6731 ) in
6732 match state.keystate with
6733 | KSnone ->
6734 let km = k, mascm in
6735 begin
6736 match
6737 let modehash = state.uioh#modehash in
6738 try Hashtbl.find modehash km
6739 with Not_found ->
6740 try Hashtbl.find globalkeyhash km
6741 with Not_found -> KMinsrt (k, m)
6742 with
6743 | KMinsrt (k, m) -> keyboard k m
6744 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6745 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6747 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6748 List.iter (fun (k, m) -> keyboard k m) insrt;
6749 state.keystate <- KSnone
6750 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6751 state.keystate <- KSinto (keys, insrt)
6752 | _ ->
6753 state.keystate <- KSnone
6755 method enter x y = state.mpos <- (x, y); pmotion x y
6756 method leave = state.mpos <- (-1, -1)
6757 method quit = raise Quit
6758 end) conf.winw conf.winh (platform = Posx) in
6760 state.wsfd <- wsfd;
6762 if not (
6763 List.exists GlMisc.check_extension
6764 [ "GL_ARB_texture_rectangle"
6765 ; "GL_EXT_texture_recangle"
6766 ; "GL_NV_texture_rectangle" ]
6768 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6770 let cr, sw =
6771 match Ne.pipe () with
6772 | Ne.Exn exn ->
6773 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6774 exit 1
6775 | Ne.Res rw -> rw
6776 and sr, cw =
6777 match Ne.pipe () with
6778 | Ne.Exn exn ->
6779 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6780 exit 1
6781 | Ne.Res rw -> rw
6784 cloexec cr;
6785 cloexec sw;
6786 cloexec sr;
6787 cloexec cw;
6789 setcheckers conf.checkers;
6790 redirectstderr ();
6792 init (cr, cw) (
6793 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6794 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6795 !Config.fontpath, !trimcachepath
6797 state.sr <- sr;
6798 state.sw <- sw;
6799 state.text <- "Opening " ^ state.path;
6800 reshape winw winh;
6801 opendoc state.path state.password;
6802 state.uioh <- uioh;
6804 let rec loop deadline =
6805 let r =
6806 match state.errfd with
6807 | None -> [state.sr; state.wsfd]
6808 | Some fd -> [state.sr; state.wsfd; fd]
6810 if state.redisplay
6811 then (
6812 state.redisplay <- false;
6813 display ();
6815 let timeout =
6816 let now = now () in
6817 if deadline > now
6818 then (
6819 if deadline = infinity
6820 then ~-.1.0
6821 else max 0.0 (deadline -. now)
6823 else 0.0
6825 let r, _, _ =
6826 try Unix.select r [] [] timeout
6827 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6829 begin match r with
6830 | [] ->
6831 state.ghyll None;
6832 let newdeadline =
6833 if state.ghyll == noghyll
6834 then
6835 match state.autoscroll with
6836 | Some step when step != 0 ->
6837 let y = state.y + step in
6838 let y =
6839 if y < 0
6840 then state.maxy
6841 else if y >= state.maxy then 0 else y
6843 gotoy y;
6844 if state.mode = View
6845 then state.text <- "";
6846 deadline +. 0.01
6847 | _ -> infinity
6848 else deadline +. 0.01
6850 loop newdeadline
6852 | l ->
6853 let rec checkfds = function
6854 | [] -> ()
6855 | fd :: rest when fd = state.sr ->
6856 let cmd = readcmd state.sr in
6857 act cmd;
6858 checkfds rest
6860 | fd :: rest when fd = state.wsfd ->
6861 Wsi.readresp fd;
6862 checkfds rest
6864 | fd :: rest ->
6865 let s = String.create 80 in
6866 let n = Unix.read fd s 0 80 in
6867 if conf.redirectstderr
6868 then (
6869 Buffer.add_substring state.errmsgs s 0 n;
6870 state.newerrmsgs <- true;
6871 state.redisplay <- true;
6873 else (
6874 prerr_string (String.sub s 0 n);
6875 flush stderr;
6877 checkfds rest
6879 checkfds l;
6880 let newdeadline =
6881 let deadline1 =
6882 if deadline = infinity
6883 then now () +. 0.01
6884 else deadline
6886 match state.autoscroll with
6887 | Some step when step != 0 -> deadline1
6888 | _ -> if state.ghyll == noghyll then infinity else deadline1
6890 loop newdeadline
6891 end;
6894 loop infinity;
6895 with Quit ->
6896 Config.save ();