Use home grown popen pervasively
[llpp.git] / main.ml
blob273106b5f345bee18c19111aeddd6175d032e38a
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 type x = int
115 and y = int
116 and tilex = int
117 and tiley = int
118 and tileparams = (x * y * width * height * tilex * tiley)
121 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
123 type mpos = int * int
124 and mstate =
125 | Msel of (mpos * mpos)
126 | Mpan of mpos
127 | Mscrolly | Mscrollx
128 | Mzoom of (int * int)
129 | Mzoomrect of (mpos * mpos)
130 | Mnone
133 type textentry = string * string * onhist option * onkey * ondone
134 and onkey = string -> int -> te
135 and ondone = string -> unit
136 and histcancel = unit -> unit
137 and onhist = ((histcmd -> string) * histcancel)
138 and histcmd = HCnext | HCprev | HCfirst | HClast
139 and te =
140 | TEstop
141 | TEdone of string
142 | TEcont of string
143 | TEswitch of textentry
146 type 'a circbuf =
147 { store : 'a array
148 ; mutable rc : int
149 ; mutable wc : int
150 ; mutable len : int
154 let bound v minv maxv =
155 max minv (min maxv v);
158 let cbnew n v =
159 { store = Array.create n v
160 ; rc = 0
161 ; wc = 0
162 ; len = 0
166 let drawstring size x y s =
167 Gl.enable `blend;
168 Gl.enable `texture_2d;
169 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
170 ignore (drawstr size x y s);
171 Gl.disable `blend;
172 Gl.disable `texture_2d;
175 let drawstring1 size x y s =
176 drawstr size x y s;
179 let drawstring2 size x y fmt =
180 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
183 let cbcap b = Array.length b.store;;
185 let cbput b v =
186 let cap = cbcap b in
187 b.store.(b.wc) <- v;
188 b.wc <- (b.wc + 1) mod cap;
189 b.rc <- b.wc;
190 b.len <- min (b.len + 1) cap;
193 let cbempty b = b.len = 0;;
195 let cbgetg b circular dir =
196 if cbempty b
197 then b.store.(0)
198 else
199 let rc = b.rc + dir in
200 let rc =
201 if circular
202 then (
203 if rc = -1
204 then b.len-1
205 else (
206 if rc = b.len
207 then 0
208 else rc
211 else max 0 (min rc (b.len-1))
213 b.rc <- rc;
214 b.store.(rc);
217 let cbget b = cbgetg b false;;
218 let cbgetc b = cbgetg b true;;
220 type page =
221 { pageno : int
222 ; pagedimno : int
223 ; pagew : int
224 ; pageh : int
225 ; pagex : int
226 ; pagey : int
227 ; pagevw : int
228 ; pagevh : int
229 ; pagedispx : int
230 ; pagedispy : int
234 let debugl l =
235 dolog "l %d dim=%d {" l.pageno l.pagedimno;
236 dolog " WxH %dx%d" l.pagew l.pageh;
237 dolog " vWxH %dx%d" l.pagevw l.pagevh;
238 dolog " pagex,y %d,%d" l.pagex l.pagey;
239 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
240 dolog "}";
243 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
244 dolog "rect {";
245 dolog " x0,y0=(% f, % f)" x0 y0;
246 dolog " x1,y1=(% f, % f)" x1 y1;
247 dolog " x2,y2=(% f, % f)" x2 y2;
248 dolog " x3,y3=(% f, % f)" x3 y3;
249 dolog "}";
252 type multicolumns = multicol * pagegeom
253 and splitcolumns = columncount * pagegeom
254 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
255 and multicol = columncount * covercount * covercount
256 and pdimno = int
257 and columncount = int
258 and covercount = int;;
260 type conf =
261 { mutable scrollbw : int
262 ; mutable scrollh : int
263 ; mutable icase : bool
264 ; mutable preload : bool
265 ; mutable pagebias : int
266 ; mutable verbose : bool
267 ; mutable debug : bool
268 ; mutable scrollstep : int
269 ; mutable maxhfit : bool
270 ; mutable crophack : bool
271 ; mutable autoscrollstep : int
272 ; mutable maxwait : float option
273 ; mutable hlinks : bool
274 ; mutable underinfo : bool
275 ; mutable interpagespace : interpagespace
276 ; mutable zoom : float
277 ; mutable presentation : bool
278 ; mutable angle : angle
279 ; mutable winw : int
280 ; mutable winh : int
281 ; mutable savebmarks : bool
282 ; mutable proportional : proportional
283 ; mutable trimmargins : trimmargins
284 ; mutable trimfuzz : irect
285 ; mutable memlimit : memsize
286 ; mutable texcount : texcount
287 ; mutable sliceheight : sliceheight
288 ; mutable thumbw : width
289 ; mutable jumpback : bool
290 ; mutable bgcolor : float * float * float
291 ; mutable bedefault : bool
292 ; mutable scrollbarinpm : bool
293 ; mutable tilew : int
294 ; mutable tileh : int
295 ; mutable mustoresize : memsize
296 ; mutable checkers : bool
297 ; mutable aalevel : int
298 ; mutable urilauncher : string
299 ; mutable pathlauncher : string
300 ; mutable colorspace : colorspace
301 ; mutable invert : bool
302 ; mutable colorscale : float
303 ; mutable redirectstderr : bool
304 ; mutable ghyllscroll : (int * int * int) option
305 ; mutable columns : columns
306 ; mutable beyecolumns : columncount option
307 ; mutable selcmd : string
308 ; mutable updatecurs : bool
309 ; mutable keyhashes : (string * keyhash) list
311 and columns =
312 | Csingle
313 | Cmulti of multicolumns
314 | Csplit of splitcolumns
317 type anchor = pageno * top;;
319 type outline = string * int * anchor;;
321 type rect = float * float * float * float * float * float * float * float;;
323 type tile = opaque * pixmapsize * elapsed
324 and elapsed = float;;
325 type pagemapkey = pageno * gen;;
326 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
327 and row = int
328 and col = int;;
330 let emptyanchor = (0, 0.0);;
332 type infochange = | Memused | Docinfo | Pdim;;
334 class type uioh = object
335 method display : unit
336 method key : int -> int -> uioh
337 method button : int -> bool -> int -> int -> int -> uioh
338 method motion : int -> int -> uioh
339 method pmotion : int -> int -> uioh
340 method infochanged : infochange -> unit
341 method scrollpw : (int * float * float)
342 method scrollph : (int * float * float)
343 method modehash : keyhash
344 end;;
346 type mode =
347 | Birdseye of (conf * leftx * pageno * pageno * anchor)
348 | Textentry of (textentry * onleave)
349 | View
350 | LinkNav of linktarget
351 and onleave = leavetextentrystatus -> unit
352 and leavetextentrystatus = | Cancel | Confirm
353 and helpitem = string * int * action
354 and action =
355 | Noaction
356 | Action of (uioh -> uioh)
357 and linktarget =
358 | Ltexact of (pageno * int)
359 | Ltgendir of int
362 let isbirdseye = function Birdseye _ -> true | _ -> false;;
363 let istextentry = function Textentry _ -> true | _ -> false;;
365 type currently =
366 | Idle
367 | Loading of (page * gen)
368 | Tiling of (
369 page * opaque * colorspace * angle * gen * col * row * width * height
371 | Outlining of outline list
374 let emptykeyhash = Hashtbl.create 0;;
375 let nouioh : uioh = object (self)
376 method display = ()
377 method key _ _ = self
378 method button _ _ _ _ _ = self
379 method motion _ _ = self
380 method pmotion _ _ = self
381 method infochanged _ = ()
382 method scrollpw = (0, nan, nan)
383 method scrollph = (0, nan, nan)
384 method modehash = emptykeyhash
385 end;;
387 type state =
388 { mutable sr : Unix.file_descr
389 ; mutable sw : Unix.file_descr
390 ; mutable wsfd : Unix.file_descr
391 ; mutable errfd : Unix.file_descr option
392 ; mutable stderr : Unix.file_descr
393 ; mutable errmsgs : Buffer.t
394 ; mutable newerrmsgs : bool
395 ; mutable w : int
396 ; mutable x : int
397 ; mutable y : int
398 ; mutable scrollw : int
399 ; mutable hscrollh : int
400 ; mutable anchor : anchor
401 ; mutable ranchors : (string * string * anchor) list
402 ; mutable maxy : int
403 ; mutable layout : page list
404 ; pagemap : (pagemapkey, opaque) Hashtbl.t
405 ; tilemap : (tilemapkey, tile) Hashtbl.t
406 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
407 ; mutable pdims : (pageno * width * height * leftx) list
408 ; mutable pagecount : int
409 ; mutable currently : currently
410 ; mutable mstate : mstate
411 ; mutable searchpattern : string
412 ; mutable rects : (pageno * recttype * rect) list
413 ; mutable rects1 : (pageno * recttype * rect) list
414 ; mutable text : string
415 ; mutable fullscreen : (width * height) option
416 ; mutable mode : mode
417 ; mutable uioh : uioh
418 ; mutable outlines : outline array
419 ; mutable bookmarks : outline list
420 ; mutable path : string
421 ; mutable password : string
422 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
423 ; mutable memused : memsize
424 ; mutable gen : gen
425 ; mutable throttle : (page list * int * float) option
426 ; mutable autoscroll : int option
427 ; mutable ghyll : (int option -> unit)
428 ; mutable help : helpitem array
429 ; mutable docinfo : (int * string) list
430 ; mutable texid : GlTex.texture_id option
431 ; hists : hists
432 ; mutable prevzoom : float
433 ; mutable progress : float
434 ; mutable redisplay : bool
435 ; mutable mpos : mpos
436 ; mutable keystate : keystate
437 ; mutable glinks : bool
439 and hists =
440 { pat : string circbuf
441 ; pag : string circbuf
442 ; nav : anchor circbuf
443 ; sel : string circbuf
447 let defconf =
448 { scrollbw = 7
449 ; scrollh = 12
450 ; icase = true
451 ; preload = true
452 ; pagebias = 0
453 ; verbose = false
454 ; debug = false
455 ; scrollstep = 24
456 ; maxhfit = true
457 ; crophack = false
458 ; autoscrollstep = 2
459 ; maxwait = None
460 ; hlinks = false
461 ; underinfo = false
462 ; interpagespace = 2
463 ; zoom = 1.0
464 ; presentation = false
465 ; angle = 0
466 ; winw = 900
467 ; winh = 900
468 ; savebmarks = true
469 ; proportional = true
470 ; trimmargins = false
471 ; trimfuzz = (0,0,0,0)
472 ; memlimit = 32 lsl 20
473 ; texcount = 256
474 ; sliceheight = 24
475 ; thumbw = 76
476 ; jumpback = true
477 ; bgcolor = (0.5, 0.5, 0.5)
478 ; bedefault = false
479 ; scrollbarinpm = true
480 ; tilew = 2048
481 ; tileh = 2048
482 ; mustoresize = 256 lsl 20
483 ; checkers = true
484 ; aalevel = 8
485 ; urilauncher =
486 (match platform with
487 | Plinux | Pfreebsd | Pdragonflybsd
488 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
489 | Posx -> "open \"%s\""
490 | Pcygwin -> "cygstart \"%s\""
491 | Punknown -> "echo %s")
492 ; pathlauncher = "lp \"%s\""
493 ; selcmd =
494 (match platform with
495 | Plinux | Pfreebsd | Pdragonflybsd
496 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
497 | Posx -> "pbcopy"
498 | Pcygwin -> "wsel"
499 | Punknown -> "cat")
500 ; colorspace = Rgb
501 ; invert = false
502 ; colorscale = 1.0
503 ; redirectstderr = false
504 ; ghyllscroll = None
505 ; columns = Csingle
506 ; beyecolumns = None
507 ; updatecurs = false
508 ; keyhashes =
509 let mk n = (n, Hashtbl.create 1) in
510 [ mk "global"
511 ; mk "info"
512 ; mk "help"
513 ; mk "outline"
514 ; mk "listview"
515 ; mk "birdseye"
516 ; mk "textentry"
517 ; mk "links"
522 let findkeyhash c name =
523 try List.assoc name c.keyhashes
524 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
527 let conf = { defconf with angle = defconf.angle };;
529 type fontstate =
530 { mutable fontsize : int
531 ; mutable wwidth : float
532 ; mutable maxrows : int
536 let fstate =
537 { fontsize = 14
538 ; wwidth = nan
539 ; maxrows = -1
543 let setfontsize n =
544 fstate.fontsize <- n;
545 fstate.wwidth <- measurestr fstate.fontsize "w";
546 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
549 let geturl s =
550 let colonpos = try String.index s ':' with Not_found -> -1 in
551 let len = String.length s in
552 if colonpos >= 0 && colonpos + 3 < len
553 then (
554 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
555 then
556 let schemestartpos =
557 try String.rindex_from s colonpos ' '
558 with Not_found -> -1
560 let scheme =
561 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
563 match scheme with
564 | "http" | "ftp" | "mailto" ->
565 let epos =
566 try String.index_from s colonpos ' '
567 with Not_found -> len
569 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
570 | _ -> ""
571 else ""
573 else ""
576 let gotouri uri =
577 if String.length conf.urilauncher = 0
578 then print_endline uri
579 else (
580 let url = geturl uri in
581 if String.length url = 0
582 then print_endline uri
583 else
584 let re = Str.regexp "%s" in
585 let command = Str.global_replace re url conf.urilauncher in
586 try popen command []
587 with exn ->
588 Printf.eprintf
589 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
590 flush stderr;
594 let version () =
595 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
596 (platform_to_string platform) Sys.word_size Sys.ocaml_version
599 let makehelp () =
600 let strings = version () :: "" :: Help.keys in
601 Array.of_list (
602 List.map (fun s ->
603 let url = geturl s in
604 if String.length url > 0
605 then (s, 0, Action (fun u -> gotouri url; u))
606 else (s, 0, Noaction)
607 ) strings);
610 let noghyll _ = ();;
611 let firstgeomcmds = "", [];;
613 let state =
614 { sr = Unix.stdin
615 ; sw = Unix.stdin
616 ; wsfd = Unix.stdin
617 ; errfd = None
618 ; stderr = Unix.stderr
619 ; errmsgs = Buffer.create 0
620 ; newerrmsgs = false
621 ; x = 0
622 ; y = 0
623 ; w = 0
624 ; scrollw = 0
625 ; hscrollh = 0
626 ; anchor = emptyanchor
627 ; ranchors = []
628 ; layout = []
629 ; maxy = max_int
630 ; tilelru = Queue.create ()
631 ; pagemap = Hashtbl.create 10
632 ; tilemap = Hashtbl.create 10
633 ; pdims = []
634 ; pagecount = 0
635 ; currently = Idle
636 ; mstate = Mnone
637 ; rects = []
638 ; rects1 = []
639 ; text = ""
640 ; mode = View
641 ; fullscreen = None
642 ; searchpattern = ""
643 ; outlines = [||]
644 ; bookmarks = []
645 ; path = ""
646 ; password = ""
647 ; geomcmds = firstgeomcmds
648 ; hists =
649 { nav = cbnew 10 (0, 0.0)
650 ; pat = cbnew 10 ""
651 ; pag = cbnew 10 ""
652 ; sel = cbnew 10 ""
654 ; memused = 0
655 ; gen = 0
656 ; throttle = None
657 ; autoscroll = None
658 ; ghyll = noghyll
659 ; help = makehelp ()
660 ; docinfo = []
661 ; texid = None
662 ; prevzoom = 1.0
663 ; progress = -1.0
664 ; uioh = nouioh
665 ; redisplay = true
666 ; mpos = (-1, -1)
667 ; keystate = KSnone
668 ; glinks = false
672 let vlog fmt =
673 if conf.verbose
674 then
675 Printf.kprintf prerr_endline fmt
676 else
677 Printf.kprintf ignore fmt
680 let launchpath () =
681 if String.length conf.pathlauncher = 0
682 then print_endline state.path
683 else (
684 let re = Str.regexp "%s" in
685 let command = Str.global_replace re state.path conf.pathlauncher in
686 try popen command []
687 with exn ->
688 Printf.eprintf
689 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
690 flush stderr;
694 let redirectstderr () =
695 if conf.redirectstderr
696 then
697 let rfd, wfd = Unix.pipe () in
698 state.stderr <- Unix.dup Unix.stderr;
699 state.errfd <- Some rfd;
700 Unix.dup2 wfd Unix.stderr;
701 else (
702 state.newerrmsgs <- false;
703 begin match state.errfd with
704 | Some fd ->
705 Unix.close fd;
706 Unix.dup2 state.stderr Unix.stderr;
707 state.errfd <- None;
708 | None -> ()
709 end;
710 prerr_string (Buffer.contents state.errmsgs);
711 flush stderr;
712 Buffer.clear state.errmsgs;
716 module G =
717 struct
718 let postRedisplay who =
719 if conf.verbose
720 then prerr_endline ("redisplay for " ^ who);
721 state.redisplay <- true;
723 end;;
725 let getopaque pageno =
726 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
727 with Not_found -> None
730 let putopaque pageno opaque =
731 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
734 let pagetranslatepoint l x y =
735 let dy = y - l.pagedispy in
736 let y = dy + l.pagey in
737 let dx = x - l.pagedispx in
738 let x = dx + l.pagex in
739 (x, y);
742 let getunder x y =
743 let rec f = function
744 | l :: rest ->
745 begin match getopaque l.pageno with
746 | Some opaque ->
747 let x0 = l.pagedispx in
748 let x1 = x0 + l.pagevw in
749 let y0 = l.pagedispy in
750 let y1 = y0 + l.pagevh in
751 if y >= y0 && y <= y1 && x >= x0 && x <= x1
752 then
753 let px, py = pagetranslatepoint l x y in
754 match whatsunder opaque px py with
755 | Unone -> f rest
756 | under -> under
757 else f rest
758 | _ ->
759 f rest
761 | [] -> Unone
763 f state.layout
766 let showtext c s =
767 state.text <- Printf.sprintf "%c%s" c s;
768 G.postRedisplay "showtext";
771 let updateunder x y =
772 match getunder x y with
773 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
774 | Ulinkuri uri ->
775 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
776 Wsi.setcursor Wsi.CURSOR_INFO
777 | Ulinkgoto (page, _) ->
778 if conf.underinfo
779 then showtext 'p' ("age: " ^ string_of_int (page+1));
780 Wsi.setcursor Wsi.CURSOR_INFO
781 | Utext s ->
782 if conf.underinfo then showtext 'f' ("ont: " ^ s);
783 Wsi.setcursor Wsi.CURSOR_TEXT
784 | Uunexpected s ->
785 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
786 Wsi.setcursor Wsi.CURSOR_INHERIT
787 | Ulaunch s ->
788 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
789 Wsi.setcursor Wsi.CURSOR_INHERIT
790 | Unamed s ->
791 if conf.underinfo then showtext 'n' ("amed: " ^ s);
792 Wsi.setcursor Wsi.CURSOR_INHERIT
793 | Uremote (filename, pageno) ->
794 if conf.underinfo then showtext 'r'
795 (Printf.sprintf "emote: %s (%d)" filename pageno);
796 Wsi.setcursor Wsi.CURSOR_INFO
799 let showlinktype under =
800 if conf.underinfo
801 then
802 match under with
803 | Unone -> ()
804 | Ulinkuri uri ->
805 showtext 'u' ("ri: " ^ uri)
806 | Ulinkgoto (page, _) ->
807 showtext 'p' ("age: " ^ string_of_int (page+1));
808 | Utext s ->
809 showtext 'f' ("ont: " ^ s);
810 | Uunexpected s ->
811 showtext 'u' ("nexpected: " ^ s);
812 | Ulaunch s ->
813 showtext 'l' ("aunch: " ^ s);
814 | Unamed s ->
815 showtext 'n' ("amed: " ^ s);
816 | Uremote (filename, pageno) ->
817 showtext 'r' (Printf.sprintf "emote: %s (%d)" filename pageno);
820 let addchar s c =
821 let b = Buffer.create (String.length s + 1) in
822 Buffer.add_string b s;
823 Buffer.add_char b c;
824 Buffer.contents b;
827 let colorspace_of_string s =
828 match String.lowercase s with
829 | "rgb" -> Rgb
830 | "bgr" -> Bgr
831 | "gray" -> Gray
832 | _ -> failwith "invalid colorspace"
835 let int_of_colorspace = function
836 | Rgb -> 0
837 | Bgr -> 1
838 | Gray -> 2
841 let colorspace_of_int = function
842 | 0 -> Rgb
843 | 1 -> Bgr
844 | 2 -> Gray
845 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
848 let colorspace_to_string = function
849 | Rgb -> "rgb"
850 | Bgr -> "bgr"
851 | Gray -> "gray"
854 let intentry_with_suffix text key =
855 let c =
856 if key >= 32 && key < 127
857 then Char.chr key
858 else '\000'
860 match Char.lowercase c with
861 | '0' .. '9' ->
862 let text = addchar text c in
863 TEcont text
865 | 'k' | 'm' | 'g' ->
866 let text = addchar text c in
867 TEcont text
869 | _ ->
870 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
871 TEcont text
874 let multicolumns_to_string (n, a, b) =
875 if a = 0 && b = 0
876 then Printf.sprintf "%d" n
877 else Printf.sprintf "%d,%d,%d" n a b;
880 let multicolumns_of_string s =
882 (int_of_string s, 0, 0)
883 with _ ->
884 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
887 let readcmd fd =
888 let s = "xxxx" in
889 let n = Unix.read fd s 0 4 in
890 if n != 4 then failwith "incomplete read(len)";
891 let len = 0
892 lor (Char.code s.[0] lsl 24)
893 lor (Char.code s.[1] lsl 16)
894 lor (Char.code s.[2] lsl 8)
895 lor (Char.code s.[3] lsl 0)
897 let s = String.create len in
898 let n = Unix.read fd s 0 len in
899 if n != len then failwith "incomplete read(data)";
903 let btod b = if b then 1 else 0;;
905 let wcmd fmt =
906 let b = Buffer.create 16 in
907 Buffer.add_string b "llll";
908 Printf.kbprintf
909 (fun b ->
910 let s = Buffer.contents b in
911 let n = String.length s in
912 let len = n - 4 in
913 (* dolog "wcmd %S" (String.sub s 4 len); *)
914 s.[0] <- Char.chr ((len lsr 24) land 0xff);
915 s.[1] <- Char.chr ((len lsr 16) land 0xff);
916 s.[2] <- Char.chr ((len lsr 8) land 0xff);
917 s.[3] <- Char.chr (len land 0xff);
918 let n' = Unix.write state.sw s 0 n in
919 if n' != n then failwith "write failed";
920 ) b fmt;
923 let calcips h =
924 if conf.presentation
925 then
926 let d = conf.winh - h in
927 max 0 ((d + 1) / 2)
928 else
929 conf.interpagespace
932 let calcheight () =
933 let rec f pn ph pi fh l =
934 match l with
935 | (n, _, h, _) :: rest ->
936 let ips = calcips h in
937 let fh =
938 if conf.presentation
939 then fh+ips
940 else (
941 if isbirdseye state.mode && pn = 0
942 then fh + ips
943 else fh
946 let fh = fh + ((n - pn) * (ph + pi)) in
947 f n h ips fh rest;
949 | [] ->
950 let inc =
951 if conf.presentation || (isbirdseye state.mode && pn = 0)
952 then 0
953 else -pi
955 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
956 max 0 fh
958 let fh = f 0 0 0 0 state.pdims in
962 let calcheight () =
963 match conf.columns with
964 | Csingle -> calcheight ()
965 | Cmulti (_, b) ->
966 if Array.length b > 0
967 then
968 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
969 y + h
970 else 0
971 | Csplit (_, b) ->
972 if Array.length b > 0
973 then
974 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
975 y + h
976 else 0
979 let getpageyh pageno =
980 let rec f pn ph pi y l =
981 match l with
982 | (n, _, h, _) :: rest ->
983 let ips = calcips h in
984 if n >= pageno
985 then
986 let h = if n = pageno then h else ph in
987 if conf.presentation && n = pageno
988 then
989 y + (pageno - pn) * (ph + pi) + pi, h
990 else
991 y + (pageno - pn) * (ph + pi), h
992 else
993 let y = y + (if conf.presentation then pi else 0) in
994 let y = y + (n - pn) * (ph + pi) in
995 f n h ips y rest
997 | [] ->
998 y + (pageno - pn) * (ph + pi), ph
1000 f 0 0 0 0 state.pdims
1003 let getpageyh pageno =
1004 match conf.columns with
1005 | Csingle -> getpageyh pageno
1006 | Cmulti (_, b) ->
1007 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1008 y, h
1009 | Csplit (c, b) ->
1010 let n = pageno*c in
1011 let (_, _, y, (_, _, h, _)) = b.(n) in
1012 y, h
1015 let getpagedim pageno =
1016 let rec f ppdim l =
1017 match l with
1018 | (n, _, _, _) as pdim :: rest ->
1019 if n >= pageno
1020 then (if n = pageno then pdim else ppdim)
1021 else f pdim rest
1023 | [] -> ppdim
1025 f (-1, -1, -1, -1) state.pdims
1028 let getpagey pageno = fst (getpageyh pageno);;
1030 let nogeomcmds cmds =
1031 match cmds with
1032 | s, [] -> String.length s = 0
1033 | _ -> false
1036 let layout1 y sh =
1037 let sh = sh - state.hscrollh in
1038 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1039 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1040 match pdims with
1041 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1042 let ips = calcips h in
1043 let yinc =
1044 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1045 then ips
1046 else 0
1048 (w, h, ips, xoff), rest, pdimno + 1, yinc
1049 | _ ->
1050 prev, pdims, pdimno, 0
1052 let dy = dy + yinc in
1053 let py = py + yinc in
1054 if pageno = state.pagecount || dy >= sh
1055 then
1056 accu
1057 else
1058 let vy = y + dy in
1059 if py + h <= vy - yinc
1060 then
1061 let py = py + h + ips in
1062 let dy = max 0 (py - y) in
1063 f ~pageno:(pageno+1)
1064 ~pdimno
1065 ~prev:curr
1068 ~pdims:rest
1069 ~accu
1070 else
1071 let pagey = vy - py in
1072 let pagevh = h - pagey in
1073 let pagevh = min (sh - dy) pagevh in
1074 let off = if yinc > 0 then py - vy else 0 in
1075 let py = py + h + ips in
1076 let pagex, dx =
1077 let xoff = xoff +
1078 if state.w < conf.winw - state.scrollw
1079 then (conf.winw - state.scrollw - state.w) / 2
1080 else 0
1082 let dispx = xoff + state.x in
1083 if dispx < 0
1084 then (-dispx, 0)
1085 else (0, dispx)
1087 let pagevw =
1088 let lw = w - pagex in
1089 min lw (conf.winw - state.scrollw)
1091 let e =
1092 { pageno = pageno
1093 ; pagedimno = pdimno
1094 ; pagew = w
1095 ; pageh = h
1096 ; pagex = pagex
1097 ; pagey = pagey + off
1098 ; pagevw = pagevw
1099 ; pagevh = pagevh - off
1100 ; pagedispx = dx
1101 ; pagedispy = dy + off
1104 let accu = e :: accu in
1105 f ~pageno:(pageno+1)
1106 ~pdimno
1107 ~prev:curr
1109 ~dy:(dy+pagevh+ips)
1110 ~pdims:rest
1111 ~accu
1113 let accu =
1115 ~pageno:0
1116 ~pdimno:~-1
1117 ~prev:(0,0,0,0)
1118 ~py:0
1119 ~dy:0
1120 ~pdims:state.pdims
1121 ~accu:[]
1123 List.rev accu
1126 let layoutN ((columns, coverA, coverB), b) y sh =
1127 let sh = sh - state.hscrollh in
1128 let rec fold accu n =
1129 if n = Array.length b
1130 then accu
1131 else
1132 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1133 if (vy - y) > sh &&
1134 (n = coverA - 1
1135 || n = state.pagecount - coverB
1136 || (n - coverA) mod columns = columns - 1)
1137 then accu
1138 else
1139 let accu =
1140 if vy + h > y
1141 then
1142 let pagey = max 0 (y - vy) in
1143 let pagedispy = if pagey > 0 then 0 else vy - y in
1144 let pagedispx, pagex =
1145 let pdx =
1146 if n = coverA - 1 || n = state.pagecount - coverB
1147 then state.x + (conf.winw - state.scrollw - w) / 2
1148 else dx + xoff + state.x
1150 if pdx < 0
1151 then 0, -pdx
1152 else pdx, 0
1154 let pagevw =
1155 let vw = conf.winw - state.scrollw - pagedispx in
1156 let pw = w - pagex in
1157 min vw pw
1159 let pagevh = min (h - pagey) (sh - pagedispy) in
1160 if pagevw > 0 && pagevh > 0
1161 then
1162 let e =
1163 { pageno = n
1164 ; pagedimno = pdimno
1165 ; pagew = w
1166 ; pageh = h
1167 ; pagex = pagex
1168 ; pagey = pagey
1169 ; pagevw = pagevw
1170 ; pagevh = pagevh
1171 ; pagedispx = pagedispx
1172 ; pagedispy = pagedispy
1175 e :: accu
1176 else
1177 accu
1178 else
1179 accu
1181 fold accu (n+1)
1183 List.rev (fold [] 0)
1186 let layoutS (columns, b) y sh =
1187 let sh = sh - state.hscrollh in
1188 let rec fold accu n =
1189 if n = Array.length b
1190 then accu
1191 else
1192 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1193 if (vy - y) > sh
1194 then accu
1195 else
1196 let accu =
1197 if vy + pageh > y
1198 then
1199 let x = xoff + state.x in
1200 let pagey = max 0 (y - vy) in
1201 let pagedispy = if pagey > 0 then 0 else vy - y in
1202 let pagedispx, pagex =
1203 if px = 0
1204 then (
1205 if x < 0
1206 then 0, -x
1207 else x, 0
1209 else (
1210 let px = px - x in
1211 if px < 0
1212 then -px, 0
1213 else 0, px
1216 let pagevw =
1217 let vw = conf.winw - pagedispx - state.scrollw in
1218 let pw = pagew - pagex in
1219 min vw pw
1221 let pagevw = min pagevw (pagew/columns) in
1222 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1223 if pagevw > 0 && pagevh > 0
1224 then
1225 let e =
1226 { pageno = n/columns
1227 ; pagedimno = pdimno
1228 ; pagew = pagew
1229 ; pageh = pageh
1230 ; pagex = pagex
1231 ; pagey = pagey
1232 ; pagevw = pagevw
1233 ; pagevh = pagevh
1234 ; pagedispx = pagedispx
1235 ; pagedispy = pagedispy
1238 e :: accu
1239 else
1240 accu
1241 else
1242 accu
1244 fold accu (n+1)
1246 List.rev (fold [] 0)
1249 let layout y sh =
1250 if nogeomcmds state.geomcmds
1251 then
1252 match conf.columns with
1253 | Csingle -> layout1 y sh
1254 | Cmulti c -> layoutN c y sh
1255 | Csplit s -> layoutS s y sh
1256 else []
1259 let clamp incr =
1260 let y = state.y + incr in
1261 let y = max 0 y in
1262 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1266 let itertiles l f =
1267 let tilex = l.pagex mod conf.tilew in
1268 let tiley = l.pagey mod conf.tileh in
1270 let col = l.pagex / conf.tilew in
1271 let row = l.pagey / conf.tileh in
1273 let rec rowloop row y0 dispy h =
1274 if h = 0
1275 then ()
1276 else (
1277 let dh = conf.tileh - y0 in
1278 let dh = min h dh in
1279 let rec colloop col x0 dispx w =
1280 if w = 0
1281 then ()
1282 else (
1283 let dw = conf.tilew - x0 in
1284 let dw = min w dw in
1286 f col row dispx dispy x0 y0 dw dh;
1287 colloop (col+1) 0 (dispx+dw) (w-dw)
1290 colloop col tilex l.pagedispx l.pagevw;
1291 rowloop (row+1) 0 (dispy+dh) (h-dh)
1294 if l.pagevw > 0 && l.pagevh > 0
1295 then rowloop row tiley l.pagedispy l.pagevh;
1298 let gettileopaque l col row =
1299 let key =
1300 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1302 try Some (Hashtbl.find state.tilemap key)
1303 with Not_found -> None
1306 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1307 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1308 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1311 let drawtiles l color =
1312 GlDraw.color color;
1313 let f col row x y tilex tiley w h =
1314 match gettileopaque l col row with
1315 | Some (opaque, _, t) ->
1316 let params = x, y, w, h, tilex, tiley in
1317 if conf.invert
1318 then (
1319 Gl.enable `blend;
1320 GlFunc.blend_func `zero `one_minus_src_color;
1322 drawtile params opaque;
1323 if conf.invert
1324 then Gl.disable `blend;
1325 if conf.debug
1326 then (
1327 let s = Printf.sprintf
1328 "%d[%d,%d] %f sec"
1329 l.pageno col row t
1331 let w = measurestr fstate.fontsize s in
1332 GlMisc.push_attrib [`current];
1333 GlDraw.color (0.0, 0.0, 0.0);
1334 GlDraw.rect
1335 (float (x-2), float (y-2))
1336 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1337 GlDraw.color (1.0, 1.0, 1.0);
1338 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1339 GlMisc.pop_attrib ();
1342 | _ ->
1343 let w =
1344 let lw = conf.winw - state.scrollw - x in
1345 min lw w
1346 and h =
1347 let lh = conf.winh - y in
1348 min lh h
1350 Gl.enable `texture_2d;
1351 begin match state.texid with
1352 | Some id ->
1353 GlTex.bind_texture `texture_2d id;
1354 let x0 = float x
1355 and y0 = float y
1356 and x1 = float (x+w)
1357 and y1 = float (y+h) in
1359 let tw = float w /. 64.0
1360 and th = float h /. 64.0 in
1361 let tx0 = float tilex /. 64.0
1362 and ty0 = float tiley /. 64.0 in
1363 let tx1 = tx0 +. tw
1364 and ty1 = ty0 +. th in
1365 GlDraw.begins `quads;
1366 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1367 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1368 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1369 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1370 GlDraw.ends ();
1372 Gl.disable `texture_2d;
1373 | None ->
1374 GlDraw.color (1.0, 1.0, 1.0);
1375 GlDraw.rect
1376 (float x, float y)
1377 (float (x+w), float (y+h));
1378 end;
1379 if w > 128 && h > fstate.fontsize + 10
1380 then (
1381 GlDraw.color (0.0, 0.0, 0.0);
1382 let c, r =
1383 if conf.verbose
1384 then (col*conf.tilew, row*conf.tileh)
1385 else col, row
1387 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1389 GlDraw.color color;
1391 itertiles l f
1394 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1396 let tilevisible1 l x y =
1397 let ax0 = l.pagex
1398 and ax1 = l.pagex + l.pagevw
1399 and ay0 = l.pagey
1400 and ay1 = l.pagey + l.pagevh in
1402 let bx0 = x
1403 and by0 = y in
1404 let bx1 = min (bx0 + conf.tilew) l.pagew
1405 and by1 = min (by0 + conf.tileh) l.pageh in
1407 let rx0 = max ax0 bx0
1408 and ry0 = max ay0 by0
1409 and rx1 = min ax1 bx1
1410 and ry1 = min ay1 by1 in
1412 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1413 nonemptyintersection
1416 let tilevisible layout n x y =
1417 let rec findpageinlayout m = function
1418 | l :: rest when l.pageno = n ->
1419 tilevisible1 l x y || (
1420 match conf.columns with
1421 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1422 | _ -> false
1424 | _ :: rest -> findpageinlayout 0 rest
1425 | [] -> false
1427 findpageinlayout 0 layout;
1430 let tileready l x y =
1431 tilevisible1 l x y &&
1432 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1435 let tilepage n p layout =
1436 let rec loop = function
1437 | l :: rest ->
1438 if l.pageno = n
1439 then
1440 let f col row _ _ _ _ _ _ =
1441 if state.currently = Idle
1442 then
1443 match gettileopaque l col row with
1444 | Some _ -> ()
1445 | None ->
1446 let x = col*conf.tilew
1447 and y = row*conf.tileh in
1448 let w =
1449 let w = l.pagew - x in
1450 min w conf.tilew
1452 let h =
1453 let h = l.pageh - y in
1454 min h conf.tileh
1456 wcmd "tile %s %d %d %d %d" p x y w h;
1457 state.currently <-
1458 Tiling (
1459 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1460 conf.tilew, conf.tileh
1463 itertiles l f;
1464 else
1465 loop rest
1467 | [] -> ()
1469 if nogeomcmds state.geomcmds
1470 then loop layout;
1473 let preloadlayout visiblepages =
1474 let presentation = conf.presentation in
1475 let interpagespace = conf.interpagespace in
1476 let maxy = state.maxy in
1477 conf.presentation <- false;
1478 conf.interpagespace <- 0;
1479 state.maxy <- calcheight ();
1480 let y =
1481 match visiblepages with
1482 | [] -> 0
1483 | l :: _ -> getpagey l.pageno + l.pagey
1485 let y = if y < conf.winh then 0 else y - conf.winh in
1486 let h = state.y - y + conf.winh*3 in
1487 let pages = layout y h in
1488 conf.presentation <- presentation;
1489 conf.interpagespace <- interpagespace;
1490 state.maxy <- maxy;
1491 pages;
1494 let load pages =
1495 let rec loop pages =
1496 if state.currently != Idle
1497 then ()
1498 else
1499 match pages with
1500 | l :: rest ->
1501 begin match getopaque l.pageno with
1502 | None ->
1503 wcmd "page %d %d" l.pageno l.pagedimno;
1504 state.currently <- Loading (l, state.gen);
1505 | Some opaque ->
1506 tilepage l.pageno opaque pages;
1507 loop rest
1508 end;
1509 | _ -> ()
1511 if nogeomcmds state.geomcmds
1512 then loop pages
1515 let preload pages =
1516 load pages;
1517 if conf.preload && state.currently = Idle
1518 then load (preloadlayout pages);
1521 let layoutready layout =
1522 let rec fold all ls =
1523 all && match ls with
1524 | l :: rest ->
1525 let seen = ref false in
1526 let allvisible = ref true in
1527 let foo col row _ _ _ _ _ _ =
1528 seen := true;
1529 allvisible := !allvisible &&
1530 begin match gettileopaque l col row with
1531 | Some _ -> true
1532 | None -> false
1535 itertiles l foo;
1536 fold (!seen && !allvisible) rest
1537 | [] -> true
1539 let alltilesvisible = fold true layout in
1540 alltilesvisible;
1543 let gotoy y =
1544 let y = bound y 0 state.maxy in
1545 let y, layout, proceed =
1546 match conf.maxwait with
1547 | Some time when state.ghyll == noghyll ->
1548 begin match state.throttle with
1549 | None ->
1550 let layout = layout y conf.winh in
1551 let ready = layoutready layout in
1552 if not ready
1553 then (
1554 load layout;
1555 state.throttle <- Some (layout, y, now ());
1557 else G.postRedisplay "gotoy showall (None)";
1558 y, layout, ready
1559 | Some (_, _, started) ->
1560 let dt = now () -. started in
1561 if dt > time
1562 then (
1563 state.throttle <- None;
1564 let layout = layout y conf.winh in
1565 load layout;
1566 G.postRedisplay "maxwait";
1567 y, layout, true
1569 else -1, [], false
1572 | _ ->
1573 let layout = layout y conf.winh in
1574 if true || layoutready layout
1575 then G.postRedisplay "gotoy ready";
1576 y, layout, true
1578 if proceed
1579 then (
1580 state.y <- y;
1581 state.layout <- layout;
1582 begin match state.mode with
1583 | LinkNav (Ltexact (pageno, linkno)) ->
1584 let rec loop = function
1585 | [] ->
1586 state.mode <- LinkNav (Ltgendir 0)
1587 | l :: _ when l.pageno = pageno ->
1588 begin match getopaque pageno with
1589 | None ->
1590 state.mode <- LinkNav (Ltgendir 0)
1591 | Some opaque ->
1592 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1593 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1594 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1595 then state.mode <- LinkNav (Ltgendir 0)
1597 | _ :: rest -> loop rest
1599 loop layout
1600 | _ -> ()
1601 end;
1602 begin match state.mode with
1603 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1604 if not (pagevisible layout pageno)
1605 then (
1606 match state.layout with
1607 | [] -> ()
1608 | l :: _ ->
1609 state.mode <- Birdseye (
1610 conf, leftx, l.pageno, hooverpageno, anchor
1613 | LinkNav (Ltgendir dir as lt) ->
1614 let linknav =
1615 let rec loop = function
1616 | [] -> lt
1617 | l :: rest ->
1618 match getopaque l.pageno with
1619 | None -> loop rest
1620 | Some opaque ->
1621 let link =
1622 let ld =
1623 if dir = 0
1624 then LDfirstvisible (l.pagex, l.pagey, dir)
1625 else (
1626 if dir > 0 then LDfirst else LDlast
1629 findlink opaque ld
1631 match link with
1632 | Lnotfound -> loop rest
1633 | Lfound n ->
1634 showlinktype (getlink opaque n);
1635 Ltexact (l.pageno, n)
1637 loop state.layout
1639 state.mode <- LinkNav linknav
1640 | _ -> ()
1641 end;
1642 preload layout;
1644 state.ghyll <- noghyll;
1645 if conf.updatecurs
1646 then (
1647 let mx, my = state.mpos in
1648 updateunder mx my;
1652 let conttiling pageno opaque =
1653 tilepage pageno opaque
1654 (if conf.preload then preloadlayout state.layout else state.layout)
1657 let gotoy_and_clear_text y =
1658 if not conf.verbose then state.text <- "";
1659 gotoy y;
1662 let getanchor () =
1663 match state.layout with
1664 | [] -> emptyanchor
1665 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1668 let getanchory (n, top) =
1669 let y, h = getpageyh n in
1670 y + (truncate (top *. float h));
1673 let gotoanchor anchor =
1674 gotoy (getanchory anchor);
1677 let addnav () =
1678 cbput state.hists.nav (getanchor ());
1681 let getnav dir =
1682 let anchor = cbgetc state.hists.nav dir in
1683 getanchory anchor;
1686 let gotoghyll y =
1687 let rec scroll f n a b =
1688 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1689 let snake f a b =
1690 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1691 if f < a
1692 then s (float f /. float a)
1693 else (
1694 if f > b
1695 then 1.0 -. s ((float (f-b) /. float (n-b)))
1696 else 1.0
1699 snake f a b
1700 and summa f n a b =
1701 (* courtesy:
1702 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1703 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1704 let iv1 = iv f in
1705 let ins = float a *. iv1
1706 and outs = float (n-b) *. iv1 in
1707 let ones = b - a in
1708 ins +. outs +. float ones
1710 let rec set (_N, _A, _B) y sy =
1711 let sum = summa 1.0 _N _A _B in
1712 let dy = float (y - sy) in
1713 state.ghyll <- (
1714 let rec gf n y1 o =
1715 if n >= _N
1716 then state.ghyll <- noghyll
1717 else
1718 let go n =
1719 let s = scroll n _N _A _B in
1720 let y1 = y1 +. ((s *. dy) /. sum) in
1721 gotoy_and_clear_text (truncate y1);
1722 state.ghyll <- gf (n+1) y1;
1724 match o with
1725 | None -> go n
1726 | Some y' -> set (_N/2, 0, 0) y' state.y
1728 gf 0 (float state.y)
1731 match conf.ghyllscroll with
1732 | None ->
1733 gotoy_and_clear_text y
1734 | Some nab ->
1735 if state.ghyll == noghyll
1736 then set nab y state.y
1737 else state.ghyll (Some y)
1740 let gotopage n top =
1741 let y, h = getpageyh n in
1742 let y = y + (truncate (top *. float h)) in
1743 gotoghyll y
1746 let gotopage1 n top =
1747 let y = getpagey n in
1748 let y = y + top in
1749 gotoghyll y
1752 let invalidate s f =
1753 state.layout <- [];
1754 state.pdims <- [];
1755 state.rects <- [];
1756 state.rects1 <- [];
1757 match state.geomcmds with
1758 | ps, [] when String.length ps = 0 ->
1759 f ();
1760 state.geomcmds <- s, [];
1762 | ps, [] ->
1763 state.geomcmds <- ps, [s, f];
1765 | ps, (s', _) :: rest when s' = s ->
1766 state.geomcmds <- ps, ((s, f) :: rest);
1768 | ps, cmds ->
1769 state.geomcmds <- ps, ((s, f) :: cmds);
1772 let opendoc path password =
1773 state.path <- path;
1774 state.password <- password;
1775 state.gen <- state.gen + 1;
1776 state.docinfo <- [];
1778 setaalevel conf.aalevel;
1779 Wsi.settitle ("llpp " ^ Filename.basename path);
1780 wcmd "open %s\000%s\000" path password;
1781 invalidate "reqlayout"
1782 (fun () ->
1783 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1786 let scalecolor c =
1787 let c = c *. conf.colorscale in
1788 (c, c, c);
1791 let scalecolor2 (r, g, b) =
1792 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1795 let represent () =
1796 let docolumns = function
1797 | Csingle -> ()
1799 | Cmulti ((columns, coverA, coverB), _) ->
1800 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1801 let rec loop pageno pdimno pdim x y rowh pdims =
1802 if pageno = state.pagecount
1803 then ()
1804 else
1805 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1806 match pdims with
1807 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1808 pdimno+1, pdim, rest
1809 | _ ->
1810 pdimno, pdim, pdims
1812 let x, y, rowh' =
1813 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1814 then (
1815 (conf.winw - state.scrollw - w) / 2,
1816 y + rowh + conf.interpagespace, h
1818 else (
1819 if (pageno - coverA) mod columns = 0
1820 then 0, y + rowh + conf.interpagespace, h
1821 else x, y, max rowh h
1824 let rec fixrow m = if m = pageno then () else
1825 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1826 if h < rowh
1827 then (
1828 let y = y + (rowh - h) / 2 in
1829 a.(m) <- (pdimno, x, y, pdim);
1831 fixrow (m+1)
1833 if pageno > 1 && (pageno - coverA) mod columns = 0
1834 then fixrow (pageno - columns);
1835 a.(pageno) <- (pdimno, x, y, pdim);
1836 let x = x + w + xoff*2 + conf.interpagespace in
1837 loop (pageno+1) pdimno pdim x y rowh' pdims
1839 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1840 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1842 | Csplit (c, _) ->
1843 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1844 let rec loop pageno pdimno pdim y pdims =
1845 if pageno = state.pagecount
1846 then ()
1847 else
1848 let pdimno, ((_, w, h, _) as pdim), pdims =
1849 match pdims with
1850 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1851 pdimno+1, pdim, rest
1852 | _ ->
1853 pdimno, pdim, pdims
1855 let cw = w / c in
1856 let rec loop1 n x y =
1857 if n = c then y else (
1858 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1859 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1862 let y = loop1 0 0 y in
1863 loop (pageno+1) pdimno pdim y pdims
1865 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1866 conf.columns <- Csplit (c, a);
1868 docolumns conf.columns;
1869 state.maxy <- calcheight ();
1870 state.hscrollh <-
1871 if state.w <= conf.winw - state.scrollw
1872 then 0
1873 else state.scrollw
1875 match state.mode with
1876 | Birdseye (_, _, pageno, _, _) ->
1877 let y, h = getpageyh pageno in
1878 let top = (conf.winh - h) / 2 in
1879 gotoy (max 0 (y - top))
1880 | _ -> gotoanchor state.anchor
1883 let reshape w h =
1884 GlDraw.viewport 0 0 w h;
1885 let firsttime = state.geomcmds == firstgeomcmds in
1886 if not firsttime && nogeomcmds state.geomcmds
1887 then state.anchor <- getanchor ();
1889 conf.winw <- w;
1890 let w = truncate (float w *. conf.zoom) - state.scrollw in
1891 let w = max w 2 in
1892 conf.winh <- h;
1893 setfontsize fstate.fontsize;
1894 GlMat.mode `modelview;
1895 GlMat.load_identity ();
1897 GlMat.mode `projection;
1898 GlMat.load_identity ();
1899 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1900 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1901 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1903 let relx =
1904 if conf.zoom <= 1.0
1905 then 0.0
1906 else float state.x /. float state.w
1908 invalidate "geometry"
1909 (fun () ->
1910 state.w <- w;
1911 if not firsttime
1912 then state.x <- truncate (relx *. float w);
1913 let w =
1914 match conf.columns with
1915 | Csingle -> w
1916 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1917 | Csplit (c, _) -> w * c
1919 wcmd "geometry %d %d" w h);
1922 let enttext () =
1923 let len = String.length state.text in
1924 let drawstring s =
1925 let hscrollh =
1926 match state.mode with
1927 | Textentry _
1928 | View -> state.hscrollh
1929 | _ -> 0
1931 let rect x w =
1932 GlDraw.rect
1933 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1934 (x+.w, float (conf.winh - hscrollh))
1937 let w = float (conf.winw - state.scrollw - 1) in
1938 if state.progress >= 0.0 && state.progress < 1.0
1939 then (
1940 GlDraw.color (0.3, 0.3, 0.3);
1941 let w1 = w *. state.progress in
1942 rect 0.0 w1;
1943 GlDraw.color (0.0, 0.0, 0.0);
1944 rect w1 (w-.w1)
1946 else (
1947 GlDraw.color (0.0, 0.0, 0.0);
1948 rect 0.0 w;
1951 GlDraw.color (1.0, 1.0, 1.0);
1952 drawstring fstate.fontsize
1953 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1955 let s =
1956 match state.mode with
1957 | Textentry ((prefix, text, _, _, _), _) ->
1958 let s =
1959 if len > 0
1960 then
1961 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1962 else
1963 Printf.sprintf "%s%s_" prefix text
1967 | _ -> state.text
1969 let s =
1970 if state.newerrmsgs
1971 then (
1972 if not (istextentry state.mode)
1973 then
1974 let s1 = "(press 'e' to review error messasges)" in
1975 if String.length s > 0 then s ^ " " ^ s1 else s1
1976 else s
1978 else s
1980 if String.length s > 0
1981 then drawstring s
1984 let gctiles () =
1985 let len = Queue.length state.tilelru in
1986 let rec loop qpos =
1987 if state.memused <= conf.memlimit
1988 then ()
1989 else (
1990 if qpos < len
1991 then
1992 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1993 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1994 let (_, pw, ph, _) = getpagedim n in
1996 gen = state.gen
1997 && colorspace = conf.colorspace
1998 && angle = conf.angle
1999 && pagew = pw
2000 && pageh = ph
2001 && (
2002 let layout =
2003 match state.throttle with
2004 | None ->
2005 if conf.preload
2006 then preloadlayout state.layout
2007 else state.layout
2008 | Some (layout, _, _) ->
2009 layout
2011 let x = col*conf.tilew
2012 and y = row*conf.tileh in
2013 tilevisible layout n x y
2015 then Queue.push lruitem state.tilelru
2016 else (
2017 wcmd "freetile %s" p;
2018 state.memused <- state.memused - s;
2019 state.uioh#infochanged Memused;
2020 Hashtbl.remove state.tilemap k;
2022 loop (qpos+1)
2025 loop 0
2028 let flushtiles () =
2029 Queue.iter (fun (k, p, s) ->
2030 wcmd "freetile %s" p;
2031 state.memused <- state.memused - s;
2032 state.uioh#infochanged Memused;
2033 Hashtbl.remove state.tilemap k;
2034 ) state.tilelru;
2035 Queue.clear state.tilelru;
2036 load state.layout;
2039 let logcurrently = function
2040 | Idle -> dolog "Idle"
2041 | Loading (l, gen) ->
2042 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2043 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2044 dolog
2045 "Tiling %d[%d,%d] page=%s cs=%s angle"
2046 l.pageno col row pageopaque
2047 (colorspace_to_string colorspace)
2049 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2050 angle gen conf.angle state.gen
2051 tilew tileh
2052 conf.tilew conf.tileh
2054 | Outlining _ ->
2055 dolog "outlining"
2058 let act cmds =
2059 (* dolog "%S" cmds; *)
2060 let op, args =
2061 let spacepos =
2062 try String.index cmds ' '
2063 with Not_found -> -1
2065 if spacepos = -1
2066 then cmds, ""
2067 else
2068 let l = String.length cmds in
2069 let op = String.sub cmds 0 spacepos in
2070 op, begin
2071 if l - spacepos < 2 then ""
2072 else String.sub cmds (spacepos+1) (l-spacepos-1)
2075 match op with
2076 | "clear" ->
2077 state.uioh#infochanged Pdim;
2078 state.pdims <- [];
2080 | "clearrects" ->
2081 state.rects <- state.rects1;
2082 G.postRedisplay "clearrects";
2084 | "continue" ->
2085 let n =
2086 try Scanf.sscanf args "%u" (fun n -> n)
2087 with exn ->
2088 dolog "error processing 'continue' %S: %s"
2089 cmds (Printexc.to_string exn);
2090 exit 1;
2092 state.pagecount <- n;
2093 begin match state.currently with
2094 | Outlining l ->
2095 state.currently <- Idle;
2096 state.outlines <- Array.of_list (List.rev l)
2097 | _ -> ()
2098 end;
2100 let cur, cmds = state.geomcmds in
2101 if String.length cur = 0
2102 then failwith "umpossible";
2104 begin match List.rev cmds with
2105 | [] ->
2106 state.geomcmds <- "", [];
2107 represent ();
2108 | (s, f) :: rest ->
2109 f ();
2110 state.geomcmds <- s, List.rev rest;
2111 end;
2112 if conf.maxwait = None
2113 then G.postRedisplay "continue";
2115 | "title" ->
2116 Wsi.settitle args
2118 | "msg" ->
2119 showtext ' ' args
2121 | "vmsg" ->
2122 if conf.verbose
2123 then showtext ' ' args
2125 | "progress" ->
2126 let progress, text =
2128 Scanf.sscanf args "%f %n"
2129 (fun f pos ->
2130 f, String.sub args pos (String.length args - pos))
2131 with exn ->
2132 dolog "error processing 'progress' %S: %s"
2133 cmds (Printexc.to_string exn);
2134 exit 1;
2136 state.text <- text;
2137 state.progress <- progress;
2138 G.postRedisplay "progress"
2140 | "firstmatch" ->
2141 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2143 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2144 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2145 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2146 with exn ->
2147 dolog "error processing 'firstmatch' %S: %s"
2148 cmds (Printexc.to_string exn);
2149 exit 1;
2151 let y = (getpagey pageno) + truncate y0 in
2152 addnav ();
2153 gotoy y;
2154 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2156 | "match" ->
2157 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2159 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2160 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2161 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2162 with exn ->
2163 dolog "error processing 'match' %S: %s"
2164 cmds (Printexc.to_string exn);
2165 exit 1;
2167 state.rects1 <-
2168 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2170 | "page" ->
2171 let pageopaque, t =
2173 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2174 with exn ->
2175 dolog "error processing 'page' %S: %s"
2176 cmds (Printexc.to_string exn);
2177 exit 1;
2179 begin match state.currently with
2180 | Loading (l, gen) ->
2181 vlog "page %d took %f sec" l.pageno t;
2182 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2183 begin match state.throttle with
2184 | None ->
2185 let preloadedpages =
2186 if conf.preload
2187 then preloadlayout state.layout
2188 else state.layout
2190 let evict () =
2191 let module IntSet =
2192 Set.Make (struct type t = int let compare = (-) end) in
2193 let set =
2194 List.fold_left (fun s l -> IntSet.add l.pageno s)
2195 IntSet.empty preloadedpages
2197 let evictedpages =
2198 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2199 if not (IntSet.mem pageno set)
2200 then (
2201 wcmd "freepage %s" opaque;
2202 key :: accu
2204 else accu
2205 ) state.pagemap []
2207 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2209 evict ();
2210 state.currently <- Idle;
2211 if gen = state.gen
2212 then (
2213 tilepage l.pageno pageopaque state.layout;
2214 load state.layout;
2215 load preloadedpages;
2216 if pagevisible state.layout l.pageno
2217 && layoutready state.layout
2218 then G.postRedisplay "page";
2221 | Some (layout, _, _) ->
2222 state.currently <- Idle;
2223 tilepage l.pageno pageopaque layout;
2224 load state.layout
2225 end;
2227 | _ ->
2228 dolog "Inconsistent loading state";
2229 logcurrently state.currently;
2230 exit 1
2233 | "tile" ->
2234 let (x, y, opaque, size, t) =
2236 Scanf.sscanf args "%u %u %s %u %f"
2237 (fun x y p size t -> (x, y, p, size, t))
2238 with exn ->
2239 dolog "error processing 'tile' %S: %s"
2240 cmds (Printexc.to_string exn);
2241 exit 1;
2243 begin match state.currently with
2244 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2245 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2247 if tilew != conf.tilew || tileh != conf.tileh
2248 then (
2249 wcmd "freetile %s" opaque;
2250 state.currently <- Idle;
2251 load state.layout;
2253 else (
2254 puttileopaque l col row gen cs angle opaque size t;
2255 state.memused <- state.memused + size;
2256 state.uioh#infochanged Memused;
2257 gctiles ();
2258 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2259 opaque, size) state.tilelru;
2261 let layout =
2262 match state.throttle with
2263 | None -> state.layout
2264 | Some (layout, _, _) -> layout
2267 state.currently <- Idle;
2268 if gen = state.gen
2269 && conf.colorspace = cs
2270 && conf.angle = angle
2271 && tilevisible layout l.pageno x y
2272 then conttiling l.pageno pageopaque;
2274 begin match state.throttle with
2275 | None ->
2276 preload state.layout;
2277 if gen = state.gen
2278 && conf.colorspace = cs
2279 && conf.angle = angle
2280 && tilevisible state.layout l.pageno x y
2281 then G.postRedisplay "tile nothrottle";
2283 | Some (layout, y, _) ->
2284 let ready = layoutready layout in
2285 if ready
2286 then (
2287 state.y <- y;
2288 state.layout <- layout;
2289 state.throttle <- None;
2290 G.postRedisplay "throttle";
2292 else load layout;
2293 end;
2296 | _ ->
2297 dolog "Inconsistent tiling state";
2298 logcurrently state.currently;
2299 exit 1
2302 | "pdim" ->
2303 let pdim =
2305 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2306 with exn ->
2307 dolog "error processing 'pdim' %S: %s"
2308 cmds (Printexc.to_string exn);
2309 exit 1;
2311 state.uioh#infochanged Pdim;
2312 state.pdims <- pdim :: state.pdims
2314 | "o" ->
2315 let (l, n, t, h, pos) =
2317 Scanf.sscanf args "%u %u %d %u %n"
2318 (fun l n t h pos -> l, n, t, h, pos)
2319 with exn ->
2320 dolog "error processing 'o' %S: %s"
2321 cmds (Printexc.to_string exn);
2322 exit 1;
2324 let s = String.sub args pos (String.length args - pos) in
2325 let outline = (s, l, (n, float t /. float h)) in
2326 begin match state.currently with
2327 | Outlining outlines ->
2328 state.currently <- Outlining (outline :: outlines)
2329 | Idle ->
2330 state.currently <- Outlining [outline]
2331 | currently ->
2332 dolog "invalid outlining state";
2333 logcurrently currently
2336 | "info" ->
2337 state.docinfo <- (1, args) :: state.docinfo
2339 | "infoend" ->
2340 state.uioh#infochanged Docinfo;
2341 state.docinfo <- List.rev state.docinfo
2343 | _ ->
2344 dolog "unknown cmd `%S'" cmds
2347 let onhist cb =
2348 let rc = cb.rc in
2349 let action = function
2350 | HCprev -> cbget cb ~-1
2351 | HCnext -> cbget cb 1
2352 | HCfirst -> cbget cb ~-(cb.rc)
2353 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2354 and cancel () = cb.rc <- rc
2355 in (action, cancel)
2358 let search pattern forward =
2359 if String.length pattern > 0
2360 then
2361 let pn, py =
2362 match state.layout with
2363 | [] -> 0, 0
2364 | l :: _ ->
2365 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2367 wcmd "search %d %d %d %d,%s\000"
2368 (btod conf.icase) pn py (btod forward) pattern;
2371 let intentry text key =
2372 let c =
2373 if key >= 32 && key < 127
2374 then Char.chr key
2375 else '\000'
2377 match c with
2378 | '0' .. '9' ->
2379 let text = addchar text c in
2380 TEcont text
2382 | _ ->
2383 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2384 TEcont text
2387 let textentry text key =
2388 if key land 0xff00 = 0xff00
2389 then TEcont text
2390 else TEcont (text ^ Wsi.toutf8 key)
2393 let reqlayout angle proportional =
2394 match state.throttle with
2395 | None ->
2396 if nogeomcmds state.geomcmds
2397 then state.anchor <- getanchor ();
2398 conf.angle <- angle mod 360;
2399 if conf.angle != 0
2400 then (
2401 match state.mode with
2402 | LinkNav _ -> state.mode <- View
2403 | _ -> ()
2405 conf.proportional <- proportional;
2406 invalidate "reqlayout"
2407 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2408 | _ -> ()
2411 let settrim trimmargins trimfuzz =
2412 if nogeomcmds state.geomcmds
2413 then state.anchor <- getanchor ();
2414 conf.trimmargins <- trimmargins;
2415 conf.trimfuzz <- trimfuzz;
2416 let x0, y0, x1, y1 = trimfuzz in
2417 invalidate "settrim"
2418 (fun () ->
2419 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2420 Hashtbl.iter (fun _ opaque ->
2421 wcmd "freepage %s" opaque;
2422 ) state.pagemap;
2423 Hashtbl.clear state.pagemap;
2426 let setzoom zoom =
2427 match state.throttle with
2428 | None ->
2429 let zoom = max 0.01 zoom in
2430 if zoom <> conf.zoom
2431 then (
2432 state.prevzoom <- conf.zoom;
2433 conf.zoom <- zoom;
2434 reshape conf.winw conf.winh;
2435 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2438 | Some (layout, y, started) ->
2439 let time =
2440 match conf.maxwait with
2441 | None -> 0.0
2442 | Some t -> t
2444 let dt = now () -. started in
2445 if dt > time
2446 then (
2447 state.y <- y;
2448 load layout;
2452 let setcolumns mode columns coverA coverB =
2453 if columns < 0
2454 then (
2455 if isbirdseye mode
2456 then showtext '!' "split mode doesn't work in bird's eye"
2457 else (
2458 conf.columns <- Csplit (-columns, [||]);
2459 state.x <- 0;
2460 conf.zoom <- 1.0;
2463 else (
2464 if columns < 2
2465 then (
2466 conf.columns <- Csingle;
2467 state.x <- 0;
2468 setzoom 1.0;
2470 else (
2471 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2472 conf.zoom <- 1.0;
2475 reshape conf.winw conf.winh;
2478 let enterbirdseye () =
2479 let zoom = float conf.thumbw /. float conf.winw in
2480 let birdseyepageno =
2481 let cy = conf.winh / 2 in
2482 let fold = function
2483 | [] -> 0
2484 | l :: rest ->
2485 let rec fold best = function
2486 | [] -> best.pageno
2487 | l :: rest ->
2488 let d = cy - (l.pagedispy + l.pagevh/2)
2489 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2490 if abs d < abs dbest
2491 then fold l rest
2492 else best.pageno
2493 in fold l rest
2495 fold state.layout
2497 state.mode <- Birdseye (
2498 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2500 conf.zoom <- zoom;
2501 conf.presentation <- false;
2502 conf.interpagespace <- 10;
2503 conf.hlinks <- false;
2504 state.x <- 0;
2505 state.mstate <- Mnone;
2506 conf.maxwait <- None;
2507 conf.columns <- (
2508 match conf.beyecolumns with
2509 | Some c ->
2510 conf.zoom <- 1.0;
2511 Cmulti ((c, 0, 0), [||])
2512 | None -> Csingle
2514 Wsi.setcursor Wsi.CURSOR_INHERIT;
2515 if conf.verbose
2516 then
2517 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2518 (100.0*.zoom)
2519 else
2520 state.text <- ""
2522 reshape conf.winw conf.winh;
2525 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2526 state.mode <- View;
2527 conf.zoom <- c.zoom;
2528 conf.presentation <- c.presentation;
2529 conf.interpagespace <- c.interpagespace;
2530 conf.maxwait <- c.maxwait;
2531 conf.hlinks <- c.hlinks;
2532 conf.beyecolumns <- (
2533 match conf.columns with
2534 | Cmulti ((c, _, _), _) -> Some c
2535 | Csingle -> None
2536 | Csplit _ -> assert false
2538 conf.columns <- (
2539 match c.columns with
2540 | Cmulti (c, _) -> Cmulti (c, [||])
2541 | Csingle -> Csingle
2542 | Csplit _ -> failwith "leaving bird's eye split mode"
2544 state.x <- leftx;
2545 if conf.verbose
2546 then
2547 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2548 (100.0*.conf.zoom)
2550 reshape conf.winw conf.winh;
2551 state.anchor <- if goback then anchor else (pageno, 0.0);
2554 let togglebirdseye () =
2555 match state.mode with
2556 | Birdseye vals -> leavebirdseye vals true
2557 | View -> enterbirdseye ()
2558 | _ -> ()
2561 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2562 let pageno = max 0 (pageno - incr) in
2563 let rec loop = function
2564 | [] -> gotopage1 pageno 0
2565 | l :: _ when l.pageno = pageno ->
2566 if l.pagedispy >= 0 && l.pagey = 0
2567 then G.postRedisplay "upbirdseye"
2568 else gotopage1 pageno 0
2569 | _ :: rest -> loop rest
2571 loop state.layout;
2572 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2575 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2576 let pageno = min (state.pagecount - 1) (pageno + incr) in
2577 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2578 let rec loop = function
2579 | [] ->
2580 let y, h = getpageyh pageno in
2581 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2582 gotoy (clamp dy)
2583 | l :: _ when l.pageno = pageno ->
2584 if l.pagevh != l.pageh
2585 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2586 else G.postRedisplay "downbirdseye"
2587 | _ :: rest -> loop rest
2589 loop state.layout
2592 let optentry mode _ key =
2593 let btos b = if b then "on" else "off" in
2594 if key >= 32 && key < 127
2595 then
2596 let c = Char.chr key in
2597 match c with
2598 | 's' ->
2599 let ondone s =
2600 try conf.scrollstep <- int_of_string s with exc ->
2601 state.text <- Printf.sprintf "bad integer `%s': %s"
2602 s (Printexc.to_string exc)
2604 TEswitch ("scroll step: ", "", None, intentry, ondone)
2606 | 'A' ->
2607 let ondone s =
2609 conf.autoscrollstep <- int_of_string s;
2610 if state.autoscroll <> None
2611 then state.autoscroll <- Some conf.autoscrollstep
2612 with exc ->
2613 state.text <- Printf.sprintf "bad integer `%s': %s"
2614 s (Printexc.to_string exc)
2616 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2618 | 'C' ->
2619 let mode = state.mode in
2620 let ondone s =
2622 let n, a, b = multicolumns_of_string s in
2623 setcolumns mode n a b;
2624 with exc ->
2625 state.text <- Printf.sprintf "bad columns `%s': %s"
2626 s (Printexc.to_string exc)
2628 TEswitch ("columns: ", "", None, textentry, ondone)
2630 | 'Z' ->
2631 let ondone s =
2633 let zoom = float (int_of_string s) /. 100.0 in
2634 setzoom zoom
2635 with exc ->
2636 state.text <- Printf.sprintf "bad integer `%s': %s"
2637 s (Printexc.to_string exc)
2639 TEswitch ("zoom: ", "", None, intentry, ondone)
2641 | 't' ->
2642 let ondone s =
2644 conf.thumbw <- bound (int_of_string s) 2 4096;
2645 state.text <-
2646 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2647 begin match mode with
2648 | Birdseye beye ->
2649 leavebirdseye beye false;
2650 enterbirdseye ();
2651 | _ -> ();
2653 with exc ->
2654 state.text <- Printf.sprintf "bad integer `%s': %s"
2655 s (Printexc.to_string exc)
2657 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2659 | 'R' ->
2660 let ondone s =
2661 match try
2662 Some (int_of_string s)
2663 with exc ->
2664 state.text <- Printf.sprintf "bad integer `%s': %s"
2665 s (Printexc.to_string exc);
2666 None
2667 with
2668 | Some angle -> reqlayout angle conf.proportional
2669 | None -> ()
2671 TEswitch ("rotation: ", "", None, intentry, ondone)
2673 | 'i' ->
2674 conf.icase <- not conf.icase;
2675 TEdone ("case insensitive search " ^ (btos conf.icase))
2677 | 'p' ->
2678 conf.preload <- not conf.preload;
2679 gotoy state.y;
2680 TEdone ("preload " ^ (btos conf.preload))
2682 | 'v' ->
2683 conf.verbose <- not conf.verbose;
2684 TEdone ("verbose " ^ (btos conf.verbose))
2686 | 'd' ->
2687 conf.debug <- not conf.debug;
2688 TEdone ("debug " ^ (btos conf.debug))
2690 | 'h' ->
2691 conf.maxhfit <- not conf.maxhfit;
2692 state.maxy <-
2693 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2694 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2696 | 'c' ->
2697 conf.crophack <- not conf.crophack;
2698 TEdone ("crophack " ^ btos conf.crophack)
2700 | 'a' ->
2701 let s =
2702 match conf.maxwait with
2703 | None ->
2704 conf.maxwait <- Some infinity;
2705 "always wait for page to complete"
2706 | Some _ ->
2707 conf.maxwait <- None;
2708 "show placeholder if page is not ready"
2710 TEdone s
2712 | 'f' ->
2713 conf.underinfo <- not conf.underinfo;
2714 TEdone ("underinfo " ^ btos conf.underinfo)
2716 | 'P' ->
2717 conf.savebmarks <- not conf.savebmarks;
2718 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2720 | 'S' ->
2721 let ondone s =
2723 let pageno, py =
2724 match state.layout with
2725 | [] -> 0, 0
2726 | l :: _ ->
2727 l.pageno, l.pagey
2729 conf.interpagespace <- int_of_string s;
2730 state.maxy <- calcheight ();
2731 let y = getpagey pageno in
2732 gotoy (y + py)
2733 with exc ->
2734 state.text <- Printf.sprintf "bad integer `%s': %s"
2735 s (Printexc.to_string exc)
2737 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2739 | 'l' ->
2740 reqlayout conf.angle (not conf.proportional);
2741 TEdone ("proportional display " ^ btos conf.proportional)
2743 | 'T' ->
2744 settrim (not conf.trimmargins) conf.trimfuzz;
2745 TEdone ("trim margins " ^ btos conf.trimmargins)
2747 | 'I' ->
2748 conf.invert <- not conf.invert;
2749 TEdone ("invert colors " ^ btos conf.invert)
2751 | 'x' ->
2752 let ondone s =
2753 cbput state.hists.sel s;
2754 conf.selcmd <- s;
2756 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2757 textentry, ondone)
2759 | _ ->
2760 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2761 TEstop
2762 else
2763 TEcont state.text
2766 class type lvsource = object
2767 method getitemcount : int
2768 method getitem : int -> (string * int)
2769 method hasaction : int -> bool
2770 method exit :
2771 uioh:uioh ->
2772 cancel:bool ->
2773 active:int ->
2774 first:int ->
2775 pan:int ->
2776 qsearch:string ->
2777 uioh option
2778 method getactive : int
2779 method getfirst : int
2780 method getqsearch : string
2781 method setqsearch : string -> unit
2782 method getpan : int
2783 end;;
2785 class virtual lvsourcebase = object
2786 val mutable m_active = 0
2787 val mutable m_first = 0
2788 val mutable m_qsearch = ""
2789 val mutable m_pan = 0
2790 method getactive = m_active
2791 method getfirst = m_first
2792 method getqsearch = m_qsearch
2793 method getpan = m_pan
2794 method setqsearch s = m_qsearch <- s
2795 end;;
2797 let withoutlastutf8 s =
2798 let len = String.length s in
2799 if len = 0
2800 then s
2801 else
2802 let rec find pos =
2803 if pos = 0
2804 then pos
2805 else
2806 let b = Char.code s.[pos] in
2807 if b land 0b110000 = 0b11000000
2808 then find (pos-1)
2809 else pos-1
2811 let first =
2812 if Char.code s.[len-1] land 0x80 = 0
2813 then len-1
2814 else find (len-1)
2816 String.sub s 0 first;
2819 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2820 let enttext te =
2821 state.mode <- Textentry (te, onleave);
2822 state.text <- "";
2823 enttext ();
2824 G.postRedisplay "textentrykeyboard enttext";
2826 let histaction cmd =
2827 match opthist with
2828 | None -> ()
2829 | Some (action, _) ->
2830 state.mode <- Textentry (
2831 (c, action cmd, opthist, onkey, ondone), onleave
2833 G.postRedisplay "textentry histaction"
2835 match key with
2836 | 0xff08 -> (* backspace *)
2837 let s = withoutlastutf8 text in
2838 let len = String.length s in
2839 if len = 0
2840 then (
2841 onleave Cancel;
2842 G.postRedisplay "textentrykeyboard after cancel";
2844 else (
2845 enttext (c, s, opthist, onkey, ondone)
2848 | 0xff0d ->
2849 ondone text;
2850 onleave Confirm;
2851 G.postRedisplay "textentrykeyboard after confirm"
2853 | 0xff52 -> histaction HCprev
2854 | 0xff54 -> histaction HCnext
2855 | 0xff50 -> histaction HCfirst
2856 | 0xff57 -> histaction HClast
2858 | 0xff1b -> (* escape*)
2859 if String.length text = 0
2860 then (
2861 begin match opthist with
2862 | None -> ()
2863 | Some (_, onhistcancel) -> onhistcancel ()
2864 end;
2865 onleave Cancel;
2866 state.text <- "";
2867 G.postRedisplay "textentrykeyboard after cancel2"
2869 else (
2870 enttext (c, "", opthist, onkey, ondone)
2873 | 0xff9f | 0xffff -> () (* delete *)
2875 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2876 begin match onkey text key with
2877 | TEdone text ->
2878 ondone text;
2879 onleave Confirm;
2880 G.postRedisplay "textentrykeyboard after confirm2";
2882 | TEcont text ->
2883 enttext (c, text, opthist, onkey, ondone);
2885 | TEstop ->
2886 onleave Cancel;
2887 G.postRedisplay "textentrykeyboard after cancel3"
2889 | TEswitch te ->
2890 state.mode <- Textentry (te, onleave);
2891 G.postRedisplay "textentrykeyboard switch";
2892 end;
2894 | _ ->
2895 vlog "unhandled key %s" (Wsi.keyname key)
2898 let firstof first active =
2899 if first > active || abs (first - active) > fstate.maxrows - 1
2900 then max 0 (active - (fstate.maxrows/2))
2901 else first
2904 let calcfirst first active =
2905 if active > first
2906 then
2907 let rows = active - first in
2908 if rows > fstate.maxrows then active - fstate.maxrows else first
2909 else active
2912 let scrollph y maxy =
2913 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2914 let sh = float conf.winh /. sh in
2915 let sh = max sh (float conf.scrollh) in
2917 let percent =
2918 if y = state.maxy
2919 then 1.0
2920 else float y /. float maxy
2922 let position = (float conf.winh -. sh) *. percent in
2924 let position =
2925 if position +. sh > float conf.winh
2926 then float conf.winh -. sh
2927 else position
2929 position, sh;
2932 let coe s = (s :> uioh);;
2934 class listview ~(source:lvsource) ~trusted ~modehash =
2935 object (self)
2936 val m_pan = source#getpan
2937 val m_first = source#getfirst
2938 val m_active = source#getactive
2939 val m_qsearch = source#getqsearch
2940 val m_prev_uioh = state.uioh
2942 method private elemunder y =
2943 let n = y / (fstate.fontsize+1) in
2944 if m_first + n < source#getitemcount
2945 then (
2946 if source#hasaction (m_first + n)
2947 then Some (m_first + n)
2948 else None
2950 else None
2952 method display =
2953 Gl.enable `blend;
2954 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2955 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2956 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2957 GlDraw.color (1., 1., 1.);
2958 Gl.enable `texture_2d;
2959 let fs = fstate.fontsize in
2960 let nfs = fs + 1 in
2961 let ww = fstate.wwidth in
2962 let tabw = 30.0*.ww in
2963 let itemcount = source#getitemcount in
2964 let rec loop row =
2965 if (row - m_first) * nfs > conf.winh
2966 then ()
2967 else (
2968 if row >= 0 && row < itemcount
2969 then (
2970 let (s, level) = source#getitem row in
2971 let y = (row - m_first) * nfs in
2972 let x = 5.0 +. float (level + m_pan) *. ww in
2973 if row = m_active
2974 then (
2975 Gl.disable `texture_2d;
2976 GlDraw.polygon_mode `both `line;
2977 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2978 GlDraw.rect (1., float (y + 1))
2979 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2980 GlDraw.polygon_mode `both `fill;
2981 GlDraw.color (1., 1., 1.);
2982 Gl.enable `texture_2d;
2985 let drawtabularstring s =
2986 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2987 if trusted
2988 then
2989 let tabpos = try String.index s '\t' with Not_found -> -1 in
2990 if tabpos > 0
2991 then
2992 let len = String.length s - tabpos - 1 in
2993 let s1 = String.sub s 0 tabpos
2994 and s2 = String.sub s (tabpos + 1) len in
2995 let nx = drawstr x s1 in
2996 let sw = nx -. x in
2997 let x = x +. (max tabw sw) in
2998 drawstr x s2
2999 else
3000 drawstr x s
3001 else
3002 drawstr x s
3004 let _ = drawtabularstring s in
3005 loop (row+1)
3009 loop m_first;
3010 Gl.disable `blend;
3011 Gl.disable `texture_2d;
3013 method updownlevel incr =
3014 let len = source#getitemcount in
3015 let curlevel =
3016 if m_active >= 0 && m_active < len
3017 then snd (source#getitem m_active)
3018 else -1
3020 let rec flow i =
3021 if i = len then i-1 else if i = -1 then 0 else
3022 let _, l = source#getitem i in
3023 if l != curlevel then i else flow (i+incr)
3025 let active = flow m_active in
3026 let first = calcfirst m_first active in
3027 G.postRedisplay "outline updownlevel";
3028 {< m_active = active; m_first = first >}
3030 method private key1 key mask =
3031 let set1 active first qsearch =
3032 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3034 let search active pattern incr =
3035 let dosearch re =
3036 let rec loop n =
3037 if n >= 0 && n < source#getitemcount
3038 then (
3039 let s, _ = source#getitem n in
3041 (try ignore (Str.search_forward re s 0); true
3042 with Not_found -> false)
3043 then Some n
3044 else loop (n + incr)
3046 else None
3048 loop active
3051 let re = Str.regexp_case_fold pattern in
3052 dosearch re
3053 with Failure s ->
3054 state.text <- s;
3055 None
3057 let itemcount = source#getitemcount in
3058 let find start incr =
3059 let rec find i =
3060 if i = -1 || i = itemcount
3061 then -1
3062 else (
3063 if source#hasaction i
3064 then i
3065 else find (i + incr)
3068 find start
3070 let set active first =
3071 let first = bound first 0 (itemcount - fstate.maxrows) in
3072 state.text <- "";
3073 coe {< m_active = active; m_first = first >}
3075 let navigate incr =
3076 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3077 let active, first =
3078 let incr1 = if incr > 0 then 1 else -1 in
3079 if isvisible m_first m_active
3080 then
3081 let next =
3082 let next = m_active + incr in
3083 let next =
3084 if next < 0 || next >= itemcount
3085 then -1
3086 else find next incr1
3088 if next = -1 || abs (m_active - next) > fstate.maxrows
3089 then -1
3090 else next
3092 if next = -1
3093 then
3094 let first = m_first + incr in
3095 let first = bound first 0 (itemcount - 1) in
3096 let next =
3097 let next = m_active + incr in
3098 let next = bound next 0 (itemcount - 1) in
3099 find next ~-incr1
3101 let active = if next = -1 then m_active else next in
3102 active, first
3103 else
3104 let first = min next m_first in
3105 let first =
3106 if abs (next - first) > fstate.maxrows
3107 then first + incr
3108 else first
3110 next, first
3111 else
3112 let first = m_first + incr in
3113 let first = bound first 0 (itemcount - 1) in
3114 let active =
3115 let next = m_active + incr in
3116 let next = bound next 0 (itemcount - 1) in
3117 let next = find next incr1 in
3118 let active =
3119 if next = -1 || abs (m_active - first) > fstate.maxrows
3120 then (
3121 let active = if m_active = -1 then next else m_active in
3122 active
3124 else next
3126 if isvisible first active
3127 then active
3128 else -1
3130 active, first
3132 G.postRedisplay "listview navigate";
3133 set active first;
3135 match key with
3136 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3137 let incr = if key = 0x72 then -1 else 1 in
3138 let active, first =
3139 match search (m_active + incr) m_qsearch incr with
3140 | None ->
3141 state.text <- m_qsearch ^ " [not found]";
3142 m_active, m_first
3143 | Some active ->
3144 state.text <- m_qsearch;
3145 active, firstof m_first active
3147 G.postRedisplay "listview ctrl-r/s";
3148 set1 active first m_qsearch;
3150 | 0xff08 -> (* backspace *)
3151 if String.length m_qsearch = 0
3152 then coe self
3153 else (
3154 let qsearch = withoutlastutf8 m_qsearch in
3155 let len = String.length qsearch in
3156 if len = 0
3157 then (
3158 state.text <- "";
3159 G.postRedisplay "listview empty qsearch";
3160 set1 m_active m_first "";
3162 else
3163 let active, first =
3164 match search m_active qsearch ~-1 with
3165 | None ->
3166 state.text <- qsearch ^ " [not found]";
3167 m_active, m_first
3168 | Some active ->
3169 state.text <- qsearch;
3170 active, firstof m_first active
3172 G.postRedisplay "listview backspace qsearch";
3173 set1 active first qsearch
3176 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3177 let pattern = m_qsearch ^ Wsi.toutf8 key in
3178 let active, first =
3179 match search m_active pattern 1 with
3180 | None ->
3181 state.text <- pattern ^ " [not found]";
3182 m_active, m_first
3183 | Some active ->
3184 state.text <- pattern;
3185 active, firstof m_first active
3187 G.postRedisplay "listview qsearch add";
3188 set1 active first pattern;
3190 | 0xff1b -> (* escape *)
3191 state.text <- "";
3192 if String.length m_qsearch = 0
3193 then (
3194 G.postRedisplay "list view escape";
3195 begin
3196 match
3197 source#exit (coe self) true m_active m_first m_pan m_qsearch
3198 with
3199 | None -> m_prev_uioh
3200 | Some uioh -> uioh
3203 else (
3204 G.postRedisplay "list view kill qsearch";
3205 source#setqsearch "";
3206 coe {< m_qsearch = "" >}
3209 | 0xff0d -> (* return *)
3210 state.text <- "";
3211 let self = {< m_qsearch = "" >} in
3212 source#setqsearch "";
3213 let opt =
3214 G.postRedisplay "listview enter";
3215 if m_active >= 0 && m_active < source#getitemcount
3216 then (
3217 source#exit (coe self) false m_active m_first m_pan "";
3219 else (
3220 source#exit (coe self) true m_active m_first m_pan "";
3223 begin match opt with
3224 | None -> m_prev_uioh
3225 | Some uioh -> uioh
3228 | 0xff9f | 0xffff -> (* delete *)
3229 coe self
3231 | 0xff52 -> navigate ~-1 (* up *)
3232 | 0xff54 -> navigate 1 (* down *)
3233 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3234 | 0xff56 -> navigate fstate.maxrows (* next *)
3236 | 0xff53 -> (* right *)
3237 state.text <- "";
3238 G.postRedisplay "listview right";
3239 coe {< m_pan = m_pan - 1 >}
3241 | 0xff51 -> (* left *)
3242 state.text <- "";
3243 G.postRedisplay "listview left";
3244 coe {< m_pan = m_pan + 1 >}
3246 | 0xff50 -> (* home *)
3247 let active = find 0 1 in
3248 G.postRedisplay "listview home";
3249 set active 0;
3251 | 0xff57 -> (* end *)
3252 let first = max 0 (itemcount - fstate.maxrows) in
3253 let active = find (itemcount - 1) ~-1 in
3254 G.postRedisplay "listview end";
3255 set active first;
3257 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3258 coe self
3260 | _ ->
3261 dolog "listview unknown key %#x" key; coe self
3263 method key key mask =
3264 match state.mode with
3265 | Textentry te -> textentrykeyboard key mask te; coe self
3266 | _ -> self#key1 key mask
3268 method button button down x y _ =
3269 let opt =
3270 match button with
3271 | 1 when x > conf.winw - conf.scrollbw ->
3272 G.postRedisplay "listview scroll";
3273 if down
3274 then
3275 let _, position, sh = self#scrollph in
3276 if y > truncate position && y < truncate (position +. sh)
3277 then (
3278 state.mstate <- Mscrolly;
3279 Some (coe self)
3281 else
3282 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3283 let first = truncate (s *. float source#getitemcount) in
3284 let first = min source#getitemcount first in
3285 Some (coe {< m_first = first; m_active = first >})
3286 else (
3287 state.mstate <- Mnone;
3288 Some (coe self);
3290 | 1 when not down ->
3291 begin match self#elemunder y with
3292 | Some n ->
3293 G.postRedisplay "listview click";
3294 source#exit
3295 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3296 | _ ->
3297 Some (coe self)
3299 | n when (n == 4 || n == 5) && not down ->
3300 let len = source#getitemcount in
3301 let first =
3302 if n = 5 && m_first + fstate.maxrows >= len
3303 then
3304 m_first
3305 else
3306 let first = m_first + (if n == 4 then -1 else 1) in
3307 bound first 0 (len - 1)
3309 G.postRedisplay "listview wheel";
3310 Some (coe {< m_first = first >})
3311 | _ ->
3312 Some (coe self)
3314 match opt with
3315 | None -> m_prev_uioh
3316 | Some uioh -> uioh
3318 method motion _ y =
3319 match state.mstate with
3320 | Mscrolly ->
3321 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3322 let first = truncate (s *. float source#getitemcount) in
3323 let first = min source#getitemcount first in
3324 G.postRedisplay "listview motion";
3325 coe {< m_first = first; m_active = first >}
3326 | _ -> coe self
3328 method pmotion x y =
3329 if x < conf.winw - conf.scrollbw
3330 then
3331 let n =
3332 match self#elemunder y with
3333 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3334 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3336 let o =
3337 if n != m_active
3338 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3339 else self
3341 coe o
3342 else (
3343 Wsi.setcursor Wsi.CURSOR_INHERIT;
3344 coe self
3347 method infochanged _ = ()
3349 method scrollpw = (0, 0.0, 0.0)
3350 method scrollph =
3351 let nfs = fstate.fontsize + 1 in
3352 let y = m_first * nfs in
3353 let itemcount = source#getitemcount in
3354 let maxi = max 0 (itemcount - fstate.maxrows) in
3355 let maxy = maxi * nfs in
3356 let p, h = scrollph y maxy in
3357 conf.scrollbw, p, h
3359 method modehash = modehash
3360 end;;
3362 class outlinelistview ~source =
3363 object (self)
3364 inherit listview
3365 ~source:(source :> lvsource)
3366 ~trusted:false
3367 ~modehash:(findkeyhash conf "outline")
3368 as super
3370 method key key mask =
3371 let calcfirst first active =
3372 if active > first
3373 then
3374 let rows = active - first in
3375 if rows > fstate.maxrows then active - fstate.maxrows else first
3376 else active
3378 let navigate incr =
3379 let active = m_active + incr in
3380 let active = bound active 0 (source#getitemcount - 1) in
3381 let first = calcfirst m_first active in
3382 G.postRedisplay "outline navigate";
3383 coe {< m_active = active; m_first = first >}
3385 let ctrl = Wsi.withctrl mask in
3386 match key with
3387 | 110 when ctrl -> (* ctrl-n *)
3388 source#narrow m_qsearch;
3389 G.postRedisplay "outline ctrl-n";
3390 coe {< m_first = 0; m_active = 0 >}
3392 | 117 when ctrl -> (* ctrl-u *)
3393 source#denarrow;
3394 G.postRedisplay "outline ctrl-u";
3395 state.text <- "";
3396 coe {< m_first = 0; m_active = 0 >}
3398 | 108 when ctrl -> (* ctrl-l *)
3399 let first = m_active - (fstate.maxrows / 2) in
3400 G.postRedisplay "outline ctrl-l";
3401 coe {< m_first = first >}
3403 | 0xff9f | 0xffff -> (* delete *)
3404 source#remove m_active;
3405 G.postRedisplay "outline delete";
3406 let active = max 0 (m_active-1) in
3407 coe {< m_first = firstof m_first active;
3408 m_active = active >}
3410 | 0xff52 -> navigate ~-1 (* up *)
3411 | 0xff54 -> navigate 1 (* down *)
3412 | 0xff55 -> (* prior *)
3413 navigate ~-(fstate.maxrows)
3414 | 0xff56 -> (* next *)
3415 navigate fstate.maxrows
3417 | 0xff53 -> (* [ctrl-]right *)
3418 let o =
3419 if ctrl
3420 then (
3421 G.postRedisplay "outline ctrl right";
3422 {< m_pan = m_pan + 1 >}
3424 else self#updownlevel 1
3426 coe o
3428 | 0xff51 -> (* [ctrl-]left *)
3429 let o =
3430 if ctrl
3431 then (
3432 G.postRedisplay "outline ctrl left";
3433 {< m_pan = m_pan - 1 >}
3435 else self#updownlevel ~-1
3437 coe o
3439 | 0xff50 -> (* home *)
3440 G.postRedisplay "outline home";
3441 coe {< m_first = 0; m_active = 0 >}
3443 | 0xff57 -> (* end *)
3444 let active = source#getitemcount - 1 in
3445 let first = max 0 (active - fstate.maxrows) in
3446 G.postRedisplay "outline end";
3447 coe {< m_active = active; m_first = first >}
3449 | _ -> super#key key mask
3452 let outlinesource usebookmarks =
3453 let empty = [||] in
3454 (object
3455 inherit lvsourcebase
3456 val mutable m_items = empty
3457 val mutable m_orig_items = empty
3458 val mutable m_prev_items = empty
3459 val mutable m_narrow_pattern = ""
3460 val mutable m_hadremovals = false
3462 method getitemcount =
3463 Array.length m_items + (if m_hadremovals then 1 else 0)
3465 method getitem n =
3466 if n == Array.length m_items && m_hadremovals
3467 then
3468 ("[Confirm removal]", 0)
3469 else
3470 let s, n, _ = m_items.(n) in
3471 (s, n)
3473 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3474 ignore (uioh, first, qsearch);
3475 let confrimremoval = m_hadremovals && active = Array.length m_items in
3476 let items =
3477 if String.length m_narrow_pattern = 0
3478 then m_orig_items
3479 else m_items
3481 if not cancel
3482 then (
3483 if not confrimremoval
3484 then(
3485 let _, _, anchor = m_items.(active) in
3486 gotoanchor anchor;
3487 m_items <- items;
3489 else (
3490 state.bookmarks <- Array.to_list m_items;
3491 m_orig_items <- m_items;
3494 else m_items <- items;
3495 m_pan <- pan;
3496 None
3498 method hasaction _ = true
3500 method greetmsg =
3501 if Array.length m_items != Array.length m_orig_items
3502 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3503 else ""
3505 method narrow pattern =
3506 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3507 match reopt with
3508 | None -> ()
3509 | Some re ->
3510 let rec loop accu n =
3511 if n = -1
3512 then (
3513 m_narrow_pattern <- pattern;
3514 m_items <- Array.of_list accu
3516 else
3517 let (s, _, _) as o = m_items.(n) in
3518 let accu =
3519 if (try ignore (Str.search_forward re s 0); true
3520 with Not_found -> false)
3521 then o :: accu
3522 else accu
3524 loop accu (n-1)
3526 loop [] (Array.length m_items - 1)
3528 method denarrow =
3529 m_orig_items <- (
3530 if usebookmarks
3531 then Array.of_list state.bookmarks
3532 else state.outlines
3534 m_items <- m_orig_items
3536 method remove m =
3537 if usebookmarks
3538 then
3539 if m >= 0 && m < Array.length m_items
3540 then (
3541 m_hadremovals <- true;
3542 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3543 let n = if n >= m then n+1 else n in
3544 m_items.(n)
3548 method reset anchor items =
3549 m_hadremovals <- false;
3550 if m_orig_items == empty || m_prev_items != items
3551 then (
3552 m_orig_items <- items;
3553 if String.length m_narrow_pattern = 0
3554 then m_items <- items;
3556 m_prev_items <- items;
3557 let rely = getanchory anchor in
3558 let active =
3559 let rec loop n best bestd =
3560 if n = Array.length m_items
3561 then best
3562 else
3563 let (_, _, anchor) = m_items.(n) in
3564 let orely = getanchory anchor in
3565 let d = abs (orely - rely) in
3566 if d < bestd
3567 then loop (n+1) n d
3568 else loop (n+1) best bestd
3570 loop 0 ~-1 max_int
3572 m_active <- active;
3573 m_first <- firstof m_first active
3574 end)
3577 let enterselector usebookmarks =
3578 let source = outlinesource usebookmarks in
3579 fun errmsg ->
3580 let outlines =
3581 if usebookmarks
3582 then Array.of_list state.bookmarks
3583 else state.outlines
3585 if Array.length outlines = 0
3586 then (
3587 showtext ' ' errmsg;
3589 else (
3590 state.text <- source#greetmsg;
3591 Wsi.setcursor Wsi.CURSOR_INHERIT;
3592 let anchor = getanchor () in
3593 source#reset anchor outlines;
3594 state.uioh <- coe (new outlinelistview ~source);
3595 G.postRedisplay "enter selector";
3599 let enteroutlinemode =
3600 let f = enterselector false in
3601 fun ()-> f "Document has no outline";
3604 let enterbookmarkmode =
3605 let f = enterselector true in
3606 fun () -> f "Document has no bookmarks (yet)";
3609 let color_of_string s =
3610 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3611 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3615 let color_to_string (r, g, b) =
3616 let r = truncate (r *. 256.0)
3617 and g = truncate (g *. 256.0)
3618 and b = truncate (b *. 256.0) in
3619 Printf.sprintf "%d/%d/%d" r g b
3622 let irect_of_string s =
3623 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3626 let irect_to_string (x0,y0,x1,y1) =
3627 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3630 let makecheckers () =
3631 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3632 following to say:
3633 converted by Issac Trotts. July 25, 2002 *)
3634 let image_height = 64
3635 and image_width = 64 in
3637 let make_image () =
3638 let image =
3639 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3641 for i = 0 to image_width - 1 do
3642 for j = 0 to image_height - 1 do
3643 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3644 (if (i land 8 ) lxor (j land 8) = 0
3645 then [|255;255;255|] else [|200;200;200|])
3646 done
3647 done;
3648 image
3650 let image = make_image () in
3651 let id = GlTex.gen_texture () in
3652 GlTex.bind_texture `texture_2d id;
3653 GlPix.store (`unpack_alignment 1);
3654 GlTex.image2d image;
3655 List.iter (GlTex.parameter ~target:`texture_2d)
3656 [ `wrap_s `repeat;
3657 `wrap_t `repeat;
3658 `mag_filter `nearest;
3659 `min_filter `nearest ];
3663 let setcheckers enabled =
3664 match state.texid with
3665 | None ->
3666 if enabled then state.texid <- Some (makecheckers ())
3668 | Some texid ->
3669 if not enabled
3670 then (
3671 GlTex.delete_texture texid;
3672 state.texid <- None;
3676 let int_of_string_with_suffix s =
3677 let l = String.length s in
3678 let s1, shift =
3679 if l > 1
3680 then
3681 let suffix = Char.lowercase s.[l-1] in
3682 match suffix with
3683 | 'k' -> String.sub s 0 (l-1), 10
3684 | 'm' -> String.sub s 0 (l-1), 20
3685 | 'g' -> String.sub s 0 (l-1), 30
3686 | _ -> s, 0
3687 else s, 0
3689 let n = int_of_string s1 in
3690 let m = n lsl shift in
3691 if m < 0 || m < n
3692 then raise (Failure "value too large")
3693 else m
3696 let string_with_suffix_of_int n =
3697 if n = 0
3698 then "0"
3699 else
3700 let n, s =
3701 if n = 0
3702 then 0, ""
3703 else (
3704 if n land ((1 lsl 20) - 1) = 0
3705 then n lsr 20, "M"
3706 else (
3707 if n land ((1 lsl 10) - 1) = 0
3708 then n lsr 10, "K"
3709 else n, ""
3713 let rec loop s n =
3714 let h = n mod 1000 in
3715 let n = n / 1000 in
3716 if n = 0
3717 then string_of_int h ^ s
3718 else (
3719 let s = Printf.sprintf "_%03d%s" h s in
3720 loop s n
3723 loop "" n ^ s;
3726 let defghyllscroll = (40, 8, 32);;
3727 let ghyllscroll_of_string s =
3728 let (n, a, b) as nab =
3729 if s = "default"
3730 then defghyllscroll
3731 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3733 if n <= a || n <= b || a >= b
3734 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3735 nab;
3738 let ghyllscroll_to_string ((n, a, b) as nab) =
3739 if nab = defghyllscroll
3740 then "default"
3741 else Printf.sprintf "%d,%d,%d" n a b;
3744 let describe_location () =
3745 let f (fn, _) l =
3746 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3748 let fn, ln = List.fold_left f (-1, -1) state.layout in
3749 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3750 let percent =
3751 if maxy <= 0
3752 then 100.
3753 else (100. *. (float state.y /. float maxy))
3755 if fn = ln
3756 then
3757 Printf.sprintf "page %d of %d [%.2f%%]"
3758 (fn+1) state.pagecount percent
3759 else
3760 Printf.sprintf
3761 "pages %d-%d of %d [%.2f%%]"
3762 (fn+1) (ln+1) state.pagecount percent
3765 let enterinfomode =
3766 let btos b = if b then "\xe2\x88\x9a" else "" in
3767 let showextended = ref false in
3768 let leave mode = function
3769 | Confirm -> state.mode <- mode
3770 | Cancel -> state.mode <- mode in
3771 let src =
3772 (object
3773 val mutable m_first_time = true
3774 val mutable m_l = []
3775 val mutable m_a = [||]
3776 val mutable m_prev_uioh = nouioh
3777 val mutable m_prev_mode = View
3779 inherit lvsourcebase
3781 method reset prev_mode prev_uioh =
3782 m_a <- Array.of_list (List.rev m_l);
3783 m_l <- [];
3784 m_prev_mode <- prev_mode;
3785 m_prev_uioh <- prev_uioh;
3786 if m_first_time
3787 then (
3788 let rec loop n =
3789 if n >= Array.length m_a
3790 then ()
3791 else
3792 match m_a.(n) with
3793 | _, _, _, Action _ -> m_active <- n
3794 | _ -> loop (n+1)
3796 loop 0;
3797 m_first_time <- false;
3800 method int name get set =
3801 m_l <-
3802 (name, `int get, 1, Action (
3803 fun u ->
3804 let ondone s =
3805 try set (int_of_string s)
3806 with exn ->
3807 state.text <- Printf.sprintf "bad integer `%s': %s"
3808 s (Printexc.to_string exn)
3810 state.text <- "";
3811 let te = name ^ ": ", "", None, intentry, ondone in
3812 state.mode <- Textentry (te, leave m_prev_mode);
3814 )) :: m_l
3816 method int_with_suffix name get set =
3817 m_l <-
3818 (name, `intws get, 1, Action (
3819 fun u ->
3820 let ondone s =
3821 try set (int_of_string_with_suffix s)
3822 with exn ->
3823 state.text <- Printf.sprintf "bad integer `%s': %s"
3824 s (Printexc.to_string exn)
3826 state.text <- "";
3827 let te =
3828 name ^ ": ", "", None, intentry_with_suffix, ondone
3830 state.mode <- Textentry (te, leave m_prev_mode);
3832 )) :: m_l
3834 method bool ?(offset=1) ?(btos=btos) name get set =
3835 m_l <-
3836 (name, `bool (btos, get), offset, Action (
3837 fun u ->
3838 let v = get () in
3839 set (not v);
3841 )) :: m_l
3843 method color name get set =
3844 m_l <-
3845 (name, `color get, 1, Action (
3846 fun u ->
3847 let invalid = (nan, nan, nan) in
3848 let ondone s =
3849 let c =
3850 try color_of_string s
3851 with exn ->
3852 state.text <- Printf.sprintf "bad color `%s': %s"
3853 s (Printexc.to_string exn);
3854 invalid
3856 if c <> invalid
3857 then set c;
3859 let te = name ^ ": ", "", None, textentry, ondone in
3860 state.text <- color_to_string (get ());
3861 state.mode <- Textentry (te, leave m_prev_mode);
3863 )) :: m_l
3865 method string name get set =
3866 m_l <-
3867 (name, `string get, 1, Action (
3868 fun u ->
3869 let ondone s = set s in
3870 let te = name ^ ": ", "", None, textentry, ondone in
3871 state.mode <- Textentry (te, leave m_prev_mode);
3873 )) :: m_l
3875 method colorspace name get set =
3876 m_l <-
3877 (name, `string get, 1, Action (
3878 fun _ ->
3879 let source =
3880 let vals = [| "rgb"; "bgr"; "gray" |] in
3881 (object
3882 inherit lvsourcebase
3884 initializer
3885 m_active <- int_of_colorspace conf.colorspace;
3886 m_first <- 0;
3888 method getitemcount = Array.length vals
3889 method getitem n = (vals.(n), 0)
3890 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3891 ignore (uioh, first, pan, qsearch);
3892 if not cancel then set active;
3893 None
3894 method hasaction _ = true
3895 end)
3897 state.text <- "";
3898 let modehash = findkeyhash conf "info" in
3899 coe (new listview ~source ~trusted:true ~modehash)
3900 )) :: m_l
3902 method caption s offset =
3903 m_l <- (s, `empty, offset, Noaction) :: m_l
3905 method caption2 s f offset =
3906 m_l <- (s, `string f, offset, Noaction) :: m_l
3908 method getitemcount = Array.length m_a
3910 method getitem n =
3911 let tostr = function
3912 | `int f -> string_of_int (f ())
3913 | `intws f -> string_with_suffix_of_int (f ())
3914 | `string f -> f ()
3915 | `color f -> color_to_string (f ())
3916 | `bool (btos, f) -> btos (f ())
3917 | `empty -> ""
3919 let name, t, offset, _ = m_a.(n) in
3920 ((let s = tostr t in
3921 if String.length s > 0
3922 then Printf.sprintf "%s\t%s" name s
3923 else name),
3924 offset)
3926 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3927 let uiohopt =
3928 if not cancel
3929 then (
3930 m_qsearch <- qsearch;
3931 let uioh =
3932 match m_a.(active) with
3933 | _, _, _, Action f -> f uioh
3934 | _ -> uioh
3936 Some uioh
3938 else None
3940 m_active <- active;
3941 m_first <- first;
3942 m_pan <- pan;
3943 uiohopt
3945 method hasaction n =
3946 match m_a.(n) with
3947 | _, _, _, Action _ -> true
3948 | _ -> false
3949 end)
3951 let rec fillsrc prevmode prevuioh =
3952 let sep () = src#caption "" 0 in
3953 let colorp name get set =
3954 src#string name
3955 (fun () -> color_to_string (get ()))
3956 (fun v ->
3958 let c = color_of_string v in
3959 set c
3960 with exn ->
3961 state.text <- Printf.sprintf "bad color `%s': %s"
3962 v (Printexc.to_string exn);
3965 let oldmode = state.mode in
3966 let birdseye = isbirdseye state.mode in
3968 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3970 src#bool "presentation mode"
3971 (fun () -> conf.presentation)
3972 (fun v ->
3973 conf.presentation <- v;
3974 state.anchor <- getanchor ();
3975 represent ());
3977 src#bool "ignore case in searches"
3978 (fun () -> conf.icase)
3979 (fun v -> conf.icase <- v);
3981 src#bool "preload"
3982 (fun () -> conf.preload)
3983 (fun v -> conf.preload <- v);
3985 src#bool "highlight links"
3986 (fun () -> conf.hlinks)
3987 (fun v -> conf.hlinks <- v);
3989 src#bool "under info"
3990 (fun () -> conf.underinfo)
3991 (fun v -> conf.underinfo <- v);
3993 src#bool "persistent bookmarks"
3994 (fun () -> conf.savebmarks)
3995 (fun v -> conf.savebmarks <- v);
3997 src#bool "proportional display"
3998 (fun () -> conf.proportional)
3999 (fun v -> reqlayout conf.angle v);
4001 src#bool "trim margins"
4002 (fun () -> conf.trimmargins)
4003 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4005 src#bool "persistent location"
4006 (fun () -> conf.jumpback)
4007 (fun v -> conf.jumpback <- v);
4009 sep ();
4010 src#int "inter-page space"
4011 (fun () -> conf.interpagespace)
4012 (fun n ->
4013 conf.interpagespace <- n;
4014 let pageno, py =
4015 match state.layout with
4016 | [] -> 0, 0
4017 | l :: _ ->
4018 l.pageno, l.pagey
4020 state.maxy <- calcheight ();
4021 let y = getpagey pageno in
4022 gotoy (y + py)
4025 src#int "page bias"
4026 (fun () -> conf.pagebias)
4027 (fun v -> conf.pagebias <- v);
4029 src#int "scroll step"
4030 (fun () -> conf.scrollstep)
4031 (fun n -> conf.scrollstep <- n);
4033 src#int "auto scroll step"
4034 (fun () ->
4035 match state.autoscroll with
4036 | Some step -> step
4037 | _ -> conf.autoscrollstep)
4038 (fun n ->
4039 if state.autoscroll <> None
4040 then state.autoscroll <- Some n;
4041 conf.autoscrollstep <- n);
4043 src#int "zoom"
4044 (fun () -> truncate (conf.zoom *. 100.))
4045 (fun v -> setzoom ((float v) /. 100.));
4047 src#int "rotation"
4048 (fun () -> conf.angle)
4049 (fun v -> reqlayout v conf.proportional);
4051 src#int "scroll bar width"
4052 (fun () -> state.scrollw)
4053 (fun v ->
4054 state.scrollw <- v;
4055 conf.scrollbw <- v;
4056 reshape conf.winw conf.winh;
4059 src#int "scroll handle height"
4060 (fun () -> conf.scrollh)
4061 (fun v -> conf.scrollh <- v;);
4063 src#int "thumbnail width"
4064 (fun () -> conf.thumbw)
4065 (fun v ->
4066 conf.thumbw <- min 4096 v;
4067 match oldmode with
4068 | Birdseye beye ->
4069 leavebirdseye beye false;
4070 enterbirdseye ()
4071 | _ -> ()
4074 let mode = state.mode in
4075 src#string "columns"
4076 (fun () ->
4077 match conf.columns with
4078 | Csingle -> "1"
4079 | Cmulti (multi, _) -> multicolumns_to_string multi
4080 | Csplit (count, _) -> "-" ^ string_of_int count
4082 (fun v ->
4083 let n, a, b = multicolumns_of_string v in
4084 setcolumns mode n a b);
4086 sep ();
4087 src#caption "Presentation mode" 0;
4088 src#bool "scrollbar visible"
4089 (fun () -> conf.scrollbarinpm)
4090 (fun v ->
4091 if v != conf.scrollbarinpm
4092 then (
4093 conf.scrollbarinpm <- v;
4094 if conf.presentation
4095 then (
4096 state.scrollw <- if v then conf.scrollbw else 0;
4097 reshape conf.winw conf.winh;
4102 sep ();
4103 src#caption "Pixmap cache" 0;
4104 src#int_with_suffix "size (advisory)"
4105 (fun () -> conf.memlimit)
4106 (fun v -> conf.memlimit <- v);
4108 src#caption2 "used"
4109 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4110 (string_with_suffix_of_int state.memused)
4111 (Hashtbl.length state.tilemap)) 1;
4113 sep ();
4114 src#caption "Layout" 0;
4115 src#caption2 "Dimension"
4116 (fun () ->
4117 Printf.sprintf "%dx%d (virtual %dx%d)"
4118 conf.winw conf.winh
4119 state.w state.maxy)
4121 if conf.debug
4122 then
4123 src#caption2 "Position" (fun () ->
4124 Printf.sprintf "%dx%d" state.x state.y
4126 else
4127 src#caption2 "Visible" (fun () -> describe_location ()) 1
4130 sep ();
4131 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4132 "Save these parameters as global defaults at exit"
4133 (fun () -> conf.bedefault)
4134 (fun v -> conf.bedefault <- v)
4137 sep ();
4138 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4139 src#bool ~offset:0 ~btos "Extended parameters"
4140 (fun () -> !showextended)
4141 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4142 if !showextended
4143 then (
4144 src#bool "checkers"
4145 (fun () -> conf.checkers)
4146 (fun v -> conf.checkers <- v; setcheckers v);
4147 src#bool "update cursor"
4148 (fun () -> conf.updatecurs)
4149 (fun v -> conf.updatecurs <- v);
4150 src#bool "verbose"
4151 (fun () -> conf.verbose)
4152 (fun v -> conf.verbose <- v);
4153 src#bool "invert colors"
4154 (fun () -> conf.invert)
4155 (fun v -> conf.invert <- v);
4156 src#bool "max fit"
4157 (fun () -> conf.maxhfit)
4158 (fun v -> conf.maxhfit <- v);
4159 src#bool "redirect stderr"
4160 (fun () -> conf.redirectstderr)
4161 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4162 src#string "uri launcher"
4163 (fun () -> conf.urilauncher)
4164 (fun v -> conf.urilauncher <- v);
4165 src#string "path launcher"
4166 (fun () -> conf.pathlauncher)
4167 (fun v -> conf.pathlauncher <- v);
4168 src#string "tile size"
4169 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4170 (fun v ->
4172 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4173 conf.tileh <- max 64 w;
4174 conf.tilew <- max 64 h;
4175 flushtiles ();
4176 with exn ->
4177 state.text <- Printf.sprintf "bad tile size `%s': %s"
4178 v (Printexc.to_string exn));
4179 src#int "texture count"
4180 (fun () -> conf.texcount)
4181 (fun v ->
4182 if realloctexts v
4183 then conf.texcount <- v
4184 else showtext '!' " Failed to set texture count please retry later"
4186 src#int "slice height"
4187 (fun () -> conf.sliceheight)
4188 (fun v ->
4189 conf.sliceheight <- v;
4190 wcmd "sliceh %d" conf.sliceheight;
4192 src#int "anti-aliasing level"
4193 (fun () -> conf.aalevel)
4194 (fun v ->
4195 conf.aalevel <- bound v 0 8;
4196 state.anchor <- getanchor ();
4197 opendoc state.path state.password;
4199 src#int "ui font size"
4200 (fun () -> fstate.fontsize)
4201 (fun v -> setfontsize (bound v 5 100));
4202 colorp "background color"
4203 (fun () -> conf.bgcolor)
4204 (fun v -> conf.bgcolor <- v);
4205 src#bool "crop hack"
4206 (fun () -> conf.crophack)
4207 (fun v -> conf.crophack <- v);
4208 src#string "trim fuzz"
4209 (fun () -> irect_to_string conf.trimfuzz)
4210 (fun v ->
4212 conf.trimfuzz <- irect_of_string v;
4213 if conf.trimmargins
4214 then settrim true conf.trimfuzz;
4215 with exn ->
4216 state.text <- Printf.sprintf "bad irect `%s': %s"
4217 v (Printexc.to_string exn)
4219 src#string "throttle"
4220 (fun () ->
4221 match conf.maxwait with
4222 | None -> "show place holder if page is not ready"
4223 | Some time ->
4224 if time = infinity
4225 then "wait for page to fully render"
4226 else
4227 "wait " ^ string_of_float time
4228 ^ " seconds before showing placeholder"
4230 (fun v ->
4232 let f = float_of_string v in
4233 if f <= 0.0
4234 then conf.maxwait <- None
4235 else conf.maxwait <- Some f
4236 with exn ->
4237 state.text <- Printf.sprintf "bad time `%s': %s"
4238 v (Printexc.to_string exn)
4240 src#string "ghyll scroll"
4241 (fun () ->
4242 match conf.ghyllscroll with
4243 | None -> ""
4244 | Some nab -> ghyllscroll_to_string nab
4246 (fun v ->
4248 let gs =
4249 if String.length v = 0
4250 then None
4251 else Some (ghyllscroll_of_string v)
4253 conf.ghyllscroll <- gs
4254 with exn ->
4255 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4256 v (Printexc.to_string exn)
4258 src#string "selection command"
4259 (fun () -> conf.selcmd)
4260 (fun v -> conf.selcmd <- v);
4261 src#colorspace "color space"
4262 (fun () -> colorspace_to_string conf.colorspace)
4263 (fun v ->
4264 conf.colorspace <- colorspace_of_int v;
4265 wcmd "cs %d" v;
4266 load state.layout;
4270 sep ();
4271 src#caption "Document" 0;
4272 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4273 src#caption2 "Pages"
4274 (fun () -> string_of_int state.pagecount) 1;
4275 src#caption2 "Dimensions"
4276 (fun () -> string_of_int (List.length state.pdims)) 1;
4277 if conf.trimmargins
4278 then (
4279 sep ();
4280 src#caption "Trimmed margins" 0;
4281 src#caption2 "Dimensions"
4282 (fun () -> string_of_int (List.length state.pdims)) 1;
4285 src#reset prevmode prevuioh;
4287 fun () ->
4288 state.text <- "";
4289 let prevmode = state.mode
4290 and prevuioh = state.uioh in
4291 fillsrc prevmode prevuioh;
4292 let source = (src :> lvsource) in
4293 let modehash = findkeyhash conf "info" in
4294 state.uioh <- coe (object (self)
4295 inherit listview ~source ~trusted:true ~modehash as super
4296 val mutable m_prevmemused = 0
4297 method infochanged = function
4298 | Memused ->
4299 if m_prevmemused != state.memused
4300 then (
4301 m_prevmemused <- state.memused;
4302 G.postRedisplay "memusedchanged";
4304 | Pdim -> G.postRedisplay "pdimchanged"
4305 | Docinfo -> fillsrc prevmode prevuioh
4307 method key key mask =
4308 if not (Wsi.withctrl mask)
4309 then
4310 match key with
4311 | 0xff51 -> coe (self#updownlevel ~-1)
4312 | 0xff53 -> coe (self#updownlevel 1)
4313 | _ -> super#key key mask
4314 else super#key key mask
4315 end);
4316 G.postRedisplay "info";
4319 let enterhelpmode =
4320 let source =
4321 (object
4322 inherit lvsourcebase
4323 method getitemcount = Array.length state.help
4324 method getitem n =
4325 let s, n, _ = state.help.(n) in
4326 (s, n)
4328 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4329 let optuioh =
4330 if not cancel
4331 then (
4332 m_qsearch <- qsearch;
4333 match state.help.(active) with
4334 | _, _, Action f -> Some (f uioh)
4335 | _ -> Some (uioh)
4337 else None
4339 m_active <- active;
4340 m_first <- first;
4341 m_pan <- pan;
4342 optuioh
4344 method hasaction n =
4345 match state.help.(n) with
4346 | _, _, Action _ -> true
4347 | _ -> false
4349 initializer
4350 m_active <- -1
4351 end)
4352 in fun () ->
4353 let modehash = findkeyhash conf "help" in
4354 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4355 G.postRedisplay "help";
4358 let entermsgsmode =
4359 let msgsource =
4360 let re = Str.regexp "[\r\n]" in
4361 (object
4362 inherit lvsourcebase
4363 val mutable m_items = [||]
4365 method getitemcount = 1 + Array.length m_items
4367 method getitem n =
4368 if n = 0
4369 then "[Clear]", 0
4370 else m_items.(n-1), 0
4372 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4373 ignore uioh;
4374 if not cancel
4375 then (
4376 if active = 0
4377 then Buffer.clear state.errmsgs;
4378 m_qsearch <- qsearch;
4380 m_active <- active;
4381 m_first <- first;
4382 m_pan <- pan;
4383 None
4385 method hasaction n =
4386 n = 0
4388 method reset =
4389 state.newerrmsgs <- false;
4390 let l = Str.split re (Buffer.contents state.errmsgs) in
4391 m_items <- Array.of_list l
4393 initializer
4394 m_active <- 0
4395 end)
4396 in fun () ->
4397 state.text <- "";
4398 msgsource#reset;
4399 let source = (msgsource :> lvsource) in
4400 let modehash = findkeyhash conf "listview" in
4401 state.uioh <- coe (object
4402 inherit listview ~source ~trusted:false ~modehash as super
4403 method display =
4404 if state.newerrmsgs
4405 then msgsource#reset;
4406 super#display
4407 end);
4408 G.postRedisplay "msgs";
4411 let quickbookmark ?title () =
4412 match state.layout with
4413 | [] -> ()
4414 | l :: _ ->
4415 let title =
4416 match title with
4417 | None ->
4418 let sec = Unix.gettimeofday () in
4419 let tm = Unix.localtime sec in
4420 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4421 (l.pageno+1)
4422 tm.Unix.tm_mday
4423 tm.Unix.tm_mon
4424 (tm.Unix.tm_year + 1900)
4425 tm.Unix.tm_hour
4426 tm.Unix.tm_min
4427 | Some title -> title
4429 state.bookmarks <-
4430 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4431 :: state.bookmarks
4434 let doreshape w h =
4435 state.fullscreen <- None;
4436 Wsi.reshape w h;
4439 let setautoscrollspeed step goingdown =
4440 let incr = max 1 ((abs step) / 2) in
4441 let incr = if goingdown then incr else -incr in
4442 let astep = step + incr in
4443 state.autoscroll <- Some astep;
4446 let gotounder = function
4447 | Ulinkgoto (pageno, top) ->
4448 if pageno >= 0
4449 then (
4450 addnav ();
4451 gotopage1 pageno top;
4454 | Ulinkuri s ->
4455 gotouri s
4457 | Uremote (filename, pageno) ->
4458 let path =
4459 if Sys.file_exists filename
4460 then filename
4461 else
4462 let dir = Filename.dirname state.path in
4463 let path = Filename.concat dir filename in
4464 if Sys.file_exists path
4465 then path
4466 else ""
4468 if String.length path > 0
4469 then (
4470 let anchor = getanchor () in
4471 let ranchor = state.path, state.password, anchor in
4472 state.anchor <- (pageno, 0.0);
4473 state.ranchors <- ranchor :: state.ranchors;
4474 opendoc path "";
4476 else showtext '!' ("Could not find " ^ filename)
4478 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4481 let canpan () =
4482 match conf.columns with
4483 | Csplit _ -> true
4484 | _ -> conf.zoom > 1.0
4487 let viewkeyboard key mask =
4488 let enttext te =
4489 let mode = state.mode in
4490 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4491 state.text <- "";
4492 enttext ();
4493 G.postRedisplay "view:enttext"
4495 let ctrl = Wsi.withctrl mask in
4496 match key with
4497 | 81 -> (* Q *)
4498 exit 0
4500 | 0xff63 -> (* insert *)
4501 if conf.angle mod 360 = 0
4502 then (
4503 state.mode <- LinkNav (Ltgendir 0);
4504 gotoy state.y;
4506 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4508 | 0xff1b | 113 -> (* escape / q *)
4509 begin match state.mstate with
4510 | Mzoomrect _ ->
4511 state.mstate <- Mnone;
4512 Wsi.setcursor Wsi.CURSOR_INHERIT;
4513 G.postRedisplay "kill zoom rect";
4514 | _ ->
4515 match state.ranchors with
4516 | [] -> raise Quit
4517 | (path, password, anchor) :: rest ->
4518 state.ranchors <- rest;
4519 state.anchor <- anchor;
4520 opendoc path password
4521 end;
4523 | 0xff08 -> (* backspace *)
4524 let y = getnav ~-1 in
4525 gotoy_and_clear_text y
4527 | 111 -> (* o *)
4528 enteroutlinemode ()
4530 | 117 -> (* u *)
4531 state.rects <- [];
4532 state.text <- "";
4533 G.postRedisplay "dehighlight";
4535 | 47 | 63 -> (* / ? *)
4536 let ondone isforw s =
4537 cbput state.hists.pat s;
4538 state.searchpattern <- s;
4539 search s isforw
4541 let s = String.create 1 in
4542 s.[0] <- Char.chr key;
4543 enttext (s, "", Some (onhist state.hists.pat),
4544 textentry, ondone (key = 47))
4546 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4547 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4548 setzoom (conf.zoom +. incr)
4550 | 43 | 0xffab -> (* + *)
4551 let ondone s =
4552 let n =
4553 try int_of_string s with exc ->
4554 state.text <- Printf.sprintf "bad integer `%s': %s"
4555 s (Printexc.to_string exc);
4556 max_int
4558 if n != max_int
4559 then (
4560 conf.pagebias <- n;
4561 state.text <- "page bias is now " ^ string_of_int n;
4564 enttext ("page bias: ", "", None, intentry, ondone)
4566 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4567 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4568 setzoom (max 0.01 (conf.zoom -. decr))
4570 | 45 | 0xffad -> (* - *)
4571 let ondone msg = state.text <- msg in
4572 enttext (
4573 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4574 optentry state.mode, ondone
4577 | 48 when ctrl -> (* ctrl-0 *)
4578 setzoom 1.0
4580 | 49 when ctrl -> (* 1 *)
4581 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4582 if zoom < 1.0
4583 then setzoom zoom
4585 | 0xffc6 -> (* f9 *)
4586 togglebirdseye ()
4588 | 57 when ctrl -> (* ctrl-9 *)
4589 togglebirdseye ()
4591 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4592 when not ctrl -> (* 0..9 *)
4593 let ondone s =
4594 let n =
4595 try int_of_string s with exc ->
4596 state.text <- Printf.sprintf "bad integer `%s': %s"
4597 s (Printexc.to_string exc);
4600 if n >= 0
4601 then (
4602 addnav ();
4603 cbput state.hists.pag (string_of_int n);
4604 gotopage1 (n + conf.pagebias - 1) 0;
4607 let pageentry text key =
4608 match Char.unsafe_chr key with
4609 | 'g' -> TEdone text
4610 | _ -> intentry text key
4612 let text = "x" in text.[0] <- Char.chr key;
4613 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4615 | 98 -> (* b *)
4616 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4617 reshape conf.winw conf.winh;
4619 | 108 -> (* l *)
4620 conf.hlinks <- not conf.hlinks;
4621 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4622 G.postRedisplay "toggle highlightlinks";
4624 | 70 -> (* F *)
4625 state.glinks <- true;
4626 let a = Char.code 'a' and z = Char.code 'z' in
4627 let ondone s =
4628 let n =
4629 let rec loop pos n = if pos = String.length s then n else
4630 let m = Char.code s.[pos] - a in
4631 loop (pos+1) (n*(z-a+1) + m)
4632 in loop 0 0
4634 if n >= 0
4635 then (
4636 let rec loop n = function
4637 | [] -> ()
4638 | l :: rest ->
4639 match getopaque l.pageno with
4640 | None -> loop n rest
4641 | Some opaque ->
4642 let m = getlinkcount opaque in
4643 if n < m
4644 then (
4645 let under = getlink opaque n in
4646 addnav ();
4647 gotounder under;
4649 else loop (n-m) rest
4651 loop n state.layout;
4654 let onkey text key =
4655 if (key >= a && key <= z)
4656 then
4657 let c = Char.unsafe_chr key in
4658 let text = addchar text c in
4659 TEcont text
4660 else
4661 TEcont text
4663 let mode = state.mode in
4664 state.mode <- Textentry (
4665 (":", "", Some (onhist state.hists.pag), onkey, ondone),
4666 fun _ ->
4667 state.glinks <- false;
4668 state.mode <- mode
4670 state.text <- "";
4671 G.postRedisplay "view:enttext"
4673 | 97 -> (* a *)
4674 begin match state.autoscroll with
4675 | Some step ->
4676 conf.autoscrollstep <- step;
4677 state.autoscroll <- None
4678 | None ->
4679 if conf.autoscrollstep = 0
4680 then state.autoscroll <- Some 1
4681 else state.autoscroll <- Some conf.autoscrollstep
4684 | 112 when ctrl -> (* ctrl-p *)
4685 launchpath ()
4687 | 80 -> (* P *)
4688 conf.presentation <- not conf.presentation;
4689 if conf.presentation
4690 then (
4691 if not conf.scrollbarinpm
4692 then state.scrollw <- 0;
4694 else
4695 state.scrollw <- conf.scrollbw;
4697 showtext ' ' ("presentation mode " ^
4698 if conf.presentation then "on" else "off");
4699 state.anchor <- getanchor ();
4700 represent ()
4702 | 102 -> (* f *)
4703 begin match state.fullscreen with
4704 | None ->
4705 state.fullscreen <- Some (conf.winw, conf.winh);
4706 Wsi.fullscreen ()
4707 | Some (w, h) ->
4708 state.fullscreen <- None;
4709 doreshape w h
4712 | 103 -> (* g *)
4713 gotoy_and_clear_text 0
4715 | 71 -> (* G *)
4716 gotopage1 (state.pagecount - 1) 0
4718 | 112 | 78 -> (* p|N *)
4719 search state.searchpattern false
4721 | 110 | 0xffc0 -> (* n|F3 *)
4722 search state.searchpattern true
4724 | 116 -> (* t *)
4725 begin match state.layout with
4726 | [] -> ()
4727 | l :: _ ->
4728 gotoy_and_clear_text (getpagey l.pageno)
4731 | 32 -> (* ' ' *)
4732 begin match List.rev state.layout with
4733 | [] -> ()
4734 | l :: _ ->
4735 let pageno = min (l.pageno+1) (state.pagecount-1) in
4736 gotoy_and_clear_text (getpagey pageno)
4739 | 0xff9f | 0xffff -> (* delete *)
4740 begin match state.layout with
4741 | [] -> ()
4742 | l :: _ ->
4743 let pageno = max 0 (l.pageno-1) in
4744 gotoy_and_clear_text (getpagey pageno)
4747 | 61 -> (* = *)
4748 showtext ' ' (describe_location ());
4750 | 119 -> (* w *)
4751 begin match state.layout with
4752 | [] -> ()
4753 | l :: _ ->
4754 doreshape (l.pagew + state.scrollw) l.pageh;
4755 G.postRedisplay "w"
4758 | 39 -> (* ' *)
4759 enterbookmarkmode ()
4761 | 104 | 0xffbe -> (* h|F1 *)
4762 enterhelpmode ()
4764 | 105 -> (* i *)
4765 enterinfomode ()
4767 | 101 when conf.redirectstderr -> (* e *)
4768 entermsgsmode ()
4770 | 109 -> (* m *)
4771 let ondone s =
4772 match state.layout with
4773 | l :: _ ->
4774 state.bookmarks <-
4775 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4776 :: state.bookmarks
4777 | _ -> ()
4779 enttext ("bookmark: ", "", None, textentry, ondone)
4781 | 126 -> (* ~ *)
4782 quickbookmark ();
4783 showtext ' ' "Quick bookmark added";
4785 | 122 -> (* z *)
4786 begin match state.layout with
4787 | l :: _ ->
4788 let rect = getpdimrect l.pagedimno in
4789 let w, h =
4790 if conf.crophack
4791 then
4792 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4793 truncate (1.2 *. (rect.(3) -. rect.(0))))
4794 else
4795 (truncate (rect.(1) -. rect.(0)),
4796 truncate (rect.(3) -. rect.(0)))
4798 let w = truncate ((float w)*.conf.zoom)
4799 and h = truncate ((float h)*.conf.zoom) in
4800 if w != 0 && h != 0
4801 then (
4802 state.anchor <- getanchor ();
4803 doreshape (w + state.scrollw) (h + conf.interpagespace)
4805 G.postRedisplay "z";
4807 | [] -> ()
4810 | 50 when ctrl -> (* ctrl-2 *)
4811 let maxw = getmaxw () in
4812 if maxw > 0.0
4813 then setzoom (maxw /. float conf.winw)
4815 | 60 | 62 -> (* < > *)
4816 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4818 | 91 | 93 -> (* [ ] *)
4819 conf.colorscale <-
4820 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4822 G.postRedisplay "brightness";
4824 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4825 setzoom state.prevzoom
4827 | 107 | 0xff52 -> (* k up *)
4828 begin match state.autoscroll with
4829 | None ->
4830 begin match state.mode with
4831 | Birdseye beye -> upbirdseye 1 beye
4832 | _ ->
4833 if ctrl
4834 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4835 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4837 | Some n ->
4838 setautoscrollspeed n false
4841 | 106 | 0xff54 -> (* j down *)
4842 begin match state.autoscroll with
4843 | None ->
4844 begin match state.mode with
4845 | Birdseye beye -> downbirdseye 1 beye
4846 | _ ->
4847 if ctrl
4848 then gotoy_and_clear_text (clamp (conf.winh/2))
4849 else gotoy_and_clear_text (clamp conf.scrollstep)
4851 | Some n ->
4852 setautoscrollspeed n true
4855 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4856 if canpan ()
4857 then
4858 let dx =
4859 if ctrl
4860 then conf.winw / 2
4861 else 10
4863 let dx = if key = 0xff51 then dx else -dx in
4864 state.x <- state.x + dx;
4865 gotoy_and_clear_text state.y
4866 else (
4867 state.text <- "";
4868 G.postRedisplay "lef/right"
4871 | 0xff55 -> (* prior *)
4872 let y =
4873 if ctrl
4874 then
4875 match state.layout with
4876 | [] -> state.y
4877 | l :: _ -> state.y - l.pagey
4878 else
4879 clamp (-conf.winh)
4881 gotoghyll y
4883 | 0xff56 -> (* next *)
4884 let y =
4885 if ctrl
4886 then
4887 match List.rev state.layout with
4888 | [] -> state.y
4889 | l :: _ -> getpagey l.pageno
4890 else
4891 clamp conf.winh
4893 gotoghyll y
4895 | 0xff50 -> gotoghyll 0
4896 | 0xff57 -> gotoghyll (clamp state.maxy)
4897 | 0xff53 when Wsi.withalt mask ->
4898 gotoghyll (getnav ~-1)
4899 | 0xff51 when Wsi.withalt mask ->
4900 gotoghyll (getnav 1)
4902 | 114 -> (* r *)
4903 state.anchor <- getanchor ();
4904 opendoc state.path state.password
4906 | 118 when conf.debug -> (* v *)
4907 state.rects <- [];
4908 List.iter (fun l ->
4909 match getopaque l.pageno with
4910 | None -> ()
4911 | Some opaque ->
4912 let x0, y0, x1, y1 = pagebbox opaque in
4913 let a,b = float x0, float y0 in
4914 let c,d = float x1, float y0 in
4915 let e,f = float x1, float y1 in
4916 let h,j = float x0, float y1 in
4917 let rect = (a,b,c,d,e,f,h,j) in
4918 debugrect rect;
4919 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4920 ) state.layout;
4921 G.postRedisplay "v";
4923 | _ ->
4924 vlog "huh? %s" (Wsi.keyname key)
4927 let linknavkeyboard key mask linknav =
4928 let getpage pageno =
4929 let rec loop = function
4930 | [] -> None
4931 | l :: _ when l.pageno = pageno -> Some l
4932 | _ :: rest -> loop rest
4933 in loop state.layout
4935 let doexact (pageno, n) =
4936 match getopaque pageno, getpage pageno with
4937 | Some opaque, Some l ->
4938 if key = 0xff0d
4939 then
4940 let under = getlink opaque n in
4941 G.postRedisplay "link gotounder";
4942 gotounder under;
4943 state.mode <- View;
4944 else
4945 let opt, dir =
4946 match key with
4947 | 0xff50 -> (* home *)
4948 Some (findlink opaque LDfirst), -1
4950 | 0xff57 -> (* end *)
4951 Some (findlink opaque LDlast), 1
4953 | 0xff51 -> (* left *)
4954 Some (findlink opaque (LDleft n)), -1
4956 | 0xff53 -> (* right *)
4957 Some (findlink opaque (LDright n)), 1
4959 | 0xff52 -> (* up *)
4960 Some (findlink opaque (LDup n)), -1
4962 | 0xff54 -> (* down *)
4963 Some (findlink opaque (LDdown n)), 1
4965 | _ -> None, 0
4967 let pwl l dir =
4968 begin match findpwl l.pageno dir with
4969 | Pwlnotfound -> ()
4970 | Pwl pageno ->
4971 let notfound dir =
4972 state.mode <- LinkNav (Ltgendir dir);
4973 let y, h = getpageyh pageno in
4974 let y =
4975 if dir < 0
4976 then y + h - conf.winh
4977 else y
4979 gotoy y
4981 begin match getopaque pageno, getpage pageno with
4982 | Some opaque, Some _ ->
4983 let link =
4984 let ld = if dir > 0 then LDfirst else LDlast in
4985 findlink opaque ld
4987 begin match link with
4988 | Lfound m ->
4989 showlinktype (getlink opaque m);
4990 state.mode <- LinkNav (Ltexact (pageno, m));
4991 G.postRedisplay "linknav jpage";
4992 | _ -> notfound dir
4993 end;
4994 | _ -> notfound dir
4995 end;
4996 end;
4998 begin match opt with
4999 | Some Lnotfound -> pwl l dir;
5000 | Some (Lfound m) ->
5001 if m = n
5002 then pwl l dir
5003 else (
5004 let _, y0, _, y1 = getlinkrect opaque m in
5005 if y0 < l.pagey
5006 then gotopage1 l.pageno y0
5007 else (
5008 let d = fstate.fontsize + 1 in
5009 if y1 - l.pagey > l.pagevh - d
5010 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5011 else G.postRedisplay "linknav";
5013 showlinktype (getlink opaque m);
5014 state.mode <- LinkNav (Ltexact (l.pageno, m));
5017 | None -> viewkeyboard key mask
5018 end;
5019 | _ -> viewkeyboard key mask
5021 if key = 0xff63
5022 then (
5023 state.mode <- View;
5024 G.postRedisplay "leave linknav"
5026 else
5027 match linknav with
5028 | Ltgendir _ -> viewkeyboard key mask
5029 | Ltexact exact -> doexact exact
5032 let keyboard key mask =
5033 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5034 then wcmd "interrupt"
5035 else state.uioh <- state.uioh#key key mask
5038 let birdseyekeyboard key mask
5039 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5040 let incr =
5041 match conf.columns with
5042 | Csingle -> 1
5043 | Cmulti ((c, _, _), _) -> c
5044 | Csplit _ -> failwith "bird's eye split mode"
5046 match key with
5047 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5048 let y, h = getpageyh pageno in
5049 let top = (conf.winh - h) / 2 in
5050 gotoy (max 0 (y - top))
5051 | 0xff0d -> leavebirdseye beye false
5052 | 0xff1b -> leavebirdseye beye true (* escape *)
5053 | 0xff52 -> upbirdseye incr beye (* prior *)
5054 | 0xff54 -> downbirdseye incr beye (* next *)
5055 | 0xff51 -> upbirdseye 1 beye (* up *)
5056 | 0xff53 -> downbirdseye 1 beye (* down *)
5058 | 0xff55 ->
5059 begin match state.layout with
5060 | l :: _ ->
5061 if l.pagey != 0
5062 then (
5063 state.mode <- Birdseye (
5064 oconf, leftx, l.pageno, hooverpageno, anchor
5066 gotopage1 l.pageno 0;
5068 else (
5069 let layout = layout (state.y-conf.winh) conf.winh in
5070 match layout with
5071 | [] -> gotoy (clamp (-conf.winh))
5072 | l :: _ ->
5073 state.mode <- Birdseye (
5074 oconf, leftx, l.pageno, hooverpageno, anchor
5076 gotopage1 l.pageno 0
5079 | [] -> gotoy (clamp (-conf.winh))
5080 end;
5082 | 0xff56 ->
5083 begin match List.rev state.layout with
5084 | l :: _ ->
5085 let layout = layout (state.y + conf.winh) conf.winh in
5086 begin match layout with
5087 | [] ->
5088 let incr = l.pageh - l.pagevh in
5089 if incr = 0
5090 then (
5091 state.mode <-
5092 Birdseye (
5093 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5095 G.postRedisplay "birdseye pagedown";
5097 else gotoy (clamp (incr + conf.interpagespace*2));
5099 | l :: _ ->
5100 state.mode <-
5101 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5102 gotopage1 l.pageno 0;
5105 | [] -> gotoy (clamp conf.winh)
5106 end;
5108 | 0xff50 ->
5109 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5110 gotopage1 0 0
5112 | 0xff57 ->
5113 let pageno = state.pagecount - 1 in
5114 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5115 if not (pagevisible state.layout pageno)
5116 then
5117 let h =
5118 match List.rev state.pdims with
5119 | [] -> conf.winh
5120 | (_, _, h, _) :: _ -> h
5122 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5123 else G.postRedisplay "birdseye end";
5124 | _ -> viewkeyboard key mask
5127 let drawpage l linkindexbase =
5128 let color =
5129 match state.mode with
5130 | Textentry _ -> scalecolor 0.4
5131 | LinkNav _
5132 | View -> scalecolor 1.0
5133 | Birdseye (_, _, pageno, hooverpageno, _) ->
5134 if l.pageno = hooverpageno
5135 then scalecolor 0.9
5136 else (
5137 if l.pageno = pageno
5138 then scalecolor 1.0
5139 else scalecolor 0.8
5142 drawtiles l color;
5143 begin match getopaque l.pageno with
5144 | Some opaque ->
5145 if tileready l l.pagex l.pagey
5146 then
5147 let x = l.pagedispx - l.pagex
5148 and y = l.pagedispy - l.pagey in
5149 let hlmask = (if conf.hlinks then 1 else 0)
5150 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5152 postprocess opaque hlmask x y linkindexbase;
5153 else 0
5155 | _ -> 0
5156 end;
5159 let scrollindicator () =
5160 let sbw, ph, sh = state.uioh#scrollph in
5161 let sbh, pw, sw = state.uioh#scrollpw in
5163 GlDraw.color (0.64, 0.64, 0.64);
5164 GlDraw.rect
5165 (float (conf.winw - sbw), 0.)
5166 (float conf.winw, float conf.winh)
5168 GlDraw.rect
5169 (0., float (conf.winh - sbh))
5170 (float (conf.winw - state.scrollw - 1), float conf.winh)
5172 GlDraw.color (0.0, 0.0, 0.0);
5174 GlDraw.rect
5175 (float (conf.winw - sbw), ph)
5176 (float conf.winw, ph +. sh)
5178 GlDraw.rect
5179 (pw, float (conf.winh - sbh))
5180 (pw +. sw, float conf.winh)
5184 let showsel () =
5185 match state.mstate with
5186 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5189 | Msel ((x0, y0), (x1, y1)) ->
5190 let rec loop = function
5191 | l :: ls ->
5192 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5193 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5194 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5195 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5196 then
5197 match getopaque l.pageno with
5198 | Some opaque ->
5199 let x0, y0 = pagetranslatepoint l x0 y0 in
5200 let x1, y1 = pagetranslatepoint l x1 y1 in
5201 seltext opaque (x0, y0, x1, y1);
5202 | _ -> ()
5203 else loop ls
5204 | [] -> ()
5206 loop state.layout
5209 let showrects rects =
5210 Gl.enable `blend;
5211 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5212 GlDraw.polygon_mode `both `fill;
5213 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5214 List.iter
5215 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5216 List.iter (fun l ->
5217 if l.pageno = pageno
5218 then (
5219 let dx = float (l.pagedispx - l.pagex) in
5220 let dy = float (l.pagedispy - l.pagey) in
5221 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5222 GlDraw.begins `quads;
5224 GlDraw.vertex2 (x0+.dx, y0+.dy);
5225 GlDraw.vertex2 (x1+.dx, y1+.dy);
5226 GlDraw.vertex2 (x2+.dx, y2+.dy);
5227 GlDraw.vertex2 (x3+.dx, y3+.dy);
5229 GlDraw.ends ();
5231 ) state.layout
5232 ) rects
5234 Gl.disable `blend;
5237 let display () =
5238 GlClear.color (scalecolor2 conf.bgcolor);
5239 GlClear.clear [`color];
5240 let rec loop linkindexbase = function
5241 | l :: rest ->
5242 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5243 loop linkindexbase rest
5244 | [] -> ()
5246 loop 0 state.layout;
5247 let rects =
5248 match state.mode with
5249 | LinkNav (Ltexact (pageno, linkno)) ->
5250 begin match getopaque pageno with
5251 | Some opaque ->
5252 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5253 (pageno, 5, (
5254 float x0, float y0,
5255 float x1, float y0,
5256 float x1, float y1,
5257 float x0, float y1)
5258 ) :: state.rects
5259 | None -> state.rects
5261 | _ -> state.rects
5263 showrects rects;
5264 showsel ();
5265 state.uioh#display;
5266 begin match state.mstate with
5267 | Mzoomrect ((x0, y0), (x1, y1)) ->
5268 Gl.enable `blend;
5269 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5270 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5271 GlDraw.rect (float x0, float y0)
5272 (float x1, float y1);
5273 Gl.disable `blend;
5274 | _ -> ()
5275 end;
5276 enttext ();
5277 scrollindicator ();
5278 Wsi.swapb ();
5281 let zoomrect x y x1 y1 =
5282 let x0 = min x x1
5283 and x1 = max x x1
5284 and y0 = min y y1 in
5285 gotoy (state.y + y0);
5286 state.anchor <- getanchor ();
5287 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5288 let margin =
5289 if state.w < conf.winw - state.scrollw
5290 then (conf.winw - state.scrollw - state.w) / 2
5291 else 0
5293 state.x <- (state.x + margin) - x0;
5294 setzoom zoom;
5295 Wsi.setcursor Wsi.CURSOR_INHERIT;
5296 state.mstate <- Mnone;
5299 let scrollx x =
5300 let winw = conf.winw - state.scrollw - 1 in
5301 let s = float x /. float winw in
5302 let destx = truncate (float (state.w + winw) *. s) in
5303 state.x <- winw - destx;
5304 gotoy_and_clear_text state.y;
5305 state.mstate <- Mscrollx;
5308 let scrolly y =
5309 let s = float y /. float conf.winh in
5310 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5311 gotoy_and_clear_text desty;
5312 state.mstate <- Mscrolly;
5315 let viewmouse button down x y mask =
5316 match button with
5317 | n when (n == 4 || n == 5) && not down ->
5318 if Wsi.withctrl mask
5319 then (
5320 match state.mstate with
5321 | Mzoom (oldn, i) ->
5322 if oldn = n
5323 then (
5324 if i = 2
5325 then
5326 let incr =
5327 match n with
5328 | 5 ->
5329 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5330 | _ ->
5331 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5333 let zoom = conf.zoom -. incr in
5334 setzoom zoom;
5335 state.mstate <- Mzoom (n, 0);
5336 else
5337 state.mstate <- Mzoom (n, i+1);
5339 else state.mstate <- Mzoom (n, 0)
5341 | _ -> state.mstate <- Mzoom (n, 0)
5343 else (
5344 match state.autoscroll with
5345 | Some step -> setautoscrollspeed step (n=4)
5346 | None ->
5347 let incr =
5348 if n = 4
5349 then -conf.scrollstep
5350 else conf.scrollstep
5352 let incr = incr * 2 in
5353 let y = clamp incr in
5354 gotoy_and_clear_text y
5357 | 1 when Wsi.withctrl mask ->
5358 if down
5359 then (
5360 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5361 state.mstate <- Mpan (x, y)
5363 else
5364 state.mstate <- Mnone
5366 | 3 ->
5367 if down
5368 then (
5369 Wsi.setcursor Wsi.CURSOR_CYCLE;
5370 let p = (x, y) in
5371 state.mstate <- Mzoomrect (p, p)
5373 else (
5374 match state.mstate with
5375 | Mzoomrect ((x0, y0), _) ->
5376 if abs (x-x0) > 10 && abs (y - y0) > 10
5377 then zoomrect x0 y0 x y
5378 else (
5379 state.mstate <- Mnone;
5380 Wsi.setcursor Wsi.CURSOR_INHERIT;
5381 G.postRedisplay "kill accidental zoom rect";
5383 | _ ->
5384 Wsi.setcursor Wsi.CURSOR_INHERIT;
5385 state.mstate <- Mnone
5388 | 1 when x > conf.winw - state.scrollw ->
5389 if down
5390 then
5391 let _, position, sh = state.uioh#scrollph in
5392 if y > truncate position && y < truncate (position +. sh)
5393 then state.mstate <- Mscrolly
5394 else scrolly y
5395 else
5396 state.mstate <- Mnone
5398 | 1 when y > conf.winh - state.hscrollh ->
5399 if down
5400 then
5401 let _, position, sw = state.uioh#scrollpw in
5402 if x > truncate position && x < truncate (position +. sw)
5403 then state.mstate <- Mscrollx
5404 else scrollx x
5405 else
5406 state.mstate <- Mnone
5408 | 1 ->
5409 let dest = if down then getunder x y else Unone in
5410 begin match dest with
5411 | Ulinkgoto _
5412 | Ulinkuri _
5413 | Uremote _
5414 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5415 gotounder dest
5417 | Unone when down ->
5418 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5419 state.mstate <- Mpan (x, y);
5421 | Unone | Utext _ ->
5422 if down
5423 then (
5424 if conf.angle mod 360 = 0
5425 then (
5426 state.mstate <- Msel ((x, y), (x, y));
5427 G.postRedisplay "mouse select";
5430 else (
5431 match state.mstate with
5432 | Mnone -> ()
5434 | Mzoom _ | Mscrollx | Mscrolly ->
5435 state.mstate <- Mnone
5437 | Mzoomrect ((x0, y0), _) ->
5438 zoomrect x0 y0 x y
5440 | Mpan _ ->
5441 Wsi.setcursor Wsi.CURSOR_INHERIT;
5442 state.mstate <- Mnone
5444 | Msel ((_, y0), (_, y1)) ->
5445 let rec loop = function
5446 | [] -> ()
5447 | l :: rest ->
5448 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5449 || ((y1 >= l.pagedispy
5450 && y1 <= (l.pagedispy + l.pagevh)))
5451 then
5452 match getopaque l.pageno with
5453 | Some opaque ->
5454 let r, w = Unix.pipe () in
5455 popen conf.selcmd [r, 0; w, -1];
5456 copysel w opaque;
5457 Unix.close r;
5458 G.postRedisplay "copysel"
5459 | _ -> ()
5460 else loop rest
5462 loop state.layout;
5463 Wsi.setcursor Wsi.CURSOR_INHERIT;
5464 state.mstate <- Mnone;
5468 | _ -> ()
5471 let birdseyemouse button down x y mask
5472 (conf, leftx, _, hooverpageno, anchor) =
5473 match button with
5474 | 1 when down ->
5475 let rec loop = function
5476 | [] -> ()
5477 | l :: rest ->
5478 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5479 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5480 then (
5481 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5483 else loop rest
5485 loop state.layout
5486 | 3 -> ()
5487 | _ -> viewmouse button down x y mask
5490 let mouse button down x y mask =
5491 state.uioh <- state.uioh#button button down x y mask;
5494 let motion ~x ~y =
5495 state.uioh <- state.uioh#motion x y
5498 let pmotion ~x ~y =
5499 state.uioh <- state.uioh#pmotion x y;
5502 let uioh = object
5503 method display = ()
5505 method key key mask =
5506 begin match state.mode with
5507 | Textentry textentry -> textentrykeyboard key mask textentry
5508 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5509 | View -> viewkeyboard key mask
5510 | LinkNav linknav -> linknavkeyboard key mask linknav
5511 end;
5512 state.uioh
5514 method button button bstate x y mask =
5515 begin match state.mode with
5516 | LinkNav _
5517 | View -> viewmouse button bstate x y mask
5518 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5519 | Textentry _ -> ()
5520 end;
5521 state.uioh
5523 method motion x y =
5524 begin match state.mode with
5525 | Textentry _ -> ()
5526 | View | Birdseye _ | LinkNav _ ->
5527 match state.mstate with
5528 | Mzoom _ | Mnone -> ()
5530 | Mpan (x0, y0) ->
5531 let dx = x - x0
5532 and dy = y0 - y in
5533 state.mstate <- Mpan (x, y);
5534 if canpan ()
5535 then state.x <- state.x + dx;
5536 let y = clamp dy in
5537 gotoy_and_clear_text y
5539 | Msel (a, _) ->
5540 state.mstate <- Msel (a, (x, y));
5541 G.postRedisplay "motion select";
5543 | Mscrolly ->
5544 let y = min conf.winh (max 0 y) in
5545 scrolly y
5547 | Mscrollx ->
5548 let x = min conf.winw (max 0 x) in
5549 scrollx x
5551 | Mzoomrect (p0, _) ->
5552 state.mstate <- Mzoomrect (p0, (x, y));
5553 G.postRedisplay "motion zoomrect";
5554 end;
5555 state.uioh
5557 method pmotion x y =
5558 begin match state.mode with
5559 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5560 let rec loop = function
5561 | [] ->
5562 if hooverpageno != -1
5563 then (
5564 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5565 G.postRedisplay "pmotion birdseye no hoover";
5567 | l :: rest ->
5568 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5569 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5570 then (
5571 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5572 G.postRedisplay "pmotion birdseye hoover";
5574 else loop rest
5576 loop state.layout
5578 | Textentry _ -> ()
5580 | LinkNav _
5581 | View ->
5582 match state.mstate with
5583 | Mnone -> updateunder x y
5584 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5586 end;
5587 state.uioh
5589 method infochanged _ = ()
5591 method scrollph =
5592 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5593 let p, h = scrollph state.y maxy in
5594 state.scrollw, p, h
5596 method scrollpw =
5597 let winw = conf.winw - state.scrollw - 1 in
5598 let fwinw = float winw in
5599 let sw =
5600 let sw = fwinw /. float state.w in
5601 let sw = fwinw *. sw in
5602 max sw (float conf.scrollh)
5604 let position, sw =
5605 let f = state.w+winw in
5606 let r = float (winw-state.x) /. float f in
5607 let p = fwinw *. r in
5608 p-.sw/.2., sw
5610 let sw =
5611 if position +. sw > fwinw
5612 then fwinw -. position
5613 else sw
5615 state.hscrollh, position, sw
5617 method modehash =
5618 let modename =
5619 match state.mode with
5620 | LinkNav _ -> "links"
5621 | Textentry _ -> "textentry"
5622 | Birdseye _ -> "birdseye"
5623 | View -> "global"
5625 findkeyhash conf modename
5626 end;;
5628 module Config =
5629 struct
5630 open Parser
5632 let fontpath = ref "";;
5634 module KeyMap =
5635 Map.Make (struct type t = (int * int) let compare = compare end);;
5637 let unent s =
5638 let l = String.length s in
5639 let b = Buffer.create l in
5640 unent b s 0 l;
5641 Buffer.contents b;
5644 let home =
5645 try Sys.getenv "HOME"
5646 with exn ->
5647 prerr_endline
5648 ("Can not determine home directory location: " ^
5649 Printexc.to_string exn);
5653 let modifier_of_string = function
5654 | "alt" -> Wsi.altmask
5655 | "shift" -> Wsi.shiftmask
5656 | "ctrl" | "control" -> Wsi.ctrlmask
5657 | "meta" -> Wsi.metamask
5658 | _ -> 0
5661 let key_of_string =
5662 let r = Str.regexp "-" in
5663 fun s ->
5664 let elems = Str.full_split r s in
5665 let f n k m =
5666 let g s =
5667 let m1 = modifier_of_string s in
5668 if m1 = 0
5669 then (Wsi.namekey s, m)
5670 else (k, m lor m1)
5671 in function
5672 | Str.Delim s when n land 1 = 0 -> g s
5673 | Str.Text s -> g s
5674 | Str.Delim _ -> (k, m)
5676 let rec loop n k m = function
5677 | [] -> (k, m)
5678 | x :: xs ->
5679 let k, m = f n k m x in
5680 loop (n+1) k m xs
5682 loop 0 0 0 elems
5685 let keys_of_string =
5686 let r = Str.regexp "[ \t]" in
5687 fun s ->
5688 let elems = Str.split r s in
5689 List.map key_of_string elems
5692 let copykeyhashes c =
5693 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5696 let config_of c attrs =
5697 let apply c k v =
5699 match k with
5700 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5701 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5702 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5703 | "preload" -> { c with preload = bool_of_string v }
5704 | "page-bias" -> { c with pagebias = int_of_string v }
5705 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5706 | "auto-scroll-step" ->
5707 { c with autoscrollstep = max 0 (int_of_string v) }
5708 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5709 | "crop-hack" -> { c with crophack = bool_of_string v }
5710 | "throttle" ->
5711 let mw =
5712 match String.lowercase v with
5713 | "true" -> Some infinity
5714 | "false" -> None
5715 | f -> Some (float_of_string f)
5717 { c with maxwait = mw}
5718 | "highlight-links" -> { c with hlinks = bool_of_string v }
5719 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5720 | "vertical-margin" ->
5721 { c with interpagespace = max 0 (int_of_string v) }
5722 | "zoom" ->
5723 let zoom = float_of_string v /. 100. in
5724 let zoom = max zoom 0.0 in
5725 { c with zoom = zoom }
5726 | "presentation" -> { c with presentation = bool_of_string v }
5727 | "rotation-angle" -> { c with angle = int_of_string v }
5728 | "width" -> { c with winw = max 20 (int_of_string v) }
5729 | "height" -> { c with winh = max 20 (int_of_string v) }
5730 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5731 | "proportional-display" -> { c with proportional = bool_of_string v }
5732 | "pixmap-cache-size" ->
5733 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5734 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5735 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5736 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5737 | "persistent-location" -> { c with jumpback = bool_of_string v }
5738 | "background-color" -> { c with bgcolor = color_of_string v }
5739 | "scrollbar-in-presentation" ->
5740 { c with scrollbarinpm = bool_of_string v }
5741 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5742 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5743 | "mupdf-store-size" ->
5744 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5745 | "checkers" -> { c with checkers = bool_of_string v }
5746 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5747 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5748 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5749 | "uri-launcher" -> { c with urilauncher = unent v }
5750 | "path-launcher" -> { c with pathlauncher = unent v }
5751 | "color-space" -> { c with colorspace = colorspace_of_string v }
5752 | "invert-colors" -> { c with invert = bool_of_string v }
5753 | "brightness" -> { c with colorscale = float_of_string v }
5754 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5755 | "ghyllscroll" ->
5756 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5757 | "columns" ->
5758 let (n, _, _) as nab = multicolumns_of_string v in
5759 if n < 0
5760 then { c with columns = Csplit (-n, [||]) }
5761 else { c with columns = Cmulti (nab, [||]) }
5762 | "birds-eye-columns" ->
5763 { c with beyecolumns = Some (max (int_of_string v) 2) }
5764 | "selection-command" -> { c with selcmd = unent v }
5765 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5766 | _ -> c
5767 with exn ->
5768 prerr_endline ("Error processing attribute (`" ^
5769 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5772 let rec fold c = function
5773 | [] -> c
5774 | (k, v) :: rest ->
5775 let c = apply c k v in
5776 fold c rest
5778 fold { c with keyhashes = copykeyhashes c } attrs;
5781 let fromstring f pos n v d =
5782 try f v
5783 with exn ->
5784 dolog "Error processing attribute (%S=%S) at %d\n%s"
5785 n v pos (Printexc.to_string exn)
5790 let bookmark_of attrs =
5791 let rec fold title page rely = function
5792 | ("title", v) :: rest -> fold v page rely rest
5793 | ("page", v) :: rest -> fold title v rely rest
5794 | ("rely", v) :: rest -> fold title page v rest
5795 | _ :: rest -> fold title page rely rest
5796 | [] -> title, page, rely
5798 fold "invalid" "0" "0" attrs
5801 let doc_of attrs =
5802 let rec fold path page rely pan = function
5803 | ("path", v) :: rest -> fold v page rely pan rest
5804 | ("page", v) :: rest -> fold path v rely pan rest
5805 | ("rely", v) :: rest -> fold path page v pan rest
5806 | ("pan", v) :: rest -> fold path page rely v rest
5807 | _ :: rest -> fold path page rely pan rest
5808 | [] -> path, page, rely, pan
5810 fold "" "0" "0" "0" attrs
5813 let map_of attrs =
5814 let rec fold rs ls = function
5815 | ("out", v) :: rest -> fold v ls rest
5816 | ("in", v) :: rest -> fold rs v rest
5817 | _ :: rest -> fold ls rs rest
5818 | [] -> ls, rs
5820 fold "" "" attrs
5823 let setconf dst src =
5824 dst.scrollbw <- src.scrollbw;
5825 dst.scrollh <- src.scrollh;
5826 dst.icase <- src.icase;
5827 dst.preload <- src.preload;
5828 dst.pagebias <- src.pagebias;
5829 dst.verbose <- src.verbose;
5830 dst.scrollstep <- src.scrollstep;
5831 dst.maxhfit <- src.maxhfit;
5832 dst.crophack <- src.crophack;
5833 dst.autoscrollstep <- src.autoscrollstep;
5834 dst.maxwait <- src.maxwait;
5835 dst.hlinks <- src.hlinks;
5836 dst.underinfo <- src.underinfo;
5837 dst.interpagespace <- src.interpagespace;
5838 dst.zoom <- src.zoom;
5839 dst.presentation <- src.presentation;
5840 dst.angle <- src.angle;
5841 dst.winw <- src.winw;
5842 dst.winh <- src.winh;
5843 dst.savebmarks <- src.savebmarks;
5844 dst.memlimit <- src.memlimit;
5845 dst.proportional <- src.proportional;
5846 dst.texcount <- src.texcount;
5847 dst.sliceheight <- src.sliceheight;
5848 dst.thumbw <- src.thumbw;
5849 dst.jumpback <- src.jumpback;
5850 dst.bgcolor <- src.bgcolor;
5851 dst.scrollbarinpm <- src.scrollbarinpm;
5852 dst.tilew <- src.tilew;
5853 dst.tileh <- src.tileh;
5854 dst.mustoresize <- src.mustoresize;
5855 dst.checkers <- src.checkers;
5856 dst.aalevel <- src.aalevel;
5857 dst.trimmargins <- src.trimmargins;
5858 dst.trimfuzz <- src.trimfuzz;
5859 dst.urilauncher <- src.urilauncher;
5860 dst.colorspace <- src.colorspace;
5861 dst.invert <- src.invert;
5862 dst.colorscale <- src.colorscale;
5863 dst.redirectstderr <- src.redirectstderr;
5864 dst.ghyllscroll <- src.ghyllscroll;
5865 dst.columns <- src.columns;
5866 dst.beyecolumns <- src.beyecolumns;
5867 dst.selcmd <- src.selcmd;
5868 dst.updatecurs <- src.updatecurs;
5869 dst.pathlauncher <- src.pathlauncher;
5870 dst.keyhashes <- copykeyhashes src;
5873 let get s =
5874 let h = Hashtbl.create 10 in
5875 let dc = { defconf with angle = defconf.angle } in
5876 let rec toplevel v t spos _ =
5877 match t with
5878 | Vdata | Vcdata | Vend -> v
5879 | Vopen ("llppconfig", _, closed) ->
5880 if closed
5881 then v
5882 else { v with f = llppconfig }
5883 | Vopen _ ->
5884 error "unexpected subelement at top level" s spos
5885 | Vclose _ -> error "unexpected close at top level" s spos
5887 and llppconfig v t spos _ =
5888 match t with
5889 | Vdata | Vcdata -> v
5890 | Vend -> error "unexpected end of input in llppconfig" s spos
5891 | Vopen ("defaults", attrs, closed) ->
5892 let c = config_of dc attrs in
5893 setconf dc c;
5894 if closed
5895 then v
5896 else { v with f = defaults }
5898 | Vopen ("ui-font", attrs, closed) ->
5899 let rec getsize size = function
5900 | [] -> size
5901 | ("size", v) :: rest ->
5902 let size =
5903 fromstring int_of_string spos "size" v fstate.fontsize in
5904 getsize size rest
5905 | l -> getsize size l
5907 fstate.fontsize <- getsize fstate.fontsize attrs;
5908 if closed
5909 then v
5910 else { v with f = uifont (Buffer.create 10) }
5912 | Vopen ("doc", attrs, closed) ->
5913 let pathent, spage, srely, span = doc_of attrs in
5914 let path = unent pathent
5915 and pageno = fromstring int_of_string spos "page" spage 0
5916 and rely = fromstring float_of_string spos "rely" srely 0.0
5917 and pan = fromstring int_of_string spos "pan" span 0 in
5918 let c = config_of dc attrs in
5919 let anchor = (pageno, rely) in
5920 if closed
5921 then (Hashtbl.add h path (c, [], pan, anchor); v)
5922 else { v with f = doc path pan anchor c [] }
5924 | Vopen _ ->
5925 error "unexpected subelement in llppconfig" s spos
5927 | Vclose "llppconfig" -> { v with f = toplevel }
5928 | Vclose _ -> error "unexpected close in llppconfig" s spos
5930 and defaults v t spos _ =
5931 match t with
5932 | Vdata | Vcdata -> v
5933 | Vend -> error "unexpected end of input in defaults" s spos
5934 | Vopen ("keymap", attrs, closed) ->
5935 let modename =
5936 try List.assoc "mode" attrs
5937 with Not_found -> "global" in
5938 if closed
5939 then v
5940 else
5941 let ret keymap =
5942 let h = findkeyhash dc modename in
5943 KeyMap.iter (Hashtbl.replace h) keymap;
5944 defaults
5946 { v with f = pkeymap ret KeyMap.empty }
5948 | Vopen (_, _, _) ->
5949 error "unexpected subelement in defaults" s spos
5951 | Vclose "defaults" ->
5952 { v with f = llppconfig }
5954 | Vclose _ -> error "unexpected close in defaults" s spos
5956 and uifont b v t spos epos =
5957 match t with
5958 | Vdata | Vcdata ->
5959 Buffer.add_substring b s spos (epos - spos);
5961 | Vopen (_, _, _) ->
5962 error "unexpected subelement in ui-font" s spos
5963 | Vclose "ui-font" ->
5964 if String.length !fontpath = 0
5965 then fontpath := Buffer.contents b;
5966 { v with f = llppconfig }
5967 | Vclose _ -> error "unexpected close in ui-font" s spos
5968 | Vend -> error "unexpected end of input in ui-font" s spos
5970 and doc path pan anchor c bookmarks v t spos _ =
5971 match t with
5972 | Vdata | Vcdata -> v
5973 | Vend -> error "unexpected end of input in doc" s spos
5974 | Vopen ("bookmarks", _, closed) ->
5975 if closed
5976 then v
5977 else { v with f = pbookmarks path pan anchor c bookmarks }
5979 | Vopen ("keymap", attrs, closed) ->
5980 let modename =
5981 try List.assoc "mode" attrs
5982 with Not_found -> "global"
5984 if closed
5985 then v
5986 else
5987 let ret keymap =
5988 let h = findkeyhash c modename in
5989 KeyMap.iter (Hashtbl.replace h) keymap;
5990 doc path pan anchor c bookmarks
5992 { v with f = pkeymap ret KeyMap.empty }
5994 | Vopen (_, _, _) ->
5995 error "unexpected subelement in doc" s spos
5997 | Vclose "doc" ->
5998 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5999 { v with f = llppconfig }
6001 | Vclose _ -> error "unexpected close in doc" s spos
6003 and pkeymap ret keymap v t spos _ =
6004 match t with
6005 | Vdata | Vcdata -> v
6006 | Vend -> error "unexpected end of input in keymap" s spos
6007 | Vopen ("map", attrs, closed) ->
6008 let r, l = map_of attrs in
6009 let kss = fromstring keys_of_string spos "in" r [] in
6010 let lss = fromstring keys_of_string spos "out" l [] in
6011 let keymap =
6012 match kss with
6013 | [] -> keymap
6014 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6015 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6017 if closed
6018 then { v with f = pkeymap ret keymap }
6019 else
6020 let f () = v in
6021 { v with f = skip "map" f }
6023 | Vopen _ ->
6024 error "unexpected subelement in keymap" s spos
6026 | Vclose "keymap" ->
6027 { v with f = ret keymap }
6029 | Vclose _ -> error "unexpected close in keymap" s spos
6031 and pbookmarks path pan anchor c bookmarks v t spos _ =
6032 match t with
6033 | Vdata | Vcdata -> v
6034 | Vend -> error "unexpected end of input in bookmarks" s spos
6035 | Vopen ("item", attrs, closed) ->
6036 let titleent, spage, srely = bookmark_of attrs in
6037 let page = fromstring int_of_string spos "page" spage 0
6038 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6039 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6040 if closed
6041 then { v with f = pbookmarks path pan anchor c bookmarks }
6042 else
6043 let f () = v in
6044 { v with f = skip "item" f }
6046 | Vopen _ ->
6047 error "unexpected subelement in bookmarks" s spos
6049 | Vclose "bookmarks" ->
6050 { v with f = doc path pan anchor c bookmarks }
6052 | Vclose _ -> error "unexpected close in bookmarks" s spos
6054 and skip tag f v t spos _ =
6055 match t with
6056 | Vdata | Vcdata -> v
6057 | Vend ->
6058 error ("unexpected end of input in skipped " ^ tag) s spos
6059 | Vopen (tag', _, closed) ->
6060 if closed
6061 then v
6062 else
6063 let f' () = { v with f = skip tag f } in
6064 { v with f = skip tag' f' }
6065 | Vclose ctag ->
6066 if tag = ctag
6067 then f ()
6068 else error ("unexpected close in skipped " ^ tag) s spos
6071 parse { f = toplevel; accu = () } s;
6072 h, dc;
6075 let do_load f ic =
6077 let len = in_channel_length ic in
6078 let s = String.create len in
6079 really_input ic s 0 len;
6080 f s;
6081 with
6082 | Parse_error (msg, s, pos) ->
6083 let subs = subs s pos in
6084 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6085 failwith ("parse error: " ^ s)
6087 | exn ->
6088 failwith ("config load error: " ^ Printexc.to_string exn)
6091 let defconfpath =
6092 let dir =
6094 let dir = Filename.concat home ".config" in
6095 if Sys.is_directory dir then dir else home
6096 with _ -> home
6098 Filename.concat dir "llpp.conf"
6101 let confpath = ref defconfpath;;
6103 let load1 f =
6104 if Sys.file_exists !confpath
6105 then
6106 match
6107 (try Some (open_in_bin !confpath)
6108 with exn ->
6109 prerr_endline
6110 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6111 Printexc.to_string exn);
6112 None
6114 with
6115 | Some ic ->
6116 begin try
6117 f (do_load get ic)
6118 with exn ->
6119 prerr_endline
6120 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6121 Printexc.to_string exn);
6122 end;
6123 close_in ic;
6125 | None -> ()
6126 else
6127 f (Hashtbl.create 0, defconf)
6130 let load () =
6131 let f (h, dc) =
6132 let pc, pb, px, pa =
6134 Hashtbl.find h (Filename.basename state.path)
6135 with Not_found -> dc, [], 0, (0, 0.0)
6137 setconf defconf dc;
6138 setconf conf pc;
6139 state.bookmarks <- pb;
6140 state.x <- px;
6141 state.scrollw <- conf.scrollbw;
6142 if conf.jumpback
6143 then state.anchor <- pa;
6144 cbput state.hists.nav pa;
6146 load1 f
6149 let add_attrs bb always dc c =
6150 let ob s a b =
6151 if always || a != b
6152 then Printf.bprintf bb "\n %s='%b'" s a
6153 and oi s a b =
6154 if always || a != b
6155 then Printf.bprintf bb "\n %s='%d'" s a
6156 and oI s a b =
6157 if always || a != b
6158 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6159 and oz s a b =
6160 if always || a <> b
6161 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6162 and oF s a b =
6163 if always || a <> b
6164 then Printf.bprintf bb "\n %s='%f'" s a
6165 and oc s a b =
6166 if always || a <> b
6167 then
6168 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6169 and oC s a b =
6170 if always || a <> b
6171 then
6172 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6173 and oR s a b =
6174 if always || a <> b
6175 then
6176 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6177 and os s a b =
6178 if always || a <> b
6179 then
6180 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6181 and og s a b =
6182 if always || a <> b
6183 then
6184 match a with
6185 | None -> ()
6186 | Some (_N, _A, _B) ->
6187 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6188 and oW s a b =
6189 if always || a <> b
6190 then
6191 let v =
6192 match a with
6193 | None -> "false"
6194 | Some f ->
6195 if f = infinity
6196 then "true"
6197 else string_of_float f
6199 Printf.bprintf bb "\n %s='%s'" s v
6200 and oco s a b =
6201 if always || a <> b
6202 then
6203 match a with
6204 | Cmulti ((n, a, b), _) when n > 1 ->
6205 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6206 | Csplit (n, _) when n > 1 ->
6207 Printf.bprintf bb "\n %s='%d'" s ~-n
6208 | _ -> ()
6209 and obeco s a b =
6210 if always || a <> b
6211 then
6212 match a with
6213 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6214 | _ -> ()
6216 let w, h =
6217 if always
6218 then dc.winw, dc.winh
6219 else
6220 match state.fullscreen with
6221 | Some wh -> wh
6222 | None -> c.winw, c.winh
6224 let zoom, presentation, interpagespace, maxwait =
6225 if always
6226 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6227 else
6228 match state.mode with
6229 | Birdseye (bc, _, _, _, _) ->
6230 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6231 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6233 oi "width" w dc.winw;
6234 oi "height" h dc.winh;
6235 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6236 oi "scroll-handle-height" c.scrollh dc.scrollh;
6237 ob "case-insensitive-search" c.icase dc.icase;
6238 ob "preload" c.preload dc.preload;
6239 oi "page-bias" c.pagebias dc.pagebias;
6240 oi "scroll-step" c.scrollstep dc.scrollstep;
6241 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6242 ob "max-height-fit" c.maxhfit dc.maxhfit;
6243 ob "crop-hack" c.crophack dc.crophack;
6244 oW "throttle" maxwait dc.maxwait;
6245 ob "highlight-links" c.hlinks dc.hlinks;
6246 ob "under-cursor-info" c.underinfo dc.underinfo;
6247 oi "vertical-margin" interpagespace dc.interpagespace;
6248 oz "zoom" zoom dc.zoom;
6249 ob "presentation" presentation dc.presentation;
6250 oi "rotation-angle" c.angle dc.angle;
6251 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6252 ob "proportional-display" c.proportional dc.proportional;
6253 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6254 oi "tex-count" c.texcount dc.texcount;
6255 oi "slice-height" c.sliceheight dc.sliceheight;
6256 oi "thumbnail-width" c.thumbw dc.thumbw;
6257 ob "persistent-location" c.jumpback dc.jumpback;
6258 oc "background-color" c.bgcolor dc.bgcolor;
6259 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6260 oi "tile-width" c.tilew dc.tilew;
6261 oi "tile-height" c.tileh dc.tileh;
6262 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6263 ob "checkers" c.checkers dc.checkers;
6264 oi "aalevel" c.aalevel dc.aalevel;
6265 ob "trim-margins" c.trimmargins dc.trimmargins;
6266 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6267 os "uri-launcher" c.urilauncher dc.urilauncher;
6268 os "path-launcher" c.pathlauncher dc.pathlauncher;
6269 oC "color-space" c.colorspace dc.colorspace;
6270 ob "invert-colors" c.invert dc.invert;
6271 oF "brightness" c.colorscale dc.colorscale;
6272 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6273 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6274 oco "columns" c.columns dc.columns;
6275 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6276 os "selection-command" c.selcmd dc.selcmd;
6277 ob "update-cursor" c.updatecurs dc.updatecurs;
6280 let keymapsbuf always dc c =
6281 let bb = Buffer.create 16 in
6282 let rec loop = function
6283 | [] -> ()
6284 | (modename, h) :: rest ->
6285 let dh = findkeyhash dc modename in
6286 if always || h <> dh
6287 then (
6288 if Hashtbl.length h > 0
6289 then (
6290 if Buffer.length bb > 0
6291 then Buffer.add_char bb '\n';
6292 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6293 Hashtbl.iter (fun i o ->
6294 let isdifferent = always ||
6296 let dO = Hashtbl.find dh i in
6297 dO <> o
6298 with Not_found -> true
6300 if isdifferent
6301 then
6302 let addkm (k, m) =
6303 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6304 if Wsi.withalt m then Buffer.add_string bb "alt-";
6305 if Wsi.withshift m then Buffer.add_string bb "shift-";
6306 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6307 Buffer.add_string bb (Wsi.keyname k);
6309 let addkms l =
6310 let rec loop = function
6311 | [] -> ()
6312 | km :: [] -> addkm km
6313 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6315 loop l
6317 Buffer.add_string bb "<map in='";
6318 addkm i;
6319 match o with
6320 | KMinsrt km ->
6321 Buffer.add_string bb "' out='";
6322 addkm km;
6323 Buffer.add_string bb "'/>\n"
6325 | KMinsrl kms ->
6326 Buffer.add_string bb "' out='";
6327 addkms kms;
6328 Buffer.add_string bb "'/>\n"
6330 | KMmulti (ins, kms) ->
6331 Buffer.add_char bb ' ';
6332 addkms ins;
6333 Buffer.add_string bb "' out='";
6334 addkms kms;
6335 Buffer.add_string bb "'/>\n"
6336 ) h;
6337 Buffer.add_string bb "</keymap>";
6340 loop rest
6342 loop c.keyhashes;
6346 let save () =
6347 let uifontsize = fstate.fontsize in
6348 let bb = Buffer.create 32768 in
6349 let f (h, dc) =
6350 let dc = if conf.bedefault then conf else dc in
6351 Buffer.add_string bb "<llppconfig>\n";
6353 if String.length !fontpath > 0
6354 then
6355 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6356 uifontsize
6357 !fontpath
6358 else (
6359 if uifontsize <> 14
6360 then
6361 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6364 Buffer.add_string bb "<defaults ";
6365 add_attrs bb true dc dc;
6366 let kb = keymapsbuf true dc dc in
6367 if Buffer.length kb > 0
6368 then (
6369 Buffer.add_string bb ">\n";
6370 Buffer.add_buffer bb kb;
6371 Buffer.add_string bb "\n</defaults>\n";
6373 else Buffer.add_string bb "/>\n";
6375 let adddoc path pan anchor c bookmarks =
6376 if bookmarks == [] && c = dc && anchor = emptyanchor
6377 then ()
6378 else (
6379 Printf.bprintf bb "<doc path='%s'"
6380 (enent path 0 (String.length path));
6382 if anchor <> emptyanchor
6383 then (
6384 let n, y = anchor in
6385 Printf.bprintf bb " page='%d'" n;
6386 if y > 1e-6
6387 then
6388 Printf.bprintf bb " rely='%f'" y
6392 if pan != 0
6393 then Printf.bprintf bb " pan='%d'" pan;
6395 add_attrs bb false dc c;
6396 let kb = keymapsbuf false dc c in
6398 begin match bookmarks with
6399 | [] ->
6400 if Buffer.length kb > 0
6401 then (
6402 Buffer.add_string bb ">\n";
6403 Buffer.add_buffer bb kb;
6404 Buffer.add_string bb "</doc>\n";
6406 else Buffer.add_string bb "/>\n"
6407 | _ ->
6408 Buffer.add_string bb ">\n<bookmarks>\n";
6409 List.iter (fun (title, _level, (page, rely)) ->
6410 Printf.bprintf bb
6411 "<item title='%s' page='%d'"
6412 (enent title 0 (String.length title))
6413 page
6415 if rely > 1e-6
6416 then
6417 Printf.bprintf bb " rely='%f'" rely
6419 Buffer.add_string bb "/>\n";
6420 ) bookmarks;
6421 Buffer.add_string bb "</bookmarks>";
6422 if Buffer.length kb > 0
6423 then (
6424 Buffer.add_string bb "\n";
6425 Buffer.add_buffer bb kb;
6427 Buffer.add_string bb "\n</doc>\n";
6428 end;
6432 let pan, conf =
6433 match state.mode with
6434 | Birdseye (c, pan, _, _, _) ->
6435 let beyecolumns =
6436 match conf.columns with
6437 | Cmulti ((c, _, _), _) -> Some c
6438 | Csingle -> None
6439 | Csplit _ -> None
6440 and columns =
6441 match c.columns with
6442 | Cmulti (c, _) -> Cmulti (c, [||])
6443 | Csingle -> Csingle
6444 | Csplit _ -> failwith "quit from bird's eye while split"
6446 pan, { c with beyecolumns = beyecolumns; columns = columns }
6447 | _ -> state.x, conf
6449 let basename = Filename.basename state.path in
6450 adddoc basename pan (getanchor ())
6451 { conf with
6452 autoscrollstep =
6453 match state.autoscroll with
6454 | Some step -> step
6455 | None -> conf.autoscrollstep }
6456 (if conf.savebmarks then state.bookmarks else []);
6458 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6459 if basename <> path
6460 then adddoc path x y c bookmarks
6461 ) h;
6462 Buffer.add_string bb "</llppconfig>";
6464 load1 f;
6465 if Buffer.length bb > 0
6466 then
6468 let tmp = !confpath ^ ".tmp" in
6469 let oc = open_out_bin tmp in
6470 Buffer.output_buffer oc bb;
6471 close_out oc;
6472 Unix.rename tmp !confpath;
6473 with exn ->
6474 prerr_endline
6475 ("error while saving configuration: " ^ Printexc.to_string exn)
6477 end;;
6479 let () =
6480 Arg.parse
6481 (Arg.align
6482 [("-p", Arg.String (fun s -> state.password <- s) ,
6483 "<password> Set password");
6485 ("-f", Arg.String (fun s -> Config.fontpath := s),
6486 "<path> Set path to the user interface font");
6488 ("-c", Arg.String (fun s -> Config.confpath := s),
6489 "<path> Set path to the configuration file");
6491 ("-v", Arg.Unit (fun () ->
6492 Printf.printf
6493 "%s\nconfiguration path: %s\n"
6494 (version ())
6495 Config.defconfpath
6497 exit 0), " Print version and exit");
6500 (fun s -> state.path <- s)
6501 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6503 if String.length state.path = 0
6504 then (prerr_endline "file name missing"; exit 1);
6506 Config.load ();
6508 let globalkeyhash = findkeyhash conf "global" in
6509 let wsfd, winw, winh = Wsi.init (object
6510 method expose =
6511 if nogeomcmds state.geomcmds
6512 then display ()
6513 method display = display ()
6514 method reshape w h = reshape w h
6515 method mouse b d x y m = mouse b d x y m
6516 method motion x y = state.mpos <- (x, y); motion x y
6517 method pmotion x y = state.mpos <- (x, y); pmotion x y
6518 method key k m =
6519 let mascm = m land (
6520 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6521 ) in
6522 match state.keystate with
6523 | KSnone ->
6524 let km = k, mascm in
6525 begin
6526 match
6527 try Hashtbl.find globalkeyhash km
6528 with Not_found ->
6529 let modehash = state.uioh#modehash in
6530 try Hashtbl.find modehash km
6531 with Not_found -> KMinsrt (k, m)
6532 with
6533 | KMinsrt (k, m) -> keyboard k m
6534 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6535 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6537 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6538 List.iter (fun (k, m) -> keyboard k m) insrt;
6539 state.keystate <- KSnone
6540 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6541 state.keystate <- KSinto (keys, insrt)
6542 | _ ->
6543 state.keystate <- KSnone
6545 method enter x y = state.mpos <- (x, y); pmotion x y
6546 method leave = state.mpos <- (-1, -1)
6547 method quit = raise Quit
6548 end) conf.winw conf.winh (platform = Posx) in
6550 state.wsfd <- wsfd;
6552 if not (
6553 List.exists GlMisc.check_extension
6554 [ "GL_ARB_texture_rectangle"
6555 ; "GL_EXT_texture_recangle"
6556 ; "GL_NV_texture_rectangle" ]
6558 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6560 let cr, sw = Unix.pipe ()
6561 and sr, cw = Unix.pipe () in
6563 cloexec cr;
6564 cloexec sw;
6565 cloexec sr;
6566 cloexec cw;
6568 setcheckers conf.checkers;
6569 redirectstderr ();
6571 init (cr, cw) (
6572 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6573 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6574 !Config.fontpath
6576 state.sr <- sr;
6577 state.sw <- sw;
6578 state.text <- "Opening " ^ state.path;
6579 reshape winw winh;
6580 opendoc state.path state.password;
6581 state.uioh <- uioh;
6583 let rec loop deadline =
6584 let r =
6585 match state.errfd with
6586 | None -> [state.sr; state.wsfd]
6587 | Some fd -> [state.sr; state.wsfd; fd]
6589 if state.redisplay
6590 then (
6591 state.redisplay <- false;
6592 display ();
6594 let timeout =
6595 let now = now () in
6596 if deadline > now
6597 then (
6598 if deadline = infinity
6599 then ~-.1.0
6600 else max 0.0 (deadline -. now)
6602 else 0.0
6604 let r, _, _ =
6605 try Unix.select r [] [] timeout
6606 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6608 begin match r with
6609 | [] ->
6610 state.ghyll None;
6611 let newdeadline =
6612 if state.ghyll == noghyll
6613 then
6614 match state.autoscroll with
6615 | Some step when step != 0 ->
6616 let y = state.y + step in
6617 let y =
6618 if y < 0
6619 then state.maxy
6620 else if y >= state.maxy then 0 else y
6622 gotoy y;
6623 if state.mode = View
6624 then state.text <- "";
6625 deadline +. 0.01
6626 | _ -> infinity
6627 else deadline +. 0.01
6629 loop newdeadline
6631 | l ->
6632 let rec checkfds = function
6633 | [] -> ()
6634 | fd :: rest when fd = state.sr ->
6635 let cmd = readcmd state.sr in
6636 act cmd;
6637 checkfds rest
6639 | fd :: rest when fd = state.wsfd ->
6640 Wsi.readresp fd;
6641 checkfds rest
6643 | fd :: rest ->
6644 let s = String.create 80 in
6645 let n = Unix.read fd s 0 80 in
6646 if conf.redirectstderr
6647 then (
6648 Buffer.add_substring state.errmsgs s 0 n;
6649 state.newerrmsgs <- true;
6650 state.redisplay <- true;
6652 else (
6653 prerr_string (String.sub s 0 n);
6654 flush stderr;
6656 checkfds rest
6658 checkfds l;
6659 let newdeadline =
6660 let deadline1 =
6661 if deadline = infinity
6662 then now () +. 0.01
6663 else deadline
6665 match state.autoscroll with
6666 | Some step when step != 0 -> deadline1
6667 | _ -> if state.ghyll == noghyll then infinity else deadline1
6669 loop newdeadline
6670 end;
6673 loop infinity;
6674 with Quit ->
6675 Config.save ();
6676 exit 0;