Proper anchoring in split column mode
[llpp.git] / main.ml
blob14c2d17a5553356bb7c0a1d52d33e21064600491
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and fontpath = string
36 and memsize = int
37 and aalevel = int
38 and irect = (int * int * int * int)
39 and trimparams = (trimmargins * irect)
40 and colorspace = | Rgb | Bgr | Gray
43 type link =
44 | Lnotfound
45 | Lfound of int
46 and linkdir =
47 | LDfirst
48 | LDlast
49 | LDfirstvisible of (int * int * int)
50 | LDleft of int
51 | LDright of int
52 | LDdown of int
53 | LDup of int
56 type pagewithlinks =
57 | Pwlnotfound
58 | Pwl of int
61 type keymap =
62 | KMinsrt of key
63 | KMinsrl of key list
64 | KMmulti of key list * key list
65 and key = int * int
66 and keyhash = (key, keymap) Hashtbl.t
67 and keystate =
68 | KSnone
69 | KSinto of (key list * key list)
72 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
73 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
75 type pipe = (Unix.file_descr * Unix.file_descr);;
77 external init : pipe -> params -> unit = "ml_init";;
78 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
79 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
80 external getpdimrect : int -> float array = "ml_getpdimrect";;
81 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
82 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
83 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
84 external measurestr : int -> string -> float = "ml_measure_string";;
85 external getmaxw : unit -> float = "ml_getmaxw";;
86 external postprocess : opaque -> int -> int -> int -> (int * string) -> int =
87 "ml_postprocess";;
88 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
89 external platform : unit -> platform = "ml_platform";;
90 external setaalevel : int -> unit = "ml_setaalevel";;
91 external realloctexts : int -> bool = "ml_realloctexts";;
92 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
93 external findlink : opaque -> linkdir -> link = "ml_findlink";;
94 external getlink : opaque -> int -> under = "ml_getlink";;
95 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
96 external getlinkcount : opaque -> int = "ml_getlinkcount";;
97 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
98 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
100 let platform_to_string = function
101 | Punknown -> "unknown"
102 | Plinux -> "Linux"
103 | Posx -> "OSX"
104 | Psun -> "Sun"
105 | Pfreebsd -> "FreeBSD"
106 | Pdragonflybsd -> "DragonflyBSD"
107 | Popenbsd -> "OpenBSD"
108 | Pnetbsd -> "NetBSD"
109 | Pcygwin -> "Cygwin"
112 let platform = platform ();;
114 let popen cmd fda =
115 if platform = Pcygwin
116 then (
117 let sh = "/bin/sh" in
118 let args = [|sh; "-c"; cmd|] in
119 let rec std si so se = function
120 | [] -> si, so, se
121 | (fd, 0) :: rest -> std fd so se rest
122 | (fd, -1) :: rest ->
123 Unix.set_close_on_exec fd;
124 std si so se rest
125 | (_, n) :: _ ->
126 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
128 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
129 ignore (Unix.create_process sh args si so se)
131 else popen cmd fda;
134 type x = int
135 and y = int
136 and tilex = int
137 and tiley = int
138 and tileparams = (x * y * width * height * tilex * tiley)
141 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
143 type mpos = int * int
144 and mstate =
145 | Msel of (mpos * mpos)
146 | Mpan of mpos
147 | Mscrolly | Mscrollx
148 | Mzoom of (int * int)
149 | Mzoomrect of (mpos * mpos)
150 | Mnone
153 type textentry = string * string * onhist option * onkey * ondone
154 and onkey = string -> int -> te
155 and ondone = string -> unit
156 and histcancel = unit -> unit
157 and onhist = ((histcmd -> string) * histcancel)
158 and histcmd = HCnext | HCprev | HCfirst | HClast
159 and te =
160 | TEstop
161 | TEdone of string
162 | TEcont of string
163 | TEswitch of textentry
166 type 'a circbuf =
167 { store : 'a array
168 ; mutable rc : int
169 ; mutable wc : int
170 ; mutable len : int
174 let bound v minv maxv =
175 max minv (min maxv v);
178 let cbnew n v =
179 { store = Array.create n v
180 ; rc = 0
181 ; wc = 0
182 ; len = 0
186 let drawstring size x y s =
187 Gl.enable `blend;
188 Gl.enable `texture_2d;
189 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
190 ignore (drawstr size x y s);
191 Gl.disable `blend;
192 Gl.disable `texture_2d;
195 let drawstring1 size x y s =
196 drawstr size x y s;
199 let drawstring2 size x y fmt =
200 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
203 let cbcap b = Array.length b.store;;
205 let cbput b v =
206 let cap = cbcap b in
207 b.store.(b.wc) <- v;
208 b.wc <- (b.wc + 1) mod cap;
209 b.rc <- b.wc;
210 b.len <- min (b.len + 1) cap;
213 let cbempty b = b.len = 0;;
215 let cbgetg b circular dir =
216 if cbempty b
217 then b.store.(0)
218 else
219 let rc = b.rc + dir in
220 let rc =
221 if circular
222 then (
223 if rc = -1
224 then b.len-1
225 else (
226 if rc = b.len
227 then 0
228 else rc
231 else max 0 (min rc (b.len-1))
233 b.rc <- rc;
234 b.store.(rc);
237 let cbget b = cbgetg b false;;
238 let cbgetc b = cbgetg b true;;
240 type page =
241 { pageno : int
242 ; pagedimno : int
243 ; pagew : int
244 ; pageh : int
245 ; pagex : int
246 ; pagey : int
247 ; pagevw : int
248 ; pagevh : int
249 ; pagedispx : int
250 ; pagedispy : int
251 ; pagecol : int
255 let debugl l =
256 dolog "l %d dim=%d {" l.pageno l.pagedimno;
257 dolog " WxH %dx%d" l.pagew l.pageh;
258 dolog " vWxH %dx%d" l.pagevw l.pagevh;
259 dolog " pagex,y %d,%d" l.pagex l.pagey;
260 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
261 dolog " column %d" l.pagecol;
262 dolog "}";
265 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
266 dolog "rect {";
267 dolog " x0,y0=(% f, % f)" x0 y0;
268 dolog " x1,y1=(% f, % f)" x1 y1;
269 dolog " x2,y2=(% f, % f)" x2 y2;
270 dolog " x3,y3=(% f, % f)" x3 y3;
271 dolog "}";
274 type multicolumns = multicol * pagegeom
275 and splitcolumns = columncount * pagegeom
276 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
277 and multicol = columncount * covercount * covercount
278 and pdimno = int
279 and columncount = int
280 and covercount = int;;
282 type conf =
283 { mutable scrollbw : int
284 ; mutable scrollh : int
285 ; mutable icase : bool
286 ; mutable preload : bool
287 ; mutable pagebias : int
288 ; mutable verbose : bool
289 ; mutable debug : bool
290 ; mutable scrollstep : int
291 ; mutable maxhfit : bool
292 ; mutable crophack : bool
293 ; mutable autoscrollstep : int
294 ; mutable maxwait : float option
295 ; mutable hlinks : bool
296 ; mutable underinfo : bool
297 ; mutable interpagespace : interpagespace
298 ; mutable zoom : float
299 ; mutable presentation : bool
300 ; mutable angle : angle
301 ; mutable winw : int
302 ; mutable winh : int
303 ; mutable savebmarks : bool
304 ; mutable proportional : proportional
305 ; mutable trimmargins : trimmargins
306 ; mutable trimfuzz : irect
307 ; mutable memlimit : memsize
308 ; mutable texcount : texcount
309 ; mutable sliceheight : sliceheight
310 ; mutable thumbw : width
311 ; mutable jumpback : bool
312 ; mutable bgcolor : float * float * float
313 ; mutable bedefault : bool
314 ; mutable scrollbarinpm : bool
315 ; mutable tilew : int
316 ; mutable tileh : int
317 ; mutable mustoresize : memsize
318 ; mutable checkers : bool
319 ; mutable aalevel : int
320 ; mutable urilauncher : string
321 ; mutable pathlauncher : string
322 ; mutable colorspace : colorspace
323 ; mutable invert : bool
324 ; mutable colorscale : float
325 ; mutable redirectstderr : bool
326 ; mutable ghyllscroll : (int * int * int) option
327 ; mutable columns : columns
328 ; mutable beyecolumns : columncount option
329 ; mutable selcmd : string
330 ; mutable updatecurs : bool
331 ; mutable keyhashes : (string * keyhash) list
333 and columns =
334 | Csingle
335 | Cmulti of multicolumns
336 | Csplit of splitcolumns
339 type anchor = pageno * top;;
341 type outline = string * int * anchor;;
343 type rect = float * float * float * float * float * float * float * float;;
345 type tile = opaque * pixmapsize * elapsed
346 and elapsed = float;;
347 type pagemapkey = pageno * gen;;
348 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
349 and row = int
350 and col = int;;
352 let emptyanchor = (0, 0.0);;
354 type infochange = | Memused | Docinfo | Pdim;;
356 class type uioh = object
357 method display : unit
358 method key : int -> int -> uioh
359 method button : int -> bool -> int -> int -> int -> uioh
360 method motion : int -> int -> uioh
361 method pmotion : int -> int -> uioh
362 method infochanged : infochange -> unit
363 method scrollpw : (int * float * float)
364 method scrollph : (int * float * float)
365 method modehash : keyhash
366 end;;
368 type mode =
369 | Birdseye of (conf * leftx * pageno * pageno * anchor)
370 | Textentry of (textentry * onleave)
371 | View
372 | LinkNav of linktarget
373 and onleave = leavetextentrystatus -> unit
374 and leavetextentrystatus = | Cancel | Confirm
375 and helpitem = string * int * action
376 and action =
377 | Noaction
378 | Action of (uioh -> uioh)
379 and linktarget =
380 | Ltexact of (pageno * int)
381 | Ltgendir of int
384 let isbirdseye = function Birdseye _ -> true | _ -> false;;
385 let istextentry = function Textentry _ -> true | _ -> false;;
387 type currently =
388 | Idle
389 | Loading of (page * gen)
390 | Tiling of (
391 page * opaque * colorspace * angle * gen * col * row * width * height
393 | Outlining of outline list
396 let emptykeyhash = Hashtbl.create 0;;
397 let nouioh : uioh = object (self)
398 method display = ()
399 method key _ _ = self
400 method button _ _ _ _ _ = self
401 method motion _ _ = self
402 method pmotion _ _ = self
403 method infochanged _ = ()
404 method scrollpw = (0, nan, nan)
405 method scrollph = (0, nan, nan)
406 method modehash = emptykeyhash
407 end;;
409 type state =
410 { mutable sr : Unix.file_descr
411 ; mutable sw : Unix.file_descr
412 ; mutable wsfd : Unix.file_descr
413 ; mutable errfd : Unix.file_descr option
414 ; mutable stderr : Unix.file_descr
415 ; mutable errmsgs : Buffer.t
416 ; mutable newerrmsgs : bool
417 ; mutable w : int
418 ; mutable x : int
419 ; mutable y : int
420 ; mutable scrollw : int
421 ; mutable hscrollh : int
422 ; mutable anchor : anchor
423 ; mutable ranchors : (string * string * anchor) list
424 ; mutable maxy : int
425 ; mutable layout : page list
426 ; pagemap : (pagemapkey, opaque) Hashtbl.t
427 ; tilemap : (tilemapkey, tile) Hashtbl.t
428 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
429 ; mutable pdims : (pageno * width * height * leftx) list
430 ; mutable pagecount : int
431 ; mutable currently : currently
432 ; mutable mstate : mstate
433 ; mutable searchpattern : string
434 ; mutable rects : (pageno * recttype * rect) list
435 ; mutable rects1 : (pageno * recttype * rect) list
436 ; mutable text : string
437 ; mutable fullscreen : (width * height) option
438 ; mutable mode : mode
439 ; mutable uioh : uioh
440 ; mutable outlines : outline array
441 ; mutable bookmarks : outline list
442 ; mutable path : string
443 ; mutable password : string
444 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
445 ; mutable memused : memsize
446 ; mutable gen : gen
447 ; mutable throttle : (page list * int * float) option
448 ; mutable autoscroll : int option
449 ; mutable ghyll : (int option -> unit)
450 ; mutable help : helpitem array
451 ; mutable docinfo : (int * string) list
452 ; mutable texid : GlTex.texture_id option
453 ; hists : hists
454 ; mutable prevzoom : float
455 ; mutable progress : float
456 ; mutable redisplay : bool
457 ; mutable mpos : mpos
458 ; mutable keystate : keystate
459 ; mutable glinks : bool
461 and hists =
462 { pat : string circbuf
463 ; pag : string circbuf
464 ; nav : anchor circbuf
465 ; sel : string circbuf
469 let defconf =
470 { scrollbw = 7
471 ; scrollh = 12
472 ; icase = true
473 ; preload = true
474 ; pagebias = 0
475 ; verbose = false
476 ; debug = false
477 ; scrollstep = 24
478 ; maxhfit = true
479 ; crophack = false
480 ; autoscrollstep = 2
481 ; maxwait = None
482 ; hlinks = false
483 ; underinfo = false
484 ; interpagespace = 2
485 ; zoom = 1.0
486 ; presentation = false
487 ; angle = 0
488 ; winw = 900
489 ; winh = 900
490 ; savebmarks = true
491 ; proportional = true
492 ; trimmargins = false
493 ; trimfuzz = (0,0,0,0)
494 ; memlimit = 32 lsl 20
495 ; texcount = 256
496 ; sliceheight = 24
497 ; thumbw = 76
498 ; jumpback = true
499 ; bgcolor = (0.5, 0.5, 0.5)
500 ; bedefault = false
501 ; scrollbarinpm = true
502 ; tilew = 2048
503 ; tileh = 2048
504 ; mustoresize = 256 lsl 20
505 ; checkers = true
506 ; aalevel = 8
507 ; urilauncher =
508 (match platform with
509 | Plinux | Pfreebsd | Pdragonflybsd
510 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
511 | Posx -> "open \"%s\""
512 | Pcygwin -> "cygstart \"%s\""
513 | Punknown -> "echo %s")
514 ; pathlauncher = "lp \"%s\""
515 ; selcmd =
516 (match platform with
517 | Plinux | Pfreebsd | Pdragonflybsd
518 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
519 | Posx -> "pbcopy"
520 | Pcygwin -> "wsel"
521 | Punknown -> "cat")
522 ; colorspace = Rgb
523 ; invert = false
524 ; colorscale = 1.0
525 ; redirectstderr = false
526 ; ghyllscroll = None
527 ; columns = Csingle
528 ; beyecolumns = None
529 ; updatecurs = false
530 ; keyhashes =
531 let mk n = (n, Hashtbl.create 1) in
532 [ mk "global"
533 ; mk "info"
534 ; mk "help"
535 ; mk "outline"
536 ; mk "listview"
537 ; mk "birdseye"
538 ; mk "textentry"
539 ; mk "links"
544 let findkeyhash c name =
545 try List.assoc name c.keyhashes
546 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
549 let conf = { defconf with angle = defconf.angle };;
551 type fontstate =
552 { mutable fontsize : int
553 ; mutable wwidth : float
554 ; mutable maxrows : int
558 let fstate =
559 { fontsize = 14
560 ; wwidth = nan
561 ; maxrows = -1
565 let setfontsize n =
566 fstate.fontsize <- n;
567 fstate.wwidth <- measurestr fstate.fontsize "w";
568 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
571 let geturl s =
572 let colonpos = try String.index s ':' with Not_found -> -1 in
573 let len = String.length s in
574 if colonpos >= 0 && colonpos + 3 < len
575 then (
576 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
577 then
578 let schemestartpos =
579 try String.rindex_from s colonpos ' '
580 with Not_found -> -1
582 let scheme =
583 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
585 match scheme with
586 | "http" | "ftp" | "mailto" ->
587 let epos =
588 try String.index_from s colonpos ' '
589 with Not_found -> len
591 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
592 | _ -> ""
593 else ""
595 else ""
598 let gotouri uri =
599 if String.length conf.urilauncher = 0
600 then print_endline uri
601 else (
602 let url = geturl uri in
603 if String.length url = 0
604 then print_endline uri
605 else
606 let re = Str.regexp "%s" in
607 let command = Str.global_replace re url conf.urilauncher in
608 try popen command []
609 with exn ->
610 Printf.eprintf
611 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
612 flush stderr;
616 let version () =
617 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
618 (platform_to_string platform) Sys.word_size Sys.ocaml_version
621 let makehelp () =
622 let strings = version () :: "" :: Help.keys in
623 Array.of_list (
624 List.map (fun s ->
625 let url = geturl s in
626 if String.length url > 0
627 then (s, 0, Action (fun u -> gotouri url; u))
628 else (s, 0, Noaction)
629 ) strings);
632 let noghyll _ = ();;
633 let firstgeomcmds = "", [];;
635 let state =
636 { sr = Unix.stdin
637 ; sw = Unix.stdin
638 ; wsfd = Unix.stdin
639 ; errfd = None
640 ; stderr = Unix.stderr
641 ; errmsgs = Buffer.create 0
642 ; newerrmsgs = false
643 ; x = 0
644 ; y = 0
645 ; w = 0
646 ; scrollw = 0
647 ; hscrollh = 0
648 ; anchor = emptyanchor
649 ; ranchors = []
650 ; layout = []
651 ; maxy = max_int
652 ; tilelru = Queue.create ()
653 ; pagemap = Hashtbl.create 10
654 ; tilemap = Hashtbl.create 10
655 ; pdims = []
656 ; pagecount = 0
657 ; currently = Idle
658 ; mstate = Mnone
659 ; rects = []
660 ; rects1 = []
661 ; text = ""
662 ; mode = View
663 ; fullscreen = None
664 ; searchpattern = ""
665 ; outlines = [||]
666 ; bookmarks = []
667 ; path = ""
668 ; password = ""
669 ; geomcmds = firstgeomcmds
670 ; hists =
671 { nav = cbnew 10 (0, 0.0)
672 ; pat = cbnew 10 ""
673 ; pag = cbnew 10 ""
674 ; sel = cbnew 10 ""
676 ; memused = 0
677 ; gen = 0
678 ; throttle = None
679 ; autoscroll = None
680 ; ghyll = noghyll
681 ; help = makehelp ()
682 ; docinfo = []
683 ; texid = None
684 ; prevzoom = 1.0
685 ; progress = -1.0
686 ; uioh = nouioh
687 ; redisplay = true
688 ; mpos = (-1, -1)
689 ; keystate = KSnone
690 ; glinks = false
694 let vlog fmt =
695 if conf.verbose
696 then
697 Printf.kprintf prerr_endline fmt
698 else
699 Printf.kprintf ignore fmt
702 let launchpath () =
703 if String.length conf.pathlauncher = 0
704 then print_endline state.path
705 else (
706 let re = Str.regexp "%s" in
707 let command = Str.global_replace re state.path conf.pathlauncher in
708 try popen command []
709 with exn ->
710 Printf.eprintf
711 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
712 flush stderr;
716 module Ne = struct
717 type 'a t = | Res of 'a | Exn of exn;;
719 let pipe () =
720 try Res (Unix.pipe ())
721 with exn -> Exn exn
724 let clo fd f =
725 try Unix.close fd
726 with exn -> f (Printexc.to_string exn)
729 let dup fd =
730 try Res (Unix.dup fd)
731 with exn -> Exn exn
734 let dup2 fd1 fd2 =
735 try Res (Unix.dup2 fd1 fd2)
736 with exn -> Exn exn
738 end;;
740 let redirectstderr () =
741 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
742 if conf.redirectstderr
743 then
744 match Ne.pipe () with
745 | Ne.Exn exn ->
746 dolog "failed to create stderr redirection pipes: %s"
747 (Printexc.to_string exn)
749 | Ne.Res (r, w) ->
750 begin match Ne.dup Unix.stderr with
751 | Ne.Exn exn ->
752 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
753 Ne.clo r (clofail "pipe/r");
754 Ne.clo w (clofail "pipe/w");
756 | Ne.Res dupstderr ->
757 begin match Ne.dup2 w Unix.stderr with
758 | Ne.Exn exn ->
759 dolog "failed to dup2 to stderr: %s"
760 (Printexc.to_string exn);
761 Ne.clo dupstderr (clofail "stderr duplicate");
762 Ne.clo r (clofail "redir pipe/r");
763 Ne.clo w (clofail "redir pipe/w");
765 | Ne.Res () ->
766 state.stderr <- dupstderr;
767 state.errfd <- Some r;
768 end;
770 else (
771 state.newerrmsgs <- false;
772 begin match state.errfd with
773 | Some fd ->
774 begin match Ne.dup2 state.stderr Unix.stderr with
775 | Ne.Exn exn ->
776 dolog "failed to dup2 original stderr: %s"
777 (Printexc.to_string exn)
778 | Ne.Res () ->
779 Ne.clo fd (clofail "dup of stderr");
780 Unix.dup2 state.stderr Unix.stderr;
781 state.errfd <- None;
782 end;
783 | None -> ()
784 end;
785 prerr_string (Buffer.contents state.errmsgs);
786 flush stderr;
787 Buffer.clear state.errmsgs;
791 module G =
792 struct
793 let postRedisplay who =
794 if conf.verbose
795 then prerr_endline ("redisplay for " ^ who);
796 state.redisplay <- true;
798 end;;
800 let getopaque pageno =
801 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
802 with Not_found -> None
805 let putopaque pageno opaque =
806 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
809 let pagetranslatepoint l x y =
810 let dy = y - l.pagedispy in
811 let y = dy + l.pagey in
812 let dx = x - l.pagedispx in
813 let x = dx + l.pagex in
814 (x, y);
817 let getunder x y =
818 let rec f = function
819 | l :: rest ->
820 begin match getopaque l.pageno with
821 | Some opaque ->
822 let x0 = l.pagedispx in
823 let x1 = x0 + l.pagevw in
824 let y0 = l.pagedispy in
825 let y1 = y0 + l.pagevh in
826 if y >= y0 && y <= y1 && x >= x0 && x <= x1
827 then
828 let px, py = pagetranslatepoint l x y in
829 match whatsunder opaque px py with
830 | Unone -> f rest
831 | under -> under
832 else f rest
833 | _ ->
834 f rest
836 | [] -> Unone
838 f state.layout
841 let showtext c s =
842 state.text <- Printf.sprintf "%c%s" c s;
843 G.postRedisplay "showtext";
846 let undertext = function
847 | Unone -> "none"
848 | Ulinkuri s -> s
849 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
850 | Utext s -> "font: " ^ s
851 | Uunexpected s -> "unexpected: " ^ s
852 | Ulaunch s -> "launch: " ^ s
853 | Unamed s -> "named: " ^ s
854 | Uremote (filename, pageno) ->
855 Printf.sprintf "%s: page %d" filename (pageno+1)
858 let updateunder x y =
859 match getunder x y with
860 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
861 | Ulinkuri uri ->
862 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
863 Wsi.setcursor Wsi.CURSOR_INFO
864 | Ulinkgoto (pageno, _) ->
865 if conf.underinfo
866 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
867 Wsi.setcursor Wsi.CURSOR_INFO
868 | Utext s ->
869 if conf.underinfo then showtext 'f' ("ont: " ^ s);
870 Wsi.setcursor Wsi.CURSOR_TEXT
871 | Uunexpected s ->
872 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
873 Wsi.setcursor Wsi.CURSOR_INHERIT
874 | Ulaunch s ->
875 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
876 Wsi.setcursor Wsi.CURSOR_INHERIT
877 | Unamed s ->
878 if conf.underinfo then showtext 'n' ("amed: " ^ s);
879 Wsi.setcursor Wsi.CURSOR_INHERIT
880 | Uremote (filename, pageno) ->
881 if conf.underinfo then showtext 'r'
882 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
883 Wsi.setcursor Wsi.CURSOR_INFO
886 let showlinktype under =
887 if conf.underinfo
888 then
889 match under with
890 | Unone -> ()
891 | under ->
892 let s = undertext under in
893 showtext ' ' s
896 let addchar s c =
897 let b = Buffer.create (String.length s + 1) in
898 Buffer.add_string b s;
899 Buffer.add_char b c;
900 Buffer.contents b;
903 let colorspace_of_string s =
904 match String.lowercase s with
905 | "rgb" -> Rgb
906 | "bgr" -> Bgr
907 | "gray" -> Gray
908 | _ -> failwith "invalid colorspace"
911 let int_of_colorspace = function
912 | Rgb -> 0
913 | Bgr -> 1
914 | Gray -> 2
917 let colorspace_of_int = function
918 | 0 -> Rgb
919 | 1 -> Bgr
920 | 2 -> Gray
921 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
924 let colorspace_to_string = function
925 | Rgb -> "rgb"
926 | Bgr -> "bgr"
927 | Gray -> "gray"
930 let intentry_with_suffix text key =
931 let c =
932 if key >= 32 && key < 127
933 then Char.chr key
934 else '\000'
936 match Char.lowercase c with
937 | '0' .. '9' ->
938 let text = addchar text c in
939 TEcont text
941 | 'k' | 'm' | 'g' ->
942 let text = addchar text c in
943 TEcont text
945 | _ ->
946 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
947 TEcont text
950 let multicolumns_to_string (n, a, b) =
951 if a = 0 && b = 0
952 then Printf.sprintf "%d" n
953 else Printf.sprintf "%d,%d,%d" n a b;
956 let multicolumns_of_string s =
958 (int_of_string s, 0, 0)
959 with _ ->
960 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
963 let readcmd fd =
964 let s = "xxxx" in
965 let n = Unix.read fd s 0 4 in
966 if n != 4 then failwith "incomplete read(len)";
967 let len = 0
968 lor (Char.code s.[0] lsl 24)
969 lor (Char.code s.[1] lsl 16)
970 lor (Char.code s.[2] lsl 8)
971 lor (Char.code s.[3] lsl 0)
973 let s = String.create len in
974 let n = Unix.read fd s 0 len in
975 if n != len then failwith "incomplete read(data)";
979 let btod b = if b then 1 else 0;;
981 let wcmd fmt =
982 let b = Buffer.create 16 in
983 Buffer.add_string b "llll";
984 Printf.kbprintf
985 (fun b ->
986 let s = Buffer.contents b in
987 let n = String.length s in
988 let len = n - 4 in
989 (* dolog "wcmd %S" (String.sub s 4 len); *)
990 s.[0] <- Char.chr ((len lsr 24) land 0xff);
991 s.[1] <- Char.chr ((len lsr 16) land 0xff);
992 s.[2] <- Char.chr ((len lsr 8) land 0xff);
993 s.[3] <- Char.chr (len land 0xff);
994 let n' = Unix.write state.sw s 0 n in
995 if n' != n then failwith "write failed";
996 ) b fmt;
999 let calcips h =
1000 if conf.presentation
1001 then
1002 let d = conf.winh - h in
1003 max 0 ((d + 1) / 2)
1004 else
1005 conf.interpagespace
1008 let calcheight () =
1009 let rec f pn ph pi fh l =
1010 match l with
1011 | (n, _, h, _) :: rest ->
1012 let ips = calcips h in
1013 let fh =
1014 if conf.presentation
1015 then fh+ips
1016 else (
1017 if isbirdseye state.mode && pn = 0
1018 then fh + ips
1019 else fh
1022 let fh = fh + ((n - pn) * (ph + pi)) in
1023 f n h ips fh rest;
1025 | [] ->
1026 let inc =
1027 if conf.presentation || (isbirdseye state.mode && pn = 0)
1028 then 0
1029 else -pi
1031 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
1032 max 0 fh
1034 let fh = f 0 0 0 0 state.pdims in
1038 let calcheight () =
1039 match conf.columns with
1040 | Csingle -> calcheight ()
1041 | Cmulti ((c, _, _), b) ->
1042 let rec loop y h n =
1043 if n < 0
1044 then loop y h (n+1)
1045 else (
1046 if n = Array.length b
1047 then y + h
1048 else
1049 let (_, _, y', (_, _, h', _)) = b.(n) in
1050 let y = min y y'
1051 and h = max h h' in
1052 loop y h (n+1)
1055 loop max_int 0 (((Array.length b - 1) / c) * c)
1056 | Csplit (_, b) ->
1057 if Array.length b > 0
1058 then
1059 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1060 y + h
1061 else 0
1064 let getpageyh pageno =
1065 let rec f pn ph pi y l =
1066 match l with
1067 | (n, _, h, _) :: rest ->
1068 let ips = calcips h in
1069 if n >= pageno
1070 then
1071 let h = if n = pageno then h else ph in
1072 if conf.presentation && n = pageno
1073 then
1074 y + (pageno - pn) * (ph + pi) + pi, h
1075 else
1076 y + (pageno - pn) * (ph + pi), h
1077 else
1078 let y = y + (if conf.presentation then pi else 0) in
1079 let y = y + (n - pn) * (ph + pi) in
1080 f n h ips y rest
1082 | [] ->
1083 y + (pageno - pn) * (ph + pi), ph
1085 f 0 0 0 0 state.pdims
1088 let getpageyh pageno =
1089 match conf.columns with
1090 | Csingle -> getpageyh pageno
1091 | Cmulti (_, b) ->
1092 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1093 y, h
1094 | Csplit (c, b) ->
1095 let n = pageno*c in
1096 let (_, _, y, (_, _, h, _)) = b.(n) in
1097 y, h
1100 let getpagedim pageno =
1101 let rec f ppdim l =
1102 match l with
1103 | (n, _, _, _) as pdim :: rest ->
1104 if n >= pageno
1105 then (if n = pageno then pdim else ppdim)
1106 else f pdim rest
1108 | [] -> ppdim
1110 f (-1, -1, -1, -1) state.pdims
1113 let getpagey pageno = fst (getpageyh pageno);;
1115 let nogeomcmds cmds =
1116 match cmds with
1117 | s, [] -> String.length s = 0
1118 | _ -> false
1121 let layout1 y sh =
1122 let sh = sh - state.hscrollh in
1123 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1124 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1125 match pdims with
1126 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1127 let ips = calcips h in
1128 let yinc =
1129 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1130 then ips
1131 else 0
1133 (w, h, ips, xoff), rest, pdimno + 1, yinc
1134 | _ ->
1135 prev, pdims, pdimno, 0
1137 let dy = dy + yinc in
1138 let py = py + yinc in
1139 if pageno = state.pagecount || dy >= sh
1140 then
1141 accu
1142 else
1143 let vy = y + dy in
1144 if py + h <= vy - yinc
1145 then
1146 let py = py + h + ips in
1147 let dy = max 0 (py - y) in
1148 f ~pageno:(pageno+1)
1149 ~pdimno
1150 ~prev:curr
1153 ~pdims:rest
1154 ~accu
1155 else
1156 let pagey = vy - py in
1157 let pagevh = h - pagey in
1158 let pagevh = min (sh - dy) pagevh in
1159 let off = if yinc > 0 then py - vy else 0 in
1160 let py = py + h + ips in
1161 let pagex, dx =
1162 let xoff = xoff +
1163 if state.w < conf.winw - state.scrollw
1164 then (conf.winw - state.scrollw - state.w) / 2
1165 else 0
1167 let dispx = xoff + state.x in
1168 if dispx < 0
1169 then (-dispx, 0)
1170 else (0, dispx)
1172 let pagevw =
1173 let lw = w - pagex in
1174 min lw (conf.winw - state.scrollw)
1176 let e =
1177 { pageno = pageno
1178 ; pagedimno = pdimno
1179 ; pagew = w
1180 ; pageh = h
1181 ; pagex = pagex
1182 ; pagey = pagey + off
1183 ; pagevw = pagevw
1184 ; pagevh = pagevh - off
1185 ; pagedispx = dx
1186 ; pagedispy = dy + off
1187 ; pagecol = 0
1190 let accu = e :: accu in
1191 f ~pageno:(pageno+1)
1192 ~pdimno
1193 ~prev:curr
1195 ~dy:(dy+pagevh+ips)
1196 ~pdims:rest
1197 ~accu
1199 let accu =
1201 ~pageno:0
1202 ~pdimno:~-1
1203 ~prev:(0,0,0,0)
1204 ~py:0
1205 ~dy:0
1206 ~pdims:state.pdims
1207 ~accu:[]
1209 List.rev accu
1212 let layoutN ((columns, coverA, coverB), b) y sh =
1213 let sh = sh - state.hscrollh in
1214 let rec fold accu n =
1215 if n = Array.length b
1216 then accu
1217 else
1218 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1219 if (vy - y) > sh &&
1220 (n = coverA - 1
1221 || n = state.pagecount - coverB
1222 || (n - coverA) mod columns = columns - 1)
1223 then accu
1224 else
1225 let accu =
1226 if vy + h > y
1227 then
1228 let pagey = max 0 (y - vy) in
1229 let pagedispy = if pagey > 0 then 0 else vy - y in
1230 let pagedispx, pagex =
1231 let pdx =
1232 if n = coverA - 1 || n = state.pagecount - coverB
1233 then state.x + (conf.winw - state.scrollw - w) / 2
1234 else dx + xoff + state.x
1236 if pdx < 0
1237 then 0, -pdx
1238 else pdx, 0
1240 let pagevw =
1241 let vw = conf.winw - state.scrollw - pagedispx in
1242 let pw = w - pagex in
1243 min vw pw
1245 let pagevh = min (h - pagey) (sh - pagedispy) in
1246 if pagevw > 0 && pagevh > 0
1247 then
1248 let e =
1249 { pageno = n
1250 ; pagedimno = pdimno
1251 ; pagew = w
1252 ; pageh = h
1253 ; pagex = pagex
1254 ; pagey = pagey
1255 ; pagevw = pagevw
1256 ; pagevh = pagevh
1257 ; pagedispx = pagedispx
1258 ; pagedispy = pagedispy
1259 ; pagecol = 0
1262 e :: accu
1263 else
1264 accu
1265 else
1266 accu
1268 fold accu (n+1)
1270 List.rev (fold [] 0)
1273 let layoutS (columns, b) y sh =
1274 let sh = sh - state.hscrollh in
1275 let rec fold accu n =
1276 if n = Array.length b
1277 then accu
1278 else
1279 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1280 if (vy - y) > sh
1281 then accu
1282 else
1283 let accu =
1284 if vy + pageh > y
1285 then
1286 let x = xoff + state.x in
1287 let pagey = max 0 (y - vy) in
1288 let pagedispy = if pagey > 0 then 0 else vy - y in
1289 let pagedispx, pagex =
1290 if px = 0
1291 then (
1292 if x < 0
1293 then 0, -x
1294 else x, 0
1296 else (
1297 let px = px - x in
1298 if px < 0
1299 then -px, 0
1300 else 0, px
1303 let pagevw =
1304 let vw = conf.winw - pagedispx - state.scrollw in
1305 let pw = pagew - pagex in
1306 min vw pw
1308 let pagevw = min pagevw (pagew/columns) in
1309 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1310 if pagevw > 0 && pagevh > 0
1311 then
1312 let e =
1313 { pageno = n/columns
1314 ; pagedimno = pdimno
1315 ; pagew = pagew
1316 ; pageh = pageh
1317 ; pagex = pagex
1318 ; pagey = pagey
1319 ; pagevw = pagevw
1320 ; pagevh = pagevh
1321 ; pagedispx = pagedispx
1322 ; pagedispy = pagedispy
1323 ; pagecol = n mod columns
1326 e :: accu
1327 else
1328 accu
1329 else
1330 accu
1332 fold accu (n+1)
1334 List.rev (fold [] 0)
1337 let layout y sh =
1338 if nogeomcmds state.geomcmds
1339 then
1340 match conf.columns with
1341 | Csingle -> layout1 y sh
1342 | Cmulti c -> layoutN c y sh
1343 | Csplit s -> layoutS s y sh
1344 else []
1347 let clamp incr =
1348 let y = state.y + incr in
1349 let y = max 0 y in
1350 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1354 let itertiles l f =
1355 let tilex = l.pagex mod conf.tilew in
1356 let tiley = l.pagey mod conf.tileh in
1358 let col = l.pagex / conf.tilew in
1359 let row = l.pagey / conf.tileh in
1361 let rec rowloop row y0 dispy h =
1362 if h = 0
1363 then ()
1364 else (
1365 let dh = conf.tileh - y0 in
1366 let dh = min h dh in
1367 let rec colloop col x0 dispx w =
1368 if w = 0
1369 then ()
1370 else (
1371 let dw = conf.tilew - x0 in
1372 let dw = min w dw in
1374 f col row dispx dispy x0 y0 dw dh;
1375 colloop (col+1) 0 (dispx+dw) (w-dw)
1378 colloop col tilex l.pagedispx l.pagevw;
1379 rowloop (row+1) 0 (dispy+dh) (h-dh)
1382 if l.pagevw > 0 && l.pagevh > 0
1383 then rowloop row tiley l.pagedispy l.pagevh;
1386 let gettileopaque l col row =
1387 let key =
1388 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1390 try Some (Hashtbl.find state.tilemap key)
1391 with Not_found -> None
1394 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1395 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1396 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1399 let drawtiles l color =
1400 GlDraw.color color;
1401 let f col row x y tilex tiley w h =
1402 match gettileopaque l col row with
1403 | Some (opaque, _, t) ->
1404 let params = x, y, w, h, tilex, tiley in
1405 if conf.invert
1406 then (
1407 Gl.enable `blend;
1408 GlFunc.blend_func `zero `one_minus_src_color;
1410 drawtile params opaque;
1411 if conf.invert
1412 then Gl.disable `blend;
1413 if conf.debug
1414 then (
1415 let s = Printf.sprintf
1416 "%d[%d,%d] %f sec"
1417 l.pageno col row t
1419 let w = measurestr fstate.fontsize s in
1420 GlMisc.push_attrib [`current];
1421 GlDraw.color (0.0, 0.0, 0.0);
1422 GlDraw.rect
1423 (float (x-2), float (y-2))
1424 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1425 GlDraw.color (1.0, 1.0, 1.0);
1426 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1427 GlMisc.pop_attrib ();
1430 | _ ->
1431 let w =
1432 let lw = conf.winw - state.scrollw - x in
1433 min lw w
1434 and h =
1435 let lh = conf.winh - y in
1436 min lh h
1438 Gl.enable `texture_2d;
1439 begin match state.texid with
1440 | Some id ->
1441 GlTex.bind_texture `texture_2d id;
1442 let x0 = float x
1443 and y0 = float y
1444 and x1 = float (x+w)
1445 and y1 = float (y+h) in
1447 let tw = float w /. 64.0
1448 and th = float h /. 64.0 in
1449 let tx0 = float tilex /. 64.0
1450 and ty0 = float tiley /. 64.0 in
1451 let tx1 = tx0 +. tw
1452 and ty1 = ty0 +. th in
1453 GlDraw.begins `quads;
1454 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1455 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1456 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1457 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1458 GlDraw.ends ();
1460 Gl.disable `texture_2d;
1461 | None ->
1462 GlDraw.color (1.0, 1.0, 1.0);
1463 GlDraw.rect
1464 (float x, float y)
1465 (float (x+w), float (y+h));
1466 end;
1467 if w > 128 && h > fstate.fontsize + 10
1468 then (
1469 GlDraw.color (0.0, 0.0, 0.0);
1470 let c, r =
1471 if conf.verbose
1472 then (col*conf.tilew, row*conf.tileh)
1473 else col, row
1475 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1477 GlDraw.color color;
1479 itertiles l f
1482 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1484 let tilevisible1 l x y =
1485 let ax0 = l.pagex
1486 and ax1 = l.pagex + l.pagevw
1487 and ay0 = l.pagey
1488 and ay1 = l.pagey + l.pagevh in
1490 let bx0 = x
1491 and by0 = y in
1492 let bx1 = min (bx0 + conf.tilew) l.pagew
1493 and by1 = min (by0 + conf.tileh) l.pageh in
1495 let rx0 = max ax0 bx0
1496 and ry0 = max ay0 by0
1497 and rx1 = min ax1 bx1
1498 and ry1 = min ay1 by1 in
1500 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1501 nonemptyintersection
1504 let tilevisible layout n x y =
1505 let rec findpageinlayout m = function
1506 | l :: rest when l.pageno = n ->
1507 tilevisible1 l x y || (
1508 match conf.columns with
1509 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1510 | _ -> false
1512 | _ :: rest -> findpageinlayout 0 rest
1513 | [] -> false
1515 findpageinlayout 0 layout;
1518 let tileready l x y =
1519 tilevisible1 l x y &&
1520 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1523 let tilepage n p layout =
1524 let rec loop = function
1525 | l :: rest ->
1526 if l.pageno = n
1527 then
1528 let f col row _ _ _ _ _ _ =
1529 if state.currently = Idle
1530 then
1531 match gettileopaque l col row with
1532 | Some _ -> ()
1533 | None ->
1534 let x = col*conf.tilew
1535 and y = row*conf.tileh in
1536 let w =
1537 let w = l.pagew - x in
1538 min w conf.tilew
1540 let h =
1541 let h = l.pageh - y in
1542 min h conf.tileh
1544 wcmd "tile %s %d %d %d %d" p x y w h;
1545 state.currently <-
1546 Tiling (
1547 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1548 conf.tilew, conf.tileh
1551 itertiles l f;
1552 else
1553 loop rest
1555 | [] -> ()
1557 if nogeomcmds state.geomcmds
1558 then loop layout;
1561 let preloadlayout visiblepages =
1562 let presentation = conf.presentation in
1563 let interpagespace = conf.interpagespace in
1564 let maxy = state.maxy in
1565 conf.presentation <- false;
1566 conf.interpagespace <- 0;
1567 state.maxy <- calcheight ();
1568 let y =
1569 match visiblepages with
1570 | [] -> 0
1571 | l :: _ -> getpagey l.pageno + l.pagey
1573 let y = if y < conf.winh then 0 else y - conf.winh in
1574 let h = state.y - y + conf.winh*3 in
1575 let pages = layout y h in
1576 conf.presentation <- presentation;
1577 conf.interpagespace <- interpagespace;
1578 state.maxy <- maxy;
1579 pages;
1582 let load pages =
1583 let rec loop pages =
1584 if state.currently != Idle
1585 then ()
1586 else
1587 match pages with
1588 | l :: rest ->
1589 begin match getopaque l.pageno with
1590 | None ->
1591 wcmd "page %d %d" l.pageno l.pagedimno;
1592 state.currently <- Loading (l, state.gen);
1593 | Some opaque ->
1594 tilepage l.pageno opaque pages;
1595 loop rest
1596 end;
1597 | _ -> ()
1599 if nogeomcmds state.geomcmds
1600 then loop pages
1603 let preload pages =
1604 load pages;
1605 if conf.preload && state.currently = Idle
1606 then load (preloadlayout pages);
1609 let layoutready layout =
1610 let rec fold all ls =
1611 all && match ls with
1612 | l :: rest ->
1613 let seen = ref false in
1614 let allvisible = ref true in
1615 let foo col row _ _ _ _ _ _ =
1616 seen := true;
1617 allvisible := !allvisible &&
1618 begin match gettileopaque l col row with
1619 | Some _ -> true
1620 | None -> false
1623 itertiles l foo;
1624 fold (!seen && !allvisible) rest
1625 | [] -> true
1627 let alltilesvisible = fold true layout in
1628 alltilesvisible;
1631 let gotoy y =
1632 let y = bound y 0 state.maxy in
1633 let y, layout, proceed =
1634 match conf.maxwait with
1635 | Some time when state.ghyll == noghyll ->
1636 begin match state.throttle with
1637 | None ->
1638 let layout = layout y conf.winh in
1639 let ready = layoutready layout in
1640 if not ready
1641 then (
1642 load layout;
1643 state.throttle <- Some (layout, y, now ());
1645 else G.postRedisplay "gotoy showall (None)";
1646 y, layout, ready
1647 | Some (_, _, started) ->
1648 let dt = now () -. started in
1649 if dt > time
1650 then (
1651 state.throttle <- None;
1652 let layout = layout y conf.winh in
1653 load layout;
1654 G.postRedisplay "maxwait";
1655 y, layout, true
1657 else -1, [], false
1660 | _ ->
1661 let layout = layout y conf.winh in
1662 if true || layoutready layout
1663 then G.postRedisplay "gotoy ready";
1664 y, layout, true
1666 if proceed
1667 then (
1668 state.y <- y;
1669 state.layout <- layout;
1670 begin match state.mode with
1671 | LinkNav (Ltexact (pageno, linkno)) ->
1672 let rec loop = function
1673 | [] ->
1674 state.mode <- LinkNav (Ltgendir 0)
1675 | l :: _ when l.pageno = pageno ->
1676 begin match getopaque pageno with
1677 | None ->
1678 state.mode <- LinkNav (Ltgendir 0)
1679 | Some opaque ->
1680 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1681 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1682 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1683 then state.mode <- LinkNav (Ltgendir 0)
1685 | _ :: rest -> loop rest
1687 loop layout
1688 | _ -> ()
1689 end;
1690 begin match state.mode with
1691 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1692 if not (pagevisible layout pageno)
1693 then (
1694 match state.layout with
1695 | [] -> ()
1696 | l :: _ ->
1697 state.mode <- Birdseye (
1698 conf, leftx, l.pageno, hooverpageno, anchor
1701 | LinkNav (Ltgendir dir as lt) ->
1702 let linknav =
1703 let rec loop = function
1704 | [] -> lt
1705 | l :: rest ->
1706 match getopaque l.pageno with
1707 | None -> loop rest
1708 | Some opaque ->
1709 let link =
1710 let ld =
1711 if dir = 0
1712 then LDfirstvisible (l.pagex, l.pagey, dir)
1713 else (
1714 if dir > 0 then LDfirst else LDlast
1717 findlink opaque ld
1719 match link with
1720 | Lnotfound -> loop rest
1721 | Lfound n ->
1722 showlinktype (getlink opaque n);
1723 Ltexact (l.pageno, n)
1725 loop state.layout
1727 state.mode <- LinkNav linknav
1728 | _ -> ()
1729 end;
1730 preload layout;
1732 state.ghyll <- noghyll;
1733 if conf.updatecurs
1734 then (
1735 let mx, my = state.mpos in
1736 updateunder mx my;
1740 let conttiling pageno opaque =
1741 tilepage pageno opaque
1742 (if conf.preload then preloadlayout state.layout else state.layout)
1745 let gotoy_and_clear_text y =
1746 if not conf.verbose then state.text <- "";
1747 gotoy y;
1750 let getanchor () =
1751 match state.layout with
1752 | [] -> emptyanchor
1753 | l :: _ ->
1754 let coloff = l.pagecol * l.pageh in
1755 (l.pageno, (float l.pagey +. float coloff) /. float l.pageh)
1758 let getanchory (n, top) =
1759 let y, h = getpageyh n in
1760 y + (truncate (top *. float h));
1763 let gotoanchor anchor =
1764 gotoy (getanchory anchor);
1767 let addnav () =
1768 cbput state.hists.nav (getanchor ());
1771 let getnav dir =
1772 let anchor = cbgetc state.hists.nav dir in
1773 getanchory anchor;
1776 let gotoghyll y =
1777 let rec scroll f n a b =
1778 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1779 let snake f a b =
1780 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1781 if f < a
1782 then s (float f /. float a)
1783 else (
1784 if f > b
1785 then 1.0 -. s ((float (f-b) /. float (n-b)))
1786 else 1.0
1789 snake f a b
1790 and summa f n a b =
1791 (* courtesy:
1792 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1793 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1794 let iv1 = iv f in
1795 let ins = float a *. iv1
1796 and outs = float (n-b) *. iv1 in
1797 let ones = b - a in
1798 ins +. outs +. float ones
1800 let rec set (_N, _A, _B) y sy =
1801 let sum = summa 1.0 _N _A _B in
1802 let dy = float (y - sy) in
1803 state.ghyll <- (
1804 let rec gf n y1 o =
1805 if n >= _N
1806 then state.ghyll <- noghyll
1807 else
1808 let go n =
1809 let s = scroll n _N _A _B in
1810 let y1 = y1 +. ((s *. dy) /. sum) in
1811 gotoy_and_clear_text (truncate y1);
1812 state.ghyll <- gf (n+1) y1;
1814 match o with
1815 | None -> go n
1816 | Some y' -> set (_N/2, 0, 0) y' state.y
1818 gf 0 (float state.y)
1821 match conf.ghyllscroll with
1822 | None ->
1823 gotoy_and_clear_text y
1824 | Some nab ->
1825 if state.ghyll == noghyll
1826 then set nab y state.y
1827 else state.ghyll (Some y)
1830 let gotopage n top =
1831 let y, h = getpageyh n in
1832 let y = y + (truncate (top *. float h)) in
1833 gotoghyll y
1836 let gotopage1 n top =
1837 let y = getpagey n in
1838 let y = y + top in
1839 gotoghyll y
1842 let invalidate s f =
1843 state.layout <- [];
1844 state.pdims <- [];
1845 state.rects <- [];
1846 state.rects1 <- [];
1847 match state.geomcmds with
1848 | ps, [] when String.length ps = 0 ->
1849 f ();
1850 state.geomcmds <- s, [];
1852 | ps, [] ->
1853 state.geomcmds <- ps, [s, f];
1855 | ps, (s', _) :: rest when s' = s ->
1856 state.geomcmds <- ps, ((s, f) :: rest);
1858 | ps, cmds ->
1859 state.geomcmds <- ps, ((s, f) :: cmds);
1862 let opendoc path password =
1863 state.path <- path;
1864 state.password <- password;
1865 state.gen <- state.gen + 1;
1866 state.docinfo <- [];
1868 setaalevel conf.aalevel;
1869 Wsi.settitle ("llpp " ^ Filename.basename path);
1870 wcmd "open %s\000%s\000" path password;
1871 invalidate "reqlayout"
1872 (fun () ->
1873 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1876 let scalecolor c =
1877 let c = c *. conf.colorscale in
1878 (c, c, c);
1881 let scalecolor2 (r, g, b) =
1882 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1885 let represent () =
1886 let docolumns = function
1887 | Csingle -> ()
1889 | Cmulti ((columns, coverA, coverB), _) ->
1890 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1891 let rec loop pageno pdimno pdim x y rowh pdims =
1892 let rec fixrow m = if m = pageno then () else
1893 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1894 if h < rowh
1895 then (
1896 let y = y + (rowh - h) / 2 in
1897 a.(m) <- (pdimno, x, y, pdim);
1899 fixrow (m+1)
1901 if pageno = state.pagecount
1902 then fixrow (((pageno - 1) / columns) * columns)
1903 else
1904 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1905 match pdims with
1906 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1907 pdimno+1, pdim, rest
1908 | _ ->
1909 pdimno, pdim, pdims
1911 let x, y, rowh' =
1912 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1913 then (
1914 (conf.winw - state.scrollw - w) / 2,
1915 y + rowh + conf.interpagespace, h
1917 else (
1918 if (pageno - coverA) mod columns = 0
1919 then 0, y + rowh + conf.interpagespace, h
1920 else x, y, max rowh h
1923 if pageno > 1 && (pageno - coverA) mod columns = 0
1924 then fixrow (pageno - columns);
1925 a.(pageno) <- (pdimno, x, y, pdim);
1926 let x = x + w + xoff*2 + conf.interpagespace in
1927 loop (pageno+1) pdimno pdim x y rowh' pdims
1929 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1930 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1932 | Csplit (c, _) ->
1933 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1934 let rec loop pageno pdimno pdim y pdims =
1935 if pageno = state.pagecount
1936 then ()
1937 else
1938 let pdimno, ((_, w, h, _) as pdim), pdims =
1939 match pdims with
1940 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1941 pdimno+1, pdim, rest
1942 | _ ->
1943 pdimno, pdim, pdims
1945 let cw = w / c in
1946 let rec loop1 n x y =
1947 if n = c then y else (
1948 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1949 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1952 let y = loop1 0 0 y in
1953 loop (pageno+1) pdimno pdim y pdims
1955 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1956 conf.columns <- Csplit (c, a);
1958 docolumns conf.columns;
1959 state.maxy <- calcheight ();
1960 state.hscrollh <-
1961 if state.w <= conf.winw - state.scrollw
1962 then 0
1963 else state.scrollw
1965 match state.mode with
1966 | Birdseye (_, _, pageno, _, _) ->
1967 let y, h = getpageyh pageno in
1968 let top = (conf.winh - h) / 2 in
1969 gotoy (max 0 (y - top))
1970 | _ -> gotoanchor state.anchor
1973 let reshape w h =
1974 GlDraw.viewport 0 0 w h;
1975 let firsttime = state.geomcmds == firstgeomcmds in
1976 if not firsttime && nogeomcmds state.geomcmds
1977 then state.anchor <- getanchor ();
1979 conf.winw <- w;
1980 let w = truncate (float w *. conf.zoom) - state.scrollw in
1981 let w = max w 2 in
1982 conf.winh <- h;
1983 setfontsize fstate.fontsize;
1984 GlMat.mode `modelview;
1985 GlMat.load_identity ();
1987 GlMat.mode `projection;
1988 GlMat.load_identity ();
1989 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1990 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1991 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1993 let relx =
1994 if conf.zoom <= 1.0
1995 then 0.0
1996 else float state.x /. float state.w
1998 invalidate "geometry"
1999 (fun () ->
2000 state.w <- w;
2001 if not firsttime
2002 then state.x <- truncate (relx *. float w);
2003 let w =
2004 match conf.columns with
2005 | Csingle -> w
2006 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2007 | Csplit (c, _) -> w * c
2009 wcmd "geometry %d %d" w h);
2012 let enttext () =
2013 let len = String.length state.text in
2014 let drawstring s =
2015 let hscrollh =
2016 match state.mode with
2017 | Textentry _
2018 | View ->
2019 let h, _, _ = state.uioh#scrollpw in
2021 | _ -> 0
2023 let rect x w =
2024 GlDraw.rect
2025 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2026 (x+.w, float (conf.winh - hscrollh))
2029 let w = float (conf.winw - state.scrollw - 1) in
2030 if state.progress >= 0.0 && state.progress < 1.0
2031 then (
2032 GlDraw.color (0.3, 0.3, 0.3);
2033 let w1 = w *. state.progress in
2034 rect 0.0 w1;
2035 GlDraw.color (0.0, 0.0, 0.0);
2036 rect w1 (w-.w1)
2038 else (
2039 GlDraw.color (0.0, 0.0, 0.0);
2040 rect 0.0 w;
2043 GlDraw.color (1.0, 1.0, 1.0);
2044 drawstring fstate.fontsize
2045 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2047 let s =
2048 match state.mode with
2049 | Textentry ((prefix, text, _, _, _), _) ->
2050 let s =
2051 if len > 0
2052 then
2053 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2054 else
2055 Printf.sprintf "%s%s_" prefix text
2059 | _ -> state.text
2061 let s =
2062 if state.newerrmsgs
2063 then (
2064 if not (istextentry state.mode)
2065 then
2066 let s1 = "(press 'e' to review error messasges)" in
2067 if String.length s > 0 then s ^ " " ^ s1 else s1
2068 else s
2070 else s
2072 if String.length s > 0
2073 then drawstring s
2076 let gctiles () =
2077 let len = Queue.length state.tilelru in
2078 let rec loop qpos =
2079 if state.memused <= conf.memlimit
2080 then ()
2081 else (
2082 if qpos < len
2083 then
2084 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2085 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2086 let (_, pw, ph, _) = getpagedim n in
2088 gen = state.gen
2089 && colorspace = conf.colorspace
2090 && angle = conf.angle
2091 && pagew = pw
2092 && pageh = ph
2093 && (
2094 let layout =
2095 match state.throttle with
2096 | None ->
2097 if conf.preload
2098 then preloadlayout state.layout
2099 else state.layout
2100 | Some (layout, _, _) ->
2101 layout
2103 let x = col*conf.tilew
2104 and y = row*conf.tileh in
2105 tilevisible layout n x y
2107 then Queue.push lruitem state.tilelru
2108 else (
2109 wcmd "freetile %s" p;
2110 state.memused <- state.memused - s;
2111 state.uioh#infochanged Memused;
2112 Hashtbl.remove state.tilemap k;
2114 loop (qpos+1)
2117 loop 0
2120 let flushtiles () =
2121 Queue.iter (fun (k, p, s) ->
2122 wcmd "freetile %s" p;
2123 state.memused <- state.memused - s;
2124 state.uioh#infochanged Memused;
2125 Hashtbl.remove state.tilemap k;
2126 ) state.tilelru;
2127 Queue.clear state.tilelru;
2128 load state.layout;
2131 let logcurrently = function
2132 | Idle -> dolog "Idle"
2133 | Loading (l, gen) ->
2134 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2135 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2136 dolog
2137 "Tiling %d[%d,%d] page=%s cs=%s angle"
2138 l.pageno col row pageopaque
2139 (colorspace_to_string colorspace)
2141 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2142 angle gen conf.angle state.gen
2143 tilew tileh
2144 conf.tilew conf.tileh
2146 | Outlining _ ->
2147 dolog "outlining"
2150 let act cmds =
2151 (* dolog "%S" cmds; *)
2152 let op, args =
2153 let spacepos =
2154 try String.index cmds ' '
2155 with Not_found -> -1
2157 if spacepos = -1
2158 then cmds, ""
2159 else
2160 let l = String.length cmds in
2161 let op = String.sub cmds 0 spacepos in
2162 op, begin
2163 if l - spacepos < 2 then ""
2164 else String.sub cmds (spacepos+1) (l-spacepos-1)
2167 match op with
2168 | "clear" ->
2169 state.uioh#infochanged Pdim;
2170 state.pdims <- [];
2172 | "clearrects" ->
2173 state.rects <- state.rects1;
2174 G.postRedisplay "clearrects";
2176 | "continue" ->
2177 let n =
2178 try Scanf.sscanf args "%u" (fun n -> n)
2179 with exn ->
2180 dolog "error processing 'continue' %S: %s"
2181 cmds (Printexc.to_string exn);
2182 exit 1;
2184 state.pagecount <- n;
2185 begin match state.currently with
2186 | Outlining l ->
2187 state.currently <- Idle;
2188 state.outlines <- Array.of_list (List.rev l)
2189 | _ -> ()
2190 end;
2192 let cur, cmds = state.geomcmds in
2193 if String.length cur = 0
2194 then failwith "umpossible";
2196 begin match List.rev cmds with
2197 | [] ->
2198 state.geomcmds <- "", [];
2199 represent ();
2200 | (s, f) :: rest ->
2201 f ();
2202 state.geomcmds <- s, List.rev rest;
2203 end;
2204 if conf.maxwait = None
2205 then G.postRedisplay "continue";
2207 | "title" ->
2208 Wsi.settitle args
2210 | "msg" ->
2211 showtext ' ' args
2213 | "vmsg" ->
2214 if conf.verbose
2215 then showtext ' ' args
2217 | "progress" ->
2218 let progress, text =
2220 Scanf.sscanf args "%f %n"
2221 (fun f pos ->
2222 f, String.sub args pos (String.length args - pos))
2223 with exn ->
2224 dolog "error processing 'progress' %S: %s"
2225 cmds (Printexc.to_string exn);
2226 exit 1;
2228 state.text <- text;
2229 state.progress <- progress;
2230 G.postRedisplay "progress"
2232 | "firstmatch" ->
2233 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2235 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2236 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2237 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2238 with exn ->
2239 dolog "error processing 'firstmatch' %S: %s"
2240 cmds (Printexc.to_string exn);
2241 exit 1;
2243 let y = (getpagey pageno) + truncate y0 in
2244 addnav ();
2245 gotoy y;
2246 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2248 | "match" ->
2249 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2251 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2252 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2253 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2254 with exn ->
2255 dolog "error processing 'match' %S: %s"
2256 cmds (Printexc.to_string exn);
2257 exit 1;
2259 state.rects1 <-
2260 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2262 | "page" ->
2263 let pageopaque, t =
2265 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2266 with exn ->
2267 dolog "error processing 'page' %S: %s"
2268 cmds (Printexc.to_string exn);
2269 exit 1;
2271 begin match state.currently with
2272 | Loading (l, gen) ->
2273 vlog "page %d took %f sec" l.pageno t;
2274 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2275 begin match state.throttle with
2276 | None ->
2277 let preloadedpages =
2278 if conf.preload
2279 then preloadlayout state.layout
2280 else state.layout
2282 let evict () =
2283 let module IntSet =
2284 Set.Make (struct type t = int let compare = (-) end) in
2285 let set =
2286 List.fold_left (fun s l -> IntSet.add l.pageno s)
2287 IntSet.empty preloadedpages
2289 let evictedpages =
2290 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2291 if not (IntSet.mem pageno set)
2292 then (
2293 wcmd "freepage %s" opaque;
2294 key :: accu
2296 else accu
2297 ) state.pagemap []
2299 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2301 evict ();
2302 state.currently <- Idle;
2303 if gen = state.gen
2304 then (
2305 tilepage l.pageno pageopaque state.layout;
2306 load state.layout;
2307 load preloadedpages;
2308 if pagevisible state.layout l.pageno
2309 && layoutready state.layout
2310 then G.postRedisplay "page";
2313 | Some (layout, _, _) ->
2314 state.currently <- Idle;
2315 tilepage l.pageno pageopaque layout;
2316 load state.layout
2317 end;
2319 | _ ->
2320 dolog "Inconsistent loading state";
2321 logcurrently state.currently;
2322 exit 1
2325 | "tile" ->
2326 let (x, y, opaque, size, t) =
2328 Scanf.sscanf args "%u %u %s %u %f"
2329 (fun x y p size t -> (x, y, p, size, t))
2330 with exn ->
2331 dolog "error processing 'tile' %S: %s"
2332 cmds (Printexc.to_string exn);
2333 exit 1;
2335 begin match state.currently with
2336 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2337 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2339 if tilew != conf.tilew || tileh != conf.tileh
2340 then (
2341 wcmd "freetile %s" opaque;
2342 state.currently <- Idle;
2343 load state.layout;
2345 else (
2346 puttileopaque l col row gen cs angle opaque size t;
2347 state.memused <- state.memused + size;
2348 state.uioh#infochanged Memused;
2349 gctiles ();
2350 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2351 opaque, size) state.tilelru;
2353 let layout =
2354 match state.throttle with
2355 | None -> state.layout
2356 | Some (layout, _, _) -> layout
2359 state.currently <- Idle;
2360 if gen = state.gen
2361 && conf.colorspace = cs
2362 && conf.angle = angle
2363 && tilevisible layout l.pageno x y
2364 then conttiling l.pageno pageopaque;
2366 begin match state.throttle with
2367 | None ->
2368 preload state.layout;
2369 if gen = state.gen
2370 && conf.colorspace = cs
2371 && conf.angle = angle
2372 && tilevisible state.layout l.pageno x y
2373 then G.postRedisplay "tile nothrottle";
2375 | Some (layout, y, _) ->
2376 let ready = layoutready layout in
2377 if ready
2378 then (
2379 state.y <- y;
2380 state.layout <- layout;
2381 state.throttle <- None;
2382 G.postRedisplay "throttle";
2384 else load layout;
2385 end;
2388 | _ ->
2389 dolog "Inconsistent tiling state";
2390 logcurrently state.currently;
2391 exit 1
2394 | "pdim" ->
2395 let pdim =
2397 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2398 with exn ->
2399 dolog "error processing 'pdim' %S: %s"
2400 cmds (Printexc.to_string exn);
2401 exit 1;
2403 state.uioh#infochanged Pdim;
2404 state.pdims <- pdim :: state.pdims
2406 | "o" ->
2407 let (l, n, t, h, pos) =
2409 Scanf.sscanf args "%u %u %d %u %n"
2410 (fun l n t h pos -> l, n, t, h, pos)
2411 with exn ->
2412 dolog "error processing 'o' %S: %s"
2413 cmds (Printexc.to_string exn);
2414 exit 1;
2416 let s = String.sub args pos (String.length args - pos) in
2417 let outline = (s, l, (n, float t /. float h)) in
2418 begin match state.currently with
2419 | Outlining outlines ->
2420 state.currently <- Outlining (outline :: outlines)
2421 | Idle ->
2422 state.currently <- Outlining [outline]
2423 | currently ->
2424 dolog "invalid outlining state";
2425 logcurrently currently
2428 | "info" ->
2429 state.docinfo <- (1, args) :: state.docinfo
2431 | "infoend" ->
2432 state.uioh#infochanged Docinfo;
2433 state.docinfo <- List.rev state.docinfo
2435 | _ ->
2436 dolog "unknown cmd `%S'" cmds
2439 let onhist cb =
2440 let rc = cb.rc in
2441 let action = function
2442 | HCprev -> cbget cb ~-1
2443 | HCnext -> cbget cb 1
2444 | HCfirst -> cbget cb ~-(cb.rc)
2445 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2446 and cancel () = cb.rc <- rc
2447 in (action, cancel)
2450 let search pattern forward =
2451 if String.length pattern > 0
2452 then
2453 let pn, py =
2454 match state.layout with
2455 | [] -> 0, 0
2456 | l :: _ ->
2457 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2459 wcmd "search %d %d %d %d,%s\000"
2460 (btod conf.icase) pn py (btod forward) pattern;
2463 let intentry text key =
2464 let c =
2465 if key >= 32 && key < 127
2466 then Char.chr key
2467 else '\000'
2469 match c with
2470 | '0' .. '9' ->
2471 let text = addchar text c in
2472 TEcont text
2474 | _ ->
2475 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2476 TEcont text
2479 let linknentry text key =
2480 let c =
2481 if key >= 32 && key < 127
2482 then Char.chr key
2483 else '\000'
2485 match c with
2486 | 'a' .. 'z' ->
2487 let text = addchar text c in
2488 TEcont text
2490 | _ ->
2491 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2492 TEcont text
2495 let linkndone f s =
2496 if String.length s > 0
2497 then (
2498 let n =
2499 let l = String.length s in
2500 let rec loop pos n = if pos = l then n else
2501 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2502 loop (pos+1) (n*26 + m)
2503 in loop 0 0
2505 let rec loop n = function
2506 | [] -> ()
2507 | l :: rest ->
2508 match getopaque l.pageno with
2509 | None -> loop n rest
2510 | Some opaque ->
2511 let m = getlinkcount opaque in
2512 if n < m
2513 then (
2514 let under = getlink opaque n in
2515 f under
2517 else loop (n-m) rest
2519 loop n state.layout;
2523 let textentry text key =
2524 if key land 0xff00 = 0xff00
2525 then TEcont text
2526 else TEcont (text ^ Wsi.toutf8 key)
2529 let reqlayout angle proportional =
2530 match state.throttle with
2531 | None ->
2532 if nogeomcmds state.geomcmds
2533 then state.anchor <- getanchor ();
2534 conf.angle <- angle mod 360;
2535 if conf.angle != 0
2536 then (
2537 match state.mode with
2538 | LinkNav _ -> state.mode <- View
2539 | _ -> ()
2541 conf.proportional <- proportional;
2542 invalidate "reqlayout"
2543 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2544 | _ -> ()
2547 let settrim trimmargins trimfuzz =
2548 if nogeomcmds state.geomcmds
2549 then state.anchor <- getanchor ();
2550 conf.trimmargins <- trimmargins;
2551 conf.trimfuzz <- trimfuzz;
2552 let x0, y0, x1, y1 = trimfuzz in
2553 invalidate "settrim"
2554 (fun () ->
2555 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2556 Hashtbl.iter (fun _ opaque ->
2557 wcmd "freepage %s" opaque;
2558 ) state.pagemap;
2559 Hashtbl.clear state.pagemap;
2562 let setzoom zoom =
2563 match state.throttle with
2564 | None ->
2565 let zoom = max 0.01 zoom in
2566 if zoom <> conf.zoom
2567 then (
2568 state.prevzoom <- conf.zoom;
2569 conf.zoom <- zoom;
2570 reshape conf.winw conf.winh;
2571 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2574 | Some (layout, y, started) ->
2575 let time =
2576 match conf.maxwait with
2577 | None -> 0.0
2578 | Some t -> t
2580 let dt = now () -. started in
2581 if dt > time
2582 then (
2583 state.y <- y;
2584 load layout;
2588 let setcolumns mode columns coverA coverB =
2589 if columns < 0
2590 then (
2591 if isbirdseye mode
2592 then showtext '!' "split mode doesn't work in bird's eye"
2593 else (
2594 conf.columns <- Csplit (-columns, [||]);
2595 state.x <- 0;
2596 conf.zoom <- 1.0;
2599 else (
2600 if columns < 2
2601 then (
2602 conf.columns <- Csingle;
2603 state.x <- 0;
2604 setzoom 1.0;
2606 else (
2607 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2608 conf.zoom <- 1.0;
2611 reshape conf.winw conf.winh;
2614 let enterbirdseye () =
2615 let zoom = float conf.thumbw /. float conf.winw in
2616 let birdseyepageno =
2617 let cy = conf.winh / 2 in
2618 let fold = function
2619 | [] -> 0
2620 | l :: rest ->
2621 let rec fold best = function
2622 | [] -> best.pageno
2623 | l :: rest ->
2624 let d = cy - (l.pagedispy + l.pagevh/2)
2625 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2626 if abs d < abs dbest
2627 then fold l rest
2628 else best.pageno
2629 in fold l rest
2631 fold state.layout
2633 state.mode <- Birdseye (
2634 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2636 conf.zoom <- zoom;
2637 conf.presentation <- false;
2638 conf.interpagespace <- 10;
2639 conf.hlinks <- false;
2640 state.x <- 0;
2641 state.mstate <- Mnone;
2642 conf.maxwait <- None;
2643 conf.columns <- (
2644 match conf.beyecolumns with
2645 | Some c ->
2646 conf.zoom <- 1.0;
2647 Cmulti ((c, 0, 0), [||])
2648 | None -> Csingle
2650 Wsi.setcursor Wsi.CURSOR_INHERIT;
2651 if conf.verbose
2652 then
2653 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2654 (100.0*.zoom)
2655 else
2656 state.text <- ""
2658 reshape conf.winw conf.winh;
2661 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2662 state.mode <- View;
2663 conf.zoom <- c.zoom;
2664 conf.presentation <- c.presentation;
2665 conf.interpagespace <- c.interpagespace;
2666 conf.maxwait <- c.maxwait;
2667 conf.hlinks <- c.hlinks;
2668 conf.beyecolumns <- (
2669 match conf.columns with
2670 | Cmulti ((c, _, _), _) -> Some c
2671 | Csingle -> None
2672 | Csplit _ -> assert false
2674 conf.columns <- (
2675 match c.columns with
2676 | Cmulti (c, _) -> Cmulti (c, [||])
2677 | Csingle -> Csingle
2678 | Csplit _ -> failwith "leaving bird's eye split mode"
2680 state.x <- leftx;
2681 if conf.verbose
2682 then
2683 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2684 (100.0*.conf.zoom)
2686 reshape conf.winw conf.winh;
2687 state.anchor <- if goback then anchor else (pageno, 0.0);
2690 let togglebirdseye () =
2691 match state.mode with
2692 | Birdseye vals -> leavebirdseye vals true
2693 | View -> enterbirdseye ()
2694 | _ -> ()
2697 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2698 let pageno = max 0 (pageno - incr) in
2699 let rec loop = function
2700 | [] -> gotopage1 pageno 0
2701 | l :: _ when l.pageno = pageno ->
2702 if l.pagedispy >= 0 && l.pagey = 0
2703 then G.postRedisplay "upbirdseye"
2704 else gotopage1 pageno 0
2705 | _ :: rest -> loop rest
2707 loop state.layout;
2708 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2711 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2712 let pageno = min (state.pagecount - 1) (pageno + incr) in
2713 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2714 let rec loop = function
2715 | [] ->
2716 let y, h = getpageyh pageno in
2717 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2718 gotoy (clamp dy)
2719 | l :: _ when l.pageno = pageno ->
2720 if l.pagevh != l.pageh
2721 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2722 else G.postRedisplay "downbirdseye"
2723 | _ :: rest -> loop rest
2725 loop state.layout
2728 let optentry mode _ key =
2729 let btos b = if b then "on" else "off" in
2730 if key >= 32 && key < 127
2731 then
2732 let c = Char.chr key in
2733 match c with
2734 | 's' ->
2735 let ondone s =
2736 try conf.scrollstep <- int_of_string s with exc ->
2737 state.text <- Printf.sprintf "bad integer `%s': %s"
2738 s (Printexc.to_string exc)
2740 TEswitch ("scroll step: ", "", None, intentry, ondone)
2742 | 'A' ->
2743 let ondone s =
2745 conf.autoscrollstep <- int_of_string s;
2746 if state.autoscroll <> None
2747 then state.autoscroll <- Some conf.autoscrollstep
2748 with exc ->
2749 state.text <- Printf.sprintf "bad integer `%s': %s"
2750 s (Printexc.to_string exc)
2752 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2754 | 'C' ->
2755 let mode = state.mode in
2756 let ondone s =
2758 let n, a, b = multicolumns_of_string s in
2759 setcolumns mode n a b;
2760 with exc ->
2761 state.text <- Printf.sprintf "bad columns `%s': %s"
2762 s (Printexc.to_string exc)
2764 TEswitch ("columns: ", "", None, textentry, ondone)
2766 | 'Z' ->
2767 let ondone s =
2769 let zoom = float (int_of_string s) /. 100.0 in
2770 setzoom zoom
2771 with exc ->
2772 state.text <- Printf.sprintf "bad integer `%s': %s"
2773 s (Printexc.to_string exc)
2775 TEswitch ("zoom: ", "", None, intentry, ondone)
2777 | 't' ->
2778 let ondone s =
2780 conf.thumbw <- bound (int_of_string s) 2 4096;
2781 state.text <-
2782 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2783 begin match mode with
2784 | Birdseye beye ->
2785 leavebirdseye beye false;
2786 enterbirdseye ();
2787 | _ -> ();
2789 with exc ->
2790 state.text <- Printf.sprintf "bad integer `%s': %s"
2791 s (Printexc.to_string exc)
2793 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2795 | 'R' ->
2796 let ondone s =
2797 match try
2798 Some (int_of_string s)
2799 with exc ->
2800 state.text <- Printf.sprintf "bad integer `%s': %s"
2801 s (Printexc.to_string exc);
2802 None
2803 with
2804 | Some angle -> reqlayout angle conf.proportional
2805 | None -> ()
2807 TEswitch ("rotation: ", "", None, intentry, ondone)
2809 | 'i' ->
2810 conf.icase <- not conf.icase;
2811 TEdone ("case insensitive search " ^ (btos conf.icase))
2813 | 'p' ->
2814 conf.preload <- not conf.preload;
2815 gotoy state.y;
2816 TEdone ("preload " ^ (btos conf.preload))
2818 | 'v' ->
2819 conf.verbose <- not conf.verbose;
2820 TEdone ("verbose " ^ (btos conf.verbose))
2822 | 'd' ->
2823 conf.debug <- not conf.debug;
2824 TEdone ("debug " ^ (btos conf.debug))
2826 | 'h' ->
2827 conf.maxhfit <- not conf.maxhfit;
2828 state.maxy <-
2829 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2830 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2832 | 'c' ->
2833 conf.crophack <- not conf.crophack;
2834 TEdone ("crophack " ^ btos conf.crophack)
2836 | 'a' ->
2837 let s =
2838 match conf.maxwait with
2839 | None ->
2840 conf.maxwait <- Some infinity;
2841 "always wait for page to complete"
2842 | Some _ ->
2843 conf.maxwait <- None;
2844 "show placeholder if page is not ready"
2846 TEdone s
2848 | 'f' ->
2849 conf.underinfo <- not conf.underinfo;
2850 TEdone ("underinfo " ^ btos conf.underinfo)
2852 | 'P' ->
2853 conf.savebmarks <- not conf.savebmarks;
2854 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2856 | 'S' ->
2857 let ondone s =
2859 let pageno, py =
2860 match state.layout with
2861 | [] -> 0, 0
2862 | l :: _ ->
2863 l.pageno, l.pagey
2865 conf.interpagespace <- int_of_string s;
2866 state.maxy <- calcheight ();
2867 let y = getpagey pageno in
2868 gotoy (y + py)
2869 with exc ->
2870 state.text <- Printf.sprintf "bad integer `%s': %s"
2871 s (Printexc.to_string exc)
2873 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2875 | 'l' ->
2876 reqlayout conf.angle (not conf.proportional);
2877 TEdone ("proportional display " ^ btos conf.proportional)
2879 | 'T' ->
2880 settrim (not conf.trimmargins) conf.trimfuzz;
2881 TEdone ("trim margins " ^ btos conf.trimmargins)
2883 | 'I' ->
2884 conf.invert <- not conf.invert;
2885 TEdone ("invert colors " ^ btos conf.invert)
2887 | 'x' ->
2888 let ondone s =
2889 cbput state.hists.sel s;
2890 conf.selcmd <- s;
2892 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2893 textentry, ondone)
2895 | _ ->
2896 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2897 TEstop
2898 else
2899 TEcont state.text
2902 class type lvsource = object
2903 method getitemcount : int
2904 method getitem : int -> (string * int)
2905 method hasaction : int -> bool
2906 method exit :
2907 uioh:uioh ->
2908 cancel:bool ->
2909 active:int ->
2910 first:int ->
2911 pan:int ->
2912 qsearch:string ->
2913 uioh option
2914 method getactive : int
2915 method getfirst : int
2916 method getqsearch : string
2917 method setqsearch : string -> unit
2918 method getpan : int
2919 end;;
2921 class virtual lvsourcebase = object
2922 val mutable m_active = 0
2923 val mutable m_first = 0
2924 val mutable m_qsearch = ""
2925 val mutable m_pan = 0
2926 method getactive = m_active
2927 method getfirst = m_first
2928 method getqsearch = m_qsearch
2929 method getpan = m_pan
2930 method setqsearch s = m_qsearch <- s
2931 end;;
2933 let withoutlastutf8 s =
2934 let len = String.length s in
2935 if len = 0
2936 then s
2937 else
2938 let rec find pos =
2939 if pos = 0
2940 then pos
2941 else
2942 let b = Char.code s.[pos] in
2943 if b land 0b110000 = 0b11000000
2944 then find (pos-1)
2945 else pos-1
2947 let first =
2948 if Char.code s.[len-1] land 0x80 = 0
2949 then len-1
2950 else find (len-1)
2952 String.sub s 0 first;
2955 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2956 let enttext te =
2957 state.mode <- Textentry (te, onleave);
2958 state.text <- "";
2959 enttext ();
2960 G.postRedisplay "textentrykeyboard enttext";
2962 let histaction cmd =
2963 match opthist with
2964 | None -> ()
2965 | Some (action, _) ->
2966 state.mode <- Textentry (
2967 (c, action cmd, opthist, onkey, ondone), onleave
2969 G.postRedisplay "textentry histaction"
2971 match key with
2972 | 0xff08 -> (* backspace *)
2973 let s = withoutlastutf8 text in
2974 let len = String.length s in
2975 if len = 0
2976 then (
2977 onleave Cancel;
2978 G.postRedisplay "textentrykeyboard after cancel";
2980 else (
2981 enttext (c, s, opthist, onkey, ondone)
2984 | 0xff0d ->
2985 ondone text;
2986 onleave Confirm;
2987 G.postRedisplay "textentrykeyboard after confirm"
2989 | 0xff52 -> histaction HCprev
2990 | 0xff54 -> histaction HCnext
2991 | 0xff50 -> histaction HCfirst
2992 | 0xff57 -> histaction HClast
2994 | 0xff1b -> (* escape*)
2995 if String.length text = 0
2996 then (
2997 begin match opthist with
2998 | None -> ()
2999 | Some (_, onhistcancel) -> onhistcancel ()
3000 end;
3001 onleave Cancel;
3002 state.text <- "";
3003 G.postRedisplay "textentrykeyboard after cancel2"
3005 else (
3006 enttext (c, "", opthist, onkey, ondone)
3009 | 0xff9f | 0xffff -> () (* delete *)
3011 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3012 begin match onkey text key with
3013 | TEdone text ->
3014 ondone text;
3015 onleave Confirm;
3016 G.postRedisplay "textentrykeyboard after confirm2";
3018 | TEcont text ->
3019 enttext (c, text, opthist, onkey, ondone);
3021 | TEstop ->
3022 onleave Cancel;
3023 G.postRedisplay "textentrykeyboard after cancel3"
3025 | TEswitch te ->
3026 state.mode <- Textentry (te, onleave);
3027 G.postRedisplay "textentrykeyboard switch";
3028 end;
3030 | _ ->
3031 vlog "unhandled key %s" (Wsi.keyname key)
3034 let firstof first active =
3035 if first > active || abs (first - active) > fstate.maxrows - 1
3036 then max 0 (active - (fstate.maxrows/2))
3037 else first
3040 let calcfirst first active =
3041 if active > first
3042 then
3043 let rows = active - first in
3044 if rows > fstate.maxrows then active - fstate.maxrows else first
3045 else active
3048 let scrollph y maxy =
3049 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3050 let sh = float conf.winh /. sh in
3051 let sh = max sh (float conf.scrollh) in
3053 let percent =
3054 if y = state.maxy
3055 then 1.0
3056 else float y /. float maxy
3058 let position = (float conf.winh -. sh) *. percent in
3060 let position =
3061 if position +. sh > float conf.winh
3062 then float conf.winh -. sh
3063 else position
3065 position, sh;
3068 let coe s = (s :> uioh);;
3070 class listview ~(source:lvsource) ~trusted ~modehash =
3071 object (self)
3072 val m_pan = source#getpan
3073 val m_first = source#getfirst
3074 val m_active = source#getactive
3075 val m_qsearch = source#getqsearch
3076 val m_prev_uioh = state.uioh
3078 method private elemunder y =
3079 let n = y / (fstate.fontsize+1) in
3080 if m_first + n < source#getitemcount
3081 then (
3082 if source#hasaction (m_first + n)
3083 then Some (m_first + n)
3084 else None
3086 else None
3088 method display =
3089 Gl.enable `blend;
3090 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3091 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3092 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3093 GlDraw.color (1., 1., 1.);
3094 Gl.enable `texture_2d;
3095 let fs = fstate.fontsize in
3096 let nfs = fs + 1 in
3097 let ww = fstate.wwidth in
3098 let tabw = 30.0*.ww in
3099 let itemcount = source#getitemcount in
3100 let rec loop row =
3101 if (row - m_first) * nfs > conf.winh
3102 then ()
3103 else (
3104 if row >= 0 && row < itemcount
3105 then (
3106 let (s, level) = source#getitem row in
3107 let y = (row - m_first) * nfs in
3108 let x = 5.0 +. float (level + m_pan) *. ww in
3109 if row = m_active
3110 then (
3111 Gl.disable `texture_2d;
3112 GlDraw.polygon_mode `both `line;
3113 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3114 GlDraw.rect (1., float (y + 1))
3115 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3116 GlDraw.polygon_mode `both `fill;
3117 GlDraw.color (1., 1., 1.);
3118 Gl.enable `texture_2d;
3121 let drawtabularstring s =
3122 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3123 if trusted
3124 then
3125 let tabpos = try String.index s '\t' with Not_found -> -1 in
3126 if tabpos > 0
3127 then
3128 let len = String.length s - tabpos - 1 in
3129 let s1 = String.sub s 0 tabpos
3130 and s2 = String.sub s (tabpos + 1) len in
3131 let nx = drawstr x s1 in
3132 let sw = nx -. x in
3133 let x = x +. (max tabw sw) in
3134 drawstr x s2
3135 else
3136 drawstr x s
3137 else
3138 drawstr x s
3140 let _ = drawtabularstring s in
3141 loop (row+1)
3145 loop m_first;
3146 Gl.disable `blend;
3147 Gl.disable `texture_2d;
3149 method updownlevel incr =
3150 let len = source#getitemcount in
3151 let curlevel =
3152 if m_active >= 0 && m_active < len
3153 then snd (source#getitem m_active)
3154 else -1
3156 let rec flow i =
3157 if i = len then i-1 else if i = -1 then 0 else
3158 let _, l = source#getitem i in
3159 if l != curlevel then i else flow (i+incr)
3161 let active = flow m_active in
3162 let first = calcfirst m_first active in
3163 G.postRedisplay "outline updownlevel";
3164 {< m_active = active; m_first = first >}
3166 method private key1 key mask =
3167 let set1 active first qsearch =
3168 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3170 let search active pattern incr =
3171 let dosearch re =
3172 let rec loop n =
3173 if n >= 0 && n < source#getitemcount
3174 then (
3175 let s, _ = source#getitem n in
3177 (try ignore (Str.search_forward re s 0); true
3178 with Not_found -> false)
3179 then Some n
3180 else loop (n + incr)
3182 else None
3184 loop active
3187 let re = Str.regexp_case_fold pattern in
3188 dosearch re
3189 with Failure s ->
3190 state.text <- s;
3191 None
3193 let itemcount = source#getitemcount in
3194 let find start incr =
3195 let rec find i =
3196 if i = -1 || i = itemcount
3197 then -1
3198 else (
3199 if source#hasaction i
3200 then i
3201 else find (i + incr)
3204 find start
3206 let set active first =
3207 let first = bound first 0 (itemcount - fstate.maxrows) in
3208 state.text <- "";
3209 coe {< m_active = active; m_first = first >}
3211 let navigate incr =
3212 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3213 let active, first =
3214 let incr1 = if incr > 0 then 1 else -1 in
3215 if isvisible m_first m_active
3216 then
3217 let next =
3218 let next = m_active + incr in
3219 let next =
3220 if next < 0 || next >= itemcount
3221 then -1
3222 else find next incr1
3224 if next = -1 || abs (m_active - next) > fstate.maxrows
3225 then -1
3226 else next
3228 if next = -1
3229 then
3230 let first = m_first + incr in
3231 let first = bound first 0 (itemcount - 1) in
3232 let next =
3233 let next = m_active + incr in
3234 let next = bound next 0 (itemcount - 1) in
3235 find next ~-incr1
3237 let active = if next = -1 then m_active else next in
3238 active, first
3239 else
3240 let first = min next m_first in
3241 let first =
3242 if abs (next - first) > fstate.maxrows
3243 then first + incr
3244 else first
3246 next, first
3247 else
3248 let first = m_first + incr in
3249 let first = bound first 0 (itemcount - 1) in
3250 let active =
3251 let next = m_active + incr in
3252 let next = bound next 0 (itemcount - 1) in
3253 let next = find next incr1 in
3254 let active =
3255 if next = -1 || abs (m_active - first) > fstate.maxrows
3256 then (
3257 let active = if m_active = -1 then next else m_active in
3258 active
3260 else next
3262 if isvisible first active
3263 then active
3264 else -1
3266 active, first
3268 G.postRedisplay "listview navigate";
3269 set active first;
3271 match key with
3272 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3273 let incr = if key = 0x72 then -1 else 1 in
3274 let active, first =
3275 match search (m_active + incr) m_qsearch incr with
3276 | None ->
3277 state.text <- m_qsearch ^ " [not found]";
3278 m_active, m_first
3279 | Some active ->
3280 state.text <- m_qsearch;
3281 active, firstof m_first active
3283 G.postRedisplay "listview ctrl-r/s";
3284 set1 active first m_qsearch;
3286 | 0xff08 -> (* backspace *)
3287 if String.length m_qsearch = 0
3288 then coe self
3289 else (
3290 let qsearch = withoutlastutf8 m_qsearch in
3291 let len = String.length qsearch in
3292 if len = 0
3293 then (
3294 state.text <- "";
3295 G.postRedisplay "listview empty qsearch";
3296 set1 m_active m_first "";
3298 else
3299 let active, first =
3300 match search m_active qsearch ~-1 with
3301 | None ->
3302 state.text <- qsearch ^ " [not found]";
3303 m_active, m_first
3304 | Some active ->
3305 state.text <- qsearch;
3306 active, firstof m_first active
3308 G.postRedisplay "listview backspace qsearch";
3309 set1 active first qsearch
3312 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3313 let pattern = m_qsearch ^ Wsi.toutf8 key in
3314 let active, first =
3315 match search m_active pattern 1 with
3316 | None ->
3317 state.text <- pattern ^ " [not found]";
3318 m_active, m_first
3319 | Some active ->
3320 state.text <- pattern;
3321 active, firstof m_first active
3323 G.postRedisplay "listview qsearch add";
3324 set1 active first pattern;
3326 | 0xff1b -> (* escape *)
3327 state.text <- "";
3328 if String.length m_qsearch = 0
3329 then (
3330 G.postRedisplay "list view escape";
3331 begin
3332 match
3333 source#exit (coe self) true m_active m_first m_pan m_qsearch
3334 with
3335 | None -> m_prev_uioh
3336 | Some uioh -> uioh
3339 else (
3340 G.postRedisplay "list view kill qsearch";
3341 source#setqsearch "";
3342 coe {< m_qsearch = "" >}
3345 | 0xff0d -> (* return *)
3346 state.text <- "";
3347 let self = {< m_qsearch = "" >} in
3348 source#setqsearch "";
3349 let opt =
3350 G.postRedisplay "listview enter";
3351 if m_active >= 0 && m_active < source#getitemcount
3352 then (
3353 source#exit (coe self) false m_active m_first m_pan "";
3355 else (
3356 source#exit (coe self) true m_active m_first m_pan "";
3359 begin match opt with
3360 | None -> m_prev_uioh
3361 | Some uioh -> uioh
3364 | 0xff9f | 0xffff -> (* delete *)
3365 coe self
3367 | 0xff52 -> navigate ~-1 (* up *)
3368 | 0xff54 -> navigate 1 (* down *)
3369 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3370 | 0xff56 -> navigate fstate.maxrows (* next *)
3372 | 0xff53 -> (* right *)
3373 state.text <- "";
3374 G.postRedisplay "listview right";
3375 coe {< m_pan = m_pan - 1 >}
3377 | 0xff51 -> (* left *)
3378 state.text <- "";
3379 G.postRedisplay "listview left";
3380 coe {< m_pan = m_pan + 1 >}
3382 | 0xff50 -> (* home *)
3383 let active = find 0 1 in
3384 G.postRedisplay "listview home";
3385 set active 0;
3387 | 0xff57 -> (* end *)
3388 let first = max 0 (itemcount - fstate.maxrows) in
3389 let active = find (itemcount - 1) ~-1 in
3390 G.postRedisplay "listview end";
3391 set active first;
3393 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3394 coe self
3396 | _ ->
3397 dolog "listview unknown key %#x" key; coe self
3399 method key key mask =
3400 match state.mode with
3401 | Textentry te -> textentrykeyboard key mask te; coe self
3402 | _ -> self#key1 key mask
3404 method button button down x y _ =
3405 let opt =
3406 match button with
3407 | 1 when x > conf.winw - conf.scrollbw ->
3408 G.postRedisplay "listview scroll";
3409 if down
3410 then
3411 let _, position, sh = self#scrollph in
3412 if y > truncate position && y < truncate (position +. sh)
3413 then (
3414 state.mstate <- Mscrolly;
3415 Some (coe self)
3417 else
3418 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3419 let first = truncate (s *. float source#getitemcount) in
3420 let first = min source#getitemcount first in
3421 Some (coe {< m_first = first; m_active = first >})
3422 else (
3423 state.mstate <- Mnone;
3424 Some (coe self);
3426 | 1 when not down ->
3427 begin match self#elemunder y with
3428 | Some n ->
3429 G.postRedisplay "listview click";
3430 source#exit
3431 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3432 | _ ->
3433 Some (coe self)
3435 | n when (n == 4 || n == 5) && not down ->
3436 let len = source#getitemcount in
3437 let first =
3438 if n = 5 && m_first + fstate.maxrows >= len
3439 then
3440 m_first
3441 else
3442 let first = m_first + (if n == 4 then -1 else 1) in
3443 bound first 0 (len - 1)
3445 G.postRedisplay "listview wheel";
3446 Some (coe {< m_first = first >})
3447 | _ ->
3448 Some (coe self)
3450 match opt with
3451 | None -> m_prev_uioh
3452 | Some uioh -> uioh
3454 method motion _ y =
3455 match state.mstate with
3456 | Mscrolly ->
3457 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3458 let first = truncate (s *. float source#getitemcount) in
3459 let first = min source#getitemcount first in
3460 G.postRedisplay "listview motion";
3461 coe {< m_first = first; m_active = first >}
3462 | _ -> coe self
3464 method pmotion x y =
3465 if x < conf.winw - conf.scrollbw
3466 then
3467 let n =
3468 match self#elemunder y with
3469 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3470 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3472 let o =
3473 if n != m_active
3474 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3475 else self
3477 coe o
3478 else (
3479 Wsi.setcursor Wsi.CURSOR_INHERIT;
3480 coe self
3483 method infochanged _ = ()
3485 method scrollpw = (0, 0.0, 0.0)
3486 method scrollph =
3487 let nfs = fstate.fontsize + 1 in
3488 let y = m_first * nfs in
3489 let itemcount = source#getitemcount in
3490 let maxi = max 0 (itemcount - fstate.maxrows) in
3491 let maxy = maxi * nfs in
3492 let p, h = scrollph y maxy in
3493 conf.scrollbw, p, h
3495 method modehash = modehash
3496 end;;
3498 class outlinelistview ~source =
3499 object (self)
3500 inherit listview
3501 ~source:(source :> lvsource)
3502 ~trusted:false
3503 ~modehash:(findkeyhash conf "outline")
3504 as super
3506 method key key mask =
3507 let calcfirst first active =
3508 if active > first
3509 then
3510 let rows = active - first in
3511 if rows > fstate.maxrows then active - fstate.maxrows else first
3512 else active
3514 let navigate incr =
3515 let active = m_active + incr in
3516 let active = bound active 0 (source#getitemcount - 1) in
3517 let first = calcfirst m_first active in
3518 G.postRedisplay "outline navigate";
3519 coe {< m_active = active; m_first = first >}
3521 let ctrl = Wsi.withctrl mask in
3522 match key with
3523 | 110 when ctrl -> (* ctrl-n *)
3524 source#narrow m_qsearch;
3525 G.postRedisplay "outline ctrl-n";
3526 coe {< m_first = 0; m_active = 0 >}
3528 | 117 when ctrl -> (* ctrl-u *)
3529 source#denarrow;
3530 G.postRedisplay "outline ctrl-u";
3531 state.text <- "";
3532 coe {< m_first = 0; m_active = 0 >}
3534 | 108 when ctrl -> (* ctrl-l *)
3535 let first = m_active - (fstate.maxrows / 2) in
3536 G.postRedisplay "outline ctrl-l";
3537 coe {< m_first = first >}
3539 | 0xff9f | 0xffff -> (* delete *)
3540 source#remove m_active;
3541 G.postRedisplay "outline delete";
3542 let active = max 0 (m_active-1) in
3543 coe {< m_first = firstof m_first active;
3544 m_active = active >}
3546 | 0xff52 -> navigate ~-1 (* up *)
3547 | 0xff54 -> navigate 1 (* down *)
3548 | 0xff55 -> (* prior *)
3549 navigate ~-(fstate.maxrows)
3550 | 0xff56 -> (* next *)
3551 navigate fstate.maxrows
3553 | 0xff53 -> (* [ctrl-]right *)
3554 let o =
3555 if ctrl
3556 then (
3557 G.postRedisplay "outline ctrl right";
3558 {< m_pan = m_pan + 1 >}
3560 else self#updownlevel 1
3562 coe o
3564 | 0xff51 -> (* [ctrl-]left *)
3565 let o =
3566 if ctrl
3567 then (
3568 G.postRedisplay "outline ctrl left";
3569 {< m_pan = m_pan - 1 >}
3571 else self#updownlevel ~-1
3573 coe o
3575 | 0xff50 -> (* home *)
3576 G.postRedisplay "outline home";
3577 coe {< m_first = 0; m_active = 0 >}
3579 | 0xff57 -> (* end *)
3580 let active = source#getitemcount - 1 in
3581 let first = max 0 (active - fstate.maxrows) in
3582 G.postRedisplay "outline end";
3583 coe {< m_active = active; m_first = first >}
3585 | _ -> super#key key mask
3588 let outlinesource usebookmarks =
3589 let empty = [||] in
3590 (object
3591 inherit lvsourcebase
3592 val mutable m_items = empty
3593 val mutable m_orig_items = empty
3594 val mutable m_prev_items = empty
3595 val mutable m_narrow_pattern = ""
3596 val mutable m_hadremovals = false
3598 method getitemcount =
3599 Array.length m_items + (if m_hadremovals then 1 else 0)
3601 method getitem n =
3602 if n == Array.length m_items && m_hadremovals
3603 then
3604 ("[Confirm removal]", 0)
3605 else
3606 let s, n, _ = m_items.(n) in
3607 (s, n)
3609 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3610 ignore (uioh, first, qsearch);
3611 let confrimremoval = m_hadremovals && active = Array.length m_items in
3612 let items =
3613 if String.length m_narrow_pattern = 0
3614 then m_orig_items
3615 else m_items
3617 if not cancel
3618 then (
3619 if not confrimremoval
3620 then(
3621 let _, _, anchor = m_items.(active) in
3622 gotoanchor anchor;
3623 m_items <- items;
3625 else (
3626 state.bookmarks <- Array.to_list m_items;
3627 m_orig_items <- m_items;
3630 else m_items <- items;
3631 m_pan <- pan;
3632 None
3634 method hasaction _ = true
3636 method greetmsg =
3637 if Array.length m_items != Array.length m_orig_items
3638 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3639 else ""
3641 method narrow pattern =
3642 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3643 match reopt with
3644 | None -> ()
3645 | Some re ->
3646 let rec loop accu n =
3647 if n = -1
3648 then (
3649 m_narrow_pattern <- pattern;
3650 m_items <- Array.of_list accu
3652 else
3653 let (s, _, _) as o = m_items.(n) in
3654 let accu =
3655 if (try ignore (Str.search_forward re s 0); true
3656 with Not_found -> false)
3657 then o :: accu
3658 else accu
3660 loop accu (n-1)
3662 loop [] (Array.length m_items - 1)
3664 method denarrow =
3665 m_orig_items <- (
3666 if usebookmarks
3667 then Array.of_list state.bookmarks
3668 else state.outlines
3670 m_items <- m_orig_items
3672 method remove m =
3673 if usebookmarks
3674 then
3675 if m >= 0 && m < Array.length m_items
3676 then (
3677 m_hadremovals <- true;
3678 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3679 let n = if n >= m then n+1 else n in
3680 m_items.(n)
3684 method reset anchor items =
3685 m_hadremovals <- false;
3686 if m_orig_items == empty || m_prev_items != items
3687 then (
3688 m_orig_items <- items;
3689 if String.length m_narrow_pattern = 0
3690 then m_items <- items;
3692 m_prev_items <- items;
3693 let rely = getanchory anchor in
3694 let active =
3695 let rec loop n best bestd =
3696 if n = Array.length m_items
3697 then best
3698 else
3699 let (_, _, anchor) = m_items.(n) in
3700 let orely = getanchory anchor in
3701 let d = abs (orely - rely) in
3702 if d < bestd
3703 then loop (n+1) n d
3704 else loop (n+1) best bestd
3706 loop 0 ~-1 max_int
3708 m_active <- active;
3709 m_first <- firstof m_first active
3710 end)
3713 let enterselector usebookmarks =
3714 let source = outlinesource usebookmarks in
3715 fun errmsg ->
3716 let outlines =
3717 if usebookmarks
3718 then Array.of_list state.bookmarks
3719 else state.outlines
3721 if Array.length outlines = 0
3722 then (
3723 showtext ' ' errmsg;
3725 else (
3726 state.text <- source#greetmsg;
3727 Wsi.setcursor Wsi.CURSOR_INHERIT;
3728 let anchor = getanchor () in
3729 source#reset anchor outlines;
3730 state.uioh <- coe (new outlinelistview ~source);
3731 G.postRedisplay "enter selector";
3735 let enteroutlinemode =
3736 let f = enterselector false in
3737 fun ()-> f "Document has no outline";
3740 let enterbookmarkmode =
3741 let f = enterselector true in
3742 fun () -> f "Document has no bookmarks (yet)";
3745 let color_of_string s =
3746 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3747 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3751 let color_to_string (r, g, b) =
3752 let r = truncate (r *. 256.0)
3753 and g = truncate (g *. 256.0)
3754 and b = truncate (b *. 256.0) in
3755 Printf.sprintf "%d/%d/%d" r g b
3758 let irect_of_string s =
3759 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3762 let irect_to_string (x0,y0,x1,y1) =
3763 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3766 let makecheckers () =
3767 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3768 following to say:
3769 converted by Issac Trotts. July 25, 2002 *)
3770 let image_height = 64
3771 and image_width = 64 in
3773 let make_image () =
3774 let image =
3775 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3777 for i = 0 to image_width - 1 do
3778 for j = 0 to image_height - 1 do
3779 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3780 (if (i land 8 ) lxor (j land 8) = 0
3781 then [|255;255;255|] else [|200;200;200|])
3782 done
3783 done;
3784 image
3786 let image = make_image () in
3787 let id = GlTex.gen_texture () in
3788 GlTex.bind_texture `texture_2d id;
3789 GlPix.store (`unpack_alignment 1);
3790 GlTex.image2d image;
3791 List.iter (GlTex.parameter ~target:`texture_2d)
3792 [ `wrap_s `repeat;
3793 `wrap_t `repeat;
3794 `mag_filter `nearest;
3795 `min_filter `nearest ];
3799 let setcheckers enabled =
3800 match state.texid with
3801 | None ->
3802 if enabled then state.texid <- Some (makecheckers ())
3804 | Some texid ->
3805 if not enabled
3806 then (
3807 GlTex.delete_texture texid;
3808 state.texid <- None;
3812 let int_of_string_with_suffix s =
3813 let l = String.length s in
3814 let s1, shift =
3815 if l > 1
3816 then
3817 let suffix = Char.lowercase s.[l-1] in
3818 match suffix with
3819 | 'k' -> String.sub s 0 (l-1), 10
3820 | 'm' -> String.sub s 0 (l-1), 20
3821 | 'g' -> String.sub s 0 (l-1), 30
3822 | _ -> s, 0
3823 else s, 0
3825 let n = int_of_string s1 in
3826 let m = n lsl shift in
3827 if m < 0 || m < n
3828 then raise (Failure "value too large")
3829 else m
3832 let string_with_suffix_of_int n =
3833 if n = 0
3834 then "0"
3835 else
3836 let n, s =
3837 if n = 0
3838 then 0, ""
3839 else (
3840 if n land ((1 lsl 20) - 1) = 0
3841 then n lsr 20, "M"
3842 else (
3843 if n land ((1 lsl 10) - 1) = 0
3844 then n lsr 10, "K"
3845 else n, ""
3849 let rec loop s n =
3850 let h = n mod 1000 in
3851 let n = n / 1000 in
3852 if n = 0
3853 then string_of_int h ^ s
3854 else (
3855 let s = Printf.sprintf "_%03d%s" h s in
3856 loop s n
3859 loop "" n ^ s;
3862 let defghyllscroll = (40, 8, 32);;
3863 let ghyllscroll_of_string s =
3864 let (n, a, b) as nab =
3865 if s = "default"
3866 then defghyllscroll
3867 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3869 if n <= a || n <= b || a >= b
3870 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3871 nab;
3874 let ghyllscroll_to_string ((n, a, b) as nab) =
3875 if nab = defghyllscroll
3876 then "default"
3877 else Printf.sprintf "%d,%d,%d" n a b;
3880 let describe_location () =
3881 let f (fn, _) l =
3882 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3884 let fn, ln = List.fold_left f (-1, -1) state.layout in
3885 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3886 let percent =
3887 if maxy <= 0
3888 then 100.
3889 else (100. *. (float state.y /. float maxy))
3891 if fn = ln
3892 then
3893 Printf.sprintf "page %d of %d [%.2f%%]"
3894 (fn+1) state.pagecount percent
3895 else
3896 Printf.sprintf
3897 "pages %d-%d of %d [%.2f%%]"
3898 (fn+1) (ln+1) state.pagecount percent
3901 let enterinfomode =
3902 let btos b = if b then "\xe2\x88\x9a" else "" in
3903 let showextended = ref false in
3904 let leave mode = function
3905 | Confirm -> state.mode <- mode
3906 | Cancel -> state.mode <- mode in
3907 let src =
3908 (object
3909 val mutable m_first_time = true
3910 val mutable m_l = []
3911 val mutable m_a = [||]
3912 val mutable m_prev_uioh = nouioh
3913 val mutable m_prev_mode = View
3915 inherit lvsourcebase
3917 method reset prev_mode prev_uioh =
3918 m_a <- Array.of_list (List.rev m_l);
3919 m_l <- [];
3920 m_prev_mode <- prev_mode;
3921 m_prev_uioh <- prev_uioh;
3922 if m_first_time
3923 then (
3924 let rec loop n =
3925 if n >= Array.length m_a
3926 then ()
3927 else
3928 match m_a.(n) with
3929 | _, _, _, Action _ -> m_active <- n
3930 | _ -> loop (n+1)
3932 loop 0;
3933 m_first_time <- false;
3936 method int name get set =
3937 m_l <-
3938 (name, `int get, 1, Action (
3939 fun u ->
3940 let ondone s =
3941 try set (int_of_string s)
3942 with exn ->
3943 state.text <- Printf.sprintf "bad integer `%s': %s"
3944 s (Printexc.to_string exn)
3946 state.text <- "";
3947 let te = name ^ ": ", "", None, intentry, ondone in
3948 state.mode <- Textentry (te, leave m_prev_mode);
3950 )) :: m_l
3952 method int_with_suffix name get set =
3953 m_l <-
3954 (name, `intws get, 1, Action (
3955 fun u ->
3956 let ondone s =
3957 try set (int_of_string_with_suffix s)
3958 with exn ->
3959 state.text <- Printf.sprintf "bad integer `%s': %s"
3960 s (Printexc.to_string exn)
3962 state.text <- "";
3963 let te =
3964 name ^ ": ", "", None, intentry_with_suffix, ondone
3966 state.mode <- Textentry (te, leave m_prev_mode);
3968 )) :: m_l
3970 method bool ?(offset=1) ?(btos=btos) name get set =
3971 m_l <-
3972 (name, `bool (btos, get), offset, Action (
3973 fun u ->
3974 let v = get () in
3975 set (not v);
3977 )) :: m_l
3979 method color name get set =
3980 m_l <-
3981 (name, `color get, 1, Action (
3982 fun u ->
3983 let invalid = (nan, nan, nan) in
3984 let ondone s =
3985 let c =
3986 try color_of_string s
3987 with exn ->
3988 state.text <- Printf.sprintf "bad color `%s': %s"
3989 s (Printexc.to_string exn);
3990 invalid
3992 if c <> invalid
3993 then set c;
3995 let te = name ^ ": ", "", None, textentry, ondone in
3996 state.text <- color_to_string (get ());
3997 state.mode <- Textentry (te, leave m_prev_mode);
3999 )) :: m_l
4001 method string name get set =
4002 m_l <-
4003 (name, `string get, 1, Action (
4004 fun u ->
4005 let ondone s = set s in
4006 let te = name ^ ": ", "", None, textentry, ondone in
4007 state.mode <- Textentry (te, leave m_prev_mode);
4009 )) :: m_l
4011 method colorspace name get set =
4012 m_l <-
4013 (name, `string get, 1, Action (
4014 fun _ ->
4015 let source =
4016 let vals = [| "rgb"; "bgr"; "gray" |] in
4017 (object
4018 inherit lvsourcebase
4020 initializer
4021 m_active <- int_of_colorspace conf.colorspace;
4022 m_first <- 0;
4024 method getitemcount = Array.length vals
4025 method getitem n = (vals.(n), 0)
4026 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4027 ignore (uioh, first, pan, qsearch);
4028 if not cancel then set active;
4029 None
4030 method hasaction _ = true
4031 end)
4033 state.text <- "";
4034 let modehash = findkeyhash conf "info" in
4035 coe (new listview ~source ~trusted:true ~modehash)
4036 )) :: m_l
4038 method caption s offset =
4039 m_l <- (s, `empty, offset, Noaction) :: m_l
4041 method caption2 s f offset =
4042 m_l <- (s, `string f, offset, Noaction) :: m_l
4044 method getitemcount = Array.length m_a
4046 method getitem n =
4047 let tostr = function
4048 | `int f -> string_of_int (f ())
4049 | `intws f -> string_with_suffix_of_int (f ())
4050 | `string f -> f ()
4051 | `color f -> color_to_string (f ())
4052 | `bool (btos, f) -> btos (f ())
4053 | `empty -> ""
4055 let name, t, offset, _ = m_a.(n) in
4056 ((let s = tostr t in
4057 if String.length s > 0
4058 then Printf.sprintf "%s\t%s" name s
4059 else name),
4060 offset)
4062 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4063 let uiohopt =
4064 if not cancel
4065 then (
4066 m_qsearch <- qsearch;
4067 let uioh =
4068 match m_a.(active) with
4069 | _, _, _, Action f -> f uioh
4070 | _ -> uioh
4072 Some uioh
4074 else None
4076 m_active <- active;
4077 m_first <- first;
4078 m_pan <- pan;
4079 uiohopt
4081 method hasaction n =
4082 match m_a.(n) with
4083 | _, _, _, Action _ -> true
4084 | _ -> false
4085 end)
4087 let rec fillsrc prevmode prevuioh =
4088 let sep () = src#caption "" 0 in
4089 let colorp name get set =
4090 src#string name
4091 (fun () -> color_to_string (get ()))
4092 (fun v ->
4094 let c = color_of_string v in
4095 set c
4096 with exn ->
4097 state.text <- Printf.sprintf "bad color `%s': %s"
4098 v (Printexc.to_string exn);
4101 let oldmode = state.mode in
4102 let birdseye = isbirdseye state.mode in
4104 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4106 src#bool "presentation mode"
4107 (fun () -> conf.presentation)
4108 (fun v ->
4109 conf.presentation <- v;
4110 state.anchor <- getanchor ();
4111 represent ());
4113 src#bool "ignore case in searches"
4114 (fun () -> conf.icase)
4115 (fun v -> conf.icase <- v);
4117 src#bool "preload"
4118 (fun () -> conf.preload)
4119 (fun v -> conf.preload <- v);
4121 src#bool "highlight links"
4122 (fun () -> conf.hlinks)
4123 (fun v -> conf.hlinks <- v);
4125 src#bool "under info"
4126 (fun () -> conf.underinfo)
4127 (fun v -> conf.underinfo <- v);
4129 src#bool "persistent bookmarks"
4130 (fun () -> conf.savebmarks)
4131 (fun v -> conf.savebmarks <- v);
4133 src#bool "proportional display"
4134 (fun () -> conf.proportional)
4135 (fun v -> reqlayout conf.angle v);
4137 src#bool "trim margins"
4138 (fun () -> conf.trimmargins)
4139 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4141 src#bool "persistent location"
4142 (fun () -> conf.jumpback)
4143 (fun v -> conf.jumpback <- v);
4145 sep ();
4146 src#int "inter-page space"
4147 (fun () -> conf.interpagespace)
4148 (fun n ->
4149 conf.interpagespace <- n;
4150 let pageno, py =
4151 match state.layout with
4152 | [] -> 0, 0
4153 | l :: _ ->
4154 l.pageno, l.pagey
4156 state.maxy <- calcheight ();
4157 let y = getpagey pageno in
4158 gotoy (y + py)
4161 src#int "page bias"
4162 (fun () -> conf.pagebias)
4163 (fun v -> conf.pagebias <- v);
4165 src#int "scroll step"
4166 (fun () -> conf.scrollstep)
4167 (fun n -> conf.scrollstep <- n);
4169 src#int "auto scroll step"
4170 (fun () ->
4171 match state.autoscroll with
4172 | Some step -> step
4173 | _ -> conf.autoscrollstep)
4174 (fun n ->
4175 if state.autoscroll <> None
4176 then state.autoscroll <- Some n;
4177 conf.autoscrollstep <- n);
4179 src#int "zoom"
4180 (fun () -> truncate (conf.zoom *. 100.))
4181 (fun v -> setzoom ((float v) /. 100.));
4183 src#int "rotation"
4184 (fun () -> conf.angle)
4185 (fun v -> reqlayout v conf.proportional);
4187 src#int "scroll bar width"
4188 (fun () -> state.scrollw)
4189 (fun v ->
4190 state.scrollw <- v;
4191 conf.scrollbw <- v;
4192 reshape conf.winw conf.winh;
4195 src#int "scroll handle height"
4196 (fun () -> conf.scrollh)
4197 (fun v -> conf.scrollh <- v;);
4199 src#int "thumbnail width"
4200 (fun () -> conf.thumbw)
4201 (fun v ->
4202 conf.thumbw <- min 4096 v;
4203 match oldmode with
4204 | Birdseye beye ->
4205 leavebirdseye beye false;
4206 enterbirdseye ()
4207 | _ -> ()
4210 let mode = state.mode in
4211 src#string "columns"
4212 (fun () ->
4213 match conf.columns with
4214 | Csingle -> "1"
4215 | Cmulti (multi, _) -> multicolumns_to_string multi
4216 | Csplit (count, _) -> "-" ^ string_of_int count
4218 (fun v ->
4219 let n, a, b = multicolumns_of_string v in
4220 setcolumns mode n a b);
4222 sep ();
4223 src#caption "Presentation mode" 0;
4224 src#bool "scrollbar visible"
4225 (fun () -> conf.scrollbarinpm)
4226 (fun v ->
4227 if v != conf.scrollbarinpm
4228 then (
4229 conf.scrollbarinpm <- v;
4230 if conf.presentation
4231 then (
4232 state.scrollw <- if v then conf.scrollbw else 0;
4233 reshape conf.winw conf.winh;
4238 sep ();
4239 src#caption "Pixmap cache" 0;
4240 src#int_with_suffix "size (advisory)"
4241 (fun () -> conf.memlimit)
4242 (fun v -> conf.memlimit <- v);
4244 src#caption2 "used"
4245 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4246 (string_with_suffix_of_int state.memused)
4247 (Hashtbl.length state.tilemap)) 1;
4249 sep ();
4250 src#caption "Layout" 0;
4251 src#caption2 "Dimension"
4252 (fun () ->
4253 Printf.sprintf "%dx%d (virtual %dx%d)"
4254 conf.winw conf.winh
4255 state.w state.maxy)
4257 if conf.debug
4258 then
4259 src#caption2 "Position" (fun () ->
4260 Printf.sprintf "%dx%d" state.x state.y
4262 else
4263 src#caption2 "Visible" (fun () -> describe_location ()) 1
4266 sep ();
4267 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4268 "Save these parameters as global defaults at exit"
4269 (fun () -> conf.bedefault)
4270 (fun v -> conf.bedefault <- v)
4273 sep ();
4274 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4275 src#bool ~offset:0 ~btos "Extended parameters"
4276 (fun () -> !showextended)
4277 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4278 if !showextended
4279 then (
4280 src#bool "checkers"
4281 (fun () -> conf.checkers)
4282 (fun v -> conf.checkers <- v; setcheckers v);
4283 src#bool "update cursor"
4284 (fun () -> conf.updatecurs)
4285 (fun v -> conf.updatecurs <- v);
4286 src#bool "verbose"
4287 (fun () -> conf.verbose)
4288 (fun v -> conf.verbose <- v);
4289 src#bool "invert colors"
4290 (fun () -> conf.invert)
4291 (fun v -> conf.invert <- v);
4292 src#bool "max fit"
4293 (fun () -> conf.maxhfit)
4294 (fun v -> conf.maxhfit <- v);
4295 src#bool "redirect stderr"
4296 (fun () -> conf.redirectstderr)
4297 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4298 src#string "uri launcher"
4299 (fun () -> conf.urilauncher)
4300 (fun v -> conf.urilauncher <- v);
4301 src#string "path launcher"
4302 (fun () -> conf.pathlauncher)
4303 (fun v -> conf.pathlauncher <- v);
4304 src#string "tile size"
4305 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4306 (fun v ->
4308 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4309 conf.tilew <- max 64 w;
4310 conf.tileh <- max 64 h;
4311 flushtiles ();
4312 with exn ->
4313 state.text <- Printf.sprintf "bad tile size `%s': %s"
4314 v (Printexc.to_string exn));
4315 src#int "texture count"
4316 (fun () -> conf.texcount)
4317 (fun v ->
4318 if realloctexts v
4319 then conf.texcount <- v
4320 else showtext '!' " Failed to set texture count please retry later"
4322 src#int "slice height"
4323 (fun () -> conf.sliceheight)
4324 (fun v ->
4325 conf.sliceheight <- v;
4326 wcmd "sliceh %d" conf.sliceheight;
4328 src#int "anti-aliasing level"
4329 (fun () -> conf.aalevel)
4330 (fun v ->
4331 conf.aalevel <- bound v 0 8;
4332 state.anchor <- getanchor ();
4333 opendoc state.path state.password;
4335 src#int "ui font size"
4336 (fun () -> fstate.fontsize)
4337 (fun v -> setfontsize (bound v 5 100));
4338 colorp "background color"
4339 (fun () -> conf.bgcolor)
4340 (fun v -> conf.bgcolor <- v);
4341 src#bool "crop hack"
4342 (fun () -> conf.crophack)
4343 (fun v -> conf.crophack <- v);
4344 src#string "trim fuzz"
4345 (fun () -> irect_to_string conf.trimfuzz)
4346 (fun v ->
4348 conf.trimfuzz <- irect_of_string v;
4349 if conf.trimmargins
4350 then settrim true conf.trimfuzz;
4351 with exn ->
4352 state.text <- Printf.sprintf "bad irect `%s': %s"
4353 v (Printexc.to_string exn)
4355 src#string "throttle"
4356 (fun () ->
4357 match conf.maxwait with
4358 | None -> "show place holder if page is not ready"
4359 | Some time ->
4360 if time = infinity
4361 then "wait for page to fully render"
4362 else
4363 "wait " ^ string_of_float time
4364 ^ " seconds before showing placeholder"
4366 (fun v ->
4368 let f = float_of_string v in
4369 if f <= 0.0
4370 then conf.maxwait <- None
4371 else conf.maxwait <- Some f
4372 with exn ->
4373 state.text <- Printf.sprintf "bad time `%s': %s"
4374 v (Printexc.to_string exn)
4376 src#string "ghyll scroll"
4377 (fun () ->
4378 match conf.ghyllscroll with
4379 | None -> ""
4380 | Some nab -> ghyllscroll_to_string nab
4382 (fun v ->
4384 let gs =
4385 if String.length v = 0
4386 then None
4387 else Some (ghyllscroll_of_string v)
4389 conf.ghyllscroll <- gs
4390 with exn ->
4391 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4392 v (Printexc.to_string exn)
4394 src#string "selection command"
4395 (fun () -> conf.selcmd)
4396 (fun v -> conf.selcmd <- v);
4397 src#colorspace "color space"
4398 (fun () -> colorspace_to_string conf.colorspace)
4399 (fun v ->
4400 conf.colorspace <- colorspace_of_int v;
4401 wcmd "cs %d" v;
4402 load state.layout;
4406 sep ();
4407 src#caption "Document" 0;
4408 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4409 src#caption2 "Pages"
4410 (fun () -> string_of_int state.pagecount) 1;
4411 src#caption2 "Dimensions"
4412 (fun () -> string_of_int (List.length state.pdims)) 1;
4413 if conf.trimmargins
4414 then (
4415 sep ();
4416 src#caption "Trimmed margins" 0;
4417 src#caption2 "Dimensions"
4418 (fun () -> string_of_int (List.length state.pdims)) 1;
4421 src#reset prevmode prevuioh;
4423 fun () ->
4424 state.text <- "";
4425 let prevmode = state.mode
4426 and prevuioh = state.uioh in
4427 fillsrc prevmode prevuioh;
4428 let source = (src :> lvsource) in
4429 let modehash = findkeyhash conf "info" in
4430 state.uioh <- coe (object (self)
4431 inherit listview ~source ~trusted:true ~modehash as super
4432 val mutable m_prevmemused = 0
4433 method infochanged = function
4434 | Memused ->
4435 if m_prevmemused != state.memused
4436 then (
4437 m_prevmemused <- state.memused;
4438 G.postRedisplay "memusedchanged";
4440 | Pdim -> G.postRedisplay "pdimchanged"
4441 | Docinfo -> fillsrc prevmode prevuioh
4443 method key key mask =
4444 if not (Wsi.withctrl mask)
4445 then
4446 match key with
4447 | 0xff51 -> coe (self#updownlevel ~-1)
4448 | 0xff53 -> coe (self#updownlevel 1)
4449 | _ -> super#key key mask
4450 else super#key key mask
4451 end);
4452 G.postRedisplay "info";
4455 let enterhelpmode =
4456 let source =
4457 (object
4458 inherit lvsourcebase
4459 method getitemcount = Array.length state.help
4460 method getitem n =
4461 let s, n, _ = state.help.(n) in
4462 (s, n)
4464 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4465 let optuioh =
4466 if not cancel
4467 then (
4468 m_qsearch <- qsearch;
4469 match state.help.(active) with
4470 | _, _, Action f -> Some (f uioh)
4471 | _ -> Some (uioh)
4473 else None
4475 m_active <- active;
4476 m_first <- first;
4477 m_pan <- pan;
4478 optuioh
4480 method hasaction n =
4481 match state.help.(n) with
4482 | _, _, Action _ -> true
4483 | _ -> false
4485 initializer
4486 m_active <- -1
4487 end)
4488 in fun () ->
4489 let modehash = findkeyhash conf "help" in
4490 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4491 G.postRedisplay "help";
4494 let entermsgsmode =
4495 let msgsource =
4496 let re = Str.regexp "[\r\n]" in
4497 (object
4498 inherit lvsourcebase
4499 val mutable m_items = [||]
4501 method getitemcount = 1 + Array.length m_items
4503 method getitem n =
4504 if n = 0
4505 then "[Clear]", 0
4506 else m_items.(n-1), 0
4508 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4509 ignore uioh;
4510 if not cancel
4511 then (
4512 if active = 0
4513 then Buffer.clear state.errmsgs;
4514 m_qsearch <- qsearch;
4516 m_active <- active;
4517 m_first <- first;
4518 m_pan <- pan;
4519 None
4521 method hasaction n =
4522 n = 0
4524 method reset =
4525 state.newerrmsgs <- false;
4526 let l = Str.split re (Buffer.contents state.errmsgs) in
4527 m_items <- Array.of_list l
4529 initializer
4530 m_active <- 0
4531 end)
4532 in fun () ->
4533 state.text <- "";
4534 msgsource#reset;
4535 let source = (msgsource :> lvsource) in
4536 let modehash = findkeyhash conf "listview" in
4537 state.uioh <- coe (object
4538 inherit listview ~source ~trusted:false ~modehash as super
4539 method display =
4540 if state.newerrmsgs
4541 then msgsource#reset;
4542 super#display
4543 end);
4544 G.postRedisplay "msgs";
4547 let quickbookmark ?title () =
4548 match state.layout with
4549 | [] -> ()
4550 | l :: _ ->
4551 let title =
4552 match title with
4553 | None ->
4554 let sec = Unix.gettimeofday () in
4555 let tm = Unix.localtime sec in
4556 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4557 (l.pageno+1)
4558 tm.Unix.tm_mday
4559 tm.Unix.tm_mon
4560 (tm.Unix.tm_year + 1900)
4561 tm.Unix.tm_hour
4562 tm.Unix.tm_min
4563 | Some title -> title
4565 state.bookmarks <-
4566 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4567 :: state.bookmarks
4570 let doreshape w h =
4571 state.fullscreen <- None;
4572 Wsi.reshape w h;
4575 let setautoscrollspeed step goingdown =
4576 let incr = max 1 ((abs step) / 2) in
4577 let incr = if goingdown then incr else -incr in
4578 let astep = step + incr in
4579 state.autoscroll <- Some astep;
4582 let gotounder = function
4583 | Ulinkgoto (pageno, top) ->
4584 if pageno >= 0
4585 then (
4586 addnav ();
4587 gotopage1 pageno top;
4590 | Ulinkuri s ->
4591 gotouri s
4593 | Uremote (filename, pageno) ->
4594 let path =
4595 if Sys.file_exists filename
4596 then filename
4597 else
4598 let dir = Filename.dirname state.path in
4599 let path = Filename.concat dir filename in
4600 if Sys.file_exists path
4601 then path
4602 else ""
4604 if String.length path > 0
4605 then (
4606 let anchor = getanchor () in
4607 let ranchor = state.path, state.password, anchor in
4608 state.anchor <- (pageno, 0.0);
4609 state.ranchors <- ranchor :: state.ranchors;
4610 opendoc path "";
4612 else showtext '!' ("Could not find " ^ filename)
4614 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4617 let canpan () =
4618 match conf.columns with
4619 | Csplit _ -> true
4620 | _ -> conf.zoom > 1.0
4623 let viewkeyboard key mask =
4624 let enttext te =
4625 let mode = state.mode in
4626 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4627 state.text <- "";
4628 enttext ();
4629 G.postRedisplay "view:enttext"
4631 let ctrl = Wsi.withctrl mask in
4632 match key with
4633 | 81 -> (* Q *)
4634 exit 0
4636 | 0xff63 -> (* insert *)
4637 if conf.angle mod 360 = 0
4638 then (
4639 state.mode <- LinkNav (Ltgendir 0);
4640 gotoy state.y;
4642 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4644 | 0xff1b | 113 -> (* escape / q *)
4645 begin match state.mstate with
4646 | Mzoomrect _ ->
4647 state.mstate <- Mnone;
4648 Wsi.setcursor Wsi.CURSOR_INHERIT;
4649 G.postRedisplay "kill zoom rect";
4650 | _ ->
4651 match state.ranchors with
4652 | [] -> raise Quit
4653 | (path, password, anchor) :: rest ->
4654 state.ranchors <- rest;
4655 state.anchor <- anchor;
4656 opendoc path password
4657 end;
4659 | 0xff08 -> (* backspace *)
4660 let y = getnav ~-1 in
4661 gotoy_and_clear_text y
4663 | 111 -> (* o *)
4664 enteroutlinemode ()
4666 | 117 -> (* u *)
4667 state.rects <- [];
4668 state.text <- "";
4669 G.postRedisplay "dehighlight";
4671 | 47 | 63 -> (* / ? *)
4672 let ondone isforw s =
4673 cbput state.hists.pat s;
4674 state.searchpattern <- s;
4675 search s isforw
4677 let s = String.create 1 in
4678 s.[0] <- Char.chr key;
4679 enttext (s, "", Some (onhist state.hists.pat),
4680 textentry, ondone (key = 47))
4682 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4683 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4684 setzoom (conf.zoom +. incr)
4686 | 43 | 0xffab -> (* + *)
4687 let ondone s =
4688 let n =
4689 try int_of_string s with exc ->
4690 state.text <- Printf.sprintf "bad integer `%s': %s"
4691 s (Printexc.to_string exc);
4692 max_int
4694 if n != max_int
4695 then (
4696 conf.pagebias <- n;
4697 state.text <- "page bias is now " ^ string_of_int n;
4700 enttext ("page bias: ", "", None, intentry, ondone)
4702 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4703 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4704 setzoom (max 0.01 (conf.zoom -. decr))
4706 | 45 | 0xffad -> (* - *)
4707 let ondone msg = state.text <- msg in
4708 enttext (
4709 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4710 optentry state.mode, ondone
4713 | 48 when ctrl -> (* ctrl-0 *)
4714 setzoom 1.0
4716 | 49 when ctrl -> (* 1 *)
4717 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4718 if zoom < 1.0
4719 then setzoom zoom
4721 | 0xffc6 -> (* f9 *)
4722 togglebirdseye ()
4724 | 57 when ctrl -> (* ctrl-9 *)
4725 togglebirdseye ()
4727 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4728 when not ctrl -> (* 0..9 *)
4729 let ondone s =
4730 let n =
4731 try int_of_string s with exc ->
4732 state.text <- Printf.sprintf "bad integer `%s': %s"
4733 s (Printexc.to_string exc);
4736 if n >= 0
4737 then (
4738 addnav ();
4739 cbput state.hists.pag (string_of_int n);
4740 gotopage1 (n + conf.pagebias - 1) 0;
4743 let pageentry text key =
4744 match Char.unsafe_chr key with
4745 | 'g' -> TEdone text
4746 | _ -> intentry text key
4748 let text = "x" in text.[0] <- Char.chr key;
4749 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4751 | 98 -> (* b *)
4752 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4753 reshape conf.winw conf.winh;
4755 | 108 -> (* l *)
4756 conf.hlinks <- not conf.hlinks;
4757 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4758 G.postRedisplay "toggle highlightlinks";
4760 | 70 -> (* F *)
4761 state.glinks <- true;
4762 let mode = state.mode in
4763 state.mode <- Textentry (
4764 (":", "", None, linknentry, linkndone (fun under ->
4765 addnav ();
4766 gotounder under
4768 ), fun _ ->
4769 state.glinks <- false;
4770 state.mode <- mode
4772 state.text <- "";
4773 G.postRedisplay "view:linkent(F)"
4775 | 121 -> (* y *)
4776 state.glinks <- true;
4777 let mode = state.mode in
4778 state.mode <- Textentry (
4779 (":", "", None, linknentry, linkndone (fun under ->
4780 match Ne.pipe () with
4781 | Ne.Exn exn ->
4782 showtext '!' (Printf.sprintf "pipe failed: %s"
4783 (Printexc.to_string exn));
4784 | Ne.Res (r, w) ->
4785 begin try
4786 popen conf.selcmd [r, 0; w, -1]
4787 with exn ->
4788 showtext '!'
4789 (Printf.sprintf "failed to execute %s: %s"
4790 conf.selcmd (Printexc.to_string exn))
4791 end;
4792 let clo cap fd =
4793 Ne.clo fd (fun msg ->
4794 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4797 let s = undertext under in
4798 (try
4799 let l = String.length s in
4800 let n = Unix.write w s 0 l in
4801 if n != l
4802 then
4803 showtext '!'
4804 (Printf.sprintf
4805 "failed to write %d characters to sel pipe, wrote %d"
4808 with exn ->
4809 showtext '!'
4810 (Printf.sprintf "failed to write to sel pipe: %s"
4811 (Printexc.to_string exn)
4814 clo "pipe/r" r;
4815 clo "pipe/w" w;
4818 fun _ ->
4819 state.glinks <- false;
4820 state.mode <- mode
4822 state.text <- "";
4823 G.postRedisplay "view:linkent"
4825 | 97 -> (* a *)
4826 begin match state.autoscroll with
4827 | Some step ->
4828 conf.autoscrollstep <- step;
4829 state.autoscroll <- None
4830 | None ->
4831 if conf.autoscrollstep = 0
4832 then state.autoscroll <- Some 1
4833 else state.autoscroll <- Some conf.autoscrollstep
4836 | 112 when ctrl -> (* ctrl-p *)
4837 launchpath ()
4839 | 80 -> (* P *)
4840 conf.presentation <- not conf.presentation;
4841 if conf.presentation
4842 then (
4843 if not conf.scrollbarinpm
4844 then state.scrollw <- 0;
4846 else
4847 state.scrollw <- conf.scrollbw;
4849 showtext ' ' ("presentation mode " ^
4850 if conf.presentation then "on" else "off");
4851 state.anchor <- getanchor ();
4852 represent ()
4854 | 102 -> (* f *)
4855 begin match state.fullscreen with
4856 | None ->
4857 state.fullscreen <- Some (conf.winw, conf.winh);
4858 Wsi.fullscreen ()
4859 | Some (w, h) ->
4860 state.fullscreen <- None;
4861 doreshape w h
4864 | 103 -> (* g *)
4865 gotoy_and_clear_text 0
4867 | 71 -> (* G *)
4868 gotopage1 (state.pagecount - 1) 0
4870 | 112 | 78 -> (* p|N *)
4871 search state.searchpattern false
4873 | 110 | 0xffc0 -> (* n|F3 *)
4874 search state.searchpattern true
4876 | 116 -> (* t *)
4877 begin match state.layout with
4878 | [] -> ()
4879 | l :: _ ->
4880 gotoy_and_clear_text (getpagey l.pageno)
4883 | 32 -> (* ' ' *)
4884 begin match List.rev state.layout with
4885 | [] -> ()
4886 | l :: _ ->
4887 let pageno = min (l.pageno+1) (state.pagecount-1) in
4888 gotoy_and_clear_text (getpagey pageno)
4891 | 0xff9f | 0xffff -> (* delete *)
4892 begin match state.layout with
4893 | [] -> ()
4894 | l :: _ ->
4895 let pageno = max 0 (l.pageno-1) in
4896 gotoy_and_clear_text (getpagey pageno)
4899 | 61 -> (* = *)
4900 showtext ' ' (describe_location ());
4902 | 119 -> (* w *)
4903 begin match state.layout with
4904 | [] -> ()
4905 | l :: _ ->
4906 doreshape (l.pagew + state.scrollw) l.pageh;
4907 G.postRedisplay "w"
4910 | 39 -> (* ' *)
4911 enterbookmarkmode ()
4913 | 104 | 0xffbe -> (* h|F1 *)
4914 enterhelpmode ()
4916 | 105 -> (* i *)
4917 enterinfomode ()
4919 | 101 when conf.redirectstderr -> (* e *)
4920 entermsgsmode ()
4922 | 109 -> (* m *)
4923 let ondone s =
4924 match state.layout with
4925 | l :: _ ->
4926 state.bookmarks <-
4927 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4928 :: state.bookmarks
4929 | _ -> ()
4931 enttext ("bookmark: ", "", None, textentry, ondone)
4933 | 126 -> (* ~ *)
4934 quickbookmark ();
4935 showtext ' ' "Quick bookmark added";
4937 | 122 -> (* z *)
4938 begin match state.layout with
4939 | l :: _ ->
4940 let rect = getpdimrect l.pagedimno in
4941 let w, h =
4942 if conf.crophack
4943 then
4944 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4945 truncate (1.2 *. (rect.(3) -. rect.(0))))
4946 else
4947 (truncate (rect.(1) -. rect.(0)),
4948 truncate (rect.(3) -. rect.(0)))
4950 let w = truncate ((float w)*.conf.zoom)
4951 and h = truncate ((float h)*.conf.zoom) in
4952 if w != 0 && h != 0
4953 then (
4954 state.anchor <- getanchor ();
4955 doreshape (w + state.scrollw) (h + conf.interpagespace)
4957 G.postRedisplay "z";
4959 | [] -> ()
4962 | 50 when ctrl -> (* ctrl-2 *)
4963 let maxw = getmaxw () in
4964 if maxw > 0.0
4965 then setzoom (maxw /. float conf.winw)
4967 | 60 | 62 -> (* < > *)
4968 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4970 | 91 | 93 -> (* [ ] *)
4971 conf.colorscale <-
4972 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4974 G.postRedisplay "brightness";
4976 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4977 setzoom state.prevzoom
4979 | 107 | 0xff52 -> (* k up *)
4980 begin match state.autoscroll with
4981 | None ->
4982 begin match state.mode with
4983 | Birdseye beye -> upbirdseye 1 beye
4984 | _ ->
4985 if ctrl
4986 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4987 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4989 | Some n ->
4990 setautoscrollspeed n false
4993 | 106 | 0xff54 -> (* j down *)
4994 begin match state.autoscroll with
4995 | None ->
4996 begin match state.mode with
4997 | Birdseye beye -> downbirdseye 1 beye
4998 | _ ->
4999 if ctrl
5000 then gotoy_and_clear_text (clamp (conf.winh/2))
5001 else gotoy_and_clear_text (clamp conf.scrollstep)
5003 | Some n ->
5004 setautoscrollspeed n true
5007 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5008 if canpan ()
5009 then
5010 let dx =
5011 if ctrl
5012 then conf.winw / 2
5013 else 10
5015 let dx = if key = 0xff51 then dx else -dx in
5016 state.x <- state.x + dx;
5017 gotoy_and_clear_text state.y
5018 else (
5019 state.text <- "";
5020 G.postRedisplay "lef/right"
5023 | 0xff55 -> (* prior *)
5024 let y =
5025 if ctrl
5026 then
5027 match state.layout with
5028 | [] -> state.y
5029 | l :: _ -> state.y - l.pagey
5030 else
5031 clamp (-conf.winh)
5033 gotoghyll y
5035 | 0xff56 -> (* next *)
5036 let y =
5037 if ctrl
5038 then
5039 match List.rev state.layout with
5040 | [] -> state.y
5041 | l :: _ -> getpagey l.pageno
5042 else
5043 clamp conf.winh
5045 gotoghyll y
5047 | 0xff50 -> gotoghyll 0
5048 | 0xff57 -> gotoghyll (clamp state.maxy)
5049 | 0xff53 when Wsi.withalt mask ->
5050 gotoghyll (getnav ~-1)
5051 | 0xff51 when Wsi.withalt mask ->
5052 gotoghyll (getnav 1)
5054 | 114 -> (* r *)
5055 state.anchor <- getanchor ();
5056 opendoc state.path state.password
5058 | 118 when conf.debug -> (* v *)
5059 state.rects <- [];
5060 List.iter (fun l ->
5061 match getopaque l.pageno with
5062 | None -> ()
5063 | Some opaque ->
5064 let x0, y0, x1, y1 = pagebbox opaque in
5065 let a,b = float x0, float y0 in
5066 let c,d = float x1, float y0 in
5067 let e,f = float x1, float y1 in
5068 let h,j = float x0, float y1 in
5069 let rect = (a,b,c,d,e,f,h,j) in
5070 debugrect rect;
5071 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5072 ) state.layout;
5073 G.postRedisplay "v";
5075 | _ ->
5076 vlog "huh? %s" (Wsi.keyname key)
5079 let linknavkeyboard key mask linknav =
5080 let getpage pageno =
5081 let rec loop = function
5082 | [] -> None
5083 | l :: _ when l.pageno = pageno -> Some l
5084 | _ :: rest -> loop rest
5085 in loop state.layout
5087 let doexact (pageno, n) =
5088 match getopaque pageno, getpage pageno with
5089 | Some opaque, Some l ->
5090 if key = 0xff0d
5091 then
5092 let under = getlink opaque n in
5093 G.postRedisplay "link gotounder";
5094 gotounder under;
5095 state.mode <- View;
5096 else
5097 let opt, dir =
5098 match key with
5099 | 0xff50 -> (* home *)
5100 Some (findlink opaque LDfirst), -1
5102 | 0xff57 -> (* end *)
5103 Some (findlink opaque LDlast), 1
5105 | 0xff51 -> (* left *)
5106 Some (findlink opaque (LDleft n)), -1
5108 | 0xff53 -> (* right *)
5109 Some (findlink opaque (LDright n)), 1
5111 | 0xff52 -> (* up *)
5112 Some (findlink opaque (LDup n)), -1
5114 | 0xff54 -> (* down *)
5115 Some (findlink opaque (LDdown n)), 1
5117 | _ -> None, 0
5119 let pwl l dir =
5120 begin match findpwl l.pageno dir with
5121 | Pwlnotfound -> ()
5122 | Pwl pageno ->
5123 let notfound dir =
5124 state.mode <- LinkNav (Ltgendir dir);
5125 let y, h = getpageyh pageno in
5126 let y =
5127 if dir < 0
5128 then y + h - conf.winh
5129 else y
5131 gotoy y
5133 begin match getopaque pageno, getpage pageno with
5134 | Some opaque, Some _ ->
5135 let link =
5136 let ld = if dir > 0 then LDfirst else LDlast in
5137 findlink opaque ld
5139 begin match link with
5140 | Lfound m ->
5141 showlinktype (getlink opaque m);
5142 state.mode <- LinkNav (Ltexact (pageno, m));
5143 G.postRedisplay "linknav jpage";
5144 | _ -> notfound dir
5145 end;
5146 | _ -> notfound dir
5147 end;
5148 end;
5150 begin match opt with
5151 | Some Lnotfound -> pwl l dir;
5152 | Some (Lfound m) ->
5153 if m = n
5154 then pwl l dir
5155 else (
5156 let _, y0, _, y1 = getlinkrect opaque m in
5157 if y0 < l.pagey
5158 then gotopage1 l.pageno y0
5159 else (
5160 let d = fstate.fontsize + 1 in
5161 if y1 - l.pagey > l.pagevh - d
5162 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5163 else G.postRedisplay "linknav";
5165 showlinktype (getlink opaque m);
5166 state.mode <- LinkNav (Ltexact (l.pageno, m));
5169 | None -> viewkeyboard key mask
5170 end;
5171 | _ -> viewkeyboard key mask
5173 if key = 0xff63
5174 then (
5175 state.mode <- View;
5176 G.postRedisplay "leave linknav"
5178 else
5179 match linknav with
5180 | Ltgendir _ -> viewkeyboard key mask
5181 | Ltexact exact -> doexact exact
5184 let keyboard key mask =
5185 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5186 then wcmd "interrupt"
5187 else state.uioh <- state.uioh#key key mask
5190 let birdseyekeyboard key mask
5191 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5192 let incr =
5193 match conf.columns with
5194 | Csingle -> 1
5195 | Cmulti ((c, _, _), _) -> c
5196 | Csplit _ -> failwith "bird's eye split mode"
5198 match key with
5199 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5200 let y, h = getpageyh pageno in
5201 let top = (conf.winh - h) / 2 in
5202 gotoy (max 0 (y - top))
5203 | 0xff0d -> leavebirdseye beye false
5204 | 0xff1b -> leavebirdseye beye true (* escape *)
5205 | 0xff52 -> upbirdseye incr beye (* prior *)
5206 | 0xff54 -> downbirdseye incr beye (* next *)
5207 | 0xff51 -> upbirdseye 1 beye (* up *)
5208 | 0xff53 -> downbirdseye 1 beye (* down *)
5210 | 0xff55 ->
5211 begin match state.layout with
5212 | l :: _ ->
5213 if l.pagey != 0
5214 then (
5215 state.mode <- Birdseye (
5216 oconf, leftx, l.pageno, hooverpageno, anchor
5218 gotopage1 l.pageno 0;
5220 else (
5221 let layout = layout (state.y-conf.winh) conf.winh in
5222 match layout with
5223 | [] -> gotoy (clamp (-conf.winh))
5224 | l :: _ ->
5225 state.mode <- Birdseye (
5226 oconf, leftx, l.pageno, hooverpageno, anchor
5228 gotopage1 l.pageno 0
5231 | [] -> gotoy (clamp (-conf.winh))
5232 end;
5234 | 0xff56 ->
5235 begin match List.rev state.layout with
5236 | l :: _ ->
5237 let layout = layout (state.y + conf.winh) conf.winh in
5238 begin match layout with
5239 | [] ->
5240 let incr = l.pageh - l.pagevh in
5241 if incr = 0
5242 then (
5243 state.mode <-
5244 Birdseye (
5245 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5247 G.postRedisplay "birdseye pagedown";
5249 else gotoy (clamp (incr + conf.interpagespace*2));
5251 | l :: _ ->
5252 state.mode <-
5253 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5254 gotopage1 l.pageno 0;
5257 | [] -> gotoy (clamp conf.winh)
5258 end;
5260 | 0xff50 ->
5261 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5262 gotopage1 0 0
5264 | 0xff57 ->
5265 let pageno = state.pagecount - 1 in
5266 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5267 if not (pagevisible state.layout pageno)
5268 then
5269 let h =
5270 match List.rev state.pdims with
5271 | [] -> conf.winh
5272 | (_, _, h, _) :: _ -> h
5274 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5275 else G.postRedisplay "birdseye end";
5276 | _ -> viewkeyboard key mask
5279 let drawpage l linkindexbase =
5280 let color =
5281 match state.mode with
5282 | Textentry _ -> scalecolor 0.4
5283 | LinkNav _
5284 | View -> scalecolor 1.0
5285 | Birdseye (_, _, pageno, hooverpageno, _) ->
5286 if l.pageno = hooverpageno
5287 then scalecolor 0.9
5288 else (
5289 if l.pageno = pageno
5290 then scalecolor 1.0
5291 else scalecolor 0.8
5294 drawtiles l color;
5295 begin match getopaque l.pageno with
5296 | Some opaque ->
5297 if tileready l l.pagex l.pagey
5298 then
5299 let x = l.pagedispx - l.pagex
5300 and y = l.pagedispy - l.pagey in
5301 let hlmask = (if conf.hlinks then 1 else 0)
5302 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5304 let s =
5305 match state.mode with
5306 | Textentry ((_, s, _, _, _), _) when state.glinks -> s
5307 | _ -> ""
5309 postprocess opaque hlmask x y (linkindexbase, s);
5310 else 0
5312 | _ -> 0
5313 end;
5316 let scrollindicator () =
5317 let sbw, ph, sh = state.uioh#scrollph in
5318 let sbh, pw, sw = state.uioh#scrollpw in
5320 GlDraw.color (0.64, 0.64, 0.64);
5321 GlDraw.rect
5322 (float (conf.winw - sbw), 0.)
5323 (float conf.winw, float conf.winh)
5325 GlDraw.rect
5326 (0., float (conf.winh - sbh))
5327 (float (conf.winw - state.scrollw - 1), float conf.winh)
5329 GlDraw.color (0.0, 0.0, 0.0);
5331 GlDraw.rect
5332 (float (conf.winw - sbw), ph)
5333 (float conf.winw, ph +. sh)
5335 GlDraw.rect
5336 (pw, float (conf.winh - sbh))
5337 (pw +. sw, float conf.winh)
5341 let showsel () =
5342 match state.mstate with
5343 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5346 | Msel ((x0, y0), (x1, y1)) ->
5347 let rec loop = function
5348 | l :: ls ->
5349 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5350 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5351 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5352 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5353 then
5354 match getopaque l.pageno with
5355 | Some opaque ->
5356 let x0, y0 = pagetranslatepoint l x0 y0 in
5357 let x1, y1 = pagetranslatepoint l x1 y1 in
5358 seltext opaque (x0, y0, x1, y1);
5359 | _ -> ()
5360 else loop ls
5361 | [] -> ()
5363 loop state.layout
5366 let showrects rects =
5367 Gl.enable `blend;
5368 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5369 GlDraw.polygon_mode `both `fill;
5370 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5371 List.iter
5372 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5373 List.iter (fun l ->
5374 if l.pageno = pageno
5375 then (
5376 let dx = float (l.pagedispx - l.pagex) in
5377 let dy = float (l.pagedispy - l.pagey) in
5378 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5379 GlDraw.begins `quads;
5381 GlDraw.vertex2 (x0+.dx, y0+.dy);
5382 GlDraw.vertex2 (x1+.dx, y1+.dy);
5383 GlDraw.vertex2 (x2+.dx, y2+.dy);
5384 GlDraw.vertex2 (x3+.dx, y3+.dy);
5386 GlDraw.ends ();
5388 ) state.layout
5389 ) rects
5391 Gl.disable `blend;
5394 let display () =
5395 GlClear.color (scalecolor2 conf.bgcolor);
5396 GlClear.clear [`color];
5397 let rec loop linkindexbase = function
5398 | l :: rest ->
5399 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5400 loop linkindexbase rest
5401 | [] -> ()
5403 loop 0 state.layout;
5404 let rects =
5405 match state.mode with
5406 | LinkNav (Ltexact (pageno, linkno)) ->
5407 begin match getopaque pageno with
5408 | Some opaque ->
5409 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5410 (pageno, 5, (
5411 float x0, float y0,
5412 float x1, float y0,
5413 float x1, float y1,
5414 float x0, float y1)
5415 ) :: state.rects
5416 | None -> state.rects
5418 | _ -> state.rects
5420 showrects rects;
5421 showsel ();
5422 state.uioh#display;
5423 begin match state.mstate with
5424 | Mzoomrect ((x0, y0), (x1, y1)) ->
5425 Gl.enable `blend;
5426 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5427 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5428 GlDraw.rect (float x0, float y0)
5429 (float x1, float y1);
5430 Gl.disable `blend;
5431 | _ -> ()
5432 end;
5433 enttext ();
5434 scrollindicator ();
5435 Wsi.swapb ();
5438 let zoomrect x y x1 y1 =
5439 let x0 = min x x1
5440 and x1 = max x x1
5441 and y0 = min y y1 in
5442 gotoy (state.y + y0);
5443 state.anchor <- getanchor ();
5444 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5445 let margin =
5446 if state.w < conf.winw - state.scrollw
5447 then (conf.winw - state.scrollw - state.w) / 2
5448 else 0
5450 state.x <- (state.x + margin) - x0;
5451 setzoom zoom;
5452 Wsi.setcursor Wsi.CURSOR_INHERIT;
5453 state.mstate <- Mnone;
5456 let scrollx x =
5457 let winw = conf.winw - state.scrollw - 1 in
5458 let s = float x /. float winw in
5459 let destx = truncate (float (state.w + winw) *. s) in
5460 state.x <- winw - destx;
5461 gotoy_and_clear_text state.y;
5462 state.mstate <- Mscrollx;
5465 let scrolly y =
5466 let s = float y /. float conf.winh in
5467 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5468 gotoy_and_clear_text desty;
5469 state.mstate <- Mscrolly;
5472 let viewmouse button down x y mask =
5473 match button with
5474 | n when (n == 4 || n == 5) && not down ->
5475 if Wsi.withctrl mask
5476 then (
5477 match state.mstate with
5478 | Mzoom (oldn, i) ->
5479 if oldn = n
5480 then (
5481 if i = 2
5482 then
5483 let incr =
5484 match n with
5485 | 5 ->
5486 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5487 | _ ->
5488 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5490 let zoom = conf.zoom -. incr in
5491 setzoom zoom;
5492 state.mstate <- Mzoom (n, 0);
5493 else
5494 state.mstate <- Mzoom (n, i+1);
5496 else state.mstate <- Mzoom (n, 0)
5498 | _ -> state.mstate <- Mzoom (n, 0)
5500 else (
5501 match state.autoscroll with
5502 | Some step -> setautoscrollspeed step (n=4)
5503 | None ->
5504 let incr =
5505 if n = 4
5506 then -conf.scrollstep
5507 else conf.scrollstep
5509 let incr = incr * 2 in
5510 let y = clamp incr in
5511 gotoy_and_clear_text y
5514 | 1 when Wsi.withctrl mask ->
5515 if down
5516 then (
5517 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5518 state.mstate <- Mpan (x, y)
5520 else
5521 state.mstate <- Mnone
5523 | 3 ->
5524 if down
5525 then (
5526 Wsi.setcursor Wsi.CURSOR_CYCLE;
5527 let p = (x, y) in
5528 state.mstate <- Mzoomrect (p, p)
5530 else (
5531 match state.mstate with
5532 | Mzoomrect ((x0, y0), _) ->
5533 if abs (x-x0) > 10 && abs (y - y0) > 10
5534 then zoomrect x0 y0 x y
5535 else (
5536 state.mstate <- Mnone;
5537 Wsi.setcursor Wsi.CURSOR_INHERIT;
5538 G.postRedisplay "kill accidental zoom rect";
5540 | _ ->
5541 Wsi.setcursor Wsi.CURSOR_INHERIT;
5542 state.mstate <- Mnone
5545 | 1 when x > conf.winw - state.scrollw ->
5546 if down
5547 then
5548 let _, position, sh = state.uioh#scrollph in
5549 if y > truncate position && y < truncate (position +. sh)
5550 then state.mstate <- Mscrolly
5551 else scrolly y
5552 else
5553 state.mstate <- Mnone
5555 | 1 when y > conf.winh - state.hscrollh ->
5556 if down
5557 then
5558 let _, position, sw = state.uioh#scrollpw in
5559 if x > truncate position && x < truncate (position +. sw)
5560 then state.mstate <- Mscrollx
5561 else scrollx x
5562 else
5563 state.mstate <- Mnone
5565 | 1 ->
5566 let dest = if down then getunder x y else Unone in
5567 begin match dest with
5568 | Ulinkgoto _
5569 | Ulinkuri _
5570 | Uremote _
5571 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5572 gotounder dest
5574 | Unone when down ->
5575 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5576 state.mstate <- Mpan (x, y);
5578 | Unone | Utext _ ->
5579 if down
5580 then (
5581 if conf.angle mod 360 = 0
5582 then (
5583 state.mstate <- Msel ((x, y), (x, y));
5584 G.postRedisplay "mouse select";
5587 else (
5588 match state.mstate with
5589 | Mnone -> ()
5591 | Mzoom _ | Mscrollx | Mscrolly ->
5592 state.mstate <- Mnone
5594 | Mzoomrect ((x0, y0), _) ->
5595 zoomrect x0 y0 x y
5597 | Mpan _ ->
5598 Wsi.setcursor Wsi.CURSOR_INHERIT;
5599 state.mstate <- Mnone
5601 | Msel ((_, y0), (_, y1)) ->
5602 let rec loop = function
5603 | [] -> ()
5604 | l :: rest ->
5605 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5606 || ((y1 >= l.pagedispy
5607 && y1 <= (l.pagedispy + l.pagevh)))
5608 then
5609 match getopaque l.pageno with
5610 | Some opaque ->
5611 begin
5612 match Ne.pipe () with
5613 | Ne.Exn exn ->
5614 showtext '!'
5615 (Printf.sprintf
5616 "can not create sel pipe: %s"
5617 (Printexc.to_string exn));
5618 | Ne.Res (r, w) ->
5619 let doclose what fd =
5620 Ne.clo fd (fun msg ->
5621 dolog "%s close failed: %s" what msg)
5623 let docopysel r w =
5624 copysel w opaque;
5625 doclose "pipe/r" r;
5626 G.postRedisplay "copysel"
5629 popen conf.selcmd [r, 0; w, -1];
5630 docopysel r w;
5631 doclose "pipe/r" r;
5632 with exn ->
5633 dolog "can not exectute %S: %s"
5634 conf.selcmd (Printexc.to_string exn);
5635 doclose "pipe/r" r;
5636 doclose "pipe/w" w;
5638 | None -> ()
5639 else loop rest
5641 loop state.layout;
5642 Wsi.setcursor Wsi.CURSOR_INHERIT;
5643 state.mstate <- Mnone;
5647 | _ -> ()
5650 let birdseyemouse button down x y mask
5651 (conf, leftx, _, hooverpageno, anchor) =
5652 match button with
5653 | 1 when down ->
5654 let rec loop = function
5655 | [] -> ()
5656 | l :: rest ->
5657 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5658 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5659 then (
5660 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5662 else loop rest
5664 loop state.layout
5665 | 3 -> ()
5666 | _ -> viewmouse button down x y mask
5669 let mouse button down x y mask =
5670 state.uioh <- state.uioh#button button down x y mask;
5673 let motion ~x ~y =
5674 state.uioh <- state.uioh#motion x y
5677 let pmotion ~x ~y =
5678 state.uioh <- state.uioh#pmotion x y;
5681 let uioh = object
5682 method display = ()
5684 method key key mask =
5685 begin match state.mode with
5686 | Textentry textentry -> textentrykeyboard key mask textentry
5687 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5688 | View -> viewkeyboard key mask
5689 | LinkNav linknav -> linknavkeyboard key mask linknav
5690 end;
5691 state.uioh
5693 method button button bstate x y mask =
5694 begin match state.mode with
5695 | LinkNav _
5696 | View -> viewmouse button bstate x y mask
5697 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5698 | Textentry _ -> ()
5699 end;
5700 state.uioh
5702 method motion x y =
5703 begin match state.mode with
5704 | Textentry _ -> ()
5705 | View | Birdseye _ | LinkNav _ ->
5706 match state.mstate with
5707 | Mzoom _ | Mnone -> ()
5709 | Mpan (x0, y0) ->
5710 let dx = x - x0
5711 and dy = y0 - y in
5712 state.mstate <- Mpan (x, y);
5713 if canpan ()
5714 then state.x <- state.x + dx;
5715 let y = clamp dy in
5716 gotoy_and_clear_text y
5718 | Msel (a, _) ->
5719 state.mstate <- Msel (a, (x, y));
5720 G.postRedisplay "motion select";
5722 | Mscrolly ->
5723 let y = min conf.winh (max 0 y) in
5724 scrolly y
5726 | Mscrollx ->
5727 let x = min conf.winw (max 0 x) in
5728 scrollx x
5730 | Mzoomrect (p0, _) ->
5731 state.mstate <- Mzoomrect (p0, (x, y));
5732 G.postRedisplay "motion zoomrect";
5733 end;
5734 state.uioh
5736 method pmotion x y =
5737 begin match state.mode with
5738 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5739 let rec loop = function
5740 | [] ->
5741 if hooverpageno != -1
5742 then (
5743 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5744 G.postRedisplay "pmotion birdseye no hoover";
5746 | l :: rest ->
5747 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5748 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5749 then (
5750 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5751 G.postRedisplay "pmotion birdseye hoover";
5753 else loop rest
5755 loop state.layout
5757 | Textentry _ -> ()
5759 | LinkNav _
5760 | View ->
5761 match state.mstate with
5762 | Mnone -> updateunder x y
5763 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5765 end;
5766 state.uioh
5768 method infochanged _ = ()
5770 method scrollph =
5771 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5772 let p, h = scrollph state.y maxy in
5773 state.scrollw, p, h
5775 method scrollpw =
5776 let winw = conf.winw - state.scrollw - 1 in
5777 let fwinw = float winw in
5778 let sw =
5779 let sw = fwinw /. float state.w in
5780 let sw = fwinw *. sw in
5781 max sw (float conf.scrollh)
5783 let position, sw =
5784 let f = state.w+winw in
5785 let r = float (winw-state.x) /. float f in
5786 let p = fwinw *. r in
5787 p-.sw/.2., sw
5789 let sw =
5790 if position +. sw > fwinw
5791 then fwinw -. position
5792 else sw
5794 state.hscrollh, position, sw
5796 method modehash =
5797 let modename =
5798 match state.mode with
5799 | LinkNav _ -> "links"
5800 | Textentry _ -> "textentry"
5801 | Birdseye _ -> "birdseye"
5802 | View -> "global"
5804 findkeyhash conf modename
5805 end;;
5807 module Config =
5808 struct
5809 open Parser
5811 let fontpath = ref "";;
5813 module KeyMap =
5814 Map.Make (struct type t = (int * int) let compare = compare end);;
5816 let unent s =
5817 let l = String.length s in
5818 let b = Buffer.create l in
5819 unent b s 0 l;
5820 Buffer.contents b;
5823 let home =
5824 try Sys.getenv "HOME"
5825 with exn ->
5826 prerr_endline
5827 ("Can not determine home directory location: " ^
5828 Printexc.to_string exn);
5832 let modifier_of_string = function
5833 | "alt" -> Wsi.altmask
5834 | "shift" -> Wsi.shiftmask
5835 | "ctrl" | "control" -> Wsi.ctrlmask
5836 | "meta" -> Wsi.metamask
5837 | _ -> 0
5840 let key_of_string =
5841 let r = Str.regexp "-" in
5842 fun s ->
5843 let elems = Str.full_split r s in
5844 let f n k m =
5845 let g s =
5846 let m1 = modifier_of_string s in
5847 if m1 = 0
5848 then (Wsi.namekey s, m)
5849 else (k, m lor m1)
5850 in function
5851 | Str.Delim s when n land 1 = 0 -> g s
5852 | Str.Text s -> g s
5853 | Str.Delim _ -> (k, m)
5855 let rec loop n k m = function
5856 | [] -> (k, m)
5857 | x :: xs ->
5858 let k, m = f n k m x in
5859 loop (n+1) k m xs
5861 loop 0 0 0 elems
5864 let keys_of_string =
5865 let r = Str.regexp "[ \t]" in
5866 fun s ->
5867 let elems = Str.split r s in
5868 List.map key_of_string elems
5871 let copykeyhashes c =
5872 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5875 let config_of c attrs =
5876 let apply c k v =
5878 match k with
5879 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5880 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5881 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5882 | "preload" -> { c with preload = bool_of_string v }
5883 | "page-bias" -> { c with pagebias = int_of_string v }
5884 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5885 | "auto-scroll-step" ->
5886 { c with autoscrollstep = max 0 (int_of_string v) }
5887 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5888 | "crop-hack" -> { c with crophack = bool_of_string v }
5889 | "throttle" ->
5890 let mw =
5891 match String.lowercase v with
5892 | "true" -> Some infinity
5893 | "false" -> None
5894 | f -> Some (float_of_string f)
5896 { c with maxwait = mw}
5897 | "highlight-links" -> { c with hlinks = bool_of_string v }
5898 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5899 | "vertical-margin" ->
5900 { c with interpagespace = max 0 (int_of_string v) }
5901 | "zoom" ->
5902 let zoom = float_of_string v /. 100. in
5903 let zoom = max zoom 0.0 in
5904 { c with zoom = zoom }
5905 | "presentation" -> { c with presentation = bool_of_string v }
5906 | "rotation-angle" -> { c with angle = int_of_string v }
5907 | "width" -> { c with winw = max 20 (int_of_string v) }
5908 | "height" -> { c with winh = max 20 (int_of_string v) }
5909 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5910 | "proportional-display" -> { c with proportional = bool_of_string v }
5911 | "pixmap-cache-size" ->
5912 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5913 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5914 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5915 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5916 | "persistent-location" -> { c with jumpback = bool_of_string v }
5917 | "background-color" -> { c with bgcolor = color_of_string v }
5918 | "scrollbar-in-presentation" ->
5919 { c with scrollbarinpm = bool_of_string v }
5920 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5921 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5922 | "mupdf-store-size" ->
5923 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5924 | "checkers" -> { c with checkers = bool_of_string v }
5925 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5926 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5927 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5928 | "uri-launcher" -> { c with urilauncher = unent v }
5929 | "path-launcher" -> { c with pathlauncher = unent v }
5930 | "color-space" -> { c with colorspace = colorspace_of_string v }
5931 | "invert-colors" -> { c with invert = bool_of_string v }
5932 | "brightness" -> { c with colorscale = float_of_string v }
5933 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5934 | "ghyllscroll" ->
5935 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5936 | "columns" ->
5937 let (n, _, _) as nab = multicolumns_of_string v in
5938 if n < 0
5939 then { c with columns = Csplit (-n, [||]) }
5940 else { c with columns = Cmulti (nab, [||]) }
5941 | "birds-eye-columns" ->
5942 { c with beyecolumns = Some (max (int_of_string v) 2) }
5943 | "selection-command" -> { c with selcmd = unent v }
5944 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5945 | _ -> c
5946 with exn ->
5947 prerr_endline ("Error processing attribute (`" ^
5948 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5951 let rec fold c = function
5952 | [] -> c
5953 | (k, v) :: rest ->
5954 let c = apply c k v in
5955 fold c rest
5957 fold { c with keyhashes = copykeyhashes c } attrs;
5960 let fromstring f pos n v d =
5961 try f v
5962 with exn ->
5963 dolog "Error processing attribute (%S=%S) at %d\n%s"
5964 n v pos (Printexc.to_string exn)
5969 let bookmark_of attrs =
5970 let rec fold title page rely = function
5971 | ("title", v) :: rest -> fold v page rely rest
5972 | ("page", v) :: rest -> fold title v rely rest
5973 | ("rely", v) :: rest -> fold title page v rest
5974 | _ :: rest -> fold title page rely rest
5975 | [] -> title, page, rely
5977 fold "invalid" "0" "0" attrs
5980 let doc_of attrs =
5981 let rec fold path page rely pan = function
5982 | ("path", v) :: rest -> fold v page rely pan rest
5983 | ("page", v) :: rest -> fold path v rely pan rest
5984 | ("rely", v) :: rest -> fold path page v pan rest
5985 | ("pan", v) :: rest -> fold path page rely v rest
5986 | _ :: rest -> fold path page rely pan rest
5987 | [] -> path, page, rely, pan
5989 fold "" "0" "0" "0" attrs
5992 let map_of attrs =
5993 let rec fold rs ls = function
5994 | ("out", v) :: rest -> fold v ls rest
5995 | ("in", v) :: rest -> fold rs v rest
5996 | _ :: rest -> fold ls rs rest
5997 | [] -> ls, rs
5999 fold "" "" attrs
6002 let setconf dst src =
6003 dst.scrollbw <- src.scrollbw;
6004 dst.scrollh <- src.scrollh;
6005 dst.icase <- src.icase;
6006 dst.preload <- src.preload;
6007 dst.pagebias <- src.pagebias;
6008 dst.verbose <- src.verbose;
6009 dst.scrollstep <- src.scrollstep;
6010 dst.maxhfit <- src.maxhfit;
6011 dst.crophack <- src.crophack;
6012 dst.autoscrollstep <- src.autoscrollstep;
6013 dst.maxwait <- src.maxwait;
6014 dst.hlinks <- src.hlinks;
6015 dst.underinfo <- src.underinfo;
6016 dst.interpagespace <- src.interpagespace;
6017 dst.zoom <- src.zoom;
6018 dst.presentation <- src.presentation;
6019 dst.angle <- src.angle;
6020 dst.winw <- src.winw;
6021 dst.winh <- src.winh;
6022 dst.savebmarks <- src.savebmarks;
6023 dst.memlimit <- src.memlimit;
6024 dst.proportional <- src.proportional;
6025 dst.texcount <- src.texcount;
6026 dst.sliceheight <- src.sliceheight;
6027 dst.thumbw <- src.thumbw;
6028 dst.jumpback <- src.jumpback;
6029 dst.bgcolor <- src.bgcolor;
6030 dst.scrollbarinpm <- src.scrollbarinpm;
6031 dst.tilew <- src.tilew;
6032 dst.tileh <- src.tileh;
6033 dst.mustoresize <- src.mustoresize;
6034 dst.checkers <- src.checkers;
6035 dst.aalevel <- src.aalevel;
6036 dst.trimmargins <- src.trimmargins;
6037 dst.trimfuzz <- src.trimfuzz;
6038 dst.urilauncher <- src.urilauncher;
6039 dst.colorspace <- src.colorspace;
6040 dst.invert <- src.invert;
6041 dst.colorscale <- src.colorscale;
6042 dst.redirectstderr <- src.redirectstderr;
6043 dst.ghyllscroll <- src.ghyllscroll;
6044 dst.columns <- src.columns;
6045 dst.beyecolumns <- src.beyecolumns;
6046 dst.selcmd <- src.selcmd;
6047 dst.updatecurs <- src.updatecurs;
6048 dst.pathlauncher <- src.pathlauncher;
6049 dst.keyhashes <- copykeyhashes src;
6052 let get s =
6053 let h = Hashtbl.create 10 in
6054 let dc = { defconf with angle = defconf.angle } in
6055 let rec toplevel v t spos _ =
6056 match t with
6057 | Vdata | Vcdata | Vend -> v
6058 | Vopen ("llppconfig", _, closed) ->
6059 if closed
6060 then v
6061 else { v with f = llppconfig }
6062 | Vopen _ ->
6063 error "unexpected subelement at top level" s spos
6064 | Vclose _ -> error "unexpected close at top level" s spos
6066 and llppconfig v t spos _ =
6067 match t with
6068 | Vdata | Vcdata -> v
6069 | Vend -> error "unexpected end of input in llppconfig" s spos
6070 | Vopen ("defaults", attrs, closed) ->
6071 let c = config_of dc attrs in
6072 setconf dc c;
6073 if closed
6074 then v
6075 else { v with f = defaults }
6077 | Vopen ("ui-font", attrs, closed) ->
6078 let rec getsize size = function
6079 | [] -> size
6080 | ("size", v) :: rest ->
6081 let size =
6082 fromstring int_of_string spos "size" v fstate.fontsize in
6083 getsize size rest
6084 | l -> getsize size l
6086 fstate.fontsize <- getsize fstate.fontsize attrs;
6087 if closed
6088 then v
6089 else { v with f = uifont (Buffer.create 10) }
6091 | Vopen ("doc", attrs, closed) ->
6092 let pathent, spage, srely, span = doc_of attrs in
6093 let path = unent pathent
6094 and pageno = fromstring int_of_string spos "page" spage 0
6095 and rely = fromstring float_of_string spos "rely" srely 0.0
6096 and pan = fromstring int_of_string spos "pan" span 0 in
6097 let c = config_of dc attrs in
6098 let anchor = (pageno, rely) in
6099 if closed
6100 then (Hashtbl.add h path (c, [], pan, anchor); v)
6101 else { v with f = doc path pan anchor c [] }
6103 | Vopen _ ->
6104 error "unexpected subelement in llppconfig" s spos
6106 | Vclose "llppconfig" -> { v with f = toplevel }
6107 | Vclose _ -> error "unexpected close in llppconfig" s spos
6109 and defaults v t spos _ =
6110 match t with
6111 | Vdata | Vcdata -> v
6112 | Vend -> error "unexpected end of input in defaults" s spos
6113 | Vopen ("keymap", attrs, closed) ->
6114 let modename =
6115 try List.assoc "mode" attrs
6116 with Not_found -> "global" in
6117 if closed
6118 then v
6119 else
6120 let ret keymap =
6121 let h = findkeyhash dc modename in
6122 KeyMap.iter (Hashtbl.replace h) keymap;
6123 defaults
6125 { v with f = pkeymap ret KeyMap.empty }
6127 | Vopen (_, _, _) ->
6128 error "unexpected subelement in defaults" s spos
6130 | Vclose "defaults" ->
6131 { v with f = llppconfig }
6133 | Vclose _ -> error "unexpected close in defaults" s spos
6135 and uifont b v t spos epos =
6136 match t with
6137 | Vdata | Vcdata ->
6138 Buffer.add_substring b s spos (epos - spos);
6140 | Vopen (_, _, _) ->
6141 error "unexpected subelement in ui-font" s spos
6142 | Vclose "ui-font" ->
6143 if String.length !fontpath = 0
6144 then fontpath := Buffer.contents b;
6145 { v with f = llppconfig }
6146 | Vclose _ -> error "unexpected close in ui-font" s spos
6147 | Vend -> error "unexpected end of input in ui-font" s spos
6149 and doc path pan anchor c bookmarks v t spos _ =
6150 match t with
6151 | Vdata | Vcdata -> v
6152 | Vend -> error "unexpected end of input in doc" s spos
6153 | Vopen ("bookmarks", _, closed) ->
6154 if closed
6155 then v
6156 else { v with f = pbookmarks path pan anchor c bookmarks }
6158 | Vopen ("keymap", attrs, closed) ->
6159 let modename =
6160 try List.assoc "mode" attrs
6161 with Not_found -> "global"
6163 if closed
6164 then v
6165 else
6166 let ret keymap =
6167 let h = findkeyhash c modename in
6168 KeyMap.iter (Hashtbl.replace h) keymap;
6169 doc path pan anchor c bookmarks
6171 { v with f = pkeymap ret KeyMap.empty }
6173 | Vopen (_, _, _) ->
6174 error "unexpected subelement in doc" s spos
6176 | Vclose "doc" ->
6177 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6178 { v with f = llppconfig }
6180 | Vclose _ -> error "unexpected close in doc" s spos
6182 and pkeymap ret keymap v t spos _ =
6183 match t with
6184 | Vdata | Vcdata -> v
6185 | Vend -> error "unexpected end of input in keymap" s spos
6186 | Vopen ("map", attrs, closed) ->
6187 let r, l = map_of attrs in
6188 let kss = fromstring keys_of_string spos "in" r [] in
6189 let lss = fromstring keys_of_string spos "out" l [] in
6190 let keymap =
6191 match kss with
6192 | [] -> keymap
6193 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6194 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6196 if closed
6197 then { v with f = pkeymap ret keymap }
6198 else
6199 let f () = v in
6200 { v with f = skip "map" f }
6202 | Vopen _ ->
6203 error "unexpected subelement in keymap" s spos
6205 | Vclose "keymap" ->
6206 { v with f = ret keymap }
6208 | Vclose _ -> error "unexpected close in keymap" s spos
6210 and pbookmarks path pan anchor c bookmarks v t spos _ =
6211 match t with
6212 | Vdata | Vcdata -> v
6213 | Vend -> error "unexpected end of input in bookmarks" s spos
6214 | Vopen ("item", attrs, closed) ->
6215 let titleent, spage, srely = bookmark_of attrs in
6216 let page = fromstring int_of_string spos "page" spage 0
6217 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6218 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6219 if closed
6220 then { v with f = pbookmarks path pan anchor c bookmarks }
6221 else
6222 let f () = v in
6223 { v with f = skip "item" f }
6225 | Vopen _ ->
6226 error "unexpected subelement in bookmarks" s spos
6228 | Vclose "bookmarks" ->
6229 { v with f = doc path pan anchor c bookmarks }
6231 | Vclose _ -> error "unexpected close in bookmarks" s spos
6233 and skip tag f v t spos _ =
6234 match t with
6235 | Vdata | Vcdata -> v
6236 | Vend ->
6237 error ("unexpected end of input in skipped " ^ tag) s spos
6238 | Vopen (tag', _, closed) ->
6239 if closed
6240 then v
6241 else
6242 let f' () = { v with f = skip tag f } in
6243 { v with f = skip tag' f' }
6244 | Vclose ctag ->
6245 if tag = ctag
6246 then f ()
6247 else error ("unexpected close in skipped " ^ tag) s spos
6250 parse { f = toplevel; accu = () } s;
6251 h, dc;
6254 let do_load f ic =
6256 let len = in_channel_length ic in
6257 let s = String.create len in
6258 really_input ic s 0 len;
6259 f s;
6260 with
6261 | Parse_error (msg, s, pos) ->
6262 let subs = subs s pos in
6263 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6264 failwith ("parse error: " ^ s)
6266 | exn ->
6267 failwith ("config load error: " ^ Printexc.to_string exn)
6270 let defconfpath =
6271 let dir =
6273 let dir = Filename.concat home ".config" in
6274 if Sys.is_directory dir then dir else home
6275 with _ -> home
6277 Filename.concat dir "llpp.conf"
6280 let confpath = ref defconfpath;;
6282 let load1 f =
6283 if Sys.file_exists !confpath
6284 then
6285 match
6286 (try Some (open_in_bin !confpath)
6287 with exn ->
6288 prerr_endline
6289 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6290 Printexc.to_string exn);
6291 None
6293 with
6294 | Some ic ->
6295 begin try
6296 f (do_load get ic)
6297 with exn ->
6298 prerr_endline
6299 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6300 Printexc.to_string exn);
6301 end;
6302 close_in ic;
6304 | None -> ()
6305 else
6306 f (Hashtbl.create 0, defconf)
6309 let load () =
6310 let f (h, dc) =
6311 let pc, pb, px, pa =
6313 Hashtbl.find h (Filename.basename state.path)
6314 with Not_found -> dc, [], 0, (0, 0.0)
6316 setconf defconf dc;
6317 setconf conf pc;
6318 state.bookmarks <- pb;
6319 state.x <- px;
6320 state.scrollw <- conf.scrollbw;
6321 if conf.jumpback
6322 then state.anchor <- pa;
6323 cbput state.hists.nav pa;
6325 load1 f
6328 let add_attrs bb always dc c =
6329 let ob s a b =
6330 if always || a != b
6331 then Printf.bprintf bb "\n %s='%b'" s a
6332 and oi s a b =
6333 if always || a != b
6334 then Printf.bprintf bb "\n %s='%d'" s a
6335 and oI s a b =
6336 if always || a != b
6337 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6338 and oz s a b =
6339 if always || a <> b
6340 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6341 and oF s a b =
6342 if always || a <> b
6343 then Printf.bprintf bb "\n %s='%f'" s a
6344 and oc s a b =
6345 if always || a <> b
6346 then
6347 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6348 and oC s a b =
6349 if always || a <> b
6350 then
6351 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6352 and oR s a b =
6353 if always || a <> b
6354 then
6355 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6356 and os s a b =
6357 if always || a <> b
6358 then
6359 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6360 and og s a b =
6361 if always || a <> b
6362 then
6363 match a with
6364 | None -> ()
6365 | Some (_N, _A, _B) ->
6366 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6367 and oW s a b =
6368 if always || a <> b
6369 then
6370 let v =
6371 match a with
6372 | None -> "false"
6373 | Some f ->
6374 if f = infinity
6375 then "true"
6376 else string_of_float f
6378 Printf.bprintf bb "\n %s='%s'" s v
6379 and oco s a b =
6380 if always || a <> b
6381 then
6382 match a with
6383 | Cmulti ((n, a, b), _) when n > 1 ->
6384 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6385 | Csplit (n, _) when n > 1 ->
6386 Printf.bprintf bb "\n %s='%d'" s ~-n
6387 | _ -> ()
6388 and obeco s a b =
6389 if always || a <> b
6390 then
6391 match a with
6392 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6393 | _ -> ()
6395 let w, h =
6396 if always
6397 then dc.winw, dc.winh
6398 else
6399 match state.fullscreen with
6400 | Some wh -> wh
6401 | None -> c.winw, c.winh
6403 let zoom, presentation, interpagespace, maxwait =
6404 if always
6405 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6406 else
6407 match state.mode with
6408 | Birdseye (bc, _, _, _, _) ->
6409 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6410 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6412 oi "width" w dc.winw;
6413 oi "height" h dc.winh;
6414 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6415 oi "scroll-handle-height" c.scrollh dc.scrollh;
6416 ob "case-insensitive-search" c.icase dc.icase;
6417 ob "preload" c.preload dc.preload;
6418 oi "page-bias" c.pagebias dc.pagebias;
6419 oi "scroll-step" c.scrollstep dc.scrollstep;
6420 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6421 ob "max-height-fit" c.maxhfit dc.maxhfit;
6422 ob "crop-hack" c.crophack dc.crophack;
6423 oW "throttle" maxwait dc.maxwait;
6424 ob "highlight-links" c.hlinks dc.hlinks;
6425 ob "under-cursor-info" c.underinfo dc.underinfo;
6426 oi "vertical-margin" interpagespace dc.interpagespace;
6427 oz "zoom" zoom dc.zoom;
6428 ob "presentation" presentation dc.presentation;
6429 oi "rotation-angle" c.angle dc.angle;
6430 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6431 ob "proportional-display" c.proportional dc.proportional;
6432 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6433 oi "tex-count" c.texcount dc.texcount;
6434 oi "slice-height" c.sliceheight dc.sliceheight;
6435 oi "thumbnail-width" c.thumbw dc.thumbw;
6436 ob "persistent-location" c.jumpback dc.jumpback;
6437 oc "background-color" c.bgcolor dc.bgcolor;
6438 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6439 oi "tile-width" c.tilew dc.tilew;
6440 oi "tile-height" c.tileh dc.tileh;
6441 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6442 ob "checkers" c.checkers dc.checkers;
6443 oi "aalevel" c.aalevel dc.aalevel;
6444 ob "trim-margins" c.trimmargins dc.trimmargins;
6445 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6446 os "uri-launcher" c.urilauncher dc.urilauncher;
6447 os "path-launcher" c.pathlauncher dc.pathlauncher;
6448 oC "color-space" c.colorspace dc.colorspace;
6449 ob "invert-colors" c.invert dc.invert;
6450 oF "brightness" c.colorscale dc.colorscale;
6451 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6452 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6453 oco "columns" c.columns dc.columns;
6454 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6455 os "selection-command" c.selcmd dc.selcmd;
6456 ob "update-cursor" c.updatecurs dc.updatecurs;
6459 let keymapsbuf always dc c =
6460 let bb = Buffer.create 16 in
6461 let rec loop = function
6462 | [] -> ()
6463 | (modename, h) :: rest ->
6464 let dh = findkeyhash dc modename in
6465 if always || h <> dh
6466 then (
6467 if Hashtbl.length h > 0
6468 then (
6469 if Buffer.length bb > 0
6470 then Buffer.add_char bb '\n';
6471 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6472 Hashtbl.iter (fun i o ->
6473 let isdifferent = always ||
6475 let dO = Hashtbl.find dh i in
6476 dO <> o
6477 with Not_found -> true
6479 if isdifferent
6480 then
6481 let addkm (k, m) =
6482 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6483 if Wsi.withalt m then Buffer.add_string bb "alt-";
6484 if Wsi.withshift m then Buffer.add_string bb "shift-";
6485 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6486 Buffer.add_string bb (Wsi.keyname k);
6488 let addkms l =
6489 let rec loop = function
6490 | [] -> ()
6491 | km :: [] -> addkm km
6492 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6494 loop l
6496 Buffer.add_string bb "<map in='";
6497 addkm i;
6498 match o with
6499 | KMinsrt km ->
6500 Buffer.add_string bb "' out='";
6501 addkm km;
6502 Buffer.add_string bb "'/>\n"
6504 | KMinsrl kms ->
6505 Buffer.add_string bb "' out='";
6506 addkms kms;
6507 Buffer.add_string bb "'/>\n"
6509 | KMmulti (ins, kms) ->
6510 Buffer.add_char bb ' ';
6511 addkms ins;
6512 Buffer.add_string bb "' out='";
6513 addkms kms;
6514 Buffer.add_string bb "'/>\n"
6515 ) h;
6516 Buffer.add_string bb "</keymap>";
6519 loop rest
6521 loop c.keyhashes;
6525 let save () =
6526 let uifontsize = fstate.fontsize in
6527 let bb = Buffer.create 32768 in
6528 let f (h, dc) =
6529 let dc = if conf.bedefault then conf else dc in
6530 Buffer.add_string bb "<llppconfig>\n";
6532 if String.length !fontpath > 0
6533 then
6534 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6535 uifontsize
6536 !fontpath
6537 else (
6538 if uifontsize <> 14
6539 then
6540 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6543 Buffer.add_string bb "<defaults ";
6544 add_attrs bb true dc dc;
6545 let kb = keymapsbuf true dc dc in
6546 if Buffer.length kb > 0
6547 then (
6548 Buffer.add_string bb ">\n";
6549 Buffer.add_buffer bb kb;
6550 Buffer.add_string bb "\n</defaults>\n";
6552 else Buffer.add_string bb "/>\n";
6554 let adddoc path pan anchor c bookmarks =
6555 if bookmarks == [] && c = dc && anchor = emptyanchor
6556 then ()
6557 else (
6558 Printf.bprintf bb "<doc path='%s'"
6559 (enent path 0 (String.length path));
6561 if anchor <> emptyanchor
6562 then (
6563 let n, y = anchor in
6564 Printf.bprintf bb " page='%d'" n;
6565 if y > 1e-6
6566 then
6567 Printf.bprintf bb " rely='%f'" y
6571 if pan != 0
6572 then Printf.bprintf bb " pan='%d'" pan;
6574 add_attrs bb false dc c;
6575 let kb = keymapsbuf false dc c in
6577 begin match bookmarks with
6578 | [] ->
6579 if Buffer.length kb > 0
6580 then (
6581 Buffer.add_string bb ">\n";
6582 Buffer.add_buffer bb kb;
6583 Buffer.add_string bb "</doc>\n";
6585 else Buffer.add_string bb "/>\n"
6586 | _ ->
6587 Buffer.add_string bb ">\n<bookmarks>\n";
6588 List.iter (fun (title, _level, (page, rely)) ->
6589 Printf.bprintf bb
6590 "<item title='%s' page='%d'"
6591 (enent title 0 (String.length title))
6592 page
6594 if rely > 1e-6
6595 then
6596 Printf.bprintf bb " rely='%f'" rely
6598 Buffer.add_string bb "/>\n";
6599 ) bookmarks;
6600 Buffer.add_string bb "</bookmarks>";
6601 if Buffer.length kb > 0
6602 then (
6603 Buffer.add_string bb "\n";
6604 Buffer.add_buffer bb kb;
6606 Buffer.add_string bb "\n</doc>\n";
6607 end;
6611 let pan, conf =
6612 match state.mode with
6613 | Birdseye (c, pan, _, _, _) ->
6614 let beyecolumns =
6615 match conf.columns with
6616 | Cmulti ((c, _, _), _) -> Some c
6617 | Csingle -> None
6618 | Csplit _ -> None
6619 and columns =
6620 match c.columns with
6621 | Cmulti (c, _) -> Cmulti (c, [||])
6622 | Csingle -> Csingle
6623 | Csplit _ -> failwith "quit from bird's eye while split"
6625 pan, { c with beyecolumns = beyecolumns; columns = columns }
6626 | _ -> state.x, conf
6628 let basename = Filename.basename state.path in
6629 adddoc basename pan (getanchor ())
6630 { conf with
6631 autoscrollstep =
6632 match state.autoscroll with
6633 | Some step -> step
6634 | None -> conf.autoscrollstep }
6635 (if conf.savebmarks then state.bookmarks else []);
6637 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6638 if basename <> path
6639 then adddoc path x y c bookmarks
6640 ) h;
6641 Buffer.add_string bb "</llppconfig>";
6643 load1 f;
6644 if Buffer.length bb > 0
6645 then
6647 let tmp = !confpath ^ ".tmp" in
6648 let oc = open_out_bin tmp in
6649 Buffer.output_buffer oc bb;
6650 close_out oc;
6651 Unix.rename tmp !confpath;
6652 with exn ->
6653 prerr_endline
6654 ("error while saving configuration: " ^ Printexc.to_string exn)
6656 end;;
6658 let () =
6659 Arg.parse
6660 (Arg.align
6661 [("-p", Arg.String (fun s -> state.password <- s) ,
6662 "<password> Set password");
6664 ("-f", Arg.String (fun s -> Config.fontpath := s),
6665 "<path> Set path to the user interface font");
6667 ("-c", Arg.String (fun s -> Config.confpath := s),
6668 "<path> Set path to the configuration file");
6670 ("-v", Arg.Unit (fun () ->
6671 Printf.printf
6672 "%s\nconfiguration path: %s\n"
6673 (version ())
6674 Config.defconfpath
6676 exit 0), " Print version and exit");
6679 (fun s -> state.path <- s)
6680 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6682 if String.length state.path = 0
6683 then (prerr_endline "file name missing"; exit 1);
6685 Config.load ();
6687 let globalkeyhash = findkeyhash conf "global" in
6688 let wsfd, winw, winh = Wsi.init (object
6689 method expose =
6690 if nogeomcmds state.geomcmds || platform == Posx
6691 then display ()
6692 else (
6693 GlFunc.draw_buffer `front;
6694 GlClear.color (scalecolor2 conf.bgcolor);
6695 GlClear.clear [`color];
6696 GlFunc.draw_buffer `back;
6698 method display = display ()
6699 method reshape w h = reshape w h
6700 method mouse b d x y m = mouse b d x y m
6701 method motion x y = state.mpos <- (x, y); motion x y
6702 method pmotion x y = state.mpos <- (x, y); pmotion x y
6703 method key k m =
6704 let mascm = m land (
6705 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6706 ) in
6707 match state.keystate with
6708 | KSnone ->
6709 let km = k, mascm in
6710 begin
6711 match
6712 try Hashtbl.find globalkeyhash km
6713 with Not_found ->
6714 let modehash = state.uioh#modehash in
6715 try Hashtbl.find modehash km
6716 with Not_found -> KMinsrt (k, m)
6717 with
6718 | KMinsrt (k, m) -> keyboard k m
6719 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6720 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6722 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6723 List.iter (fun (k, m) -> keyboard k m) insrt;
6724 state.keystate <- KSnone
6725 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6726 state.keystate <- KSinto (keys, insrt)
6727 | _ ->
6728 state.keystate <- KSnone
6730 method enter x y = state.mpos <- (x, y); pmotion x y
6731 method leave = state.mpos <- (-1, -1)
6732 method quit = raise Quit
6733 end) conf.winw conf.winh (platform = Posx) in
6735 state.wsfd <- wsfd;
6737 if not (
6738 List.exists GlMisc.check_extension
6739 [ "GL_ARB_texture_rectangle"
6740 ; "GL_EXT_texture_recangle"
6741 ; "GL_NV_texture_rectangle" ]
6743 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6745 let cr, sw =
6746 match Ne.pipe () with
6747 | Ne.Exn exn ->
6748 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6749 exit 1
6750 | Ne.Res rw -> rw
6751 and sr, cw =
6752 match Ne.pipe () with
6753 | Ne.Exn exn ->
6754 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6755 exit 1
6756 | Ne.Res rw -> rw
6759 cloexec cr;
6760 cloexec sw;
6761 cloexec sr;
6762 cloexec cw;
6764 setcheckers conf.checkers;
6765 redirectstderr ();
6767 init (cr, cw) (
6768 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6769 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6770 !Config.fontpath
6772 state.sr <- sr;
6773 state.sw <- sw;
6774 state.text <- "Opening " ^ state.path;
6775 reshape winw winh;
6776 opendoc state.path state.password;
6777 state.uioh <- uioh;
6779 let rec loop deadline =
6780 let r =
6781 match state.errfd with
6782 | None -> [state.sr; state.wsfd]
6783 | Some fd -> [state.sr; state.wsfd; fd]
6785 if state.redisplay
6786 then (
6787 state.redisplay <- false;
6788 display ();
6790 let timeout =
6791 let now = now () in
6792 if deadline > now
6793 then (
6794 if deadline = infinity
6795 then ~-.1.0
6796 else max 0.0 (deadline -. now)
6798 else 0.0
6800 let r, _, _ =
6801 try Unix.select r [] [] timeout
6802 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6804 begin match r with
6805 | [] ->
6806 state.ghyll None;
6807 let newdeadline =
6808 if state.ghyll == noghyll
6809 then
6810 match state.autoscroll with
6811 | Some step when step != 0 ->
6812 let y = state.y + step in
6813 let y =
6814 if y < 0
6815 then state.maxy
6816 else if y >= state.maxy then 0 else y
6818 gotoy y;
6819 if state.mode = View
6820 then state.text <- "";
6821 deadline +. 0.01
6822 | _ -> infinity
6823 else deadline +. 0.01
6825 loop newdeadline
6827 | l ->
6828 let rec checkfds = function
6829 | [] -> ()
6830 | fd :: rest when fd = state.sr ->
6831 let cmd = readcmd state.sr in
6832 act cmd;
6833 checkfds rest
6835 | fd :: rest when fd = state.wsfd ->
6836 Wsi.readresp fd;
6837 checkfds rest
6839 | fd :: rest ->
6840 let s = String.create 80 in
6841 let n = Unix.read fd s 0 80 in
6842 if conf.redirectstderr
6843 then (
6844 Buffer.add_substring state.errmsgs s 0 n;
6845 state.newerrmsgs <- true;
6846 state.redisplay <- true;
6848 else (
6849 prerr_string (String.sub s 0 n);
6850 flush stderr;
6852 checkfds rest
6854 checkfds l;
6855 let newdeadline =
6856 let deadline1 =
6857 if deadline = infinity
6858 then now () +. 0.01
6859 else deadline
6861 match state.autoscroll with
6862 | Some step when step != 0 -> deadline1
6863 | _ -> if state.ghyll == noghyll then infinity else deadline1
6865 loop newdeadline
6866 end;
6869 loop infinity;
6870 with Quit ->
6871 Config.save ();
6872 exit 0;