Simplify
[llpp.git] / main.ml
blobcb89a67ab2e8f4d9045e5e6b2829b4c233605957
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 = 128 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 _ -> getpageyh pageno
1018 let getpagedim pageno =
1019 let rec f ppdim l =
1020 match l with
1021 | (n, _, _, _) as pdim :: rest ->
1022 if n >= pageno
1023 then (if n = pageno then pdim else ppdim)
1024 else f pdim rest
1026 | [] -> ppdim
1028 f (-1, -1, -1, -1) state.pdims
1031 let getpagey pageno = fst (getpageyh pageno);;
1033 let nogeomcmds cmds =
1034 match cmds with
1035 | s, [] -> String.length s = 0
1036 | _ -> false
1039 let layout1 y sh =
1040 let sh = sh - state.hscrollh in
1041 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1042 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1043 match pdims with
1044 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1045 let ips = calcips h in
1046 let yinc =
1047 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1048 then ips
1049 else 0
1051 (w, h, ips, xoff), rest, pdimno + 1, yinc
1052 | _ ->
1053 prev, pdims, pdimno, 0
1055 let dy = dy + yinc in
1056 let py = py + yinc in
1057 if pageno = state.pagecount || dy >= sh
1058 then
1059 accu
1060 else
1061 let vy = y + dy in
1062 if py + h <= vy - yinc
1063 then
1064 let py = py + h + ips in
1065 let dy = max 0 (py - y) in
1066 f ~pageno:(pageno+1)
1067 ~pdimno
1068 ~prev:curr
1071 ~pdims:rest
1072 ~accu
1073 else
1074 let pagey = vy - py in
1075 let pagevh = h - pagey in
1076 let pagevh = min (sh - dy) pagevh in
1077 let off = if yinc > 0 then py - vy else 0 in
1078 let py = py + h + ips in
1079 let pagex, dx =
1080 let xoff = xoff +
1081 if state.w < conf.winw - state.scrollw
1082 then (conf.winw - state.scrollw - state.w) / 2
1083 else 0
1085 let dispx = xoff + state.x in
1086 if dispx < 0
1087 then (-dispx, 0)
1088 else (0, dispx)
1090 let pagevw =
1091 let lw = w - pagex in
1092 min lw (conf.winw - state.scrollw)
1094 let e =
1095 { pageno = pageno
1096 ; pagedimno = pdimno
1097 ; pagew = w
1098 ; pageh = h
1099 ; pagex = pagex
1100 ; pagey = pagey + off
1101 ; pagevw = pagevw
1102 ; pagevh = pagevh - off
1103 ; pagedispx = dx
1104 ; pagedispy = dy + off
1107 let accu = e :: accu in
1108 f ~pageno:(pageno+1)
1109 ~pdimno
1110 ~prev:curr
1112 ~dy:(dy+pagevh+ips)
1113 ~pdims:rest
1114 ~accu
1116 let accu =
1118 ~pageno:0
1119 ~pdimno:~-1
1120 ~prev:(0,0,0,0)
1121 ~py:0
1122 ~dy:0
1123 ~pdims:state.pdims
1124 ~accu:[]
1126 List.rev accu
1129 let layoutN ((columns, coverA, coverB), b) y sh =
1130 let sh = sh - state.hscrollh in
1131 let rec fold accu n =
1132 if n = Array.length b
1133 then accu
1134 else
1135 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1136 if (vy - y) > sh &&
1137 (n = coverA - 1
1138 || n = state.pagecount - coverB
1139 || (n - coverA) mod columns = columns - 1)
1140 then accu
1141 else
1142 let accu =
1143 if vy + h > y
1144 then
1145 let pagey = max 0 (y - vy) in
1146 let pagedispy = if pagey > 0 then 0 else vy - y in
1147 let pagedispx, pagex =
1148 let pdx =
1149 if n = coverA - 1 || n = state.pagecount - coverB
1150 then state.x + (conf.winw - state.scrollw - w) / 2
1151 else dx + xoff + state.x
1153 if pdx < 0
1154 then 0, -pdx
1155 else pdx, 0
1157 let pagevw =
1158 let vw = conf.winw - state.scrollw - pagedispx in
1159 let pw = w - pagex in
1160 min vw pw
1162 let pagevh = min (h - pagey) (sh - pagedispy) in
1163 if pagevw > 0 && pagevh > 0
1164 then
1165 let e =
1166 { pageno = n
1167 ; pagedimno = pdimno
1168 ; pagew = w
1169 ; pageh = h
1170 ; pagex = pagex
1171 ; pagey = pagey
1172 ; pagevw = pagevw
1173 ; pagevh = pagevh
1174 ; pagedispx = pagedispx
1175 ; pagedispy = pagedispy
1178 e :: accu
1179 else
1180 accu
1181 else
1182 accu
1184 fold accu (n+1)
1186 List.rev (fold [] 0)
1189 let layoutS (columns, b) y sh =
1190 let sh = sh - state.hscrollh in
1191 let rec fold accu n =
1192 if n = Array.length b
1193 then accu
1194 else
1195 let pdimno, px, vy, (_, pw, h, xoff) = b.(n) in
1196 if (vy - y) > sh
1197 then accu
1198 else
1199 let accu =
1200 if vy + h > y
1201 then
1202 let x = xoff + state.x in
1203 let pagey = max 0 (y - vy) in
1204 let pagedispy = if pagey > 0 then 0 else vy - y in
1205 let pagedispx, pagex =
1206 if x < 0
1207 then 0, px - x
1208 else x, px
1210 let w = pw/columns in
1211 let pagevw = min (w - (pagex mod w)) (conf.winw - state.scrollw) in
1212 let pagevh = min (h - pagey) (sh - pagedispy) in
1213 if pagevw > 0 && pagevh > 0
1214 then
1215 let e =
1216 { pageno = n/columns
1217 ; pagedimno = pdimno
1218 ; pagew = pw
1219 ; pageh = h
1220 ; pagex = pagex
1221 ; pagey = pagey
1222 ; pagevw = pagevw
1223 ; pagevh = pagevh
1224 ; pagedispx = pagedispx
1225 ; pagedispy = pagedispy
1228 e :: accu
1229 else
1230 accu
1231 else
1232 accu
1234 fold accu (n+1)
1236 List.rev (fold [] 0)
1239 let layout y sh =
1240 if nogeomcmds state.geomcmds
1241 then
1242 match conf.columns with
1243 | Csingle -> layout1 y sh
1244 | Cmulti c -> layoutN c y sh
1245 | Csplit s -> layoutS s y sh
1246 else []
1249 let clamp incr =
1250 let y = state.y + incr in
1251 let y = max 0 y in
1252 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1256 let itertiles l f =
1257 let tilex = l.pagex mod conf.tilew in
1258 let tiley = l.pagey mod conf.tileh in
1260 let col = l.pagex / conf.tilew in
1261 let row = l.pagey / conf.tileh in
1263 let vw =
1264 let a = l.pagew - l.pagex in
1265 let b = conf.winw - state.scrollw in
1266 min a b
1267 and vh = l.pagevh in
1269 let rec rowloop row y0 dispy h =
1270 if h = 0
1271 then ()
1272 else (
1273 let dh = conf.tileh - y0 in
1274 let dh = min h dh in
1275 let rec colloop col x0 dispx w =
1276 if w = 0
1277 then ()
1278 else (
1279 let dw = conf.tilew - x0 in
1280 let dw = min w dw in
1282 f col row dispx dispy x0 y0 dw dh;
1283 colloop (col+1) 0 (dispx+dw) (w-dw)
1286 colloop col tilex l.pagedispx vw;
1287 rowloop (row+1) 0 (dispy+dh) (h-dh)
1290 if vw > 0 && vh > 0
1291 then rowloop row tiley l.pagedispy vh;
1294 let gettileopaque l col row =
1295 let key =
1296 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1298 try Some (Hashtbl.find state.tilemap key)
1299 with Not_found -> None
1302 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1303 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1304 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1307 let drawtiles l color =
1308 GlDraw.color color;
1309 let f col row x y tilex tiley w h =
1310 match gettileopaque l col row with
1311 | Some (opaque, _, t) ->
1312 let params = x, y, w, h, tilex, tiley in
1313 if conf.invert
1314 then (
1315 Gl.enable `blend;
1316 GlFunc.blend_func `zero `one_minus_src_color;
1318 drawtile params opaque;
1319 if conf.invert
1320 then Gl.disable `blend;
1321 if conf.debug
1322 then (
1323 let s = Printf.sprintf
1324 "%d[%d,%d] %f sec"
1325 l.pageno col row t
1327 let w = measurestr fstate.fontsize s in
1328 GlMisc.push_attrib [`current];
1329 GlDraw.color (0.0, 0.0, 0.0);
1330 GlDraw.rect
1331 (float (x-2), float (y-2))
1332 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1333 GlDraw.color (1.0, 1.0, 1.0);
1334 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1335 GlMisc.pop_attrib ();
1338 | _ ->
1339 let w =
1340 let lw = conf.winw - state.scrollw - x in
1341 min lw w
1342 and h =
1343 let lh = conf.winh - y in
1344 min lh h
1346 Gl.enable `texture_2d;
1347 begin match state.texid with
1348 | Some id ->
1349 GlTex.bind_texture `texture_2d id;
1350 let x0 = float x
1351 and y0 = float y
1352 and x1 = float (x+w)
1353 and y1 = float (y+h) in
1355 let tw = float w /. 64.0
1356 and th = float h /. 64.0 in
1357 let tx0 = float tilex /. 64.0
1358 and ty0 = float tiley /. 64.0 in
1359 let tx1 = tx0 +. tw
1360 and ty1 = ty0 +. th in
1361 GlDraw.begins `quads;
1362 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1363 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1364 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1365 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1366 GlDraw.ends ();
1368 Gl.disable `texture_2d;
1369 | None ->
1370 GlDraw.color (1.0, 1.0, 1.0);
1371 GlDraw.rect
1372 (float x, float y)
1373 (float (x+w), float (y+h));
1374 end;
1375 if w > 128 && h > fstate.fontsize + 10
1376 then (
1377 GlDraw.color (0.0, 0.0, 0.0);
1378 let c, r =
1379 if conf.verbose
1380 then (col*conf.tilew, row*conf.tileh)
1381 else col, row
1383 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1385 GlDraw.color color;
1387 itertiles l f
1390 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1392 let tilevisible1 l x y =
1393 let ax0 = l.pagex
1394 and ax1 = l.pagex + l.pagevw
1395 and ay0 = l.pagey
1396 and ay1 = l.pagey + l.pagevh in
1398 let bx0 = x
1399 and by0 = y in
1400 let bx1 = min (bx0 + conf.tilew) l.pagew
1401 and by1 = min (by0 + conf.tileh) l.pageh in
1403 let rx0 = max ax0 bx0
1404 and ry0 = max ay0 by0
1405 and rx1 = min ax1 bx1
1406 and ry1 = min ay1 by1 in
1408 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1409 nonemptyintersection
1412 let tilevisible layout n x y =
1413 let rec findpageinlayout = function
1414 | l :: _ when l.pageno = n -> tilevisible1 l x y
1415 | _ :: rest -> findpageinlayout rest
1416 | [] -> false
1418 findpageinlayout layout
1421 let tileready l x y =
1422 tilevisible1 l x y &&
1423 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1426 let tilepage n p layout =
1427 let rec loop = function
1428 | l :: rest ->
1429 if l.pageno = n
1430 then
1431 let f col row _ _ _ _ _ _ =
1432 if state.currently = Idle
1433 then
1434 match gettileopaque l col row with
1435 | Some _ -> ()
1436 | None ->
1437 let x = col*conf.tilew
1438 and y = row*conf.tileh in
1439 let w =
1440 let w = l.pagew - x in
1441 min w conf.tilew
1443 let h =
1444 let h = l.pageh - y in
1445 min h conf.tileh
1447 wcmd "tile %s %d %d %d %d" p x y w h;
1448 state.currently <-
1449 Tiling (
1450 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1451 conf.tilew, conf.tileh
1454 itertiles l f;
1455 else
1456 loop rest
1458 | [] -> ()
1460 if nogeomcmds state.geomcmds
1461 then loop layout;
1464 let preloadlayout visiblepages =
1465 let presentation = conf.presentation in
1466 let interpagespace = conf.interpagespace in
1467 let maxy = state.maxy in
1468 conf.presentation <- false;
1469 conf.interpagespace <- 0;
1470 state.maxy <- calcheight ();
1471 let y =
1472 match visiblepages with
1473 | [] -> 0
1474 | l :: _ -> getpagey l.pageno + l.pagey
1476 let y = if y < conf.winh then 0 else y - conf.winh in
1477 let h = state.y - y + conf.winh*3 in
1478 let pages = layout y h in
1479 conf.presentation <- presentation;
1480 conf.interpagespace <- interpagespace;
1481 state.maxy <- maxy;
1482 pages;
1485 let load pages =
1486 let rec loop pages =
1487 if state.currently != Idle
1488 then ()
1489 else
1490 match pages with
1491 | l :: rest ->
1492 begin match getopaque l.pageno with
1493 | None ->
1494 wcmd "page %d %d" l.pageno l.pagedimno;
1495 state.currently <- Loading (l, state.gen);
1496 | Some opaque ->
1497 tilepage l.pageno opaque pages;
1498 loop rest
1499 end;
1500 | _ -> ()
1502 if nogeomcmds state.geomcmds
1503 then loop pages
1506 let preload pages =
1507 load pages;
1508 if conf.preload && state.currently = Idle
1509 then load (preloadlayout pages);
1512 let layoutready layout =
1513 let rec fold all ls =
1514 all && match ls with
1515 | l :: rest ->
1516 let seen = ref false in
1517 let allvisible = ref true in
1518 let foo col row _ _ _ _ _ _ =
1519 seen := true;
1520 allvisible := !allvisible &&
1521 begin match gettileopaque l col row with
1522 | Some _ -> true
1523 | None -> false
1526 itertiles l foo;
1527 fold (!seen && !allvisible) rest
1528 | [] -> true
1530 let alltilesvisible = fold true layout in
1531 alltilesvisible;
1534 let gotoy y =
1535 let y = bound y 0 state.maxy in
1536 let y, layout, proceed =
1537 match conf.maxwait with
1538 | Some time when state.ghyll == noghyll ->
1539 begin match state.throttle with
1540 | None ->
1541 let layout = layout y conf.winh in
1542 let ready = layoutready layout in
1543 if not ready
1544 then (
1545 load layout;
1546 state.throttle <- Some (layout, y, now ());
1548 else G.postRedisplay "gotoy showall (None)";
1549 y, layout, ready
1550 | Some (_, _, started) ->
1551 let dt = now () -. started in
1552 if dt > time
1553 then (
1554 state.throttle <- None;
1555 let layout = layout y conf.winh in
1556 load layout;
1557 G.postRedisplay "maxwait";
1558 y, layout, true
1560 else -1, [], false
1563 | _ ->
1564 let layout = layout y conf.winh in
1565 if true || layoutready layout
1566 then G.postRedisplay "gotoy ready";
1567 y, layout, true
1569 if proceed
1570 then (
1571 state.y <- y;
1572 state.layout <- layout;
1573 begin match state.mode with
1574 | LinkNav (Ltexact (pageno, linkno)) ->
1575 let rec loop = function
1576 | [] ->
1577 state.mode <- LinkNav (Ltgendir 0)
1578 | l :: _ when l.pageno = pageno ->
1579 begin match getopaque pageno with
1580 | None ->
1581 state.mode <- LinkNav (Ltgendir 0)
1582 | Some opaque ->
1583 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1584 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1585 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1586 then state.mode <- LinkNav (Ltgendir 0)
1588 | _ :: rest -> loop rest
1590 loop layout
1591 | _ -> ()
1592 end;
1593 begin match state.mode with
1594 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1595 if not (pagevisible layout pageno)
1596 then (
1597 match state.layout with
1598 | [] -> ()
1599 | l :: _ ->
1600 state.mode <- Birdseye (
1601 conf, leftx, l.pageno, hooverpageno, anchor
1604 | LinkNav (Ltgendir dir as lt) ->
1605 let linknav =
1606 let rec loop = function
1607 | [] -> lt
1608 | l :: rest ->
1609 match getopaque l.pageno with
1610 | None -> loop rest
1611 | Some opaque ->
1612 let link =
1613 let ld =
1614 if dir = 0
1615 then LDfirstvisible (l.pagex, l.pagey, dir)
1616 else (
1617 if dir > 0 then LDfirst else LDlast
1620 findlink opaque ld
1622 match link with
1623 | Lnotfound -> loop rest
1624 | Lfound n ->
1625 showlinktype (getlink opaque n);
1626 Ltexact (l.pageno, n)
1628 loop state.layout
1630 state.mode <- LinkNav linknav
1631 | _ -> ()
1632 end;
1633 preload layout;
1635 state.ghyll <- noghyll;
1636 if conf.updatecurs
1637 then (
1638 let mx, my = state.mpos in
1639 updateunder mx my;
1643 let conttiling pageno opaque =
1644 tilepage pageno opaque
1645 (if conf.preload then preloadlayout state.layout else state.layout)
1648 let gotoy_and_clear_text y =
1649 if not conf.verbose then state.text <- "";
1650 gotoy y;
1653 let getanchor () =
1654 match state.layout with
1655 | [] -> emptyanchor
1656 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1659 let getanchory (n, top) =
1660 let y, h = getpageyh n in
1661 y + (truncate (top *. float h));
1664 let gotoanchor anchor =
1665 gotoy (getanchory anchor);
1668 let addnav () =
1669 cbput state.hists.nav (getanchor ());
1672 let getnav dir =
1673 let anchor = cbgetc state.hists.nav dir in
1674 getanchory anchor;
1677 let gotoghyll y =
1678 let rec scroll f n a b =
1679 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1680 let snake f a b =
1681 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1682 if f < a
1683 then s (float f /. float a)
1684 else (
1685 if f > b
1686 then 1.0 -. s ((float (f-b) /. float (n-b)))
1687 else 1.0
1690 snake f a b
1691 and summa f n a b =
1692 (* courtesy:
1693 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1694 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1695 let iv1 = iv f in
1696 let ins = float a *. iv1
1697 and outs = float (n-b) *. iv1 in
1698 let ones = b - a in
1699 ins +. outs +. float ones
1701 let rec set (_N, _A, _B) y sy =
1702 let sum = summa 1.0 _N _A _B in
1703 let dy = float (y - sy) in
1704 state.ghyll <- (
1705 let rec gf n y1 o =
1706 if n >= _N
1707 then state.ghyll <- noghyll
1708 else
1709 let go n =
1710 let s = scroll n _N _A _B in
1711 let y1 = y1 +. ((s *. dy) /. sum) in
1712 gotoy_and_clear_text (truncate y1);
1713 state.ghyll <- gf (n+1) y1;
1715 match o with
1716 | None -> go n
1717 | Some y' -> set (_N/2, 0, 0) y' state.y
1719 gf 0 (float state.y)
1722 match conf.ghyllscroll with
1723 | None ->
1724 gotoy_and_clear_text y
1725 | Some nab ->
1726 if state.ghyll == noghyll
1727 then set nab y state.y
1728 else state.ghyll (Some y)
1731 let gotopage n top =
1732 let y, h = getpageyh n in
1733 let y = y + (truncate (top *. float h)) in
1734 gotoghyll y
1737 let gotopage1 n top =
1738 let y = getpagey n in
1739 let y = y + top in
1740 gotoghyll y
1743 let invalidate s f =
1744 state.layout <- [];
1745 state.pdims <- [];
1746 state.rects <- [];
1747 state.rects1 <- [];
1748 match state.geomcmds with
1749 | ps, [] when String.length ps = 0 ->
1750 f ();
1751 state.geomcmds <- s, [];
1753 | ps, [] ->
1754 state.geomcmds <- ps, [s, f];
1756 | ps, (s', _) :: rest when s' = s ->
1757 state.geomcmds <- ps, ((s, f) :: rest);
1759 | ps, cmds ->
1760 state.geomcmds <- ps, ((s, f) :: cmds);
1763 let opendoc path password =
1764 state.path <- path;
1765 state.password <- password;
1766 state.gen <- state.gen + 1;
1767 state.docinfo <- [];
1769 setaalevel conf.aalevel;
1770 Wsi.settitle ("llpp " ^ Filename.basename path);
1771 wcmd "open %s\000%s\000" path password;
1772 invalidate "reqlayout"
1773 (fun () ->
1774 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1777 let scalecolor c =
1778 let c = c *. conf.colorscale in
1779 (c, c, c);
1782 let scalecolor2 (r, g, b) =
1783 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1786 let represent () =
1787 let docolumns = function
1788 | Csingle -> ()
1790 | Cmulti ((columns, coverA, coverB), _) ->
1791 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1792 let rec loop pageno pdimno pdim x y rowh pdims =
1793 if pageno = state.pagecount
1794 then ()
1795 else
1796 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1797 match pdims with
1798 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1799 pdimno+1, pdim, rest
1800 | _ ->
1801 pdimno, pdim, pdims
1803 let x, y, rowh' =
1804 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1805 then (
1806 (conf.winw - state.scrollw - w) / 2,
1807 y + rowh + conf.interpagespace, h
1809 else (
1810 if (pageno - coverA) mod columns = 0
1811 then 0, y + rowh + conf.interpagespace, h
1812 else x, y, max rowh h
1815 let rec fixrow m = if m = pageno then () else
1816 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1817 if h < rowh
1818 then (
1819 let y = y + (rowh - h) / 2 in
1820 a.(m) <- (pdimno, x, y, pdim);
1822 fixrow (m+1)
1824 if pageno > 1 && (pageno - coverA) mod columns = 0
1825 then fixrow (pageno - columns);
1826 a.(pageno) <- (pdimno, x, y, pdim);
1827 let x = x + w + xoff*2 + conf.interpagespace in
1828 loop (pageno+1) pdimno pdim x y rowh' pdims
1830 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1831 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1833 | Csplit (c, _) ->
1834 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1835 let rec loop pageno pdimno pdim y pdims =
1836 if pageno = state.pagecount
1837 then ()
1838 else
1839 let pdimno, ((_, w, h, _) as pdim), pdims =
1840 match pdims with
1841 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1842 pdimno+1, pdim, rest
1843 | _ ->
1844 pdimno, pdim, pdims
1846 let cw = w / c in
1847 let rec loop1 n x y =
1848 if n = c then y else (
1849 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1850 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1853 let y = loop1 0 0 y in
1854 loop (pageno+1) pdimno pdim y pdims
1856 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1857 conf.columns <- Csplit (c, a);
1859 docolumns conf.columns;
1860 state.maxy <- calcheight ();
1861 state.hscrollh <-
1862 if state.w <= conf.winw - state.scrollw
1863 then 0
1864 else state.scrollw
1866 match state.mode with
1867 | Birdseye (_, _, pageno, _, _) ->
1868 let y, h = getpageyh pageno in
1869 let top = (conf.winh - h) / 2 in
1870 gotoy (max 0 (y - top))
1871 | _ -> gotoanchor state.anchor
1874 let reshape w h =
1875 GlDraw.viewport 0 0 w h;
1876 if state.geomcmds != firstgeomcmds && nogeomcmds state.geomcmds
1877 then state.anchor <- getanchor ();
1879 conf.winw <- w;
1880 let w = truncate (float w *. conf.zoom) - state.scrollw in
1881 let w = max w 2 in
1882 conf.winh <- h;
1883 setfontsize fstate.fontsize;
1884 GlMat.mode `modelview;
1885 GlMat.load_identity ();
1887 GlMat.mode `projection;
1888 GlMat.load_identity ();
1889 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1890 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1891 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1893 let relx =
1894 if conf.zoom <= 1.0
1895 then 0.0
1896 else float state.x /. float state.w
1898 invalidate "geometry"
1899 (fun () ->
1900 state.w <- w;
1901 state.x <- truncate (relx *. float w);
1902 let w =
1903 match conf.columns with
1904 | Csingle -> w
1905 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1906 | Csplit (c, _) -> w * c
1908 wcmd "geometry %d %d" w h);
1911 let enttext () =
1912 let len = String.length state.text in
1913 let drawstring s =
1914 let hscrollh =
1915 match state.mode with
1916 | Textentry _
1917 | View -> state.hscrollh
1918 | _ -> 0
1920 let rect x w =
1921 GlDraw.rect
1922 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1923 (x+.w, float (conf.winh - hscrollh))
1926 let w = float (conf.winw - state.scrollw - 1) in
1927 if state.progress >= 0.0 && state.progress < 1.0
1928 then (
1929 GlDraw.color (0.3, 0.3, 0.3);
1930 let w1 = w *. state.progress in
1931 rect 0.0 w1;
1932 GlDraw.color (0.0, 0.0, 0.0);
1933 rect w1 (w-.w1)
1935 else (
1936 GlDraw.color (0.0, 0.0, 0.0);
1937 rect 0.0 w;
1940 GlDraw.color (1.0, 1.0, 1.0);
1941 drawstring fstate.fontsize
1942 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1944 let s =
1945 match state.mode with
1946 | Textentry ((prefix, text, _, _, _), _) ->
1947 let s =
1948 if len > 0
1949 then
1950 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1951 else
1952 Printf.sprintf "%s%s_" prefix text
1956 | _ -> state.text
1958 let s =
1959 if state.newerrmsgs
1960 then (
1961 if not (istextentry state.mode)
1962 then
1963 let s1 = "(press 'e' to review error messasges)" in
1964 if String.length s > 0 then s ^ " " ^ s1 else s1
1965 else s
1967 else s
1969 if String.length s > 0
1970 then drawstring s
1973 let gctiles () =
1974 let len = Queue.length state.tilelru in
1975 let rec loop qpos =
1976 if state.memused <= conf.memlimit
1977 then ()
1978 else (
1979 if qpos < len
1980 then
1981 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1982 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1983 let (_, pw, ph, _) = getpagedim n in
1985 gen = state.gen
1986 && colorspace = conf.colorspace
1987 && angle = conf.angle
1988 && pagew = pw
1989 && pageh = ph
1990 && (
1991 let layout =
1992 match state.throttle with
1993 | None ->
1994 if conf.preload
1995 then preloadlayout state.layout
1996 else state.layout
1997 | Some (layout, _, _) ->
1998 layout
2000 let x = col*conf.tilew
2001 and y = row*conf.tileh in
2002 tilevisible layout n x y
2004 then Queue.push lruitem state.tilelru
2005 else (
2006 wcmd "freetile %s" p;
2007 state.memused <- state.memused - s;
2008 state.uioh#infochanged Memused;
2009 Hashtbl.remove state.tilemap k;
2011 loop (qpos+1)
2014 loop 0
2017 let flushtiles () =
2018 Queue.iter (fun (k, p, s) ->
2019 wcmd "freetile %s" p;
2020 state.memused <- state.memused - s;
2021 state.uioh#infochanged Memused;
2022 Hashtbl.remove state.tilemap k;
2023 ) state.tilelru;
2024 Queue.clear state.tilelru;
2025 load state.layout;
2028 let logcurrently = function
2029 | Idle -> dolog "Idle"
2030 | Loading (l, gen) ->
2031 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2032 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2033 dolog
2034 "Tiling %d[%d,%d] page=%s cs=%s angle"
2035 l.pageno col row pageopaque
2036 (colorspace_to_string colorspace)
2038 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2039 angle gen conf.angle state.gen
2040 tilew tileh
2041 conf.tilew conf.tileh
2043 | Outlining _ ->
2044 dolog "outlining"
2047 let act cmds =
2048 (* dolog "%S" cmds; *)
2049 let op, args =
2050 let spacepos =
2051 try String.index cmds ' '
2052 with Not_found -> -1
2054 if spacepos = -1
2055 then cmds, ""
2056 else
2057 let l = String.length cmds in
2058 let op = String.sub cmds 0 spacepos in
2059 op, begin
2060 if l - spacepos < 2 then ""
2061 else String.sub cmds (spacepos+1) (l-spacepos-1)
2064 match op with
2065 | "clear" ->
2066 state.uioh#infochanged Pdim;
2067 state.pdims <- [];
2069 | "clearrects" ->
2070 state.rects <- state.rects1;
2071 G.postRedisplay "clearrects";
2073 | "continue" ->
2074 let n =
2075 try Scanf.sscanf args "%u" (fun n -> n)
2076 with exn ->
2077 dolog "error processing 'continue' %S: %s"
2078 cmds (Printexc.to_string exn);
2079 exit 1;
2081 state.pagecount <- n;
2082 begin match state.currently with
2083 | Outlining l ->
2084 state.currently <- Idle;
2085 state.outlines <- Array.of_list (List.rev l)
2086 | _ -> ()
2087 end;
2089 let cur, cmds = state.geomcmds in
2090 if String.length cur = 0
2091 then failwith "umpossible";
2093 begin match List.rev cmds with
2094 | [] ->
2095 state.geomcmds <- "", [];
2096 represent ();
2097 | (s, f) :: rest ->
2098 f ();
2099 state.geomcmds <- s, List.rev rest;
2100 end;
2101 if conf.maxwait = None
2102 then G.postRedisplay "continue";
2104 | "title" ->
2105 Wsi.settitle args
2107 | "msg" ->
2108 showtext ' ' args
2110 | "vmsg" ->
2111 if conf.verbose
2112 then showtext ' ' args
2114 | "progress" ->
2115 let progress, text =
2117 Scanf.sscanf args "%f %n"
2118 (fun f pos ->
2119 f, String.sub args pos (String.length args - pos))
2120 with exn ->
2121 dolog "error processing 'progress' %S: %s"
2122 cmds (Printexc.to_string exn);
2123 exit 1;
2125 state.text <- text;
2126 state.progress <- progress;
2127 G.postRedisplay "progress"
2129 | "firstmatch" ->
2130 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2132 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2133 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2134 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2135 with exn ->
2136 dolog "error processing 'firstmatch' %S: %s"
2137 cmds (Printexc.to_string exn);
2138 exit 1;
2140 let y = (getpagey pageno) + truncate y0 in
2141 addnav ();
2142 gotoy y;
2143 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2145 | "match" ->
2146 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2148 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2149 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2150 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2151 with exn ->
2152 dolog "error processing 'match' %S: %s"
2153 cmds (Printexc.to_string exn);
2154 exit 1;
2156 state.rects1 <-
2157 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2159 | "page" ->
2160 let pageopaque, t =
2162 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2163 with exn ->
2164 dolog "error processing 'page' %S: %s"
2165 cmds (Printexc.to_string exn);
2166 exit 1;
2168 begin match state.currently with
2169 | Loading (l, gen) ->
2170 vlog "page %d took %f sec" l.pageno t;
2171 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2172 begin match state.throttle with
2173 | None ->
2174 let preloadedpages =
2175 if conf.preload
2176 then preloadlayout state.layout
2177 else state.layout
2179 let evict () =
2180 let module IntSet =
2181 Set.Make (struct type t = int let compare = (-) end) in
2182 let set =
2183 List.fold_left (fun s l -> IntSet.add l.pageno s)
2184 IntSet.empty preloadedpages
2186 let evictedpages =
2187 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2188 if not (IntSet.mem pageno set)
2189 then (
2190 wcmd "freepage %s" opaque;
2191 key :: accu
2193 else accu
2194 ) state.pagemap []
2196 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2198 evict ();
2199 state.currently <- Idle;
2200 if gen = state.gen
2201 then (
2202 tilepage l.pageno pageopaque state.layout;
2203 load state.layout;
2204 load preloadedpages;
2205 if pagevisible state.layout l.pageno
2206 && layoutready state.layout
2207 then G.postRedisplay "page";
2210 | Some (layout, _, _) ->
2211 state.currently <- Idle;
2212 tilepage l.pageno pageopaque layout;
2213 load state.layout
2214 end;
2216 | _ ->
2217 dolog "Inconsistent loading state";
2218 logcurrently state.currently;
2219 exit 1
2222 | "tile" ->
2223 let (x, y, opaque, size, t) =
2225 Scanf.sscanf args "%u %u %s %u %f"
2226 (fun x y p size t -> (x, y, p, size, t))
2227 with exn ->
2228 dolog "error processing 'tile' %S: %s"
2229 cmds (Printexc.to_string exn);
2230 exit 1;
2232 begin match state.currently with
2233 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2234 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2236 if tilew != conf.tilew || tileh != conf.tileh
2237 then (
2238 wcmd "freetile %s" opaque;
2239 state.currently <- Idle;
2240 load state.layout;
2242 else (
2243 puttileopaque l col row gen cs angle opaque size t;
2244 state.memused <- state.memused + size;
2245 state.uioh#infochanged Memused;
2246 gctiles ();
2247 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2248 opaque, size) state.tilelru;
2250 let layout =
2251 match state.throttle with
2252 | None -> state.layout
2253 | Some (layout, _, _) -> layout
2256 state.currently <- Idle;
2257 if gen = state.gen
2258 && conf.colorspace = cs
2259 && conf.angle = angle
2260 && tilevisible layout l.pageno x y
2261 then conttiling l.pageno pageopaque;
2263 begin match state.throttle with
2264 | None ->
2265 preload state.layout;
2266 if gen = state.gen
2267 && conf.colorspace = cs
2268 && conf.angle = angle
2269 && tilevisible state.layout l.pageno x y
2270 then G.postRedisplay "tile nothrottle";
2272 | Some (layout, y, _) ->
2273 let ready = layoutready layout in
2274 if ready
2275 then (
2276 state.y <- y;
2277 state.layout <- layout;
2278 state.throttle <- None;
2279 G.postRedisplay "throttle";
2281 else load layout;
2282 end;
2285 | _ ->
2286 dolog "Inconsistent tiling state";
2287 logcurrently state.currently;
2288 exit 1
2291 | "pdim" ->
2292 let pdim =
2294 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2295 with exn ->
2296 dolog "error processing 'pdim' %S: %s"
2297 cmds (Printexc.to_string exn);
2298 exit 1;
2300 state.uioh#infochanged Pdim;
2301 state.pdims <- pdim :: state.pdims
2303 | "o" ->
2304 let (l, n, t, h, pos) =
2306 Scanf.sscanf args "%u %u %d %u %n"
2307 (fun l n t h pos -> l, n, t, h, pos)
2308 with exn ->
2309 dolog "error processing 'o' %S: %s"
2310 cmds (Printexc.to_string exn);
2311 exit 1;
2313 let s = String.sub args pos (String.length args - pos) in
2314 let outline = (s, l, (n, float t /. float h)) in
2315 begin match state.currently with
2316 | Outlining outlines ->
2317 state.currently <- Outlining (outline :: outlines)
2318 | Idle ->
2319 state.currently <- Outlining [outline]
2320 | currently ->
2321 dolog "invalid outlining state";
2322 logcurrently currently
2325 | "info" ->
2326 state.docinfo <- (1, args) :: state.docinfo
2328 | "infoend" ->
2329 state.uioh#infochanged Docinfo;
2330 state.docinfo <- List.rev state.docinfo
2332 | _ ->
2333 dolog "unknown cmd `%S'" cmds
2336 let onhist cb =
2337 let rc = cb.rc in
2338 let action = function
2339 | HCprev -> cbget cb ~-1
2340 | HCnext -> cbget cb 1
2341 | HCfirst -> cbget cb ~-(cb.rc)
2342 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2343 and cancel () = cb.rc <- rc
2344 in (action, cancel)
2347 let search pattern forward =
2348 if String.length pattern > 0
2349 then
2350 let pn, py =
2351 match state.layout with
2352 | [] -> 0, 0
2353 | l :: _ ->
2354 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2356 wcmd "search %d %d %d %d,%s\000"
2357 (btod conf.icase) pn py (btod forward) pattern;
2360 let intentry text key =
2361 let c =
2362 if key >= 32 && key < 127
2363 then Char.chr key
2364 else '\000'
2366 match c with
2367 | '0' .. '9' ->
2368 let text = addchar text c in
2369 TEcont text
2371 | _ ->
2372 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2373 TEcont text
2376 let textentry text key =
2377 if key land 0xff00 = 0xff00
2378 then TEcont text
2379 else TEcont (text ^ Wsi.toutf8 key)
2382 let reqlayout angle proportional =
2383 match state.throttle with
2384 | None ->
2385 if nogeomcmds state.geomcmds
2386 then state.anchor <- getanchor ();
2387 conf.angle <- angle mod 360;
2388 if conf.angle != 0
2389 then (
2390 match state.mode with
2391 | LinkNav _ -> state.mode <- View
2392 | _ -> ()
2394 conf.proportional <- proportional;
2395 invalidate "reqlayout"
2396 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2397 | _ -> ()
2400 let settrim trimmargins trimfuzz =
2401 if nogeomcmds state.geomcmds
2402 then state.anchor <- getanchor ();
2403 conf.trimmargins <- trimmargins;
2404 conf.trimfuzz <- trimfuzz;
2405 let x0, y0, x1, y1 = trimfuzz in
2406 invalidate "settrim"
2407 (fun () ->
2408 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2409 Hashtbl.iter (fun _ opaque ->
2410 wcmd "freepage %s" opaque;
2411 ) state.pagemap;
2412 Hashtbl.clear state.pagemap;
2415 let setzoom zoom =
2416 match state.throttle with
2417 | None ->
2418 let zoom = max 0.01 zoom in
2419 if zoom <> conf.zoom
2420 then (
2421 state.prevzoom <- conf.zoom;
2422 conf.zoom <- zoom;
2423 reshape conf.winw conf.winh;
2424 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2427 | Some (layout, y, started) ->
2428 let time =
2429 match conf.maxwait with
2430 | None -> 0.0
2431 | Some t -> t
2433 let dt = now () -. started in
2434 if dt > time
2435 then (
2436 state.y <- y;
2437 load layout;
2441 let setcolumns mode columns coverA coverB =
2442 if columns < 0
2443 then (
2444 if isbirdseye mode
2445 then showtext '!' "split mode doesn't work in bird's eye"
2446 else (
2447 conf.columns <- Csplit (-columns, [||]);
2448 state.x <- 0;
2449 conf.zoom <- 1.0;
2452 else (
2453 if columns < 2
2454 then (
2455 conf.columns <- Csingle;
2456 state.x <- 0;
2457 setzoom 1.0;
2459 else (
2460 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2461 conf.zoom <- 1.0;
2464 reshape conf.winw conf.winh;
2467 let enterbirdseye () =
2468 let zoom = float conf.thumbw /. float conf.winw in
2469 let birdseyepageno =
2470 let cy = conf.winh / 2 in
2471 let fold = function
2472 | [] -> 0
2473 | l :: rest ->
2474 let rec fold best = function
2475 | [] -> best.pageno
2476 | l :: rest ->
2477 let d = cy - (l.pagedispy + l.pagevh/2)
2478 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2479 if abs d < abs dbest
2480 then fold l rest
2481 else best.pageno
2482 in fold l rest
2484 fold state.layout
2486 state.mode <- Birdseye (
2487 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2489 conf.zoom <- zoom;
2490 conf.presentation <- false;
2491 conf.interpagespace <- 10;
2492 conf.hlinks <- false;
2493 state.x <- 0;
2494 state.mstate <- Mnone;
2495 conf.maxwait <- None;
2496 conf.columns <- (
2497 match conf.beyecolumns with
2498 | Some c ->
2499 conf.zoom <- 1.0;
2500 Cmulti ((c, 0, 0), [||])
2501 | None -> Csingle
2503 Wsi.setcursor Wsi.CURSOR_INHERIT;
2504 if conf.verbose
2505 then
2506 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2507 (100.0*.zoom)
2508 else
2509 state.text <- ""
2511 reshape conf.winw conf.winh;
2514 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2515 state.mode <- View;
2516 conf.zoom <- c.zoom;
2517 conf.presentation <- c.presentation;
2518 conf.interpagespace <- c.interpagespace;
2519 conf.maxwait <- c.maxwait;
2520 conf.hlinks <- c.hlinks;
2521 conf.beyecolumns <- (
2522 match conf.columns with
2523 | Cmulti ((c, _, _), _) -> Some c
2524 | Csingle -> None
2525 | Csplit _ -> assert false
2527 conf.columns <- (
2528 match c.columns with
2529 | Cmulti (c, _) -> Cmulti (c, [||])
2530 | Csingle -> Csingle
2531 | Csplit _ -> failwith "leaving bird's eye split mode"
2533 state.x <- leftx;
2534 if conf.verbose
2535 then
2536 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2537 (100.0*.conf.zoom)
2539 reshape conf.winw conf.winh;
2540 state.anchor <- if goback then anchor else (pageno, 0.0);
2543 let togglebirdseye () =
2544 match state.mode with
2545 | Birdseye vals -> leavebirdseye vals true
2546 | View -> enterbirdseye ()
2547 | _ -> ()
2550 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2551 let pageno = max 0 (pageno - incr) in
2552 let rec loop = function
2553 | [] -> gotopage1 pageno 0
2554 | l :: _ when l.pageno = pageno ->
2555 if l.pagedispy >= 0 && l.pagey = 0
2556 then G.postRedisplay "upbirdseye"
2557 else gotopage1 pageno 0
2558 | _ :: rest -> loop rest
2560 loop state.layout;
2561 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2564 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2565 let pageno = min (state.pagecount - 1) (pageno + incr) in
2566 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2567 let rec loop = function
2568 | [] ->
2569 let y, h = getpageyh pageno in
2570 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2571 gotoy (clamp dy)
2572 | l :: _ when l.pageno = pageno ->
2573 if l.pagevh != l.pageh
2574 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2575 else G.postRedisplay "downbirdseye"
2576 | _ :: rest -> loop rest
2578 loop state.layout
2581 let optentry mode _ key =
2582 let btos b = if b then "on" else "off" in
2583 if key >= 32 && key < 127
2584 then
2585 let c = Char.chr key in
2586 match c with
2587 | 's' ->
2588 let ondone s =
2589 try conf.scrollstep <- int_of_string s with exc ->
2590 state.text <- Printf.sprintf "bad integer `%s': %s"
2591 s (Printexc.to_string exc)
2593 TEswitch ("scroll step: ", "", None, intentry, ondone)
2595 | 'A' ->
2596 let ondone s =
2598 conf.autoscrollstep <- int_of_string s;
2599 if state.autoscroll <> None
2600 then state.autoscroll <- Some conf.autoscrollstep
2601 with exc ->
2602 state.text <- Printf.sprintf "bad integer `%s': %s"
2603 s (Printexc.to_string exc)
2605 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2607 | 'C' ->
2608 let mode = state.mode in
2609 let ondone s =
2611 let n, a, b = multicolumns_of_string s in
2612 setcolumns mode n a b;
2613 with exc ->
2614 state.text <- Printf.sprintf "bad columns `%s': %s"
2615 s (Printexc.to_string exc)
2617 TEswitch ("columns: ", "", None, textentry, ondone)
2619 | 'Z' ->
2620 let ondone s =
2622 let zoom = float (int_of_string s) /. 100.0 in
2623 setzoom zoom
2624 with exc ->
2625 state.text <- Printf.sprintf "bad integer `%s': %s"
2626 s (Printexc.to_string exc)
2628 TEswitch ("zoom: ", "", None, intentry, ondone)
2630 | 't' ->
2631 let ondone s =
2633 conf.thumbw <- bound (int_of_string s) 2 4096;
2634 state.text <-
2635 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2636 begin match mode with
2637 | Birdseye beye ->
2638 leavebirdseye beye false;
2639 enterbirdseye ();
2640 | _ -> ();
2642 with exc ->
2643 state.text <- Printf.sprintf "bad integer `%s': %s"
2644 s (Printexc.to_string exc)
2646 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2648 | 'R' ->
2649 let ondone s =
2650 match try
2651 Some (int_of_string s)
2652 with exc ->
2653 state.text <- Printf.sprintf "bad integer `%s': %s"
2654 s (Printexc.to_string exc);
2655 None
2656 with
2657 | Some angle -> reqlayout angle conf.proportional
2658 | None -> ()
2660 TEswitch ("rotation: ", "", None, intentry, ondone)
2662 | 'i' ->
2663 conf.icase <- not conf.icase;
2664 TEdone ("case insensitive search " ^ (btos conf.icase))
2666 | 'p' ->
2667 conf.preload <- not conf.preload;
2668 gotoy state.y;
2669 TEdone ("preload " ^ (btos conf.preload))
2671 | 'v' ->
2672 conf.verbose <- not conf.verbose;
2673 TEdone ("verbose " ^ (btos conf.verbose))
2675 | 'd' ->
2676 conf.debug <- not conf.debug;
2677 TEdone ("debug " ^ (btos conf.debug))
2679 | 'h' ->
2680 conf.maxhfit <- not conf.maxhfit;
2681 state.maxy <-
2682 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2683 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2685 | 'c' ->
2686 conf.crophack <- not conf.crophack;
2687 TEdone ("crophack " ^ btos conf.crophack)
2689 | 'a' ->
2690 let s =
2691 match conf.maxwait with
2692 | None ->
2693 conf.maxwait <- Some infinity;
2694 "always wait for page to complete"
2695 | Some _ ->
2696 conf.maxwait <- None;
2697 "show placeholder if page is not ready"
2699 TEdone s
2701 | 'f' ->
2702 conf.underinfo <- not conf.underinfo;
2703 TEdone ("underinfo " ^ btos conf.underinfo)
2705 | 'P' ->
2706 conf.savebmarks <- not conf.savebmarks;
2707 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2709 | 'S' ->
2710 let ondone s =
2712 let pageno, py =
2713 match state.layout with
2714 | [] -> 0, 0
2715 | l :: _ ->
2716 l.pageno, l.pagey
2718 conf.interpagespace <- int_of_string s;
2719 state.maxy <- calcheight ();
2720 let y = getpagey pageno in
2721 gotoy (y + py)
2722 with exc ->
2723 state.text <- Printf.sprintf "bad integer `%s': %s"
2724 s (Printexc.to_string exc)
2726 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2728 | 'l' ->
2729 reqlayout conf.angle (not conf.proportional);
2730 TEdone ("proportional display " ^ btos conf.proportional)
2732 | 'T' ->
2733 settrim (not conf.trimmargins) conf.trimfuzz;
2734 TEdone ("trim margins " ^ btos conf.trimmargins)
2736 | 'I' ->
2737 conf.invert <- not conf.invert;
2738 TEdone ("invert colors " ^ btos conf.invert)
2740 | 'x' ->
2741 let ondone s =
2742 cbput state.hists.sel s;
2743 conf.selcmd <- s;
2745 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2746 textentry, ondone)
2748 | _ ->
2749 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2750 TEstop
2751 else
2752 TEcont state.text
2755 class type lvsource = object
2756 method getitemcount : int
2757 method getitem : int -> (string * int)
2758 method hasaction : int -> bool
2759 method exit :
2760 uioh:uioh ->
2761 cancel:bool ->
2762 active:int ->
2763 first:int ->
2764 pan:int ->
2765 qsearch:string ->
2766 uioh option
2767 method getactive : int
2768 method getfirst : int
2769 method getqsearch : string
2770 method setqsearch : string -> unit
2771 method getpan : int
2772 end;;
2774 class virtual lvsourcebase = object
2775 val mutable m_active = 0
2776 val mutable m_first = 0
2777 val mutable m_qsearch = ""
2778 val mutable m_pan = 0
2779 method getactive = m_active
2780 method getfirst = m_first
2781 method getqsearch = m_qsearch
2782 method getpan = m_pan
2783 method setqsearch s = m_qsearch <- s
2784 end;;
2786 let withoutlastutf8 s =
2787 let len = String.length s in
2788 if len = 0
2789 then s
2790 else
2791 let rec find pos =
2792 if pos = 0
2793 then pos
2794 else
2795 let b = Char.code s.[pos] in
2796 if b land 0b110000 = 0b11000000
2797 then find (pos-1)
2798 else pos-1
2800 let first =
2801 if Char.code s.[len-1] land 0x80 = 0
2802 then len-1
2803 else find (len-1)
2805 String.sub s 0 first;
2808 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2809 let enttext te =
2810 state.mode <- Textentry (te, onleave);
2811 state.text <- "";
2812 enttext ();
2813 G.postRedisplay "textentrykeyboard enttext";
2815 let histaction cmd =
2816 match opthist with
2817 | None -> ()
2818 | Some (action, _) ->
2819 state.mode <- Textentry (
2820 (c, action cmd, opthist, onkey, ondone), onleave
2822 G.postRedisplay "textentry histaction"
2824 match key with
2825 | 0xff08 -> (* backspace *)
2826 let s = withoutlastutf8 text in
2827 let len = String.length s in
2828 if len = 0
2829 then (
2830 onleave Cancel;
2831 G.postRedisplay "textentrykeyboard after cancel";
2833 else (
2834 enttext (c, s, opthist, onkey, ondone)
2837 | 0xff0d ->
2838 ondone text;
2839 onleave Confirm;
2840 G.postRedisplay "textentrykeyboard after confirm"
2842 | 0xff52 -> histaction HCprev
2843 | 0xff54 -> histaction HCnext
2844 | 0xff50 -> histaction HCfirst
2845 | 0xff57 -> histaction HClast
2847 | 0xff1b -> (* escape*)
2848 if String.length text = 0
2849 then (
2850 begin match opthist with
2851 | None -> ()
2852 | Some (_, onhistcancel) -> onhistcancel ()
2853 end;
2854 onleave Cancel;
2855 state.text <- "";
2856 G.postRedisplay "textentrykeyboard after cancel2"
2858 else (
2859 enttext (c, "", opthist, onkey, ondone)
2862 | 0xff9f | 0xffff -> () (* delete *)
2864 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2865 begin match onkey text key with
2866 | TEdone text ->
2867 ondone text;
2868 onleave Confirm;
2869 G.postRedisplay "textentrykeyboard after confirm2";
2871 | TEcont text ->
2872 enttext (c, text, opthist, onkey, ondone);
2874 | TEstop ->
2875 onleave Cancel;
2876 G.postRedisplay "textentrykeyboard after cancel3"
2878 | TEswitch te ->
2879 state.mode <- Textentry (te, onleave);
2880 G.postRedisplay "textentrykeyboard switch";
2881 end;
2883 | _ ->
2884 vlog "unhandled key %s" (Wsi.keyname key)
2887 let firstof first active =
2888 if first > active || abs (first - active) > fstate.maxrows - 1
2889 then max 0 (active - (fstate.maxrows/2))
2890 else first
2893 let calcfirst first active =
2894 if active > first
2895 then
2896 let rows = active - first in
2897 if rows > fstate.maxrows then active - fstate.maxrows else first
2898 else active
2901 let scrollph y maxy =
2902 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2903 let sh = float conf.winh /. sh in
2904 let sh = max sh (float conf.scrollh) in
2906 let percent =
2907 if y = state.maxy
2908 then 1.0
2909 else float y /. float maxy
2911 let position = (float conf.winh -. sh) *. percent in
2913 let position =
2914 if position +. sh > float conf.winh
2915 then float conf.winh -. sh
2916 else position
2918 position, sh;
2921 let coe s = (s :> uioh);;
2923 class listview ~(source:lvsource) ~trusted ~modehash =
2924 object (self)
2925 val m_pan = source#getpan
2926 val m_first = source#getfirst
2927 val m_active = source#getactive
2928 val m_qsearch = source#getqsearch
2929 val m_prev_uioh = state.uioh
2931 method private elemunder y =
2932 let n = y / (fstate.fontsize+1) in
2933 if m_first + n < source#getitemcount
2934 then (
2935 if source#hasaction (m_first + n)
2936 then Some (m_first + n)
2937 else None
2939 else None
2941 method display =
2942 Gl.enable `blend;
2943 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2944 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2945 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2946 GlDraw.color (1., 1., 1.);
2947 Gl.enable `texture_2d;
2948 let fs = fstate.fontsize in
2949 let nfs = fs + 1 in
2950 let ww = fstate.wwidth in
2951 let tabw = 30.0*.ww in
2952 let itemcount = source#getitemcount in
2953 let rec loop row =
2954 if (row - m_first) * nfs > conf.winh
2955 then ()
2956 else (
2957 if row >= 0 && row < itemcount
2958 then (
2959 let (s, level) = source#getitem row in
2960 let y = (row - m_first) * nfs in
2961 let x = 5.0 +. float (level + m_pan) *. ww in
2962 if row = m_active
2963 then (
2964 Gl.disable `texture_2d;
2965 GlDraw.polygon_mode `both `line;
2966 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2967 GlDraw.rect (1., float (y + 1))
2968 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2969 GlDraw.polygon_mode `both `fill;
2970 GlDraw.color (1., 1., 1.);
2971 Gl.enable `texture_2d;
2974 let drawtabularstring s =
2975 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2976 if trusted
2977 then
2978 let tabpos = try String.index s '\t' with Not_found -> -1 in
2979 if tabpos > 0
2980 then
2981 let len = String.length s - tabpos - 1 in
2982 let s1 = String.sub s 0 tabpos
2983 and s2 = String.sub s (tabpos + 1) len in
2984 let nx = drawstr x s1 in
2985 let sw = nx -. x in
2986 let x = x +. (max tabw sw) in
2987 drawstr x s2
2988 else
2989 drawstr x s
2990 else
2991 drawstr x s
2993 let _ = drawtabularstring s in
2994 loop (row+1)
2998 loop m_first;
2999 Gl.disable `blend;
3000 Gl.disable `texture_2d;
3002 method updownlevel incr =
3003 let len = source#getitemcount in
3004 let curlevel =
3005 if m_active >= 0 && m_active < len
3006 then snd (source#getitem m_active)
3007 else -1
3009 let rec flow i =
3010 if i = len then i-1 else if i = -1 then 0 else
3011 let _, l = source#getitem i in
3012 if l != curlevel then i else flow (i+incr)
3014 let active = flow m_active in
3015 let first = calcfirst m_first active in
3016 G.postRedisplay "outline updownlevel";
3017 {< m_active = active; m_first = first >}
3019 method private key1 key mask =
3020 let set1 active first qsearch =
3021 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3023 let search active pattern incr =
3024 let dosearch re =
3025 let rec loop n =
3026 if n >= 0 && n < source#getitemcount
3027 then (
3028 let s, _ = source#getitem n in
3030 (try ignore (Str.search_forward re s 0); true
3031 with Not_found -> false)
3032 then Some n
3033 else loop (n + incr)
3035 else None
3037 loop active
3040 let re = Str.regexp_case_fold pattern in
3041 dosearch re
3042 with Failure s ->
3043 state.text <- s;
3044 None
3046 let itemcount = source#getitemcount in
3047 let find start incr =
3048 let rec find i =
3049 if i = -1 || i = itemcount
3050 then -1
3051 else (
3052 if source#hasaction i
3053 then i
3054 else find (i + incr)
3057 find start
3059 let set active first =
3060 let first = bound first 0 (itemcount - fstate.maxrows) in
3061 state.text <- "";
3062 coe {< m_active = active; m_first = first >}
3064 let navigate incr =
3065 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3066 let active, first =
3067 let incr1 = if incr > 0 then 1 else -1 in
3068 if isvisible m_first m_active
3069 then
3070 let next =
3071 let next = m_active + incr in
3072 let next =
3073 if next < 0 || next >= itemcount
3074 then -1
3075 else find next incr1
3077 if next = -1 || abs (m_active - next) > fstate.maxrows
3078 then -1
3079 else next
3081 if next = -1
3082 then
3083 let first = m_first + incr in
3084 let first = bound first 0 (itemcount - 1) in
3085 let next =
3086 let next = m_active + incr in
3087 let next = bound next 0 (itemcount - 1) in
3088 find next ~-incr1
3090 let active = if next = -1 then m_active else next in
3091 active, first
3092 else
3093 let first = min next m_first in
3094 let first =
3095 if abs (next - first) > fstate.maxrows
3096 then first + incr
3097 else first
3099 next, first
3100 else
3101 let first = m_first + incr in
3102 let first = bound first 0 (itemcount - 1) in
3103 let active =
3104 let next = m_active + incr in
3105 let next = bound next 0 (itemcount - 1) in
3106 let next = find next incr1 in
3107 let active =
3108 if next = -1 || abs (m_active - first) > fstate.maxrows
3109 then (
3110 let active = if m_active = -1 then next else m_active in
3111 active
3113 else next
3115 if isvisible first active
3116 then active
3117 else -1
3119 active, first
3121 G.postRedisplay "listview navigate";
3122 set active first;
3124 match key with
3125 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3126 let incr = if key = 0x72 then -1 else 1 in
3127 let active, first =
3128 match search (m_active + incr) m_qsearch incr with
3129 | None ->
3130 state.text <- m_qsearch ^ " [not found]";
3131 m_active, m_first
3132 | Some active ->
3133 state.text <- m_qsearch;
3134 active, firstof m_first active
3136 G.postRedisplay "listview ctrl-r/s";
3137 set1 active first m_qsearch;
3139 | 0xff08 -> (* backspace *)
3140 if String.length m_qsearch = 0
3141 then coe self
3142 else (
3143 let qsearch = withoutlastutf8 m_qsearch in
3144 let len = String.length qsearch in
3145 if len = 0
3146 then (
3147 state.text <- "";
3148 G.postRedisplay "listview empty qsearch";
3149 set1 m_active m_first "";
3151 else
3152 let active, first =
3153 match search m_active qsearch ~-1 with
3154 | None ->
3155 state.text <- qsearch ^ " [not found]";
3156 m_active, m_first
3157 | Some active ->
3158 state.text <- qsearch;
3159 active, firstof m_first active
3161 G.postRedisplay "listview backspace qsearch";
3162 set1 active first qsearch
3165 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3166 let pattern = m_qsearch ^ Wsi.toutf8 key in
3167 let active, first =
3168 match search m_active pattern 1 with
3169 | None ->
3170 state.text <- pattern ^ " [not found]";
3171 m_active, m_first
3172 | Some active ->
3173 state.text <- pattern;
3174 active, firstof m_first active
3176 G.postRedisplay "listview qsearch add";
3177 set1 active first pattern;
3179 | 0xff1b -> (* escape *)
3180 state.text <- "";
3181 if String.length m_qsearch = 0
3182 then (
3183 G.postRedisplay "list view escape";
3184 begin
3185 match
3186 source#exit (coe self) true m_active m_first m_pan m_qsearch
3187 with
3188 | None -> m_prev_uioh
3189 | Some uioh -> uioh
3192 else (
3193 G.postRedisplay "list view kill qsearch";
3194 source#setqsearch "";
3195 coe {< m_qsearch = "" >}
3198 | 0xff0d -> (* return *)
3199 state.text <- "";
3200 let self = {< m_qsearch = "" >} in
3201 source#setqsearch "";
3202 let opt =
3203 G.postRedisplay "listview enter";
3204 if m_active >= 0 && m_active < source#getitemcount
3205 then (
3206 source#exit (coe self) false m_active m_first m_pan "";
3208 else (
3209 source#exit (coe self) true m_active m_first m_pan "";
3212 begin match opt with
3213 | None -> m_prev_uioh
3214 | Some uioh -> uioh
3217 | 0xff9f | 0xffff -> (* delete *)
3218 coe self
3220 | 0xff52 -> navigate ~-1 (* up *)
3221 | 0xff54 -> navigate 1 (* down *)
3222 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3223 | 0xff56 -> navigate fstate.maxrows (* next *)
3225 | 0xff53 -> (* right *)
3226 state.text <- "";
3227 G.postRedisplay "listview right";
3228 coe {< m_pan = m_pan - 1 >}
3230 | 0xff51 -> (* left *)
3231 state.text <- "";
3232 G.postRedisplay "listview left";
3233 coe {< m_pan = m_pan + 1 >}
3235 | 0xff50 -> (* home *)
3236 let active = find 0 1 in
3237 G.postRedisplay "listview home";
3238 set active 0;
3240 | 0xff57 -> (* end *)
3241 let first = max 0 (itemcount - fstate.maxrows) in
3242 let active = find (itemcount - 1) ~-1 in
3243 G.postRedisplay "listview end";
3244 set active first;
3246 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3247 coe self
3249 | _ ->
3250 dolog "listview unknown key %#x" key; coe self
3252 method key key mask =
3253 match state.mode with
3254 | Textentry te -> textentrykeyboard key mask te; coe self
3255 | _ -> self#key1 key mask
3257 method button button down x y _ =
3258 let opt =
3259 match button with
3260 | 1 when x > conf.winw - conf.scrollbw ->
3261 G.postRedisplay "listview scroll";
3262 if down
3263 then
3264 let _, position, sh = self#scrollph in
3265 if y > truncate position && y < truncate (position +. sh)
3266 then (
3267 state.mstate <- Mscrolly;
3268 Some (coe self)
3270 else
3271 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3272 let first = truncate (s *. float source#getitemcount) in
3273 let first = min source#getitemcount first in
3274 Some (coe {< m_first = first; m_active = first >})
3275 else (
3276 state.mstate <- Mnone;
3277 Some (coe self);
3279 | 1 when not down ->
3280 begin match self#elemunder y with
3281 | Some n ->
3282 G.postRedisplay "listview click";
3283 source#exit
3284 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3285 | _ ->
3286 Some (coe self)
3288 | n when (n == 4 || n == 5) && not down ->
3289 let len = source#getitemcount in
3290 let first =
3291 if n = 5 && m_first + fstate.maxrows >= len
3292 then
3293 m_first
3294 else
3295 let first = m_first + (if n == 4 then -1 else 1) in
3296 bound first 0 (len - 1)
3298 G.postRedisplay "listview wheel";
3299 Some (coe {< m_first = first >})
3300 | _ ->
3301 Some (coe self)
3303 match opt with
3304 | None -> m_prev_uioh
3305 | Some uioh -> uioh
3307 method motion _ y =
3308 match state.mstate with
3309 | Mscrolly ->
3310 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3311 let first = truncate (s *. float source#getitemcount) in
3312 let first = min source#getitemcount first in
3313 G.postRedisplay "listview motion";
3314 coe {< m_first = first; m_active = first >}
3315 | _ -> coe self
3317 method pmotion x y =
3318 if x < conf.winw - conf.scrollbw
3319 then
3320 let n =
3321 match self#elemunder y with
3322 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3323 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3325 let o =
3326 if n != m_active
3327 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3328 else self
3330 coe o
3331 else (
3332 Wsi.setcursor Wsi.CURSOR_INHERIT;
3333 coe self
3336 method infochanged _ = ()
3338 method scrollpw = (0, 0.0, 0.0)
3339 method scrollph =
3340 let nfs = fstate.fontsize + 1 in
3341 let y = m_first * nfs in
3342 let itemcount = source#getitemcount in
3343 let maxi = max 0 (itemcount - fstate.maxrows) in
3344 let maxy = maxi * nfs in
3345 let p, h = scrollph y maxy in
3346 conf.scrollbw, p, h
3348 method modehash = modehash
3349 end;;
3351 class outlinelistview ~source =
3352 object (self)
3353 inherit listview
3354 ~source:(source :> lvsource)
3355 ~trusted:false
3356 ~modehash:(findkeyhash conf "outline")
3357 as super
3359 method key key mask =
3360 let calcfirst first active =
3361 if active > first
3362 then
3363 let rows = active - first in
3364 if rows > fstate.maxrows then active - fstate.maxrows else first
3365 else active
3367 let navigate incr =
3368 let active = m_active + incr in
3369 let active = bound active 0 (source#getitemcount - 1) in
3370 let first = calcfirst m_first active in
3371 G.postRedisplay "outline navigate";
3372 coe {< m_active = active; m_first = first >}
3374 let ctrl = Wsi.withctrl mask in
3375 match key with
3376 | 110 when ctrl -> (* ctrl-n *)
3377 source#narrow m_qsearch;
3378 G.postRedisplay "outline ctrl-n";
3379 coe {< m_first = 0; m_active = 0 >}
3381 | 117 when ctrl -> (* ctrl-u *)
3382 source#denarrow;
3383 G.postRedisplay "outline ctrl-u";
3384 state.text <- "";
3385 coe {< m_first = 0; m_active = 0 >}
3387 | 108 when ctrl -> (* ctrl-l *)
3388 let first = m_active - (fstate.maxrows / 2) in
3389 G.postRedisplay "outline ctrl-l";
3390 coe {< m_first = first >}
3392 | 0xff9f | 0xffff -> (* delete *)
3393 source#remove m_active;
3394 G.postRedisplay "outline delete";
3395 let active = max 0 (m_active-1) in
3396 coe {< m_first = firstof m_first active;
3397 m_active = active >}
3399 | 0xff52 -> navigate ~-1 (* up *)
3400 | 0xff54 -> navigate 1 (* down *)
3401 | 0xff55 -> (* prior *)
3402 navigate ~-(fstate.maxrows)
3403 | 0xff56 -> (* next *)
3404 navigate fstate.maxrows
3406 | 0xff53 -> (* [ctrl-]right *)
3407 let o =
3408 if ctrl
3409 then (
3410 G.postRedisplay "outline ctrl right";
3411 {< m_pan = m_pan + 1 >}
3413 else self#updownlevel 1
3415 coe o
3417 | 0xff51 -> (* [ctrl-]left *)
3418 let o =
3419 if ctrl
3420 then (
3421 G.postRedisplay "outline ctrl left";
3422 {< m_pan = m_pan - 1 >}
3424 else self#updownlevel ~-1
3426 coe o
3428 | 0xff50 -> (* home *)
3429 G.postRedisplay "outline home";
3430 coe {< m_first = 0; m_active = 0 >}
3432 | 0xff57 -> (* end *)
3433 let active = source#getitemcount - 1 in
3434 let first = max 0 (active - fstate.maxrows) in
3435 G.postRedisplay "outline end";
3436 coe {< m_active = active; m_first = first >}
3438 | _ -> super#key key mask
3441 let outlinesource usebookmarks =
3442 let empty = [||] in
3443 (object
3444 inherit lvsourcebase
3445 val mutable m_items = empty
3446 val mutable m_orig_items = empty
3447 val mutable m_prev_items = empty
3448 val mutable m_narrow_pattern = ""
3449 val mutable m_hadremovals = false
3451 method getitemcount =
3452 Array.length m_items + (if m_hadremovals then 1 else 0)
3454 method getitem n =
3455 if n == Array.length m_items && m_hadremovals
3456 then
3457 ("[Confirm removal]", 0)
3458 else
3459 let s, n, _ = m_items.(n) in
3460 (s, n)
3462 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3463 ignore (uioh, first, qsearch);
3464 let confrimremoval = m_hadremovals && active = Array.length m_items in
3465 let items =
3466 if String.length m_narrow_pattern = 0
3467 then m_orig_items
3468 else m_items
3470 if not cancel
3471 then (
3472 if not confrimremoval
3473 then(
3474 let _, _, anchor = m_items.(active) in
3475 gotoanchor anchor;
3476 m_items <- items;
3478 else (
3479 state.bookmarks <- Array.to_list m_items;
3480 m_orig_items <- m_items;
3483 else m_items <- items;
3484 m_pan <- pan;
3485 None
3487 method hasaction _ = true
3489 method greetmsg =
3490 if Array.length m_items != Array.length m_orig_items
3491 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3492 else ""
3494 method narrow pattern =
3495 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3496 match reopt with
3497 | None -> ()
3498 | Some re ->
3499 let rec loop accu n =
3500 if n = -1
3501 then (
3502 m_narrow_pattern <- pattern;
3503 m_items <- Array.of_list accu
3505 else
3506 let (s, _, _) as o = m_items.(n) in
3507 let accu =
3508 if (try ignore (Str.search_forward re s 0); true
3509 with Not_found -> false)
3510 then o :: accu
3511 else accu
3513 loop accu (n-1)
3515 loop [] (Array.length m_items - 1)
3517 method denarrow =
3518 m_orig_items <- (
3519 if usebookmarks
3520 then Array.of_list state.bookmarks
3521 else state.outlines
3523 m_items <- m_orig_items
3525 method remove m =
3526 if usebookmarks
3527 then
3528 if m >= 0 && m < Array.length m_items
3529 then (
3530 m_hadremovals <- true;
3531 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3532 let n = if n >= m then n+1 else n in
3533 m_items.(n)
3537 method reset anchor items =
3538 m_hadremovals <- false;
3539 if m_orig_items == empty || m_prev_items != items
3540 then (
3541 m_orig_items <- items;
3542 if String.length m_narrow_pattern = 0
3543 then m_items <- items;
3545 m_prev_items <- items;
3546 let rely = getanchory anchor in
3547 let active =
3548 let rec loop n best bestd =
3549 if n = Array.length m_items
3550 then best
3551 else
3552 let (_, _, anchor) = m_items.(n) in
3553 let orely = getanchory anchor in
3554 let d = abs (orely - rely) in
3555 if d < bestd
3556 then loop (n+1) n d
3557 else loop (n+1) best bestd
3559 loop 0 ~-1 max_int
3561 m_active <- active;
3562 m_first <- firstof m_first active
3563 end)
3566 let enterselector usebookmarks =
3567 let source = outlinesource usebookmarks in
3568 fun errmsg ->
3569 let outlines =
3570 if usebookmarks
3571 then Array.of_list state.bookmarks
3572 else state.outlines
3574 if Array.length outlines = 0
3575 then (
3576 showtext ' ' errmsg;
3578 else (
3579 state.text <- source#greetmsg;
3580 Wsi.setcursor Wsi.CURSOR_INHERIT;
3581 let anchor = getanchor () in
3582 source#reset anchor outlines;
3583 state.uioh <- coe (new outlinelistview ~source);
3584 G.postRedisplay "enter selector";
3588 let enteroutlinemode =
3589 let f = enterselector false in
3590 fun ()-> f "Document has no outline";
3593 let enterbookmarkmode =
3594 let f = enterselector true in
3595 fun () -> f "Document has no bookmarks (yet)";
3598 let color_of_string s =
3599 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3600 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3604 let color_to_string (r, g, b) =
3605 let r = truncate (r *. 256.0)
3606 and g = truncate (g *. 256.0)
3607 and b = truncate (b *. 256.0) in
3608 Printf.sprintf "%d/%d/%d" r g b
3611 let irect_of_string s =
3612 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3615 let irect_to_string (x0,y0,x1,y1) =
3616 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3619 let makecheckers () =
3620 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3621 following to say:
3622 converted by Issac Trotts. July 25, 2002 *)
3623 let image_height = 64
3624 and image_width = 64 in
3626 let make_image () =
3627 let image =
3628 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3630 for i = 0 to image_width - 1 do
3631 for j = 0 to image_height - 1 do
3632 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3633 (if (i land 8 ) lxor (j land 8) = 0
3634 then [|255;255;255|] else [|200;200;200|])
3635 done
3636 done;
3637 image
3639 let image = make_image () in
3640 let id = GlTex.gen_texture () in
3641 GlTex.bind_texture `texture_2d id;
3642 GlPix.store (`unpack_alignment 1);
3643 GlTex.image2d image;
3644 List.iter (GlTex.parameter ~target:`texture_2d)
3645 [ `wrap_s `repeat;
3646 `wrap_t `repeat;
3647 `mag_filter `nearest;
3648 `min_filter `nearest ];
3652 let setcheckers enabled =
3653 match state.texid with
3654 | None ->
3655 if enabled then state.texid <- Some (makecheckers ())
3657 | Some texid ->
3658 if not enabled
3659 then (
3660 GlTex.delete_texture texid;
3661 state.texid <- None;
3665 let int_of_string_with_suffix s =
3666 let l = String.length s in
3667 let s1, shift =
3668 if l > 1
3669 then
3670 let suffix = Char.lowercase s.[l-1] in
3671 match suffix with
3672 | 'k' -> String.sub s 0 (l-1), 10
3673 | 'm' -> String.sub s 0 (l-1), 20
3674 | 'g' -> String.sub s 0 (l-1), 30
3675 | _ -> s, 0
3676 else s, 0
3678 let n = int_of_string s1 in
3679 let m = n lsl shift in
3680 if m < 0 || m < n
3681 then raise (Failure "value too large")
3682 else m
3685 let string_with_suffix_of_int n =
3686 if n = 0
3687 then "0"
3688 else
3689 let n, s =
3690 if n = 0
3691 then 0, ""
3692 else (
3693 if n land ((1 lsl 20) - 1) = 0
3694 then n lsr 20, "M"
3695 else (
3696 if n land ((1 lsl 10) - 1) = 0
3697 then n lsr 10, "K"
3698 else n, ""
3702 let rec loop s n =
3703 let h = n mod 1000 in
3704 let n = n / 1000 in
3705 if n = 0
3706 then string_of_int h ^ s
3707 else (
3708 let s = Printf.sprintf "_%03d%s" h s in
3709 loop s n
3712 loop "" n ^ s;
3715 let defghyllscroll = (40, 8, 32);;
3716 let ghyllscroll_of_string s =
3717 let (n, a, b) as nab =
3718 if s = "default"
3719 then defghyllscroll
3720 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3722 if n <= a || n <= b || a >= b
3723 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3724 nab;
3727 let ghyllscroll_to_string ((n, a, b) as nab) =
3728 if nab = defghyllscroll
3729 then "default"
3730 else Printf.sprintf "%d,%d,%d" n a b;
3733 let describe_location () =
3734 let f (fn, _) l =
3735 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3737 let fn, ln = List.fold_left f (-1, -1) state.layout in
3738 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3739 let percent =
3740 if maxy <= 0
3741 then 100.
3742 else (100. *. (float state.y /. float maxy))
3744 if fn = ln
3745 then
3746 Printf.sprintf "page %d of %d [%.2f%%]"
3747 (fn+1) state.pagecount percent
3748 else
3749 Printf.sprintf
3750 "pages %d-%d of %d [%.2f%%]"
3751 (fn+1) (ln+1) state.pagecount percent
3754 let enterinfomode =
3755 let btos b = if b then "\xe2\x88\x9a" else "" in
3756 let showextended = ref false in
3757 let leave mode = function
3758 | Confirm -> state.mode <- mode
3759 | Cancel -> state.mode <- mode in
3760 let src =
3761 (object
3762 val mutable m_first_time = true
3763 val mutable m_l = []
3764 val mutable m_a = [||]
3765 val mutable m_prev_uioh = nouioh
3766 val mutable m_prev_mode = View
3768 inherit lvsourcebase
3770 method reset prev_mode prev_uioh =
3771 m_a <- Array.of_list (List.rev m_l);
3772 m_l <- [];
3773 m_prev_mode <- prev_mode;
3774 m_prev_uioh <- prev_uioh;
3775 if m_first_time
3776 then (
3777 let rec loop n =
3778 if n >= Array.length m_a
3779 then ()
3780 else
3781 match m_a.(n) with
3782 | _, _, _, Action _ -> m_active <- n
3783 | _ -> loop (n+1)
3785 loop 0;
3786 m_first_time <- false;
3789 method int name get set =
3790 m_l <-
3791 (name, `int get, 1, Action (
3792 fun u ->
3793 let ondone s =
3794 try set (int_of_string s)
3795 with exn ->
3796 state.text <- Printf.sprintf "bad integer `%s': %s"
3797 s (Printexc.to_string exn)
3799 state.text <- "";
3800 let te = name ^ ": ", "", None, intentry, ondone in
3801 state.mode <- Textentry (te, leave m_prev_mode);
3803 )) :: m_l
3805 method int_with_suffix name get set =
3806 m_l <-
3807 (name, `intws get, 1, Action (
3808 fun u ->
3809 let ondone s =
3810 try set (int_of_string_with_suffix s)
3811 with exn ->
3812 state.text <- Printf.sprintf "bad integer `%s': %s"
3813 s (Printexc.to_string exn)
3815 state.text <- "";
3816 let te =
3817 name ^ ": ", "", None, intentry_with_suffix, ondone
3819 state.mode <- Textentry (te, leave m_prev_mode);
3821 )) :: m_l
3823 method bool ?(offset=1) ?(btos=btos) name get set =
3824 m_l <-
3825 (name, `bool (btos, get), offset, Action (
3826 fun u ->
3827 let v = get () in
3828 set (not v);
3830 )) :: m_l
3832 method color name get set =
3833 m_l <-
3834 (name, `color get, 1, Action (
3835 fun u ->
3836 let invalid = (nan, nan, nan) in
3837 let ondone s =
3838 let c =
3839 try color_of_string s
3840 with exn ->
3841 state.text <- Printf.sprintf "bad color `%s': %s"
3842 s (Printexc.to_string exn);
3843 invalid
3845 if c <> invalid
3846 then set c;
3848 let te = name ^ ": ", "", None, textentry, ondone in
3849 state.text <- color_to_string (get ());
3850 state.mode <- Textentry (te, leave m_prev_mode);
3852 )) :: m_l
3854 method string name get set =
3855 m_l <-
3856 (name, `string get, 1, Action (
3857 fun u ->
3858 let ondone s = set s in
3859 let te = name ^ ": ", "", None, textentry, ondone in
3860 state.mode <- Textentry (te, leave m_prev_mode);
3862 )) :: m_l
3864 method colorspace name get set =
3865 m_l <-
3866 (name, `string get, 1, Action (
3867 fun _ ->
3868 let source =
3869 let vals = [| "rgb"; "bgr"; "gray" |] in
3870 (object
3871 inherit lvsourcebase
3873 initializer
3874 m_active <- int_of_colorspace conf.colorspace;
3875 m_first <- 0;
3877 method getitemcount = Array.length vals
3878 method getitem n = (vals.(n), 0)
3879 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3880 ignore (uioh, first, pan, qsearch);
3881 if not cancel then set active;
3882 None
3883 method hasaction _ = true
3884 end)
3886 state.text <- "";
3887 let modehash = findkeyhash conf "info" in
3888 coe (new listview ~source ~trusted:true ~modehash)
3889 )) :: m_l
3891 method caption s offset =
3892 m_l <- (s, `empty, offset, Noaction) :: m_l
3894 method caption2 s f offset =
3895 m_l <- (s, `string f, offset, Noaction) :: m_l
3897 method getitemcount = Array.length m_a
3899 method getitem n =
3900 let tostr = function
3901 | `int f -> string_of_int (f ())
3902 | `intws f -> string_with_suffix_of_int (f ())
3903 | `string f -> f ()
3904 | `color f -> color_to_string (f ())
3905 | `bool (btos, f) -> btos (f ())
3906 | `empty -> ""
3908 let name, t, offset, _ = m_a.(n) in
3909 ((let s = tostr t in
3910 if String.length s > 0
3911 then Printf.sprintf "%s\t%s" name s
3912 else name),
3913 offset)
3915 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3916 let uiohopt =
3917 if not cancel
3918 then (
3919 m_qsearch <- qsearch;
3920 let uioh =
3921 match m_a.(active) with
3922 | _, _, _, Action f -> f uioh
3923 | _ -> uioh
3925 Some uioh
3927 else None
3929 m_active <- active;
3930 m_first <- first;
3931 m_pan <- pan;
3932 uiohopt
3934 method hasaction n =
3935 match m_a.(n) with
3936 | _, _, _, Action _ -> true
3937 | _ -> false
3938 end)
3940 let rec fillsrc prevmode prevuioh =
3941 let sep () = src#caption "" 0 in
3942 let colorp name get set =
3943 src#string name
3944 (fun () -> color_to_string (get ()))
3945 (fun v ->
3947 let c = color_of_string v in
3948 set c
3949 with exn ->
3950 state.text <- Printf.sprintf "bad color `%s': %s"
3951 v (Printexc.to_string exn);
3954 let oldmode = state.mode in
3955 let birdseye = isbirdseye state.mode in
3957 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3959 src#bool "presentation mode"
3960 (fun () -> conf.presentation)
3961 (fun v ->
3962 conf.presentation <- v;
3963 state.anchor <- getanchor ();
3964 represent ());
3966 src#bool "ignore case in searches"
3967 (fun () -> conf.icase)
3968 (fun v -> conf.icase <- v);
3970 src#bool "preload"
3971 (fun () -> conf.preload)
3972 (fun v -> conf.preload <- v);
3974 src#bool "highlight links"
3975 (fun () -> conf.hlinks)
3976 (fun v -> conf.hlinks <- v);
3978 src#bool "under info"
3979 (fun () -> conf.underinfo)
3980 (fun v -> conf.underinfo <- v);
3982 src#bool "persistent bookmarks"
3983 (fun () -> conf.savebmarks)
3984 (fun v -> conf.savebmarks <- v);
3986 src#bool "proportional display"
3987 (fun () -> conf.proportional)
3988 (fun v -> reqlayout conf.angle v);
3990 src#bool "trim margins"
3991 (fun () -> conf.trimmargins)
3992 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3994 src#bool "persistent location"
3995 (fun () -> conf.jumpback)
3996 (fun v -> conf.jumpback <- v);
3998 sep ();
3999 src#int "inter-page space"
4000 (fun () -> conf.interpagespace)
4001 (fun n ->
4002 conf.interpagespace <- n;
4003 let pageno, py =
4004 match state.layout with
4005 | [] -> 0, 0
4006 | l :: _ ->
4007 l.pageno, l.pagey
4009 state.maxy <- calcheight ();
4010 let y = getpagey pageno in
4011 gotoy (y + py)
4014 src#int "page bias"
4015 (fun () -> conf.pagebias)
4016 (fun v -> conf.pagebias <- v);
4018 src#int "scroll step"
4019 (fun () -> conf.scrollstep)
4020 (fun n -> conf.scrollstep <- n);
4022 src#int "auto scroll step"
4023 (fun () ->
4024 match state.autoscroll with
4025 | Some step -> step
4026 | _ -> conf.autoscrollstep)
4027 (fun n ->
4028 if state.autoscroll <> None
4029 then state.autoscroll <- Some n;
4030 conf.autoscrollstep <- n);
4032 src#int "zoom"
4033 (fun () -> truncate (conf.zoom *. 100.))
4034 (fun v -> setzoom ((float v) /. 100.));
4036 src#int "rotation"
4037 (fun () -> conf.angle)
4038 (fun v -> reqlayout v conf.proportional);
4040 src#int "scroll bar width"
4041 (fun () -> state.scrollw)
4042 (fun v ->
4043 state.scrollw <- v;
4044 conf.scrollbw <- v;
4045 reshape conf.winw conf.winh;
4048 src#int "scroll handle height"
4049 (fun () -> conf.scrollh)
4050 (fun v -> conf.scrollh <- v;);
4052 src#int "thumbnail width"
4053 (fun () -> conf.thumbw)
4054 (fun v ->
4055 conf.thumbw <- min 4096 v;
4056 match oldmode with
4057 | Birdseye beye ->
4058 leavebirdseye beye false;
4059 enterbirdseye ()
4060 | _ -> ()
4063 let mode = state.mode in
4064 src#string "columns"
4065 (fun () ->
4066 match conf.columns with
4067 | Csingle -> "1"
4068 | Cmulti (multi, _) -> multicolumns_to_string multi
4069 | Csplit (count, _) -> string_of_int count
4071 (fun v ->
4072 let n, a, b = multicolumns_of_string v in
4073 setcolumns mode n a b);
4075 sep ();
4076 src#caption "Presentation mode" 0;
4077 src#bool "scrollbar visible"
4078 (fun () -> conf.scrollbarinpm)
4079 (fun v ->
4080 if v != conf.scrollbarinpm
4081 then (
4082 conf.scrollbarinpm <- v;
4083 if conf.presentation
4084 then (
4085 state.scrollw <- if v then conf.scrollbw else 0;
4086 reshape conf.winw conf.winh;
4091 sep ();
4092 src#caption "Pixmap cache" 0;
4093 src#int_with_suffix "size (advisory)"
4094 (fun () -> conf.memlimit)
4095 (fun v -> conf.memlimit <- v);
4097 src#caption2 "used"
4098 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4099 (string_with_suffix_of_int state.memused)
4100 (Hashtbl.length state.tilemap)) 1;
4102 sep ();
4103 src#caption "Layout" 0;
4104 src#caption2 "Dimension"
4105 (fun () ->
4106 Printf.sprintf "%dx%d (virtual %dx%d)"
4107 conf.winw conf.winh
4108 state.w state.maxy)
4110 if conf.debug
4111 then
4112 src#caption2 "Position" (fun () ->
4113 Printf.sprintf "%dx%d" state.x state.y
4115 else
4116 src#caption2 "Visible" (fun () -> describe_location ()) 1
4119 sep ();
4120 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4121 "Save these parameters as global defaults at exit"
4122 (fun () -> conf.bedefault)
4123 (fun v -> conf.bedefault <- v)
4126 sep ();
4127 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4128 src#bool ~offset:0 ~btos "Extended parameters"
4129 (fun () -> !showextended)
4130 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4131 if !showextended
4132 then (
4133 src#bool "checkers"
4134 (fun () -> conf.checkers)
4135 (fun v -> conf.checkers <- v; setcheckers v);
4136 src#bool "update cursor"
4137 (fun () -> conf.updatecurs)
4138 (fun v -> conf.updatecurs <- v);
4139 src#bool "verbose"
4140 (fun () -> conf.verbose)
4141 (fun v -> conf.verbose <- v);
4142 src#bool "invert colors"
4143 (fun () -> conf.invert)
4144 (fun v -> conf.invert <- v);
4145 src#bool "max fit"
4146 (fun () -> conf.maxhfit)
4147 (fun v -> conf.maxhfit <- v);
4148 src#bool "redirect stderr"
4149 (fun () -> conf.redirectstderr)
4150 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4151 src#string "uri launcher"
4152 (fun () -> conf.urilauncher)
4153 (fun v -> conf.urilauncher <- v);
4154 src#string "path launcher"
4155 (fun () -> conf.pathlauncher)
4156 (fun v -> conf.pathlauncher <- v);
4157 src#string "tile size"
4158 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4159 (fun v ->
4161 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4162 conf.tileh <- max 64 w;
4163 conf.tilew <- max 64 h;
4164 flushtiles ();
4165 with exn ->
4166 state.text <- Printf.sprintf "bad tile size `%s': %s"
4167 v (Printexc.to_string exn));
4168 src#int "texture count"
4169 (fun () -> conf.texcount)
4170 (fun v ->
4171 if realloctexts v
4172 then conf.texcount <- v
4173 else showtext '!' " Failed to set texture count please retry later"
4175 src#int "slice height"
4176 (fun () -> conf.sliceheight)
4177 (fun v ->
4178 conf.sliceheight <- v;
4179 wcmd "sliceh %d" conf.sliceheight;
4181 src#int "anti-aliasing level"
4182 (fun () -> conf.aalevel)
4183 (fun v ->
4184 conf.aalevel <- bound v 0 8;
4185 state.anchor <- getanchor ();
4186 opendoc state.path state.password;
4188 src#int "ui font size"
4189 (fun () -> fstate.fontsize)
4190 (fun v -> setfontsize (bound v 5 100));
4191 colorp "background color"
4192 (fun () -> conf.bgcolor)
4193 (fun v -> conf.bgcolor <- v);
4194 src#bool "crop hack"
4195 (fun () -> conf.crophack)
4196 (fun v -> conf.crophack <- v);
4197 src#string "trim fuzz"
4198 (fun () -> irect_to_string conf.trimfuzz)
4199 (fun v ->
4201 conf.trimfuzz <- irect_of_string v;
4202 if conf.trimmargins
4203 then settrim true conf.trimfuzz;
4204 with exn ->
4205 state.text <- Printf.sprintf "bad irect `%s': %s"
4206 v (Printexc.to_string exn)
4208 src#string "throttle"
4209 (fun () ->
4210 match conf.maxwait with
4211 | None -> "show place holder if page is not ready"
4212 | Some time ->
4213 if time = infinity
4214 then "wait for page to fully render"
4215 else
4216 "wait " ^ string_of_float time
4217 ^ " seconds before showing placeholder"
4219 (fun v ->
4221 let f = float_of_string v in
4222 if f <= 0.0
4223 then conf.maxwait <- None
4224 else conf.maxwait <- Some f
4225 with exn ->
4226 state.text <- Printf.sprintf "bad time `%s': %s"
4227 v (Printexc.to_string exn)
4229 src#string "ghyll scroll"
4230 (fun () ->
4231 match conf.ghyllscroll with
4232 | None -> ""
4233 | Some nab -> ghyllscroll_to_string nab
4235 (fun v ->
4237 let gs =
4238 if String.length v = 0
4239 then None
4240 else Some (ghyllscroll_of_string v)
4242 conf.ghyllscroll <- gs
4243 with exn ->
4244 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4245 v (Printexc.to_string exn)
4247 src#string "selection command"
4248 (fun () -> conf.selcmd)
4249 (fun v -> conf.selcmd <- v);
4250 src#colorspace "color space"
4251 (fun () -> colorspace_to_string conf.colorspace)
4252 (fun v ->
4253 conf.colorspace <- colorspace_of_int v;
4254 wcmd "cs %d" v;
4255 load state.layout;
4259 sep ();
4260 src#caption "Document" 0;
4261 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4262 src#caption2 "Pages"
4263 (fun () -> string_of_int state.pagecount) 1;
4264 src#caption2 "Dimensions"
4265 (fun () -> string_of_int (List.length state.pdims)) 1;
4266 if conf.trimmargins
4267 then (
4268 sep ();
4269 src#caption "Trimmed margins" 0;
4270 src#caption2 "Dimensions"
4271 (fun () -> string_of_int (List.length state.pdims)) 1;
4274 src#reset prevmode prevuioh;
4276 fun () ->
4277 state.text <- "";
4278 let prevmode = state.mode
4279 and prevuioh = state.uioh in
4280 fillsrc prevmode prevuioh;
4281 let source = (src :> lvsource) in
4282 let modehash = findkeyhash conf "info" in
4283 state.uioh <- coe (object (self)
4284 inherit listview ~source ~trusted:true ~modehash as super
4285 val mutable m_prevmemused = 0
4286 method infochanged = function
4287 | Memused ->
4288 if m_prevmemused != state.memused
4289 then (
4290 m_prevmemused <- state.memused;
4291 G.postRedisplay "memusedchanged";
4293 | Pdim -> G.postRedisplay "pdimchanged"
4294 | Docinfo -> fillsrc prevmode prevuioh
4296 method key key mask =
4297 if not (Wsi.withctrl mask)
4298 then
4299 match key with
4300 | 0xff51 -> coe (self#updownlevel ~-1)
4301 | 0xff53 -> coe (self#updownlevel 1)
4302 | _ -> super#key key mask
4303 else super#key key mask
4304 end);
4305 G.postRedisplay "info";
4308 let enterhelpmode =
4309 let source =
4310 (object
4311 inherit lvsourcebase
4312 method getitemcount = Array.length state.help
4313 method getitem n =
4314 let s, n, _ = state.help.(n) in
4315 (s, n)
4317 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4318 let optuioh =
4319 if not cancel
4320 then (
4321 m_qsearch <- qsearch;
4322 match state.help.(active) with
4323 | _, _, Action f -> Some (f uioh)
4324 | _ -> Some (uioh)
4326 else None
4328 m_active <- active;
4329 m_first <- first;
4330 m_pan <- pan;
4331 optuioh
4333 method hasaction n =
4334 match state.help.(n) with
4335 | _, _, Action _ -> true
4336 | _ -> false
4338 initializer
4339 m_active <- -1
4340 end)
4341 in fun () ->
4342 let modehash = findkeyhash conf "help" in
4343 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4344 G.postRedisplay "help";
4347 let entermsgsmode =
4348 let msgsource =
4349 let re = Str.regexp "[\r\n]" in
4350 (object
4351 inherit lvsourcebase
4352 val mutable m_items = [||]
4354 method getitemcount = 1 + Array.length m_items
4356 method getitem n =
4357 if n = 0
4358 then "[Clear]", 0
4359 else m_items.(n-1), 0
4361 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4362 ignore uioh;
4363 if not cancel
4364 then (
4365 if active = 0
4366 then Buffer.clear state.errmsgs;
4367 m_qsearch <- qsearch;
4369 m_active <- active;
4370 m_first <- first;
4371 m_pan <- pan;
4372 None
4374 method hasaction n =
4375 n = 0
4377 method reset =
4378 state.newerrmsgs <- false;
4379 let l = Str.split re (Buffer.contents state.errmsgs) in
4380 m_items <- Array.of_list l
4382 initializer
4383 m_active <- 0
4384 end)
4385 in fun () ->
4386 state.text <- "";
4387 msgsource#reset;
4388 let source = (msgsource :> lvsource) in
4389 let modehash = findkeyhash conf "listview" in
4390 state.uioh <- coe (object
4391 inherit listview ~source ~trusted:false ~modehash as super
4392 method display =
4393 if state.newerrmsgs
4394 then msgsource#reset;
4395 super#display
4396 end);
4397 G.postRedisplay "msgs";
4400 let quickbookmark ?title () =
4401 match state.layout with
4402 | [] -> ()
4403 | l :: _ ->
4404 let title =
4405 match title with
4406 | None ->
4407 let sec = Unix.gettimeofday () in
4408 let tm = Unix.localtime sec in
4409 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4410 (l.pageno+1)
4411 tm.Unix.tm_mday
4412 tm.Unix.tm_mon
4413 (tm.Unix.tm_year + 1900)
4414 tm.Unix.tm_hour
4415 tm.Unix.tm_min
4416 | Some title -> title
4418 state.bookmarks <-
4419 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4420 :: state.bookmarks
4423 let doreshape w h =
4424 state.fullscreen <- None;
4425 Wsi.reshape w h;
4428 let setautoscrollspeed step goingdown =
4429 let incr = max 1 ((abs step) / 2) in
4430 let incr = if goingdown then incr else -incr in
4431 let astep = step + incr in
4432 state.autoscroll <- Some astep;
4435 let gotounder = function
4436 | Ulinkgoto (pageno, top) ->
4437 if pageno >= 0
4438 then (
4439 addnav ();
4440 gotopage1 pageno top;
4443 | Ulinkuri s ->
4444 gotouri s
4446 | Uremote (filename, pageno) ->
4447 let path =
4448 if Sys.file_exists filename
4449 then filename
4450 else
4451 let dir = Filename.dirname state.path in
4452 let path = Filename.concat dir filename in
4453 if Sys.file_exists path
4454 then path
4455 else ""
4457 if String.length path > 0
4458 then (
4459 let anchor = getanchor () in
4460 let ranchor = state.path, state.password, anchor in
4461 state.anchor <- (pageno, 0.0);
4462 state.ranchors <- ranchor :: state.ranchors;
4463 opendoc path "";
4465 else showtext '!' ("Could not find " ^ filename)
4467 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4470 let canpan () =
4471 match conf.columns with
4472 | Csplit _ -> true
4473 | _ -> conf.zoom > 1.0
4476 let viewkeyboard key mask =
4477 let enttext te =
4478 let mode = state.mode in
4479 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4480 state.text <- "";
4481 enttext ();
4482 G.postRedisplay "view:enttext"
4484 let ctrl = Wsi.withctrl mask in
4485 match key with
4486 | 81 -> (* Q *)
4487 exit 0
4489 | 0xff63 -> (* insert *)
4490 if conf.angle mod 360 = 0
4491 then (
4492 state.mode <- LinkNav (Ltgendir 0);
4493 gotoy state.y;
4495 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4497 | 0xff1b | 113 -> (* escape / q *)
4498 begin match state.mstate with
4499 | Mzoomrect _ ->
4500 state.mstate <- Mnone;
4501 Wsi.setcursor Wsi.CURSOR_INHERIT;
4502 G.postRedisplay "kill zoom rect";
4503 | _ ->
4504 match state.ranchors with
4505 | [] -> raise Quit
4506 | (path, password, anchor) :: rest ->
4507 state.ranchors <- rest;
4508 state.anchor <- anchor;
4509 opendoc path password
4510 end;
4512 | 0xff08 -> (* backspace *)
4513 let y = getnav ~-1 in
4514 gotoy_and_clear_text y
4516 | 111 -> (* o *)
4517 enteroutlinemode ()
4519 | 117 -> (* u *)
4520 state.rects <- [];
4521 state.text <- "";
4522 G.postRedisplay "dehighlight";
4524 | 47 | 63 -> (* / ? *)
4525 let ondone isforw s =
4526 cbput state.hists.pat s;
4527 state.searchpattern <- s;
4528 search s isforw
4530 let s = String.create 1 in
4531 s.[0] <- Char.chr key;
4532 enttext (s, "", Some (onhist state.hists.pat),
4533 textentry, ondone (key = 47))
4535 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4536 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4537 setzoom (conf.zoom +. incr)
4539 | 43 | 0xffab -> (* + *)
4540 let ondone s =
4541 let n =
4542 try int_of_string s with exc ->
4543 state.text <- Printf.sprintf "bad integer `%s': %s"
4544 s (Printexc.to_string exc);
4545 max_int
4547 if n != max_int
4548 then (
4549 conf.pagebias <- n;
4550 state.text <- "page bias is now " ^ string_of_int n;
4553 enttext ("page bias: ", "", None, intentry, ondone)
4555 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4556 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4557 setzoom (max 0.01 (conf.zoom -. decr))
4559 | 45 | 0xffad -> (* - *)
4560 let ondone msg = state.text <- msg in
4561 enttext (
4562 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4563 optentry state.mode, ondone
4566 | 48 when ctrl -> (* ctrl-0 *)
4567 setzoom 1.0
4569 | 49 when ctrl -> (* 1 *)
4570 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4571 if zoom < 1.0
4572 then setzoom zoom
4574 | 0xffc6 -> (* f9 *)
4575 togglebirdseye ()
4577 | 57 when ctrl -> (* ctrl-9 *)
4578 togglebirdseye ()
4580 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4581 when not ctrl -> (* 0..9 *)
4582 let ondone s =
4583 let n =
4584 try int_of_string s with exc ->
4585 state.text <- Printf.sprintf "bad integer `%s': %s"
4586 s (Printexc.to_string exc);
4589 if n >= 0
4590 then (
4591 addnav ();
4592 cbput state.hists.pag (string_of_int n);
4593 gotopage1 (n + conf.pagebias - 1) 0;
4596 let pageentry text key =
4597 match Char.unsafe_chr key with
4598 | 'g' -> TEdone text
4599 | _ -> intentry text key
4601 let text = "x" in text.[0] <- Char.chr key;
4602 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4604 | 98 -> (* b *)
4605 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4606 reshape conf.winw conf.winh;
4608 | 108 -> (* l *)
4609 conf.hlinks <- not conf.hlinks;
4610 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4611 G.postRedisplay "toggle highlightlinks";
4613 | 70 -> (* F *)
4614 state.glinks <- true;
4615 let ondone s =
4616 let n =
4617 try int_of_string s with exc ->
4618 state.text <- Printf.sprintf "bad integer `%s': %s"
4619 s (Printexc.to_string exc);
4622 if n >= 0
4623 then (
4624 let rec loop n = function
4625 | [] -> ()
4626 | l :: rest ->
4627 match getopaque l.pageno with
4628 | None -> loop n rest
4629 | Some opaque ->
4630 let m = getlinkcount opaque in
4631 if n < m
4632 then (
4633 let under = getlink opaque n in
4634 addnav ();
4635 gotounder under;
4637 else loop (n-m) rest
4639 loop n state.layout;
4642 let onkey text key =
4643 match Char.unsafe_chr key with
4644 | 'g' -> TEdone text
4645 | _ -> intentry text key
4647 let mode = state.mode in
4648 state.mode <- Textentry (
4649 (":", "", Some (onhist state.hists.pag), onkey, ondone),
4650 fun _ ->
4651 state.glinks <- false;
4652 state.mode <- mode
4654 state.text <- "";
4655 G.postRedisplay "view:enttext"
4657 | 97 -> (* a *)
4658 begin match state.autoscroll with
4659 | Some step ->
4660 conf.autoscrollstep <- step;
4661 state.autoscroll <- None
4662 | None ->
4663 if conf.autoscrollstep = 0
4664 then state.autoscroll <- Some 1
4665 else state.autoscroll <- Some conf.autoscrollstep
4668 | 112 when ctrl -> (* ctrl-p *)
4669 launchpath ()
4671 | 80 -> (* P *)
4672 conf.presentation <- not conf.presentation;
4673 if conf.presentation
4674 then (
4675 if not conf.scrollbarinpm
4676 then state.scrollw <- 0;
4678 else
4679 state.scrollw <- conf.scrollbw;
4681 showtext ' ' ("presentation mode " ^
4682 if conf.presentation then "on" else "off");
4683 state.anchor <- getanchor ();
4684 represent ()
4686 | 102 -> (* f *)
4687 begin match state.fullscreen with
4688 | None ->
4689 state.fullscreen <- Some (conf.winw, conf.winh);
4690 Wsi.fullscreen ()
4691 | Some (w, h) ->
4692 state.fullscreen <- None;
4693 doreshape w h
4696 | 103 -> (* g *)
4697 gotoy_and_clear_text 0
4699 | 71 -> (* G *)
4700 gotopage1 (state.pagecount - 1) 0
4702 | 112 | 78 -> (* p|N *)
4703 search state.searchpattern false
4705 | 110 | 0xffc0 -> (* n|F3 *)
4706 search state.searchpattern true
4708 | 116 -> (* t *)
4709 begin match state.layout with
4710 | [] -> ()
4711 | l :: _ ->
4712 gotoy_and_clear_text (getpagey l.pageno)
4715 | 32 -> (* ' ' *)
4716 begin match List.rev state.layout with
4717 | [] -> ()
4718 | l :: _ ->
4719 let pageno = min (l.pageno+1) (state.pagecount-1) in
4720 gotoy_and_clear_text (getpagey pageno)
4723 | 0xff9f | 0xffff -> (* delete *)
4724 begin match state.layout with
4725 | [] -> ()
4726 | l :: _ ->
4727 let pageno = max 0 (l.pageno-1) in
4728 gotoy_and_clear_text (getpagey pageno)
4731 | 61 -> (* = *)
4732 showtext ' ' (describe_location ());
4734 | 119 -> (* w *)
4735 begin match state.layout with
4736 | [] -> ()
4737 | l :: _ ->
4738 doreshape (l.pagew + state.scrollw) l.pageh;
4739 G.postRedisplay "w"
4742 | 39 -> (* ' *)
4743 enterbookmarkmode ()
4745 | 104 | 0xffbe -> (* h|F1 *)
4746 enterhelpmode ()
4748 | 105 -> (* i *)
4749 enterinfomode ()
4751 | 101 when conf.redirectstderr -> (* e *)
4752 entermsgsmode ()
4754 | 109 -> (* m *)
4755 let ondone s =
4756 match state.layout with
4757 | l :: _ ->
4758 state.bookmarks <-
4759 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4760 :: state.bookmarks
4761 | _ -> ()
4763 enttext ("bookmark: ", "", None, textentry, ondone)
4765 | 126 -> (* ~ *)
4766 quickbookmark ();
4767 showtext ' ' "Quick bookmark added";
4769 | 122 -> (* z *)
4770 begin match state.layout with
4771 | l :: _ ->
4772 let rect = getpdimrect l.pagedimno in
4773 let w, h =
4774 if conf.crophack
4775 then
4776 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4777 truncate (1.2 *. (rect.(3) -. rect.(0))))
4778 else
4779 (truncate (rect.(1) -. rect.(0)),
4780 truncate (rect.(3) -. rect.(0)))
4782 let w = truncate ((float w)*.conf.zoom)
4783 and h = truncate ((float h)*.conf.zoom) in
4784 if w != 0 && h != 0
4785 then (
4786 state.anchor <- getanchor ();
4787 doreshape (w + state.scrollw) (h + conf.interpagespace)
4789 G.postRedisplay "z";
4791 | [] -> ()
4794 | 50 when ctrl -> (* ctrl-2 *)
4795 let maxw = getmaxw () in
4796 if maxw > 0.0
4797 then setzoom (maxw /. float conf.winw)
4799 | 60 | 62 -> (* < > *)
4800 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4802 | 91 | 93 -> (* [ ] *)
4803 conf.colorscale <-
4804 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4806 G.postRedisplay "brightness";
4808 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4809 setzoom state.prevzoom
4811 | 107 | 0xff52 -> (* k up *)
4812 begin match state.autoscroll with
4813 | None ->
4814 begin match state.mode with
4815 | Birdseye beye -> upbirdseye 1 beye
4816 | _ ->
4817 if ctrl
4818 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4819 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4821 | Some n ->
4822 setautoscrollspeed n false
4825 | 106 | 0xff54 -> (* j down *)
4826 begin match state.autoscroll with
4827 | None ->
4828 begin match state.mode with
4829 | Birdseye beye -> downbirdseye 1 beye
4830 | _ ->
4831 if ctrl
4832 then gotoy_and_clear_text (clamp (conf.winh/2))
4833 else gotoy_and_clear_text (clamp conf.scrollstep)
4835 | Some n ->
4836 setautoscrollspeed n true
4839 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4840 if canpan ()
4841 then
4842 let dx =
4843 if ctrl
4844 then conf.winw / 2
4845 else 10
4847 let dx = if key = 0xff51 then dx else -dx in
4848 state.x <- state.x + dx;
4849 gotoy_and_clear_text state.y
4850 else (
4851 state.text <- "";
4852 G.postRedisplay "lef/right"
4855 | 0xff55 -> (* prior *)
4856 let y =
4857 if ctrl
4858 then
4859 match state.layout with
4860 | [] -> state.y
4861 | l :: _ -> state.y - l.pagey
4862 else
4863 clamp (-conf.winh)
4865 gotoghyll y
4867 | 0xff56 -> (* next *)
4868 let y =
4869 if ctrl
4870 then
4871 match List.rev state.layout with
4872 | [] -> state.y
4873 | l :: _ -> getpagey l.pageno
4874 else
4875 clamp conf.winh
4877 gotoghyll y
4879 | 0xff50 -> gotoghyll 0
4880 | 0xff57 -> gotoghyll (clamp state.maxy)
4881 | 0xff53 when Wsi.withalt mask ->
4882 gotoghyll (getnav ~-1)
4883 | 0xff51 when Wsi.withalt mask ->
4884 gotoghyll (getnav 1)
4886 | 114 -> (* r *)
4887 state.anchor <- getanchor ();
4888 opendoc state.path state.password
4890 | 118 when conf.debug -> (* v *)
4891 state.rects <- [];
4892 List.iter (fun l ->
4893 match getopaque l.pageno with
4894 | None -> ()
4895 | Some opaque ->
4896 let x0, y0, x1, y1 = pagebbox opaque in
4897 let a,b = float x0, float y0 in
4898 let c,d = float x1, float y0 in
4899 let e,f = float x1, float y1 in
4900 let h,j = float x0, float y1 in
4901 let rect = (a,b,c,d,e,f,h,j) in
4902 debugrect rect;
4903 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4904 ) state.layout;
4905 G.postRedisplay "v";
4907 | _ ->
4908 vlog "huh? %s" (Wsi.keyname key)
4911 let linknavkeyboard key mask linknav =
4912 let getpage pageno =
4913 let rec loop = function
4914 | [] -> None
4915 | l :: _ when l.pageno = pageno -> Some l
4916 | _ :: rest -> loop rest
4917 in loop state.layout
4919 let doexact (pageno, n) =
4920 match getopaque pageno, getpage pageno with
4921 | Some opaque, Some l ->
4922 if key = 0xff0d
4923 then
4924 let under = getlink opaque n in
4925 G.postRedisplay "link gotounder";
4926 gotounder under;
4927 state.mode <- View;
4928 else
4929 let opt, dir =
4930 match key with
4931 | 0xff50 -> (* home *)
4932 Some (findlink opaque LDfirst), -1
4934 | 0xff57 -> (* end *)
4935 Some (findlink opaque LDlast), 1
4937 | 0xff51 -> (* left *)
4938 Some (findlink opaque (LDleft n)), -1
4940 | 0xff53 -> (* right *)
4941 Some (findlink opaque (LDright n)), 1
4943 | 0xff52 -> (* up *)
4944 Some (findlink opaque (LDup n)), -1
4946 | 0xff54 -> (* down *)
4947 Some (findlink opaque (LDdown n)), 1
4949 | _ -> None, 0
4951 let pwl l dir =
4952 begin match findpwl l.pageno dir with
4953 | Pwlnotfound -> ()
4954 | Pwl pageno ->
4955 let notfound dir =
4956 state.mode <- LinkNav (Ltgendir dir);
4957 let y, h = getpageyh pageno in
4958 let y =
4959 if dir < 0
4960 then y + h - conf.winh
4961 else y
4963 gotoy y
4965 begin match getopaque pageno, getpage pageno with
4966 | Some opaque, Some _ ->
4967 let link =
4968 let ld = if dir > 0 then LDfirst else LDlast in
4969 findlink opaque ld
4971 begin match link with
4972 | Lfound m ->
4973 showlinktype (getlink opaque m);
4974 state.mode <- LinkNav (Ltexact (pageno, m));
4975 G.postRedisplay "linknav jpage";
4976 | _ -> notfound dir
4977 end;
4978 | _ -> notfound dir
4979 end;
4980 end;
4982 begin match opt with
4983 | Some Lnotfound -> pwl l dir;
4984 | Some (Lfound m) ->
4985 if m = n
4986 then pwl l dir
4987 else (
4988 let _, y0, _, y1 = getlinkrect opaque m in
4989 if y0 < l.pagey
4990 then gotopage1 l.pageno y0
4991 else (
4992 let d = fstate.fontsize + 1 in
4993 if y1 - l.pagey > l.pagevh - d
4994 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
4995 else G.postRedisplay "linknav";
4997 showlinktype (getlink opaque m);
4998 state.mode <- LinkNav (Ltexact (l.pageno, m));
5001 | None -> viewkeyboard key mask
5002 end;
5003 | _ -> viewkeyboard key mask
5005 if key = 0xff63
5006 then (
5007 state.mode <- View;
5008 G.postRedisplay "leave linknav"
5010 else
5011 match linknav with
5012 | Ltgendir _ -> viewkeyboard key mask
5013 | Ltexact exact -> doexact exact
5016 let keyboard key mask =
5017 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5018 then wcmd "interrupt"
5019 else state.uioh <- state.uioh#key key mask
5022 let birdseyekeyboard key mask
5023 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5024 let incr =
5025 match conf.columns with
5026 | Csingle -> 1
5027 | Cmulti ((c, _, _), _) -> c
5028 | Csplit _ -> failwith "bird's eye split mode"
5030 match key with
5031 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5032 let y, h = getpageyh pageno in
5033 let top = (conf.winh - h) / 2 in
5034 gotoy (max 0 (y - top))
5035 | 0xff0d -> leavebirdseye beye false
5036 | 0xff1b -> leavebirdseye beye true (* escape *)
5037 | 0xff52 -> upbirdseye incr beye (* prior *)
5038 | 0xff54 -> downbirdseye incr beye (* next *)
5039 | 0xff51 -> upbirdseye 1 beye (* up *)
5040 | 0xff53 -> downbirdseye 1 beye (* down *)
5042 | 0xff55 ->
5043 begin match state.layout with
5044 | l :: _ ->
5045 if l.pagey != 0
5046 then (
5047 state.mode <- Birdseye (
5048 oconf, leftx, l.pageno, hooverpageno, anchor
5050 gotopage1 l.pageno 0;
5052 else (
5053 let layout = layout (state.y-conf.winh) conf.winh in
5054 match layout with
5055 | [] -> gotoy (clamp (-conf.winh))
5056 | l :: _ ->
5057 state.mode <- Birdseye (
5058 oconf, leftx, l.pageno, hooverpageno, anchor
5060 gotopage1 l.pageno 0
5063 | [] -> gotoy (clamp (-conf.winh))
5064 end;
5066 | 0xff56 ->
5067 begin match List.rev state.layout with
5068 | l :: _ ->
5069 let layout = layout (state.y + conf.winh) conf.winh in
5070 begin match layout with
5071 | [] ->
5072 let incr = l.pageh - l.pagevh in
5073 if incr = 0
5074 then (
5075 state.mode <-
5076 Birdseye (
5077 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5079 G.postRedisplay "birdseye pagedown";
5081 else gotoy (clamp (incr + conf.interpagespace*2));
5083 | l :: _ ->
5084 state.mode <-
5085 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5086 gotopage1 l.pageno 0;
5089 | [] -> gotoy (clamp conf.winh)
5090 end;
5092 | 0xff50 ->
5093 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5094 gotopage1 0 0
5096 | 0xff57 ->
5097 let pageno = state.pagecount - 1 in
5098 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5099 if not (pagevisible state.layout pageno)
5100 then
5101 let h =
5102 match List.rev state.pdims with
5103 | [] -> conf.winh
5104 | (_, _, h, _) :: _ -> h
5106 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5107 else G.postRedisplay "birdseye end";
5108 | _ -> viewkeyboard key mask
5111 let drawpage l linkindexbase =
5112 let color =
5113 match state.mode with
5114 | Textentry _ -> scalecolor 0.4
5115 | LinkNav _
5116 | View -> scalecolor 1.0
5117 | Birdseye (_, _, pageno, hooverpageno, _) ->
5118 if l.pageno = hooverpageno
5119 then scalecolor 0.9
5120 else (
5121 if l.pageno = pageno
5122 then scalecolor 1.0
5123 else scalecolor 0.8
5126 drawtiles l color;
5127 begin match getopaque l.pageno with
5128 | Some opaque ->
5129 if tileready l l.pagex l.pagey
5130 then
5131 let x = l.pagedispx - l.pagex
5132 and y = l.pagedispy - l.pagey in
5133 let hlmask = (if conf.hlinks then 1 else 0)
5134 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5136 postprocess opaque hlmask x y linkindexbase;
5137 else 0
5139 | _ -> 0
5140 end;
5143 let scrollindicator () =
5144 let sbw, ph, sh = state.uioh#scrollph in
5145 let sbh, pw, sw = state.uioh#scrollpw in
5147 GlDraw.color (0.64, 0.64, 0.64);
5148 GlDraw.rect
5149 (float (conf.winw - sbw), 0.)
5150 (float conf.winw, float conf.winh)
5152 GlDraw.rect
5153 (0., float (conf.winh - sbh))
5154 (float (conf.winw - state.scrollw - 1), float conf.winh)
5156 GlDraw.color (0.0, 0.0, 0.0);
5158 GlDraw.rect
5159 (float (conf.winw - sbw), ph)
5160 (float conf.winw, ph +. sh)
5162 GlDraw.rect
5163 (pw, float (conf.winh - sbh))
5164 (pw +. sw, float conf.winh)
5168 let showsel () =
5169 match state.mstate with
5170 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5173 | Msel ((x0, y0), (x1, y1)) ->
5174 let rec loop = function
5175 | l :: ls ->
5176 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5177 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5178 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5179 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5180 then
5181 match getopaque l.pageno with
5182 | Some opaque ->
5183 let x0, y0 = pagetranslatepoint l x0 y0 in
5184 let x1, y1 = pagetranslatepoint l x1 y1 in
5185 seltext opaque (x0, y0, x1, y1);
5186 | _ -> ()
5187 else loop ls
5188 | [] -> ()
5190 loop state.layout
5193 let showrects rects =
5194 Gl.enable `blend;
5195 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5196 GlDraw.polygon_mode `both `fill;
5197 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5198 List.iter
5199 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5200 List.iter (fun l ->
5201 if l.pageno = pageno
5202 then (
5203 let dx = float (l.pagedispx - l.pagex) in
5204 let dy = float (l.pagedispy - l.pagey) in
5205 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5206 GlDraw.begins `quads;
5208 GlDraw.vertex2 (x0+.dx, y0+.dy);
5209 GlDraw.vertex2 (x1+.dx, y1+.dy);
5210 GlDraw.vertex2 (x2+.dx, y2+.dy);
5211 GlDraw.vertex2 (x3+.dx, y3+.dy);
5213 GlDraw.ends ();
5215 ) state.layout
5216 ) rects
5218 Gl.disable `blend;
5221 let display () =
5222 GlClear.color (scalecolor2 conf.bgcolor);
5223 GlClear.clear [`color];
5224 let rec loop linkindexbase = function
5225 | l :: rest ->
5226 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5227 loop linkindexbase rest
5228 | [] -> ()
5230 loop 0 state.layout;
5231 let rects =
5232 match state.mode with
5233 | LinkNav (Ltexact (pageno, linkno)) ->
5234 begin match getopaque pageno with
5235 | Some opaque ->
5236 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5237 (pageno, 5, (
5238 float x0, float y0,
5239 float x1, float y0,
5240 float x1, float y1,
5241 float x0, float y1)
5242 ) :: state.rects
5243 | None -> state.rects
5245 | _ -> state.rects
5247 showrects rects;
5248 showsel ();
5249 state.uioh#display;
5250 begin match state.mstate with
5251 | Mzoomrect ((x0, y0), (x1, y1)) ->
5252 Gl.enable `blend;
5253 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5254 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5255 GlDraw.rect (float x0, float y0)
5256 (float x1, float y1);
5257 Gl.disable `blend;
5258 | _ -> ()
5259 end;
5260 enttext ();
5261 scrollindicator ();
5262 Wsi.swapb ();
5265 let zoomrect x y x1 y1 =
5266 let x0 = min x x1
5267 and x1 = max x x1
5268 and y0 = min y y1 in
5269 gotoy (state.y + y0);
5270 state.anchor <- getanchor ();
5271 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5272 let margin =
5273 if state.w < conf.winw - state.scrollw
5274 then (conf.winw - state.scrollw - state.w) / 2
5275 else 0
5277 state.x <- (state.x + margin) - x0;
5278 setzoom zoom;
5279 Wsi.setcursor Wsi.CURSOR_INHERIT;
5280 state.mstate <- Mnone;
5283 let scrollx x =
5284 let winw = conf.winw - state.scrollw - 1 in
5285 let s = float x /. float winw in
5286 let destx = truncate (float (state.w + winw) *. s) in
5287 state.x <- winw - destx;
5288 gotoy_and_clear_text state.y;
5289 state.mstate <- Mscrollx;
5292 let scrolly y =
5293 let s = float y /. float conf.winh in
5294 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5295 gotoy_and_clear_text desty;
5296 state.mstate <- Mscrolly;
5299 let viewmouse button down x y mask =
5300 match button with
5301 | n when (n == 4 || n == 5) && not down ->
5302 if Wsi.withctrl mask
5303 then (
5304 match state.mstate with
5305 | Mzoom (oldn, i) ->
5306 if oldn = n
5307 then (
5308 if i = 2
5309 then
5310 let incr =
5311 match n with
5312 | 5 ->
5313 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5314 | _ ->
5315 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5317 let zoom = conf.zoom -. incr in
5318 setzoom zoom;
5319 state.mstate <- Mzoom (n, 0);
5320 else
5321 state.mstate <- Mzoom (n, i+1);
5323 else state.mstate <- Mzoom (n, 0)
5325 | _ -> state.mstate <- Mzoom (n, 0)
5327 else (
5328 match state.autoscroll with
5329 | Some step -> setautoscrollspeed step (n=4)
5330 | None ->
5331 let incr =
5332 if n = 4
5333 then -conf.scrollstep
5334 else conf.scrollstep
5336 let incr = incr * 2 in
5337 let y = clamp incr in
5338 gotoy_and_clear_text y
5341 | 1 when Wsi.withctrl mask ->
5342 if down
5343 then (
5344 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5345 state.mstate <- Mpan (x, y)
5347 else
5348 state.mstate <- Mnone
5350 | 3 ->
5351 if down
5352 then (
5353 Wsi.setcursor Wsi.CURSOR_CYCLE;
5354 let p = (x, y) in
5355 state.mstate <- Mzoomrect (p, p)
5357 else (
5358 match state.mstate with
5359 | Mzoomrect ((x0, y0), _) ->
5360 if abs (x-x0) > 10 && abs (y - y0) > 10
5361 then zoomrect x0 y0 x y
5362 else (
5363 state.mstate <- Mnone;
5364 Wsi.setcursor Wsi.CURSOR_INHERIT;
5365 G.postRedisplay "kill accidental zoom rect";
5367 | _ ->
5368 Wsi.setcursor Wsi.CURSOR_INHERIT;
5369 state.mstate <- Mnone
5372 | 1 when x > conf.winw - state.scrollw ->
5373 if down
5374 then
5375 let _, position, sh = state.uioh#scrollph in
5376 if y > truncate position && y < truncate (position +. sh)
5377 then state.mstate <- Mscrolly
5378 else scrolly y
5379 else
5380 state.mstate <- Mnone
5382 | 1 when y > conf.winh - state.hscrollh ->
5383 if down
5384 then
5385 let _, position, sw = state.uioh#scrollpw in
5386 if x > truncate position && x < truncate (position +. sw)
5387 then state.mstate <- Mscrollx
5388 else scrollx x
5389 else
5390 state.mstate <- Mnone
5392 | 1 ->
5393 let dest = if down then getunder x y else Unone in
5394 begin match dest with
5395 | Ulinkgoto _
5396 | Ulinkuri _
5397 | Uremote _
5398 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5399 gotounder dest
5401 | Unone when down ->
5402 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5403 state.mstate <- Mpan (x, y);
5405 | Unone | Utext _ ->
5406 if down
5407 then (
5408 if conf.angle mod 360 = 0
5409 then (
5410 state.mstate <- Msel ((x, y), (x, y));
5411 G.postRedisplay "mouse select";
5414 else (
5415 match state.mstate with
5416 | Mnone -> ()
5418 | Mzoom _ | Mscrollx | Mscrolly ->
5419 state.mstate <- Mnone
5421 | Mzoomrect ((x0, y0), _) ->
5422 zoomrect x0 y0 x y
5424 | Mpan _ ->
5425 Wsi.setcursor Wsi.CURSOR_INHERIT;
5426 state.mstate <- Mnone
5428 | Msel ((_, y0), (_, y1)) ->
5429 let rec loop = function
5430 | [] -> ()
5431 | l :: rest ->
5432 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5433 || ((y1 >= l.pagedispy
5434 && y1 <= (l.pagedispy + l.pagevh)))
5435 then
5436 match getopaque l.pageno with
5437 | Some opaque ->
5438 copysel conf.selcmd opaque;
5439 G.postRedisplay "copysel"
5440 | _ -> ()
5441 else loop rest
5443 loop state.layout;
5444 Wsi.setcursor Wsi.CURSOR_INHERIT;
5445 state.mstate <- Mnone;
5449 | _ -> ()
5452 let birdseyemouse button down x y mask
5453 (conf, leftx, _, hooverpageno, anchor) =
5454 match button with
5455 | 1 when down ->
5456 let rec loop = function
5457 | [] -> ()
5458 | l :: rest ->
5459 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5460 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5461 then (
5462 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5464 else loop rest
5466 loop state.layout
5467 | 3 -> ()
5468 | _ -> viewmouse button down x y mask
5471 let mouse button down x y mask =
5472 state.uioh <- state.uioh#button button down x y mask;
5475 let motion ~x ~y =
5476 state.uioh <- state.uioh#motion x y
5479 let pmotion ~x ~y =
5480 state.uioh <- state.uioh#pmotion x y;
5483 let uioh = object
5484 method display = ()
5486 method key key mask =
5487 begin match state.mode with
5488 | Textentry textentry -> textentrykeyboard key mask textentry
5489 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5490 | View -> viewkeyboard key mask
5491 | LinkNav linknav -> linknavkeyboard key mask linknav
5492 end;
5493 state.uioh
5495 method button button bstate x y mask =
5496 begin match state.mode with
5497 | LinkNav _
5498 | View -> viewmouse button bstate x y mask
5499 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5500 | Textentry _ -> ()
5501 end;
5502 state.uioh
5504 method motion x y =
5505 begin match state.mode with
5506 | Textentry _ -> ()
5507 | View | Birdseye _ | LinkNav _ ->
5508 match state.mstate with
5509 | Mzoom _ | Mnone -> ()
5511 | Mpan (x0, y0) ->
5512 let dx = x - x0
5513 and dy = y0 - y in
5514 state.mstate <- Mpan (x, y);
5515 if canpan ()
5516 then state.x <- state.x + dx;
5517 let y = clamp dy in
5518 gotoy_and_clear_text y
5520 | Msel (a, _) ->
5521 state.mstate <- Msel (a, (x, y));
5522 G.postRedisplay "motion select";
5524 | Mscrolly ->
5525 let y = min conf.winh (max 0 y) in
5526 scrolly y
5528 | Mscrollx ->
5529 let x = min conf.winw (max 0 x) in
5530 scrollx x
5532 | Mzoomrect (p0, _) ->
5533 state.mstate <- Mzoomrect (p0, (x, y));
5534 G.postRedisplay "motion zoomrect";
5535 end;
5536 state.uioh
5538 method pmotion x y =
5539 begin match state.mode with
5540 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5541 let rec loop = function
5542 | [] ->
5543 if hooverpageno != -1
5544 then (
5545 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5546 G.postRedisplay "pmotion birdseye no hoover";
5548 | l :: rest ->
5549 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5550 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5551 then (
5552 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5553 G.postRedisplay "pmotion birdseye hoover";
5555 else loop rest
5557 loop state.layout
5559 | Textentry _ -> ()
5561 | LinkNav _
5562 | View ->
5563 match state.mstate with
5564 | Mnone -> updateunder x y
5565 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5567 end;
5568 state.uioh
5570 method infochanged _ = ()
5572 method scrollph =
5573 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5574 let p, h = scrollph state.y maxy in
5575 state.scrollw, p, h
5577 method scrollpw =
5578 let winw = conf.winw - state.scrollw - 1 in
5579 let fwinw = float winw in
5580 let sw =
5581 let sw = fwinw /. float state.w in
5582 let sw = fwinw *. sw in
5583 max sw (float conf.scrollh)
5585 let position, sw =
5586 let f = state.w+winw in
5587 let r = float (winw-state.x) /. float f in
5588 let p = fwinw *. r in
5589 p-.sw/.2., sw
5591 let sw =
5592 if position +. sw > fwinw
5593 then fwinw -. position
5594 else sw
5596 state.hscrollh, position, sw
5598 method modehash =
5599 let modename =
5600 match state.mode with
5601 | LinkNav _ -> "links"
5602 | Textentry _ -> "textentry"
5603 | Birdseye _ -> "birdseye"
5604 | View -> "global"
5606 findkeyhash conf modename
5607 end;;
5609 module Config =
5610 struct
5611 open Parser
5613 let fontpath = ref "";;
5615 module KeyMap =
5616 Map.Make (struct type t = (int * int) let compare = compare end);;
5618 let unent s =
5619 let l = String.length s in
5620 let b = Buffer.create l in
5621 unent b s 0 l;
5622 Buffer.contents b;
5625 let home =
5626 try Sys.getenv "HOME"
5627 with exn ->
5628 prerr_endline
5629 ("Can not determine home directory location: " ^
5630 Printexc.to_string exn);
5634 let modifier_of_string = function
5635 | "alt" -> Wsi.altmask
5636 | "shift" -> Wsi.shiftmask
5637 | "ctrl" | "control" -> Wsi.ctrlmask
5638 | "meta" -> Wsi.metamask
5639 | _ -> 0
5642 let key_of_string =
5643 let r = Str.regexp "-" in
5644 fun s ->
5645 let elems = Str.full_split r s in
5646 let f n k m =
5647 let g s =
5648 let m1 = modifier_of_string s in
5649 if m1 = 0
5650 then (Wsi.namekey s, m)
5651 else (k, m lor m1)
5652 in function
5653 | Str.Delim s when n land 1 = 0 -> g s
5654 | Str.Text s -> g s
5655 | Str.Delim _ -> (k, m)
5657 let rec loop n k m = function
5658 | [] -> (k, m)
5659 | x :: xs ->
5660 let k, m = f n k m x in
5661 loop (n+1) k m xs
5663 loop 0 0 0 elems
5666 let keys_of_string =
5667 let r = Str.regexp "[ \t]" in
5668 fun s ->
5669 let elems = Str.split r s in
5670 List.map key_of_string elems
5673 let copykeyhashes c =
5674 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5677 let config_of c attrs =
5678 let apply c k v =
5680 match k with
5681 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5682 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5683 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5684 | "preload" -> { c with preload = bool_of_string v }
5685 | "page-bias" -> { c with pagebias = int_of_string v }
5686 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5687 | "auto-scroll-step" ->
5688 { c with autoscrollstep = max 0 (int_of_string v) }
5689 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5690 | "crop-hack" -> { c with crophack = bool_of_string v }
5691 | "throttle" ->
5692 let mw =
5693 match String.lowercase v with
5694 | "true" -> Some infinity
5695 | "false" -> None
5696 | f -> Some (float_of_string f)
5698 { c with maxwait = mw}
5699 | "highlight-links" -> { c with hlinks = bool_of_string v }
5700 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5701 | "vertical-margin" ->
5702 { c with interpagespace = max 0 (int_of_string v) }
5703 | "zoom" ->
5704 let zoom = float_of_string v /. 100. in
5705 let zoom = max zoom 0.0 in
5706 { c with zoom = zoom }
5707 | "presentation" -> { c with presentation = bool_of_string v }
5708 | "rotation-angle" -> { c with angle = int_of_string v }
5709 | "width" -> { c with winw = max 20 (int_of_string v) }
5710 | "height" -> { c with winh = max 20 (int_of_string v) }
5711 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5712 | "proportional-display" -> { c with proportional = bool_of_string v }
5713 | "pixmap-cache-size" ->
5714 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5715 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5716 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5717 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5718 | "persistent-location" -> { c with jumpback = bool_of_string v }
5719 | "background-color" -> { c with bgcolor = color_of_string v }
5720 | "scrollbar-in-presentation" ->
5721 { c with scrollbarinpm = bool_of_string v }
5722 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5723 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5724 | "mupdf-store-size" ->
5725 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5726 | "checkers" -> { c with checkers = bool_of_string v }
5727 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5728 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5729 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5730 | "uri-launcher" -> { c with urilauncher = unent v }
5731 | "path-launcher" -> { c with pathlauncher = unent v }
5732 | "color-space" -> { c with colorspace = colorspace_of_string v }
5733 | "invert-colors" -> { c with invert = bool_of_string v }
5734 | "brightness" -> { c with colorscale = float_of_string v }
5735 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5736 | "ghyllscroll" ->
5737 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5738 | "columns" ->
5739 let (n, _, _) as nab = multicolumns_of_string v in
5740 if n < 0
5741 then { c with columns = Csplit (-n, [||]) }
5742 else { c with columns = Cmulti (nab, [||]) }
5743 | "birds-eye-columns" ->
5744 { c with beyecolumns = Some (max (int_of_string v) 2) }
5745 | "selection-command" -> { c with selcmd = unent v }
5746 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5747 | _ -> c
5748 with exn ->
5749 prerr_endline ("Error processing attribute (`" ^
5750 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5753 let rec fold c = function
5754 | [] -> c
5755 | (k, v) :: rest ->
5756 let c = apply c k v in
5757 fold c rest
5759 fold { c with keyhashes = copykeyhashes c } attrs;
5762 let fromstring f pos n v d =
5763 try f v
5764 with exn ->
5765 dolog "Error processing attribute (%S=%S) at %d\n%s"
5766 n v pos (Printexc.to_string exn)
5771 let bookmark_of attrs =
5772 let rec fold title page rely = function
5773 | ("title", v) :: rest -> fold v page rely rest
5774 | ("page", v) :: rest -> fold title v rely rest
5775 | ("rely", v) :: rest -> fold title page v rest
5776 | _ :: rest -> fold title page rely rest
5777 | [] -> title, page, rely
5779 fold "invalid" "0" "0" attrs
5782 let doc_of attrs =
5783 let rec fold path page rely pan = function
5784 | ("path", v) :: rest -> fold v page rely pan rest
5785 | ("page", v) :: rest -> fold path v rely pan rest
5786 | ("rely", v) :: rest -> fold path page v pan rest
5787 | ("pan", v) :: rest -> fold path page rely v rest
5788 | _ :: rest -> fold path page rely pan rest
5789 | [] -> path, page, rely, pan
5791 fold "" "0" "0" "0" attrs
5794 let map_of attrs =
5795 let rec fold rs ls = function
5796 | ("out", v) :: rest -> fold v ls rest
5797 | ("in", v) :: rest -> fold rs v rest
5798 | _ :: rest -> fold ls rs rest
5799 | [] -> ls, rs
5801 fold "" "" attrs
5804 let setconf dst src =
5805 dst.scrollbw <- src.scrollbw;
5806 dst.scrollh <- src.scrollh;
5807 dst.icase <- src.icase;
5808 dst.preload <- src.preload;
5809 dst.pagebias <- src.pagebias;
5810 dst.verbose <- src.verbose;
5811 dst.scrollstep <- src.scrollstep;
5812 dst.maxhfit <- src.maxhfit;
5813 dst.crophack <- src.crophack;
5814 dst.autoscrollstep <- src.autoscrollstep;
5815 dst.maxwait <- src.maxwait;
5816 dst.hlinks <- src.hlinks;
5817 dst.underinfo <- src.underinfo;
5818 dst.interpagespace <- src.interpagespace;
5819 dst.zoom <- src.zoom;
5820 dst.presentation <- src.presentation;
5821 dst.angle <- src.angle;
5822 dst.winw <- src.winw;
5823 dst.winh <- src.winh;
5824 dst.savebmarks <- src.savebmarks;
5825 dst.memlimit <- src.memlimit;
5826 dst.proportional <- src.proportional;
5827 dst.texcount <- src.texcount;
5828 dst.sliceheight <- src.sliceheight;
5829 dst.thumbw <- src.thumbw;
5830 dst.jumpback <- src.jumpback;
5831 dst.bgcolor <- src.bgcolor;
5832 dst.scrollbarinpm <- src.scrollbarinpm;
5833 dst.tilew <- src.tilew;
5834 dst.tileh <- src.tileh;
5835 dst.mustoresize <- src.mustoresize;
5836 dst.checkers <- src.checkers;
5837 dst.aalevel <- src.aalevel;
5838 dst.trimmargins <- src.trimmargins;
5839 dst.trimfuzz <- src.trimfuzz;
5840 dst.urilauncher <- src.urilauncher;
5841 dst.colorspace <- src.colorspace;
5842 dst.invert <- src.invert;
5843 dst.colorscale <- src.colorscale;
5844 dst.redirectstderr <- src.redirectstderr;
5845 dst.ghyllscroll <- src.ghyllscroll;
5846 dst.columns <- src.columns;
5847 dst.beyecolumns <- src.beyecolumns;
5848 dst.selcmd <- src.selcmd;
5849 dst.updatecurs <- src.updatecurs;
5850 dst.pathlauncher <- src.pathlauncher;
5851 dst.keyhashes <- copykeyhashes src;
5854 let get s =
5855 let h = Hashtbl.create 10 in
5856 let dc = { defconf with angle = defconf.angle } in
5857 let rec toplevel v t spos _ =
5858 match t with
5859 | Vdata | Vcdata | Vend -> v
5860 | Vopen ("llppconfig", _, closed) ->
5861 if closed
5862 then v
5863 else { v with f = llppconfig }
5864 | Vopen _ ->
5865 error "unexpected subelement at top level" s spos
5866 | Vclose _ -> error "unexpected close at top level" s spos
5868 and llppconfig v t spos _ =
5869 match t with
5870 | Vdata | Vcdata -> v
5871 | Vend -> error "unexpected end of input in llppconfig" s spos
5872 | Vopen ("defaults", attrs, closed) ->
5873 let c = config_of dc attrs in
5874 setconf dc c;
5875 if closed
5876 then v
5877 else { v with f = defaults }
5879 | Vopen ("ui-font", attrs, closed) ->
5880 let rec getsize size = function
5881 | [] -> size
5882 | ("size", v) :: rest ->
5883 let size =
5884 fromstring int_of_string spos "size" v fstate.fontsize in
5885 getsize size rest
5886 | l -> getsize size l
5888 fstate.fontsize <- getsize fstate.fontsize attrs;
5889 if closed
5890 then v
5891 else { v with f = uifont (Buffer.create 10) }
5893 | Vopen ("doc", attrs, closed) ->
5894 let pathent, spage, srely, span = doc_of attrs in
5895 let path = unent pathent
5896 and pageno = fromstring int_of_string spos "page" spage 0
5897 and rely = fromstring float_of_string spos "rely" srely 0.0
5898 and pan = fromstring int_of_string spos "pan" span 0 in
5899 let c = config_of dc attrs in
5900 let anchor = (pageno, rely) in
5901 if closed
5902 then (Hashtbl.add h path (c, [], pan, anchor); v)
5903 else { v with f = doc path pan anchor c [] }
5905 | Vopen _ ->
5906 error "unexpected subelement in llppconfig" s spos
5908 | Vclose "llppconfig" -> { v with f = toplevel }
5909 | Vclose _ -> error "unexpected close in llppconfig" s spos
5911 and defaults v t spos _ =
5912 match t with
5913 | Vdata | Vcdata -> v
5914 | Vend -> error "unexpected end of input in defaults" s spos
5915 | Vopen ("keymap", attrs, closed) ->
5916 let modename =
5917 try List.assoc "mode" attrs
5918 with Not_found -> "global" in
5919 if closed
5920 then v
5921 else
5922 let ret keymap =
5923 let h = findkeyhash dc modename in
5924 KeyMap.iter (Hashtbl.replace h) keymap;
5925 defaults
5927 { v with f = pkeymap ret KeyMap.empty }
5929 | Vopen (_, _, _) ->
5930 error "unexpected subelement in defaults" s spos
5932 | Vclose "defaults" ->
5933 { v with f = llppconfig }
5935 | Vclose _ -> error "unexpected close in defaults" s spos
5937 and uifont b v t spos epos =
5938 match t with
5939 | Vdata | Vcdata ->
5940 Buffer.add_substring b s spos (epos - spos);
5942 | Vopen (_, _, _) ->
5943 error "unexpected subelement in ui-font" s spos
5944 | Vclose "ui-font" ->
5945 if String.length !fontpath = 0
5946 then fontpath := Buffer.contents b;
5947 { v with f = llppconfig }
5948 | Vclose _ -> error "unexpected close in ui-font" s spos
5949 | Vend -> error "unexpected end of input in ui-font" s spos
5951 and doc path pan anchor c bookmarks v t spos _ =
5952 match t with
5953 | Vdata | Vcdata -> v
5954 | Vend -> error "unexpected end of input in doc" s spos
5955 | Vopen ("bookmarks", _, closed) ->
5956 if closed
5957 then v
5958 else { v with f = pbookmarks path pan anchor c bookmarks }
5960 | Vopen ("keymap", attrs, closed) ->
5961 let modename =
5962 try List.assoc "mode" attrs
5963 with Not_found -> "global"
5965 if closed
5966 then v
5967 else
5968 let ret keymap =
5969 let h = findkeyhash c modename in
5970 KeyMap.iter (Hashtbl.replace h) keymap;
5971 doc path pan anchor c bookmarks
5973 { v with f = pkeymap ret KeyMap.empty }
5975 | Vopen (_, _, _) ->
5976 error "unexpected subelement in doc" s spos
5978 | Vclose "doc" ->
5979 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5980 { v with f = llppconfig }
5982 | Vclose _ -> error "unexpected close in doc" s spos
5984 and pkeymap ret keymap v t spos _ =
5985 match t with
5986 | Vdata | Vcdata -> v
5987 | Vend -> error "unexpected end of input in keymap" s spos
5988 | Vopen ("map", attrs, closed) ->
5989 let r, l = map_of attrs in
5990 let kss = fromstring keys_of_string spos "in" r [] in
5991 let lss = fromstring keys_of_string spos "out" l [] in
5992 let keymap =
5993 match kss with
5994 | [] -> keymap
5995 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5996 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5998 if closed
5999 then { v with f = pkeymap ret keymap }
6000 else
6001 let f () = v in
6002 { v with f = skip "map" f }
6004 | Vopen _ ->
6005 error "unexpected subelement in keymap" s spos
6007 | Vclose "keymap" ->
6008 { v with f = ret keymap }
6010 | Vclose _ -> error "unexpected close in keymap" s spos
6012 and pbookmarks path pan anchor c bookmarks v t spos _ =
6013 match t with
6014 | Vdata | Vcdata -> v
6015 | Vend -> error "unexpected end of input in bookmarks" s spos
6016 | Vopen ("item", attrs, closed) ->
6017 let titleent, spage, srely = bookmark_of attrs in
6018 let page = fromstring int_of_string spos "page" spage 0
6019 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6020 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6021 if closed
6022 then { v with f = pbookmarks path pan anchor c bookmarks }
6023 else
6024 let f () = v in
6025 { v with f = skip "item" f }
6027 | Vopen _ ->
6028 error "unexpected subelement in bookmarks" s spos
6030 | Vclose "bookmarks" ->
6031 { v with f = doc path pan anchor c bookmarks }
6033 | Vclose _ -> error "unexpected close in bookmarks" s spos
6035 and skip tag f v t spos _ =
6036 match t with
6037 | Vdata | Vcdata -> v
6038 | Vend ->
6039 error ("unexpected end of input in skipped " ^ tag) s spos
6040 | Vopen (tag', _, closed) ->
6041 if closed
6042 then v
6043 else
6044 let f' () = { v with f = skip tag f } in
6045 { v with f = skip tag' f' }
6046 | Vclose ctag ->
6047 if tag = ctag
6048 then f ()
6049 else error ("unexpected close in skipped " ^ tag) s spos
6052 parse { f = toplevel; accu = () } s;
6053 h, dc;
6056 let do_load f ic =
6058 let len = in_channel_length ic in
6059 let s = String.create len in
6060 really_input ic s 0 len;
6061 f s;
6062 with
6063 | Parse_error (msg, s, pos) ->
6064 let subs = subs s pos in
6065 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6066 failwith ("parse error: " ^ s)
6068 | exn ->
6069 failwith ("config load error: " ^ Printexc.to_string exn)
6072 let defconfpath =
6073 let dir =
6075 let dir = Filename.concat home ".config" in
6076 if Sys.is_directory dir then dir else home
6077 with _ -> home
6079 Filename.concat dir "llpp.conf"
6082 let confpath = ref defconfpath;;
6084 let load1 f =
6085 if Sys.file_exists !confpath
6086 then
6087 match
6088 (try Some (open_in_bin !confpath)
6089 with exn ->
6090 prerr_endline
6091 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6092 Printexc.to_string exn);
6093 None
6095 with
6096 | Some ic ->
6097 begin try
6098 f (do_load get ic)
6099 with exn ->
6100 prerr_endline
6101 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6102 Printexc.to_string exn);
6103 end;
6104 close_in ic;
6106 | None -> ()
6107 else
6108 f (Hashtbl.create 0, defconf)
6111 let load () =
6112 let f (h, dc) =
6113 let pc, pb, px, pa =
6115 Hashtbl.find h (Filename.basename state.path)
6116 with Not_found -> dc, [], 0, (0, 0.0)
6118 setconf defconf dc;
6119 setconf conf pc;
6120 state.bookmarks <- pb;
6121 state.x <- px;
6122 state.scrollw <- conf.scrollbw;
6123 if conf.jumpback
6124 then state.anchor <- pa;
6125 cbput state.hists.nav pa;
6127 load1 f
6130 let add_attrs bb always dc c =
6131 let ob s a b =
6132 if always || a != b
6133 then Printf.bprintf bb "\n %s='%b'" s a
6134 and oi s a b =
6135 if always || a != b
6136 then Printf.bprintf bb "\n %s='%d'" s a
6137 and oI s a b =
6138 if always || a != b
6139 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6140 and oz s a b =
6141 if always || a <> b
6142 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6143 and oF s a b =
6144 if always || a <> b
6145 then Printf.bprintf bb "\n %s='%f'" s a
6146 and oc s a b =
6147 if always || a <> b
6148 then
6149 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6150 and oC s a b =
6151 if always || a <> b
6152 then
6153 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6154 and oR s a b =
6155 if always || a <> b
6156 then
6157 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6158 and os s a b =
6159 if always || a <> b
6160 then
6161 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6162 and og s a b =
6163 if always || a <> b
6164 then
6165 match a with
6166 | None -> ()
6167 | Some (_N, _A, _B) ->
6168 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6169 and oW s a b =
6170 if always || a <> b
6171 then
6172 let v =
6173 match a with
6174 | None -> "false"
6175 | Some f ->
6176 if f = infinity
6177 then "true"
6178 else string_of_float f
6180 Printf.bprintf bb "\n %s='%s'" s v
6181 and oco s a b =
6182 if always || a <> b
6183 then
6184 match a with
6185 | Cmulti ((n, a, b), _) when n > 1 ->
6186 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6187 | Csplit (n, _) when n > 1 ->
6188 Printf.bprintf bb "\n %s='%d'" s ~-n
6189 | _ -> ()
6190 and obeco s a b =
6191 if always || a <> b
6192 then
6193 match a with
6194 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6195 | _ -> ()
6197 let w, h =
6198 if always
6199 then dc.winw, dc.winh
6200 else
6201 match state.fullscreen with
6202 | Some wh -> wh
6203 | None -> c.winw, c.winh
6205 let zoom, presentation, interpagespace, maxwait =
6206 if always
6207 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6208 else
6209 match state.mode with
6210 | Birdseye (bc, _, _, _, _) ->
6211 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6212 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6214 oi "width" w dc.winw;
6215 oi "height" h dc.winh;
6216 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6217 oi "scroll-handle-height" c.scrollh dc.scrollh;
6218 ob "case-insensitive-search" c.icase dc.icase;
6219 ob "preload" c.preload dc.preload;
6220 oi "page-bias" c.pagebias dc.pagebias;
6221 oi "scroll-step" c.scrollstep dc.scrollstep;
6222 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6223 ob "max-height-fit" c.maxhfit dc.maxhfit;
6224 ob "crop-hack" c.crophack dc.crophack;
6225 oW "throttle" maxwait dc.maxwait;
6226 ob "highlight-links" c.hlinks dc.hlinks;
6227 ob "under-cursor-info" c.underinfo dc.underinfo;
6228 oi "vertical-margin" interpagespace dc.interpagespace;
6229 oz "zoom" zoom dc.zoom;
6230 ob "presentation" presentation dc.presentation;
6231 oi "rotation-angle" c.angle dc.angle;
6232 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6233 ob "proportional-display" c.proportional dc.proportional;
6234 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6235 oi "tex-count" c.texcount dc.texcount;
6236 oi "slice-height" c.sliceheight dc.sliceheight;
6237 oi "thumbnail-width" c.thumbw dc.thumbw;
6238 ob "persistent-location" c.jumpback dc.jumpback;
6239 oc "background-color" c.bgcolor dc.bgcolor;
6240 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6241 oi "tile-width" c.tilew dc.tilew;
6242 oi "tile-height" c.tileh dc.tileh;
6243 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6244 ob "checkers" c.checkers dc.checkers;
6245 oi "aalevel" c.aalevel dc.aalevel;
6246 ob "trim-margins" c.trimmargins dc.trimmargins;
6247 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6248 os "uri-launcher" c.urilauncher dc.urilauncher;
6249 os "path-launcher" c.pathlauncher dc.pathlauncher;
6250 oC "color-space" c.colorspace dc.colorspace;
6251 ob "invert-colors" c.invert dc.invert;
6252 oF "brightness" c.colorscale dc.colorscale;
6253 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6254 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6255 oco "columns" c.columns dc.columns;
6256 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6257 os "selection-command" c.selcmd dc.selcmd;
6258 ob "update-cursor" c.updatecurs dc.updatecurs;
6261 let keymapsbuf always dc c =
6262 let bb = Buffer.create 16 in
6263 let rec loop = function
6264 | [] -> ()
6265 | (modename, h) :: rest ->
6266 let dh = findkeyhash dc modename in
6267 if always || h <> dh
6268 then (
6269 if Hashtbl.length h > 0
6270 then (
6271 if Buffer.length bb > 0
6272 then Buffer.add_char bb '\n';
6273 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6274 Hashtbl.iter (fun i o ->
6275 let isdifferent = always ||
6277 let dO = Hashtbl.find dh i in
6278 dO <> o
6279 with Not_found -> true
6281 if isdifferent
6282 then
6283 let addkm (k, m) =
6284 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6285 if Wsi.withalt m then Buffer.add_string bb "alt-";
6286 if Wsi.withshift m then Buffer.add_string bb "shift-";
6287 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6288 Buffer.add_string bb (Wsi.keyname k);
6290 let addkms l =
6291 let rec loop = function
6292 | [] -> ()
6293 | km :: [] -> addkm km
6294 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6296 loop l
6298 Buffer.add_string bb "<map in='";
6299 addkm i;
6300 match o with
6301 | KMinsrt km ->
6302 Buffer.add_string bb "' out='";
6303 addkm km;
6304 Buffer.add_string bb "'/>\n"
6306 | KMinsrl kms ->
6307 Buffer.add_string bb "' out='";
6308 addkms kms;
6309 Buffer.add_string bb "'/>\n"
6311 | KMmulti (ins, kms) ->
6312 Buffer.add_char bb ' ';
6313 addkms ins;
6314 Buffer.add_string bb "' out='";
6315 addkms kms;
6316 Buffer.add_string bb "'/>\n"
6317 ) h;
6318 Buffer.add_string bb "</keymap>";
6321 loop rest
6323 loop c.keyhashes;
6327 let save () =
6328 let uifontsize = fstate.fontsize in
6329 let bb = Buffer.create 32768 in
6330 let f (h, dc) =
6331 let dc = if conf.bedefault then conf else dc in
6332 Buffer.add_string bb "<llppconfig>\n";
6334 if String.length !fontpath > 0
6335 then
6336 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6337 uifontsize
6338 !fontpath
6339 else (
6340 if uifontsize <> 14
6341 then
6342 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6345 Buffer.add_string bb "<defaults ";
6346 add_attrs bb true dc dc;
6347 let kb = keymapsbuf true dc dc in
6348 if Buffer.length kb > 0
6349 then (
6350 Buffer.add_string bb ">\n";
6351 Buffer.add_buffer bb kb;
6352 Buffer.add_string bb "\n</defaults>\n";
6354 else Buffer.add_string bb "/>\n";
6356 let adddoc path pan anchor c bookmarks =
6357 if bookmarks == [] && c = dc && anchor = emptyanchor
6358 then ()
6359 else (
6360 Printf.bprintf bb "<doc path='%s'"
6361 (enent path 0 (String.length path));
6363 if anchor <> emptyanchor
6364 then (
6365 let n, y = anchor in
6366 Printf.bprintf bb " page='%d'" n;
6367 if y > 1e-6
6368 then
6369 Printf.bprintf bb " rely='%f'" y
6373 if pan != 0
6374 then Printf.bprintf bb " pan='%d'" pan;
6376 add_attrs bb false dc c;
6377 let kb = keymapsbuf false dc c in
6379 begin match bookmarks with
6380 | [] ->
6381 if Buffer.length kb > 0
6382 then (
6383 Buffer.add_string bb ">\n";
6384 Buffer.add_buffer bb kb;
6385 Buffer.add_string bb "</doc>\n";
6387 else Buffer.add_string bb "/>\n"
6388 | _ ->
6389 Buffer.add_string bb ">\n<bookmarks>\n";
6390 List.iter (fun (title, _level, (page, rely)) ->
6391 Printf.bprintf bb
6392 "<item title='%s' page='%d'"
6393 (enent title 0 (String.length title))
6394 page
6396 if rely > 1e-6
6397 then
6398 Printf.bprintf bb " rely='%f'" rely
6400 Buffer.add_string bb "/>\n";
6401 ) bookmarks;
6402 Buffer.add_string bb "</bookmarks>";
6403 if Buffer.length kb > 0
6404 then (
6405 Buffer.add_string bb "\n";
6406 Buffer.add_buffer bb kb;
6408 Buffer.add_string bb "\n</doc>\n";
6409 end;
6413 let pan, conf =
6414 match state.mode with
6415 | Birdseye (c, pan, _, _, _) ->
6416 let beyecolumns =
6417 match conf.columns with
6418 | Cmulti ((c, _, _), _) -> Some c
6419 | Csingle -> None
6420 | Csplit _ -> None
6421 and columns =
6422 match c.columns with
6423 | Cmulti (c, _) -> Cmulti (c, [||])
6424 | Csingle -> Csingle
6425 | Csplit _ -> failwith "quit from bird's eye while split"
6427 pan, { c with beyecolumns = beyecolumns; columns = columns }
6428 | _ -> state.x, conf
6430 let basename = Filename.basename state.path in
6431 adddoc basename pan (getanchor ())
6432 { conf with
6433 autoscrollstep =
6434 match state.autoscroll with
6435 | Some step -> step
6436 | None -> conf.autoscrollstep }
6437 (if conf.savebmarks then state.bookmarks else []);
6439 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6440 if basename <> path
6441 then adddoc path x y c bookmarks
6442 ) h;
6443 Buffer.add_string bb "</llppconfig>";
6445 load1 f;
6446 if Buffer.length bb > 0
6447 then
6449 let tmp = !confpath ^ ".tmp" in
6450 let oc = open_out_bin tmp in
6451 Buffer.output_buffer oc bb;
6452 close_out oc;
6453 Unix.rename tmp !confpath;
6454 with exn ->
6455 prerr_endline
6456 ("error while saving configuration: " ^ Printexc.to_string exn)
6458 end;;
6460 let () =
6461 Arg.parse
6462 (Arg.align
6463 [("-p", Arg.String (fun s -> state.password <- s) ,
6464 "<password> Set password");
6466 ("-f", Arg.String (fun s -> Config.fontpath := s),
6467 "<path> Set path to the user interface font");
6469 ("-c", Arg.String (fun s -> Config.confpath := s),
6470 "<path> Set path to the configuration file");
6472 ("-v", Arg.Unit (fun () ->
6473 Printf.printf
6474 "%s\nconfiguration path: %s\n"
6475 (version ())
6476 Config.defconfpath
6478 exit 0), " Print version and exit");
6481 (fun s -> state.path <- s)
6482 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6484 if String.length state.path = 0
6485 then (prerr_endline "file name missing"; exit 1);
6487 Config.load ();
6489 let globalkeyhash = findkeyhash conf "global" in
6490 let wsfd, winw, winh = Wsi.init (object
6491 method expose =
6492 if nogeomcmds state.geomcmds
6493 then display ()
6494 method display = display ()
6495 method reshape w h = reshape w h
6496 method mouse b d x y m = mouse b d x y m
6497 method motion x y = state.mpos <- (x, y); motion x y
6498 method pmotion x y = state.mpos <- (x, y); pmotion x y
6499 method key k m =
6500 let mascm = m land (
6501 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6502 ) in
6503 match state.keystate with
6504 | KSnone ->
6505 let km = k, mascm in
6506 begin
6507 match
6508 try Hashtbl.find globalkeyhash km
6509 with Not_found ->
6510 let modehash = state.uioh#modehash in
6511 try Hashtbl.find modehash km
6512 with Not_found -> KMinsrt (k, m)
6513 with
6514 | KMinsrt (k, m) -> keyboard k m
6515 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6516 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6518 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6519 List.iter (fun (k, m) -> keyboard k m) insrt;
6520 state.keystate <- KSnone
6521 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6522 state.keystate <- KSinto (keys, insrt)
6523 | _ ->
6524 state.keystate <- KSnone
6526 method enter x y = state.mpos <- (x, y); pmotion x y
6527 method leave = state.mpos <- (-1, -1)
6528 method quit = raise Quit
6529 end) conf.winw conf.winh (platform = Posx) in
6531 state.wsfd <- wsfd;
6533 if not (
6534 List.exists GlMisc.check_extension
6535 [ "GL_ARB_texture_rectangle"
6536 ; "GL_EXT_texture_recangle"
6537 ; "GL_NV_texture_rectangle" ]
6539 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6541 let cr, sw = Unix.pipe ()
6542 and sr, cw = Unix.pipe () in
6544 cloexec cr;
6545 cloexec sw;
6546 cloexec sr;
6547 cloexec cw;
6549 setcheckers conf.checkers;
6550 redirectstderr ();
6552 init (cr, cw) (
6553 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6554 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6555 !Config.fontpath
6557 state.sr <- sr;
6558 state.sw <- sw;
6559 state.text <- "Opening " ^ state.path;
6560 reshape winw winh;
6561 opendoc state.path state.password;
6562 state.uioh <- uioh;
6564 let rec loop deadline =
6565 let r =
6566 match state.errfd with
6567 | None -> [state.sr; state.wsfd]
6568 | Some fd -> [state.sr; state.wsfd; fd]
6570 if state.redisplay
6571 then (
6572 state.redisplay <- false;
6573 display ();
6575 let timeout =
6576 let now = now () in
6577 if deadline > now
6578 then (
6579 if deadline = infinity
6580 then ~-.1.0
6581 else max 0.0 (deadline -. now)
6583 else 0.0
6585 let r, _, _ =
6586 try Unix.select r [] [] timeout
6587 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6589 begin match r with
6590 | [] ->
6591 state.ghyll None;
6592 let newdeadline =
6593 if state.ghyll == noghyll
6594 then
6595 match state.autoscroll with
6596 | Some step when step != 0 ->
6597 let y = state.y + step in
6598 let y =
6599 if y < 0
6600 then state.maxy
6601 else if y >= state.maxy then 0 else y
6603 gotoy y;
6604 if state.mode = View
6605 then state.text <- "";
6606 deadline +. 0.01
6607 | _ -> infinity
6608 else deadline +. 0.01
6610 loop newdeadline
6612 | l ->
6613 let rec checkfds = function
6614 | [] -> ()
6615 | fd :: rest when fd = state.sr ->
6616 let cmd = readcmd state.sr in
6617 act cmd;
6618 checkfds rest
6620 | fd :: rest when fd = state.wsfd ->
6621 Wsi.readresp fd;
6622 checkfds rest
6624 | fd :: rest ->
6625 let s = String.create 80 in
6626 let n = Unix.read fd s 0 80 in
6627 if conf.redirectstderr
6628 then (
6629 Buffer.add_substring state.errmsgs s 0 n;
6630 state.newerrmsgs <- true;
6631 state.redisplay <- true;
6633 else (
6634 prerr_string (String.sub s 0 n);
6635 flush stderr;
6637 checkfds rest
6639 checkfds l;
6640 let newdeadline =
6641 let deadline1 =
6642 if deadline = infinity
6643 then now () +. 0.01
6644 else deadline
6646 match state.autoscroll with
6647 | Some step when step != 0 -> deadline1
6648 | _ -> if state.ghyll == noghyll then infinity else deadline1
6650 loop newdeadline
6651 end;
6654 loop infinity;
6655 with Quit ->
6656 Config.save ();
6657 exit 0;