Counter productive, link numbers are unstable
[llpp.git] / main.ml
blob9d86b356805b41b34c11dc8b2dca6253fd43eb3b
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 columns =
252 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
253 and multicol = columncount * covercount * covercount
254 and pdimno = int
255 and columncount = int
256 and covercount = int;;
258 type conf =
259 { mutable scrollbw : int
260 ; mutable scrollh : int
261 ; mutable icase : bool
262 ; mutable preload : bool
263 ; mutable pagebias : int
264 ; mutable verbose : bool
265 ; mutable debug : bool
266 ; mutable scrollstep : int
267 ; mutable maxhfit : bool
268 ; mutable crophack : bool
269 ; mutable autoscrollstep : int
270 ; mutable maxwait : float option
271 ; mutable hlinks : bool
272 ; mutable underinfo : bool
273 ; mutable interpagespace : interpagespace
274 ; mutable zoom : float
275 ; mutable presentation : bool
276 ; mutable angle : angle
277 ; mutable winw : int
278 ; mutable winh : int
279 ; mutable savebmarks : bool
280 ; mutable proportional : proportional
281 ; mutable trimmargins : trimmargins
282 ; mutable trimfuzz : irect
283 ; mutable memlimit : memsize
284 ; mutable texcount : texcount
285 ; mutable sliceheight : sliceheight
286 ; mutable thumbw : width
287 ; mutable jumpback : bool
288 ; mutable bgcolor : float * float * float
289 ; mutable bedefault : bool
290 ; mutable scrollbarinpm : bool
291 ; mutable tilew : int
292 ; mutable tileh : int
293 ; mutable mustoresize : memsize
294 ; mutable checkers : bool
295 ; mutable aalevel : int
296 ; mutable urilauncher : string
297 ; mutable pathlauncher : string
298 ; mutable colorspace : colorspace
299 ; mutable invert : bool
300 ; mutable colorscale : float
301 ; mutable redirectstderr : bool
302 ; mutable ghyllscroll : (int * int * int) option
303 ; mutable columns : columns option
304 ; mutable beyecolumns : columncount option
305 ; mutable selcmd : string
306 ; mutable updatecurs : bool
307 ; mutable keyhashes : (string * keyhash) list
311 type anchor = pageno * top;;
313 type outline = string * int * anchor;;
315 type rect = float * float * float * float * float * float * float * float;;
317 type tile = opaque * pixmapsize * elapsed
318 and elapsed = float;;
319 type pagemapkey = pageno * gen;;
320 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
321 and row = int
322 and col = int;;
324 let emptyanchor = (0, 0.0);;
326 type infochange = | Memused | Docinfo | Pdim;;
328 class type uioh = object
329 method display : unit
330 method key : int -> int -> uioh
331 method button : int -> bool -> int -> int -> int -> uioh
332 method motion : int -> int -> uioh
333 method pmotion : int -> int -> uioh
334 method infochanged : infochange -> unit
335 method scrollpw : (int * float * float)
336 method scrollph : (int * float * float)
337 method modehash : keyhash
338 end;;
340 type mode =
341 | Birdseye of (conf * leftx * pageno * pageno * anchor)
342 | Textentry of (textentry * onleave)
343 | View
344 | LinkNav of linktarget
345 and onleave = leavetextentrystatus -> unit
346 and leavetextentrystatus = | Cancel | Confirm
347 and helpitem = string * int * action
348 and action =
349 | Noaction
350 | Action of (uioh -> uioh)
351 and linktarget =
352 | Ltexact of (pageno * int)
353 | Ltgendir of int
356 let isbirdseye = function Birdseye _ -> true | _ -> false;;
357 let istextentry = function Textentry _ -> true | _ -> false;;
359 type currently =
360 | Idle
361 | Loading of (page * gen)
362 | Tiling of (
363 page * opaque * colorspace * angle * gen * col * row * width * height
365 | Outlining of outline list
368 let emptykeyhash = Hashtbl.create 0;;
369 let nouioh : uioh = object (self)
370 method display = ()
371 method key _ _ = self
372 method button _ _ _ _ _ = self
373 method motion _ _ = self
374 method pmotion _ _ = self
375 method infochanged _ = ()
376 method scrollpw = (0, nan, nan)
377 method scrollph = (0, nan, nan)
378 method modehash = emptykeyhash
379 end;;
381 type state =
382 { mutable sr : Unix.file_descr
383 ; mutable sw : Unix.file_descr
384 ; mutable wsfd : Unix.file_descr
385 ; mutable errfd : Unix.file_descr option
386 ; mutable stderr : Unix.file_descr
387 ; mutable errmsgs : Buffer.t
388 ; mutable newerrmsgs : bool
389 ; mutable w : int
390 ; mutable x : int
391 ; mutable y : int
392 ; mutable scrollw : int
393 ; mutable hscrollh : int
394 ; mutable anchor : anchor
395 ; mutable ranchors : (string * string * anchor) list
396 ; mutable maxy : int
397 ; mutable layout : page list
398 ; pagemap : (pagemapkey, opaque) Hashtbl.t
399 ; tilemap : (tilemapkey, tile) Hashtbl.t
400 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
401 ; mutable pdims : (pageno * width * height * leftx) list
402 ; mutable pagecount : int
403 ; mutable currently : currently
404 ; mutable mstate : mstate
405 ; mutable searchpattern : string
406 ; mutable rects : (pageno * recttype * rect) list
407 ; mutable rects1 : (pageno * recttype * rect) list
408 ; mutable text : string
409 ; mutable fullscreen : (width * height) option
410 ; mutable mode : mode
411 ; mutable uioh : uioh
412 ; mutable outlines : outline array
413 ; mutable bookmarks : outline list
414 ; mutable path : string
415 ; mutable password : string
416 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
417 ; mutable memused : memsize
418 ; mutable gen : gen
419 ; mutable throttle : (page list * int * float) option
420 ; mutable autoscroll : int option
421 ; mutable ghyll : (int option -> unit)
422 ; mutable help : helpitem array
423 ; mutable docinfo : (int * string) list
424 ; mutable texid : GlTex.texture_id option
425 ; hists : hists
426 ; mutable prevzoom : float
427 ; mutable progress : float
428 ; mutable redisplay : bool
429 ; mutable mpos : mpos
430 ; mutable keystate : keystate
431 ; mutable glinks : bool
433 and hists =
434 { pat : string circbuf
435 ; pag : string circbuf
436 ; nav : anchor circbuf
437 ; sel : string circbuf
441 let defconf =
442 { scrollbw = 7
443 ; scrollh = 12
444 ; icase = true
445 ; preload = true
446 ; pagebias = 0
447 ; verbose = false
448 ; debug = false
449 ; scrollstep = 24
450 ; maxhfit = true
451 ; crophack = false
452 ; autoscrollstep = 2
453 ; maxwait = None
454 ; hlinks = false
455 ; underinfo = false
456 ; interpagespace = 2
457 ; zoom = 1.0
458 ; presentation = false
459 ; angle = 0
460 ; winw = 900
461 ; winh = 900
462 ; savebmarks = true
463 ; proportional = true
464 ; trimmargins = false
465 ; trimfuzz = (0,0,0,0)
466 ; memlimit = 32 lsl 20
467 ; texcount = 256
468 ; sliceheight = 24
469 ; thumbw = 76
470 ; jumpback = true
471 ; bgcolor = (0.5, 0.5, 0.5)
472 ; bedefault = false
473 ; scrollbarinpm = true
474 ; tilew = 2048
475 ; tileh = 2048
476 ; mustoresize = 128 lsl 20
477 ; checkers = true
478 ; aalevel = 8
479 ; urilauncher =
480 (match platform with
481 | Plinux | Pfreebsd | Pdragonflybsd
482 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
483 | Posx -> "open \"%s\""
484 | Pcygwin -> "cygstart %s"
485 | Punknown -> "echo %s")
486 ; pathlauncher = "lp \"%s\""
487 ; selcmd =
488 (match platform with
489 | Plinux | Pfreebsd | Pdragonflybsd
490 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
491 | Posx -> "pbcopy"
492 | Pcygwin -> "wsel"
493 | Punknown -> "cat")
494 ; colorspace = Rgb
495 ; invert = false
496 ; colorscale = 1.0
497 ; redirectstderr = false
498 ; ghyllscroll = None
499 ; columns = None
500 ; beyecolumns = None
501 ; updatecurs = false
502 ; keyhashes =
503 let mk n = (n, Hashtbl.create 1) in
504 [ mk "global"
505 ; mk "info"
506 ; mk "help"
507 ; mk "outline"
508 ; mk "listview"
509 ; mk "birdseye"
510 ; mk "textentry"
511 ; mk "links"
516 let findkeyhash c name =
517 try List.assoc name c.keyhashes
518 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
521 let conf = { defconf with angle = defconf.angle };;
523 type fontstate =
524 { mutable fontsize : int
525 ; mutable wwidth : float
526 ; mutable maxrows : int
530 let fstate =
531 { fontsize = 14
532 ; wwidth = nan
533 ; maxrows = -1
537 let setfontsize n =
538 fstate.fontsize <- n;
539 fstate.wwidth <- measurestr fstate.fontsize "w";
540 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
543 let geturl s =
544 let colonpos = try String.index s ':' with Not_found -> -1 in
545 let len = String.length s in
546 if colonpos >= 0 && colonpos + 3 < len
547 then (
548 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
549 then
550 let schemestartpos =
551 try String.rindex_from s colonpos ' '
552 with Not_found -> -1
554 let scheme =
555 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
557 match scheme with
558 | "http" | "ftp" | "mailto" ->
559 let epos =
560 try String.index_from s colonpos ' '
561 with Not_found -> len
563 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
564 | _ -> ""
565 else ""
567 else ""
570 let popen =
571 let shell, farg = "/bin/sh", "-c" in
572 fun s ->
573 let args = [|shell; farg; s|] in
574 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
577 let gotouri uri =
578 if String.length conf.urilauncher = 0
579 then print_endline uri
580 else (
581 let url = geturl uri in
582 if String.length url = 0
583 then print_endline uri
584 else
585 let re = Str.regexp "%s" in
586 let command = Str.global_replace re url conf.urilauncher in
587 try popen command
588 with exn ->
589 Printf.eprintf
590 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
591 flush stderr;
595 let version () =
596 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
597 (platform_to_string platform) Sys.word_size Sys.ocaml_version
600 let makehelp () =
601 let strings = version () :: "" :: Help.keys in
602 Array.of_list (
603 List.map (fun s ->
604 let url = geturl s in
605 if String.length url > 0
606 then (s, 0, Action (fun u -> gotouri url; u))
607 else (s, 0, Noaction)
608 ) strings);
611 let noghyll _ = ();;
612 let firstgeomcmds = "", [];;
614 let state =
615 { sr = Unix.stdin
616 ; sw = Unix.stdin
617 ; wsfd = Unix.stdin
618 ; errfd = None
619 ; stderr = Unix.stderr
620 ; errmsgs = Buffer.create 0
621 ; newerrmsgs = false
622 ; x = 0
623 ; y = 0
624 ; w = 0
625 ; scrollw = 0
626 ; hscrollh = 0
627 ; anchor = emptyanchor
628 ; ranchors = []
629 ; layout = []
630 ; maxy = max_int
631 ; tilelru = Queue.create ()
632 ; pagemap = Hashtbl.create 10
633 ; tilemap = Hashtbl.create 10
634 ; pdims = []
635 ; pagecount = 0
636 ; currently = Idle
637 ; mstate = Mnone
638 ; rects = []
639 ; rects1 = []
640 ; text = ""
641 ; mode = View
642 ; fullscreen = None
643 ; searchpattern = ""
644 ; outlines = [||]
645 ; bookmarks = []
646 ; path = ""
647 ; password = ""
648 ; geomcmds = firstgeomcmds
649 ; hists =
650 { nav = cbnew 10 (0, 0.0)
651 ; pat = cbnew 10 ""
652 ; pag = cbnew 10 ""
653 ; sel = cbnew 10 ""
655 ; memused = 0
656 ; gen = 0
657 ; throttle = None
658 ; autoscroll = None
659 ; ghyll = noghyll
660 ; help = makehelp ()
661 ; docinfo = []
662 ; texid = None
663 ; prevzoom = 1.0
664 ; progress = -1.0
665 ; uioh = nouioh
666 ; redisplay = true
667 ; mpos = (-1, -1)
668 ; keystate = KSnone
669 ; glinks = false
673 let vlog fmt =
674 if conf.verbose
675 then
676 Printf.kprintf prerr_endline fmt
677 else
678 Printf.kprintf ignore fmt
681 let launchpath () =
682 if String.length conf.pathlauncher = 0
683 then print_endline state.path
684 else (
685 let re = Str.regexp "%s" in
686 let command = Str.global_replace re state.path conf.pathlauncher in
687 try popen command
688 with exn ->
689 Printf.eprintf
690 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
691 flush stderr;
695 let redirectstderr () =
696 if conf.redirectstderr
697 then
698 let rfd, wfd = Unix.pipe () in
699 state.stderr <- Unix.dup Unix.stderr;
700 state.errfd <- Some rfd;
701 Unix.dup2 wfd Unix.stderr;
702 else (
703 state.newerrmsgs <- false;
704 begin match state.errfd with
705 | Some fd ->
706 Unix.close fd;
707 Unix.dup2 state.stderr Unix.stderr;
708 state.errfd <- None;
709 | None -> ()
710 end;
711 prerr_string (Buffer.contents state.errmsgs);
712 flush stderr;
713 Buffer.clear state.errmsgs;
717 module G =
718 struct
719 let postRedisplay who =
720 if conf.verbose
721 then prerr_endline ("redisplay for " ^ who);
722 state.redisplay <- true;
724 end;;
726 let getopaque pageno =
727 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
728 with Not_found -> None
731 let putopaque pageno opaque =
732 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
735 let pagetranslatepoint l x y =
736 let dy = y - l.pagedispy in
737 let y = dy + l.pagey in
738 let dx = x - l.pagedispx in
739 let x = dx + l.pagex in
740 (x, y);
743 let getunder x y =
744 let rec f = function
745 | l :: rest ->
746 begin match getopaque l.pageno with
747 | Some opaque ->
748 let x0 = l.pagedispx in
749 let x1 = x0 + l.pagevw in
750 let y0 = l.pagedispy in
751 let y1 = y0 + l.pagevh in
752 if y >= y0 && y <= y1 && x >= x0 && x <= x1
753 then
754 let px, py = pagetranslatepoint l x y in
755 match whatsunder opaque px py with
756 | Unone -> f rest
757 | under -> under
758 else f rest
759 | _ ->
760 f rest
762 | [] -> Unone
764 f state.layout
767 let showtext c s =
768 state.text <- Printf.sprintf "%c%s" c s;
769 G.postRedisplay "showtext";
772 let updateunder x y =
773 match getunder x y with
774 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
775 | Ulinkuri uri ->
776 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
777 Wsi.setcursor Wsi.CURSOR_INFO
778 | Ulinkgoto (page, _) ->
779 if conf.underinfo
780 then showtext 'p' ("age: " ^ string_of_int (page+1));
781 Wsi.setcursor Wsi.CURSOR_INFO
782 | Utext s ->
783 if conf.underinfo then showtext 'f' ("ont: " ^ s);
784 Wsi.setcursor Wsi.CURSOR_TEXT
785 | Uunexpected s ->
786 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
787 Wsi.setcursor Wsi.CURSOR_INHERIT
788 | Ulaunch s ->
789 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
790 Wsi.setcursor Wsi.CURSOR_INHERIT
791 | Unamed s ->
792 if conf.underinfo then showtext 'n' ("amed: " ^ s);
793 Wsi.setcursor Wsi.CURSOR_INHERIT
794 | Uremote (filename, pageno) ->
795 if conf.underinfo then showtext 'r'
796 (Printf.sprintf "emote: %s (%d)" filename pageno);
797 Wsi.setcursor Wsi.CURSOR_INFO
800 let showlinktype under =
801 if conf.underinfo
802 then
803 match under with
804 | Unone -> ()
805 | Ulinkuri uri ->
806 showtext 'u' ("ri: " ^ uri)
807 | Ulinkgoto (page, _) ->
808 showtext 'p' ("age: " ^ string_of_int (page+1));
809 | Utext s ->
810 showtext 'f' ("ont: " ^ s);
811 | Uunexpected s ->
812 showtext 'u' ("nexpected: " ^ s);
813 | Ulaunch s ->
814 showtext 'l' ("aunch: " ^ s);
815 | Unamed s ->
816 showtext 'n' ("amed: " ^ s);
817 | Uremote (filename, pageno) ->
818 showtext 'r' (Printf.sprintf "emote: %s (%d)" filename pageno);
821 let addchar s c =
822 let b = Buffer.create (String.length s + 1) in
823 Buffer.add_string b s;
824 Buffer.add_char b c;
825 Buffer.contents b;
828 let colorspace_of_string s =
829 match String.lowercase s with
830 | "rgb" -> Rgb
831 | "bgr" -> Bgr
832 | "gray" -> Gray
833 | _ -> failwith "invalid colorspace"
836 let int_of_colorspace = function
837 | Rgb -> 0
838 | Bgr -> 1
839 | Gray -> 2
842 let colorspace_of_int = function
843 | 0 -> Rgb
844 | 1 -> Bgr
845 | 2 -> Gray
846 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
849 let colorspace_to_string = function
850 | Rgb -> "rgb"
851 | Bgr -> "bgr"
852 | Gray -> "gray"
855 let intentry_with_suffix text key =
856 let c =
857 if key >= 32 && key < 127
858 then Char.chr key
859 else '\000'
861 match Char.lowercase c with
862 | '0' .. '9' ->
863 let text = addchar text c in
864 TEcont text
866 | 'k' | 'm' | 'g' ->
867 let text = addchar text c in
868 TEcont text
870 | _ ->
871 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
872 TEcont text
875 let columns_to_string (n, a, b) =
876 if a = 0 && b = 0
877 then Printf.sprintf "%d" n
878 else Printf.sprintf "%d,%d,%d" n a b;
881 let columns_of_string s =
883 (int_of_string s, 0, 0)
884 with _ ->
885 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
888 let readcmd fd =
889 let s = "xxxx" in
890 let n = Unix.read fd s 0 4 in
891 if n != 4 then failwith "incomplete read(len)";
892 let len = 0
893 lor (Char.code s.[0] lsl 24)
894 lor (Char.code s.[1] lsl 16)
895 lor (Char.code s.[2] lsl 8)
896 lor (Char.code s.[3] lsl 0)
898 let s = String.create len in
899 let n = Unix.read fd s 0 len in
900 if n != len then failwith "incomplete read(data)";
904 let btod b = if b then 1 else 0;;
906 let wcmd fmt =
907 let b = Buffer.create 16 in
908 Buffer.add_string b "llll";
909 Printf.kbprintf
910 (fun b ->
911 let s = Buffer.contents b in
912 let n = String.length s in
913 let len = n - 4 in
914 (* dolog "wcmd %S" (String.sub s 4 len); *)
915 s.[0] <- Char.chr ((len lsr 24) land 0xff);
916 s.[1] <- Char.chr ((len lsr 16) land 0xff);
917 s.[2] <- Char.chr ((len lsr 8) land 0xff);
918 s.[3] <- Char.chr (len land 0xff);
919 let n' = Unix.write state.sw s 0 n in
920 if n' != n then failwith "write failed";
921 ) b fmt;
924 let calcips h =
925 if conf.presentation
926 then
927 let d = conf.winh - h in
928 max 0 ((d + 1) / 2)
929 else
930 conf.interpagespace
933 let calcheight () =
934 let rec f pn ph pi fh l =
935 match l with
936 | (n, _, h, _) :: rest ->
937 let ips = calcips h in
938 let fh =
939 if conf.presentation
940 then fh+ips
941 else (
942 if isbirdseye state.mode && pn = 0
943 then fh + ips
944 else fh
947 let fh = fh + ((n - pn) * (ph + pi)) in
948 f n h ips fh rest;
950 | [] ->
951 let inc =
952 if conf.presentation || (isbirdseye state.mode && pn = 0)
953 then 0
954 else -pi
956 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
957 max 0 fh
959 let fh = f 0 0 0 0 state.pdims in
963 let calcheight () =
964 match conf.columns with
965 | None -> calcheight ()
966 | Some (_, b) ->
967 if Array.length b > 0
968 then
969 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
970 y + h
971 else 0
974 let getpageyh pageno =
975 let rec f pn ph pi y l =
976 match l with
977 | (n, _, h, _) :: rest ->
978 let ips = calcips h in
979 if n >= pageno
980 then
981 let h = if n = pageno then h else ph in
982 if conf.presentation && n = pageno
983 then
984 y + (pageno - pn) * (ph + pi) + pi, h
985 else
986 y + (pageno - pn) * (ph + pi), h
987 else
988 let y = y + (if conf.presentation then pi else 0) in
989 let y = y + (n - pn) * (ph + pi) in
990 f n h ips y rest
992 | [] ->
993 y + (pageno - pn) * (ph + pi), ph
995 f 0 0 0 0 state.pdims
998 let getpageyh pageno =
999 match conf.columns with
1000 | None -> getpageyh pageno
1001 | Some (_, b) ->
1002 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1003 y, h
1006 let getpagedim pageno =
1007 let rec f ppdim l =
1008 match l with
1009 | (n, _, _, _) as pdim :: rest ->
1010 if n >= pageno
1011 then (if n = pageno then pdim else ppdim)
1012 else f pdim rest
1014 | [] -> ppdim
1016 f (-1, -1, -1, -1) state.pdims
1019 let getpagey pageno = fst (getpageyh pageno);;
1021 let nogeomcmds cmds =
1022 match cmds with
1023 | s, [] -> String.length s = 0
1024 | _ -> false
1027 let layout1 y sh =
1028 let sh = sh - state.hscrollh in
1029 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1030 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1031 match pdims with
1032 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1033 let ips = calcips h in
1034 let yinc =
1035 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1036 then ips
1037 else 0
1039 (w, h, ips, xoff), rest, pdimno + 1, yinc
1040 | _ ->
1041 prev, pdims, pdimno, 0
1043 let dy = dy + yinc in
1044 let py = py + yinc in
1045 if pageno = state.pagecount || dy >= sh
1046 then
1047 accu
1048 else
1049 let vy = y + dy in
1050 if py + h <= vy - yinc
1051 then
1052 let py = py + h + ips in
1053 let dy = max 0 (py - y) in
1054 f ~pageno:(pageno+1)
1055 ~pdimno
1056 ~prev:curr
1059 ~pdims:rest
1060 ~accu
1061 else
1062 let pagey = vy - py in
1063 let pagevh = h - pagey in
1064 let pagevh = min (sh - dy) pagevh in
1065 let off = if yinc > 0 then py - vy else 0 in
1066 let py = py + h + ips in
1067 let pagex, dx =
1068 let xoff = xoff +
1069 if state.w < conf.winw - state.scrollw
1070 then (conf.winw - state.scrollw - state.w) / 2
1071 else 0
1073 let dispx = xoff + state.x in
1074 if dispx < 0
1075 then (-dispx, 0)
1076 else (0, dispx)
1078 let pagevw =
1079 let lw = w - pagex in
1080 min lw (conf.winw - state.scrollw)
1082 let e =
1083 { pageno = pageno
1084 ; pagedimno = pdimno
1085 ; pagew = w
1086 ; pageh = h
1087 ; pagex = pagex
1088 ; pagey = pagey + off
1089 ; pagevw = pagevw
1090 ; pagevh = pagevh - off
1091 ; pagedispx = dx
1092 ; pagedispy = dy + off
1095 let accu = e :: accu in
1096 f ~pageno:(pageno+1)
1097 ~pdimno
1098 ~prev:curr
1100 ~dy:(dy+pagevh+ips)
1101 ~pdims:rest
1102 ~accu
1104 if nogeomcmds state.geomcmds
1105 then (
1106 let accu =
1108 ~pageno:0
1109 ~pdimno:~-1
1110 ~prev:(0,0,0,0)
1111 ~py:0
1112 ~dy:0
1113 ~pdims:state.pdims
1114 ~accu:[]
1116 List.rev accu
1118 else
1122 let layoutN ((columns, coverA, coverB), b) y sh =
1123 let sh = sh - state.hscrollh in
1124 let rec fold accu n =
1125 if n = Array.length b
1126 then accu
1127 else
1128 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1129 if (vy - y) > sh &&
1130 (n = coverA - 1
1131 || n = state.pagecount - coverB
1132 || (n - coverA) mod columns = columns - 1)
1133 then accu
1134 else
1135 let accu =
1136 if vy + h > y
1137 then
1138 let pagey = max 0 (y - vy) in
1139 let pagedispy = if pagey > 0 then 0 else vy - y in
1140 let pagedispx, pagex, pagevw =
1141 let pdx =
1142 if n = coverA - 1 || n = state.pagecount - coverB
1143 then state.x + (conf.winw - state.scrollw - w) / 2
1144 else dx + xoff + state.x
1146 if pdx < 0
1147 then 0, -pdx, w + pdx
1148 else pdx, 0, min (conf.winw - state.scrollw) w
1150 let pagevh = min (h - pagey) (sh - pagedispy) in
1151 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1152 then
1153 let e =
1154 { pageno = n
1155 ; pagedimno = pdimno
1156 ; pagew = w
1157 ; pageh = h
1158 ; pagex = pagex
1159 ; pagey = pagey
1160 ; pagevw = pagevw
1161 ; pagevh = pagevh
1162 ; pagedispx = pagedispx
1163 ; pagedispy = pagedispy
1166 e :: accu
1167 else
1168 accu
1169 else
1170 accu
1172 fold accu (n+1)
1174 if nogeomcmds state.geomcmds
1175 then List.rev (fold [] 0)
1176 else []
1179 let layout y sh =
1180 match conf.columns with
1181 | None -> layout1 y sh
1182 | Some c -> layoutN c y sh
1185 let clamp incr =
1186 let y = state.y + incr in
1187 let y = max 0 y in
1188 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1192 let itertiles l f =
1193 let tilex = l.pagex mod conf.tilew in
1194 let tiley = l.pagey mod conf.tileh in
1196 let col = l.pagex / conf.tilew in
1197 let row = l.pagey / conf.tileh in
1199 let vw =
1200 let a = l.pagew - l.pagex in
1201 let b = conf.winw - state.scrollw in
1202 min a b
1203 and vh = l.pagevh in
1205 let rec rowloop row y0 dispy h =
1206 if h = 0
1207 then ()
1208 else (
1209 let dh = conf.tileh - y0 in
1210 let dh = min h dh in
1211 let rec colloop col x0 dispx w =
1212 if w = 0
1213 then ()
1214 else (
1215 let dw = conf.tilew - x0 in
1216 let dw = min w dw in
1218 f col row dispx dispy x0 y0 dw dh;
1219 colloop (col+1) 0 (dispx+dw) (w-dw)
1222 colloop col tilex l.pagedispx vw;
1223 rowloop (row+1) 0 (dispy+dh) (h-dh)
1226 if vw > 0 && vh > 0
1227 then rowloop row tiley l.pagedispy vh;
1230 let gettileopaque l col row =
1231 let key =
1232 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1234 try Some (Hashtbl.find state.tilemap key)
1235 with Not_found -> None
1238 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1239 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1240 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1243 let drawtiles l color =
1244 GlDraw.color color;
1245 let f col row x y tilex tiley w h =
1246 match gettileopaque l col row with
1247 | Some (opaque, _, t) ->
1248 let params = x, y, w, h, tilex, tiley in
1249 if conf.invert
1250 then (
1251 Gl.enable `blend;
1252 GlFunc.blend_func `zero `one_minus_src_color;
1254 drawtile params opaque;
1255 if conf.invert
1256 then Gl.disable `blend;
1257 if conf.debug
1258 then (
1259 let s = Printf.sprintf
1260 "%d[%d,%d] %f sec"
1261 l.pageno col row t
1263 let w = measurestr fstate.fontsize s in
1264 GlMisc.push_attrib [`current];
1265 GlDraw.color (0.0, 0.0, 0.0);
1266 GlDraw.rect
1267 (float (x-2), float (y-2))
1268 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1269 GlDraw.color (1.0, 1.0, 1.0);
1270 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1271 GlMisc.pop_attrib ();
1274 | _ ->
1275 let w =
1276 let lw = conf.winw - state.scrollw - x in
1277 min lw w
1278 and h =
1279 let lh = conf.winh - y in
1280 min lh h
1282 Gl.enable `texture_2d;
1283 begin match state.texid with
1284 | Some id ->
1285 GlTex.bind_texture `texture_2d id;
1286 let x0 = float x
1287 and y0 = float y
1288 and x1 = float (x+w)
1289 and y1 = float (y+h) in
1291 let tw = float w /. 64.0
1292 and th = float h /. 64.0 in
1293 let tx0 = float tilex /. 64.0
1294 and ty0 = float tiley /. 64.0 in
1295 let tx1 = tx0 +. tw
1296 and ty1 = ty0 +. th in
1297 GlDraw.begins `quads;
1298 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1299 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1300 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1301 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1302 GlDraw.ends ();
1304 Gl.disable `texture_2d;
1305 | None ->
1306 GlDraw.color (1.0, 1.0, 1.0);
1307 GlDraw.rect
1308 (float x, float y)
1309 (float (x+w), float (y+h));
1310 end;
1311 if w > 128 && h > fstate.fontsize + 10
1312 then (
1313 GlDraw.color (0.0, 0.0, 0.0);
1314 let c, r =
1315 if conf.verbose
1316 then (col*conf.tilew, row*conf.tileh)
1317 else col, row
1319 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1321 GlDraw.color color;
1323 itertiles l f
1326 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1328 let tilevisible1 l x y =
1329 let ax0 = l.pagex
1330 and ax1 = l.pagex + l.pagevw
1331 and ay0 = l.pagey
1332 and ay1 = l.pagey + l.pagevh in
1334 let bx0 = x
1335 and by0 = y in
1336 let bx1 = min (bx0 + conf.tilew) l.pagew
1337 and by1 = min (by0 + conf.tileh) l.pageh in
1339 let rx0 = max ax0 bx0
1340 and ry0 = max ay0 by0
1341 and rx1 = min ax1 bx1
1342 and ry1 = min ay1 by1 in
1344 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1345 nonemptyintersection
1348 let tilevisible layout n x y =
1349 let rec findpageinlayout = function
1350 | l :: _ when l.pageno = n -> tilevisible1 l x y
1351 | _ :: rest -> findpageinlayout rest
1352 | [] -> false
1354 findpageinlayout layout
1357 let tileready l x y =
1358 tilevisible1 l x y &&
1359 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1362 let tilepage n p layout =
1363 let rec loop = function
1364 | l :: rest ->
1365 if l.pageno = n
1366 then
1367 let f col row _ _ _ _ _ _ =
1368 if state.currently = Idle
1369 then
1370 match gettileopaque l col row with
1371 | Some _ -> ()
1372 | None ->
1373 let x = col*conf.tilew
1374 and y = row*conf.tileh in
1375 let w =
1376 let w = l.pagew - x in
1377 min w conf.tilew
1379 let h =
1380 let h = l.pageh - y in
1381 min h conf.tileh
1383 wcmd "tile %s %d %d %d %d" p x y w h;
1384 state.currently <-
1385 Tiling (
1386 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1387 conf.tilew, conf.tileh
1390 itertiles l f;
1391 else
1392 loop rest
1394 | [] -> ()
1396 if nogeomcmds state.geomcmds
1397 then loop layout;
1400 let preloadlayout visiblepages =
1401 let presentation = conf.presentation in
1402 let interpagespace = conf.interpagespace in
1403 let maxy = state.maxy in
1404 conf.presentation <- false;
1405 conf.interpagespace <- 0;
1406 state.maxy <- calcheight ();
1407 let y =
1408 match visiblepages with
1409 | [] -> 0
1410 | l :: _ -> getpagey l.pageno + l.pagey
1412 let y = if y < conf.winh then 0 else y - conf.winh in
1413 let h = state.y - y + conf.winh*3 in
1414 let pages = layout y h in
1415 conf.presentation <- presentation;
1416 conf.interpagespace <- interpagespace;
1417 state.maxy <- maxy;
1418 pages;
1421 let load pages =
1422 let rec loop pages =
1423 if state.currently != Idle
1424 then ()
1425 else
1426 match pages with
1427 | l :: rest ->
1428 begin match getopaque l.pageno with
1429 | None ->
1430 wcmd "page %d %d" l.pageno l.pagedimno;
1431 state.currently <- Loading (l, state.gen);
1432 | Some opaque ->
1433 tilepage l.pageno opaque pages;
1434 loop rest
1435 end;
1436 | _ -> ()
1438 if nogeomcmds state.geomcmds
1439 then loop pages
1442 let preload pages =
1443 load pages;
1444 if conf.preload && state.currently = Idle
1445 then load (preloadlayout pages);
1448 let layoutready layout =
1449 let rec fold all ls =
1450 all && match ls with
1451 | l :: rest ->
1452 let seen = ref false in
1453 let allvisible = ref true in
1454 let foo col row _ _ _ _ _ _ =
1455 seen := true;
1456 allvisible := !allvisible &&
1457 begin match gettileopaque l col row with
1458 | Some _ -> true
1459 | None -> false
1462 itertiles l foo;
1463 fold (!seen && !allvisible) rest
1464 | [] -> true
1466 let alltilesvisible = fold true layout in
1467 alltilesvisible;
1470 let gotoy y =
1471 let y = bound y 0 state.maxy in
1472 let y, layout, proceed =
1473 match conf.maxwait with
1474 | Some time when state.ghyll == noghyll ->
1475 begin match state.throttle with
1476 | None ->
1477 let layout = layout y conf.winh in
1478 let ready = layoutready layout in
1479 if not ready
1480 then (
1481 load layout;
1482 state.throttle <- Some (layout, y, now ());
1484 else G.postRedisplay "gotoy showall (None)";
1485 y, layout, ready
1486 | Some (_, _, started) ->
1487 let dt = now () -. started in
1488 if dt > time
1489 then (
1490 state.throttle <- None;
1491 let layout = layout y conf.winh in
1492 load layout;
1493 G.postRedisplay "maxwait";
1494 y, layout, true
1496 else -1, [], false
1499 | _ ->
1500 let layout = layout y conf.winh in
1501 if true || layoutready layout
1502 then G.postRedisplay "gotoy ready";
1503 y, layout, true
1505 if proceed
1506 then (
1507 state.y <- y;
1508 state.layout <- layout;
1509 begin match state.mode with
1510 | LinkNav (Ltexact (pageno, linkno)) ->
1511 let rec loop = function
1512 | [] ->
1513 state.mode <- LinkNav (Ltgendir 0)
1514 | l :: _ when l.pageno = pageno ->
1515 begin match getopaque pageno with
1516 | None ->
1517 state.mode <- LinkNav (Ltgendir 0)
1518 | Some opaque ->
1519 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1520 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1521 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1522 then state.mode <- LinkNav (Ltgendir 0)
1524 | _ :: rest -> loop rest
1526 loop layout
1527 | _ -> ()
1528 end;
1529 begin match state.mode with
1530 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1531 if not (pagevisible layout pageno)
1532 then (
1533 match state.layout with
1534 | [] -> ()
1535 | l :: _ ->
1536 state.mode <- Birdseye (
1537 conf, leftx, l.pageno, hooverpageno, anchor
1540 | LinkNav (Ltgendir dir as lt) ->
1541 let linknav =
1542 let rec loop = function
1543 | [] -> lt
1544 | l :: rest ->
1545 match getopaque l.pageno with
1546 | None -> loop rest
1547 | Some opaque ->
1548 let link =
1549 let ld =
1550 if dir = 0
1551 then LDfirstvisible (l.pagex, l.pagey, dir)
1552 else (
1553 if dir > 0 then LDfirst else LDlast
1556 findlink opaque ld
1558 match link with
1559 | Lnotfound -> loop rest
1560 | Lfound n ->
1561 showlinktype (getlink opaque n);
1562 Ltexact (l.pageno, n)
1564 loop state.layout
1566 state.mode <- LinkNav linknav
1567 | _ -> ()
1568 end;
1569 preload layout;
1571 state.ghyll <- noghyll;
1572 if conf.updatecurs
1573 then (
1574 let mx, my = state.mpos in
1575 updateunder mx my;
1579 let conttiling pageno opaque =
1580 tilepage pageno opaque
1581 (if conf.preload then preloadlayout state.layout else state.layout)
1584 let gotoy_and_clear_text y =
1585 if not conf.verbose then state.text <- "";
1586 gotoy y;
1589 let getanchor () =
1590 match state.layout with
1591 | [] -> emptyanchor
1592 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1595 let getanchory (n, top) =
1596 let y, h = getpageyh n in
1597 y + (truncate (top *. float h));
1600 let gotoanchor anchor =
1601 gotoy (getanchory anchor);
1604 let addnav () =
1605 cbput state.hists.nav (getanchor ());
1608 let getnav dir =
1609 let anchor = cbgetc state.hists.nav dir in
1610 getanchory anchor;
1613 let gotoghyll y =
1614 let rec scroll f n a b =
1615 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1616 let snake f a b =
1617 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1618 if f < a
1619 then s (float f /. float a)
1620 else (
1621 if f > b
1622 then 1.0 -. s ((float (f-b) /. float (n-b)))
1623 else 1.0
1626 snake f a b
1627 and summa f n a b =
1628 (* courtesy:
1629 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1630 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1631 let iv1 = iv f in
1632 let ins = float a *. iv1
1633 and outs = float (n-b) *. iv1 in
1634 let ones = b - a in
1635 ins +. outs +. float ones
1637 let rec set (_N, _A, _B) y sy =
1638 let sum = summa 1.0 _N _A _B in
1639 let dy = float (y - sy) in
1640 state.ghyll <- (
1641 let rec gf n y1 o =
1642 if n >= _N
1643 then state.ghyll <- noghyll
1644 else
1645 let go n =
1646 let s = scroll n _N _A _B in
1647 let y1 = y1 +. ((s *. dy) /. sum) in
1648 gotoy_and_clear_text (truncate y1);
1649 state.ghyll <- gf (n+1) y1;
1651 match o with
1652 | None -> go n
1653 | Some y' -> set (_N/2, 0, 0) y' state.y
1655 gf 0 (float state.y)
1658 match conf.ghyllscroll with
1659 | None ->
1660 gotoy_and_clear_text y
1661 | Some nab ->
1662 if state.ghyll == noghyll
1663 then set nab y state.y
1664 else state.ghyll (Some y)
1667 let gotopage n top =
1668 let y, h = getpageyh n in
1669 let y = y + (truncate (top *. float h)) in
1670 gotoghyll y
1673 let gotopage1 n top =
1674 let y = getpagey n in
1675 let y = y + top in
1676 gotoghyll y
1679 let invalidate s f =
1680 state.layout <- [];
1681 state.pdims <- [];
1682 state.rects <- [];
1683 state.rects1 <- [];
1684 match state.geomcmds with
1685 | ps, [] when String.length ps = 0 ->
1686 f ();
1687 state.geomcmds <- s, [];
1689 | ps, [] ->
1690 state.geomcmds <- ps, [s, f];
1692 | ps, (s', _) :: rest when s' = s ->
1693 state.geomcmds <- ps, ((s, f) :: rest);
1695 | ps, cmds ->
1696 state.geomcmds <- ps, ((s, f) :: cmds);
1699 let opendoc path password =
1700 state.path <- path;
1701 state.password <- password;
1702 state.gen <- state.gen + 1;
1703 state.docinfo <- [];
1705 setaalevel conf.aalevel;
1706 Wsi.settitle ("llpp " ^ Filename.basename path);
1707 wcmd "open %s\000%s\000" path password;
1708 invalidate "reqlayout"
1709 (fun () ->
1710 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1713 let scalecolor c =
1714 let c = c *. conf.colorscale in
1715 (c, c, c);
1718 let scalecolor2 (r, g, b) =
1719 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1722 let represent () =
1723 let docolumns = function
1724 | None -> ()
1725 | Some ((columns, coverA, coverB), _) ->
1726 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1727 let rec loop pageno pdimno pdim x y rowh pdims =
1728 if pageno = state.pagecount
1729 then ()
1730 else
1731 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1732 match pdims with
1733 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1734 pdimno+1, pdim, rest
1735 | _ ->
1736 pdimno, pdim, pdims
1738 let x, y, rowh' =
1739 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1740 then (
1741 (conf.winw - state.scrollw - w) / 2,
1742 y + rowh + conf.interpagespace, h
1744 else (
1745 if (pageno - coverA) mod columns = 0
1746 then 0, y + rowh + conf.interpagespace, h
1747 else x, y, max rowh h
1750 let rec fixrow m = if m = pageno then () else
1751 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1752 if h < rowh
1753 then (
1754 let y = y + (rowh - h) / 2 in
1755 a.(m) <- (pdimno, x, y, pdim);
1757 fixrow (m+1)
1759 if pageno > 1 && (pageno - coverA) mod columns = 0
1760 then fixrow (pageno - columns);
1761 a.(pageno) <- (pdimno, x, y, pdim);
1762 let x = x + w + xoff*2 + conf.interpagespace in
1763 loop (pageno+1) pdimno pdim x y rowh' pdims
1765 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1766 conf.columns <- Some ((columns, coverA, coverB), a);
1768 docolumns conf.columns;
1769 state.maxy <- calcheight ();
1770 state.hscrollh <-
1771 if state.w <= conf.winw - state.scrollw
1772 then 0
1773 else state.scrollw
1775 match state.mode with
1776 | Birdseye (_, _, pageno, _, _) ->
1777 let y, h = getpageyh pageno in
1778 let top = (conf.winh - h) / 2 in
1779 gotoy (max 0 (y - top))
1780 | _ -> gotoanchor state.anchor
1783 let reshape w h =
1784 GlDraw.viewport 0 0 w h;
1785 if state.geomcmds != firstgeomcmds && nogeomcmds state.geomcmds
1786 then state.anchor <- getanchor ();
1788 conf.winw <- w;
1789 let w = truncate (float w *. conf.zoom) - state.scrollw in
1790 let w = max w 2 in
1791 conf.winh <- h;
1792 setfontsize fstate.fontsize;
1793 GlMat.mode `modelview;
1794 GlMat.load_identity ();
1796 GlMat.mode `projection;
1797 GlMat.load_identity ();
1798 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1799 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1800 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1802 let relx =
1803 if conf.zoom <= 1.0
1804 then 0.0
1805 else float state.x /. float state.w
1807 invalidate "geometry"
1808 (fun () ->
1809 state.w <- w;
1810 state.x <- truncate (relx *. float w);
1811 let w =
1812 match conf.columns with
1813 | None -> w
1814 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1816 wcmd "geometry %d %d" w h);
1819 let enttext () =
1820 let len = String.length state.text in
1821 let drawstring s =
1822 let hscrollh =
1823 match state.mode with
1824 | Textentry _
1825 | View -> state.hscrollh
1826 | _ -> 0
1828 let rect x w =
1829 GlDraw.rect
1830 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1831 (x+.w, float (conf.winh - hscrollh))
1834 let w = float (conf.winw - state.scrollw - 1) in
1835 if state.progress >= 0.0 && state.progress < 1.0
1836 then (
1837 GlDraw.color (0.3, 0.3, 0.3);
1838 let w1 = w *. state.progress in
1839 rect 0.0 w1;
1840 GlDraw.color (0.0, 0.0, 0.0);
1841 rect w1 (w-.w1)
1843 else (
1844 GlDraw.color (0.0, 0.0, 0.0);
1845 rect 0.0 w;
1848 GlDraw.color (1.0, 1.0, 1.0);
1849 drawstring fstate.fontsize
1850 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1852 let s =
1853 match state.mode with
1854 | Textentry ((prefix, text, _, _, _), _) ->
1855 let s =
1856 if len > 0
1857 then
1858 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1859 else
1860 Printf.sprintf "%s%s_" prefix text
1864 | _ -> state.text
1866 let s =
1867 if state.newerrmsgs
1868 then (
1869 if not (istextentry state.mode)
1870 then
1871 let s1 = "(press 'e' to review error messasges)" in
1872 if String.length s > 0 then s ^ " " ^ s1 else s1
1873 else s
1875 else s
1877 if String.length s > 0
1878 then drawstring s
1881 let gctiles () =
1882 let len = Queue.length state.tilelru in
1883 let rec loop qpos =
1884 if state.memused <= conf.memlimit
1885 then ()
1886 else (
1887 if qpos < len
1888 then
1889 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1890 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1891 let (_, pw, ph, _) = getpagedim n in
1893 gen = state.gen
1894 && colorspace = conf.colorspace
1895 && angle = conf.angle
1896 && pagew = pw
1897 && pageh = ph
1898 && (
1899 let layout =
1900 match state.throttle with
1901 | None ->
1902 if conf.preload
1903 then preloadlayout state.layout
1904 else state.layout
1905 | Some (layout, _, _) ->
1906 layout
1908 let x = col*conf.tilew
1909 and y = row*conf.tileh in
1910 tilevisible layout n x y
1912 then Queue.push lruitem state.tilelru
1913 else (
1914 wcmd "freetile %s" p;
1915 state.memused <- state.memused - s;
1916 state.uioh#infochanged Memused;
1917 Hashtbl.remove state.tilemap k;
1919 loop (qpos+1)
1922 loop 0
1925 let flushtiles () =
1926 Queue.iter (fun (k, p, s) ->
1927 wcmd "freetile %s" p;
1928 state.memused <- state.memused - s;
1929 state.uioh#infochanged Memused;
1930 Hashtbl.remove state.tilemap k;
1931 ) state.tilelru;
1932 Queue.clear state.tilelru;
1933 load state.layout;
1936 let logcurrently = function
1937 | Idle -> dolog "Idle"
1938 | Loading (l, gen) ->
1939 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1940 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1941 dolog
1942 "Tiling %d[%d,%d] page=%s cs=%s angle"
1943 l.pageno col row pageopaque
1944 (colorspace_to_string colorspace)
1946 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1947 angle gen conf.angle state.gen
1948 tilew tileh
1949 conf.tilew conf.tileh
1951 | Outlining _ ->
1952 dolog "outlining"
1955 let act cmds =
1956 (* dolog "%S" cmds; *)
1957 let op, args =
1958 let spacepos =
1959 try String.index cmds ' '
1960 with Not_found -> -1
1962 if spacepos = -1
1963 then cmds, ""
1964 else
1965 let l = String.length cmds in
1966 let op = String.sub cmds 0 spacepos in
1967 op, begin
1968 if l - spacepos < 2 then ""
1969 else String.sub cmds (spacepos+1) (l-spacepos-1)
1972 match op with
1973 | "clear" ->
1974 state.uioh#infochanged Pdim;
1975 state.pdims <- [];
1977 | "clearrects" ->
1978 state.rects <- state.rects1;
1979 G.postRedisplay "clearrects";
1981 | "continue" ->
1982 let n =
1983 try Scanf.sscanf args "%u" (fun n -> n)
1984 with exn ->
1985 dolog "error processing 'continue' %S: %s"
1986 cmds (Printexc.to_string exn);
1987 exit 1;
1989 state.pagecount <- n;
1990 begin match state.currently with
1991 | Outlining l ->
1992 state.currently <- Idle;
1993 state.outlines <- Array.of_list (List.rev l)
1994 | _ -> ()
1995 end;
1997 let cur, cmds = state.geomcmds in
1998 if String.length cur = 0
1999 then failwith "umpossible";
2001 begin match List.rev cmds with
2002 | [] ->
2003 state.geomcmds <- "", [];
2004 represent ();
2005 | (s, f) :: rest ->
2006 f ();
2007 state.geomcmds <- s, List.rev rest;
2008 end;
2009 if conf.maxwait = None
2010 then G.postRedisplay "continue";
2012 | "title" ->
2013 Wsi.settitle args
2015 | "msg" ->
2016 showtext ' ' args
2018 | "vmsg" ->
2019 if conf.verbose
2020 then showtext ' ' args
2022 | "progress" ->
2023 let progress, text =
2025 Scanf.sscanf args "%f %n"
2026 (fun f pos ->
2027 f, String.sub args pos (String.length args - pos))
2028 with exn ->
2029 dolog "error processing 'progress' %S: %s"
2030 cmds (Printexc.to_string exn);
2031 exit 1;
2033 state.text <- text;
2034 state.progress <- progress;
2035 G.postRedisplay "progress"
2037 | "firstmatch" ->
2038 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2040 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2041 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2042 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2043 with exn ->
2044 dolog "error processing 'firstmatch' %S: %s"
2045 cmds (Printexc.to_string exn);
2046 exit 1;
2048 let y = (getpagey pageno) + truncate y0 in
2049 addnav ();
2050 gotoy y;
2051 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2053 | "match" ->
2054 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2056 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2057 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2058 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2059 with exn ->
2060 dolog "error processing 'match' %S: %s"
2061 cmds (Printexc.to_string exn);
2062 exit 1;
2064 state.rects1 <-
2065 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2067 | "page" ->
2068 let pageopaque, t =
2070 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2071 with exn ->
2072 dolog "error processing 'page' %S: %s"
2073 cmds (Printexc.to_string exn);
2074 exit 1;
2076 begin match state.currently with
2077 | Loading (l, gen) ->
2078 vlog "page %d took %f sec" l.pageno t;
2079 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2080 begin match state.throttle with
2081 | None ->
2082 let preloadedpages =
2083 if conf.preload
2084 then preloadlayout state.layout
2085 else state.layout
2087 let evict () =
2088 let module IntSet =
2089 Set.Make (struct type t = int let compare = (-) end) in
2090 let set =
2091 List.fold_left (fun s l -> IntSet.add l.pageno s)
2092 IntSet.empty preloadedpages
2094 let evictedpages =
2095 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2096 if not (IntSet.mem pageno set)
2097 then (
2098 wcmd "freepage %s" opaque;
2099 key :: accu
2101 else accu
2102 ) state.pagemap []
2104 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2106 evict ();
2107 state.currently <- Idle;
2108 if gen = state.gen
2109 then (
2110 tilepage l.pageno pageopaque state.layout;
2111 load state.layout;
2112 load preloadedpages;
2113 if pagevisible state.layout l.pageno
2114 && layoutready state.layout
2115 then G.postRedisplay "page";
2118 | Some (layout, _, _) ->
2119 state.currently <- Idle;
2120 tilepage l.pageno pageopaque layout;
2121 load state.layout
2122 end;
2124 | _ ->
2125 dolog "Inconsistent loading state";
2126 logcurrently state.currently;
2127 exit 1
2130 | "tile" ->
2131 let (x, y, opaque, size, t) =
2133 Scanf.sscanf args "%u %u %s %u %f"
2134 (fun x y p size t -> (x, y, p, size, t))
2135 with exn ->
2136 dolog "error processing 'tile' %S: %s"
2137 cmds (Printexc.to_string exn);
2138 exit 1;
2140 begin match state.currently with
2141 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2142 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2144 if tilew != conf.tilew || tileh != conf.tileh
2145 then (
2146 wcmd "freetile %s" opaque;
2147 state.currently <- Idle;
2148 load state.layout;
2150 else (
2151 puttileopaque l col row gen cs angle opaque size t;
2152 state.memused <- state.memused + size;
2153 state.uioh#infochanged Memused;
2154 gctiles ();
2155 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2156 opaque, size) state.tilelru;
2158 let layout =
2159 match state.throttle with
2160 | None -> state.layout
2161 | Some (layout, _, _) -> layout
2164 state.currently <- Idle;
2165 if gen = state.gen
2166 && conf.colorspace = cs
2167 && conf.angle = angle
2168 && tilevisible layout l.pageno x y
2169 then conttiling l.pageno pageopaque;
2171 begin match state.throttle with
2172 | None ->
2173 preload state.layout;
2174 if gen = state.gen
2175 && conf.colorspace = cs
2176 && conf.angle = angle
2177 && tilevisible state.layout l.pageno x y
2178 then G.postRedisplay "tile nothrottle";
2180 | Some (layout, y, _) ->
2181 let ready = layoutready layout in
2182 if ready
2183 then (
2184 state.y <- y;
2185 state.layout <- layout;
2186 state.throttle <- None;
2187 G.postRedisplay "throttle";
2189 else load layout;
2190 end;
2193 | _ ->
2194 dolog "Inconsistent tiling state";
2195 logcurrently state.currently;
2196 exit 1
2199 | "pdim" ->
2200 let pdim =
2202 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2203 with exn ->
2204 dolog "error processing 'pdim' %S: %s"
2205 cmds (Printexc.to_string exn);
2206 exit 1;
2208 state.uioh#infochanged Pdim;
2209 state.pdims <- pdim :: state.pdims
2211 | "o" ->
2212 let (l, n, t, h, pos) =
2214 Scanf.sscanf args "%u %u %d %u %n"
2215 (fun l n t h pos -> l, n, t, h, pos)
2216 with exn ->
2217 dolog "error processing 'o' %S: %s"
2218 cmds (Printexc.to_string exn);
2219 exit 1;
2221 let s = String.sub args pos (String.length args - pos) in
2222 let outline = (s, l, (n, float t /. float h)) in
2223 begin match state.currently with
2224 | Outlining outlines ->
2225 state.currently <- Outlining (outline :: outlines)
2226 | Idle ->
2227 state.currently <- Outlining [outline]
2228 | currently ->
2229 dolog "invalid outlining state";
2230 logcurrently currently
2233 | "info" ->
2234 state.docinfo <- (1, args) :: state.docinfo
2236 | "infoend" ->
2237 state.uioh#infochanged Docinfo;
2238 state.docinfo <- List.rev state.docinfo
2240 | _ ->
2241 dolog "unknown cmd `%S'" cmds
2244 let onhist cb =
2245 let rc = cb.rc in
2246 let action = function
2247 | HCprev -> cbget cb ~-1
2248 | HCnext -> cbget cb 1
2249 | HCfirst -> cbget cb ~-(cb.rc)
2250 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2251 and cancel () = cb.rc <- rc
2252 in (action, cancel)
2255 let search pattern forward =
2256 if String.length pattern > 0
2257 then
2258 let pn, py =
2259 match state.layout with
2260 | [] -> 0, 0
2261 | l :: _ ->
2262 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2264 wcmd "search %d %d %d %d,%s\000"
2265 (btod conf.icase) pn py (btod forward) pattern;
2268 let intentry text key =
2269 let c =
2270 if key >= 32 && key < 127
2271 then Char.chr key
2272 else '\000'
2274 match c with
2275 | '0' .. '9' ->
2276 let text = addchar text c in
2277 TEcont text
2279 | _ ->
2280 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2281 TEcont text
2284 let textentry text key =
2285 if key land 0xff00 = 0xff00
2286 then TEcont text
2287 else TEcont (text ^ Wsi.toutf8 key)
2290 let reqlayout angle proportional =
2291 match state.throttle with
2292 | None ->
2293 if nogeomcmds state.geomcmds
2294 then state.anchor <- getanchor ();
2295 conf.angle <- angle mod 360;
2296 if conf.angle != 0
2297 then (
2298 match state.mode with
2299 | LinkNav _ -> state.mode <- View
2300 | _ -> ()
2302 conf.proportional <- proportional;
2303 invalidate "reqlayout"
2304 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2305 | _ -> ()
2308 let settrim trimmargins trimfuzz =
2309 if nogeomcmds state.geomcmds
2310 then state.anchor <- getanchor ();
2311 conf.trimmargins <- trimmargins;
2312 conf.trimfuzz <- trimfuzz;
2313 let x0, y0, x1, y1 = trimfuzz in
2314 invalidate "settrim"
2315 (fun () ->
2316 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2317 Hashtbl.iter (fun _ opaque ->
2318 wcmd "freepage %s" opaque;
2319 ) state.pagemap;
2320 Hashtbl.clear state.pagemap;
2323 let setzoom zoom =
2324 match state.throttle with
2325 | None ->
2326 let zoom = max 0.01 zoom in
2327 if zoom <> conf.zoom
2328 then (
2329 state.prevzoom <- conf.zoom;
2330 conf.zoom <- zoom;
2331 reshape conf.winw conf.winh;
2332 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2335 | Some (layout, y, started) ->
2336 let time =
2337 match conf.maxwait with
2338 | None -> 0.0
2339 | Some t -> t
2341 let dt = now () -. started in
2342 if dt > time
2343 then (
2344 state.y <- y;
2345 load layout;
2349 let setcolumns columns coverA coverB =
2350 if columns < 2
2351 then (
2352 conf.columns <- None;
2353 state.x <- 0;
2354 setzoom 1.0;
2356 else (
2357 conf.columns <- Some ((columns, coverA, coverB), [||]);
2358 conf.zoom <- 1.0;
2360 reshape conf.winw conf.winh;
2363 let enterbirdseye () =
2364 let zoom = float conf.thumbw /. float conf.winw in
2365 let birdseyepageno =
2366 let cy = conf.winh / 2 in
2367 let fold = function
2368 | [] -> 0
2369 | l :: rest ->
2370 let rec fold best = function
2371 | [] -> best.pageno
2372 | l :: rest ->
2373 let d = cy - (l.pagedispy + l.pagevh/2)
2374 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2375 if abs d < abs dbest
2376 then fold l rest
2377 else best.pageno
2378 in fold l rest
2380 fold state.layout
2382 state.mode <- Birdseye (
2383 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2385 conf.zoom <- zoom;
2386 conf.presentation <- false;
2387 conf.interpagespace <- 10;
2388 conf.hlinks <- false;
2389 state.x <- 0;
2390 state.mstate <- Mnone;
2391 conf.maxwait <- None;
2392 conf.columns <- (
2393 match conf.beyecolumns with
2394 | Some c ->
2395 conf.zoom <- 1.0;
2396 Some ((c, 0, 0), [||])
2397 | None -> None
2399 Wsi.setcursor Wsi.CURSOR_INHERIT;
2400 if conf.verbose
2401 then
2402 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2403 (100.0*.zoom)
2404 else
2405 state.text <- ""
2407 reshape conf.winw conf.winh;
2410 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2411 state.mode <- View;
2412 conf.zoom <- c.zoom;
2413 conf.presentation <- c.presentation;
2414 conf.interpagespace <- c.interpagespace;
2415 conf.maxwait <- c.maxwait;
2416 conf.hlinks <- c.hlinks;
2417 conf.beyecolumns <- (
2418 match conf.columns with
2419 | Some ((c, _, _), _) -> Some c
2420 | None -> None
2422 conf.columns <- (
2423 match c.columns with
2424 | Some (c, _) -> Some (c, [||])
2425 | None -> None
2427 state.x <- leftx;
2428 if conf.verbose
2429 then
2430 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2431 (100.0*.conf.zoom)
2433 reshape conf.winw conf.winh;
2434 state.anchor <- if goback then anchor else (pageno, 0.0);
2437 let togglebirdseye () =
2438 match state.mode with
2439 | Birdseye vals -> leavebirdseye vals true
2440 | View -> enterbirdseye ()
2441 | _ -> ()
2444 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2445 let pageno = max 0 (pageno - incr) in
2446 let rec loop = function
2447 | [] -> gotopage1 pageno 0
2448 | l :: _ when l.pageno = pageno ->
2449 if l.pagedispy >= 0 && l.pagey = 0
2450 then G.postRedisplay "upbirdseye"
2451 else gotopage1 pageno 0
2452 | _ :: rest -> loop rest
2454 loop state.layout;
2455 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2458 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2459 let pageno = min (state.pagecount - 1) (pageno + incr) in
2460 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2461 let rec loop = function
2462 | [] ->
2463 let y, h = getpageyh pageno in
2464 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2465 gotoy (clamp dy)
2466 | l :: _ when l.pageno = pageno ->
2467 if l.pagevh != l.pageh
2468 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2469 else G.postRedisplay "downbirdseye"
2470 | _ :: rest -> loop rest
2472 loop state.layout
2475 let optentry mode _ key =
2476 let btos b = if b then "on" else "off" in
2477 if key >= 32 && key < 127
2478 then
2479 let c = Char.chr key in
2480 match c with
2481 | 's' ->
2482 let ondone s =
2483 try conf.scrollstep <- int_of_string s with exc ->
2484 state.text <- Printf.sprintf "bad integer `%s': %s"
2485 s (Printexc.to_string exc)
2487 TEswitch ("scroll step: ", "", None, intentry, ondone)
2489 | 'A' ->
2490 let ondone s =
2492 conf.autoscrollstep <- int_of_string s;
2493 if state.autoscroll <> None
2494 then state.autoscroll <- Some conf.autoscrollstep
2495 with exc ->
2496 state.text <- Printf.sprintf "bad integer `%s': %s"
2497 s (Printexc.to_string exc)
2499 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2501 | 'C' ->
2502 let ondone s =
2504 let n, a, b = columns_of_string s in
2505 setcolumns n a b;
2506 with exc ->
2507 state.text <- Printf.sprintf "bad columns `%s': %s"
2508 s (Printexc.to_string exc)
2510 TEswitch ("columns: ", "", None, textentry, ondone)
2512 | 'Z' ->
2513 let ondone s =
2515 let zoom = float (int_of_string s) /. 100.0 in
2516 setzoom zoom
2517 with exc ->
2518 state.text <- Printf.sprintf "bad integer `%s': %s"
2519 s (Printexc.to_string exc)
2521 TEswitch ("zoom: ", "", None, intentry, ondone)
2523 | 't' ->
2524 let ondone s =
2526 conf.thumbw <- bound (int_of_string s) 2 4096;
2527 state.text <-
2528 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2529 begin match mode with
2530 | Birdseye beye ->
2531 leavebirdseye beye false;
2532 enterbirdseye ();
2533 | _ -> ();
2535 with exc ->
2536 state.text <- Printf.sprintf "bad integer `%s': %s"
2537 s (Printexc.to_string exc)
2539 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2541 | 'R' ->
2542 let ondone s =
2543 match try
2544 Some (int_of_string s)
2545 with exc ->
2546 state.text <- Printf.sprintf "bad integer `%s': %s"
2547 s (Printexc.to_string exc);
2548 None
2549 with
2550 | Some angle -> reqlayout angle conf.proportional
2551 | None -> ()
2553 TEswitch ("rotation: ", "", None, intentry, ondone)
2555 | 'i' ->
2556 conf.icase <- not conf.icase;
2557 TEdone ("case insensitive search " ^ (btos conf.icase))
2559 | 'p' ->
2560 conf.preload <- not conf.preload;
2561 gotoy state.y;
2562 TEdone ("preload " ^ (btos conf.preload))
2564 | 'v' ->
2565 conf.verbose <- not conf.verbose;
2566 TEdone ("verbose " ^ (btos conf.verbose))
2568 | 'd' ->
2569 conf.debug <- not conf.debug;
2570 TEdone ("debug " ^ (btos conf.debug))
2572 | 'h' ->
2573 conf.maxhfit <- not conf.maxhfit;
2574 state.maxy <-
2575 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2576 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2578 | 'c' ->
2579 conf.crophack <- not conf.crophack;
2580 TEdone ("crophack " ^ btos conf.crophack)
2582 | 'a' ->
2583 let s =
2584 match conf.maxwait with
2585 | None ->
2586 conf.maxwait <- Some infinity;
2587 "always wait for page to complete"
2588 | Some _ ->
2589 conf.maxwait <- None;
2590 "show placeholder if page is not ready"
2592 TEdone s
2594 | 'f' ->
2595 conf.underinfo <- not conf.underinfo;
2596 TEdone ("underinfo " ^ btos conf.underinfo)
2598 | 'P' ->
2599 conf.savebmarks <- not conf.savebmarks;
2600 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2602 | 'S' ->
2603 let ondone s =
2605 let pageno, py =
2606 match state.layout with
2607 | [] -> 0, 0
2608 | l :: _ ->
2609 l.pageno, l.pagey
2611 conf.interpagespace <- int_of_string s;
2612 state.maxy <- calcheight ();
2613 let y = getpagey pageno in
2614 gotoy (y + py)
2615 with exc ->
2616 state.text <- Printf.sprintf "bad integer `%s': %s"
2617 s (Printexc.to_string exc)
2619 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2621 | 'l' ->
2622 reqlayout conf.angle (not conf.proportional);
2623 TEdone ("proportional display " ^ btos conf.proportional)
2625 | 'T' ->
2626 settrim (not conf.trimmargins) conf.trimfuzz;
2627 TEdone ("trim margins " ^ btos conf.trimmargins)
2629 | 'I' ->
2630 conf.invert <- not conf.invert;
2631 TEdone ("invert colors " ^ btos conf.invert)
2633 | 'x' ->
2634 let ondone s =
2635 cbput state.hists.sel s;
2636 conf.selcmd <- s;
2638 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2639 textentry, ondone)
2641 | _ ->
2642 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2643 TEstop
2644 else
2645 TEcont state.text
2648 class type lvsource = object
2649 method getitemcount : int
2650 method getitem : int -> (string * int)
2651 method hasaction : int -> bool
2652 method exit :
2653 uioh:uioh ->
2654 cancel:bool ->
2655 active:int ->
2656 first:int ->
2657 pan:int ->
2658 qsearch:string ->
2659 uioh option
2660 method getactive : int
2661 method getfirst : int
2662 method getqsearch : string
2663 method setqsearch : string -> unit
2664 method getpan : int
2665 end;;
2667 class virtual lvsourcebase = object
2668 val mutable m_active = 0
2669 val mutable m_first = 0
2670 val mutable m_qsearch = ""
2671 val mutable m_pan = 0
2672 method getactive = m_active
2673 method getfirst = m_first
2674 method getqsearch = m_qsearch
2675 method getpan = m_pan
2676 method setqsearch s = m_qsearch <- s
2677 end;;
2679 let withoutlastutf8 s =
2680 let len = String.length s in
2681 if len = 0
2682 then s
2683 else
2684 let rec find pos =
2685 if pos = 0
2686 then pos
2687 else
2688 let b = Char.code s.[pos] in
2689 if b land 0b110000 = 0b11000000
2690 then find (pos-1)
2691 else pos-1
2693 let first =
2694 if Char.code s.[len-1] land 0x80 = 0
2695 then len-1
2696 else find (len-1)
2698 String.sub s 0 first;
2701 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2702 let enttext te =
2703 state.mode <- Textentry (te, onleave);
2704 state.text <- "";
2705 enttext ();
2706 G.postRedisplay "textentrykeyboard enttext";
2708 let histaction cmd =
2709 match opthist with
2710 | None -> ()
2711 | Some (action, _) ->
2712 state.mode <- Textentry (
2713 (c, action cmd, opthist, onkey, ondone), onleave
2715 G.postRedisplay "textentry histaction"
2717 match key with
2718 | 0xff08 -> (* backspace *)
2719 let s = withoutlastutf8 text in
2720 let len = String.length s in
2721 if len = 0
2722 then (
2723 onleave Cancel;
2724 G.postRedisplay "textentrykeyboard after cancel";
2726 else (
2727 enttext (c, s, opthist, onkey, ondone)
2730 | 0xff0d ->
2731 ondone text;
2732 onleave Confirm;
2733 G.postRedisplay "textentrykeyboard after confirm"
2735 | 0xff52 -> histaction HCprev
2736 | 0xff54 -> histaction HCnext
2737 | 0xff50 -> histaction HCfirst
2738 | 0xff57 -> histaction HClast
2740 | 0xff1b -> (* escape*)
2741 if String.length text = 0
2742 then (
2743 begin match opthist with
2744 | None -> ()
2745 | Some (_, onhistcancel) -> onhistcancel ()
2746 end;
2747 onleave Cancel;
2748 state.text <- "";
2749 G.postRedisplay "textentrykeyboard after cancel2"
2751 else (
2752 enttext (c, "", opthist, onkey, ondone)
2755 | 0xff9f | 0xffff -> () (* delete *)
2757 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2758 begin match onkey text key with
2759 | TEdone text ->
2760 ondone text;
2761 onleave Confirm;
2762 G.postRedisplay "textentrykeyboard after confirm2";
2764 | TEcont text ->
2765 enttext (c, text, opthist, onkey, ondone);
2767 | TEstop ->
2768 onleave Cancel;
2769 G.postRedisplay "textentrykeyboard after cancel3"
2771 | TEswitch te ->
2772 state.mode <- Textentry (te, onleave);
2773 G.postRedisplay "textentrykeyboard switch";
2774 end;
2776 | _ ->
2777 vlog "unhandled key %s" (Wsi.keyname key)
2780 let firstof first active =
2781 if first > active || abs (first - active) > fstate.maxrows - 1
2782 then max 0 (active - (fstate.maxrows/2))
2783 else first
2786 let calcfirst first active =
2787 if active > first
2788 then
2789 let rows = active - first in
2790 if rows > fstate.maxrows then active - fstate.maxrows else first
2791 else active
2794 let scrollph y maxy =
2795 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2796 let sh = float conf.winh /. sh in
2797 let sh = max sh (float conf.scrollh) in
2799 let percent =
2800 if y = state.maxy
2801 then 1.0
2802 else float y /. float maxy
2804 let position = (float conf.winh -. sh) *. percent in
2806 let position =
2807 if position +. sh > float conf.winh
2808 then float conf.winh -. sh
2809 else position
2811 position, sh;
2814 let coe s = (s :> uioh);;
2816 class listview ~(source:lvsource) ~trusted ~modehash =
2817 object (self)
2818 val m_pan = source#getpan
2819 val m_first = source#getfirst
2820 val m_active = source#getactive
2821 val m_qsearch = source#getqsearch
2822 val m_prev_uioh = state.uioh
2824 method private elemunder y =
2825 let n = y / (fstate.fontsize+1) in
2826 if m_first + n < source#getitemcount
2827 then (
2828 if source#hasaction (m_first + n)
2829 then Some (m_first + n)
2830 else None
2832 else None
2834 method display =
2835 Gl.enable `blend;
2836 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2837 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2838 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2839 GlDraw.color (1., 1., 1.);
2840 Gl.enable `texture_2d;
2841 let fs = fstate.fontsize in
2842 let nfs = fs + 1 in
2843 let ww = fstate.wwidth in
2844 let tabw = 30.0*.ww in
2845 let itemcount = source#getitemcount in
2846 let rec loop row =
2847 if (row - m_first) * nfs > conf.winh
2848 then ()
2849 else (
2850 if row >= 0 && row < itemcount
2851 then (
2852 let (s, level) = source#getitem row in
2853 let y = (row - m_first) * nfs in
2854 let x = 5.0 +. float (level + m_pan) *. ww in
2855 if row = m_active
2856 then (
2857 Gl.disable `texture_2d;
2858 GlDraw.polygon_mode `both `line;
2859 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2860 GlDraw.rect (1., float (y + 1))
2861 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2862 GlDraw.polygon_mode `both `fill;
2863 GlDraw.color (1., 1., 1.);
2864 Gl.enable `texture_2d;
2867 let drawtabularstring s =
2868 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2869 if trusted
2870 then
2871 let tabpos = try String.index s '\t' with Not_found -> -1 in
2872 if tabpos > 0
2873 then
2874 let len = String.length s - tabpos - 1 in
2875 let s1 = String.sub s 0 tabpos
2876 and s2 = String.sub s (tabpos + 1) len in
2877 let nx = drawstr x s1 in
2878 let sw = nx -. x in
2879 let x = x +. (max tabw sw) in
2880 drawstr x s2
2881 else
2882 drawstr x s
2883 else
2884 drawstr x s
2886 let _ = drawtabularstring s in
2887 loop (row+1)
2891 loop m_first;
2892 Gl.disable `blend;
2893 Gl.disable `texture_2d;
2895 method updownlevel incr =
2896 let len = source#getitemcount in
2897 let curlevel =
2898 if m_active >= 0 && m_active < len
2899 then snd (source#getitem m_active)
2900 else -1
2902 let rec flow i =
2903 if i = len then i-1 else if i = -1 then 0 else
2904 let _, l = source#getitem i in
2905 if l != curlevel then i else flow (i+incr)
2907 let active = flow m_active in
2908 let first = calcfirst m_first active in
2909 G.postRedisplay "outline updownlevel";
2910 {< m_active = active; m_first = first >}
2912 method private key1 key mask =
2913 let set1 active first qsearch =
2914 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2916 let search active pattern incr =
2917 let dosearch re =
2918 let rec loop n =
2919 if n >= 0 && n < source#getitemcount
2920 then (
2921 let s, _ = source#getitem n in
2923 (try ignore (Str.search_forward re s 0); true
2924 with Not_found -> false)
2925 then Some n
2926 else loop (n + incr)
2928 else None
2930 loop active
2933 let re = Str.regexp_case_fold pattern in
2934 dosearch re
2935 with Failure s ->
2936 state.text <- s;
2937 None
2939 let itemcount = source#getitemcount in
2940 let find start incr =
2941 let rec find i =
2942 if i = -1 || i = itemcount
2943 then -1
2944 else (
2945 if source#hasaction i
2946 then i
2947 else find (i + incr)
2950 find start
2952 let set active first =
2953 let first = bound first 0 (itemcount - fstate.maxrows) in
2954 state.text <- "";
2955 coe {< m_active = active; m_first = first >}
2957 let navigate incr =
2958 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2959 let active, first =
2960 let incr1 = if incr > 0 then 1 else -1 in
2961 if isvisible m_first m_active
2962 then
2963 let next =
2964 let next = m_active + incr in
2965 let next =
2966 if next < 0 || next >= itemcount
2967 then -1
2968 else find next incr1
2970 if next = -1 || abs (m_active - next) > fstate.maxrows
2971 then -1
2972 else next
2974 if next = -1
2975 then
2976 let first = m_first + incr in
2977 let first = bound first 0 (itemcount - 1) in
2978 let next =
2979 let next = m_active + incr in
2980 let next = bound next 0 (itemcount - 1) in
2981 find next ~-incr1
2983 let active = if next = -1 then m_active else next in
2984 active, first
2985 else
2986 let first = min next m_first in
2987 let first =
2988 if abs (next - first) > fstate.maxrows
2989 then first + incr
2990 else first
2992 next, first
2993 else
2994 let first = m_first + incr in
2995 let first = bound first 0 (itemcount - 1) in
2996 let active =
2997 let next = m_active + incr in
2998 let next = bound next 0 (itemcount - 1) in
2999 let next = find next incr1 in
3000 let active =
3001 if next = -1 || abs (m_active - first) > fstate.maxrows
3002 then (
3003 let active = if m_active = -1 then next else m_active in
3004 active
3006 else next
3008 if isvisible first active
3009 then active
3010 else -1
3012 active, first
3014 G.postRedisplay "listview navigate";
3015 set active first;
3017 match key with
3018 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3019 let incr = if key = 0x72 then -1 else 1 in
3020 let active, first =
3021 match search (m_active + incr) m_qsearch incr with
3022 | None ->
3023 state.text <- m_qsearch ^ " [not found]";
3024 m_active, m_first
3025 | Some active ->
3026 state.text <- m_qsearch;
3027 active, firstof m_first active
3029 G.postRedisplay "listview ctrl-r/s";
3030 set1 active first m_qsearch;
3032 | 0xff08 -> (* backspace *)
3033 if String.length m_qsearch = 0
3034 then coe self
3035 else (
3036 let qsearch = withoutlastutf8 m_qsearch in
3037 let len = String.length qsearch in
3038 if len = 0
3039 then (
3040 state.text <- "";
3041 G.postRedisplay "listview empty qsearch";
3042 set1 m_active m_first "";
3044 else
3045 let active, first =
3046 match search m_active qsearch ~-1 with
3047 | None ->
3048 state.text <- qsearch ^ " [not found]";
3049 m_active, m_first
3050 | Some active ->
3051 state.text <- qsearch;
3052 active, firstof m_first active
3054 G.postRedisplay "listview backspace qsearch";
3055 set1 active first qsearch
3058 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3059 let pattern = m_qsearch ^ Wsi.toutf8 key in
3060 let active, first =
3061 match search m_active pattern 1 with
3062 | None ->
3063 state.text <- pattern ^ " [not found]";
3064 m_active, m_first
3065 | Some active ->
3066 state.text <- pattern;
3067 active, firstof m_first active
3069 G.postRedisplay "listview qsearch add";
3070 set1 active first pattern;
3072 | 0xff1b -> (* escape *)
3073 state.text <- "";
3074 if String.length m_qsearch = 0
3075 then (
3076 G.postRedisplay "list view escape";
3077 begin
3078 match
3079 source#exit (coe self) true m_active m_first m_pan m_qsearch
3080 with
3081 | None -> m_prev_uioh
3082 | Some uioh -> uioh
3085 else (
3086 G.postRedisplay "list view kill qsearch";
3087 source#setqsearch "";
3088 coe {< m_qsearch = "" >}
3091 | 0xff0d -> (* return *)
3092 state.text <- "";
3093 let self = {< m_qsearch = "" >} in
3094 source#setqsearch "";
3095 let opt =
3096 G.postRedisplay "listview enter";
3097 if m_active >= 0 && m_active < source#getitemcount
3098 then (
3099 source#exit (coe self) false m_active m_first m_pan "";
3101 else (
3102 source#exit (coe self) true m_active m_first m_pan "";
3105 begin match opt with
3106 | None -> m_prev_uioh
3107 | Some uioh -> uioh
3110 | 0xff9f | 0xffff -> (* delete *)
3111 coe self
3113 | 0xff52 -> navigate ~-1 (* up *)
3114 | 0xff54 -> navigate 1 (* down *)
3115 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3116 | 0xff56 -> navigate fstate.maxrows (* next *)
3118 | 0xff53 -> (* right *)
3119 state.text <- "";
3120 G.postRedisplay "listview right";
3121 coe {< m_pan = m_pan - 1 >}
3123 | 0xff51 -> (* left *)
3124 state.text <- "";
3125 G.postRedisplay "listview left";
3126 coe {< m_pan = m_pan + 1 >}
3128 | 0xff50 -> (* home *)
3129 let active = find 0 1 in
3130 G.postRedisplay "listview home";
3131 set active 0;
3133 | 0xff57 -> (* end *)
3134 let first = max 0 (itemcount - fstate.maxrows) in
3135 let active = find (itemcount - 1) ~-1 in
3136 G.postRedisplay "listview end";
3137 set active first;
3139 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3140 coe self
3142 | _ ->
3143 dolog "listview unknown key %#x" key; coe self
3145 method key key mask =
3146 match state.mode with
3147 | Textentry te -> textentrykeyboard key mask te; coe self
3148 | _ -> self#key1 key mask
3150 method button button down x y _ =
3151 let opt =
3152 match button with
3153 | 1 when x > conf.winw - conf.scrollbw ->
3154 G.postRedisplay "listview scroll";
3155 if down
3156 then
3157 let _, position, sh = self#scrollph in
3158 if y > truncate position && y < truncate (position +. sh)
3159 then (
3160 state.mstate <- Mscrolly;
3161 Some (coe self)
3163 else
3164 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3165 let first = truncate (s *. float source#getitemcount) in
3166 let first = min source#getitemcount first in
3167 Some (coe {< m_first = first; m_active = first >})
3168 else (
3169 state.mstate <- Mnone;
3170 Some (coe self);
3172 | 1 when not down ->
3173 begin match self#elemunder y with
3174 | Some n ->
3175 G.postRedisplay "listview click";
3176 source#exit
3177 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3178 | _ ->
3179 Some (coe self)
3181 | n when (n == 4 || n == 5) && not down ->
3182 let len = source#getitemcount in
3183 let first =
3184 if n = 5 && m_first + fstate.maxrows >= len
3185 then
3186 m_first
3187 else
3188 let first = m_first + (if n == 4 then -1 else 1) in
3189 bound first 0 (len - 1)
3191 G.postRedisplay "listview wheel";
3192 Some (coe {< m_first = first >})
3193 | _ ->
3194 Some (coe self)
3196 match opt with
3197 | None -> m_prev_uioh
3198 | Some uioh -> uioh
3200 method motion _ y =
3201 match state.mstate with
3202 | Mscrolly ->
3203 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3204 let first = truncate (s *. float source#getitemcount) in
3205 let first = min source#getitemcount first in
3206 G.postRedisplay "listview motion";
3207 coe {< m_first = first; m_active = first >}
3208 | _ -> coe self
3210 method pmotion x y =
3211 if x < conf.winw - conf.scrollbw
3212 then
3213 let n =
3214 match self#elemunder y with
3215 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3216 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3218 let o =
3219 if n != m_active
3220 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3221 else self
3223 coe o
3224 else (
3225 Wsi.setcursor Wsi.CURSOR_INHERIT;
3226 coe self
3229 method infochanged _ = ()
3231 method scrollpw = (0, 0.0, 0.0)
3232 method scrollph =
3233 let nfs = fstate.fontsize + 1 in
3234 let y = m_first * nfs in
3235 let itemcount = source#getitemcount in
3236 let maxi = max 0 (itemcount - fstate.maxrows) in
3237 let maxy = maxi * nfs in
3238 let p, h = scrollph y maxy in
3239 conf.scrollbw, p, h
3241 method modehash = modehash
3242 end;;
3244 class outlinelistview ~source =
3245 object (self)
3246 inherit listview
3247 ~source:(source :> lvsource)
3248 ~trusted:false
3249 ~modehash:(findkeyhash conf "outline")
3250 as super
3252 method key key mask =
3253 let calcfirst first active =
3254 if active > first
3255 then
3256 let rows = active - first in
3257 if rows > fstate.maxrows then active - fstate.maxrows else first
3258 else active
3260 let navigate incr =
3261 let active = m_active + incr in
3262 let active = bound active 0 (source#getitemcount - 1) in
3263 let first = calcfirst m_first active in
3264 G.postRedisplay "outline navigate";
3265 coe {< m_active = active; m_first = first >}
3267 let ctrl = Wsi.withctrl mask in
3268 match key with
3269 | 110 when ctrl -> (* ctrl-n *)
3270 source#narrow m_qsearch;
3271 G.postRedisplay "outline ctrl-n";
3272 coe {< m_first = 0; m_active = 0 >}
3274 | 117 when ctrl -> (* ctrl-u *)
3275 source#denarrow;
3276 G.postRedisplay "outline ctrl-u";
3277 state.text <- "";
3278 coe {< m_first = 0; m_active = 0 >}
3280 | 108 when ctrl -> (* ctrl-l *)
3281 let first = m_active - (fstate.maxrows / 2) in
3282 G.postRedisplay "outline ctrl-l";
3283 coe {< m_first = first >}
3285 | 0xff9f | 0xffff -> (* delete *)
3286 source#remove m_active;
3287 G.postRedisplay "outline delete";
3288 let active = max 0 (m_active-1) in
3289 coe {< m_first = firstof m_first active;
3290 m_active = active >}
3292 | 0xff52 -> navigate ~-1 (* up *)
3293 | 0xff54 -> navigate 1 (* down *)
3294 | 0xff55 -> (* prior *)
3295 navigate ~-(fstate.maxrows)
3296 | 0xff56 -> (* next *)
3297 navigate fstate.maxrows
3299 | 0xff53 -> (* [ctrl-]right *)
3300 let o =
3301 if ctrl
3302 then (
3303 G.postRedisplay "outline ctrl right";
3304 {< m_pan = m_pan + 1 >}
3306 else self#updownlevel 1
3308 coe o
3310 | 0xff51 -> (* [ctrl-]left *)
3311 let o =
3312 if ctrl
3313 then (
3314 G.postRedisplay "outline ctrl left";
3315 {< m_pan = m_pan - 1 >}
3317 else self#updownlevel ~-1
3319 coe o
3321 | 0xff50 -> (* home *)
3322 G.postRedisplay "outline home";
3323 coe {< m_first = 0; m_active = 0 >}
3325 | 0xff57 -> (* end *)
3326 let active = source#getitemcount - 1 in
3327 let first = max 0 (active - fstate.maxrows) in
3328 G.postRedisplay "outline end";
3329 coe {< m_active = active; m_first = first >}
3331 | _ -> super#key key mask
3334 let outlinesource usebookmarks =
3335 let empty = [||] in
3336 (object
3337 inherit lvsourcebase
3338 val mutable m_items = empty
3339 val mutable m_orig_items = empty
3340 val mutable m_prev_items = empty
3341 val mutable m_narrow_pattern = ""
3342 val mutable m_hadremovals = false
3344 method getitemcount =
3345 Array.length m_items + (if m_hadremovals then 1 else 0)
3347 method getitem n =
3348 if n == Array.length m_items && m_hadremovals
3349 then
3350 ("[Confirm removal]", 0)
3351 else
3352 let s, n, _ = m_items.(n) in
3353 (s, n)
3355 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3356 ignore (uioh, first, qsearch);
3357 let confrimremoval = m_hadremovals && active = Array.length m_items in
3358 let items =
3359 if String.length m_narrow_pattern = 0
3360 then m_orig_items
3361 else m_items
3363 if not cancel
3364 then (
3365 if not confrimremoval
3366 then(
3367 let _, _, anchor = m_items.(active) in
3368 gotoanchor anchor;
3369 m_items <- items;
3371 else (
3372 state.bookmarks <- Array.to_list m_items;
3373 m_orig_items <- m_items;
3376 else m_items <- items;
3377 m_pan <- pan;
3378 None
3380 method hasaction _ = true
3382 method greetmsg =
3383 if Array.length m_items != Array.length m_orig_items
3384 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3385 else ""
3387 method narrow pattern =
3388 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3389 match reopt with
3390 | None -> ()
3391 | Some re ->
3392 let rec loop accu n =
3393 if n = -1
3394 then (
3395 m_narrow_pattern <- pattern;
3396 m_items <- Array.of_list accu
3398 else
3399 let (s, _, _) as o = m_items.(n) in
3400 let accu =
3401 if (try ignore (Str.search_forward re s 0); true
3402 with Not_found -> false)
3403 then o :: accu
3404 else accu
3406 loop accu (n-1)
3408 loop [] (Array.length m_items - 1)
3410 method denarrow =
3411 m_orig_items <- (
3412 if usebookmarks
3413 then Array.of_list state.bookmarks
3414 else state.outlines
3416 m_items <- m_orig_items
3418 method remove m =
3419 if usebookmarks
3420 then
3421 if m >= 0 && m < Array.length m_items
3422 then (
3423 m_hadremovals <- true;
3424 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3425 let n = if n >= m then n+1 else n in
3426 m_items.(n)
3430 method reset anchor items =
3431 m_hadremovals <- false;
3432 if m_orig_items == empty || m_prev_items != items
3433 then (
3434 m_orig_items <- items;
3435 if String.length m_narrow_pattern = 0
3436 then m_items <- items;
3438 m_prev_items <- items;
3439 let rely = getanchory anchor in
3440 let active =
3441 let rec loop n best bestd =
3442 if n = Array.length m_items
3443 then best
3444 else
3445 let (_, _, anchor) = m_items.(n) in
3446 let orely = getanchory anchor in
3447 let d = abs (orely - rely) in
3448 if d < bestd
3449 then loop (n+1) n d
3450 else loop (n+1) best bestd
3452 loop 0 ~-1 max_int
3454 m_active <- active;
3455 m_first <- firstof m_first active
3456 end)
3459 let enterselector usebookmarks =
3460 let source = outlinesource usebookmarks in
3461 fun errmsg ->
3462 let outlines =
3463 if usebookmarks
3464 then Array.of_list state.bookmarks
3465 else state.outlines
3467 if Array.length outlines = 0
3468 then (
3469 showtext ' ' errmsg;
3471 else (
3472 state.text <- source#greetmsg;
3473 Wsi.setcursor Wsi.CURSOR_INHERIT;
3474 let anchor = getanchor () in
3475 source#reset anchor outlines;
3476 state.uioh <- coe (new outlinelistview ~source);
3477 G.postRedisplay "enter selector";
3481 let enteroutlinemode =
3482 let f = enterselector false in
3483 fun ()-> f "Document has no outline";
3486 let enterbookmarkmode =
3487 let f = enterselector true in
3488 fun () -> f "Document has no bookmarks (yet)";
3491 let color_of_string s =
3492 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3493 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3497 let color_to_string (r, g, b) =
3498 let r = truncate (r *. 256.0)
3499 and g = truncate (g *. 256.0)
3500 and b = truncate (b *. 256.0) in
3501 Printf.sprintf "%d/%d/%d" r g b
3504 let irect_of_string s =
3505 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3508 let irect_to_string (x0,y0,x1,y1) =
3509 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3512 let makecheckers () =
3513 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3514 following to say:
3515 converted by Issac Trotts. July 25, 2002 *)
3516 let image_height = 64
3517 and image_width = 64 in
3519 let make_image () =
3520 let image =
3521 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3523 for i = 0 to image_width - 1 do
3524 for j = 0 to image_height - 1 do
3525 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3526 (if (i land 8 ) lxor (j land 8) = 0
3527 then [|255;255;255|] else [|200;200;200|])
3528 done
3529 done;
3530 image
3532 let image = make_image () in
3533 let id = GlTex.gen_texture () in
3534 GlTex.bind_texture `texture_2d id;
3535 GlPix.store (`unpack_alignment 1);
3536 GlTex.image2d image;
3537 List.iter (GlTex.parameter ~target:`texture_2d)
3538 [ `wrap_s `repeat;
3539 `wrap_t `repeat;
3540 `mag_filter `nearest;
3541 `min_filter `nearest ];
3545 let setcheckers enabled =
3546 match state.texid with
3547 | None ->
3548 if enabled then state.texid <- Some (makecheckers ())
3550 | Some texid ->
3551 if not enabled
3552 then (
3553 GlTex.delete_texture texid;
3554 state.texid <- None;
3558 let int_of_string_with_suffix s =
3559 let l = String.length s in
3560 let s1, shift =
3561 if l > 1
3562 then
3563 let suffix = Char.lowercase s.[l-1] in
3564 match suffix with
3565 | 'k' -> String.sub s 0 (l-1), 10
3566 | 'm' -> String.sub s 0 (l-1), 20
3567 | 'g' -> String.sub s 0 (l-1), 30
3568 | _ -> s, 0
3569 else s, 0
3571 let n = int_of_string s1 in
3572 let m = n lsl shift in
3573 if m < 0 || m < n
3574 then raise (Failure "value too large")
3575 else m
3578 let string_with_suffix_of_int n =
3579 if n = 0
3580 then "0"
3581 else
3582 let n, s =
3583 if n = 0
3584 then 0, ""
3585 else (
3586 if n land ((1 lsl 20) - 1) = 0
3587 then n lsr 20, "M"
3588 else (
3589 if n land ((1 lsl 10) - 1) = 0
3590 then n lsr 10, "K"
3591 else n, ""
3595 let rec loop s n =
3596 let h = n mod 1000 in
3597 let n = n / 1000 in
3598 if n = 0
3599 then string_of_int h ^ s
3600 else (
3601 let s = Printf.sprintf "_%03d%s" h s in
3602 loop s n
3605 loop "" n ^ s;
3608 let defghyllscroll = (40, 8, 32);;
3609 let ghyllscroll_of_string s =
3610 let (n, a, b) as nab =
3611 if s = "default"
3612 then defghyllscroll
3613 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3615 if n <= a || n <= b || a >= b
3616 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3617 nab;
3620 let ghyllscroll_to_string ((n, a, b) as nab) =
3621 if nab = defghyllscroll
3622 then "default"
3623 else Printf.sprintf "%d,%d,%d" n a b;
3626 let describe_location () =
3627 let f (fn, _) l =
3628 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3630 let fn, ln = List.fold_left f (-1, -1) state.layout in
3631 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3632 let percent =
3633 if maxy <= 0
3634 then 100.
3635 else (100. *. (float state.y /. float maxy))
3637 if fn = ln
3638 then
3639 Printf.sprintf "page %d of %d [%.2f%%]"
3640 (fn+1) state.pagecount percent
3641 else
3642 Printf.sprintf
3643 "pages %d-%d of %d [%.2f%%]"
3644 (fn+1) (ln+1) state.pagecount percent
3647 let enterinfomode =
3648 let btos b = if b then "\xe2\x88\x9a" else "" in
3649 let showextended = ref false in
3650 let leave mode = function
3651 | Confirm -> state.mode <- mode
3652 | Cancel -> state.mode <- mode in
3653 let src =
3654 (object
3655 val mutable m_first_time = true
3656 val mutable m_l = []
3657 val mutable m_a = [||]
3658 val mutable m_prev_uioh = nouioh
3659 val mutable m_prev_mode = View
3661 inherit lvsourcebase
3663 method reset prev_mode prev_uioh =
3664 m_a <- Array.of_list (List.rev m_l);
3665 m_l <- [];
3666 m_prev_mode <- prev_mode;
3667 m_prev_uioh <- prev_uioh;
3668 if m_first_time
3669 then (
3670 let rec loop n =
3671 if n >= Array.length m_a
3672 then ()
3673 else
3674 match m_a.(n) with
3675 | _, _, _, Action _ -> m_active <- n
3676 | _ -> loop (n+1)
3678 loop 0;
3679 m_first_time <- false;
3682 method int name get set =
3683 m_l <-
3684 (name, `int get, 1, Action (
3685 fun u ->
3686 let ondone s =
3687 try set (int_of_string s)
3688 with exn ->
3689 state.text <- Printf.sprintf "bad integer `%s': %s"
3690 s (Printexc.to_string exn)
3692 state.text <- "";
3693 let te = name ^ ": ", "", None, intentry, ondone in
3694 state.mode <- Textentry (te, leave m_prev_mode);
3696 )) :: m_l
3698 method int_with_suffix name get set =
3699 m_l <-
3700 (name, `intws get, 1, Action (
3701 fun u ->
3702 let ondone s =
3703 try set (int_of_string_with_suffix s)
3704 with exn ->
3705 state.text <- Printf.sprintf "bad integer `%s': %s"
3706 s (Printexc.to_string exn)
3708 state.text <- "";
3709 let te =
3710 name ^ ": ", "", None, intentry_with_suffix, ondone
3712 state.mode <- Textentry (te, leave m_prev_mode);
3714 )) :: m_l
3716 method bool ?(offset=1) ?(btos=btos) name get set =
3717 m_l <-
3718 (name, `bool (btos, get), offset, Action (
3719 fun u ->
3720 let v = get () in
3721 set (not v);
3723 )) :: m_l
3725 method color name get set =
3726 m_l <-
3727 (name, `color get, 1, Action (
3728 fun u ->
3729 let invalid = (nan, nan, nan) in
3730 let ondone s =
3731 let c =
3732 try color_of_string s
3733 with exn ->
3734 state.text <- Printf.sprintf "bad color `%s': %s"
3735 s (Printexc.to_string exn);
3736 invalid
3738 if c <> invalid
3739 then set c;
3741 let te = name ^ ": ", "", None, textentry, ondone in
3742 state.text <- color_to_string (get ());
3743 state.mode <- Textentry (te, leave m_prev_mode);
3745 )) :: m_l
3747 method string name get set =
3748 m_l <-
3749 (name, `string get, 1, Action (
3750 fun u ->
3751 let ondone s = set s in
3752 let te = name ^ ": ", "", None, textentry, ondone in
3753 state.mode <- Textentry (te, leave m_prev_mode);
3755 )) :: m_l
3757 method colorspace name get set =
3758 m_l <-
3759 (name, `string get, 1, Action (
3760 fun _ ->
3761 let source =
3762 let vals = [| "rgb"; "bgr"; "gray" |] in
3763 (object
3764 inherit lvsourcebase
3766 initializer
3767 m_active <- int_of_colorspace conf.colorspace;
3768 m_first <- 0;
3770 method getitemcount = Array.length vals
3771 method getitem n = (vals.(n), 0)
3772 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3773 ignore (uioh, first, pan, qsearch);
3774 if not cancel then set active;
3775 None
3776 method hasaction _ = true
3777 end)
3779 state.text <- "";
3780 let modehash = findkeyhash conf "info" in
3781 coe (new listview ~source ~trusted:true ~modehash)
3782 )) :: m_l
3784 method caption s offset =
3785 m_l <- (s, `empty, offset, Noaction) :: m_l
3787 method caption2 s f offset =
3788 m_l <- (s, `string f, offset, Noaction) :: m_l
3790 method getitemcount = Array.length m_a
3792 method getitem n =
3793 let tostr = function
3794 | `int f -> string_of_int (f ())
3795 | `intws f -> string_with_suffix_of_int (f ())
3796 | `string f -> f ()
3797 | `color f -> color_to_string (f ())
3798 | `bool (btos, f) -> btos (f ())
3799 | `empty -> ""
3801 let name, t, offset, _ = m_a.(n) in
3802 ((let s = tostr t in
3803 if String.length s > 0
3804 then Printf.sprintf "%s\t%s" name s
3805 else name),
3806 offset)
3808 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3809 let uiohopt =
3810 if not cancel
3811 then (
3812 m_qsearch <- qsearch;
3813 let uioh =
3814 match m_a.(active) with
3815 | _, _, _, Action f -> f uioh
3816 | _ -> uioh
3818 Some uioh
3820 else None
3822 m_active <- active;
3823 m_first <- first;
3824 m_pan <- pan;
3825 uiohopt
3827 method hasaction n =
3828 match m_a.(n) with
3829 | _, _, _, Action _ -> true
3830 | _ -> false
3831 end)
3833 let rec fillsrc prevmode prevuioh =
3834 let sep () = src#caption "" 0 in
3835 let colorp name get set =
3836 src#string name
3837 (fun () -> color_to_string (get ()))
3838 (fun v ->
3840 let c = color_of_string v in
3841 set c
3842 with exn ->
3843 state.text <- Printf.sprintf "bad color `%s': %s"
3844 v (Printexc.to_string exn);
3847 let oldmode = state.mode in
3848 let birdseye = isbirdseye state.mode in
3850 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3852 src#bool "presentation mode"
3853 (fun () -> conf.presentation)
3854 (fun v ->
3855 conf.presentation <- v;
3856 state.anchor <- getanchor ();
3857 represent ());
3859 src#bool "ignore case in searches"
3860 (fun () -> conf.icase)
3861 (fun v -> conf.icase <- v);
3863 src#bool "preload"
3864 (fun () -> conf.preload)
3865 (fun v -> conf.preload <- v);
3867 src#bool "highlight links"
3868 (fun () -> conf.hlinks)
3869 (fun v -> conf.hlinks <- v);
3871 src#bool "under info"
3872 (fun () -> conf.underinfo)
3873 (fun v -> conf.underinfo <- v);
3875 src#bool "persistent bookmarks"
3876 (fun () -> conf.savebmarks)
3877 (fun v -> conf.savebmarks <- v);
3879 src#bool "proportional display"
3880 (fun () -> conf.proportional)
3881 (fun v -> reqlayout conf.angle v);
3883 src#bool "trim margins"
3884 (fun () -> conf.trimmargins)
3885 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3887 src#bool "persistent location"
3888 (fun () -> conf.jumpback)
3889 (fun v -> conf.jumpback <- v);
3891 sep ();
3892 src#int "inter-page space"
3893 (fun () -> conf.interpagespace)
3894 (fun n ->
3895 conf.interpagespace <- n;
3896 let pageno, py =
3897 match state.layout with
3898 | [] -> 0, 0
3899 | l :: _ ->
3900 l.pageno, l.pagey
3902 state.maxy <- calcheight ();
3903 let y = getpagey pageno in
3904 gotoy (y + py)
3907 src#int "page bias"
3908 (fun () -> conf.pagebias)
3909 (fun v -> conf.pagebias <- v);
3911 src#int "scroll step"
3912 (fun () -> conf.scrollstep)
3913 (fun n -> conf.scrollstep <- n);
3915 src#int "auto scroll step"
3916 (fun () ->
3917 match state.autoscroll with
3918 | Some step -> step
3919 | _ -> conf.autoscrollstep)
3920 (fun n ->
3921 if state.autoscroll <> None
3922 then state.autoscroll <- Some n;
3923 conf.autoscrollstep <- n);
3925 src#int "zoom"
3926 (fun () -> truncate (conf.zoom *. 100.))
3927 (fun v -> setzoom ((float v) /. 100.));
3929 src#int "rotation"
3930 (fun () -> conf.angle)
3931 (fun v -> reqlayout v conf.proportional);
3933 src#int "scroll bar width"
3934 (fun () -> state.scrollw)
3935 (fun v ->
3936 state.scrollw <- v;
3937 conf.scrollbw <- v;
3938 reshape conf.winw conf.winh;
3941 src#int "scroll handle height"
3942 (fun () -> conf.scrollh)
3943 (fun v -> conf.scrollh <- v;);
3945 src#int "thumbnail width"
3946 (fun () -> conf.thumbw)
3947 (fun v ->
3948 conf.thumbw <- min 4096 v;
3949 match oldmode with
3950 | Birdseye beye ->
3951 leavebirdseye beye false;
3952 enterbirdseye ()
3953 | _ -> ()
3956 src#string "columns"
3957 (fun () ->
3958 match conf.columns with
3959 | None -> "1"
3960 | Some (multicol, _) -> columns_to_string multicol)
3961 (fun v ->
3962 let n, a, b = columns_of_string v in
3963 setcolumns n a b);
3965 sep ();
3966 src#caption "Presentation mode" 0;
3967 src#bool "scrollbar visible"
3968 (fun () -> conf.scrollbarinpm)
3969 (fun v ->
3970 if v != conf.scrollbarinpm
3971 then (
3972 conf.scrollbarinpm <- v;
3973 if conf.presentation
3974 then (
3975 state.scrollw <- if v then conf.scrollbw else 0;
3976 reshape conf.winw conf.winh;
3981 sep ();
3982 src#caption "Pixmap cache" 0;
3983 src#int_with_suffix "size (advisory)"
3984 (fun () -> conf.memlimit)
3985 (fun v -> conf.memlimit <- v);
3987 src#caption2 "used"
3988 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3989 (string_with_suffix_of_int state.memused)
3990 (Hashtbl.length state.tilemap)) 1;
3992 sep ();
3993 src#caption "Layout" 0;
3994 src#caption2 "Dimension"
3995 (fun () ->
3996 Printf.sprintf "%dx%d (virtual %dx%d)"
3997 conf.winw conf.winh
3998 state.w state.maxy)
4000 if conf.debug
4001 then
4002 src#caption2 "Position" (fun () ->
4003 Printf.sprintf "%dx%d" state.x state.y
4005 else
4006 src#caption2 "Visible" (fun () -> describe_location ()) 1
4009 sep ();
4010 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4011 "Save these parameters as global defaults at exit"
4012 (fun () -> conf.bedefault)
4013 (fun v -> conf.bedefault <- v)
4016 sep ();
4017 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4018 src#bool ~offset:0 ~btos "Extended parameters"
4019 (fun () -> !showextended)
4020 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4021 if !showextended
4022 then (
4023 src#bool "checkers"
4024 (fun () -> conf.checkers)
4025 (fun v -> conf.checkers <- v; setcheckers v);
4026 src#bool "update cursor"
4027 (fun () -> conf.updatecurs)
4028 (fun v -> conf.updatecurs <- v);
4029 src#bool "verbose"
4030 (fun () -> conf.verbose)
4031 (fun v -> conf.verbose <- v);
4032 src#bool "invert colors"
4033 (fun () -> conf.invert)
4034 (fun v -> conf.invert <- v);
4035 src#bool "max fit"
4036 (fun () -> conf.maxhfit)
4037 (fun v -> conf.maxhfit <- v);
4038 src#bool "redirect stderr"
4039 (fun () -> conf.redirectstderr)
4040 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4041 src#string "uri launcher"
4042 (fun () -> conf.urilauncher)
4043 (fun v -> conf.urilauncher <- v);
4044 src#string "path launcher"
4045 (fun () -> conf.pathlauncher)
4046 (fun v -> conf.pathlauncher <- v);
4047 src#string "tile size"
4048 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4049 (fun v ->
4051 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4052 conf.tileh <- max 64 w;
4053 conf.tilew <- max 64 h;
4054 flushtiles ();
4055 with exn ->
4056 state.text <- Printf.sprintf "bad tile size `%s': %s"
4057 v (Printexc.to_string exn));
4058 src#int "texture count"
4059 (fun () -> conf.texcount)
4060 (fun v ->
4061 if realloctexts v
4062 then conf.texcount <- v
4063 else showtext '!' " Failed to set texture count please retry later"
4065 src#int "slice height"
4066 (fun () -> conf.sliceheight)
4067 (fun v ->
4068 conf.sliceheight <- v;
4069 wcmd "sliceh %d" conf.sliceheight;
4071 src#int "anti-aliasing level"
4072 (fun () -> conf.aalevel)
4073 (fun v ->
4074 conf.aalevel <- bound v 0 8;
4075 state.anchor <- getanchor ();
4076 opendoc state.path state.password;
4078 src#int "ui font size"
4079 (fun () -> fstate.fontsize)
4080 (fun v -> setfontsize (bound v 5 100));
4081 colorp "background color"
4082 (fun () -> conf.bgcolor)
4083 (fun v -> conf.bgcolor <- v);
4084 src#bool "crop hack"
4085 (fun () -> conf.crophack)
4086 (fun v -> conf.crophack <- v);
4087 src#string "trim fuzz"
4088 (fun () -> irect_to_string conf.trimfuzz)
4089 (fun v ->
4091 conf.trimfuzz <- irect_of_string v;
4092 if conf.trimmargins
4093 then settrim true conf.trimfuzz;
4094 with exn ->
4095 state.text <- Printf.sprintf "bad irect `%s': %s"
4096 v (Printexc.to_string exn)
4098 src#string "throttle"
4099 (fun () ->
4100 match conf.maxwait with
4101 | None -> "show place holder if page is not ready"
4102 | Some time ->
4103 if time = infinity
4104 then "wait for page to fully render"
4105 else
4106 "wait " ^ string_of_float time
4107 ^ " seconds before showing placeholder"
4109 (fun v ->
4111 let f = float_of_string v in
4112 if f <= 0.0
4113 then conf.maxwait <- None
4114 else conf.maxwait <- Some f
4115 with exn ->
4116 state.text <- Printf.sprintf "bad time `%s': %s"
4117 v (Printexc.to_string exn)
4119 src#string "ghyll scroll"
4120 (fun () ->
4121 match conf.ghyllscroll with
4122 | None -> ""
4123 | Some nab -> ghyllscroll_to_string nab
4125 (fun v ->
4127 let gs =
4128 if String.length v = 0
4129 then None
4130 else Some (ghyllscroll_of_string v)
4132 conf.ghyllscroll <- gs
4133 with exn ->
4134 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4135 v (Printexc.to_string exn)
4137 src#string "selection command"
4138 (fun () -> conf.selcmd)
4139 (fun v -> conf.selcmd <- v);
4140 src#colorspace "color space"
4141 (fun () -> colorspace_to_string conf.colorspace)
4142 (fun v ->
4143 conf.colorspace <- colorspace_of_int v;
4144 wcmd "cs %d" v;
4145 load state.layout;
4149 sep ();
4150 src#caption "Document" 0;
4151 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4152 src#caption2 "Pages"
4153 (fun () -> string_of_int state.pagecount) 1;
4154 src#caption2 "Dimensions"
4155 (fun () -> string_of_int (List.length state.pdims)) 1;
4156 if conf.trimmargins
4157 then (
4158 sep ();
4159 src#caption "Trimmed margins" 0;
4160 src#caption2 "Dimensions"
4161 (fun () -> string_of_int (List.length state.pdims)) 1;
4164 src#reset prevmode prevuioh;
4166 fun () ->
4167 state.text <- "";
4168 let prevmode = state.mode
4169 and prevuioh = state.uioh in
4170 fillsrc prevmode prevuioh;
4171 let source = (src :> lvsource) in
4172 let modehash = findkeyhash conf "info" in
4173 state.uioh <- coe (object (self)
4174 inherit listview ~source ~trusted:true ~modehash as super
4175 val mutable m_prevmemused = 0
4176 method infochanged = function
4177 | Memused ->
4178 if m_prevmemused != state.memused
4179 then (
4180 m_prevmemused <- state.memused;
4181 G.postRedisplay "memusedchanged";
4183 | Pdim -> G.postRedisplay "pdimchanged"
4184 | Docinfo -> fillsrc prevmode prevuioh
4186 method key key mask =
4187 if not (Wsi.withctrl mask)
4188 then
4189 match key with
4190 | 0xff51 -> coe (self#updownlevel ~-1)
4191 | 0xff53 -> coe (self#updownlevel 1)
4192 | _ -> super#key key mask
4193 else super#key key mask
4194 end);
4195 G.postRedisplay "info";
4198 let enterhelpmode =
4199 let source =
4200 (object
4201 inherit lvsourcebase
4202 method getitemcount = Array.length state.help
4203 method getitem n =
4204 let s, n, _ = state.help.(n) in
4205 (s, n)
4207 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4208 let optuioh =
4209 if not cancel
4210 then (
4211 m_qsearch <- qsearch;
4212 match state.help.(active) with
4213 | _, _, Action f -> Some (f uioh)
4214 | _ -> Some (uioh)
4216 else None
4218 m_active <- active;
4219 m_first <- first;
4220 m_pan <- pan;
4221 optuioh
4223 method hasaction n =
4224 match state.help.(n) with
4225 | _, _, Action _ -> true
4226 | _ -> false
4228 initializer
4229 m_active <- -1
4230 end)
4231 in fun () ->
4232 let modehash = findkeyhash conf "help" in
4233 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4234 G.postRedisplay "help";
4237 let entermsgsmode =
4238 let msgsource =
4239 let re = Str.regexp "[\r\n]" in
4240 (object
4241 inherit lvsourcebase
4242 val mutable m_items = [||]
4244 method getitemcount = 1 + Array.length m_items
4246 method getitem n =
4247 if n = 0
4248 then "[Clear]", 0
4249 else m_items.(n-1), 0
4251 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4252 ignore uioh;
4253 if not cancel
4254 then (
4255 if active = 0
4256 then Buffer.clear state.errmsgs;
4257 m_qsearch <- qsearch;
4259 m_active <- active;
4260 m_first <- first;
4261 m_pan <- pan;
4262 None
4264 method hasaction n =
4265 n = 0
4267 method reset =
4268 state.newerrmsgs <- false;
4269 let l = Str.split re (Buffer.contents state.errmsgs) in
4270 m_items <- Array.of_list l
4272 initializer
4273 m_active <- 0
4274 end)
4275 in fun () ->
4276 state.text <- "";
4277 msgsource#reset;
4278 let source = (msgsource :> lvsource) in
4279 let modehash = findkeyhash conf "listview" in
4280 state.uioh <- coe (object
4281 inherit listview ~source ~trusted:false ~modehash as super
4282 method display =
4283 if state.newerrmsgs
4284 then msgsource#reset;
4285 super#display
4286 end);
4287 G.postRedisplay "msgs";
4290 let quickbookmark ?title () =
4291 match state.layout with
4292 | [] -> ()
4293 | l :: _ ->
4294 let title =
4295 match title with
4296 | None ->
4297 let sec = Unix.gettimeofday () in
4298 let tm = Unix.localtime sec in
4299 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4300 (l.pageno+1)
4301 tm.Unix.tm_mday
4302 tm.Unix.tm_mon
4303 (tm.Unix.tm_year + 1900)
4304 tm.Unix.tm_hour
4305 tm.Unix.tm_min
4306 | Some title -> title
4308 state.bookmarks <-
4309 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4310 :: state.bookmarks
4313 let doreshape w h =
4314 state.fullscreen <- None;
4315 Wsi.reshape w h;
4318 let setautoscrollspeed step goingdown =
4319 let incr = max 1 ((abs step) / 2) in
4320 let incr = if goingdown then incr else -incr in
4321 let astep = step + incr in
4322 state.autoscroll <- Some astep;
4325 let gotounder = function
4326 | Ulinkgoto (pageno, top) ->
4327 if pageno >= 0
4328 then (
4329 addnav ();
4330 gotopage1 pageno top;
4333 | Ulinkuri s ->
4334 gotouri s
4336 | Uremote (filename, pageno) ->
4337 let path =
4338 if Sys.file_exists filename
4339 then filename
4340 else
4341 let dir = Filename.dirname state.path in
4342 let path = Filename.concat dir filename in
4343 if Sys.file_exists path
4344 then path
4345 else ""
4347 if String.length path > 0
4348 then (
4349 let anchor = getanchor () in
4350 let ranchor = state.path, state.password, anchor in
4351 state.anchor <- (pageno, 0.0);
4352 state.ranchors <- ranchor :: state.ranchors;
4353 opendoc path "";
4355 else showtext '!' ("Could not find " ^ filename)
4357 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4360 let viewkeyboard key mask =
4361 let enttext te =
4362 let mode = state.mode in
4363 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4364 state.text <- "";
4365 enttext ();
4366 G.postRedisplay "view:enttext"
4368 let ctrl = Wsi.withctrl mask in
4369 match key with
4370 | 81 -> (* Q *)
4371 exit 0
4373 | 0xff63 -> (* insert *)
4374 if conf.angle mod 360 = 0
4375 then (
4376 state.mode <- LinkNav (Ltgendir 0);
4377 gotoy state.y;
4379 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4381 | 0xff1b | 113 -> (* escape / q *)
4382 begin match state.mstate with
4383 | Mzoomrect _ ->
4384 state.mstate <- Mnone;
4385 Wsi.setcursor Wsi.CURSOR_INHERIT;
4386 G.postRedisplay "kill zoom rect";
4387 | _ ->
4388 match state.ranchors with
4389 | [] -> raise Quit
4390 | (path, password, anchor) :: rest ->
4391 state.ranchors <- rest;
4392 state.anchor <- anchor;
4393 opendoc path password
4394 end;
4396 | 0xff08 -> (* backspace *)
4397 let y = getnav ~-1 in
4398 gotoy_and_clear_text y
4400 | 111 -> (* o *)
4401 enteroutlinemode ()
4403 | 117 -> (* u *)
4404 state.rects <- [];
4405 state.text <- "";
4406 G.postRedisplay "dehighlight";
4408 | 47 | 63 -> (* / ? *)
4409 let ondone isforw s =
4410 cbput state.hists.pat s;
4411 state.searchpattern <- s;
4412 search s isforw
4414 let s = String.create 1 in
4415 s.[0] <- Char.chr key;
4416 enttext (s, "", Some (onhist state.hists.pat),
4417 textentry, ondone (key = 47))
4419 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4420 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4421 setzoom (conf.zoom +. incr)
4423 | 43 | 0xffab -> (* + *)
4424 let ondone s =
4425 let n =
4426 try int_of_string s with exc ->
4427 state.text <- Printf.sprintf "bad integer `%s': %s"
4428 s (Printexc.to_string exc);
4429 max_int
4431 if n != max_int
4432 then (
4433 conf.pagebias <- n;
4434 state.text <- "page bias is now " ^ string_of_int n;
4437 enttext ("page bias: ", "", None, intentry, ondone)
4439 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4440 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4441 setzoom (max 0.01 (conf.zoom -. decr))
4443 | 45 | 0xffad -> (* - *)
4444 let ondone msg = state.text <- msg in
4445 enttext (
4446 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4447 optentry state.mode, ondone
4450 | 48 when ctrl -> (* ctrl-0 *)
4451 setzoom 1.0
4453 | 49 when ctrl -> (* 1 *)
4454 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4455 if zoom < 1.0
4456 then setzoom zoom
4458 | 0xffc6 -> (* f9 *)
4459 togglebirdseye ()
4461 | 57 when ctrl -> (* ctrl-9 *)
4462 togglebirdseye ()
4464 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4465 when not ctrl -> (* 0..9 *)
4466 let ondone s =
4467 let n =
4468 try int_of_string s with exc ->
4469 state.text <- Printf.sprintf "bad integer `%s': %s"
4470 s (Printexc.to_string exc);
4473 if n >= 0
4474 then (
4475 addnav ();
4476 cbput state.hists.pag (string_of_int n);
4477 gotopage1 (n + conf.pagebias - 1) 0;
4480 let pageentry text key =
4481 match Char.unsafe_chr key with
4482 | 'g' -> TEdone text
4483 | _ -> intentry text key
4485 let text = "x" in text.[0] <- Char.chr key;
4486 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4488 | 98 -> (* b *)
4489 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4490 reshape conf.winw conf.winh;
4492 | 108 -> (* l *)
4493 conf.hlinks <- not conf.hlinks;
4494 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4495 G.postRedisplay "toggle highlightlinks";
4497 | 70 -> (* F *)
4498 state.glinks <- true;
4499 let ondone s =
4500 let n =
4501 try int_of_string s with exc ->
4502 state.text <- Printf.sprintf "bad integer `%s': %s"
4503 s (Printexc.to_string exc);
4506 if n >= 0
4507 then (
4508 let rec loop n = function
4509 | [] -> ()
4510 | l :: rest ->
4511 match getopaque l.pageno with
4512 | None -> loop n rest
4513 | Some opaque ->
4514 let m = getlinkcount opaque in
4515 if n < m
4516 then (
4517 let under = getlink opaque n in
4518 addnav ();
4519 gotounder under;
4521 else loop (n-m) rest
4523 loop n state.layout;
4526 let onkey text key =
4527 match Char.unsafe_chr key with
4528 | 'g' -> TEdone text
4529 | _ -> intentry text key
4531 let mode = state.mode in
4532 state.mode <- Textentry (
4533 (":", "", Some (onhist state.hists.pag), onkey, ondone),
4534 fun _ ->
4535 state.glinks <- false;
4536 state.mode <- mode
4538 state.text <- "";
4539 G.postRedisplay "view:enttext"
4541 | 97 -> (* a *)
4542 begin match state.autoscroll with
4543 | Some step ->
4544 conf.autoscrollstep <- step;
4545 state.autoscroll <- None
4546 | None ->
4547 if conf.autoscrollstep = 0
4548 then state.autoscroll <- Some 1
4549 else state.autoscroll <- Some conf.autoscrollstep
4552 | 80 -> (* P *)
4553 conf.presentation <- not conf.presentation;
4554 if conf.presentation
4555 then (
4556 if not conf.scrollbarinpm
4557 then state.scrollw <- 0;
4559 else
4560 state.scrollw <- conf.scrollbw;
4562 showtext ' ' ("presentation mode " ^
4563 if conf.presentation then "on" else "off");
4564 state.anchor <- getanchor ();
4565 represent ()
4567 | 102 -> (* f *)
4568 begin match state.fullscreen with
4569 | None ->
4570 state.fullscreen <- Some (conf.winw, conf.winh);
4571 Wsi.fullscreen ()
4572 | Some (w, h) ->
4573 state.fullscreen <- None;
4574 doreshape w h
4577 | 103 -> (* g *)
4578 gotoy_and_clear_text 0
4580 | 71 -> (* G *)
4581 gotopage1 (state.pagecount - 1) 0
4583 | 112 | 78 -> (* p|N *)
4584 search state.searchpattern false
4586 | 110 | 0xffc0 -> (* n|F3 *)
4587 search state.searchpattern true
4589 | 116 -> (* t *)
4590 begin match state.layout with
4591 | [] -> ()
4592 | l :: _ ->
4593 gotoy_and_clear_text (getpagey l.pageno)
4596 | 32 -> (* ' ' *)
4597 begin match List.rev state.layout with
4598 | [] -> ()
4599 | l :: _ ->
4600 let pageno = min (l.pageno+1) (state.pagecount-1) in
4601 gotoy_and_clear_text (getpagey pageno)
4604 | 0xff9f | 0xffff -> (* delete *)
4605 begin match state.layout with
4606 | [] -> ()
4607 | l :: _ ->
4608 let pageno = max 0 (l.pageno-1) in
4609 gotoy_and_clear_text (getpagey pageno)
4612 | 61 -> (* = *)
4613 showtext ' ' (describe_location ());
4615 | 119 -> (* w *)
4616 begin match state.layout with
4617 | [] -> ()
4618 | l :: _ ->
4619 doreshape (l.pagew + state.scrollw) l.pageh;
4620 G.postRedisplay "w"
4623 | 39 -> (* ' *)
4624 enterbookmarkmode ()
4626 | 104 | 0xffbe -> (* h|F1 *)
4627 enterhelpmode ()
4629 | 105 -> (* i *)
4630 enterinfomode ()
4632 | 101 when conf.redirectstderr -> (* e *)
4633 entermsgsmode ()
4635 | 109 -> (* m *)
4636 let ondone s =
4637 match state.layout with
4638 | l :: _ ->
4639 state.bookmarks <-
4640 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4641 :: state.bookmarks
4642 | _ -> ()
4644 enttext ("bookmark: ", "", None, textentry, ondone)
4646 | 126 -> (* ~ *)
4647 quickbookmark ();
4648 showtext ' ' "Quick bookmark added";
4650 | 122 -> (* z *)
4651 begin match state.layout with
4652 | l :: _ ->
4653 let rect = getpdimrect l.pagedimno in
4654 let w, h =
4655 if conf.crophack
4656 then
4657 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4658 truncate (1.2 *. (rect.(3) -. rect.(0))))
4659 else
4660 (truncate (rect.(1) -. rect.(0)),
4661 truncate (rect.(3) -. rect.(0)))
4663 let w = truncate ((float w)*.conf.zoom)
4664 and h = truncate ((float h)*.conf.zoom) in
4665 if w != 0 && h != 0
4666 then (
4667 state.anchor <- getanchor ();
4668 doreshape (w + state.scrollw) (h + conf.interpagespace)
4670 G.postRedisplay "z";
4672 | [] -> ()
4675 | 50 when ctrl -> (* ctrl-2 *)
4676 let maxw = getmaxw () in
4677 if maxw > 0.0
4678 then setzoom (maxw /. float conf.winw)
4680 | 60 | 62 -> (* < > *)
4681 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4683 | 91 | 93 -> (* [ ] *)
4684 conf.colorscale <-
4685 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4687 G.postRedisplay "brightness";
4689 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4690 setzoom state.prevzoom
4692 | 107 | 0xff52 -> (* k up *)
4693 begin match state.autoscroll with
4694 | None ->
4695 begin match state.mode with
4696 | Birdseye beye -> upbirdseye 1 beye
4697 | _ ->
4698 if ctrl
4699 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4700 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4702 | Some n ->
4703 setautoscrollspeed n false
4706 | 106 | 0xff54 -> (* j down *)
4707 begin match state.autoscroll with
4708 | None ->
4709 begin match state.mode with
4710 | Birdseye beye -> downbirdseye 1 beye
4711 | _ ->
4712 if ctrl
4713 then gotoy_and_clear_text (clamp (conf.winh/2))
4714 else gotoy_and_clear_text (clamp conf.scrollstep)
4716 | Some n ->
4717 setautoscrollspeed n true
4720 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4721 if conf.zoom > 1.0
4722 then
4723 let dx =
4724 if ctrl
4725 then conf.winw / 2
4726 else 10
4728 let dx = if key = 0xff51 then dx else -dx in
4729 state.x <- state.x + dx;
4730 gotoy_and_clear_text state.y
4731 else (
4732 state.text <- "";
4733 G.postRedisplay "lef/right"
4736 | 0xff55 -> (* prior *)
4737 let y =
4738 if ctrl
4739 then
4740 match state.layout with
4741 | [] -> state.y
4742 | l :: _ -> state.y - l.pagey
4743 else
4744 clamp (-conf.winh)
4746 gotoghyll y
4748 | 0xff56 -> (* next *)
4749 let y =
4750 if ctrl
4751 then
4752 match List.rev state.layout with
4753 | [] -> state.y
4754 | l :: _ -> getpagey l.pageno
4755 else
4756 clamp conf.winh
4758 gotoghyll y
4760 | 0xff50 -> gotoghyll 0
4761 | 0xff57 -> gotoghyll (clamp state.maxy)
4762 | 0xff53 when Wsi.withalt mask ->
4763 gotoghyll (getnav ~-1)
4764 | 0xff51 when Wsi.withalt mask ->
4765 gotoghyll (getnav 1)
4767 | 114 -> (* r *)
4768 state.anchor <- getanchor ();
4769 opendoc state.path state.password
4771 | 76 -> (* L *)
4772 launchpath ()
4774 | 118 when conf.debug -> (* v *)
4775 state.rects <- [];
4776 List.iter (fun l ->
4777 match getopaque l.pageno with
4778 | None -> ()
4779 | Some opaque ->
4780 let x0, y0, x1, y1 = pagebbox opaque in
4781 let a,b = float x0, float y0 in
4782 let c,d = float x1, float y0 in
4783 let e,f = float x1, float y1 in
4784 let h,j = float x0, float y1 in
4785 let rect = (a,b,c,d,e,f,h,j) in
4786 debugrect rect;
4787 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4788 ) state.layout;
4789 G.postRedisplay "v";
4791 | _ ->
4792 vlog "huh? %s" (Wsi.keyname key)
4795 let linknavkeyboard key mask linknav =
4796 let getpage pageno =
4797 let rec loop = function
4798 | [] -> None
4799 | l :: _ when l.pageno = pageno -> Some l
4800 | _ :: rest -> loop rest
4801 in loop state.layout
4803 let doexact (pageno, n) =
4804 match getopaque pageno, getpage pageno with
4805 | Some opaque, Some l ->
4806 if key = 0xff0d
4807 then
4808 let under = getlink opaque n in
4809 G.postRedisplay "link gotounder";
4810 gotounder under;
4811 state.mode <- View;
4812 else
4813 let opt, dir =
4814 match key with
4815 | 0xff50 -> (* home *)
4816 Some (findlink opaque LDfirst), -1
4818 | 0xff57 -> (* end *)
4819 Some (findlink opaque LDlast), 1
4821 | 0xff51 -> (* left *)
4822 Some (findlink opaque (LDleft n)), -1
4824 | 0xff53 -> (* right *)
4825 Some (findlink opaque (LDright n)), 1
4827 | 0xff52 -> (* up *)
4828 Some (findlink opaque (LDup n)), -1
4830 | 0xff54 -> (* down *)
4831 Some (findlink opaque (LDdown n)), 1
4833 | _ -> None, 0
4835 let pwl l dir =
4836 begin match findpwl l.pageno dir with
4837 | Pwlnotfound -> ()
4838 | Pwl pageno ->
4839 let notfound dir =
4840 state.mode <- LinkNav (Ltgendir dir);
4841 let y, h = getpageyh pageno in
4842 let y =
4843 if dir < 0
4844 then y + h - conf.winh
4845 else y
4847 gotoy y
4849 begin match getopaque pageno, getpage pageno with
4850 | Some opaque, Some _ ->
4851 let link =
4852 let ld = if dir > 0 then LDfirst else LDlast in
4853 findlink opaque ld
4855 begin match link with
4856 | Lfound m ->
4857 showlinktype (getlink opaque m);
4858 state.mode <- LinkNav (Ltexact (pageno, m));
4859 G.postRedisplay "linknav jpage";
4860 | _ -> notfound dir
4861 end;
4862 | _ -> notfound dir
4863 end;
4864 end;
4866 begin match opt with
4867 | Some Lnotfound -> pwl l dir;
4868 | Some (Lfound m) ->
4869 if m = n
4870 then pwl l dir
4871 else (
4872 let _, y0, _, y1 = getlinkrect opaque m in
4873 if y0 < l.pagey
4874 then gotopage1 l.pageno y0
4875 else (
4876 let d = fstate.fontsize + 1 in
4877 if y1 - l.pagey > l.pagevh - d
4878 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
4879 else G.postRedisplay "linknav";
4881 showlinktype (getlink opaque m);
4882 state.mode <- LinkNav (Ltexact (l.pageno, m));
4885 | None -> viewkeyboard key mask
4886 end;
4887 | _ -> viewkeyboard key mask
4889 if key = 0xff63
4890 then (
4891 state.mode <- View;
4892 G.postRedisplay "leave linknav"
4894 else
4895 match linknav with
4896 | Ltgendir _ -> viewkeyboard key mask
4897 | Ltexact exact -> doexact exact
4900 let keyboard key mask =
4901 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4902 then wcmd "interrupt"
4903 else state.uioh <- state.uioh#key key mask
4906 let birdseyekeyboard key mask
4907 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4908 let incr =
4909 match conf.columns with
4910 | None -> 1
4911 | Some ((c, _, _), _) -> c
4913 match key with
4914 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4915 let y, h = getpageyh pageno in
4916 let top = (conf.winh - h) / 2 in
4917 gotoy (max 0 (y - top))
4918 | 0xff0d -> leavebirdseye beye false
4919 | 0xff1b -> leavebirdseye beye true (* escape *)
4920 | 0xff52 -> upbirdseye incr beye (* prior *)
4921 | 0xff54 -> downbirdseye incr beye (* next *)
4922 | 0xff51 -> upbirdseye 1 beye (* up *)
4923 | 0xff53 -> downbirdseye 1 beye (* down *)
4925 | 0xff55 ->
4926 begin match state.layout with
4927 | l :: _ ->
4928 if l.pagey != 0
4929 then (
4930 state.mode <- Birdseye (
4931 oconf, leftx, l.pageno, hooverpageno, anchor
4933 gotopage1 l.pageno 0;
4935 else (
4936 let layout = layout (state.y-conf.winh) conf.winh in
4937 match layout with
4938 | [] -> gotoy (clamp (-conf.winh))
4939 | l :: _ ->
4940 state.mode <- Birdseye (
4941 oconf, leftx, l.pageno, hooverpageno, anchor
4943 gotopage1 l.pageno 0
4946 | [] -> gotoy (clamp (-conf.winh))
4947 end;
4949 | 0xff56 ->
4950 begin match List.rev state.layout with
4951 | l :: _ ->
4952 let layout = layout (state.y + conf.winh) conf.winh in
4953 begin match layout with
4954 | [] ->
4955 let incr = l.pageh - l.pagevh in
4956 if incr = 0
4957 then (
4958 state.mode <-
4959 Birdseye (
4960 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4962 G.postRedisplay "birdseye pagedown";
4964 else gotoy (clamp (incr + conf.interpagespace*2));
4966 | l :: _ ->
4967 state.mode <-
4968 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4969 gotopage1 l.pageno 0;
4972 | [] -> gotoy (clamp conf.winh)
4973 end;
4975 | 0xff50 ->
4976 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4977 gotopage1 0 0
4979 | 0xff57 ->
4980 let pageno = state.pagecount - 1 in
4981 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4982 if not (pagevisible state.layout pageno)
4983 then
4984 let h =
4985 match List.rev state.pdims with
4986 | [] -> conf.winh
4987 | (_, _, h, _) :: _ -> h
4989 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4990 else G.postRedisplay "birdseye end";
4991 | _ -> viewkeyboard key mask
4994 let drawpage l linkindexbase =
4995 let color =
4996 match state.mode with
4997 | Textentry _ -> scalecolor 0.4
4998 | LinkNav _
4999 | View -> scalecolor 1.0
5000 | Birdseye (_, _, pageno, hooverpageno, _) ->
5001 if l.pageno = hooverpageno
5002 then scalecolor 0.9
5003 else (
5004 if l.pageno = pageno
5005 then scalecolor 1.0
5006 else scalecolor 0.8
5009 drawtiles l color;
5010 begin match getopaque l.pageno with
5011 | Some opaque ->
5012 if tileready l l.pagex l.pagey
5013 then
5014 let x = l.pagedispx - l.pagex
5015 and y = l.pagedispy - l.pagey in
5016 let hlmask = (if conf.hlinks then 1 else 0)
5017 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5019 postprocess opaque hlmask x y linkindexbase;
5020 else 0
5022 | _ -> 0
5023 end;
5026 let scrollindicator () =
5027 let sbw, ph, sh = state.uioh#scrollph in
5028 let sbh, pw, sw = state.uioh#scrollpw in
5030 GlDraw.color (0.64, 0.64, 0.64);
5031 GlDraw.rect
5032 (float (conf.winw - sbw), 0.)
5033 (float conf.winw, float conf.winh)
5035 GlDraw.rect
5036 (0., float (conf.winh - sbh))
5037 (float (conf.winw - state.scrollw - 1), float conf.winh)
5039 GlDraw.color (0.0, 0.0, 0.0);
5041 GlDraw.rect
5042 (float (conf.winw - sbw), ph)
5043 (float conf.winw, ph +. sh)
5045 GlDraw.rect
5046 (pw, float (conf.winh - sbh))
5047 (pw +. sw, float conf.winh)
5051 let showsel () =
5052 match state.mstate with
5053 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5056 | Msel ((x0, y0), (x1, y1)) ->
5057 let rec loop = function
5058 | l :: ls ->
5059 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5060 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5061 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5062 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5063 then
5064 match getopaque l.pageno with
5065 | Some opaque ->
5066 let x0, y0 = pagetranslatepoint l x0 y0 in
5067 let x1, y1 = pagetranslatepoint l x1 y1 in
5068 seltext opaque (x0, y0, x1, y1);
5069 | _ -> ()
5070 else loop ls
5071 | [] -> ()
5073 loop state.layout
5076 let showrects rects =
5077 Gl.enable `blend;
5078 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5079 GlDraw.polygon_mode `both `fill;
5080 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5081 List.iter
5082 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5083 List.iter (fun l ->
5084 if l.pageno = pageno
5085 then (
5086 let dx = float (l.pagedispx - l.pagex) in
5087 let dy = float (l.pagedispy - l.pagey) in
5088 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5089 GlDraw.begins `quads;
5091 GlDraw.vertex2 (x0+.dx, y0+.dy);
5092 GlDraw.vertex2 (x1+.dx, y1+.dy);
5093 GlDraw.vertex2 (x2+.dx, y2+.dy);
5094 GlDraw.vertex2 (x3+.dx, y3+.dy);
5096 GlDraw.ends ();
5098 ) state.layout
5099 ) rects
5101 Gl.disable `blend;
5104 let display () =
5105 GlClear.color (scalecolor2 conf.bgcolor);
5106 GlClear.clear [`color];
5107 let rec loop linkindexbase = function
5108 | l :: rest ->
5109 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5110 loop linkindexbase rest
5111 | [] -> ()
5113 loop 0 state.layout;
5114 let rects =
5115 match state.mode with
5116 | LinkNav (Ltexact (pageno, linkno)) ->
5117 begin match getopaque pageno with
5118 | Some opaque ->
5119 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5120 (pageno, 5, (
5121 float x0, float y0,
5122 float x1, float y0,
5123 float x1, float y1,
5124 float x0, float y1)
5125 ) :: state.rects
5126 | None -> state.rects
5128 | _ -> state.rects
5130 showrects rects;
5131 showsel ();
5132 state.uioh#display;
5133 begin match state.mstate with
5134 | Mzoomrect ((x0, y0), (x1, y1)) ->
5135 Gl.enable `blend;
5136 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5137 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5138 GlDraw.rect (float x0, float y0)
5139 (float x1, float y1);
5140 Gl.disable `blend;
5141 | _ -> ()
5142 end;
5143 enttext ();
5144 scrollindicator ();
5145 Wsi.swapb ();
5148 let zoomrect x y x1 y1 =
5149 let x0 = min x x1
5150 and x1 = max x x1
5151 and y0 = min y y1 in
5152 gotoy (state.y + y0);
5153 state.anchor <- getanchor ();
5154 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5155 let margin =
5156 if state.w < conf.winw - state.scrollw
5157 then (conf.winw - state.scrollw - state.w) / 2
5158 else 0
5160 state.x <- (state.x + margin) - x0;
5161 setzoom zoom;
5162 Wsi.setcursor Wsi.CURSOR_INHERIT;
5163 state.mstate <- Mnone;
5166 let scrollx x =
5167 let winw = conf.winw - state.scrollw - 1 in
5168 let s = float x /. float winw in
5169 let destx = truncate (float (state.w + winw) *. s) in
5170 state.x <- winw - destx;
5171 gotoy_and_clear_text state.y;
5172 state.mstate <- Mscrollx;
5175 let scrolly y =
5176 let s = float y /. float conf.winh in
5177 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5178 gotoy_and_clear_text desty;
5179 state.mstate <- Mscrolly;
5182 let viewmouse button down x y mask =
5183 match button with
5184 | n when (n == 4 || n == 5) && not down ->
5185 if Wsi.withctrl mask
5186 then (
5187 match state.mstate with
5188 | Mzoom (oldn, i) ->
5189 if oldn = n
5190 then (
5191 if i = 2
5192 then
5193 let incr =
5194 match n with
5195 | 5 ->
5196 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5197 | _ ->
5198 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5200 let zoom = conf.zoom -. incr in
5201 setzoom zoom;
5202 state.mstate <- Mzoom (n, 0);
5203 else
5204 state.mstate <- Mzoom (n, i+1);
5206 else state.mstate <- Mzoom (n, 0)
5208 | _ -> state.mstate <- Mzoom (n, 0)
5210 else (
5211 match state.autoscroll with
5212 | Some step -> setautoscrollspeed step (n=4)
5213 | None ->
5214 let incr =
5215 if n = 4
5216 then -conf.scrollstep
5217 else conf.scrollstep
5219 let incr = incr * 2 in
5220 let y = clamp incr in
5221 gotoy_and_clear_text y
5224 | 1 when Wsi.withctrl mask ->
5225 if down
5226 then (
5227 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5228 state.mstate <- Mpan (x, y)
5230 else
5231 state.mstate <- Mnone
5233 | 3 ->
5234 if down
5235 then (
5236 Wsi.setcursor Wsi.CURSOR_CYCLE;
5237 let p = (x, y) in
5238 state.mstate <- Mzoomrect (p, p)
5240 else (
5241 match state.mstate with
5242 | Mzoomrect ((x0, y0), _) ->
5243 if abs (x-x0) > 10 && abs (y - y0) > 10
5244 then zoomrect x0 y0 x y
5245 else (
5246 state.mstate <- Mnone;
5247 Wsi.setcursor Wsi.CURSOR_INHERIT;
5248 G.postRedisplay "kill accidental zoom rect";
5250 | _ ->
5251 Wsi.setcursor Wsi.CURSOR_INHERIT;
5252 state.mstate <- Mnone
5255 | 1 when x > conf.winw - state.scrollw ->
5256 if down
5257 then
5258 let _, position, sh = state.uioh#scrollph in
5259 if y > truncate position && y < truncate (position +. sh)
5260 then state.mstate <- Mscrolly
5261 else scrolly y
5262 else
5263 state.mstate <- Mnone
5265 | 1 when y > conf.winh - state.hscrollh ->
5266 if down
5267 then
5268 let _, position, sw = state.uioh#scrollpw in
5269 if x > truncate position && x < truncate (position +. sw)
5270 then state.mstate <- Mscrollx
5271 else scrollx x
5272 else
5273 state.mstate <- Mnone
5275 | 1 ->
5276 let dest = if down then getunder x y else Unone in
5277 begin match dest with
5278 | Ulinkgoto (pageno, top) ->
5279 if pageno >= 0
5280 then (
5281 addnav ();
5282 gotopage1 pageno top;
5285 | Ulinkuri s ->
5286 gotouri s
5288 | Uremote (filename, pageno) ->
5289 let path =
5290 if Sys.file_exists filename
5291 then filename
5292 else
5293 let dir = Filename.dirname state.path in
5294 let path = Filename.concat dir filename in
5295 if Sys.file_exists path
5296 then path
5297 else ""
5299 if String.length path > 0
5300 then (
5301 let anchor = getanchor () in
5302 let ranchor = state.path, state.password, anchor in
5303 state.anchor <- (pageno, 0.0);
5304 state.ranchors <- ranchor :: state.ranchors;
5305 opendoc path "";
5307 else showtext '!' ("Could not find " ^ filename)
5309 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
5311 | Unone when down ->
5312 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5313 state.mstate <- Mpan (x, y);
5315 | Unone | Utext _ ->
5316 if down
5317 then (
5318 if conf.angle mod 360 = 0
5319 then (
5320 state.mstate <- Msel ((x, y), (x, y));
5321 G.postRedisplay "mouse select";
5324 else (
5325 match state.mstate with
5326 | Mnone -> ()
5328 | Mzoom _ | Mscrollx | Mscrolly ->
5329 state.mstate <- Mnone
5331 | Mzoomrect ((x0, y0), _) ->
5332 zoomrect x0 y0 x y
5334 | Mpan _ ->
5335 Wsi.setcursor Wsi.CURSOR_INHERIT;
5336 state.mstate <- Mnone
5338 | Msel ((_, y0), (_, y1)) ->
5339 let rec loop = function
5340 | [] -> ()
5341 | l :: rest ->
5342 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5343 || ((y1 >= l.pagedispy
5344 && y1 <= (l.pagedispy + l.pagevh)))
5345 then
5346 match getopaque l.pageno with
5347 | Some opaque ->
5348 copysel conf.selcmd opaque;
5349 G.postRedisplay "copysel"
5350 | _ -> ()
5351 else loop rest
5353 loop state.layout;
5354 Wsi.setcursor Wsi.CURSOR_INHERIT;
5355 state.mstate <- Mnone;
5359 | _ -> ()
5362 let birdseyemouse button down x y mask
5363 (conf, leftx, _, hooverpageno, anchor) =
5364 match button with
5365 | 1 when down ->
5366 let rec loop = function
5367 | [] -> ()
5368 | l :: rest ->
5369 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5370 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5371 then (
5372 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5374 else loop rest
5376 loop state.layout
5377 | 3 -> ()
5378 | _ -> viewmouse button down x y mask
5381 let mouse button down x y mask =
5382 state.uioh <- state.uioh#button button down x y mask;
5385 let motion ~x ~y =
5386 state.uioh <- state.uioh#motion x y
5389 let pmotion ~x ~y =
5390 state.uioh <- state.uioh#pmotion x y;
5393 let uioh = object
5394 method display = ()
5396 method key key mask =
5397 begin match state.mode with
5398 | Textentry textentry -> textentrykeyboard key mask textentry
5399 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5400 | View -> viewkeyboard key mask
5401 | LinkNav linknav -> linknavkeyboard key mask linknav
5402 end;
5403 state.uioh
5405 method button button bstate x y mask =
5406 begin match state.mode with
5407 | LinkNav _
5408 | View -> viewmouse button bstate x y mask
5409 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5410 | Textentry _ -> ()
5411 end;
5412 state.uioh
5414 method motion x y =
5415 begin match state.mode with
5416 | Textentry _ -> ()
5417 | View | Birdseye _ | LinkNav _ ->
5418 match state.mstate with
5419 | Mzoom _ | Mnone -> ()
5421 | Mpan (x0, y0) ->
5422 let dx = x - x0
5423 and dy = y0 - y in
5424 state.mstate <- Mpan (x, y);
5425 if conf.zoom > 1.0 then state.x <- state.x + dx;
5426 let y = clamp dy in
5427 gotoy_and_clear_text y
5429 | Msel (a, _) ->
5430 state.mstate <- Msel (a, (x, y));
5431 G.postRedisplay "motion select";
5433 | Mscrolly ->
5434 let y = min conf.winh (max 0 y) in
5435 scrolly y
5437 | Mscrollx ->
5438 let x = min conf.winw (max 0 x) in
5439 scrollx x
5441 | Mzoomrect (p0, _) ->
5442 state.mstate <- Mzoomrect (p0, (x, y));
5443 G.postRedisplay "motion zoomrect";
5444 end;
5445 state.uioh
5447 method pmotion x y =
5448 begin match state.mode with
5449 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5450 let rec loop = function
5451 | [] ->
5452 if hooverpageno != -1
5453 then (
5454 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5455 G.postRedisplay "pmotion birdseye no hoover";
5457 | l :: rest ->
5458 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5459 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5460 then (
5461 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5462 G.postRedisplay "pmotion birdseye hoover";
5464 else loop rest
5466 loop state.layout
5468 | Textentry _ -> ()
5470 | LinkNav _
5471 | View ->
5472 match state.mstate with
5473 | Mnone -> updateunder x y
5474 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5476 end;
5477 state.uioh
5479 method infochanged _ = ()
5481 method scrollph =
5482 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5483 let p, h = scrollph state.y maxy in
5484 state.scrollw, p, h
5486 method scrollpw =
5487 let winw = conf.winw - state.scrollw - 1 in
5488 let fwinw = float winw in
5489 let sw =
5490 let sw = fwinw /. float state.w in
5491 let sw = fwinw *. sw in
5492 max sw (float conf.scrollh)
5494 let position, sw =
5495 let f = state.w+winw in
5496 let r = float (winw-state.x) /. float f in
5497 let p = fwinw *. r in
5498 p-.sw/.2., sw
5500 let sw =
5501 if position +. sw > fwinw
5502 then fwinw -. position
5503 else sw
5505 state.hscrollh, position, sw
5507 method modehash =
5508 let modename =
5509 match state.mode with
5510 | LinkNav _ -> "links"
5511 | Textentry _ -> "textentry"
5512 | Birdseye _ -> "birdseye"
5513 | View -> "global"
5515 findkeyhash conf modename
5516 end;;
5518 module Config =
5519 struct
5520 open Parser
5522 let fontpath = ref "";;
5524 module KeyMap =
5525 Map.Make (struct type t = (int * int) let compare = compare end);;
5527 let unent s =
5528 let l = String.length s in
5529 let b = Buffer.create l in
5530 unent b s 0 l;
5531 Buffer.contents b;
5534 let home =
5535 try Sys.getenv "HOME"
5536 with exn ->
5537 prerr_endline
5538 ("Can not determine home directory location: " ^
5539 Printexc.to_string exn);
5543 let modifier_of_string = function
5544 | "alt" -> Wsi.altmask
5545 | "shift" -> Wsi.shiftmask
5546 | "ctrl" | "control" -> Wsi.ctrlmask
5547 | "meta" -> Wsi.metamask
5548 | _ -> 0
5551 let key_of_string =
5552 let r = Str.regexp "-" in
5553 fun s ->
5554 let elems = Str.full_split r s in
5555 let f n k m =
5556 let g s =
5557 let m1 = modifier_of_string s in
5558 if m1 = 0
5559 then (Wsi.namekey s, m)
5560 else (k, m lor m1)
5561 in function
5562 | Str.Delim s when n land 1 = 0 -> g s
5563 | Str.Text s -> g s
5564 | Str.Delim _ -> (k, m)
5566 let rec loop n k m = function
5567 | [] -> (k, m)
5568 | x :: xs ->
5569 let k, m = f n k m x in
5570 loop (n+1) k m xs
5572 loop 0 0 0 elems
5575 let keys_of_string =
5576 let r = Str.regexp "[ \t]" in
5577 fun s ->
5578 let elems = Str.split r s in
5579 List.map key_of_string elems
5582 let copykeyhashes c =
5583 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5586 let config_of c attrs =
5587 let apply c k v =
5589 match k with
5590 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5591 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5592 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5593 | "preload" -> { c with preload = bool_of_string v }
5594 | "page-bias" -> { c with pagebias = int_of_string v }
5595 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5596 | "auto-scroll-step" ->
5597 { c with autoscrollstep = max 0 (int_of_string v) }
5598 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5599 | "crop-hack" -> { c with crophack = bool_of_string v }
5600 | "throttle" ->
5601 let mw =
5602 match String.lowercase v with
5603 | "true" -> Some infinity
5604 | "false" -> None
5605 | f -> Some (float_of_string f)
5607 { c with maxwait = mw}
5608 | "highlight-links" -> { c with hlinks = bool_of_string v }
5609 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5610 | "vertical-margin" ->
5611 { c with interpagespace = max 0 (int_of_string v) }
5612 | "zoom" ->
5613 let zoom = float_of_string v /. 100. in
5614 let zoom = max zoom 0.0 in
5615 { c with zoom = zoom }
5616 | "presentation" -> { c with presentation = bool_of_string v }
5617 | "rotation-angle" -> { c with angle = int_of_string v }
5618 | "width" -> { c with winw = max 20 (int_of_string v) }
5619 | "height" -> { c with winh = max 20 (int_of_string v) }
5620 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5621 | "proportional-display" -> { c with proportional = bool_of_string v }
5622 | "pixmap-cache-size" ->
5623 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5624 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5625 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5626 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5627 | "persistent-location" -> { c with jumpback = bool_of_string v }
5628 | "background-color" -> { c with bgcolor = color_of_string v }
5629 | "scrollbar-in-presentation" ->
5630 { c with scrollbarinpm = bool_of_string v }
5631 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5632 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5633 | "mupdf-store-size" ->
5634 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5635 | "checkers" -> { c with checkers = bool_of_string v }
5636 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5637 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5638 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5639 | "uri-launcher" -> { c with urilauncher = unent v }
5640 | "path-launcher" -> { c with pathlauncher = unent v }
5641 | "color-space" -> { c with colorspace = colorspace_of_string v }
5642 | "invert-colors" -> { c with invert = bool_of_string v }
5643 | "brightness" -> { c with colorscale = float_of_string v }
5644 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5645 | "ghyllscroll" ->
5646 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5647 | "columns" ->
5648 let nab = columns_of_string v in
5649 { c with columns = Some (nab, [||]) }
5650 | "birds-eye-columns" ->
5651 { c with beyecolumns = Some (max (int_of_string v) 2) }
5652 | "selection-command" -> { c with selcmd = unent v }
5653 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5654 | _ -> c
5655 with exn ->
5656 prerr_endline ("Error processing attribute (`" ^
5657 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5660 let rec fold c = function
5661 | [] -> c
5662 | (k, v) :: rest ->
5663 let c = apply c k v in
5664 fold c rest
5666 fold { c with keyhashes = copykeyhashes c } attrs;
5669 let fromstring f pos n v d =
5670 try f v
5671 with exn ->
5672 dolog "Error processing attribute (%S=%S) at %d\n%s"
5673 n v pos (Printexc.to_string exn)
5678 let bookmark_of attrs =
5679 let rec fold title page rely = function
5680 | ("title", v) :: rest -> fold v page rely rest
5681 | ("page", v) :: rest -> fold title v rely rest
5682 | ("rely", v) :: rest -> fold title page v rest
5683 | _ :: rest -> fold title page rely rest
5684 | [] -> title, page, rely
5686 fold "invalid" "0" "0" attrs
5689 let doc_of attrs =
5690 let rec fold path page rely pan = function
5691 | ("path", v) :: rest -> fold v page rely pan rest
5692 | ("page", v) :: rest -> fold path v rely pan rest
5693 | ("rely", v) :: rest -> fold path page v pan rest
5694 | ("pan", v) :: rest -> fold path page rely v rest
5695 | _ :: rest -> fold path page rely pan rest
5696 | [] -> path, page, rely, pan
5698 fold "" "0" "0" "0" attrs
5701 let map_of attrs =
5702 let rec fold rs ls = function
5703 | ("out", v) :: rest -> fold v ls rest
5704 | ("in", v) :: rest -> fold rs v rest
5705 | _ :: rest -> fold ls rs rest
5706 | [] -> ls, rs
5708 fold "" "" attrs
5711 let setconf dst src =
5712 dst.scrollbw <- src.scrollbw;
5713 dst.scrollh <- src.scrollh;
5714 dst.icase <- src.icase;
5715 dst.preload <- src.preload;
5716 dst.pagebias <- src.pagebias;
5717 dst.verbose <- src.verbose;
5718 dst.scrollstep <- src.scrollstep;
5719 dst.maxhfit <- src.maxhfit;
5720 dst.crophack <- src.crophack;
5721 dst.autoscrollstep <- src.autoscrollstep;
5722 dst.maxwait <- src.maxwait;
5723 dst.hlinks <- src.hlinks;
5724 dst.underinfo <- src.underinfo;
5725 dst.interpagespace <- src.interpagespace;
5726 dst.zoom <- src.zoom;
5727 dst.presentation <- src.presentation;
5728 dst.angle <- src.angle;
5729 dst.winw <- src.winw;
5730 dst.winh <- src.winh;
5731 dst.savebmarks <- src.savebmarks;
5732 dst.memlimit <- src.memlimit;
5733 dst.proportional <- src.proportional;
5734 dst.texcount <- src.texcount;
5735 dst.sliceheight <- src.sliceheight;
5736 dst.thumbw <- src.thumbw;
5737 dst.jumpback <- src.jumpback;
5738 dst.bgcolor <- src.bgcolor;
5739 dst.scrollbarinpm <- src.scrollbarinpm;
5740 dst.tilew <- src.tilew;
5741 dst.tileh <- src.tileh;
5742 dst.mustoresize <- src.mustoresize;
5743 dst.checkers <- src.checkers;
5744 dst.aalevel <- src.aalevel;
5745 dst.trimmargins <- src.trimmargins;
5746 dst.trimfuzz <- src.trimfuzz;
5747 dst.urilauncher <- src.urilauncher;
5748 dst.colorspace <- src.colorspace;
5749 dst.invert <- src.invert;
5750 dst.colorscale <- src.colorscale;
5751 dst.redirectstderr <- src.redirectstderr;
5752 dst.ghyllscroll <- src.ghyllscroll;
5753 dst.columns <- src.columns;
5754 dst.beyecolumns <- src.beyecolumns;
5755 dst.selcmd <- src.selcmd;
5756 dst.updatecurs <- src.updatecurs;
5757 dst.pathlauncher <- src.pathlauncher;
5758 dst.keyhashes <- copykeyhashes src;
5761 let get s =
5762 let h = Hashtbl.create 10 in
5763 let dc = { defconf with angle = defconf.angle } in
5764 let rec toplevel v t spos _ =
5765 match t with
5766 | Vdata | Vcdata | Vend -> v
5767 | Vopen ("llppconfig", _, closed) ->
5768 if closed
5769 then v
5770 else { v with f = llppconfig }
5771 | Vopen _ ->
5772 error "unexpected subelement at top level" s spos
5773 | Vclose _ -> error "unexpected close at top level" s spos
5775 and llppconfig v t spos _ =
5776 match t with
5777 | Vdata | Vcdata -> v
5778 | Vend -> error "unexpected end of input in llppconfig" s spos
5779 | Vopen ("defaults", attrs, closed) ->
5780 let c = config_of dc attrs in
5781 setconf dc c;
5782 if closed
5783 then v
5784 else { v with f = defaults }
5786 | Vopen ("ui-font", attrs, closed) ->
5787 let rec getsize size = function
5788 | [] -> size
5789 | ("size", v) :: rest ->
5790 let size =
5791 fromstring int_of_string spos "size" v fstate.fontsize in
5792 getsize size rest
5793 | l -> getsize size l
5795 fstate.fontsize <- getsize fstate.fontsize attrs;
5796 if closed
5797 then v
5798 else { v with f = uifont (Buffer.create 10) }
5800 | Vopen ("doc", attrs, closed) ->
5801 let pathent, spage, srely, span = doc_of attrs in
5802 let path = unent pathent
5803 and pageno = fromstring int_of_string spos "page" spage 0
5804 and rely = fromstring float_of_string spos "rely" srely 0.0
5805 and pan = fromstring int_of_string spos "pan" span 0 in
5806 let c = config_of dc attrs in
5807 let anchor = (pageno, rely) in
5808 if closed
5809 then (Hashtbl.add h path (c, [], pan, anchor); v)
5810 else { v with f = doc path pan anchor c [] }
5812 | Vopen _ ->
5813 error "unexpected subelement in llppconfig" s spos
5815 | Vclose "llppconfig" -> { v with f = toplevel }
5816 | Vclose _ -> error "unexpected close in llppconfig" s spos
5818 and defaults v t spos _ =
5819 match t with
5820 | Vdata | Vcdata -> v
5821 | Vend -> error "unexpected end of input in defaults" s spos
5822 | Vopen ("keymap", attrs, closed) ->
5823 let modename =
5824 try List.assoc "mode" attrs
5825 with Not_found -> "global" in
5826 if closed
5827 then v
5828 else
5829 let ret keymap =
5830 let h = findkeyhash dc modename in
5831 KeyMap.iter (Hashtbl.replace h) keymap;
5832 defaults
5834 { v with f = pkeymap ret KeyMap.empty }
5836 | Vopen (_, _, _) ->
5837 error "unexpected subelement in defaults" s spos
5839 | Vclose "defaults" ->
5840 { v with f = llppconfig }
5842 | Vclose _ -> error "unexpected close in defaults" s spos
5844 and uifont b v t spos epos =
5845 match t with
5846 | Vdata | Vcdata ->
5847 Buffer.add_substring b s spos (epos - spos);
5849 | Vopen (_, _, _) ->
5850 error "unexpected subelement in ui-font" s spos
5851 | Vclose "ui-font" ->
5852 if String.length !fontpath = 0
5853 then fontpath := Buffer.contents b;
5854 { v with f = llppconfig }
5855 | Vclose _ -> error "unexpected close in ui-font" s spos
5856 | Vend -> error "unexpected end of input in ui-font" s spos
5858 and doc path pan anchor c bookmarks v t spos _ =
5859 match t with
5860 | Vdata | Vcdata -> v
5861 | Vend -> error "unexpected end of input in doc" s spos
5862 | Vopen ("bookmarks", _, closed) ->
5863 if closed
5864 then v
5865 else { v with f = pbookmarks path pan anchor c bookmarks }
5867 | Vopen ("keymap", attrs, closed) ->
5868 let modename =
5869 try List.assoc "mode" attrs
5870 with Not_found -> "global"
5872 if closed
5873 then v
5874 else
5875 let ret keymap =
5876 let h = findkeyhash c modename in
5877 KeyMap.iter (Hashtbl.replace h) keymap;
5878 doc path pan anchor c bookmarks
5880 { v with f = pkeymap ret KeyMap.empty }
5882 | Vopen (_, _, _) ->
5883 error "unexpected subelement in doc" s spos
5885 | Vclose "doc" ->
5886 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5887 { v with f = llppconfig }
5889 | Vclose _ -> error "unexpected close in doc" s spos
5891 and pkeymap ret keymap v t spos _ =
5892 match t with
5893 | Vdata | Vcdata -> v
5894 | Vend -> error "unexpected end of input in keymap" s spos
5895 | Vopen ("map", attrs, closed) ->
5896 let r, l = map_of attrs in
5897 let kss = fromstring keys_of_string spos "in" r [] in
5898 let lss = fromstring keys_of_string spos "out" l [] in
5899 let keymap =
5900 match kss with
5901 | [] -> keymap
5902 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5903 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5905 if closed
5906 then { v with f = pkeymap ret keymap }
5907 else
5908 let f () = v in
5909 { v with f = skip "map" f }
5911 | Vopen _ ->
5912 error "unexpected subelement in keymap" s spos
5914 | Vclose "keymap" ->
5915 { v with f = ret keymap }
5917 | Vclose _ -> error "unexpected close in keymap" s spos
5919 and pbookmarks path pan anchor c bookmarks v t spos _ =
5920 match t with
5921 | Vdata | Vcdata -> v
5922 | Vend -> error "unexpected end of input in bookmarks" s spos
5923 | Vopen ("item", attrs, closed) ->
5924 let titleent, spage, srely = bookmark_of attrs in
5925 let page = fromstring int_of_string spos "page" spage 0
5926 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5927 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5928 if closed
5929 then { v with f = pbookmarks path pan anchor c bookmarks }
5930 else
5931 let f () = v in
5932 { v with f = skip "item" f }
5934 | Vopen _ ->
5935 error "unexpected subelement in bookmarks" s spos
5937 | Vclose "bookmarks" ->
5938 { v with f = doc path pan anchor c bookmarks }
5940 | Vclose _ -> error "unexpected close in bookmarks" s spos
5942 and skip tag f v t spos _ =
5943 match t with
5944 | Vdata | Vcdata -> v
5945 | Vend ->
5946 error ("unexpected end of input in skipped " ^ tag) s spos
5947 | Vopen (tag', _, closed) ->
5948 if closed
5949 then v
5950 else
5951 let f' () = { v with f = skip tag f } in
5952 { v with f = skip tag' f' }
5953 | Vclose ctag ->
5954 if tag = ctag
5955 then f ()
5956 else error ("unexpected close in skipped " ^ tag) s spos
5959 parse { f = toplevel; accu = () } s;
5960 h, dc;
5963 let do_load f ic =
5965 let len = in_channel_length ic in
5966 let s = String.create len in
5967 really_input ic s 0 len;
5968 f s;
5969 with
5970 | Parse_error (msg, s, pos) ->
5971 let subs = subs s pos in
5972 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5973 failwith ("parse error: " ^ s)
5975 | exn ->
5976 failwith ("config load error: " ^ Printexc.to_string exn)
5979 let defconfpath =
5980 let dir =
5982 let dir = Filename.concat home ".config" in
5983 if Sys.is_directory dir then dir else home
5984 with _ -> home
5986 Filename.concat dir "llpp.conf"
5989 let confpath = ref defconfpath;;
5991 let load1 f =
5992 if Sys.file_exists !confpath
5993 then
5994 match
5995 (try Some (open_in_bin !confpath)
5996 with exn ->
5997 prerr_endline
5998 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5999 Printexc.to_string exn);
6000 None
6002 with
6003 | Some ic ->
6004 begin try
6005 f (do_load get ic)
6006 with exn ->
6007 prerr_endline
6008 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6009 Printexc.to_string exn);
6010 end;
6011 close_in ic;
6013 | None -> ()
6014 else
6015 f (Hashtbl.create 0, defconf)
6018 let load () =
6019 let f (h, dc) =
6020 let pc, pb, px, pa =
6022 Hashtbl.find h (Filename.basename state.path)
6023 with Not_found -> dc, [], 0, (0, 0.0)
6025 setconf defconf dc;
6026 setconf conf pc;
6027 state.bookmarks <- pb;
6028 state.x <- px;
6029 state.scrollw <- conf.scrollbw;
6030 if conf.jumpback
6031 then state.anchor <- pa;
6032 cbput state.hists.nav pa;
6034 load1 f
6037 let add_attrs bb always dc c =
6038 let ob s a b =
6039 if always || a != b
6040 then Printf.bprintf bb "\n %s='%b'" s a
6041 and oi s a b =
6042 if always || a != b
6043 then Printf.bprintf bb "\n %s='%d'" s a
6044 and oI s a b =
6045 if always || a != b
6046 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6047 and oz s a b =
6048 if always || a <> b
6049 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6050 and oF s a b =
6051 if always || a <> b
6052 then Printf.bprintf bb "\n %s='%f'" s a
6053 and oc s a b =
6054 if always || a <> b
6055 then
6056 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6057 and oC s a b =
6058 if always || a <> b
6059 then
6060 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6061 and oR s a b =
6062 if always || a <> b
6063 then
6064 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6065 and os s a b =
6066 if always || a <> b
6067 then
6068 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6069 and og s a b =
6070 if always || a <> b
6071 then
6072 match a with
6073 | None -> ()
6074 | Some (_N, _A, _B) ->
6075 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6076 and oW s a b =
6077 if always || a <> b
6078 then
6079 let v =
6080 match a with
6081 | None -> "false"
6082 | Some f ->
6083 if f = infinity
6084 then "true"
6085 else string_of_float f
6087 Printf.bprintf bb "\n %s='%s'" s v
6088 and oco s a b =
6089 if always || a <> b
6090 then
6091 match a with
6092 | Some ((n, a, b), _) when n > 1 ->
6093 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6094 | _ -> ()
6095 and obeco s a b =
6096 if always || a <> b
6097 then
6098 match a with
6099 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6100 | _ -> ()
6102 let w, h =
6103 if always
6104 then dc.winw, dc.winh
6105 else
6106 match state.fullscreen with
6107 | Some wh -> wh
6108 | None -> c.winw, c.winh
6110 let zoom, presentation, interpagespace, maxwait =
6111 if always
6112 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6113 else
6114 match state.mode with
6115 | Birdseye (bc, _, _, _, _) ->
6116 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6117 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6119 oi "width" w dc.winw;
6120 oi "height" h dc.winh;
6121 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6122 oi "scroll-handle-height" c.scrollh dc.scrollh;
6123 ob "case-insensitive-search" c.icase dc.icase;
6124 ob "preload" c.preload dc.preload;
6125 oi "page-bias" c.pagebias dc.pagebias;
6126 oi "scroll-step" c.scrollstep dc.scrollstep;
6127 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6128 ob "max-height-fit" c.maxhfit dc.maxhfit;
6129 ob "crop-hack" c.crophack dc.crophack;
6130 oW "throttle" maxwait dc.maxwait;
6131 ob "highlight-links" c.hlinks dc.hlinks;
6132 ob "under-cursor-info" c.underinfo dc.underinfo;
6133 oi "vertical-margin" interpagespace dc.interpagespace;
6134 oz "zoom" zoom dc.zoom;
6135 ob "presentation" presentation dc.presentation;
6136 oi "rotation-angle" c.angle dc.angle;
6137 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6138 ob "proportional-display" c.proportional dc.proportional;
6139 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6140 oi "tex-count" c.texcount dc.texcount;
6141 oi "slice-height" c.sliceheight dc.sliceheight;
6142 oi "thumbnail-width" c.thumbw dc.thumbw;
6143 ob "persistent-location" c.jumpback dc.jumpback;
6144 oc "background-color" c.bgcolor dc.bgcolor;
6145 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6146 oi "tile-width" c.tilew dc.tilew;
6147 oi "tile-height" c.tileh dc.tileh;
6148 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6149 ob "checkers" c.checkers dc.checkers;
6150 oi "aalevel" c.aalevel dc.aalevel;
6151 ob "trim-margins" c.trimmargins dc.trimmargins;
6152 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6153 os "uri-launcher" c.urilauncher dc.urilauncher;
6154 os "path-launcher" c.pathlauncher dc.pathlauncher;
6155 oC "color-space" c.colorspace dc.colorspace;
6156 ob "invert-colors" c.invert dc.invert;
6157 oF "brightness" c.colorscale dc.colorscale;
6158 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6159 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6160 oco "columns" c.columns dc.columns;
6161 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6162 os "selection-command" c.selcmd dc.selcmd;
6163 ob "update-cursor" c.updatecurs dc.updatecurs;
6166 let keymapsbuf always dc c =
6167 let bb = Buffer.create 16 in
6168 let rec loop = function
6169 | [] -> ()
6170 | (modename, h) :: rest ->
6171 let dh = findkeyhash dc modename in
6172 if always || h <> dh
6173 then (
6174 if Hashtbl.length h > 0
6175 then (
6176 if Buffer.length bb > 0
6177 then Buffer.add_char bb '\n';
6178 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6179 Hashtbl.iter (fun i o ->
6180 let isdifferent = always ||
6182 let dO = Hashtbl.find dh i in
6183 dO <> o
6184 with Not_found -> true
6186 if isdifferent
6187 then
6188 let addkm (k, m) =
6189 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6190 if Wsi.withalt m then Buffer.add_string bb "alt-";
6191 if Wsi.withshift m then Buffer.add_string bb "shift-";
6192 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6193 Buffer.add_string bb (Wsi.keyname k);
6195 let addkms l =
6196 let rec loop = function
6197 | [] -> ()
6198 | km :: [] -> addkm km
6199 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6201 loop l
6203 Buffer.add_string bb "<map in='";
6204 addkm i;
6205 match o with
6206 | KMinsrt km ->
6207 Buffer.add_string bb "' out='";
6208 addkm km;
6209 Buffer.add_string bb "'/>\n"
6211 | KMinsrl kms ->
6212 Buffer.add_string bb "' out='";
6213 addkms kms;
6214 Buffer.add_string bb "'/>\n"
6216 | KMmulti (ins, kms) ->
6217 Buffer.add_char bb ' ';
6218 addkms ins;
6219 Buffer.add_string bb "' out='";
6220 addkms kms;
6221 Buffer.add_string bb "'/>\n"
6222 ) h;
6223 Buffer.add_string bb "</keymap>";
6226 loop rest
6228 loop c.keyhashes;
6232 let save () =
6233 let uifontsize = fstate.fontsize in
6234 let bb = Buffer.create 32768 in
6235 let f (h, dc) =
6236 let dc = if conf.bedefault then conf else dc in
6237 Buffer.add_string bb "<llppconfig>\n";
6239 if String.length !fontpath > 0
6240 then
6241 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6242 uifontsize
6243 !fontpath
6244 else (
6245 if uifontsize <> 14
6246 then
6247 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6250 Buffer.add_string bb "<defaults ";
6251 add_attrs bb true dc dc;
6252 let kb = keymapsbuf true dc dc in
6253 if Buffer.length kb > 0
6254 then (
6255 Buffer.add_string bb ">\n";
6256 Buffer.add_buffer bb kb;
6257 Buffer.add_string bb "\n</defaults>\n";
6259 else Buffer.add_string bb "/>\n";
6261 let adddoc path pan anchor c bookmarks =
6262 if bookmarks == [] && c = dc && anchor = emptyanchor
6263 then ()
6264 else (
6265 Printf.bprintf bb "<doc path='%s'"
6266 (enent path 0 (String.length path));
6268 if anchor <> emptyanchor
6269 then (
6270 let n, y = anchor in
6271 Printf.bprintf bb " page='%d'" n;
6272 if y > 1e-6
6273 then
6274 Printf.bprintf bb " rely='%f'" y
6278 if pan != 0
6279 then Printf.bprintf bb " pan='%d'" pan;
6281 add_attrs bb false dc c;
6282 let kb = keymapsbuf false dc c in
6284 begin match bookmarks with
6285 | [] ->
6286 if Buffer.length kb > 0
6287 then (
6288 Buffer.add_string bb ">\n";
6289 Buffer.add_buffer bb kb;
6290 Buffer.add_string bb "</doc>\n";
6292 else Buffer.add_string bb "/>\n"
6293 | _ ->
6294 Buffer.add_string bb ">\n<bookmarks>\n";
6295 List.iter (fun (title, _level, (page, rely)) ->
6296 Printf.bprintf bb
6297 "<item title='%s' page='%d'"
6298 (enent title 0 (String.length title))
6299 page
6301 if rely > 1e-6
6302 then
6303 Printf.bprintf bb " rely='%f'" rely
6305 Buffer.add_string bb "/>\n";
6306 ) bookmarks;
6307 Buffer.add_string bb "</bookmarks>";
6308 if Buffer.length kb > 0
6309 then (
6310 Buffer.add_string bb "\n";
6311 Buffer.add_buffer bb kb;
6313 Buffer.add_string bb "\n</doc>\n";
6314 end;
6318 let pan, conf =
6319 match state.mode with
6320 | Birdseye (c, pan, _, _, _) ->
6321 let beyecolumns =
6322 match conf.columns with
6323 | Some ((c, _, _), _) -> Some c
6324 | None -> None
6325 and columns =
6326 match c.columns with
6327 | Some (c, _) -> Some (c, [||])
6328 | None -> None
6330 pan, { c with beyecolumns = beyecolumns; columns = columns }
6331 | _ -> state.x, conf
6333 let basename = Filename.basename state.path in
6334 adddoc basename pan (getanchor ())
6335 { conf with
6336 autoscrollstep =
6337 match state.autoscroll with
6338 | Some step -> step
6339 | None -> conf.autoscrollstep }
6340 (if conf.savebmarks then state.bookmarks else []);
6342 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6343 if basename <> path
6344 then adddoc path x y c bookmarks
6345 ) h;
6346 Buffer.add_string bb "</llppconfig>";
6348 load1 f;
6349 if Buffer.length bb > 0
6350 then
6352 let tmp = !confpath ^ ".tmp" in
6353 let oc = open_out_bin tmp in
6354 Buffer.output_buffer oc bb;
6355 close_out oc;
6356 Unix.rename tmp !confpath;
6357 with exn ->
6358 prerr_endline
6359 ("error while saving configuration: " ^ Printexc.to_string exn)
6361 end;;
6363 let () =
6364 Arg.parse
6365 (Arg.align
6366 [("-p", Arg.String (fun s -> state.password <- s) ,
6367 "<password> Set password");
6369 ("-f", Arg.String (fun s -> Config.fontpath := s),
6370 "<path> Set path to the user interface font");
6372 ("-c", Arg.String (fun s -> Config.confpath := s),
6373 "<path> Set path to the configuration file");
6375 ("-v", Arg.Unit (fun () ->
6376 Printf.printf
6377 "%s\nconfiguration path: %s\n"
6378 (version ())
6379 Config.defconfpath
6381 exit 0), " Print version and exit");
6384 (fun s -> state.path <- s)
6385 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6387 if String.length state.path = 0
6388 then (prerr_endline "file name missing"; exit 1);
6390 Config.load ();
6392 let globalkeyhash = findkeyhash conf "global" in
6393 let wsfd, winw, winh = Wsi.init (object
6394 method expose =
6395 if nogeomcmds state.geomcmds
6396 then display ()
6397 method display = display ()
6398 method reshape w h = reshape w h
6399 method mouse b d x y m = mouse b d x y m
6400 method motion x y = state.mpos <- (x, y); motion x y
6401 method pmotion x y = state.mpos <- (x, y); pmotion x y
6402 method key k m =
6403 let mascm = m land (
6404 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6405 ) in
6406 match state.keystate with
6407 | KSnone ->
6408 let km = k, mascm in
6409 begin
6410 match
6411 try Hashtbl.find globalkeyhash km
6412 with Not_found ->
6413 let modehash = state.uioh#modehash in
6414 try Hashtbl.find modehash km
6415 with Not_found -> KMinsrt (k, m)
6416 with
6417 | KMinsrt (k, m) -> keyboard k m
6418 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6419 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6421 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6422 List.iter (fun (k, m) -> keyboard k m) insrt;
6423 state.keystate <- KSnone
6424 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6425 state.keystate <- KSinto (keys, insrt)
6426 | _ ->
6427 state.keystate <- KSnone
6429 method enter x y = state.mpos <- (x, y); pmotion x y
6430 method leave = state.mpos <- (-1, -1)
6431 method quit = raise Quit
6432 end) conf.winw conf.winh (platform = Posx) in
6434 state.wsfd <- wsfd;
6436 if not (
6437 List.exists GlMisc.check_extension
6438 [ "GL_ARB_texture_rectangle"
6439 ; "GL_EXT_texture_recangle"
6440 ; "GL_NV_texture_rectangle" ]
6442 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6444 let cr, sw = Unix.pipe ()
6445 and sr, cw = Unix.pipe () in
6447 cloexec cr;
6448 cloexec sw;
6449 cloexec sr;
6450 cloexec cw;
6452 setcheckers conf.checkers;
6453 redirectstderr ();
6455 init (cr, cw) (
6456 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6457 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6458 !Config.fontpath
6460 state.sr <- sr;
6461 state.sw <- sw;
6462 state.text <- "Opening " ^ state.path;
6463 reshape winw winh;
6464 opendoc state.path state.password;
6465 state.uioh <- uioh;
6467 let rec loop deadline =
6468 let r =
6469 match state.errfd with
6470 | None -> [state.sr; state.wsfd]
6471 | Some fd -> [state.sr; state.wsfd; fd]
6473 if state.redisplay
6474 then (
6475 state.redisplay <- false;
6476 display ();
6478 let timeout =
6479 let now = now () in
6480 if deadline > now
6481 then (
6482 if deadline = infinity
6483 then ~-.1.0
6484 else max 0.0 (deadline -. now)
6486 else 0.0
6488 let r, _, _ =
6489 try Unix.select r [] [] timeout
6490 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6492 begin match r with
6493 | [] ->
6494 state.ghyll None;
6495 let newdeadline =
6496 if state.ghyll == noghyll
6497 then
6498 match state.autoscroll with
6499 | Some step when step != 0 ->
6500 let y = state.y + step in
6501 let y =
6502 if y < 0
6503 then state.maxy
6504 else if y >= state.maxy then 0 else y
6506 gotoy y;
6507 if state.mode = View
6508 then state.text <- "";
6509 deadline +. 0.01
6510 | _ -> infinity
6511 else deadline +. 0.01
6513 loop newdeadline
6515 | l ->
6516 let rec checkfds = function
6517 | [] -> ()
6518 | fd :: rest when fd = state.sr ->
6519 let cmd = readcmd state.sr in
6520 act cmd;
6521 checkfds rest
6523 | fd :: rest when fd = state.wsfd ->
6524 Wsi.readresp fd;
6525 checkfds rest
6527 | fd :: rest ->
6528 let s = String.create 80 in
6529 let n = Unix.read fd s 0 80 in
6530 if conf.redirectstderr
6531 then (
6532 Buffer.add_substring state.errmsgs s 0 n;
6533 state.newerrmsgs <- true;
6534 state.redisplay <- true;
6536 else (
6537 prerr_string (String.sub s 0 n);
6538 flush stderr;
6540 checkfds rest
6542 checkfds l;
6543 let newdeadline =
6544 let deadline1 =
6545 if deadline = infinity
6546 then now () +. 0.01
6547 else deadline
6549 match state.autoscroll with
6550 | Some step when step != 0 -> deadline1
6551 | _ -> if state.ghyll == noghyll then infinity else deadline1
6553 loop newdeadline
6554 end;
6557 loop infinity;
6558 with Quit ->
6559 Config.save ();
6560 exit 0;