Make Cygwin happy again (no spawn.h)
[llpp.git] / main.ml
blob9dcc39c6e5123172b91c46bc6fae73b2e70bbc30
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 -> int =
87 "ml_postprocess";;
88 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
89 external platform : unit -> platform = "ml_platform";;
90 external setaalevel : int -> unit = "ml_setaalevel";;
91 external realloctexts : int -> bool = "ml_realloctexts";;
92 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
93 external findlink : opaque -> linkdir -> link = "ml_findlink";;
94 external getlink : opaque -> int -> under = "ml_getlink";;
95 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
96 external getlinkcount : opaque -> int = "ml_getlinkcount";;
97 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
98 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
100 let platform_to_string = function
101 | Punknown -> "unknown"
102 | Plinux -> "Linux"
103 | Posx -> "OSX"
104 | Psun -> "Sun"
105 | Pfreebsd -> "FreeBSD"
106 | Pdragonflybsd -> "DragonflyBSD"
107 | Popenbsd -> "OpenBSD"
108 | Pnetbsd -> "NetBSD"
109 | Pcygwin -> "Cygwin"
112 let platform = platform ();;
114 let popen cmd fda =
115 if platform = Pcygwin
116 then (
117 let sh = "/bin/sh" in
118 let args = [|sh; "-c"; cmd|] in
119 let rec std si so se = function
120 | [] -> si, so, se
121 | (fd, 0) :: rest -> std fd so se rest
122 | (fd, -1) :: rest ->
123 Unix.set_close_on_exec fd;
124 std si so se rest
125 | (_, n) :: _ ->
126 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
128 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
129 ignore (Unix.create_process sh args si so se)
131 else popen cmd fda;
134 type x = int
135 and y = int
136 and tilex = int
137 and tiley = int
138 and tileparams = (x * y * width * height * tilex * tiley)
141 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
143 type mpos = int * int
144 and mstate =
145 | Msel of (mpos * mpos)
146 | Mpan of mpos
147 | Mscrolly | Mscrollx
148 | Mzoom of (int * int)
149 | Mzoomrect of (mpos * mpos)
150 | Mnone
153 type textentry = string * string * onhist option * onkey * ondone
154 and onkey = string -> int -> te
155 and ondone = string -> unit
156 and histcancel = unit -> unit
157 and onhist = ((histcmd -> string) * histcancel)
158 and histcmd = HCnext | HCprev | HCfirst | HClast
159 and te =
160 | TEstop
161 | TEdone of string
162 | TEcont of string
163 | TEswitch of textentry
166 type 'a circbuf =
167 { store : 'a array
168 ; mutable rc : int
169 ; mutable wc : int
170 ; mutable len : int
174 let bound v minv maxv =
175 max minv (min maxv v);
178 let cbnew n v =
179 { store = Array.create n v
180 ; rc = 0
181 ; wc = 0
182 ; len = 0
186 let drawstring size x y s =
187 Gl.enable `blend;
188 Gl.enable `texture_2d;
189 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
190 ignore (drawstr size x y s);
191 Gl.disable `blend;
192 Gl.disable `texture_2d;
195 let drawstring1 size x y s =
196 drawstr size x y s;
199 let drawstring2 size x y fmt =
200 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
203 let cbcap b = Array.length b.store;;
205 let cbput b v =
206 let cap = cbcap b in
207 b.store.(b.wc) <- v;
208 b.wc <- (b.wc + 1) mod cap;
209 b.rc <- b.wc;
210 b.len <- min (b.len + 1) cap;
213 let cbempty b = b.len = 0;;
215 let cbgetg b circular dir =
216 if cbempty b
217 then b.store.(0)
218 else
219 let rc = b.rc + dir in
220 let rc =
221 if circular
222 then (
223 if rc = -1
224 then b.len-1
225 else (
226 if rc = b.len
227 then 0
228 else rc
231 else max 0 (min rc (b.len-1))
233 b.rc <- rc;
234 b.store.(rc);
237 let cbget b = cbgetg b false;;
238 let cbgetc b = cbgetg b true;;
240 type page =
241 { pageno : int
242 ; pagedimno : int
243 ; pagew : int
244 ; pageh : int
245 ; pagex : int
246 ; pagey : int
247 ; pagevw : int
248 ; pagevh : int
249 ; pagedispx : int
250 ; pagedispy : int
254 let debugl l =
255 dolog "l %d dim=%d {" l.pageno l.pagedimno;
256 dolog " WxH %dx%d" l.pagew l.pageh;
257 dolog " vWxH %dx%d" l.pagevw l.pagevh;
258 dolog " pagex,y %d,%d" l.pagex l.pagey;
259 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
260 dolog "}";
263 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
264 dolog "rect {";
265 dolog " x0,y0=(% f, % f)" x0 y0;
266 dolog " x1,y1=(% f, % f)" x1 y1;
267 dolog " x2,y2=(% f, % f)" x2 y2;
268 dolog " x3,y3=(% f, % f)" x3 y3;
269 dolog "}";
272 type multicolumns = multicol * pagegeom
273 and splitcolumns = columncount * pagegeom
274 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
275 and multicol = columncount * covercount * covercount
276 and pdimno = int
277 and columncount = int
278 and covercount = int;;
280 type conf =
281 { mutable scrollbw : int
282 ; mutable scrollh : int
283 ; mutable icase : bool
284 ; mutable preload : bool
285 ; mutable pagebias : int
286 ; mutable verbose : bool
287 ; mutable debug : bool
288 ; mutable scrollstep : int
289 ; mutable maxhfit : bool
290 ; mutable crophack : bool
291 ; mutable autoscrollstep : int
292 ; mutable maxwait : float option
293 ; mutable hlinks : bool
294 ; mutable underinfo : bool
295 ; mutable interpagespace : interpagespace
296 ; mutable zoom : float
297 ; mutable presentation : bool
298 ; mutable angle : angle
299 ; mutable winw : int
300 ; mutable winh : int
301 ; mutable savebmarks : bool
302 ; mutable proportional : proportional
303 ; mutable trimmargins : trimmargins
304 ; mutable trimfuzz : irect
305 ; mutable memlimit : memsize
306 ; mutable texcount : texcount
307 ; mutable sliceheight : sliceheight
308 ; mutable thumbw : width
309 ; mutable jumpback : bool
310 ; mutable bgcolor : float * float * float
311 ; mutable bedefault : bool
312 ; mutable scrollbarinpm : bool
313 ; mutable tilew : int
314 ; mutable tileh : int
315 ; mutable mustoresize : memsize
316 ; mutable checkers : bool
317 ; mutable aalevel : int
318 ; mutable urilauncher : string
319 ; mutable pathlauncher : string
320 ; mutable colorspace : colorspace
321 ; mutable invert : bool
322 ; mutable colorscale : float
323 ; mutable redirectstderr : bool
324 ; mutable ghyllscroll : (int * int * int) option
325 ; mutable columns : columns
326 ; mutable beyecolumns : columncount option
327 ; mutable selcmd : string
328 ; mutable updatecurs : bool
329 ; mutable keyhashes : (string * keyhash) list
331 and columns =
332 | Csingle
333 | Cmulti of multicolumns
334 | Csplit of splitcolumns
337 type anchor = pageno * top;;
339 type outline = string * int * anchor;;
341 type rect = float * float * float * float * float * float * float * float;;
343 type tile = opaque * pixmapsize * elapsed
344 and elapsed = float;;
345 type pagemapkey = pageno * gen;;
346 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
347 and row = int
348 and col = int;;
350 let emptyanchor = (0, 0.0);;
352 type infochange = | Memused | Docinfo | Pdim;;
354 class type uioh = object
355 method display : unit
356 method key : int -> int -> uioh
357 method button : int -> bool -> int -> int -> int -> uioh
358 method motion : int -> int -> uioh
359 method pmotion : int -> int -> uioh
360 method infochanged : infochange -> unit
361 method scrollpw : (int * float * float)
362 method scrollph : (int * float * float)
363 method modehash : keyhash
364 end;;
366 type mode =
367 | Birdseye of (conf * leftx * pageno * pageno * anchor)
368 | Textentry of (textentry * onleave)
369 | View
370 | LinkNav of linktarget
371 and onleave = leavetextentrystatus -> unit
372 and leavetextentrystatus = | Cancel | Confirm
373 and helpitem = string * int * action
374 and action =
375 | Noaction
376 | Action of (uioh -> uioh)
377 and linktarget =
378 | Ltexact of (pageno * int)
379 | Ltgendir of int
382 let isbirdseye = function Birdseye _ -> true | _ -> false;;
383 let istextentry = function Textentry _ -> true | _ -> false;;
385 type currently =
386 | Idle
387 | Loading of (page * gen)
388 | Tiling of (
389 page * opaque * colorspace * angle * gen * col * row * width * height
391 | Outlining of outline list
394 let emptykeyhash = Hashtbl.create 0;;
395 let nouioh : uioh = object (self)
396 method display = ()
397 method key _ _ = self
398 method button _ _ _ _ _ = self
399 method motion _ _ = self
400 method pmotion _ _ = self
401 method infochanged _ = ()
402 method scrollpw = (0, nan, nan)
403 method scrollph = (0, nan, nan)
404 method modehash = emptykeyhash
405 end;;
407 type state =
408 { mutable sr : Unix.file_descr
409 ; mutable sw : Unix.file_descr
410 ; mutable wsfd : Unix.file_descr
411 ; mutable errfd : Unix.file_descr option
412 ; mutable stderr : Unix.file_descr
413 ; mutable errmsgs : Buffer.t
414 ; mutable newerrmsgs : bool
415 ; mutable w : int
416 ; mutable x : int
417 ; mutable y : int
418 ; mutable scrollw : int
419 ; mutable hscrollh : int
420 ; mutable anchor : anchor
421 ; mutable ranchors : (string * string * anchor) list
422 ; mutable maxy : int
423 ; mutable layout : page list
424 ; pagemap : (pagemapkey, opaque) Hashtbl.t
425 ; tilemap : (tilemapkey, tile) Hashtbl.t
426 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
427 ; mutable pdims : (pageno * width * height * leftx) list
428 ; mutable pagecount : int
429 ; mutable currently : currently
430 ; mutable mstate : mstate
431 ; mutable searchpattern : string
432 ; mutable rects : (pageno * recttype * rect) list
433 ; mutable rects1 : (pageno * recttype * rect) list
434 ; mutable text : string
435 ; mutable fullscreen : (width * height) option
436 ; mutable mode : mode
437 ; mutable uioh : uioh
438 ; mutable outlines : outline array
439 ; mutable bookmarks : outline list
440 ; mutable path : string
441 ; mutable password : string
442 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
443 ; mutable memused : memsize
444 ; mutable gen : gen
445 ; mutable throttle : (page list * int * float) option
446 ; mutable autoscroll : int option
447 ; mutable ghyll : (int option -> unit)
448 ; mutable help : helpitem array
449 ; mutable docinfo : (int * string) list
450 ; mutable texid : GlTex.texture_id option
451 ; hists : hists
452 ; mutable prevzoom : float
453 ; mutable progress : float
454 ; mutable redisplay : bool
455 ; mutable mpos : mpos
456 ; mutable keystate : keystate
457 ; mutable glinks : bool
459 and hists =
460 { pat : string circbuf
461 ; pag : string circbuf
462 ; nav : anchor circbuf
463 ; sel : string circbuf
467 let defconf =
468 { scrollbw = 7
469 ; scrollh = 12
470 ; icase = true
471 ; preload = true
472 ; pagebias = 0
473 ; verbose = false
474 ; debug = false
475 ; scrollstep = 24
476 ; maxhfit = true
477 ; crophack = false
478 ; autoscrollstep = 2
479 ; maxwait = None
480 ; hlinks = false
481 ; underinfo = false
482 ; interpagespace = 2
483 ; zoom = 1.0
484 ; presentation = false
485 ; angle = 0
486 ; winw = 900
487 ; winh = 900
488 ; savebmarks = true
489 ; proportional = true
490 ; trimmargins = false
491 ; trimfuzz = (0,0,0,0)
492 ; memlimit = 32 lsl 20
493 ; texcount = 256
494 ; sliceheight = 24
495 ; thumbw = 76
496 ; jumpback = true
497 ; bgcolor = (0.5, 0.5, 0.5)
498 ; bedefault = false
499 ; scrollbarinpm = true
500 ; tilew = 2048
501 ; tileh = 2048
502 ; mustoresize = 256 lsl 20
503 ; checkers = true
504 ; aalevel = 8
505 ; urilauncher =
506 (match platform with
507 | Plinux | Pfreebsd | Pdragonflybsd
508 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
509 | Posx -> "open \"%s\""
510 | Pcygwin -> "cygstart \"%s\""
511 | Punknown -> "echo %s")
512 ; pathlauncher = "lp \"%s\""
513 ; selcmd =
514 (match platform with
515 | Plinux | Pfreebsd | Pdragonflybsd
516 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
517 | Posx -> "pbcopy"
518 | Pcygwin -> "wsel"
519 | Punknown -> "cat")
520 ; colorspace = Rgb
521 ; invert = false
522 ; colorscale = 1.0
523 ; redirectstderr = false
524 ; ghyllscroll = None
525 ; columns = Csingle
526 ; beyecolumns = None
527 ; updatecurs = false
528 ; keyhashes =
529 let mk n = (n, Hashtbl.create 1) in
530 [ mk "global"
531 ; mk "info"
532 ; mk "help"
533 ; mk "outline"
534 ; mk "listview"
535 ; mk "birdseye"
536 ; mk "textentry"
537 ; mk "links"
542 let findkeyhash c name =
543 try List.assoc name c.keyhashes
544 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
547 let conf = { defconf with angle = defconf.angle };;
549 type fontstate =
550 { mutable fontsize : int
551 ; mutable wwidth : float
552 ; mutable maxrows : int
556 let fstate =
557 { fontsize = 14
558 ; wwidth = nan
559 ; maxrows = -1
563 let setfontsize n =
564 fstate.fontsize <- n;
565 fstate.wwidth <- measurestr fstate.fontsize "w";
566 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
569 let geturl s =
570 let colonpos = try String.index s ':' with Not_found -> -1 in
571 let len = String.length s in
572 if colonpos >= 0 && colonpos + 3 < len
573 then (
574 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
575 then
576 let schemestartpos =
577 try String.rindex_from s colonpos ' '
578 with Not_found -> -1
580 let scheme =
581 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
583 match scheme with
584 | "http" | "ftp" | "mailto" ->
585 let epos =
586 try String.index_from s colonpos ' '
587 with Not_found -> len
589 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
590 | _ -> ""
591 else ""
593 else ""
596 let gotouri uri =
597 if String.length conf.urilauncher = 0
598 then print_endline uri
599 else (
600 let url = geturl uri in
601 if String.length url = 0
602 then print_endline uri
603 else
604 let re = Str.regexp "%s" in
605 let command = Str.global_replace re url conf.urilauncher in
606 try popen command []
607 with exn ->
608 Printf.eprintf
609 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
610 flush stderr;
614 let version () =
615 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
616 (platform_to_string platform) Sys.word_size Sys.ocaml_version
619 let makehelp () =
620 let strings = version () :: "" :: Help.keys in
621 Array.of_list (
622 List.map (fun s ->
623 let url = geturl s in
624 if String.length url > 0
625 then (s, 0, Action (fun u -> gotouri url; u))
626 else (s, 0, Noaction)
627 ) strings);
630 let noghyll _ = ();;
631 let firstgeomcmds = "", [];;
633 let state =
634 { sr = Unix.stdin
635 ; sw = Unix.stdin
636 ; wsfd = Unix.stdin
637 ; errfd = None
638 ; stderr = Unix.stderr
639 ; errmsgs = Buffer.create 0
640 ; newerrmsgs = false
641 ; x = 0
642 ; y = 0
643 ; w = 0
644 ; scrollw = 0
645 ; hscrollh = 0
646 ; anchor = emptyanchor
647 ; ranchors = []
648 ; layout = []
649 ; maxy = max_int
650 ; tilelru = Queue.create ()
651 ; pagemap = Hashtbl.create 10
652 ; tilemap = Hashtbl.create 10
653 ; pdims = []
654 ; pagecount = 0
655 ; currently = Idle
656 ; mstate = Mnone
657 ; rects = []
658 ; rects1 = []
659 ; text = ""
660 ; mode = View
661 ; fullscreen = None
662 ; searchpattern = ""
663 ; outlines = [||]
664 ; bookmarks = []
665 ; path = ""
666 ; password = ""
667 ; geomcmds = firstgeomcmds
668 ; hists =
669 { nav = cbnew 10 (0, 0.0)
670 ; pat = cbnew 10 ""
671 ; pag = cbnew 10 ""
672 ; sel = cbnew 10 ""
674 ; memused = 0
675 ; gen = 0
676 ; throttle = None
677 ; autoscroll = None
678 ; ghyll = noghyll
679 ; help = makehelp ()
680 ; docinfo = []
681 ; texid = None
682 ; prevzoom = 1.0
683 ; progress = -1.0
684 ; uioh = nouioh
685 ; redisplay = true
686 ; mpos = (-1, -1)
687 ; keystate = KSnone
688 ; glinks = false
692 let vlog fmt =
693 if conf.verbose
694 then
695 Printf.kprintf prerr_endline fmt
696 else
697 Printf.kprintf ignore fmt
700 let launchpath () =
701 if String.length conf.pathlauncher = 0
702 then print_endline state.path
703 else (
704 let re = Str.regexp "%s" in
705 let command = Str.global_replace re state.path conf.pathlauncher in
706 try popen command []
707 with exn ->
708 Printf.eprintf
709 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
710 flush stderr;
714 let redirectstderr () =
715 if conf.redirectstderr
716 then
717 let rfd, wfd = Unix.pipe () in
718 state.stderr <- Unix.dup Unix.stderr;
719 state.errfd <- Some rfd;
720 Unix.dup2 wfd Unix.stderr;
721 else (
722 state.newerrmsgs <- false;
723 begin match state.errfd with
724 | Some fd ->
725 Unix.close fd;
726 Unix.dup2 state.stderr Unix.stderr;
727 state.errfd <- None;
728 | None -> ()
729 end;
730 prerr_string (Buffer.contents state.errmsgs);
731 flush stderr;
732 Buffer.clear state.errmsgs;
736 module G =
737 struct
738 let postRedisplay who =
739 if conf.verbose
740 then prerr_endline ("redisplay for " ^ who);
741 state.redisplay <- true;
743 end;;
745 let getopaque pageno =
746 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
747 with Not_found -> None
750 let putopaque pageno opaque =
751 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
754 let pagetranslatepoint l x y =
755 let dy = y - l.pagedispy in
756 let y = dy + l.pagey in
757 let dx = x - l.pagedispx in
758 let x = dx + l.pagex in
759 (x, y);
762 let getunder x y =
763 let rec f = function
764 | l :: rest ->
765 begin match getopaque l.pageno with
766 | Some opaque ->
767 let x0 = l.pagedispx in
768 let x1 = x0 + l.pagevw in
769 let y0 = l.pagedispy in
770 let y1 = y0 + l.pagevh in
771 if y >= y0 && y <= y1 && x >= x0 && x <= x1
772 then
773 let px, py = pagetranslatepoint l x y in
774 match whatsunder opaque px py with
775 | Unone -> f rest
776 | under -> under
777 else f rest
778 | _ ->
779 f rest
781 | [] -> Unone
783 f state.layout
786 let showtext c s =
787 state.text <- Printf.sprintf "%c%s" c s;
788 G.postRedisplay "showtext";
791 let updateunder x y =
792 match getunder x y with
793 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
794 | Ulinkuri uri ->
795 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
796 Wsi.setcursor Wsi.CURSOR_INFO
797 | Ulinkgoto (page, _) ->
798 if conf.underinfo
799 then showtext 'p' ("age: " ^ string_of_int (page+1));
800 Wsi.setcursor Wsi.CURSOR_INFO
801 | Utext s ->
802 if conf.underinfo then showtext 'f' ("ont: " ^ s);
803 Wsi.setcursor Wsi.CURSOR_TEXT
804 | Uunexpected s ->
805 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
806 Wsi.setcursor Wsi.CURSOR_INHERIT
807 | Ulaunch s ->
808 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
809 Wsi.setcursor Wsi.CURSOR_INHERIT
810 | Unamed s ->
811 if conf.underinfo then showtext 'n' ("amed: " ^ s);
812 Wsi.setcursor Wsi.CURSOR_INHERIT
813 | Uremote (filename, pageno) ->
814 if conf.underinfo then showtext 'r'
815 (Printf.sprintf "emote: %s (%d)" filename pageno);
816 Wsi.setcursor Wsi.CURSOR_INFO
819 let showlinktype under =
820 if conf.underinfo
821 then
822 match under with
823 | Unone -> ()
824 | Ulinkuri uri ->
825 showtext 'u' ("ri: " ^ uri)
826 | Ulinkgoto (page, _) ->
827 showtext 'p' ("age: " ^ string_of_int (page+1));
828 | Utext s ->
829 showtext 'f' ("ont: " ^ s);
830 | Uunexpected s ->
831 showtext 'u' ("nexpected: " ^ s);
832 | Ulaunch s ->
833 showtext 'l' ("aunch: " ^ s);
834 | Unamed s ->
835 showtext 'n' ("amed: " ^ s);
836 | Uremote (filename, pageno) ->
837 showtext 'r' (Printf.sprintf "emote: %s (%d)" filename pageno);
840 let addchar s c =
841 let b = Buffer.create (String.length s + 1) in
842 Buffer.add_string b s;
843 Buffer.add_char b c;
844 Buffer.contents b;
847 let colorspace_of_string s =
848 match String.lowercase s with
849 | "rgb" -> Rgb
850 | "bgr" -> Bgr
851 | "gray" -> Gray
852 | _ -> failwith "invalid colorspace"
855 let int_of_colorspace = function
856 | Rgb -> 0
857 | Bgr -> 1
858 | Gray -> 2
861 let colorspace_of_int = function
862 | 0 -> Rgb
863 | 1 -> Bgr
864 | 2 -> Gray
865 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
868 let colorspace_to_string = function
869 | Rgb -> "rgb"
870 | Bgr -> "bgr"
871 | Gray -> "gray"
874 let intentry_with_suffix text key =
875 let c =
876 if key >= 32 && key < 127
877 then Char.chr key
878 else '\000'
880 match Char.lowercase c with
881 | '0' .. '9' ->
882 let text = addchar text c in
883 TEcont text
885 | 'k' | 'm' | 'g' ->
886 let text = addchar text c in
887 TEcont text
889 | _ ->
890 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
891 TEcont text
894 let multicolumns_to_string (n, a, b) =
895 if a = 0 && b = 0
896 then Printf.sprintf "%d" n
897 else Printf.sprintf "%d,%d,%d" n a b;
900 let multicolumns_of_string s =
902 (int_of_string s, 0, 0)
903 with _ ->
904 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
907 let readcmd fd =
908 let s = "xxxx" in
909 let n = Unix.read fd s 0 4 in
910 if n != 4 then failwith "incomplete read(len)";
911 let len = 0
912 lor (Char.code s.[0] lsl 24)
913 lor (Char.code s.[1] lsl 16)
914 lor (Char.code s.[2] lsl 8)
915 lor (Char.code s.[3] lsl 0)
917 let s = String.create len in
918 let n = Unix.read fd s 0 len in
919 if n != len then failwith "incomplete read(data)";
923 let btod b = if b then 1 else 0;;
925 let wcmd fmt =
926 let b = Buffer.create 16 in
927 Buffer.add_string b "llll";
928 Printf.kbprintf
929 (fun b ->
930 let s = Buffer.contents b in
931 let n = String.length s in
932 let len = n - 4 in
933 (* dolog "wcmd %S" (String.sub s 4 len); *)
934 s.[0] <- Char.chr ((len lsr 24) land 0xff);
935 s.[1] <- Char.chr ((len lsr 16) land 0xff);
936 s.[2] <- Char.chr ((len lsr 8) land 0xff);
937 s.[3] <- Char.chr (len land 0xff);
938 let n' = Unix.write state.sw s 0 n in
939 if n' != n then failwith "write failed";
940 ) b fmt;
943 let calcips h =
944 if conf.presentation
945 then
946 let d = conf.winh - h in
947 max 0 ((d + 1) / 2)
948 else
949 conf.interpagespace
952 let calcheight () =
953 let rec f pn ph pi fh l =
954 match l with
955 | (n, _, h, _) :: rest ->
956 let ips = calcips h in
957 let fh =
958 if conf.presentation
959 then fh+ips
960 else (
961 if isbirdseye state.mode && pn = 0
962 then fh + ips
963 else fh
966 let fh = fh + ((n - pn) * (ph + pi)) in
967 f n h ips fh rest;
969 | [] ->
970 let inc =
971 if conf.presentation || (isbirdseye state.mode && pn = 0)
972 then 0
973 else -pi
975 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
976 max 0 fh
978 let fh = f 0 0 0 0 state.pdims in
982 let calcheight () =
983 match conf.columns with
984 | Csingle -> calcheight ()
985 | Cmulti (_, b) ->
986 if Array.length b > 0
987 then
988 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
989 y + h
990 else 0
991 | Csplit (_, b) ->
992 if Array.length b > 0
993 then
994 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
995 y + h
996 else 0
999 let getpageyh pageno =
1000 let rec f pn ph pi y l =
1001 match l with
1002 | (n, _, h, _) :: rest ->
1003 let ips = calcips h in
1004 if n >= pageno
1005 then
1006 let h = if n = pageno then h else ph in
1007 if conf.presentation && n = pageno
1008 then
1009 y + (pageno - pn) * (ph + pi) + pi, h
1010 else
1011 y + (pageno - pn) * (ph + pi), h
1012 else
1013 let y = y + (if conf.presentation then pi else 0) in
1014 let y = y + (n - pn) * (ph + pi) in
1015 f n h ips y rest
1017 | [] ->
1018 y + (pageno - pn) * (ph + pi), ph
1020 f 0 0 0 0 state.pdims
1023 let getpageyh pageno =
1024 match conf.columns with
1025 | Csingle -> getpageyh pageno
1026 | Cmulti (_, b) ->
1027 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1028 y, h
1029 | Csplit (c, b) ->
1030 let n = pageno*c in
1031 let (_, _, y, (_, _, h, _)) = b.(n) in
1032 y, h
1035 let getpagedim pageno =
1036 let rec f ppdim l =
1037 match l with
1038 | (n, _, _, _) as pdim :: rest ->
1039 if n >= pageno
1040 then (if n = pageno then pdim else ppdim)
1041 else f pdim rest
1043 | [] -> ppdim
1045 f (-1, -1, -1, -1) state.pdims
1048 let getpagey pageno = fst (getpageyh pageno);;
1050 let nogeomcmds cmds =
1051 match cmds with
1052 | s, [] -> String.length s = 0
1053 | _ -> false
1056 let layout1 y sh =
1057 let sh = sh - state.hscrollh in
1058 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1059 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1060 match pdims with
1061 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1062 let ips = calcips h in
1063 let yinc =
1064 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1065 then ips
1066 else 0
1068 (w, h, ips, xoff), rest, pdimno + 1, yinc
1069 | _ ->
1070 prev, pdims, pdimno, 0
1072 let dy = dy + yinc in
1073 let py = py + yinc in
1074 if pageno = state.pagecount || dy >= sh
1075 then
1076 accu
1077 else
1078 let vy = y + dy in
1079 if py + h <= vy - yinc
1080 then
1081 let py = py + h + ips in
1082 let dy = max 0 (py - y) in
1083 f ~pageno:(pageno+1)
1084 ~pdimno
1085 ~prev:curr
1088 ~pdims:rest
1089 ~accu
1090 else
1091 let pagey = vy - py in
1092 let pagevh = h - pagey in
1093 let pagevh = min (sh - dy) pagevh in
1094 let off = if yinc > 0 then py - vy else 0 in
1095 let py = py + h + ips in
1096 let pagex, dx =
1097 let xoff = xoff +
1098 if state.w < conf.winw - state.scrollw
1099 then (conf.winw - state.scrollw - state.w) / 2
1100 else 0
1102 let dispx = xoff + state.x in
1103 if dispx < 0
1104 then (-dispx, 0)
1105 else (0, dispx)
1107 let pagevw =
1108 let lw = w - pagex in
1109 min lw (conf.winw - state.scrollw)
1111 let e =
1112 { pageno = pageno
1113 ; pagedimno = pdimno
1114 ; pagew = w
1115 ; pageh = h
1116 ; pagex = pagex
1117 ; pagey = pagey + off
1118 ; pagevw = pagevw
1119 ; pagevh = pagevh - off
1120 ; pagedispx = dx
1121 ; pagedispy = dy + off
1124 let accu = e :: accu in
1125 f ~pageno:(pageno+1)
1126 ~pdimno
1127 ~prev:curr
1129 ~dy:(dy+pagevh+ips)
1130 ~pdims:rest
1131 ~accu
1133 let accu =
1135 ~pageno:0
1136 ~pdimno:~-1
1137 ~prev:(0,0,0,0)
1138 ~py:0
1139 ~dy:0
1140 ~pdims:state.pdims
1141 ~accu:[]
1143 List.rev accu
1146 let layoutN ((columns, coverA, coverB), b) y sh =
1147 let sh = sh - state.hscrollh in
1148 let rec fold accu n =
1149 if n = Array.length b
1150 then accu
1151 else
1152 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1153 if (vy - y) > sh &&
1154 (n = coverA - 1
1155 || n = state.pagecount - coverB
1156 || (n - coverA) mod columns = columns - 1)
1157 then accu
1158 else
1159 let accu =
1160 if vy + h > y
1161 then
1162 let pagey = max 0 (y - vy) in
1163 let pagedispy = if pagey > 0 then 0 else vy - y in
1164 let pagedispx, pagex =
1165 let pdx =
1166 if n = coverA - 1 || n = state.pagecount - coverB
1167 then state.x + (conf.winw - state.scrollw - w) / 2
1168 else dx + xoff + state.x
1170 if pdx < 0
1171 then 0, -pdx
1172 else pdx, 0
1174 let pagevw =
1175 let vw = conf.winw - state.scrollw - pagedispx in
1176 let pw = w - pagex in
1177 min vw pw
1179 let pagevh = min (h - pagey) (sh - pagedispy) in
1180 if pagevw > 0 && pagevh > 0
1181 then
1182 let e =
1183 { pageno = n
1184 ; pagedimno = pdimno
1185 ; pagew = w
1186 ; pageh = h
1187 ; pagex = pagex
1188 ; pagey = pagey
1189 ; pagevw = pagevw
1190 ; pagevh = pagevh
1191 ; pagedispx = pagedispx
1192 ; pagedispy = pagedispy
1195 e :: accu
1196 else
1197 accu
1198 else
1199 accu
1201 fold accu (n+1)
1203 List.rev (fold [] 0)
1206 let layoutS (columns, b) y sh =
1207 let sh = sh - state.hscrollh in
1208 let rec fold accu n =
1209 if n = Array.length b
1210 then accu
1211 else
1212 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1213 if (vy - y) > sh
1214 then accu
1215 else
1216 let accu =
1217 if vy + pageh > y
1218 then
1219 let x = xoff + state.x in
1220 let pagey = max 0 (y - vy) in
1221 let pagedispy = if pagey > 0 then 0 else vy - y in
1222 let pagedispx, pagex =
1223 if px = 0
1224 then (
1225 if x < 0
1226 then 0, -x
1227 else x, 0
1229 else (
1230 let px = px - x in
1231 if px < 0
1232 then -px, 0
1233 else 0, px
1236 let pagevw =
1237 let vw = conf.winw - pagedispx - state.scrollw in
1238 let pw = pagew - pagex in
1239 min vw pw
1241 let pagevw = min pagevw (pagew/columns) in
1242 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1243 if pagevw > 0 && pagevh > 0
1244 then
1245 let e =
1246 { pageno = n/columns
1247 ; pagedimno = pdimno
1248 ; pagew = pagew
1249 ; pageh = pageh
1250 ; pagex = pagex
1251 ; pagey = pagey
1252 ; pagevw = pagevw
1253 ; pagevh = pagevh
1254 ; pagedispx = pagedispx
1255 ; pagedispy = pagedispy
1258 e :: accu
1259 else
1260 accu
1261 else
1262 accu
1264 fold accu (n+1)
1266 List.rev (fold [] 0)
1269 let layout y sh =
1270 if nogeomcmds state.geomcmds
1271 then
1272 match conf.columns with
1273 | Csingle -> layout1 y sh
1274 | Cmulti c -> layoutN c y sh
1275 | Csplit s -> layoutS s y sh
1276 else []
1279 let clamp incr =
1280 let y = state.y + incr in
1281 let y = max 0 y in
1282 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1286 let itertiles l f =
1287 let tilex = l.pagex mod conf.tilew in
1288 let tiley = l.pagey mod conf.tileh in
1290 let col = l.pagex / conf.tilew in
1291 let row = l.pagey / conf.tileh in
1293 let rec rowloop row y0 dispy h =
1294 if h = 0
1295 then ()
1296 else (
1297 let dh = conf.tileh - y0 in
1298 let dh = min h dh in
1299 let rec colloop col x0 dispx w =
1300 if w = 0
1301 then ()
1302 else (
1303 let dw = conf.tilew - x0 in
1304 let dw = min w dw in
1306 f col row dispx dispy x0 y0 dw dh;
1307 colloop (col+1) 0 (dispx+dw) (w-dw)
1310 colloop col tilex l.pagedispx l.pagevw;
1311 rowloop (row+1) 0 (dispy+dh) (h-dh)
1314 if l.pagevw > 0 && l.pagevh > 0
1315 then rowloop row tiley l.pagedispy l.pagevh;
1318 let gettileopaque l col row =
1319 let key =
1320 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1322 try Some (Hashtbl.find state.tilemap key)
1323 with Not_found -> None
1326 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1327 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1328 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1331 let drawtiles l color =
1332 GlDraw.color color;
1333 let f col row x y tilex tiley w h =
1334 match gettileopaque l col row with
1335 | Some (opaque, _, t) ->
1336 let params = x, y, w, h, tilex, tiley in
1337 if conf.invert
1338 then (
1339 Gl.enable `blend;
1340 GlFunc.blend_func `zero `one_minus_src_color;
1342 drawtile params opaque;
1343 if conf.invert
1344 then Gl.disable `blend;
1345 if conf.debug
1346 then (
1347 let s = Printf.sprintf
1348 "%d[%d,%d] %f sec"
1349 l.pageno col row t
1351 let w = measurestr fstate.fontsize s in
1352 GlMisc.push_attrib [`current];
1353 GlDraw.color (0.0, 0.0, 0.0);
1354 GlDraw.rect
1355 (float (x-2), float (y-2))
1356 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1357 GlDraw.color (1.0, 1.0, 1.0);
1358 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1359 GlMisc.pop_attrib ();
1362 | _ ->
1363 let w =
1364 let lw = conf.winw - state.scrollw - x in
1365 min lw w
1366 and h =
1367 let lh = conf.winh - y in
1368 min lh h
1370 Gl.enable `texture_2d;
1371 begin match state.texid with
1372 | Some id ->
1373 GlTex.bind_texture `texture_2d id;
1374 let x0 = float x
1375 and y0 = float y
1376 and x1 = float (x+w)
1377 and y1 = float (y+h) in
1379 let tw = float w /. 64.0
1380 and th = float h /. 64.0 in
1381 let tx0 = float tilex /. 64.0
1382 and ty0 = float tiley /. 64.0 in
1383 let tx1 = tx0 +. tw
1384 and ty1 = ty0 +. th in
1385 GlDraw.begins `quads;
1386 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1387 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1388 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1389 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1390 GlDraw.ends ();
1392 Gl.disable `texture_2d;
1393 | None ->
1394 GlDraw.color (1.0, 1.0, 1.0);
1395 GlDraw.rect
1396 (float x, float y)
1397 (float (x+w), float (y+h));
1398 end;
1399 if w > 128 && h > fstate.fontsize + 10
1400 then (
1401 GlDraw.color (0.0, 0.0, 0.0);
1402 let c, r =
1403 if conf.verbose
1404 then (col*conf.tilew, row*conf.tileh)
1405 else col, row
1407 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1409 GlDraw.color color;
1411 itertiles l f
1414 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1416 let tilevisible1 l x y =
1417 let ax0 = l.pagex
1418 and ax1 = l.pagex + l.pagevw
1419 and ay0 = l.pagey
1420 and ay1 = l.pagey + l.pagevh in
1422 let bx0 = x
1423 and by0 = y in
1424 let bx1 = min (bx0 + conf.tilew) l.pagew
1425 and by1 = min (by0 + conf.tileh) l.pageh in
1427 let rx0 = max ax0 bx0
1428 and ry0 = max ay0 by0
1429 and rx1 = min ax1 bx1
1430 and ry1 = min ay1 by1 in
1432 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1433 nonemptyintersection
1436 let tilevisible layout n x y =
1437 let rec findpageinlayout m = function
1438 | l :: rest when l.pageno = n ->
1439 tilevisible1 l x y || (
1440 match conf.columns with
1441 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1442 | _ -> false
1444 | _ :: rest -> findpageinlayout 0 rest
1445 | [] -> false
1447 findpageinlayout 0 layout;
1450 let tileready l x y =
1451 tilevisible1 l x y &&
1452 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1455 let tilepage n p layout =
1456 let rec loop = function
1457 | l :: rest ->
1458 if l.pageno = n
1459 then
1460 let f col row _ _ _ _ _ _ =
1461 if state.currently = Idle
1462 then
1463 match gettileopaque l col row with
1464 | Some _ -> ()
1465 | None ->
1466 let x = col*conf.tilew
1467 and y = row*conf.tileh in
1468 let w =
1469 let w = l.pagew - x in
1470 min w conf.tilew
1472 let h =
1473 let h = l.pageh - y in
1474 min h conf.tileh
1476 wcmd "tile %s %d %d %d %d" p x y w h;
1477 state.currently <-
1478 Tiling (
1479 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1480 conf.tilew, conf.tileh
1483 itertiles l f;
1484 else
1485 loop rest
1487 | [] -> ()
1489 if nogeomcmds state.geomcmds
1490 then loop layout;
1493 let preloadlayout visiblepages =
1494 let presentation = conf.presentation in
1495 let interpagespace = conf.interpagespace in
1496 let maxy = state.maxy in
1497 conf.presentation <- false;
1498 conf.interpagespace <- 0;
1499 state.maxy <- calcheight ();
1500 let y =
1501 match visiblepages with
1502 | [] -> 0
1503 | l :: _ -> getpagey l.pageno + l.pagey
1505 let y = if y < conf.winh then 0 else y - conf.winh in
1506 let h = state.y - y + conf.winh*3 in
1507 let pages = layout y h in
1508 conf.presentation <- presentation;
1509 conf.interpagespace <- interpagespace;
1510 state.maxy <- maxy;
1511 pages;
1514 let load pages =
1515 let rec loop pages =
1516 if state.currently != Idle
1517 then ()
1518 else
1519 match pages with
1520 | l :: rest ->
1521 begin match getopaque l.pageno with
1522 | None ->
1523 wcmd "page %d %d" l.pageno l.pagedimno;
1524 state.currently <- Loading (l, state.gen);
1525 | Some opaque ->
1526 tilepage l.pageno opaque pages;
1527 loop rest
1528 end;
1529 | _ -> ()
1531 if nogeomcmds state.geomcmds
1532 then loop pages
1535 let preload pages =
1536 load pages;
1537 if conf.preload && state.currently = Idle
1538 then load (preloadlayout pages);
1541 let layoutready layout =
1542 let rec fold all ls =
1543 all && match ls with
1544 | l :: rest ->
1545 let seen = ref false in
1546 let allvisible = ref true in
1547 let foo col row _ _ _ _ _ _ =
1548 seen := true;
1549 allvisible := !allvisible &&
1550 begin match gettileopaque l col row with
1551 | Some _ -> true
1552 | None -> false
1555 itertiles l foo;
1556 fold (!seen && !allvisible) rest
1557 | [] -> true
1559 let alltilesvisible = fold true layout in
1560 alltilesvisible;
1563 let gotoy y =
1564 let y = bound y 0 state.maxy in
1565 let y, layout, proceed =
1566 match conf.maxwait with
1567 | Some time when state.ghyll == noghyll ->
1568 begin match state.throttle with
1569 | None ->
1570 let layout = layout y conf.winh in
1571 let ready = layoutready layout in
1572 if not ready
1573 then (
1574 load layout;
1575 state.throttle <- Some (layout, y, now ());
1577 else G.postRedisplay "gotoy showall (None)";
1578 y, layout, ready
1579 | Some (_, _, started) ->
1580 let dt = now () -. started in
1581 if dt > time
1582 then (
1583 state.throttle <- None;
1584 let layout = layout y conf.winh in
1585 load layout;
1586 G.postRedisplay "maxwait";
1587 y, layout, true
1589 else -1, [], false
1592 | _ ->
1593 let layout = layout y conf.winh in
1594 if true || layoutready layout
1595 then G.postRedisplay "gotoy ready";
1596 y, layout, true
1598 if proceed
1599 then (
1600 state.y <- y;
1601 state.layout <- layout;
1602 begin match state.mode with
1603 | LinkNav (Ltexact (pageno, linkno)) ->
1604 let rec loop = function
1605 | [] ->
1606 state.mode <- LinkNav (Ltgendir 0)
1607 | l :: _ when l.pageno = pageno ->
1608 begin match getopaque pageno with
1609 | None ->
1610 state.mode <- LinkNav (Ltgendir 0)
1611 | Some opaque ->
1612 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1613 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1614 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1615 then state.mode <- LinkNav (Ltgendir 0)
1617 | _ :: rest -> loop rest
1619 loop layout
1620 | _ -> ()
1621 end;
1622 begin match state.mode with
1623 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1624 if not (pagevisible layout pageno)
1625 then (
1626 match state.layout with
1627 | [] -> ()
1628 | l :: _ ->
1629 state.mode <- Birdseye (
1630 conf, leftx, l.pageno, hooverpageno, anchor
1633 | LinkNav (Ltgendir dir as lt) ->
1634 let linknav =
1635 let rec loop = function
1636 | [] -> lt
1637 | l :: rest ->
1638 match getopaque l.pageno with
1639 | None -> loop rest
1640 | Some opaque ->
1641 let link =
1642 let ld =
1643 if dir = 0
1644 then LDfirstvisible (l.pagex, l.pagey, dir)
1645 else (
1646 if dir > 0 then LDfirst else LDlast
1649 findlink opaque ld
1651 match link with
1652 | Lnotfound -> loop rest
1653 | Lfound n ->
1654 showlinktype (getlink opaque n);
1655 Ltexact (l.pageno, n)
1657 loop state.layout
1659 state.mode <- LinkNav linknav
1660 | _ -> ()
1661 end;
1662 preload layout;
1664 state.ghyll <- noghyll;
1665 if conf.updatecurs
1666 then (
1667 let mx, my = state.mpos in
1668 updateunder mx my;
1672 let conttiling pageno opaque =
1673 tilepage pageno opaque
1674 (if conf.preload then preloadlayout state.layout else state.layout)
1677 let gotoy_and_clear_text y =
1678 if not conf.verbose then state.text <- "";
1679 gotoy y;
1682 let getanchor () =
1683 match state.layout with
1684 | [] -> emptyanchor
1685 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1688 let getanchory (n, top) =
1689 let y, h = getpageyh n in
1690 y + (truncate (top *. float h));
1693 let gotoanchor anchor =
1694 gotoy (getanchory anchor);
1697 let addnav () =
1698 cbput state.hists.nav (getanchor ());
1701 let getnav dir =
1702 let anchor = cbgetc state.hists.nav dir in
1703 getanchory anchor;
1706 let gotoghyll y =
1707 let rec scroll f n a b =
1708 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1709 let snake f a b =
1710 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1711 if f < a
1712 then s (float f /. float a)
1713 else (
1714 if f > b
1715 then 1.0 -. s ((float (f-b) /. float (n-b)))
1716 else 1.0
1719 snake f a b
1720 and summa f n a b =
1721 (* courtesy:
1722 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1723 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1724 let iv1 = iv f in
1725 let ins = float a *. iv1
1726 and outs = float (n-b) *. iv1 in
1727 let ones = b - a in
1728 ins +. outs +. float ones
1730 let rec set (_N, _A, _B) y sy =
1731 let sum = summa 1.0 _N _A _B in
1732 let dy = float (y - sy) in
1733 state.ghyll <- (
1734 let rec gf n y1 o =
1735 if n >= _N
1736 then state.ghyll <- noghyll
1737 else
1738 let go n =
1739 let s = scroll n _N _A _B in
1740 let y1 = y1 +. ((s *. dy) /. sum) in
1741 gotoy_and_clear_text (truncate y1);
1742 state.ghyll <- gf (n+1) y1;
1744 match o with
1745 | None -> go n
1746 | Some y' -> set (_N/2, 0, 0) y' state.y
1748 gf 0 (float state.y)
1751 match conf.ghyllscroll with
1752 | None ->
1753 gotoy_and_clear_text y
1754 | Some nab ->
1755 if state.ghyll == noghyll
1756 then set nab y state.y
1757 else state.ghyll (Some y)
1760 let gotopage n top =
1761 let y, h = getpageyh n in
1762 let y = y + (truncate (top *. float h)) in
1763 gotoghyll y
1766 let gotopage1 n top =
1767 let y = getpagey n in
1768 let y = y + top in
1769 gotoghyll y
1772 let invalidate s f =
1773 state.layout <- [];
1774 state.pdims <- [];
1775 state.rects <- [];
1776 state.rects1 <- [];
1777 match state.geomcmds with
1778 | ps, [] when String.length ps = 0 ->
1779 f ();
1780 state.geomcmds <- s, [];
1782 | ps, [] ->
1783 state.geomcmds <- ps, [s, f];
1785 | ps, (s', _) :: rest when s' = s ->
1786 state.geomcmds <- ps, ((s, f) :: rest);
1788 | ps, cmds ->
1789 state.geomcmds <- ps, ((s, f) :: cmds);
1792 let opendoc path password =
1793 state.path <- path;
1794 state.password <- password;
1795 state.gen <- state.gen + 1;
1796 state.docinfo <- [];
1798 setaalevel conf.aalevel;
1799 Wsi.settitle ("llpp " ^ Filename.basename path);
1800 wcmd "open %s\000%s\000" path password;
1801 invalidate "reqlayout"
1802 (fun () ->
1803 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1806 let scalecolor c =
1807 let c = c *. conf.colorscale in
1808 (c, c, c);
1811 let scalecolor2 (r, g, b) =
1812 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1815 let represent () =
1816 let docolumns = function
1817 | Csingle -> ()
1819 | Cmulti ((columns, coverA, coverB), _) ->
1820 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1821 let rec loop pageno pdimno pdim x y rowh pdims =
1822 if pageno = state.pagecount
1823 then ()
1824 else
1825 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1826 match pdims with
1827 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1828 pdimno+1, pdim, rest
1829 | _ ->
1830 pdimno, pdim, pdims
1832 let x, y, rowh' =
1833 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1834 then (
1835 (conf.winw - state.scrollw - w) / 2,
1836 y + rowh + conf.interpagespace, h
1838 else (
1839 if (pageno - coverA) mod columns = 0
1840 then 0, y + rowh + conf.interpagespace, h
1841 else x, y, max rowh h
1844 let rec fixrow m = if m = pageno then () else
1845 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1846 if h < rowh
1847 then (
1848 let y = y + (rowh - h) / 2 in
1849 a.(m) <- (pdimno, x, y, pdim);
1851 fixrow (m+1)
1853 if pageno > 1 && (pageno - coverA) mod columns = 0
1854 then fixrow (pageno - columns);
1855 a.(pageno) <- (pdimno, x, y, pdim);
1856 let x = x + w + xoff*2 + conf.interpagespace in
1857 loop (pageno+1) pdimno pdim x y rowh' pdims
1859 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1860 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1862 | Csplit (c, _) ->
1863 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1864 let rec loop pageno pdimno pdim y pdims =
1865 if pageno = state.pagecount
1866 then ()
1867 else
1868 let pdimno, ((_, w, h, _) as pdim), pdims =
1869 match pdims with
1870 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1871 pdimno+1, pdim, rest
1872 | _ ->
1873 pdimno, pdim, pdims
1875 let cw = w / c in
1876 let rec loop1 n x y =
1877 if n = c then y else (
1878 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1879 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1882 let y = loop1 0 0 y in
1883 loop (pageno+1) pdimno pdim y pdims
1885 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1886 conf.columns <- Csplit (c, a);
1888 docolumns conf.columns;
1889 state.maxy <- calcheight ();
1890 state.hscrollh <-
1891 if state.w <= conf.winw - state.scrollw
1892 then 0
1893 else state.scrollw
1895 match state.mode with
1896 | Birdseye (_, _, pageno, _, _) ->
1897 let y, h = getpageyh pageno in
1898 let top = (conf.winh - h) / 2 in
1899 gotoy (max 0 (y - top))
1900 | _ -> gotoanchor state.anchor
1903 let reshape w h =
1904 GlDraw.viewport 0 0 w h;
1905 let firsttime = state.geomcmds == firstgeomcmds in
1906 if not firsttime && nogeomcmds state.geomcmds
1907 then state.anchor <- getanchor ();
1909 conf.winw <- w;
1910 let w = truncate (float w *. conf.zoom) - state.scrollw in
1911 let w = max w 2 in
1912 conf.winh <- h;
1913 setfontsize fstate.fontsize;
1914 GlMat.mode `modelview;
1915 GlMat.load_identity ();
1917 GlMat.mode `projection;
1918 GlMat.load_identity ();
1919 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1920 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1921 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1923 let relx =
1924 if conf.zoom <= 1.0
1925 then 0.0
1926 else float state.x /. float state.w
1928 invalidate "geometry"
1929 (fun () ->
1930 state.w <- w;
1931 if not firsttime
1932 then state.x <- truncate (relx *. float w);
1933 let w =
1934 match conf.columns with
1935 | Csingle -> w
1936 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1937 | Csplit (c, _) -> w * c
1939 wcmd "geometry %d %d" w h);
1942 let enttext () =
1943 let len = String.length state.text in
1944 let drawstring s =
1945 let hscrollh =
1946 match state.mode with
1947 | Textentry _
1948 | View -> state.hscrollh
1949 | _ -> 0
1951 let rect x w =
1952 GlDraw.rect
1953 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1954 (x+.w, float (conf.winh - hscrollh))
1957 let w = float (conf.winw - state.scrollw - 1) in
1958 if state.progress >= 0.0 && state.progress < 1.0
1959 then (
1960 GlDraw.color (0.3, 0.3, 0.3);
1961 let w1 = w *. state.progress in
1962 rect 0.0 w1;
1963 GlDraw.color (0.0, 0.0, 0.0);
1964 rect w1 (w-.w1)
1966 else (
1967 GlDraw.color (0.0, 0.0, 0.0);
1968 rect 0.0 w;
1971 GlDraw.color (1.0, 1.0, 1.0);
1972 drawstring fstate.fontsize
1973 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1975 let s =
1976 match state.mode with
1977 | Textentry ((prefix, text, _, _, _), _) ->
1978 let s =
1979 if len > 0
1980 then
1981 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1982 else
1983 Printf.sprintf "%s%s_" prefix text
1987 | _ -> state.text
1989 let s =
1990 if state.newerrmsgs
1991 then (
1992 if not (istextentry state.mode)
1993 then
1994 let s1 = "(press 'e' to review error messasges)" in
1995 if String.length s > 0 then s ^ " " ^ s1 else s1
1996 else s
1998 else s
2000 if String.length s > 0
2001 then drawstring s
2004 let gctiles () =
2005 let len = Queue.length state.tilelru in
2006 let rec loop qpos =
2007 if state.memused <= conf.memlimit
2008 then ()
2009 else (
2010 if qpos < len
2011 then
2012 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2013 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2014 let (_, pw, ph, _) = getpagedim n in
2016 gen = state.gen
2017 && colorspace = conf.colorspace
2018 && angle = conf.angle
2019 && pagew = pw
2020 && pageh = ph
2021 && (
2022 let layout =
2023 match state.throttle with
2024 | None ->
2025 if conf.preload
2026 then preloadlayout state.layout
2027 else state.layout
2028 | Some (layout, _, _) ->
2029 layout
2031 let x = col*conf.tilew
2032 and y = row*conf.tileh in
2033 tilevisible layout n x y
2035 then Queue.push lruitem state.tilelru
2036 else (
2037 wcmd "freetile %s" p;
2038 state.memused <- state.memused - s;
2039 state.uioh#infochanged Memused;
2040 Hashtbl.remove state.tilemap k;
2042 loop (qpos+1)
2045 loop 0
2048 let flushtiles () =
2049 Queue.iter (fun (k, p, s) ->
2050 wcmd "freetile %s" p;
2051 state.memused <- state.memused - s;
2052 state.uioh#infochanged Memused;
2053 Hashtbl.remove state.tilemap k;
2054 ) state.tilelru;
2055 Queue.clear state.tilelru;
2056 load state.layout;
2059 let logcurrently = function
2060 | Idle -> dolog "Idle"
2061 | Loading (l, gen) ->
2062 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2063 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2064 dolog
2065 "Tiling %d[%d,%d] page=%s cs=%s angle"
2066 l.pageno col row pageopaque
2067 (colorspace_to_string colorspace)
2069 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2070 angle gen conf.angle state.gen
2071 tilew tileh
2072 conf.tilew conf.tileh
2074 | Outlining _ ->
2075 dolog "outlining"
2078 let act cmds =
2079 (* dolog "%S" cmds; *)
2080 let op, args =
2081 let spacepos =
2082 try String.index cmds ' '
2083 with Not_found -> -1
2085 if spacepos = -1
2086 then cmds, ""
2087 else
2088 let l = String.length cmds in
2089 let op = String.sub cmds 0 spacepos in
2090 op, begin
2091 if l - spacepos < 2 then ""
2092 else String.sub cmds (spacepos+1) (l-spacepos-1)
2095 match op with
2096 | "clear" ->
2097 state.uioh#infochanged Pdim;
2098 state.pdims <- [];
2100 | "clearrects" ->
2101 state.rects <- state.rects1;
2102 G.postRedisplay "clearrects";
2104 | "continue" ->
2105 let n =
2106 try Scanf.sscanf args "%u" (fun n -> n)
2107 with exn ->
2108 dolog "error processing 'continue' %S: %s"
2109 cmds (Printexc.to_string exn);
2110 exit 1;
2112 state.pagecount <- n;
2113 begin match state.currently with
2114 | Outlining l ->
2115 state.currently <- Idle;
2116 state.outlines <- Array.of_list (List.rev l)
2117 | _ -> ()
2118 end;
2120 let cur, cmds = state.geomcmds in
2121 if String.length cur = 0
2122 then failwith "umpossible";
2124 begin match List.rev cmds with
2125 | [] ->
2126 state.geomcmds <- "", [];
2127 represent ();
2128 | (s, f) :: rest ->
2129 f ();
2130 state.geomcmds <- s, List.rev rest;
2131 end;
2132 if conf.maxwait = None
2133 then G.postRedisplay "continue";
2135 | "title" ->
2136 Wsi.settitle args
2138 | "msg" ->
2139 showtext ' ' args
2141 | "vmsg" ->
2142 if conf.verbose
2143 then showtext ' ' args
2145 | "progress" ->
2146 let progress, text =
2148 Scanf.sscanf args "%f %n"
2149 (fun f pos ->
2150 f, String.sub args pos (String.length args - pos))
2151 with exn ->
2152 dolog "error processing 'progress' %S: %s"
2153 cmds (Printexc.to_string exn);
2154 exit 1;
2156 state.text <- text;
2157 state.progress <- progress;
2158 G.postRedisplay "progress"
2160 | "firstmatch" ->
2161 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2163 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2164 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2165 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2166 with exn ->
2167 dolog "error processing 'firstmatch' %S: %s"
2168 cmds (Printexc.to_string exn);
2169 exit 1;
2171 let y = (getpagey pageno) + truncate y0 in
2172 addnav ();
2173 gotoy y;
2174 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2176 | "match" ->
2177 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2179 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2180 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2181 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2182 with exn ->
2183 dolog "error processing 'match' %S: %s"
2184 cmds (Printexc.to_string exn);
2185 exit 1;
2187 state.rects1 <-
2188 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2190 | "page" ->
2191 let pageopaque, t =
2193 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2194 with exn ->
2195 dolog "error processing 'page' %S: %s"
2196 cmds (Printexc.to_string exn);
2197 exit 1;
2199 begin match state.currently with
2200 | Loading (l, gen) ->
2201 vlog "page %d took %f sec" l.pageno t;
2202 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2203 begin match state.throttle with
2204 | None ->
2205 let preloadedpages =
2206 if conf.preload
2207 then preloadlayout state.layout
2208 else state.layout
2210 let evict () =
2211 let module IntSet =
2212 Set.Make (struct type t = int let compare = (-) end) in
2213 let set =
2214 List.fold_left (fun s l -> IntSet.add l.pageno s)
2215 IntSet.empty preloadedpages
2217 let evictedpages =
2218 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2219 if not (IntSet.mem pageno set)
2220 then (
2221 wcmd "freepage %s" opaque;
2222 key :: accu
2224 else accu
2225 ) state.pagemap []
2227 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2229 evict ();
2230 state.currently <- Idle;
2231 if gen = state.gen
2232 then (
2233 tilepage l.pageno pageopaque state.layout;
2234 load state.layout;
2235 load preloadedpages;
2236 if pagevisible state.layout l.pageno
2237 && layoutready state.layout
2238 then G.postRedisplay "page";
2241 | Some (layout, _, _) ->
2242 state.currently <- Idle;
2243 tilepage l.pageno pageopaque layout;
2244 load state.layout
2245 end;
2247 | _ ->
2248 dolog "Inconsistent loading state";
2249 logcurrently state.currently;
2250 exit 1
2253 | "tile" ->
2254 let (x, y, opaque, size, t) =
2256 Scanf.sscanf args "%u %u %s %u %f"
2257 (fun x y p size t -> (x, y, p, size, t))
2258 with exn ->
2259 dolog "error processing 'tile' %S: %s"
2260 cmds (Printexc.to_string exn);
2261 exit 1;
2263 begin match state.currently with
2264 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2265 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2267 if tilew != conf.tilew || tileh != conf.tileh
2268 then (
2269 wcmd "freetile %s" opaque;
2270 state.currently <- Idle;
2271 load state.layout;
2273 else (
2274 puttileopaque l col row gen cs angle opaque size t;
2275 state.memused <- state.memused + size;
2276 state.uioh#infochanged Memused;
2277 gctiles ();
2278 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2279 opaque, size) state.tilelru;
2281 let layout =
2282 match state.throttle with
2283 | None -> state.layout
2284 | Some (layout, _, _) -> layout
2287 state.currently <- Idle;
2288 if gen = state.gen
2289 && conf.colorspace = cs
2290 && conf.angle = angle
2291 && tilevisible layout l.pageno x y
2292 then conttiling l.pageno pageopaque;
2294 begin match state.throttle with
2295 | None ->
2296 preload state.layout;
2297 if gen = state.gen
2298 && conf.colorspace = cs
2299 && conf.angle = angle
2300 && tilevisible state.layout l.pageno x y
2301 then G.postRedisplay "tile nothrottle";
2303 | Some (layout, y, _) ->
2304 let ready = layoutready layout in
2305 if ready
2306 then (
2307 state.y <- y;
2308 state.layout <- layout;
2309 state.throttle <- None;
2310 G.postRedisplay "throttle";
2312 else load layout;
2313 end;
2316 | _ ->
2317 dolog "Inconsistent tiling state";
2318 logcurrently state.currently;
2319 exit 1
2322 | "pdim" ->
2323 let pdim =
2325 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2326 with exn ->
2327 dolog "error processing 'pdim' %S: %s"
2328 cmds (Printexc.to_string exn);
2329 exit 1;
2331 state.uioh#infochanged Pdim;
2332 state.pdims <- pdim :: state.pdims
2334 | "o" ->
2335 let (l, n, t, h, pos) =
2337 Scanf.sscanf args "%u %u %d %u %n"
2338 (fun l n t h pos -> l, n, t, h, pos)
2339 with exn ->
2340 dolog "error processing 'o' %S: %s"
2341 cmds (Printexc.to_string exn);
2342 exit 1;
2344 let s = String.sub args pos (String.length args - pos) in
2345 let outline = (s, l, (n, float t /. float h)) in
2346 begin match state.currently with
2347 | Outlining outlines ->
2348 state.currently <- Outlining (outline :: outlines)
2349 | Idle ->
2350 state.currently <- Outlining [outline]
2351 | currently ->
2352 dolog "invalid outlining state";
2353 logcurrently currently
2356 | "info" ->
2357 state.docinfo <- (1, args) :: state.docinfo
2359 | "infoend" ->
2360 state.uioh#infochanged Docinfo;
2361 state.docinfo <- List.rev state.docinfo
2363 | _ ->
2364 dolog "unknown cmd `%S'" cmds
2367 let onhist cb =
2368 let rc = cb.rc in
2369 let action = function
2370 | HCprev -> cbget cb ~-1
2371 | HCnext -> cbget cb 1
2372 | HCfirst -> cbget cb ~-(cb.rc)
2373 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2374 and cancel () = cb.rc <- rc
2375 in (action, cancel)
2378 let search pattern forward =
2379 if String.length pattern > 0
2380 then
2381 let pn, py =
2382 match state.layout with
2383 | [] -> 0, 0
2384 | l :: _ ->
2385 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2387 wcmd "search %d %d %d %d,%s\000"
2388 (btod conf.icase) pn py (btod forward) pattern;
2391 let intentry text key =
2392 let c =
2393 if key >= 32 && key < 127
2394 then Char.chr key
2395 else '\000'
2397 match c with
2398 | '0' .. '9' ->
2399 let text = addchar text c in
2400 TEcont text
2402 | _ ->
2403 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2404 TEcont text
2407 let textentry text key =
2408 if key land 0xff00 = 0xff00
2409 then TEcont text
2410 else TEcont (text ^ Wsi.toutf8 key)
2413 let reqlayout angle proportional =
2414 match state.throttle with
2415 | None ->
2416 if nogeomcmds state.geomcmds
2417 then state.anchor <- getanchor ();
2418 conf.angle <- angle mod 360;
2419 if conf.angle != 0
2420 then (
2421 match state.mode with
2422 | LinkNav _ -> state.mode <- View
2423 | _ -> ()
2425 conf.proportional <- proportional;
2426 invalidate "reqlayout"
2427 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2428 | _ -> ()
2431 let settrim trimmargins trimfuzz =
2432 if nogeomcmds state.geomcmds
2433 then state.anchor <- getanchor ();
2434 conf.trimmargins <- trimmargins;
2435 conf.trimfuzz <- trimfuzz;
2436 let x0, y0, x1, y1 = trimfuzz in
2437 invalidate "settrim"
2438 (fun () ->
2439 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2440 Hashtbl.iter (fun _ opaque ->
2441 wcmd "freepage %s" opaque;
2442 ) state.pagemap;
2443 Hashtbl.clear state.pagemap;
2446 let setzoom zoom =
2447 match state.throttle with
2448 | None ->
2449 let zoom = max 0.01 zoom in
2450 if zoom <> conf.zoom
2451 then (
2452 state.prevzoom <- conf.zoom;
2453 conf.zoom <- zoom;
2454 reshape conf.winw conf.winh;
2455 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2458 | Some (layout, y, started) ->
2459 let time =
2460 match conf.maxwait with
2461 | None -> 0.0
2462 | Some t -> t
2464 let dt = now () -. started in
2465 if dt > time
2466 then (
2467 state.y <- y;
2468 load layout;
2472 let setcolumns mode columns coverA coverB =
2473 if columns < 0
2474 then (
2475 if isbirdseye mode
2476 then showtext '!' "split mode doesn't work in bird's eye"
2477 else (
2478 conf.columns <- Csplit (-columns, [||]);
2479 state.x <- 0;
2480 conf.zoom <- 1.0;
2483 else (
2484 if columns < 2
2485 then (
2486 conf.columns <- Csingle;
2487 state.x <- 0;
2488 setzoom 1.0;
2490 else (
2491 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2492 conf.zoom <- 1.0;
2495 reshape conf.winw conf.winh;
2498 let enterbirdseye () =
2499 let zoom = float conf.thumbw /. float conf.winw in
2500 let birdseyepageno =
2501 let cy = conf.winh / 2 in
2502 let fold = function
2503 | [] -> 0
2504 | l :: rest ->
2505 let rec fold best = function
2506 | [] -> best.pageno
2507 | l :: rest ->
2508 let d = cy - (l.pagedispy + l.pagevh/2)
2509 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2510 if abs d < abs dbest
2511 then fold l rest
2512 else best.pageno
2513 in fold l rest
2515 fold state.layout
2517 state.mode <- Birdseye (
2518 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2520 conf.zoom <- zoom;
2521 conf.presentation <- false;
2522 conf.interpagespace <- 10;
2523 conf.hlinks <- false;
2524 state.x <- 0;
2525 state.mstate <- Mnone;
2526 conf.maxwait <- None;
2527 conf.columns <- (
2528 match conf.beyecolumns with
2529 | Some c ->
2530 conf.zoom <- 1.0;
2531 Cmulti ((c, 0, 0), [||])
2532 | None -> Csingle
2534 Wsi.setcursor Wsi.CURSOR_INHERIT;
2535 if conf.verbose
2536 then
2537 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2538 (100.0*.zoom)
2539 else
2540 state.text <- ""
2542 reshape conf.winw conf.winh;
2545 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2546 state.mode <- View;
2547 conf.zoom <- c.zoom;
2548 conf.presentation <- c.presentation;
2549 conf.interpagespace <- c.interpagespace;
2550 conf.maxwait <- c.maxwait;
2551 conf.hlinks <- c.hlinks;
2552 conf.beyecolumns <- (
2553 match conf.columns with
2554 | Cmulti ((c, _, _), _) -> Some c
2555 | Csingle -> None
2556 | Csplit _ -> assert false
2558 conf.columns <- (
2559 match c.columns with
2560 | Cmulti (c, _) -> Cmulti (c, [||])
2561 | Csingle -> Csingle
2562 | Csplit _ -> failwith "leaving bird's eye split mode"
2564 state.x <- leftx;
2565 if conf.verbose
2566 then
2567 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2568 (100.0*.conf.zoom)
2570 reshape conf.winw conf.winh;
2571 state.anchor <- if goback then anchor else (pageno, 0.0);
2574 let togglebirdseye () =
2575 match state.mode with
2576 | Birdseye vals -> leavebirdseye vals true
2577 | View -> enterbirdseye ()
2578 | _ -> ()
2581 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2582 let pageno = max 0 (pageno - incr) in
2583 let rec loop = function
2584 | [] -> gotopage1 pageno 0
2585 | l :: _ when l.pageno = pageno ->
2586 if l.pagedispy >= 0 && l.pagey = 0
2587 then G.postRedisplay "upbirdseye"
2588 else gotopage1 pageno 0
2589 | _ :: rest -> loop rest
2591 loop state.layout;
2592 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2595 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2596 let pageno = min (state.pagecount - 1) (pageno + incr) in
2597 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2598 let rec loop = function
2599 | [] ->
2600 let y, h = getpageyh pageno in
2601 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2602 gotoy (clamp dy)
2603 | l :: _ when l.pageno = pageno ->
2604 if l.pagevh != l.pageh
2605 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2606 else G.postRedisplay "downbirdseye"
2607 | _ :: rest -> loop rest
2609 loop state.layout
2612 let optentry mode _ key =
2613 let btos b = if b then "on" else "off" in
2614 if key >= 32 && key < 127
2615 then
2616 let c = Char.chr key in
2617 match c with
2618 | 's' ->
2619 let ondone s =
2620 try conf.scrollstep <- int_of_string s with exc ->
2621 state.text <- Printf.sprintf "bad integer `%s': %s"
2622 s (Printexc.to_string exc)
2624 TEswitch ("scroll step: ", "", None, intentry, ondone)
2626 | 'A' ->
2627 let ondone s =
2629 conf.autoscrollstep <- int_of_string s;
2630 if state.autoscroll <> None
2631 then state.autoscroll <- Some conf.autoscrollstep
2632 with exc ->
2633 state.text <- Printf.sprintf "bad integer `%s': %s"
2634 s (Printexc.to_string exc)
2636 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2638 | 'C' ->
2639 let mode = state.mode in
2640 let ondone s =
2642 let n, a, b = multicolumns_of_string s in
2643 setcolumns mode n a b;
2644 with exc ->
2645 state.text <- Printf.sprintf "bad columns `%s': %s"
2646 s (Printexc.to_string exc)
2648 TEswitch ("columns: ", "", None, textentry, ondone)
2650 | 'Z' ->
2651 let ondone s =
2653 let zoom = float (int_of_string s) /. 100.0 in
2654 setzoom zoom
2655 with exc ->
2656 state.text <- Printf.sprintf "bad integer `%s': %s"
2657 s (Printexc.to_string exc)
2659 TEswitch ("zoom: ", "", None, intentry, ondone)
2661 | 't' ->
2662 let ondone s =
2664 conf.thumbw <- bound (int_of_string s) 2 4096;
2665 state.text <-
2666 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2667 begin match mode with
2668 | Birdseye beye ->
2669 leavebirdseye beye false;
2670 enterbirdseye ();
2671 | _ -> ();
2673 with exc ->
2674 state.text <- Printf.sprintf "bad integer `%s': %s"
2675 s (Printexc.to_string exc)
2677 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2679 | 'R' ->
2680 let ondone s =
2681 match try
2682 Some (int_of_string s)
2683 with exc ->
2684 state.text <- Printf.sprintf "bad integer `%s': %s"
2685 s (Printexc.to_string exc);
2686 None
2687 with
2688 | Some angle -> reqlayout angle conf.proportional
2689 | None -> ()
2691 TEswitch ("rotation: ", "", None, intentry, ondone)
2693 | 'i' ->
2694 conf.icase <- not conf.icase;
2695 TEdone ("case insensitive search " ^ (btos conf.icase))
2697 | 'p' ->
2698 conf.preload <- not conf.preload;
2699 gotoy state.y;
2700 TEdone ("preload " ^ (btos conf.preload))
2702 | 'v' ->
2703 conf.verbose <- not conf.verbose;
2704 TEdone ("verbose " ^ (btos conf.verbose))
2706 | 'd' ->
2707 conf.debug <- not conf.debug;
2708 TEdone ("debug " ^ (btos conf.debug))
2710 | 'h' ->
2711 conf.maxhfit <- not conf.maxhfit;
2712 state.maxy <-
2713 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2714 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2716 | 'c' ->
2717 conf.crophack <- not conf.crophack;
2718 TEdone ("crophack " ^ btos conf.crophack)
2720 | 'a' ->
2721 let s =
2722 match conf.maxwait with
2723 | None ->
2724 conf.maxwait <- Some infinity;
2725 "always wait for page to complete"
2726 | Some _ ->
2727 conf.maxwait <- None;
2728 "show placeholder if page is not ready"
2730 TEdone s
2732 | 'f' ->
2733 conf.underinfo <- not conf.underinfo;
2734 TEdone ("underinfo " ^ btos conf.underinfo)
2736 | 'P' ->
2737 conf.savebmarks <- not conf.savebmarks;
2738 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2740 | 'S' ->
2741 let ondone s =
2743 let pageno, py =
2744 match state.layout with
2745 | [] -> 0, 0
2746 | l :: _ ->
2747 l.pageno, l.pagey
2749 conf.interpagespace <- int_of_string s;
2750 state.maxy <- calcheight ();
2751 let y = getpagey pageno in
2752 gotoy (y + py)
2753 with exc ->
2754 state.text <- Printf.sprintf "bad integer `%s': %s"
2755 s (Printexc.to_string exc)
2757 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2759 | 'l' ->
2760 reqlayout conf.angle (not conf.proportional);
2761 TEdone ("proportional display " ^ btos conf.proportional)
2763 | 'T' ->
2764 settrim (not conf.trimmargins) conf.trimfuzz;
2765 TEdone ("trim margins " ^ btos conf.trimmargins)
2767 | 'I' ->
2768 conf.invert <- not conf.invert;
2769 TEdone ("invert colors " ^ btos conf.invert)
2771 | 'x' ->
2772 let ondone s =
2773 cbput state.hists.sel s;
2774 conf.selcmd <- s;
2776 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2777 textentry, ondone)
2779 | _ ->
2780 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2781 TEstop
2782 else
2783 TEcont state.text
2786 class type lvsource = object
2787 method getitemcount : int
2788 method getitem : int -> (string * int)
2789 method hasaction : int -> bool
2790 method exit :
2791 uioh:uioh ->
2792 cancel:bool ->
2793 active:int ->
2794 first:int ->
2795 pan:int ->
2796 qsearch:string ->
2797 uioh option
2798 method getactive : int
2799 method getfirst : int
2800 method getqsearch : string
2801 method setqsearch : string -> unit
2802 method getpan : int
2803 end;;
2805 class virtual lvsourcebase = object
2806 val mutable m_active = 0
2807 val mutable m_first = 0
2808 val mutable m_qsearch = ""
2809 val mutable m_pan = 0
2810 method getactive = m_active
2811 method getfirst = m_first
2812 method getqsearch = m_qsearch
2813 method getpan = m_pan
2814 method setqsearch s = m_qsearch <- s
2815 end;;
2817 let withoutlastutf8 s =
2818 let len = String.length s in
2819 if len = 0
2820 then s
2821 else
2822 let rec find pos =
2823 if pos = 0
2824 then pos
2825 else
2826 let b = Char.code s.[pos] in
2827 if b land 0b110000 = 0b11000000
2828 then find (pos-1)
2829 else pos-1
2831 let first =
2832 if Char.code s.[len-1] land 0x80 = 0
2833 then len-1
2834 else find (len-1)
2836 String.sub s 0 first;
2839 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2840 let enttext te =
2841 state.mode <- Textentry (te, onleave);
2842 state.text <- "";
2843 enttext ();
2844 G.postRedisplay "textentrykeyboard enttext";
2846 let histaction cmd =
2847 match opthist with
2848 | None -> ()
2849 | Some (action, _) ->
2850 state.mode <- Textentry (
2851 (c, action cmd, opthist, onkey, ondone), onleave
2853 G.postRedisplay "textentry histaction"
2855 match key with
2856 | 0xff08 -> (* backspace *)
2857 let s = withoutlastutf8 text in
2858 let len = String.length s in
2859 if len = 0
2860 then (
2861 onleave Cancel;
2862 G.postRedisplay "textentrykeyboard after cancel";
2864 else (
2865 enttext (c, s, opthist, onkey, ondone)
2868 | 0xff0d ->
2869 ondone text;
2870 onleave Confirm;
2871 G.postRedisplay "textentrykeyboard after confirm"
2873 | 0xff52 -> histaction HCprev
2874 | 0xff54 -> histaction HCnext
2875 | 0xff50 -> histaction HCfirst
2876 | 0xff57 -> histaction HClast
2878 | 0xff1b -> (* escape*)
2879 if String.length text = 0
2880 then (
2881 begin match opthist with
2882 | None -> ()
2883 | Some (_, onhistcancel) -> onhistcancel ()
2884 end;
2885 onleave Cancel;
2886 state.text <- "";
2887 G.postRedisplay "textentrykeyboard after cancel2"
2889 else (
2890 enttext (c, "", opthist, onkey, ondone)
2893 | 0xff9f | 0xffff -> () (* delete *)
2895 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2896 begin match onkey text key with
2897 | TEdone text ->
2898 ondone text;
2899 onleave Confirm;
2900 G.postRedisplay "textentrykeyboard after confirm2";
2902 | TEcont text ->
2903 enttext (c, text, opthist, onkey, ondone);
2905 | TEstop ->
2906 onleave Cancel;
2907 G.postRedisplay "textentrykeyboard after cancel3"
2909 | TEswitch te ->
2910 state.mode <- Textentry (te, onleave);
2911 G.postRedisplay "textentrykeyboard switch";
2912 end;
2914 | _ ->
2915 vlog "unhandled key %s" (Wsi.keyname key)
2918 let firstof first active =
2919 if first > active || abs (first - active) > fstate.maxrows - 1
2920 then max 0 (active - (fstate.maxrows/2))
2921 else first
2924 let calcfirst first active =
2925 if active > first
2926 then
2927 let rows = active - first in
2928 if rows > fstate.maxrows then active - fstate.maxrows else first
2929 else active
2932 let scrollph y maxy =
2933 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2934 let sh = float conf.winh /. sh in
2935 let sh = max sh (float conf.scrollh) in
2937 let percent =
2938 if y = state.maxy
2939 then 1.0
2940 else float y /. float maxy
2942 let position = (float conf.winh -. sh) *. percent in
2944 let position =
2945 if position +. sh > float conf.winh
2946 then float conf.winh -. sh
2947 else position
2949 position, sh;
2952 let coe s = (s :> uioh);;
2954 class listview ~(source:lvsource) ~trusted ~modehash =
2955 object (self)
2956 val m_pan = source#getpan
2957 val m_first = source#getfirst
2958 val m_active = source#getactive
2959 val m_qsearch = source#getqsearch
2960 val m_prev_uioh = state.uioh
2962 method private elemunder y =
2963 let n = y / (fstate.fontsize+1) in
2964 if m_first + n < source#getitemcount
2965 then (
2966 if source#hasaction (m_first + n)
2967 then Some (m_first + n)
2968 else None
2970 else None
2972 method display =
2973 Gl.enable `blend;
2974 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2975 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2976 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2977 GlDraw.color (1., 1., 1.);
2978 Gl.enable `texture_2d;
2979 let fs = fstate.fontsize in
2980 let nfs = fs + 1 in
2981 let ww = fstate.wwidth in
2982 let tabw = 30.0*.ww in
2983 let itemcount = source#getitemcount in
2984 let rec loop row =
2985 if (row - m_first) * nfs > conf.winh
2986 then ()
2987 else (
2988 if row >= 0 && row < itemcount
2989 then (
2990 let (s, level) = source#getitem row in
2991 let y = (row - m_first) * nfs in
2992 let x = 5.0 +. float (level + m_pan) *. ww in
2993 if row = m_active
2994 then (
2995 Gl.disable `texture_2d;
2996 GlDraw.polygon_mode `both `line;
2997 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2998 GlDraw.rect (1., float (y + 1))
2999 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3000 GlDraw.polygon_mode `both `fill;
3001 GlDraw.color (1., 1., 1.);
3002 Gl.enable `texture_2d;
3005 let drawtabularstring s =
3006 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3007 if trusted
3008 then
3009 let tabpos = try String.index s '\t' with Not_found -> -1 in
3010 if tabpos > 0
3011 then
3012 let len = String.length s - tabpos - 1 in
3013 let s1 = String.sub s 0 tabpos
3014 and s2 = String.sub s (tabpos + 1) len in
3015 let nx = drawstr x s1 in
3016 let sw = nx -. x in
3017 let x = x +. (max tabw sw) in
3018 drawstr x s2
3019 else
3020 drawstr x s
3021 else
3022 drawstr x s
3024 let _ = drawtabularstring s in
3025 loop (row+1)
3029 loop m_first;
3030 Gl.disable `blend;
3031 Gl.disable `texture_2d;
3033 method updownlevel incr =
3034 let len = source#getitemcount in
3035 let curlevel =
3036 if m_active >= 0 && m_active < len
3037 then snd (source#getitem m_active)
3038 else -1
3040 let rec flow i =
3041 if i = len then i-1 else if i = -1 then 0 else
3042 let _, l = source#getitem i in
3043 if l != curlevel then i else flow (i+incr)
3045 let active = flow m_active in
3046 let first = calcfirst m_first active in
3047 G.postRedisplay "outline updownlevel";
3048 {< m_active = active; m_first = first >}
3050 method private key1 key mask =
3051 let set1 active first qsearch =
3052 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3054 let search active pattern incr =
3055 let dosearch re =
3056 let rec loop n =
3057 if n >= 0 && n < source#getitemcount
3058 then (
3059 let s, _ = source#getitem n in
3061 (try ignore (Str.search_forward re s 0); true
3062 with Not_found -> false)
3063 then Some n
3064 else loop (n + incr)
3066 else None
3068 loop active
3071 let re = Str.regexp_case_fold pattern in
3072 dosearch re
3073 with Failure s ->
3074 state.text <- s;
3075 None
3077 let itemcount = source#getitemcount in
3078 let find start incr =
3079 let rec find i =
3080 if i = -1 || i = itemcount
3081 then -1
3082 else (
3083 if source#hasaction i
3084 then i
3085 else find (i + incr)
3088 find start
3090 let set active first =
3091 let first = bound first 0 (itemcount - fstate.maxrows) in
3092 state.text <- "";
3093 coe {< m_active = active; m_first = first >}
3095 let navigate incr =
3096 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3097 let active, first =
3098 let incr1 = if incr > 0 then 1 else -1 in
3099 if isvisible m_first m_active
3100 then
3101 let next =
3102 let next = m_active + incr in
3103 let next =
3104 if next < 0 || next >= itemcount
3105 then -1
3106 else find next incr1
3108 if next = -1 || abs (m_active - next) > fstate.maxrows
3109 then -1
3110 else next
3112 if next = -1
3113 then
3114 let first = m_first + incr in
3115 let first = bound first 0 (itemcount - 1) in
3116 let next =
3117 let next = m_active + incr in
3118 let next = bound next 0 (itemcount - 1) in
3119 find next ~-incr1
3121 let active = if next = -1 then m_active else next in
3122 active, first
3123 else
3124 let first = min next m_first in
3125 let first =
3126 if abs (next - first) > fstate.maxrows
3127 then first + incr
3128 else first
3130 next, first
3131 else
3132 let first = m_first + incr in
3133 let first = bound first 0 (itemcount - 1) in
3134 let active =
3135 let next = m_active + incr in
3136 let next = bound next 0 (itemcount - 1) in
3137 let next = find next incr1 in
3138 let active =
3139 if next = -1 || abs (m_active - first) > fstate.maxrows
3140 then (
3141 let active = if m_active = -1 then next else m_active in
3142 active
3144 else next
3146 if isvisible first active
3147 then active
3148 else -1
3150 active, first
3152 G.postRedisplay "listview navigate";
3153 set active first;
3155 match key with
3156 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3157 let incr = if key = 0x72 then -1 else 1 in
3158 let active, first =
3159 match search (m_active + incr) m_qsearch incr with
3160 | None ->
3161 state.text <- m_qsearch ^ " [not found]";
3162 m_active, m_first
3163 | Some active ->
3164 state.text <- m_qsearch;
3165 active, firstof m_first active
3167 G.postRedisplay "listview ctrl-r/s";
3168 set1 active first m_qsearch;
3170 | 0xff08 -> (* backspace *)
3171 if String.length m_qsearch = 0
3172 then coe self
3173 else (
3174 let qsearch = withoutlastutf8 m_qsearch in
3175 let len = String.length qsearch in
3176 if len = 0
3177 then (
3178 state.text <- "";
3179 G.postRedisplay "listview empty qsearch";
3180 set1 m_active m_first "";
3182 else
3183 let active, first =
3184 match search m_active qsearch ~-1 with
3185 | None ->
3186 state.text <- qsearch ^ " [not found]";
3187 m_active, m_first
3188 | Some active ->
3189 state.text <- qsearch;
3190 active, firstof m_first active
3192 G.postRedisplay "listview backspace qsearch";
3193 set1 active first qsearch
3196 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3197 let pattern = m_qsearch ^ Wsi.toutf8 key in
3198 let active, first =
3199 match search m_active pattern 1 with
3200 | None ->
3201 state.text <- pattern ^ " [not found]";
3202 m_active, m_first
3203 | Some active ->
3204 state.text <- pattern;
3205 active, firstof m_first active
3207 G.postRedisplay "listview qsearch add";
3208 set1 active first pattern;
3210 | 0xff1b -> (* escape *)
3211 state.text <- "";
3212 if String.length m_qsearch = 0
3213 then (
3214 G.postRedisplay "list view escape";
3215 begin
3216 match
3217 source#exit (coe self) true m_active m_first m_pan m_qsearch
3218 with
3219 | None -> m_prev_uioh
3220 | Some uioh -> uioh
3223 else (
3224 G.postRedisplay "list view kill qsearch";
3225 source#setqsearch "";
3226 coe {< m_qsearch = "" >}
3229 | 0xff0d -> (* return *)
3230 state.text <- "";
3231 let self = {< m_qsearch = "" >} in
3232 source#setqsearch "";
3233 let opt =
3234 G.postRedisplay "listview enter";
3235 if m_active >= 0 && m_active < source#getitemcount
3236 then (
3237 source#exit (coe self) false m_active m_first m_pan "";
3239 else (
3240 source#exit (coe self) true m_active m_first m_pan "";
3243 begin match opt with
3244 | None -> m_prev_uioh
3245 | Some uioh -> uioh
3248 | 0xff9f | 0xffff -> (* delete *)
3249 coe self
3251 | 0xff52 -> navigate ~-1 (* up *)
3252 | 0xff54 -> navigate 1 (* down *)
3253 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3254 | 0xff56 -> navigate fstate.maxrows (* next *)
3256 | 0xff53 -> (* right *)
3257 state.text <- "";
3258 G.postRedisplay "listview right";
3259 coe {< m_pan = m_pan - 1 >}
3261 | 0xff51 -> (* left *)
3262 state.text <- "";
3263 G.postRedisplay "listview left";
3264 coe {< m_pan = m_pan + 1 >}
3266 | 0xff50 -> (* home *)
3267 let active = find 0 1 in
3268 G.postRedisplay "listview home";
3269 set active 0;
3271 | 0xff57 -> (* end *)
3272 let first = max 0 (itemcount - fstate.maxrows) in
3273 let active = find (itemcount - 1) ~-1 in
3274 G.postRedisplay "listview end";
3275 set active first;
3277 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3278 coe self
3280 | _ ->
3281 dolog "listview unknown key %#x" key; coe self
3283 method key key mask =
3284 match state.mode with
3285 | Textentry te -> textentrykeyboard key mask te; coe self
3286 | _ -> self#key1 key mask
3288 method button button down x y _ =
3289 let opt =
3290 match button with
3291 | 1 when x > conf.winw - conf.scrollbw ->
3292 G.postRedisplay "listview scroll";
3293 if down
3294 then
3295 let _, position, sh = self#scrollph in
3296 if y > truncate position && y < truncate (position +. sh)
3297 then (
3298 state.mstate <- Mscrolly;
3299 Some (coe self)
3301 else
3302 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3303 let first = truncate (s *. float source#getitemcount) in
3304 let first = min source#getitemcount first in
3305 Some (coe {< m_first = first; m_active = first >})
3306 else (
3307 state.mstate <- Mnone;
3308 Some (coe self);
3310 | 1 when not down ->
3311 begin match self#elemunder y with
3312 | Some n ->
3313 G.postRedisplay "listview click";
3314 source#exit
3315 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3316 | _ ->
3317 Some (coe self)
3319 | n when (n == 4 || n == 5) && not down ->
3320 let len = source#getitemcount in
3321 let first =
3322 if n = 5 && m_first + fstate.maxrows >= len
3323 then
3324 m_first
3325 else
3326 let first = m_first + (if n == 4 then -1 else 1) in
3327 bound first 0 (len - 1)
3329 G.postRedisplay "listview wheel";
3330 Some (coe {< m_first = first >})
3331 | _ ->
3332 Some (coe self)
3334 match opt with
3335 | None -> m_prev_uioh
3336 | Some uioh -> uioh
3338 method motion _ y =
3339 match state.mstate with
3340 | Mscrolly ->
3341 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3342 let first = truncate (s *. float source#getitemcount) in
3343 let first = min source#getitemcount first in
3344 G.postRedisplay "listview motion";
3345 coe {< m_first = first; m_active = first >}
3346 | _ -> coe self
3348 method pmotion x y =
3349 if x < conf.winw - conf.scrollbw
3350 then
3351 let n =
3352 match self#elemunder y with
3353 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3354 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3356 let o =
3357 if n != m_active
3358 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3359 else self
3361 coe o
3362 else (
3363 Wsi.setcursor Wsi.CURSOR_INHERIT;
3364 coe self
3367 method infochanged _ = ()
3369 method scrollpw = (0, 0.0, 0.0)
3370 method scrollph =
3371 let nfs = fstate.fontsize + 1 in
3372 let y = m_first * nfs in
3373 let itemcount = source#getitemcount in
3374 let maxi = max 0 (itemcount - fstate.maxrows) in
3375 let maxy = maxi * nfs in
3376 let p, h = scrollph y maxy in
3377 conf.scrollbw, p, h
3379 method modehash = modehash
3380 end;;
3382 class outlinelistview ~source =
3383 object (self)
3384 inherit listview
3385 ~source:(source :> lvsource)
3386 ~trusted:false
3387 ~modehash:(findkeyhash conf "outline")
3388 as super
3390 method key key mask =
3391 let calcfirst first active =
3392 if active > first
3393 then
3394 let rows = active - first in
3395 if rows > fstate.maxrows then active - fstate.maxrows else first
3396 else active
3398 let navigate incr =
3399 let active = m_active + incr in
3400 let active = bound active 0 (source#getitemcount - 1) in
3401 let first = calcfirst m_first active in
3402 G.postRedisplay "outline navigate";
3403 coe {< m_active = active; m_first = first >}
3405 let ctrl = Wsi.withctrl mask in
3406 match key with
3407 | 110 when ctrl -> (* ctrl-n *)
3408 source#narrow m_qsearch;
3409 G.postRedisplay "outline ctrl-n";
3410 coe {< m_first = 0; m_active = 0 >}
3412 | 117 when ctrl -> (* ctrl-u *)
3413 source#denarrow;
3414 G.postRedisplay "outline ctrl-u";
3415 state.text <- "";
3416 coe {< m_first = 0; m_active = 0 >}
3418 | 108 when ctrl -> (* ctrl-l *)
3419 let first = m_active - (fstate.maxrows / 2) in
3420 G.postRedisplay "outline ctrl-l";
3421 coe {< m_first = first >}
3423 | 0xff9f | 0xffff -> (* delete *)
3424 source#remove m_active;
3425 G.postRedisplay "outline delete";
3426 let active = max 0 (m_active-1) in
3427 coe {< m_first = firstof m_first active;
3428 m_active = active >}
3430 | 0xff52 -> navigate ~-1 (* up *)
3431 | 0xff54 -> navigate 1 (* down *)
3432 | 0xff55 -> (* prior *)
3433 navigate ~-(fstate.maxrows)
3434 | 0xff56 -> (* next *)
3435 navigate fstate.maxrows
3437 | 0xff53 -> (* [ctrl-]right *)
3438 let o =
3439 if ctrl
3440 then (
3441 G.postRedisplay "outline ctrl right";
3442 {< m_pan = m_pan + 1 >}
3444 else self#updownlevel 1
3446 coe o
3448 | 0xff51 -> (* [ctrl-]left *)
3449 let o =
3450 if ctrl
3451 then (
3452 G.postRedisplay "outline ctrl left";
3453 {< m_pan = m_pan - 1 >}
3455 else self#updownlevel ~-1
3457 coe o
3459 | 0xff50 -> (* home *)
3460 G.postRedisplay "outline home";
3461 coe {< m_first = 0; m_active = 0 >}
3463 | 0xff57 -> (* end *)
3464 let active = source#getitemcount - 1 in
3465 let first = max 0 (active - fstate.maxrows) in
3466 G.postRedisplay "outline end";
3467 coe {< m_active = active; m_first = first >}
3469 | _ -> super#key key mask
3472 let outlinesource usebookmarks =
3473 let empty = [||] in
3474 (object
3475 inherit lvsourcebase
3476 val mutable m_items = empty
3477 val mutable m_orig_items = empty
3478 val mutable m_prev_items = empty
3479 val mutable m_narrow_pattern = ""
3480 val mutable m_hadremovals = false
3482 method getitemcount =
3483 Array.length m_items + (if m_hadremovals then 1 else 0)
3485 method getitem n =
3486 if n == Array.length m_items && m_hadremovals
3487 then
3488 ("[Confirm removal]", 0)
3489 else
3490 let s, n, _ = m_items.(n) in
3491 (s, n)
3493 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3494 ignore (uioh, first, qsearch);
3495 let confrimremoval = m_hadremovals && active = Array.length m_items in
3496 let items =
3497 if String.length m_narrow_pattern = 0
3498 then m_orig_items
3499 else m_items
3501 if not cancel
3502 then (
3503 if not confrimremoval
3504 then(
3505 let _, _, anchor = m_items.(active) in
3506 gotoanchor anchor;
3507 m_items <- items;
3509 else (
3510 state.bookmarks <- Array.to_list m_items;
3511 m_orig_items <- m_items;
3514 else m_items <- items;
3515 m_pan <- pan;
3516 None
3518 method hasaction _ = true
3520 method greetmsg =
3521 if Array.length m_items != Array.length m_orig_items
3522 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3523 else ""
3525 method narrow pattern =
3526 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3527 match reopt with
3528 | None -> ()
3529 | Some re ->
3530 let rec loop accu n =
3531 if n = -1
3532 then (
3533 m_narrow_pattern <- pattern;
3534 m_items <- Array.of_list accu
3536 else
3537 let (s, _, _) as o = m_items.(n) in
3538 let accu =
3539 if (try ignore (Str.search_forward re s 0); true
3540 with Not_found -> false)
3541 then o :: accu
3542 else accu
3544 loop accu (n-1)
3546 loop [] (Array.length m_items - 1)
3548 method denarrow =
3549 m_orig_items <- (
3550 if usebookmarks
3551 then Array.of_list state.bookmarks
3552 else state.outlines
3554 m_items <- m_orig_items
3556 method remove m =
3557 if usebookmarks
3558 then
3559 if m >= 0 && m < Array.length m_items
3560 then (
3561 m_hadremovals <- true;
3562 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3563 let n = if n >= m then n+1 else n in
3564 m_items.(n)
3568 method reset anchor items =
3569 m_hadremovals <- false;
3570 if m_orig_items == empty || m_prev_items != items
3571 then (
3572 m_orig_items <- items;
3573 if String.length m_narrow_pattern = 0
3574 then m_items <- items;
3576 m_prev_items <- items;
3577 let rely = getanchory anchor in
3578 let active =
3579 let rec loop n best bestd =
3580 if n = Array.length m_items
3581 then best
3582 else
3583 let (_, _, anchor) = m_items.(n) in
3584 let orely = getanchory anchor in
3585 let d = abs (orely - rely) in
3586 if d < bestd
3587 then loop (n+1) n d
3588 else loop (n+1) best bestd
3590 loop 0 ~-1 max_int
3592 m_active <- active;
3593 m_first <- firstof m_first active
3594 end)
3597 let enterselector usebookmarks =
3598 let source = outlinesource usebookmarks in
3599 fun errmsg ->
3600 let outlines =
3601 if usebookmarks
3602 then Array.of_list state.bookmarks
3603 else state.outlines
3605 if Array.length outlines = 0
3606 then (
3607 showtext ' ' errmsg;
3609 else (
3610 state.text <- source#greetmsg;
3611 Wsi.setcursor Wsi.CURSOR_INHERIT;
3612 let anchor = getanchor () in
3613 source#reset anchor outlines;
3614 state.uioh <- coe (new outlinelistview ~source);
3615 G.postRedisplay "enter selector";
3619 let enteroutlinemode =
3620 let f = enterselector false in
3621 fun ()-> f "Document has no outline";
3624 let enterbookmarkmode =
3625 let f = enterselector true in
3626 fun () -> f "Document has no bookmarks (yet)";
3629 let color_of_string s =
3630 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3631 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3635 let color_to_string (r, g, b) =
3636 let r = truncate (r *. 256.0)
3637 and g = truncate (g *. 256.0)
3638 and b = truncate (b *. 256.0) in
3639 Printf.sprintf "%d/%d/%d" r g b
3642 let irect_of_string s =
3643 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3646 let irect_to_string (x0,y0,x1,y1) =
3647 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3650 let makecheckers () =
3651 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3652 following to say:
3653 converted by Issac Trotts. July 25, 2002 *)
3654 let image_height = 64
3655 and image_width = 64 in
3657 let make_image () =
3658 let image =
3659 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3661 for i = 0 to image_width - 1 do
3662 for j = 0 to image_height - 1 do
3663 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3664 (if (i land 8 ) lxor (j land 8) = 0
3665 then [|255;255;255|] else [|200;200;200|])
3666 done
3667 done;
3668 image
3670 let image = make_image () in
3671 let id = GlTex.gen_texture () in
3672 GlTex.bind_texture `texture_2d id;
3673 GlPix.store (`unpack_alignment 1);
3674 GlTex.image2d image;
3675 List.iter (GlTex.parameter ~target:`texture_2d)
3676 [ `wrap_s `repeat;
3677 `wrap_t `repeat;
3678 `mag_filter `nearest;
3679 `min_filter `nearest ];
3683 let setcheckers enabled =
3684 match state.texid with
3685 | None ->
3686 if enabled then state.texid <- Some (makecheckers ())
3688 | Some texid ->
3689 if not enabled
3690 then (
3691 GlTex.delete_texture texid;
3692 state.texid <- None;
3696 let int_of_string_with_suffix s =
3697 let l = String.length s in
3698 let s1, shift =
3699 if l > 1
3700 then
3701 let suffix = Char.lowercase s.[l-1] in
3702 match suffix with
3703 | 'k' -> String.sub s 0 (l-1), 10
3704 | 'm' -> String.sub s 0 (l-1), 20
3705 | 'g' -> String.sub s 0 (l-1), 30
3706 | _ -> s, 0
3707 else s, 0
3709 let n = int_of_string s1 in
3710 let m = n lsl shift in
3711 if m < 0 || m < n
3712 then raise (Failure "value too large")
3713 else m
3716 let string_with_suffix_of_int n =
3717 if n = 0
3718 then "0"
3719 else
3720 let n, s =
3721 if n = 0
3722 then 0, ""
3723 else (
3724 if n land ((1 lsl 20) - 1) = 0
3725 then n lsr 20, "M"
3726 else (
3727 if n land ((1 lsl 10) - 1) = 0
3728 then n lsr 10, "K"
3729 else n, ""
3733 let rec loop s n =
3734 let h = n mod 1000 in
3735 let n = n / 1000 in
3736 if n = 0
3737 then string_of_int h ^ s
3738 else (
3739 let s = Printf.sprintf "_%03d%s" h s in
3740 loop s n
3743 loop "" n ^ s;
3746 let defghyllscroll = (40, 8, 32);;
3747 let ghyllscroll_of_string s =
3748 let (n, a, b) as nab =
3749 if s = "default"
3750 then defghyllscroll
3751 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3753 if n <= a || n <= b || a >= b
3754 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3755 nab;
3758 let ghyllscroll_to_string ((n, a, b) as nab) =
3759 if nab = defghyllscroll
3760 then "default"
3761 else Printf.sprintf "%d,%d,%d" n a b;
3764 let describe_location () =
3765 let f (fn, _) l =
3766 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3768 let fn, ln = List.fold_left f (-1, -1) state.layout in
3769 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3770 let percent =
3771 if maxy <= 0
3772 then 100.
3773 else (100. *. (float state.y /. float maxy))
3775 if fn = ln
3776 then
3777 Printf.sprintf "page %d of %d [%.2f%%]"
3778 (fn+1) state.pagecount percent
3779 else
3780 Printf.sprintf
3781 "pages %d-%d of %d [%.2f%%]"
3782 (fn+1) (ln+1) state.pagecount percent
3785 let enterinfomode =
3786 let btos b = if b then "\xe2\x88\x9a" else "" in
3787 let showextended = ref false in
3788 let leave mode = function
3789 | Confirm -> state.mode <- mode
3790 | Cancel -> state.mode <- mode in
3791 let src =
3792 (object
3793 val mutable m_first_time = true
3794 val mutable m_l = []
3795 val mutable m_a = [||]
3796 val mutable m_prev_uioh = nouioh
3797 val mutable m_prev_mode = View
3799 inherit lvsourcebase
3801 method reset prev_mode prev_uioh =
3802 m_a <- Array.of_list (List.rev m_l);
3803 m_l <- [];
3804 m_prev_mode <- prev_mode;
3805 m_prev_uioh <- prev_uioh;
3806 if m_first_time
3807 then (
3808 let rec loop n =
3809 if n >= Array.length m_a
3810 then ()
3811 else
3812 match m_a.(n) with
3813 | _, _, _, Action _ -> m_active <- n
3814 | _ -> loop (n+1)
3816 loop 0;
3817 m_first_time <- false;
3820 method int name get set =
3821 m_l <-
3822 (name, `int get, 1, Action (
3823 fun u ->
3824 let ondone s =
3825 try set (int_of_string s)
3826 with exn ->
3827 state.text <- Printf.sprintf "bad integer `%s': %s"
3828 s (Printexc.to_string exn)
3830 state.text <- "";
3831 let te = name ^ ": ", "", None, intentry, ondone in
3832 state.mode <- Textentry (te, leave m_prev_mode);
3834 )) :: m_l
3836 method int_with_suffix name get set =
3837 m_l <-
3838 (name, `intws get, 1, Action (
3839 fun u ->
3840 let ondone s =
3841 try set (int_of_string_with_suffix s)
3842 with exn ->
3843 state.text <- Printf.sprintf "bad integer `%s': %s"
3844 s (Printexc.to_string exn)
3846 state.text <- "";
3847 let te =
3848 name ^ ": ", "", None, intentry_with_suffix, ondone
3850 state.mode <- Textentry (te, leave m_prev_mode);
3852 )) :: m_l
3854 method bool ?(offset=1) ?(btos=btos) name get set =
3855 m_l <-
3856 (name, `bool (btos, get), offset, Action (
3857 fun u ->
3858 let v = get () in
3859 set (not v);
3861 )) :: m_l
3863 method color name get set =
3864 m_l <-
3865 (name, `color get, 1, Action (
3866 fun u ->
3867 let invalid = (nan, nan, nan) in
3868 let ondone s =
3869 let c =
3870 try color_of_string s
3871 with exn ->
3872 state.text <- Printf.sprintf "bad color `%s': %s"
3873 s (Printexc.to_string exn);
3874 invalid
3876 if c <> invalid
3877 then set c;
3879 let te = name ^ ": ", "", None, textentry, ondone in
3880 state.text <- color_to_string (get ());
3881 state.mode <- Textentry (te, leave m_prev_mode);
3883 )) :: m_l
3885 method string name get set =
3886 m_l <-
3887 (name, `string get, 1, Action (
3888 fun u ->
3889 let ondone s = set s in
3890 let te = name ^ ": ", "", None, textentry, ondone in
3891 state.mode <- Textentry (te, leave m_prev_mode);
3893 )) :: m_l
3895 method colorspace name get set =
3896 m_l <-
3897 (name, `string get, 1, Action (
3898 fun _ ->
3899 let source =
3900 let vals = [| "rgb"; "bgr"; "gray" |] in
3901 (object
3902 inherit lvsourcebase
3904 initializer
3905 m_active <- int_of_colorspace conf.colorspace;
3906 m_first <- 0;
3908 method getitemcount = Array.length vals
3909 method getitem n = (vals.(n), 0)
3910 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3911 ignore (uioh, first, pan, qsearch);
3912 if not cancel then set active;
3913 None
3914 method hasaction _ = true
3915 end)
3917 state.text <- "";
3918 let modehash = findkeyhash conf "info" in
3919 coe (new listview ~source ~trusted:true ~modehash)
3920 )) :: m_l
3922 method caption s offset =
3923 m_l <- (s, `empty, offset, Noaction) :: m_l
3925 method caption2 s f offset =
3926 m_l <- (s, `string f, offset, Noaction) :: m_l
3928 method getitemcount = Array.length m_a
3930 method getitem n =
3931 let tostr = function
3932 | `int f -> string_of_int (f ())
3933 | `intws f -> string_with_suffix_of_int (f ())
3934 | `string f -> f ()
3935 | `color f -> color_to_string (f ())
3936 | `bool (btos, f) -> btos (f ())
3937 | `empty -> ""
3939 let name, t, offset, _ = m_a.(n) in
3940 ((let s = tostr t in
3941 if String.length s > 0
3942 then Printf.sprintf "%s\t%s" name s
3943 else name),
3944 offset)
3946 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3947 let uiohopt =
3948 if not cancel
3949 then (
3950 m_qsearch <- qsearch;
3951 let uioh =
3952 match m_a.(active) with
3953 | _, _, _, Action f -> f uioh
3954 | _ -> uioh
3956 Some uioh
3958 else None
3960 m_active <- active;
3961 m_first <- first;
3962 m_pan <- pan;
3963 uiohopt
3965 method hasaction n =
3966 match m_a.(n) with
3967 | _, _, _, Action _ -> true
3968 | _ -> false
3969 end)
3971 let rec fillsrc prevmode prevuioh =
3972 let sep () = src#caption "" 0 in
3973 let colorp name get set =
3974 src#string name
3975 (fun () -> color_to_string (get ()))
3976 (fun v ->
3978 let c = color_of_string v in
3979 set c
3980 with exn ->
3981 state.text <- Printf.sprintf "bad color `%s': %s"
3982 v (Printexc.to_string exn);
3985 let oldmode = state.mode in
3986 let birdseye = isbirdseye state.mode in
3988 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3990 src#bool "presentation mode"
3991 (fun () -> conf.presentation)
3992 (fun v ->
3993 conf.presentation <- v;
3994 state.anchor <- getanchor ();
3995 represent ());
3997 src#bool "ignore case in searches"
3998 (fun () -> conf.icase)
3999 (fun v -> conf.icase <- v);
4001 src#bool "preload"
4002 (fun () -> conf.preload)
4003 (fun v -> conf.preload <- v);
4005 src#bool "highlight links"
4006 (fun () -> conf.hlinks)
4007 (fun v -> conf.hlinks <- v);
4009 src#bool "under info"
4010 (fun () -> conf.underinfo)
4011 (fun v -> conf.underinfo <- v);
4013 src#bool "persistent bookmarks"
4014 (fun () -> conf.savebmarks)
4015 (fun v -> conf.savebmarks <- v);
4017 src#bool "proportional display"
4018 (fun () -> conf.proportional)
4019 (fun v -> reqlayout conf.angle v);
4021 src#bool "trim margins"
4022 (fun () -> conf.trimmargins)
4023 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4025 src#bool "persistent location"
4026 (fun () -> conf.jumpback)
4027 (fun v -> conf.jumpback <- v);
4029 sep ();
4030 src#int "inter-page space"
4031 (fun () -> conf.interpagespace)
4032 (fun n ->
4033 conf.interpagespace <- n;
4034 let pageno, py =
4035 match state.layout with
4036 | [] -> 0, 0
4037 | l :: _ ->
4038 l.pageno, l.pagey
4040 state.maxy <- calcheight ();
4041 let y = getpagey pageno in
4042 gotoy (y + py)
4045 src#int "page bias"
4046 (fun () -> conf.pagebias)
4047 (fun v -> conf.pagebias <- v);
4049 src#int "scroll step"
4050 (fun () -> conf.scrollstep)
4051 (fun n -> conf.scrollstep <- n);
4053 src#int "auto scroll step"
4054 (fun () ->
4055 match state.autoscroll with
4056 | Some step -> step
4057 | _ -> conf.autoscrollstep)
4058 (fun n ->
4059 if state.autoscroll <> None
4060 then state.autoscroll <- Some n;
4061 conf.autoscrollstep <- n);
4063 src#int "zoom"
4064 (fun () -> truncate (conf.zoom *. 100.))
4065 (fun v -> setzoom ((float v) /. 100.));
4067 src#int "rotation"
4068 (fun () -> conf.angle)
4069 (fun v -> reqlayout v conf.proportional);
4071 src#int "scroll bar width"
4072 (fun () -> state.scrollw)
4073 (fun v ->
4074 state.scrollw <- v;
4075 conf.scrollbw <- v;
4076 reshape conf.winw conf.winh;
4079 src#int "scroll handle height"
4080 (fun () -> conf.scrollh)
4081 (fun v -> conf.scrollh <- v;);
4083 src#int "thumbnail width"
4084 (fun () -> conf.thumbw)
4085 (fun v ->
4086 conf.thumbw <- min 4096 v;
4087 match oldmode with
4088 | Birdseye beye ->
4089 leavebirdseye beye false;
4090 enterbirdseye ()
4091 | _ -> ()
4094 let mode = state.mode in
4095 src#string "columns"
4096 (fun () ->
4097 match conf.columns with
4098 | Csingle -> "1"
4099 | Cmulti (multi, _) -> multicolumns_to_string multi
4100 | Csplit (count, _) -> "-" ^ string_of_int count
4102 (fun v ->
4103 let n, a, b = multicolumns_of_string v in
4104 setcolumns mode n a b);
4106 sep ();
4107 src#caption "Presentation mode" 0;
4108 src#bool "scrollbar visible"
4109 (fun () -> conf.scrollbarinpm)
4110 (fun v ->
4111 if v != conf.scrollbarinpm
4112 then (
4113 conf.scrollbarinpm <- v;
4114 if conf.presentation
4115 then (
4116 state.scrollw <- if v then conf.scrollbw else 0;
4117 reshape conf.winw conf.winh;
4122 sep ();
4123 src#caption "Pixmap cache" 0;
4124 src#int_with_suffix "size (advisory)"
4125 (fun () -> conf.memlimit)
4126 (fun v -> conf.memlimit <- v);
4128 src#caption2 "used"
4129 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4130 (string_with_suffix_of_int state.memused)
4131 (Hashtbl.length state.tilemap)) 1;
4133 sep ();
4134 src#caption "Layout" 0;
4135 src#caption2 "Dimension"
4136 (fun () ->
4137 Printf.sprintf "%dx%d (virtual %dx%d)"
4138 conf.winw conf.winh
4139 state.w state.maxy)
4141 if conf.debug
4142 then
4143 src#caption2 "Position" (fun () ->
4144 Printf.sprintf "%dx%d" state.x state.y
4146 else
4147 src#caption2 "Visible" (fun () -> describe_location ()) 1
4150 sep ();
4151 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4152 "Save these parameters as global defaults at exit"
4153 (fun () -> conf.bedefault)
4154 (fun v -> conf.bedefault <- v)
4157 sep ();
4158 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4159 src#bool ~offset:0 ~btos "Extended parameters"
4160 (fun () -> !showextended)
4161 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4162 if !showextended
4163 then (
4164 src#bool "checkers"
4165 (fun () -> conf.checkers)
4166 (fun v -> conf.checkers <- v; setcheckers v);
4167 src#bool "update cursor"
4168 (fun () -> conf.updatecurs)
4169 (fun v -> conf.updatecurs <- v);
4170 src#bool "verbose"
4171 (fun () -> conf.verbose)
4172 (fun v -> conf.verbose <- v);
4173 src#bool "invert colors"
4174 (fun () -> conf.invert)
4175 (fun v -> conf.invert <- v);
4176 src#bool "max fit"
4177 (fun () -> conf.maxhfit)
4178 (fun v -> conf.maxhfit <- v);
4179 src#bool "redirect stderr"
4180 (fun () -> conf.redirectstderr)
4181 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4182 src#string "uri launcher"
4183 (fun () -> conf.urilauncher)
4184 (fun v -> conf.urilauncher <- v);
4185 src#string "path launcher"
4186 (fun () -> conf.pathlauncher)
4187 (fun v -> conf.pathlauncher <- v);
4188 src#string "tile size"
4189 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4190 (fun v ->
4192 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4193 conf.tileh <- max 64 w;
4194 conf.tilew <- max 64 h;
4195 flushtiles ();
4196 with exn ->
4197 state.text <- Printf.sprintf "bad tile size `%s': %s"
4198 v (Printexc.to_string exn));
4199 src#int "texture count"
4200 (fun () -> conf.texcount)
4201 (fun v ->
4202 if realloctexts v
4203 then conf.texcount <- v
4204 else showtext '!' " Failed to set texture count please retry later"
4206 src#int "slice height"
4207 (fun () -> conf.sliceheight)
4208 (fun v ->
4209 conf.sliceheight <- v;
4210 wcmd "sliceh %d" conf.sliceheight;
4212 src#int "anti-aliasing level"
4213 (fun () -> conf.aalevel)
4214 (fun v ->
4215 conf.aalevel <- bound v 0 8;
4216 state.anchor <- getanchor ();
4217 opendoc state.path state.password;
4219 src#int "ui font size"
4220 (fun () -> fstate.fontsize)
4221 (fun v -> setfontsize (bound v 5 100));
4222 colorp "background color"
4223 (fun () -> conf.bgcolor)
4224 (fun v -> conf.bgcolor <- v);
4225 src#bool "crop hack"
4226 (fun () -> conf.crophack)
4227 (fun v -> conf.crophack <- v);
4228 src#string "trim fuzz"
4229 (fun () -> irect_to_string conf.trimfuzz)
4230 (fun v ->
4232 conf.trimfuzz <- irect_of_string v;
4233 if conf.trimmargins
4234 then settrim true conf.trimfuzz;
4235 with exn ->
4236 state.text <- Printf.sprintf "bad irect `%s': %s"
4237 v (Printexc.to_string exn)
4239 src#string "throttle"
4240 (fun () ->
4241 match conf.maxwait with
4242 | None -> "show place holder if page is not ready"
4243 | Some time ->
4244 if time = infinity
4245 then "wait for page to fully render"
4246 else
4247 "wait " ^ string_of_float time
4248 ^ " seconds before showing placeholder"
4250 (fun v ->
4252 let f = float_of_string v in
4253 if f <= 0.0
4254 then conf.maxwait <- None
4255 else conf.maxwait <- Some f
4256 with exn ->
4257 state.text <- Printf.sprintf "bad time `%s': %s"
4258 v (Printexc.to_string exn)
4260 src#string "ghyll scroll"
4261 (fun () ->
4262 match conf.ghyllscroll with
4263 | None -> ""
4264 | Some nab -> ghyllscroll_to_string nab
4266 (fun v ->
4268 let gs =
4269 if String.length v = 0
4270 then None
4271 else Some (ghyllscroll_of_string v)
4273 conf.ghyllscroll <- gs
4274 with exn ->
4275 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4276 v (Printexc.to_string exn)
4278 src#string "selection command"
4279 (fun () -> conf.selcmd)
4280 (fun v -> conf.selcmd <- v);
4281 src#colorspace "color space"
4282 (fun () -> colorspace_to_string conf.colorspace)
4283 (fun v ->
4284 conf.colorspace <- colorspace_of_int v;
4285 wcmd "cs %d" v;
4286 load state.layout;
4290 sep ();
4291 src#caption "Document" 0;
4292 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4293 src#caption2 "Pages"
4294 (fun () -> string_of_int state.pagecount) 1;
4295 src#caption2 "Dimensions"
4296 (fun () -> string_of_int (List.length state.pdims)) 1;
4297 if conf.trimmargins
4298 then (
4299 sep ();
4300 src#caption "Trimmed margins" 0;
4301 src#caption2 "Dimensions"
4302 (fun () -> string_of_int (List.length state.pdims)) 1;
4305 src#reset prevmode prevuioh;
4307 fun () ->
4308 state.text <- "";
4309 let prevmode = state.mode
4310 and prevuioh = state.uioh in
4311 fillsrc prevmode prevuioh;
4312 let source = (src :> lvsource) in
4313 let modehash = findkeyhash conf "info" in
4314 state.uioh <- coe (object (self)
4315 inherit listview ~source ~trusted:true ~modehash as super
4316 val mutable m_prevmemused = 0
4317 method infochanged = function
4318 | Memused ->
4319 if m_prevmemused != state.memused
4320 then (
4321 m_prevmemused <- state.memused;
4322 G.postRedisplay "memusedchanged";
4324 | Pdim -> G.postRedisplay "pdimchanged"
4325 | Docinfo -> fillsrc prevmode prevuioh
4327 method key key mask =
4328 if not (Wsi.withctrl mask)
4329 then
4330 match key with
4331 | 0xff51 -> coe (self#updownlevel ~-1)
4332 | 0xff53 -> coe (self#updownlevel 1)
4333 | _ -> super#key key mask
4334 else super#key key mask
4335 end);
4336 G.postRedisplay "info";
4339 let enterhelpmode =
4340 let source =
4341 (object
4342 inherit lvsourcebase
4343 method getitemcount = Array.length state.help
4344 method getitem n =
4345 let s, n, _ = state.help.(n) in
4346 (s, n)
4348 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4349 let optuioh =
4350 if not cancel
4351 then (
4352 m_qsearch <- qsearch;
4353 match state.help.(active) with
4354 | _, _, Action f -> Some (f uioh)
4355 | _ -> Some (uioh)
4357 else None
4359 m_active <- active;
4360 m_first <- first;
4361 m_pan <- pan;
4362 optuioh
4364 method hasaction n =
4365 match state.help.(n) with
4366 | _, _, Action _ -> true
4367 | _ -> false
4369 initializer
4370 m_active <- -1
4371 end)
4372 in fun () ->
4373 let modehash = findkeyhash conf "help" in
4374 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4375 G.postRedisplay "help";
4378 let entermsgsmode =
4379 let msgsource =
4380 let re = Str.regexp "[\r\n]" in
4381 (object
4382 inherit lvsourcebase
4383 val mutable m_items = [||]
4385 method getitemcount = 1 + Array.length m_items
4387 method getitem n =
4388 if n = 0
4389 then "[Clear]", 0
4390 else m_items.(n-1), 0
4392 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4393 ignore uioh;
4394 if not cancel
4395 then (
4396 if active = 0
4397 then Buffer.clear state.errmsgs;
4398 m_qsearch <- qsearch;
4400 m_active <- active;
4401 m_first <- first;
4402 m_pan <- pan;
4403 None
4405 method hasaction n =
4406 n = 0
4408 method reset =
4409 state.newerrmsgs <- false;
4410 let l = Str.split re (Buffer.contents state.errmsgs) in
4411 m_items <- Array.of_list l
4413 initializer
4414 m_active <- 0
4415 end)
4416 in fun () ->
4417 state.text <- "";
4418 msgsource#reset;
4419 let source = (msgsource :> lvsource) in
4420 let modehash = findkeyhash conf "listview" in
4421 state.uioh <- coe (object
4422 inherit listview ~source ~trusted:false ~modehash as super
4423 method display =
4424 if state.newerrmsgs
4425 then msgsource#reset;
4426 super#display
4427 end);
4428 G.postRedisplay "msgs";
4431 let quickbookmark ?title () =
4432 match state.layout with
4433 | [] -> ()
4434 | l :: _ ->
4435 let title =
4436 match title with
4437 | None ->
4438 let sec = Unix.gettimeofday () in
4439 let tm = Unix.localtime sec in
4440 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4441 (l.pageno+1)
4442 tm.Unix.tm_mday
4443 tm.Unix.tm_mon
4444 (tm.Unix.tm_year + 1900)
4445 tm.Unix.tm_hour
4446 tm.Unix.tm_min
4447 | Some title -> title
4449 state.bookmarks <-
4450 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4451 :: state.bookmarks
4454 let doreshape w h =
4455 state.fullscreen <- None;
4456 Wsi.reshape w h;
4459 let setautoscrollspeed step goingdown =
4460 let incr = max 1 ((abs step) / 2) in
4461 let incr = if goingdown then incr else -incr in
4462 let astep = step + incr in
4463 state.autoscroll <- Some astep;
4466 let gotounder = function
4467 | Ulinkgoto (pageno, top) ->
4468 if pageno >= 0
4469 then (
4470 addnav ();
4471 gotopage1 pageno top;
4474 | Ulinkuri s ->
4475 gotouri s
4477 | Uremote (filename, pageno) ->
4478 let path =
4479 if Sys.file_exists filename
4480 then filename
4481 else
4482 let dir = Filename.dirname state.path in
4483 let path = Filename.concat dir filename in
4484 if Sys.file_exists path
4485 then path
4486 else ""
4488 if String.length path > 0
4489 then (
4490 let anchor = getanchor () in
4491 let ranchor = state.path, state.password, anchor in
4492 state.anchor <- (pageno, 0.0);
4493 state.ranchors <- ranchor :: state.ranchors;
4494 opendoc path "";
4496 else showtext '!' ("Could not find " ^ filename)
4498 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4501 let canpan () =
4502 match conf.columns with
4503 | Csplit _ -> true
4504 | _ -> conf.zoom > 1.0
4507 let viewkeyboard key mask =
4508 let enttext te =
4509 let mode = state.mode in
4510 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4511 state.text <- "";
4512 enttext ();
4513 G.postRedisplay "view:enttext"
4515 let ctrl = Wsi.withctrl mask in
4516 match key with
4517 | 81 -> (* Q *)
4518 exit 0
4520 | 0xff63 -> (* insert *)
4521 if conf.angle mod 360 = 0
4522 then (
4523 state.mode <- LinkNav (Ltgendir 0);
4524 gotoy state.y;
4526 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4528 | 0xff1b | 113 -> (* escape / q *)
4529 begin match state.mstate with
4530 | Mzoomrect _ ->
4531 state.mstate <- Mnone;
4532 Wsi.setcursor Wsi.CURSOR_INHERIT;
4533 G.postRedisplay "kill zoom rect";
4534 | _ ->
4535 match state.ranchors with
4536 | [] -> raise Quit
4537 | (path, password, anchor) :: rest ->
4538 state.ranchors <- rest;
4539 state.anchor <- anchor;
4540 opendoc path password
4541 end;
4543 | 0xff08 -> (* backspace *)
4544 let y = getnav ~-1 in
4545 gotoy_and_clear_text y
4547 | 111 -> (* o *)
4548 enteroutlinemode ()
4550 | 117 -> (* u *)
4551 state.rects <- [];
4552 state.text <- "";
4553 G.postRedisplay "dehighlight";
4555 | 47 | 63 -> (* / ? *)
4556 let ondone isforw s =
4557 cbput state.hists.pat s;
4558 state.searchpattern <- s;
4559 search s isforw
4561 let s = String.create 1 in
4562 s.[0] <- Char.chr key;
4563 enttext (s, "", Some (onhist state.hists.pat),
4564 textentry, ondone (key = 47))
4566 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4567 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4568 setzoom (conf.zoom +. incr)
4570 | 43 | 0xffab -> (* + *)
4571 let ondone s =
4572 let n =
4573 try int_of_string s with exc ->
4574 state.text <- Printf.sprintf "bad integer `%s': %s"
4575 s (Printexc.to_string exc);
4576 max_int
4578 if n != max_int
4579 then (
4580 conf.pagebias <- n;
4581 state.text <- "page bias is now " ^ string_of_int n;
4584 enttext ("page bias: ", "", None, intentry, ondone)
4586 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4587 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4588 setzoom (max 0.01 (conf.zoom -. decr))
4590 | 45 | 0xffad -> (* - *)
4591 let ondone msg = state.text <- msg in
4592 enttext (
4593 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4594 optentry state.mode, ondone
4597 | 48 when ctrl -> (* ctrl-0 *)
4598 setzoom 1.0
4600 | 49 when ctrl -> (* 1 *)
4601 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4602 if zoom < 1.0
4603 then setzoom zoom
4605 | 0xffc6 -> (* f9 *)
4606 togglebirdseye ()
4608 | 57 when ctrl -> (* ctrl-9 *)
4609 togglebirdseye ()
4611 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4612 when not ctrl -> (* 0..9 *)
4613 let ondone s =
4614 let n =
4615 try int_of_string s with exc ->
4616 state.text <- Printf.sprintf "bad integer `%s': %s"
4617 s (Printexc.to_string exc);
4620 if n >= 0
4621 then (
4622 addnav ();
4623 cbput state.hists.pag (string_of_int n);
4624 gotopage1 (n + conf.pagebias - 1) 0;
4627 let pageentry text key =
4628 match Char.unsafe_chr key with
4629 | 'g' -> TEdone text
4630 | _ -> intentry text key
4632 let text = "x" in text.[0] <- Char.chr key;
4633 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4635 | 98 -> (* b *)
4636 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4637 reshape conf.winw conf.winh;
4639 | 108 -> (* l *)
4640 conf.hlinks <- not conf.hlinks;
4641 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4642 G.postRedisplay "toggle highlightlinks";
4644 | 70 -> (* F *)
4645 state.glinks <- true;
4646 let a = Char.code 'a' and z = Char.code 'z' in
4647 let ondone s =
4648 let n =
4649 let rec loop pos n = if pos = String.length s then n else
4650 let m = Char.code s.[pos] - a in
4651 loop (pos+1) (n*(z-a+1) + m)
4652 in loop 0 0
4654 if n >= 0
4655 then (
4656 let rec loop n = function
4657 | [] -> ()
4658 | l :: rest ->
4659 match getopaque l.pageno with
4660 | None -> loop n rest
4661 | Some opaque ->
4662 let m = getlinkcount opaque in
4663 if n < m
4664 then (
4665 let under = getlink opaque n in
4666 addnav ();
4667 gotounder under;
4669 else loop (n-m) rest
4671 loop n state.layout;
4674 let onkey text key =
4675 if (key >= a && key <= z)
4676 then
4677 let c = Char.unsafe_chr key in
4678 let text = addchar text c in
4679 TEcont text
4680 else
4681 TEcont text
4683 let mode = state.mode in
4684 state.mode <- Textentry (
4685 (":", "", Some (onhist state.hists.pag), onkey, ondone),
4686 fun _ ->
4687 state.glinks <- false;
4688 state.mode <- mode
4690 state.text <- "";
4691 G.postRedisplay "view:enttext"
4693 | 97 -> (* a *)
4694 begin match state.autoscroll with
4695 | Some step ->
4696 conf.autoscrollstep <- step;
4697 state.autoscroll <- None
4698 | None ->
4699 if conf.autoscrollstep = 0
4700 then state.autoscroll <- Some 1
4701 else state.autoscroll <- Some conf.autoscrollstep
4704 | 112 when ctrl -> (* ctrl-p *)
4705 launchpath ()
4707 | 80 -> (* P *)
4708 conf.presentation <- not conf.presentation;
4709 if conf.presentation
4710 then (
4711 if not conf.scrollbarinpm
4712 then state.scrollw <- 0;
4714 else
4715 state.scrollw <- conf.scrollbw;
4717 showtext ' ' ("presentation mode " ^
4718 if conf.presentation then "on" else "off");
4719 state.anchor <- getanchor ();
4720 represent ()
4722 | 102 -> (* f *)
4723 begin match state.fullscreen with
4724 | None ->
4725 state.fullscreen <- Some (conf.winw, conf.winh);
4726 Wsi.fullscreen ()
4727 | Some (w, h) ->
4728 state.fullscreen <- None;
4729 doreshape w h
4732 | 103 -> (* g *)
4733 gotoy_and_clear_text 0
4735 | 71 -> (* G *)
4736 gotopage1 (state.pagecount - 1) 0
4738 | 112 | 78 -> (* p|N *)
4739 search state.searchpattern false
4741 | 110 | 0xffc0 -> (* n|F3 *)
4742 search state.searchpattern true
4744 | 116 -> (* t *)
4745 begin match state.layout with
4746 | [] -> ()
4747 | l :: _ ->
4748 gotoy_and_clear_text (getpagey l.pageno)
4751 | 32 -> (* ' ' *)
4752 begin match List.rev state.layout with
4753 | [] -> ()
4754 | l :: _ ->
4755 let pageno = min (l.pageno+1) (state.pagecount-1) in
4756 gotoy_and_clear_text (getpagey pageno)
4759 | 0xff9f | 0xffff -> (* delete *)
4760 begin match state.layout with
4761 | [] -> ()
4762 | l :: _ ->
4763 let pageno = max 0 (l.pageno-1) in
4764 gotoy_and_clear_text (getpagey pageno)
4767 | 61 -> (* = *)
4768 showtext ' ' (describe_location ());
4770 | 119 -> (* w *)
4771 begin match state.layout with
4772 | [] -> ()
4773 | l :: _ ->
4774 doreshape (l.pagew + state.scrollw) l.pageh;
4775 G.postRedisplay "w"
4778 | 39 -> (* ' *)
4779 enterbookmarkmode ()
4781 | 104 | 0xffbe -> (* h|F1 *)
4782 enterhelpmode ()
4784 | 105 -> (* i *)
4785 enterinfomode ()
4787 | 101 when conf.redirectstderr -> (* e *)
4788 entermsgsmode ()
4790 | 109 -> (* m *)
4791 let ondone s =
4792 match state.layout with
4793 | l :: _ ->
4794 state.bookmarks <-
4795 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4796 :: state.bookmarks
4797 | _ -> ()
4799 enttext ("bookmark: ", "", None, textentry, ondone)
4801 | 126 -> (* ~ *)
4802 quickbookmark ();
4803 showtext ' ' "Quick bookmark added";
4805 | 122 -> (* z *)
4806 begin match state.layout with
4807 | l :: _ ->
4808 let rect = getpdimrect l.pagedimno in
4809 let w, h =
4810 if conf.crophack
4811 then
4812 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4813 truncate (1.2 *. (rect.(3) -. rect.(0))))
4814 else
4815 (truncate (rect.(1) -. rect.(0)),
4816 truncate (rect.(3) -. rect.(0)))
4818 let w = truncate ((float w)*.conf.zoom)
4819 and h = truncate ((float h)*.conf.zoom) in
4820 if w != 0 && h != 0
4821 then (
4822 state.anchor <- getanchor ();
4823 doreshape (w + state.scrollw) (h + conf.interpagespace)
4825 G.postRedisplay "z";
4827 | [] -> ()
4830 | 50 when ctrl -> (* ctrl-2 *)
4831 let maxw = getmaxw () in
4832 if maxw > 0.0
4833 then setzoom (maxw /. float conf.winw)
4835 | 60 | 62 -> (* < > *)
4836 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4838 | 91 | 93 -> (* [ ] *)
4839 conf.colorscale <-
4840 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4842 G.postRedisplay "brightness";
4844 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4845 setzoom state.prevzoom
4847 | 107 | 0xff52 -> (* k up *)
4848 begin match state.autoscroll with
4849 | None ->
4850 begin match state.mode with
4851 | Birdseye beye -> upbirdseye 1 beye
4852 | _ ->
4853 if ctrl
4854 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4855 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4857 | Some n ->
4858 setautoscrollspeed n false
4861 | 106 | 0xff54 -> (* j down *)
4862 begin match state.autoscroll with
4863 | None ->
4864 begin match state.mode with
4865 | Birdseye beye -> downbirdseye 1 beye
4866 | _ ->
4867 if ctrl
4868 then gotoy_and_clear_text (clamp (conf.winh/2))
4869 else gotoy_and_clear_text (clamp conf.scrollstep)
4871 | Some n ->
4872 setautoscrollspeed n true
4875 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4876 if canpan ()
4877 then
4878 let dx =
4879 if ctrl
4880 then conf.winw / 2
4881 else 10
4883 let dx = if key = 0xff51 then dx else -dx in
4884 state.x <- state.x + dx;
4885 gotoy_and_clear_text state.y
4886 else (
4887 state.text <- "";
4888 G.postRedisplay "lef/right"
4891 | 0xff55 -> (* prior *)
4892 let y =
4893 if ctrl
4894 then
4895 match state.layout with
4896 | [] -> state.y
4897 | l :: _ -> state.y - l.pagey
4898 else
4899 clamp (-conf.winh)
4901 gotoghyll y
4903 | 0xff56 -> (* next *)
4904 let y =
4905 if ctrl
4906 then
4907 match List.rev state.layout with
4908 | [] -> state.y
4909 | l :: _ -> getpagey l.pageno
4910 else
4911 clamp conf.winh
4913 gotoghyll y
4915 | 0xff50 -> gotoghyll 0
4916 | 0xff57 -> gotoghyll (clamp state.maxy)
4917 | 0xff53 when Wsi.withalt mask ->
4918 gotoghyll (getnav ~-1)
4919 | 0xff51 when Wsi.withalt mask ->
4920 gotoghyll (getnav 1)
4922 | 114 -> (* r *)
4923 state.anchor <- getanchor ();
4924 opendoc state.path state.password
4926 | 118 when conf.debug -> (* v *)
4927 state.rects <- [];
4928 List.iter (fun l ->
4929 match getopaque l.pageno with
4930 | None -> ()
4931 | Some opaque ->
4932 let x0, y0, x1, y1 = pagebbox opaque in
4933 let a,b = float x0, float y0 in
4934 let c,d = float x1, float y0 in
4935 let e,f = float x1, float y1 in
4936 let h,j = float x0, float y1 in
4937 let rect = (a,b,c,d,e,f,h,j) in
4938 debugrect rect;
4939 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4940 ) state.layout;
4941 G.postRedisplay "v";
4943 | _ ->
4944 vlog "huh? %s" (Wsi.keyname key)
4947 let linknavkeyboard key mask linknav =
4948 let getpage pageno =
4949 let rec loop = function
4950 | [] -> None
4951 | l :: _ when l.pageno = pageno -> Some l
4952 | _ :: rest -> loop rest
4953 in loop state.layout
4955 let doexact (pageno, n) =
4956 match getopaque pageno, getpage pageno with
4957 | Some opaque, Some l ->
4958 if key = 0xff0d
4959 then
4960 let under = getlink opaque n in
4961 G.postRedisplay "link gotounder";
4962 gotounder under;
4963 state.mode <- View;
4964 else
4965 let opt, dir =
4966 match key with
4967 | 0xff50 -> (* home *)
4968 Some (findlink opaque LDfirst), -1
4970 | 0xff57 -> (* end *)
4971 Some (findlink opaque LDlast), 1
4973 | 0xff51 -> (* left *)
4974 Some (findlink opaque (LDleft n)), -1
4976 | 0xff53 -> (* right *)
4977 Some (findlink opaque (LDright n)), 1
4979 | 0xff52 -> (* up *)
4980 Some (findlink opaque (LDup n)), -1
4982 | 0xff54 -> (* down *)
4983 Some (findlink opaque (LDdown n)), 1
4985 | _ -> None, 0
4987 let pwl l dir =
4988 begin match findpwl l.pageno dir with
4989 | Pwlnotfound -> ()
4990 | Pwl pageno ->
4991 let notfound dir =
4992 state.mode <- LinkNav (Ltgendir dir);
4993 let y, h = getpageyh pageno in
4994 let y =
4995 if dir < 0
4996 then y + h - conf.winh
4997 else y
4999 gotoy y
5001 begin match getopaque pageno, getpage pageno with
5002 | Some opaque, Some _ ->
5003 let link =
5004 let ld = if dir > 0 then LDfirst else LDlast in
5005 findlink opaque ld
5007 begin match link with
5008 | Lfound m ->
5009 showlinktype (getlink opaque m);
5010 state.mode <- LinkNav (Ltexact (pageno, m));
5011 G.postRedisplay "linknav jpage";
5012 | _ -> notfound dir
5013 end;
5014 | _ -> notfound dir
5015 end;
5016 end;
5018 begin match opt with
5019 | Some Lnotfound -> pwl l dir;
5020 | Some (Lfound m) ->
5021 if m = n
5022 then pwl l dir
5023 else (
5024 let _, y0, _, y1 = getlinkrect opaque m in
5025 if y0 < l.pagey
5026 then gotopage1 l.pageno y0
5027 else (
5028 let d = fstate.fontsize + 1 in
5029 if y1 - l.pagey > l.pagevh - d
5030 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5031 else G.postRedisplay "linknav";
5033 showlinktype (getlink opaque m);
5034 state.mode <- LinkNav (Ltexact (l.pageno, m));
5037 | None -> viewkeyboard key mask
5038 end;
5039 | _ -> viewkeyboard key mask
5041 if key = 0xff63
5042 then (
5043 state.mode <- View;
5044 G.postRedisplay "leave linknav"
5046 else
5047 match linknav with
5048 | Ltgendir _ -> viewkeyboard key mask
5049 | Ltexact exact -> doexact exact
5052 let keyboard key mask =
5053 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5054 then wcmd "interrupt"
5055 else state.uioh <- state.uioh#key key mask
5058 let birdseyekeyboard key mask
5059 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5060 let incr =
5061 match conf.columns with
5062 | Csingle -> 1
5063 | Cmulti ((c, _, _), _) -> c
5064 | Csplit _ -> failwith "bird's eye split mode"
5066 match key with
5067 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5068 let y, h = getpageyh pageno in
5069 let top = (conf.winh - h) / 2 in
5070 gotoy (max 0 (y - top))
5071 | 0xff0d -> leavebirdseye beye false
5072 | 0xff1b -> leavebirdseye beye true (* escape *)
5073 | 0xff52 -> upbirdseye incr beye (* prior *)
5074 | 0xff54 -> downbirdseye incr beye (* next *)
5075 | 0xff51 -> upbirdseye 1 beye (* up *)
5076 | 0xff53 -> downbirdseye 1 beye (* down *)
5078 | 0xff55 ->
5079 begin match state.layout with
5080 | l :: _ ->
5081 if l.pagey != 0
5082 then (
5083 state.mode <- Birdseye (
5084 oconf, leftx, l.pageno, hooverpageno, anchor
5086 gotopage1 l.pageno 0;
5088 else (
5089 let layout = layout (state.y-conf.winh) conf.winh in
5090 match layout with
5091 | [] -> gotoy (clamp (-conf.winh))
5092 | l :: _ ->
5093 state.mode <- Birdseye (
5094 oconf, leftx, l.pageno, hooverpageno, anchor
5096 gotopage1 l.pageno 0
5099 | [] -> gotoy (clamp (-conf.winh))
5100 end;
5102 | 0xff56 ->
5103 begin match List.rev state.layout with
5104 | l :: _ ->
5105 let layout = layout (state.y + conf.winh) conf.winh in
5106 begin match layout with
5107 | [] ->
5108 let incr = l.pageh - l.pagevh in
5109 if incr = 0
5110 then (
5111 state.mode <-
5112 Birdseye (
5113 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5115 G.postRedisplay "birdseye pagedown";
5117 else gotoy (clamp (incr + conf.interpagespace*2));
5119 | l :: _ ->
5120 state.mode <-
5121 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5122 gotopage1 l.pageno 0;
5125 | [] -> gotoy (clamp conf.winh)
5126 end;
5128 | 0xff50 ->
5129 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5130 gotopage1 0 0
5132 | 0xff57 ->
5133 let pageno = state.pagecount - 1 in
5134 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5135 if not (pagevisible state.layout pageno)
5136 then
5137 let h =
5138 match List.rev state.pdims with
5139 | [] -> conf.winh
5140 | (_, _, h, _) :: _ -> h
5142 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5143 else G.postRedisplay "birdseye end";
5144 | _ -> viewkeyboard key mask
5147 let drawpage l linkindexbase =
5148 let color =
5149 match state.mode with
5150 | Textentry _ -> scalecolor 0.4
5151 | LinkNav _
5152 | View -> scalecolor 1.0
5153 | Birdseye (_, _, pageno, hooverpageno, _) ->
5154 if l.pageno = hooverpageno
5155 then scalecolor 0.9
5156 else (
5157 if l.pageno = pageno
5158 then scalecolor 1.0
5159 else scalecolor 0.8
5162 drawtiles l color;
5163 begin match getopaque l.pageno with
5164 | Some opaque ->
5165 if tileready l l.pagex l.pagey
5166 then
5167 let x = l.pagedispx - l.pagex
5168 and y = l.pagedispy - l.pagey in
5169 let hlmask = (if conf.hlinks then 1 else 0)
5170 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5172 postprocess opaque hlmask x y linkindexbase;
5173 else 0
5175 | _ -> 0
5176 end;
5179 let scrollindicator () =
5180 let sbw, ph, sh = state.uioh#scrollph in
5181 let sbh, pw, sw = state.uioh#scrollpw in
5183 GlDraw.color (0.64, 0.64, 0.64);
5184 GlDraw.rect
5185 (float (conf.winw - sbw), 0.)
5186 (float conf.winw, float conf.winh)
5188 GlDraw.rect
5189 (0., float (conf.winh - sbh))
5190 (float (conf.winw - state.scrollw - 1), float conf.winh)
5192 GlDraw.color (0.0, 0.0, 0.0);
5194 GlDraw.rect
5195 (float (conf.winw - sbw), ph)
5196 (float conf.winw, ph +. sh)
5198 GlDraw.rect
5199 (pw, float (conf.winh - sbh))
5200 (pw +. sw, float conf.winh)
5204 let showsel () =
5205 match state.mstate with
5206 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5209 | Msel ((x0, y0), (x1, y1)) ->
5210 let rec loop = function
5211 | l :: ls ->
5212 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5213 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5214 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5215 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5216 then
5217 match getopaque l.pageno with
5218 | Some opaque ->
5219 let x0, y0 = pagetranslatepoint l x0 y0 in
5220 let x1, y1 = pagetranslatepoint l x1 y1 in
5221 seltext opaque (x0, y0, x1, y1);
5222 | _ -> ()
5223 else loop ls
5224 | [] -> ()
5226 loop state.layout
5229 let showrects rects =
5230 Gl.enable `blend;
5231 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5232 GlDraw.polygon_mode `both `fill;
5233 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5234 List.iter
5235 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5236 List.iter (fun l ->
5237 if l.pageno = pageno
5238 then (
5239 let dx = float (l.pagedispx - l.pagex) in
5240 let dy = float (l.pagedispy - l.pagey) in
5241 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5242 GlDraw.begins `quads;
5244 GlDraw.vertex2 (x0+.dx, y0+.dy);
5245 GlDraw.vertex2 (x1+.dx, y1+.dy);
5246 GlDraw.vertex2 (x2+.dx, y2+.dy);
5247 GlDraw.vertex2 (x3+.dx, y3+.dy);
5249 GlDraw.ends ();
5251 ) state.layout
5252 ) rects
5254 Gl.disable `blend;
5257 let display () =
5258 GlClear.color (scalecolor2 conf.bgcolor);
5259 GlClear.clear [`color];
5260 let rec loop linkindexbase = function
5261 | l :: rest ->
5262 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5263 loop linkindexbase rest
5264 | [] -> ()
5266 loop 0 state.layout;
5267 let rects =
5268 match state.mode with
5269 | LinkNav (Ltexact (pageno, linkno)) ->
5270 begin match getopaque pageno with
5271 | Some opaque ->
5272 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5273 (pageno, 5, (
5274 float x0, float y0,
5275 float x1, float y0,
5276 float x1, float y1,
5277 float x0, float y1)
5278 ) :: state.rects
5279 | None -> state.rects
5281 | _ -> state.rects
5283 showrects rects;
5284 showsel ();
5285 state.uioh#display;
5286 begin match state.mstate with
5287 | Mzoomrect ((x0, y0), (x1, y1)) ->
5288 Gl.enable `blend;
5289 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5290 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5291 GlDraw.rect (float x0, float y0)
5292 (float x1, float y1);
5293 Gl.disable `blend;
5294 | _ -> ()
5295 end;
5296 enttext ();
5297 scrollindicator ();
5298 Wsi.swapb ();
5301 let zoomrect x y x1 y1 =
5302 let x0 = min x x1
5303 and x1 = max x x1
5304 and y0 = min y y1 in
5305 gotoy (state.y + y0);
5306 state.anchor <- getanchor ();
5307 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5308 let margin =
5309 if state.w < conf.winw - state.scrollw
5310 then (conf.winw - state.scrollw - state.w) / 2
5311 else 0
5313 state.x <- (state.x + margin) - x0;
5314 setzoom zoom;
5315 Wsi.setcursor Wsi.CURSOR_INHERIT;
5316 state.mstate <- Mnone;
5319 let scrollx x =
5320 let winw = conf.winw - state.scrollw - 1 in
5321 let s = float x /. float winw in
5322 let destx = truncate (float (state.w + winw) *. s) in
5323 state.x <- winw - destx;
5324 gotoy_and_clear_text state.y;
5325 state.mstate <- Mscrollx;
5328 let scrolly y =
5329 let s = float y /. float conf.winh in
5330 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5331 gotoy_and_clear_text desty;
5332 state.mstate <- Mscrolly;
5335 let viewmouse button down x y mask =
5336 match button with
5337 | n when (n == 4 || n == 5) && not down ->
5338 if Wsi.withctrl mask
5339 then (
5340 match state.mstate with
5341 | Mzoom (oldn, i) ->
5342 if oldn = n
5343 then (
5344 if i = 2
5345 then
5346 let incr =
5347 match n with
5348 | 5 ->
5349 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5350 | _ ->
5351 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5353 let zoom = conf.zoom -. incr in
5354 setzoom zoom;
5355 state.mstate <- Mzoom (n, 0);
5356 else
5357 state.mstate <- Mzoom (n, i+1);
5359 else state.mstate <- Mzoom (n, 0)
5361 | _ -> state.mstate <- Mzoom (n, 0)
5363 else (
5364 match state.autoscroll with
5365 | Some step -> setautoscrollspeed step (n=4)
5366 | None ->
5367 let incr =
5368 if n = 4
5369 then -conf.scrollstep
5370 else conf.scrollstep
5372 let incr = incr * 2 in
5373 let y = clamp incr in
5374 gotoy_and_clear_text y
5377 | 1 when Wsi.withctrl mask ->
5378 if down
5379 then (
5380 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5381 state.mstate <- Mpan (x, y)
5383 else
5384 state.mstate <- Mnone
5386 | 3 ->
5387 if down
5388 then (
5389 Wsi.setcursor Wsi.CURSOR_CYCLE;
5390 let p = (x, y) in
5391 state.mstate <- Mzoomrect (p, p)
5393 else (
5394 match state.mstate with
5395 | Mzoomrect ((x0, y0), _) ->
5396 if abs (x-x0) > 10 && abs (y - y0) > 10
5397 then zoomrect x0 y0 x y
5398 else (
5399 state.mstate <- Mnone;
5400 Wsi.setcursor Wsi.CURSOR_INHERIT;
5401 G.postRedisplay "kill accidental zoom rect";
5403 | _ ->
5404 Wsi.setcursor Wsi.CURSOR_INHERIT;
5405 state.mstate <- Mnone
5408 | 1 when x > conf.winw - state.scrollw ->
5409 if down
5410 then
5411 let _, position, sh = state.uioh#scrollph in
5412 if y > truncate position && y < truncate (position +. sh)
5413 then state.mstate <- Mscrolly
5414 else scrolly y
5415 else
5416 state.mstate <- Mnone
5418 | 1 when y > conf.winh - state.hscrollh ->
5419 if down
5420 then
5421 let _, position, sw = state.uioh#scrollpw in
5422 if x > truncate position && x < truncate (position +. sw)
5423 then state.mstate <- Mscrollx
5424 else scrollx x
5425 else
5426 state.mstate <- Mnone
5428 | 1 ->
5429 let dest = if down then getunder x y else Unone in
5430 begin match dest with
5431 | Ulinkgoto _
5432 | Ulinkuri _
5433 | Uremote _
5434 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5435 gotounder dest
5437 | Unone when down ->
5438 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5439 state.mstate <- Mpan (x, y);
5441 | Unone | Utext _ ->
5442 if down
5443 then (
5444 if conf.angle mod 360 = 0
5445 then (
5446 state.mstate <- Msel ((x, y), (x, y));
5447 G.postRedisplay "mouse select";
5450 else (
5451 match state.mstate with
5452 | Mnone -> ()
5454 | Mzoom _ | Mscrollx | Mscrolly ->
5455 state.mstate <- Mnone
5457 | Mzoomrect ((x0, y0), _) ->
5458 zoomrect x0 y0 x y
5460 | Mpan _ ->
5461 Wsi.setcursor Wsi.CURSOR_INHERIT;
5462 state.mstate <- Mnone
5464 | Msel ((_, y0), (_, y1)) ->
5465 let rec loop = function
5466 | [] -> ()
5467 | l :: rest ->
5468 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5469 || ((y1 >= l.pagedispy
5470 && y1 <= (l.pagedispy + l.pagevh)))
5471 then
5472 match getopaque l.pageno with
5473 | Some opaque ->
5474 begin
5475 match
5476 try Some (Unix.pipe ())
5477 with exn ->
5478 dolog "can not create sel pipe: %s"
5479 (Printexc.to_string exn);
5480 None
5481 with
5482 | None -> ()
5483 | Some (r, w) ->
5484 let doclose what fd =
5485 try Unix.close fd
5486 with exn ->
5487 dolog "%s close failed: %s"
5488 what (Printexc.to_string exn)
5490 let docopysel r w =
5491 copysel w opaque;
5492 doclose "pipe/r" r;
5493 G.postRedisplay "copysel"
5496 popen conf.selcmd [r, 0; w, -1];
5497 docopysel r w;
5498 with exn ->
5499 dolog "can not exectute %S: %s"
5500 conf.selcmd (Printexc.to_string exn);
5501 doclose "pipe/r" r;
5502 doclose "pipe/w" w;
5504 | None -> ()
5505 else loop rest
5507 loop state.layout;
5508 Wsi.setcursor Wsi.CURSOR_INHERIT;
5509 state.mstate <- Mnone;
5513 | _ -> ()
5516 let birdseyemouse button down x y mask
5517 (conf, leftx, _, hooverpageno, anchor) =
5518 match button with
5519 | 1 when down ->
5520 let rec loop = function
5521 | [] -> ()
5522 | l :: rest ->
5523 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5524 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5525 then (
5526 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5528 else loop rest
5530 loop state.layout
5531 | 3 -> ()
5532 | _ -> viewmouse button down x y mask
5535 let mouse button down x y mask =
5536 state.uioh <- state.uioh#button button down x y mask;
5539 let motion ~x ~y =
5540 state.uioh <- state.uioh#motion x y
5543 let pmotion ~x ~y =
5544 state.uioh <- state.uioh#pmotion x y;
5547 let uioh = object
5548 method display = ()
5550 method key key mask =
5551 begin match state.mode with
5552 | Textentry textentry -> textentrykeyboard key mask textentry
5553 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5554 | View -> viewkeyboard key mask
5555 | LinkNav linknav -> linknavkeyboard key mask linknav
5556 end;
5557 state.uioh
5559 method button button bstate x y mask =
5560 begin match state.mode with
5561 | LinkNav _
5562 | View -> viewmouse button bstate x y mask
5563 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5564 | Textentry _ -> ()
5565 end;
5566 state.uioh
5568 method motion x y =
5569 begin match state.mode with
5570 | Textentry _ -> ()
5571 | View | Birdseye _ | LinkNav _ ->
5572 match state.mstate with
5573 | Mzoom _ | Mnone -> ()
5575 | Mpan (x0, y0) ->
5576 let dx = x - x0
5577 and dy = y0 - y in
5578 state.mstate <- Mpan (x, y);
5579 if canpan ()
5580 then state.x <- state.x + dx;
5581 let y = clamp dy in
5582 gotoy_and_clear_text y
5584 | Msel (a, _) ->
5585 state.mstate <- Msel (a, (x, y));
5586 G.postRedisplay "motion select";
5588 | Mscrolly ->
5589 let y = min conf.winh (max 0 y) in
5590 scrolly y
5592 | Mscrollx ->
5593 let x = min conf.winw (max 0 x) in
5594 scrollx x
5596 | Mzoomrect (p0, _) ->
5597 state.mstate <- Mzoomrect (p0, (x, y));
5598 G.postRedisplay "motion zoomrect";
5599 end;
5600 state.uioh
5602 method pmotion x y =
5603 begin match state.mode with
5604 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5605 let rec loop = function
5606 | [] ->
5607 if hooverpageno != -1
5608 then (
5609 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5610 G.postRedisplay "pmotion birdseye no hoover";
5612 | l :: rest ->
5613 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5614 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5615 then (
5616 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5617 G.postRedisplay "pmotion birdseye hoover";
5619 else loop rest
5621 loop state.layout
5623 | Textentry _ -> ()
5625 | LinkNav _
5626 | View ->
5627 match state.mstate with
5628 | Mnone -> updateunder x y
5629 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5631 end;
5632 state.uioh
5634 method infochanged _ = ()
5636 method scrollph =
5637 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5638 let p, h = scrollph state.y maxy in
5639 state.scrollw, p, h
5641 method scrollpw =
5642 let winw = conf.winw - state.scrollw - 1 in
5643 let fwinw = float winw in
5644 let sw =
5645 let sw = fwinw /. float state.w in
5646 let sw = fwinw *. sw in
5647 max sw (float conf.scrollh)
5649 let position, sw =
5650 let f = state.w+winw in
5651 let r = float (winw-state.x) /. float f in
5652 let p = fwinw *. r in
5653 p-.sw/.2., sw
5655 let sw =
5656 if position +. sw > fwinw
5657 then fwinw -. position
5658 else sw
5660 state.hscrollh, position, sw
5662 method modehash =
5663 let modename =
5664 match state.mode with
5665 | LinkNav _ -> "links"
5666 | Textentry _ -> "textentry"
5667 | Birdseye _ -> "birdseye"
5668 | View -> "global"
5670 findkeyhash conf modename
5671 end;;
5673 module Config =
5674 struct
5675 open Parser
5677 let fontpath = ref "";;
5679 module KeyMap =
5680 Map.Make (struct type t = (int * int) let compare = compare end);;
5682 let unent s =
5683 let l = String.length s in
5684 let b = Buffer.create l in
5685 unent b s 0 l;
5686 Buffer.contents b;
5689 let home =
5690 try Sys.getenv "HOME"
5691 with exn ->
5692 prerr_endline
5693 ("Can not determine home directory location: " ^
5694 Printexc.to_string exn);
5698 let modifier_of_string = function
5699 | "alt" -> Wsi.altmask
5700 | "shift" -> Wsi.shiftmask
5701 | "ctrl" | "control" -> Wsi.ctrlmask
5702 | "meta" -> Wsi.metamask
5703 | _ -> 0
5706 let key_of_string =
5707 let r = Str.regexp "-" in
5708 fun s ->
5709 let elems = Str.full_split r s in
5710 let f n k m =
5711 let g s =
5712 let m1 = modifier_of_string s in
5713 if m1 = 0
5714 then (Wsi.namekey s, m)
5715 else (k, m lor m1)
5716 in function
5717 | Str.Delim s when n land 1 = 0 -> g s
5718 | Str.Text s -> g s
5719 | Str.Delim _ -> (k, m)
5721 let rec loop n k m = function
5722 | [] -> (k, m)
5723 | x :: xs ->
5724 let k, m = f n k m x in
5725 loop (n+1) k m xs
5727 loop 0 0 0 elems
5730 let keys_of_string =
5731 let r = Str.regexp "[ \t]" in
5732 fun s ->
5733 let elems = Str.split r s in
5734 List.map key_of_string elems
5737 let copykeyhashes c =
5738 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5741 let config_of c attrs =
5742 let apply c k v =
5744 match k with
5745 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5746 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5747 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5748 | "preload" -> { c with preload = bool_of_string v }
5749 | "page-bias" -> { c with pagebias = int_of_string v }
5750 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5751 | "auto-scroll-step" ->
5752 { c with autoscrollstep = max 0 (int_of_string v) }
5753 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5754 | "crop-hack" -> { c with crophack = bool_of_string v }
5755 | "throttle" ->
5756 let mw =
5757 match String.lowercase v with
5758 | "true" -> Some infinity
5759 | "false" -> None
5760 | f -> Some (float_of_string f)
5762 { c with maxwait = mw}
5763 | "highlight-links" -> { c with hlinks = bool_of_string v }
5764 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5765 | "vertical-margin" ->
5766 { c with interpagespace = max 0 (int_of_string v) }
5767 | "zoom" ->
5768 let zoom = float_of_string v /. 100. in
5769 let zoom = max zoom 0.0 in
5770 { c with zoom = zoom }
5771 | "presentation" -> { c with presentation = bool_of_string v }
5772 | "rotation-angle" -> { c with angle = int_of_string v }
5773 | "width" -> { c with winw = max 20 (int_of_string v) }
5774 | "height" -> { c with winh = max 20 (int_of_string v) }
5775 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5776 | "proportional-display" -> { c with proportional = bool_of_string v }
5777 | "pixmap-cache-size" ->
5778 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5779 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5780 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5781 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5782 | "persistent-location" -> { c with jumpback = bool_of_string v }
5783 | "background-color" -> { c with bgcolor = color_of_string v }
5784 | "scrollbar-in-presentation" ->
5785 { c with scrollbarinpm = bool_of_string v }
5786 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5787 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5788 | "mupdf-store-size" ->
5789 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5790 | "checkers" -> { c with checkers = bool_of_string v }
5791 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5792 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5793 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5794 | "uri-launcher" -> { c with urilauncher = unent v }
5795 | "path-launcher" -> { c with pathlauncher = unent v }
5796 | "color-space" -> { c with colorspace = colorspace_of_string v }
5797 | "invert-colors" -> { c with invert = bool_of_string v }
5798 | "brightness" -> { c with colorscale = float_of_string v }
5799 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5800 | "ghyllscroll" ->
5801 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5802 | "columns" ->
5803 let (n, _, _) as nab = multicolumns_of_string v in
5804 if n < 0
5805 then { c with columns = Csplit (-n, [||]) }
5806 else { c with columns = Cmulti (nab, [||]) }
5807 | "birds-eye-columns" ->
5808 { c with beyecolumns = Some (max (int_of_string v) 2) }
5809 | "selection-command" -> { c with selcmd = unent v }
5810 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5811 | _ -> c
5812 with exn ->
5813 prerr_endline ("Error processing attribute (`" ^
5814 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5817 let rec fold c = function
5818 | [] -> c
5819 | (k, v) :: rest ->
5820 let c = apply c k v in
5821 fold c rest
5823 fold { c with keyhashes = copykeyhashes c } attrs;
5826 let fromstring f pos n v d =
5827 try f v
5828 with exn ->
5829 dolog "Error processing attribute (%S=%S) at %d\n%s"
5830 n v pos (Printexc.to_string exn)
5835 let bookmark_of attrs =
5836 let rec fold title page rely = function
5837 | ("title", v) :: rest -> fold v page rely rest
5838 | ("page", v) :: rest -> fold title v rely rest
5839 | ("rely", v) :: rest -> fold title page v rest
5840 | _ :: rest -> fold title page rely rest
5841 | [] -> title, page, rely
5843 fold "invalid" "0" "0" attrs
5846 let doc_of attrs =
5847 let rec fold path page rely pan = function
5848 | ("path", v) :: rest -> fold v page rely pan rest
5849 | ("page", v) :: rest -> fold path v rely pan rest
5850 | ("rely", v) :: rest -> fold path page v pan rest
5851 | ("pan", v) :: rest -> fold path page rely v rest
5852 | _ :: rest -> fold path page rely pan rest
5853 | [] -> path, page, rely, pan
5855 fold "" "0" "0" "0" attrs
5858 let map_of attrs =
5859 let rec fold rs ls = function
5860 | ("out", v) :: rest -> fold v ls rest
5861 | ("in", v) :: rest -> fold rs v rest
5862 | _ :: rest -> fold ls rs rest
5863 | [] -> ls, rs
5865 fold "" "" attrs
5868 let setconf dst src =
5869 dst.scrollbw <- src.scrollbw;
5870 dst.scrollh <- src.scrollh;
5871 dst.icase <- src.icase;
5872 dst.preload <- src.preload;
5873 dst.pagebias <- src.pagebias;
5874 dst.verbose <- src.verbose;
5875 dst.scrollstep <- src.scrollstep;
5876 dst.maxhfit <- src.maxhfit;
5877 dst.crophack <- src.crophack;
5878 dst.autoscrollstep <- src.autoscrollstep;
5879 dst.maxwait <- src.maxwait;
5880 dst.hlinks <- src.hlinks;
5881 dst.underinfo <- src.underinfo;
5882 dst.interpagespace <- src.interpagespace;
5883 dst.zoom <- src.zoom;
5884 dst.presentation <- src.presentation;
5885 dst.angle <- src.angle;
5886 dst.winw <- src.winw;
5887 dst.winh <- src.winh;
5888 dst.savebmarks <- src.savebmarks;
5889 dst.memlimit <- src.memlimit;
5890 dst.proportional <- src.proportional;
5891 dst.texcount <- src.texcount;
5892 dst.sliceheight <- src.sliceheight;
5893 dst.thumbw <- src.thumbw;
5894 dst.jumpback <- src.jumpback;
5895 dst.bgcolor <- src.bgcolor;
5896 dst.scrollbarinpm <- src.scrollbarinpm;
5897 dst.tilew <- src.tilew;
5898 dst.tileh <- src.tileh;
5899 dst.mustoresize <- src.mustoresize;
5900 dst.checkers <- src.checkers;
5901 dst.aalevel <- src.aalevel;
5902 dst.trimmargins <- src.trimmargins;
5903 dst.trimfuzz <- src.trimfuzz;
5904 dst.urilauncher <- src.urilauncher;
5905 dst.colorspace <- src.colorspace;
5906 dst.invert <- src.invert;
5907 dst.colorscale <- src.colorscale;
5908 dst.redirectstderr <- src.redirectstderr;
5909 dst.ghyllscroll <- src.ghyllscroll;
5910 dst.columns <- src.columns;
5911 dst.beyecolumns <- src.beyecolumns;
5912 dst.selcmd <- src.selcmd;
5913 dst.updatecurs <- src.updatecurs;
5914 dst.pathlauncher <- src.pathlauncher;
5915 dst.keyhashes <- copykeyhashes src;
5918 let get s =
5919 let h = Hashtbl.create 10 in
5920 let dc = { defconf with angle = defconf.angle } in
5921 let rec toplevel v t spos _ =
5922 match t with
5923 | Vdata | Vcdata | Vend -> v
5924 | Vopen ("llppconfig", _, closed) ->
5925 if closed
5926 then v
5927 else { v with f = llppconfig }
5928 | Vopen _ ->
5929 error "unexpected subelement at top level" s spos
5930 | Vclose _ -> error "unexpected close at top level" s spos
5932 and llppconfig v t spos _ =
5933 match t with
5934 | Vdata | Vcdata -> v
5935 | Vend -> error "unexpected end of input in llppconfig" s spos
5936 | Vopen ("defaults", attrs, closed) ->
5937 let c = config_of dc attrs in
5938 setconf dc c;
5939 if closed
5940 then v
5941 else { v with f = defaults }
5943 | Vopen ("ui-font", attrs, closed) ->
5944 let rec getsize size = function
5945 | [] -> size
5946 | ("size", v) :: rest ->
5947 let size =
5948 fromstring int_of_string spos "size" v fstate.fontsize in
5949 getsize size rest
5950 | l -> getsize size l
5952 fstate.fontsize <- getsize fstate.fontsize attrs;
5953 if closed
5954 then v
5955 else { v with f = uifont (Buffer.create 10) }
5957 | Vopen ("doc", attrs, closed) ->
5958 let pathent, spage, srely, span = doc_of attrs in
5959 let path = unent pathent
5960 and pageno = fromstring int_of_string spos "page" spage 0
5961 and rely = fromstring float_of_string spos "rely" srely 0.0
5962 and pan = fromstring int_of_string spos "pan" span 0 in
5963 let c = config_of dc attrs in
5964 let anchor = (pageno, rely) in
5965 if closed
5966 then (Hashtbl.add h path (c, [], pan, anchor); v)
5967 else { v with f = doc path pan anchor c [] }
5969 | Vopen _ ->
5970 error "unexpected subelement in llppconfig" s spos
5972 | Vclose "llppconfig" -> { v with f = toplevel }
5973 | Vclose _ -> error "unexpected close in llppconfig" s spos
5975 and defaults v t spos _ =
5976 match t with
5977 | Vdata | Vcdata -> v
5978 | Vend -> error "unexpected end of input in defaults" s spos
5979 | Vopen ("keymap", attrs, closed) ->
5980 let modename =
5981 try List.assoc "mode" attrs
5982 with Not_found -> "global" in
5983 if closed
5984 then v
5985 else
5986 let ret keymap =
5987 let h = findkeyhash dc modename in
5988 KeyMap.iter (Hashtbl.replace h) keymap;
5989 defaults
5991 { v with f = pkeymap ret KeyMap.empty }
5993 | Vopen (_, _, _) ->
5994 error "unexpected subelement in defaults" s spos
5996 | Vclose "defaults" ->
5997 { v with f = llppconfig }
5999 | Vclose _ -> error "unexpected close in defaults" s spos
6001 and uifont b v t spos epos =
6002 match t with
6003 | Vdata | Vcdata ->
6004 Buffer.add_substring b s spos (epos - spos);
6006 | Vopen (_, _, _) ->
6007 error "unexpected subelement in ui-font" s spos
6008 | Vclose "ui-font" ->
6009 if String.length !fontpath = 0
6010 then fontpath := Buffer.contents b;
6011 { v with f = llppconfig }
6012 | Vclose _ -> error "unexpected close in ui-font" s spos
6013 | Vend -> error "unexpected end of input in ui-font" s spos
6015 and doc path pan anchor c bookmarks v t spos _ =
6016 match t with
6017 | Vdata | Vcdata -> v
6018 | Vend -> error "unexpected end of input in doc" s spos
6019 | Vopen ("bookmarks", _, closed) ->
6020 if closed
6021 then v
6022 else { v with f = pbookmarks path pan anchor c bookmarks }
6024 | Vopen ("keymap", attrs, closed) ->
6025 let modename =
6026 try List.assoc "mode" attrs
6027 with Not_found -> "global"
6029 if closed
6030 then v
6031 else
6032 let ret keymap =
6033 let h = findkeyhash c modename in
6034 KeyMap.iter (Hashtbl.replace h) keymap;
6035 doc path pan anchor c bookmarks
6037 { v with f = pkeymap ret KeyMap.empty }
6039 | Vopen (_, _, _) ->
6040 error "unexpected subelement in doc" s spos
6042 | Vclose "doc" ->
6043 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6044 { v with f = llppconfig }
6046 | Vclose _ -> error "unexpected close in doc" s spos
6048 and pkeymap ret keymap v t spos _ =
6049 match t with
6050 | Vdata | Vcdata -> v
6051 | Vend -> error "unexpected end of input in keymap" s spos
6052 | Vopen ("map", attrs, closed) ->
6053 let r, l = map_of attrs in
6054 let kss = fromstring keys_of_string spos "in" r [] in
6055 let lss = fromstring keys_of_string spos "out" l [] in
6056 let keymap =
6057 match kss with
6058 | [] -> keymap
6059 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6060 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6062 if closed
6063 then { v with f = pkeymap ret keymap }
6064 else
6065 let f () = v in
6066 { v with f = skip "map" f }
6068 | Vopen _ ->
6069 error "unexpected subelement in keymap" s spos
6071 | Vclose "keymap" ->
6072 { v with f = ret keymap }
6074 | Vclose _ -> error "unexpected close in keymap" s spos
6076 and pbookmarks path pan anchor c bookmarks v t spos _ =
6077 match t with
6078 | Vdata | Vcdata -> v
6079 | Vend -> error "unexpected end of input in bookmarks" s spos
6080 | Vopen ("item", attrs, closed) ->
6081 let titleent, spage, srely = bookmark_of attrs in
6082 let page = fromstring int_of_string spos "page" spage 0
6083 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6084 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6085 if closed
6086 then { v with f = pbookmarks path pan anchor c bookmarks }
6087 else
6088 let f () = v in
6089 { v with f = skip "item" f }
6091 | Vopen _ ->
6092 error "unexpected subelement in bookmarks" s spos
6094 | Vclose "bookmarks" ->
6095 { v with f = doc path pan anchor c bookmarks }
6097 | Vclose _ -> error "unexpected close in bookmarks" s spos
6099 and skip tag f v t spos _ =
6100 match t with
6101 | Vdata | Vcdata -> v
6102 | Vend ->
6103 error ("unexpected end of input in skipped " ^ tag) s spos
6104 | Vopen (tag', _, closed) ->
6105 if closed
6106 then v
6107 else
6108 let f' () = { v with f = skip tag f } in
6109 { v with f = skip tag' f' }
6110 | Vclose ctag ->
6111 if tag = ctag
6112 then f ()
6113 else error ("unexpected close in skipped " ^ tag) s spos
6116 parse { f = toplevel; accu = () } s;
6117 h, dc;
6120 let do_load f ic =
6122 let len = in_channel_length ic in
6123 let s = String.create len in
6124 really_input ic s 0 len;
6125 f s;
6126 with
6127 | Parse_error (msg, s, pos) ->
6128 let subs = subs s pos in
6129 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6130 failwith ("parse error: " ^ s)
6132 | exn ->
6133 failwith ("config load error: " ^ Printexc.to_string exn)
6136 let defconfpath =
6137 let dir =
6139 let dir = Filename.concat home ".config" in
6140 if Sys.is_directory dir then dir else home
6141 with _ -> home
6143 Filename.concat dir "llpp.conf"
6146 let confpath = ref defconfpath;;
6148 let load1 f =
6149 if Sys.file_exists !confpath
6150 then
6151 match
6152 (try Some (open_in_bin !confpath)
6153 with exn ->
6154 prerr_endline
6155 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6156 Printexc.to_string exn);
6157 None
6159 with
6160 | Some ic ->
6161 begin try
6162 f (do_load get ic)
6163 with exn ->
6164 prerr_endline
6165 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6166 Printexc.to_string exn);
6167 end;
6168 close_in ic;
6170 | None -> ()
6171 else
6172 f (Hashtbl.create 0, defconf)
6175 let load () =
6176 let f (h, dc) =
6177 let pc, pb, px, pa =
6179 Hashtbl.find h (Filename.basename state.path)
6180 with Not_found -> dc, [], 0, (0, 0.0)
6182 setconf defconf dc;
6183 setconf conf pc;
6184 state.bookmarks <- pb;
6185 state.x <- px;
6186 state.scrollw <- conf.scrollbw;
6187 if conf.jumpback
6188 then state.anchor <- pa;
6189 cbput state.hists.nav pa;
6191 load1 f
6194 let add_attrs bb always dc c =
6195 let ob s a b =
6196 if always || a != b
6197 then Printf.bprintf bb "\n %s='%b'" s a
6198 and oi s a b =
6199 if always || a != b
6200 then Printf.bprintf bb "\n %s='%d'" s a
6201 and oI s a b =
6202 if always || a != b
6203 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6204 and oz s a b =
6205 if always || a <> b
6206 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6207 and oF s a b =
6208 if always || a <> b
6209 then Printf.bprintf bb "\n %s='%f'" s a
6210 and oc s a b =
6211 if always || a <> b
6212 then
6213 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6214 and oC s a b =
6215 if always || a <> b
6216 then
6217 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6218 and oR s a b =
6219 if always || a <> b
6220 then
6221 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6222 and os s a b =
6223 if always || a <> b
6224 then
6225 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6226 and og s a b =
6227 if always || a <> b
6228 then
6229 match a with
6230 | None -> ()
6231 | Some (_N, _A, _B) ->
6232 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6233 and oW s a b =
6234 if always || a <> b
6235 then
6236 let v =
6237 match a with
6238 | None -> "false"
6239 | Some f ->
6240 if f = infinity
6241 then "true"
6242 else string_of_float f
6244 Printf.bprintf bb "\n %s='%s'" s v
6245 and oco s a b =
6246 if always || a <> b
6247 then
6248 match a with
6249 | Cmulti ((n, a, b), _) when n > 1 ->
6250 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6251 | Csplit (n, _) when n > 1 ->
6252 Printf.bprintf bb "\n %s='%d'" s ~-n
6253 | _ -> ()
6254 and obeco s a b =
6255 if always || a <> b
6256 then
6257 match a with
6258 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6259 | _ -> ()
6261 let w, h =
6262 if always
6263 then dc.winw, dc.winh
6264 else
6265 match state.fullscreen with
6266 | Some wh -> wh
6267 | None -> c.winw, c.winh
6269 let zoom, presentation, interpagespace, maxwait =
6270 if always
6271 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6272 else
6273 match state.mode with
6274 | Birdseye (bc, _, _, _, _) ->
6275 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6276 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6278 oi "width" w dc.winw;
6279 oi "height" h dc.winh;
6280 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6281 oi "scroll-handle-height" c.scrollh dc.scrollh;
6282 ob "case-insensitive-search" c.icase dc.icase;
6283 ob "preload" c.preload dc.preload;
6284 oi "page-bias" c.pagebias dc.pagebias;
6285 oi "scroll-step" c.scrollstep dc.scrollstep;
6286 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6287 ob "max-height-fit" c.maxhfit dc.maxhfit;
6288 ob "crop-hack" c.crophack dc.crophack;
6289 oW "throttle" maxwait dc.maxwait;
6290 ob "highlight-links" c.hlinks dc.hlinks;
6291 ob "under-cursor-info" c.underinfo dc.underinfo;
6292 oi "vertical-margin" interpagespace dc.interpagespace;
6293 oz "zoom" zoom dc.zoom;
6294 ob "presentation" presentation dc.presentation;
6295 oi "rotation-angle" c.angle dc.angle;
6296 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6297 ob "proportional-display" c.proportional dc.proportional;
6298 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6299 oi "tex-count" c.texcount dc.texcount;
6300 oi "slice-height" c.sliceheight dc.sliceheight;
6301 oi "thumbnail-width" c.thumbw dc.thumbw;
6302 ob "persistent-location" c.jumpback dc.jumpback;
6303 oc "background-color" c.bgcolor dc.bgcolor;
6304 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6305 oi "tile-width" c.tilew dc.tilew;
6306 oi "tile-height" c.tileh dc.tileh;
6307 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6308 ob "checkers" c.checkers dc.checkers;
6309 oi "aalevel" c.aalevel dc.aalevel;
6310 ob "trim-margins" c.trimmargins dc.trimmargins;
6311 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6312 os "uri-launcher" c.urilauncher dc.urilauncher;
6313 os "path-launcher" c.pathlauncher dc.pathlauncher;
6314 oC "color-space" c.colorspace dc.colorspace;
6315 ob "invert-colors" c.invert dc.invert;
6316 oF "brightness" c.colorscale dc.colorscale;
6317 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6318 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6319 oco "columns" c.columns dc.columns;
6320 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6321 os "selection-command" c.selcmd dc.selcmd;
6322 ob "update-cursor" c.updatecurs dc.updatecurs;
6325 let keymapsbuf always dc c =
6326 let bb = Buffer.create 16 in
6327 let rec loop = function
6328 | [] -> ()
6329 | (modename, h) :: rest ->
6330 let dh = findkeyhash dc modename in
6331 if always || h <> dh
6332 then (
6333 if Hashtbl.length h > 0
6334 then (
6335 if Buffer.length bb > 0
6336 then Buffer.add_char bb '\n';
6337 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6338 Hashtbl.iter (fun i o ->
6339 let isdifferent = always ||
6341 let dO = Hashtbl.find dh i in
6342 dO <> o
6343 with Not_found -> true
6345 if isdifferent
6346 then
6347 let addkm (k, m) =
6348 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6349 if Wsi.withalt m then Buffer.add_string bb "alt-";
6350 if Wsi.withshift m then Buffer.add_string bb "shift-";
6351 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6352 Buffer.add_string bb (Wsi.keyname k);
6354 let addkms l =
6355 let rec loop = function
6356 | [] -> ()
6357 | km :: [] -> addkm km
6358 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6360 loop l
6362 Buffer.add_string bb "<map in='";
6363 addkm i;
6364 match o with
6365 | KMinsrt km ->
6366 Buffer.add_string bb "' out='";
6367 addkm km;
6368 Buffer.add_string bb "'/>\n"
6370 | KMinsrl kms ->
6371 Buffer.add_string bb "' out='";
6372 addkms kms;
6373 Buffer.add_string bb "'/>\n"
6375 | KMmulti (ins, kms) ->
6376 Buffer.add_char bb ' ';
6377 addkms ins;
6378 Buffer.add_string bb "' out='";
6379 addkms kms;
6380 Buffer.add_string bb "'/>\n"
6381 ) h;
6382 Buffer.add_string bb "</keymap>";
6385 loop rest
6387 loop c.keyhashes;
6391 let save () =
6392 let uifontsize = fstate.fontsize in
6393 let bb = Buffer.create 32768 in
6394 let f (h, dc) =
6395 let dc = if conf.bedefault then conf else dc in
6396 Buffer.add_string bb "<llppconfig>\n";
6398 if String.length !fontpath > 0
6399 then
6400 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6401 uifontsize
6402 !fontpath
6403 else (
6404 if uifontsize <> 14
6405 then
6406 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6409 Buffer.add_string bb "<defaults ";
6410 add_attrs bb true dc dc;
6411 let kb = keymapsbuf true dc dc in
6412 if Buffer.length kb > 0
6413 then (
6414 Buffer.add_string bb ">\n";
6415 Buffer.add_buffer bb kb;
6416 Buffer.add_string bb "\n</defaults>\n";
6418 else Buffer.add_string bb "/>\n";
6420 let adddoc path pan anchor c bookmarks =
6421 if bookmarks == [] && c = dc && anchor = emptyanchor
6422 then ()
6423 else (
6424 Printf.bprintf bb "<doc path='%s'"
6425 (enent path 0 (String.length path));
6427 if anchor <> emptyanchor
6428 then (
6429 let n, y = anchor in
6430 Printf.bprintf bb " page='%d'" n;
6431 if y > 1e-6
6432 then
6433 Printf.bprintf bb " rely='%f'" y
6437 if pan != 0
6438 then Printf.bprintf bb " pan='%d'" pan;
6440 add_attrs bb false dc c;
6441 let kb = keymapsbuf false dc c in
6443 begin match bookmarks with
6444 | [] ->
6445 if Buffer.length kb > 0
6446 then (
6447 Buffer.add_string bb ">\n";
6448 Buffer.add_buffer bb kb;
6449 Buffer.add_string bb "</doc>\n";
6451 else Buffer.add_string bb "/>\n"
6452 | _ ->
6453 Buffer.add_string bb ">\n<bookmarks>\n";
6454 List.iter (fun (title, _level, (page, rely)) ->
6455 Printf.bprintf bb
6456 "<item title='%s' page='%d'"
6457 (enent title 0 (String.length title))
6458 page
6460 if rely > 1e-6
6461 then
6462 Printf.bprintf bb " rely='%f'" rely
6464 Buffer.add_string bb "/>\n";
6465 ) bookmarks;
6466 Buffer.add_string bb "</bookmarks>";
6467 if Buffer.length kb > 0
6468 then (
6469 Buffer.add_string bb "\n";
6470 Buffer.add_buffer bb kb;
6472 Buffer.add_string bb "\n</doc>\n";
6473 end;
6477 let pan, conf =
6478 match state.mode with
6479 | Birdseye (c, pan, _, _, _) ->
6480 let beyecolumns =
6481 match conf.columns with
6482 | Cmulti ((c, _, _), _) -> Some c
6483 | Csingle -> None
6484 | Csplit _ -> None
6485 and columns =
6486 match c.columns with
6487 | Cmulti (c, _) -> Cmulti (c, [||])
6488 | Csingle -> Csingle
6489 | Csplit _ -> failwith "quit from bird's eye while split"
6491 pan, { c with beyecolumns = beyecolumns; columns = columns }
6492 | _ -> state.x, conf
6494 let basename = Filename.basename state.path in
6495 adddoc basename pan (getanchor ())
6496 { conf with
6497 autoscrollstep =
6498 match state.autoscroll with
6499 | Some step -> step
6500 | None -> conf.autoscrollstep }
6501 (if conf.savebmarks then state.bookmarks else []);
6503 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6504 if basename <> path
6505 then adddoc path x y c bookmarks
6506 ) h;
6507 Buffer.add_string bb "</llppconfig>";
6509 load1 f;
6510 if Buffer.length bb > 0
6511 then
6513 let tmp = !confpath ^ ".tmp" in
6514 let oc = open_out_bin tmp in
6515 Buffer.output_buffer oc bb;
6516 close_out oc;
6517 Unix.rename tmp !confpath;
6518 with exn ->
6519 prerr_endline
6520 ("error while saving configuration: " ^ Printexc.to_string exn)
6522 end;;
6524 let () =
6525 Arg.parse
6526 (Arg.align
6527 [("-p", Arg.String (fun s -> state.password <- s) ,
6528 "<password> Set password");
6530 ("-f", Arg.String (fun s -> Config.fontpath := s),
6531 "<path> Set path to the user interface font");
6533 ("-c", Arg.String (fun s -> Config.confpath := s),
6534 "<path> Set path to the configuration file");
6536 ("-v", Arg.Unit (fun () ->
6537 Printf.printf
6538 "%s\nconfiguration path: %s\n"
6539 (version ())
6540 Config.defconfpath
6542 exit 0), " Print version and exit");
6545 (fun s -> state.path <- s)
6546 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6548 if String.length state.path = 0
6549 then (prerr_endline "file name missing"; exit 1);
6551 Config.load ();
6553 let globalkeyhash = findkeyhash conf "global" in
6554 let wsfd, winw, winh = Wsi.init (object
6555 method expose =
6556 if nogeomcmds state.geomcmds
6557 then display ()
6558 method display = display ()
6559 method reshape w h = reshape w h
6560 method mouse b d x y m = mouse b d x y m
6561 method motion x y = state.mpos <- (x, y); motion x y
6562 method pmotion x y = state.mpos <- (x, y); pmotion x y
6563 method key k m =
6564 let mascm = m land (
6565 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6566 ) in
6567 match state.keystate with
6568 | KSnone ->
6569 let km = k, mascm in
6570 begin
6571 match
6572 try Hashtbl.find globalkeyhash km
6573 with Not_found ->
6574 let modehash = state.uioh#modehash in
6575 try Hashtbl.find modehash km
6576 with Not_found -> KMinsrt (k, m)
6577 with
6578 | KMinsrt (k, m) -> keyboard k m
6579 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6580 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6582 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6583 List.iter (fun (k, m) -> keyboard k m) insrt;
6584 state.keystate <- KSnone
6585 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6586 state.keystate <- KSinto (keys, insrt)
6587 | _ ->
6588 state.keystate <- KSnone
6590 method enter x y = state.mpos <- (x, y); pmotion x y
6591 method leave = state.mpos <- (-1, -1)
6592 method quit = raise Quit
6593 end) conf.winw conf.winh (platform = Posx) in
6595 state.wsfd <- wsfd;
6597 if not (
6598 List.exists GlMisc.check_extension
6599 [ "GL_ARB_texture_rectangle"
6600 ; "GL_EXT_texture_recangle"
6601 ; "GL_NV_texture_rectangle" ]
6603 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6605 let cr, sw = Unix.pipe ()
6606 and sr, cw = Unix.pipe () in
6608 cloexec cr;
6609 cloexec sw;
6610 cloexec sr;
6611 cloexec cw;
6613 setcheckers conf.checkers;
6614 redirectstderr ();
6616 init (cr, cw) (
6617 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6618 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6619 !Config.fontpath
6621 state.sr <- sr;
6622 state.sw <- sw;
6623 state.text <- "Opening " ^ state.path;
6624 reshape winw winh;
6625 opendoc state.path state.password;
6626 state.uioh <- uioh;
6628 let rec loop deadline =
6629 let r =
6630 match state.errfd with
6631 | None -> [state.sr; state.wsfd]
6632 | Some fd -> [state.sr; state.wsfd; fd]
6634 if state.redisplay
6635 then (
6636 state.redisplay <- false;
6637 display ();
6639 let timeout =
6640 let now = now () in
6641 if deadline > now
6642 then (
6643 if deadline = infinity
6644 then ~-.1.0
6645 else max 0.0 (deadline -. now)
6647 else 0.0
6649 let r, _, _ =
6650 try Unix.select r [] [] timeout
6651 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6653 begin match r with
6654 | [] ->
6655 state.ghyll None;
6656 let newdeadline =
6657 if state.ghyll == noghyll
6658 then
6659 match state.autoscroll with
6660 | Some step when step != 0 ->
6661 let y = state.y + step in
6662 let y =
6663 if y < 0
6664 then state.maxy
6665 else if y >= state.maxy then 0 else y
6667 gotoy y;
6668 if state.mode = View
6669 then state.text <- "";
6670 deadline +. 0.01
6671 | _ -> infinity
6672 else deadline +. 0.01
6674 loop newdeadline
6676 | l ->
6677 let rec checkfds = function
6678 | [] -> ()
6679 | fd :: rest when fd = state.sr ->
6680 let cmd = readcmd state.sr in
6681 act cmd;
6682 checkfds rest
6684 | fd :: rest when fd = state.wsfd ->
6685 Wsi.readresp fd;
6686 checkfds rest
6688 | fd :: rest ->
6689 let s = String.create 80 in
6690 let n = Unix.read fd s 0 80 in
6691 if conf.redirectstderr
6692 then (
6693 Buffer.add_substring state.errmsgs s 0 n;
6694 state.newerrmsgs <- true;
6695 state.redisplay <- true;
6697 else (
6698 prerr_string (String.sub s 0 n);
6699 flush stderr;
6701 checkfds rest
6703 checkfds l;
6704 let newdeadline =
6705 let deadline1 =
6706 if deadline = infinity
6707 then now () +. 0.01
6708 else deadline
6710 match state.autoscroll with
6711 | Some step when step != 0 -> deadline1
6712 | _ -> if state.ghyll == noghyll then infinity else deadline1
6714 loop newdeadline
6715 end;
6718 loop infinity;
6719 with Quit ->
6720 Config.save ();
6721 exit 0;