Hide non-applicable links
[llpp.git] / main.ml
blobb06f815ebf517f7226ca077f35a76e402440b207
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
254 let debugl l =
255 dolog "l %d dim=%d {" l.pageno l.pagedimno;
256 dolog " WxH %dx%d" l.pagew l.pageh;
257 dolog " vWxH %dx%d" l.pagevw l.pagevh;
258 dolog " pagex,y %d,%d" l.pagex l.pagey;
259 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
260 dolog "}";
263 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
264 dolog "rect {";
265 dolog " x0,y0=(% f, % f)" x0 y0;
266 dolog " x1,y1=(% f, % f)" x1 y1;
267 dolog " x2,y2=(% f, % f)" x2 y2;
268 dolog " x3,y3=(% f, % f)" x3 y3;
269 dolog "}";
272 type multicolumns = multicol * pagegeom
273 and splitcolumns = columncount * pagegeom
274 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
275 and multicol = columncount * covercount * covercount
276 and pdimno = int
277 and columncount = int
278 and covercount = int;;
280 type conf =
281 { mutable scrollbw : int
282 ; mutable scrollh : int
283 ; mutable icase : bool
284 ; mutable preload : bool
285 ; mutable pagebias : int
286 ; mutable verbose : bool
287 ; mutable debug : bool
288 ; mutable scrollstep : int
289 ; mutable maxhfit : bool
290 ; mutable crophack : bool
291 ; mutable autoscrollstep : int
292 ; mutable maxwait : float option
293 ; mutable hlinks : bool
294 ; mutable underinfo : bool
295 ; mutable interpagespace : interpagespace
296 ; mutable zoom : float
297 ; mutable presentation : bool
298 ; mutable angle : angle
299 ; mutable winw : int
300 ; mutable winh : int
301 ; mutable savebmarks : bool
302 ; mutable proportional : proportional
303 ; mutable trimmargins : trimmargins
304 ; mutable trimfuzz : irect
305 ; mutable memlimit : memsize
306 ; mutable texcount : texcount
307 ; mutable sliceheight : sliceheight
308 ; mutable thumbw : width
309 ; mutable jumpback : bool
310 ; mutable bgcolor : float * float * float
311 ; mutable bedefault : bool
312 ; mutable scrollbarinpm : bool
313 ; mutable tilew : int
314 ; mutable tileh : int
315 ; mutable mustoresize : memsize
316 ; mutable checkers : bool
317 ; mutable aalevel : int
318 ; mutable urilauncher : string
319 ; mutable pathlauncher : string
320 ; mutable colorspace : colorspace
321 ; mutable invert : bool
322 ; mutable colorscale : float
323 ; mutable redirectstderr : bool
324 ; mutable ghyllscroll : (int * int * int) option
325 ; mutable columns : columns
326 ; mutable beyecolumns : columncount option
327 ; mutable selcmd : string
328 ; mutable updatecurs : bool
329 ; mutable keyhashes : (string * keyhash) list
331 and columns =
332 | Csingle
333 | Cmulti of multicolumns
334 | Csplit of splitcolumns
337 type anchor = pageno * top;;
339 type outline = string * int * anchor;;
341 type rect = float * float * float * float * float * float * float * float;;
343 type tile = opaque * pixmapsize * elapsed
344 and elapsed = float;;
345 type pagemapkey = pageno * gen;;
346 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
347 and row = int
348 and col = int;;
350 let emptyanchor = (0, 0.0);;
352 type infochange = | Memused | Docinfo | Pdim;;
354 class type uioh = object
355 method display : unit
356 method key : int -> int -> uioh
357 method button : int -> bool -> int -> int -> int -> uioh
358 method motion : int -> int -> uioh
359 method pmotion : int -> int -> uioh
360 method infochanged : infochange -> unit
361 method scrollpw : (int * float * float)
362 method scrollph : (int * float * float)
363 method modehash : keyhash
364 end;;
366 type mode =
367 | Birdseye of (conf * leftx * pageno * pageno * anchor)
368 | Textentry of (textentry * onleave)
369 | View
370 | LinkNav of linktarget
371 and onleave = leavetextentrystatus -> unit
372 and leavetextentrystatus = | Cancel | Confirm
373 and helpitem = string * int * action
374 and action =
375 | Noaction
376 | Action of (uioh -> uioh)
377 and linktarget =
378 | Ltexact of (pageno * int)
379 | Ltgendir of int
382 let isbirdseye = function Birdseye _ -> true | _ -> false;;
383 let istextentry = function Textentry _ -> true | _ -> false;;
385 type currently =
386 | Idle
387 | Loading of (page * gen)
388 | Tiling of (
389 page * opaque * colorspace * angle * gen * col * row * width * height
391 | Outlining of outline list
394 let emptykeyhash = Hashtbl.create 0;;
395 let nouioh : uioh = object (self)
396 method display = ()
397 method key _ _ = self
398 method button _ _ _ _ _ = self
399 method motion _ _ = self
400 method pmotion _ _ = self
401 method infochanged _ = ()
402 method scrollpw = (0, nan, nan)
403 method scrollph = (0, nan, nan)
404 method modehash = emptykeyhash
405 end;;
407 type state =
408 { mutable sr : Unix.file_descr
409 ; mutable sw : Unix.file_descr
410 ; mutable wsfd : Unix.file_descr
411 ; mutable errfd : Unix.file_descr option
412 ; mutable stderr : Unix.file_descr
413 ; mutable errmsgs : Buffer.t
414 ; mutable newerrmsgs : bool
415 ; mutable w : int
416 ; mutable x : int
417 ; mutable y : int
418 ; mutable scrollw : int
419 ; mutable hscrollh : int
420 ; mutable anchor : anchor
421 ; mutable ranchors : (string * string * anchor) list
422 ; mutable maxy : int
423 ; mutable layout : page list
424 ; pagemap : (pagemapkey, opaque) Hashtbl.t
425 ; tilemap : (tilemapkey, tile) Hashtbl.t
426 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
427 ; mutable pdims : (pageno * width * height * leftx) list
428 ; mutable pagecount : int
429 ; mutable currently : currently
430 ; mutable mstate : mstate
431 ; mutable searchpattern : string
432 ; mutable rects : (pageno * recttype * rect) list
433 ; mutable rects1 : (pageno * recttype * rect) list
434 ; mutable text : string
435 ; mutable fullscreen : (width * height) option
436 ; mutable mode : mode
437 ; mutable uioh : uioh
438 ; mutable outlines : outline array
439 ; mutable bookmarks : outline list
440 ; mutable path : string
441 ; mutable password : string
442 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
443 ; mutable memused : memsize
444 ; mutable gen : gen
445 ; mutable throttle : (page list * int * float) option
446 ; mutable autoscroll : int option
447 ; mutable ghyll : (int option -> unit)
448 ; mutable help : helpitem array
449 ; mutable docinfo : (int * string) list
450 ; mutable texid : GlTex.texture_id option
451 ; hists : hists
452 ; mutable prevzoom : float
453 ; mutable progress : float
454 ; mutable redisplay : bool
455 ; mutable mpos : mpos
456 ; mutable keystate : keystate
457 ; mutable glinks : bool
459 and hists =
460 { pat : string circbuf
461 ; pag : string circbuf
462 ; nav : anchor circbuf
463 ; sel : string circbuf
467 let defconf =
468 { scrollbw = 7
469 ; scrollh = 12
470 ; icase = true
471 ; preload = true
472 ; pagebias = 0
473 ; verbose = false
474 ; debug = false
475 ; scrollstep = 24
476 ; maxhfit = true
477 ; crophack = false
478 ; autoscrollstep = 2
479 ; maxwait = None
480 ; hlinks = false
481 ; underinfo = false
482 ; interpagespace = 2
483 ; zoom = 1.0
484 ; presentation = false
485 ; angle = 0
486 ; winw = 900
487 ; winh = 900
488 ; savebmarks = true
489 ; proportional = true
490 ; trimmargins = false
491 ; trimfuzz = (0,0,0,0)
492 ; memlimit = 32 lsl 20
493 ; texcount = 256
494 ; sliceheight = 24
495 ; thumbw = 76
496 ; jumpback = true
497 ; bgcolor = (0.5, 0.5, 0.5)
498 ; bedefault = false
499 ; scrollbarinpm = true
500 ; tilew = 2048
501 ; tileh = 2048
502 ; mustoresize = 256 lsl 20
503 ; checkers = true
504 ; aalevel = 8
505 ; urilauncher =
506 (match platform with
507 | Plinux | Pfreebsd | Pdragonflybsd
508 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
509 | Posx -> "open \"%s\""
510 | Pcygwin -> "cygstart \"%s\""
511 | Punknown -> "echo %s")
512 ; pathlauncher = "lp \"%s\""
513 ; selcmd =
514 (match platform with
515 | Plinux | Pfreebsd | Pdragonflybsd
516 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
517 | Posx -> "pbcopy"
518 | Pcygwin -> "wsel"
519 | Punknown -> "cat")
520 ; colorspace = Rgb
521 ; invert = false
522 ; colorscale = 1.0
523 ; redirectstderr = false
524 ; ghyllscroll = None
525 ; columns = Csingle
526 ; beyecolumns = None
527 ; updatecurs = false
528 ; keyhashes =
529 let mk n = (n, Hashtbl.create 1) in
530 [ mk "global"
531 ; mk "info"
532 ; mk "help"
533 ; mk "outline"
534 ; mk "listview"
535 ; mk "birdseye"
536 ; mk "textentry"
537 ; mk "links"
542 let findkeyhash c name =
543 try List.assoc name c.keyhashes
544 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
547 let conf = { defconf with angle = defconf.angle };;
549 type fontstate =
550 { mutable fontsize : int
551 ; mutable wwidth : float
552 ; mutable maxrows : int
556 let fstate =
557 { fontsize = 14
558 ; wwidth = nan
559 ; maxrows = -1
563 let setfontsize n =
564 fstate.fontsize <- n;
565 fstate.wwidth <- measurestr fstate.fontsize "w";
566 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
569 let geturl s =
570 let colonpos = try String.index s ':' with Not_found -> -1 in
571 let len = String.length s in
572 if colonpos >= 0 && colonpos + 3 < len
573 then (
574 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
575 then
576 let schemestartpos =
577 try String.rindex_from s colonpos ' '
578 with Not_found -> -1
580 let scheme =
581 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
583 match scheme with
584 | "http" | "ftp" | "mailto" ->
585 let epos =
586 try String.index_from s colonpos ' '
587 with Not_found -> len
589 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
590 | _ -> ""
591 else ""
593 else ""
596 let gotouri uri =
597 if String.length conf.urilauncher = 0
598 then print_endline uri
599 else (
600 let url = geturl uri in
601 if String.length url = 0
602 then print_endline uri
603 else
604 let re = Str.regexp "%s" in
605 let command = Str.global_replace re url conf.urilauncher in
606 try popen command []
607 with exn ->
608 Printf.eprintf
609 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
610 flush stderr;
614 let version () =
615 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
616 (platform_to_string platform) Sys.word_size Sys.ocaml_version
619 let makehelp () =
620 let strings = version () :: "" :: Help.keys in
621 Array.of_list (
622 List.map (fun s ->
623 let url = geturl s in
624 if String.length url > 0
625 then (s, 0, Action (fun u -> gotouri url; u))
626 else (s, 0, Noaction)
627 ) strings);
630 let noghyll _ = ();;
631 let firstgeomcmds = "", [];;
633 let state =
634 { sr = Unix.stdin
635 ; sw = Unix.stdin
636 ; wsfd = Unix.stdin
637 ; errfd = None
638 ; stderr = Unix.stderr
639 ; errmsgs = Buffer.create 0
640 ; newerrmsgs = false
641 ; x = 0
642 ; y = 0
643 ; w = 0
644 ; scrollw = 0
645 ; hscrollh = 0
646 ; anchor = emptyanchor
647 ; ranchors = []
648 ; layout = []
649 ; maxy = max_int
650 ; tilelru = Queue.create ()
651 ; pagemap = Hashtbl.create 10
652 ; tilemap = Hashtbl.create 10
653 ; pdims = []
654 ; pagecount = 0
655 ; currently = Idle
656 ; mstate = Mnone
657 ; rects = []
658 ; rects1 = []
659 ; text = ""
660 ; mode = View
661 ; fullscreen = None
662 ; searchpattern = ""
663 ; outlines = [||]
664 ; bookmarks = []
665 ; path = ""
666 ; password = ""
667 ; geomcmds = firstgeomcmds
668 ; hists =
669 { nav = cbnew 10 (0, 0.0)
670 ; pat = cbnew 10 ""
671 ; pag = cbnew 10 ""
672 ; sel = cbnew 10 ""
674 ; memused = 0
675 ; gen = 0
676 ; throttle = None
677 ; autoscroll = None
678 ; ghyll = noghyll
679 ; help = makehelp ()
680 ; docinfo = []
681 ; texid = None
682 ; prevzoom = 1.0
683 ; progress = -1.0
684 ; uioh = nouioh
685 ; redisplay = true
686 ; mpos = (-1, -1)
687 ; keystate = KSnone
688 ; glinks = false
692 let vlog fmt =
693 if conf.verbose
694 then
695 Printf.kprintf prerr_endline fmt
696 else
697 Printf.kprintf ignore fmt
700 let launchpath () =
701 if String.length conf.pathlauncher = 0
702 then print_endline state.path
703 else (
704 let re = Str.regexp "%s" in
705 let command = Str.global_replace re state.path conf.pathlauncher in
706 try popen command []
707 with exn ->
708 Printf.eprintf
709 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
710 flush stderr;
714 let redirectstderr () =
715 if conf.redirectstderr
716 then
717 let rfd, wfd = Unix.pipe () in
718 state.stderr <- Unix.dup Unix.stderr;
719 state.errfd <- Some rfd;
720 Unix.dup2 wfd Unix.stderr;
721 else (
722 state.newerrmsgs <- false;
723 begin match state.errfd with
724 | Some fd ->
725 Unix.close fd;
726 Unix.dup2 state.stderr Unix.stderr;
727 state.errfd <- None;
728 | None -> ()
729 end;
730 prerr_string (Buffer.contents state.errmsgs);
731 flush stderr;
732 Buffer.clear state.errmsgs;
736 module G =
737 struct
738 let postRedisplay who =
739 if conf.verbose
740 then prerr_endline ("redisplay for " ^ who);
741 state.redisplay <- true;
743 end;;
745 let getopaque pageno =
746 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
747 with Not_found -> None
750 let putopaque pageno opaque =
751 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
754 let pagetranslatepoint l x y =
755 let dy = y - l.pagedispy in
756 let y = dy + l.pagey in
757 let dx = x - l.pagedispx in
758 let x = dx + l.pagex in
759 (x, y);
762 let getunder x y =
763 let rec f = function
764 | l :: rest ->
765 begin match getopaque l.pageno with
766 | Some opaque ->
767 let x0 = l.pagedispx in
768 let x1 = x0 + l.pagevw in
769 let y0 = l.pagedispy in
770 let y1 = y0 + l.pagevh in
771 if y >= y0 && y <= y1 && x >= x0 && x <= x1
772 then
773 let px, py = pagetranslatepoint l x y in
774 match whatsunder opaque px py with
775 | Unone -> f rest
776 | under -> under
777 else f rest
778 | _ ->
779 f rest
781 | [] -> Unone
783 f state.layout
786 let showtext c s =
787 state.text <- Printf.sprintf "%c%s" c s;
788 G.postRedisplay "showtext";
791 let updateunder x y =
792 match getunder x y with
793 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
794 | Ulinkuri uri ->
795 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
796 Wsi.setcursor Wsi.CURSOR_INFO
797 | Ulinkgoto (page, _) ->
798 if conf.underinfo
799 then showtext 'p' ("age: " ^ string_of_int (page+1));
800 Wsi.setcursor Wsi.CURSOR_INFO
801 | Utext s ->
802 if conf.underinfo then showtext 'f' ("ont: " ^ s);
803 Wsi.setcursor Wsi.CURSOR_TEXT
804 | Uunexpected s ->
805 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
806 Wsi.setcursor Wsi.CURSOR_INHERIT
807 | Ulaunch s ->
808 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
809 Wsi.setcursor Wsi.CURSOR_INHERIT
810 | Unamed s ->
811 if conf.underinfo then showtext 'n' ("amed: " ^ s);
812 Wsi.setcursor Wsi.CURSOR_INHERIT
813 | Uremote (filename, pageno) ->
814 if conf.underinfo then showtext 'r'
815 (Printf.sprintf "emote: %s (%d)" filename pageno);
816 Wsi.setcursor Wsi.CURSOR_INFO
819 let showlinktype under =
820 if conf.underinfo
821 then
822 match under with
823 | Unone -> ()
824 | Ulinkuri uri ->
825 showtext 'u' ("ri: " ^ uri)
826 | Ulinkgoto (page, _) ->
827 showtext 'p' ("age: " ^ string_of_int (page+1));
828 | Utext s ->
829 showtext 'f' ("ont: " ^ s);
830 | Uunexpected s ->
831 showtext 'u' ("nexpected: " ^ s);
832 | Ulaunch s ->
833 showtext 'l' ("aunch: " ^ s);
834 | Unamed s ->
835 showtext 'n' ("amed: " ^ s);
836 | Uremote (filename, pageno) ->
837 showtext 'r' (Printf.sprintf "emote: %s (%d)" filename pageno);
840 let addchar s c =
841 let b = Buffer.create (String.length s + 1) in
842 Buffer.add_string b s;
843 Buffer.add_char b c;
844 Buffer.contents b;
847 let colorspace_of_string s =
848 match String.lowercase s with
849 | "rgb" -> Rgb
850 | "bgr" -> Bgr
851 | "gray" -> Gray
852 | _ -> failwith "invalid colorspace"
855 let int_of_colorspace = function
856 | Rgb -> 0
857 | Bgr -> 1
858 | Gray -> 2
861 let colorspace_of_int = function
862 | 0 -> Rgb
863 | 1 -> Bgr
864 | 2 -> Gray
865 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
868 let colorspace_to_string = function
869 | Rgb -> "rgb"
870 | Bgr -> "bgr"
871 | Gray -> "gray"
874 let intentry_with_suffix text key =
875 let c =
876 if key >= 32 && key < 127
877 then Char.chr key
878 else '\000'
880 match Char.lowercase c with
881 | '0' .. '9' ->
882 let text = addchar text c in
883 TEcont text
885 | 'k' | 'm' | 'g' ->
886 let text = addchar text c in
887 TEcont text
889 | _ ->
890 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
891 TEcont text
894 let multicolumns_to_string (n, a, b) =
895 if a = 0 && b = 0
896 then Printf.sprintf "%d" n
897 else Printf.sprintf "%d,%d,%d" n a b;
900 let multicolumns_of_string s =
902 (int_of_string s, 0, 0)
903 with _ ->
904 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
907 let readcmd fd =
908 let s = "xxxx" in
909 let n = Unix.read fd s 0 4 in
910 if n != 4 then failwith "incomplete read(len)";
911 let len = 0
912 lor (Char.code s.[0] lsl 24)
913 lor (Char.code s.[1] lsl 16)
914 lor (Char.code s.[2] lsl 8)
915 lor (Char.code s.[3] lsl 0)
917 let s = String.create len in
918 let n = Unix.read fd s 0 len in
919 if n != len then failwith "incomplete read(data)";
923 let btod b = if b then 1 else 0;;
925 let wcmd fmt =
926 let b = Buffer.create 16 in
927 Buffer.add_string b "llll";
928 Printf.kbprintf
929 (fun b ->
930 let s = Buffer.contents b in
931 let n = String.length s in
932 let len = n - 4 in
933 (* dolog "wcmd %S" (String.sub s 4 len); *)
934 s.[0] <- Char.chr ((len lsr 24) land 0xff);
935 s.[1] <- Char.chr ((len lsr 16) land 0xff);
936 s.[2] <- Char.chr ((len lsr 8) land 0xff);
937 s.[3] <- Char.chr (len land 0xff);
938 let n' = Unix.write state.sw s 0 n in
939 if n' != n then failwith "write failed";
940 ) b fmt;
943 let calcips h =
944 if conf.presentation
945 then
946 let d = conf.winh - h in
947 max 0 ((d + 1) / 2)
948 else
949 conf.interpagespace
952 let calcheight () =
953 let rec f pn ph pi fh l =
954 match l with
955 | (n, _, h, _) :: rest ->
956 let ips = calcips h in
957 let fh =
958 if conf.presentation
959 then fh+ips
960 else (
961 if isbirdseye state.mode && pn = 0
962 then fh + ips
963 else fh
966 let fh = fh + ((n - pn) * (ph + pi)) in
967 f n h ips fh rest;
969 | [] ->
970 let inc =
971 if conf.presentation || (isbirdseye state.mode && pn = 0)
972 then 0
973 else -pi
975 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
976 max 0 fh
978 let fh = f 0 0 0 0 state.pdims in
982 let calcheight () =
983 match conf.columns with
984 | Csingle -> calcheight ()
985 | Cmulti ((c, _, _), b) ->
986 let rec loop y h n =
987 if n < 0
988 then loop y h (n+1)
989 else (
990 if n = Array.length b
991 then y + h
992 else
993 let (_, _, y', (_, _, h', _)) = b.(n) in
994 let y = min y y'
995 and h = max h h' in
996 loop y h (n+1)
999 loop max_int 0 (((Array.length b - 1) / c) * c)
1000 | Csplit (_, b) ->
1001 if Array.length b > 0
1002 then
1003 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1004 y + h
1005 else 0
1008 let getpageyh pageno =
1009 let rec f pn ph pi y l =
1010 match l with
1011 | (n, _, h, _) :: rest ->
1012 let ips = calcips h in
1013 if n >= pageno
1014 then
1015 let h = if n = pageno then h else ph in
1016 if conf.presentation && n = pageno
1017 then
1018 y + (pageno - pn) * (ph + pi) + pi, h
1019 else
1020 y + (pageno - pn) * (ph + pi), h
1021 else
1022 let y = y + (if conf.presentation then pi else 0) in
1023 let y = y + (n - pn) * (ph + pi) in
1024 f n h ips y rest
1026 | [] ->
1027 y + (pageno - pn) * (ph + pi), ph
1029 f 0 0 0 0 state.pdims
1032 let getpageyh pageno =
1033 match conf.columns with
1034 | Csingle -> getpageyh pageno
1035 | Cmulti (_, b) ->
1036 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1037 y, h
1038 | Csplit (c, b) ->
1039 let n = pageno*c in
1040 let (_, _, y, (_, _, h, _)) = b.(n) in
1041 y, h
1044 let getpagedim pageno =
1045 let rec f ppdim l =
1046 match l with
1047 | (n, _, _, _) as pdim :: rest ->
1048 if n >= pageno
1049 then (if n = pageno then pdim else ppdim)
1050 else f pdim rest
1052 | [] -> ppdim
1054 f (-1, -1, -1, -1) state.pdims
1057 let getpagey pageno = fst (getpageyh pageno);;
1059 let nogeomcmds cmds =
1060 match cmds with
1061 | s, [] -> String.length s = 0
1062 | _ -> false
1065 let layout1 y sh =
1066 let sh = sh - state.hscrollh in
1067 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1068 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1069 match pdims with
1070 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1071 let ips = calcips h in
1072 let yinc =
1073 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1074 then ips
1075 else 0
1077 (w, h, ips, xoff), rest, pdimno + 1, yinc
1078 | _ ->
1079 prev, pdims, pdimno, 0
1081 let dy = dy + yinc in
1082 let py = py + yinc in
1083 if pageno = state.pagecount || dy >= sh
1084 then
1085 accu
1086 else
1087 let vy = y + dy in
1088 if py + h <= vy - yinc
1089 then
1090 let py = py + h + ips in
1091 let dy = max 0 (py - y) in
1092 f ~pageno:(pageno+1)
1093 ~pdimno
1094 ~prev:curr
1097 ~pdims:rest
1098 ~accu
1099 else
1100 let pagey = vy - py in
1101 let pagevh = h - pagey in
1102 let pagevh = min (sh - dy) pagevh in
1103 let off = if yinc > 0 then py - vy else 0 in
1104 let py = py + h + ips in
1105 let pagex, dx =
1106 let xoff = xoff +
1107 if state.w < conf.winw - state.scrollw
1108 then (conf.winw - state.scrollw - state.w) / 2
1109 else 0
1111 let dispx = xoff + state.x in
1112 if dispx < 0
1113 then (-dispx, 0)
1114 else (0, dispx)
1116 let pagevw =
1117 let lw = w - pagex in
1118 min lw (conf.winw - state.scrollw)
1120 let e =
1121 { pageno = pageno
1122 ; pagedimno = pdimno
1123 ; pagew = w
1124 ; pageh = h
1125 ; pagex = pagex
1126 ; pagey = pagey + off
1127 ; pagevw = pagevw
1128 ; pagevh = pagevh - off
1129 ; pagedispx = dx
1130 ; pagedispy = dy + off
1133 let accu = e :: accu in
1134 f ~pageno:(pageno+1)
1135 ~pdimno
1136 ~prev:curr
1138 ~dy:(dy+pagevh+ips)
1139 ~pdims:rest
1140 ~accu
1142 let accu =
1144 ~pageno:0
1145 ~pdimno:~-1
1146 ~prev:(0,0,0,0)
1147 ~py:0
1148 ~dy:0
1149 ~pdims:state.pdims
1150 ~accu:[]
1152 List.rev accu
1155 let layoutN ((columns, coverA, coverB), b) y sh =
1156 let sh = sh - state.hscrollh in
1157 let rec fold accu n =
1158 if n = Array.length b
1159 then accu
1160 else
1161 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1162 if (vy - y) > sh &&
1163 (n = coverA - 1
1164 || n = state.pagecount - coverB
1165 || (n - coverA) mod columns = columns - 1)
1166 then accu
1167 else
1168 let accu =
1169 if vy + h > y
1170 then
1171 let pagey = max 0 (y - vy) in
1172 let pagedispy = if pagey > 0 then 0 else vy - y in
1173 let pagedispx, pagex =
1174 let pdx =
1175 if n = coverA - 1 || n = state.pagecount - coverB
1176 then state.x + (conf.winw - state.scrollw - w) / 2
1177 else dx + xoff + state.x
1179 if pdx < 0
1180 then 0, -pdx
1181 else pdx, 0
1183 let pagevw =
1184 let vw = conf.winw - state.scrollw - pagedispx in
1185 let pw = w - pagex in
1186 min vw pw
1188 let pagevh = min (h - pagey) (sh - pagedispy) in
1189 if pagevw > 0 && pagevh > 0
1190 then
1191 let e =
1192 { pageno = n
1193 ; pagedimno = pdimno
1194 ; pagew = w
1195 ; pageh = h
1196 ; pagex = pagex
1197 ; pagey = pagey
1198 ; pagevw = pagevw
1199 ; pagevh = pagevh
1200 ; pagedispx = pagedispx
1201 ; pagedispy = pagedispy
1204 e :: accu
1205 else
1206 accu
1207 else
1208 accu
1210 fold accu (n+1)
1212 List.rev (fold [] 0)
1215 let layoutS (columns, b) y sh =
1216 let sh = sh - state.hscrollh in
1217 let rec fold accu n =
1218 if n = Array.length b
1219 then accu
1220 else
1221 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1222 if (vy - y) > sh
1223 then accu
1224 else
1225 let accu =
1226 if vy + pageh > y
1227 then
1228 let x = xoff + state.x in
1229 let pagey = max 0 (y - vy) in
1230 let pagedispy = if pagey > 0 then 0 else vy - y in
1231 let pagedispx, pagex =
1232 if px = 0
1233 then (
1234 if x < 0
1235 then 0, -x
1236 else x, 0
1238 else (
1239 let px = px - x in
1240 if px < 0
1241 then -px, 0
1242 else 0, px
1245 let pagevw =
1246 let vw = conf.winw - pagedispx - state.scrollw in
1247 let pw = pagew - pagex in
1248 min vw pw
1250 let pagevw = min pagevw (pagew/columns) in
1251 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1252 if pagevw > 0 && pagevh > 0
1253 then
1254 let e =
1255 { pageno = n/columns
1256 ; pagedimno = pdimno
1257 ; pagew = pagew
1258 ; pageh = pageh
1259 ; pagex = pagex
1260 ; pagey = pagey
1261 ; pagevw = pagevw
1262 ; pagevh = pagevh
1263 ; pagedispx = pagedispx
1264 ; pagedispy = pagedispy
1267 e :: accu
1268 else
1269 accu
1270 else
1271 accu
1273 fold accu (n+1)
1275 List.rev (fold [] 0)
1278 let layout y sh =
1279 if nogeomcmds state.geomcmds
1280 then
1281 match conf.columns with
1282 | Csingle -> layout1 y sh
1283 | Cmulti c -> layoutN c y sh
1284 | Csplit s -> layoutS s y sh
1285 else []
1288 let clamp incr =
1289 let y = state.y + incr in
1290 let y = max 0 y in
1291 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1295 let itertiles l f =
1296 let tilex = l.pagex mod conf.tilew in
1297 let tiley = l.pagey mod conf.tileh in
1299 let col = l.pagex / conf.tilew in
1300 let row = l.pagey / conf.tileh in
1302 let rec rowloop row y0 dispy h =
1303 if h = 0
1304 then ()
1305 else (
1306 let dh = conf.tileh - y0 in
1307 let dh = min h dh in
1308 let rec colloop col x0 dispx w =
1309 if w = 0
1310 then ()
1311 else (
1312 let dw = conf.tilew - x0 in
1313 let dw = min w dw in
1315 f col row dispx dispy x0 y0 dw dh;
1316 colloop (col+1) 0 (dispx+dw) (w-dw)
1319 colloop col tilex l.pagedispx l.pagevw;
1320 rowloop (row+1) 0 (dispy+dh) (h-dh)
1323 if l.pagevw > 0 && l.pagevh > 0
1324 then rowloop row tiley l.pagedispy l.pagevh;
1327 let gettileopaque l col row =
1328 let key =
1329 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1331 try Some (Hashtbl.find state.tilemap key)
1332 with Not_found -> None
1335 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1336 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1337 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1340 let drawtiles l color =
1341 GlDraw.color color;
1342 let f col row x y tilex tiley w h =
1343 match gettileopaque l col row with
1344 | Some (opaque, _, t) ->
1345 let params = x, y, w, h, tilex, tiley in
1346 if conf.invert
1347 then (
1348 Gl.enable `blend;
1349 GlFunc.blend_func `zero `one_minus_src_color;
1351 drawtile params opaque;
1352 if conf.invert
1353 then Gl.disable `blend;
1354 if conf.debug
1355 then (
1356 let s = Printf.sprintf
1357 "%d[%d,%d] %f sec"
1358 l.pageno col row t
1360 let w = measurestr fstate.fontsize s in
1361 GlMisc.push_attrib [`current];
1362 GlDraw.color (0.0, 0.0, 0.0);
1363 GlDraw.rect
1364 (float (x-2), float (y-2))
1365 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1366 GlDraw.color (1.0, 1.0, 1.0);
1367 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1368 GlMisc.pop_attrib ();
1371 | _ ->
1372 let w =
1373 let lw = conf.winw - state.scrollw - x in
1374 min lw w
1375 and h =
1376 let lh = conf.winh - y in
1377 min lh h
1379 Gl.enable `texture_2d;
1380 begin match state.texid with
1381 | Some id ->
1382 GlTex.bind_texture `texture_2d id;
1383 let x0 = float x
1384 and y0 = float y
1385 and x1 = float (x+w)
1386 and y1 = float (y+h) in
1388 let tw = float w /. 64.0
1389 and th = float h /. 64.0 in
1390 let tx0 = float tilex /. 64.0
1391 and ty0 = float tiley /. 64.0 in
1392 let tx1 = tx0 +. tw
1393 and ty1 = ty0 +. th in
1394 GlDraw.begins `quads;
1395 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1396 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1397 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1398 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1399 GlDraw.ends ();
1401 Gl.disable `texture_2d;
1402 | None ->
1403 GlDraw.color (1.0, 1.0, 1.0);
1404 GlDraw.rect
1405 (float x, float y)
1406 (float (x+w), float (y+h));
1407 end;
1408 if w > 128 && h > fstate.fontsize + 10
1409 then (
1410 GlDraw.color (0.0, 0.0, 0.0);
1411 let c, r =
1412 if conf.verbose
1413 then (col*conf.tilew, row*conf.tileh)
1414 else col, row
1416 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1418 GlDraw.color color;
1420 itertiles l f
1423 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1425 let tilevisible1 l x y =
1426 let ax0 = l.pagex
1427 and ax1 = l.pagex + l.pagevw
1428 and ay0 = l.pagey
1429 and ay1 = l.pagey + l.pagevh in
1431 let bx0 = x
1432 and by0 = y in
1433 let bx1 = min (bx0 + conf.tilew) l.pagew
1434 and by1 = min (by0 + conf.tileh) l.pageh in
1436 let rx0 = max ax0 bx0
1437 and ry0 = max ay0 by0
1438 and rx1 = min ax1 bx1
1439 and ry1 = min ay1 by1 in
1441 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1442 nonemptyintersection
1445 let tilevisible layout n x y =
1446 let rec findpageinlayout m = function
1447 | l :: rest when l.pageno = n ->
1448 tilevisible1 l x y || (
1449 match conf.columns with
1450 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1451 | _ -> false
1453 | _ :: rest -> findpageinlayout 0 rest
1454 | [] -> false
1456 findpageinlayout 0 layout;
1459 let tileready l x y =
1460 tilevisible1 l x y &&
1461 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1464 let tilepage n p layout =
1465 let rec loop = function
1466 | l :: rest ->
1467 if l.pageno = n
1468 then
1469 let f col row _ _ _ _ _ _ =
1470 if state.currently = Idle
1471 then
1472 match gettileopaque l col row with
1473 | Some _ -> ()
1474 | None ->
1475 let x = col*conf.tilew
1476 and y = row*conf.tileh in
1477 let w =
1478 let w = l.pagew - x in
1479 min w conf.tilew
1481 let h =
1482 let h = l.pageh - y in
1483 min h conf.tileh
1485 wcmd "tile %s %d %d %d %d" p x y w h;
1486 state.currently <-
1487 Tiling (
1488 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1489 conf.tilew, conf.tileh
1492 itertiles l f;
1493 else
1494 loop rest
1496 | [] -> ()
1498 if nogeomcmds state.geomcmds
1499 then loop layout;
1502 let preloadlayout visiblepages =
1503 let presentation = conf.presentation in
1504 let interpagespace = conf.interpagespace in
1505 let maxy = state.maxy in
1506 conf.presentation <- false;
1507 conf.interpagespace <- 0;
1508 state.maxy <- calcheight ();
1509 let y =
1510 match visiblepages with
1511 | [] -> 0
1512 | l :: _ -> getpagey l.pageno + l.pagey
1514 let y = if y < conf.winh then 0 else y - conf.winh in
1515 let h = state.y - y + conf.winh*3 in
1516 let pages = layout y h in
1517 conf.presentation <- presentation;
1518 conf.interpagespace <- interpagespace;
1519 state.maxy <- maxy;
1520 pages;
1523 let load pages =
1524 let rec loop pages =
1525 if state.currently != Idle
1526 then ()
1527 else
1528 match pages with
1529 | l :: rest ->
1530 begin match getopaque l.pageno with
1531 | None ->
1532 wcmd "page %d %d" l.pageno l.pagedimno;
1533 state.currently <- Loading (l, state.gen);
1534 | Some opaque ->
1535 tilepage l.pageno opaque pages;
1536 loop rest
1537 end;
1538 | _ -> ()
1540 if nogeomcmds state.geomcmds
1541 then loop pages
1544 let preload pages =
1545 load pages;
1546 if conf.preload && state.currently = Idle
1547 then load (preloadlayout pages);
1550 let layoutready layout =
1551 let rec fold all ls =
1552 all && match ls with
1553 | l :: rest ->
1554 let seen = ref false in
1555 let allvisible = ref true in
1556 let foo col row _ _ _ _ _ _ =
1557 seen := true;
1558 allvisible := !allvisible &&
1559 begin match gettileopaque l col row with
1560 | Some _ -> true
1561 | None -> false
1564 itertiles l foo;
1565 fold (!seen && !allvisible) rest
1566 | [] -> true
1568 let alltilesvisible = fold true layout in
1569 alltilesvisible;
1572 let gotoy y =
1573 let y = bound y 0 state.maxy in
1574 let y, layout, proceed =
1575 match conf.maxwait with
1576 | Some time when state.ghyll == noghyll ->
1577 begin match state.throttle with
1578 | None ->
1579 let layout = layout y conf.winh in
1580 let ready = layoutready layout in
1581 if not ready
1582 then (
1583 load layout;
1584 state.throttle <- Some (layout, y, now ());
1586 else G.postRedisplay "gotoy showall (None)";
1587 y, layout, ready
1588 | Some (_, _, started) ->
1589 let dt = now () -. started in
1590 if dt > time
1591 then (
1592 state.throttle <- None;
1593 let layout = layout y conf.winh in
1594 load layout;
1595 G.postRedisplay "maxwait";
1596 y, layout, true
1598 else -1, [], false
1601 | _ ->
1602 let layout = layout y conf.winh in
1603 if true || layoutready layout
1604 then G.postRedisplay "gotoy ready";
1605 y, layout, true
1607 if proceed
1608 then (
1609 state.y <- y;
1610 state.layout <- layout;
1611 begin match state.mode with
1612 | LinkNav (Ltexact (pageno, linkno)) ->
1613 let rec loop = function
1614 | [] ->
1615 state.mode <- LinkNav (Ltgendir 0)
1616 | l :: _ when l.pageno = pageno ->
1617 begin match getopaque pageno with
1618 | None ->
1619 state.mode <- LinkNav (Ltgendir 0)
1620 | Some opaque ->
1621 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1622 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1623 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1624 then state.mode <- LinkNav (Ltgendir 0)
1626 | _ :: rest -> loop rest
1628 loop layout
1629 | _ -> ()
1630 end;
1631 begin match state.mode with
1632 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1633 if not (pagevisible layout pageno)
1634 then (
1635 match state.layout with
1636 | [] -> ()
1637 | l :: _ ->
1638 state.mode <- Birdseye (
1639 conf, leftx, l.pageno, hooverpageno, anchor
1642 | LinkNav (Ltgendir dir as lt) ->
1643 let linknav =
1644 let rec loop = function
1645 | [] -> lt
1646 | l :: rest ->
1647 match getopaque l.pageno with
1648 | None -> loop rest
1649 | Some opaque ->
1650 let link =
1651 let ld =
1652 if dir = 0
1653 then LDfirstvisible (l.pagex, l.pagey, dir)
1654 else (
1655 if dir > 0 then LDfirst else LDlast
1658 findlink opaque ld
1660 match link with
1661 | Lnotfound -> loop rest
1662 | Lfound n ->
1663 showlinktype (getlink opaque n);
1664 Ltexact (l.pageno, n)
1666 loop state.layout
1668 state.mode <- LinkNav linknav
1669 | _ -> ()
1670 end;
1671 preload layout;
1673 state.ghyll <- noghyll;
1674 if conf.updatecurs
1675 then (
1676 let mx, my = state.mpos in
1677 updateunder mx my;
1681 let conttiling pageno opaque =
1682 tilepage pageno opaque
1683 (if conf.preload then preloadlayout state.layout else state.layout)
1686 let gotoy_and_clear_text y =
1687 if not conf.verbose then state.text <- "";
1688 gotoy y;
1691 let getanchor () =
1692 match state.layout with
1693 | [] -> emptyanchor
1694 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1697 let getanchory (n, top) =
1698 let y, h = getpageyh n in
1699 y + (truncate (top *. float h));
1702 let gotoanchor anchor =
1703 gotoy (getanchory anchor);
1706 let addnav () =
1707 cbput state.hists.nav (getanchor ());
1710 let getnav dir =
1711 let anchor = cbgetc state.hists.nav dir in
1712 getanchory anchor;
1715 let gotoghyll y =
1716 let rec scroll f n a b =
1717 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1718 let snake f a b =
1719 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1720 if f < a
1721 then s (float f /. float a)
1722 else (
1723 if f > b
1724 then 1.0 -. s ((float (f-b) /. float (n-b)))
1725 else 1.0
1728 snake f a b
1729 and summa f n a b =
1730 (* courtesy:
1731 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1732 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1733 let iv1 = iv f in
1734 let ins = float a *. iv1
1735 and outs = float (n-b) *. iv1 in
1736 let ones = b - a in
1737 ins +. outs +. float ones
1739 let rec set (_N, _A, _B) y sy =
1740 let sum = summa 1.0 _N _A _B in
1741 let dy = float (y - sy) in
1742 state.ghyll <- (
1743 let rec gf n y1 o =
1744 if n >= _N
1745 then state.ghyll <- noghyll
1746 else
1747 let go n =
1748 let s = scroll n _N _A _B in
1749 let y1 = y1 +. ((s *. dy) /. sum) in
1750 gotoy_and_clear_text (truncate y1);
1751 state.ghyll <- gf (n+1) y1;
1753 match o with
1754 | None -> go n
1755 | Some y' -> set (_N/2, 0, 0) y' state.y
1757 gf 0 (float state.y)
1760 match conf.ghyllscroll with
1761 | None ->
1762 gotoy_and_clear_text y
1763 | Some nab ->
1764 if state.ghyll == noghyll
1765 then set nab y state.y
1766 else state.ghyll (Some y)
1769 let gotopage n top =
1770 let y, h = getpageyh n in
1771 let y = y + (truncate (top *. float h)) in
1772 gotoghyll y
1775 let gotopage1 n top =
1776 let y = getpagey n in
1777 let y = y + top in
1778 gotoghyll y
1781 let invalidate s f =
1782 state.layout <- [];
1783 state.pdims <- [];
1784 state.rects <- [];
1785 state.rects1 <- [];
1786 match state.geomcmds with
1787 | ps, [] when String.length ps = 0 ->
1788 f ();
1789 state.geomcmds <- s, [];
1791 | ps, [] ->
1792 state.geomcmds <- ps, [s, f];
1794 | ps, (s', _) :: rest when s' = s ->
1795 state.geomcmds <- ps, ((s, f) :: rest);
1797 | ps, cmds ->
1798 state.geomcmds <- ps, ((s, f) :: cmds);
1801 let opendoc path password =
1802 state.path <- path;
1803 state.password <- password;
1804 state.gen <- state.gen + 1;
1805 state.docinfo <- [];
1807 setaalevel conf.aalevel;
1808 Wsi.settitle ("llpp " ^ Filename.basename path);
1809 wcmd "open %s\000%s\000" path password;
1810 invalidate "reqlayout"
1811 (fun () ->
1812 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1815 let scalecolor c =
1816 let c = c *. conf.colorscale in
1817 (c, c, c);
1820 let scalecolor2 (r, g, b) =
1821 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1824 let represent () =
1825 let docolumns = function
1826 | Csingle -> ()
1828 | Cmulti ((columns, coverA, coverB), _) ->
1829 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1830 let rec loop pageno pdimno pdim x y rowh pdims =
1831 let rec fixrow m = if m = pageno then () else
1832 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1833 if h < rowh
1834 then (
1835 let y = y + (rowh - h) / 2 in
1836 a.(m) <- (pdimno, x, y, pdim);
1838 fixrow (m+1)
1840 if pageno = state.pagecount
1841 then fixrow (((pageno - 1) / columns) * columns)
1842 else
1843 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1844 match pdims with
1845 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1846 pdimno+1, pdim, rest
1847 | _ ->
1848 pdimno, pdim, pdims
1850 let x, y, rowh' =
1851 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1852 then (
1853 (conf.winw - state.scrollw - w) / 2,
1854 y + rowh + conf.interpagespace, h
1856 else (
1857 if (pageno - coverA) mod columns = 0
1858 then 0, y + rowh + conf.interpagespace, h
1859 else x, y, max rowh h
1862 if pageno > 1 && (pageno - coverA) mod columns = 0
1863 then fixrow (pageno - columns);
1864 a.(pageno) <- (pdimno, x, y, pdim);
1865 let x = x + w + xoff*2 + conf.interpagespace in
1866 loop (pageno+1) pdimno pdim x y rowh' pdims
1868 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1869 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1871 | Csplit (c, _) ->
1872 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1873 let rec loop pageno pdimno pdim y pdims =
1874 if pageno = state.pagecount
1875 then ()
1876 else
1877 let pdimno, ((_, w, h, _) as pdim), pdims =
1878 match pdims with
1879 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1880 pdimno+1, pdim, rest
1881 | _ ->
1882 pdimno, pdim, pdims
1884 let cw = w / c in
1885 let rec loop1 n x y =
1886 if n = c then y else (
1887 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1888 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1891 let y = loop1 0 0 y in
1892 loop (pageno+1) pdimno pdim y pdims
1894 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1895 conf.columns <- Csplit (c, a);
1897 docolumns conf.columns;
1898 state.maxy <- calcheight ();
1899 state.hscrollh <-
1900 if state.w <= conf.winw - state.scrollw
1901 then 0
1902 else state.scrollw
1904 match state.mode with
1905 | Birdseye (_, _, pageno, _, _) ->
1906 let y, h = getpageyh pageno in
1907 let top = (conf.winh - h) / 2 in
1908 gotoy (max 0 (y - top))
1909 | _ -> gotoanchor state.anchor
1912 let reshape w h =
1913 GlDraw.viewport 0 0 w h;
1914 let firsttime = state.geomcmds == firstgeomcmds in
1915 if not firsttime && nogeomcmds state.geomcmds
1916 then state.anchor <- getanchor ();
1918 conf.winw <- w;
1919 let w = truncate (float w *. conf.zoom) - state.scrollw in
1920 let w = max w 2 in
1921 conf.winh <- h;
1922 setfontsize fstate.fontsize;
1923 GlMat.mode `modelview;
1924 GlMat.load_identity ();
1926 GlMat.mode `projection;
1927 GlMat.load_identity ();
1928 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1929 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1930 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1932 let relx =
1933 if conf.zoom <= 1.0
1934 then 0.0
1935 else float state.x /. float state.w
1937 invalidate "geometry"
1938 (fun () ->
1939 state.w <- w;
1940 if not firsttime
1941 then state.x <- truncate (relx *. float w);
1942 let w =
1943 match conf.columns with
1944 | Csingle -> w
1945 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1946 | Csplit (c, _) -> w * c
1948 wcmd "geometry %d %d" w h);
1951 let enttext () =
1952 let len = String.length state.text in
1953 let drawstring s =
1954 let hscrollh =
1955 match state.mode with
1956 | Textentry _
1957 | View -> state.hscrollh
1958 | _ -> 0
1960 let rect x w =
1961 GlDraw.rect
1962 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1963 (x+.w, float (conf.winh - hscrollh))
1966 let w = float (conf.winw - state.scrollw - 1) in
1967 if state.progress >= 0.0 && state.progress < 1.0
1968 then (
1969 GlDraw.color (0.3, 0.3, 0.3);
1970 let w1 = w *. state.progress in
1971 rect 0.0 w1;
1972 GlDraw.color (0.0, 0.0, 0.0);
1973 rect w1 (w-.w1)
1975 else (
1976 GlDraw.color (0.0, 0.0, 0.0);
1977 rect 0.0 w;
1980 GlDraw.color (1.0, 1.0, 1.0);
1981 drawstring fstate.fontsize
1982 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1984 let s =
1985 match state.mode with
1986 | Textentry ((prefix, text, _, _, _), _) ->
1987 let s =
1988 if len > 0
1989 then
1990 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1991 else
1992 Printf.sprintf "%s%s_" prefix text
1996 | _ -> state.text
1998 let s =
1999 if state.newerrmsgs
2000 then (
2001 if not (istextentry state.mode)
2002 then
2003 let s1 = "(press 'e' to review error messasges)" in
2004 if String.length s > 0 then s ^ " " ^ s1 else s1
2005 else s
2007 else s
2009 if String.length s > 0
2010 then drawstring s
2013 let gctiles () =
2014 let len = Queue.length state.tilelru in
2015 let rec loop qpos =
2016 if state.memused <= conf.memlimit
2017 then ()
2018 else (
2019 if qpos < len
2020 then
2021 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2022 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2023 let (_, pw, ph, _) = getpagedim n in
2025 gen = state.gen
2026 && colorspace = conf.colorspace
2027 && angle = conf.angle
2028 && pagew = pw
2029 && pageh = ph
2030 && (
2031 let layout =
2032 match state.throttle with
2033 | None ->
2034 if conf.preload
2035 then preloadlayout state.layout
2036 else state.layout
2037 | Some (layout, _, _) ->
2038 layout
2040 let x = col*conf.tilew
2041 and y = row*conf.tileh in
2042 tilevisible layout n x y
2044 then Queue.push lruitem state.tilelru
2045 else (
2046 wcmd "freetile %s" p;
2047 state.memused <- state.memused - s;
2048 state.uioh#infochanged Memused;
2049 Hashtbl.remove state.tilemap k;
2051 loop (qpos+1)
2054 loop 0
2057 let flushtiles () =
2058 Queue.iter (fun (k, p, s) ->
2059 wcmd "freetile %s" p;
2060 state.memused <- state.memused - s;
2061 state.uioh#infochanged Memused;
2062 Hashtbl.remove state.tilemap k;
2063 ) state.tilelru;
2064 Queue.clear state.tilelru;
2065 load state.layout;
2068 let logcurrently = function
2069 | Idle -> dolog "Idle"
2070 | Loading (l, gen) ->
2071 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2072 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2073 dolog
2074 "Tiling %d[%d,%d] page=%s cs=%s angle"
2075 l.pageno col row pageopaque
2076 (colorspace_to_string colorspace)
2078 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2079 angle gen conf.angle state.gen
2080 tilew tileh
2081 conf.tilew conf.tileh
2083 | Outlining _ ->
2084 dolog "outlining"
2087 let act cmds =
2088 (* dolog "%S" cmds; *)
2089 let op, args =
2090 let spacepos =
2091 try String.index cmds ' '
2092 with Not_found -> -1
2094 if spacepos = -1
2095 then cmds, ""
2096 else
2097 let l = String.length cmds in
2098 let op = String.sub cmds 0 spacepos in
2099 op, begin
2100 if l - spacepos < 2 then ""
2101 else String.sub cmds (spacepos+1) (l-spacepos-1)
2104 match op with
2105 | "clear" ->
2106 state.uioh#infochanged Pdim;
2107 state.pdims <- [];
2109 | "clearrects" ->
2110 state.rects <- state.rects1;
2111 G.postRedisplay "clearrects";
2113 | "continue" ->
2114 let n =
2115 try Scanf.sscanf args "%u" (fun n -> n)
2116 with exn ->
2117 dolog "error processing 'continue' %S: %s"
2118 cmds (Printexc.to_string exn);
2119 exit 1;
2121 state.pagecount <- n;
2122 begin match state.currently with
2123 | Outlining l ->
2124 state.currently <- Idle;
2125 state.outlines <- Array.of_list (List.rev l)
2126 | _ -> ()
2127 end;
2129 let cur, cmds = state.geomcmds in
2130 if String.length cur = 0
2131 then failwith "umpossible";
2133 begin match List.rev cmds with
2134 | [] ->
2135 state.geomcmds <- "", [];
2136 represent ();
2137 | (s, f) :: rest ->
2138 f ();
2139 state.geomcmds <- s, List.rev rest;
2140 end;
2141 if conf.maxwait = None
2142 then G.postRedisplay "continue";
2144 | "title" ->
2145 Wsi.settitle args
2147 | "msg" ->
2148 showtext ' ' args
2150 | "vmsg" ->
2151 if conf.verbose
2152 then showtext ' ' args
2154 | "progress" ->
2155 let progress, text =
2157 Scanf.sscanf args "%f %n"
2158 (fun f pos ->
2159 f, String.sub args pos (String.length args - pos))
2160 with exn ->
2161 dolog "error processing 'progress' %S: %s"
2162 cmds (Printexc.to_string exn);
2163 exit 1;
2165 state.text <- text;
2166 state.progress <- progress;
2167 G.postRedisplay "progress"
2169 | "firstmatch" ->
2170 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2172 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2173 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2174 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2175 with exn ->
2176 dolog "error processing 'firstmatch' %S: %s"
2177 cmds (Printexc.to_string exn);
2178 exit 1;
2180 let y = (getpagey pageno) + truncate y0 in
2181 addnav ();
2182 gotoy y;
2183 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2185 | "match" ->
2186 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2188 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2189 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2190 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2191 with exn ->
2192 dolog "error processing 'match' %S: %s"
2193 cmds (Printexc.to_string exn);
2194 exit 1;
2196 state.rects1 <-
2197 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2199 | "page" ->
2200 let pageopaque, t =
2202 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2203 with exn ->
2204 dolog "error processing 'page' %S: %s"
2205 cmds (Printexc.to_string exn);
2206 exit 1;
2208 begin match state.currently with
2209 | Loading (l, gen) ->
2210 vlog "page %d took %f sec" l.pageno t;
2211 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2212 begin match state.throttle with
2213 | None ->
2214 let preloadedpages =
2215 if conf.preload
2216 then preloadlayout state.layout
2217 else state.layout
2219 let evict () =
2220 let module IntSet =
2221 Set.Make (struct type t = int let compare = (-) end) in
2222 let set =
2223 List.fold_left (fun s l -> IntSet.add l.pageno s)
2224 IntSet.empty preloadedpages
2226 let evictedpages =
2227 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2228 if not (IntSet.mem pageno set)
2229 then (
2230 wcmd "freepage %s" opaque;
2231 key :: accu
2233 else accu
2234 ) state.pagemap []
2236 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2238 evict ();
2239 state.currently <- Idle;
2240 if gen = state.gen
2241 then (
2242 tilepage l.pageno pageopaque state.layout;
2243 load state.layout;
2244 load preloadedpages;
2245 if pagevisible state.layout l.pageno
2246 && layoutready state.layout
2247 then G.postRedisplay "page";
2250 | Some (layout, _, _) ->
2251 state.currently <- Idle;
2252 tilepage l.pageno pageopaque layout;
2253 load state.layout
2254 end;
2256 | _ ->
2257 dolog "Inconsistent loading state";
2258 logcurrently state.currently;
2259 exit 1
2262 | "tile" ->
2263 let (x, y, opaque, size, t) =
2265 Scanf.sscanf args "%u %u %s %u %f"
2266 (fun x y p size t -> (x, y, p, size, t))
2267 with exn ->
2268 dolog "error processing 'tile' %S: %s"
2269 cmds (Printexc.to_string exn);
2270 exit 1;
2272 begin match state.currently with
2273 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2274 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2276 if tilew != conf.tilew || tileh != conf.tileh
2277 then (
2278 wcmd "freetile %s" opaque;
2279 state.currently <- Idle;
2280 load state.layout;
2282 else (
2283 puttileopaque l col row gen cs angle opaque size t;
2284 state.memused <- state.memused + size;
2285 state.uioh#infochanged Memused;
2286 gctiles ();
2287 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2288 opaque, size) state.tilelru;
2290 let layout =
2291 match state.throttle with
2292 | None -> state.layout
2293 | Some (layout, _, _) -> layout
2296 state.currently <- Idle;
2297 if gen = state.gen
2298 && conf.colorspace = cs
2299 && conf.angle = angle
2300 && tilevisible layout l.pageno x y
2301 then conttiling l.pageno pageopaque;
2303 begin match state.throttle with
2304 | None ->
2305 preload state.layout;
2306 if gen = state.gen
2307 && conf.colorspace = cs
2308 && conf.angle = angle
2309 && tilevisible state.layout l.pageno x y
2310 then G.postRedisplay "tile nothrottle";
2312 | Some (layout, y, _) ->
2313 let ready = layoutready layout in
2314 if ready
2315 then (
2316 state.y <- y;
2317 state.layout <- layout;
2318 state.throttle <- None;
2319 G.postRedisplay "throttle";
2321 else load layout;
2322 end;
2325 | _ ->
2326 dolog "Inconsistent tiling state";
2327 logcurrently state.currently;
2328 exit 1
2331 | "pdim" ->
2332 let pdim =
2334 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2335 with exn ->
2336 dolog "error processing 'pdim' %S: %s"
2337 cmds (Printexc.to_string exn);
2338 exit 1;
2340 state.uioh#infochanged Pdim;
2341 state.pdims <- pdim :: state.pdims
2343 | "o" ->
2344 let (l, n, t, h, pos) =
2346 Scanf.sscanf args "%u %u %d %u %n"
2347 (fun l n t h pos -> l, n, t, h, pos)
2348 with exn ->
2349 dolog "error processing 'o' %S: %s"
2350 cmds (Printexc.to_string exn);
2351 exit 1;
2353 let s = String.sub args pos (String.length args - pos) in
2354 let outline = (s, l, (n, float t /. float h)) in
2355 begin match state.currently with
2356 | Outlining outlines ->
2357 state.currently <- Outlining (outline :: outlines)
2358 | Idle ->
2359 state.currently <- Outlining [outline]
2360 | currently ->
2361 dolog "invalid outlining state";
2362 logcurrently currently
2365 | "info" ->
2366 state.docinfo <- (1, args) :: state.docinfo
2368 | "infoend" ->
2369 state.uioh#infochanged Docinfo;
2370 state.docinfo <- List.rev state.docinfo
2372 | _ ->
2373 dolog "unknown cmd `%S'" cmds
2376 let onhist cb =
2377 let rc = cb.rc in
2378 let action = function
2379 | HCprev -> cbget cb ~-1
2380 | HCnext -> cbget cb 1
2381 | HCfirst -> cbget cb ~-(cb.rc)
2382 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2383 and cancel () = cb.rc <- rc
2384 in (action, cancel)
2387 let search pattern forward =
2388 if String.length pattern > 0
2389 then
2390 let pn, py =
2391 match state.layout with
2392 | [] -> 0, 0
2393 | l :: _ ->
2394 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2396 wcmd "search %d %d %d %d,%s\000"
2397 (btod conf.icase) pn py (btod forward) pattern;
2400 let intentry text key =
2401 let c =
2402 if key >= 32 && key < 127
2403 then Char.chr key
2404 else '\000'
2406 match c with
2407 | '0' .. '9' ->
2408 let text = addchar text c in
2409 TEcont text
2411 | _ ->
2412 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2413 TEcont text
2416 let textentry text key =
2417 if key land 0xff00 = 0xff00
2418 then TEcont text
2419 else TEcont (text ^ Wsi.toutf8 key)
2422 let reqlayout angle proportional =
2423 match state.throttle with
2424 | None ->
2425 if nogeomcmds state.geomcmds
2426 then state.anchor <- getanchor ();
2427 conf.angle <- angle mod 360;
2428 if conf.angle != 0
2429 then (
2430 match state.mode with
2431 | LinkNav _ -> state.mode <- View
2432 | _ -> ()
2434 conf.proportional <- proportional;
2435 invalidate "reqlayout"
2436 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2437 | _ -> ()
2440 let settrim trimmargins trimfuzz =
2441 if nogeomcmds state.geomcmds
2442 then state.anchor <- getanchor ();
2443 conf.trimmargins <- trimmargins;
2444 conf.trimfuzz <- trimfuzz;
2445 let x0, y0, x1, y1 = trimfuzz in
2446 invalidate "settrim"
2447 (fun () ->
2448 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2449 Hashtbl.iter (fun _ opaque ->
2450 wcmd "freepage %s" opaque;
2451 ) state.pagemap;
2452 Hashtbl.clear state.pagemap;
2455 let setzoom zoom =
2456 match state.throttle with
2457 | None ->
2458 let zoom = max 0.01 zoom in
2459 if zoom <> conf.zoom
2460 then (
2461 state.prevzoom <- conf.zoom;
2462 conf.zoom <- zoom;
2463 reshape conf.winw conf.winh;
2464 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2467 | Some (layout, y, started) ->
2468 let time =
2469 match conf.maxwait with
2470 | None -> 0.0
2471 | Some t -> t
2473 let dt = now () -. started in
2474 if dt > time
2475 then (
2476 state.y <- y;
2477 load layout;
2481 let setcolumns mode columns coverA coverB =
2482 if columns < 0
2483 then (
2484 if isbirdseye mode
2485 then showtext '!' "split mode doesn't work in bird's eye"
2486 else (
2487 conf.columns <- Csplit (-columns, [||]);
2488 state.x <- 0;
2489 conf.zoom <- 1.0;
2492 else (
2493 if columns < 2
2494 then (
2495 conf.columns <- Csingle;
2496 state.x <- 0;
2497 setzoom 1.0;
2499 else (
2500 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2501 conf.zoom <- 1.0;
2504 reshape conf.winw conf.winh;
2507 let enterbirdseye () =
2508 let zoom = float conf.thumbw /. float conf.winw in
2509 let birdseyepageno =
2510 let cy = conf.winh / 2 in
2511 let fold = function
2512 | [] -> 0
2513 | l :: rest ->
2514 let rec fold best = function
2515 | [] -> best.pageno
2516 | l :: rest ->
2517 let d = cy - (l.pagedispy + l.pagevh/2)
2518 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2519 if abs d < abs dbest
2520 then fold l rest
2521 else best.pageno
2522 in fold l rest
2524 fold state.layout
2526 state.mode <- Birdseye (
2527 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2529 conf.zoom <- zoom;
2530 conf.presentation <- false;
2531 conf.interpagespace <- 10;
2532 conf.hlinks <- false;
2533 state.x <- 0;
2534 state.mstate <- Mnone;
2535 conf.maxwait <- None;
2536 conf.columns <- (
2537 match conf.beyecolumns with
2538 | Some c ->
2539 conf.zoom <- 1.0;
2540 Cmulti ((c, 0, 0), [||])
2541 | None -> Csingle
2543 Wsi.setcursor Wsi.CURSOR_INHERIT;
2544 if conf.verbose
2545 then
2546 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2547 (100.0*.zoom)
2548 else
2549 state.text <- ""
2551 reshape conf.winw conf.winh;
2554 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2555 state.mode <- View;
2556 conf.zoom <- c.zoom;
2557 conf.presentation <- c.presentation;
2558 conf.interpagespace <- c.interpagespace;
2559 conf.maxwait <- c.maxwait;
2560 conf.hlinks <- c.hlinks;
2561 conf.beyecolumns <- (
2562 match conf.columns with
2563 | Cmulti ((c, _, _), _) -> Some c
2564 | Csingle -> None
2565 | Csplit _ -> assert false
2567 conf.columns <- (
2568 match c.columns with
2569 | Cmulti (c, _) -> Cmulti (c, [||])
2570 | Csingle -> Csingle
2571 | Csplit _ -> failwith "leaving bird's eye split mode"
2573 state.x <- leftx;
2574 if conf.verbose
2575 then
2576 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2577 (100.0*.conf.zoom)
2579 reshape conf.winw conf.winh;
2580 state.anchor <- if goback then anchor else (pageno, 0.0);
2583 let togglebirdseye () =
2584 match state.mode with
2585 | Birdseye vals -> leavebirdseye vals true
2586 | View -> enterbirdseye ()
2587 | _ -> ()
2590 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2591 let pageno = max 0 (pageno - incr) in
2592 let rec loop = function
2593 | [] -> gotopage1 pageno 0
2594 | l :: _ when l.pageno = pageno ->
2595 if l.pagedispy >= 0 && l.pagey = 0
2596 then G.postRedisplay "upbirdseye"
2597 else gotopage1 pageno 0
2598 | _ :: rest -> loop rest
2600 loop state.layout;
2601 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2604 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2605 let pageno = min (state.pagecount - 1) (pageno + incr) in
2606 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2607 let rec loop = function
2608 | [] ->
2609 let y, h = getpageyh pageno in
2610 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2611 gotoy (clamp dy)
2612 | l :: _ when l.pageno = pageno ->
2613 if l.pagevh != l.pageh
2614 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2615 else G.postRedisplay "downbirdseye"
2616 | _ :: rest -> loop rest
2618 loop state.layout
2621 let optentry mode _ key =
2622 let btos b = if b then "on" else "off" in
2623 if key >= 32 && key < 127
2624 then
2625 let c = Char.chr key in
2626 match c with
2627 | 's' ->
2628 let ondone s =
2629 try conf.scrollstep <- int_of_string s with exc ->
2630 state.text <- Printf.sprintf "bad integer `%s': %s"
2631 s (Printexc.to_string exc)
2633 TEswitch ("scroll step: ", "", None, intentry, ondone)
2635 | 'A' ->
2636 let ondone s =
2638 conf.autoscrollstep <- int_of_string s;
2639 if state.autoscroll <> None
2640 then state.autoscroll <- Some conf.autoscrollstep
2641 with exc ->
2642 state.text <- Printf.sprintf "bad integer `%s': %s"
2643 s (Printexc.to_string exc)
2645 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2647 | 'C' ->
2648 let mode = state.mode in
2649 let ondone s =
2651 let n, a, b = multicolumns_of_string s in
2652 setcolumns mode n a b;
2653 with exc ->
2654 state.text <- Printf.sprintf "bad columns `%s': %s"
2655 s (Printexc.to_string exc)
2657 TEswitch ("columns: ", "", None, textentry, ondone)
2659 | 'Z' ->
2660 let ondone s =
2662 let zoom = float (int_of_string s) /. 100.0 in
2663 setzoom zoom
2664 with exc ->
2665 state.text <- Printf.sprintf "bad integer `%s': %s"
2666 s (Printexc.to_string exc)
2668 TEswitch ("zoom: ", "", None, intentry, ondone)
2670 | 't' ->
2671 let ondone s =
2673 conf.thumbw <- bound (int_of_string s) 2 4096;
2674 state.text <-
2675 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2676 begin match mode with
2677 | Birdseye beye ->
2678 leavebirdseye beye false;
2679 enterbirdseye ();
2680 | _ -> ();
2682 with exc ->
2683 state.text <- Printf.sprintf "bad integer `%s': %s"
2684 s (Printexc.to_string exc)
2686 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2688 | 'R' ->
2689 let ondone s =
2690 match try
2691 Some (int_of_string s)
2692 with exc ->
2693 state.text <- Printf.sprintf "bad integer `%s': %s"
2694 s (Printexc.to_string exc);
2695 None
2696 with
2697 | Some angle -> reqlayout angle conf.proportional
2698 | None -> ()
2700 TEswitch ("rotation: ", "", None, intentry, ondone)
2702 | 'i' ->
2703 conf.icase <- not conf.icase;
2704 TEdone ("case insensitive search " ^ (btos conf.icase))
2706 | 'p' ->
2707 conf.preload <- not conf.preload;
2708 gotoy state.y;
2709 TEdone ("preload " ^ (btos conf.preload))
2711 | 'v' ->
2712 conf.verbose <- not conf.verbose;
2713 TEdone ("verbose " ^ (btos conf.verbose))
2715 | 'd' ->
2716 conf.debug <- not conf.debug;
2717 TEdone ("debug " ^ (btos conf.debug))
2719 | 'h' ->
2720 conf.maxhfit <- not conf.maxhfit;
2721 state.maxy <-
2722 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2723 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2725 | 'c' ->
2726 conf.crophack <- not conf.crophack;
2727 TEdone ("crophack " ^ btos conf.crophack)
2729 | 'a' ->
2730 let s =
2731 match conf.maxwait with
2732 | None ->
2733 conf.maxwait <- Some infinity;
2734 "always wait for page to complete"
2735 | Some _ ->
2736 conf.maxwait <- None;
2737 "show placeholder if page is not ready"
2739 TEdone s
2741 | 'f' ->
2742 conf.underinfo <- not conf.underinfo;
2743 TEdone ("underinfo " ^ btos conf.underinfo)
2745 | 'P' ->
2746 conf.savebmarks <- not conf.savebmarks;
2747 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2749 | 'S' ->
2750 let ondone s =
2752 let pageno, py =
2753 match state.layout with
2754 | [] -> 0, 0
2755 | l :: _ ->
2756 l.pageno, l.pagey
2758 conf.interpagespace <- int_of_string s;
2759 state.maxy <- calcheight ();
2760 let y = getpagey pageno in
2761 gotoy (y + py)
2762 with exc ->
2763 state.text <- Printf.sprintf "bad integer `%s': %s"
2764 s (Printexc.to_string exc)
2766 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2768 | 'l' ->
2769 reqlayout conf.angle (not conf.proportional);
2770 TEdone ("proportional display " ^ btos conf.proportional)
2772 | 'T' ->
2773 settrim (not conf.trimmargins) conf.trimfuzz;
2774 TEdone ("trim margins " ^ btos conf.trimmargins)
2776 | 'I' ->
2777 conf.invert <- not conf.invert;
2778 TEdone ("invert colors " ^ btos conf.invert)
2780 | 'x' ->
2781 let ondone s =
2782 cbput state.hists.sel s;
2783 conf.selcmd <- s;
2785 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2786 textentry, ondone)
2788 | _ ->
2789 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2790 TEstop
2791 else
2792 TEcont state.text
2795 class type lvsource = object
2796 method getitemcount : int
2797 method getitem : int -> (string * int)
2798 method hasaction : int -> bool
2799 method exit :
2800 uioh:uioh ->
2801 cancel:bool ->
2802 active:int ->
2803 first:int ->
2804 pan:int ->
2805 qsearch:string ->
2806 uioh option
2807 method getactive : int
2808 method getfirst : int
2809 method getqsearch : string
2810 method setqsearch : string -> unit
2811 method getpan : int
2812 end;;
2814 class virtual lvsourcebase = object
2815 val mutable m_active = 0
2816 val mutable m_first = 0
2817 val mutable m_qsearch = ""
2818 val mutable m_pan = 0
2819 method getactive = m_active
2820 method getfirst = m_first
2821 method getqsearch = m_qsearch
2822 method getpan = m_pan
2823 method setqsearch s = m_qsearch <- s
2824 end;;
2826 let withoutlastutf8 s =
2827 let len = String.length s in
2828 if len = 0
2829 then s
2830 else
2831 let rec find pos =
2832 if pos = 0
2833 then pos
2834 else
2835 let b = Char.code s.[pos] in
2836 if b land 0b110000 = 0b11000000
2837 then find (pos-1)
2838 else pos-1
2840 let first =
2841 if Char.code s.[len-1] land 0x80 = 0
2842 then len-1
2843 else find (len-1)
2845 String.sub s 0 first;
2848 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2849 let enttext te =
2850 state.mode <- Textentry (te, onleave);
2851 state.text <- "";
2852 enttext ();
2853 G.postRedisplay "textentrykeyboard enttext";
2855 let histaction cmd =
2856 match opthist with
2857 | None -> ()
2858 | Some (action, _) ->
2859 state.mode <- Textentry (
2860 (c, action cmd, opthist, onkey, ondone), onleave
2862 G.postRedisplay "textentry histaction"
2864 match key with
2865 | 0xff08 -> (* backspace *)
2866 let s = withoutlastutf8 text in
2867 let len = String.length s in
2868 if len = 0
2869 then (
2870 onleave Cancel;
2871 G.postRedisplay "textentrykeyboard after cancel";
2873 else (
2874 enttext (c, s, opthist, onkey, ondone)
2877 | 0xff0d ->
2878 ondone text;
2879 onleave Confirm;
2880 G.postRedisplay "textentrykeyboard after confirm"
2882 | 0xff52 -> histaction HCprev
2883 | 0xff54 -> histaction HCnext
2884 | 0xff50 -> histaction HCfirst
2885 | 0xff57 -> histaction HClast
2887 | 0xff1b -> (* escape*)
2888 if String.length text = 0
2889 then (
2890 begin match opthist with
2891 | None -> ()
2892 | Some (_, onhistcancel) -> onhistcancel ()
2893 end;
2894 onleave Cancel;
2895 state.text <- "";
2896 G.postRedisplay "textentrykeyboard after cancel2"
2898 else (
2899 enttext (c, "", opthist, onkey, ondone)
2902 | 0xff9f | 0xffff -> () (* delete *)
2904 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2905 begin match onkey text key with
2906 | TEdone text ->
2907 ondone text;
2908 onleave Confirm;
2909 G.postRedisplay "textentrykeyboard after confirm2";
2911 | TEcont text ->
2912 enttext (c, text, opthist, onkey, ondone);
2914 | TEstop ->
2915 onleave Cancel;
2916 G.postRedisplay "textentrykeyboard after cancel3"
2918 | TEswitch te ->
2919 state.mode <- Textentry (te, onleave);
2920 G.postRedisplay "textentrykeyboard switch";
2921 end;
2923 | _ ->
2924 vlog "unhandled key %s" (Wsi.keyname key)
2927 let firstof first active =
2928 if first > active || abs (first - active) > fstate.maxrows - 1
2929 then max 0 (active - (fstate.maxrows/2))
2930 else first
2933 let calcfirst first active =
2934 if active > first
2935 then
2936 let rows = active - first in
2937 if rows > fstate.maxrows then active - fstate.maxrows else first
2938 else active
2941 let scrollph y maxy =
2942 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2943 let sh = float conf.winh /. sh in
2944 let sh = max sh (float conf.scrollh) in
2946 let percent =
2947 if y = state.maxy
2948 then 1.0
2949 else float y /. float maxy
2951 let position = (float conf.winh -. sh) *. percent in
2953 let position =
2954 if position +. sh > float conf.winh
2955 then float conf.winh -. sh
2956 else position
2958 position, sh;
2961 let coe s = (s :> uioh);;
2963 class listview ~(source:lvsource) ~trusted ~modehash =
2964 object (self)
2965 val m_pan = source#getpan
2966 val m_first = source#getfirst
2967 val m_active = source#getactive
2968 val m_qsearch = source#getqsearch
2969 val m_prev_uioh = state.uioh
2971 method private elemunder y =
2972 let n = y / (fstate.fontsize+1) in
2973 if m_first + n < source#getitemcount
2974 then (
2975 if source#hasaction (m_first + n)
2976 then Some (m_first + n)
2977 else None
2979 else None
2981 method display =
2982 Gl.enable `blend;
2983 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2984 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2985 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2986 GlDraw.color (1., 1., 1.);
2987 Gl.enable `texture_2d;
2988 let fs = fstate.fontsize in
2989 let nfs = fs + 1 in
2990 let ww = fstate.wwidth in
2991 let tabw = 30.0*.ww in
2992 let itemcount = source#getitemcount in
2993 let rec loop row =
2994 if (row - m_first) * nfs > conf.winh
2995 then ()
2996 else (
2997 if row >= 0 && row < itemcount
2998 then (
2999 let (s, level) = source#getitem row in
3000 let y = (row - m_first) * nfs in
3001 let x = 5.0 +. float (level + m_pan) *. ww in
3002 if row = m_active
3003 then (
3004 Gl.disable `texture_2d;
3005 GlDraw.polygon_mode `both `line;
3006 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3007 GlDraw.rect (1., float (y + 1))
3008 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3009 GlDraw.polygon_mode `both `fill;
3010 GlDraw.color (1., 1., 1.);
3011 Gl.enable `texture_2d;
3014 let drawtabularstring s =
3015 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3016 if trusted
3017 then
3018 let tabpos = try String.index s '\t' with Not_found -> -1 in
3019 if tabpos > 0
3020 then
3021 let len = String.length s - tabpos - 1 in
3022 let s1 = String.sub s 0 tabpos
3023 and s2 = String.sub s (tabpos + 1) len in
3024 let nx = drawstr x s1 in
3025 let sw = nx -. x in
3026 let x = x +. (max tabw sw) in
3027 drawstr x s2
3028 else
3029 drawstr x s
3030 else
3031 drawstr x s
3033 let _ = drawtabularstring s in
3034 loop (row+1)
3038 loop m_first;
3039 Gl.disable `blend;
3040 Gl.disable `texture_2d;
3042 method updownlevel incr =
3043 let len = source#getitemcount in
3044 let curlevel =
3045 if m_active >= 0 && m_active < len
3046 then snd (source#getitem m_active)
3047 else -1
3049 let rec flow i =
3050 if i = len then i-1 else if i = -1 then 0 else
3051 let _, l = source#getitem i in
3052 if l != curlevel then i else flow (i+incr)
3054 let active = flow m_active in
3055 let first = calcfirst m_first active in
3056 G.postRedisplay "outline updownlevel";
3057 {< m_active = active; m_first = first >}
3059 method private key1 key mask =
3060 let set1 active first qsearch =
3061 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3063 let search active pattern incr =
3064 let dosearch re =
3065 let rec loop n =
3066 if n >= 0 && n < source#getitemcount
3067 then (
3068 let s, _ = source#getitem n in
3070 (try ignore (Str.search_forward re s 0); true
3071 with Not_found -> false)
3072 then Some n
3073 else loop (n + incr)
3075 else None
3077 loop active
3080 let re = Str.regexp_case_fold pattern in
3081 dosearch re
3082 with Failure s ->
3083 state.text <- s;
3084 None
3086 let itemcount = source#getitemcount in
3087 let find start incr =
3088 let rec find i =
3089 if i = -1 || i = itemcount
3090 then -1
3091 else (
3092 if source#hasaction i
3093 then i
3094 else find (i + incr)
3097 find start
3099 let set active first =
3100 let first = bound first 0 (itemcount - fstate.maxrows) in
3101 state.text <- "";
3102 coe {< m_active = active; m_first = first >}
3104 let navigate incr =
3105 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3106 let active, first =
3107 let incr1 = if incr > 0 then 1 else -1 in
3108 if isvisible m_first m_active
3109 then
3110 let next =
3111 let next = m_active + incr in
3112 let next =
3113 if next < 0 || next >= itemcount
3114 then -1
3115 else find next incr1
3117 if next = -1 || abs (m_active - next) > fstate.maxrows
3118 then -1
3119 else next
3121 if next = -1
3122 then
3123 let first = m_first + incr in
3124 let first = bound first 0 (itemcount - 1) in
3125 let next =
3126 let next = m_active + incr in
3127 let next = bound next 0 (itemcount - 1) in
3128 find next ~-incr1
3130 let active = if next = -1 then m_active else next in
3131 active, first
3132 else
3133 let first = min next m_first in
3134 let first =
3135 if abs (next - first) > fstate.maxrows
3136 then first + incr
3137 else first
3139 next, first
3140 else
3141 let first = m_first + incr in
3142 let first = bound first 0 (itemcount - 1) in
3143 let active =
3144 let next = m_active + incr in
3145 let next = bound next 0 (itemcount - 1) in
3146 let next = find next incr1 in
3147 let active =
3148 if next = -1 || abs (m_active - first) > fstate.maxrows
3149 then (
3150 let active = if m_active = -1 then next else m_active in
3151 active
3153 else next
3155 if isvisible first active
3156 then active
3157 else -1
3159 active, first
3161 G.postRedisplay "listview navigate";
3162 set active first;
3164 match key with
3165 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3166 let incr = if key = 0x72 then -1 else 1 in
3167 let active, first =
3168 match search (m_active + incr) m_qsearch incr with
3169 | None ->
3170 state.text <- m_qsearch ^ " [not found]";
3171 m_active, m_first
3172 | Some active ->
3173 state.text <- m_qsearch;
3174 active, firstof m_first active
3176 G.postRedisplay "listview ctrl-r/s";
3177 set1 active first m_qsearch;
3179 | 0xff08 -> (* backspace *)
3180 if String.length m_qsearch = 0
3181 then coe self
3182 else (
3183 let qsearch = withoutlastutf8 m_qsearch in
3184 let len = String.length qsearch in
3185 if len = 0
3186 then (
3187 state.text <- "";
3188 G.postRedisplay "listview empty qsearch";
3189 set1 m_active m_first "";
3191 else
3192 let active, first =
3193 match search m_active qsearch ~-1 with
3194 | None ->
3195 state.text <- qsearch ^ " [not found]";
3196 m_active, m_first
3197 | Some active ->
3198 state.text <- qsearch;
3199 active, firstof m_first active
3201 G.postRedisplay "listview backspace qsearch";
3202 set1 active first qsearch
3205 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3206 let pattern = m_qsearch ^ Wsi.toutf8 key in
3207 let active, first =
3208 match search m_active pattern 1 with
3209 | None ->
3210 state.text <- pattern ^ " [not found]";
3211 m_active, m_first
3212 | Some active ->
3213 state.text <- pattern;
3214 active, firstof m_first active
3216 G.postRedisplay "listview qsearch add";
3217 set1 active first pattern;
3219 | 0xff1b -> (* escape *)
3220 state.text <- "";
3221 if String.length m_qsearch = 0
3222 then (
3223 G.postRedisplay "list view escape";
3224 begin
3225 match
3226 source#exit (coe self) true m_active m_first m_pan m_qsearch
3227 with
3228 | None -> m_prev_uioh
3229 | Some uioh -> uioh
3232 else (
3233 G.postRedisplay "list view kill qsearch";
3234 source#setqsearch "";
3235 coe {< m_qsearch = "" >}
3238 | 0xff0d -> (* return *)
3239 state.text <- "";
3240 let self = {< m_qsearch = "" >} in
3241 source#setqsearch "";
3242 let opt =
3243 G.postRedisplay "listview enter";
3244 if m_active >= 0 && m_active < source#getitemcount
3245 then (
3246 source#exit (coe self) false m_active m_first m_pan "";
3248 else (
3249 source#exit (coe self) true m_active m_first m_pan "";
3252 begin match opt with
3253 | None -> m_prev_uioh
3254 | Some uioh -> uioh
3257 | 0xff9f | 0xffff -> (* delete *)
3258 coe self
3260 | 0xff52 -> navigate ~-1 (* up *)
3261 | 0xff54 -> navigate 1 (* down *)
3262 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3263 | 0xff56 -> navigate fstate.maxrows (* next *)
3265 | 0xff53 -> (* right *)
3266 state.text <- "";
3267 G.postRedisplay "listview right";
3268 coe {< m_pan = m_pan - 1 >}
3270 | 0xff51 -> (* left *)
3271 state.text <- "";
3272 G.postRedisplay "listview left";
3273 coe {< m_pan = m_pan + 1 >}
3275 | 0xff50 -> (* home *)
3276 let active = find 0 1 in
3277 G.postRedisplay "listview home";
3278 set active 0;
3280 | 0xff57 -> (* end *)
3281 let first = max 0 (itemcount - fstate.maxrows) in
3282 let active = find (itemcount - 1) ~-1 in
3283 G.postRedisplay "listview end";
3284 set active first;
3286 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3287 coe self
3289 | _ ->
3290 dolog "listview unknown key %#x" key; coe self
3292 method key key mask =
3293 match state.mode with
3294 | Textentry te -> textentrykeyboard key mask te; coe self
3295 | _ -> self#key1 key mask
3297 method button button down x y _ =
3298 let opt =
3299 match button with
3300 | 1 when x > conf.winw - conf.scrollbw ->
3301 G.postRedisplay "listview scroll";
3302 if down
3303 then
3304 let _, position, sh = self#scrollph in
3305 if y > truncate position && y < truncate (position +. sh)
3306 then (
3307 state.mstate <- Mscrolly;
3308 Some (coe self)
3310 else
3311 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3312 let first = truncate (s *. float source#getitemcount) in
3313 let first = min source#getitemcount first in
3314 Some (coe {< m_first = first; m_active = first >})
3315 else (
3316 state.mstate <- Mnone;
3317 Some (coe self);
3319 | 1 when not down ->
3320 begin match self#elemunder y with
3321 | Some n ->
3322 G.postRedisplay "listview click";
3323 source#exit
3324 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3325 | _ ->
3326 Some (coe self)
3328 | n when (n == 4 || n == 5) && not down ->
3329 let len = source#getitemcount in
3330 let first =
3331 if n = 5 && m_first + fstate.maxrows >= len
3332 then
3333 m_first
3334 else
3335 let first = m_first + (if n == 4 then -1 else 1) in
3336 bound first 0 (len - 1)
3338 G.postRedisplay "listview wheel";
3339 Some (coe {< m_first = first >})
3340 | _ ->
3341 Some (coe self)
3343 match opt with
3344 | None -> m_prev_uioh
3345 | Some uioh -> uioh
3347 method motion _ y =
3348 match state.mstate with
3349 | Mscrolly ->
3350 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3351 let first = truncate (s *. float source#getitemcount) in
3352 let first = min source#getitemcount first in
3353 G.postRedisplay "listview motion";
3354 coe {< m_first = first; m_active = first >}
3355 | _ -> coe self
3357 method pmotion x y =
3358 if x < conf.winw - conf.scrollbw
3359 then
3360 let n =
3361 match self#elemunder y with
3362 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3363 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3365 let o =
3366 if n != m_active
3367 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3368 else self
3370 coe o
3371 else (
3372 Wsi.setcursor Wsi.CURSOR_INHERIT;
3373 coe self
3376 method infochanged _ = ()
3378 method scrollpw = (0, 0.0, 0.0)
3379 method scrollph =
3380 let nfs = fstate.fontsize + 1 in
3381 let y = m_first * nfs in
3382 let itemcount = source#getitemcount in
3383 let maxi = max 0 (itemcount - fstate.maxrows) in
3384 let maxy = maxi * nfs in
3385 let p, h = scrollph y maxy in
3386 conf.scrollbw, p, h
3388 method modehash = modehash
3389 end;;
3391 class outlinelistview ~source =
3392 object (self)
3393 inherit listview
3394 ~source:(source :> lvsource)
3395 ~trusted:false
3396 ~modehash:(findkeyhash conf "outline")
3397 as super
3399 method key key mask =
3400 let calcfirst first active =
3401 if active > first
3402 then
3403 let rows = active - first in
3404 if rows > fstate.maxrows then active - fstate.maxrows else first
3405 else active
3407 let navigate incr =
3408 let active = m_active + incr in
3409 let active = bound active 0 (source#getitemcount - 1) in
3410 let first = calcfirst m_first active in
3411 G.postRedisplay "outline navigate";
3412 coe {< m_active = active; m_first = first >}
3414 let ctrl = Wsi.withctrl mask in
3415 match key with
3416 | 110 when ctrl -> (* ctrl-n *)
3417 source#narrow m_qsearch;
3418 G.postRedisplay "outline ctrl-n";
3419 coe {< m_first = 0; m_active = 0 >}
3421 | 117 when ctrl -> (* ctrl-u *)
3422 source#denarrow;
3423 G.postRedisplay "outline ctrl-u";
3424 state.text <- "";
3425 coe {< m_first = 0; m_active = 0 >}
3427 | 108 when ctrl -> (* ctrl-l *)
3428 let first = m_active - (fstate.maxrows / 2) in
3429 G.postRedisplay "outline ctrl-l";
3430 coe {< m_first = first >}
3432 | 0xff9f | 0xffff -> (* delete *)
3433 source#remove m_active;
3434 G.postRedisplay "outline delete";
3435 let active = max 0 (m_active-1) in
3436 coe {< m_first = firstof m_first active;
3437 m_active = active >}
3439 | 0xff52 -> navigate ~-1 (* up *)
3440 | 0xff54 -> navigate 1 (* down *)
3441 | 0xff55 -> (* prior *)
3442 navigate ~-(fstate.maxrows)
3443 | 0xff56 -> (* next *)
3444 navigate fstate.maxrows
3446 | 0xff53 -> (* [ctrl-]right *)
3447 let o =
3448 if ctrl
3449 then (
3450 G.postRedisplay "outline ctrl right";
3451 {< m_pan = m_pan + 1 >}
3453 else self#updownlevel 1
3455 coe o
3457 | 0xff51 -> (* [ctrl-]left *)
3458 let o =
3459 if ctrl
3460 then (
3461 G.postRedisplay "outline ctrl left";
3462 {< m_pan = m_pan - 1 >}
3464 else self#updownlevel ~-1
3466 coe o
3468 | 0xff50 -> (* home *)
3469 G.postRedisplay "outline home";
3470 coe {< m_first = 0; m_active = 0 >}
3472 | 0xff57 -> (* end *)
3473 let active = source#getitemcount - 1 in
3474 let first = max 0 (active - fstate.maxrows) in
3475 G.postRedisplay "outline end";
3476 coe {< m_active = active; m_first = first >}
3478 | _ -> super#key key mask
3481 let outlinesource usebookmarks =
3482 let empty = [||] in
3483 (object
3484 inherit lvsourcebase
3485 val mutable m_items = empty
3486 val mutable m_orig_items = empty
3487 val mutable m_prev_items = empty
3488 val mutable m_narrow_pattern = ""
3489 val mutable m_hadremovals = false
3491 method getitemcount =
3492 Array.length m_items + (if m_hadremovals then 1 else 0)
3494 method getitem n =
3495 if n == Array.length m_items && m_hadremovals
3496 then
3497 ("[Confirm removal]", 0)
3498 else
3499 let s, n, _ = m_items.(n) in
3500 (s, n)
3502 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3503 ignore (uioh, first, qsearch);
3504 let confrimremoval = m_hadremovals && active = Array.length m_items in
3505 let items =
3506 if String.length m_narrow_pattern = 0
3507 then m_orig_items
3508 else m_items
3510 if not cancel
3511 then (
3512 if not confrimremoval
3513 then(
3514 let _, _, anchor = m_items.(active) in
3515 gotoanchor anchor;
3516 m_items <- items;
3518 else (
3519 state.bookmarks <- Array.to_list m_items;
3520 m_orig_items <- m_items;
3523 else m_items <- items;
3524 m_pan <- pan;
3525 None
3527 method hasaction _ = true
3529 method greetmsg =
3530 if Array.length m_items != Array.length m_orig_items
3531 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3532 else ""
3534 method narrow pattern =
3535 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3536 match reopt with
3537 | None -> ()
3538 | Some re ->
3539 let rec loop accu n =
3540 if n = -1
3541 then (
3542 m_narrow_pattern <- pattern;
3543 m_items <- Array.of_list accu
3545 else
3546 let (s, _, _) as o = m_items.(n) in
3547 let accu =
3548 if (try ignore (Str.search_forward re s 0); true
3549 with Not_found -> false)
3550 then o :: accu
3551 else accu
3553 loop accu (n-1)
3555 loop [] (Array.length m_items - 1)
3557 method denarrow =
3558 m_orig_items <- (
3559 if usebookmarks
3560 then Array.of_list state.bookmarks
3561 else state.outlines
3563 m_items <- m_orig_items
3565 method remove m =
3566 if usebookmarks
3567 then
3568 if m >= 0 && m < Array.length m_items
3569 then (
3570 m_hadremovals <- true;
3571 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3572 let n = if n >= m then n+1 else n in
3573 m_items.(n)
3577 method reset anchor items =
3578 m_hadremovals <- false;
3579 if m_orig_items == empty || m_prev_items != items
3580 then (
3581 m_orig_items <- items;
3582 if String.length m_narrow_pattern = 0
3583 then m_items <- items;
3585 m_prev_items <- items;
3586 let rely = getanchory anchor in
3587 let active =
3588 let rec loop n best bestd =
3589 if n = Array.length m_items
3590 then best
3591 else
3592 let (_, _, anchor) = m_items.(n) in
3593 let orely = getanchory anchor in
3594 let d = abs (orely - rely) in
3595 if d < bestd
3596 then loop (n+1) n d
3597 else loop (n+1) best bestd
3599 loop 0 ~-1 max_int
3601 m_active <- active;
3602 m_first <- firstof m_first active
3603 end)
3606 let enterselector usebookmarks =
3607 let source = outlinesource usebookmarks in
3608 fun errmsg ->
3609 let outlines =
3610 if usebookmarks
3611 then Array.of_list state.bookmarks
3612 else state.outlines
3614 if Array.length outlines = 0
3615 then (
3616 showtext ' ' errmsg;
3618 else (
3619 state.text <- source#greetmsg;
3620 Wsi.setcursor Wsi.CURSOR_INHERIT;
3621 let anchor = getanchor () in
3622 source#reset anchor outlines;
3623 state.uioh <- coe (new outlinelistview ~source);
3624 G.postRedisplay "enter selector";
3628 let enteroutlinemode =
3629 let f = enterselector false in
3630 fun ()-> f "Document has no outline";
3633 let enterbookmarkmode =
3634 let f = enterselector true in
3635 fun () -> f "Document has no bookmarks (yet)";
3638 let color_of_string s =
3639 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3640 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3644 let color_to_string (r, g, b) =
3645 let r = truncate (r *. 256.0)
3646 and g = truncate (g *. 256.0)
3647 and b = truncate (b *. 256.0) in
3648 Printf.sprintf "%d/%d/%d" r g b
3651 let irect_of_string s =
3652 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3655 let irect_to_string (x0,y0,x1,y1) =
3656 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3659 let makecheckers () =
3660 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3661 following to say:
3662 converted by Issac Trotts. July 25, 2002 *)
3663 let image_height = 64
3664 and image_width = 64 in
3666 let make_image () =
3667 let image =
3668 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3670 for i = 0 to image_width - 1 do
3671 for j = 0 to image_height - 1 do
3672 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3673 (if (i land 8 ) lxor (j land 8) = 0
3674 then [|255;255;255|] else [|200;200;200|])
3675 done
3676 done;
3677 image
3679 let image = make_image () in
3680 let id = GlTex.gen_texture () in
3681 GlTex.bind_texture `texture_2d id;
3682 GlPix.store (`unpack_alignment 1);
3683 GlTex.image2d image;
3684 List.iter (GlTex.parameter ~target:`texture_2d)
3685 [ `wrap_s `repeat;
3686 `wrap_t `repeat;
3687 `mag_filter `nearest;
3688 `min_filter `nearest ];
3692 let setcheckers enabled =
3693 match state.texid with
3694 | None ->
3695 if enabled then state.texid <- Some (makecheckers ())
3697 | Some texid ->
3698 if not enabled
3699 then (
3700 GlTex.delete_texture texid;
3701 state.texid <- None;
3705 let int_of_string_with_suffix s =
3706 let l = String.length s in
3707 let s1, shift =
3708 if l > 1
3709 then
3710 let suffix = Char.lowercase s.[l-1] in
3711 match suffix with
3712 | 'k' -> String.sub s 0 (l-1), 10
3713 | 'm' -> String.sub s 0 (l-1), 20
3714 | 'g' -> String.sub s 0 (l-1), 30
3715 | _ -> s, 0
3716 else s, 0
3718 let n = int_of_string s1 in
3719 let m = n lsl shift in
3720 if m < 0 || m < n
3721 then raise (Failure "value too large")
3722 else m
3725 let string_with_suffix_of_int n =
3726 if n = 0
3727 then "0"
3728 else
3729 let n, s =
3730 if n = 0
3731 then 0, ""
3732 else (
3733 if n land ((1 lsl 20) - 1) = 0
3734 then n lsr 20, "M"
3735 else (
3736 if n land ((1 lsl 10) - 1) = 0
3737 then n lsr 10, "K"
3738 else n, ""
3742 let rec loop s n =
3743 let h = n mod 1000 in
3744 let n = n / 1000 in
3745 if n = 0
3746 then string_of_int h ^ s
3747 else (
3748 let s = Printf.sprintf "_%03d%s" h s in
3749 loop s n
3752 loop "" n ^ s;
3755 let defghyllscroll = (40, 8, 32);;
3756 let ghyllscroll_of_string s =
3757 let (n, a, b) as nab =
3758 if s = "default"
3759 then defghyllscroll
3760 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3762 if n <= a || n <= b || a >= b
3763 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3764 nab;
3767 let ghyllscroll_to_string ((n, a, b) as nab) =
3768 if nab = defghyllscroll
3769 then "default"
3770 else Printf.sprintf "%d,%d,%d" n a b;
3773 let describe_location () =
3774 let f (fn, _) l =
3775 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3777 let fn, ln = List.fold_left f (-1, -1) state.layout in
3778 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3779 let percent =
3780 if maxy <= 0
3781 then 100.
3782 else (100. *. (float state.y /. float maxy))
3784 if fn = ln
3785 then
3786 Printf.sprintf "page %d of %d [%.2f%%]"
3787 (fn+1) state.pagecount percent
3788 else
3789 Printf.sprintf
3790 "pages %d-%d of %d [%.2f%%]"
3791 (fn+1) (ln+1) state.pagecount percent
3794 let enterinfomode =
3795 let btos b = if b then "\xe2\x88\x9a" else "" in
3796 let showextended = ref false in
3797 let leave mode = function
3798 | Confirm -> state.mode <- mode
3799 | Cancel -> state.mode <- mode in
3800 let src =
3801 (object
3802 val mutable m_first_time = true
3803 val mutable m_l = []
3804 val mutable m_a = [||]
3805 val mutable m_prev_uioh = nouioh
3806 val mutable m_prev_mode = View
3808 inherit lvsourcebase
3810 method reset prev_mode prev_uioh =
3811 m_a <- Array.of_list (List.rev m_l);
3812 m_l <- [];
3813 m_prev_mode <- prev_mode;
3814 m_prev_uioh <- prev_uioh;
3815 if m_first_time
3816 then (
3817 let rec loop n =
3818 if n >= Array.length m_a
3819 then ()
3820 else
3821 match m_a.(n) with
3822 | _, _, _, Action _ -> m_active <- n
3823 | _ -> loop (n+1)
3825 loop 0;
3826 m_first_time <- false;
3829 method int name get set =
3830 m_l <-
3831 (name, `int get, 1, Action (
3832 fun u ->
3833 let ondone s =
3834 try set (int_of_string s)
3835 with exn ->
3836 state.text <- Printf.sprintf "bad integer `%s': %s"
3837 s (Printexc.to_string exn)
3839 state.text <- "";
3840 let te = name ^ ": ", "", None, intentry, ondone in
3841 state.mode <- Textentry (te, leave m_prev_mode);
3843 )) :: m_l
3845 method int_with_suffix name get set =
3846 m_l <-
3847 (name, `intws get, 1, Action (
3848 fun u ->
3849 let ondone s =
3850 try set (int_of_string_with_suffix s)
3851 with exn ->
3852 state.text <- Printf.sprintf "bad integer `%s': %s"
3853 s (Printexc.to_string exn)
3855 state.text <- "";
3856 let te =
3857 name ^ ": ", "", None, intentry_with_suffix, ondone
3859 state.mode <- Textentry (te, leave m_prev_mode);
3861 )) :: m_l
3863 method bool ?(offset=1) ?(btos=btos) name get set =
3864 m_l <-
3865 (name, `bool (btos, get), offset, Action (
3866 fun u ->
3867 let v = get () in
3868 set (not v);
3870 )) :: m_l
3872 method color name get set =
3873 m_l <-
3874 (name, `color get, 1, Action (
3875 fun u ->
3876 let invalid = (nan, nan, nan) in
3877 let ondone s =
3878 let c =
3879 try color_of_string s
3880 with exn ->
3881 state.text <- Printf.sprintf "bad color `%s': %s"
3882 s (Printexc.to_string exn);
3883 invalid
3885 if c <> invalid
3886 then set c;
3888 let te = name ^ ": ", "", None, textentry, ondone in
3889 state.text <- color_to_string (get ());
3890 state.mode <- Textentry (te, leave m_prev_mode);
3892 )) :: m_l
3894 method string name get set =
3895 m_l <-
3896 (name, `string get, 1, Action (
3897 fun u ->
3898 let ondone s = set s in
3899 let te = name ^ ": ", "", None, textentry, ondone in
3900 state.mode <- Textentry (te, leave m_prev_mode);
3902 )) :: m_l
3904 method colorspace name get set =
3905 m_l <-
3906 (name, `string get, 1, Action (
3907 fun _ ->
3908 let source =
3909 let vals = [| "rgb"; "bgr"; "gray" |] in
3910 (object
3911 inherit lvsourcebase
3913 initializer
3914 m_active <- int_of_colorspace conf.colorspace;
3915 m_first <- 0;
3917 method getitemcount = Array.length vals
3918 method getitem n = (vals.(n), 0)
3919 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3920 ignore (uioh, first, pan, qsearch);
3921 if not cancel then set active;
3922 None
3923 method hasaction _ = true
3924 end)
3926 state.text <- "";
3927 let modehash = findkeyhash conf "info" in
3928 coe (new listview ~source ~trusted:true ~modehash)
3929 )) :: m_l
3931 method caption s offset =
3932 m_l <- (s, `empty, offset, Noaction) :: m_l
3934 method caption2 s f offset =
3935 m_l <- (s, `string f, offset, Noaction) :: m_l
3937 method getitemcount = Array.length m_a
3939 method getitem n =
3940 let tostr = function
3941 | `int f -> string_of_int (f ())
3942 | `intws f -> string_with_suffix_of_int (f ())
3943 | `string f -> f ()
3944 | `color f -> color_to_string (f ())
3945 | `bool (btos, f) -> btos (f ())
3946 | `empty -> ""
3948 let name, t, offset, _ = m_a.(n) in
3949 ((let s = tostr t in
3950 if String.length s > 0
3951 then Printf.sprintf "%s\t%s" name s
3952 else name),
3953 offset)
3955 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3956 let uiohopt =
3957 if not cancel
3958 then (
3959 m_qsearch <- qsearch;
3960 let uioh =
3961 match m_a.(active) with
3962 | _, _, _, Action f -> f uioh
3963 | _ -> uioh
3965 Some uioh
3967 else None
3969 m_active <- active;
3970 m_first <- first;
3971 m_pan <- pan;
3972 uiohopt
3974 method hasaction n =
3975 match m_a.(n) with
3976 | _, _, _, Action _ -> true
3977 | _ -> false
3978 end)
3980 let rec fillsrc prevmode prevuioh =
3981 let sep () = src#caption "" 0 in
3982 let colorp name get set =
3983 src#string name
3984 (fun () -> color_to_string (get ()))
3985 (fun v ->
3987 let c = color_of_string v in
3988 set c
3989 with exn ->
3990 state.text <- Printf.sprintf "bad color `%s': %s"
3991 v (Printexc.to_string exn);
3994 let oldmode = state.mode in
3995 let birdseye = isbirdseye state.mode in
3997 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3999 src#bool "presentation mode"
4000 (fun () -> conf.presentation)
4001 (fun v ->
4002 conf.presentation <- v;
4003 state.anchor <- getanchor ();
4004 represent ());
4006 src#bool "ignore case in searches"
4007 (fun () -> conf.icase)
4008 (fun v -> conf.icase <- v);
4010 src#bool "preload"
4011 (fun () -> conf.preload)
4012 (fun v -> conf.preload <- v);
4014 src#bool "highlight links"
4015 (fun () -> conf.hlinks)
4016 (fun v -> conf.hlinks <- v);
4018 src#bool "under info"
4019 (fun () -> conf.underinfo)
4020 (fun v -> conf.underinfo <- v);
4022 src#bool "persistent bookmarks"
4023 (fun () -> conf.savebmarks)
4024 (fun v -> conf.savebmarks <- v);
4026 src#bool "proportional display"
4027 (fun () -> conf.proportional)
4028 (fun v -> reqlayout conf.angle v);
4030 src#bool "trim margins"
4031 (fun () -> conf.trimmargins)
4032 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4034 src#bool "persistent location"
4035 (fun () -> conf.jumpback)
4036 (fun v -> conf.jumpback <- v);
4038 sep ();
4039 src#int "inter-page space"
4040 (fun () -> conf.interpagespace)
4041 (fun n ->
4042 conf.interpagespace <- n;
4043 let pageno, py =
4044 match state.layout with
4045 | [] -> 0, 0
4046 | l :: _ ->
4047 l.pageno, l.pagey
4049 state.maxy <- calcheight ();
4050 let y = getpagey pageno in
4051 gotoy (y + py)
4054 src#int "page bias"
4055 (fun () -> conf.pagebias)
4056 (fun v -> conf.pagebias <- v);
4058 src#int "scroll step"
4059 (fun () -> conf.scrollstep)
4060 (fun n -> conf.scrollstep <- n);
4062 src#int "auto scroll step"
4063 (fun () ->
4064 match state.autoscroll with
4065 | Some step -> step
4066 | _ -> conf.autoscrollstep)
4067 (fun n ->
4068 if state.autoscroll <> None
4069 then state.autoscroll <- Some n;
4070 conf.autoscrollstep <- n);
4072 src#int "zoom"
4073 (fun () -> truncate (conf.zoom *. 100.))
4074 (fun v -> setzoom ((float v) /. 100.));
4076 src#int "rotation"
4077 (fun () -> conf.angle)
4078 (fun v -> reqlayout v conf.proportional);
4080 src#int "scroll bar width"
4081 (fun () -> state.scrollw)
4082 (fun v ->
4083 state.scrollw <- v;
4084 conf.scrollbw <- v;
4085 reshape conf.winw conf.winh;
4088 src#int "scroll handle height"
4089 (fun () -> conf.scrollh)
4090 (fun v -> conf.scrollh <- v;);
4092 src#int "thumbnail width"
4093 (fun () -> conf.thumbw)
4094 (fun v ->
4095 conf.thumbw <- min 4096 v;
4096 match oldmode with
4097 | Birdseye beye ->
4098 leavebirdseye beye false;
4099 enterbirdseye ()
4100 | _ -> ()
4103 let mode = state.mode in
4104 src#string "columns"
4105 (fun () ->
4106 match conf.columns with
4107 | Csingle -> "1"
4108 | Cmulti (multi, _) -> multicolumns_to_string multi
4109 | Csplit (count, _) -> "-" ^ string_of_int count
4111 (fun v ->
4112 let n, a, b = multicolumns_of_string v in
4113 setcolumns mode n a b);
4115 sep ();
4116 src#caption "Presentation mode" 0;
4117 src#bool "scrollbar visible"
4118 (fun () -> conf.scrollbarinpm)
4119 (fun v ->
4120 if v != conf.scrollbarinpm
4121 then (
4122 conf.scrollbarinpm <- v;
4123 if conf.presentation
4124 then (
4125 state.scrollw <- if v then conf.scrollbw else 0;
4126 reshape conf.winw conf.winh;
4131 sep ();
4132 src#caption "Pixmap cache" 0;
4133 src#int_with_suffix "size (advisory)"
4134 (fun () -> conf.memlimit)
4135 (fun v -> conf.memlimit <- v);
4137 src#caption2 "used"
4138 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4139 (string_with_suffix_of_int state.memused)
4140 (Hashtbl.length state.tilemap)) 1;
4142 sep ();
4143 src#caption "Layout" 0;
4144 src#caption2 "Dimension"
4145 (fun () ->
4146 Printf.sprintf "%dx%d (virtual %dx%d)"
4147 conf.winw conf.winh
4148 state.w state.maxy)
4150 if conf.debug
4151 then
4152 src#caption2 "Position" (fun () ->
4153 Printf.sprintf "%dx%d" state.x state.y
4155 else
4156 src#caption2 "Visible" (fun () -> describe_location ()) 1
4159 sep ();
4160 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4161 "Save these parameters as global defaults at exit"
4162 (fun () -> conf.bedefault)
4163 (fun v -> conf.bedefault <- v)
4166 sep ();
4167 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4168 src#bool ~offset:0 ~btos "Extended parameters"
4169 (fun () -> !showextended)
4170 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4171 if !showextended
4172 then (
4173 src#bool "checkers"
4174 (fun () -> conf.checkers)
4175 (fun v -> conf.checkers <- v; setcheckers v);
4176 src#bool "update cursor"
4177 (fun () -> conf.updatecurs)
4178 (fun v -> conf.updatecurs <- v);
4179 src#bool "verbose"
4180 (fun () -> conf.verbose)
4181 (fun v -> conf.verbose <- v);
4182 src#bool "invert colors"
4183 (fun () -> conf.invert)
4184 (fun v -> conf.invert <- v);
4185 src#bool "max fit"
4186 (fun () -> conf.maxhfit)
4187 (fun v -> conf.maxhfit <- v);
4188 src#bool "redirect stderr"
4189 (fun () -> conf.redirectstderr)
4190 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4191 src#string "uri launcher"
4192 (fun () -> conf.urilauncher)
4193 (fun v -> conf.urilauncher <- v);
4194 src#string "path launcher"
4195 (fun () -> conf.pathlauncher)
4196 (fun v -> conf.pathlauncher <- v);
4197 src#string "tile size"
4198 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4199 (fun v ->
4201 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4202 conf.tileh <- max 64 w;
4203 conf.tilew <- max 64 h;
4204 flushtiles ();
4205 with exn ->
4206 state.text <- Printf.sprintf "bad tile size `%s': %s"
4207 v (Printexc.to_string exn));
4208 src#int "texture count"
4209 (fun () -> conf.texcount)
4210 (fun v ->
4211 if realloctexts v
4212 then conf.texcount <- v
4213 else showtext '!' " Failed to set texture count please retry later"
4215 src#int "slice height"
4216 (fun () -> conf.sliceheight)
4217 (fun v ->
4218 conf.sliceheight <- v;
4219 wcmd "sliceh %d" conf.sliceheight;
4221 src#int "anti-aliasing level"
4222 (fun () -> conf.aalevel)
4223 (fun v ->
4224 conf.aalevel <- bound v 0 8;
4225 state.anchor <- getanchor ();
4226 opendoc state.path state.password;
4228 src#int "ui font size"
4229 (fun () -> fstate.fontsize)
4230 (fun v -> setfontsize (bound v 5 100));
4231 colorp "background color"
4232 (fun () -> conf.bgcolor)
4233 (fun v -> conf.bgcolor <- v);
4234 src#bool "crop hack"
4235 (fun () -> conf.crophack)
4236 (fun v -> conf.crophack <- v);
4237 src#string "trim fuzz"
4238 (fun () -> irect_to_string conf.trimfuzz)
4239 (fun v ->
4241 conf.trimfuzz <- irect_of_string v;
4242 if conf.trimmargins
4243 then settrim true conf.trimfuzz;
4244 with exn ->
4245 state.text <- Printf.sprintf "bad irect `%s': %s"
4246 v (Printexc.to_string exn)
4248 src#string "throttle"
4249 (fun () ->
4250 match conf.maxwait with
4251 | None -> "show place holder if page is not ready"
4252 | Some time ->
4253 if time = infinity
4254 then "wait for page to fully render"
4255 else
4256 "wait " ^ string_of_float time
4257 ^ " seconds before showing placeholder"
4259 (fun v ->
4261 let f = float_of_string v in
4262 if f <= 0.0
4263 then conf.maxwait <- None
4264 else conf.maxwait <- Some f
4265 with exn ->
4266 state.text <- Printf.sprintf "bad time `%s': %s"
4267 v (Printexc.to_string exn)
4269 src#string "ghyll scroll"
4270 (fun () ->
4271 match conf.ghyllscroll with
4272 | None -> ""
4273 | Some nab -> ghyllscroll_to_string nab
4275 (fun v ->
4277 let gs =
4278 if String.length v = 0
4279 then None
4280 else Some (ghyllscroll_of_string v)
4282 conf.ghyllscroll <- gs
4283 with exn ->
4284 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4285 v (Printexc.to_string exn)
4287 src#string "selection command"
4288 (fun () -> conf.selcmd)
4289 (fun v -> conf.selcmd <- v);
4290 src#colorspace "color space"
4291 (fun () -> colorspace_to_string conf.colorspace)
4292 (fun v ->
4293 conf.colorspace <- colorspace_of_int v;
4294 wcmd "cs %d" v;
4295 load state.layout;
4299 sep ();
4300 src#caption "Document" 0;
4301 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4302 src#caption2 "Pages"
4303 (fun () -> string_of_int state.pagecount) 1;
4304 src#caption2 "Dimensions"
4305 (fun () -> string_of_int (List.length state.pdims)) 1;
4306 if conf.trimmargins
4307 then (
4308 sep ();
4309 src#caption "Trimmed margins" 0;
4310 src#caption2 "Dimensions"
4311 (fun () -> string_of_int (List.length state.pdims)) 1;
4314 src#reset prevmode prevuioh;
4316 fun () ->
4317 state.text <- "";
4318 let prevmode = state.mode
4319 and prevuioh = state.uioh in
4320 fillsrc prevmode prevuioh;
4321 let source = (src :> lvsource) in
4322 let modehash = findkeyhash conf "info" in
4323 state.uioh <- coe (object (self)
4324 inherit listview ~source ~trusted:true ~modehash as super
4325 val mutable m_prevmemused = 0
4326 method infochanged = function
4327 | Memused ->
4328 if m_prevmemused != state.memused
4329 then (
4330 m_prevmemused <- state.memused;
4331 G.postRedisplay "memusedchanged";
4333 | Pdim -> G.postRedisplay "pdimchanged"
4334 | Docinfo -> fillsrc prevmode prevuioh
4336 method key key mask =
4337 if not (Wsi.withctrl mask)
4338 then
4339 match key with
4340 | 0xff51 -> coe (self#updownlevel ~-1)
4341 | 0xff53 -> coe (self#updownlevel 1)
4342 | _ -> super#key key mask
4343 else super#key key mask
4344 end);
4345 G.postRedisplay "info";
4348 let enterhelpmode =
4349 let source =
4350 (object
4351 inherit lvsourcebase
4352 method getitemcount = Array.length state.help
4353 method getitem n =
4354 let s, n, _ = state.help.(n) in
4355 (s, n)
4357 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4358 let optuioh =
4359 if not cancel
4360 then (
4361 m_qsearch <- qsearch;
4362 match state.help.(active) with
4363 | _, _, Action f -> Some (f uioh)
4364 | _ -> Some (uioh)
4366 else None
4368 m_active <- active;
4369 m_first <- first;
4370 m_pan <- pan;
4371 optuioh
4373 method hasaction n =
4374 match state.help.(n) with
4375 | _, _, Action _ -> true
4376 | _ -> false
4378 initializer
4379 m_active <- -1
4380 end)
4381 in fun () ->
4382 let modehash = findkeyhash conf "help" in
4383 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4384 G.postRedisplay "help";
4387 let entermsgsmode =
4388 let msgsource =
4389 let re = Str.regexp "[\r\n]" in
4390 (object
4391 inherit lvsourcebase
4392 val mutable m_items = [||]
4394 method getitemcount = 1 + Array.length m_items
4396 method getitem n =
4397 if n = 0
4398 then "[Clear]", 0
4399 else m_items.(n-1), 0
4401 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4402 ignore uioh;
4403 if not cancel
4404 then (
4405 if active = 0
4406 then Buffer.clear state.errmsgs;
4407 m_qsearch <- qsearch;
4409 m_active <- active;
4410 m_first <- first;
4411 m_pan <- pan;
4412 None
4414 method hasaction n =
4415 n = 0
4417 method reset =
4418 state.newerrmsgs <- false;
4419 let l = Str.split re (Buffer.contents state.errmsgs) in
4420 m_items <- Array.of_list l
4422 initializer
4423 m_active <- 0
4424 end)
4425 in fun () ->
4426 state.text <- "";
4427 msgsource#reset;
4428 let source = (msgsource :> lvsource) in
4429 let modehash = findkeyhash conf "listview" in
4430 state.uioh <- coe (object
4431 inherit listview ~source ~trusted:false ~modehash as super
4432 method display =
4433 if state.newerrmsgs
4434 then msgsource#reset;
4435 super#display
4436 end);
4437 G.postRedisplay "msgs";
4440 let quickbookmark ?title () =
4441 match state.layout with
4442 | [] -> ()
4443 | l :: _ ->
4444 let title =
4445 match title with
4446 | None ->
4447 let sec = Unix.gettimeofday () in
4448 let tm = Unix.localtime sec in
4449 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4450 (l.pageno+1)
4451 tm.Unix.tm_mday
4452 tm.Unix.tm_mon
4453 (tm.Unix.tm_year + 1900)
4454 tm.Unix.tm_hour
4455 tm.Unix.tm_min
4456 | Some title -> title
4458 state.bookmarks <-
4459 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4460 :: state.bookmarks
4463 let doreshape w h =
4464 state.fullscreen <- None;
4465 Wsi.reshape w h;
4468 let setautoscrollspeed step goingdown =
4469 let incr = max 1 ((abs step) / 2) in
4470 let incr = if goingdown then incr else -incr in
4471 let astep = step + incr in
4472 state.autoscroll <- Some astep;
4475 let gotounder = function
4476 | Ulinkgoto (pageno, top) ->
4477 if pageno >= 0
4478 then (
4479 addnav ();
4480 gotopage1 pageno top;
4483 | Ulinkuri s ->
4484 gotouri s
4486 | Uremote (filename, pageno) ->
4487 let path =
4488 if Sys.file_exists filename
4489 then filename
4490 else
4491 let dir = Filename.dirname state.path in
4492 let path = Filename.concat dir filename in
4493 if Sys.file_exists path
4494 then path
4495 else ""
4497 if String.length path > 0
4498 then (
4499 let anchor = getanchor () in
4500 let ranchor = state.path, state.password, anchor in
4501 state.anchor <- (pageno, 0.0);
4502 state.ranchors <- ranchor :: state.ranchors;
4503 opendoc path "";
4505 else showtext '!' ("Could not find " ^ filename)
4507 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4510 let canpan () =
4511 match conf.columns with
4512 | Csplit _ -> true
4513 | _ -> conf.zoom > 1.0
4516 let viewkeyboard key mask =
4517 let enttext te =
4518 let mode = state.mode in
4519 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4520 state.text <- "";
4521 enttext ();
4522 G.postRedisplay "view:enttext"
4524 let ctrl = Wsi.withctrl mask in
4525 match key with
4526 | 81 -> (* Q *)
4527 exit 0
4529 | 0xff63 -> (* insert *)
4530 if conf.angle mod 360 = 0
4531 then (
4532 state.mode <- LinkNav (Ltgendir 0);
4533 gotoy state.y;
4535 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4537 | 0xff1b | 113 -> (* escape / q *)
4538 begin match state.mstate with
4539 | Mzoomrect _ ->
4540 state.mstate <- Mnone;
4541 Wsi.setcursor Wsi.CURSOR_INHERIT;
4542 G.postRedisplay "kill zoom rect";
4543 | _ ->
4544 match state.ranchors with
4545 | [] -> raise Quit
4546 | (path, password, anchor) :: rest ->
4547 state.ranchors <- rest;
4548 state.anchor <- anchor;
4549 opendoc path password
4550 end;
4552 | 0xff08 -> (* backspace *)
4553 let y = getnav ~-1 in
4554 gotoy_and_clear_text y
4556 | 111 -> (* o *)
4557 enteroutlinemode ()
4559 | 117 -> (* u *)
4560 state.rects <- [];
4561 state.text <- "";
4562 G.postRedisplay "dehighlight";
4564 | 47 | 63 -> (* / ? *)
4565 let ondone isforw s =
4566 cbput state.hists.pat s;
4567 state.searchpattern <- s;
4568 search s isforw
4570 let s = String.create 1 in
4571 s.[0] <- Char.chr key;
4572 enttext (s, "", Some (onhist state.hists.pat),
4573 textentry, ondone (key = 47))
4575 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4576 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4577 setzoom (conf.zoom +. incr)
4579 | 43 | 0xffab -> (* + *)
4580 let ondone s =
4581 let n =
4582 try int_of_string s with exc ->
4583 state.text <- Printf.sprintf "bad integer `%s': %s"
4584 s (Printexc.to_string exc);
4585 max_int
4587 if n != max_int
4588 then (
4589 conf.pagebias <- n;
4590 state.text <- "page bias is now " ^ string_of_int n;
4593 enttext ("page bias: ", "", None, intentry, ondone)
4595 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4596 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4597 setzoom (max 0.01 (conf.zoom -. decr))
4599 | 45 | 0xffad -> (* - *)
4600 let ondone msg = state.text <- msg in
4601 enttext (
4602 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4603 optentry state.mode, ondone
4606 | 48 when ctrl -> (* ctrl-0 *)
4607 setzoom 1.0
4609 | 49 when ctrl -> (* 1 *)
4610 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4611 if zoom < 1.0
4612 then setzoom zoom
4614 | 0xffc6 -> (* f9 *)
4615 togglebirdseye ()
4617 | 57 when ctrl -> (* ctrl-9 *)
4618 togglebirdseye ()
4620 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4621 when not ctrl -> (* 0..9 *)
4622 let ondone s =
4623 let n =
4624 try int_of_string s with exc ->
4625 state.text <- Printf.sprintf "bad integer `%s': %s"
4626 s (Printexc.to_string exc);
4629 if n >= 0
4630 then (
4631 addnav ();
4632 cbput state.hists.pag (string_of_int n);
4633 gotopage1 (n + conf.pagebias - 1) 0;
4636 let pageentry text key =
4637 match Char.unsafe_chr key with
4638 | 'g' -> TEdone text
4639 | _ -> intentry text key
4641 let text = "x" in text.[0] <- Char.chr key;
4642 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4644 | 98 -> (* b *)
4645 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4646 reshape conf.winw conf.winh;
4648 | 108 -> (* l *)
4649 conf.hlinks <- not conf.hlinks;
4650 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4651 G.postRedisplay "toggle highlightlinks";
4653 | 70 -> (* F *)
4654 state.glinks <- true;
4655 let a = Char.code 'a' and z = Char.code 'z' in
4656 let ondone s =
4657 let n =
4658 let rec loop pos n = if pos = String.length s then n else
4659 let m = Char.code s.[pos] - a in
4660 loop (pos+1) (n*(z-a+1) + m)
4661 in loop 0 0
4663 if n >= 0
4664 then (
4665 let rec loop n = function
4666 | [] -> ()
4667 | l :: rest ->
4668 match getopaque l.pageno with
4669 | None -> loop n rest
4670 | Some opaque ->
4671 let m = getlinkcount opaque in
4672 if n < m
4673 then (
4674 let under = getlink opaque n in
4675 addnav ();
4676 gotounder under;
4678 else loop (n-m) rest
4680 loop n state.layout;
4683 let onkey text key =
4684 if (key >= a && key <= z)
4685 then
4686 let c = Char.unsafe_chr key in
4687 let text = addchar text c in
4688 TEcont text
4689 else
4690 TEcont text
4692 let mode = state.mode in
4693 state.mode <- Textentry (
4694 (":", "", Some (onhist state.hists.pag), onkey, ondone),
4695 fun _ ->
4696 state.glinks <- false;
4697 state.mode <- mode
4699 state.text <- "";
4700 G.postRedisplay "view:enttext"
4702 | 97 -> (* a *)
4703 begin match state.autoscroll with
4704 | Some step ->
4705 conf.autoscrollstep <- step;
4706 state.autoscroll <- None
4707 | None ->
4708 if conf.autoscrollstep = 0
4709 then state.autoscroll <- Some 1
4710 else state.autoscroll <- Some conf.autoscrollstep
4713 | 112 when ctrl -> (* ctrl-p *)
4714 launchpath ()
4716 | 80 -> (* P *)
4717 conf.presentation <- not conf.presentation;
4718 if conf.presentation
4719 then (
4720 if not conf.scrollbarinpm
4721 then state.scrollw <- 0;
4723 else
4724 state.scrollw <- conf.scrollbw;
4726 showtext ' ' ("presentation mode " ^
4727 if conf.presentation then "on" else "off");
4728 state.anchor <- getanchor ();
4729 represent ()
4731 | 102 -> (* f *)
4732 begin match state.fullscreen with
4733 | None ->
4734 state.fullscreen <- Some (conf.winw, conf.winh);
4735 Wsi.fullscreen ()
4736 | Some (w, h) ->
4737 state.fullscreen <- None;
4738 doreshape w h
4741 | 103 -> (* g *)
4742 gotoy_and_clear_text 0
4744 | 71 -> (* G *)
4745 gotopage1 (state.pagecount - 1) 0
4747 | 112 | 78 -> (* p|N *)
4748 search state.searchpattern false
4750 | 110 | 0xffc0 -> (* n|F3 *)
4751 search state.searchpattern true
4753 | 116 -> (* t *)
4754 begin match state.layout with
4755 | [] -> ()
4756 | l :: _ ->
4757 gotoy_and_clear_text (getpagey l.pageno)
4760 | 32 -> (* ' ' *)
4761 begin match List.rev state.layout with
4762 | [] -> ()
4763 | l :: _ ->
4764 let pageno = min (l.pageno+1) (state.pagecount-1) in
4765 gotoy_and_clear_text (getpagey pageno)
4768 | 0xff9f | 0xffff -> (* delete *)
4769 begin match state.layout with
4770 | [] -> ()
4771 | l :: _ ->
4772 let pageno = max 0 (l.pageno-1) in
4773 gotoy_and_clear_text (getpagey pageno)
4776 | 61 -> (* = *)
4777 showtext ' ' (describe_location ());
4779 | 119 -> (* w *)
4780 begin match state.layout with
4781 | [] -> ()
4782 | l :: _ ->
4783 doreshape (l.pagew + state.scrollw) l.pageh;
4784 G.postRedisplay "w"
4787 | 39 -> (* ' *)
4788 enterbookmarkmode ()
4790 | 104 | 0xffbe -> (* h|F1 *)
4791 enterhelpmode ()
4793 | 105 -> (* i *)
4794 enterinfomode ()
4796 | 101 when conf.redirectstderr -> (* e *)
4797 entermsgsmode ()
4799 | 109 -> (* m *)
4800 let ondone s =
4801 match state.layout with
4802 | l :: _ ->
4803 state.bookmarks <-
4804 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4805 :: state.bookmarks
4806 | _ -> ()
4808 enttext ("bookmark: ", "", None, textentry, ondone)
4810 | 126 -> (* ~ *)
4811 quickbookmark ();
4812 showtext ' ' "Quick bookmark added";
4814 | 122 -> (* z *)
4815 begin match state.layout with
4816 | l :: _ ->
4817 let rect = getpdimrect l.pagedimno in
4818 let w, h =
4819 if conf.crophack
4820 then
4821 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4822 truncate (1.2 *. (rect.(3) -. rect.(0))))
4823 else
4824 (truncate (rect.(1) -. rect.(0)),
4825 truncate (rect.(3) -. rect.(0)))
4827 let w = truncate ((float w)*.conf.zoom)
4828 and h = truncate ((float h)*.conf.zoom) in
4829 if w != 0 && h != 0
4830 then (
4831 state.anchor <- getanchor ();
4832 doreshape (w + state.scrollw) (h + conf.interpagespace)
4834 G.postRedisplay "z";
4836 | [] -> ()
4839 | 50 when ctrl -> (* ctrl-2 *)
4840 let maxw = getmaxw () in
4841 if maxw > 0.0
4842 then setzoom (maxw /. float conf.winw)
4844 | 60 | 62 -> (* < > *)
4845 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4847 | 91 | 93 -> (* [ ] *)
4848 conf.colorscale <-
4849 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4851 G.postRedisplay "brightness";
4853 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4854 setzoom state.prevzoom
4856 | 107 | 0xff52 -> (* k up *)
4857 begin match state.autoscroll with
4858 | None ->
4859 begin match state.mode with
4860 | Birdseye beye -> upbirdseye 1 beye
4861 | _ ->
4862 if ctrl
4863 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4864 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4866 | Some n ->
4867 setautoscrollspeed n false
4870 | 106 | 0xff54 -> (* j down *)
4871 begin match state.autoscroll with
4872 | None ->
4873 begin match state.mode with
4874 | Birdseye beye -> downbirdseye 1 beye
4875 | _ ->
4876 if ctrl
4877 then gotoy_and_clear_text (clamp (conf.winh/2))
4878 else gotoy_and_clear_text (clamp conf.scrollstep)
4880 | Some n ->
4881 setautoscrollspeed n true
4884 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4885 if canpan ()
4886 then
4887 let dx =
4888 if ctrl
4889 then conf.winw / 2
4890 else 10
4892 let dx = if key = 0xff51 then dx else -dx in
4893 state.x <- state.x + dx;
4894 gotoy_and_clear_text state.y
4895 else (
4896 state.text <- "";
4897 G.postRedisplay "lef/right"
4900 | 0xff55 -> (* prior *)
4901 let y =
4902 if ctrl
4903 then
4904 match state.layout with
4905 | [] -> state.y
4906 | l :: _ -> state.y - l.pagey
4907 else
4908 clamp (-conf.winh)
4910 gotoghyll y
4912 | 0xff56 -> (* next *)
4913 let y =
4914 if ctrl
4915 then
4916 match List.rev state.layout with
4917 | [] -> state.y
4918 | l :: _ -> getpagey l.pageno
4919 else
4920 clamp conf.winh
4922 gotoghyll y
4924 | 0xff50 -> gotoghyll 0
4925 | 0xff57 -> gotoghyll (clamp state.maxy)
4926 | 0xff53 when Wsi.withalt mask ->
4927 gotoghyll (getnav ~-1)
4928 | 0xff51 when Wsi.withalt mask ->
4929 gotoghyll (getnav 1)
4931 | 114 -> (* r *)
4932 state.anchor <- getanchor ();
4933 opendoc state.path state.password
4935 | 118 when conf.debug -> (* v *)
4936 state.rects <- [];
4937 List.iter (fun l ->
4938 match getopaque l.pageno with
4939 | None -> ()
4940 | Some opaque ->
4941 let x0, y0, x1, y1 = pagebbox opaque in
4942 let a,b = float x0, float y0 in
4943 let c,d = float x1, float y0 in
4944 let e,f = float x1, float y1 in
4945 let h,j = float x0, float y1 in
4946 let rect = (a,b,c,d,e,f,h,j) in
4947 debugrect rect;
4948 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4949 ) state.layout;
4950 G.postRedisplay "v";
4952 | _ ->
4953 vlog "huh? %s" (Wsi.keyname key)
4956 let linknavkeyboard key mask linknav =
4957 let getpage pageno =
4958 let rec loop = function
4959 | [] -> None
4960 | l :: _ when l.pageno = pageno -> Some l
4961 | _ :: rest -> loop rest
4962 in loop state.layout
4964 let doexact (pageno, n) =
4965 match getopaque pageno, getpage pageno with
4966 | Some opaque, Some l ->
4967 if key = 0xff0d
4968 then
4969 let under = getlink opaque n in
4970 G.postRedisplay "link gotounder";
4971 gotounder under;
4972 state.mode <- View;
4973 else
4974 let opt, dir =
4975 match key with
4976 | 0xff50 -> (* home *)
4977 Some (findlink opaque LDfirst), -1
4979 | 0xff57 -> (* end *)
4980 Some (findlink opaque LDlast), 1
4982 | 0xff51 -> (* left *)
4983 Some (findlink opaque (LDleft n)), -1
4985 | 0xff53 -> (* right *)
4986 Some (findlink opaque (LDright n)), 1
4988 | 0xff52 -> (* up *)
4989 Some (findlink opaque (LDup n)), -1
4991 | 0xff54 -> (* down *)
4992 Some (findlink opaque (LDdown n)), 1
4994 | _ -> None, 0
4996 let pwl l dir =
4997 begin match findpwl l.pageno dir with
4998 | Pwlnotfound -> ()
4999 | Pwl pageno ->
5000 let notfound dir =
5001 state.mode <- LinkNav (Ltgendir dir);
5002 let y, h = getpageyh pageno in
5003 let y =
5004 if dir < 0
5005 then y + h - conf.winh
5006 else y
5008 gotoy y
5010 begin match getopaque pageno, getpage pageno with
5011 | Some opaque, Some _ ->
5012 let link =
5013 let ld = if dir > 0 then LDfirst else LDlast in
5014 findlink opaque ld
5016 begin match link with
5017 | Lfound m ->
5018 showlinktype (getlink opaque m);
5019 state.mode <- LinkNav (Ltexact (pageno, m));
5020 G.postRedisplay "linknav jpage";
5021 | _ -> notfound dir
5022 end;
5023 | _ -> notfound dir
5024 end;
5025 end;
5027 begin match opt with
5028 | Some Lnotfound -> pwl l dir;
5029 | Some (Lfound m) ->
5030 if m = n
5031 then pwl l dir
5032 else (
5033 let _, y0, _, y1 = getlinkrect opaque m in
5034 if y0 < l.pagey
5035 then gotopage1 l.pageno y0
5036 else (
5037 let d = fstate.fontsize + 1 in
5038 if y1 - l.pagey > l.pagevh - d
5039 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5040 else G.postRedisplay "linknav";
5042 showlinktype (getlink opaque m);
5043 state.mode <- LinkNav (Ltexact (l.pageno, m));
5046 | None -> viewkeyboard key mask
5047 end;
5048 | _ -> viewkeyboard key mask
5050 if key = 0xff63
5051 then (
5052 state.mode <- View;
5053 G.postRedisplay "leave linknav"
5055 else
5056 match linknav with
5057 | Ltgendir _ -> viewkeyboard key mask
5058 | Ltexact exact -> doexact exact
5061 let keyboard key mask =
5062 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5063 then wcmd "interrupt"
5064 else state.uioh <- state.uioh#key key mask
5067 let birdseyekeyboard key mask
5068 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5069 let incr =
5070 match conf.columns with
5071 | Csingle -> 1
5072 | Cmulti ((c, _, _), _) -> c
5073 | Csplit _ -> failwith "bird's eye split mode"
5075 match key with
5076 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5077 let y, h = getpageyh pageno in
5078 let top = (conf.winh - h) / 2 in
5079 gotoy (max 0 (y - top))
5080 | 0xff0d -> leavebirdseye beye false
5081 | 0xff1b -> leavebirdseye beye true (* escape *)
5082 | 0xff52 -> upbirdseye incr beye (* prior *)
5083 | 0xff54 -> downbirdseye incr beye (* next *)
5084 | 0xff51 -> upbirdseye 1 beye (* up *)
5085 | 0xff53 -> downbirdseye 1 beye (* down *)
5087 | 0xff55 ->
5088 begin match state.layout with
5089 | l :: _ ->
5090 if l.pagey != 0
5091 then (
5092 state.mode <- Birdseye (
5093 oconf, leftx, l.pageno, hooverpageno, anchor
5095 gotopage1 l.pageno 0;
5097 else (
5098 let layout = layout (state.y-conf.winh) conf.winh in
5099 match layout with
5100 | [] -> gotoy (clamp (-conf.winh))
5101 | l :: _ ->
5102 state.mode <- Birdseye (
5103 oconf, leftx, l.pageno, hooverpageno, anchor
5105 gotopage1 l.pageno 0
5108 | [] -> gotoy (clamp (-conf.winh))
5109 end;
5111 | 0xff56 ->
5112 begin match List.rev state.layout with
5113 | l :: _ ->
5114 let layout = layout (state.y + conf.winh) conf.winh in
5115 begin match layout with
5116 | [] ->
5117 let incr = l.pageh - l.pagevh in
5118 if incr = 0
5119 then (
5120 state.mode <-
5121 Birdseye (
5122 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5124 G.postRedisplay "birdseye pagedown";
5126 else gotoy (clamp (incr + conf.interpagespace*2));
5128 | l :: _ ->
5129 state.mode <-
5130 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5131 gotopage1 l.pageno 0;
5134 | [] -> gotoy (clamp conf.winh)
5135 end;
5137 | 0xff50 ->
5138 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5139 gotopage1 0 0
5141 | 0xff57 ->
5142 let pageno = state.pagecount - 1 in
5143 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5144 if not (pagevisible state.layout pageno)
5145 then
5146 let h =
5147 match List.rev state.pdims with
5148 | [] -> conf.winh
5149 | (_, _, h, _) :: _ -> h
5151 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5152 else G.postRedisplay "birdseye end";
5153 | _ -> viewkeyboard key mask
5156 let drawpage l linkindexbase =
5157 let color =
5158 match state.mode with
5159 | Textentry _ -> scalecolor 0.4
5160 | LinkNav _
5161 | View -> scalecolor 1.0
5162 | Birdseye (_, _, pageno, hooverpageno, _) ->
5163 if l.pageno = hooverpageno
5164 then scalecolor 0.9
5165 else (
5166 if l.pageno = pageno
5167 then scalecolor 1.0
5168 else scalecolor 0.8
5171 drawtiles l color;
5172 begin match getopaque l.pageno with
5173 | Some opaque ->
5174 if tileready l l.pagex l.pagey
5175 then
5176 let x = l.pagedispx - l.pagex
5177 and y = l.pagedispy - l.pagey in
5178 let hlmask = (if conf.hlinks then 1 else 0)
5179 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5181 let s =
5182 match state.mode with
5183 | Textentry ((_, s, _, _, _), _) when state.glinks -> s
5184 | _ -> ""
5186 postprocess opaque hlmask x y (linkindexbase, s);
5187 else 0
5189 | _ -> 0
5190 end;
5193 let scrollindicator () =
5194 let sbw, ph, sh = state.uioh#scrollph in
5195 let sbh, pw, sw = state.uioh#scrollpw in
5197 GlDraw.color (0.64, 0.64, 0.64);
5198 GlDraw.rect
5199 (float (conf.winw - sbw), 0.)
5200 (float conf.winw, float conf.winh)
5202 GlDraw.rect
5203 (0., float (conf.winh - sbh))
5204 (float (conf.winw - state.scrollw - 1), float conf.winh)
5206 GlDraw.color (0.0, 0.0, 0.0);
5208 GlDraw.rect
5209 (float (conf.winw - sbw), ph)
5210 (float conf.winw, ph +. sh)
5212 GlDraw.rect
5213 (pw, float (conf.winh - sbh))
5214 (pw +. sw, float conf.winh)
5218 let showsel () =
5219 match state.mstate with
5220 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5223 | Msel ((x0, y0), (x1, y1)) ->
5224 let rec loop = function
5225 | l :: ls ->
5226 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5227 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5228 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5229 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5230 then
5231 match getopaque l.pageno with
5232 | Some opaque ->
5233 let x0, y0 = pagetranslatepoint l x0 y0 in
5234 let x1, y1 = pagetranslatepoint l x1 y1 in
5235 seltext opaque (x0, y0, x1, y1);
5236 | _ -> ()
5237 else loop ls
5238 | [] -> ()
5240 loop state.layout
5243 let showrects rects =
5244 Gl.enable `blend;
5245 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5246 GlDraw.polygon_mode `both `fill;
5247 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5248 List.iter
5249 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5250 List.iter (fun l ->
5251 if l.pageno = pageno
5252 then (
5253 let dx = float (l.pagedispx - l.pagex) in
5254 let dy = float (l.pagedispy - l.pagey) in
5255 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5256 GlDraw.begins `quads;
5258 GlDraw.vertex2 (x0+.dx, y0+.dy);
5259 GlDraw.vertex2 (x1+.dx, y1+.dy);
5260 GlDraw.vertex2 (x2+.dx, y2+.dy);
5261 GlDraw.vertex2 (x3+.dx, y3+.dy);
5263 GlDraw.ends ();
5265 ) state.layout
5266 ) rects
5268 Gl.disable `blend;
5271 let display () =
5272 GlClear.color (scalecolor2 conf.bgcolor);
5273 GlClear.clear [`color];
5274 let rec loop linkindexbase = function
5275 | l :: rest ->
5276 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5277 loop linkindexbase rest
5278 | [] -> ()
5280 loop 0 state.layout;
5281 let rects =
5282 match state.mode with
5283 | LinkNav (Ltexact (pageno, linkno)) ->
5284 begin match getopaque pageno with
5285 | Some opaque ->
5286 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5287 (pageno, 5, (
5288 float x0, float y0,
5289 float x1, float y0,
5290 float x1, float y1,
5291 float x0, float y1)
5292 ) :: state.rects
5293 | None -> state.rects
5295 | _ -> state.rects
5297 showrects rects;
5298 showsel ();
5299 state.uioh#display;
5300 begin match state.mstate with
5301 | Mzoomrect ((x0, y0), (x1, y1)) ->
5302 Gl.enable `blend;
5303 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5304 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5305 GlDraw.rect (float x0, float y0)
5306 (float x1, float y1);
5307 Gl.disable `blend;
5308 | _ -> ()
5309 end;
5310 enttext ();
5311 scrollindicator ();
5312 Wsi.swapb ();
5315 let zoomrect x y x1 y1 =
5316 let x0 = min x x1
5317 and x1 = max x x1
5318 and y0 = min y y1 in
5319 gotoy (state.y + y0);
5320 state.anchor <- getanchor ();
5321 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5322 let margin =
5323 if state.w < conf.winw - state.scrollw
5324 then (conf.winw - state.scrollw - state.w) / 2
5325 else 0
5327 state.x <- (state.x + margin) - x0;
5328 setzoom zoom;
5329 Wsi.setcursor Wsi.CURSOR_INHERIT;
5330 state.mstate <- Mnone;
5333 let scrollx x =
5334 let winw = conf.winw - state.scrollw - 1 in
5335 let s = float x /. float winw in
5336 let destx = truncate (float (state.w + winw) *. s) in
5337 state.x <- winw - destx;
5338 gotoy_and_clear_text state.y;
5339 state.mstate <- Mscrollx;
5342 let scrolly y =
5343 let s = float y /. float conf.winh in
5344 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5345 gotoy_and_clear_text desty;
5346 state.mstate <- Mscrolly;
5349 let viewmouse button down x y mask =
5350 match button with
5351 | n when (n == 4 || n == 5) && not down ->
5352 if Wsi.withctrl mask
5353 then (
5354 match state.mstate with
5355 | Mzoom (oldn, i) ->
5356 if oldn = n
5357 then (
5358 if i = 2
5359 then
5360 let incr =
5361 match n with
5362 | 5 ->
5363 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5364 | _ ->
5365 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5367 let zoom = conf.zoom -. incr in
5368 setzoom zoom;
5369 state.mstate <- Mzoom (n, 0);
5370 else
5371 state.mstate <- Mzoom (n, i+1);
5373 else state.mstate <- Mzoom (n, 0)
5375 | _ -> state.mstate <- Mzoom (n, 0)
5377 else (
5378 match state.autoscroll with
5379 | Some step -> setautoscrollspeed step (n=4)
5380 | None ->
5381 let incr =
5382 if n = 4
5383 then -conf.scrollstep
5384 else conf.scrollstep
5386 let incr = incr * 2 in
5387 let y = clamp incr in
5388 gotoy_and_clear_text y
5391 | 1 when Wsi.withctrl mask ->
5392 if down
5393 then (
5394 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5395 state.mstate <- Mpan (x, y)
5397 else
5398 state.mstate <- Mnone
5400 | 3 ->
5401 if down
5402 then (
5403 Wsi.setcursor Wsi.CURSOR_CYCLE;
5404 let p = (x, y) in
5405 state.mstate <- Mzoomrect (p, p)
5407 else (
5408 match state.mstate with
5409 | Mzoomrect ((x0, y0), _) ->
5410 if abs (x-x0) > 10 && abs (y - y0) > 10
5411 then zoomrect x0 y0 x y
5412 else (
5413 state.mstate <- Mnone;
5414 Wsi.setcursor Wsi.CURSOR_INHERIT;
5415 G.postRedisplay "kill accidental zoom rect";
5417 | _ ->
5418 Wsi.setcursor Wsi.CURSOR_INHERIT;
5419 state.mstate <- Mnone
5422 | 1 when x > conf.winw - state.scrollw ->
5423 if down
5424 then
5425 let _, position, sh = state.uioh#scrollph in
5426 if y > truncate position && y < truncate (position +. sh)
5427 then state.mstate <- Mscrolly
5428 else scrolly y
5429 else
5430 state.mstate <- Mnone
5432 | 1 when y > conf.winh - state.hscrollh ->
5433 if down
5434 then
5435 let _, position, sw = state.uioh#scrollpw in
5436 if x > truncate position && x < truncate (position +. sw)
5437 then state.mstate <- Mscrollx
5438 else scrollx x
5439 else
5440 state.mstate <- Mnone
5442 | 1 ->
5443 let dest = if down then getunder x y else Unone in
5444 begin match dest with
5445 | Ulinkgoto _
5446 | Ulinkuri _
5447 | Uremote _
5448 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5449 gotounder dest
5451 | Unone when down ->
5452 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5453 state.mstate <- Mpan (x, y);
5455 | Unone | Utext _ ->
5456 if down
5457 then (
5458 if conf.angle mod 360 = 0
5459 then (
5460 state.mstate <- Msel ((x, y), (x, y));
5461 G.postRedisplay "mouse select";
5464 else (
5465 match state.mstate with
5466 | Mnone -> ()
5468 | Mzoom _ | Mscrollx | Mscrolly ->
5469 state.mstate <- Mnone
5471 | Mzoomrect ((x0, y0), _) ->
5472 zoomrect x0 y0 x y
5474 | Mpan _ ->
5475 Wsi.setcursor Wsi.CURSOR_INHERIT;
5476 state.mstate <- Mnone
5478 | Msel ((_, y0), (_, y1)) ->
5479 let rec loop = function
5480 | [] -> ()
5481 | l :: rest ->
5482 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5483 || ((y1 >= l.pagedispy
5484 && y1 <= (l.pagedispy + l.pagevh)))
5485 then
5486 match getopaque l.pageno with
5487 | Some opaque ->
5488 begin
5489 match
5490 try Some (Unix.pipe ())
5491 with exn ->
5492 dolog "can not create sel pipe: %s"
5493 (Printexc.to_string exn);
5494 None
5495 with
5496 | None -> ()
5497 | Some (r, w) ->
5498 let doclose what fd =
5499 try Unix.close fd
5500 with exn ->
5501 dolog "%s close failed: %s"
5502 what (Printexc.to_string exn)
5504 let docopysel r w =
5505 copysel w opaque;
5506 doclose "pipe/r" r;
5507 G.postRedisplay "copysel"
5510 popen conf.selcmd [r, 0; w, -1];
5511 docopysel r w;
5512 with exn ->
5513 dolog "can not exectute %S: %s"
5514 conf.selcmd (Printexc.to_string exn);
5515 doclose "pipe/r" r;
5516 doclose "pipe/w" w;
5518 | None -> ()
5519 else loop rest
5521 loop state.layout;
5522 Wsi.setcursor Wsi.CURSOR_INHERIT;
5523 state.mstate <- Mnone;
5527 | _ -> ()
5530 let birdseyemouse button down x y mask
5531 (conf, leftx, _, hooverpageno, anchor) =
5532 match button with
5533 | 1 when down ->
5534 let rec loop = function
5535 | [] -> ()
5536 | l :: rest ->
5537 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5538 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5539 then (
5540 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5542 else loop rest
5544 loop state.layout
5545 | 3 -> ()
5546 | _ -> viewmouse button down x y mask
5549 let mouse button down x y mask =
5550 state.uioh <- state.uioh#button button down x y mask;
5553 let motion ~x ~y =
5554 state.uioh <- state.uioh#motion x y
5557 let pmotion ~x ~y =
5558 state.uioh <- state.uioh#pmotion x y;
5561 let uioh = object
5562 method display = ()
5564 method key key mask =
5565 begin match state.mode with
5566 | Textentry textentry -> textentrykeyboard key mask textentry
5567 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5568 | View -> viewkeyboard key mask
5569 | LinkNav linknav -> linknavkeyboard key mask linknav
5570 end;
5571 state.uioh
5573 method button button bstate x y mask =
5574 begin match state.mode with
5575 | LinkNav _
5576 | View -> viewmouse button bstate x y mask
5577 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5578 | Textentry _ -> ()
5579 end;
5580 state.uioh
5582 method motion x y =
5583 begin match state.mode with
5584 | Textentry _ -> ()
5585 | View | Birdseye _ | LinkNav _ ->
5586 match state.mstate with
5587 | Mzoom _ | Mnone -> ()
5589 | Mpan (x0, y0) ->
5590 let dx = x - x0
5591 and dy = y0 - y in
5592 state.mstate <- Mpan (x, y);
5593 if canpan ()
5594 then state.x <- state.x + dx;
5595 let y = clamp dy in
5596 gotoy_and_clear_text y
5598 | Msel (a, _) ->
5599 state.mstate <- Msel (a, (x, y));
5600 G.postRedisplay "motion select";
5602 | Mscrolly ->
5603 let y = min conf.winh (max 0 y) in
5604 scrolly y
5606 | Mscrollx ->
5607 let x = min conf.winw (max 0 x) in
5608 scrollx x
5610 | Mzoomrect (p0, _) ->
5611 state.mstate <- Mzoomrect (p0, (x, y));
5612 G.postRedisplay "motion zoomrect";
5613 end;
5614 state.uioh
5616 method pmotion x y =
5617 begin match state.mode with
5618 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5619 let rec loop = function
5620 | [] ->
5621 if hooverpageno != -1
5622 then (
5623 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5624 G.postRedisplay "pmotion birdseye no hoover";
5626 | l :: rest ->
5627 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5628 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5629 then (
5630 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5631 G.postRedisplay "pmotion birdseye hoover";
5633 else loop rest
5635 loop state.layout
5637 | Textentry _ -> ()
5639 | LinkNav _
5640 | View ->
5641 match state.mstate with
5642 | Mnone -> updateunder x y
5643 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5645 end;
5646 state.uioh
5648 method infochanged _ = ()
5650 method scrollph =
5651 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5652 let p, h = scrollph state.y maxy in
5653 state.scrollw, p, h
5655 method scrollpw =
5656 let winw = conf.winw - state.scrollw - 1 in
5657 let fwinw = float winw in
5658 let sw =
5659 let sw = fwinw /. float state.w in
5660 let sw = fwinw *. sw in
5661 max sw (float conf.scrollh)
5663 let position, sw =
5664 let f = state.w+winw in
5665 let r = float (winw-state.x) /. float f in
5666 let p = fwinw *. r in
5667 p-.sw/.2., sw
5669 let sw =
5670 if position +. sw > fwinw
5671 then fwinw -. position
5672 else sw
5674 state.hscrollh, position, sw
5676 method modehash =
5677 let modename =
5678 match state.mode with
5679 | LinkNav _ -> "links"
5680 | Textentry _ -> "textentry"
5681 | Birdseye _ -> "birdseye"
5682 | View -> "global"
5684 findkeyhash conf modename
5685 end;;
5687 module Config =
5688 struct
5689 open Parser
5691 let fontpath = ref "";;
5693 module KeyMap =
5694 Map.Make (struct type t = (int * int) let compare = compare end);;
5696 let unent s =
5697 let l = String.length s in
5698 let b = Buffer.create l in
5699 unent b s 0 l;
5700 Buffer.contents b;
5703 let home =
5704 try Sys.getenv "HOME"
5705 with exn ->
5706 prerr_endline
5707 ("Can not determine home directory location: " ^
5708 Printexc.to_string exn);
5712 let modifier_of_string = function
5713 | "alt" -> Wsi.altmask
5714 | "shift" -> Wsi.shiftmask
5715 | "ctrl" | "control" -> Wsi.ctrlmask
5716 | "meta" -> Wsi.metamask
5717 | _ -> 0
5720 let key_of_string =
5721 let r = Str.regexp "-" in
5722 fun s ->
5723 let elems = Str.full_split r s in
5724 let f n k m =
5725 let g s =
5726 let m1 = modifier_of_string s in
5727 if m1 = 0
5728 then (Wsi.namekey s, m)
5729 else (k, m lor m1)
5730 in function
5731 | Str.Delim s when n land 1 = 0 -> g s
5732 | Str.Text s -> g s
5733 | Str.Delim _ -> (k, m)
5735 let rec loop n k m = function
5736 | [] -> (k, m)
5737 | x :: xs ->
5738 let k, m = f n k m x in
5739 loop (n+1) k m xs
5741 loop 0 0 0 elems
5744 let keys_of_string =
5745 let r = Str.regexp "[ \t]" in
5746 fun s ->
5747 let elems = Str.split r s in
5748 List.map key_of_string elems
5751 let copykeyhashes c =
5752 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5755 let config_of c attrs =
5756 let apply c k v =
5758 match k with
5759 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5760 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5761 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5762 | "preload" -> { c with preload = bool_of_string v }
5763 | "page-bias" -> { c with pagebias = int_of_string v }
5764 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5765 | "auto-scroll-step" ->
5766 { c with autoscrollstep = max 0 (int_of_string v) }
5767 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5768 | "crop-hack" -> { c with crophack = bool_of_string v }
5769 | "throttle" ->
5770 let mw =
5771 match String.lowercase v with
5772 | "true" -> Some infinity
5773 | "false" -> None
5774 | f -> Some (float_of_string f)
5776 { c with maxwait = mw}
5777 | "highlight-links" -> { c with hlinks = bool_of_string v }
5778 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5779 | "vertical-margin" ->
5780 { c with interpagespace = max 0 (int_of_string v) }
5781 | "zoom" ->
5782 let zoom = float_of_string v /. 100. in
5783 let zoom = max zoom 0.0 in
5784 { c with zoom = zoom }
5785 | "presentation" -> { c with presentation = bool_of_string v }
5786 | "rotation-angle" -> { c with angle = int_of_string v }
5787 | "width" -> { c with winw = max 20 (int_of_string v) }
5788 | "height" -> { c with winh = max 20 (int_of_string v) }
5789 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5790 | "proportional-display" -> { c with proportional = bool_of_string v }
5791 | "pixmap-cache-size" ->
5792 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5793 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5794 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5795 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5796 | "persistent-location" -> { c with jumpback = bool_of_string v }
5797 | "background-color" -> { c with bgcolor = color_of_string v }
5798 | "scrollbar-in-presentation" ->
5799 { c with scrollbarinpm = bool_of_string v }
5800 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5801 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5802 | "mupdf-store-size" ->
5803 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5804 | "checkers" -> { c with checkers = bool_of_string v }
5805 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5806 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5807 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5808 | "uri-launcher" -> { c with urilauncher = unent v }
5809 | "path-launcher" -> { c with pathlauncher = unent v }
5810 | "color-space" -> { c with colorspace = colorspace_of_string v }
5811 | "invert-colors" -> { c with invert = bool_of_string v }
5812 | "brightness" -> { c with colorscale = float_of_string v }
5813 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5814 | "ghyllscroll" ->
5815 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5816 | "columns" ->
5817 let (n, _, _) as nab = multicolumns_of_string v in
5818 if n < 0
5819 then { c with columns = Csplit (-n, [||]) }
5820 else { c with columns = Cmulti (nab, [||]) }
5821 | "birds-eye-columns" ->
5822 { c with beyecolumns = Some (max (int_of_string v) 2) }
5823 | "selection-command" -> { c with selcmd = unent v }
5824 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5825 | _ -> c
5826 with exn ->
5827 prerr_endline ("Error processing attribute (`" ^
5828 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5831 let rec fold c = function
5832 | [] -> c
5833 | (k, v) :: rest ->
5834 let c = apply c k v in
5835 fold c rest
5837 fold { c with keyhashes = copykeyhashes c } attrs;
5840 let fromstring f pos n v d =
5841 try f v
5842 with exn ->
5843 dolog "Error processing attribute (%S=%S) at %d\n%s"
5844 n v pos (Printexc.to_string exn)
5849 let bookmark_of attrs =
5850 let rec fold title page rely = function
5851 | ("title", v) :: rest -> fold v page rely rest
5852 | ("page", v) :: rest -> fold title v rely rest
5853 | ("rely", v) :: rest -> fold title page v rest
5854 | _ :: rest -> fold title page rely rest
5855 | [] -> title, page, rely
5857 fold "invalid" "0" "0" attrs
5860 let doc_of attrs =
5861 let rec fold path page rely pan = function
5862 | ("path", v) :: rest -> fold v page rely pan rest
5863 | ("page", v) :: rest -> fold path v rely pan rest
5864 | ("rely", v) :: rest -> fold path page v pan rest
5865 | ("pan", v) :: rest -> fold path page rely v rest
5866 | _ :: rest -> fold path page rely pan rest
5867 | [] -> path, page, rely, pan
5869 fold "" "0" "0" "0" attrs
5872 let map_of attrs =
5873 let rec fold rs ls = function
5874 | ("out", v) :: rest -> fold v ls rest
5875 | ("in", v) :: rest -> fold rs v rest
5876 | _ :: rest -> fold ls rs rest
5877 | [] -> ls, rs
5879 fold "" "" attrs
5882 let setconf dst src =
5883 dst.scrollbw <- src.scrollbw;
5884 dst.scrollh <- src.scrollh;
5885 dst.icase <- src.icase;
5886 dst.preload <- src.preload;
5887 dst.pagebias <- src.pagebias;
5888 dst.verbose <- src.verbose;
5889 dst.scrollstep <- src.scrollstep;
5890 dst.maxhfit <- src.maxhfit;
5891 dst.crophack <- src.crophack;
5892 dst.autoscrollstep <- src.autoscrollstep;
5893 dst.maxwait <- src.maxwait;
5894 dst.hlinks <- src.hlinks;
5895 dst.underinfo <- src.underinfo;
5896 dst.interpagespace <- src.interpagespace;
5897 dst.zoom <- src.zoom;
5898 dst.presentation <- src.presentation;
5899 dst.angle <- src.angle;
5900 dst.winw <- src.winw;
5901 dst.winh <- src.winh;
5902 dst.savebmarks <- src.savebmarks;
5903 dst.memlimit <- src.memlimit;
5904 dst.proportional <- src.proportional;
5905 dst.texcount <- src.texcount;
5906 dst.sliceheight <- src.sliceheight;
5907 dst.thumbw <- src.thumbw;
5908 dst.jumpback <- src.jumpback;
5909 dst.bgcolor <- src.bgcolor;
5910 dst.scrollbarinpm <- src.scrollbarinpm;
5911 dst.tilew <- src.tilew;
5912 dst.tileh <- src.tileh;
5913 dst.mustoresize <- src.mustoresize;
5914 dst.checkers <- src.checkers;
5915 dst.aalevel <- src.aalevel;
5916 dst.trimmargins <- src.trimmargins;
5917 dst.trimfuzz <- src.trimfuzz;
5918 dst.urilauncher <- src.urilauncher;
5919 dst.colorspace <- src.colorspace;
5920 dst.invert <- src.invert;
5921 dst.colorscale <- src.colorscale;
5922 dst.redirectstderr <- src.redirectstderr;
5923 dst.ghyllscroll <- src.ghyllscroll;
5924 dst.columns <- src.columns;
5925 dst.beyecolumns <- src.beyecolumns;
5926 dst.selcmd <- src.selcmd;
5927 dst.updatecurs <- src.updatecurs;
5928 dst.pathlauncher <- src.pathlauncher;
5929 dst.keyhashes <- copykeyhashes src;
5932 let get s =
5933 let h = Hashtbl.create 10 in
5934 let dc = { defconf with angle = defconf.angle } in
5935 let rec toplevel v t spos _ =
5936 match t with
5937 | Vdata | Vcdata | Vend -> v
5938 | Vopen ("llppconfig", _, closed) ->
5939 if closed
5940 then v
5941 else { v with f = llppconfig }
5942 | Vopen _ ->
5943 error "unexpected subelement at top level" s spos
5944 | Vclose _ -> error "unexpected close at top level" s spos
5946 and llppconfig v t spos _ =
5947 match t with
5948 | Vdata | Vcdata -> v
5949 | Vend -> error "unexpected end of input in llppconfig" s spos
5950 | Vopen ("defaults", attrs, closed) ->
5951 let c = config_of dc attrs in
5952 setconf dc c;
5953 if closed
5954 then v
5955 else { v with f = defaults }
5957 | Vopen ("ui-font", attrs, closed) ->
5958 let rec getsize size = function
5959 | [] -> size
5960 | ("size", v) :: rest ->
5961 let size =
5962 fromstring int_of_string spos "size" v fstate.fontsize in
5963 getsize size rest
5964 | l -> getsize size l
5966 fstate.fontsize <- getsize fstate.fontsize attrs;
5967 if closed
5968 then v
5969 else { v with f = uifont (Buffer.create 10) }
5971 | Vopen ("doc", attrs, closed) ->
5972 let pathent, spage, srely, span = doc_of attrs in
5973 let path = unent pathent
5974 and pageno = fromstring int_of_string spos "page" spage 0
5975 and rely = fromstring float_of_string spos "rely" srely 0.0
5976 and pan = fromstring int_of_string spos "pan" span 0 in
5977 let c = config_of dc attrs in
5978 let anchor = (pageno, rely) in
5979 if closed
5980 then (Hashtbl.add h path (c, [], pan, anchor); v)
5981 else { v with f = doc path pan anchor c [] }
5983 | Vopen _ ->
5984 error "unexpected subelement in llppconfig" s spos
5986 | Vclose "llppconfig" -> { v with f = toplevel }
5987 | Vclose _ -> error "unexpected close in llppconfig" s spos
5989 and defaults v t spos _ =
5990 match t with
5991 | Vdata | Vcdata -> v
5992 | Vend -> error "unexpected end of input in defaults" s spos
5993 | Vopen ("keymap", attrs, closed) ->
5994 let modename =
5995 try List.assoc "mode" attrs
5996 with Not_found -> "global" in
5997 if closed
5998 then v
5999 else
6000 let ret keymap =
6001 let h = findkeyhash dc modename in
6002 KeyMap.iter (Hashtbl.replace h) keymap;
6003 defaults
6005 { v with f = pkeymap ret KeyMap.empty }
6007 | Vopen (_, _, _) ->
6008 error "unexpected subelement in defaults" s spos
6010 | Vclose "defaults" ->
6011 { v with f = llppconfig }
6013 | Vclose _ -> error "unexpected close in defaults" s spos
6015 and uifont b v t spos epos =
6016 match t with
6017 | Vdata | Vcdata ->
6018 Buffer.add_substring b s spos (epos - spos);
6020 | Vopen (_, _, _) ->
6021 error "unexpected subelement in ui-font" s spos
6022 | Vclose "ui-font" ->
6023 if String.length !fontpath = 0
6024 then fontpath := Buffer.contents b;
6025 { v with f = llppconfig }
6026 | Vclose _ -> error "unexpected close in ui-font" s spos
6027 | Vend -> error "unexpected end of input in ui-font" s spos
6029 and doc path pan anchor c bookmarks v t spos _ =
6030 match t with
6031 | Vdata | Vcdata -> v
6032 | Vend -> error "unexpected end of input in doc" s spos
6033 | Vopen ("bookmarks", _, closed) ->
6034 if closed
6035 then v
6036 else { v with f = pbookmarks path pan anchor c bookmarks }
6038 | Vopen ("keymap", attrs, closed) ->
6039 let modename =
6040 try List.assoc "mode" attrs
6041 with Not_found -> "global"
6043 if closed
6044 then v
6045 else
6046 let ret keymap =
6047 let h = findkeyhash c modename in
6048 KeyMap.iter (Hashtbl.replace h) keymap;
6049 doc path pan anchor c bookmarks
6051 { v with f = pkeymap ret KeyMap.empty }
6053 | Vopen (_, _, _) ->
6054 error "unexpected subelement in doc" s spos
6056 | Vclose "doc" ->
6057 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6058 { v with f = llppconfig }
6060 | Vclose _ -> error "unexpected close in doc" s spos
6062 and pkeymap ret keymap v t spos _ =
6063 match t with
6064 | Vdata | Vcdata -> v
6065 | Vend -> error "unexpected end of input in keymap" s spos
6066 | Vopen ("map", attrs, closed) ->
6067 let r, l = map_of attrs in
6068 let kss = fromstring keys_of_string spos "in" r [] in
6069 let lss = fromstring keys_of_string spos "out" l [] in
6070 let keymap =
6071 match kss with
6072 | [] -> keymap
6073 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6074 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6076 if closed
6077 then { v with f = pkeymap ret keymap }
6078 else
6079 let f () = v in
6080 { v with f = skip "map" f }
6082 | Vopen _ ->
6083 error "unexpected subelement in keymap" s spos
6085 | Vclose "keymap" ->
6086 { v with f = ret keymap }
6088 | Vclose _ -> error "unexpected close in keymap" s spos
6090 and pbookmarks path pan anchor c bookmarks v t spos _ =
6091 match t with
6092 | Vdata | Vcdata -> v
6093 | Vend -> error "unexpected end of input in bookmarks" s spos
6094 | Vopen ("item", attrs, closed) ->
6095 let titleent, spage, srely = bookmark_of attrs in
6096 let page = fromstring int_of_string spos "page" spage 0
6097 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6098 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6099 if closed
6100 then { v with f = pbookmarks path pan anchor c bookmarks }
6101 else
6102 let f () = v in
6103 { v with f = skip "item" f }
6105 | Vopen _ ->
6106 error "unexpected subelement in bookmarks" s spos
6108 | Vclose "bookmarks" ->
6109 { v with f = doc path pan anchor c bookmarks }
6111 | Vclose _ -> error "unexpected close in bookmarks" s spos
6113 and skip tag f v t spos _ =
6114 match t with
6115 | Vdata | Vcdata -> v
6116 | Vend ->
6117 error ("unexpected end of input in skipped " ^ tag) s spos
6118 | Vopen (tag', _, closed) ->
6119 if closed
6120 then v
6121 else
6122 let f' () = { v with f = skip tag f } in
6123 { v with f = skip tag' f' }
6124 | Vclose ctag ->
6125 if tag = ctag
6126 then f ()
6127 else error ("unexpected close in skipped " ^ tag) s spos
6130 parse { f = toplevel; accu = () } s;
6131 h, dc;
6134 let do_load f ic =
6136 let len = in_channel_length ic in
6137 let s = String.create len in
6138 really_input ic s 0 len;
6139 f s;
6140 with
6141 | Parse_error (msg, s, pos) ->
6142 let subs = subs s pos in
6143 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6144 failwith ("parse error: " ^ s)
6146 | exn ->
6147 failwith ("config load error: " ^ Printexc.to_string exn)
6150 let defconfpath =
6151 let dir =
6153 let dir = Filename.concat home ".config" in
6154 if Sys.is_directory dir then dir else home
6155 with _ -> home
6157 Filename.concat dir "llpp.conf"
6160 let confpath = ref defconfpath;;
6162 let load1 f =
6163 if Sys.file_exists !confpath
6164 then
6165 match
6166 (try Some (open_in_bin !confpath)
6167 with exn ->
6168 prerr_endline
6169 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6170 Printexc.to_string exn);
6171 None
6173 with
6174 | Some ic ->
6175 begin try
6176 f (do_load get ic)
6177 with exn ->
6178 prerr_endline
6179 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6180 Printexc.to_string exn);
6181 end;
6182 close_in ic;
6184 | None -> ()
6185 else
6186 f (Hashtbl.create 0, defconf)
6189 let load () =
6190 let f (h, dc) =
6191 let pc, pb, px, pa =
6193 Hashtbl.find h (Filename.basename state.path)
6194 with Not_found -> dc, [], 0, (0, 0.0)
6196 setconf defconf dc;
6197 setconf conf pc;
6198 state.bookmarks <- pb;
6199 state.x <- px;
6200 state.scrollw <- conf.scrollbw;
6201 if conf.jumpback
6202 then state.anchor <- pa;
6203 cbput state.hists.nav pa;
6205 load1 f
6208 let add_attrs bb always dc c =
6209 let ob s a b =
6210 if always || a != b
6211 then Printf.bprintf bb "\n %s='%b'" s a
6212 and oi s a b =
6213 if always || a != b
6214 then Printf.bprintf bb "\n %s='%d'" s a
6215 and oI s a b =
6216 if always || a != b
6217 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6218 and oz s a b =
6219 if always || a <> b
6220 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6221 and oF s a b =
6222 if always || a <> b
6223 then Printf.bprintf bb "\n %s='%f'" s a
6224 and oc s a b =
6225 if always || a <> b
6226 then
6227 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6228 and oC s a b =
6229 if always || a <> b
6230 then
6231 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6232 and oR s a b =
6233 if always || a <> b
6234 then
6235 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6236 and os s a b =
6237 if always || a <> b
6238 then
6239 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6240 and og s a b =
6241 if always || a <> b
6242 then
6243 match a with
6244 | None -> ()
6245 | Some (_N, _A, _B) ->
6246 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6247 and oW s a b =
6248 if always || a <> b
6249 then
6250 let v =
6251 match a with
6252 | None -> "false"
6253 | Some f ->
6254 if f = infinity
6255 then "true"
6256 else string_of_float f
6258 Printf.bprintf bb "\n %s='%s'" s v
6259 and oco s a b =
6260 if always || a <> b
6261 then
6262 match a with
6263 | Cmulti ((n, a, b), _) when n > 1 ->
6264 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6265 | Csplit (n, _) when n > 1 ->
6266 Printf.bprintf bb "\n %s='%d'" s ~-n
6267 | _ -> ()
6268 and obeco s a b =
6269 if always || a <> b
6270 then
6271 match a with
6272 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6273 | _ -> ()
6275 let w, h =
6276 if always
6277 then dc.winw, dc.winh
6278 else
6279 match state.fullscreen with
6280 | Some wh -> wh
6281 | None -> c.winw, c.winh
6283 let zoom, presentation, interpagespace, maxwait =
6284 if always
6285 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6286 else
6287 match state.mode with
6288 | Birdseye (bc, _, _, _, _) ->
6289 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6290 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6292 oi "width" w dc.winw;
6293 oi "height" h dc.winh;
6294 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6295 oi "scroll-handle-height" c.scrollh dc.scrollh;
6296 ob "case-insensitive-search" c.icase dc.icase;
6297 ob "preload" c.preload dc.preload;
6298 oi "page-bias" c.pagebias dc.pagebias;
6299 oi "scroll-step" c.scrollstep dc.scrollstep;
6300 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6301 ob "max-height-fit" c.maxhfit dc.maxhfit;
6302 ob "crop-hack" c.crophack dc.crophack;
6303 oW "throttle" maxwait dc.maxwait;
6304 ob "highlight-links" c.hlinks dc.hlinks;
6305 ob "under-cursor-info" c.underinfo dc.underinfo;
6306 oi "vertical-margin" interpagespace dc.interpagespace;
6307 oz "zoom" zoom dc.zoom;
6308 ob "presentation" presentation dc.presentation;
6309 oi "rotation-angle" c.angle dc.angle;
6310 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6311 ob "proportional-display" c.proportional dc.proportional;
6312 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6313 oi "tex-count" c.texcount dc.texcount;
6314 oi "slice-height" c.sliceheight dc.sliceheight;
6315 oi "thumbnail-width" c.thumbw dc.thumbw;
6316 ob "persistent-location" c.jumpback dc.jumpback;
6317 oc "background-color" c.bgcolor dc.bgcolor;
6318 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6319 oi "tile-width" c.tilew dc.tilew;
6320 oi "tile-height" c.tileh dc.tileh;
6321 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6322 ob "checkers" c.checkers dc.checkers;
6323 oi "aalevel" c.aalevel dc.aalevel;
6324 ob "trim-margins" c.trimmargins dc.trimmargins;
6325 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6326 os "uri-launcher" c.urilauncher dc.urilauncher;
6327 os "path-launcher" c.pathlauncher dc.pathlauncher;
6328 oC "color-space" c.colorspace dc.colorspace;
6329 ob "invert-colors" c.invert dc.invert;
6330 oF "brightness" c.colorscale dc.colorscale;
6331 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6332 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6333 oco "columns" c.columns dc.columns;
6334 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6335 os "selection-command" c.selcmd dc.selcmd;
6336 ob "update-cursor" c.updatecurs dc.updatecurs;
6339 let keymapsbuf always dc c =
6340 let bb = Buffer.create 16 in
6341 let rec loop = function
6342 | [] -> ()
6343 | (modename, h) :: rest ->
6344 let dh = findkeyhash dc modename in
6345 if always || h <> dh
6346 then (
6347 if Hashtbl.length h > 0
6348 then (
6349 if Buffer.length bb > 0
6350 then Buffer.add_char bb '\n';
6351 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6352 Hashtbl.iter (fun i o ->
6353 let isdifferent = always ||
6355 let dO = Hashtbl.find dh i in
6356 dO <> o
6357 with Not_found -> true
6359 if isdifferent
6360 then
6361 let addkm (k, m) =
6362 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6363 if Wsi.withalt m then Buffer.add_string bb "alt-";
6364 if Wsi.withshift m then Buffer.add_string bb "shift-";
6365 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6366 Buffer.add_string bb (Wsi.keyname k);
6368 let addkms l =
6369 let rec loop = function
6370 | [] -> ()
6371 | km :: [] -> addkm km
6372 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6374 loop l
6376 Buffer.add_string bb "<map in='";
6377 addkm i;
6378 match o with
6379 | KMinsrt km ->
6380 Buffer.add_string bb "' out='";
6381 addkm km;
6382 Buffer.add_string bb "'/>\n"
6384 | KMinsrl kms ->
6385 Buffer.add_string bb "' out='";
6386 addkms kms;
6387 Buffer.add_string bb "'/>\n"
6389 | KMmulti (ins, kms) ->
6390 Buffer.add_char bb ' ';
6391 addkms ins;
6392 Buffer.add_string bb "' out='";
6393 addkms kms;
6394 Buffer.add_string bb "'/>\n"
6395 ) h;
6396 Buffer.add_string bb "</keymap>";
6399 loop rest
6401 loop c.keyhashes;
6405 let save () =
6406 let uifontsize = fstate.fontsize in
6407 let bb = Buffer.create 32768 in
6408 let f (h, dc) =
6409 let dc = if conf.bedefault then conf else dc in
6410 Buffer.add_string bb "<llppconfig>\n";
6412 if String.length !fontpath > 0
6413 then
6414 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6415 uifontsize
6416 !fontpath
6417 else (
6418 if uifontsize <> 14
6419 then
6420 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6423 Buffer.add_string bb "<defaults ";
6424 add_attrs bb true dc dc;
6425 let kb = keymapsbuf true dc dc in
6426 if Buffer.length kb > 0
6427 then (
6428 Buffer.add_string bb ">\n";
6429 Buffer.add_buffer bb kb;
6430 Buffer.add_string bb "\n</defaults>\n";
6432 else Buffer.add_string bb "/>\n";
6434 let adddoc path pan anchor c bookmarks =
6435 if bookmarks == [] && c = dc && anchor = emptyanchor
6436 then ()
6437 else (
6438 Printf.bprintf bb "<doc path='%s'"
6439 (enent path 0 (String.length path));
6441 if anchor <> emptyanchor
6442 then (
6443 let n, y = anchor in
6444 Printf.bprintf bb " page='%d'" n;
6445 if y > 1e-6
6446 then
6447 Printf.bprintf bb " rely='%f'" y
6451 if pan != 0
6452 then Printf.bprintf bb " pan='%d'" pan;
6454 add_attrs bb false dc c;
6455 let kb = keymapsbuf false dc c in
6457 begin match bookmarks with
6458 | [] ->
6459 if Buffer.length kb > 0
6460 then (
6461 Buffer.add_string bb ">\n";
6462 Buffer.add_buffer bb kb;
6463 Buffer.add_string bb "</doc>\n";
6465 else Buffer.add_string bb "/>\n"
6466 | _ ->
6467 Buffer.add_string bb ">\n<bookmarks>\n";
6468 List.iter (fun (title, _level, (page, rely)) ->
6469 Printf.bprintf bb
6470 "<item title='%s' page='%d'"
6471 (enent title 0 (String.length title))
6472 page
6474 if rely > 1e-6
6475 then
6476 Printf.bprintf bb " rely='%f'" rely
6478 Buffer.add_string bb "/>\n";
6479 ) bookmarks;
6480 Buffer.add_string bb "</bookmarks>";
6481 if Buffer.length kb > 0
6482 then (
6483 Buffer.add_string bb "\n";
6484 Buffer.add_buffer bb kb;
6486 Buffer.add_string bb "\n</doc>\n";
6487 end;
6491 let pan, conf =
6492 match state.mode with
6493 | Birdseye (c, pan, _, _, _) ->
6494 let beyecolumns =
6495 match conf.columns with
6496 | Cmulti ((c, _, _), _) -> Some c
6497 | Csingle -> None
6498 | Csplit _ -> None
6499 and columns =
6500 match c.columns with
6501 | Cmulti (c, _) -> Cmulti (c, [||])
6502 | Csingle -> Csingle
6503 | Csplit _ -> failwith "quit from bird's eye while split"
6505 pan, { c with beyecolumns = beyecolumns; columns = columns }
6506 | _ -> state.x, conf
6508 let basename = Filename.basename state.path in
6509 adddoc basename pan (getanchor ())
6510 { conf with
6511 autoscrollstep =
6512 match state.autoscroll with
6513 | Some step -> step
6514 | None -> conf.autoscrollstep }
6515 (if conf.savebmarks then state.bookmarks else []);
6517 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6518 if basename <> path
6519 then adddoc path x y c bookmarks
6520 ) h;
6521 Buffer.add_string bb "</llppconfig>";
6523 load1 f;
6524 if Buffer.length bb > 0
6525 then
6527 let tmp = !confpath ^ ".tmp" in
6528 let oc = open_out_bin tmp in
6529 Buffer.output_buffer oc bb;
6530 close_out oc;
6531 Unix.rename tmp !confpath;
6532 with exn ->
6533 prerr_endline
6534 ("error while saving configuration: " ^ Printexc.to_string exn)
6536 end;;
6538 let () =
6539 Arg.parse
6540 (Arg.align
6541 [("-p", Arg.String (fun s -> state.password <- s) ,
6542 "<password> Set password");
6544 ("-f", Arg.String (fun s -> Config.fontpath := s),
6545 "<path> Set path to the user interface font");
6547 ("-c", Arg.String (fun s -> Config.confpath := s),
6548 "<path> Set path to the configuration file");
6550 ("-v", Arg.Unit (fun () ->
6551 Printf.printf
6552 "%s\nconfiguration path: %s\n"
6553 (version ())
6554 Config.defconfpath
6556 exit 0), " Print version and exit");
6559 (fun s -> state.path <- s)
6560 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6562 if String.length state.path = 0
6563 then (prerr_endline "file name missing"; exit 1);
6565 Config.load ();
6567 let globalkeyhash = findkeyhash conf "global" in
6568 let wsfd, winw, winh = Wsi.init (object
6569 method expose =
6570 if nogeomcmds state.geomcmds
6571 then display ()
6572 else (
6573 GlFunc.draw_buffer `front;
6574 GlClear.color (scalecolor2 conf.bgcolor);
6575 GlClear.clear [`color];
6576 GlFunc.draw_buffer `back;
6578 method display = display ()
6579 method reshape w h = reshape w h
6580 method mouse b d x y m = mouse b d x y m
6581 method motion x y = state.mpos <- (x, y); motion x y
6582 method pmotion x y = state.mpos <- (x, y); pmotion x y
6583 method key k m =
6584 let mascm = m land (
6585 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6586 ) in
6587 match state.keystate with
6588 | KSnone ->
6589 let km = k, mascm in
6590 begin
6591 match
6592 try Hashtbl.find globalkeyhash km
6593 with Not_found ->
6594 let modehash = state.uioh#modehash in
6595 try Hashtbl.find modehash km
6596 with Not_found -> KMinsrt (k, m)
6597 with
6598 | KMinsrt (k, m) -> keyboard k m
6599 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6600 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6602 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6603 List.iter (fun (k, m) -> keyboard k m) insrt;
6604 state.keystate <- KSnone
6605 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6606 state.keystate <- KSinto (keys, insrt)
6607 | _ ->
6608 state.keystate <- KSnone
6610 method enter x y = state.mpos <- (x, y); pmotion x y
6611 method leave = state.mpos <- (-1, -1)
6612 method quit = raise Quit
6613 end) conf.winw conf.winh (platform = Posx) in
6615 state.wsfd <- wsfd;
6617 if not (
6618 List.exists GlMisc.check_extension
6619 [ "GL_ARB_texture_rectangle"
6620 ; "GL_EXT_texture_recangle"
6621 ; "GL_NV_texture_rectangle" ]
6623 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6625 let cr, sw = Unix.pipe ()
6626 and sr, cw = Unix.pipe () in
6628 cloexec cr;
6629 cloexec sw;
6630 cloexec sr;
6631 cloexec cw;
6633 setcheckers conf.checkers;
6634 redirectstderr ();
6636 init (cr, cw) (
6637 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6638 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6639 !Config.fontpath
6641 state.sr <- sr;
6642 state.sw <- sw;
6643 state.text <- "Opening " ^ state.path;
6644 reshape winw winh;
6645 opendoc state.path state.password;
6646 state.uioh <- uioh;
6648 let rec loop deadline =
6649 let r =
6650 match state.errfd with
6651 | None -> [state.sr; state.wsfd]
6652 | Some fd -> [state.sr; state.wsfd; fd]
6654 if state.redisplay
6655 then (
6656 state.redisplay <- false;
6657 display ();
6659 let timeout =
6660 let now = now () in
6661 if deadline > now
6662 then (
6663 if deadline = infinity
6664 then ~-.1.0
6665 else max 0.0 (deadline -. now)
6667 else 0.0
6669 let r, _, _ =
6670 try Unix.select r [] [] timeout
6671 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6673 begin match r with
6674 | [] ->
6675 state.ghyll None;
6676 let newdeadline =
6677 if state.ghyll == noghyll
6678 then
6679 match state.autoscroll with
6680 | Some step when step != 0 ->
6681 let y = state.y + step in
6682 let y =
6683 if y < 0
6684 then state.maxy
6685 else if y >= state.maxy then 0 else y
6687 gotoy y;
6688 if state.mode = View
6689 then state.text <- "";
6690 deadline +. 0.01
6691 | _ -> infinity
6692 else deadline +. 0.01
6694 loop newdeadline
6696 | l ->
6697 let rec checkfds = function
6698 | [] -> ()
6699 | fd :: rest when fd = state.sr ->
6700 let cmd = readcmd state.sr in
6701 act cmd;
6702 checkfds rest
6704 | fd :: rest when fd = state.wsfd ->
6705 Wsi.readresp fd;
6706 checkfds rest
6708 | fd :: rest ->
6709 let s = String.create 80 in
6710 let n = Unix.read fd s 0 80 in
6711 if conf.redirectstderr
6712 then (
6713 Buffer.add_substring state.errmsgs s 0 n;
6714 state.newerrmsgs <- true;
6715 state.redisplay <- true;
6717 else (
6718 prerr_string (String.sub s 0 n);
6719 flush stderr;
6721 checkfds rest
6723 checkfds l;
6724 let newdeadline =
6725 let deadline1 =
6726 if deadline = infinity
6727 then now () +. 0.01
6728 else deadline
6730 match state.autoscroll with
6731 | Some step when step != 0 -> deadline1
6732 | _ -> if state.ghyll == noghyll then infinity else deadline1
6734 loop newdeadline
6735 end;
6738 loop infinity;
6739 with Quit ->
6740 Config.save ();
6741 exit 0;