Thinko
[llpp.git] / main.ml
blob90e472cf4686dbf1374e321e71241aed27ead900
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 undertext = function
792 | Unone -> "none"
793 | Ulinkuri s -> s
794 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
795 | Utext s -> "font: " ^ s
796 | Uunexpected s -> "unexpected: " ^ s
797 | Ulaunch s -> "launch: " ^ s
798 | Unamed s -> "named: " ^ s
799 | Uremote (filename, pageno) ->
800 Printf.sprintf "%s: page %d" filename (pageno+1)
803 let updateunder x y =
804 match getunder x y with
805 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
806 | Ulinkuri uri ->
807 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
808 Wsi.setcursor Wsi.CURSOR_INFO
809 | Ulinkgoto (pageno, _) ->
810 if conf.underinfo
811 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
812 Wsi.setcursor Wsi.CURSOR_INFO
813 | Utext s ->
814 if conf.underinfo then showtext 'f' ("ont: " ^ s);
815 Wsi.setcursor Wsi.CURSOR_TEXT
816 | Uunexpected s ->
817 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
818 Wsi.setcursor Wsi.CURSOR_INHERIT
819 | Ulaunch s ->
820 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
821 Wsi.setcursor Wsi.CURSOR_INHERIT
822 | Unamed s ->
823 if conf.underinfo then showtext 'n' ("amed: " ^ s);
824 Wsi.setcursor Wsi.CURSOR_INHERIT
825 | Uremote (filename, pageno) ->
826 if conf.underinfo then showtext 'r'
827 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
828 Wsi.setcursor Wsi.CURSOR_INFO
831 let showlinktype under =
832 if conf.underinfo
833 then
834 match under with
835 | Unone -> ()
836 | under ->
837 let s = undertext under in
838 showtext ' ' s
841 let addchar s c =
842 let b = Buffer.create (String.length s + 1) in
843 Buffer.add_string b s;
844 Buffer.add_char b c;
845 Buffer.contents b;
848 let colorspace_of_string s =
849 match String.lowercase s with
850 | "rgb" -> Rgb
851 | "bgr" -> Bgr
852 | "gray" -> Gray
853 | _ -> failwith "invalid colorspace"
856 let int_of_colorspace = function
857 | Rgb -> 0
858 | Bgr -> 1
859 | Gray -> 2
862 let colorspace_of_int = function
863 | 0 -> Rgb
864 | 1 -> Bgr
865 | 2 -> Gray
866 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
869 let colorspace_to_string = function
870 | Rgb -> "rgb"
871 | Bgr -> "bgr"
872 | Gray -> "gray"
875 let intentry_with_suffix text key =
876 let c =
877 if key >= 32 && key < 127
878 then Char.chr key
879 else '\000'
881 match Char.lowercase c with
882 | '0' .. '9' ->
883 let text = addchar text c in
884 TEcont text
886 | 'k' | 'm' | 'g' ->
887 let text = addchar text c in
888 TEcont text
890 | _ ->
891 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
892 TEcont text
895 let multicolumns_to_string (n, a, b) =
896 if a = 0 && b = 0
897 then Printf.sprintf "%d" n
898 else Printf.sprintf "%d,%d,%d" n a b;
901 let multicolumns_of_string s =
903 (int_of_string s, 0, 0)
904 with _ ->
905 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
908 let readcmd fd =
909 let s = "xxxx" in
910 let n = Unix.read fd s 0 4 in
911 if n != 4 then failwith "incomplete read(len)";
912 let len = 0
913 lor (Char.code s.[0] lsl 24)
914 lor (Char.code s.[1] lsl 16)
915 lor (Char.code s.[2] lsl 8)
916 lor (Char.code s.[3] lsl 0)
918 let s = String.create len in
919 let n = Unix.read fd s 0 len in
920 if n != len then failwith "incomplete read(data)";
924 let btod b = if b then 1 else 0;;
926 let wcmd fmt =
927 let b = Buffer.create 16 in
928 Buffer.add_string b "llll";
929 Printf.kbprintf
930 (fun b ->
931 let s = Buffer.contents b in
932 let n = String.length s in
933 let len = n - 4 in
934 (* dolog "wcmd %S" (String.sub s 4 len); *)
935 s.[0] <- Char.chr ((len lsr 24) land 0xff);
936 s.[1] <- Char.chr ((len lsr 16) land 0xff);
937 s.[2] <- Char.chr ((len lsr 8) land 0xff);
938 s.[3] <- Char.chr (len land 0xff);
939 let n' = Unix.write state.sw s 0 n in
940 if n' != n then failwith "write failed";
941 ) b fmt;
944 let calcips h =
945 if conf.presentation
946 then
947 let d = conf.winh - h in
948 max 0 ((d + 1) / 2)
949 else
950 conf.interpagespace
953 let calcheight () =
954 let rec f pn ph pi fh l =
955 match l with
956 | (n, _, h, _) :: rest ->
957 let ips = calcips h in
958 let fh =
959 if conf.presentation
960 then fh+ips
961 else (
962 if isbirdseye state.mode && pn = 0
963 then fh + ips
964 else fh
967 let fh = fh + ((n - pn) * (ph + pi)) in
968 f n h ips fh rest;
970 | [] ->
971 let inc =
972 if conf.presentation || (isbirdseye state.mode && pn = 0)
973 then 0
974 else -pi
976 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
977 max 0 fh
979 let fh = f 0 0 0 0 state.pdims in
983 let calcheight () =
984 match conf.columns with
985 | Csingle -> calcheight ()
986 | Cmulti ((c, _, _), b) ->
987 let rec loop y h n =
988 if n < 0
989 then loop y h (n+1)
990 else (
991 if n = Array.length b
992 then y + h
993 else
994 let (_, _, y', (_, _, h', _)) = b.(n) in
995 let y = min y y'
996 and h = max h h' in
997 loop y h (n+1)
1000 loop max_int 0 (((Array.length b - 1) / c) * c)
1001 | Csplit (_, b) ->
1002 if Array.length b > 0
1003 then
1004 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1005 y + h
1006 else 0
1009 let getpageyh pageno =
1010 let rec f pn ph pi y l =
1011 match l with
1012 | (n, _, h, _) :: rest ->
1013 let ips = calcips h in
1014 if n >= pageno
1015 then
1016 let h = if n = pageno then h else ph in
1017 if conf.presentation && n = pageno
1018 then
1019 y + (pageno - pn) * (ph + pi) + pi, h
1020 else
1021 y + (pageno - pn) * (ph + pi), h
1022 else
1023 let y = y + (if conf.presentation then pi else 0) in
1024 let y = y + (n - pn) * (ph + pi) in
1025 f n h ips y rest
1027 | [] ->
1028 y + (pageno - pn) * (ph + pi), ph
1030 f 0 0 0 0 state.pdims
1033 let getpageyh pageno =
1034 match conf.columns with
1035 | Csingle -> getpageyh pageno
1036 | Cmulti (_, b) ->
1037 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1038 y, h
1039 | Csplit (c, b) ->
1040 let n = pageno*c in
1041 let (_, _, y, (_, _, h, _)) = b.(n) in
1042 y, h
1045 let getpagedim pageno =
1046 let rec f ppdim l =
1047 match l with
1048 | (n, _, _, _) as pdim :: rest ->
1049 if n >= pageno
1050 then (if n = pageno then pdim else ppdim)
1051 else f pdim rest
1053 | [] -> ppdim
1055 f (-1, -1, -1, -1) state.pdims
1058 let getpagey pageno = fst (getpageyh pageno);;
1060 let nogeomcmds cmds =
1061 match cmds with
1062 | s, [] -> String.length s = 0
1063 | _ -> false
1066 let layout1 y sh =
1067 let sh = sh - state.hscrollh in
1068 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1069 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1070 match pdims with
1071 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1072 let ips = calcips h in
1073 let yinc =
1074 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1075 then ips
1076 else 0
1078 (w, h, ips, xoff), rest, pdimno + 1, yinc
1079 | _ ->
1080 prev, pdims, pdimno, 0
1082 let dy = dy + yinc in
1083 let py = py + yinc in
1084 if pageno = state.pagecount || dy >= sh
1085 then
1086 accu
1087 else
1088 let vy = y + dy in
1089 if py + h <= vy - yinc
1090 then
1091 let py = py + h + ips in
1092 let dy = max 0 (py - y) in
1093 f ~pageno:(pageno+1)
1094 ~pdimno
1095 ~prev:curr
1098 ~pdims:rest
1099 ~accu
1100 else
1101 let pagey = vy - py in
1102 let pagevh = h - pagey in
1103 let pagevh = min (sh - dy) pagevh in
1104 let off = if yinc > 0 then py - vy else 0 in
1105 let py = py + h + ips in
1106 let pagex, dx =
1107 let xoff = xoff +
1108 if state.w < conf.winw - state.scrollw
1109 then (conf.winw - state.scrollw - state.w) / 2
1110 else 0
1112 let dispx = xoff + state.x in
1113 if dispx < 0
1114 then (-dispx, 0)
1115 else (0, dispx)
1117 let pagevw =
1118 let lw = w - pagex in
1119 min lw (conf.winw - state.scrollw)
1121 let e =
1122 { pageno = pageno
1123 ; pagedimno = pdimno
1124 ; pagew = w
1125 ; pageh = h
1126 ; pagex = pagex
1127 ; pagey = pagey + off
1128 ; pagevw = pagevw
1129 ; pagevh = pagevh - off
1130 ; pagedispx = dx
1131 ; pagedispy = dy + off
1134 let accu = e :: accu in
1135 f ~pageno:(pageno+1)
1136 ~pdimno
1137 ~prev:curr
1139 ~dy:(dy+pagevh+ips)
1140 ~pdims:rest
1141 ~accu
1143 let accu =
1145 ~pageno:0
1146 ~pdimno:~-1
1147 ~prev:(0,0,0,0)
1148 ~py:0
1149 ~dy:0
1150 ~pdims:state.pdims
1151 ~accu:[]
1153 List.rev accu
1156 let layoutN ((columns, coverA, coverB), b) y sh =
1157 let sh = sh - state.hscrollh in
1158 let rec fold accu n =
1159 if n = Array.length b
1160 then accu
1161 else
1162 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1163 if (vy - y) > sh &&
1164 (n = coverA - 1
1165 || n = state.pagecount - coverB
1166 || (n - coverA) mod columns = columns - 1)
1167 then accu
1168 else
1169 let accu =
1170 if vy + h > y
1171 then
1172 let pagey = max 0 (y - vy) in
1173 let pagedispy = if pagey > 0 then 0 else vy - y in
1174 let pagedispx, pagex =
1175 let pdx =
1176 if n = coverA - 1 || n = state.pagecount - coverB
1177 then state.x + (conf.winw - state.scrollw - w) / 2
1178 else dx + xoff + state.x
1180 if pdx < 0
1181 then 0, -pdx
1182 else pdx, 0
1184 let pagevw =
1185 let vw = conf.winw - state.scrollw - pagedispx in
1186 let pw = w - pagex in
1187 min vw pw
1189 let pagevh = min (h - pagey) (sh - pagedispy) in
1190 if pagevw > 0 && pagevh > 0
1191 then
1192 let e =
1193 { pageno = n
1194 ; pagedimno = pdimno
1195 ; pagew = w
1196 ; pageh = h
1197 ; pagex = pagex
1198 ; pagey = pagey
1199 ; pagevw = pagevw
1200 ; pagevh = pagevh
1201 ; pagedispx = pagedispx
1202 ; pagedispy = pagedispy
1205 e :: accu
1206 else
1207 accu
1208 else
1209 accu
1211 fold accu (n+1)
1213 List.rev (fold [] 0)
1216 let layoutS (columns, b) y sh =
1217 let sh = sh - state.hscrollh in
1218 let rec fold accu n =
1219 if n = Array.length b
1220 then accu
1221 else
1222 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1223 if (vy - y) > sh
1224 then accu
1225 else
1226 let accu =
1227 if vy + pageh > y
1228 then
1229 let x = xoff + state.x in
1230 let pagey = max 0 (y - vy) in
1231 let pagedispy = if pagey > 0 then 0 else vy - y in
1232 let pagedispx, pagex =
1233 if px = 0
1234 then (
1235 if x < 0
1236 then 0, -x
1237 else x, 0
1239 else (
1240 let px = px - x in
1241 if px < 0
1242 then -px, 0
1243 else 0, px
1246 let pagevw =
1247 let vw = conf.winw - pagedispx - state.scrollw in
1248 let pw = pagew - pagex in
1249 min vw pw
1251 let pagevw = min pagevw (pagew/columns) in
1252 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1253 if pagevw > 0 && pagevh > 0
1254 then
1255 let e =
1256 { pageno = n/columns
1257 ; pagedimno = pdimno
1258 ; pagew = pagew
1259 ; pageh = pageh
1260 ; pagex = pagex
1261 ; pagey = pagey
1262 ; pagevw = pagevw
1263 ; pagevh = pagevh
1264 ; pagedispx = pagedispx
1265 ; pagedispy = pagedispy
1268 e :: accu
1269 else
1270 accu
1271 else
1272 accu
1274 fold accu (n+1)
1276 List.rev (fold [] 0)
1279 let layout y sh =
1280 if nogeomcmds state.geomcmds
1281 then
1282 match conf.columns with
1283 | Csingle -> layout1 y sh
1284 | Cmulti c -> layoutN c y sh
1285 | Csplit s -> layoutS s y sh
1286 else []
1289 let clamp incr =
1290 let y = state.y + incr in
1291 let y = max 0 y in
1292 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1296 let itertiles l f =
1297 let tilex = l.pagex mod conf.tilew in
1298 let tiley = l.pagey mod conf.tileh in
1300 let col = l.pagex / conf.tilew in
1301 let row = l.pagey / conf.tileh in
1303 let rec rowloop row y0 dispy h =
1304 if h = 0
1305 then ()
1306 else (
1307 let dh = conf.tileh - y0 in
1308 let dh = min h dh in
1309 let rec colloop col x0 dispx w =
1310 if w = 0
1311 then ()
1312 else (
1313 let dw = conf.tilew - x0 in
1314 let dw = min w dw in
1316 f col row dispx dispy x0 y0 dw dh;
1317 colloop (col+1) 0 (dispx+dw) (w-dw)
1320 colloop col tilex l.pagedispx l.pagevw;
1321 rowloop (row+1) 0 (dispy+dh) (h-dh)
1324 if l.pagevw > 0 && l.pagevh > 0
1325 then rowloop row tiley l.pagedispy l.pagevh;
1328 let gettileopaque l col row =
1329 let key =
1330 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1332 try Some (Hashtbl.find state.tilemap key)
1333 with Not_found -> None
1336 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1337 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1338 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1341 let drawtiles l color =
1342 GlDraw.color color;
1343 let f col row x y tilex tiley w h =
1344 match gettileopaque l col row with
1345 | Some (opaque, _, t) ->
1346 let params = x, y, w, h, tilex, tiley in
1347 if conf.invert
1348 then (
1349 Gl.enable `blend;
1350 GlFunc.blend_func `zero `one_minus_src_color;
1352 drawtile params opaque;
1353 if conf.invert
1354 then Gl.disable `blend;
1355 if conf.debug
1356 then (
1357 let s = Printf.sprintf
1358 "%d[%d,%d] %f sec"
1359 l.pageno col row t
1361 let w = measurestr fstate.fontsize s in
1362 GlMisc.push_attrib [`current];
1363 GlDraw.color (0.0, 0.0, 0.0);
1364 GlDraw.rect
1365 (float (x-2), float (y-2))
1366 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1367 GlDraw.color (1.0, 1.0, 1.0);
1368 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1369 GlMisc.pop_attrib ();
1372 | _ ->
1373 let w =
1374 let lw = conf.winw - state.scrollw - x in
1375 min lw w
1376 and h =
1377 let lh = conf.winh - y in
1378 min lh h
1380 Gl.enable `texture_2d;
1381 begin match state.texid with
1382 | Some id ->
1383 GlTex.bind_texture `texture_2d id;
1384 let x0 = float x
1385 and y0 = float y
1386 and x1 = float (x+w)
1387 and y1 = float (y+h) in
1389 let tw = float w /. 64.0
1390 and th = float h /. 64.0 in
1391 let tx0 = float tilex /. 64.0
1392 and ty0 = float tiley /. 64.0 in
1393 let tx1 = tx0 +. tw
1394 and ty1 = ty0 +. th in
1395 GlDraw.begins `quads;
1396 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1397 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1398 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1399 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1400 GlDraw.ends ();
1402 Gl.disable `texture_2d;
1403 | None ->
1404 GlDraw.color (1.0, 1.0, 1.0);
1405 GlDraw.rect
1406 (float x, float y)
1407 (float (x+w), float (y+h));
1408 end;
1409 if w > 128 && h > fstate.fontsize + 10
1410 then (
1411 GlDraw.color (0.0, 0.0, 0.0);
1412 let c, r =
1413 if conf.verbose
1414 then (col*conf.tilew, row*conf.tileh)
1415 else col, row
1417 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1419 GlDraw.color color;
1421 itertiles l f
1424 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1426 let tilevisible1 l x y =
1427 let ax0 = l.pagex
1428 and ax1 = l.pagex + l.pagevw
1429 and ay0 = l.pagey
1430 and ay1 = l.pagey + l.pagevh in
1432 let bx0 = x
1433 and by0 = y in
1434 let bx1 = min (bx0 + conf.tilew) l.pagew
1435 and by1 = min (by0 + conf.tileh) l.pageh in
1437 let rx0 = max ax0 bx0
1438 and ry0 = max ay0 by0
1439 and rx1 = min ax1 bx1
1440 and ry1 = min ay1 by1 in
1442 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1443 nonemptyintersection
1446 let tilevisible layout n x y =
1447 let rec findpageinlayout m = function
1448 | l :: rest when l.pageno = n ->
1449 tilevisible1 l x y || (
1450 match conf.columns with
1451 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1452 | _ -> false
1454 | _ :: rest -> findpageinlayout 0 rest
1455 | [] -> false
1457 findpageinlayout 0 layout;
1460 let tileready l x y =
1461 tilevisible1 l x y &&
1462 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1465 let tilepage n p layout =
1466 let rec loop = function
1467 | l :: rest ->
1468 if l.pageno = n
1469 then
1470 let f col row _ _ _ _ _ _ =
1471 if state.currently = Idle
1472 then
1473 match gettileopaque l col row with
1474 | Some _ -> ()
1475 | None ->
1476 let x = col*conf.tilew
1477 and y = row*conf.tileh in
1478 let w =
1479 let w = l.pagew - x in
1480 min w conf.tilew
1482 let h =
1483 let h = l.pageh - y in
1484 min h conf.tileh
1486 wcmd "tile %s %d %d %d %d" p x y w h;
1487 state.currently <-
1488 Tiling (
1489 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1490 conf.tilew, conf.tileh
1493 itertiles l f;
1494 else
1495 loop rest
1497 | [] -> ()
1499 if nogeomcmds state.geomcmds
1500 then loop layout;
1503 let preloadlayout visiblepages =
1504 let presentation = conf.presentation in
1505 let interpagespace = conf.interpagespace in
1506 let maxy = state.maxy in
1507 conf.presentation <- false;
1508 conf.interpagespace <- 0;
1509 state.maxy <- calcheight ();
1510 let y =
1511 match visiblepages with
1512 | [] -> 0
1513 | l :: _ -> getpagey l.pageno + l.pagey
1515 let y = if y < conf.winh then 0 else y - conf.winh in
1516 let h = state.y - y + conf.winh*3 in
1517 let pages = layout y h in
1518 conf.presentation <- presentation;
1519 conf.interpagespace <- interpagespace;
1520 state.maxy <- maxy;
1521 pages;
1524 let load pages =
1525 let rec loop pages =
1526 if state.currently != Idle
1527 then ()
1528 else
1529 match pages with
1530 | l :: rest ->
1531 begin match getopaque l.pageno with
1532 | None ->
1533 wcmd "page %d %d" l.pageno l.pagedimno;
1534 state.currently <- Loading (l, state.gen);
1535 | Some opaque ->
1536 tilepage l.pageno opaque pages;
1537 loop rest
1538 end;
1539 | _ -> ()
1541 if nogeomcmds state.geomcmds
1542 then loop pages
1545 let preload pages =
1546 load pages;
1547 if conf.preload && state.currently = Idle
1548 then load (preloadlayout pages);
1551 let layoutready layout =
1552 let rec fold all ls =
1553 all && match ls with
1554 | l :: rest ->
1555 let seen = ref false in
1556 let allvisible = ref true in
1557 let foo col row _ _ _ _ _ _ =
1558 seen := true;
1559 allvisible := !allvisible &&
1560 begin match gettileopaque l col row with
1561 | Some _ -> true
1562 | None -> false
1565 itertiles l foo;
1566 fold (!seen && !allvisible) rest
1567 | [] -> true
1569 let alltilesvisible = fold true layout in
1570 alltilesvisible;
1573 let gotoy y =
1574 let y = bound y 0 state.maxy in
1575 let y, layout, proceed =
1576 match conf.maxwait with
1577 | Some time when state.ghyll == noghyll ->
1578 begin match state.throttle with
1579 | None ->
1580 let layout = layout y conf.winh in
1581 let ready = layoutready layout in
1582 if not ready
1583 then (
1584 load layout;
1585 state.throttle <- Some (layout, y, now ());
1587 else G.postRedisplay "gotoy showall (None)";
1588 y, layout, ready
1589 | Some (_, _, started) ->
1590 let dt = now () -. started in
1591 if dt > time
1592 then (
1593 state.throttle <- None;
1594 let layout = layout y conf.winh in
1595 load layout;
1596 G.postRedisplay "maxwait";
1597 y, layout, true
1599 else -1, [], false
1602 | _ ->
1603 let layout = layout y conf.winh in
1604 if true || layoutready layout
1605 then G.postRedisplay "gotoy ready";
1606 y, layout, true
1608 if proceed
1609 then (
1610 state.y <- y;
1611 state.layout <- layout;
1612 begin match state.mode with
1613 | LinkNav (Ltexact (pageno, linkno)) ->
1614 let rec loop = function
1615 | [] ->
1616 state.mode <- LinkNav (Ltgendir 0)
1617 | l :: _ when l.pageno = pageno ->
1618 begin match getopaque pageno with
1619 | None ->
1620 state.mode <- LinkNav (Ltgendir 0)
1621 | Some opaque ->
1622 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1623 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1624 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1625 then state.mode <- LinkNav (Ltgendir 0)
1627 | _ :: rest -> loop rest
1629 loop layout
1630 | _ -> ()
1631 end;
1632 begin match state.mode with
1633 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1634 if not (pagevisible layout pageno)
1635 then (
1636 match state.layout with
1637 | [] -> ()
1638 | l :: _ ->
1639 state.mode <- Birdseye (
1640 conf, leftx, l.pageno, hooverpageno, anchor
1643 | LinkNav (Ltgendir dir as lt) ->
1644 let linknav =
1645 let rec loop = function
1646 | [] -> lt
1647 | l :: rest ->
1648 match getopaque l.pageno with
1649 | None -> loop rest
1650 | Some opaque ->
1651 let link =
1652 let ld =
1653 if dir = 0
1654 then LDfirstvisible (l.pagex, l.pagey, dir)
1655 else (
1656 if dir > 0 then LDfirst else LDlast
1659 findlink opaque ld
1661 match link with
1662 | Lnotfound -> loop rest
1663 | Lfound n ->
1664 showlinktype (getlink opaque n);
1665 Ltexact (l.pageno, n)
1667 loop state.layout
1669 state.mode <- LinkNav linknav
1670 | _ -> ()
1671 end;
1672 preload layout;
1674 state.ghyll <- noghyll;
1675 if conf.updatecurs
1676 then (
1677 let mx, my = state.mpos in
1678 updateunder mx my;
1682 let conttiling pageno opaque =
1683 tilepage pageno opaque
1684 (if conf.preload then preloadlayout state.layout else state.layout)
1687 let gotoy_and_clear_text y =
1688 if not conf.verbose then state.text <- "";
1689 gotoy y;
1692 let getanchor () =
1693 match state.layout with
1694 | [] -> emptyanchor
1695 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1698 let getanchory (n, top) =
1699 let y, h = getpageyh n in
1700 y + (truncate (top *. float h));
1703 let gotoanchor anchor =
1704 gotoy (getanchory anchor);
1707 let addnav () =
1708 cbput state.hists.nav (getanchor ());
1711 let getnav dir =
1712 let anchor = cbgetc state.hists.nav dir in
1713 getanchory anchor;
1716 let gotoghyll y =
1717 let rec scroll f n a b =
1718 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1719 let snake f a b =
1720 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1721 if f < a
1722 then s (float f /. float a)
1723 else (
1724 if f > b
1725 then 1.0 -. s ((float (f-b) /. float (n-b)))
1726 else 1.0
1729 snake f a b
1730 and summa f n a b =
1731 (* courtesy:
1732 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1733 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1734 let iv1 = iv f in
1735 let ins = float a *. iv1
1736 and outs = float (n-b) *. iv1 in
1737 let ones = b - a in
1738 ins +. outs +. float ones
1740 let rec set (_N, _A, _B) y sy =
1741 let sum = summa 1.0 _N _A _B in
1742 let dy = float (y - sy) in
1743 state.ghyll <- (
1744 let rec gf n y1 o =
1745 if n >= _N
1746 then state.ghyll <- noghyll
1747 else
1748 let go n =
1749 let s = scroll n _N _A _B in
1750 let y1 = y1 +. ((s *. dy) /. sum) in
1751 gotoy_and_clear_text (truncate y1);
1752 state.ghyll <- gf (n+1) y1;
1754 match o with
1755 | None -> go n
1756 | Some y' -> set (_N/2, 0, 0) y' state.y
1758 gf 0 (float state.y)
1761 match conf.ghyllscroll with
1762 | None ->
1763 gotoy_and_clear_text y
1764 | Some nab ->
1765 if state.ghyll == noghyll
1766 then set nab y state.y
1767 else state.ghyll (Some y)
1770 let gotopage n top =
1771 let y, h = getpageyh n in
1772 let y = y + (truncate (top *. float h)) in
1773 gotoghyll y
1776 let gotopage1 n top =
1777 let y = getpagey n in
1778 let y = y + top in
1779 gotoghyll y
1782 let invalidate s f =
1783 state.layout <- [];
1784 state.pdims <- [];
1785 state.rects <- [];
1786 state.rects1 <- [];
1787 match state.geomcmds with
1788 | ps, [] when String.length ps = 0 ->
1789 f ();
1790 state.geomcmds <- s, [];
1792 | ps, [] ->
1793 state.geomcmds <- ps, [s, f];
1795 | ps, (s', _) :: rest when s' = s ->
1796 state.geomcmds <- ps, ((s, f) :: rest);
1798 | ps, cmds ->
1799 state.geomcmds <- ps, ((s, f) :: cmds);
1802 let opendoc path password =
1803 state.path <- path;
1804 state.password <- password;
1805 state.gen <- state.gen + 1;
1806 state.docinfo <- [];
1808 setaalevel conf.aalevel;
1809 Wsi.settitle ("llpp " ^ Filename.basename path);
1810 wcmd "open %s\000%s\000" path password;
1811 invalidate "reqlayout"
1812 (fun () ->
1813 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1816 let scalecolor c =
1817 let c = c *. conf.colorscale in
1818 (c, c, c);
1821 let scalecolor2 (r, g, b) =
1822 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1825 let represent () =
1826 let docolumns = function
1827 | Csingle -> ()
1829 | Cmulti ((columns, coverA, coverB), _) ->
1830 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1831 let rec loop pageno pdimno pdim x y rowh pdims =
1832 let rec fixrow m = if m = pageno then () else
1833 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1834 if h < rowh
1835 then (
1836 let y = y + (rowh - h) / 2 in
1837 a.(m) <- (pdimno, x, y, pdim);
1839 fixrow (m+1)
1841 if pageno = state.pagecount
1842 then fixrow (((pageno - 1) / columns) * columns)
1843 else
1844 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1845 match pdims with
1846 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1847 pdimno+1, pdim, rest
1848 | _ ->
1849 pdimno, pdim, pdims
1851 let x, y, rowh' =
1852 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1853 then (
1854 (conf.winw - state.scrollw - w) / 2,
1855 y + rowh + conf.interpagespace, h
1857 else (
1858 if (pageno - coverA) mod columns = 0
1859 then 0, y + rowh + conf.interpagespace, h
1860 else x, y, max rowh h
1863 if pageno > 1 && (pageno - coverA) mod columns = 0
1864 then fixrow (pageno - columns);
1865 a.(pageno) <- (pdimno, x, y, pdim);
1866 let x = x + w + xoff*2 + conf.interpagespace in
1867 loop (pageno+1) pdimno pdim x y rowh' pdims
1869 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1870 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1872 | Csplit (c, _) ->
1873 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1874 let rec loop pageno pdimno pdim y pdims =
1875 if pageno = state.pagecount
1876 then ()
1877 else
1878 let pdimno, ((_, w, h, _) as pdim), pdims =
1879 match pdims with
1880 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1881 pdimno+1, pdim, rest
1882 | _ ->
1883 pdimno, pdim, pdims
1885 let cw = w / c in
1886 let rec loop1 n x y =
1887 if n = c then y else (
1888 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1889 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1892 let y = loop1 0 0 y in
1893 loop (pageno+1) pdimno pdim y pdims
1895 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1896 conf.columns <- Csplit (c, a);
1898 docolumns conf.columns;
1899 state.maxy <- calcheight ();
1900 state.hscrollh <-
1901 if state.w <= conf.winw - state.scrollw
1902 then 0
1903 else state.scrollw
1905 match state.mode with
1906 | Birdseye (_, _, pageno, _, _) ->
1907 let y, h = getpageyh pageno in
1908 let top = (conf.winh - h) / 2 in
1909 gotoy (max 0 (y - top))
1910 | _ -> gotoanchor state.anchor
1913 let reshape w h =
1914 GlDraw.viewport 0 0 w h;
1915 let firsttime = state.geomcmds == firstgeomcmds in
1916 if not firsttime && nogeomcmds state.geomcmds
1917 then state.anchor <- getanchor ();
1919 conf.winw <- w;
1920 let w = truncate (float w *. conf.zoom) - state.scrollw in
1921 let w = max w 2 in
1922 conf.winh <- h;
1923 setfontsize fstate.fontsize;
1924 GlMat.mode `modelview;
1925 GlMat.load_identity ();
1927 GlMat.mode `projection;
1928 GlMat.load_identity ();
1929 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1930 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1931 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1933 let relx =
1934 if conf.zoom <= 1.0
1935 then 0.0
1936 else float state.x /. float state.w
1938 invalidate "geometry"
1939 (fun () ->
1940 state.w <- w;
1941 if not firsttime
1942 then state.x <- truncate (relx *. float w);
1943 let w =
1944 match conf.columns with
1945 | Csingle -> w
1946 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1947 | Csplit (c, _) -> w * c
1949 wcmd "geometry %d %d" w h);
1952 let enttext () =
1953 let len = String.length state.text in
1954 let drawstring s =
1955 let hscrollh =
1956 match state.mode with
1957 | Textentry _
1958 | View -> state.hscrollh
1959 | _ -> 0
1961 let rect x w =
1962 GlDraw.rect
1963 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1964 (x+.w, float (conf.winh - hscrollh))
1967 let w = float (conf.winw - state.scrollw - 1) in
1968 if state.progress >= 0.0 && state.progress < 1.0
1969 then (
1970 GlDraw.color (0.3, 0.3, 0.3);
1971 let w1 = w *. state.progress in
1972 rect 0.0 w1;
1973 GlDraw.color (0.0, 0.0, 0.0);
1974 rect w1 (w-.w1)
1976 else (
1977 GlDraw.color (0.0, 0.0, 0.0);
1978 rect 0.0 w;
1981 GlDraw.color (1.0, 1.0, 1.0);
1982 drawstring fstate.fontsize
1983 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1985 let s =
1986 match state.mode with
1987 | Textentry ((prefix, text, _, _, _), _) ->
1988 let s =
1989 if len > 0
1990 then
1991 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1992 else
1993 Printf.sprintf "%s%s_" prefix text
1997 | _ -> state.text
1999 let s =
2000 if state.newerrmsgs
2001 then (
2002 if not (istextentry state.mode)
2003 then
2004 let s1 = "(press 'e' to review error messasges)" in
2005 if String.length s > 0 then s ^ " " ^ s1 else s1
2006 else s
2008 else s
2010 if String.length s > 0
2011 then drawstring s
2014 let gctiles () =
2015 let len = Queue.length state.tilelru in
2016 let rec loop qpos =
2017 if state.memused <= conf.memlimit
2018 then ()
2019 else (
2020 if qpos < len
2021 then
2022 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2023 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2024 let (_, pw, ph, _) = getpagedim n in
2026 gen = state.gen
2027 && colorspace = conf.colorspace
2028 && angle = conf.angle
2029 && pagew = pw
2030 && pageh = ph
2031 && (
2032 let layout =
2033 match state.throttle with
2034 | None ->
2035 if conf.preload
2036 then preloadlayout state.layout
2037 else state.layout
2038 | Some (layout, _, _) ->
2039 layout
2041 let x = col*conf.tilew
2042 and y = row*conf.tileh in
2043 tilevisible layout n x y
2045 then Queue.push lruitem state.tilelru
2046 else (
2047 wcmd "freetile %s" p;
2048 state.memused <- state.memused - s;
2049 state.uioh#infochanged Memused;
2050 Hashtbl.remove state.tilemap k;
2052 loop (qpos+1)
2055 loop 0
2058 let flushtiles () =
2059 Queue.iter (fun (k, p, s) ->
2060 wcmd "freetile %s" p;
2061 state.memused <- state.memused - s;
2062 state.uioh#infochanged Memused;
2063 Hashtbl.remove state.tilemap k;
2064 ) state.tilelru;
2065 Queue.clear state.tilelru;
2066 load state.layout;
2069 let logcurrently = function
2070 | Idle -> dolog "Idle"
2071 | Loading (l, gen) ->
2072 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2073 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2074 dolog
2075 "Tiling %d[%d,%d] page=%s cs=%s angle"
2076 l.pageno col row pageopaque
2077 (colorspace_to_string colorspace)
2079 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2080 angle gen conf.angle state.gen
2081 tilew tileh
2082 conf.tilew conf.tileh
2084 | Outlining _ ->
2085 dolog "outlining"
2088 let act cmds =
2089 (* dolog "%S" cmds; *)
2090 let op, args =
2091 let spacepos =
2092 try String.index cmds ' '
2093 with Not_found -> -1
2095 if spacepos = -1
2096 then cmds, ""
2097 else
2098 let l = String.length cmds in
2099 let op = String.sub cmds 0 spacepos in
2100 op, begin
2101 if l - spacepos < 2 then ""
2102 else String.sub cmds (spacepos+1) (l-spacepos-1)
2105 match op with
2106 | "clear" ->
2107 state.uioh#infochanged Pdim;
2108 state.pdims <- [];
2110 | "clearrects" ->
2111 state.rects <- state.rects1;
2112 G.postRedisplay "clearrects";
2114 | "continue" ->
2115 let n =
2116 try Scanf.sscanf args "%u" (fun n -> n)
2117 with exn ->
2118 dolog "error processing 'continue' %S: %s"
2119 cmds (Printexc.to_string exn);
2120 exit 1;
2122 state.pagecount <- n;
2123 begin match state.currently with
2124 | Outlining l ->
2125 state.currently <- Idle;
2126 state.outlines <- Array.of_list (List.rev l)
2127 | _ -> ()
2128 end;
2130 let cur, cmds = state.geomcmds in
2131 if String.length cur = 0
2132 then failwith "umpossible";
2134 begin match List.rev cmds with
2135 | [] ->
2136 state.geomcmds <- "", [];
2137 represent ();
2138 | (s, f) :: rest ->
2139 f ();
2140 state.geomcmds <- s, List.rev rest;
2141 end;
2142 if conf.maxwait = None
2143 then G.postRedisplay "continue";
2145 | "title" ->
2146 Wsi.settitle args
2148 | "msg" ->
2149 showtext ' ' args
2151 | "vmsg" ->
2152 if conf.verbose
2153 then showtext ' ' args
2155 | "progress" ->
2156 let progress, text =
2158 Scanf.sscanf args "%f %n"
2159 (fun f pos ->
2160 f, String.sub args pos (String.length args - pos))
2161 with exn ->
2162 dolog "error processing 'progress' %S: %s"
2163 cmds (Printexc.to_string exn);
2164 exit 1;
2166 state.text <- text;
2167 state.progress <- progress;
2168 G.postRedisplay "progress"
2170 | "firstmatch" ->
2171 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2173 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2174 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2175 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2176 with exn ->
2177 dolog "error processing 'firstmatch' %S: %s"
2178 cmds (Printexc.to_string exn);
2179 exit 1;
2181 let y = (getpagey pageno) + truncate y0 in
2182 addnav ();
2183 gotoy y;
2184 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2186 | "match" ->
2187 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2189 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2190 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2191 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2192 with exn ->
2193 dolog "error processing 'match' %S: %s"
2194 cmds (Printexc.to_string exn);
2195 exit 1;
2197 state.rects1 <-
2198 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2200 | "page" ->
2201 let pageopaque, t =
2203 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2204 with exn ->
2205 dolog "error processing 'page' %S: %s"
2206 cmds (Printexc.to_string exn);
2207 exit 1;
2209 begin match state.currently with
2210 | Loading (l, gen) ->
2211 vlog "page %d took %f sec" l.pageno t;
2212 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2213 begin match state.throttle with
2214 | None ->
2215 let preloadedpages =
2216 if conf.preload
2217 then preloadlayout state.layout
2218 else state.layout
2220 let evict () =
2221 let module IntSet =
2222 Set.Make (struct type t = int let compare = (-) end) in
2223 let set =
2224 List.fold_left (fun s l -> IntSet.add l.pageno s)
2225 IntSet.empty preloadedpages
2227 let evictedpages =
2228 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2229 if not (IntSet.mem pageno set)
2230 then (
2231 wcmd "freepage %s" opaque;
2232 key :: accu
2234 else accu
2235 ) state.pagemap []
2237 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2239 evict ();
2240 state.currently <- Idle;
2241 if gen = state.gen
2242 then (
2243 tilepage l.pageno pageopaque state.layout;
2244 load state.layout;
2245 load preloadedpages;
2246 if pagevisible state.layout l.pageno
2247 && layoutready state.layout
2248 then G.postRedisplay "page";
2251 | Some (layout, _, _) ->
2252 state.currently <- Idle;
2253 tilepage l.pageno pageopaque layout;
2254 load state.layout
2255 end;
2257 | _ ->
2258 dolog "Inconsistent loading state";
2259 logcurrently state.currently;
2260 exit 1
2263 | "tile" ->
2264 let (x, y, opaque, size, t) =
2266 Scanf.sscanf args "%u %u %s %u %f"
2267 (fun x y p size t -> (x, y, p, size, t))
2268 with exn ->
2269 dolog "error processing 'tile' %S: %s"
2270 cmds (Printexc.to_string exn);
2271 exit 1;
2273 begin match state.currently with
2274 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2275 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2277 if tilew != conf.tilew || tileh != conf.tileh
2278 then (
2279 wcmd "freetile %s" opaque;
2280 state.currently <- Idle;
2281 load state.layout;
2283 else (
2284 puttileopaque l col row gen cs angle opaque size t;
2285 state.memused <- state.memused + size;
2286 state.uioh#infochanged Memused;
2287 gctiles ();
2288 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2289 opaque, size) state.tilelru;
2291 let layout =
2292 match state.throttle with
2293 | None -> state.layout
2294 | Some (layout, _, _) -> layout
2297 state.currently <- Idle;
2298 if gen = state.gen
2299 && conf.colorspace = cs
2300 && conf.angle = angle
2301 && tilevisible layout l.pageno x y
2302 then conttiling l.pageno pageopaque;
2304 begin match state.throttle with
2305 | None ->
2306 preload state.layout;
2307 if gen = state.gen
2308 && conf.colorspace = cs
2309 && conf.angle = angle
2310 && tilevisible state.layout l.pageno x y
2311 then G.postRedisplay "tile nothrottle";
2313 | Some (layout, y, _) ->
2314 let ready = layoutready layout in
2315 if ready
2316 then (
2317 state.y <- y;
2318 state.layout <- layout;
2319 state.throttle <- None;
2320 G.postRedisplay "throttle";
2322 else load layout;
2323 end;
2326 | _ ->
2327 dolog "Inconsistent tiling state";
2328 logcurrently state.currently;
2329 exit 1
2332 | "pdim" ->
2333 let pdim =
2335 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2336 with exn ->
2337 dolog "error processing 'pdim' %S: %s"
2338 cmds (Printexc.to_string exn);
2339 exit 1;
2341 state.uioh#infochanged Pdim;
2342 state.pdims <- pdim :: state.pdims
2344 | "o" ->
2345 let (l, n, t, h, pos) =
2347 Scanf.sscanf args "%u %u %d %u %n"
2348 (fun l n t h pos -> l, n, t, h, pos)
2349 with exn ->
2350 dolog "error processing 'o' %S: %s"
2351 cmds (Printexc.to_string exn);
2352 exit 1;
2354 let s = String.sub args pos (String.length args - pos) in
2355 let outline = (s, l, (n, float t /. float h)) in
2356 begin match state.currently with
2357 | Outlining outlines ->
2358 state.currently <- Outlining (outline :: outlines)
2359 | Idle ->
2360 state.currently <- Outlining [outline]
2361 | currently ->
2362 dolog "invalid outlining state";
2363 logcurrently currently
2366 | "info" ->
2367 state.docinfo <- (1, args) :: state.docinfo
2369 | "infoend" ->
2370 state.uioh#infochanged Docinfo;
2371 state.docinfo <- List.rev state.docinfo
2373 | _ ->
2374 dolog "unknown cmd `%S'" cmds
2377 let onhist cb =
2378 let rc = cb.rc in
2379 let action = function
2380 | HCprev -> cbget cb ~-1
2381 | HCnext -> cbget cb 1
2382 | HCfirst -> cbget cb ~-(cb.rc)
2383 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2384 and cancel () = cb.rc <- rc
2385 in (action, cancel)
2388 let search pattern forward =
2389 if String.length pattern > 0
2390 then
2391 let pn, py =
2392 match state.layout with
2393 | [] -> 0, 0
2394 | l :: _ ->
2395 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2397 wcmd "search %d %d %d %d,%s\000"
2398 (btod conf.icase) pn py (btod forward) pattern;
2401 let intentry text key =
2402 let c =
2403 if key >= 32 && key < 127
2404 then Char.chr key
2405 else '\000'
2407 match c with
2408 | '0' .. '9' ->
2409 let text = addchar text c in
2410 TEcont text
2412 | _ ->
2413 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2414 TEcont text
2417 let linknentry text key =
2418 let c =
2419 if key >= 32 && key < 127
2420 then Char.chr key
2421 else '\000'
2423 match c with
2424 | 'a' .. 'z' ->
2425 let text = addchar text c in
2426 TEcont text
2428 | _ ->
2429 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2430 TEcont text
2433 let linkndone f s =
2434 if String.length s > 0
2435 then (
2436 let n =
2437 let rec loop pos n = if pos = String.length s then n else
2438 let m = Char.code s.[pos] - 97 in
2439 loop (pos+1) (n*26 + m)
2440 in loop 0 0
2442 let rec loop n = function
2443 | [] -> ()
2444 | l :: rest ->
2445 match getopaque l.pageno with
2446 | None -> loop n rest
2447 | Some opaque ->
2448 let m = getlinkcount opaque in
2449 if n < m
2450 then (
2451 let under = getlink opaque n in
2452 f under
2454 else loop (n-m) rest
2456 loop n state.layout;
2460 let textentry text key =
2461 if key land 0xff00 = 0xff00
2462 then TEcont text
2463 else TEcont (text ^ Wsi.toutf8 key)
2466 let reqlayout angle proportional =
2467 match state.throttle with
2468 | None ->
2469 if nogeomcmds state.geomcmds
2470 then state.anchor <- getanchor ();
2471 conf.angle <- angle mod 360;
2472 if conf.angle != 0
2473 then (
2474 match state.mode with
2475 | LinkNav _ -> state.mode <- View
2476 | _ -> ()
2478 conf.proportional <- proportional;
2479 invalidate "reqlayout"
2480 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2481 | _ -> ()
2484 let settrim trimmargins trimfuzz =
2485 if nogeomcmds state.geomcmds
2486 then state.anchor <- getanchor ();
2487 conf.trimmargins <- trimmargins;
2488 conf.trimfuzz <- trimfuzz;
2489 let x0, y0, x1, y1 = trimfuzz in
2490 invalidate "settrim"
2491 (fun () ->
2492 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2493 Hashtbl.iter (fun _ opaque ->
2494 wcmd "freepage %s" opaque;
2495 ) state.pagemap;
2496 Hashtbl.clear state.pagemap;
2499 let setzoom zoom =
2500 match state.throttle with
2501 | None ->
2502 let zoom = max 0.01 zoom in
2503 if zoom <> conf.zoom
2504 then (
2505 state.prevzoom <- conf.zoom;
2506 conf.zoom <- zoom;
2507 reshape conf.winw conf.winh;
2508 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2511 | Some (layout, y, started) ->
2512 let time =
2513 match conf.maxwait with
2514 | None -> 0.0
2515 | Some t -> t
2517 let dt = now () -. started in
2518 if dt > time
2519 then (
2520 state.y <- y;
2521 load layout;
2525 let setcolumns mode columns coverA coverB =
2526 if columns < 0
2527 then (
2528 if isbirdseye mode
2529 then showtext '!' "split mode doesn't work in bird's eye"
2530 else (
2531 conf.columns <- Csplit (-columns, [||]);
2532 state.x <- 0;
2533 conf.zoom <- 1.0;
2536 else (
2537 if columns < 2
2538 then (
2539 conf.columns <- Csingle;
2540 state.x <- 0;
2541 setzoom 1.0;
2543 else (
2544 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2545 conf.zoom <- 1.0;
2548 reshape conf.winw conf.winh;
2551 let enterbirdseye () =
2552 let zoom = float conf.thumbw /. float conf.winw in
2553 let birdseyepageno =
2554 let cy = conf.winh / 2 in
2555 let fold = function
2556 | [] -> 0
2557 | l :: rest ->
2558 let rec fold best = function
2559 | [] -> best.pageno
2560 | l :: rest ->
2561 let d = cy - (l.pagedispy + l.pagevh/2)
2562 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2563 if abs d < abs dbest
2564 then fold l rest
2565 else best.pageno
2566 in fold l rest
2568 fold state.layout
2570 state.mode <- Birdseye (
2571 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2573 conf.zoom <- zoom;
2574 conf.presentation <- false;
2575 conf.interpagespace <- 10;
2576 conf.hlinks <- false;
2577 state.x <- 0;
2578 state.mstate <- Mnone;
2579 conf.maxwait <- None;
2580 conf.columns <- (
2581 match conf.beyecolumns with
2582 | Some c ->
2583 conf.zoom <- 1.0;
2584 Cmulti ((c, 0, 0), [||])
2585 | None -> Csingle
2587 Wsi.setcursor Wsi.CURSOR_INHERIT;
2588 if conf.verbose
2589 then
2590 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2591 (100.0*.zoom)
2592 else
2593 state.text <- ""
2595 reshape conf.winw conf.winh;
2598 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2599 state.mode <- View;
2600 conf.zoom <- c.zoom;
2601 conf.presentation <- c.presentation;
2602 conf.interpagespace <- c.interpagespace;
2603 conf.maxwait <- c.maxwait;
2604 conf.hlinks <- c.hlinks;
2605 conf.beyecolumns <- (
2606 match conf.columns with
2607 | Cmulti ((c, _, _), _) -> Some c
2608 | Csingle -> None
2609 | Csplit _ -> assert false
2611 conf.columns <- (
2612 match c.columns with
2613 | Cmulti (c, _) -> Cmulti (c, [||])
2614 | Csingle -> Csingle
2615 | Csplit _ -> failwith "leaving bird's eye split mode"
2617 state.x <- leftx;
2618 if conf.verbose
2619 then
2620 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2621 (100.0*.conf.zoom)
2623 reshape conf.winw conf.winh;
2624 state.anchor <- if goback then anchor else (pageno, 0.0);
2627 let togglebirdseye () =
2628 match state.mode with
2629 | Birdseye vals -> leavebirdseye vals true
2630 | View -> enterbirdseye ()
2631 | _ -> ()
2634 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2635 let pageno = max 0 (pageno - incr) in
2636 let rec loop = function
2637 | [] -> gotopage1 pageno 0
2638 | l :: _ when l.pageno = pageno ->
2639 if l.pagedispy >= 0 && l.pagey = 0
2640 then G.postRedisplay "upbirdseye"
2641 else gotopage1 pageno 0
2642 | _ :: rest -> loop rest
2644 loop state.layout;
2645 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2648 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2649 let pageno = min (state.pagecount - 1) (pageno + incr) in
2650 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2651 let rec loop = function
2652 | [] ->
2653 let y, h = getpageyh pageno in
2654 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2655 gotoy (clamp dy)
2656 | l :: _ when l.pageno = pageno ->
2657 if l.pagevh != l.pageh
2658 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2659 else G.postRedisplay "downbirdseye"
2660 | _ :: rest -> loop rest
2662 loop state.layout
2665 let optentry mode _ key =
2666 let btos b = if b then "on" else "off" in
2667 if key >= 32 && key < 127
2668 then
2669 let c = Char.chr key in
2670 match c with
2671 | 's' ->
2672 let ondone s =
2673 try conf.scrollstep <- int_of_string s with exc ->
2674 state.text <- Printf.sprintf "bad integer `%s': %s"
2675 s (Printexc.to_string exc)
2677 TEswitch ("scroll step: ", "", None, intentry, ondone)
2679 | 'A' ->
2680 let ondone s =
2682 conf.autoscrollstep <- int_of_string s;
2683 if state.autoscroll <> None
2684 then state.autoscroll <- Some conf.autoscrollstep
2685 with exc ->
2686 state.text <- Printf.sprintf "bad integer `%s': %s"
2687 s (Printexc.to_string exc)
2689 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2691 | 'C' ->
2692 let mode = state.mode in
2693 let ondone s =
2695 let n, a, b = multicolumns_of_string s in
2696 setcolumns mode n a b;
2697 with exc ->
2698 state.text <- Printf.sprintf "bad columns `%s': %s"
2699 s (Printexc.to_string exc)
2701 TEswitch ("columns: ", "", None, textentry, ondone)
2703 | 'Z' ->
2704 let ondone s =
2706 let zoom = float (int_of_string s) /. 100.0 in
2707 setzoom zoom
2708 with exc ->
2709 state.text <- Printf.sprintf "bad integer `%s': %s"
2710 s (Printexc.to_string exc)
2712 TEswitch ("zoom: ", "", None, intentry, ondone)
2714 | 't' ->
2715 let ondone s =
2717 conf.thumbw <- bound (int_of_string s) 2 4096;
2718 state.text <-
2719 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2720 begin match mode with
2721 | Birdseye beye ->
2722 leavebirdseye beye false;
2723 enterbirdseye ();
2724 | _ -> ();
2726 with exc ->
2727 state.text <- Printf.sprintf "bad integer `%s': %s"
2728 s (Printexc.to_string exc)
2730 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2732 | 'R' ->
2733 let ondone s =
2734 match try
2735 Some (int_of_string s)
2736 with exc ->
2737 state.text <- Printf.sprintf "bad integer `%s': %s"
2738 s (Printexc.to_string exc);
2739 None
2740 with
2741 | Some angle -> reqlayout angle conf.proportional
2742 | None -> ()
2744 TEswitch ("rotation: ", "", None, intentry, ondone)
2746 | 'i' ->
2747 conf.icase <- not conf.icase;
2748 TEdone ("case insensitive search " ^ (btos conf.icase))
2750 | 'p' ->
2751 conf.preload <- not conf.preload;
2752 gotoy state.y;
2753 TEdone ("preload " ^ (btos conf.preload))
2755 | 'v' ->
2756 conf.verbose <- not conf.verbose;
2757 TEdone ("verbose " ^ (btos conf.verbose))
2759 | 'd' ->
2760 conf.debug <- not conf.debug;
2761 TEdone ("debug " ^ (btos conf.debug))
2763 | 'h' ->
2764 conf.maxhfit <- not conf.maxhfit;
2765 state.maxy <-
2766 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2767 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2769 | 'c' ->
2770 conf.crophack <- not conf.crophack;
2771 TEdone ("crophack " ^ btos conf.crophack)
2773 | 'a' ->
2774 let s =
2775 match conf.maxwait with
2776 | None ->
2777 conf.maxwait <- Some infinity;
2778 "always wait for page to complete"
2779 | Some _ ->
2780 conf.maxwait <- None;
2781 "show placeholder if page is not ready"
2783 TEdone s
2785 | 'f' ->
2786 conf.underinfo <- not conf.underinfo;
2787 TEdone ("underinfo " ^ btos conf.underinfo)
2789 | 'P' ->
2790 conf.savebmarks <- not conf.savebmarks;
2791 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2793 | 'S' ->
2794 let ondone s =
2796 let pageno, py =
2797 match state.layout with
2798 | [] -> 0, 0
2799 | l :: _ ->
2800 l.pageno, l.pagey
2802 conf.interpagespace <- int_of_string s;
2803 state.maxy <- calcheight ();
2804 let y = getpagey pageno in
2805 gotoy (y + py)
2806 with exc ->
2807 state.text <- Printf.sprintf "bad integer `%s': %s"
2808 s (Printexc.to_string exc)
2810 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2812 | 'l' ->
2813 reqlayout conf.angle (not conf.proportional);
2814 TEdone ("proportional display " ^ btos conf.proportional)
2816 | 'T' ->
2817 settrim (not conf.trimmargins) conf.trimfuzz;
2818 TEdone ("trim margins " ^ btos conf.trimmargins)
2820 | 'I' ->
2821 conf.invert <- not conf.invert;
2822 TEdone ("invert colors " ^ btos conf.invert)
2824 | 'x' ->
2825 let ondone s =
2826 cbput state.hists.sel s;
2827 conf.selcmd <- s;
2829 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2830 textentry, ondone)
2832 | _ ->
2833 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2834 TEstop
2835 else
2836 TEcont state.text
2839 class type lvsource = object
2840 method getitemcount : int
2841 method getitem : int -> (string * int)
2842 method hasaction : int -> bool
2843 method exit :
2844 uioh:uioh ->
2845 cancel:bool ->
2846 active:int ->
2847 first:int ->
2848 pan:int ->
2849 qsearch:string ->
2850 uioh option
2851 method getactive : int
2852 method getfirst : int
2853 method getqsearch : string
2854 method setqsearch : string -> unit
2855 method getpan : int
2856 end;;
2858 class virtual lvsourcebase = object
2859 val mutable m_active = 0
2860 val mutable m_first = 0
2861 val mutable m_qsearch = ""
2862 val mutable m_pan = 0
2863 method getactive = m_active
2864 method getfirst = m_first
2865 method getqsearch = m_qsearch
2866 method getpan = m_pan
2867 method setqsearch s = m_qsearch <- s
2868 end;;
2870 let withoutlastutf8 s =
2871 let len = String.length s in
2872 if len = 0
2873 then s
2874 else
2875 let rec find pos =
2876 if pos = 0
2877 then pos
2878 else
2879 let b = Char.code s.[pos] in
2880 if b land 0b110000 = 0b11000000
2881 then find (pos-1)
2882 else pos-1
2884 let first =
2885 if Char.code s.[len-1] land 0x80 = 0
2886 then len-1
2887 else find (len-1)
2889 String.sub s 0 first;
2892 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2893 let enttext te =
2894 state.mode <- Textentry (te, onleave);
2895 state.text <- "";
2896 enttext ();
2897 G.postRedisplay "textentrykeyboard enttext";
2899 let histaction cmd =
2900 match opthist with
2901 | None -> ()
2902 | Some (action, _) ->
2903 state.mode <- Textentry (
2904 (c, action cmd, opthist, onkey, ondone), onleave
2906 G.postRedisplay "textentry histaction"
2908 match key with
2909 | 0xff08 -> (* backspace *)
2910 let s = withoutlastutf8 text in
2911 let len = String.length s in
2912 if len = 0
2913 then (
2914 onleave Cancel;
2915 G.postRedisplay "textentrykeyboard after cancel";
2917 else (
2918 enttext (c, s, opthist, onkey, ondone)
2921 | 0xff0d ->
2922 ondone text;
2923 onleave Confirm;
2924 G.postRedisplay "textentrykeyboard after confirm"
2926 | 0xff52 -> histaction HCprev
2927 | 0xff54 -> histaction HCnext
2928 | 0xff50 -> histaction HCfirst
2929 | 0xff57 -> histaction HClast
2931 | 0xff1b -> (* escape*)
2932 if String.length text = 0
2933 then (
2934 begin match opthist with
2935 | None -> ()
2936 | Some (_, onhistcancel) -> onhistcancel ()
2937 end;
2938 onleave Cancel;
2939 state.text <- "";
2940 G.postRedisplay "textentrykeyboard after cancel2"
2942 else (
2943 enttext (c, "", opthist, onkey, ondone)
2946 | 0xff9f | 0xffff -> () (* delete *)
2948 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2949 begin match onkey text key with
2950 | TEdone text ->
2951 ondone text;
2952 onleave Confirm;
2953 G.postRedisplay "textentrykeyboard after confirm2";
2955 | TEcont text ->
2956 enttext (c, text, opthist, onkey, ondone);
2958 | TEstop ->
2959 onleave Cancel;
2960 G.postRedisplay "textentrykeyboard after cancel3"
2962 | TEswitch te ->
2963 state.mode <- Textentry (te, onleave);
2964 G.postRedisplay "textentrykeyboard switch";
2965 end;
2967 | _ ->
2968 vlog "unhandled key %s" (Wsi.keyname key)
2971 let firstof first active =
2972 if first > active || abs (first - active) > fstate.maxrows - 1
2973 then max 0 (active - (fstate.maxrows/2))
2974 else first
2977 let calcfirst first active =
2978 if active > first
2979 then
2980 let rows = active - first in
2981 if rows > fstate.maxrows then active - fstate.maxrows else first
2982 else active
2985 let scrollph y maxy =
2986 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2987 let sh = float conf.winh /. sh in
2988 let sh = max sh (float conf.scrollh) in
2990 let percent =
2991 if y = state.maxy
2992 then 1.0
2993 else float y /. float maxy
2995 let position = (float conf.winh -. sh) *. percent in
2997 let position =
2998 if position +. sh > float conf.winh
2999 then float conf.winh -. sh
3000 else position
3002 position, sh;
3005 let coe s = (s :> uioh);;
3007 class listview ~(source:lvsource) ~trusted ~modehash =
3008 object (self)
3009 val m_pan = source#getpan
3010 val m_first = source#getfirst
3011 val m_active = source#getactive
3012 val m_qsearch = source#getqsearch
3013 val m_prev_uioh = state.uioh
3015 method private elemunder y =
3016 let n = y / (fstate.fontsize+1) in
3017 if m_first + n < source#getitemcount
3018 then (
3019 if source#hasaction (m_first + n)
3020 then Some (m_first + n)
3021 else None
3023 else None
3025 method display =
3026 Gl.enable `blend;
3027 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3028 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3029 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3030 GlDraw.color (1., 1., 1.);
3031 Gl.enable `texture_2d;
3032 let fs = fstate.fontsize in
3033 let nfs = fs + 1 in
3034 let ww = fstate.wwidth in
3035 let tabw = 30.0*.ww in
3036 let itemcount = source#getitemcount in
3037 let rec loop row =
3038 if (row - m_first) * nfs > conf.winh
3039 then ()
3040 else (
3041 if row >= 0 && row < itemcount
3042 then (
3043 let (s, level) = source#getitem row in
3044 let y = (row - m_first) * nfs in
3045 let x = 5.0 +. float (level + m_pan) *. ww in
3046 if row = m_active
3047 then (
3048 Gl.disable `texture_2d;
3049 GlDraw.polygon_mode `both `line;
3050 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3051 GlDraw.rect (1., float (y + 1))
3052 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3053 GlDraw.polygon_mode `both `fill;
3054 GlDraw.color (1., 1., 1.);
3055 Gl.enable `texture_2d;
3058 let drawtabularstring s =
3059 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3060 if trusted
3061 then
3062 let tabpos = try String.index s '\t' with Not_found -> -1 in
3063 if tabpos > 0
3064 then
3065 let len = String.length s - tabpos - 1 in
3066 let s1 = String.sub s 0 tabpos
3067 and s2 = String.sub s (tabpos + 1) len in
3068 let nx = drawstr x s1 in
3069 let sw = nx -. x in
3070 let x = x +. (max tabw sw) in
3071 drawstr x s2
3072 else
3073 drawstr x s
3074 else
3075 drawstr x s
3077 let _ = drawtabularstring s in
3078 loop (row+1)
3082 loop m_first;
3083 Gl.disable `blend;
3084 Gl.disable `texture_2d;
3086 method updownlevel incr =
3087 let len = source#getitemcount in
3088 let curlevel =
3089 if m_active >= 0 && m_active < len
3090 then snd (source#getitem m_active)
3091 else -1
3093 let rec flow i =
3094 if i = len then i-1 else if i = -1 then 0 else
3095 let _, l = source#getitem i in
3096 if l != curlevel then i else flow (i+incr)
3098 let active = flow m_active in
3099 let first = calcfirst m_first active in
3100 G.postRedisplay "outline updownlevel";
3101 {< m_active = active; m_first = first >}
3103 method private key1 key mask =
3104 let set1 active first qsearch =
3105 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3107 let search active pattern incr =
3108 let dosearch re =
3109 let rec loop n =
3110 if n >= 0 && n < source#getitemcount
3111 then (
3112 let s, _ = source#getitem n in
3114 (try ignore (Str.search_forward re s 0); true
3115 with Not_found -> false)
3116 then Some n
3117 else loop (n + incr)
3119 else None
3121 loop active
3124 let re = Str.regexp_case_fold pattern in
3125 dosearch re
3126 with Failure s ->
3127 state.text <- s;
3128 None
3130 let itemcount = source#getitemcount in
3131 let find start incr =
3132 let rec find i =
3133 if i = -1 || i = itemcount
3134 then -1
3135 else (
3136 if source#hasaction i
3137 then i
3138 else find (i + incr)
3141 find start
3143 let set active first =
3144 let first = bound first 0 (itemcount - fstate.maxrows) in
3145 state.text <- "";
3146 coe {< m_active = active; m_first = first >}
3148 let navigate incr =
3149 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3150 let active, first =
3151 let incr1 = if incr > 0 then 1 else -1 in
3152 if isvisible m_first m_active
3153 then
3154 let next =
3155 let next = m_active + incr in
3156 let next =
3157 if next < 0 || next >= itemcount
3158 then -1
3159 else find next incr1
3161 if next = -1 || abs (m_active - next) > fstate.maxrows
3162 then -1
3163 else next
3165 if next = -1
3166 then
3167 let first = m_first + incr in
3168 let first = bound first 0 (itemcount - 1) in
3169 let next =
3170 let next = m_active + incr in
3171 let next = bound next 0 (itemcount - 1) in
3172 find next ~-incr1
3174 let active = if next = -1 then m_active else next in
3175 active, first
3176 else
3177 let first = min next m_first in
3178 let first =
3179 if abs (next - first) > fstate.maxrows
3180 then first + incr
3181 else first
3183 next, first
3184 else
3185 let first = m_first + incr in
3186 let first = bound first 0 (itemcount - 1) in
3187 let active =
3188 let next = m_active + incr in
3189 let next = bound next 0 (itemcount - 1) in
3190 let next = find next incr1 in
3191 let active =
3192 if next = -1 || abs (m_active - first) > fstate.maxrows
3193 then (
3194 let active = if m_active = -1 then next else m_active in
3195 active
3197 else next
3199 if isvisible first active
3200 then active
3201 else -1
3203 active, first
3205 G.postRedisplay "listview navigate";
3206 set active first;
3208 match key with
3209 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3210 let incr = if key = 0x72 then -1 else 1 in
3211 let active, first =
3212 match search (m_active + incr) m_qsearch incr with
3213 | None ->
3214 state.text <- m_qsearch ^ " [not found]";
3215 m_active, m_first
3216 | Some active ->
3217 state.text <- m_qsearch;
3218 active, firstof m_first active
3220 G.postRedisplay "listview ctrl-r/s";
3221 set1 active first m_qsearch;
3223 | 0xff08 -> (* backspace *)
3224 if String.length m_qsearch = 0
3225 then coe self
3226 else (
3227 let qsearch = withoutlastutf8 m_qsearch in
3228 let len = String.length qsearch in
3229 if len = 0
3230 then (
3231 state.text <- "";
3232 G.postRedisplay "listview empty qsearch";
3233 set1 m_active m_first "";
3235 else
3236 let active, first =
3237 match search m_active qsearch ~-1 with
3238 | None ->
3239 state.text <- qsearch ^ " [not found]";
3240 m_active, m_first
3241 | Some active ->
3242 state.text <- qsearch;
3243 active, firstof m_first active
3245 G.postRedisplay "listview backspace qsearch";
3246 set1 active first qsearch
3249 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3250 let pattern = m_qsearch ^ Wsi.toutf8 key in
3251 let active, first =
3252 match search m_active pattern 1 with
3253 | None ->
3254 state.text <- pattern ^ " [not found]";
3255 m_active, m_first
3256 | Some active ->
3257 state.text <- pattern;
3258 active, firstof m_first active
3260 G.postRedisplay "listview qsearch add";
3261 set1 active first pattern;
3263 | 0xff1b -> (* escape *)
3264 state.text <- "";
3265 if String.length m_qsearch = 0
3266 then (
3267 G.postRedisplay "list view escape";
3268 begin
3269 match
3270 source#exit (coe self) true m_active m_first m_pan m_qsearch
3271 with
3272 | None -> m_prev_uioh
3273 | Some uioh -> uioh
3276 else (
3277 G.postRedisplay "list view kill qsearch";
3278 source#setqsearch "";
3279 coe {< m_qsearch = "" >}
3282 | 0xff0d -> (* return *)
3283 state.text <- "";
3284 let self = {< m_qsearch = "" >} in
3285 source#setqsearch "";
3286 let opt =
3287 G.postRedisplay "listview enter";
3288 if m_active >= 0 && m_active < source#getitemcount
3289 then (
3290 source#exit (coe self) false m_active m_first m_pan "";
3292 else (
3293 source#exit (coe self) true m_active m_first m_pan "";
3296 begin match opt with
3297 | None -> m_prev_uioh
3298 | Some uioh -> uioh
3301 | 0xff9f | 0xffff -> (* delete *)
3302 coe self
3304 | 0xff52 -> navigate ~-1 (* up *)
3305 | 0xff54 -> navigate 1 (* down *)
3306 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3307 | 0xff56 -> navigate fstate.maxrows (* next *)
3309 | 0xff53 -> (* right *)
3310 state.text <- "";
3311 G.postRedisplay "listview right";
3312 coe {< m_pan = m_pan - 1 >}
3314 | 0xff51 -> (* left *)
3315 state.text <- "";
3316 G.postRedisplay "listview left";
3317 coe {< m_pan = m_pan + 1 >}
3319 | 0xff50 -> (* home *)
3320 let active = find 0 1 in
3321 G.postRedisplay "listview home";
3322 set active 0;
3324 | 0xff57 -> (* end *)
3325 let first = max 0 (itemcount - fstate.maxrows) in
3326 let active = find (itemcount - 1) ~-1 in
3327 G.postRedisplay "listview end";
3328 set active first;
3330 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3331 coe self
3333 | _ ->
3334 dolog "listview unknown key %#x" key; coe self
3336 method key key mask =
3337 match state.mode with
3338 | Textentry te -> textentrykeyboard key mask te; coe self
3339 | _ -> self#key1 key mask
3341 method button button down x y _ =
3342 let opt =
3343 match button with
3344 | 1 when x > conf.winw - conf.scrollbw ->
3345 G.postRedisplay "listview scroll";
3346 if down
3347 then
3348 let _, position, sh = self#scrollph in
3349 if y > truncate position && y < truncate (position +. sh)
3350 then (
3351 state.mstate <- Mscrolly;
3352 Some (coe self)
3354 else
3355 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3356 let first = truncate (s *. float source#getitemcount) in
3357 let first = min source#getitemcount first in
3358 Some (coe {< m_first = first; m_active = first >})
3359 else (
3360 state.mstate <- Mnone;
3361 Some (coe self);
3363 | 1 when not down ->
3364 begin match self#elemunder y with
3365 | Some n ->
3366 G.postRedisplay "listview click";
3367 source#exit
3368 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3369 | _ ->
3370 Some (coe self)
3372 | n when (n == 4 || n == 5) && not down ->
3373 let len = source#getitemcount in
3374 let first =
3375 if n = 5 && m_first + fstate.maxrows >= len
3376 then
3377 m_first
3378 else
3379 let first = m_first + (if n == 4 then -1 else 1) in
3380 bound first 0 (len - 1)
3382 G.postRedisplay "listview wheel";
3383 Some (coe {< m_first = first >})
3384 | _ ->
3385 Some (coe self)
3387 match opt with
3388 | None -> m_prev_uioh
3389 | Some uioh -> uioh
3391 method motion _ y =
3392 match state.mstate with
3393 | Mscrolly ->
3394 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3395 let first = truncate (s *. float source#getitemcount) in
3396 let first = min source#getitemcount first in
3397 G.postRedisplay "listview motion";
3398 coe {< m_first = first; m_active = first >}
3399 | _ -> coe self
3401 method pmotion x y =
3402 if x < conf.winw - conf.scrollbw
3403 then
3404 let n =
3405 match self#elemunder y with
3406 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3407 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3409 let o =
3410 if n != m_active
3411 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3412 else self
3414 coe o
3415 else (
3416 Wsi.setcursor Wsi.CURSOR_INHERIT;
3417 coe self
3420 method infochanged _ = ()
3422 method scrollpw = (0, 0.0, 0.0)
3423 method scrollph =
3424 let nfs = fstate.fontsize + 1 in
3425 let y = m_first * nfs in
3426 let itemcount = source#getitemcount in
3427 let maxi = max 0 (itemcount - fstate.maxrows) in
3428 let maxy = maxi * nfs in
3429 let p, h = scrollph y maxy in
3430 conf.scrollbw, p, h
3432 method modehash = modehash
3433 end;;
3435 class outlinelistview ~source =
3436 object (self)
3437 inherit listview
3438 ~source:(source :> lvsource)
3439 ~trusted:false
3440 ~modehash:(findkeyhash conf "outline")
3441 as super
3443 method key key mask =
3444 let calcfirst first active =
3445 if active > first
3446 then
3447 let rows = active - first in
3448 if rows > fstate.maxrows then active - fstate.maxrows else first
3449 else active
3451 let navigate incr =
3452 let active = m_active + incr in
3453 let active = bound active 0 (source#getitemcount - 1) in
3454 let first = calcfirst m_first active in
3455 G.postRedisplay "outline navigate";
3456 coe {< m_active = active; m_first = first >}
3458 let ctrl = Wsi.withctrl mask in
3459 match key with
3460 | 110 when ctrl -> (* ctrl-n *)
3461 source#narrow m_qsearch;
3462 G.postRedisplay "outline ctrl-n";
3463 coe {< m_first = 0; m_active = 0 >}
3465 | 117 when ctrl -> (* ctrl-u *)
3466 source#denarrow;
3467 G.postRedisplay "outline ctrl-u";
3468 state.text <- "";
3469 coe {< m_first = 0; m_active = 0 >}
3471 | 108 when ctrl -> (* ctrl-l *)
3472 let first = m_active - (fstate.maxrows / 2) in
3473 G.postRedisplay "outline ctrl-l";
3474 coe {< m_first = first >}
3476 | 0xff9f | 0xffff -> (* delete *)
3477 source#remove m_active;
3478 G.postRedisplay "outline delete";
3479 let active = max 0 (m_active-1) in
3480 coe {< m_first = firstof m_first active;
3481 m_active = active >}
3483 | 0xff52 -> navigate ~-1 (* up *)
3484 | 0xff54 -> navigate 1 (* down *)
3485 | 0xff55 -> (* prior *)
3486 navigate ~-(fstate.maxrows)
3487 | 0xff56 -> (* next *)
3488 navigate fstate.maxrows
3490 | 0xff53 -> (* [ctrl-]right *)
3491 let o =
3492 if ctrl
3493 then (
3494 G.postRedisplay "outline ctrl right";
3495 {< m_pan = m_pan + 1 >}
3497 else self#updownlevel 1
3499 coe o
3501 | 0xff51 -> (* [ctrl-]left *)
3502 let o =
3503 if ctrl
3504 then (
3505 G.postRedisplay "outline ctrl left";
3506 {< m_pan = m_pan - 1 >}
3508 else self#updownlevel ~-1
3510 coe o
3512 | 0xff50 -> (* home *)
3513 G.postRedisplay "outline home";
3514 coe {< m_first = 0; m_active = 0 >}
3516 | 0xff57 -> (* end *)
3517 let active = source#getitemcount - 1 in
3518 let first = max 0 (active - fstate.maxrows) in
3519 G.postRedisplay "outline end";
3520 coe {< m_active = active; m_first = first >}
3522 | _ -> super#key key mask
3525 let outlinesource usebookmarks =
3526 let empty = [||] in
3527 (object
3528 inherit lvsourcebase
3529 val mutable m_items = empty
3530 val mutable m_orig_items = empty
3531 val mutable m_prev_items = empty
3532 val mutable m_narrow_pattern = ""
3533 val mutable m_hadremovals = false
3535 method getitemcount =
3536 Array.length m_items + (if m_hadremovals then 1 else 0)
3538 method getitem n =
3539 if n == Array.length m_items && m_hadremovals
3540 then
3541 ("[Confirm removal]", 0)
3542 else
3543 let s, n, _ = m_items.(n) in
3544 (s, n)
3546 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3547 ignore (uioh, first, qsearch);
3548 let confrimremoval = m_hadremovals && active = Array.length m_items in
3549 let items =
3550 if String.length m_narrow_pattern = 0
3551 then m_orig_items
3552 else m_items
3554 if not cancel
3555 then (
3556 if not confrimremoval
3557 then(
3558 let _, _, anchor = m_items.(active) in
3559 gotoanchor anchor;
3560 m_items <- items;
3562 else (
3563 state.bookmarks <- Array.to_list m_items;
3564 m_orig_items <- m_items;
3567 else m_items <- items;
3568 m_pan <- pan;
3569 None
3571 method hasaction _ = true
3573 method greetmsg =
3574 if Array.length m_items != Array.length m_orig_items
3575 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3576 else ""
3578 method narrow pattern =
3579 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3580 match reopt with
3581 | None -> ()
3582 | Some re ->
3583 let rec loop accu n =
3584 if n = -1
3585 then (
3586 m_narrow_pattern <- pattern;
3587 m_items <- Array.of_list accu
3589 else
3590 let (s, _, _) as o = m_items.(n) in
3591 let accu =
3592 if (try ignore (Str.search_forward re s 0); true
3593 with Not_found -> false)
3594 then o :: accu
3595 else accu
3597 loop accu (n-1)
3599 loop [] (Array.length m_items - 1)
3601 method denarrow =
3602 m_orig_items <- (
3603 if usebookmarks
3604 then Array.of_list state.bookmarks
3605 else state.outlines
3607 m_items <- m_orig_items
3609 method remove m =
3610 if usebookmarks
3611 then
3612 if m >= 0 && m < Array.length m_items
3613 then (
3614 m_hadremovals <- true;
3615 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3616 let n = if n >= m then n+1 else n in
3617 m_items.(n)
3621 method reset anchor items =
3622 m_hadremovals <- false;
3623 if m_orig_items == empty || m_prev_items != items
3624 then (
3625 m_orig_items <- items;
3626 if String.length m_narrow_pattern = 0
3627 then m_items <- items;
3629 m_prev_items <- items;
3630 let rely = getanchory anchor in
3631 let active =
3632 let rec loop n best bestd =
3633 if n = Array.length m_items
3634 then best
3635 else
3636 let (_, _, anchor) = m_items.(n) in
3637 let orely = getanchory anchor in
3638 let d = abs (orely - rely) in
3639 if d < bestd
3640 then loop (n+1) n d
3641 else loop (n+1) best bestd
3643 loop 0 ~-1 max_int
3645 m_active <- active;
3646 m_first <- firstof m_first active
3647 end)
3650 let enterselector usebookmarks =
3651 let source = outlinesource usebookmarks in
3652 fun errmsg ->
3653 let outlines =
3654 if usebookmarks
3655 then Array.of_list state.bookmarks
3656 else state.outlines
3658 if Array.length outlines = 0
3659 then (
3660 showtext ' ' errmsg;
3662 else (
3663 state.text <- source#greetmsg;
3664 Wsi.setcursor Wsi.CURSOR_INHERIT;
3665 let anchor = getanchor () in
3666 source#reset anchor outlines;
3667 state.uioh <- coe (new outlinelistview ~source);
3668 G.postRedisplay "enter selector";
3672 let enteroutlinemode =
3673 let f = enterselector false in
3674 fun ()-> f "Document has no outline";
3677 let enterbookmarkmode =
3678 let f = enterselector true in
3679 fun () -> f "Document has no bookmarks (yet)";
3682 let color_of_string s =
3683 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3684 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3688 let color_to_string (r, g, b) =
3689 let r = truncate (r *. 256.0)
3690 and g = truncate (g *. 256.0)
3691 and b = truncate (b *. 256.0) in
3692 Printf.sprintf "%d/%d/%d" r g b
3695 let irect_of_string s =
3696 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3699 let irect_to_string (x0,y0,x1,y1) =
3700 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3703 let makecheckers () =
3704 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3705 following to say:
3706 converted by Issac Trotts. July 25, 2002 *)
3707 let image_height = 64
3708 and image_width = 64 in
3710 let make_image () =
3711 let image =
3712 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3714 for i = 0 to image_width - 1 do
3715 for j = 0 to image_height - 1 do
3716 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3717 (if (i land 8 ) lxor (j land 8) = 0
3718 then [|255;255;255|] else [|200;200;200|])
3719 done
3720 done;
3721 image
3723 let image = make_image () in
3724 let id = GlTex.gen_texture () in
3725 GlTex.bind_texture `texture_2d id;
3726 GlPix.store (`unpack_alignment 1);
3727 GlTex.image2d image;
3728 List.iter (GlTex.parameter ~target:`texture_2d)
3729 [ `wrap_s `repeat;
3730 `wrap_t `repeat;
3731 `mag_filter `nearest;
3732 `min_filter `nearest ];
3736 let setcheckers enabled =
3737 match state.texid with
3738 | None ->
3739 if enabled then state.texid <- Some (makecheckers ())
3741 | Some texid ->
3742 if not enabled
3743 then (
3744 GlTex.delete_texture texid;
3745 state.texid <- None;
3749 let int_of_string_with_suffix s =
3750 let l = String.length s in
3751 let s1, shift =
3752 if l > 1
3753 then
3754 let suffix = Char.lowercase s.[l-1] in
3755 match suffix with
3756 | 'k' -> String.sub s 0 (l-1), 10
3757 | 'm' -> String.sub s 0 (l-1), 20
3758 | 'g' -> String.sub s 0 (l-1), 30
3759 | _ -> s, 0
3760 else s, 0
3762 let n = int_of_string s1 in
3763 let m = n lsl shift in
3764 if m < 0 || m < n
3765 then raise (Failure "value too large")
3766 else m
3769 let string_with_suffix_of_int n =
3770 if n = 0
3771 then "0"
3772 else
3773 let n, s =
3774 if n = 0
3775 then 0, ""
3776 else (
3777 if n land ((1 lsl 20) - 1) = 0
3778 then n lsr 20, "M"
3779 else (
3780 if n land ((1 lsl 10) - 1) = 0
3781 then n lsr 10, "K"
3782 else n, ""
3786 let rec loop s n =
3787 let h = n mod 1000 in
3788 let n = n / 1000 in
3789 if n = 0
3790 then string_of_int h ^ s
3791 else (
3792 let s = Printf.sprintf "_%03d%s" h s in
3793 loop s n
3796 loop "" n ^ s;
3799 let defghyllscroll = (40, 8, 32);;
3800 let ghyllscroll_of_string s =
3801 let (n, a, b) as nab =
3802 if s = "default"
3803 then defghyllscroll
3804 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3806 if n <= a || n <= b || a >= b
3807 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3808 nab;
3811 let ghyllscroll_to_string ((n, a, b) as nab) =
3812 if nab = defghyllscroll
3813 then "default"
3814 else Printf.sprintf "%d,%d,%d" n a b;
3817 let describe_location () =
3818 let f (fn, _) l =
3819 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3821 let fn, ln = List.fold_left f (-1, -1) state.layout in
3822 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3823 let percent =
3824 if maxy <= 0
3825 then 100.
3826 else (100. *. (float state.y /. float maxy))
3828 if fn = ln
3829 then
3830 Printf.sprintf "page %d of %d [%.2f%%]"
3831 (fn+1) state.pagecount percent
3832 else
3833 Printf.sprintf
3834 "pages %d-%d of %d [%.2f%%]"
3835 (fn+1) (ln+1) state.pagecount percent
3838 let enterinfomode =
3839 let btos b = if b then "\xe2\x88\x9a" else "" in
3840 let showextended = ref false in
3841 let leave mode = function
3842 | Confirm -> state.mode <- mode
3843 | Cancel -> state.mode <- mode in
3844 let src =
3845 (object
3846 val mutable m_first_time = true
3847 val mutable m_l = []
3848 val mutable m_a = [||]
3849 val mutable m_prev_uioh = nouioh
3850 val mutable m_prev_mode = View
3852 inherit lvsourcebase
3854 method reset prev_mode prev_uioh =
3855 m_a <- Array.of_list (List.rev m_l);
3856 m_l <- [];
3857 m_prev_mode <- prev_mode;
3858 m_prev_uioh <- prev_uioh;
3859 if m_first_time
3860 then (
3861 let rec loop n =
3862 if n >= Array.length m_a
3863 then ()
3864 else
3865 match m_a.(n) with
3866 | _, _, _, Action _ -> m_active <- n
3867 | _ -> loop (n+1)
3869 loop 0;
3870 m_first_time <- false;
3873 method int name get set =
3874 m_l <-
3875 (name, `int get, 1, Action (
3876 fun u ->
3877 let ondone s =
3878 try set (int_of_string s)
3879 with exn ->
3880 state.text <- Printf.sprintf "bad integer `%s': %s"
3881 s (Printexc.to_string exn)
3883 state.text <- "";
3884 let te = name ^ ": ", "", None, intentry, ondone in
3885 state.mode <- Textentry (te, leave m_prev_mode);
3887 )) :: m_l
3889 method int_with_suffix name get set =
3890 m_l <-
3891 (name, `intws get, 1, Action (
3892 fun u ->
3893 let ondone s =
3894 try set (int_of_string_with_suffix s)
3895 with exn ->
3896 state.text <- Printf.sprintf "bad integer `%s': %s"
3897 s (Printexc.to_string exn)
3899 state.text <- "";
3900 let te =
3901 name ^ ": ", "", None, intentry_with_suffix, ondone
3903 state.mode <- Textentry (te, leave m_prev_mode);
3905 )) :: m_l
3907 method bool ?(offset=1) ?(btos=btos) name get set =
3908 m_l <-
3909 (name, `bool (btos, get), offset, Action (
3910 fun u ->
3911 let v = get () in
3912 set (not v);
3914 )) :: m_l
3916 method color name get set =
3917 m_l <-
3918 (name, `color get, 1, Action (
3919 fun u ->
3920 let invalid = (nan, nan, nan) in
3921 let ondone s =
3922 let c =
3923 try color_of_string s
3924 with exn ->
3925 state.text <- Printf.sprintf "bad color `%s': %s"
3926 s (Printexc.to_string exn);
3927 invalid
3929 if c <> invalid
3930 then set c;
3932 let te = name ^ ": ", "", None, textentry, ondone in
3933 state.text <- color_to_string (get ());
3934 state.mode <- Textentry (te, leave m_prev_mode);
3936 )) :: m_l
3938 method string name get set =
3939 m_l <-
3940 (name, `string get, 1, Action (
3941 fun u ->
3942 let ondone s = set s in
3943 let te = name ^ ": ", "", None, textentry, ondone in
3944 state.mode <- Textentry (te, leave m_prev_mode);
3946 )) :: m_l
3948 method colorspace name get set =
3949 m_l <-
3950 (name, `string get, 1, Action (
3951 fun _ ->
3952 let source =
3953 let vals = [| "rgb"; "bgr"; "gray" |] in
3954 (object
3955 inherit lvsourcebase
3957 initializer
3958 m_active <- int_of_colorspace conf.colorspace;
3959 m_first <- 0;
3961 method getitemcount = Array.length vals
3962 method getitem n = (vals.(n), 0)
3963 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3964 ignore (uioh, first, pan, qsearch);
3965 if not cancel then set active;
3966 None
3967 method hasaction _ = true
3968 end)
3970 state.text <- "";
3971 let modehash = findkeyhash conf "info" in
3972 coe (new listview ~source ~trusted:true ~modehash)
3973 )) :: m_l
3975 method caption s offset =
3976 m_l <- (s, `empty, offset, Noaction) :: m_l
3978 method caption2 s f offset =
3979 m_l <- (s, `string f, offset, Noaction) :: m_l
3981 method getitemcount = Array.length m_a
3983 method getitem n =
3984 let tostr = function
3985 | `int f -> string_of_int (f ())
3986 | `intws f -> string_with_suffix_of_int (f ())
3987 | `string f -> f ()
3988 | `color f -> color_to_string (f ())
3989 | `bool (btos, f) -> btos (f ())
3990 | `empty -> ""
3992 let name, t, offset, _ = m_a.(n) in
3993 ((let s = tostr t in
3994 if String.length s > 0
3995 then Printf.sprintf "%s\t%s" name s
3996 else name),
3997 offset)
3999 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4000 let uiohopt =
4001 if not cancel
4002 then (
4003 m_qsearch <- qsearch;
4004 let uioh =
4005 match m_a.(active) with
4006 | _, _, _, Action f -> f uioh
4007 | _ -> uioh
4009 Some uioh
4011 else None
4013 m_active <- active;
4014 m_first <- first;
4015 m_pan <- pan;
4016 uiohopt
4018 method hasaction n =
4019 match m_a.(n) with
4020 | _, _, _, Action _ -> true
4021 | _ -> false
4022 end)
4024 let rec fillsrc prevmode prevuioh =
4025 let sep () = src#caption "" 0 in
4026 let colorp name get set =
4027 src#string name
4028 (fun () -> color_to_string (get ()))
4029 (fun v ->
4031 let c = color_of_string v in
4032 set c
4033 with exn ->
4034 state.text <- Printf.sprintf "bad color `%s': %s"
4035 v (Printexc.to_string exn);
4038 let oldmode = state.mode in
4039 let birdseye = isbirdseye state.mode in
4041 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4043 src#bool "presentation mode"
4044 (fun () -> conf.presentation)
4045 (fun v ->
4046 conf.presentation <- v;
4047 state.anchor <- getanchor ();
4048 represent ());
4050 src#bool "ignore case in searches"
4051 (fun () -> conf.icase)
4052 (fun v -> conf.icase <- v);
4054 src#bool "preload"
4055 (fun () -> conf.preload)
4056 (fun v -> conf.preload <- v);
4058 src#bool "highlight links"
4059 (fun () -> conf.hlinks)
4060 (fun v -> conf.hlinks <- v);
4062 src#bool "under info"
4063 (fun () -> conf.underinfo)
4064 (fun v -> conf.underinfo <- v);
4066 src#bool "persistent bookmarks"
4067 (fun () -> conf.savebmarks)
4068 (fun v -> conf.savebmarks <- v);
4070 src#bool "proportional display"
4071 (fun () -> conf.proportional)
4072 (fun v -> reqlayout conf.angle v);
4074 src#bool "trim margins"
4075 (fun () -> conf.trimmargins)
4076 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4078 src#bool "persistent location"
4079 (fun () -> conf.jumpback)
4080 (fun v -> conf.jumpback <- v);
4082 sep ();
4083 src#int "inter-page space"
4084 (fun () -> conf.interpagespace)
4085 (fun n ->
4086 conf.interpagespace <- n;
4087 let pageno, py =
4088 match state.layout with
4089 | [] -> 0, 0
4090 | l :: _ ->
4091 l.pageno, l.pagey
4093 state.maxy <- calcheight ();
4094 let y = getpagey pageno in
4095 gotoy (y + py)
4098 src#int "page bias"
4099 (fun () -> conf.pagebias)
4100 (fun v -> conf.pagebias <- v);
4102 src#int "scroll step"
4103 (fun () -> conf.scrollstep)
4104 (fun n -> conf.scrollstep <- n);
4106 src#int "auto scroll step"
4107 (fun () ->
4108 match state.autoscroll with
4109 | Some step -> step
4110 | _ -> conf.autoscrollstep)
4111 (fun n ->
4112 if state.autoscroll <> None
4113 then state.autoscroll <- Some n;
4114 conf.autoscrollstep <- n);
4116 src#int "zoom"
4117 (fun () -> truncate (conf.zoom *. 100.))
4118 (fun v -> setzoom ((float v) /. 100.));
4120 src#int "rotation"
4121 (fun () -> conf.angle)
4122 (fun v -> reqlayout v conf.proportional);
4124 src#int "scroll bar width"
4125 (fun () -> state.scrollw)
4126 (fun v ->
4127 state.scrollw <- v;
4128 conf.scrollbw <- v;
4129 reshape conf.winw conf.winh;
4132 src#int "scroll handle height"
4133 (fun () -> conf.scrollh)
4134 (fun v -> conf.scrollh <- v;);
4136 src#int "thumbnail width"
4137 (fun () -> conf.thumbw)
4138 (fun v ->
4139 conf.thumbw <- min 4096 v;
4140 match oldmode with
4141 | Birdseye beye ->
4142 leavebirdseye beye false;
4143 enterbirdseye ()
4144 | _ -> ()
4147 let mode = state.mode in
4148 src#string "columns"
4149 (fun () ->
4150 match conf.columns with
4151 | Csingle -> "1"
4152 | Cmulti (multi, _) -> multicolumns_to_string multi
4153 | Csplit (count, _) -> "-" ^ string_of_int count
4155 (fun v ->
4156 let n, a, b = multicolumns_of_string v in
4157 setcolumns mode n a b);
4159 sep ();
4160 src#caption "Presentation mode" 0;
4161 src#bool "scrollbar visible"
4162 (fun () -> conf.scrollbarinpm)
4163 (fun v ->
4164 if v != conf.scrollbarinpm
4165 then (
4166 conf.scrollbarinpm <- v;
4167 if conf.presentation
4168 then (
4169 state.scrollw <- if v then conf.scrollbw else 0;
4170 reshape conf.winw conf.winh;
4175 sep ();
4176 src#caption "Pixmap cache" 0;
4177 src#int_with_suffix "size (advisory)"
4178 (fun () -> conf.memlimit)
4179 (fun v -> conf.memlimit <- v);
4181 src#caption2 "used"
4182 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4183 (string_with_suffix_of_int state.memused)
4184 (Hashtbl.length state.tilemap)) 1;
4186 sep ();
4187 src#caption "Layout" 0;
4188 src#caption2 "Dimension"
4189 (fun () ->
4190 Printf.sprintf "%dx%d (virtual %dx%d)"
4191 conf.winw conf.winh
4192 state.w state.maxy)
4194 if conf.debug
4195 then
4196 src#caption2 "Position" (fun () ->
4197 Printf.sprintf "%dx%d" state.x state.y
4199 else
4200 src#caption2 "Visible" (fun () -> describe_location ()) 1
4203 sep ();
4204 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4205 "Save these parameters as global defaults at exit"
4206 (fun () -> conf.bedefault)
4207 (fun v -> conf.bedefault <- v)
4210 sep ();
4211 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4212 src#bool ~offset:0 ~btos "Extended parameters"
4213 (fun () -> !showextended)
4214 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4215 if !showextended
4216 then (
4217 src#bool "checkers"
4218 (fun () -> conf.checkers)
4219 (fun v -> conf.checkers <- v; setcheckers v);
4220 src#bool "update cursor"
4221 (fun () -> conf.updatecurs)
4222 (fun v -> conf.updatecurs <- v);
4223 src#bool "verbose"
4224 (fun () -> conf.verbose)
4225 (fun v -> conf.verbose <- v);
4226 src#bool "invert colors"
4227 (fun () -> conf.invert)
4228 (fun v -> conf.invert <- v);
4229 src#bool "max fit"
4230 (fun () -> conf.maxhfit)
4231 (fun v -> conf.maxhfit <- v);
4232 src#bool "redirect stderr"
4233 (fun () -> conf.redirectstderr)
4234 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4235 src#string "uri launcher"
4236 (fun () -> conf.urilauncher)
4237 (fun v -> conf.urilauncher <- v);
4238 src#string "path launcher"
4239 (fun () -> conf.pathlauncher)
4240 (fun v -> conf.pathlauncher <- v);
4241 src#string "tile size"
4242 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4243 (fun v ->
4245 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4246 conf.tilew <- max 64 w;
4247 conf.tileh <- max 64 h;
4248 flushtiles ();
4249 with exn ->
4250 state.text <- Printf.sprintf "bad tile size `%s': %s"
4251 v (Printexc.to_string exn));
4252 src#int "texture count"
4253 (fun () -> conf.texcount)
4254 (fun v ->
4255 if realloctexts v
4256 then conf.texcount <- v
4257 else showtext '!' " Failed to set texture count please retry later"
4259 src#int "slice height"
4260 (fun () -> conf.sliceheight)
4261 (fun v ->
4262 conf.sliceheight <- v;
4263 wcmd "sliceh %d" conf.sliceheight;
4265 src#int "anti-aliasing level"
4266 (fun () -> conf.aalevel)
4267 (fun v ->
4268 conf.aalevel <- bound v 0 8;
4269 state.anchor <- getanchor ();
4270 opendoc state.path state.password;
4272 src#int "ui font size"
4273 (fun () -> fstate.fontsize)
4274 (fun v -> setfontsize (bound v 5 100));
4275 colorp "background color"
4276 (fun () -> conf.bgcolor)
4277 (fun v -> conf.bgcolor <- v);
4278 src#bool "crop hack"
4279 (fun () -> conf.crophack)
4280 (fun v -> conf.crophack <- v);
4281 src#string "trim fuzz"
4282 (fun () -> irect_to_string conf.trimfuzz)
4283 (fun v ->
4285 conf.trimfuzz <- irect_of_string v;
4286 if conf.trimmargins
4287 then settrim true conf.trimfuzz;
4288 with exn ->
4289 state.text <- Printf.sprintf "bad irect `%s': %s"
4290 v (Printexc.to_string exn)
4292 src#string "throttle"
4293 (fun () ->
4294 match conf.maxwait with
4295 | None -> "show place holder if page is not ready"
4296 | Some time ->
4297 if time = infinity
4298 then "wait for page to fully render"
4299 else
4300 "wait " ^ string_of_float time
4301 ^ " seconds before showing placeholder"
4303 (fun v ->
4305 let f = float_of_string v in
4306 if f <= 0.0
4307 then conf.maxwait <- None
4308 else conf.maxwait <- Some f
4309 with exn ->
4310 state.text <- Printf.sprintf "bad time `%s': %s"
4311 v (Printexc.to_string exn)
4313 src#string "ghyll scroll"
4314 (fun () ->
4315 match conf.ghyllscroll with
4316 | None -> ""
4317 | Some nab -> ghyllscroll_to_string nab
4319 (fun v ->
4321 let gs =
4322 if String.length v = 0
4323 then None
4324 else Some (ghyllscroll_of_string v)
4326 conf.ghyllscroll <- gs
4327 with exn ->
4328 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4329 v (Printexc.to_string exn)
4331 src#string "selection command"
4332 (fun () -> conf.selcmd)
4333 (fun v -> conf.selcmd <- v);
4334 src#colorspace "color space"
4335 (fun () -> colorspace_to_string conf.colorspace)
4336 (fun v ->
4337 conf.colorspace <- colorspace_of_int v;
4338 wcmd "cs %d" v;
4339 load state.layout;
4343 sep ();
4344 src#caption "Document" 0;
4345 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4346 src#caption2 "Pages"
4347 (fun () -> string_of_int state.pagecount) 1;
4348 src#caption2 "Dimensions"
4349 (fun () -> string_of_int (List.length state.pdims)) 1;
4350 if conf.trimmargins
4351 then (
4352 sep ();
4353 src#caption "Trimmed margins" 0;
4354 src#caption2 "Dimensions"
4355 (fun () -> string_of_int (List.length state.pdims)) 1;
4358 src#reset prevmode prevuioh;
4360 fun () ->
4361 state.text <- "";
4362 let prevmode = state.mode
4363 and prevuioh = state.uioh in
4364 fillsrc prevmode prevuioh;
4365 let source = (src :> lvsource) in
4366 let modehash = findkeyhash conf "info" in
4367 state.uioh <- coe (object (self)
4368 inherit listview ~source ~trusted:true ~modehash as super
4369 val mutable m_prevmemused = 0
4370 method infochanged = function
4371 | Memused ->
4372 if m_prevmemused != state.memused
4373 then (
4374 m_prevmemused <- state.memused;
4375 G.postRedisplay "memusedchanged";
4377 | Pdim -> G.postRedisplay "pdimchanged"
4378 | Docinfo -> fillsrc prevmode prevuioh
4380 method key key mask =
4381 if not (Wsi.withctrl mask)
4382 then
4383 match key with
4384 | 0xff51 -> coe (self#updownlevel ~-1)
4385 | 0xff53 -> coe (self#updownlevel 1)
4386 | _ -> super#key key mask
4387 else super#key key mask
4388 end);
4389 G.postRedisplay "info";
4392 let enterhelpmode =
4393 let source =
4394 (object
4395 inherit lvsourcebase
4396 method getitemcount = Array.length state.help
4397 method getitem n =
4398 let s, n, _ = state.help.(n) in
4399 (s, n)
4401 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4402 let optuioh =
4403 if not cancel
4404 then (
4405 m_qsearch <- qsearch;
4406 match state.help.(active) with
4407 | _, _, Action f -> Some (f uioh)
4408 | _ -> Some (uioh)
4410 else None
4412 m_active <- active;
4413 m_first <- first;
4414 m_pan <- pan;
4415 optuioh
4417 method hasaction n =
4418 match state.help.(n) with
4419 | _, _, Action _ -> true
4420 | _ -> false
4422 initializer
4423 m_active <- -1
4424 end)
4425 in fun () ->
4426 let modehash = findkeyhash conf "help" in
4427 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4428 G.postRedisplay "help";
4431 let entermsgsmode =
4432 let msgsource =
4433 let re = Str.regexp "[\r\n]" in
4434 (object
4435 inherit lvsourcebase
4436 val mutable m_items = [||]
4438 method getitemcount = 1 + Array.length m_items
4440 method getitem n =
4441 if n = 0
4442 then "[Clear]", 0
4443 else m_items.(n-1), 0
4445 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4446 ignore uioh;
4447 if not cancel
4448 then (
4449 if active = 0
4450 then Buffer.clear state.errmsgs;
4451 m_qsearch <- qsearch;
4453 m_active <- active;
4454 m_first <- first;
4455 m_pan <- pan;
4456 None
4458 method hasaction n =
4459 n = 0
4461 method reset =
4462 state.newerrmsgs <- false;
4463 let l = Str.split re (Buffer.contents state.errmsgs) in
4464 m_items <- Array.of_list l
4466 initializer
4467 m_active <- 0
4468 end)
4469 in fun () ->
4470 state.text <- "";
4471 msgsource#reset;
4472 let source = (msgsource :> lvsource) in
4473 let modehash = findkeyhash conf "listview" in
4474 state.uioh <- coe (object
4475 inherit listview ~source ~trusted:false ~modehash as super
4476 method display =
4477 if state.newerrmsgs
4478 then msgsource#reset;
4479 super#display
4480 end);
4481 G.postRedisplay "msgs";
4484 let quickbookmark ?title () =
4485 match state.layout with
4486 | [] -> ()
4487 | l :: _ ->
4488 let title =
4489 match title with
4490 | None ->
4491 let sec = Unix.gettimeofday () in
4492 let tm = Unix.localtime sec in
4493 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4494 (l.pageno+1)
4495 tm.Unix.tm_mday
4496 tm.Unix.tm_mon
4497 (tm.Unix.tm_year + 1900)
4498 tm.Unix.tm_hour
4499 tm.Unix.tm_min
4500 | Some title -> title
4502 state.bookmarks <-
4503 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4504 :: state.bookmarks
4507 let doreshape w h =
4508 state.fullscreen <- None;
4509 Wsi.reshape w h;
4512 let setautoscrollspeed step goingdown =
4513 let incr = max 1 ((abs step) / 2) in
4514 let incr = if goingdown then incr else -incr in
4515 let astep = step + incr in
4516 state.autoscroll <- Some astep;
4519 let gotounder = function
4520 | Ulinkgoto (pageno, top) ->
4521 if pageno >= 0
4522 then (
4523 addnav ();
4524 gotopage1 pageno top;
4527 | Ulinkuri s ->
4528 gotouri s
4530 | Uremote (filename, pageno) ->
4531 let path =
4532 if Sys.file_exists filename
4533 then filename
4534 else
4535 let dir = Filename.dirname state.path in
4536 let path = Filename.concat dir filename in
4537 if Sys.file_exists path
4538 then path
4539 else ""
4541 if String.length path > 0
4542 then (
4543 let anchor = getanchor () in
4544 let ranchor = state.path, state.password, anchor in
4545 state.anchor <- (pageno, 0.0);
4546 state.ranchors <- ranchor :: state.ranchors;
4547 opendoc path "";
4549 else showtext '!' ("Could not find " ^ filename)
4551 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4554 let canpan () =
4555 match conf.columns with
4556 | Csplit _ -> true
4557 | _ -> conf.zoom > 1.0
4560 let viewkeyboard key mask =
4561 let enttext te =
4562 let mode = state.mode in
4563 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4564 state.text <- "";
4565 enttext ();
4566 G.postRedisplay "view:enttext"
4568 let ctrl = Wsi.withctrl mask in
4569 match key with
4570 | 81 -> (* Q *)
4571 exit 0
4573 | 0xff63 -> (* insert *)
4574 if conf.angle mod 360 = 0
4575 then (
4576 state.mode <- LinkNav (Ltgendir 0);
4577 gotoy state.y;
4579 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4581 | 0xff1b | 113 -> (* escape / q *)
4582 begin match state.mstate with
4583 | Mzoomrect _ ->
4584 state.mstate <- Mnone;
4585 Wsi.setcursor Wsi.CURSOR_INHERIT;
4586 G.postRedisplay "kill zoom rect";
4587 | _ ->
4588 match state.ranchors with
4589 | [] -> raise Quit
4590 | (path, password, anchor) :: rest ->
4591 state.ranchors <- rest;
4592 state.anchor <- anchor;
4593 opendoc path password
4594 end;
4596 | 0xff08 -> (* backspace *)
4597 let y = getnav ~-1 in
4598 gotoy_and_clear_text y
4600 | 111 -> (* o *)
4601 enteroutlinemode ()
4603 | 117 -> (* u *)
4604 state.rects <- [];
4605 state.text <- "";
4606 G.postRedisplay "dehighlight";
4608 | 47 | 63 -> (* / ? *)
4609 let ondone isforw s =
4610 cbput state.hists.pat s;
4611 state.searchpattern <- s;
4612 search s isforw
4614 let s = String.create 1 in
4615 s.[0] <- Char.chr key;
4616 enttext (s, "", Some (onhist state.hists.pat),
4617 textentry, ondone (key = 47))
4619 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4620 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4621 setzoom (conf.zoom +. incr)
4623 | 43 | 0xffab -> (* + *)
4624 let ondone s =
4625 let n =
4626 try int_of_string s with exc ->
4627 state.text <- Printf.sprintf "bad integer `%s': %s"
4628 s (Printexc.to_string exc);
4629 max_int
4631 if n != max_int
4632 then (
4633 conf.pagebias <- n;
4634 state.text <- "page bias is now " ^ string_of_int n;
4637 enttext ("page bias: ", "", None, intentry, ondone)
4639 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4640 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4641 setzoom (max 0.01 (conf.zoom -. decr))
4643 | 45 | 0xffad -> (* - *)
4644 let ondone msg = state.text <- msg in
4645 enttext (
4646 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4647 optentry state.mode, ondone
4650 | 48 when ctrl -> (* ctrl-0 *)
4651 setzoom 1.0
4653 | 49 when ctrl -> (* 1 *)
4654 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4655 if zoom < 1.0
4656 then setzoom zoom
4658 | 0xffc6 -> (* f9 *)
4659 togglebirdseye ()
4661 | 57 when ctrl -> (* ctrl-9 *)
4662 togglebirdseye ()
4664 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4665 when not ctrl -> (* 0..9 *)
4666 let ondone s =
4667 let n =
4668 try int_of_string s with exc ->
4669 state.text <- Printf.sprintf "bad integer `%s': %s"
4670 s (Printexc.to_string exc);
4673 if n >= 0
4674 then (
4675 addnav ();
4676 cbput state.hists.pag (string_of_int n);
4677 gotopage1 (n + conf.pagebias - 1) 0;
4680 let pageentry text key =
4681 match Char.unsafe_chr key with
4682 | 'g' -> TEdone text
4683 | _ -> intentry text key
4685 let text = "x" in text.[0] <- Char.chr key;
4686 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4688 | 98 -> (* b *)
4689 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4690 reshape conf.winw conf.winh;
4692 | 108 -> (* l *)
4693 conf.hlinks <- not conf.hlinks;
4694 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4695 G.postRedisplay "toggle highlightlinks";
4697 | 70 -> (* F *)
4698 state.glinks <- true;
4699 let mode = state.mode in
4700 state.mode <- Textentry (
4701 (":", "", None, linknentry, linkndone (fun under ->
4702 addnav ();
4703 gotounder under
4705 ), fun _ ->
4706 state.glinks <- false;
4707 state.mode <- mode
4709 state.text <- "";
4710 G.postRedisplay "view:linkent(F)"
4712 | 121 -> (* y *)
4713 state.glinks <- true;
4714 let mode = state.mode in
4715 state.mode <- Textentry (
4716 (":", "", None, linknentry, linkndone (fun under ->
4717 let rw =
4719 Some (Unix.pipe ())
4720 with exn ->
4721 showtext '!' (Printf.sprintf "pipe failed: %s"
4722 (Printexc.to_string exn));
4723 None
4725 match rw with
4726 | Some (r, w) ->
4727 begin try
4728 popen conf.selcmd [r, 0; w, -1]
4729 with exn ->
4730 showtext '!'
4731 (Printf.sprintf "failed to execute %s: %s"
4732 conf.selcmd (Printexc.to_string exn))
4733 end;
4734 let clo cap fd =
4735 try Unix.close fd
4736 with exn ->
4737 showtext '!' (Printf.sprintf "failed to close %s: %s"
4738 cap (Printexc.to_string exn))
4740 let s = undertext under in
4741 (try
4742 let l = String.length s in
4743 let n = Unix.write w s 0 l in
4744 if n != l
4745 then
4746 showtext '!'
4747 (Printf.sprintf
4748 "failed to write %d characters to sel pipe, wrote %d"
4751 with exn ->
4752 showtext '!'
4753 (Printf.sprintf "failed to write to sel pipe: %s"
4754 (Printexc.to_string exn)
4757 clo "pipe/r" r;
4758 clo "pipe/w" w;
4759 | None -> ()
4762 fun _ ->
4763 state.glinks <- false;
4764 state.mode <- mode
4766 state.text <- "";
4767 G.postRedisplay "view:linkent"
4769 | 97 -> (* a *)
4770 begin match state.autoscroll with
4771 | Some step ->
4772 conf.autoscrollstep <- step;
4773 state.autoscroll <- None
4774 | None ->
4775 if conf.autoscrollstep = 0
4776 then state.autoscroll <- Some 1
4777 else state.autoscroll <- Some conf.autoscrollstep
4780 | 112 when ctrl -> (* ctrl-p *)
4781 launchpath ()
4783 | 80 -> (* P *)
4784 conf.presentation <- not conf.presentation;
4785 if conf.presentation
4786 then (
4787 if not conf.scrollbarinpm
4788 then state.scrollw <- 0;
4790 else
4791 state.scrollw <- conf.scrollbw;
4793 showtext ' ' ("presentation mode " ^
4794 if conf.presentation then "on" else "off");
4795 state.anchor <- getanchor ();
4796 represent ()
4798 | 102 -> (* f *)
4799 begin match state.fullscreen with
4800 | None ->
4801 state.fullscreen <- Some (conf.winw, conf.winh);
4802 Wsi.fullscreen ()
4803 | Some (w, h) ->
4804 state.fullscreen <- None;
4805 doreshape w h
4808 | 103 -> (* g *)
4809 gotoy_and_clear_text 0
4811 | 71 -> (* G *)
4812 gotopage1 (state.pagecount - 1) 0
4814 | 112 | 78 -> (* p|N *)
4815 search state.searchpattern false
4817 | 110 | 0xffc0 -> (* n|F3 *)
4818 search state.searchpattern true
4820 | 116 -> (* t *)
4821 begin match state.layout with
4822 | [] -> ()
4823 | l :: _ ->
4824 gotoy_and_clear_text (getpagey l.pageno)
4827 | 32 -> (* ' ' *)
4828 begin match List.rev state.layout with
4829 | [] -> ()
4830 | l :: _ ->
4831 let pageno = min (l.pageno+1) (state.pagecount-1) in
4832 gotoy_and_clear_text (getpagey pageno)
4835 | 0xff9f | 0xffff -> (* delete *)
4836 begin match state.layout with
4837 | [] -> ()
4838 | l :: _ ->
4839 let pageno = max 0 (l.pageno-1) in
4840 gotoy_and_clear_text (getpagey pageno)
4843 | 61 -> (* = *)
4844 showtext ' ' (describe_location ());
4846 | 119 -> (* w *)
4847 begin match state.layout with
4848 | [] -> ()
4849 | l :: _ ->
4850 doreshape (l.pagew + state.scrollw) l.pageh;
4851 G.postRedisplay "w"
4854 | 39 -> (* ' *)
4855 enterbookmarkmode ()
4857 | 104 | 0xffbe -> (* h|F1 *)
4858 enterhelpmode ()
4860 | 105 -> (* i *)
4861 enterinfomode ()
4863 | 101 when conf.redirectstderr -> (* e *)
4864 entermsgsmode ()
4866 | 109 -> (* m *)
4867 let ondone s =
4868 match state.layout with
4869 | l :: _ ->
4870 state.bookmarks <-
4871 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4872 :: state.bookmarks
4873 | _ -> ()
4875 enttext ("bookmark: ", "", None, textentry, ondone)
4877 | 126 -> (* ~ *)
4878 quickbookmark ();
4879 showtext ' ' "Quick bookmark added";
4881 | 122 -> (* z *)
4882 begin match state.layout with
4883 | l :: _ ->
4884 let rect = getpdimrect l.pagedimno in
4885 let w, h =
4886 if conf.crophack
4887 then
4888 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4889 truncate (1.2 *. (rect.(3) -. rect.(0))))
4890 else
4891 (truncate (rect.(1) -. rect.(0)),
4892 truncate (rect.(3) -. rect.(0)))
4894 let w = truncate ((float w)*.conf.zoom)
4895 and h = truncate ((float h)*.conf.zoom) in
4896 if w != 0 && h != 0
4897 then (
4898 state.anchor <- getanchor ();
4899 doreshape (w + state.scrollw) (h + conf.interpagespace)
4901 G.postRedisplay "z";
4903 | [] -> ()
4906 | 50 when ctrl -> (* ctrl-2 *)
4907 let maxw = getmaxw () in
4908 if maxw > 0.0
4909 then setzoom (maxw /. float conf.winw)
4911 | 60 | 62 -> (* < > *)
4912 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4914 | 91 | 93 -> (* [ ] *)
4915 conf.colorscale <-
4916 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4918 G.postRedisplay "brightness";
4920 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4921 setzoom state.prevzoom
4923 | 107 | 0xff52 -> (* k up *)
4924 begin match state.autoscroll with
4925 | None ->
4926 begin match state.mode with
4927 | Birdseye beye -> upbirdseye 1 beye
4928 | _ ->
4929 if ctrl
4930 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4931 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4933 | Some n ->
4934 setautoscrollspeed n false
4937 | 106 | 0xff54 -> (* j down *)
4938 begin match state.autoscroll with
4939 | None ->
4940 begin match state.mode with
4941 | Birdseye beye -> downbirdseye 1 beye
4942 | _ ->
4943 if ctrl
4944 then gotoy_and_clear_text (clamp (conf.winh/2))
4945 else gotoy_and_clear_text (clamp conf.scrollstep)
4947 | Some n ->
4948 setautoscrollspeed n true
4951 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4952 if canpan ()
4953 then
4954 let dx =
4955 if ctrl
4956 then conf.winw / 2
4957 else 10
4959 let dx = if key = 0xff51 then dx else -dx in
4960 state.x <- state.x + dx;
4961 gotoy_and_clear_text state.y
4962 else (
4963 state.text <- "";
4964 G.postRedisplay "lef/right"
4967 | 0xff55 -> (* prior *)
4968 let y =
4969 if ctrl
4970 then
4971 match state.layout with
4972 | [] -> state.y
4973 | l :: _ -> state.y - l.pagey
4974 else
4975 clamp (-conf.winh)
4977 gotoghyll y
4979 | 0xff56 -> (* next *)
4980 let y =
4981 if ctrl
4982 then
4983 match List.rev state.layout with
4984 | [] -> state.y
4985 | l :: _ -> getpagey l.pageno
4986 else
4987 clamp conf.winh
4989 gotoghyll y
4991 | 0xff50 -> gotoghyll 0
4992 | 0xff57 -> gotoghyll (clamp state.maxy)
4993 | 0xff53 when Wsi.withalt mask ->
4994 gotoghyll (getnav ~-1)
4995 | 0xff51 when Wsi.withalt mask ->
4996 gotoghyll (getnav 1)
4998 | 114 -> (* r *)
4999 state.anchor <- getanchor ();
5000 opendoc state.path state.password
5002 | 118 when conf.debug -> (* v *)
5003 state.rects <- [];
5004 List.iter (fun l ->
5005 match getopaque l.pageno with
5006 | None -> ()
5007 | Some opaque ->
5008 let x0, y0, x1, y1 = pagebbox opaque in
5009 let a,b = float x0, float y0 in
5010 let c,d = float x1, float y0 in
5011 let e,f = float x1, float y1 in
5012 let h,j = float x0, float y1 in
5013 let rect = (a,b,c,d,e,f,h,j) in
5014 debugrect rect;
5015 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5016 ) state.layout;
5017 G.postRedisplay "v";
5019 | _ ->
5020 vlog "huh? %s" (Wsi.keyname key)
5023 let linknavkeyboard key mask linknav =
5024 let getpage pageno =
5025 let rec loop = function
5026 | [] -> None
5027 | l :: _ when l.pageno = pageno -> Some l
5028 | _ :: rest -> loop rest
5029 in loop state.layout
5031 let doexact (pageno, n) =
5032 match getopaque pageno, getpage pageno with
5033 | Some opaque, Some l ->
5034 if key = 0xff0d
5035 then
5036 let under = getlink opaque n in
5037 G.postRedisplay "link gotounder";
5038 gotounder under;
5039 state.mode <- View;
5040 else
5041 let opt, dir =
5042 match key with
5043 | 0xff50 -> (* home *)
5044 Some (findlink opaque LDfirst), -1
5046 | 0xff57 -> (* end *)
5047 Some (findlink opaque LDlast), 1
5049 | 0xff51 -> (* left *)
5050 Some (findlink opaque (LDleft n)), -1
5052 | 0xff53 -> (* right *)
5053 Some (findlink opaque (LDright n)), 1
5055 | 0xff52 -> (* up *)
5056 Some (findlink opaque (LDup n)), -1
5058 | 0xff54 -> (* down *)
5059 Some (findlink opaque (LDdown n)), 1
5061 | _ -> None, 0
5063 let pwl l dir =
5064 begin match findpwl l.pageno dir with
5065 | Pwlnotfound -> ()
5066 | Pwl pageno ->
5067 let notfound dir =
5068 state.mode <- LinkNav (Ltgendir dir);
5069 let y, h = getpageyh pageno in
5070 let y =
5071 if dir < 0
5072 then y + h - conf.winh
5073 else y
5075 gotoy y
5077 begin match getopaque pageno, getpage pageno with
5078 | Some opaque, Some _ ->
5079 let link =
5080 let ld = if dir > 0 then LDfirst else LDlast in
5081 findlink opaque ld
5083 begin match link with
5084 | Lfound m ->
5085 showlinktype (getlink opaque m);
5086 state.mode <- LinkNav (Ltexact (pageno, m));
5087 G.postRedisplay "linknav jpage";
5088 | _ -> notfound dir
5089 end;
5090 | _ -> notfound dir
5091 end;
5092 end;
5094 begin match opt with
5095 | Some Lnotfound -> pwl l dir;
5096 | Some (Lfound m) ->
5097 if m = n
5098 then pwl l dir
5099 else (
5100 let _, y0, _, y1 = getlinkrect opaque m in
5101 if y0 < l.pagey
5102 then gotopage1 l.pageno y0
5103 else (
5104 let d = fstate.fontsize + 1 in
5105 if y1 - l.pagey > l.pagevh - d
5106 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5107 else G.postRedisplay "linknav";
5109 showlinktype (getlink opaque m);
5110 state.mode <- LinkNav (Ltexact (l.pageno, m));
5113 | None -> viewkeyboard key mask
5114 end;
5115 | _ -> viewkeyboard key mask
5117 if key = 0xff63
5118 then (
5119 state.mode <- View;
5120 G.postRedisplay "leave linknav"
5122 else
5123 match linknav with
5124 | Ltgendir _ -> viewkeyboard key mask
5125 | Ltexact exact -> doexact exact
5128 let keyboard key mask =
5129 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5130 then wcmd "interrupt"
5131 else state.uioh <- state.uioh#key key mask
5134 let birdseyekeyboard key mask
5135 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5136 let incr =
5137 match conf.columns with
5138 | Csingle -> 1
5139 | Cmulti ((c, _, _), _) -> c
5140 | Csplit _ -> failwith "bird's eye split mode"
5142 match key with
5143 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5144 let y, h = getpageyh pageno in
5145 let top = (conf.winh - h) / 2 in
5146 gotoy (max 0 (y - top))
5147 | 0xff0d -> leavebirdseye beye false
5148 | 0xff1b -> leavebirdseye beye true (* escape *)
5149 | 0xff52 -> upbirdseye incr beye (* prior *)
5150 | 0xff54 -> downbirdseye incr beye (* next *)
5151 | 0xff51 -> upbirdseye 1 beye (* up *)
5152 | 0xff53 -> downbirdseye 1 beye (* down *)
5154 | 0xff55 ->
5155 begin match state.layout with
5156 | l :: _ ->
5157 if l.pagey != 0
5158 then (
5159 state.mode <- Birdseye (
5160 oconf, leftx, l.pageno, hooverpageno, anchor
5162 gotopage1 l.pageno 0;
5164 else (
5165 let layout = layout (state.y-conf.winh) conf.winh in
5166 match layout with
5167 | [] -> gotoy (clamp (-conf.winh))
5168 | l :: _ ->
5169 state.mode <- Birdseye (
5170 oconf, leftx, l.pageno, hooverpageno, anchor
5172 gotopage1 l.pageno 0
5175 | [] -> gotoy (clamp (-conf.winh))
5176 end;
5178 | 0xff56 ->
5179 begin match List.rev state.layout with
5180 | l :: _ ->
5181 let layout = layout (state.y + conf.winh) conf.winh in
5182 begin match layout with
5183 | [] ->
5184 let incr = l.pageh - l.pagevh in
5185 if incr = 0
5186 then (
5187 state.mode <-
5188 Birdseye (
5189 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5191 G.postRedisplay "birdseye pagedown";
5193 else gotoy (clamp (incr + conf.interpagespace*2));
5195 | l :: _ ->
5196 state.mode <-
5197 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5198 gotopage1 l.pageno 0;
5201 | [] -> gotoy (clamp conf.winh)
5202 end;
5204 | 0xff50 ->
5205 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5206 gotopage1 0 0
5208 | 0xff57 ->
5209 let pageno = state.pagecount - 1 in
5210 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5211 if not (pagevisible state.layout pageno)
5212 then
5213 let h =
5214 match List.rev state.pdims with
5215 | [] -> conf.winh
5216 | (_, _, h, _) :: _ -> h
5218 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5219 else G.postRedisplay "birdseye end";
5220 | _ -> viewkeyboard key mask
5223 let drawpage l linkindexbase =
5224 let color =
5225 match state.mode with
5226 | Textentry _ -> scalecolor 0.4
5227 | LinkNav _
5228 | View -> scalecolor 1.0
5229 | Birdseye (_, _, pageno, hooverpageno, _) ->
5230 if l.pageno = hooverpageno
5231 then scalecolor 0.9
5232 else (
5233 if l.pageno = pageno
5234 then scalecolor 1.0
5235 else scalecolor 0.8
5238 drawtiles l color;
5239 begin match getopaque l.pageno with
5240 | Some opaque ->
5241 if tileready l l.pagex l.pagey
5242 then
5243 let x = l.pagedispx - l.pagex
5244 and y = l.pagedispy - l.pagey in
5245 let hlmask = (if conf.hlinks then 1 else 0)
5246 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5248 let s =
5249 match state.mode with
5250 | Textentry ((_, s, _, _, _), _) when state.glinks -> s
5251 | _ -> ""
5253 postprocess opaque hlmask x y (linkindexbase, s);
5254 else 0
5256 | _ -> 0
5257 end;
5260 let scrollindicator () =
5261 let sbw, ph, sh = state.uioh#scrollph in
5262 let sbh, pw, sw = state.uioh#scrollpw in
5264 GlDraw.color (0.64, 0.64, 0.64);
5265 GlDraw.rect
5266 (float (conf.winw - sbw), 0.)
5267 (float conf.winw, float conf.winh)
5269 GlDraw.rect
5270 (0., float (conf.winh - sbh))
5271 (float (conf.winw - state.scrollw - 1), float conf.winh)
5273 GlDraw.color (0.0, 0.0, 0.0);
5275 GlDraw.rect
5276 (float (conf.winw - sbw), ph)
5277 (float conf.winw, ph +. sh)
5279 GlDraw.rect
5280 (pw, float (conf.winh - sbh))
5281 (pw +. sw, float conf.winh)
5285 let showsel () =
5286 match state.mstate with
5287 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5290 | Msel ((x0, y0), (x1, y1)) ->
5291 let rec loop = function
5292 | l :: ls ->
5293 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5294 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5295 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5296 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5297 then
5298 match getopaque l.pageno with
5299 | Some opaque ->
5300 let x0, y0 = pagetranslatepoint l x0 y0 in
5301 let x1, y1 = pagetranslatepoint l x1 y1 in
5302 seltext opaque (x0, y0, x1, y1);
5303 | _ -> ()
5304 else loop ls
5305 | [] -> ()
5307 loop state.layout
5310 let showrects rects =
5311 Gl.enable `blend;
5312 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5313 GlDraw.polygon_mode `both `fill;
5314 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5315 List.iter
5316 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5317 List.iter (fun l ->
5318 if l.pageno = pageno
5319 then (
5320 let dx = float (l.pagedispx - l.pagex) in
5321 let dy = float (l.pagedispy - l.pagey) in
5322 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5323 GlDraw.begins `quads;
5325 GlDraw.vertex2 (x0+.dx, y0+.dy);
5326 GlDraw.vertex2 (x1+.dx, y1+.dy);
5327 GlDraw.vertex2 (x2+.dx, y2+.dy);
5328 GlDraw.vertex2 (x3+.dx, y3+.dy);
5330 GlDraw.ends ();
5332 ) state.layout
5333 ) rects
5335 Gl.disable `blend;
5338 let display () =
5339 GlClear.color (scalecolor2 conf.bgcolor);
5340 GlClear.clear [`color];
5341 let rec loop linkindexbase = function
5342 | l :: rest ->
5343 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5344 loop linkindexbase rest
5345 | [] -> ()
5347 loop 0 state.layout;
5348 let rects =
5349 match state.mode with
5350 | LinkNav (Ltexact (pageno, linkno)) ->
5351 begin match getopaque pageno with
5352 | Some opaque ->
5353 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5354 (pageno, 5, (
5355 float x0, float y0,
5356 float x1, float y0,
5357 float x1, float y1,
5358 float x0, float y1)
5359 ) :: state.rects
5360 | None -> state.rects
5362 | _ -> state.rects
5364 showrects rects;
5365 showsel ();
5366 state.uioh#display;
5367 begin match state.mstate with
5368 | Mzoomrect ((x0, y0), (x1, y1)) ->
5369 Gl.enable `blend;
5370 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5371 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5372 GlDraw.rect (float x0, float y0)
5373 (float x1, float y1);
5374 Gl.disable `blend;
5375 | _ -> ()
5376 end;
5377 enttext ();
5378 scrollindicator ();
5379 Wsi.swapb ();
5382 let zoomrect x y x1 y1 =
5383 let x0 = min x x1
5384 and x1 = max x x1
5385 and y0 = min y y1 in
5386 gotoy (state.y + y0);
5387 state.anchor <- getanchor ();
5388 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5389 let margin =
5390 if state.w < conf.winw - state.scrollw
5391 then (conf.winw - state.scrollw - state.w) / 2
5392 else 0
5394 state.x <- (state.x + margin) - x0;
5395 setzoom zoom;
5396 Wsi.setcursor Wsi.CURSOR_INHERIT;
5397 state.mstate <- Mnone;
5400 let scrollx x =
5401 let winw = conf.winw - state.scrollw - 1 in
5402 let s = float x /. float winw in
5403 let destx = truncate (float (state.w + winw) *. s) in
5404 state.x <- winw - destx;
5405 gotoy_and_clear_text state.y;
5406 state.mstate <- Mscrollx;
5409 let scrolly y =
5410 let s = float y /. float conf.winh in
5411 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5412 gotoy_and_clear_text desty;
5413 state.mstate <- Mscrolly;
5416 let viewmouse button down x y mask =
5417 match button with
5418 | n when (n == 4 || n == 5) && not down ->
5419 if Wsi.withctrl mask
5420 then (
5421 match state.mstate with
5422 | Mzoom (oldn, i) ->
5423 if oldn = n
5424 then (
5425 if i = 2
5426 then
5427 let incr =
5428 match n with
5429 | 5 ->
5430 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5431 | _ ->
5432 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5434 let zoom = conf.zoom -. incr in
5435 setzoom zoom;
5436 state.mstate <- Mzoom (n, 0);
5437 else
5438 state.mstate <- Mzoom (n, i+1);
5440 else state.mstate <- Mzoom (n, 0)
5442 | _ -> state.mstate <- Mzoom (n, 0)
5444 else (
5445 match state.autoscroll with
5446 | Some step -> setautoscrollspeed step (n=4)
5447 | None ->
5448 let incr =
5449 if n = 4
5450 then -conf.scrollstep
5451 else conf.scrollstep
5453 let incr = incr * 2 in
5454 let y = clamp incr in
5455 gotoy_and_clear_text y
5458 | 1 when Wsi.withctrl mask ->
5459 if down
5460 then (
5461 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5462 state.mstate <- Mpan (x, y)
5464 else
5465 state.mstate <- Mnone
5467 | 3 ->
5468 if down
5469 then (
5470 Wsi.setcursor Wsi.CURSOR_CYCLE;
5471 let p = (x, y) in
5472 state.mstate <- Mzoomrect (p, p)
5474 else (
5475 match state.mstate with
5476 | Mzoomrect ((x0, y0), _) ->
5477 if abs (x-x0) > 10 && abs (y - y0) > 10
5478 then zoomrect x0 y0 x y
5479 else (
5480 state.mstate <- Mnone;
5481 Wsi.setcursor Wsi.CURSOR_INHERIT;
5482 G.postRedisplay "kill accidental zoom rect";
5484 | _ ->
5485 Wsi.setcursor Wsi.CURSOR_INHERIT;
5486 state.mstate <- Mnone
5489 | 1 when x > conf.winw - state.scrollw ->
5490 if down
5491 then
5492 let _, position, sh = state.uioh#scrollph in
5493 if y > truncate position && y < truncate (position +. sh)
5494 then state.mstate <- Mscrolly
5495 else scrolly y
5496 else
5497 state.mstate <- Mnone
5499 | 1 when y > conf.winh - state.hscrollh ->
5500 if down
5501 then
5502 let _, position, sw = state.uioh#scrollpw in
5503 if x > truncate position && x < truncate (position +. sw)
5504 then state.mstate <- Mscrollx
5505 else scrollx x
5506 else
5507 state.mstate <- Mnone
5509 | 1 ->
5510 let dest = if down then getunder x y else Unone in
5511 begin match dest with
5512 | Ulinkgoto _
5513 | Ulinkuri _
5514 | Uremote _
5515 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5516 gotounder dest
5518 | Unone when down ->
5519 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5520 state.mstate <- Mpan (x, y);
5522 | Unone | Utext _ ->
5523 if down
5524 then (
5525 if conf.angle mod 360 = 0
5526 then (
5527 state.mstate <- Msel ((x, y), (x, y));
5528 G.postRedisplay "mouse select";
5531 else (
5532 match state.mstate with
5533 | Mnone -> ()
5535 | Mzoom _ | Mscrollx | Mscrolly ->
5536 state.mstate <- Mnone
5538 | Mzoomrect ((x0, y0), _) ->
5539 zoomrect x0 y0 x y
5541 | Mpan _ ->
5542 Wsi.setcursor Wsi.CURSOR_INHERIT;
5543 state.mstate <- Mnone
5545 | Msel ((_, y0), (_, y1)) ->
5546 let rec loop = function
5547 | [] -> ()
5548 | l :: rest ->
5549 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5550 || ((y1 >= l.pagedispy
5551 && y1 <= (l.pagedispy + l.pagevh)))
5552 then
5553 match getopaque l.pageno with
5554 | Some opaque ->
5555 begin
5556 match
5557 try Some (Unix.pipe ())
5558 with exn ->
5559 dolog "can not create sel pipe: %s"
5560 (Printexc.to_string exn);
5561 None
5562 with
5563 | None -> ()
5564 | Some (r, w) ->
5565 let doclose what fd =
5566 try Unix.close fd
5567 with exn ->
5568 dolog "%s close failed: %s"
5569 what (Printexc.to_string exn)
5571 let docopysel r w =
5572 copysel w opaque;
5573 doclose "pipe/r" r;
5574 G.postRedisplay "copysel"
5577 popen conf.selcmd [r, 0; w, -1];
5578 docopysel r w;
5579 with exn ->
5580 dolog "can not exectute %S: %s"
5581 conf.selcmd (Printexc.to_string exn);
5582 doclose "pipe/r" r;
5583 doclose "pipe/w" w;
5585 | None -> ()
5586 else loop rest
5588 loop state.layout;
5589 Wsi.setcursor Wsi.CURSOR_INHERIT;
5590 state.mstate <- Mnone;
5594 | _ -> ()
5597 let birdseyemouse button down x y mask
5598 (conf, leftx, _, hooverpageno, anchor) =
5599 match button with
5600 | 1 when down ->
5601 let rec loop = function
5602 | [] -> ()
5603 | l :: rest ->
5604 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5605 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5606 then (
5607 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5609 else loop rest
5611 loop state.layout
5612 | 3 -> ()
5613 | _ -> viewmouse button down x y mask
5616 let mouse button down x y mask =
5617 state.uioh <- state.uioh#button button down x y mask;
5620 let motion ~x ~y =
5621 state.uioh <- state.uioh#motion x y
5624 let pmotion ~x ~y =
5625 state.uioh <- state.uioh#pmotion x y;
5628 let uioh = object
5629 method display = ()
5631 method key key mask =
5632 begin match state.mode with
5633 | Textentry textentry -> textentrykeyboard key mask textentry
5634 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5635 | View -> viewkeyboard key mask
5636 | LinkNav linknav -> linknavkeyboard key mask linknav
5637 end;
5638 state.uioh
5640 method button button bstate x y mask =
5641 begin match state.mode with
5642 | LinkNav _
5643 | View -> viewmouse button bstate x y mask
5644 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5645 | Textentry _ -> ()
5646 end;
5647 state.uioh
5649 method motion x y =
5650 begin match state.mode with
5651 | Textentry _ -> ()
5652 | View | Birdseye _ | LinkNav _ ->
5653 match state.mstate with
5654 | Mzoom _ | Mnone -> ()
5656 | Mpan (x0, y0) ->
5657 let dx = x - x0
5658 and dy = y0 - y in
5659 state.mstate <- Mpan (x, y);
5660 if canpan ()
5661 then state.x <- state.x + dx;
5662 let y = clamp dy in
5663 gotoy_and_clear_text y
5665 | Msel (a, _) ->
5666 state.mstate <- Msel (a, (x, y));
5667 G.postRedisplay "motion select";
5669 | Mscrolly ->
5670 let y = min conf.winh (max 0 y) in
5671 scrolly y
5673 | Mscrollx ->
5674 let x = min conf.winw (max 0 x) in
5675 scrollx x
5677 | Mzoomrect (p0, _) ->
5678 state.mstate <- Mzoomrect (p0, (x, y));
5679 G.postRedisplay "motion zoomrect";
5680 end;
5681 state.uioh
5683 method pmotion x y =
5684 begin match state.mode with
5685 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5686 let rec loop = function
5687 | [] ->
5688 if hooverpageno != -1
5689 then (
5690 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5691 G.postRedisplay "pmotion birdseye no hoover";
5693 | l :: rest ->
5694 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5695 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5696 then (
5697 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5698 G.postRedisplay "pmotion birdseye hoover";
5700 else loop rest
5702 loop state.layout
5704 | Textentry _ -> ()
5706 | LinkNav _
5707 | View ->
5708 match state.mstate with
5709 | Mnone -> updateunder x y
5710 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5712 end;
5713 state.uioh
5715 method infochanged _ = ()
5717 method scrollph =
5718 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5719 let p, h = scrollph state.y maxy in
5720 state.scrollw, p, h
5722 method scrollpw =
5723 let winw = conf.winw - state.scrollw - 1 in
5724 let fwinw = float winw in
5725 let sw =
5726 let sw = fwinw /. float state.w in
5727 let sw = fwinw *. sw in
5728 max sw (float conf.scrollh)
5730 let position, sw =
5731 let f = state.w+winw in
5732 let r = float (winw-state.x) /. float f in
5733 let p = fwinw *. r in
5734 p-.sw/.2., sw
5736 let sw =
5737 if position +. sw > fwinw
5738 then fwinw -. position
5739 else sw
5741 state.hscrollh, position, sw
5743 method modehash =
5744 let modename =
5745 match state.mode with
5746 | LinkNav _ -> "links"
5747 | Textentry _ -> "textentry"
5748 | Birdseye _ -> "birdseye"
5749 | View -> "global"
5751 findkeyhash conf modename
5752 end;;
5754 module Config =
5755 struct
5756 open Parser
5758 let fontpath = ref "";;
5760 module KeyMap =
5761 Map.Make (struct type t = (int * int) let compare = compare end);;
5763 let unent s =
5764 let l = String.length s in
5765 let b = Buffer.create l in
5766 unent b s 0 l;
5767 Buffer.contents b;
5770 let home =
5771 try Sys.getenv "HOME"
5772 with exn ->
5773 prerr_endline
5774 ("Can not determine home directory location: " ^
5775 Printexc.to_string exn);
5779 let modifier_of_string = function
5780 | "alt" -> Wsi.altmask
5781 | "shift" -> Wsi.shiftmask
5782 | "ctrl" | "control" -> Wsi.ctrlmask
5783 | "meta" -> Wsi.metamask
5784 | _ -> 0
5787 let key_of_string =
5788 let r = Str.regexp "-" in
5789 fun s ->
5790 let elems = Str.full_split r s in
5791 let f n k m =
5792 let g s =
5793 let m1 = modifier_of_string s in
5794 if m1 = 0
5795 then (Wsi.namekey s, m)
5796 else (k, m lor m1)
5797 in function
5798 | Str.Delim s when n land 1 = 0 -> g s
5799 | Str.Text s -> g s
5800 | Str.Delim _ -> (k, m)
5802 let rec loop n k m = function
5803 | [] -> (k, m)
5804 | x :: xs ->
5805 let k, m = f n k m x in
5806 loop (n+1) k m xs
5808 loop 0 0 0 elems
5811 let keys_of_string =
5812 let r = Str.regexp "[ \t]" in
5813 fun s ->
5814 let elems = Str.split r s in
5815 List.map key_of_string elems
5818 let copykeyhashes c =
5819 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5822 let config_of c attrs =
5823 let apply c k v =
5825 match k with
5826 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5827 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5828 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5829 | "preload" -> { c with preload = bool_of_string v }
5830 | "page-bias" -> { c with pagebias = int_of_string v }
5831 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5832 | "auto-scroll-step" ->
5833 { c with autoscrollstep = max 0 (int_of_string v) }
5834 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5835 | "crop-hack" -> { c with crophack = bool_of_string v }
5836 | "throttle" ->
5837 let mw =
5838 match String.lowercase v with
5839 | "true" -> Some infinity
5840 | "false" -> None
5841 | f -> Some (float_of_string f)
5843 { c with maxwait = mw}
5844 | "highlight-links" -> { c with hlinks = bool_of_string v }
5845 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5846 | "vertical-margin" ->
5847 { c with interpagespace = max 0 (int_of_string v) }
5848 | "zoom" ->
5849 let zoom = float_of_string v /. 100. in
5850 let zoom = max zoom 0.0 in
5851 { c with zoom = zoom }
5852 | "presentation" -> { c with presentation = bool_of_string v }
5853 | "rotation-angle" -> { c with angle = int_of_string v }
5854 | "width" -> { c with winw = max 20 (int_of_string v) }
5855 | "height" -> { c with winh = max 20 (int_of_string v) }
5856 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5857 | "proportional-display" -> { c with proportional = bool_of_string v }
5858 | "pixmap-cache-size" ->
5859 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5860 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5861 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5862 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5863 | "persistent-location" -> { c with jumpback = bool_of_string v }
5864 | "background-color" -> { c with bgcolor = color_of_string v }
5865 | "scrollbar-in-presentation" ->
5866 { c with scrollbarinpm = bool_of_string v }
5867 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5868 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5869 | "mupdf-store-size" ->
5870 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5871 | "checkers" -> { c with checkers = bool_of_string v }
5872 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5873 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5874 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5875 | "uri-launcher" -> { c with urilauncher = unent v }
5876 | "path-launcher" -> { c with pathlauncher = unent v }
5877 | "color-space" -> { c with colorspace = colorspace_of_string v }
5878 | "invert-colors" -> { c with invert = bool_of_string v }
5879 | "brightness" -> { c with colorscale = float_of_string v }
5880 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5881 | "ghyllscroll" ->
5882 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5883 | "columns" ->
5884 let (n, _, _) as nab = multicolumns_of_string v in
5885 if n < 0
5886 then { c with columns = Csplit (-n, [||]) }
5887 else { c with columns = Cmulti (nab, [||]) }
5888 | "birds-eye-columns" ->
5889 { c with beyecolumns = Some (max (int_of_string v) 2) }
5890 | "selection-command" -> { c with selcmd = unent v }
5891 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5892 | _ -> c
5893 with exn ->
5894 prerr_endline ("Error processing attribute (`" ^
5895 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5898 let rec fold c = function
5899 | [] -> c
5900 | (k, v) :: rest ->
5901 let c = apply c k v in
5902 fold c rest
5904 fold { c with keyhashes = copykeyhashes c } attrs;
5907 let fromstring f pos n v d =
5908 try f v
5909 with exn ->
5910 dolog "Error processing attribute (%S=%S) at %d\n%s"
5911 n v pos (Printexc.to_string exn)
5916 let bookmark_of attrs =
5917 let rec fold title page rely = function
5918 | ("title", v) :: rest -> fold v page rely rest
5919 | ("page", v) :: rest -> fold title v rely rest
5920 | ("rely", v) :: rest -> fold title page v rest
5921 | _ :: rest -> fold title page rely rest
5922 | [] -> title, page, rely
5924 fold "invalid" "0" "0" attrs
5927 let doc_of attrs =
5928 let rec fold path page rely pan = function
5929 | ("path", v) :: rest -> fold v page rely pan rest
5930 | ("page", v) :: rest -> fold path v rely pan rest
5931 | ("rely", v) :: rest -> fold path page v pan rest
5932 | ("pan", v) :: rest -> fold path page rely v rest
5933 | _ :: rest -> fold path page rely pan rest
5934 | [] -> path, page, rely, pan
5936 fold "" "0" "0" "0" attrs
5939 let map_of attrs =
5940 let rec fold rs ls = function
5941 | ("out", v) :: rest -> fold v ls rest
5942 | ("in", v) :: rest -> fold rs v rest
5943 | _ :: rest -> fold ls rs rest
5944 | [] -> ls, rs
5946 fold "" "" attrs
5949 let setconf dst src =
5950 dst.scrollbw <- src.scrollbw;
5951 dst.scrollh <- src.scrollh;
5952 dst.icase <- src.icase;
5953 dst.preload <- src.preload;
5954 dst.pagebias <- src.pagebias;
5955 dst.verbose <- src.verbose;
5956 dst.scrollstep <- src.scrollstep;
5957 dst.maxhfit <- src.maxhfit;
5958 dst.crophack <- src.crophack;
5959 dst.autoscrollstep <- src.autoscrollstep;
5960 dst.maxwait <- src.maxwait;
5961 dst.hlinks <- src.hlinks;
5962 dst.underinfo <- src.underinfo;
5963 dst.interpagespace <- src.interpagespace;
5964 dst.zoom <- src.zoom;
5965 dst.presentation <- src.presentation;
5966 dst.angle <- src.angle;
5967 dst.winw <- src.winw;
5968 dst.winh <- src.winh;
5969 dst.savebmarks <- src.savebmarks;
5970 dst.memlimit <- src.memlimit;
5971 dst.proportional <- src.proportional;
5972 dst.texcount <- src.texcount;
5973 dst.sliceheight <- src.sliceheight;
5974 dst.thumbw <- src.thumbw;
5975 dst.jumpback <- src.jumpback;
5976 dst.bgcolor <- src.bgcolor;
5977 dst.scrollbarinpm <- src.scrollbarinpm;
5978 dst.tilew <- src.tilew;
5979 dst.tileh <- src.tileh;
5980 dst.mustoresize <- src.mustoresize;
5981 dst.checkers <- src.checkers;
5982 dst.aalevel <- src.aalevel;
5983 dst.trimmargins <- src.trimmargins;
5984 dst.trimfuzz <- src.trimfuzz;
5985 dst.urilauncher <- src.urilauncher;
5986 dst.colorspace <- src.colorspace;
5987 dst.invert <- src.invert;
5988 dst.colorscale <- src.colorscale;
5989 dst.redirectstderr <- src.redirectstderr;
5990 dst.ghyllscroll <- src.ghyllscroll;
5991 dst.columns <- src.columns;
5992 dst.beyecolumns <- src.beyecolumns;
5993 dst.selcmd <- src.selcmd;
5994 dst.updatecurs <- src.updatecurs;
5995 dst.pathlauncher <- src.pathlauncher;
5996 dst.keyhashes <- copykeyhashes src;
5999 let get s =
6000 let h = Hashtbl.create 10 in
6001 let dc = { defconf with angle = defconf.angle } in
6002 let rec toplevel v t spos _ =
6003 match t with
6004 | Vdata | Vcdata | Vend -> v
6005 | Vopen ("llppconfig", _, closed) ->
6006 if closed
6007 then v
6008 else { v with f = llppconfig }
6009 | Vopen _ ->
6010 error "unexpected subelement at top level" s spos
6011 | Vclose _ -> error "unexpected close at top level" s spos
6013 and llppconfig v t spos _ =
6014 match t with
6015 | Vdata | Vcdata -> v
6016 | Vend -> error "unexpected end of input in llppconfig" s spos
6017 | Vopen ("defaults", attrs, closed) ->
6018 let c = config_of dc attrs in
6019 setconf dc c;
6020 if closed
6021 then v
6022 else { v with f = defaults }
6024 | Vopen ("ui-font", attrs, closed) ->
6025 let rec getsize size = function
6026 | [] -> size
6027 | ("size", v) :: rest ->
6028 let size =
6029 fromstring int_of_string spos "size" v fstate.fontsize in
6030 getsize size rest
6031 | l -> getsize size l
6033 fstate.fontsize <- getsize fstate.fontsize attrs;
6034 if closed
6035 then v
6036 else { v with f = uifont (Buffer.create 10) }
6038 | Vopen ("doc", attrs, closed) ->
6039 let pathent, spage, srely, span = doc_of attrs in
6040 let path = unent pathent
6041 and pageno = fromstring int_of_string spos "page" spage 0
6042 and rely = fromstring float_of_string spos "rely" srely 0.0
6043 and pan = fromstring int_of_string spos "pan" span 0 in
6044 let c = config_of dc attrs in
6045 let anchor = (pageno, rely) in
6046 if closed
6047 then (Hashtbl.add h path (c, [], pan, anchor); v)
6048 else { v with f = doc path pan anchor c [] }
6050 | Vopen _ ->
6051 error "unexpected subelement in llppconfig" s spos
6053 | Vclose "llppconfig" -> { v with f = toplevel }
6054 | Vclose _ -> error "unexpected close in llppconfig" s spos
6056 and defaults v t spos _ =
6057 match t with
6058 | Vdata | Vcdata -> v
6059 | Vend -> error "unexpected end of input in defaults" s spos
6060 | Vopen ("keymap", attrs, closed) ->
6061 let modename =
6062 try List.assoc "mode" attrs
6063 with Not_found -> "global" in
6064 if closed
6065 then v
6066 else
6067 let ret keymap =
6068 let h = findkeyhash dc modename in
6069 KeyMap.iter (Hashtbl.replace h) keymap;
6070 defaults
6072 { v with f = pkeymap ret KeyMap.empty }
6074 | Vopen (_, _, _) ->
6075 error "unexpected subelement in defaults" s spos
6077 | Vclose "defaults" ->
6078 { v with f = llppconfig }
6080 | Vclose _ -> error "unexpected close in defaults" s spos
6082 and uifont b v t spos epos =
6083 match t with
6084 | Vdata | Vcdata ->
6085 Buffer.add_substring b s spos (epos - spos);
6087 | Vopen (_, _, _) ->
6088 error "unexpected subelement in ui-font" s spos
6089 | Vclose "ui-font" ->
6090 if String.length !fontpath = 0
6091 then fontpath := Buffer.contents b;
6092 { v with f = llppconfig }
6093 | Vclose _ -> error "unexpected close in ui-font" s spos
6094 | Vend -> error "unexpected end of input in ui-font" s spos
6096 and doc path pan anchor c bookmarks v t spos _ =
6097 match t with
6098 | Vdata | Vcdata -> v
6099 | Vend -> error "unexpected end of input in doc" s spos
6100 | Vopen ("bookmarks", _, closed) ->
6101 if closed
6102 then v
6103 else { v with f = pbookmarks path pan anchor c bookmarks }
6105 | Vopen ("keymap", attrs, closed) ->
6106 let modename =
6107 try List.assoc "mode" attrs
6108 with Not_found -> "global"
6110 if closed
6111 then v
6112 else
6113 let ret keymap =
6114 let h = findkeyhash c modename in
6115 KeyMap.iter (Hashtbl.replace h) keymap;
6116 doc path pan anchor c bookmarks
6118 { v with f = pkeymap ret KeyMap.empty }
6120 | Vopen (_, _, _) ->
6121 error "unexpected subelement in doc" s spos
6123 | Vclose "doc" ->
6124 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6125 { v with f = llppconfig }
6127 | Vclose _ -> error "unexpected close in doc" s spos
6129 and pkeymap ret keymap v t spos _ =
6130 match t with
6131 | Vdata | Vcdata -> v
6132 | Vend -> error "unexpected end of input in keymap" s spos
6133 | Vopen ("map", attrs, closed) ->
6134 let r, l = map_of attrs in
6135 let kss = fromstring keys_of_string spos "in" r [] in
6136 let lss = fromstring keys_of_string spos "out" l [] in
6137 let keymap =
6138 match kss with
6139 | [] -> keymap
6140 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6141 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6143 if closed
6144 then { v with f = pkeymap ret keymap }
6145 else
6146 let f () = v in
6147 { v with f = skip "map" f }
6149 | Vopen _ ->
6150 error "unexpected subelement in keymap" s spos
6152 | Vclose "keymap" ->
6153 { v with f = ret keymap }
6155 | Vclose _ -> error "unexpected close in keymap" s spos
6157 and pbookmarks path pan anchor c bookmarks v t spos _ =
6158 match t with
6159 | Vdata | Vcdata -> v
6160 | Vend -> error "unexpected end of input in bookmarks" s spos
6161 | Vopen ("item", attrs, closed) ->
6162 let titleent, spage, srely = bookmark_of attrs in
6163 let page = fromstring int_of_string spos "page" spage 0
6164 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6165 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6166 if closed
6167 then { v with f = pbookmarks path pan anchor c bookmarks }
6168 else
6169 let f () = v in
6170 { v with f = skip "item" f }
6172 | Vopen _ ->
6173 error "unexpected subelement in bookmarks" s spos
6175 | Vclose "bookmarks" ->
6176 { v with f = doc path pan anchor c bookmarks }
6178 | Vclose _ -> error "unexpected close in bookmarks" s spos
6180 and skip tag f v t spos _ =
6181 match t with
6182 | Vdata | Vcdata -> v
6183 | Vend ->
6184 error ("unexpected end of input in skipped " ^ tag) s spos
6185 | Vopen (tag', _, closed) ->
6186 if closed
6187 then v
6188 else
6189 let f' () = { v with f = skip tag f } in
6190 { v with f = skip tag' f' }
6191 | Vclose ctag ->
6192 if tag = ctag
6193 then f ()
6194 else error ("unexpected close in skipped " ^ tag) s spos
6197 parse { f = toplevel; accu = () } s;
6198 h, dc;
6201 let do_load f ic =
6203 let len = in_channel_length ic in
6204 let s = String.create len in
6205 really_input ic s 0 len;
6206 f s;
6207 with
6208 | Parse_error (msg, s, pos) ->
6209 let subs = subs s pos in
6210 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6211 failwith ("parse error: " ^ s)
6213 | exn ->
6214 failwith ("config load error: " ^ Printexc.to_string exn)
6217 let defconfpath =
6218 let dir =
6220 let dir = Filename.concat home ".config" in
6221 if Sys.is_directory dir then dir else home
6222 with _ -> home
6224 Filename.concat dir "llpp.conf"
6227 let confpath = ref defconfpath;;
6229 let load1 f =
6230 if Sys.file_exists !confpath
6231 then
6232 match
6233 (try Some (open_in_bin !confpath)
6234 with exn ->
6235 prerr_endline
6236 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6237 Printexc.to_string exn);
6238 None
6240 with
6241 | Some ic ->
6242 begin try
6243 f (do_load get ic)
6244 with exn ->
6245 prerr_endline
6246 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6247 Printexc.to_string exn);
6248 end;
6249 close_in ic;
6251 | None -> ()
6252 else
6253 f (Hashtbl.create 0, defconf)
6256 let load () =
6257 let f (h, dc) =
6258 let pc, pb, px, pa =
6260 Hashtbl.find h (Filename.basename state.path)
6261 with Not_found -> dc, [], 0, (0, 0.0)
6263 setconf defconf dc;
6264 setconf conf pc;
6265 state.bookmarks <- pb;
6266 state.x <- px;
6267 state.scrollw <- conf.scrollbw;
6268 if conf.jumpback
6269 then state.anchor <- pa;
6270 cbput state.hists.nav pa;
6272 load1 f
6275 let add_attrs bb always dc c =
6276 let ob s a b =
6277 if always || a != b
6278 then Printf.bprintf bb "\n %s='%b'" s a
6279 and oi s a b =
6280 if always || a != b
6281 then Printf.bprintf bb "\n %s='%d'" s a
6282 and oI s a b =
6283 if always || a != b
6284 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6285 and oz s a b =
6286 if always || a <> b
6287 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6288 and oF s a b =
6289 if always || a <> b
6290 then Printf.bprintf bb "\n %s='%f'" s a
6291 and oc s a b =
6292 if always || a <> b
6293 then
6294 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6295 and oC s a b =
6296 if always || a <> b
6297 then
6298 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6299 and oR s a b =
6300 if always || a <> b
6301 then
6302 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6303 and os s a b =
6304 if always || a <> b
6305 then
6306 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6307 and og s a b =
6308 if always || a <> b
6309 then
6310 match a with
6311 | None -> ()
6312 | Some (_N, _A, _B) ->
6313 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6314 and oW s a b =
6315 if always || a <> b
6316 then
6317 let v =
6318 match a with
6319 | None -> "false"
6320 | Some f ->
6321 if f = infinity
6322 then "true"
6323 else string_of_float f
6325 Printf.bprintf bb "\n %s='%s'" s v
6326 and oco s a b =
6327 if always || a <> b
6328 then
6329 match a with
6330 | Cmulti ((n, a, b), _) when n > 1 ->
6331 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6332 | Csplit (n, _) when n > 1 ->
6333 Printf.bprintf bb "\n %s='%d'" s ~-n
6334 | _ -> ()
6335 and obeco s a b =
6336 if always || a <> b
6337 then
6338 match a with
6339 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6340 | _ -> ()
6342 let w, h =
6343 if always
6344 then dc.winw, dc.winh
6345 else
6346 match state.fullscreen with
6347 | Some wh -> wh
6348 | None -> c.winw, c.winh
6350 let zoom, presentation, interpagespace, maxwait =
6351 if always
6352 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6353 else
6354 match state.mode with
6355 | Birdseye (bc, _, _, _, _) ->
6356 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6357 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6359 oi "width" w dc.winw;
6360 oi "height" h dc.winh;
6361 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6362 oi "scroll-handle-height" c.scrollh dc.scrollh;
6363 ob "case-insensitive-search" c.icase dc.icase;
6364 ob "preload" c.preload dc.preload;
6365 oi "page-bias" c.pagebias dc.pagebias;
6366 oi "scroll-step" c.scrollstep dc.scrollstep;
6367 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6368 ob "max-height-fit" c.maxhfit dc.maxhfit;
6369 ob "crop-hack" c.crophack dc.crophack;
6370 oW "throttle" maxwait dc.maxwait;
6371 ob "highlight-links" c.hlinks dc.hlinks;
6372 ob "under-cursor-info" c.underinfo dc.underinfo;
6373 oi "vertical-margin" interpagespace dc.interpagespace;
6374 oz "zoom" zoom dc.zoom;
6375 ob "presentation" presentation dc.presentation;
6376 oi "rotation-angle" c.angle dc.angle;
6377 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6378 ob "proportional-display" c.proportional dc.proportional;
6379 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6380 oi "tex-count" c.texcount dc.texcount;
6381 oi "slice-height" c.sliceheight dc.sliceheight;
6382 oi "thumbnail-width" c.thumbw dc.thumbw;
6383 ob "persistent-location" c.jumpback dc.jumpback;
6384 oc "background-color" c.bgcolor dc.bgcolor;
6385 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6386 oi "tile-width" c.tilew dc.tilew;
6387 oi "tile-height" c.tileh dc.tileh;
6388 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6389 ob "checkers" c.checkers dc.checkers;
6390 oi "aalevel" c.aalevel dc.aalevel;
6391 ob "trim-margins" c.trimmargins dc.trimmargins;
6392 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6393 os "uri-launcher" c.urilauncher dc.urilauncher;
6394 os "path-launcher" c.pathlauncher dc.pathlauncher;
6395 oC "color-space" c.colorspace dc.colorspace;
6396 ob "invert-colors" c.invert dc.invert;
6397 oF "brightness" c.colorscale dc.colorscale;
6398 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6399 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6400 oco "columns" c.columns dc.columns;
6401 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6402 os "selection-command" c.selcmd dc.selcmd;
6403 ob "update-cursor" c.updatecurs dc.updatecurs;
6406 let keymapsbuf always dc c =
6407 let bb = Buffer.create 16 in
6408 let rec loop = function
6409 | [] -> ()
6410 | (modename, h) :: rest ->
6411 let dh = findkeyhash dc modename in
6412 if always || h <> dh
6413 then (
6414 if Hashtbl.length h > 0
6415 then (
6416 if Buffer.length bb > 0
6417 then Buffer.add_char bb '\n';
6418 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6419 Hashtbl.iter (fun i o ->
6420 let isdifferent = always ||
6422 let dO = Hashtbl.find dh i in
6423 dO <> o
6424 with Not_found -> true
6426 if isdifferent
6427 then
6428 let addkm (k, m) =
6429 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6430 if Wsi.withalt m then Buffer.add_string bb "alt-";
6431 if Wsi.withshift m then Buffer.add_string bb "shift-";
6432 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6433 Buffer.add_string bb (Wsi.keyname k);
6435 let addkms l =
6436 let rec loop = function
6437 | [] -> ()
6438 | km :: [] -> addkm km
6439 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6441 loop l
6443 Buffer.add_string bb "<map in='";
6444 addkm i;
6445 match o with
6446 | KMinsrt km ->
6447 Buffer.add_string bb "' out='";
6448 addkm km;
6449 Buffer.add_string bb "'/>\n"
6451 | KMinsrl kms ->
6452 Buffer.add_string bb "' out='";
6453 addkms kms;
6454 Buffer.add_string bb "'/>\n"
6456 | KMmulti (ins, kms) ->
6457 Buffer.add_char bb ' ';
6458 addkms ins;
6459 Buffer.add_string bb "' out='";
6460 addkms kms;
6461 Buffer.add_string bb "'/>\n"
6462 ) h;
6463 Buffer.add_string bb "</keymap>";
6466 loop rest
6468 loop c.keyhashes;
6472 let save () =
6473 let uifontsize = fstate.fontsize in
6474 let bb = Buffer.create 32768 in
6475 let f (h, dc) =
6476 let dc = if conf.bedefault then conf else dc in
6477 Buffer.add_string bb "<llppconfig>\n";
6479 if String.length !fontpath > 0
6480 then
6481 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6482 uifontsize
6483 !fontpath
6484 else (
6485 if uifontsize <> 14
6486 then
6487 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6490 Buffer.add_string bb "<defaults ";
6491 add_attrs bb true dc dc;
6492 let kb = keymapsbuf true dc dc in
6493 if Buffer.length kb > 0
6494 then (
6495 Buffer.add_string bb ">\n";
6496 Buffer.add_buffer bb kb;
6497 Buffer.add_string bb "\n</defaults>\n";
6499 else Buffer.add_string bb "/>\n";
6501 let adddoc path pan anchor c bookmarks =
6502 if bookmarks == [] && c = dc && anchor = emptyanchor
6503 then ()
6504 else (
6505 Printf.bprintf bb "<doc path='%s'"
6506 (enent path 0 (String.length path));
6508 if anchor <> emptyanchor
6509 then (
6510 let n, y = anchor in
6511 Printf.bprintf bb " page='%d'" n;
6512 if y > 1e-6
6513 then
6514 Printf.bprintf bb " rely='%f'" y
6518 if pan != 0
6519 then Printf.bprintf bb " pan='%d'" pan;
6521 add_attrs bb false dc c;
6522 let kb = keymapsbuf false dc c in
6524 begin match bookmarks with
6525 | [] ->
6526 if Buffer.length kb > 0
6527 then (
6528 Buffer.add_string bb ">\n";
6529 Buffer.add_buffer bb kb;
6530 Buffer.add_string bb "</doc>\n";
6532 else Buffer.add_string bb "/>\n"
6533 | _ ->
6534 Buffer.add_string bb ">\n<bookmarks>\n";
6535 List.iter (fun (title, _level, (page, rely)) ->
6536 Printf.bprintf bb
6537 "<item title='%s' page='%d'"
6538 (enent title 0 (String.length title))
6539 page
6541 if rely > 1e-6
6542 then
6543 Printf.bprintf bb " rely='%f'" rely
6545 Buffer.add_string bb "/>\n";
6546 ) bookmarks;
6547 Buffer.add_string bb "</bookmarks>";
6548 if Buffer.length kb > 0
6549 then (
6550 Buffer.add_string bb "\n";
6551 Buffer.add_buffer bb kb;
6553 Buffer.add_string bb "\n</doc>\n";
6554 end;
6558 let pan, conf =
6559 match state.mode with
6560 | Birdseye (c, pan, _, _, _) ->
6561 let beyecolumns =
6562 match conf.columns with
6563 | Cmulti ((c, _, _), _) -> Some c
6564 | Csingle -> None
6565 | Csplit _ -> None
6566 and columns =
6567 match c.columns with
6568 | Cmulti (c, _) -> Cmulti (c, [||])
6569 | Csingle -> Csingle
6570 | Csplit _ -> failwith "quit from bird's eye while split"
6572 pan, { c with beyecolumns = beyecolumns; columns = columns }
6573 | _ -> state.x, conf
6575 let basename = Filename.basename state.path in
6576 adddoc basename pan (getanchor ())
6577 { conf with
6578 autoscrollstep =
6579 match state.autoscroll with
6580 | Some step -> step
6581 | None -> conf.autoscrollstep }
6582 (if conf.savebmarks then state.bookmarks else []);
6584 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6585 if basename <> path
6586 then adddoc path x y c bookmarks
6587 ) h;
6588 Buffer.add_string bb "</llppconfig>";
6590 load1 f;
6591 if Buffer.length bb > 0
6592 then
6594 let tmp = !confpath ^ ".tmp" in
6595 let oc = open_out_bin tmp in
6596 Buffer.output_buffer oc bb;
6597 close_out oc;
6598 Unix.rename tmp !confpath;
6599 with exn ->
6600 prerr_endline
6601 ("error while saving configuration: " ^ Printexc.to_string exn)
6603 end;;
6605 let () =
6606 Arg.parse
6607 (Arg.align
6608 [("-p", Arg.String (fun s -> state.password <- s) ,
6609 "<password> Set password");
6611 ("-f", Arg.String (fun s -> Config.fontpath := s),
6612 "<path> Set path to the user interface font");
6614 ("-c", Arg.String (fun s -> Config.confpath := s),
6615 "<path> Set path to the configuration file");
6617 ("-v", Arg.Unit (fun () ->
6618 Printf.printf
6619 "%s\nconfiguration path: %s\n"
6620 (version ())
6621 Config.defconfpath
6623 exit 0), " Print version and exit");
6626 (fun s -> state.path <- s)
6627 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6629 if String.length state.path = 0
6630 then (prerr_endline "file name missing"; exit 1);
6632 Config.load ();
6634 let globalkeyhash = findkeyhash conf "global" in
6635 let wsfd, winw, winh = Wsi.init (object
6636 method expose =
6637 if nogeomcmds state.geomcmds || platform == Posx
6638 then display ()
6639 else (
6640 GlFunc.draw_buffer `front;
6641 GlClear.color (scalecolor2 conf.bgcolor);
6642 GlClear.clear [`color];
6643 GlFunc.draw_buffer `back;
6645 method display = display ()
6646 method reshape w h = reshape w h
6647 method mouse b d x y m = mouse b d x y m
6648 method motion x y = state.mpos <- (x, y); motion x y
6649 method pmotion x y = state.mpos <- (x, y); pmotion x y
6650 method key k m =
6651 let mascm = m land (
6652 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6653 ) in
6654 match state.keystate with
6655 | KSnone ->
6656 let km = k, mascm in
6657 begin
6658 match
6659 try Hashtbl.find globalkeyhash km
6660 with Not_found ->
6661 let modehash = state.uioh#modehash in
6662 try Hashtbl.find modehash km
6663 with Not_found -> KMinsrt (k, m)
6664 with
6665 | KMinsrt (k, m) -> keyboard k m
6666 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6667 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6669 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6670 List.iter (fun (k, m) -> keyboard k m) insrt;
6671 state.keystate <- KSnone
6672 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6673 state.keystate <- KSinto (keys, insrt)
6674 | _ ->
6675 state.keystate <- KSnone
6677 method enter x y = state.mpos <- (x, y); pmotion x y
6678 method leave = state.mpos <- (-1, -1)
6679 method quit = raise Quit
6680 end) conf.winw conf.winh (platform = Posx) in
6682 state.wsfd <- wsfd;
6684 if not (
6685 List.exists GlMisc.check_extension
6686 [ "GL_ARB_texture_rectangle"
6687 ; "GL_EXT_texture_recangle"
6688 ; "GL_NV_texture_rectangle" ]
6690 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6692 let cr, sw = Unix.pipe ()
6693 and sr, cw = Unix.pipe () in
6695 cloexec cr;
6696 cloexec sw;
6697 cloexec sr;
6698 cloexec cw;
6700 setcheckers conf.checkers;
6701 redirectstderr ();
6703 init (cr, cw) (
6704 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6705 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6706 !Config.fontpath
6708 state.sr <- sr;
6709 state.sw <- sw;
6710 state.text <- "Opening " ^ state.path;
6711 reshape winw winh;
6712 opendoc state.path state.password;
6713 state.uioh <- uioh;
6715 let rec loop deadline =
6716 let r =
6717 match state.errfd with
6718 | None -> [state.sr; state.wsfd]
6719 | Some fd -> [state.sr; state.wsfd; fd]
6721 if state.redisplay
6722 then (
6723 state.redisplay <- false;
6724 display ();
6726 let timeout =
6727 let now = now () in
6728 if deadline > now
6729 then (
6730 if deadline = infinity
6731 then ~-.1.0
6732 else max 0.0 (deadline -. now)
6734 else 0.0
6736 let r, _, _ =
6737 try Unix.select r [] [] timeout
6738 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6740 begin match r with
6741 | [] ->
6742 state.ghyll None;
6743 let newdeadline =
6744 if state.ghyll == noghyll
6745 then
6746 match state.autoscroll with
6747 | Some step when step != 0 ->
6748 let y = state.y + step in
6749 let y =
6750 if y < 0
6751 then state.maxy
6752 else if y >= state.maxy then 0 else y
6754 gotoy y;
6755 if state.mode = View
6756 then state.text <- "";
6757 deadline +. 0.01
6758 | _ -> infinity
6759 else deadline +. 0.01
6761 loop newdeadline
6763 | l ->
6764 let rec checkfds = function
6765 | [] -> ()
6766 | fd :: rest when fd = state.sr ->
6767 let cmd = readcmd state.sr in
6768 act cmd;
6769 checkfds rest
6771 | fd :: rest when fd = state.wsfd ->
6772 Wsi.readresp fd;
6773 checkfds rest
6775 | fd :: rest ->
6776 let s = String.create 80 in
6777 let n = Unix.read fd s 0 80 in
6778 if conf.redirectstderr
6779 then (
6780 Buffer.add_substring state.errmsgs s 0 n;
6781 state.newerrmsgs <- true;
6782 state.redisplay <- true;
6784 else (
6785 prerr_string (String.sub s 0 n);
6786 flush stderr;
6788 checkfds rest
6790 checkfds l;
6791 let newdeadline =
6792 let deadline1 =
6793 if deadline = infinity
6794 then now () +. 0.01
6795 else deadline
6797 match state.autoscroll with
6798 | Some step when step != 0 -> deadline1
6799 | _ -> if state.ghyll == noghyll then infinity else deadline1
6801 loop newdeadline
6802 end;
6805 loop infinity;
6806 with Quit ->
6807 Config.save ();
6808 exit 0;