Avoid zombies under cygwin too
[llpp.git] / main.ml
blob1a62d78e496be11628734cbfc6c5775bd736cde0
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 : string -> 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"
99 let platform_to_string = function
100 | Punknown -> "unknown"
101 | Plinux -> "Linux"
102 | Posx -> "OSX"
103 | Psun -> "Sun"
104 | Pfreebsd -> "FreeBSD"
105 | Pdragonflybsd -> "DragonflyBSD"
106 | Popenbsd -> "OpenBSD"
107 | Pnetbsd -> "NetBSD"
108 | Pcygwin -> "Cygwin"
111 let platform = platform ();;
113 type x = int
114 and y = int
115 and tilex = int
116 and tiley = int
117 and tileparams = (x * y * width * height * tilex * tiley)
120 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
122 type mpos = int * int
123 and mstate =
124 | Msel of (mpos * mpos)
125 | Mpan of mpos
126 | Mscrolly | Mscrollx
127 | Mzoom of (int * int)
128 | Mzoomrect of (mpos * mpos)
129 | Mnone
132 type textentry = string * string * onhist option * onkey * ondone
133 and onkey = string -> int -> te
134 and ondone = string -> unit
135 and histcancel = unit -> unit
136 and onhist = ((histcmd -> string) * histcancel)
137 and histcmd = HCnext | HCprev | HCfirst | HClast
138 and te =
139 | TEstop
140 | TEdone of string
141 | TEcont of string
142 | TEswitch of textentry
145 type 'a circbuf =
146 { store : 'a array
147 ; mutable rc : int
148 ; mutable wc : int
149 ; mutable len : int
153 let bound v minv maxv =
154 max minv (min maxv v);
157 let cbnew n v =
158 { store = Array.create n v
159 ; rc = 0
160 ; wc = 0
161 ; len = 0
165 let drawstring size x y s =
166 Gl.enable `blend;
167 Gl.enable `texture_2d;
168 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
169 ignore (drawstr size x y s);
170 Gl.disable `blend;
171 Gl.disable `texture_2d;
174 let drawstring1 size x y s =
175 drawstr size x y s;
178 let drawstring2 size x y fmt =
179 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
182 let cbcap b = Array.length b.store;;
184 let cbput b v =
185 let cap = cbcap b in
186 b.store.(b.wc) <- v;
187 b.wc <- (b.wc + 1) mod cap;
188 b.rc <- b.wc;
189 b.len <- min (b.len + 1) cap;
192 let cbempty b = b.len = 0;;
194 let cbgetg b circular dir =
195 if cbempty b
196 then b.store.(0)
197 else
198 let rc = b.rc + dir in
199 let rc =
200 if circular
201 then (
202 if rc = -1
203 then b.len-1
204 else (
205 if rc = b.len
206 then 0
207 else rc
210 else max 0 (min rc (b.len-1))
212 b.rc <- rc;
213 b.store.(rc);
216 let cbget b = cbgetg b false;;
217 let cbgetc b = cbgetg b true;;
219 type page =
220 { pageno : int
221 ; pagedimno : int
222 ; pagew : int
223 ; pageh : int
224 ; pagex : int
225 ; pagey : int
226 ; pagevw : int
227 ; pagevh : int
228 ; pagedispx : int
229 ; pagedispy : int
233 let debugl l =
234 dolog "l %d dim=%d {" l.pageno l.pagedimno;
235 dolog " WxH %dx%d" l.pagew l.pageh;
236 dolog " vWxH %dx%d" l.pagevw l.pagevh;
237 dolog " pagex,y %d,%d" l.pagex l.pagey;
238 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
239 dolog "}";
242 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
243 dolog "rect {";
244 dolog " x0,y0=(% f, % f)" x0 y0;
245 dolog " x1,y1=(% f, % f)" x1 y1;
246 dolog " x2,y2=(% f, % f)" x2 y2;
247 dolog " x3,y3=(% f, % f)" x3 y3;
248 dolog "}";
251 type multicolumns = multicol * pagegeom
252 and splitcolumns = columncount * pagegeom
253 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
254 and multicol = columncount * covercount * covercount
255 and pdimno = int
256 and columncount = int
257 and covercount = int;;
259 type conf =
260 { mutable scrollbw : int
261 ; mutable scrollh : int
262 ; mutable icase : bool
263 ; mutable preload : bool
264 ; mutable pagebias : int
265 ; mutable verbose : bool
266 ; mutable debug : bool
267 ; mutable scrollstep : int
268 ; mutable maxhfit : bool
269 ; mutable crophack : bool
270 ; mutable autoscrollstep : int
271 ; mutable maxwait : float option
272 ; mutable hlinks : bool
273 ; mutable underinfo : bool
274 ; mutable interpagespace : interpagespace
275 ; mutable zoom : float
276 ; mutable presentation : bool
277 ; mutable angle : angle
278 ; mutable winw : int
279 ; mutable winh : int
280 ; mutable savebmarks : bool
281 ; mutable proportional : proportional
282 ; mutable trimmargins : trimmargins
283 ; mutable trimfuzz : irect
284 ; mutable memlimit : memsize
285 ; mutable texcount : texcount
286 ; mutable sliceheight : sliceheight
287 ; mutable thumbw : width
288 ; mutable jumpback : bool
289 ; mutable bgcolor : float * float * float
290 ; mutable bedefault : bool
291 ; mutable scrollbarinpm : bool
292 ; mutable tilew : int
293 ; mutable tileh : int
294 ; mutable mustoresize : memsize
295 ; mutable checkers : bool
296 ; mutable aalevel : int
297 ; mutable urilauncher : string
298 ; mutable pathlauncher : string
299 ; mutable colorspace : colorspace
300 ; mutable invert : bool
301 ; mutable colorscale : float
302 ; mutable redirectstderr : bool
303 ; mutable ghyllscroll : (int * int * int) option
304 ; mutable columns : columns
305 ; mutable beyecolumns : columncount option
306 ; mutable selcmd : string
307 ; mutable updatecurs : bool
308 ; mutable keyhashes : (string * keyhash) list
310 and columns =
311 | Csingle
312 | Cmulti of multicolumns
313 | Csplit of splitcolumns
316 type anchor = pageno * top;;
318 type outline = string * int * anchor;;
320 type rect = float * float * float * float * float * float * float * float;;
322 type tile = opaque * pixmapsize * elapsed
323 and elapsed = float;;
324 type pagemapkey = pageno * gen;;
325 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
326 and row = int
327 and col = int;;
329 let emptyanchor = (0, 0.0);;
331 type infochange = | Memused | Docinfo | Pdim;;
333 class type uioh = object
334 method display : unit
335 method key : int -> int -> uioh
336 method button : int -> bool -> int -> int -> int -> uioh
337 method motion : int -> int -> uioh
338 method pmotion : int -> int -> uioh
339 method infochanged : infochange -> unit
340 method scrollpw : (int * float * float)
341 method scrollph : (int * float * float)
342 method modehash : keyhash
343 end;;
345 type mode =
346 | Birdseye of (conf * leftx * pageno * pageno * anchor)
347 | Textentry of (textentry * onleave)
348 | View
349 | LinkNav of linktarget
350 and onleave = leavetextentrystatus -> unit
351 and leavetextentrystatus = | Cancel | Confirm
352 and helpitem = string * int * action
353 and action =
354 | Noaction
355 | Action of (uioh -> uioh)
356 and linktarget =
357 | Ltexact of (pageno * int)
358 | Ltgendir of int
361 let isbirdseye = function Birdseye _ -> true | _ -> false;;
362 let istextentry = function Textentry _ -> true | _ -> false;;
364 type currently =
365 | Idle
366 | Loading of (page * gen)
367 | Tiling of (
368 page * opaque * colorspace * angle * gen * col * row * width * height
370 | Outlining of outline list
373 let emptykeyhash = Hashtbl.create 0;;
374 let nouioh : uioh = object (self)
375 method display = ()
376 method key _ _ = self
377 method button _ _ _ _ _ = self
378 method motion _ _ = self
379 method pmotion _ _ = self
380 method infochanged _ = ()
381 method scrollpw = (0, nan, nan)
382 method scrollph = (0, nan, nan)
383 method modehash = emptykeyhash
384 end;;
386 type state =
387 { mutable sr : Unix.file_descr
388 ; mutable sw : Unix.file_descr
389 ; mutable wsfd : Unix.file_descr
390 ; mutable errfd : Unix.file_descr option
391 ; mutable stderr : Unix.file_descr
392 ; mutable errmsgs : Buffer.t
393 ; mutable newerrmsgs : bool
394 ; mutable w : int
395 ; mutable x : int
396 ; mutable y : int
397 ; mutable scrollw : int
398 ; mutable hscrollh : int
399 ; mutable anchor : anchor
400 ; mutable ranchors : (string * string * anchor) list
401 ; mutable maxy : int
402 ; mutable layout : page list
403 ; pagemap : (pagemapkey, opaque) Hashtbl.t
404 ; tilemap : (tilemapkey, tile) Hashtbl.t
405 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
406 ; mutable pdims : (pageno * width * height * leftx) list
407 ; mutable pagecount : int
408 ; mutable currently : currently
409 ; mutable mstate : mstate
410 ; mutable searchpattern : string
411 ; mutable rects : (pageno * recttype * rect) list
412 ; mutable rects1 : (pageno * recttype * rect) list
413 ; mutable text : string
414 ; mutable fullscreen : (width * height) option
415 ; mutable mode : mode
416 ; mutable uioh : uioh
417 ; mutable outlines : outline array
418 ; mutable bookmarks : outline list
419 ; mutable path : string
420 ; mutable password : string
421 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
422 ; mutable memused : memsize
423 ; mutable gen : gen
424 ; mutable throttle : (page list * int * float) option
425 ; mutable autoscroll : int option
426 ; mutable ghyll : (int option -> unit)
427 ; mutable help : helpitem array
428 ; mutable docinfo : (int * string) list
429 ; mutable texid : GlTex.texture_id option
430 ; hists : hists
431 ; mutable prevzoom : float
432 ; mutable progress : float
433 ; mutable redisplay : bool
434 ; mutable mpos : mpos
435 ; mutable keystate : keystate
436 ; mutable glinks : bool
438 and hists =
439 { pat : string circbuf
440 ; pag : string circbuf
441 ; nav : anchor circbuf
442 ; sel : string circbuf
446 let defconf =
447 { scrollbw = 7
448 ; scrollh = 12
449 ; icase = true
450 ; preload = true
451 ; pagebias = 0
452 ; verbose = false
453 ; debug = false
454 ; scrollstep = 24
455 ; maxhfit = true
456 ; crophack = false
457 ; autoscrollstep = 2
458 ; maxwait = None
459 ; hlinks = false
460 ; underinfo = false
461 ; interpagespace = 2
462 ; zoom = 1.0
463 ; presentation = false
464 ; angle = 0
465 ; winw = 900
466 ; winh = 900
467 ; savebmarks = true
468 ; proportional = true
469 ; trimmargins = false
470 ; trimfuzz = (0,0,0,0)
471 ; memlimit = 32 lsl 20
472 ; texcount = 256
473 ; sliceheight = 24
474 ; thumbw = 76
475 ; jumpback = true
476 ; bgcolor = (0.5, 0.5, 0.5)
477 ; bedefault = false
478 ; scrollbarinpm = true
479 ; tilew = 2048
480 ; tileh = 2048
481 ; mustoresize = 256 lsl 20
482 ; checkers = true
483 ; aalevel = 8
484 ; urilauncher =
485 (match platform with
486 | Plinux | Pfreebsd | Pdragonflybsd
487 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
488 | Posx -> "open \"%s\""
489 | Pcygwin -> "cygstart %s"
490 | Punknown -> "echo %s")
491 ; pathlauncher = "lp \"%s\""
492 ; selcmd =
493 (match platform with
494 | Plinux | Pfreebsd | Pdragonflybsd
495 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
496 | Posx -> "pbcopy"
497 | Pcygwin -> "wsel"
498 | Punknown -> "cat")
499 ; colorspace = Rgb
500 ; invert = false
501 ; colorscale = 1.0
502 ; redirectstderr = false
503 ; ghyllscroll = None
504 ; columns = Csingle
505 ; beyecolumns = None
506 ; updatecurs = false
507 ; keyhashes =
508 let mk n = (n, Hashtbl.create 1) in
509 [ mk "global"
510 ; mk "info"
511 ; mk "help"
512 ; mk "outline"
513 ; mk "listview"
514 ; mk "birdseye"
515 ; mk "textentry"
516 ; mk "links"
521 let findkeyhash c name =
522 try List.assoc name c.keyhashes
523 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
526 let conf = { defconf with angle = defconf.angle };;
528 type fontstate =
529 { mutable fontsize : int
530 ; mutable wwidth : float
531 ; mutable maxrows : int
535 let fstate =
536 { fontsize = 14
537 ; wwidth = nan
538 ; maxrows = -1
542 let setfontsize n =
543 fstate.fontsize <- n;
544 fstate.wwidth <- measurestr fstate.fontsize "w";
545 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
548 let geturl s =
549 let colonpos = try String.index s ':' with Not_found -> -1 in
550 let len = String.length s in
551 if colonpos >= 0 && colonpos + 3 < len
552 then (
553 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
554 then
555 let schemestartpos =
556 try String.rindex_from s colonpos ' '
557 with Not_found -> -1
559 let scheme =
560 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
562 match scheme with
563 | "http" | "ftp" | "mailto" ->
564 let epos =
565 try String.index_from s colonpos ' '
566 with Not_found -> len
568 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
569 | _ -> ""
570 else ""
572 else ""
575 let popen =
576 let shell, farg = "/bin/sh", "-c" in
577 fun s ->
578 let args = [|shell; farg; s|] in
579 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
582 let gotouri uri =
583 if String.length conf.urilauncher = 0
584 then print_endline uri
585 else (
586 let url = geturl uri in
587 if String.length url = 0
588 then print_endline uri
589 else
590 let re = Str.regexp "%s" in
591 let command = Str.global_replace re url conf.urilauncher in
592 try popen command
593 with exn ->
594 Printf.eprintf
595 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
596 flush stderr;
600 let version () =
601 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
602 (platform_to_string platform) Sys.word_size Sys.ocaml_version
605 let makehelp () =
606 let strings = version () :: "" :: Help.keys in
607 Array.of_list (
608 List.map (fun s ->
609 let url = geturl s in
610 if String.length url > 0
611 then (s, 0, Action (fun u -> gotouri url; u))
612 else (s, 0, Noaction)
613 ) strings);
616 let noghyll _ = ();;
617 let firstgeomcmds = "", [];;
619 let state =
620 { sr = Unix.stdin
621 ; sw = Unix.stdin
622 ; wsfd = Unix.stdin
623 ; errfd = None
624 ; stderr = Unix.stderr
625 ; errmsgs = Buffer.create 0
626 ; newerrmsgs = false
627 ; x = 0
628 ; y = 0
629 ; w = 0
630 ; scrollw = 0
631 ; hscrollh = 0
632 ; anchor = emptyanchor
633 ; ranchors = []
634 ; layout = []
635 ; maxy = max_int
636 ; tilelru = Queue.create ()
637 ; pagemap = Hashtbl.create 10
638 ; tilemap = Hashtbl.create 10
639 ; pdims = []
640 ; pagecount = 0
641 ; currently = Idle
642 ; mstate = Mnone
643 ; rects = []
644 ; rects1 = []
645 ; text = ""
646 ; mode = View
647 ; fullscreen = None
648 ; searchpattern = ""
649 ; outlines = [||]
650 ; bookmarks = []
651 ; path = ""
652 ; password = ""
653 ; geomcmds = firstgeomcmds
654 ; hists =
655 { nav = cbnew 10 (0, 0.0)
656 ; pat = cbnew 10 ""
657 ; pag = cbnew 10 ""
658 ; sel = cbnew 10 ""
660 ; memused = 0
661 ; gen = 0
662 ; throttle = None
663 ; autoscroll = None
664 ; ghyll = noghyll
665 ; help = makehelp ()
666 ; docinfo = []
667 ; texid = None
668 ; prevzoom = 1.0
669 ; progress = -1.0
670 ; uioh = nouioh
671 ; redisplay = true
672 ; mpos = (-1, -1)
673 ; keystate = KSnone
674 ; glinks = false
678 let vlog fmt =
679 if conf.verbose
680 then
681 Printf.kprintf prerr_endline fmt
682 else
683 Printf.kprintf ignore fmt
686 let launchpath () =
687 if String.length conf.pathlauncher = 0
688 then print_endline state.path
689 else (
690 let re = Str.regexp "%s" in
691 let command = Str.global_replace re state.path conf.pathlauncher in
692 try popen command
693 with exn ->
694 Printf.eprintf
695 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
696 flush stderr;
700 let redirectstderr () =
701 if conf.redirectstderr
702 then
703 let rfd, wfd = Unix.pipe () in
704 state.stderr <- Unix.dup Unix.stderr;
705 state.errfd <- Some rfd;
706 Unix.dup2 wfd Unix.stderr;
707 else (
708 state.newerrmsgs <- false;
709 begin match state.errfd with
710 | Some fd ->
711 Unix.close fd;
712 Unix.dup2 state.stderr Unix.stderr;
713 state.errfd <- None;
714 | None -> ()
715 end;
716 prerr_string (Buffer.contents state.errmsgs);
717 flush stderr;
718 Buffer.clear state.errmsgs;
722 module G =
723 struct
724 let postRedisplay who =
725 if conf.verbose
726 then prerr_endline ("redisplay for " ^ who);
727 state.redisplay <- true;
729 end;;
731 let getopaque pageno =
732 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
733 with Not_found -> None
736 let putopaque pageno opaque =
737 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
740 let pagetranslatepoint l x y =
741 let dy = y - l.pagedispy in
742 let y = dy + l.pagey in
743 let dx = x - l.pagedispx in
744 let x = dx + l.pagex in
745 (x, y);
748 let getunder x y =
749 let rec f = function
750 | l :: rest ->
751 begin match getopaque l.pageno with
752 | Some opaque ->
753 let x0 = l.pagedispx in
754 let x1 = x0 + l.pagevw in
755 let y0 = l.pagedispy in
756 let y1 = y0 + l.pagevh in
757 if y >= y0 && y <= y1 && x >= x0 && x <= x1
758 then
759 let px, py = pagetranslatepoint l x y in
760 match whatsunder opaque px py with
761 | Unone -> f rest
762 | under -> under
763 else f rest
764 | _ ->
765 f rest
767 | [] -> Unone
769 f state.layout
772 let showtext c s =
773 state.text <- Printf.sprintf "%c%s" c s;
774 G.postRedisplay "showtext";
777 let updateunder x y =
778 match getunder x y with
779 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
780 | Ulinkuri uri ->
781 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
782 Wsi.setcursor Wsi.CURSOR_INFO
783 | Ulinkgoto (page, _) ->
784 if conf.underinfo
785 then showtext 'p' ("age: " ^ string_of_int (page+1));
786 Wsi.setcursor Wsi.CURSOR_INFO
787 | Utext s ->
788 if conf.underinfo then showtext 'f' ("ont: " ^ s);
789 Wsi.setcursor Wsi.CURSOR_TEXT
790 | Uunexpected s ->
791 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
792 Wsi.setcursor Wsi.CURSOR_INHERIT
793 | Ulaunch s ->
794 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
795 Wsi.setcursor Wsi.CURSOR_INHERIT
796 | Unamed s ->
797 if conf.underinfo then showtext 'n' ("amed: " ^ s);
798 Wsi.setcursor Wsi.CURSOR_INHERIT
799 | Uremote (filename, pageno) ->
800 if conf.underinfo then showtext 'r'
801 (Printf.sprintf "emote: %s (%d)" filename pageno);
802 Wsi.setcursor Wsi.CURSOR_INFO
805 let showlinktype under =
806 if conf.underinfo
807 then
808 match under with
809 | Unone -> ()
810 | Ulinkuri uri ->
811 showtext 'u' ("ri: " ^ uri)
812 | Ulinkgoto (page, _) ->
813 showtext 'p' ("age: " ^ string_of_int (page+1));
814 | Utext s ->
815 showtext 'f' ("ont: " ^ s);
816 | Uunexpected s ->
817 showtext 'u' ("nexpected: " ^ s);
818 | Ulaunch s ->
819 showtext 'l' ("aunch: " ^ s);
820 | Unamed s ->
821 showtext 'n' ("amed: " ^ s);
822 | Uremote (filename, pageno) ->
823 showtext 'r' (Printf.sprintf "emote: %s (%d)" filename pageno);
826 let addchar s c =
827 let b = Buffer.create (String.length s + 1) in
828 Buffer.add_string b s;
829 Buffer.add_char b c;
830 Buffer.contents b;
833 let colorspace_of_string s =
834 match String.lowercase s with
835 | "rgb" -> Rgb
836 | "bgr" -> Bgr
837 | "gray" -> Gray
838 | _ -> failwith "invalid colorspace"
841 let int_of_colorspace = function
842 | Rgb -> 0
843 | Bgr -> 1
844 | Gray -> 2
847 let colorspace_of_int = function
848 | 0 -> Rgb
849 | 1 -> Bgr
850 | 2 -> Gray
851 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
854 let colorspace_to_string = function
855 | Rgb -> "rgb"
856 | Bgr -> "bgr"
857 | Gray -> "gray"
860 let intentry_with_suffix text key =
861 let c =
862 if key >= 32 && key < 127
863 then Char.chr key
864 else '\000'
866 match Char.lowercase c with
867 | '0' .. '9' ->
868 let text = addchar text c in
869 TEcont text
871 | 'k' | 'm' | 'g' ->
872 let text = addchar text c in
873 TEcont text
875 | _ ->
876 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
877 TEcont text
880 let multicolumns_to_string (n, a, b) =
881 if a = 0 && b = 0
882 then Printf.sprintf "%d" n
883 else Printf.sprintf "%d,%d,%d" n a b;
886 let multicolumns_of_string s =
888 (int_of_string s, 0, 0)
889 with _ ->
890 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
893 let readcmd fd =
894 let s = "xxxx" in
895 let n = Unix.read fd s 0 4 in
896 if n != 4 then failwith "incomplete read(len)";
897 let len = 0
898 lor (Char.code s.[0] lsl 24)
899 lor (Char.code s.[1] lsl 16)
900 lor (Char.code s.[2] lsl 8)
901 lor (Char.code s.[3] lsl 0)
903 let s = String.create len in
904 let n = Unix.read fd s 0 len in
905 if n != len then failwith "incomplete read(data)";
909 let btod b = if b then 1 else 0;;
911 let wcmd fmt =
912 let b = Buffer.create 16 in
913 Buffer.add_string b "llll";
914 Printf.kbprintf
915 (fun b ->
916 let s = Buffer.contents b in
917 let n = String.length s in
918 let len = n - 4 in
919 (* dolog "wcmd %S" (String.sub s 4 len); *)
920 s.[0] <- Char.chr ((len lsr 24) land 0xff);
921 s.[1] <- Char.chr ((len lsr 16) land 0xff);
922 s.[2] <- Char.chr ((len lsr 8) land 0xff);
923 s.[3] <- Char.chr (len land 0xff);
924 let n' = Unix.write state.sw s 0 n in
925 if n' != n then failwith "write failed";
926 ) b fmt;
929 let calcips h =
930 if conf.presentation
931 then
932 let d = conf.winh - h in
933 max 0 ((d + 1) / 2)
934 else
935 conf.interpagespace
938 let calcheight () =
939 let rec f pn ph pi fh l =
940 match l with
941 | (n, _, h, _) :: rest ->
942 let ips = calcips h in
943 let fh =
944 if conf.presentation
945 then fh+ips
946 else (
947 if isbirdseye state.mode && pn = 0
948 then fh + ips
949 else fh
952 let fh = fh + ((n - pn) * (ph + pi)) in
953 f n h ips fh rest;
955 | [] ->
956 let inc =
957 if conf.presentation || (isbirdseye state.mode && pn = 0)
958 then 0
959 else -pi
961 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
962 max 0 fh
964 let fh = f 0 0 0 0 state.pdims in
968 let calcheight () =
969 match conf.columns with
970 | Csingle -> calcheight ()
971 | Cmulti (_, 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
977 | Csplit (_, b) ->
978 if Array.length b > 0
979 then
980 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
981 y + h
982 else 0
985 let getpageyh pageno =
986 let rec f pn ph pi y l =
987 match l with
988 | (n, _, h, _) :: rest ->
989 let ips = calcips h in
990 if n >= pageno
991 then
992 let h = if n = pageno then h else ph in
993 if conf.presentation && n = pageno
994 then
995 y + (pageno - pn) * (ph + pi) + pi, h
996 else
997 y + (pageno - pn) * (ph + pi), h
998 else
999 let y = y + (if conf.presentation then pi else 0) in
1000 let y = y + (n - pn) * (ph + pi) in
1001 f n h ips y rest
1003 | [] ->
1004 y + (pageno - pn) * (ph + pi), ph
1006 f 0 0 0 0 state.pdims
1009 let getpageyh pageno =
1010 match conf.columns with
1011 | Csingle -> getpageyh pageno
1012 | Cmulti (_, b) ->
1013 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1014 y, h
1015 | Csplit (c, b) ->
1016 let n = pageno*c in
1017 let (_, _, y, (_, _, h, _)) = b.(n) in
1018 y, h
1021 let getpagedim pageno =
1022 let rec f ppdim l =
1023 match l with
1024 | (n, _, _, _) as pdim :: rest ->
1025 if n >= pageno
1026 then (if n = pageno then pdim else ppdim)
1027 else f pdim rest
1029 | [] -> ppdim
1031 f (-1, -1, -1, -1) state.pdims
1034 let getpagey pageno = fst (getpageyh pageno);;
1036 let nogeomcmds cmds =
1037 match cmds with
1038 | s, [] -> String.length s = 0
1039 | _ -> false
1042 let layout1 y sh =
1043 let sh = sh - state.hscrollh in
1044 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1045 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1046 match pdims with
1047 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1048 let ips = calcips h in
1049 let yinc =
1050 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1051 then ips
1052 else 0
1054 (w, h, ips, xoff), rest, pdimno + 1, yinc
1055 | _ ->
1056 prev, pdims, pdimno, 0
1058 let dy = dy + yinc in
1059 let py = py + yinc in
1060 if pageno = state.pagecount || dy >= sh
1061 then
1062 accu
1063 else
1064 let vy = y + dy in
1065 if py + h <= vy - yinc
1066 then
1067 let py = py + h + ips in
1068 let dy = max 0 (py - y) in
1069 f ~pageno:(pageno+1)
1070 ~pdimno
1071 ~prev:curr
1074 ~pdims:rest
1075 ~accu
1076 else
1077 let pagey = vy - py in
1078 let pagevh = h - pagey in
1079 let pagevh = min (sh - dy) pagevh in
1080 let off = if yinc > 0 then py - vy else 0 in
1081 let py = py + h + ips in
1082 let pagex, dx =
1083 let xoff = xoff +
1084 if state.w < conf.winw - state.scrollw
1085 then (conf.winw - state.scrollw - state.w) / 2
1086 else 0
1088 let dispx = xoff + state.x in
1089 if dispx < 0
1090 then (-dispx, 0)
1091 else (0, dispx)
1093 let pagevw =
1094 let lw = w - pagex in
1095 min lw (conf.winw - state.scrollw)
1097 let e =
1098 { pageno = pageno
1099 ; pagedimno = pdimno
1100 ; pagew = w
1101 ; pageh = h
1102 ; pagex = pagex
1103 ; pagey = pagey + off
1104 ; pagevw = pagevw
1105 ; pagevh = pagevh - off
1106 ; pagedispx = dx
1107 ; pagedispy = dy + off
1110 let accu = e :: accu in
1111 f ~pageno:(pageno+1)
1112 ~pdimno
1113 ~prev:curr
1115 ~dy:(dy+pagevh+ips)
1116 ~pdims:rest
1117 ~accu
1119 let accu =
1121 ~pageno:0
1122 ~pdimno:~-1
1123 ~prev:(0,0,0,0)
1124 ~py:0
1125 ~dy:0
1126 ~pdims:state.pdims
1127 ~accu:[]
1129 List.rev accu
1132 let layoutN ((columns, coverA, coverB), b) y sh =
1133 let sh = sh - state.hscrollh in
1134 let rec fold accu n =
1135 if n = Array.length b
1136 then accu
1137 else
1138 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1139 if (vy - y) > sh &&
1140 (n = coverA - 1
1141 || n = state.pagecount - coverB
1142 || (n - coverA) mod columns = columns - 1)
1143 then accu
1144 else
1145 let accu =
1146 if vy + h > y
1147 then
1148 let pagey = max 0 (y - vy) in
1149 let pagedispy = if pagey > 0 then 0 else vy - y in
1150 let pagedispx, pagex =
1151 let pdx =
1152 if n = coverA - 1 || n = state.pagecount - coverB
1153 then state.x + (conf.winw - state.scrollw - w) / 2
1154 else dx + xoff + state.x
1156 if pdx < 0
1157 then 0, -pdx
1158 else pdx, 0
1160 let pagevw =
1161 let vw = conf.winw - state.scrollw - pagedispx in
1162 let pw = w - pagex in
1163 min vw pw
1165 let pagevh = min (h - pagey) (sh - pagedispy) in
1166 if pagevw > 0 && pagevh > 0
1167 then
1168 let e =
1169 { pageno = n
1170 ; pagedimno = pdimno
1171 ; pagew = w
1172 ; pageh = h
1173 ; pagex = pagex
1174 ; pagey = pagey
1175 ; pagevw = pagevw
1176 ; pagevh = pagevh
1177 ; pagedispx = pagedispx
1178 ; pagedispy = pagedispy
1181 e :: accu
1182 else
1183 accu
1184 else
1185 accu
1187 fold accu (n+1)
1189 List.rev (fold [] 0)
1192 let layoutS (columns, b) y sh =
1193 let sh = sh - state.hscrollh in
1194 let rec fold accu n =
1195 if n = Array.length b
1196 then accu
1197 else
1198 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1199 if (vy - y) > sh
1200 then accu
1201 else
1202 let accu =
1203 if vy + pageh > y
1204 then
1205 let x = xoff + state.x in
1206 let pagey = max 0 (y - vy) in
1207 let pagedispy = if pagey > 0 then 0 else vy - y in
1208 let pagedispx, pagex =
1209 if px = 0
1210 then (
1211 if x < 0
1212 then 0, -x
1213 else x, 0
1215 else (
1216 let px = px - x in
1217 if px < 0
1218 then -px, 0
1219 else 0, px
1222 let pagevw =
1223 let vw = conf.winw - pagedispx - state.scrollw in
1224 let pw = pagew - pagex in
1225 min vw pw
1227 let pagevw = min pagevw (pagew/columns) in
1228 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1229 if pagevw > 0 && pagevh > 0
1230 then
1231 let e =
1232 { pageno = n/columns
1233 ; pagedimno = pdimno
1234 ; pagew = pagew
1235 ; pageh = pageh
1236 ; pagex = pagex
1237 ; pagey = pagey
1238 ; pagevw = pagevw
1239 ; pagevh = pagevh
1240 ; pagedispx = pagedispx
1241 ; pagedispy = pagedispy
1244 e :: accu
1245 else
1246 accu
1247 else
1248 accu
1250 fold accu (n+1)
1252 List.rev (fold [] 0)
1255 let layout y sh =
1256 if nogeomcmds state.geomcmds
1257 then
1258 match conf.columns with
1259 | Csingle -> layout1 y sh
1260 | Cmulti c -> layoutN c y sh
1261 | Csplit s -> layoutS s y sh
1262 else []
1265 let clamp incr =
1266 let y = state.y + incr in
1267 let y = max 0 y in
1268 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1272 let itertiles l f =
1273 let tilex = l.pagex mod conf.tilew in
1274 let tiley = l.pagey mod conf.tileh in
1276 let col = l.pagex / conf.tilew in
1277 let row = l.pagey / conf.tileh in
1279 let rec rowloop row y0 dispy h =
1280 if h = 0
1281 then ()
1282 else (
1283 let dh = conf.tileh - y0 in
1284 let dh = min h dh in
1285 let rec colloop col x0 dispx w =
1286 if w = 0
1287 then ()
1288 else (
1289 let dw = conf.tilew - x0 in
1290 let dw = min w dw in
1292 f col row dispx dispy x0 y0 dw dh;
1293 colloop (col+1) 0 (dispx+dw) (w-dw)
1296 colloop col tilex l.pagedispx l.pagevw;
1297 rowloop (row+1) 0 (dispy+dh) (h-dh)
1300 if l.pagevw > 0 && l.pagevh > 0
1301 then rowloop row tiley l.pagedispy l.pagevh;
1304 let gettileopaque l col row =
1305 let key =
1306 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1308 try Some (Hashtbl.find state.tilemap key)
1309 with Not_found -> None
1312 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1313 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1314 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1317 let drawtiles l color =
1318 GlDraw.color color;
1319 let f col row x y tilex tiley w h =
1320 match gettileopaque l col row with
1321 | Some (opaque, _, t) ->
1322 let params = x, y, w, h, tilex, tiley in
1323 if conf.invert
1324 then (
1325 Gl.enable `blend;
1326 GlFunc.blend_func `zero `one_minus_src_color;
1328 drawtile params opaque;
1329 if conf.invert
1330 then Gl.disable `blend;
1331 if conf.debug
1332 then (
1333 let s = Printf.sprintf
1334 "%d[%d,%d] %f sec"
1335 l.pageno col row t
1337 let w = measurestr fstate.fontsize s in
1338 GlMisc.push_attrib [`current];
1339 GlDraw.color (0.0, 0.0, 0.0);
1340 GlDraw.rect
1341 (float (x-2), float (y-2))
1342 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1343 GlDraw.color (1.0, 1.0, 1.0);
1344 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1345 GlMisc.pop_attrib ();
1348 | _ ->
1349 let w =
1350 let lw = conf.winw - state.scrollw - x in
1351 min lw w
1352 and h =
1353 let lh = conf.winh - y in
1354 min lh h
1356 Gl.enable `texture_2d;
1357 begin match state.texid with
1358 | Some id ->
1359 GlTex.bind_texture `texture_2d id;
1360 let x0 = float x
1361 and y0 = float y
1362 and x1 = float (x+w)
1363 and y1 = float (y+h) in
1365 let tw = float w /. 64.0
1366 and th = float h /. 64.0 in
1367 let tx0 = float tilex /. 64.0
1368 and ty0 = float tiley /. 64.0 in
1369 let tx1 = tx0 +. tw
1370 and ty1 = ty0 +. th in
1371 GlDraw.begins `quads;
1372 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1373 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1374 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1375 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1376 GlDraw.ends ();
1378 Gl.disable `texture_2d;
1379 | None ->
1380 GlDraw.color (1.0, 1.0, 1.0);
1381 GlDraw.rect
1382 (float x, float y)
1383 (float (x+w), float (y+h));
1384 end;
1385 if w > 128 && h > fstate.fontsize + 10
1386 then (
1387 GlDraw.color (0.0, 0.0, 0.0);
1388 let c, r =
1389 if conf.verbose
1390 then (col*conf.tilew, row*conf.tileh)
1391 else col, row
1393 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1395 GlDraw.color color;
1397 itertiles l f
1400 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1402 let tilevisible1 l x y =
1403 let ax0 = l.pagex
1404 and ax1 = l.pagex + l.pagevw
1405 and ay0 = l.pagey
1406 and ay1 = l.pagey + l.pagevh in
1408 let bx0 = x
1409 and by0 = y in
1410 let bx1 = min (bx0 + conf.tilew) l.pagew
1411 and by1 = min (by0 + conf.tileh) l.pageh in
1413 let rx0 = max ax0 bx0
1414 and ry0 = max ay0 by0
1415 and rx1 = min ax1 bx1
1416 and ry1 = min ay1 by1 in
1418 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1419 nonemptyintersection
1422 let tilevisible layout n x y =
1423 let rec findpageinlayout m = function
1424 | l :: rest when l.pageno = n ->
1425 tilevisible1 l x y || (
1426 match conf.columns with
1427 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1428 | _ -> false
1430 | _ :: rest -> findpageinlayout 0 rest
1431 | [] -> false
1433 findpageinlayout 0 layout;
1436 let tileready l x y =
1437 tilevisible1 l x y &&
1438 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1441 let tilepage n p layout =
1442 let rec loop = function
1443 | l :: rest ->
1444 if l.pageno = n
1445 then
1446 let f col row _ _ _ _ _ _ =
1447 if state.currently = Idle
1448 then
1449 match gettileopaque l col row with
1450 | Some _ -> ()
1451 | None ->
1452 let x = col*conf.tilew
1453 and y = row*conf.tileh in
1454 let w =
1455 let w = l.pagew - x in
1456 min w conf.tilew
1458 let h =
1459 let h = l.pageh - y in
1460 min h conf.tileh
1462 wcmd "tile %s %d %d %d %d" p x y w h;
1463 state.currently <-
1464 Tiling (
1465 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1466 conf.tilew, conf.tileh
1469 itertiles l f;
1470 else
1471 loop rest
1473 | [] -> ()
1475 if nogeomcmds state.geomcmds
1476 then loop layout;
1479 let preloadlayout visiblepages =
1480 let presentation = conf.presentation in
1481 let interpagespace = conf.interpagespace in
1482 let maxy = state.maxy in
1483 conf.presentation <- false;
1484 conf.interpagespace <- 0;
1485 state.maxy <- calcheight ();
1486 let y =
1487 match visiblepages with
1488 | [] -> 0
1489 | l :: _ -> getpagey l.pageno + l.pagey
1491 let y = if y < conf.winh then 0 else y - conf.winh in
1492 let h = state.y - y + conf.winh*3 in
1493 let pages = layout y h in
1494 conf.presentation <- presentation;
1495 conf.interpagespace <- interpagespace;
1496 state.maxy <- maxy;
1497 pages;
1500 let load pages =
1501 let rec loop pages =
1502 if state.currently != Idle
1503 then ()
1504 else
1505 match pages with
1506 | l :: rest ->
1507 begin match getopaque l.pageno with
1508 | None ->
1509 wcmd "page %d %d" l.pageno l.pagedimno;
1510 state.currently <- Loading (l, state.gen);
1511 | Some opaque ->
1512 tilepage l.pageno opaque pages;
1513 loop rest
1514 end;
1515 | _ -> ()
1517 if nogeomcmds state.geomcmds
1518 then loop pages
1521 let preload pages =
1522 load pages;
1523 if conf.preload && state.currently = Idle
1524 then load (preloadlayout pages);
1527 let layoutready layout =
1528 let rec fold all ls =
1529 all && match ls with
1530 | l :: rest ->
1531 let seen = ref false in
1532 let allvisible = ref true in
1533 let foo col row _ _ _ _ _ _ =
1534 seen := true;
1535 allvisible := !allvisible &&
1536 begin match gettileopaque l col row with
1537 | Some _ -> true
1538 | None -> false
1541 itertiles l foo;
1542 fold (!seen && !allvisible) rest
1543 | [] -> true
1545 let alltilesvisible = fold true layout in
1546 alltilesvisible;
1549 let gotoy y =
1550 let y = bound y 0 state.maxy in
1551 let y, layout, proceed =
1552 match conf.maxwait with
1553 | Some time when state.ghyll == noghyll ->
1554 begin match state.throttle with
1555 | None ->
1556 let layout = layout y conf.winh in
1557 let ready = layoutready layout in
1558 if not ready
1559 then (
1560 load layout;
1561 state.throttle <- Some (layout, y, now ());
1563 else G.postRedisplay "gotoy showall (None)";
1564 y, layout, ready
1565 | Some (_, _, started) ->
1566 let dt = now () -. started in
1567 if dt > time
1568 then (
1569 state.throttle <- None;
1570 let layout = layout y conf.winh in
1571 load layout;
1572 G.postRedisplay "maxwait";
1573 y, layout, true
1575 else -1, [], false
1578 | _ ->
1579 let layout = layout y conf.winh in
1580 if true || layoutready layout
1581 then G.postRedisplay "gotoy ready";
1582 y, layout, true
1584 if proceed
1585 then (
1586 state.y <- y;
1587 state.layout <- layout;
1588 begin match state.mode with
1589 | LinkNav (Ltexact (pageno, linkno)) ->
1590 let rec loop = function
1591 | [] ->
1592 state.mode <- LinkNav (Ltgendir 0)
1593 | l :: _ when l.pageno = pageno ->
1594 begin match getopaque pageno with
1595 | None ->
1596 state.mode <- LinkNav (Ltgendir 0)
1597 | Some opaque ->
1598 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1599 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1600 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1601 then state.mode <- LinkNav (Ltgendir 0)
1603 | _ :: rest -> loop rest
1605 loop layout
1606 | _ -> ()
1607 end;
1608 begin match state.mode with
1609 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1610 if not (pagevisible layout pageno)
1611 then (
1612 match state.layout with
1613 | [] -> ()
1614 | l :: _ ->
1615 state.mode <- Birdseye (
1616 conf, leftx, l.pageno, hooverpageno, anchor
1619 | LinkNav (Ltgendir dir as lt) ->
1620 let linknav =
1621 let rec loop = function
1622 | [] -> lt
1623 | l :: rest ->
1624 match getopaque l.pageno with
1625 | None -> loop rest
1626 | Some opaque ->
1627 let link =
1628 let ld =
1629 if dir = 0
1630 then LDfirstvisible (l.pagex, l.pagey, dir)
1631 else (
1632 if dir > 0 then LDfirst else LDlast
1635 findlink opaque ld
1637 match link with
1638 | Lnotfound -> loop rest
1639 | Lfound n ->
1640 showlinktype (getlink opaque n);
1641 Ltexact (l.pageno, n)
1643 loop state.layout
1645 state.mode <- LinkNav linknav
1646 | _ -> ()
1647 end;
1648 preload layout;
1650 state.ghyll <- noghyll;
1651 if conf.updatecurs
1652 then (
1653 let mx, my = state.mpos in
1654 updateunder mx my;
1658 let conttiling pageno opaque =
1659 tilepage pageno opaque
1660 (if conf.preload then preloadlayout state.layout else state.layout)
1663 let gotoy_and_clear_text y =
1664 if not conf.verbose then state.text <- "";
1665 gotoy y;
1668 let getanchor () =
1669 match state.layout with
1670 | [] -> emptyanchor
1671 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1674 let getanchory (n, top) =
1675 let y, h = getpageyh n in
1676 y + (truncate (top *. float h));
1679 let gotoanchor anchor =
1680 gotoy (getanchory anchor);
1683 let addnav () =
1684 cbput state.hists.nav (getanchor ());
1687 let getnav dir =
1688 let anchor = cbgetc state.hists.nav dir in
1689 getanchory anchor;
1692 let gotoghyll y =
1693 let rec scroll f n a b =
1694 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1695 let snake f a b =
1696 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1697 if f < a
1698 then s (float f /. float a)
1699 else (
1700 if f > b
1701 then 1.0 -. s ((float (f-b) /. float (n-b)))
1702 else 1.0
1705 snake f a b
1706 and summa f n a b =
1707 (* courtesy:
1708 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1709 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1710 let iv1 = iv f in
1711 let ins = float a *. iv1
1712 and outs = float (n-b) *. iv1 in
1713 let ones = b - a in
1714 ins +. outs +. float ones
1716 let rec set (_N, _A, _B) y sy =
1717 let sum = summa 1.0 _N _A _B in
1718 let dy = float (y - sy) in
1719 state.ghyll <- (
1720 let rec gf n y1 o =
1721 if n >= _N
1722 then state.ghyll <- noghyll
1723 else
1724 let go n =
1725 let s = scroll n _N _A _B in
1726 let y1 = y1 +. ((s *. dy) /. sum) in
1727 gotoy_and_clear_text (truncate y1);
1728 state.ghyll <- gf (n+1) y1;
1730 match o with
1731 | None -> go n
1732 | Some y' -> set (_N/2, 0, 0) y' state.y
1734 gf 0 (float state.y)
1737 match conf.ghyllscroll with
1738 | None ->
1739 gotoy_and_clear_text y
1740 | Some nab ->
1741 if state.ghyll == noghyll
1742 then set nab y state.y
1743 else state.ghyll (Some y)
1746 let gotopage n top =
1747 let y, h = getpageyh n in
1748 let y = y + (truncate (top *. float h)) in
1749 gotoghyll y
1752 let gotopage1 n top =
1753 let y = getpagey n in
1754 let y = y + top in
1755 gotoghyll y
1758 let invalidate s f =
1759 state.layout <- [];
1760 state.pdims <- [];
1761 state.rects <- [];
1762 state.rects1 <- [];
1763 match state.geomcmds with
1764 | ps, [] when String.length ps = 0 ->
1765 f ();
1766 state.geomcmds <- s, [];
1768 | ps, [] ->
1769 state.geomcmds <- ps, [s, f];
1771 | ps, (s', _) :: rest when s' = s ->
1772 state.geomcmds <- ps, ((s, f) :: rest);
1774 | ps, cmds ->
1775 state.geomcmds <- ps, ((s, f) :: cmds);
1778 let opendoc path password =
1779 state.path <- path;
1780 state.password <- password;
1781 state.gen <- state.gen + 1;
1782 state.docinfo <- [];
1784 setaalevel conf.aalevel;
1785 Wsi.settitle ("llpp " ^ Filename.basename path);
1786 wcmd "open %s\000%s\000" path password;
1787 invalidate "reqlayout"
1788 (fun () ->
1789 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1792 let scalecolor c =
1793 let c = c *. conf.colorscale in
1794 (c, c, c);
1797 let scalecolor2 (r, g, b) =
1798 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1801 let represent () =
1802 let docolumns = function
1803 | Csingle -> ()
1805 | Cmulti ((columns, coverA, coverB), _) ->
1806 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1807 let rec loop pageno pdimno pdim x y rowh pdims =
1808 if pageno = state.pagecount
1809 then ()
1810 else
1811 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1812 match pdims with
1813 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1814 pdimno+1, pdim, rest
1815 | _ ->
1816 pdimno, pdim, pdims
1818 let x, y, rowh' =
1819 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1820 then (
1821 (conf.winw - state.scrollw - w) / 2,
1822 y + rowh + conf.interpagespace, h
1824 else (
1825 if (pageno - coverA) mod columns = 0
1826 then 0, y + rowh + conf.interpagespace, h
1827 else x, y, max rowh h
1830 let rec fixrow m = if m = pageno then () else
1831 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1832 if h < rowh
1833 then (
1834 let y = y + (rowh - h) / 2 in
1835 a.(m) <- (pdimno, x, y, pdim);
1837 fixrow (m+1)
1839 if pageno > 1 && (pageno - coverA) mod columns = 0
1840 then fixrow (pageno - columns);
1841 a.(pageno) <- (pdimno, x, y, pdim);
1842 let x = x + w + xoff*2 + conf.interpagespace in
1843 loop (pageno+1) pdimno pdim x y rowh' pdims
1845 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1846 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1848 | Csplit (c, _) ->
1849 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1850 let rec loop pageno pdimno pdim y pdims =
1851 if pageno = state.pagecount
1852 then ()
1853 else
1854 let pdimno, ((_, w, h, _) as pdim), pdims =
1855 match pdims with
1856 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1857 pdimno+1, pdim, rest
1858 | _ ->
1859 pdimno, pdim, pdims
1861 let cw = w / c in
1862 let rec loop1 n x y =
1863 if n = c then y else (
1864 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1865 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1868 let y = loop1 0 0 y in
1869 loop (pageno+1) pdimno pdim y pdims
1871 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1872 conf.columns <- Csplit (c, a);
1874 docolumns conf.columns;
1875 state.maxy <- calcheight ();
1876 state.hscrollh <-
1877 if state.w <= conf.winw - state.scrollw
1878 then 0
1879 else state.scrollw
1881 match state.mode with
1882 | Birdseye (_, _, pageno, _, _) ->
1883 let y, h = getpageyh pageno in
1884 let top = (conf.winh - h) / 2 in
1885 gotoy (max 0 (y - top))
1886 | _ -> gotoanchor state.anchor
1889 let reshape w h =
1890 GlDraw.viewport 0 0 w h;
1891 let firsttime = state.geomcmds == firstgeomcmds in
1892 if not firsttime && nogeomcmds state.geomcmds
1893 then state.anchor <- getanchor ();
1895 conf.winw <- w;
1896 let w = truncate (float w *. conf.zoom) - state.scrollw in
1897 let w = max w 2 in
1898 conf.winh <- h;
1899 setfontsize fstate.fontsize;
1900 GlMat.mode `modelview;
1901 GlMat.load_identity ();
1903 GlMat.mode `projection;
1904 GlMat.load_identity ();
1905 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1906 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1907 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1909 let relx =
1910 if conf.zoom <= 1.0
1911 then 0.0
1912 else float state.x /. float state.w
1914 invalidate "geometry"
1915 (fun () ->
1916 state.w <- w;
1917 if not firsttime
1918 then state.x <- truncate (relx *. float w);
1919 let w =
1920 match conf.columns with
1921 | Csingle -> w
1922 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1923 | Csplit (c, _) -> w * c
1925 wcmd "geometry %d %d" w h);
1928 let enttext () =
1929 let len = String.length state.text in
1930 let drawstring s =
1931 let hscrollh =
1932 match state.mode with
1933 | Textentry _
1934 | View -> state.hscrollh
1935 | _ -> 0
1937 let rect x w =
1938 GlDraw.rect
1939 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1940 (x+.w, float (conf.winh - hscrollh))
1943 let w = float (conf.winw - state.scrollw - 1) in
1944 if state.progress >= 0.0 && state.progress < 1.0
1945 then (
1946 GlDraw.color (0.3, 0.3, 0.3);
1947 let w1 = w *. state.progress in
1948 rect 0.0 w1;
1949 GlDraw.color (0.0, 0.0, 0.0);
1950 rect w1 (w-.w1)
1952 else (
1953 GlDraw.color (0.0, 0.0, 0.0);
1954 rect 0.0 w;
1957 GlDraw.color (1.0, 1.0, 1.0);
1958 drawstring fstate.fontsize
1959 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1961 let s =
1962 match state.mode with
1963 | Textentry ((prefix, text, _, _, _), _) ->
1964 let s =
1965 if len > 0
1966 then
1967 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1968 else
1969 Printf.sprintf "%s%s_" prefix text
1973 | _ -> state.text
1975 let s =
1976 if state.newerrmsgs
1977 then (
1978 if not (istextentry state.mode)
1979 then
1980 let s1 = "(press 'e' to review error messasges)" in
1981 if String.length s > 0 then s ^ " " ^ s1 else s1
1982 else s
1984 else s
1986 if String.length s > 0
1987 then drawstring s
1990 let gctiles () =
1991 let len = Queue.length state.tilelru in
1992 let rec loop qpos =
1993 if state.memused <= conf.memlimit
1994 then ()
1995 else (
1996 if qpos < len
1997 then
1998 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1999 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2000 let (_, pw, ph, _) = getpagedim n in
2002 gen = state.gen
2003 && colorspace = conf.colorspace
2004 && angle = conf.angle
2005 && pagew = pw
2006 && pageh = ph
2007 && (
2008 let layout =
2009 match state.throttle with
2010 | None ->
2011 if conf.preload
2012 then preloadlayout state.layout
2013 else state.layout
2014 | Some (layout, _, _) ->
2015 layout
2017 let x = col*conf.tilew
2018 and y = row*conf.tileh in
2019 tilevisible layout n x y
2021 then Queue.push lruitem state.tilelru
2022 else (
2023 wcmd "freetile %s" p;
2024 state.memused <- state.memused - s;
2025 state.uioh#infochanged Memused;
2026 Hashtbl.remove state.tilemap k;
2028 loop (qpos+1)
2031 loop 0
2034 let flushtiles () =
2035 Queue.iter (fun (k, p, s) ->
2036 wcmd "freetile %s" p;
2037 state.memused <- state.memused - s;
2038 state.uioh#infochanged Memused;
2039 Hashtbl.remove state.tilemap k;
2040 ) state.tilelru;
2041 Queue.clear state.tilelru;
2042 load state.layout;
2045 let logcurrently = function
2046 | Idle -> dolog "Idle"
2047 | Loading (l, gen) ->
2048 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2049 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2050 dolog
2051 "Tiling %d[%d,%d] page=%s cs=%s angle"
2052 l.pageno col row pageopaque
2053 (colorspace_to_string colorspace)
2055 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2056 angle gen conf.angle state.gen
2057 tilew tileh
2058 conf.tilew conf.tileh
2060 | Outlining _ ->
2061 dolog "outlining"
2064 let act cmds =
2065 (* dolog "%S" cmds; *)
2066 let op, args =
2067 let spacepos =
2068 try String.index cmds ' '
2069 with Not_found -> -1
2071 if spacepos = -1
2072 then cmds, ""
2073 else
2074 let l = String.length cmds in
2075 let op = String.sub cmds 0 spacepos in
2076 op, begin
2077 if l - spacepos < 2 then ""
2078 else String.sub cmds (spacepos+1) (l-spacepos-1)
2081 match op with
2082 | "clear" ->
2083 state.uioh#infochanged Pdim;
2084 state.pdims <- [];
2086 | "clearrects" ->
2087 state.rects <- state.rects1;
2088 G.postRedisplay "clearrects";
2090 | "continue" ->
2091 let n =
2092 try Scanf.sscanf args "%u" (fun n -> n)
2093 with exn ->
2094 dolog "error processing 'continue' %S: %s"
2095 cmds (Printexc.to_string exn);
2096 exit 1;
2098 state.pagecount <- n;
2099 begin match state.currently with
2100 | Outlining l ->
2101 state.currently <- Idle;
2102 state.outlines <- Array.of_list (List.rev l)
2103 | _ -> ()
2104 end;
2106 let cur, cmds = state.geomcmds in
2107 if String.length cur = 0
2108 then failwith "umpossible";
2110 begin match List.rev cmds with
2111 | [] ->
2112 state.geomcmds <- "", [];
2113 represent ();
2114 | (s, f) :: rest ->
2115 f ();
2116 state.geomcmds <- s, List.rev rest;
2117 end;
2118 if conf.maxwait = None
2119 then G.postRedisplay "continue";
2121 | "title" ->
2122 Wsi.settitle args
2124 | "msg" ->
2125 showtext ' ' args
2127 | "vmsg" ->
2128 if conf.verbose
2129 then showtext ' ' args
2131 | "progress" ->
2132 let progress, text =
2134 Scanf.sscanf args "%f %n"
2135 (fun f pos ->
2136 f, String.sub args pos (String.length args - pos))
2137 with exn ->
2138 dolog "error processing 'progress' %S: %s"
2139 cmds (Printexc.to_string exn);
2140 exit 1;
2142 state.text <- text;
2143 state.progress <- progress;
2144 G.postRedisplay "progress"
2146 | "firstmatch" ->
2147 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2149 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2150 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2151 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2152 with exn ->
2153 dolog "error processing 'firstmatch' %S: %s"
2154 cmds (Printexc.to_string exn);
2155 exit 1;
2157 let y = (getpagey pageno) + truncate y0 in
2158 addnav ();
2159 gotoy y;
2160 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2162 | "match" ->
2163 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2165 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2166 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2167 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2168 with exn ->
2169 dolog "error processing 'match' %S: %s"
2170 cmds (Printexc.to_string exn);
2171 exit 1;
2173 state.rects1 <-
2174 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2176 | "page" ->
2177 let pageopaque, t =
2179 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2180 with exn ->
2181 dolog "error processing 'page' %S: %s"
2182 cmds (Printexc.to_string exn);
2183 exit 1;
2185 begin match state.currently with
2186 | Loading (l, gen) ->
2187 vlog "page %d took %f sec" l.pageno t;
2188 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2189 begin match state.throttle with
2190 | None ->
2191 let preloadedpages =
2192 if conf.preload
2193 then preloadlayout state.layout
2194 else state.layout
2196 let evict () =
2197 let module IntSet =
2198 Set.Make (struct type t = int let compare = (-) end) in
2199 let set =
2200 List.fold_left (fun s l -> IntSet.add l.pageno s)
2201 IntSet.empty preloadedpages
2203 let evictedpages =
2204 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2205 if not (IntSet.mem pageno set)
2206 then (
2207 wcmd "freepage %s" opaque;
2208 key :: accu
2210 else accu
2211 ) state.pagemap []
2213 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2215 evict ();
2216 state.currently <- Idle;
2217 if gen = state.gen
2218 then (
2219 tilepage l.pageno pageopaque state.layout;
2220 load state.layout;
2221 load preloadedpages;
2222 if pagevisible state.layout l.pageno
2223 && layoutready state.layout
2224 then G.postRedisplay "page";
2227 | Some (layout, _, _) ->
2228 state.currently <- Idle;
2229 tilepage l.pageno pageopaque layout;
2230 load state.layout
2231 end;
2233 | _ ->
2234 dolog "Inconsistent loading state";
2235 logcurrently state.currently;
2236 exit 1
2239 | "tile" ->
2240 let (x, y, opaque, size, t) =
2242 Scanf.sscanf args "%u %u %s %u %f"
2243 (fun x y p size t -> (x, y, p, size, t))
2244 with exn ->
2245 dolog "error processing 'tile' %S: %s"
2246 cmds (Printexc.to_string exn);
2247 exit 1;
2249 begin match state.currently with
2250 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2251 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2253 if tilew != conf.tilew || tileh != conf.tileh
2254 then (
2255 wcmd "freetile %s" opaque;
2256 state.currently <- Idle;
2257 load state.layout;
2259 else (
2260 puttileopaque l col row gen cs angle opaque size t;
2261 state.memused <- state.memused + size;
2262 state.uioh#infochanged Memused;
2263 gctiles ();
2264 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2265 opaque, size) state.tilelru;
2267 let layout =
2268 match state.throttle with
2269 | None -> state.layout
2270 | Some (layout, _, _) -> layout
2273 state.currently <- Idle;
2274 if gen = state.gen
2275 && conf.colorspace = cs
2276 && conf.angle = angle
2277 && tilevisible layout l.pageno x y
2278 then conttiling l.pageno pageopaque;
2280 begin match state.throttle with
2281 | None ->
2282 preload state.layout;
2283 if gen = state.gen
2284 && conf.colorspace = cs
2285 && conf.angle = angle
2286 && tilevisible state.layout l.pageno x y
2287 then G.postRedisplay "tile nothrottle";
2289 | Some (layout, y, _) ->
2290 let ready = layoutready layout in
2291 if ready
2292 then (
2293 state.y <- y;
2294 state.layout <- layout;
2295 state.throttle <- None;
2296 G.postRedisplay "throttle";
2298 else load layout;
2299 end;
2302 | _ ->
2303 dolog "Inconsistent tiling state";
2304 logcurrently state.currently;
2305 exit 1
2308 | "pdim" ->
2309 let pdim =
2311 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2312 with exn ->
2313 dolog "error processing 'pdim' %S: %s"
2314 cmds (Printexc.to_string exn);
2315 exit 1;
2317 state.uioh#infochanged Pdim;
2318 state.pdims <- pdim :: state.pdims
2320 | "o" ->
2321 let (l, n, t, h, pos) =
2323 Scanf.sscanf args "%u %u %d %u %n"
2324 (fun l n t h pos -> l, n, t, h, pos)
2325 with exn ->
2326 dolog "error processing 'o' %S: %s"
2327 cmds (Printexc.to_string exn);
2328 exit 1;
2330 let s = String.sub args pos (String.length args - pos) in
2331 let outline = (s, l, (n, float t /. float h)) in
2332 begin match state.currently with
2333 | Outlining outlines ->
2334 state.currently <- Outlining (outline :: outlines)
2335 | Idle ->
2336 state.currently <- Outlining [outline]
2337 | currently ->
2338 dolog "invalid outlining state";
2339 logcurrently currently
2342 | "info" ->
2343 state.docinfo <- (1, args) :: state.docinfo
2345 | "infoend" ->
2346 state.uioh#infochanged Docinfo;
2347 state.docinfo <- List.rev state.docinfo
2349 | _ ->
2350 dolog "unknown cmd `%S'" cmds
2353 let onhist cb =
2354 let rc = cb.rc in
2355 let action = function
2356 | HCprev -> cbget cb ~-1
2357 | HCnext -> cbget cb 1
2358 | HCfirst -> cbget cb ~-(cb.rc)
2359 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2360 and cancel () = cb.rc <- rc
2361 in (action, cancel)
2364 let search pattern forward =
2365 if String.length pattern > 0
2366 then
2367 let pn, py =
2368 match state.layout with
2369 | [] -> 0, 0
2370 | l :: _ ->
2371 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2373 wcmd "search %d %d %d %d,%s\000"
2374 (btod conf.icase) pn py (btod forward) pattern;
2377 let intentry text key =
2378 let c =
2379 if key >= 32 && key < 127
2380 then Char.chr key
2381 else '\000'
2383 match c with
2384 | '0' .. '9' ->
2385 let text = addchar text c in
2386 TEcont text
2388 | _ ->
2389 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2390 TEcont text
2393 let textentry text key =
2394 if key land 0xff00 = 0xff00
2395 then TEcont text
2396 else TEcont (text ^ Wsi.toutf8 key)
2399 let reqlayout angle proportional =
2400 match state.throttle with
2401 | None ->
2402 if nogeomcmds state.geomcmds
2403 then state.anchor <- getanchor ();
2404 conf.angle <- angle mod 360;
2405 if conf.angle != 0
2406 then (
2407 match state.mode with
2408 | LinkNav _ -> state.mode <- View
2409 | _ -> ()
2411 conf.proportional <- proportional;
2412 invalidate "reqlayout"
2413 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2414 | _ -> ()
2417 let settrim trimmargins trimfuzz =
2418 if nogeomcmds state.geomcmds
2419 then state.anchor <- getanchor ();
2420 conf.trimmargins <- trimmargins;
2421 conf.trimfuzz <- trimfuzz;
2422 let x0, y0, x1, y1 = trimfuzz in
2423 invalidate "settrim"
2424 (fun () ->
2425 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2426 Hashtbl.iter (fun _ opaque ->
2427 wcmd "freepage %s" opaque;
2428 ) state.pagemap;
2429 Hashtbl.clear state.pagemap;
2432 let setzoom zoom =
2433 match state.throttle with
2434 | None ->
2435 let zoom = max 0.01 zoom in
2436 if zoom <> conf.zoom
2437 then (
2438 state.prevzoom <- conf.zoom;
2439 conf.zoom <- zoom;
2440 reshape conf.winw conf.winh;
2441 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2444 | Some (layout, y, started) ->
2445 let time =
2446 match conf.maxwait with
2447 | None -> 0.0
2448 | Some t -> t
2450 let dt = now () -. started in
2451 if dt > time
2452 then (
2453 state.y <- y;
2454 load layout;
2458 let setcolumns mode columns coverA coverB =
2459 if columns < 0
2460 then (
2461 if isbirdseye mode
2462 then showtext '!' "split mode doesn't work in bird's eye"
2463 else (
2464 conf.columns <- Csplit (-columns, [||]);
2465 state.x <- 0;
2466 conf.zoom <- 1.0;
2469 else (
2470 if columns < 2
2471 then (
2472 conf.columns <- Csingle;
2473 state.x <- 0;
2474 setzoom 1.0;
2476 else (
2477 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2478 conf.zoom <- 1.0;
2481 reshape conf.winw conf.winh;
2484 let enterbirdseye () =
2485 let zoom = float conf.thumbw /. float conf.winw in
2486 let birdseyepageno =
2487 let cy = conf.winh / 2 in
2488 let fold = function
2489 | [] -> 0
2490 | l :: rest ->
2491 let rec fold best = function
2492 | [] -> best.pageno
2493 | l :: rest ->
2494 let d = cy - (l.pagedispy + l.pagevh/2)
2495 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2496 if abs d < abs dbest
2497 then fold l rest
2498 else best.pageno
2499 in fold l rest
2501 fold state.layout
2503 state.mode <- Birdseye (
2504 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2506 conf.zoom <- zoom;
2507 conf.presentation <- false;
2508 conf.interpagespace <- 10;
2509 conf.hlinks <- false;
2510 state.x <- 0;
2511 state.mstate <- Mnone;
2512 conf.maxwait <- None;
2513 conf.columns <- (
2514 match conf.beyecolumns with
2515 | Some c ->
2516 conf.zoom <- 1.0;
2517 Cmulti ((c, 0, 0), [||])
2518 | None -> Csingle
2520 Wsi.setcursor Wsi.CURSOR_INHERIT;
2521 if conf.verbose
2522 then
2523 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2524 (100.0*.zoom)
2525 else
2526 state.text <- ""
2528 reshape conf.winw conf.winh;
2531 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2532 state.mode <- View;
2533 conf.zoom <- c.zoom;
2534 conf.presentation <- c.presentation;
2535 conf.interpagespace <- c.interpagespace;
2536 conf.maxwait <- c.maxwait;
2537 conf.hlinks <- c.hlinks;
2538 conf.beyecolumns <- (
2539 match conf.columns with
2540 | Cmulti ((c, _, _), _) -> Some c
2541 | Csingle -> None
2542 | Csplit _ -> assert false
2544 conf.columns <- (
2545 match c.columns with
2546 | Cmulti (c, _) -> Cmulti (c, [||])
2547 | Csingle -> Csingle
2548 | Csplit _ -> failwith "leaving bird's eye split mode"
2550 state.x <- leftx;
2551 if conf.verbose
2552 then
2553 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2554 (100.0*.conf.zoom)
2556 reshape conf.winw conf.winh;
2557 state.anchor <- if goback then anchor else (pageno, 0.0);
2560 let togglebirdseye () =
2561 match state.mode with
2562 | Birdseye vals -> leavebirdseye vals true
2563 | View -> enterbirdseye ()
2564 | _ -> ()
2567 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2568 let pageno = max 0 (pageno - incr) in
2569 let rec loop = function
2570 | [] -> gotopage1 pageno 0
2571 | l :: _ when l.pageno = pageno ->
2572 if l.pagedispy >= 0 && l.pagey = 0
2573 then G.postRedisplay "upbirdseye"
2574 else gotopage1 pageno 0
2575 | _ :: rest -> loop rest
2577 loop state.layout;
2578 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2581 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2582 let pageno = min (state.pagecount - 1) (pageno + incr) in
2583 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2584 let rec loop = function
2585 | [] ->
2586 let y, h = getpageyh pageno in
2587 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2588 gotoy (clamp dy)
2589 | l :: _ when l.pageno = pageno ->
2590 if l.pagevh != l.pageh
2591 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2592 else G.postRedisplay "downbirdseye"
2593 | _ :: rest -> loop rest
2595 loop state.layout
2598 let optentry mode _ key =
2599 let btos b = if b then "on" else "off" in
2600 if key >= 32 && key < 127
2601 then
2602 let c = Char.chr key in
2603 match c with
2604 | 's' ->
2605 let ondone s =
2606 try conf.scrollstep <- int_of_string s with exc ->
2607 state.text <- Printf.sprintf "bad integer `%s': %s"
2608 s (Printexc.to_string exc)
2610 TEswitch ("scroll step: ", "", None, intentry, ondone)
2612 | 'A' ->
2613 let ondone s =
2615 conf.autoscrollstep <- int_of_string s;
2616 if state.autoscroll <> None
2617 then state.autoscroll <- Some conf.autoscrollstep
2618 with exc ->
2619 state.text <- Printf.sprintf "bad integer `%s': %s"
2620 s (Printexc.to_string exc)
2622 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2624 | 'C' ->
2625 let mode = state.mode in
2626 let ondone s =
2628 let n, a, b = multicolumns_of_string s in
2629 setcolumns mode n a b;
2630 with exc ->
2631 state.text <- Printf.sprintf "bad columns `%s': %s"
2632 s (Printexc.to_string exc)
2634 TEswitch ("columns: ", "", None, textentry, ondone)
2636 | 'Z' ->
2637 let ondone s =
2639 let zoom = float (int_of_string s) /. 100.0 in
2640 setzoom zoom
2641 with exc ->
2642 state.text <- Printf.sprintf "bad integer `%s': %s"
2643 s (Printexc.to_string exc)
2645 TEswitch ("zoom: ", "", None, intentry, ondone)
2647 | 't' ->
2648 let ondone s =
2650 conf.thumbw <- bound (int_of_string s) 2 4096;
2651 state.text <-
2652 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2653 begin match mode with
2654 | Birdseye beye ->
2655 leavebirdseye beye false;
2656 enterbirdseye ();
2657 | _ -> ();
2659 with exc ->
2660 state.text <- Printf.sprintf "bad integer `%s': %s"
2661 s (Printexc.to_string exc)
2663 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2665 | 'R' ->
2666 let ondone s =
2667 match try
2668 Some (int_of_string s)
2669 with exc ->
2670 state.text <- Printf.sprintf "bad integer `%s': %s"
2671 s (Printexc.to_string exc);
2672 None
2673 with
2674 | Some angle -> reqlayout angle conf.proportional
2675 | None -> ()
2677 TEswitch ("rotation: ", "", None, intentry, ondone)
2679 | 'i' ->
2680 conf.icase <- not conf.icase;
2681 TEdone ("case insensitive search " ^ (btos conf.icase))
2683 | 'p' ->
2684 conf.preload <- not conf.preload;
2685 gotoy state.y;
2686 TEdone ("preload " ^ (btos conf.preload))
2688 | 'v' ->
2689 conf.verbose <- not conf.verbose;
2690 TEdone ("verbose " ^ (btos conf.verbose))
2692 | 'd' ->
2693 conf.debug <- not conf.debug;
2694 TEdone ("debug " ^ (btos conf.debug))
2696 | 'h' ->
2697 conf.maxhfit <- not conf.maxhfit;
2698 state.maxy <-
2699 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2700 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2702 | 'c' ->
2703 conf.crophack <- not conf.crophack;
2704 TEdone ("crophack " ^ btos conf.crophack)
2706 | 'a' ->
2707 let s =
2708 match conf.maxwait with
2709 | None ->
2710 conf.maxwait <- Some infinity;
2711 "always wait for page to complete"
2712 | Some _ ->
2713 conf.maxwait <- None;
2714 "show placeholder if page is not ready"
2716 TEdone s
2718 | 'f' ->
2719 conf.underinfo <- not conf.underinfo;
2720 TEdone ("underinfo " ^ btos conf.underinfo)
2722 | 'P' ->
2723 conf.savebmarks <- not conf.savebmarks;
2724 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2726 | 'S' ->
2727 let ondone s =
2729 let pageno, py =
2730 match state.layout with
2731 | [] -> 0, 0
2732 | l :: _ ->
2733 l.pageno, l.pagey
2735 conf.interpagespace <- int_of_string s;
2736 state.maxy <- calcheight ();
2737 let y = getpagey pageno in
2738 gotoy (y + py)
2739 with exc ->
2740 state.text <- Printf.sprintf "bad integer `%s': %s"
2741 s (Printexc.to_string exc)
2743 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2745 | 'l' ->
2746 reqlayout conf.angle (not conf.proportional);
2747 TEdone ("proportional display " ^ btos conf.proportional)
2749 | 'T' ->
2750 settrim (not conf.trimmargins) conf.trimfuzz;
2751 TEdone ("trim margins " ^ btos conf.trimmargins)
2753 | 'I' ->
2754 conf.invert <- not conf.invert;
2755 TEdone ("invert colors " ^ btos conf.invert)
2757 | 'x' ->
2758 let ondone s =
2759 cbput state.hists.sel s;
2760 conf.selcmd <- s;
2762 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2763 textentry, ondone)
2765 | _ ->
2766 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2767 TEstop
2768 else
2769 TEcont state.text
2772 class type lvsource = object
2773 method getitemcount : int
2774 method getitem : int -> (string * int)
2775 method hasaction : int -> bool
2776 method exit :
2777 uioh:uioh ->
2778 cancel:bool ->
2779 active:int ->
2780 first:int ->
2781 pan:int ->
2782 qsearch:string ->
2783 uioh option
2784 method getactive : int
2785 method getfirst : int
2786 method getqsearch : string
2787 method setqsearch : string -> unit
2788 method getpan : int
2789 end;;
2791 class virtual lvsourcebase = object
2792 val mutable m_active = 0
2793 val mutable m_first = 0
2794 val mutable m_qsearch = ""
2795 val mutable m_pan = 0
2796 method getactive = m_active
2797 method getfirst = m_first
2798 method getqsearch = m_qsearch
2799 method getpan = m_pan
2800 method setqsearch s = m_qsearch <- s
2801 end;;
2803 let withoutlastutf8 s =
2804 let len = String.length s in
2805 if len = 0
2806 then s
2807 else
2808 let rec find pos =
2809 if pos = 0
2810 then pos
2811 else
2812 let b = Char.code s.[pos] in
2813 if b land 0b110000 = 0b11000000
2814 then find (pos-1)
2815 else pos-1
2817 let first =
2818 if Char.code s.[len-1] land 0x80 = 0
2819 then len-1
2820 else find (len-1)
2822 String.sub s 0 first;
2825 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2826 let enttext te =
2827 state.mode <- Textentry (te, onleave);
2828 state.text <- "";
2829 enttext ();
2830 G.postRedisplay "textentrykeyboard enttext";
2832 let histaction cmd =
2833 match opthist with
2834 | None -> ()
2835 | Some (action, _) ->
2836 state.mode <- Textentry (
2837 (c, action cmd, opthist, onkey, ondone), onleave
2839 G.postRedisplay "textentry histaction"
2841 match key with
2842 | 0xff08 -> (* backspace *)
2843 let s = withoutlastutf8 text in
2844 let len = String.length s in
2845 if len = 0
2846 then (
2847 onleave Cancel;
2848 G.postRedisplay "textentrykeyboard after cancel";
2850 else (
2851 enttext (c, s, opthist, onkey, ondone)
2854 | 0xff0d ->
2855 ondone text;
2856 onleave Confirm;
2857 G.postRedisplay "textentrykeyboard after confirm"
2859 | 0xff52 -> histaction HCprev
2860 | 0xff54 -> histaction HCnext
2861 | 0xff50 -> histaction HCfirst
2862 | 0xff57 -> histaction HClast
2864 | 0xff1b -> (* escape*)
2865 if String.length text = 0
2866 then (
2867 begin match opthist with
2868 | None -> ()
2869 | Some (_, onhistcancel) -> onhistcancel ()
2870 end;
2871 onleave Cancel;
2872 state.text <- "";
2873 G.postRedisplay "textentrykeyboard after cancel2"
2875 else (
2876 enttext (c, "", opthist, onkey, ondone)
2879 | 0xff9f | 0xffff -> () (* delete *)
2881 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2882 begin match onkey text key with
2883 | TEdone text ->
2884 ondone text;
2885 onleave Confirm;
2886 G.postRedisplay "textentrykeyboard after confirm2";
2888 | TEcont text ->
2889 enttext (c, text, opthist, onkey, ondone);
2891 | TEstop ->
2892 onleave Cancel;
2893 G.postRedisplay "textentrykeyboard after cancel3"
2895 | TEswitch te ->
2896 state.mode <- Textentry (te, onleave);
2897 G.postRedisplay "textentrykeyboard switch";
2898 end;
2900 | _ ->
2901 vlog "unhandled key %s" (Wsi.keyname key)
2904 let firstof first active =
2905 if first > active || abs (first - active) > fstate.maxrows - 1
2906 then max 0 (active - (fstate.maxrows/2))
2907 else first
2910 let calcfirst first active =
2911 if active > first
2912 then
2913 let rows = active - first in
2914 if rows > fstate.maxrows then active - fstate.maxrows else first
2915 else active
2918 let scrollph y maxy =
2919 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2920 let sh = float conf.winh /. sh in
2921 let sh = max sh (float conf.scrollh) in
2923 let percent =
2924 if y = state.maxy
2925 then 1.0
2926 else float y /. float maxy
2928 let position = (float conf.winh -. sh) *. percent in
2930 let position =
2931 if position +. sh > float conf.winh
2932 then float conf.winh -. sh
2933 else position
2935 position, sh;
2938 let coe s = (s :> uioh);;
2940 class listview ~(source:lvsource) ~trusted ~modehash =
2941 object (self)
2942 val m_pan = source#getpan
2943 val m_first = source#getfirst
2944 val m_active = source#getactive
2945 val m_qsearch = source#getqsearch
2946 val m_prev_uioh = state.uioh
2948 method private elemunder y =
2949 let n = y / (fstate.fontsize+1) in
2950 if m_first + n < source#getitemcount
2951 then (
2952 if source#hasaction (m_first + n)
2953 then Some (m_first + n)
2954 else None
2956 else None
2958 method display =
2959 Gl.enable `blend;
2960 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2961 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2962 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2963 GlDraw.color (1., 1., 1.);
2964 Gl.enable `texture_2d;
2965 let fs = fstate.fontsize in
2966 let nfs = fs + 1 in
2967 let ww = fstate.wwidth in
2968 let tabw = 30.0*.ww in
2969 let itemcount = source#getitemcount in
2970 let rec loop row =
2971 if (row - m_first) * nfs > conf.winh
2972 then ()
2973 else (
2974 if row >= 0 && row < itemcount
2975 then (
2976 let (s, level) = source#getitem row in
2977 let y = (row - m_first) * nfs in
2978 let x = 5.0 +. float (level + m_pan) *. ww in
2979 if row = m_active
2980 then (
2981 Gl.disable `texture_2d;
2982 GlDraw.polygon_mode `both `line;
2983 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2984 GlDraw.rect (1., float (y + 1))
2985 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2986 GlDraw.polygon_mode `both `fill;
2987 GlDraw.color (1., 1., 1.);
2988 Gl.enable `texture_2d;
2991 let drawtabularstring s =
2992 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2993 if trusted
2994 then
2995 let tabpos = try String.index s '\t' with Not_found -> -1 in
2996 if tabpos > 0
2997 then
2998 let len = String.length s - tabpos - 1 in
2999 let s1 = String.sub s 0 tabpos
3000 and s2 = String.sub s (tabpos + 1) len in
3001 let nx = drawstr x s1 in
3002 let sw = nx -. x in
3003 let x = x +. (max tabw sw) in
3004 drawstr x s2
3005 else
3006 drawstr x s
3007 else
3008 drawstr x s
3010 let _ = drawtabularstring s in
3011 loop (row+1)
3015 loop m_first;
3016 Gl.disable `blend;
3017 Gl.disable `texture_2d;
3019 method updownlevel incr =
3020 let len = source#getitemcount in
3021 let curlevel =
3022 if m_active >= 0 && m_active < len
3023 then snd (source#getitem m_active)
3024 else -1
3026 let rec flow i =
3027 if i = len then i-1 else if i = -1 then 0 else
3028 let _, l = source#getitem i in
3029 if l != curlevel then i else flow (i+incr)
3031 let active = flow m_active in
3032 let first = calcfirst m_first active in
3033 G.postRedisplay "outline updownlevel";
3034 {< m_active = active; m_first = first >}
3036 method private key1 key mask =
3037 let set1 active first qsearch =
3038 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3040 let search active pattern incr =
3041 let dosearch re =
3042 let rec loop n =
3043 if n >= 0 && n < source#getitemcount
3044 then (
3045 let s, _ = source#getitem n in
3047 (try ignore (Str.search_forward re s 0); true
3048 with Not_found -> false)
3049 then Some n
3050 else loop (n + incr)
3052 else None
3054 loop active
3057 let re = Str.regexp_case_fold pattern in
3058 dosearch re
3059 with Failure s ->
3060 state.text <- s;
3061 None
3063 let itemcount = source#getitemcount in
3064 let find start incr =
3065 let rec find i =
3066 if i = -1 || i = itemcount
3067 then -1
3068 else (
3069 if source#hasaction i
3070 then i
3071 else find (i + incr)
3074 find start
3076 let set active first =
3077 let first = bound first 0 (itemcount - fstate.maxrows) in
3078 state.text <- "";
3079 coe {< m_active = active; m_first = first >}
3081 let navigate incr =
3082 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3083 let active, first =
3084 let incr1 = if incr > 0 then 1 else -1 in
3085 if isvisible m_first m_active
3086 then
3087 let next =
3088 let next = m_active + incr in
3089 let next =
3090 if next < 0 || next >= itemcount
3091 then -1
3092 else find next incr1
3094 if next = -1 || abs (m_active - next) > fstate.maxrows
3095 then -1
3096 else next
3098 if next = -1
3099 then
3100 let first = m_first + incr in
3101 let first = bound first 0 (itemcount - 1) in
3102 let next =
3103 let next = m_active + incr in
3104 let next = bound next 0 (itemcount - 1) in
3105 find next ~-incr1
3107 let active = if next = -1 then m_active else next in
3108 active, first
3109 else
3110 let first = min next m_first in
3111 let first =
3112 if abs (next - first) > fstate.maxrows
3113 then first + incr
3114 else first
3116 next, first
3117 else
3118 let first = m_first + incr in
3119 let first = bound first 0 (itemcount - 1) in
3120 let active =
3121 let next = m_active + incr in
3122 let next = bound next 0 (itemcount - 1) in
3123 let next = find next incr1 in
3124 let active =
3125 if next = -1 || abs (m_active - first) > fstate.maxrows
3126 then (
3127 let active = if m_active = -1 then next else m_active in
3128 active
3130 else next
3132 if isvisible first active
3133 then active
3134 else -1
3136 active, first
3138 G.postRedisplay "listview navigate";
3139 set active first;
3141 match key with
3142 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3143 let incr = if key = 0x72 then -1 else 1 in
3144 let active, first =
3145 match search (m_active + incr) m_qsearch incr with
3146 | None ->
3147 state.text <- m_qsearch ^ " [not found]";
3148 m_active, m_first
3149 | Some active ->
3150 state.text <- m_qsearch;
3151 active, firstof m_first active
3153 G.postRedisplay "listview ctrl-r/s";
3154 set1 active first m_qsearch;
3156 | 0xff08 -> (* backspace *)
3157 if String.length m_qsearch = 0
3158 then coe self
3159 else (
3160 let qsearch = withoutlastutf8 m_qsearch in
3161 let len = String.length qsearch in
3162 if len = 0
3163 then (
3164 state.text <- "";
3165 G.postRedisplay "listview empty qsearch";
3166 set1 m_active m_first "";
3168 else
3169 let active, first =
3170 match search m_active qsearch ~-1 with
3171 | None ->
3172 state.text <- qsearch ^ " [not found]";
3173 m_active, m_first
3174 | Some active ->
3175 state.text <- qsearch;
3176 active, firstof m_first active
3178 G.postRedisplay "listview backspace qsearch";
3179 set1 active first qsearch
3182 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3183 let pattern = m_qsearch ^ Wsi.toutf8 key in
3184 let active, first =
3185 match search m_active pattern 1 with
3186 | None ->
3187 state.text <- pattern ^ " [not found]";
3188 m_active, m_first
3189 | Some active ->
3190 state.text <- pattern;
3191 active, firstof m_first active
3193 G.postRedisplay "listview qsearch add";
3194 set1 active first pattern;
3196 | 0xff1b -> (* escape *)
3197 state.text <- "";
3198 if String.length m_qsearch = 0
3199 then (
3200 G.postRedisplay "list view escape";
3201 begin
3202 match
3203 source#exit (coe self) true m_active m_first m_pan m_qsearch
3204 with
3205 | None -> m_prev_uioh
3206 | Some uioh -> uioh
3209 else (
3210 G.postRedisplay "list view kill qsearch";
3211 source#setqsearch "";
3212 coe {< m_qsearch = "" >}
3215 | 0xff0d -> (* return *)
3216 state.text <- "";
3217 let self = {< m_qsearch = "" >} in
3218 source#setqsearch "";
3219 let opt =
3220 G.postRedisplay "listview enter";
3221 if m_active >= 0 && m_active < source#getitemcount
3222 then (
3223 source#exit (coe self) false m_active m_first m_pan "";
3225 else (
3226 source#exit (coe self) true m_active m_first m_pan "";
3229 begin match opt with
3230 | None -> m_prev_uioh
3231 | Some uioh -> uioh
3234 | 0xff9f | 0xffff -> (* delete *)
3235 coe self
3237 | 0xff52 -> navigate ~-1 (* up *)
3238 | 0xff54 -> navigate 1 (* down *)
3239 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3240 | 0xff56 -> navigate fstate.maxrows (* next *)
3242 | 0xff53 -> (* right *)
3243 state.text <- "";
3244 G.postRedisplay "listview right";
3245 coe {< m_pan = m_pan - 1 >}
3247 | 0xff51 -> (* left *)
3248 state.text <- "";
3249 G.postRedisplay "listview left";
3250 coe {< m_pan = m_pan + 1 >}
3252 | 0xff50 -> (* home *)
3253 let active = find 0 1 in
3254 G.postRedisplay "listview home";
3255 set active 0;
3257 | 0xff57 -> (* end *)
3258 let first = max 0 (itemcount - fstate.maxrows) in
3259 let active = find (itemcount - 1) ~-1 in
3260 G.postRedisplay "listview end";
3261 set active first;
3263 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3264 coe self
3266 | _ ->
3267 dolog "listview unknown key %#x" key; coe self
3269 method key key mask =
3270 match state.mode with
3271 | Textentry te -> textentrykeyboard key mask te; coe self
3272 | _ -> self#key1 key mask
3274 method button button down x y _ =
3275 let opt =
3276 match button with
3277 | 1 when x > conf.winw - conf.scrollbw ->
3278 G.postRedisplay "listview scroll";
3279 if down
3280 then
3281 let _, position, sh = self#scrollph in
3282 if y > truncate position && y < truncate (position +. sh)
3283 then (
3284 state.mstate <- Mscrolly;
3285 Some (coe self)
3287 else
3288 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3289 let first = truncate (s *. float source#getitemcount) in
3290 let first = min source#getitemcount first in
3291 Some (coe {< m_first = first; m_active = first >})
3292 else (
3293 state.mstate <- Mnone;
3294 Some (coe self);
3296 | 1 when not down ->
3297 begin match self#elemunder y with
3298 | Some n ->
3299 G.postRedisplay "listview click";
3300 source#exit
3301 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3302 | _ ->
3303 Some (coe self)
3305 | n when (n == 4 || n == 5) && not down ->
3306 let len = source#getitemcount in
3307 let first =
3308 if n = 5 && m_first + fstate.maxrows >= len
3309 then
3310 m_first
3311 else
3312 let first = m_first + (if n == 4 then -1 else 1) in
3313 bound first 0 (len - 1)
3315 G.postRedisplay "listview wheel";
3316 Some (coe {< m_first = first >})
3317 | _ ->
3318 Some (coe self)
3320 match opt with
3321 | None -> m_prev_uioh
3322 | Some uioh -> uioh
3324 method motion _ y =
3325 match state.mstate with
3326 | Mscrolly ->
3327 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3328 let first = truncate (s *. float source#getitemcount) in
3329 let first = min source#getitemcount first in
3330 G.postRedisplay "listview motion";
3331 coe {< m_first = first; m_active = first >}
3332 | _ -> coe self
3334 method pmotion x y =
3335 if x < conf.winw - conf.scrollbw
3336 then
3337 let n =
3338 match self#elemunder y with
3339 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3340 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3342 let o =
3343 if n != m_active
3344 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3345 else self
3347 coe o
3348 else (
3349 Wsi.setcursor Wsi.CURSOR_INHERIT;
3350 coe self
3353 method infochanged _ = ()
3355 method scrollpw = (0, 0.0, 0.0)
3356 method scrollph =
3357 let nfs = fstate.fontsize + 1 in
3358 let y = m_first * nfs in
3359 let itemcount = source#getitemcount in
3360 let maxi = max 0 (itemcount - fstate.maxrows) in
3361 let maxy = maxi * nfs in
3362 let p, h = scrollph y maxy in
3363 conf.scrollbw, p, h
3365 method modehash = modehash
3366 end;;
3368 class outlinelistview ~source =
3369 object (self)
3370 inherit listview
3371 ~source:(source :> lvsource)
3372 ~trusted:false
3373 ~modehash:(findkeyhash conf "outline")
3374 as super
3376 method key key mask =
3377 let calcfirst first active =
3378 if active > first
3379 then
3380 let rows = active - first in
3381 if rows > fstate.maxrows then active - fstate.maxrows else first
3382 else active
3384 let navigate incr =
3385 let active = m_active + incr in
3386 let active = bound active 0 (source#getitemcount - 1) in
3387 let first = calcfirst m_first active in
3388 G.postRedisplay "outline navigate";
3389 coe {< m_active = active; m_first = first >}
3391 let ctrl = Wsi.withctrl mask in
3392 match key with
3393 | 110 when ctrl -> (* ctrl-n *)
3394 source#narrow m_qsearch;
3395 G.postRedisplay "outline ctrl-n";
3396 coe {< m_first = 0; m_active = 0 >}
3398 | 117 when ctrl -> (* ctrl-u *)
3399 source#denarrow;
3400 G.postRedisplay "outline ctrl-u";
3401 state.text <- "";
3402 coe {< m_first = 0; m_active = 0 >}
3404 | 108 when ctrl -> (* ctrl-l *)
3405 let first = m_active - (fstate.maxrows / 2) in
3406 G.postRedisplay "outline ctrl-l";
3407 coe {< m_first = first >}
3409 | 0xff9f | 0xffff -> (* delete *)
3410 source#remove m_active;
3411 G.postRedisplay "outline delete";
3412 let active = max 0 (m_active-1) in
3413 coe {< m_first = firstof m_first active;
3414 m_active = active >}
3416 | 0xff52 -> navigate ~-1 (* up *)
3417 | 0xff54 -> navigate 1 (* down *)
3418 | 0xff55 -> (* prior *)
3419 navigate ~-(fstate.maxrows)
3420 | 0xff56 -> (* next *)
3421 navigate fstate.maxrows
3423 | 0xff53 -> (* [ctrl-]right *)
3424 let o =
3425 if ctrl
3426 then (
3427 G.postRedisplay "outline ctrl right";
3428 {< m_pan = m_pan + 1 >}
3430 else self#updownlevel 1
3432 coe o
3434 | 0xff51 -> (* [ctrl-]left *)
3435 let o =
3436 if ctrl
3437 then (
3438 G.postRedisplay "outline ctrl left";
3439 {< m_pan = m_pan - 1 >}
3441 else self#updownlevel ~-1
3443 coe o
3445 | 0xff50 -> (* home *)
3446 G.postRedisplay "outline home";
3447 coe {< m_first = 0; m_active = 0 >}
3449 | 0xff57 -> (* end *)
3450 let active = source#getitemcount - 1 in
3451 let first = max 0 (active - fstate.maxrows) in
3452 G.postRedisplay "outline end";
3453 coe {< m_active = active; m_first = first >}
3455 | _ -> super#key key mask
3458 let outlinesource usebookmarks =
3459 let empty = [||] in
3460 (object
3461 inherit lvsourcebase
3462 val mutable m_items = empty
3463 val mutable m_orig_items = empty
3464 val mutable m_prev_items = empty
3465 val mutable m_narrow_pattern = ""
3466 val mutable m_hadremovals = false
3468 method getitemcount =
3469 Array.length m_items + (if m_hadremovals then 1 else 0)
3471 method getitem n =
3472 if n == Array.length m_items && m_hadremovals
3473 then
3474 ("[Confirm removal]", 0)
3475 else
3476 let s, n, _ = m_items.(n) in
3477 (s, n)
3479 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3480 ignore (uioh, first, qsearch);
3481 let confrimremoval = m_hadremovals && active = Array.length m_items in
3482 let items =
3483 if String.length m_narrow_pattern = 0
3484 then m_orig_items
3485 else m_items
3487 if not cancel
3488 then (
3489 if not confrimremoval
3490 then(
3491 let _, _, anchor = m_items.(active) in
3492 gotoanchor anchor;
3493 m_items <- items;
3495 else (
3496 state.bookmarks <- Array.to_list m_items;
3497 m_orig_items <- m_items;
3500 else m_items <- items;
3501 m_pan <- pan;
3502 None
3504 method hasaction _ = true
3506 method greetmsg =
3507 if Array.length m_items != Array.length m_orig_items
3508 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3509 else ""
3511 method narrow pattern =
3512 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3513 match reopt with
3514 | None -> ()
3515 | Some re ->
3516 let rec loop accu n =
3517 if n = -1
3518 then (
3519 m_narrow_pattern <- pattern;
3520 m_items <- Array.of_list accu
3522 else
3523 let (s, _, _) as o = m_items.(n) in
3524 let accu =
3525 if (try ignore (Str.search_forward re s 0); true
3526 with Not_found -> false)
3527 then o :: accu
3528 else accu
3530 loop accu (n-1)
3532 loop [] (Array.length m_items - 1)
3534 method denarrow =
3535 m_orig_items <- (
3536 if usebookmarks
3537 then Array.of_list state.bookmarks
3538 else state.outlines
3540 m_items <- m_orig_items
3542 method remove m =
3543 if usebookmarks
3544 then
3545 if m >= 0 && m < Array.length m_items
3546 then (
3547 m_hadremovals <- true;
3548 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3549 let n = if n >= m then n+1 else n in
3550 m_items.(n)
3554 method reset anchor items =
3555 m_hadremovals <- false;
3556 if m_orig_items == empty || m_prev_items != items
3557 then (
3558 m_orig_items <- items;
3559 if String.length m_narrow_pattern = 0
3560 then m_items <- items;
3562 m_prev_items <- items;
3563 let rely = getanchory anchor in
3564 let active =
3565 let rec loop n best bestd =
3566 if n = Array.length m_items
3567 then best
3568 else
3569 let (_, _, anchor) = m_items.(n) in
3570 let orely = getanchory anchor in
3571 let d = abs (orely - rely) in
3572 if d < bestd
3573 then loop (n+1) n d
3574 else loop (n+1) best bestd
3576 loop 0 ~-1 max_int
3578 m_active <- active;
3579 m_first <- firstof m_first active
3580 end)
3583 let enterselector usebookmarks =
3584 let source = outlinesource usebookmarks in
3585 fun errmsg ->
3586 let outlines =
3587 if usebookmarks
3588 then Array.of_list state.bookmarks
3589 else state.outlines
3591 if Array.length outlines = 0
3592 then (
3593 showtext ' ' errmsg;
3595 else (
3596 state.text <- source#greetmsg;
3597 Wsi.setcursor Wsi.CURSOR_INHERIT;
3598 let anchor = getanchor () in
3599 source#reset anchor outlines;
3600 state.uioh <- coe (new outlinelistview ~source);
3601 G.postRedisplay "enter selector";
3605 let enteroutlinemode =
3606 let f = enterselector false in
3607 fun ()-> f "Document has no outline";
3610 let enterbookmarkmode =
3611 let f = enterselector true in
3612 fun () -> f "Document has no bookmarks (yet)";
3615 let color_of_string s =
3616 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3617 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3621 let color_to_string (r, g, b) =
3622 let r = truncate (r *. 256.0)
3623 and g = truncate (g *. 256.0)
3624 and b = truncate (b *. 256.0) in
3625 Printf.sprintf "%d/%d/%d" r g b
3628 let irect_of_string s =
3629 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3632 let irect_to_string (x0,y0,x1,y1) =
3633 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3636 let makecheckers () =
3637 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3638 following to say:
3639 converted by Issac Trotts. July 25, 2002 *)
3640 let image_height = 64
3641 and image_width = 64 in
3643 let make_image () =
3644 let image =
3645 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3647 for i = 0 to image_width - 1 do
3648 for j = 0 to image_height - 1 do
3649 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3650 (if (i land 8 ) lxor (j land 8) = 0
3651 then [|255;255;255|] else [|200;200;200|])
3652 done
3653 done;
3654 image
3656 let image = make_image () in
3657 let id = GlTex.gen_texture () in
3658 GlTex.bind_texture `texture_2d id;
3659 GlPix.store (`unpack_alignment 1);
3660 GlTex.image2d image;
3661 List.iter (GlTex.parameter ~target:`texture_2d)
3662 [ `wrap_s `repeat;
3663 `wrap_t `repeat;
3664 `mag_filter `nearest;
3665 `min_filter `nearest ];
3669 let setcheckers enabled =
3670 match state.texid with
3671 | None ->
3672 if enabled then state.texid <- Some (makecheckers ())
3674 | Some texid ->
3675 if not enabled
3676 then (
3677 GlTex.delete_texture texid;
3678 state.texid <- None;
3682 let int_of_string_with_suffix s =
3683 let l = String.length s in
3684 let s1, shift =
3685 if l > 1
3686 then
3687 let suffix = Char.lowercase s.[l-1] in
3688 match suffix with
3689 | 'k' -> String.sub s 0 (l-1), 10
3690 | 'm' -> String.sub s 0 (l-1), 20
3691 | 'g' -> String.sub s 0 (l-1), 30
3692 | _ -> s, 0
3693 else s, 0
3695 let n = int_of_string s1 in
3696 let m = n lsl shift in
3697 if m < 0 || m < n
3698 then raise (Failure "value too large")
3699 else m
3702 let string_with_suffix_of_int n =
3703 if n = 0
3704 then "0"
3705 else
3706 let n, s =
3707 if n = 0
3708 then 0, ""
3709 else (
3710 if n land ((1 lsl 20) - 1) = 0
3711 then n lsr 20, "M"
3712 else (
3713 if n land ((1 lsl 10) - 1) = 0
3714 then n lsr 10, "K"
3715 else n, ""
3719 let rec loop s n =
3720 let h = n mod 1000 in
3721 let n = n / 1000 in
3722 if n = 0
3723 then string_of_int h ^ s
3724 else (
3725 let s = Printf.sprintf "_%03d%s" h s in
3726 loop s n
3729 loop "" n ^ s;
3732 let defghyllscroll = (40, 8, 32);;
3733 let ghyllscroll_of_string s =
3734 let (n, a, b) as nab =
3735 if s = "default"
3736 then defghyllscroll
3737 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3739 if n <= a || n <= b || a >= b
3740 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3741 nab;
3744 let ghyllscroll_to_string ((n, a, b) as nab) =
3745 if nab = defghyllscroll
3746 then "default"
3747 else Printf.sprintf "%d,%d,%d" n a b;
3750 let describe_location () =
3751 let f (fn, _) l =
3752 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3754 let fn, ln = List.fold_left f (-1, -1) state.layout in
3755 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3756 let percent =
3757 if maxy <= 0
3758 then 100.
3759 else (100. *. (float state.y /. float maxy))
3761 if fn = ln
3762 then
3763 Printf.sprintf "page %d of %d [%.2f%%]"
3764 (fn+1) state.pagecount percent
3765 else
3766 Printf.sprintf
3767 "pages %d-%d of %d [%.2f%%]"
3768 (fn+1) (ln+1) state.pagecount percent
3771 let enterinfomode =
3772 let btos b = if b then "\xe2\x88\x9a" else "" in
3773 let showextended = ref false in
3774 let leave mode = function
3775 | Confirm -> state.mode <- mode
3776 | Cancel -> state.mode <- mode in
3777 let src =
3778 (object
3779 val mutable m_first_time = true
3780 val mutable m_l = []
3781 val mutable m_a = [||]
3782 val mutable m_prev_uioh = nouioh
3783 val mutable m_prev_mode = View
3785 inherit lvsourcebase
3787 method reset prev_mode prev_uioh =
3788 m_a <- Array.of_list (List.rev m_l);
3789 m_l <- [];
3790 m_prev_mode <- prev_mode;
3791 m_prev_uioh <- prev_uioh;
3792 if m_first_time
3793 then (
3794 let rec loop n =
3795 if n >= Array.length m_a
3796 then ()
3797 else
3798 match m_a.(n) with
3799 | _, _, _, Action _ -> m_active <- n
3800 | _ -> loop (n+1)
3802 loop 0;
3803 m_first_time <- false;
3806 method int name get set =
3807 m_l <-
3808 (name, `int get, 1, Action (
3809 fun u ->
3810 let ondone s =
3811 try set (int_of_string s)
3812 with exn ->
3813 state.text <- Printf.sprintf "bad integer `%s': %s"
3814 s (Printexc.to_string exn)
3816 state.text <- "";
3817 let te = name ^ ": ", "", None, intentry, ondone in
3818 state.mode <- Textentry (te, leave m_prev_mode);
3820 )) :: m_l
3822 method int_with_suffix name get set =
3823 m_l <-
3824 (name, `intws get, 1, Action (
3825 fun u ->
3826 let ondone s =
3827 try set (int_of_string_with_suffix s)
3828 with exn ->
3829 state.text <- Printf.sprintf "bad integer `%s': %s"
3830 s (Printexc.to_string exn)
3832 state.text <- "";
3833 let te =
3834 name ^ ": ", "", None, intentry_with_suffix, ondone
3836 state.mode <- Textentry (te, leave m_prev_mode);
3838 )) :: m_l
3840 method bool ?(offset=1) ?(btos=btos) name get set =
3841 m_l <-
3842 (name, `bool (btos, get), offset, Action (
3843 fun u ->
3844 let v = get () in
3845 set (not v);
3847 )) :: m_l
3849 method color name get set =
3850 m_l <-
3851 (name, `color get, 1, Action (
3852 fun u ->
3853 let invalid = (nan, nan, nan) in
3854 let ondone s =
3855 let c =
3856 try color_of_string s
3857 with exn ->
3858 state.text <- Printf.sprintf "bad color `%s': %s"
3859 s (Printexc.to_string exn);
3860 invalid
3862 if c <> invalid
3863 then set c;
3865 let te = name ^ ": ", "", None, textentry, ondone in
3866 state.text <- color_to_string (get ());
3867 state.mode <- Textentry (te, leave m_prev_mode);
3869 )) :: m_l
3871 method string name get set =
3872 m_l <-
3873 (name, `string get, 1, Action (
3874 fun u ->
3875 let ondone s = set s in
3876 let te = name ^ ": ", "", None, textentry, ondone in
3877 state.mode <- Textentry (te, leave m_prev_mode);
3879 )) :: m_l
3881 method colorspace name get set =
3882 m_l <-
3883 (name, `string get, 1, Action (
3884 fun _ ->
3885 let source =
3886 let vals = [| "rgb"; "bgr"; "gray" |] in
3887 (object
3888 inherit lvsourcebase
3890 initializer
3891 m_active <- int_of_colorspace conf.colorspace;
3892 m_first <- 0;
3894 method getitemcount = Array.length vals
3895 method getitem n = (vals.(n), 0)
3896 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3897 ignore (uioh, first, pan, qsearch);
3898 if not cancel then set active;
3899 None
3900 method hasaction _ = true
3901 end)
3903 state.text <- "";
3904 let modehash = findkeyhash conf "info" in
3905 coe (new listview ~source ~trusted:true ~modehash)
3906 )) :: m_l
3908 method caption s offset =
3909 m_l <- (s, `empty, offset, Noaction) :: m_l
3911 method caption2 s f offset =
3912 m_l <- (s, `string f, offset, Noaction) :: m_l
3914 method getitemcount = Array.length m_a
3916 method getitem n =
3917 let tostr = function
3918 | `int f -> string_of_int (f ())
3919 | `intws f -> string_with_suffix_of_int (f ())
3920 | `string f -> f ()
3921 | `color f -> color_to_string (f ())
3922 | `bool (btos, f) -> btos (f ())
3923 | `empty -> ""
3925 let name, t, offset, _ = m_a.(n) in
3926 ((let s = tostr t in
3927 if String.length s > 0
3928 then Printf.sprintf "%s\t%s" name s
3929 else name),
3930 offset)
3932 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3933 let uiohopt =
3934 if not cancel
3935 then (
3936 m_qsearch <- qsearch;
3937 let uioh =
3938 match m_a.(active) with
3939 | _, _, _, Action f -> f uioh
3940 | _ -> uioh
3942 Some uioh
3944 else None
3946 m_active <- active;
3947 m_first <- first;
3948 m_pan <- pan;
3949 uiohopt
3951 method hasaction n =
3952 match m_a.(n) with
3953 | _, _, _, Action _ -> true
3954 | _ -> false
3955 end)
3957 let rec fillsrc prevmode prevuioh =
3958 let sep () = src#caption "" 0 in
3959 let colorp name get set =
3960 src#string name
3961 (fun () -> color_to_string (get ()))
3962 (fun v ->
3964 let c = color_of_string v in
3965 set c
3966 with exn ->
3967 state.text <- Printf.sprintf "bad color `%s': %s"
3968 v (Printexc.to_string exn);
3971 let oldmode = state.mode in
3972 let birdseye = isbirdseye state.mode in
3974 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3976 src#bool "presentation mode"
3977 (fun () -> conf.presentation)
3978 (fun v ->
3979 conf.presentation <- v;
3980 state.anchor <- getanchor ();
3981 represent ());
3983 src#bool "ignore case in searches"
3984 (fun () -> conf.icase)
3985 (fun v -> conf.icase <- v);
3987 src#bool "preload"
3988 (fun () -> conf.preload)
3989 (fun v -> conf.preload <- v);
3991 src#bool "highlight links"
3992 (fun () -> conf.hlinks)
3993 (fun v -> conf.hlinks <- v);
3995 src#bool "under info"
3996 (fun () -> conf.underinfo)
3997 (fun v -> conf.underinfo <- v);
3999 src#bool "persistent bookmarks"
4000 (fun () -> conf.savebmarks)
4001 (fun v -> conf.savebmarks <- v);
4003 src#bool "proportional display"
4004 (fun () -> conf.proportional)
4005 (fun v -> reqlayout conf.angle v);
4007 src#bool "trim margins"
4008 (fun () -> conf.trimmargins)
4009 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4011 src#bool "persistent location"
4012 (fun () -> conf.jumpback)
4013 (fun v -> conf.jumpback <- v);
4015 sep ();
4016 src#int "inter-page space"
4017 (fun () -> conf.interpagespace)
4018 (fun n ->
4019 conf.interpagespace <- n;
4020 let pageno, py =
4021 match state.layout with
4022 | [] -> 0, 0
4023 | l :: _ ->
4024 l.pageno, l.pagey
4026 state.maxy <- calcheight ();
4027 let y = getpagey pageno in
4028 gotoy (y + py)
4031 src#int "page bias"
4032 (fun () -> conf.pagebias)
4033 (fun v -> conf.pagebias <- v);
4035 src#int "scroll step"
4036 (fun () -> conf.scrollstep)
4037 (fun n -> conf.scrollstep <- n);
4039 src#int "auto scroll step"
4040 (fun () ->
4041 match state.autoscroll with
4042 | Some step -> step
4043 | _ -> conf.autoscrollstep)
4044 (fun n ->
4045 if state.autoscroll <> None
4046 then state.autoscroll <- Some n;
4047 conf.autoscrollstep <- n);
4049 src#int "zoom"
4050 (fun () -> truncate (conf.zoom *. 100.))
4051 (fun v -> setzoom ((float v) /. 100.));
4053 src#int "rotation"
4054 (fun () -> conf.angle)
4055 (fun v -> reqlayout v conf.proportional);
4057 src#int "scroll bar width"
4058 (fun () -> state.scrollw)
4059 (fun v ->
4060 state.scrollw <- v;
4061 conf.scrollbw <- v;
4062 reshape conf.winw conf.winh;
4065 src#int "scroll handle height"
4066 (fun () -> conf.scrollh)
4067 (fun v -> conf.scrollh <- v;);
4069 src#int "thumbnail width"
4070 (fun () -> conf.thumbw)
4071 (fun v ->
4072 conf.thumbw <- min 4096 v;
4073 match oldmode with
4074 | Birdseye beye ->
4075 leavebirdseye beye false;
4076 enterbirdseye ()
4077 | _ -> ()
4080 let mode = state.mode in
4081 src#string "columns"
4082 (fun () ->
4083 match conf.columns with
4084 | Csingle -> "1"
4085 | Cmulti (multi, _) -> multicolumns_to_string multi
4086 | Csplit (count, _) -> "-" ^ string_of_int count
4088 (fun v ->
4089 let n, a, b = multicolumns_of_string v in
4090 setcolumns mode n a b);
4092 sep ();
4093 src#caption "Presentation mode" 0;
4094 src#bool "scrollbar visible"
4095 (fun () -> conf.scrollbarinpm)
4096 (fun v ->
4097 if v != conf.scrollbarinpm
4098 then (
4099 conf.scrollbarinpm <- v;
4100 if conf.presentation
4101 then (
4102 state.scrollw <- if v then conf.scrollbw else 0;
4103 reshape conf.winw conf.winh;
4108 sep ();
4109 src#caption "Pixmap cache" 0;
4110 src#int_with_suffix "size (advisory)"
4111 (fun () -> conf.memlimit)
4112 (fun v -> conf.memlimit <- v);
4114 src#caption2 "used"
4115 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4116 (string_with_suffix_of_int state.memused)
4117 (Hashtbl.length state.tilemap)) 1;
4119 sep ();
4120 src#caption "Layout" 0;
4121 src#caption2 "Dimension"
4122 (fun () ->
4123 Printf.sprintf "%dx%d (virtual %dx%d)"
4124 conf.winw conf.winh
4125 state.w state.maxy)
4127 if conf.debug
4128 then
4129 src#caption2 "Position" (fun () ->
4130 Printf.sprintf "%dx%d" state.x state.y
4132 else
4133 src#caption2 "Visible" (fun () -> describe_location ()) 1
4136 sep ();
4137 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4138 "Save these parameters as global defaults at exit"
4139 (fun () -> conf.bedefault)
4140 (fun v -> conf.bedefault <- v)
4143 sep ();
4144 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4145 src#bool ~offset:0 ~btos "Extended parameters"
4146 (fun () -> !showextended)
4147 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4148 if !showextended
4149 then (
4150 src#bool "checkers"
4151 (fun () -> conf.checkers)
4152 (fun v -> conf.checkers <- v; setcheckers v);
4153 src#bool "update cursor"
4154 (fun () -> conf.updatecurs)
4155 (fun v -> conf.updatecurs <- v);
4156 src#bool "verbose"
4157 (fun () -> conf.verbose)
4158 (fun v -> conf.verbose <- v);
4159 src#bool "invert colors"
4160 (fun () -> conf.invert)
4161 (fun v -> conf.invert <- v);
4162 src#bool "max fit"
4163 (fun () -> conf.maxhfit)
4164 (fun v -> conf.maxhfit <- v);
4165 src#bool "redirect stderr"
4166 (fun () -> conf.redirectstderr)
4167 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4168 src#string "uri launcher"
4169 (fun () -> conf.urilauncher)
4170 (fun v -> conf.urilauncher <- v);
4171 src#string "path launcher"
4172 (fun () -> conf.pathlauncher)
4173 (fun v -> conf.pathlauncher <- v);
4174 src#string "tile size"
4175 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4176 (fun v ->
4178 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4179 conf.tileh <- max 64 w;
4180 conf.tilew <- max 64 h;
4181 flushtiles ();
4182 with exn ->
4183 state.text <- Printf.sprintf "bad tile size `%s': %s"
4184 v (Printexc.to_string exn));
4185 src#int "texture count"
4186 (fun () -> conf.texcount)
4187 (fun v ->
4188 if realloctexts v
4189 then conf.texcount <- v
4190 else showtext '!' " Failed to set texture count please retry later"
4192 src#int "slice height"
4193 (fun () -> conf.sliceheight)
4194 (fun v ->
4195 conf.sliceheight <- v;
4196 wcmd "sliceh %d" conf.sliceheight;
4198 src#int "anti-aliasing level"
4199 (fun () -> conf.aalevel)
4200 (fun v ->
4201 conf.aalevel <- bound v 0 8;
4202 state.anchor <- getanchor ();
4203 opendoc state.path state.password;
4205 src#int "ui font size"
4206 (fun () -> fstate.fontsize)
4207 (fun v -> setfontsize (bound v 5 100));
4208 colorp "background color"
4209 (fun () -> conf.bgcolor)
4210 (fun v -> conf.bgcolor <- v);
4211 src#bool "crop hack"
4212 (fun () -> conf.crophack)
4213 (fun v -> conf.crophack <- v);
4214 src#string "trim fuzz"
4215 (fun () -> irect_to_string conf.trimfuzz)
4216 (fun v ->
4218 conf.trimfuzz <- irect_of_string v;
4219 if conf.trimmargins
4220 then settrim true conf.trimfuzz;
4221 with exn ->
4222 state.text <- Printf.sprintf "bad irect `%s': %s"
4223 v (Printexc.to_string exn)
4225 src#string "throttle"
4226 (fun () ->
4227 match conf.maxwait with
4228 | None -> "show place holder if page is not ready"
4229 | Some time ->
4230 if time = infinity
4231 then "wait for page to fully render"
4232 else
4233 "wait " ^ string_of_float time
4234 ^ " seconds before showing placeholder"
4236 (fun v ->
4238 let f = float_of_string v in
4239 if f <= 0.0
4240 then conf.maxwait <- None
4241 else conf.maxwait <- Some f
4242 with exn ->
4243 state.text <- Printf.sprintf "bad time `%s': %s"
4244 v (Printexc.to_string exn)
4246 src#string "ghyll scroll"
4247 (fun () ->
4248 match conf.ghyllscroll with
4249 | None -> ""
4250 | Some nab -> ghyllscroll_to_string nab
4252 (fun v ->
4254 let gs =
4255 if String.length v = 0
4256 then None
4257 else Some (ghyllscroll_of_string v)
4259 conf.ghyllscroll <- gs
4260 with exn ->
4261 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4262 v (Printexc.to_string exn)
4264 src#string "selection command"
4265 (fun () -> conf.selcmd)
4266 (fun v -> conf.selcmd <- v);
4267 src#colorspace "color space"
4268 (fun () -> colorspace_to_string conf.colorspace)
4269 (fun v ->
4270 conf.colorspace <- colorspace_of_int v;
4271 wcmd "cs %d" v;
4272 load state.layout;
4276 sep ();
4277 src#caption "Document" 0;
4278 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4279 src#caption2 "Pages"
4280 (fun () -> string_of_int state.pagecount) 1;
4281 src#caption2 "Dimensions"
4282 (fun () -> string_of_int (List.length state.pdims)) 1;
4283 if conf.trimmargins
4284 then (
4285 sep ();
4286 src#caption "Trimmed margins" 0;
4287 src#caption2 "Dimensions"
4288 (fun () -> string_of_int (List.length state.pdims)) 1;
4291 src#reset prevmode prevuioh;
4293 fun () ->
4294 state.text <- "";
4295 let prevmode = state.mode
4296 and prevuioh = state.uioh in
4297 fillsrc prevmode prevuioh;
4298 let source = (src :> lvsource) in
4299 let modehash = findkeyhash conf "info" in
4300 state.uioh <- coe (object (self)
4301 inherit listview ~source ~trusted:true ~modehash as super
4302 val mutable m_prevmemused = 0
4303 method infochanged = function
4304 | Memused ->
4305 if m_prevmemused != state.memused
4306 then (
4307 m_prevmemused <- state.memused;
4308 G.postRedisplay "memusedchanged";
4310 | Pdim -> G.postRedisplay "pdimchanged"
4311 | Docinfo -> fillsrc prevmode prevuioh
4313 method key key mask =
4314 if not (Wsi.withctrl mask)
4315 then
4316 match key with
4317 | 0xff51 -> coe (self#updownlevel ~-1)
4318 | 0xff53 -> coe (self#updownlevel 1)
4319 | _ -> super#key key mask
4320 else super#key key mask
4321 end);
4322 G.postRedisplay "info";
4325 let enterhelpmode =
4326 let source =
4327 (object
4328 inherit lvsourcebase
4329 method getitemcount = Array.length state.help
4330 method getitem n =
4331 let s, n, _ = state.help.(n) in
4332 (s, n)
4334 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4335 let optuioh =
4336 if not cancel
4337 then (
4338 m_qsearch <- qsearch;
4339 match state.help.(active) with
4340 | _, _, Action f -> Some (f uioh)
4341 | _ -> Some (uioh)
4343 else None
4345 m_active <- active;
4346 m_first <- first;
4347 m_pan <- pan;
4348 optuioh
4350 method hasaction n =
4351 match state.help.(n) with
4352 | _, _, Action _ -> true
4353 | _ -> false
4355 initializer
4356 m_active <- -1
4357 end)
4358 in fun () ->
4359 let modehash = findkeyhash conf "help" in
4360 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4361 G.postRedisplay "help";
4364 let entermsgsmode =
4365 let msgsource =
4366 let re = Str.regexp "[\r\n]" in
4367 (object
4368 inherit lvsourcebase
4369 val mutable m_items = [||]
4371 method getitemcount = 1 + Array.length m_items
4373 method getitem n =
4374 if n = 0
4375 then "[Clear]", 0
4376 else m_items.(n-1), 0
4378 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4379 ignore uioh;
4380 if not cancel
4381 then (
4382 if active = 0
4383 then Buffer.clear state.errmsgs;
4384 m_qsearch <- qsearch;
4386 m_active <- active;
4387 m_first <- first;
4388 m_pan <- pan;
4389 None
4391 method hasaction n =
4392 n = 0
4394 method reset =
4395 state.newerrmsgs <- false;
4396 let l = Str.split re (Buffer.contents state.errmsgs) in
4397 m_items <- Array.of_list l
4399 initializer
4400 m_active <- 0
4401 end)
4402 in fun () ->
4403 state.text <- "";
4404 msgsource#reset;
4405 let source = (msgsource :> lvsource) in
4406 let modehash = findkeyhash conf "listview" in
4407 state.uioh <- coe (object
4408 inherit listview ~source ~trusted:false ~modehash as super
4409 method display =
4410 if state.newerrmsgs
4411 then msgsource#reset;
4412 super#display
4413 end);
4414 G.postRedisplay "msgs";
4417 let quickbookmark ?title () =
4418 match state.layout with
4419 | [] -> ()
4420 | l :: _ ->
4421 let title =
4422 match title with
4423 | None ->
4424 let sec = Unix.gettimeofday () in
4425 let tm = Unix.localtime sec in
4426 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4427 (l.pageno+1)
4428 tm.Unix.tm_mday
4429 tm.Unix.tm_mon
4430 (tm.Unix.tm_year + 1900)
4431 tm.Unix.tm_hour
4432 tm.Unix.tm_min
4433 | Some title -> title
4435 state.bookmarks <-
4436 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4437 :: state.bookmarks
4440 let doreshape w h =
4441 state.fullscreen <- None;
4442 Wsi.reshape w h;
4445 let setautoscrollspeed step goingdown =
4446 let incr = max 1 ((abs step) / 2) in
4447 let incr = if goingdown then incr else -incr in
4448 let astep = step + incr in
4449 state.autoscroll <- Some astep;
4452 let gotounder = function
4453 | Ulinkgoto (pageno, top) ->
4454 if pageno >= 0
4455 then (
4456 addnav ();
4457 gotopage1 pageno top;
4460 | Ulinkuri s ->
4461 gotouri s
4463 | Uremote (filename, pageno) ->
4464 let path =
4465 if Sys.file_exists filename
4466 then filename
4467 else
4468 let dir = Filename.dirname state.path in
4469 let path = Filename.concat dir filename in
4470 if Sys.file_exists path
4471 then path
4472 else ""
4474 if String.length path > 0
4475 then (
4476 let anchor = getanchor () in
4477 let ranchor = state.path, state.password, anchor in
4478 state.anchor <- (pageno, 0.0);
4479 state.ranchors <- ranchor :: state.ranchors;
4480 opendoc path "";
4482 else showtext '!' ("Could not find " ^ filename)
4484 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4487 let canpan () =
4488 match conf.columns with
4489 | Csplit _ -> true
4490 | _ -> conf.zoom > 1.0
4493 let viewkeyboard key mask =
4494 let enttext te =
4495 let mode = state.mode in
4496 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4497 state.text <- "";
4498 enttext ();
4499 G.postRedisplay "view:enttext"
4501 let ctrl = Wsi.withctrl mask in
4502 match key with
4503 | 81 -> (* Q *)
4504 exit 0
4506 | 0xff63 -> (* insert *)
4507 if conf.angle mod 360 = 0
4508 then (
4509 state.mode <- LinkNav (Ltgendir 0);
4510 gotoy state.y;
4512 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4514 | 0xff1b | 113 -> (* escape / q *)
4515 begin match state.mstate with
4516 | Mzoomrect _ ->
4517 state.mstate <- Mnone;
4518 Wsi.setcursor Wsi.CURSOR_INHERIT;
4519 G.postRedisplay "kill zoom rect";
4520 | _ ->
4521 match state.ranchors with
4522 | [] -> raise Quit
4523 | (path, password, anchor) :: rest ->
4524 state.ranchors <- rest;
4525 state.anchor <- anchor;
4526 opendoc path password
4527 end;
4529 | 0xff08 -> (* backspace *)
4530 let y = getnav ~-1 in
4531 gotoy_and_clear_text y
4533 | 111 -> (* o *)
4534 enteroutlinemode ()
4536 | 117 -> (* u *)
4537 state.rects <- [];
4538 state.text <- "";
4539 G.postRedisplay "dehighlight";
4541 | 47 | 63 -> (* / ? *)
4542 let ondone isforw s =
4543 cbput state.hists.pat s;
4544 state.searchpattern <- s;
4545 search s isforw
4547 let s = String.create 1 in
4548 s.[0] <- Char.chr key;
4549 enttext (s, "", Some (onhist state.hists.pat),
4550 textentry, ondone (key = 47))
4552 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4553 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4554 setzoom (conf.zoom +. incr)
4556 | 43 | 0xffab -> (* + *)
4557 let ondone s =
4558 let n =
4559 try int_of_string s with exc ->
4560 state.text <- Printf.sprintf "bad integer `%s': %s"
4561 s (Printexc.to_string exc);
4562 max_int
4564 if n != max_int
4565 then (
4566 conf.pagebias <- n;
4567 state.text <- "page bias is now " ^ string_of_int n;
4570 enttext ("page bias: ", "", None, intentry, ondone)
4572 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4573 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4574 setzoom (max 0.01 (conf.zoom -. decr))
4576 | 45 | 0xffad -> (* - *)
4577 let ondone msg = state.text <- msg in
4578 enttext (
4579 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4580 optentry state.mode, ondone
4583 | 48 when ctrl -> (* ctrl-0 *)
4584 setzoom 1.0
4586 | 49 when ctrl -> (* 1 *)
4587 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4588 if zoom < 1.0
4589 then setzoom zoom
4591 | 0xffc6 -> (* f9 *)
4592 togglebirdseye ()
4594 | 57 when ctrl -> (* ctrl-9 *)
4595 togglebirdseye ()
4597 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4598 when not ctrl -> (* 0..9 *)
4599 let ondone s =
4600 let n =
4601 try int_of_string s with exc ->
4602 state.text <- Printf.sprintf "bad integer `%s': %s"
4603 s (Printexc.to_string exc);
4606 if n >= 0
4607 then (
4608 addnav ();
4609 cbput state.hists.pag (string_of_int n);
4610 gotopage1 (n + conf.pagebias - 1) 0;
4613 let pageentry text key =
4614 match Char.unsafe_chr key with
4615 | 'g' -> TEdone text
4616 | _ -> intentry text key
4618 let text = "x" in text.[0] <- Char.chr key;
4619 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4621 | 98 -> (* b *)
4622 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4623 reshape conf.winw conf.winh;
4625 | 108 -> (* l *)
4626 conf.hlinks <- not conf.hlinks;
4627 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4628 G.postRedisplay "toggle highlightlinks";
4630 | 70 -> (* F *)
4631 state.glinks <- true;
4632 let a = Char.code 'a' and z = Char.code 'z' in
4633 let ondone s =
4634 let n =
4635 let rec loop pos n = if pos = String.length s then n else
4636 let m = Char.code s.[pos] - a in
4637 loop (pos+1) (n*(z-a+1) + m)
4638 in loop 0 0
4640 if n >= 0
4641 then (
4642 let rec loop n = function
4643 | [] -> ()
4644 | l :: rest ->
4645 match getopaque l.pageno with
4646 | None -> loop n rest
4647 | Some opaque ->
4648 let m = getlinkcount opaque in
4649 if n < m
4650 then (
4651 let under = getlink opaque n in
4652 addnav ();
4653 gotounder under;
4655 else loop (n-m) rest
4657 loop n state.layout;
4660 let onkey text key =
4661 if (key >= a && key <= z)
4662 then
4663 let c = Char.unsafe_chr key in
4664 let text = addchar text c in
4665 TEcont text
4666 else
4667 TEcont text
4669 let mode = state.mode in
4670 state.mode <- Textentry (
4671 (":", "", Some (onhist state.hists.pag), onkey, ondone),
4672 fun _ ->
4673 state.glinks <- false;
4674 state.mode <- mode
4676 state.text <- "";
4677 G.postRedisplay "view:enttext"
4679 | 97 -> (* a *)
4680 begin match state.autoscroll with
4681 | Some step ->
4682 conf.autoscrollstep <- step;
4683 state.autoscroll <- None
4684 | None ->
4685 if conf.autoscrollstep = 0
4686 then state.autoscroll <- Some 1
4687 else state.autoscroll <- Some conf.autoscrollstep
4690 | 112 when ctrl -> (* ctrl-p *)
4691 launchpath ()
4693 | 80 -> (* P *)
4694 conf.presentation <- not conf.presentation;
4695 if conf.presentation
4696 then (
4697 if not conf.scrollbarinpm
4698 then state.scrollw <- 0;
4700 else
4701 state.scrollw <- conf.scrollbw;
4703 showtext ' ' ("presentation mode " ^
4704 if conf.presentation then "on" else "off");
4705 state.anchor <- getanchor ();
4706 represent ()
4708 | 102 -> (* f *)
4709 begin match state.fullscreen with
4710 | None ->
4711 state.fullscreen <- Some (conf.winw, conf.winh);
4712 Wsi.fullscreen ()
4713 | Some (w, h) ->
4714 state.fullscreen <- None;
4715 doreshape w h
4718 | 103 -> (* g *)
4719 gotoy_and_clear_text 0
4721 | 71 -> (* G *)
4722 gotopage1 (state.pagecount - 1) 0
4724 | 112 | 78 -> (* p|N *)
4725 search state.searchpattern false
4727 | 110 | 0xffc0 -> (* n|F3 *)
4728 search state.searchpattern true
4730 | 116 -> (* t *)
4731 begin match state.layout with
4732 | [] -> ()
4733 | l :: _ ->
4734 gotoy_and_clear_text (getpagey l.pageno)
4737 | 32 -> (* ' ' *)
4738 begin match List.rev state.layout with
4739 | [] -> ()
4740 | l :: _ ->
4741 let pageno = min (l.pageno+1) (state.pagecount-1) in
4742 gotoy_and_clear_text (getpagey pageno)
4745 | 0xff9f | 0xffff -> (* delete *)
4746 begin match state.layout with
4747 | [] -> ()
4748 | l :: _ ->
4749 let pageno = max 0 (l.pageno-1) in
4750 gotoy_and_clear_text (getpagey pageno)
4753 | 61 -> (* = *)
4754 showtext ' ' (describe_location ());
4756 | 119 -> (* w *)
4757 begin match state.layout with
4758 | [] -> ()
4759 | l :: _ ->
4760 doreshape (l.pagew + state.scrollw) l.pageh;
4761 G.postRedisplay "w"
4764 | 39 -> (* ' *)
4765 enterbookmarkmode ()
4767 | 104 | 0xffbe -> (* h|F1 *)
4768 enterhelpmode ()
4770 | 105 -> (* i *)
4771 enterinfomode ()
4773 | 101 when conf.redirectstderr -> (* e *)
4774 entermsgsmode ()
4776 | 109 -> (* m *)
4777 let ondone s =
4778 match state.layout with
4779 | l :: _ ->
4780 state.bookmarks <-
4781 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4782 :: state.bookmarks
4783 | _ -> ()
4785 enttext ("bookmark: ", "", None, textentry, ondone)
4787 | 126 -> (* ~ *)
4788 quickbookmark ();
4789 showtext ' ' "Quick bookmark added";
4791 | 122 -> (* z *)
4792 begin match state.layout with
4793 | l :: _ ->
4794 let rect = getpdimrect l.pagedimno in
4795 let w, h =
4796 if conf.crophack
4797 then
4798 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4799 truncate (1.2 *. (rect.(3) -. rect.(0))))
4800 else
4801 (truncate (rect.(1) -. rect.(0)),
4802 truncate (rect.(3) -. rect.(0)))
4804 let w = truncate ((float w)*.conf.zoom)
4805 and h = truncate ((float h)*.conf.zoom) in
4806 if w != 0 && h != 0
4807 then (
4808 state.anchor <- getanchor ();
4809 doreshape (w + state.scrollw) (h + conf.interpagespace)
4811 G.postRedisplay "z";
4813 | [] -> ()
4816 | 50 when ctrl -> (* ctrl-2 *)
4817 let maxw = getmaxw () in
4818 if maxw > 0.0
4819 then setzoom (maxw /. float conf.winw)
4821 | 60 | 62 -> (* < > *)
4822 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4824 | 91 | 93 -> (* [ ] *)
4825 conf.colorscale <-
4826 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4828 G.postRedisplay "brightness";
4830 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4831 setzoom state.prevzoom
4833 | 107 | 0xff52 -> (* k up *)
4834 begin match state.autoscroll with
4835 | None ->
4836 begin match state.mode with
4837 | Birdseye beye -> upbirdseye 1 beye
4838 | _ ->
4839 if ctrl
4840 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4841 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4843 | Some n ->
4844 setautoscrollspeed n false
4847 | 106 | 0xff54 -> (* j down *)
4848 begin match state.autoscroll with
4849 | None ->
4850 begin match state.mode with
4851 | Birdseye beye -> downbirdseye 1 beye
4852 | _ ->
4853 if ctrl
4854 then gotoy_and_clear_text (clamp (conf.winh/2))
4855 else gotoy_and_clear_text (clamp conf.scrollstep)
4857 | Some n ->
4858 setautoscrollspeed n true
4861 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4862 if canpan ()
4863 then
4864 let dx =
4865 if ctrl
4866 then conf.winw / 2
4867 else 10
4869 let dx = if key = 0xff51 then dx else -dx in
4870 state.x <- state.x + dx;
4871 gotoy_and_clear_text state.y
4872 else (
4873 state.text <- "";
4874 G.postRedisplay "lef/right"
4877 | 0xff55 -> (* prior *)
4878 let y =
4879 if ctrl
4880 then
4881 match state.layout with
4882 | [] -> state.y
4883 | l :: _ -> state.y - l.pagey
4884 else
4885 clamp (-conf.winh)
4887 gotoghyll y
4889 | 0xff56 -> (* next *)
4890 let y =
4891 if ctrl
4892 then
4893 match List.rev state.layout with
4894 | [] -> state.y
4895 | l :: _ -> getpagey l.pageno
4896 else
4897 clamp conf.winh
4899 gotoghyll y
4901 | 0xff50 -> gotoghyll 0
4902 | 0xff57 -> gotoghyll (clamp state.maxy)
4903 | 0xff53 when Wsi.withalt mask ->
4904 gotoghyll (getnav ~-1)
4905 | 0xff51 when Wsi.withalt mask ->
4906 gotoghyll (getnav 1)
4908 | 114 -> (* r *)
4909 state.anchor <- getanchor ();
4910 opendoc state.path state.password
4912 | 118 when conf.debug -> (* v *)
4913 state.rects <- [];
4914 List.iter (fun l ->
4915 match getopaque l.pageno with
4916 | None -> ()
4917 | Some opaque ->
4918 let x0, y0, x1, y1 = pagebbox opaque in
4919 let a,b = float x0, float y0 in
4920 let c,d = float x1, float y0 in
4921 let e,f = float x1, float y1 in
4922 let h,j = float x0, float y1 in
4923 let rect = (a,b,c,d,e,f,h,j) in
4924 debugrect rect;
4925 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4926 ) state.layout;
4927 G.postRedisplay "v";
4929 | _ ->
4930 vlog "huh? %s" (Wsi.keyname key)
4933 let linknavkeyboard key mask linknav =
4934 let getpage pageno =
4935 let rec loop = function
4936 | [] -> None
4937 | l :: _ when l.pageno = pageno -> Some l
4938 | _ :: rest -> loop rest
4939 in loop state.layout
4941 let doexact (pageno, n) =
4942 match getopaque pageno, getpage pageno with
4943 | Some opaque, Some l ->
4944 if key = 0xff0d
4945 then
4946 let under = getlink opaque n in
4947 G.postRedisplay "link gotounder";
4948 gotounder under;
4949 state.mode <- View;
4950 else
4951 let opt, dir =
4952 match key with
4953 | 0xff50 -> (* home *)
4954 Some (findlink opaque LDfirst), -1
4956 | 0xff57 -> (* end *)
4957 Some (findlink opaque LDlast), 1
4959 | 0xff51 -> (* left *)
4960 Some (findlink opaque (LDleft n)), -1
4962 | 0xff53 -> (* right *)
4963 Some (findlink opaque (LDright n)), 1
4965 | 0xff52 -> (* up *)
4966 Some (findlink opaque (LDup n)), -1
4968 | 0xff54 -> (* down *)
4969 Some (findlink opaque (LDdown n)), 1
4971 | _ -> None, 0
4973 let pwl l dir =
4974 begin match findpwl l.pageno dir with
4975 | Pwlnotfound -> ()
4976 | Pwl pageno ->
4977 let notfound dir =
4978 state.mode <- LinkNav (Ltgendir dir);
4979 let y, h = getpageyh pageno in
4980 let y =
4981 if dir < 0
4982 then y + h - conf.winh
4983 else y
4985 gotoy y
4987 begin match getopaque pageno, getpage pageno with
4988 | Some opaque, Some _ ->
4989 let link =
4990 let ld = if dir > 0 then LDfirst else LDlast in
4991 findlink opaque ld
4993 begin match link with
4994 | Lfound m ->
4995 showlinktype (getlink opaque m);
4996 state.mode <- LinkNav (Ltexact (pageno, m));
4997 G.postRedisplay "linknav jpage";
4998 | _ -> notfound dir
4999 end;
5000 | _ -> notfound dir
5001 end;
5002 end;
5004 begin match opt with
5005 | Some Lnotfound -> pwl l dir;
5006 | Some (Lfound m) ->
5007 if m = n
5008 then pwl l dir
5009 else (
5010 let _, y0, _, y1 = getlinkrect opaque m in
5011 if y0 < l.pagey
5012 then gotopage1 l.pageno y0
5013 else (
5014 let d = fstate.fontsize + 1 in
5015 if y1 - l.pagey > l.pagevh - d
5016 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5017 else G.postRedisplay "linknav";
5019 showlinktype (getlink opaque m);
5020 state.mode <- LinkNav (Ltexact (l.pageno, m));
5023 | None -> viewkeyboard key mask
5024 end;
5025 | _ -> viewkeyboard key mask
5027 if key = 0xff63
5028 then (
5029 state.mode <- View;
5030 G.postRedisplay "leave linknav"
5032 else
5033 match linknav with
5034 | Ltgendir _ -> viewkeyboard key mask
5035 | Ltexact exact -> doexact exact
5038 let keyboard key mask =
5039 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5040 then wcmd "interrupt"
5041 else state.uioh <- state.uioh#key key mask
5044 let birdseyekeyboard key mask
5045 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5046 let incr =
5047 match conf.columns with
5048 | Csingle -> 1
5049 | Cmulti ((c, _, _), _) -> c
5050 | Csplit _ -> failwith "bird's eye split mode"
5052 match key with
5053 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5054 let y, h = getpageyh pageno in
5055 let top = (conf.winh - h) / 2 in
5056 gotoy (max 0 (y - top))
5057 | 0xff0d -> leavebirdseye beye false
5058 | 0xff1b -> leavebirdseye beye true (* escape *)
5059 | 0xff52 -> upbirdseye incr beye (* prior *)
5060 | 0xff54 -> downbirdseye incr beye (* next *)
5061 | 0xff51 -> upbirdseye 1 beye (* up *)
5062 | 0xff53 -> downbirdseye 1 beye (* down *)
5064 | 0xff55 ->
5065 begin match state.layout with
5066 | l :: _ ->
5067 if l.pagey != 0
5068 then (
5069 state.mode <- Birdseye (
5070 oconf, leftx, l.pageno, hooverpageno, anchor
5072 gotopage1 l.pageno 0;
5074 else (
5075 let layout = layout (state.y-conf.winh) conf.winh in
5076 match layout with
5077 | [] -> gotoy (clamp (-conf.winh))
5078 | l :: _ ->
5079 state.mode <- Birdseye (
5080 oconf, leftx, l.pageno, hooverpageno, anchor
5082 gotopage1 l.pageno 0
5085 | [] -> gotoy (clamp (-conf.winh))
5086 end;
5088 | 0xff56 ->
5089 begin match List.rev state.layout with
5090 | l :: _ ->
5091 let layout = layout (state.y + conf.winh) conf.winh in
5092 begin match layout with
5093 | [] ->
5094 let incr = l.pageh - l.pagevh in
5095 if incr = 0
5096 then (
5097 state.mode <-
5098 Birdseye (
5099 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5101 G.postRedisplay "birdseye pagedown";
5103 else gotoy (clamp (incr + conf.interpagespace*2));
5105 | l :: _ ->
5106 state.mode <-
5107 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5108 gotopage1 l.pageno 0;
5111 | [] -> gotoy (clamp conf.winh)
5112 end;
5114 | 0xff50 ->
5115 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5116 gotopage1 0 0
5118 | 0xff57 ->
5119 let pageno = state.pagecount - 1 in
5120 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5121 if not (pagevisible state.layout pageno)
5122 then
5123 let h =
5124 match List.rev state.pdims with
5125 | [] -> conf.winh
5126 | (_, _, h, _) :: _ -> h
5128 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5129 else G.postRedisplay "birdseye end";
5130 | _ -> viewkeyboard key mask
5133 let drawpage l linkindexbase =
5134 let color =
5135 match state.mode with
5136 | Textentry _ -> scalecolor 0.4
5137 | LinkNav _
5138 | View -> scalecolor 1.0
5139 | Birdseye (_, _, pageno, hooverpageno, _) ->
5140 if l.pageno = hooverpageno
5141 then scalecolor 0.9
5142 else (
5143 if l.pageno = pageno
5144 then scalecolor 1.0
5145 else scalecolor 0.8
5148 drawtiles l color;
5149 begin match getopaque l.pageno with
5150 | Some opaque ->
5151 if tileready l l.pagex l.pagey
5152 then
5153 let x = l.pagedispx - l.pagex
5154 and y = l.pagedispy - l.pagey in
5155 let hlmask = (if conf.hlinks then 1 else 0)
5156 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5158 postprocess opaque hlmask x y linkindexbase;
5159 else 0
5161 | _ -> 0
5162 end;
5165 let scrollindicator () =
5166 let sbw, ph, sh = state.uioh#scrollph in
5167 let sbh, pw, sw = state.uioh#scrollpw in
5169 GlDraw.color (0.64, 0.64, 0.64);
5170 GlDraw.rect
5171 (float (conf.winw - sbw), 0.)
5172 (float conf.winw, float conf.winh)
5174 GlDraw.rect
5175 (0., float (conf.winh - sbh))
5176 (float (conf.winw - state.scrollw - 1), float conf.winh)
5178 GlDraw.color (0.0, 0.0, 0.0);
5180 GlDraw.rect
5181 (float (conf.winw - sbw), ph)
5182 (float conf.winw, ph +. sh)
5184 GlDraw.rect
5185 (pw, float (conf.winh - sbh))
5186 (pw +. sw, float conf.winh)
5190 let showsel () =
5191 match state.mstate with
5192 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5195 | Msel ((x0, y0), (x1, y1)) ->
5196 let rec loop = function
5197 | l :: ls ->
5198 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5199 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5200 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5201 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5202 then
5203 match getopaque l.pageno with
5204 | Some opaque ->
5205 let x0, y0 = pagetranslatepoint l x0 y0 in
5206 let x1, y1 = pagetranslatepoint l x1 y1 in
5207 seltext opaque (x0, y0, x1, y1);
5208 | _ -> ()
5209 else loop ls
5210 | [] -> ()
5212 loop state.layout
5215 let showrects rects =
5216 Gl.enable `blend;
5217 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5218 GlDraw.polygon_mode `both `fill;
5219 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5220 List.iter
5221 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5222 List.iter (fun l ->
5223 if l.pageno = pageno
5224 then (
5225 let dx = float (l.pagedispx - l.pagex) in
5226 let dy = float (l.pagedispy - l.pagey) in
5227 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5228 GlDraw.begins `quads;
5230 GlDraw.vertex2 (x0+.dx, y0+.dy);
5231 GlDraw.vertex2 (x1+.dx, y1+.dy);
5232 GlDraw.vertex2 (x2+.dx, y2+.dy);
5233 GlDraw.vertex2 (x3+.dx, y3+.dy);
5235 GlDraw.ends ();
5237 ) state.layout
5238 ) rects
5240 Gl.disable `blend;
5243 let display () =
5244 GlClear.color (scalecolor2 conf.bgcolor);
5245 GlClear.clear [`color];
5246 let rec loop linkindexbase = function
5247 | l :: rest ->
5248 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5249 loop linkindexbase rest
5250 | [] -> ()
5252 loop 0 state.layout;
5253 let rects =
5254 match state.mode with
5255 | LinkNav (Ltexact (pageno, linkno)) ->
5256 begin match getopaque pageno with
5257 | Some opaque ->
5258 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5259 (pageno, 5, (
5260 float x0, float y0,
5261 float x1, float y0,
5262 float x1, float y1,
5263 float x0, float y1)
5264 ) :: state.rects
5265 | None -> state.rects
5267 | _ -> state.rects
5269 showrects rects;
5270 showsel ();
5271 state.uioh#display;
5272 begin match state.mstate with
5273 | Mzoomrect ((x0, y0), (x1, y1)) ->
5274 Gl.enable `blend;
5275 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5276 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5277 GlDraw.rect (float x0, float y0)
5278 (float x1, float y1);
5279 Gl.disable `blend;
5280 | _ -> ()
5281 end;
5282 enttext ();
5283 scrollindicator ();
5284 Wsi.swapb ();
5287 let zoomrect x y x1 y1 =
5288 let x0 = min x x1
5289 and x1 = max x x1
5290 and y0 = min y y1 in
5291 gotoy (state.y + y0);
5292 state.anchor <- getanchor ();
5293 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5294 let margin =
5295 if state.w < conf.winw - state.scrollw
5296 then (conf.winw - state.scrollw - state.w) / 2
5297 else 0
5299 state.x <- (state.x + margin) - x0;
5300 setzoom zoom;
5301 Wsi.setcursor Wsi.CURSOR_INHERIT;
5302 state.mstate <- Mnone;
5305 let scrollx x =
5306 let winw = conf.winw - state.scrollw - 1 in
5307 let s = float x /. float winw in
5308 let destx = truncate (float (state.w + winw) *. s) in
5309 state.x <- winw - destx;
5310 gotoy_and_clear_text state.y;
5311 state.mstate <- Mscrollx;
5314 let scrolly y =
5315 let s = float y /. float conf.winh in
5316 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5317 gotoy_and_clear_text desty;
5318 state.mstate <- Mscrolly;
5321 let viewmouse button down x y mask =
5322 match button with
5323 | n when (n == 4 || n == 5) && not down ->
5324 if Wsi.withctrl mask
5325 then (
5326 match state.mstate with
5327 | Mzoom (oldn, i) ->
5328 if oldn = n
5329 then (
5330 if i = 2
5331 then
5332 let incr =
5333 match n with
5334 | 5 ->
5335 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5336 | _ ->
5337 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5339 let zoom = conf.zoom -. incr in
5340 setzoom zoom;
5341 state.mstate <- Mzoom (n, 0);
5342 else
5343 state.mstate <- Mzoom (n, i+1);
5345 else state.mstate <- Mzoom (n, 0)
5347 | _ -> state.mstate <- Mzoom (n, 0)
5349 else (
5350 match state.autoscroll with
5351 | Some step -> setautoscrollspeed step (n=4)
5352 | None ->
5353 let incr =
5354 if n = 4
5355 then -conf.scrollstep
5356 else conf.scrollstep
5358 let incr = incr * 2 in
5359 let y = clamp incr in
5360 gotoy_and_clear_text y
5363 | 1 when Wsi.withctrl mask ->
5364 if down
5365 then (
5366 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5367 state.mstate <- Mpan (x, y)
5369 else
5370 state.mstate <- Mnone
5372 | 3 ->
5373 if down
5374 then (
5375 Wsi.setcursor Wsi.CURSOR_CYCLE;
5376 let p = (x, y) in
5377 state.mstate <- Mzoomrect (p, p)
5379 else (
5380 match state.mstate with
5381 | Mzoomrect ((x0, y0), _) ->
5382 if abs (x-x0) > 10 && abs (y - y0) > 10
5383 then zoomrect x0 y0 x y
5384 else (
5385 state.mstate <- Mnone;
5386 Wsi.setcursor Wsi.CURSOR_INHERIT;
5387 G.postRedisplay "kill accidental zoom rect";
5389 | _ ->
5390 Wsi.setcursor Wsi.CURSOR_INHERIT;
5391 state.mstate <- Mnone
5394 | 1 when x > conf.winw - state.scrollw ->
5395 if down
5396 then
5397 let _, position, sh = state.uioh#scrollph in
5398 if y > truncate position && y < truncate (position +. sh)
5399 then state.mstate <- Mscrolly
5400 else scrolly y
5401 else
5402 state.mstate <- Mnone
5404 | 1 when y > conf.winh - state.hscrollh ->
5405 if down
5406 then
5407 let _, position, sw = state.uioh#scrollpw in
5408 if x > truncate position && x < truncate (position +. sw)
5409 then state.mstate <- Mscrollx
5410 else scrollx x
5411 else
5412 state.mstate <- Mnone
5414 | 1 ->
5415 let dest = if down then getunder x y else Unone in
5416 begin match dest with
5417 | Ulinkgoto _
5418 | Ulinkuri _
5419 | Uremote _
5420 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5421 gotounder dest
5423 | Unone when down ->
5424 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5425 state.mstate <- Mpan (x, y);
5427 | Unone | Utext _ ->
5428 if down
5429 then (
5430 if conf.angle mod 360 = 0
5431 then (
5432 state.mstate <- Msel ((x, y), (x, y));
5433 G.postRedisplay "mouse select";
5436 else (
5437 match state.mstate with
5438 | Mnone -> ()
5440 | Mzoom _ | Mscrollx | Mscrolly ->
5441 state.mstate <- Mnone
5443 | Mzoomrect ((x0, y0), _) ->
5444 zoomrect x0 y0 x y
5446 | Mpan _ ->
5447 Wsi.setcursor Wsi.CURSOR_INHERIT;
5448 state.mstate <- Mnone
5450 | Msel ((_, y0), (_, y1)) ->
5451 let rec loop = function
5452 | [] -> ()
5453 | l :: rest ->
5454 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5455 || ((y1 >= l.pagedispy
5456 && y1 <= (l.pagedispy + l.pagevh)))
5457 then
5458 match getopaque l.pageno with
5459 | Some opaque ->
5460 copysel conf.selcmd opaque;
5461 G.postRedisplay "copysel"
5462 | _ -> ()
5463 else loop rest
5465 loop state.layout;
5466 Wsi.setcursor Wsi.CURSOR_INHERIT;
5467 state.mstate <- Mnone;
5471 | _ -> ()
5474 let birdseyemouse button down x y mask
5475 (conf, leftx, _, hooverpageno, anchor) =
5476 match button with
5477 | 1 when down ->
5478 let rec loop = function
5479 | [] -> ()
5480 | l :: rest ->
5481 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5482 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5483 then (
5484 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5486 else loop rest
5488 loop state.layout
5489 | 3 -> ()
5490 | _ -> viewmouse button down x y mask
5493 let mouse button down x y mask =
5494 state.uioh <- state.uioh#button button down x y mask;
5497 let motion ~x ~y =
5498 state.uioh <- state.uioh#motion x y
5501 let pmotion ~x ~y =
5502 state.uioh <- state.uioh#pmotion x y;
5505 let uioh = object
5506 method display = ()
5508 method key key mask =
5509 begin match state.mode with
5510 | Textentry textentry -> textentrykeyboard key mask textentry
5511 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5512 | View -> viewkeyboard key mask
5513 | LinkNav linknav -> linknavkeyboard key mask linknav
5514 end;
5515 state.uioh
5517 method button button bstate x y mask =
5518 begin match state.mode with
5519 | LinkNav _
5520 | View -> viewmouse button bstate x y mask
5521 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5522 | Textentry _ -> ()
5523 end;
5524 state.uioh
5526 method motion x y =
5527 begin match state.mode with
5528 | Textentry _ -> ()
5529 | View | Birdseye _ | LinkNav _ ->
5530 match state.mstate with
5531 | Mzoom _ | Mnone -> ()
5533 | Mpan (x0, y0) ->
5534 let dx = x - x0
5535 and dy = y0 - y in
5536 state.mstate <- Mpan (x, y);
5537 if canpan ()
5538 then state.x <- state.x + dx;
5539 let y = clamp dy in
5540 gotoy_and_clear_text y
5542 | Msel (a, _) ->
5543 state.mstate <- Msel (a, (x, y));
5544 G.postRedisplay "motion select";
5546 | Mscrolly ->
5547 let y = min conf.winh (max 0 y) in
5548 scrolly y
5550 | Mscrollx ->
5551 let x = min conf.winw (max 0 x) in
5552 scrollx x
5554 | Mzoomrect (p0, _) ->
5555 state.mstate <- Mzoomrect (p0, (x, y));
5556 G.postRedisplay "motion zoomrect";
5557 end;
5558 state.uioh
5560 method pmotion x y =
5561 begin match state.mode with
5562 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5563 let rec loop = function
5564 | [] ->
5565 if hooverpageno != -1
5566 then (
5567 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5568 G.postRedisplay "pmotion birdseye no hoover";
5570 | l :: rest ->
5571 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5572 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5573 then (
5574 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5575 G.postRedisplay "pmotion birdseye hoover";
5577 else loop rest
5579 loop state.layout
5581 | Textentry _ -> ()
5583 | LinkNav _
5584 | View ->
5585 match state.mstate with
5586 | Mnone -> updateunder x y
5587 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5589 end;
5590 state.uioh
5592 method infochanged _ = ()
5594 method scrollph =
5595 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5596 let p, h = scrollph state.y maxy in
5597 state.scrollw, p, h
5599 method scrollpw =
5600 let winw = conf.winw - state.scrollw - 1 in
5601 let fwinw = float winw in
5602 let sw =
5603 let sw = fwinw /. float state.w in
5604 let sw = fwinw *. sw in
5605 max sw (float conf.scrollh)
5607 let position, sw =
5608 let f = state.w+winw in
5609 let r = float (winw-state.x) /. float f in
5610 let p = fwinw *. r in
5611 p-.sw/.2., sw
5613 let sw =
5614 if position +. sw > fwinw
5615 then fwinw -. position
5616 else sw
5618 state.hscrollh, position, sw
5620 method modehash =
5621 let modename =
5622 match state.mode with
5623 | LinkNav _ -> "links"
5624 | Textentry _ -> "textentry"
5625 | Birdseye _ -> "birdseye"
5626 | View -> "global"
5628 findkeyhash conf modename
5629 end;;
5631 module Config =
5632 struct
5633 open Parser
5635 let fontpath = ref "";;
5637 module KeyMap =
5638 Map.Make (struct type t = (int * int) let compare = compare end);;
5640 let unent s =
5641 let l = String.length s in
5642 let b = Buffer.create l in
5643 unent b s 0 l;
5644 Buffer.contents b;
5647 let home =
5648 try Sys.getenv "HOME"
5649 with exn ->
5650 prerr_endline
5651 ("Can not determine home directory location: " ^
5652 Printexc.to_string exn);
5656 let modifier_of_string = function
5657 | "alt" -> Wsi.altmask
5658 | "shift" -> Wsi.shiftmask
5659 | "ctrl" | "control" -> Wsi.ctrlmask
5660 | "meta" -> Wsi.metamask
5661 | _ -> 0
5664 let key_of_string =
5665 let r = Str.regexp "-" in
5666 fun s ->
5667 let elems = Str.full_split r s in
5668 let f n k m =
5669 let g s =
5670 let m1 = modifier_of_string s in
5671 if m1 = 0
5672 then (Wsi.namekey s, m)
5673 else (k, m lor m1)
5674 in function
5675 | Str.Delim s when n land 1 = 0 -> g s
5676 | Str.Text s -> g s
5677 | Str.Delim _ -> (k, m)
5679 let rec loop n k m = function
5680 | [] -> (k, m)
5681 | x :: xs ->
5682 let k, m = f n k m x in
5683 loop (n+1) k m xs
5685 loop 0 0 0 elems
5688 let keys_of_string =
5689 let r = Str.regexp "[ \t]" in
5690 fun s ->
5691 let elems = Str.split r s in
5692 List.map key_of_string elems
5695 let copykeyhashes c =
5696 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5699 let config_of c attrs =
5700 let apply c k v =
5702 match k with
5703 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5704 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5705 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5706 | "preload" -> { c with preload = bool_of_string v }
5707 | "page-bias" -> { c with pagebias = int_of_string v }
5708 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5709 | "auto-scroll-step" ->
5710 { c with autoscrollstep = max 0 (int_of_string v) }
5711 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5712 | "crop-hack" -> { c with crophack = bool_of_string v }
5713 | "throttle" ->
5714 let mw =
5715 match String.lowercase v with
5716 | "true" -> Some infinity
5717 | "false" -> None
5718 | f -> Some (float_of_string f)
5720 { c with maxwait = mw}
5721 | "highlight-links" -> { c with hlinks = bool_of_string v }
5722 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5723 | "vertical-margin" ->
5724 { c with interpagespace = max 0 (int_of_string v) }
5725 | "zoom" ->
5726 let zoom = float_of_string v /. 100. in
5727 let zoom = max zoom 0.0 in
5728 { c with zoom = zoom }
5729 | "presentation" -> { c with presentation = bool_of_string v }
5730 | "rotation-angle" -> { c with angle = int_of_string v }
5731 | "width" -> { c with winw = max 20 (int_of_string v) }
5732 | "height" -> { c with winh = max 20 (int_of_string v) }
5733 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5734 | "proportional-display" -> { c with proportional = bool_of_string v }
5735 | "pixmap-cache-size" ->
5736 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5737 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5738 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5739 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5740 | "persistent-location" -> { c with jumpback = bool_of_string v }
5741 | "background-color" -> { c with bgcolor = color_of_string v }
5742 | "scrollbar-in-presentation" ->
5743 { c with scrollbarinpm = bool_of_string v }
5744 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5745 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5746 | "mupdf-store-size" ->
5747 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5748 | "checkers" -> { c with checkers = bool_of_string v }
5749 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5750 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5751 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5752 | "uri-launcher" -> { c with urilauncher = unent v }
5753 | "path-launcher" -> { c with pathlauncher = unent v }
5754 | "color-space" -> { c with colorspace = colorspace_of_string v }
5755 | "invert-colors" -> { c with invert = bool_of_string v }
5756 | "brightness" -> { c with colorscale = float_of_string v }
5757 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5758 | "ghyllscroll" ->
5759 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5760 | "columns" ->
5761 let (n, _, _) as nab = multicolumns_of_string v in
5762 if n < 0
5763 then { c with columns = Csplit (-n, [||]) }
5764 else { c with columns = Cmulti (nab, [||]) }
5765 | "birds-eye-columns" ->
5766 { c with beyecolumns = Some (max (int_of_string v) 2) }
5767 | "selection-command" -> { c with selcmd = unent v }
5768 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5769 | _ -> c
5770 with exn ->
5771 prerr_endline ("Error processing attribute (`" ^
5772 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5775 let rec fold c = function
5776 | [] -> c
5777 | (k, v) :: rest ->
5778 let c = apply c k v in
5779 fold c rest
5781 fold { c with keyhashes = copykeyhashes c } attrs;
5784 let fromstring f pos n v d =
5785 try f v
5786 with exn ->
5787 dolog "Error processing attribute (%S=%S) at %d\n%s"
5788 n v pos (Printexc.to_string exn)
5793 let bookmark_of attrs =
5794 let rec fold title page rely = function
5795 | ("title", v) :: rest -> fold v page rely rest
5796 | ("page", v) :: rest -> fold title v rely rest
5797 | ("rely", v) :: rest -> fold title page v rest
5798 | _ :: rest -> fold title page rely rest
5799 | [] -> title, page, rely
5801 fold "invalid" "0" "0" attrs
5804 let doc_of attrs =
5805 let rec fold path page rely pan = function
5806 | ("path", v) :: rest -> fold v page rely pan rest
5807 | ("page", v) :: rest -> fold path v rely pan rest
5808 | ("rely", v) :: rest -> fold path page v pan rest
5809 | ("pan", v) :: rest -> fold path page rely v rest
5810 | _ :: rest -> fold path page rely pan rest
5811 | [] -> path, page, rely, pan
5813 fold "" "0" "0" "0" attrs
5816 let map_of attrs =
5817 let rec fold rs ls = function
5818 | ("out", v) :: rest -> fold v ls rest
5819 | ("in", v) :: rest -> fold rs v rest
5820 | _ :: rest -> fold ls rs rest
5821 | [] -> ls, rs
5823 fold "" "" attrs
5826 let setconf dst src =
5827 dst.scrollbw <- src.scrollbw;
5828 dst.scrollh <- src.scrollh;
5829 dst.icase <- src.icase;
5830 dst.preload <- src.preload;
5831 dst.pagebias <- src.pagebias;
5832 dst.verbose <- src.verbose;
5833 dst.scrollstep <- src.scrollstep;
5834 dst.maxhfit <- src.maxhfit;
5835 dst.crophack <- src.crophack;
5836 dst.autoscrollstep <- src.autoscrollstep;
5837 dst.maxwait <- src.maxwait;
5838 dst.hlinks <- src.hlinks;
5839 dst.underinfo <- src.underinfo;
5840 dst.interpagespace <- src.interpagespace;
5841 dst.zoom <- src.zoom;
5842 dst.presentation <- src.presentation;
5843 dst.angle <- src.angle;
5844 dst.winw <- src.winw;
5845 dst.winh <- src.winh;
5846 dst.savebmarks <- src.savebmarks;
5847 dst.memlimit <- src.memlimit;
5848 dst.proportional <- src.proportional;
5849 dst.texcount <- src.texcount;
5850 dst.sliceheight <- src.sliceheight;
5851 dst.thumbw <- src.thumbw;
5852 dst.jumpback <- src.jumpback;
5853 dst.bgcolor <- src.bgcolor;
5854 dst.scrollbarinpm <- src.scrollbarinpm;
5855 dst.tilew <- src.tilew;
5856 dst.tileh <- src.tileh;
5857 dst.mustoresize <- src.mustoresize;
5858 dst.checkers <- src.checkers;
5859 dst.aalevel <- src.aalevel;
5860 dst.trimmargins <- src.trimmargins;
5861 dst.trimfuzz <- src.trimfuzz;
5862 dst.urilauncher <- src.urilauncher;
5863 dst.colorspace <- src.colorspace;
5864 dst.invert <- src.invert;
5865 dst.colorscale <- src.colorscale;
5866 dst.redirectstderr <- src.redirectstderr;
5867 dst.ghyllscroll <- src.ghyllscroll;
5868 dst.columns <- src.columns;
5869 dst.beyecolumns <- src.beyecolumns;
5870 dst.selcmd <- src.selcmd;
5871 dst.updatecurs <- src.updatecurs;
5872 dst.pathlauncher <- src.pathlauncher;
5873 dst.keyhashes <- copykeyhashes src;
5876 let get s =
5877 let h = Hashtbl.create 10 in
5878 let dc = { defconf with angle = defconf.angle } in
5879 let rec toplevel v t spos _ =
5880 match t with
5881 | Vdata | Vcdata | Vend -> v
5882 | Vopen ("llppconfig", _, closed) ->
5883 if closed
5884 then v
5885 else { v with f = llppconfig }
5886 | Vopen _ ->
5887 error "unexpected subelement at top level" s spos
5888 | Vclose _ -> error "unexpected close at top level" s spos
5890 and llppconfig v t spos _ =
5891 match t with
5892 | Vdata | Vcdata -> v
5893 | Vend -> error "unexpected end of input in llppconfig" s spos
5894 | Vopen ("defaults", attrs, closed) ->
5895 let c = config_of dc attrs in
5896 setconf dc c;
5897 if closed
5898 then v
5899 else { v with f = defaults }
5901 | Vopen ("ui-font", attrs, closed) ->
5902 let rec getsize size = function
5903 | [] -> size
5904 | ("size", v) :: rest ->
5905 let size =
5906 fromstring int_of_string spos "size" v fstate.fontsize in
5907 getsize size rest
5908 | l -> getsize size l
5910 fstate.fontsize <- getsize fstate.fontsize attrs;
5911 if closed
5912 then v
5913 else { v with f = uifont (Buffer.create 10) }
5915 | Vopen ("doc", attrs, closed) ->
5916 let pathent, spage, srely, span = doc_of attrs in
5917 let path = unent pathent
5918 and pageno = fromstring int_of_string spos "page" spage 0
5919 and rely = fromstring float_of_string spos "rely" srely 0.0
5920 and pan = fromstring int_of_string spos "pan" span 0 in
5921 let c = config_of dc attrs in
5922 let anchor = (pageno, rely) in
5923 if closed
5924 then (Hashtbl.add h path (c, [], pan, anchor); v)
5925 else { v with f = doc path pan anchor c [] }
5927 | Vopen _ ->
5928 error "unexpected subelement in llppconfig" s spos
5930 | Vclose "llppconfig" -> { v with f = toplevel }
5931 | Vclose _ -> error "unexpected close in llppconfig" s spos
5933 and defaults v t spos _ =
5934 match t with
5935 | Vdata | Vcdata -> v
5936 | Vend -> error "unexpected end of input in defaults" s spos
5937 | Vopen ("keymap", attrs, closed) ->
5938 let modename =
5939 try List.assoc "mode" attrs
5940 with Not_found -> "global" in
5941 if closed
5942 then v
5943 else
5944 let ret keymap =
5945 let h = findkeyhash dc modename in
5946 KeyMap.iter (Hashtbl.replace h) keymap;
5947 defaults
5949 { v with f = pkeymap ret KeyMap.empty }
5951 | Vopen (_, _, _) ->
5952 error "unexpected subelement in defaults" s spos
5954 | Vclose "defaults" ->
5955 { v with f = llppconfig }
5957 | Vclose _ -> error "unexpected close in defaults" s spos
5959 and uifont b v t spos epos =
5960 match t with
5961 | Vdata | Vcdata ->
5962 Buffer.add_substring b s spos (epos - spos);
5964 | Vopen (_, _, _) ->
5965 error "unexpected subelement in ui-font" s spos
5966 | Vclose "ui-font" ->
5967 if String.length !fontpath = 0
5968 then fontpath := Buffer.contents b;
5969 { v with f = llppconfig }
5970 | Vclose _ -> error "unexpected close in ui-font" s spos
5971 | Vend -> error "unexpected end of input in ui-font" s spos
5973 and doc path pan anchor c bookmarks v t spos _ =
5974 match t with
5975 | Vdata | Vcdata -> v
5976 | Vend -> error "unexpected end of input in doc" s spos
5977 | Vopen ("bookmarks", _, closed) ->
5978 if closed
5979 then v
5980 else { v with f = pbookmarks path pan anchor c bookmarks }
5982 | Vopen ("keymap", attrs, closed) ->
5983 let modename =
5984 try List.assoc "mode" attrs
5985 with Not_found -> "global"
5987 if closed
5988 then v
5989 else
5990 let ret keymap =
5991 let h = findkeyhash c modename in
5992 KeyMap.iter (Hashtbl.replace h) keymap;
5993 doc path pan anchor c bookmarks
5995 { v with f = pkeymap ret KeyMap.empty }
5997 | Vopen (_, _, _) ->
5998 error "unexpected subelement in doc" s spos
6000 | Vclose "doc" ->
6001 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6002 { v with f = llppconfig }
6004 | Vclose _ -> error "unexpected close in doc" s spos
6006 and pkeymap ret keymap v t spos _ =
6007 match t with
6008 | Vdata | Vcdata -> v
6009 | Vend -> error "unexpected end of input in keymap" s spos
6010 | Vopen ("map", attrs, closed) ->
6011 let r, l = map_of attrs in
6012 let kss = fromstring keys_of_string spos "in" r [] in
6013 let lss = fromstring keys_of_string spos "out" l [] in
6014 let keymap =
6015 match kss with
6016 | [] -> keymap
6017 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6018 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6020 if closed
6021 then { v with f = pkeymap ret keymap }
6022 else
6023 let f () = v in
6024 { v with f = skip "map" f }
6026 | Vopen _ ->
6027 error "unexpected subelement in keymap" s spos
6029 | Vclose "keymap" ->
6030 { v with f = ret keymap }
6032 | Vclose _ -> error "unexpected close in keymap" s spos
6034 and pbookmarks path pan anchor c bookmarks v t spos _ =
6035 match t with
6036 | Vdata | Vcdata -> v
6037 | Vend -> error "unexpected end of input in bookmarks" s spos
6038 | Vopen ("item", attrs, closed) ->
6039 let titleent, spage, srely = bookmark_of attrs in
6040 let page = fromstring int_of_string spos "page" spage 0
6041 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6042 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6043 if closed
6044 then { v with f = pbookmarks path pan anchor c bookmarks }
6045 else
6046 let f () = v in
6047 { v with f = skip "item" f }
6049 | Vopen _ ->
6050 error "unexpected subelement in bookmarks" s spos
6052 | Vclose "bookmarks" ->
6053 { v with f = doc path pan anchor c bookmarks }
6055 | Vclose _ -> error "unexpected close in bookmarks" s spos
6057 and skip tag f v t spos _ =
6058 match t with
6059 | Vdata | Vcdata -> v
6060 | Vend ->
6061 error ("unexpected end of input in skipped " ^ tag) s spos
6062 | Vopen (tag', _, closed) ->
6063 if closed
6064 then v
6065 else
6066 let f' () = { v with f = skip tag f } in
6067 { v with f = skip tag' f' }
6068 | Vclose ctag ->
6069 if tag = ctag
6070 then f ()
6071 else error ("unexpected close in skipped " ^ tag) s spos
6074 parse { f = toplevel; accu = () } s;
6075 h, dc;
6078 let do_load f ic =
6080 let len = in_channel_length ic in
6081 let s = String.create len in
6082 really_input ic s 0 len;
6083 f s;
6084 with
6085 | Parse_error (msg, s, pos) ->
6086 let subs = subs s pos in
6087 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6088 failwith ("parse error: " ^ s)
6090 | exn ->
6091 failwith ("config load error: " ^ Printexc.to_string exn)
6094 let defconfpath =
6095 let dir =
6097 let dir = Filename.concat home ".config" in
6098 if Sys.is_directory dir then dir else home
6099 with _ -> home
6101 Filename.concat dir "llpp.conf"
6104 let confpath = ref defconfpath;;
6106 let load1 f =
6107 if Sys.file_exists !confpath
6108 then
6109 match
6110 (try Some (open_in_bin !confpath)
6111 with exn ->
6112 prerr_endline
6113 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6114 Printexc.to_string exn);
6115 None
6117 with
6118 | Some ic ->
6119 begin try
6120 f (do_load get ic)
6121 with exn ->
6122 prerr_endline
6123 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6124 Printexc.to_string exn);
6125 end;
6126 close_in ic;
6128 | None -> ()
6129 else
6130 f (Hashtbl.create 0, defconf)
6133 let load () =
6134 let f (h, dc) =
6135 let pc, pb, px, pa =
6137 Hashtbl.find h (Filename.basename state.path)
6138 with Not_found -> dc, [], 0, (0, 0.0)
6140 setconf defconf dc;
6141 setconf conf pc;
6142 state.bookmarks <- pb;
6143 state.x <- px;
6144 state.scrollw <- conf.scrollbw;
6145 if conf.jumpback
6146 then state.anchor <- pa;
6147 cbput state.hists.nav pa;
6149 load1 f
6152 let add_attrs bb always dc c =
6153 let ob s a b =
6154 if always || a != b
6155 then Printf.bprintf bb "\n %s='%b'" s a
6156 and oi s a b =
6157 if always || a != b
6158 then Printf.bprintf bb "\n %s='%d'" s a
6159 and oI s a b =
6160 if always || a != b
6161 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6162 and oz s a b =
6163 if always || a <> b
6164 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6165 and oF s a b =
6166 if always || a <> b
6167 then Printf.bprintf bb "\n %s='%f'" s a
6168 and oc s a b =
6169 if always || a <> b
6170 then
6171 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6172 and oC s a b =
6173 if always || a <> b
6174 then
6175 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6176 and oR s a b =
6177 if always || a <> b
6178 then
6179 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6180 and os s a b =
6181 if always || a <> b
6182 then
6183 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6184 and og s a b =
6185 if always || a <> b
6186 then
6187 match a with
6188 | None -> ()
6189 | Some (_N, _A, _B) ->
6190 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6191 and oW s a b =
6192 if always || a <> b
6193 then
6194 let v =
6195 match a with
6196 | None -> "false"
6197 | Some f ->
6198 if f = infinity
6199 then "true"
6200 else string_of_float f
6202 Printf.bprintf bb "\n %s='%s'" s v
6203 and oco s a b =
6204 if always || a <> b
6205 then
6206 match a with
6207 | Cmulti ((n, a, b), _) when n > 1 ->
6208 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6209 | Csplit (n, _) when n > 1 ->
6210 Printf.bprintf bb "\n %s='%d'" s ~-n
6211 | _ -> ()
6212 and obeco s a b =
6213 if always || a <> b
6214 then
6215 match a with
6216 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6217 | _ -> ()
6219 let w, h =
6220 if always
6221 then dc.winw, dc.winh
6222 else
6223 match state.fullscreen with
6224 | Some wh -> wh
6225 | None -> c.winw, c.winh
6227 let zoom, presentation, interpagespace, maxwait =
6228 if always
6229 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6230 else
6231 match state.mode with
6232 | Birdseye (bc, _, _, _, _) ->
6233 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6234 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6236 oi "width" w dc.winw;
6237 oi "height" h dc.winh;
6238 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6239 oi "scroll-handle-height" c.scrollh dc.scrollh;
6240 ob "case-insensitive-search" c.icase dc.icase;
6241 ob "preload" c.preload dc.preload;
6242 oi "page-bias" c.pagebias dc.pagebias;
6243 oi "scroll-step" c.scrollstep dc.scrollstep;
6244 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6245 ob "max-height-fit" c.maxhfit dc.maxhfit;
6246 ob "crop-hack" c.crophack dc.crophack;
6247 oW "throttle" maxwait dc.maxwait;
6248 ob "highlight-links" c.hlinks dc.hlinks;
6249 ob "under-cursor-info" c.underinfo dc.underinfo;
6250 oi "vertical-margin" interpagespace dc.interpagespace;
6251 oz "zoom" zoom dc.zoom;
6252 ob "presentation" presentation dc.presentation;
6253 oi "rotation-angle" c.angle dc.angle;
6254 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6255 ob "proportional-display" c.proportional dc.proportional;
6256 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6257 oi "tex-count" c.texcount dc.texcount;
6258 oi "slice-height" c.sliceheight dc.sliceheight;
6259 oi "thumbnail-width" c.thumbw dc.thumbw;
6260 ob "persistent-location" c.jumpback dc.jumpback;
6261 oc "background-color" c.bgcolor dc.bgcolor;
6262 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6263 oi "tile-width" c.tilew dc.tilew;
6264 oi "tile-height" c.tileh dc.tileh;
6265 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6266 ob "checkers" c.checkers dc.checkers;
6267 oi "aalevel" c.aalevel dc.aalevel;
6268 ob "trim-margins" c.trimmargins dc.trimmargins;
6269 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6270 os "uri-launcher" c.urilauncher dc.urilauncher;
6271 os "path-launcher" c.pathlauncher dc.pathlauncher;
6272 oC "color-space" c.colorspace dc.colorspace;
6273 ob "invert-colors" c.invert dc.invert;
6274 oF "brightness" c.colorscale dc.colorscale;
6275 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6276 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6277 oco "columns" c.columns dc.columns;
6278 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6279 os "selection-command" c.selcmd dc.selcmd;
6280 ob "update-cursor" c.updatecurs dc.updatecurs;
6283 let keymapsbuf always dc c =
6284 let bb = Buffer.create 16 in
6285 let rec loop = function
6286 | [] -> ()
6287 | (modename, h) :: rest ->
6288 let dh = findkeyhash dc modename in
6289 if always || h <> dh
6290 then (
6291 if Hashtbl.length h > 0
6292 then (
6293 if Buffer.length bb > 0
6294 then Buffer.add_char bb '\n';
6295 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6296 Hashtbl.iter (fun i o ->
6297 let isdifferent = always ||
6299 let dO = Hashtbl.find dh i in
6300 dO <> o
6301 with Not_found -> true
6303 if isdifferent
6304 then
6305 let addkm (k, m) =
6306 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6307 if Wsi.withalt m then Buffer.add_string bb "alt-";
6308 if Wsi.withshift m then Buffer.add_string bb "shift-";
6309 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6310 Buffer.add_string bb (Wsi.keyname k);
6312 let addkms l =
6313 let rec loop = function
6314 | [] -> ()
6315 | km :: [] -> addkm km
6316 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6318 loop l
6320 Buffer.add_string bb "<map in='";
6321 addkm i;
6322 match o with
6323 | KMinsrt km ->
6324 Buffer.add_string bb "' out='";
6325 addkm km;
6326 Buffer.add_string bb "'/>\n"
6328 | KMinsrl kms ->
6329 Buffer.add_string bb "' out='";
6330 addkms kms;
6331 Buffer.add_string bb "'/>\n"
6333 | KMmulti (ins, kms) ->
6334 Buffer.add_char bb ' ';
6335 addkms ins;
6336 Buffer.add_string bb "' out='";
6337 addkms kms;
6338 Buffer.add_string bb "'/>\n"
6339 ) h;
6340 Buffer.add_string bb "</keymap>";
6343 loop rest
6345 loop c.keyhashes;
6349 let save () =
6350 let uifontsize = fstate.fontsize in
6351 let bb = Buffer.create 32768 in
6352 let f (h, dc) =
6353 let dc = if conf.bedefault then conf else dc in
6354 Buffer.add_string bb "<llppconfig>\n";
6356 if String.length !fontpath > 0
6357 then
6358 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6359 uifontsize
6360 !fontpath
6361 else (
6362 if uifontsize <> 14
6363 then
6364 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6367 Buffer.add_string bb "<defaults ";
6368 add_attrs bb true dc dc;
6369 let kb = keymapsbuf true dc dc in
6370 if Buffer.length kb > 0
6371 then (
6372 Buffer.add_string bb ">\n";
6373 Buffer.add_buffer bb kb;
6374 Buffer.add_string bb "\n</defaults>\n";
6376 else Buffer.add_string bb "/>\n";
6378 let adddoc path pan anchor c bookmarks =
6379 if bookmarks == [] && c = dc && anchor = emptyanchor
6380 then ()
6381 else (
6382 Printf.bprintf bb "<doc path='%s'"
6383 (enent path 0 (String.length path));
6385 if anchor <> emptyanchor
6386 then (
6387 let n, y = anchor in
6388 Printf.bprintf bb " page='%d'" n;
6389 if y > 1e-6
6390 then
6391 Printf.bprintf bb " rely='%f'" y
6395 if pan != 0
6396 then Printf.bprintf bb " pan='%d'" pan;
6398 add_attrs bb false dc c;
6399 let kb = keymapsbuf false dc c in
6401 begin match bookmarks with
6402 | [] ->
6403 if Buffer.length kb > 0
6404 then (
6405 Buffer.add_string bb ">\n";
6406 Buffer.add_buffer bb kb;
6407 Buffer.add_string bb "</doc>\n";
6409 else Buffer.add_string bb "/>\n"
6410 | _ ->
6411 Buffer.add_string bb ">\n<bookmarks>\n";
6412 List.iter (fun (title, _level, (page, rely)) ->
6413 Printf.bprintf bb
6414 "<item title='%s' page='%d'"
6415 (enent title 0 (String.length title))
6416 page
6418 if rely > 1e-6
6419 then
6420 Printf.bprintf bb " rely='%f'" rely
6422 Buffer.add_string bb "/>\n";
6423 ) bookmarks;
6424 Buffer.add_string bb "</bookmarks>";
6425 if Buffer.length kb > 0
6426 then (
6427 Buffer.add_string bb "\n";
6428 Buffer.add_buffer bb kb;
6430 Buffer.add_string bb "\n</doc>\n";
6431 end;
6435 let pan, conf =
6436 match state.mode with
6437 | Birdseye (c, pan, _, _, _) ->
6438 let beyecolumns =
6439 match conf.columns with
6440 | Cmulti ((c, _, _), _) -> Some c
6441 | Csingle -> None
6442 | Csplit _ -> None
6443 and columns =
6444 match c.columns with
6445 | Cmulti (c, _) -> Cmulti (c, [||])
6446 | Csingle -> Csingle
6447 | Csplit _ -> failwith "quit from bird's eye while split"
6449 pan, { c with beyecolumns = beyecolumns; columns = columns }
6450 | _ -> state.x, conf
6452 let basename = Filename.basename state.path in
6453 adddoc basename pan (getanchor ())
6454 { conf with
6455 autoscrollstep =
6456 match state.autoscroll with
6457 | Some step -> step
6458 | None -> conf.autoscrollstep }
6459 (if conf.savebmarks then state.bookmarks else []);
6461 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6462 if basename <> path
6463 then adddoc path x y c bookmarks
6464 ) h;
6465 Buffer.add_string bb "</llppconfig>";
6467 load1 f;
6468 if Buffer.length bb > 0
6469 then
6471 let tmp = !confpath ^ ".tmp" in
6472 let oc = open_out_bin tmp in
6473 Buffer.output_buffer oc bb;
6474 close_out oc;
6475 Unix.rename tmp !confpath;
6476 with exn ->
6477 prerr_endline
6478 ("error while saving configuration: " ^ Printexc.to_string exn)
6480 end;;
6482 let () =
6483 Arg.parse
6484 (Arg.align
6485 [("-p", Arg.String (fun s -> state.password <- s) ,
6486 "<password> Set password");
6488 ("-f", Arg.String (fun s -> Config.fontpath := s),
6489 "<path> Set path to the user interface font");
6491 ("-c", Arg.String (fun s -> Config.confpath := s),
6492 "<path> Set path to the configuration file");
6494 ("-v", Arg.Unit (fun () ->
6495 Printf.printf
6496 "%s\nconfiguration path: %s\n"
6497 (version ())
6498 Config.defconfpath
6500 exit 0), " Print version and exit");
6503 (fun s -> state.path <- s)
6504 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6506 if String.length state.path = 0
6507 then (prerr_endline "file name missing"; exit 1);
6509 Config.load ();
6511 let globalkeyhash = findkeyhash conf "global" in
6512 let wsfd, winw, winh = Wsi.init (object
6513 method expose =
6514 if nogeomcmds state.geomcmds
6515 then display ()
6516 method display = display ()
6517 method reshape w h = reshape w h
6518 method mouse b d x y m = mouse b d x y m
6519 method motion x y = state.mpos <- (x, y); motion x y
6520 method pmotion x y = state.mpos <- (x, y); pmotion x y
6521 method key k m =
6522 let mascm = m land (
6523 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6524 ) in
6525 match state.keystate with
6526 | KSnone ->
6527 let km = k, mascm in
6528 begin
6529 match
6530 try Hashtbl.find globalkeyhash km
6531 with Not_found ->
6532 let modehash = state.uioh#modehash in
6533 try Hashtbl.find modehash km
6534 with Not_found -> KMinsrt (k, m)
6535 with
6536 | KMinsrt (k, m) -> keyboard k m
6537 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6538 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6540 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6541 List.iter (fun (k, m) -> keyboard k m) insrt;
6542 state.keystate <- KSnone
6543 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6544 state.keystate <- KSinto (keys, insrt)
6545 | _ ->
6546 state.keystate <- KSnone
6548 method enter x y = state.mpos <- (x, y); pmotion x y
6549 method leave = state.mpos <- (-1, -1)
6550 method quit = raise Quit
6551 end) conf.winw conf.winh (platform = Posx) in
6553 state.wsfd <- wsfd;
6555 if not (
6556 List.exists GlMisc.check_extension
6557 [ "GL_ARB_texture_rectangle"
6558 ; "GL_EXT_texture_recangle"
6559 ; "GL_NV_texture_rectangle" ]
6561 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6563 let cr, sw = Unix.pipe ()
6564 and sr, cw = Unix.pipe () in
6566 cloexec cr;
6567 cloexec sw;
6568 cloexec sr;
6569 cloexec cw;
6571 setcheckers conf.checkers;
6572 redirectstderr ();
6574 init (cr, cw) (
6575 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6576 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6577 !Config.fontpath
6579 state.sr <- sr;
6580 state.sw <- sw;
6581 state.text <- "Opening " ^ state.path;
6582 reshape winw winh;
6583 opendoc state.path state.password;
6584 state.uioh <- uioh;
6586 let rec loop deadline =
6587 let r =
6588 match state.errfd with
6589 | None -> [state.sr; state.wsfd]
6590 | Some fd -> [state.sr; state.wsfd; fd]
6592 if state.redisplay
6593 then (
6594 state.redisplay <- false;
6595 display ();
6597 let timeout =
6598 let now = now () in
6599 if deadline > now
6600 then (
6601 if deadline = infinity
6602 then ~-.1.0
6603 else max 0.0 (deadline -. now)
6605 else 0.0
6607 let r, _, _ =
6608 try Unix.select r [] [] timeout
6609 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6611 begin match r with
6612 | [] ->
6613 state.ghyll None;
6614 let newdeadline =
6615 if state.ghyll == noghyll
6616 then
6617 match state.autoscroll with
6618 | Some step when step != 0 ->
6619 let y = state.y + step in
6620 let y =
6621 if y < 0
6622 then state.maxy
6623 else if y >= state.maxy then 0 else y
6625 gotoy y;
6626 if state.mode = View
6627 then state.text <- "";
6628 deadline +. 0.01
6629 | _ -> infinity
6630 else deadline +. 0.01
6632 loop newdeadline
6634 | l ->
6635 let rec checkfds = function
6636 | [] -> ()
6637 | fd :: rest when fd = state.sr ->
6638 let cmd = readcmd state.sr in
6639 act cmd;
6640 checkfds rest
6642 | fd :: rest when fd = state.wsfd ->
6643 Wsi.readresp fd;
6644 checkfds rest
6646 | fd :: rest ->
6647 let s = String.create 80 in
6648 let n = Unix.read fd s 0 80 in
6649 if conf.redirectstderr
6650 then (
6651 Buffer.add_substring state.errmsgs s 0 n;
6652 state.newerrmsgs <- true;
6653 state.redisplay <- true;
6655 else (
6656 prerr_string (String.sub s 0 n);
6657 flush stderr;
6659 checkfds rest
6661 checkfds l;
6662 let newdeadline =
6663 let deadline1 =
6664 if deadline = infinity
6665 then now () +. 0.01
6666 else deadline
6668 match state.autoscroll with
6669 | Some step when step != 0 -> deadline1
6670 | _ -> if state.ghyll == noghyll then infinity else deadline1
6672 loop newdeadline
6673 end;
6676 loop infinity;
6677 with Quit ->
6678 Config.save ();
6679 exit 0;