Fix keymap saving
[llpp.git] / main.ml
blob1a8457879dbd7f1bd6bea03cf58138ad7a53ec02
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 | Uunexpected of string
7 | Ulaunch of string
8 | Unamed of string
9 | Uremote of (string * int)
10 and facename = string;;
12 let dolog fmt = Printf.kprintf prerr_endline fmt;;
13 let now = Unix.gettimeofday;;
15 type params = (angle * proportional * trimparams
16 * texcount * sliceheight * memsize
17 * colorspace * fontpath)
18 and pageno = int
19 and width = int
20 and height = int
21 and leftx = int
22 and opaque = string
23 and recttype = int
24 and pixmapsize = int
25 and angle = int
26 and proportional = bool
27 and trimmargins = bool
28 and interpagespace = int
29 and texcount = int
30 and sliceheight = int
31 and gen = int
32 and top = float
33 and fontpath = string
34 and memsize = int
35 and aalevel = int
36 and irect = (int * int * int * int)
37 and trimparams = (trimmargins * irect)
38 and colorspace = | Rgb | Bgr | Gray
41 type keymap =
42 | KMinsrt of key
43 | KMinsrl of key list
44 | KMmulti of key list * key list
45 and key = int * int
46 and keyhash = (key, keymap) Hashtbl.t
47 and keystate =
48 | KSnone
49 | KSinto of (key list * key list)
52 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
53 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
55 type pipe = (Unix.file_descr * Unix.file_descr);;
57 external init : pipe -> params -> unit = "ml_init";;
58 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
59 external copysel : string -> opaque -> unit = "ml_copysel";;
60 external getpdimrect : int -> float array = "ml_getpdimrect";;
61 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
62 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
63 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
64 external measurestr : int -> string -> float = "ml_measure_string";;
65 external getmaxw : unit -> float = "ml_getmaxw";;
66 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
67 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
68 external platform : unit -> platform = "ml_platform";;
69 external setaalevel : int -> unit = "ml_setaalevel";;
70 external realloctexts : int -> bool = "ml_realloctexts";;
71 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
73 let platform_to_string = function
74 | Punknown -> "unknown"
75 | Plinux -> "Linux"
76 | Posx -> "OSX"
77 | Psun -> "Sun"
78 | Pfreebsd -> "FreeBSD"
79 | Pdragonflybsd -> "DragonflyBSD"
80 | Popenbsd -> "OpenBSD"
81 | Pnetbsd -> "NetBSD"
82 | Pcygwin -> "Cygwin"
85 let platform = platform ();;
87 type x = int
88 and y = int
89 and tilex = int
90 and tiley = int
91 and tileparams = (x * y * width * height * tilex * tiley)
94 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
96 type mpos = int * int
97 and mstate =
98 | Msel of (mpos * mpos)
99 | Mpan of mpos
100 | Mscrolly | Mscrollx
101 | Mzoom of (int * int)
102 | Mzoomrect of (mpos * mpos)
103 | Mnone
106 type textentry = string * string * onhist option * onkey * ondone
107 and onkey = string -> int -> te
108 and ondone = string -> unit
109 and histcancel = unit -> unit
110 and onhist = ((histcmd -> string) * histcancel)
111 and histcmd = HCnext | HCprev | HCfirst | HClast
112 and te =
113 | TEstop
114 | TEdone of string
115 | TEcont of string
116 | TEswitch of textentry
119 type 'a circbuf =
120 { store : 'a array
121 ; mutable rc : int
122 ; mutable wc : int
123 ; mutable len : int
127 let bound v minv maxv =
128 max minv (min maxv v);
131 let cbnew n v =
132 { store = Array.create n v
133 ; rc = 0
134 ; wc = 0
135 ; len = 0
139 let drawstring size x y s =
140 Gl.enable `blend;
141 Gl.enable `texture_2d;
142 ignore (drawstr size x y s);
143 Gl.disable `blend;
144 Gl.disable `texture_2d;
147 let drawstring1 size x y s =
148 drawstr size x y s;
151 let drawstring2 size x y fmt =
152 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
155 let cbcap b = Array.length b.store;;
157 let cbput b v =
158 let cap = cbcap b in
159 b.store.(b.wc) <- v;
160 b.wc <- (b.wc + 1) mod cap;
161 b.rc <- b.wc;
162 b.len <- min (b.len + 1) cap;
165 let cbempty b = b.len = 0;;
167 let cbgetg b circular dir =
168 if cbempty b
169 then b.store.(0)
170 else
171 let rc = b.rc + dir in
172 let rc =
173 if circular
174 then (
175 if rc = -1
176 then b.len-1
177 else (
178 if rc = b.len
179 then 0
180 else rc
183 else max 0 (min rc (b.len-1))
185 b.rc <- rc;
186 b.store.(rc);
189 let cbget b = cbgetg b false;;
190 let cbgetc b = cbgetg b true;;
192 type page =
193 { pageno : int
194 ; pagedimno : int
195 ; pagew : int
196 ; pageh : int
197 ; pagex : int
198 ; pagey : int
199 ; pagevw : int
200 ; pagevh : int
201 ; pagedispx : int
202 ; pagedispy : int
206 let debugl l =
207 dolog "l %d dim=%d {" l.pageno l.pagedimno;
208 dolog " WxH %dx%d" l.pagew l.pageh;
209 dolog " vWxH %dx%d" l.pagevw l.pagevh;
210 dolog " pagex,y %d,%d" l.pagex l.pagey;
211 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
212 dolog "}";
215 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
216 dolog "rect {";
217 dolog " x0,y0=(% f, % f)" x0 y0;
218 dolog " x1,y1=(% f, % f)" x1 y1;
219 dolog " x2,y2=(% f, % f)" x2 y2;
220 dolog " x3,y3=(% f, % f)" x3 y3;
221 dolog "}";
224 type columns =
225 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
226 and multicol = columncount * covercount * covercount
227 and pdimno = int
228 and columncount = int
229 and covercount = int;;
231 type conf =
232 { mutable scrollbw : int
233 ; mutable scrollh : int
234 ; mutable icase : bool
235 ; mutable preload : bool
236 ; mutable pagebias : int
237 ; mutable verbose : bool
238 ; mutable debug : bool
239 ; mutable scrollstep : int
240 ; mutable maxhfit : bool
241 ; mutable crophack : bool
242 ; mutable autoscrollstep : int
243 ; mutable maxwait : float option
244 ; mutable hlinks : bool
245 ; mutable underinfo : bool
246 ; mutable interpagespace : interpagespace
247 ; mutable zoom : float
248 ; mutable presentation : bool
249 ; mutable angle : angle
250 ; mutable winw : int
251 ; mutable winh : int
252 ; mutable savebmarks : bool
253 ; mutable proportional : proportional
254 ; mutable trimmargins : trimmargins
255 ; mutable trimfuzz : irect
256 ; mutable memlimit : memsize
257 ; mutable texcount : texcount
258 ; mutable sliceheight : sliceheight
259 ; mutable thumbw : width
260 ; mutable jumpback : bool
261 ; mutable bgcolor : float * float * float
262 ; mutable bedefault : bool
263 ; mutable scrollbarinpm : bool
264 ; mutable tilew : int
265 ; mutable tileh : int
266 ; mutable mustoresize : memsize
267 ; mutable checkers : bool
268 ; mutable aalevel : int
269 ; mutable urilauncher : string
270 ; mutable pathlauncher : string
271 ; mutable colorspace : colorspace
272 ; mutable invert : bool
273 ; mutable colorscale : float
274 ; mutable redirectstderr : bool
275 ; mutable ghyllscroll : (int * int * int) option
276 ; mutable columns : columns option
277 ; mutable beyecolumns : columncount option
278 ; mutable selcmd : string
279 ; mutable updatecurs : bool
280 ; mutable keyhashes : (string * keyhash) list
284 type anchor = pageno * top;;
286 type outline = string * int * anchor;;
288 type rect = float * float * float * float * float * float * float * float;;
290 type tile = opaque * pixmapsize * elapsed
291 and elapsed = float;;
292 type pagemapkey = pageno * gen;;
293 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
294 and row = int
295 and col = int;;
297 let emptyanchor = (0, 0.0);;
299 type infochange = | Memused | Docinfo | Pdim;;
301 class type uioh = object
302 method display : unit
303 method key : int -> int -> uioh
304 method button : int -> bool -> int -> int -> int -> uioh
305 method motion : int -> int -> uioh
306 method pmotion : int -> int -> uioh
307 method infochanged : infochange -> unit
308 method scrollpw : (int * float * float)
309 method scrollph : (int * float * float)
310 method modehash : keyhash
311 end;;
313 type mode =
314 | Birdseye of (conf * leftx * pageno * pageno * anchor)
315 | Textentry of (textentry * onleave)
316 | View
317 and onleave = leavetextentrystatus -> unit
318 and leavetextentrystatus = | Cancel | Confirm
319 and helpitem = string * int * action
320 and action =
321 | Noaction
322 | Action of (uioh -> uioh)
325 let isbirdseye = function Birdseye _ -> true | _ -> false;;
326 let istextentry = function Textentry _ -> true | _ -> false;;
328 type currently =
329 | Idle
330 | Loading of (page * gen)
331 | Tiling of (
332 page * opaque * colorspace * angle * gen * col * row * width * height
334 | Outlining of outline list
337 let emptykeyhash = Hashtbl.create 0;;
338 let nouioh : uioh = object (self)
339 method display = ()
340 method key _ _ = self
341 method button _ _ _ _ _ = self
342 method motion _ _ = self
343 method pmotion _ _ = self
344 method infochanged _ = ()
345 method scrollpw = (0, nan, nan)
346 method scrollph = (0, nan, nan)
347 method modehash = emptykeyhash
348 end;;
350 type state =
351 { mutable sr : Unix.file_descr
352 ; mutable sw : Unix.file_descr
353 ; mutable wsfd : Unix.file_descr
354 ; mutable errfd : Unix.file_descr option
355 ; mutable stderr : Unix.file_descr
356 ; mutable errmsgs : Buffer.t
357 ; mutable newerrmsgs : bool
358 ; mutable w : int
359 ; mutable x : int
360 ; mutable y : int
361 ; mutable scrollw : int
362 ; mutable hscrollh : int
363 ; mutable anchor : anchor
364 ; mutable ranchors : (string * string * anchor) list
365 ; mutable maxy : int
366 ; mutable layout : page list
367 ; pagemap : (pagemapkey, opaque) Hashtbl.t
368 ; tilemap : (tilemapkey, tile) Hashtbl.t
369 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
370 ; mutable pdims : (pageno * width * height * leftx) list
371 ; mutable pagecount : int
372 ; mutable currently : currently
373 ; mutable mstate : mstate
374 ; mutable searchpattern : string
375 ; mutable rects : (pageno * recttype * rect) list
376 ; mutable rects1 : (pageno * recttype * rect) list
377 ; mutable text : string
378 ; mutable fullscreen : (width * height) option
379 ; mutable mode : mode
380 ; mutable uioh : uioh
381 ; mutable outlines : outline array
382 ; mutable bookmarks : outline list
383 ; mutable path : string
384 ; mutable password : string
385 ; mutable invalidated : int
386 ; mutable memused : memsize
387 ; mutable gen : gen
388 ; mutable throttle : (page list * int * float) option
389 ; mutable autoscroll : int option
390 ; mutable ghyll : (int option -> unit)
391 ; mutable help : helpitem array
392 ; mutable docinfo : (int * string) list
393 ; mutable texid : GlTex.texture_id option
394 ; hists : hists
395 ; mutable prevzoom : float
396 ; mutable progress : float
397 ; mutable redisplay : bool
398 ; mutable mpos : mpos
399 ; mutable keystate : keystate
401 and hists =
402 { pat : string circbuf
403 ; pag : string circbuf
404 ; nav : anchor circbuf
405 ; sel : string circbuf
409 let defconf =
410 { scrollbw = 7
411 ; scrollh = 12
412 ; icase = true
413 ; preload = true
414 ; pagebias = 0
415 ; verbose = false
416 ; debug = false
417 ; scrollstep = 24
418 ; maxhfit = true
419 ; crophack = false
420 ; autoscrollstep = 2
421 ; maxwait = None
422 ; hlinks = false
423 ; underinfo = false
424 ; interpagespace = 2
425 ; zoom = 1.0
426 ; presentation = false
427 ; angle = 0
428 ; winw = 900
429 ; winh = 900
430 ; savebmarks = true
431 ; proportional = true
432 ; trimmargins = false
433 ; trimfuzz = (0,0,0,0)
434 ; memlimit = 32 lsl 20
435 ; texcount = 256
436 ; sliceheight = 24
437 ; thumbw = 76
438 ; jumpback = true
439 ; bgcolor = (0.5, 0.5, 0.5)
440 ; bedefault = false
441 ; scrollbarinpm = true
442 ; tilew = 2048
443 ; tileh = 2048
444 ; mustoresize = 128 lsl 20
445 ; checkers = true
446 ; aalevel = 8
447 ; urilauncher =
448 (match platform with
449 | Plinux | Pfreebsd | Pdragonflybsd
450 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
451 | Posx -> "open \"%s\""
452 | Pcygwin -> "cygstart %s"
453 | Punknown -> "echo %s")
454 ; pathlauncher = "lp \"%s\""
455 ; selcmd =
456 (match platform with
457 | Plinux | Pfreebsd | Pdragonflybsd
458 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
459 | Posx -> "pbcopy"
460 | Pcygwin -> "wsel"
461 | Punknown -> "cat")
462 ; colorspace = Rgb
463 ; invert = false
464 ; colorscale = 1.0
465 ; redirectstderr = false
466 ; ghyllscroll = None
467 ; columns = None
468 ; beyecolumns = None
469 ; updatecurs = false
470 ; keyhashes =
471 let mk n = (n, Hashtbl.create 1) in
472 [ mk "global"
473 ; mk "info"
474 ; mk "help"
475 ; mk "outline"
476 ; mk "listview"
477 ; mk "birdseye"
478 ; mk "textentry"
483 let findkeyhash c name =
484 try List.assoc name c.keyhashes
485 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
488 let conf = { defconf with angle = defconf.angle };;
490 type fontstate =
491 { mutable fontsize : int
492 ; mutable wwidth : float
493 ; mutable maxrows : int
497 let fstate =
498 { fontsize = 14
499 ; wwidth = nan
500 ; maxrows = -1
504 let setfontsize n =
505 fstate.fontsize <- n;
506 fstate.wwidth <- measurestr fstate.fontsize "w";
507 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
510 let geturl s =
511 let colonpos = try String.index s ':' with Not_found -> -1 in
512 let len = String.length s in
513 if colonpos >= 0 && colonpos + 3 < len
514 then (
515 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
516 then
517 let schemestartpos =
518 try String.rindex_from s colonpos ' '
519 with Not_found -> -1
521 let scheme =
522 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
524 match scheme with
525 | "http" | "ftp" | "mailto" ->
526 let epos =
527 try String.index_from s colonpos ' '
528 with Not_found -> len
530 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
531 | _ -> ""
532 else ""
534 else ""
537 let popen =
538 let shell, farg = "/bin/sh", "-c" in
539 fun s ->
540 let args = [|shell; farg; s|] in
541 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
544 let gotouri uri =
545 if String.length conf.urilauncher = 0
546 then print_endline uri
547 else (
548 let url = geturl uri in
549 if String.length url = 0
550 then print_endline uri
551 else
552 let re = Str.regexp "%s" in
553 let command = Str.global_replace re url conf.urilauncher in
554 try popen command
555 with exn ->
556 Printf.eprintf
557 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
558 flush stderr;
562 let version () =
563 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
564 (platform_to_string platform) Sys.word_size Sys.ocaml_version
567 let makehelp () =
568 let strings = version () :: "" :: Help.keys in
569 Array.of_list (
570 List.map (fun s ->
571 let url = geturl s in
572 if String.length url > 0
573 then (s, 0, Action (fun u -> gotouri url; u))
574 else (s, 0, Noaction)
575 ) strings);
578 let noghyll _ = ();;
580 let state =
581 { sr = Unix.stdin
582 ; sw = Unix.stdin
583 ; wsfd = Unix.stdin
584 ; errfd = None
585 ; stderr = Unix.stderr
586 ; errmsgs = Buffer.create 0
587 ; newerrmsgs = false
588 ; x = 0
589 ; y = 0
590 ; w = 0
591 ; scrollw = 0
592 ; hscrollh = 0
593 ; anchor = emptyanchor
594 ; ranchors = []
595 ; layout = []
596 ; maxy = max_int
597 ; tilelru = Queue.create ()
598 ; pagemap = Hashtbl.create 10
599 ; tilemap = Hashtbl.create 10
600 ; pdims = []
601 ; pagecount = 0
602 ; currently = Idle
603 ; mstate = Mnone
604 ; rects = []
605 ; rects1 = []
606 ; text = ""
607 ; mode = View
608 ; fullscreen = None
609 ; searchpattern = ""
610 ; outlines = [||]
611 ; bookmarks = []
612 ; path = ""
613 ; password = ""
614 ; invalidated = 0
615 ; hists =
616 { nav = cbnew 10 (0, 0.0)
617 ; pat = cbnew 10 ""
618 ; pag = cbnew 10 ""
619 ; sel = cbnew 10 ""
621 ; memused = 0
622 ; gen = 0
623 ; throttle = None
624 ; autoscroll = None
625 ; ghyll = noghyll
626 ; help = makehelp ()
627 ; docinfo = []
628 ; texid = None
629 ; prevzoom = 1.0
630 ; progress = -1.0
631 ; uioh = nouioh
632 ; redisplay = false
633 ; mpos = (-1, -1)
634 ; keystate = KSnone
638 let vlog fmt =
639 if conf.verbose
640 then
641 Printf.kprintf prerr_endline fmt
642 else
643 Printf.kprintf ignore fmt
646 let launchpath () =
647 if String.length conf.pathlauncher = 0
648 then print_endline state.path
649 else (
650 let re = Str.regexp "%s" in
651 let command = Str.global_replace re state.path conf.pathlauncher in
652 try popen command
653 with exn ->
654 Printf.eprintf
655 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
656 flush stderr;
660 let redirectstderr () =
661 if conf.redirectstderr
662 then
663 let rfd, wfd = Unix.pipe () in
664 state.stderr <- Unix.dup Unix.stderr;
665 state.errfd <- Some rfd;
666 Unix.dup2 wfd Unix.stderr;
667 else (
668 state.newerrmsgs <- false;
669 begin match state.errfd with
670 | Some fd ->
671 Unix.close fd;
672 Unix.dup2 state.stderr Unix.stderr;
673 state.errfd <- None;
674 | None -> ()
675 end;
676 prerr_string (Buffer.contents state.errmsgs);
677 flush stderr;
678 Buffer.clear state.errmsgs;
682 module G =
683 struct
684 let postRedisplay who =
685 if conf.verbose
686 then prerr_endline ("redisplay for " ^ who);
687 state.redisplay <- true;
689 end;;
691 let getopaque pageno =
692 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
693 with Not_found -> None
696 let putopaque pageno opaque =
697 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
700 let pagetranslatepoint l x y =
701 let dy = y - l.pagedispy in
702 let y = dy + l.pagey in
703 let dx = x - l.pagedispx in
704 let x = dx + l.pagex in
705 (x, y);
708 let getunder x y =
709 let rec f = function
710 | l :: rest ->
711 begin match getopaque l.pageno with
712 | Some opaque ->
713 let x0 = l.pagedispx in
714 let x1 = x0 + l.pagevw in
715 let y0 = l.pagedispy in
716 let y1 = y0 + l.pagevh in
717 if y >= y0 && y <= y1 && x >= x0 && x <= x1
718 then
719 let px, py = pagetranslatepoint l x y in
720 match whatsunder opaque px py with
721 | Unone -> f rest
722 | under -> under
723 else f rest
724 | _ ->
725 f rest
727 | [] -> Unone
729 f state.layout
732 let showtext c s =
733 state.text <- Printf.sprintf "%c%s" c s;
734 G.postRedisplay "showtext";
737 let updateunder x y =
738 match getunder x y with
739 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
740 | Ulinkuri uri ->
741 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
742 Wsi.setcursor Wsi.CURSOR_INFO
743 | Ulinkgoto (page, _) ->
744 if conf.underinfo
745 then showtext 'p' ("age: " ^ string_of_int (page+1));
746 Wsi.setcursor Wsi.CURSOR_INFO
747 | Utext s ->
748 if conf.underinfo then showtext 'f' ("ont: " ^ s);
749 Wsi.setcursor Wsi.CURSOR_TEXT
750 | Uunexpected s ->
751 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
752 Wsi.setcursor Wsi.CURSOR_INHERIT
753 | Ulaunch s ->
754 if conf.underinfo then showtext 'l' ("launch: " ^ s);
755 Wsi.setcursor Wsi.CURSOR_INHERIT
756 | Unamed s ->
757 if conf.underinfo then showtext 'n' ("named: " ^ s);
758 Wsi.setcursor Wsi.CURSOR_INHERIT
759 | Uremote (filename, pageno) ->
760 if conf.underinfo then showtext 'r'
761 (Printf.sprintf "emote: %s (%d)" filename pageno);
762 Wsi.setcursor Wsi.CURSOR_INFO
765 let addchar s c =
766 let b = Buffer.create (String.length s + 1) in
767 Buffer.add_string b s;
768 Buffer.add_char b c;
769 Buffer.contents b;
772 let colorspace_of_string s =
773 match String.lowercase s with
774 | "rgb" -> Rgb
775 | "bgr" -> Bgr
776 | "gray" -> Gray
777 | _ -> failwith "invalid colorspace"
780 let int_of_colorspace = function
781 | Rgb -> 0
782 | Bgr -> 1
783 | Gray -> 2
786 let colorspace_of_int = function
787 | 0 -> Rgb
788 | 1 -> Bgr
789 | 2 -> Gray
790 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
793 let colorspace_to_string = function
794 | Rgb -> "rgb"
795 | Bgr -> "bgr"
796 | Gray -> "gray"
799 let intentry_with_suffix text key =
800 let c =
801 if key >= 32 && key < 127
802 then Char.chr key
803 else '\000'
805 match Char.lowercase c with
806 | '0' .. '9' ->
807 let text = addchar text c in
808 TEcont text
810 | 'k' | 'm' | 'g' ->
811 let text = addchar text c in
812 TEcont text
814 | _ ->
815 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
816 TEcont text
819 let columns_to_string (n, a, b) =
820 if a = 0 && b = 0
821 then Printf.sprintf "%d" n
822 else Printf.sprintf "%d,%d,%d" n a b;
825 let columns_of_string s =
827 (int_of_string s, 0, 0)
828 with _ ->
829 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
832 let writecmd fd s =
833 let len = String.length s in
834 let n = 4 + len in
835 let b = Buffer.create n in
836 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
837 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
838 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
839 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
840 Buffer.add_string b s;
841 let s' = Buffer.contents b in
842 let n' = Unix.write fd s' 0 n in
843 if n' != n then failwith "write failed";
846 let readcmd fd =
847 let s = "xxxx" in
848 let n = Unix.read fd s 0 4 in
849 if n != 4 then failwith "incomplete read(len)";
850 let len = 0
851 lor (Char.code s.[0] lsl 24)
852 lor (Char.code s.[1] lsl 16)
853 lor (Char.code s.[2] lsl 8)
854 lor (Char.code s.[3] lsl 0)
856 let s = String.create len in
857 let n = Unix.read fd s 0 len in
858 if n != len then failwith "incomplete read(data)";
862 let makecmd s l =
863 let b = Buffer.create 10 in
864 Buffer.add_string b s;
865 let rec combine = function
866 | [] -> b
867 | x :: xs ->
868 Buffer.add_char b ' ';
869 let s =
870 match x with
871 | `b b -> if b then "1" else "0"
872 | `s s -> s
873 | `i i -> string_of_int i
874 | `f f -> string_of_float f
875 | `I f -> string_of_int (truncate f)
877 Buffer.add_string b s;
878 combine xs;
880 combine l;
883 let wcmd s l =
884 let cmd = Buffer.contents (makecmd s l) in
885 writecmd state.sw cmd;
888 let calcips h =
889 if conf.presentation
890 then
891 let d = conf.winh - h in
892 max 0 ((d + 1) / 2)
893 else
894 conf.interpagespace
897 let calcheight () =
898 let rec f pn ph pi fh l =
899 match l with
900 | (n, _, h, _) :: rest ->
901 let ips = calcips h in
902 let fh =
903 if conf.presentation
904 then fh+ips
905 else (
906 if isbirdseye state.mode && pn = 0
907 then fh + ips
908 else fh
911 let fh = fh + ((n - pn) * (ph + pi)) in
912 f n h ips fh rest;
914 | [] ->
915 let inc =
916 if conf.presentation || (isbirdseye state.mode && pn = 0)
917 then 0
918 else -pi
920 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
921 max 0 fh
923 let fh = f 0 0 0 0 state.pdims in
927 let calcheight () =
928 match conf.columns with
929 | None -> calcheight ()
930 | Some (_, b) ->
931 if Array.length b > 0
932 then
933 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
934 y + h
935 else 0
938 let getpageyh pageno =
939 let rec f pn ph pi y l =
940 match l with
941 | (n, _, h, _) :: rest ->
942 let ips = calcips h in
943 if n >= pageno
944 then
945 let h = if n = pageno then h else ph in
946 if conf.presentation && n = pageno
947 then
948 y + (pageno - pn) * (ph + pi) + pi, h
949 else
950 y + (pageno - pn) * (ph + pi), h
951 else
952 let y = y + (if conf.presentation then pi else 0) in
953 let y = y + (n - pn) * (ph + pi) in
954 f n h ips y rest
956 | [] ->
957 y + (pageno - pn) * (ph + pi), ph
959 f 0 0 0 0 state.pdims
962 let getpageyh pageno =
963 match conf.columns with
964 | None -> getpageyh pageno
965 | Some (_, b) ->
966 let (_, _, y, (_, _, h, _)) = b.(pageno) in
967 y, h
970 let getpagedim pageno =
971 let rec f ppdim l =
972 match l with
973 | (n, _, _, _) as pdim :: rest ->
974 if n >= pageno
975 then (if n = pageno then pdim else ppdim)
976 else f pdim rest
978 | [] -> ppdim
980 f (-1, -1, -1, -1) state.pdims
983 let getpagey pageno = fst (getpageyh pageno);;
985 let layout1 y sh =
986 let sh = sh - state.hscrollh in
987 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
988 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
989 match pdims with
990 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
991 let ips = calcips h in
992 let yinc =
993 if conf.presentation || (isbirdseye state.mode && pageno = 0)
994 then ips
995 else 0
997 (w, h, ips, xoff), rest, pdimno + 1, yinc
998 | _ ->
999 prev, pdims, pdimno, 0
1001 let dy = dy + yinc in
1002 let py = py + yinc in
1003 if pageno = state.pagecount || dy >= sh
1004 then
1005 accu
1006 else
1007 let vy = y + dy in
1008 if py + h <= vy - yinc
1009 then
1010 let py = py + h + ips in
1011 let dy = max 0 (py - y) in
1012 f ~pageno:(pageno+1)
1013 ~pdimno
1014 ~prev:curr
1017 ~pdims:rest
1018 ~accu
1019 else
1020 let pagey = vy - py in
1021 let pagevh = h - pagey in
1022 let pagevh = min (sh - dy) pagevh in
1023 let off = if yinc > 0 then py - vy else 0 in
1024 let py = py + h + ips in
1025 let pagex, dx =
1026 let xoff = xoff +
1027 if state.w < conf.winw - state.scrollw
1028 then (conf.winw - state.scrollw - state.w) / 2
1029 else 0
1031 let dispx = xoff + state.x in
1032 if dispx < 0
1033 then (-dispx, 0)
1034 else (0, dispx)
1036 let pagevw =
1037 let lw = w - pagex in
1038 min lw (conf.winw - state.scrollw)
1040 let e =
1041 { pageno = pageno
1042 ; pagedimno = pdimno
1043 ; pagew = w
1044 ; pageh = h
1045 ; pagex = pagex
1046 ; pagey = pagey + off
1047 ; pagevw = pagevw
1048 ; pagevh = pagevh - off
1049 ; pagedispx = dx
1050 ; pagedispy = dy + off
1053 let accu = e :: accu in
1054 f ~pageno:(pageno+1)
1055 ~pdimno
1056 ~prev:curr
1058 ~dy:(dy+pagevh+ips)
1059 ~pdims:rest
1060 ~accu
1062 if state.invalidated = 0
1063 then (
1064 let accu =
1066 ~pageno:0
1067 ~pdimno:~-1
1068 ~prev:(0,0,0,0)
1069 ~py:0
1070 ~dy:0
1071 ~pdims:state.pdims
1072 ~accu:[]
1074 List.rev accu
1076 else
1080 let layoutN ((columns, coverA, coverB), b) y sh =
1081 let sh = sh - state.hscrollh in
1082 let rec fold accu n =
1083 if n = Array.length b
1084 then accu
1085 else
1086 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1087 if (vy - y) > sh &&
1088 (n = coverA - 1
1089 || n = state.pagecount - coverB
1090 || (n - coverA) mod columns = columns - 1)
1091 then accu
1092 else
1093 let accu =
1094 if vy + h > y
1095 then
1096 let pagey = max 0 (y - vy) in
1097 let pagedispy = if pagey > 0 then 0 else vy - y in
1098 let pagedispx, pagex, pagevw =
1099 let pdx =
1100 if n = coverA - 1 || n = state.pagecount - coverB
1101 then state.x + (conf.winw - state.scrollw - w) / 2
1102 else dx + xoff + state.x
1104 if pdx < 0
1105 then 0, -pdx, w + pdx
1106 else pdx, 0, min (conf.winw - state.scrollw) w
1108 let pagevh = min (h - pagey) (sh - pagedispy) in
1109 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1110 then
1111 let e =
1112 { pageno = n
1113 ; pagedimno = pdimno
1114 ; pagew = w
1115 ; pageh = h
1116 ; pagex = pagex
1117 ; pagey = pagey
1118 ; pagevw = pagevw
1119 ; pagevh = pagevh
1120 ; pagedispx = pagedispx
1121 ; pagedispy = pagedispy
1124 e :: accu
1125 else
1126 accu
1127 else
1128 accu
1130 fold accu (n+1)
1132 if state.invalidated = 0
1133 then List.rev (fold [] 0)
1134 else []
1137 let layout y sh =
1138 match conf.columns with
1139 | None -> layout1 y sh
1140 | Some c -> layoutN c y sh
1143 let clamp incr =
1144 let y = state.y + incr in
1145 let y = max 0 y in
1146 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1150 let itertiles l f =
1151 let tilex = l.pagex mod conf.tilew in
1152 let tiley = l.pagey mod conf.tileh in
1154 let col = l.pagex / conf.tilew in
1155 let row = l.pagey / conf.tileh in
1157 let vw =
1158 let a = l.pagew - l.pagex in
1159 let b = conf.winw - state.scrollw in
1160 min a b
1161 and vh = l.pagevh in
1163 let rec rowloop row y0 dispy h =
1164 if h = 0
1165 then ()
1166 else (
1167 let dh = conf.tileh - y0 in
1168 let dh = min h dh in
1169 let rec colloop col x0 dispx w =
1170 if w = 0
1171 then ()
1172 else (
1173 let dw = conf.tilew - x0 in
1174 let dw = min w dw in
1176 f col row dispx dispy x0 y0 dw dh;
1177 colloop (col+1) 0 (dispx+dw) (w-dw)
1180 colloop col tilex l.pagedispx vw;
1181 rowloop (row+1) 0 (dispy+dh) (h-dh)
1184 if vw > 0 && vh > 0
1185 then rowloop row tiley l.pagedispy vh;
1188 let gettileopaque l col row =
1189 let key =
1190 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1192 try Some (Hashtbl.find state.tilemap key)
1193 with Not_found -> None
1196 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1197 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1198 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1201 let drawtiles l color =
1202 GlDraw.color color;
1203 let f col row x y tilex tiley w h =
1204 match gettileopaque l col row with
1205 | Some (opaque, _, t) ->
1206 let params = x, y, w, h, tilex, tiley in
1207 if conf.invert
1208 then (
1209 Gl.enable `blend;
1210 GlFunc.blend_func `zero `one_minus_src_color;
1212 drawtile params opaque;
1213 if conf.invert
1214 then Gl.disable `blend;
1215 if conf.debug
1216 then (
1217 let s = Printf.sprintf
1218 "%d[%d,%d] %f sec"
1219 l.pageno col row t
1221 let w = measurestr fstate.fontsize s in
1222 GlMisc.push_attrib [`current];
1223 GlDraw.color (0.0, 0.0, 0.0);
1224 GlDraw.rect
1225 (float (x-2), float (y-2))
1226 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1227 GlDraw.color (1.0, 1.0, 1.0);
1228 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1229 GlMisc.pop_attrib ();
1232 | _ ->
1233 let w =
1234 let lw = conf.winw - state.scrollw - x in
1235 min lw w
1236 and h =
1237 let lh = conf.winh - y in
1238 min lh h
1240 Gl.enable `texture_2d;
1241 begin match state.texid with
1242 | Some id ->
1243 GlTex.bind_texture `texture_2d id;
1244 let x0 = float x
1245 and y0 = float y
1246 and x1 = float (x+w)
1247 and y1 = float (y+h) in
1249 let tw = float w /. 64.0
1250 and th = float h /. 64.0 in
1251 let tx0 = float tilex /. 64.0
1252 and ty0 = float tiley /. 64.0 in
1253 let tx1 = tx0 +. tw
1254 and ty1 = ty0 +. th in
1255 GlDraw.begins `quads;
1256 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1257 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1258 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1259 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1260 GlDraw.ends ();
1262 Gl.disable `texture_2d;
1263 | None ->
1264 GlDraw.color (1.0, 1.0, 1.0);
1265 GlDraw.rect
1266 (float x, float y)
1267 (float (x+w), float (y+h));
1268 end;
1269 if w > 128 && h > fstate.fontsize + 10
1270 then (
1271 GlDraw.color (0.0, 0.0, 0.0);
1272 let c, r =
1273 if conf.verbose
1274 then (col*conf.tilew, row*conf.tileh)
1275 else col, row
1277 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1279 GlDraw.color color;
1281 itertiles l f
1284 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1286 let tilevisible1 l x y =
1287 let ax0 = l.pagex
1288 and ax1 = l.pagex + l.pagevw
1289 and ay0 = l.pagey
1290 and ay1 = l.pagey + l.pagevh in
1292 let bx0 = x
1293 and by0 = y in
1294 let bx1 = min (bx0 + conf.tilew) l.pagew
1295 and by1 = min (by0 + conf.tileh) l.pageh in
1297 let rx0 = max ax0 bx0
1298 and ry0 = max ay0 by0
1299 and rx1 = min ax1 bx1
1300 and ry1 = min ay1 by1 in
1302 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1303 nonemptyintersection
1306 let tilevisible layout n x y =
1307 let rec findpageinlayout = function
1308 | l :: _ when l.pageno = n -> tilevisible1 l x y
1309 | _ :: rest -> findpageinlayout rest
1310 | [] -> false
1312 findpageinlayout layout
1315 let tileready l x y =
1316 tilevisible1 l x y &&
1317 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1320 let tilepage n p layout =
1321 let rec loop = function
1322 | l :: rest ->
1323 if l.pageno = n
1324 then
1325 let f col row _ _ _ _ _ _ =
1326 if state.currently = Idle
1327 then
1328 match gettileopaque l col row with
1329 | Some _ -> ()
1330 | None ->
1331 let x = col*conf.tilew
1332 and y = row*conf.tileh in
1333 let w =
1334 let w = l.pagew - x in
1335 min w conf.tilew
1337 let h =
1338 let h = l.pageh - y in
1339 min h conf.tileh
1341 wcmd "tile"
1342 [`s p
1343 ;`i x
1344 ;`i y
1345 ;`i w
1346 ;`i h
1348 state.currently <-
1349 Tiling (
1350 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1351 conf.tilew, conf.tileh
1354 itertiles l f;
1355 else
1356 loop rest
1358 | [] -> ()
1360 if state.invalidated = 0 then loop layout;
1363 let preloadlayout visiblepages =
1364 let presentation = conf.presentation in
1365 let interpagespace = conf.interpagespace in
1366 let maxy = state.maxy in
1367 conf.presentation <- false;
1368 conf.interpagespace <- 0;
1369 state.maxy <- calcheight ();
1370 let y =
1371 match visiblepages with
1372 | [] -> 0
1373 | l :: _ -> getpagey l.pageno + l.pagey
1375 let y = if y < conf.winh then 0 else y - conf.winh in
1376 let h = state.y - y + conf.winh*3 in
1377 let pages = layout y h in
1378 conf.presentation <- presentation;
1379 conf.interpagespace <- interpagespace;
1380 state.maxy <- maxy;
1381 pages;
1384 let load pages =
1385 let rec loop pages =
1386 if state.currently != Idle
1387 then ()
1388 else
1389 match pages with
1390 | l :: rest ->
1391 begin match getopaque l.pageno with
1392 | None ->
1393 wcmd "page" [`i l.pageno; `i l.pagedimno];
1394 state.currently <- Loading (l, state.gen);
1395 | Some opaque ->
1396 tilepage l.pageno opaque pages;
1397 loop rest
1398 end;
1399 | _ -> ()
1401 if state.invalidated = 0 then loop pages
1404 let preload pages =
1405 load pages;
1406 if conf.preload && state.currently = Idle
1407 then load (preloadlayout pages);
1410 let layoutready layout =
1411 let rec fold all ls =
1412 all && match ls with
1413 | l :: rest ->
1414 let seen = ref false in
1415 let allvisible = ref true in
1416 let foo col row _ _ _ _ _ _ =
1417 seen := true;
1418 allvisible := !allvisible &&
1419 begin match gettileopaque l col row with
1420 | Some _ -> true
1421 | None -> false
1424 itertiles l foo;
1425 fold (!seen && !allvisible) rest
1426 | [] -> true
1428 let alltilesvisible = fold true layout in
1429 alltilesvisible;
1432 let gotoy y =
1433 let y = bound y 0 state.maxy in
1434 let y, layout, proceed =
1435 match conf.maxwait with
1436 | Some time when state.ghyll == noghyll ->
1437 begin match state.throttle with
1438 | None ->
1439 let layout = layout y conf.winh in
1440 let ready = layoutready layout in
1441 if not ready
1442 then (
1443 load layout;
1444 state.throttle <- Some (layout, y, now ());
1446 else G.postRedisplay "gotoy showall (None)";
1447 y, layout, ready
1448 | Some (_, _, started) ->
1449 let dt = now () -. started in
1450 if dt > time
1451 then (
1452 state.throttle <- None;
1453 let layout = layout y conf.winh in
1454 load layout;
1455 G.postRedisplay "maxwait";
1456 y, layout, true
1458 else -1, [], false
1461 | _ ->
1462 let layout = layout y conf.winh in
1463 if true || layoutready layout
1464 then G.postRedisplay "gotoy ready";
1465 y, layout, true
1467 if proceed
1468 then (
1469 state.y <- y;
1470 state.layout <- layout;
1471 begin match state.mode with
1472 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1473 if not (pagevisible layout pageno)
1474 then (
1475 match state.layout with
1476 | [] -> ()
1477 | l :: _ ->
1478 state.mode <- Birdseye (
1479 conf, leftx, l.pageno, hooverpageno, anchor
1482 | _ -> ()
1483 end;
1484 preload layout;
1486 state.ghyll <- noghyll;
1489 let conttiling pageno opaque =
1490 tilepage pageno opaque
1491 (if conf.preload then preloadlayout state.layout else state.layout)
1494 let gotoy_and_clear_text y =
1495 gotoy y;
1496 if not conf.verbose then state.text <- "";
1499 let getanchor () =
1500 match state.layout with
1501 | [] -> emptyanchor
1502 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1505 let getanchory (n, top) =
1506 let y, h = getpageyh n in
1507 y + (truncate (top *. float h));
1510 let gotoanchor anchor =
1511 gotoy (getanchory anchor);
1514 let addnav () =
1515 cbput state.hists.nav (getanchor ());
1518 let getnav dir =
1519 let anchor = cbgetc state.hists.nav dir in
1520 getanchory anchor;
1523 let gotoghyll y =
1524 let rec scroll f n a b =
1525 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1526 let snake f a b =
1527 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1528 if f < a
1529 then s (float f /. float a)
1530 else (
1531 if f > b
1532 then 1.0 -. s ((float (f-b) /. float (n-b)))
1533 else 1.0
1536 snake f a b
1537 and summa f n a b =
1538 (* courtesy:
1539 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1540 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1541 let iv1 = iv f in
1542 let ins = float a *. iv1
1543 and outs = float (n-b) *. iv1 in
1544 let ones = b - a in
1545 ins +. outs +. float ones
1547 let rec set (_N, _A, _B) y sy =
1548 let sum = summa 1.0 _N _A _B in
1549 let dy = float (y - sy) in
1550 state.ghyll <- (
1551 let rec gf n y1 o =
1552 if n >= _N
1553 then state.ghyll <- noghyll
1554 else
1555 let go n =
1556 let s = scroll n _N _A _B in
1557 let y1 = y1 +. ((s *. dy) /. sum) in
1558 gotoy_and_clear_text (truncate y1);
1559 state.ghyll <- gf (n+1) y1;
1561 match o with
1562 | None -> go n
1563 | Some y' -> set (_N/2, 0, 0) y' state.y
1565 gf 0 (float state.y)
1568 match conf.ghyllscroll with
1569 | None ->
1570 gotoy_and_clear_text y
1571 | Some nab ->
1572 if state.ghyll == noghyll
1573 then set nab y state.y
1574 else state.ghyll (Some y)
1577 let gotopage n top =
1578 let y, h = getpageyh n in
1579 let y = y + (truncate (top *. float h)) in
1580 gotoghyll y
1583 let gotopage1 n top =
1584 let y = getpagey n in
1585 let y = y + top in
1586 gotoghyll y
1589 let invalidate () =
1590 state.layout <- [];
1591 state.pdims <- [];
1592 state.rects <- [];
1593 state.rects1 <- [];
1594 state.invalidated <- state.invalidated + 1;
1597 let writeopen path password =
1598 writecmd state.sw ("open " ^ path ^ "\000" ^ password ^ "\000");
1601 let opendoc path password =
1602 invalidate ();
1603 state.path <- path;
1604 state.password <- password;
1605 state.gen <- state.gen + 1;
1606 state.docinfo <- [];
1608 setaalevel conf.aalevel;
1609 writeopen path password;
1610 Wsi.settitle ("llpp " ^ Filename.basename path);
1611 wcmd "geometry" [`i state.w; `i conf.winh];
1614 let scalecolor c =
1615 let c = c *. conf.colorscale in
1616 (c, c, c);
1619 let scalecolor2 (r, g, b) =
1620 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1623 let represent () =
1624 let docolumns = function
1625 | None -> ()
1626 | Some ((columns, coverA, coverB), _) ->
1627 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1628 let rec loop pageno pdimno pdim x y rowh pdims =
1629 if pageno = state.pagecount
1630 then ()
1631 else
1632 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1633 match pdims with
1634 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1635 pdimno+1, pdim, rest
1636 | _ ->
1637 pdimno, pdim, pdims
1639 let x, y, rowh' =
1640 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1641 then (
1642 (conf.winw - state.scrollw - w) / 2,
1643 y + rowh + conf.interpagespace, h
1645 else (
1646 if (pageno - coverA) mod columns = 0
1647 then 0, y + rowh + conf.interpagespace, h
1648 else x, y, max rowh h
1651 let rec fixrow m = if m = pageno then () else
1652 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1653 if h < rowh
1654 then (
1655 let y = y + (rowh - h) / 2 in
1656 a.(m) <- (pdimno, x, y, pdim);
1658 fixrow (m+1)
1660 if pageno > 1 && (pageno - coverA) mod columns = 0
1661 then fixrow (pageno - columns);
1662 a.(pageno) <- (pdimno, x, y, pdim);
1663 let x = x + w + xoff*2 + conf.interpagespace in
1664 loop (pageno+1) pdimno pdim x y rowh' pdims
1666 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1667 conf.columns <- Some ((columns, coverA, coverB), a);
1669 docolumns conf.columns;
1670 state.maxy <- calcheight ();
1671 state.hscrollh <-
1672 if state.w <= conf.winw - state.scrollw
1673 then 0
1674 else state.scrollw
1676 match state.mode with
1677 | Birdseye (_, _, pageno, _, _) ->
1678 let y, h = getpageyh pageno in
1679 let top = (conf.winh - h) / 2 in
1680 gotoy (max 0 (y - top))
1681 | _ -> gotoanchor state.anchor
1684 let reshape =
1685 let firsttime = ref true in
1686 fun ~w ~h ->
1687 GlDraw.viewport 0 0 w h;
1688 if state.invalidated = 0 && not !firsttime
1689 then state.anchor <- getanchor ();
1691 firsttime := false;
1692 conf.winw <- w;
1693 let w = truncate (float w *. conf.zoom) - state.scrollw in
1694 let w = max w 2 in
1695 state.w <- w;
1696 conf.winh <- h;
1697 setfontsize fstate.fontsize;
1698 GlMat.mode `modelview;
1699 GlMat.load_identity ();
1701 GlMat.mode `projection;
1702 GlMat.load_identity ();
1703 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1704 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1705 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1707 let w =
1708 match conf.columns with
1709 | None -> w
1710 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1712 invalidate ();
1713 wcmd "geometry" [`i w; `i h];
1716 let enttext () =
1717 let len = String.length state.text in
1718 let drawstring s =
1719 let hscrollh =
1720 match state.mode with
1721 | View -> state.hscrollh
1722 | _ -> 0
1724 let rect x w =
1725 GlDraw.rect
1726 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1727 (x+.w, float (conf.winh - hscrollh))
1730 let w = float (conf.winw - state.scrollw - 1) in
1731 if state.progress >= 0.0 && state.progress < 1.0
1732 then (
1733 GlDraw.color (0.3, 0.3, 0.3);
1734 let w1 = w *. state.progress in
1735 rect 0.0 w1;
1736 GlDraw.color (0.0, 0.0, 0.0);
1737 rect w1 (w-.w1)
1739 else (
1740 GlDraw.color (0.0, 0.0, 0.0);
1741 rect 0.0 w;
1744 GlDraw.color (1.0, 1.0, 1.0);
1745 drawstring fstate.fontsize
1746 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1748 let s =
1749 match state.mode with
1750 | Textentry ((prefix, text, _, _, _), _) ->
1751 let s =
1752 if len > 0
1753 then
1754 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1755 else
1756 Printf.sprintf "%s%s_" prefix text
1760 | _ -> state.text
1762 let s =
1763 if state.newerrmsgs
1764 then (
1765 if not (istextentry state.mode)
1766 then
1767 let s1 = "(press 'e' to review error messasges)" in
1768 if String.length s > 0 then s ^ " " ^ s1 else s1
1769 else s
1771 else s
1773 if String.length s > 0
1774 then drawstring s
1777 let gctiles () =
1778 let len = Queue.length state.tilelru in
1779 let rec loop qpos =
1780 if state.memused <= conf.memlimit
1781 then ()
1782 else (
1783 if qpos < len
1784 then
1785 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1786 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1787 let (_, pw, ph, _) = getpagedim n in
1789 gen = state.gen
1790 && colorspace = conf.colorspace
1791 && angle = conf.angle
1792 && pagew = pw
1793 && pageh = ph
1794 && (
1795 let layout =
1796 match state.throttle with
1797 | None ->
1798 if conf.preload
1799 then preloadlayout state.layout
1800 else state.layout
1801 | Some (layout, _, _) ->
1802 layout
1804 let x = col*conf.tilew
1805 and y = row*conf.tileh in
1806 tilevisible layout n x y
1808 then Queue.push lruitem state.tilelru
1809 else (
1810 wcmd "freetile" [`s p];
1811 state.memused <- state.memused - s;
1812 state.uioh#infochanged Memused;
1813 Hashtbl.remove state.tilemap k;
1815 loop (qpos+1)
1818 loop 0
1821 let flushtiles () =
1822 Queue.iter (fun (k, p, s) ->
1823 wcmd "freetile" [`s p];
1824 state.memused <- state.memused - s;
1825 state.uioh#infochanged Memused;
1826 Hashtbl.remove state.tilemap k;
1827 ) state.tilelru;
1828 Queue.clear state.tilelru;
1829 load state.layout;
1832 let logcurrently = function
1833 | Idle -> dolog "Idle"
1834 | Loading (l, gen) ->
1835 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1836 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1837 dolog
1838 "Tiling %d[%d,%d] page=%s cs=%s angle"
1839 l.pageno col row pageopaque
1840 (colorspace_to_string colorspace)
1842 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1843 angle gen conf.angle state.gen
1844 tilew tileh
1845 conf.tilew conf.tileh
1847 | Outlining _ ->
1848 dolog "outlining"
1851 let act cmds =
1852 (* dolog "%S" cmds; *)
1853 let op, args =
1854 let spacepos =
1855 try String.index cmds ' '
1856 with Not_found -> -1
1858 if spacepos = -1
1859 then cmds, ""
1860 else
1861 let l = String.length cmds in
1862 let op = String.sub cmds 0 spacepos in
1863 op, begin
1864 if l - spacepos < 2 then ""
1865 else String.sub cmds (spacepos+1) (l-spacepos-1)
1868 match op with
1869 | "clear" ->
1870 state.uioh#infochanged Pdim;
1871 state.pdims <- [];
1873 | "clearrects" ->
1874 state.rects <- state.rects1;
1875 G.postRedisplay "clearrects";
1877 | "continue" ->
1878 let n =
1879 try Scanf.sscanf args "%u" (fun n -> n)
1880 with exn ->
1881 dolog "error processing 'continue' %S: %s"
1882 cmds (Printexc.to_string exn);
1883 exit 1;
1885 state.pagecount <- n;
1886 state.invalidated <- state.invalidated - 1;
1887 begin match state.currently with
1888 | Outlining l ->
1889 state.currently <- Idle;
1890 state.outlines <- Array.of_list (List.rev l)
1891 | _ -> ()
1892 end;
1893 if state.invalidated = 0
1894 then represent ();
1895 if conf.maxwait = None
1896 then G.postRedisplay "continue";
1898 | "title" ->
1899 Wsi.settitle args
1901 | "msg" ->
1902 showtext ' ' args
1904 | "vmsg" ->
1905 if conf.verbose
1906 then showtext ' ' args
1908 | "progress" ->
1909 let progress, text =
1911 Scanf.sscanf args "%f %n"
1912 (fun f pos ->
1913 f, String.sub args pos (String.length args - pos))
1914 with exn ->
1915 dolog "error processing 'progress' %S: %s"
1916 cmds (Printexc.to_string exn);
1917 exit 1;
1919 state.text <- text;
1920 state.progress <- progress;
1921 G.postRedisplay "progress"
1923 | "firstmatch" ->
1924 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1926 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1927 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1928 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1929 with exn ->
1930 dolog "error processing 'firstmatch' %S: %s"
1931 cmds (Printexc.to_string exn);
1932 exit 1;
1934 let y = (getpagey pageno) + truncate y0 in
1935 addnav ();
1936 gotoy y;
1937 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1939 | "match" ->
1940 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1942 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1943 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1944 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1945 with exn ->
1946 dolog "error processing 'match' %S: %s"
1947 cmds (Printexc.to_string exn);
1948 exit 1;
1950 state.rects1 <-
1951 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1953 | "page" ->
1954 let pageopaque, t =
1956 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1957 with exn ->
1958 dolog "error processing 'page' %S: %s"
1959 cmds (Printexc.to_string exn);
1960 exit 1;
1962 begin match state.currently with
1963 | Loading (l, gen) ->
1964 vlog "page %d took %f sec" l.pageno t;
1965 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1966 begin match state.throttle with
1967 | None ->
1968 let preloadedpages =
1969 if conf.preload
1970 then preloadlayout state.layout
1971 else state.layout
1973 let evict () =
1974 let module IntSet =
1975 Set.Make (struct type t = int let compare = (-) end) in
1976 let set =
1977 List.fold_left (fun s l -> IntSet.add l.pageno s)
1978 IntSet.empty preloadedpages
1980 let evictedpages =
1981 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1982 if not (IntSet.mem pageno set)
1983 then (
1984 wcmd "freepage" [`s opaque];
1985 key :: accu
1987 else accu
1988 ) state.pagemap []
1990 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1992 evict ();
1993 state.currently <- Idle;
1994 if gen = state.gen
1995 then (
1996 tilepage l.pageno pageopaque state.layout;
1997 load state.layout;
1998 load preloadedpages;
1999 if pagevisible state.layout l.pageno
2000 && layoutready state.layout
2001 then G.postRedisplay "page";
2004 | Some (layout, _, _) ->
2005 state.currently <- Idle;
2006 tilepage l.pageno pageopaque layout;
2007 load state.layout
2008 end;
2010 | _ ->
2011 dolog "Inconsistent loading state";
2012 logcurrently state.currently;
2013 exit 1
2016 | "tile" ->
2017 let (x, y, opaque, size, t) =
2019 Scanf.sscanf args "%u %u %s %u %f"
2020 (fun x y p size t -> (x, y, p, size, t))
2021 with exn ->
2022 dolog "error processing 'tile' %S: %s"
2023 cmds (Printexc.to_string exn);
2024 exit 1;
2026 begin match state.currently with
2027 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2028 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2030 if tilew != conf.tilew || tileh != conf.tileh
2031 then (
2032 wcmd "freetile" [`s opaque];
2033 state.currently <- Idle;
2034 load state.layout;
2036 else (
2037 puttileopaque l col row gen cs angle opaque size t;
2038 state.memused <- state.memused + size;
2039 state.uioh#infochanged Memused;
2040 gctiles ();
2041 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2042 opaque, size) state.tilelru;
2044 let layout =
2045 match state.throttle with
2046 | None -> state.layout
2047 | Some (layout, _, _) -> layout
2050 state.currently <- Idle;
2051 if gen = state.gen
2052 && conf.colorspace = cs
2053 && conf.angle = angle
2054 && tilevisible layout l.pageno x y
2055 then conttiling l.pageno pageopaque;
2057 begin match state.throttle with
2058 | None ->
2059 preload state.layout;
2060 if gen = state.gen
2061 && conf.colorspace = cs
2062 && conf.angle = angle
2063 && tilevisible state.layout l.pageno x y
2064 then G.postRedisplay "tile nothrottle";
2066 | Some (layout, y, _) ->
2067 let ready = layoutready layout in
2068 if ready
2069 then (
2070 state.y <- y;
2071 state.layout <- layout;
2072 state.throttle <- None;
2073 G.postRedisplay "throttle";
2075 else load layout;
2076 end;
2079 | _ ->
2080 dolog "Inconsistent tiling state";
2081 logcurrently state.currently;
2082 exit 1
2085 | "pdim" ->
2086 let pdim =
2088 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2089 with exn ->
2090 dolog "error processing 'pdim' %S: %s"
2091 cmds (Printexc.to_string exn);
2092 exit 1;
2094 state.uioh#infochanged Pdim;
2095 state.pdims <- pdim :: state.pdims
2097 | "o" ->
2098 let (l, n, t, h, pos) =
2100 Scanf.sscanf args "%u %u %d %u %n"
2101 (fun l n t h pos -> l, n, t, h, pos)
2102 with exn ->
2103 dolog "error processing 'o' %S: %s"
2104 cmds (Printexc.to_string exn);
2105 exit 1;
2107 let s = String.sub args pos (String.length args - pos) in
2108 let outline = (s, l, (n, float t /. float h)) in
2109 begin match state.currently with
2110 | Outlining outlines ->
2111 state.currently <- Outlining (outline :: outlines)
2112 | Idle ->
2113 state.currently <- Outlining [outline]
2114 | currently ->
2115 dolog "invalid outlining state";
2116 logcurrently currently
2119 | "info" ->
2120 state.docinfo <- (1, args) :: state.docinfo
2122 | "infoend" ->
2123 state.uioh#infochanged Docinfo;
2124 state.docinfo <- List.rev state.docinfo
2126 | _ ->
2127 dolog "unknown cmd `%S'" cmds
2130 let onhist cb =
2131 let rc = cb.rc in
2132 let action = function
2133 | HCprev -> cbget cb ~-1
2134 | HCnext -> cbget cb 1
2135 | HCfirst -> cbget cb ~-(cb.rc)
2136 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2137 and cancel () = cb.rc <- rc
2138 in (action, cancel)
2141 let search pattern forward =
2142 if String.length pattern > 0
2143 then
2144 let pn, py =
2145 match state.layout with
2146 | [] -> 0, 0
2147 | l :: _ ->
2148 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2150 let cmd =
2151 let b = makecmd "search"
2152 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
2154 Buffer.add_char b ',';
2155 Buffer.add_string b pattern;
2156 Buffer.add_char b '\000';
2157 Buffer.contents b;
2159 writecmd state.sw cmd;
2162 let intentry text key =
2163 let c =
2164 if key >= 32 && key < 127
2165 then Char.chr key
2166 else '\000'
2168 match c with
2169 | '0' .. '9' ->
2170 let text = addchar text c in
2171 TEcont text
2173 | _ ->
2174 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2175 TEcont text
2178 let textentry text key =
2179 if key land 0xff00 = 0xff00
2180 then TEcont text
2181 else TEcont (text ^ Wsi.toutf8 key)
2184 let reqlayout angle proportional =
2185 match state.throttle with
2186 | None ->
2187 if state.invalidated = 0 then state.anchor <- getanchor ();
2188 conf.angle <- angle mod 360;
2189 conf.proportional <- proportional;
2190 invalidate ();
2191 wcmd "reqlayout" [`i conf.angle; `b proportional];
2192 | _ -> ()
2195 let settrim trimmargins trimfuzz =
2196 if state.invalidated = 0 then state.anchor <- getanchor ();
2197 conf.trimmargins <- trimmargins;
2198 conf.trimfuzz <- trimfuzz;
2199 let x0, y0, x1, y1 = trimfuzz in
2200 invalidate ();
2201 wcmd "settrim" [
2202 `b conf.trimmargins;
2203 `i x0;
2204 `i y0;
2205 `i x1;
2206 `i y1;
2208 Hashtbl.iter (fun _ opaque ->
2209 wcmd "freepage" [`s opaque];
2210 ) state.pagemap;
2211 Hashtbl.clear state.pagemap;
2214 let setzoom zoom =
2215 match state.throttle with
2216 | None ->
2217 let zoom = max 0.01 zoom in
2218 if zoom <> conf.zoom
2219 then (
2220 state.prevzoom <- conf.zoom;
2221 let relx =
2222 if zoom <= 1.0
2223 then (state.x <- 0; 0.0)
2224 else float state.x /. float state.w
2226 conf.zoom <- zoom;
2227 reshape conf.winw conf.winh;
2228 if zoom > 1.0
2229 then (
2230 let x = relx *. float state.w in
2231 state.x <- truncate x;
2233 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2236 | Some (layout, y, started) ->
2237 let time =
2238 match conf.maxwait with
2239 | None -> 0.0
2240 | Some t -> t
2242 let dt = now () -. started in
2243 if dt > time
2244 then (
2245 state.y <- y;
2246 load layout;
2250 let setcolumns columns coverA coverB =
2251 if columns < 2
2252 then (
2253 conf.columns <- None;
2254 state.x <- 0;
2255 setzoom 1.0;
2257 else (
2258 conf.columns <- Some ((columns, coverA, coverB), [||]);
2259 conf.zoom <- 1.0;
2261 reshape conf.winw conf.winh;
2264 let enterbirdseye () =
2265 let zoom = float conf.thumbw /. float conf.winw in
2266 let birdseyepageno =
2267 let cy = conf.winh / 2 in
2268 let fold = function
2269 | [] -> 0
2270 | l :: rest ->
2271 let rec fold best = function
2272 | [] -> best.pageno
2273 | l :: rest ->
2274 let d = cy - (l.pagedispy + l.pagevh/2)
2275 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2276 if abs d < abs dbest
2277 then fold l rest
2278 else best.pageno
2279 in fold l rest
2281 fold state.layout
2283 state.mode <- Birdseye (
2284 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2286 conf.zoom <- zoom;
2287 conf.presentation <- false;
2288 conf.interpagespace <- 10;
2289 conf.hlinks <- false;
2290 state.x <- 0;
2291 state.mstate <- Mnone;
2292 conf.maxwait <- None;
2293 conf.columns <- (
2294 match conf.beyecolumns with
2295 | Some c ->
2296 conf.zoom <- 1.0;
2297 Some ((c, 0, 0), [||])
2298 | None -> None
2300 Wsi.setcursor Wsi.CURSOR_INHERIT;
2301 if conf.verbose
2302 then
2303 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2304 (100.0*.zoom)
2305 else
2306 state.text <- ""
2308 reshape conf.winw conf.winh;
2311 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2312 state.mode <- View;
2313 conf.zoom <- c.zoom;
2314 conf.presentation <- c.presentation;
2315 conf.interpagespace <- c.interpagespace;
2316 conf.maxwait <- c.maxwait;
2317 conf.hlinks <- c.hlinks;
2318 conf.beyecolumns <- (
2319 match conf.columns with
2320 | Some ((c, _, _), _) -> Some c
2321 | None -> None
2323 conf.columns <- (
2324 match c.columns with
2325 | Some (c, _) -> Some (c, [||])
2326 | None -> None
2328 state.x <- leftx;
2329 if conf.verbose
2330 then
2331 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2332 (100.0*.conf.zoom)
2334 reshape conf.winw conf.winh;
2335 state.anchor <- if goback then anchor else (pageno, 0.0);
2338 let togglebirdseye () =
2339 match state.mode with
2340 | Birdseye vals -> leavebirdseye vals true
2341 | View -> enterbirdseye ()
2342 | _ -> ()
2345 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2346 let pageno = max 0 (pageno - incr) in
2347 let rec loop = function
2348 | [] -> gotopage1 pageno 0
2349 | l :: _ when l.pageno = pageno ->
2350 if l.pagedispy >= 0 && l.pagey = 0
2351 then G.postRedisplay "upbirdseye"
2352 else gotopage1 pageno 0
2353 | _ :: rest -> loop rest
2355 loop state.layout;
2356 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2359 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2360 let pageno = min (state.pagecount - 1) (pageno + incr) in
2361 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2362 let rec loop = function
2363 | [] ->
2364 let y, h = getpageyh pageno in
2365 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2366 gotoy (clamp dy)
2367 | l :: _ when l.pageno = pageno ->
2368 if l.pagevh != l.pageh
2369 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2370 else G.postRedisplay "downbirdseye"
2371 | _ :: rest -> loop rest
2373 loop state.layout
2376 let optentry mode _ key =
2377 let btos b = if b then "on" else "off" in
2378 if key >= 32 && key < 127
2379 then
2380 let c = Char.chr key in
2381 match c with
2382 | 's' ->
2383 let ondone s =
2384 try conf.scrollstep <- int_of_string s with exc ->
2385 state.text <- Printf.sprintf "bad integer `%s': %s"
2386 s (Printexc.to_string exc)
2388 TEswitch ("scroll step: ", "", None, intentry, ondone)
2390 | 'A' ->
2391 let ondone s =
2393 conf.autoscrollstep <- int_of_string s;
2394 if state.autoscroll <> None
2395 then state.autoscroll <- Some conf.autoscrollstep
2396 with exc ->
2397 state.text <- Printf.sprintf "bad integer `%s': %s"
2398 s (Printexc.to_string exc)
2400 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2402 | 'C' ->
2403 let ondone s =
2405 let n, a, b = columns_of_string s in
2406 setcolumns n a b;
2407 with exc ->
2408 state.text <- Printf.sprintf "bad columns `%s': %s"
2409 s (Printexc.to_string exc)
2411 TEswitch ("columns: ", "", None, textentry, ondone)
2413 | 'Z' ->
2414 let ondone s =
2416 let zoom = float (int_of_string s) /. 100.0 in
2417 setzoom zoom
2418 with exc ->
2419 state.text <- Printf.sprintf "bad integer `%s': %s"
2420 s (Printexc.to_string exc)
2422 TEswitch ("zoom: ", "", None, intentry, ondone)
2424 | 't' ->
2425 let ondone s =
2427 conf.thumbw <- bound (int_of_string s) 2 4096;
2428 state.text <-
2429 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2430 begin match mode with
2431 | Birdseye beye ->
2432 leavebirdseye beye false;
2433 enterbirdseye ();
2434 | _ -> ();
2436 with exc ->
2437 state.text <- Printf.sprintf "bad integer `%s': %s"
2438 s (Printexc.to_string exc)
2440 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2442 | 'R' ->
2443 let ondone s =
2444 match try
2445 Some (int_of_string s)
2446 with exc ->
2447 state.text <- Printf.sprintf "bad integer `%s': %s"
2448 s (Printexc.to_string exc);
2449 None
2450 with
2451 | Some angle -> reqlayout angle conf.proportional
2452 | None -> ()
2454 TEswitch ("rotation: ", "", None, intentry, ondone)
2456 | 'i' ->
2457 conf.icase <- not conf.icase;
2458 TEdone ("case insensitive search " ^ (btos conf.icase))
2460 | 'p' ->
2461 conf.preload <- not conf.preload;
2462 gotoy state.y;
2463 TEdone ("preload " ^ (btos conf.preload))
2465 | 'v' ->
2466 conf.verbose <- not conf.verbose;
2467 TEdone ("verbose " ^ (btos conf.verbose))
2469 | 'd' ->
2470 conf.debug <- not conf.debug;
2471 TEdone ("debug " ^ (btos conf.debug))
2473 | 'h' ->
2474 conf.maxhfit <- not conf.maxhfit;
2475 state.maxy <-
2476 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2477 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2479 | 'c' ->
2480 conf.crophack <- not conf.crophack;
2481 TEdone ("crophack " ^ btos conf.crophack)
2483 | 'a' ->
2484 let s =
2485 match conf.maxwait with
2486 | None ->
2487 conf.maxwait <- Some infinity;
2488 "always wait for page to complete"
2489 | Some _ ->
2490 conf.maxwait <- None;
2491 "show placeholder if page is not ready"
2493 TEdone s
2495 | 'f' ->
2496 conf.underinfo <- not conf.underinfo;
2497 TEdone ("underinfo " ^ btos conf.underinfo)
2499 | 'P' ->
2500 conf.savebmarks <- not conf.savebmarks;
2501 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2503 | 'S' ->
2504 let ondone s =
2506 let pageno, py =
2507 match state.layout with
2508 | [] -> 0, 0
2509 | l :: _ ->
2510 l.pageno, l.pagey
2512 conf.interpagespace <- int_of_string s;
2513 state.maxy <- calcheight ();
2514 let y = getpagey pageno in
2515 gotoy (y + py)
2516 with exc ->
2517 state.text <- Printf.sprintf "bad integer `%s': %s"
2518 s (Printexc.to_string exc)
2520 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2522 | 'l' ->
2523 reqlayout conf.angle (not conf.proportional);
2524 TEdone ("proportional display " ^ btos conf.proportional)
2526 | 'T' ->
2527 settrim (not conf.trimmargins) conf.trimfuzz;
2528 TEdone ("trim margins " ^ btos conf.trimmargins)
2530 | 'I' ->
2531 conf.invert <- not conf.invert;
2532 TEdone ("invert colors " ^ btos conf.invert)
2534 | 'x' ->
2535 let ondone s =
2536 cbput state.hists.sel s;
2537 conf.selcmd <- s;
2539 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2540 textentry, ondone)
2542 | _ ->
2543 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2544 TEstop
2545 else
2546 TEcont state.text
2549 class type lvsource = object
2550 method getitemcount : int
2551 method getitem : int -> (string * int)
2552 method hasaction : int -> bool
2553 method exit :
2554 uioh:uioh ->
2555 cancel:bool ->
2556 active:int ->
2557 first:int ->
2558 pan:int ->
2559 qsearch:string ->
2560 uioh option
2561 method getactive : int
2562 method getfirst : int
2563 method getqsearch : string
2564 method setqsearch : string -> unit
2565 method getpan : int
2566 end;;
2568 class virtual lvsourcebase = object
2569 val mutable m_active = 0
2570 val mutable m_first = 0
2571 val mutable m_qsearch = ""
2572 val mutable m_pan = 0
2573 method getactive = m_active
2574 method getfirst = m_first
2575 method getqsearch = m_qsearch
2576 method getpan = m_pan
2577 method setqsearch s = m_qsearch <- s
2578 end;;
2580 let withoutlastutf8 s =
2581 let len = String.length s in
2582 if len = 0
2583 then s
2584 else
2585 let rec find pos =
2586 if pos = 0
2587 then pos
2588 else
2589 let b = Char.code s.[pos] in
2590 if b land 0b110000 = 0b11000000
2591 then find (pos-1)
2592 else pos-1
2594 let first =
2595 if Char.code s.[len-1] land 0x80 = 0
2596 then len-1
2597 else find (len-1)
2599 String.sub s 0 first;
2602 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2603 let enttext te =
2604 state.mode <- Textentry (te, onleave);
2605 state.text <- "";
2606 enttext ();
2607 G.postRedisplay "textentrykeyboard enttext";
2609 let histaction cmd =
2610 match opthist with
2611 | None -> ()
2612 | Some (action, _) ->
2613 state.mode <- Textentry (
2614 (c, action cmd, opthist, onkey, ondone), onleave
2616 G.postRedisplay "textentry histaction"
2618 match key with
2619 | 0xff08 -> (* backspace *)
2620 let s = withoutlastutf8 text in
2621 let len = String.length s in
2622 if len = 0
2623 then (
2624 onleave Cancel;
2625 G.postRedisplay "textentrykeyboard after cancel";
2627 else (
2628 enttext (c, s, opthist, onkey, ondone)
2631 | 0xff0d ->
2632 ondone text;
2633 onleave Confirm;
2634 G.postRedisplay "textentrykeyboard after confirm"
2636 | 0xff52 -> histaction HCprev
2637 | 0xff54 -> histaction HCnext
2638 | 0xff50 -> histaction HCfirst
2639 | 0xff57 -> histaction HClast
2641 | 0xff1b -> (* escape*)
2642 if String.length text = 0
2643 then (
2644 begin match opthist with
2645 | None -> ()
2646 | Some (_, onhistcancel) -> onhistcancel ()
2647 end;
2648 onleave Cancel;
2649 state.text <- "";
2650 G.postRedisplay "textentrykeyboard after cancel2"
2652 else (
2653 enttext (c, "", opthist, onkey, ondone)
2656 | 0xff9f | 0xffff -> () (* delete *)
2658 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2659 begin match onkey text key with
2660 | TEdone text ->
2661 ondone text;
2662 onleave Confirm;
2663 G.postRedisplay "textentrykeyboard after confirm2";
2665 | TEcont text ->
2666 enttext (c, text, opthist, onkey, ondone);
2668 | TEstop ->
2669 onleave Cancel;
2670 G.postRedisplay "textentrykeyboard after cancel3"
2672 | TEswitch te ->
2673 state.mode <- Textentry (te, onleave);
2674 G.postRedisplay "textentrykeyboard switch";
2675 end;
2677 | _ ->
2678 vlog "unhandled key %#x" key
2681 let firstof first active =
2682 if first > active || abs (first - active) > fstate.maxrows - 1
2683 then max 0 (active - (fstate.maxrows/2))
2684 else first
2687 let calcfirst first active =
2688 if active > first
2689 then
2690 let rows = active - first in
2691 if rows > fstate.maxrows then active - fstate.maxrows else first
2692 else active
2695 let scrollph y maxy =
2696 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2697 let sh = float conf.winh /. sh in
2698 let sh = max sh (float conf.scrollh) in
2700 let percent =
2701 if y = state.maxy
2702 then 1.0
2703 else float y /. float maxy
2705 let position = (float conf.winh -. sh) *. percent in
2707 let position =
2708 if position +. sh > float conf.winh
2709 then float conf.winh -. sh
2710 else position
2712 position, sh;
2715 let coe s = (s :> uioh);;
2717 class listview ~(source:lvsource) ~trusted ~modehash =
2718 object (self)
2719 val m_pan = source#getpan
2720 val m_first = source#getfirst
2721 val m_active = source#getactive
2722 val m_qsearch = source#getqsearch
2723 val m_prev_uioh = state.uioh
2725 method private elemunder y =
2726 let n = y / (fstate.fontsize+1) in
2727 if m_first + n < source#getitemcount
2728 then (
2729 if source#hasaction (m_first + n)
2730 then Some (m_first + n)
2731 else None
2733 else None
2735 method display =
2736 Gl.enable `blend;
2737 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2738 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2739 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2740 GlDraw.color (1., 1., 1.);
2741 Gl.enable `texture_2d;
2742 let fs = fstate.fontsize in
2743 let nfs = fs + 1 in
2744 let ww = fstate.wwidth in
2745 let tabw = 30.0*.ww in
2746 let itemcount = source#getitemcount in
2747 let rec loop row =
2748 if (row - m_first) * nfs > conf.winh
2749 then ()
2750 else (
2751 if row >= 0 && row < itemcount
2752 then (
2753 let (s, level) = source#getitem row in
2754 let y = (row - m_first) * nfs in
2755 let x = 5.0 +. float (level + m_pan) *. ww in
2756 if row = m_active
2757 then (
2758 Gl.disable `texture_2d;
2759 GlDraw.polygon_mode `both `line;
2760 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2761 GlDraw.rect (1., float (y + 1))
2762 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2763 GlDraw.polygon_mode `both `fill;
2764 GlDraw.color (1., 1., 1.);
2765 Gl.enable `texture_2d;
2768 let drawtabularstring s =
2769 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2770 if trusted
2771 then
2772 let tabpos = try String.index s '\t' with Not_found -> -1 in
2773 if tabpos > 0
2774 then
2775 let len = String.length s - tabpos - 1 in
2776 let s1 = String.sub s 0 tabpos
2777 and s2 = String.sub s (tabpos + 1) len in
2778 let nx = drawstr x s1 in
2779 let sw = nx -. x in
2780 let x = x +. (max tabw sw) in
2781 drawstr x s2
2782 else
2783 drawstr x s
2784 else
2785 drawstr x s
2787 let _ = drawtabularstring s in
2788 loop (row+1)
2792 loop m_first;
2793 Gl.disable `blend;
2794 Gl.disable `texture_2d;
2796 method updownlevel incr =
2797 let len = source#getitemcount in
2798 let curlevel =
2799 if m_active >= 0 && m_active < len
2800 then snd (source#getitem m_active)
2801 else -1
2803 let rec flow i =
2804 if i = len then i-1 else if i = -1 then 0 else
2805 let _, l = source#getitem i in
2806 if l != curlevel then i else flow (i+incr)
2808 let active = flow m_active in
2809 let first = calcfirst m_first active in
2810 G.postRedisplay "outline updownlevel";
2811 {< m_active = active; m_first = first >}
2813 method private key1 key mask =
2814 let set1 active first qsearch =
2815 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2817 let search active pattern incr =
2818 let dosearch re =
2819 let rec loop n =
2820 if n >= 0 && n < source#getitemcount
2821 then (
2822 let s, _ = source#getitem n in
2824 (try ignore (Str.search_forward re s 0); true
2825 with Not_found -> false)
2826 then Some n
2827 else loop (n + incr)
2829 else None
2831 loop active
2834 let re = Str.regexp_case_fold pattern in
2835 dosearch re
2836 with Failure s ->
2837 state.text <- s;
2838 None
2840 let itemcount = source#getitemcount in
2841 let find start incr =
2842 let rec find i =
2843 if i = -1 || i = itemcount
2844 then -1
2845 else (
2846 if source#hasaction i
2847 then i
2848 else find (i + incr)
2851 find start
2853 let set active first =
2854 let first = bound first 0 (itemcount - fstate.maxrows) in
2855 state.text <- "";
2856 coe {< m_active = active; m_first = first >}
2858 let navigate incr =
2859 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2860 let active, first =
2861 let incr1 = if incr > 0 then 1 else -1 in
2862 if isvisible m_first m_active
2863 then
2864 let next =
2865 let next = m_active + incr in
2866 let next =
2867 if next < 0 || next >= itemcount
2868 then -1
2869 else find next incr1
2871 if next = -1 || abs (m_active - next) > fstate.maxrows
2872 then -1
2873 else next
2875 if next = -1
2876 then
2877 let first = m_first + incr in
2878 let first = bound first 0 (itemcount - 1) in
2879 let next =
2880 let next = m_active + incr in
2881 let next = bound next 0 (itemcount - 1) in
2882 find next ~-incr1
2884 let active = if next = -1 then m_active else next in
2885 active, first
2886 else
2887 let first = min next m_first in
2888 let first =
2889 if abs (next - first) > fstate.maxrows
2890 then first + incr
2891 else first
2893 next, first
2894 else
2895 let first = m_first + incr in
2896 let first = bound first 0 (itemcount - 1) in
2897 let active =
2898 let next = m_active + incr in
2899 let next = bound next 0 (itemcount - 1) in
2900 let next = find next incr1 in
2901 let active =
2902 if next = -1 || abs (m_active - first) > fstate.maxrows
2903 then (
2904 let active = if m_active = -1 then next else m_active in
2905 active
2907 else next
2909 if isvisible first active
2910 then active
2911 else -1
2913 active, first
2915 G.postRedisplay "listview navigate";
2916 set active first;
2918 match key with
2919 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2920 let incr = if key = 0x72 then -1 else 1 in
2921 let active, first =
2922 match search (m_active + incr) m_qsearch incr with
2923 | None ->
2924 state.text <- m_qsearch ^ " [not found]";
2925 m_active, m_first
2926 | Some active ->
2927 state.text <- m_qsearch;
2928 active, firstof m_first active
2930 G.postRedisplay "listview ctrl-r/s";
2931 set1 active first m_qsearch;
2933 | 0xff08 -> (* backspace *)
2934 if String.length m_qsearch = 0
2935 then coe self
2936 else (
2937 let qsearch = withoutlastutf8 m_qsearch in
2938 let len = String.length qsearch in
2939 if len = 0
2940 then (
2941 state.text <- "";
2942 G.postRedisplay "listview empty qsearch";
2943 set1 m_active m_first "";
2945 else
2946 let active, first =
2947 match search m_active qsearch ~-1 with
2948 | None ->
2949 state.text <- qsearch ^ " [not found]";
2950 m_active, m_first
2951 | Some active ->
2952 state.text <- qsearch;
2953 active, firstof m_first active
2955 G.postRedisplay "listview backspace qsearch";
2956 set1 active first qsearch
2959 | key when ((key >= 32 && key < 127)
2960 || (key != 0 && key land 0xff00 != 0xff00)) ->
2961 let pattern =
2962 if key >= 32 && key < 127
2963 then addchar m_qsearch (Char.chr key)
2964 else m_qsearch ^ Wsi.toutf8 key
2966 let active, first =
2967 match search m_active pattern 1 with
2968 | None ->
2969 state.text <- pattern ^ " [not found]";
2970 m_active, m_first
2971 | Some active ->
2972 state.text <- pattern;
2973 active, firstof m_first active
2975 G.postRedisplay "listview qsearch add";
2976 set1 active first pattern;
2978 | 0xff1b -> (* escape *)
2979 state.text <- "";
2980 if String.length m_qsearch = 0
2981 then (
2982 G.postRedisplay "list view escape";
2983 begin
2984 match
2985 source#exit (coe self) true m_active m_first m_pan m_qsearch
2986 with
2987 | None -> m_prev_uioh
2988 | Some uioh -> uioh
2991 else (
2992 G.postRedisplay "list view kill qsearch";
2993 source#setqsearch "";
2994 coe {< m_qsearch = "" >}
2997 | 0xff0d -> (* return *)
2998 state.text <- "";
2999 let self = {< m_qsearch = "" >} in
3000 source#setqsearch "";
3001 let opt =
3002 G.postRedisplay "listview enter";
3003 if m_active >= 0 && m_active < source#getitemcount
3004 then (
3005 source#exit (coe self) false m_active m_first m_pan "";
3007 else (
3008 source#exit (coe self) true m_active m_first m_pan "";
3011 begin match opt with
3012 | None -> m_prev_uioh
3013 | Some uioh -> uioh
3016 | 0xff9f | 0xffff -> (* delete *)
3017 coe self
3019 | 0xff52 -> navigate ~-1 (* up *)
3020 | 0xff54 -> navigate 1 (* down *)
3021 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3022 | 0xff56 -> navigate fstate.maxrows (* next *)
3024 | 0xff53 -> (* right *)
3025 state.text <- "";
3026 G.postRedisplay "listview right";
3027 coe {< m_pan = m_pan - 1 >}
3029 | 0xff51 -> (* left *)
3030 state.text <- "";
3031 G.postRedisplay "listview left";
3032 coe {< m_pan = m_pan + 1 >}
3034 | 0xff50 -> (* home *)
3035 let active = find 0 1 in
3036 G.postRedisplay "listview home";
3037 set active 0;
3039 | 0xff57 -> (* end *)
3040 let first = max 0 (itemcount - fstate.maxrows) in
3041 let active = find (itemcount - 1) ~-1 in
3042 G.postRedisplay "listview end";
3043 set active first;
3045 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3046 coe self
3048 | _ ->
3049 dolog "listview unknown key %#x" key; coe self
3051 method key key mask =
3052 match state.mode with
3053 | Textentry te -> textentrykeyboard key mask te; coe self
3054 | _ -> self#key1 key mask
3056 method button button down x y _ =
3057 let opt =
3058 match button with
3059 | 1 when x > conf.winw - conf.scrollbw ->
3060 G.postRedisplay "listview scroll";
3061 if down
3062 then
3063 let _, position, sh = self#scrollph in
3064 if y > truncate position && y < truncate (position +. sh)
3065 then (
3066 state.mstate <- Mscrolly;
3067 Some (coe self)
3069 else
3070 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3071 let first = truncate (s *. float source#getitemcount) in
3072 let first = min source#getitemcount first in
3073 Some (coe {< m_first = first; m_active = first >})
3074 else (
3075 state.mstate <- Mnone;
3076 Some (coe self);
3078 | 1 when not down ->
3079 begin match self#elemunder y with
3080 | Some n ->
3081 G.postRedisplay "listview click";
3082 source#exit
3083 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3084 | _ ->
3085 Some (coe self)
3087 | n when (n == 4 || n == 5) && not down ->
3088 let len = source#getitemcount in
3089 let first =
3090 if n = 5 && m_first + fstate.maxrows >= len
3091 then
3092 m_first
3093 else
3094 let first = m_first + (if n == 4 then -1 else 1) in
3095 bound first 0 (len - 1)
3097 G.postRedisplay "listview wheel";
3098 Some (coe {< m_first = first >})
3099 | _ ->
3100 Some (coe self)
3102 match opt with
3103 | None -> m_prev_uioh
3104 | Some uioh -> uioh
3106 method motion _ y =
3107 match state.mstate with
3108 | Mscrolly ->
3109 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3110 let first = truncate (s *. float source#getitemcount) in
3111 let first = min source#getitemcount first in
3112 G.postRedisplay "listview motion";
3113 coe {< m_first = first; m_active = first >}
3114 | _ -> coe self
3116 method pmotion x y =
3117 if x < conf.winw - conf.scrollbw
3118 then
3119 let n =
3120 match self#elemunder y with
3121 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3122 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3124 let o =
3125 if n != m_active
3126 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3127 else self
3129 coe o
3130 else (
3131 Wsi.setcursor Wsi.CURSOR_INHERIT;
3132 coe self
3135 method infochanged _ = ()
3137 method scrollpw = (0, 0.0, 0.0)
3138 method scrollph =
3139 let nfs = fstate.fontsize + 1 in
3140 let y = m_first * nfs in
3141 let itemcount = source#getitemcount in
3142 let maxi = max 0 (itemcount - fstate.maxrows) in
3143 let maxy = maxi * nfs in
3144 let p, h = scrollph y maxy in
3145 conf.scrollbw, p, h
3147 method modehash = modehash
3148 end;;
3150 class outlinelistview ~source =
3151 object (self)
3152 inherit listview
3153 ~source:(source :> lvsource)
3154 ~trusted:false
3155 ~modehash:(findkeyhash conf "outline")
3156 as super
3158 method key key mask =
3159 let calcfirst first active =
3160 if active > first
3161 then
3162 let rows = active - first in
3163 if rows > fstate.maxrows then active - fstate.maxrows else first
3164 else active
3166 let navigate incr =
3167 let active = m_active + incr in
3168 let active = bound active 0 (source#getitemcount - 1) in
3169 let first = calcfirst m_first active in
3170 G.postRedisplay "outline navigate";
3171 coe {< m_active = active; m_first = first >}
3173 let ctrl = Wsi.withctrl mask in
3174 match key with
3175 | 110 when ctrl -> (* ctrl-n *)
3176 source#narrow m_qsearch;
3177 G.postRedisplay "outline ctrl-n";
3178 coe {< m_first = 0; m_active = 0 >}
3180 | 117 when ctrl -> (* ctrl-u *)
3181 source#denarrow;
3182 G.postRedisplay "outline ctrl-u";
3183 state.text <- "";
3184 coe {< m_first = 0; m_active = 0 >}
3186 | 108 when ctrl -> (* ctrl-l *)
3187 let first = m_active - (fstate.maxrows / 2) in
3188 G.postRedisplay "outline ctrl-l";
3189 coe {< m_first = first >}
3191 | 0xff9f | 0xffff -> (* delete *)
3192 source#remove m_active;
3193 G.postRedisplay "outline delete";
3194 let active = max 0 (m_active-1) in
3195 coe {< m_first = firstof m_first active;
3196 m_active = active >}
3198 | 0xff52 -> navigate ~-1 (* up *)
3199 | 0xff54 -> navigate 1 (* down *)
3200 | 0xff55 -> (* prior *)
3201 navigate ~-(fstate.maxrows)
3202 | 0xff56 -> (* next *)
3203 navigate fstate.maxrows
3205 | 0xff53 -> (* [ctrl-]right *)
3206 let o =
3207 if ctrl
3208 then (
3209 G.postRedisplay "outline ctrl right";
3210 {< m_pan = m_pan + 1 >}
3212 else self#updownlevel 1
3214 coe o
3216 | 0xff51 -> (* [ctrl-]left *)
3217 let o =
3218 if ctrl
3219 then (
3220 G.postRedisplay "outline ctrl left";
3221 {< m_pan = m_pan - 1 >}
3223 else self#updownlevel ~-1
3225 coe o
3227 | 0xff50 -> (* home *)
3228 G.postRedisplay "outline home";
3229 coe {< m_first = 0; m_active = 0 >}
3231 | 0xff57 -> (* end *)
3232 let active = source#getitemcount - 1 in
3233 let first = max 0 (active - fstate.maxrows) in
3234 G.postRedisplay "outline end";
3235 coe {< m_active = active; m_first = first >}
3237 | _ -> super#key key mask
3240 let outlinesource usebookmarks =
3241 let empty = [||] in
3242 (object
3243 inherit lvsourcebase
3244 val mutable m_items = empty
3245 val mutable m_orig_items = empty
3246 val mutable m_prev_items = empty
3247 val mutable m_narrow_pattern = ""
3248 val mutable m_hadremovals = false
3250 method getitemcount =
3251 Array.length m_items + (if m_hadremovals then 1 else 0)
3253 method getitem n =
3254 if n == Array.length m_items && m_hadremovals
3255 then
3256 ("[Confirm removal]", 0)
3257 else
3258 let s, n, _ = m_items.(n) in
3259 (s, n)
3261 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3262 ignore (uioh, first, qsearch);
3263 let confrimremoval = m_hadremovals && active = Array.length m_items in
3264 let items =
3265 if String.length m_narrow_pattern = 0
3266 then m_orig_items
3267 else m_items
3269 if not cancel
3270 then (
3271 if not confrimremoval
3272 then(
3273 let _, _, anchor = m_items.(active) in
3274 gotoanchor anchor;
3275 m_items <- items;
3277 else (
3278 state.bookmarks <- Array.to_list m_items;
3279 m_orig_items <- m_items;
3282 else m_items <- items;
3283 m_pan <- pan;
3284 None
3286 method hasaction _ = true
3288 method greetmsg =
3289 if Array.length m_items != Array.length m_orig_items
3290 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3291 else ""
3293 method narrow pattern =
3294 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3295 match reopt with
3296 | None -> ()
3297 | Some re ->
3298 let rec loop accu n =
3299 if n = -1
3300 then (
3301 m_narrow_pattern <- pattern;
3302 m_items <- Array.of_list accu
3304 else
3305 let (s, _, _) as o = m_items.(n) in
3306 let accu =
3307 if (try ignore (Str.search_forward re s 0); true
3308 with Not_found -> false)
3309 then o :: accu
3310 else accu
3312 loop accu (n-1)
3314 loop [] (Array.length m_items - 1)
3316 method denarrow =
3317 m_orig_items <- (
3318 if usebookmarks
3319 then Array.of_list state.bookmarks
3320 else state.outlines
3322 m_items <- m_orig_items
3324 method remove m =
3325 if usebookmarks
3326 then
3327 if m >= 0 && m < Array.length m_items
3328 then (
3329 m_hadremovals <- true;
3330 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3331 let n = if n >= m then n+1 else n in
3332 m_items.(n)
3336 method reset anchor items =
3337 m_hadremovals <- false;
3338 if m_orig_items == empty || m_prev_items != items
3339 then (
3340 m_orig_items <- items;
3341 if String.length m_narrow_pattern = 0
3342 then m_items <- items;
3344 m_prev_items <- items;
3345 let rely = getanchory anchor in
3346 let active =
3347 let rec loop n best bestd =
3348 if n = Array.length m_items
3349 then best
3350 else
3351 let (_, _, anchor) = m_items.(n) in
3352 let orely = getanchory anchor in
3353 let d = abs (orely - rely) in
3354 if d < bestd
3355 then loop (n+1) n d
3356 else loop (n+1) best bestd
3358 loop 0 ~-1 max_int
3360 m_active <- active;
3361 m_first <- firstof m_first active
3362 end)
3365 let enterselector usebookmarks =
3366 let source = outlinesource usebookmarks in
3367 fun errmsg ->
3368 let outlines =
3369 if usebookmarks
3370 then Array.of_list state.bookmarks
3371 else state.outlines
3373 if Array.length outlines = 0
3374 then (
3375 showtext ' ' errmsg;
3377 else (
3378 state.text <- source#greetmsg;
3379 Wsi.setcursor Wsi.CURSOR_INHERIT;
3380 let anchor = getanchor () in
3381 source#reset anchor outlines;
3382 state.uioh <- coe (new outlinelistview ~source);
3383 G.postRedisplay "enter selector";
3387 let enteroutlinemode =
3388 let f = enterselector false in
3389 fun ()-> f "Document has no outline";
3392 let enterbookmarkmode =
3393 let f = enterselector true in
3394 fun () -> f "Document has no bookmarks (yet)";
3397 let color_of_string s =
3398 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3399 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3403 let color_to_string (r, g, b) =
3404 let r = truncate (r *. 256.0)
3405 and g = truncate (g *. 256.0)
3406 and b = truncate (b *. 256.0) in
3407 Printf.sprintf "%d/%d/%d" r g b
3410 let irect_of_string s =
3411 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3414 let irect_to_string (x0,y0,x1,y1) =
3415 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3418 let makecheckers () =
3419 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3420 following to say:
3421 converted by Issac Trotts. July 25, 2002 *)
3422 let image_height = 64
3423 and image_width = 64 in
3425 let make_image () =
3426 let image =
3427 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3429 for i = 0 to image_width - 1 do
3430 for j = 0 to image_height - 1 do
3431 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3432 (if (i land 8 ) lxor (j land 8) = 0
3433 then [|255;255;255|] else [|200;200;200|])
3434 done
3435 done;
3436 image
3438 let image = make_image () in
3439 let id = GlTex.gen_texture () in
3440 GlTex.bind_texture `texture_2d id;
3441 GlPix.store (`unpack_alignment 1);
3442 GlTex.image2d image;
3443 List.iter (GlTex.parameter ~target:`texture_2d)
3444 [ `wrap_s `repeat;
3445 `wrap_t `repeat;
3446 `mag_filter `nearest;
3447 `min_filter `nearest ];
3451 let setcheckers enabled =
3452 match state.texid with
3453 | None ->
3454 if enabled then state.texid <- Some (makecheckers ())
3456 | Some texid ->
3457 if not enabled
3458 then (
3459 GlTex.delete_texture texid;
3460 state.texid <- None;
3464 let int_of_string_with_suffix s =
3465 let l = String.length s in
3466 let s1, shift =
3467 if l > 1
3468 then
3469 let suffix = Char.lowercase s.[l-1] in
3470 match suffix with
3471 | 'k' -> String.sub s 0 (l-1), 10
3472 | 'm' -> String.sub s 0 (l-1), 20
3473 | 'g' -> String.sub s 0 (l-1), 30
3474 | _ -> s, 0
3475 else s, 0
3477 let n = int_of_string s1 in
3478 let m = n lsl shift in
3479 if m < 0 || m < n
3480 then raise (Failure "value too large")
3481 else m
3484 let string_with_suffix_of_int n =
3485 if n = 0
3486 then "0"
3487 else
3488 let n, s =
3489 if n = 0
3490 then 0, ""
3491 else (
3492 if n land ((1 lsl 20) - 1) = 0
3493 then n lsr 20, "M"
3494 else (
3495 if n land ((1 lsl 10) - 1) = 0
3496 then n lsr 10, "K"
3497 else n, ""
3501 let rec loop s n =
3502 let h = n mod 1000 in
3503 let n = n / 1000 in
3504 if n = 0
3505 then string_of_int h ^ s
3506 else (
3507 let s = Printf.sprintf "_%03d%s" h s in
3508 loop s n
3511 loop "" n ^ s;
3514 let defghyllscroll = (40, 8, 32);;
3515 let ghyllscroll_of_string s =
3516 let (n, a, b) as nab =
3517 if s = "default"
3518 then defghyllscroll
3519 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3521 if n <= a || n <= b || a >= b
3522 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3523 nab;
3526 let ghyllscroll_to_string ((n, a, b) as nab) =
3527 if nab = defghyllscroll
3528 then "default"
3529 else Printf.sprintf "%d,%d,%d" n a b;
3532 let describe_location () =
3533 let f (fn, _) l =
3534 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3536 let fn, ln = List.fold_left f (-1, -1) state.layout in
3537 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3538 let percent =
3539 if maxy <= 0
3540 then 100.
3541 else (100. *. (float state.y /. float maxy))
3543 if fn = ln
3544 then
3545 Printf.sprintf "page %d of %d [%.2f%%]"
3546 (fn+1) state.pagecount percent
3547 else
3548 Printf.sprintf
3549 "pages %d-%d of %d [%.2f%%]"
3550 (fn+1) (ln+1) state.pagecount percent
3553 let enterinfomode =
3554 let btos b = if b then "\xe2\x88\x9a" else "" in
3555 let showextended = ref false in
3556 let leave mode = function
3557 | Confirm -> state.mode <- mode
3558 | Cancel -> state.mode <- mode in
3559 let src =
3560 (object
3561 val mutable m_first_time = true
3562 val mutable m_l = []
3563 val mutable m_a = [||]
3564 val mutable m_prev_uioh = nouioh
3565 val mutable m_prev_mode = View
3567 inherit lvsourcebase
3569 method reset prev_mode prev_uioh =
3570 m_a <- Array.of_list (List.rev m_l);
3571 m_l <- [];
3572 m_prev_mode <- prev_mode;
3573 m_prev_uioh <- prev_uioh;
3574 if m_first_time
3575 then (
3576 let rec loop n =
3577 if n >= Array.length m_a
3578 then ()
3579 else
3580 match m_a.(n) with
3581 | _, _, _, Action _ -> m_active <- n
3582 | _ -> loop (n+1)
3584 loop 0;
3585 m_first_time <- false;
3588 method int name get set =
3589 m_l <-
3590 (name, `int get, 1, Action (
3591 fun u ->
3592 let ondone s =
3593 try set (int_of_string s)
3594 with exn ->
3595 state.text <- Printf.sprintf "bad integer `%s': %s"
3596 s (Printexc.to_string exn)
3598 state.text <- "";
3599 let te = name ^ ": ", "", None, intentry, ondone in
3600 state.mode <- Textentry (te, leave m_prev_mode);
3602 )) :: m_l
3604 method int_with_suffix name get set =
3605 m_l <-
3606 (name, `intws get, 1, Action (
3607 fun u ->
3608 let ondone s =
3609 try set (int_of_string_with_suffix s)
3610 with exn ->
3611 state.text <- Printf.sprintf "bad integer `%s': %s"
3612 s (Printexc.to_string exn)
3614 state.text <- "";
3615 let te =
3616 name ^ ": ", "", None, intentry_with_suffix, ondone
3618 state.mode <- Textentry (te, leave m_prev_mode);
3620 )) :: m_l
3622 method bool ?(offset=1) ?(btos=btos) name get set =
3623 m_l <-
3624 (name, `bool (btos, get), offset, Action (
3625 fun u ->
3626 let v = get () in
3627 set (not v);
3629 )) :: m_l
3631 method color name get set =
3632 m_l <-
3633 (name, `color get, 1, Action (
3634 fun u ->
3635 let invalid = (nan, nan, nan) in
3636 let ondone s =
3637 let c =
3638 try color_of_string s
3639 with exn ->
3640 state.text <- Printf.sprintf "bad color `%s': %s"
3641 s (Printexc.to_string exn);
3642 invalid
3644 if c <> invalid
3645 then set c;
3647 let te = name ^ ": ", "", None, textentry, ondone in
3648 state.text <- color_to_string (get ());
3649 state.mode <- Textentry (te, leave m_prev_mode);
3651 )) :: m_l
3653 method string name get set =
3654 m_l <-
3655 (name, `string get, 1, Action (
3656 fun u ->
3657 let ondone s = set s in
3658 let te = name ^ ": ", "", None, textentry, ondone in
3659 state.mode <- Textentry (te, leave m_prev_mode);
3661 )) :: m_l
3663 method colorspace name get set =
3664 m_l <-
3665 (name, `string get, 1, Action (
3666 fun _ ->
3667 let source =
3668 let vals = [| "rgb"; "bgr"; "gray" |] in
3669 (object
3670 inherit lvsourcebase
3672 initializer
3673 m_active <- int_of_colorspace conf.colorspace;
3674 m_first <- 0;
3676 method getitemcount = Array.length vals
3677 method getitem n = (vals.(n), 0)
3678 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3679 ignore (uioh, first, pan, qsearch);
3680 if not cancel then set active;
3681 None
3682 method hasaction _ = true
3683 end)
3685 state.text <- "";
3686 let modehash = findkeyhash conf "info" in
3687 coe (new listview ~source ~trusted:true ~modehash)
3688 )) :: m_l
3690 method caption s offset =
3691 m_l <- (s, `empty, offset, Noaction) :: m_l
3693 method caption2 s f offset =
3694 m_l <- (s, `string f, offset, Noaction) :: m_l
3696 method getitemcount = Array.length m_a
3698 method getitem n =
3699 let tostr = function
3700 | `int f -> string_of_int (f ())
3701 | `intws f -> string_with_suffix_of_int (f ())
3702 | `string f -> f ()
3703 | `color f -> color_to_string (f ())
3704 | `bool (btos, f) -> btos (f ())
3705 | `empty -> ""
3707 let name, t, offset, _ = m_a.(n) in
3708 ((let s = tostr t in
3709 if String.length s > 0
3710 then Printf.sprintf "%s\t%s" name s
3711 else name),
3712 offset)
3714 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3715 let uiohopt =
3716 if not cancel
3717 then (
3718 m_qsearch <- qsearch;
3719 let uioh =
3720 match m_a.(active) with
3721 | _, _, _, Action f -> f uioh
3722 | _ -> uioh
3724 Some uioh
3726 else None
3728 m_active <- active;
3729 m_first <- first;
3730 m_pan <- pan;
3731 uiohopt
3733 method hasaction n =
3734 match m_a.(n) with
3735 | _, _, _, Action _ -> true
3736 | _ -> false
3737 end)
3739 let rec fillsrc prevmode prevuioh =
3740 let sep () = src#caption "" 0 in
3741 let colorp name get set =
3742 src#string name
3743 (fun () -> color_to_string (get ()))
3744 (fun v ->
3746 let c = color_of_string v in
3747 set c
3748 with exn ->
3749 state.text <- Printf.sprintf "bad color `%s': %s"
3750 v (Printexc.to_string exn);
3753 let oldmode = state.mode in
3754 let birdseye = isbirdseye state.mode in
3756 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3758 src#bool "presentation mode"
3759 (fun () -> conf.presentation)
3760 (fun v ->
3761 conf.presentation <- v;
3762 state.anchor <- getanchor ();
3763 represent ());
3765 src#bool "ignore case in searches"
3766 (fun () -> conf.icase)
3767 (fun v -> conf.icase <- v);
3769 src#bool "preload"
3770 (fun () -> conf.preload)
3771 (fun v -> conf.preload <- v);
3773 src#bool "highlight links"
3774 (fun () -> conf.hlinks)
3775 (fun v -> conf.hlinks <- v);
3777 src#bool "under info"
3778 (fun () -> conf.underinfo)
3779 (fun v -> conf.underinfo <- v);
3781 src#bool "persistent bookmarks"
3782 (fun () -> conf.savebmarks)
3783 (fun v -> conf.savebmarks <- v);
3785 src#bool "proportional display"
3786 (fun () -> conf.proportional)
3787 (fun v -> reqlayout conf.angle v);
3789 src#bool "trim margins"
3790 (fun () -> conf.trimmargins)
3791 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3793 src#bool "persistent location"
3794 (fun () -> conf.jumpback)
3795 (fun v -> conf.jumpback <- v);
3797 sep ();
3798 src#int "inter-page space"
3799 (fun () -> conf.interpagespace)
3800 (fun n ->
3801 conf.interpagespace <- n;
3802 let pageno, py =
3803 match state.layout with
3804 | [] -> 0, 0
3805 | l :: _ ->
3806 l.pageno, l.pagey
3808 state.maxy <- calcheight ();
3809 let y = getpagey pageno in
3810 gotoy (y + py)
3813 src#int "page bias"
3814 (fun () -> conf.pagebias)
3815 (fun v -> conf.pagebias <- v);
3817 src#int "scroll step"
3818 (fun () -> conf.scrollstep)
3819 (fun n -> conf.scrollstep <- n);
3821 src#int "auto scroll step"
3822 (fun () ->
3823 match state.autoscroll with
3824 | Some step -> step
3825 | _ -> conf.autoscrollstep)
3826 (fun n ->
3827 if state.autoscroll <> None
3828 then state.autoscroll <- Some n;
3829 conf.autoscrollstep <- n);
3831 src#int "zoom"
3832 (fun () -> truncate (conf.zoom *. 100.))
3833 (fun v -> setzoom ((float v) /. 100.));
3835 src#int "rotation"
3836 (fun () -> conf.angle)
3837 (fun v -> reqlayout v conf.proportional);
3839 src#int "scroll bar width"
3840 (fun () -> state.scrollw)
3841 (fun v ->
3842 state.scrollw <- v;
3843 conf.scrollbw <- v;
3844 reshape conf.winw conf.winh;
3847 src#int "scroll handle height"
3848 (fun () -> conf.scrollh)
3849 (fun v -> conf.scrollh <- v;);
3851 src#int "thumbnail width"
3852 (fun () -> conf.thumbw)
3853 (fun v ->
3854 conf.thumbw <- min 4096 v;
3855 match oldmode with
3856 | Birdseye beye ->
3857 leavebirdseye beye false;
3858 enterbirdseye ()
3859 | _ -> ()
3862 src#string "columns"
3863 (fun () ->
3864 match conf.columns with
3865 | None -> "1"
3866 | Some (multicol, _) -> columns_to_string multicol)
3867 (fun v ->
3868 let n, a, b = columns_of_string v in
3869 setcolumns n a b);
3871 sep ();
3872 src#caption "Presentation mode" 0;
3873 src#bool "scrollbar visible"
3874 (fun () -> conf.scrollbarinpm)
3875 (fun v ->
3876 if v != conf.scrollbarinpm
3877 then (
3878 conf.scrollbarinpm <- v;
3879 if conf.presentation
3880 then (
3881 state.scrollw <- if v then conf.scrollbw else 0;
3882 reshape conf.winw conf.winh;
3887 sep ();
3888 src#caption "Pixmap cache" 0;
3889 src#int_with_suffix "size (advisory)"
3890 (fun () -> conf.memlimit)
3891 (fun v -> conf.memlimit <- v);
3893 src#caption2 "used"
3894 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3895 (string_with_suffix_of_int state.memused)
3896 (Hashtbl.length state.tilemap)) 1;
3898 sep ();
3899 src#caption "Layout" 0;
3900 src#caption2 "Dimension"
3901 (fun () ->
3902 Printf.sprintf "%dx%d (virtual %dx%d)"
3903 conf.winw conf.winh
3904 state.w state.maxy)
3906 if conf.debug
3907 then
3908 src#caption2 "Position" (fun () ->
3909 Printf.sprintf "%dx%d" state.x state.y
3911 else
3912 src#caption2 "Visible" (fun () -> describe_location ()) 1
3915 sep ();
3916 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3917 "Save these parameters as global defaults at exit"
3918 (fun () -> conf.bedefault)
3919 (fun v -> conf.bedefault <- v)
3922 sep ();
3923 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3924 src#bool ~offset:0 ~btos "Extended parameters"
3925 (fun () -> !showextended)
3926 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3927 if !showextended
3928 then (
3929 src#bool "checkers"
3930 (fun () -> conf.checkers)
3931 (fun v -> conf.checkers <- v; setcheckers v);
3932 src#bool "update cursor"
3933 (fun () -> conf.updatecurs)
3934 (fun v -> conf.updatecurs <- v);
3935 src#bool "verbose"
3936 (fun () -> conf.verbose)
3937 (fun v -> conf.verbose <- v);
3938 src#bool "invert colors"
3939 (fun () -> conf.invert)
3940 (fun v -> conf.invert <- v);
3941 src#bool "max fit"
3942 (fun () -> conf.maxhfit)
3943 (fun v -> conf.maxhfit <- v);
3944 src#bool "redirect stderr"
3945 (fun () -> conf.redirectstderr)
3946 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3947 src#string "uri launcher"
3948 (fun () -> conf.urilauncher)
3949 (fun v -> conf.urilauncher <- v);
3950 src#string "path launcher"
3951 (fun () -> conf.pathlauncher)
3952 (fun v -> conf.pathlauncher <- v);
3953 src#string "tile size"
3954 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3955 (fun v ->
3957 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3958 conf.tileh <- max 64 w;
3959 conf.tilew <- max 64 h;
3960 flushtiles ();
3961 with exn ->
3962 state.text <- Printf.sprintf "bad tile size `%s': %s"
3963 v (Printexc.to_string exn));
3964 src#int "texture count"
3965 (fun () -> conf.texcount)
3966 (fun v ->
3967 if realloctexts v
3968 then conf.texcount <- v
3969 else showtext '!' " Failed to set texture count please retry later"
3971 src#int "slice height"
3972 (fun () -> conf.sliceheight)
3973 (fun v ->
3974 conf.sliceheight <- v;
3975 wcmd "sliceh" [`i conf.sliceheight];
3977 src#int "anti-aliasing level"
3978 (fun () -> conf.aalevel)
3979 (fun v ->
3980 conf.aalevel <- bound v 0 8;
3981 state.anchor <- getanchor ();
3982 opendoc state.path state.password;
3984 src#int "ui font size"
3985 (fun () -> fstate.fontsize)
3986 (fun v -> setfontsize (bound v 5 100));
3987 colorp "background color"
3988 (fun () -> conf.bgcolor)
3989 (fun v -> conf.bgcolor <- v);
3990 src#bool "crop hack"
3991 (fun () -> conf.crophack)
3992 (fun v -> conf.crophack <- v);
3993 src#string "trim fuzz"
3994 (fun () -> irect_to_string conf.trimfuzz)
3995 (fun v ->
3997 conf.trimfuzz <- irect_of_string v;
3998 if conf.trimmargins
3999 then settrim true conf.trimfuzz;
4000 with exn ->
4001 state.text <- Printf.sprintf "bad irect `%s': %s"
4002 v (Printexc.to_string exn)
4004 src#string "throttle"
4005 (fun () ->
4006 match conf.maxwait with
4007 | None -> "show place holder if page is not ready"
4008 | Some time ->
4009 if time = infinity
4010 then "wait for page to fully render"
4011 else
4012 "wait " ^ string_of_float time
4013 ^ " seconds before showing placeholder"
4015 (fun v ->
4017 let f = float_of_string v in
4018 if f <= 0.0
4019 then conf.maxwait <- None
4020 else conf.maxwait <- Some f
4021 with exn ->
4022 state.text <- Printf.sprintf "bad time `%s': %s"
4023 v (Printexc.to_string exn)
4025 src#string "ghyll scroll"
4026 (fun () ->
4027 match conf.ghyllscroll with
4028 | None -> ""
4029 | Some nab -> ghyllscroll_to_string nab
4031 (fun v ->
4033 let gs =
4034 if String.length v = 0
4035 then None
4036 else Some (ghyllscroll_of_string v)
4038 conf.ghyllscroll <- gs
4039 with exn ->
4040 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4041 v (Printexc.to_string exn)
4043 src#string "selection command"
4044 (fun () -> conf.selcmd)
4045 (fun v -> conf.selcmd <- v);
4046 src#colorspace "color space"
4047 (fun () -> colorspace_to_string conf.colorspace)
4048 (fun v ->
4049 conf.colorspace <- colorspace_of_int v;
4050 wcmd "cs" [`i v];
4051 load state.layout;
4055 sep ();
4056 src#caption "Document" 0;
4057 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4058 src#caption2 "Pages"
4059 (fun () -> string_of_int state.pagecount) 1;
4060 src#caption2 "Dimensions"
4061 (fun () -> string_of_int (List.length state.pdims)) 1;
4062 if conf.trimmargins
4063 then (
4064 sep ();
4065 src#caption "Trimmed margins" 0;
4066 src#caption2 "Dimensions"
4067 (fun () -> string_of_int (List.length state.pdims)) 1;
4070 src#reset prevmode prevuioh;
4072 fun () ->
4073 state.text <- "";
4074 let prevmode = state.mode
4075 and prevuioh = state.uioh in
4076 fillsrc prevmode prevuioh;
4077 let source = (src :> lvsource) in
4078 let modehash = findkeyhash conf "info" in
4079 state.uioh <- coe (object (self)
4080 inherit listview ~source ~trusted:true ~modehash as super
4081 val mutable m_prevmemused = 0
4082 method infochanged = function
4083 | Memused ->
4084 if m_prevmemused != state.memused
4085 then (
4086 m_prevmemused <- state.memused;
4087 G.postRedisplay "memusedchanged";
4089 | Pdim -> G.postRedisplay "pdimchanged"
4090 | Docinfo -> fillsrc prevmode prevuioh
4092 method key key mask =
4093 if not (Wsi.withctrl mask)
4094 then
4095 match key with
4096 | 0xff51 -> coe (self#updownlevel ~-1)
4097 | 0xff53 -> coe (self#updownlevel 1)
4098 | _ -> super#key key mask
4099 else super#key key mask
4100 end);
4101 G.postRedisplay "info";
4104 let enterhelpmode =
4105 let source =
4106 (object
4107 inherit lvsourcebase
4108 method getitemcount = Array.length state.help
4109 method getitem n =
4110 let s, n, _ = state.help.(n) in
4111 (s, n)
4113 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4114 let optuioh =
4115 if not cancel
4116 then (
4117 m_qsearch <- qsearch;
4118 match state.help.(active) with
4119 | _, _, Action f -> Some (f uioh)
4120 | _ -> Some (uioh)
4122 else None
4124 m_active <- active;
4125 m_first <- first;
4126 m_pan <- pan;
4127 optuioh
4129 method hasaction n =
4130 match state.help.(n) with
4131 | _, _, Action _ -> true
4132 | _ -> false
4134 initializer
4135 m_active <- -1
4136 end)
4137 in fun () ->
4138 let modehash = findkeyhash conf "help" in
4139 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4140 G.postRedisplay "help";
4143 let entermsgsmode =
4144 let msgsource =
4145 let re = Str.regexp "[\r\n]" in
4146 (object
4147 inherit lvsourcebase
4148 val mutable m_items = [||]
4150 method getitemcount = 1 + Array.length m_items
4152 method getitem n =
4153 if n = 0
4154 then "[Clear]", 0
4155 else m_items.(n-1), 0
4157 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4158 ignore uioh;
4159 if not cancel
4160 then (
4161 if active = 0
4162 then Buffer.clear state.errmsgs;
4163 m_qsearch <- qsearch;
4165 m_active <- active;
4166 m_first <- first;
4167 m_pan <- pan;
4168 None
4170 method hasaction n =
4171 n = 0
4173 method reset =
4174 state.newerrmsgs <- false;
4175 let l = Str.split re (Buffer.contents state.errmsgs) in
4176 m_items <- Array.of_list l
4178 initializer
4179 m_active <- 0
4180 end)
4181 in fun () ->
4182 state.text <- "";
4183 msgsource#reset;
4184 let source = (msgsource :> lvsource) in
4185 let modehash = findkeyhash conf "listview" in
4186 state.uioh <- coe (object
4187 inherit listview ~source ~trusted:false ~modehash as super
4188 method display =
4189 if state.newerrmsgs
4190 then msgsource#reset;
4191 super#display
4192 end);
4193 G.postRedisplay "msgs";
4196 let quickbookmark ?title () =
4197 match state.layout with
4198 | [] -> ()
4199 | l :: _ ->
4200 let title =
4201 match title with
4202 | None ->
4203 let sec = Unix.gettimeofday () in
4204 let tm = Unix.localtime sec in
4205 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4206 (l.pageno+1)
4207 tm.Unix.tm_mday
4208 tm.Unix.tm_mon
4209 (tm.Unix.tm_year + 1900)
4210 tm.Unix.tm_hour
4211 tm.Unix.tm_min
4212 | Some title -> title
4214 state.bookmarks <-
4215 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4216 :: state.bookmarks
4219 let doreshape w h =
4220 state.fullscreen <- None;
4221 Wsi.reshape w h;
4224 let setautoscrollspeed step goingdown =
4225 let incr = max 1 ((abs step) / 2) in
4226 let incr = if goingdown then incr else -incr in
4227 let astep = step + incr in
4228 state.autoscroll <- Some astep;
4231 let viewkeyboard key mask =
4232 let enttext te =
4233 let mode = state.mode in
4234 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4235 state.text <- "";
4236 enttext ();
4237 G.postRedisplay "view:enttext"
4239 let ctrl = Wsi.withctrl mask in
4240 match key with
4241 | 81 -> (* Q *)
4242 exit 0
4244 | 0xff1b | 113 -> (* escape / q *)
4245 begin match state.mstate with
4246 | Mzoomrect _ ->
4247 state.mstate <- Mnone;
4248 Wsi.setcursor Wsi.CURSOR_INHERIT;
4249 G.postRedisplay "kill zoom rect";
4250 | _ ->
4251 match state.ranchors with
4252 | [] -> raise Wsi.Quit
4253 | (path, password, anchor) :: rest ->
4254 state.ranchors <- rest;
4255 state.anchor <- anchor;
4256 opendoc path password
4257 end;
4259 | 0xff08 -> (* backspace *)
4260 let y = getnav ~-1 in
4261 gotoy_and_clear_text y
4263 | 111 -> (* o *)
4264 enteroutlinemode ()
4266 | 117 -> (* u *)
4267 state.rects <- [];
4268 state.text <- "";
4269 G.postRedisplay "dehighlight";
4271 | 47 | 63 -> (* / ? *)
4272 let ondone isforw s =
4273 cbput state.hists.pat s;
4274 state.searchpattern <- s;
4275 search s isforw
4277 let s = String.create 1 in
4278 s.[0] <- Char.chr key;
4279 enttext (s, "", Some (onhist state.hists.pat),
4280 textentry, ondone (key = 47))
4282 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4283 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4284 setzoom (conf.zoom +. incr)
4286 | 43 | 0xffab -> (* + *)
4287 let ondone s =
4288 let n =
4289 try int_of_string s with exc ->
4290 state.text <- Printf.sprintf "bad integer `%s': %s"
4291 s (Printexc.to_string exc);
4292 max_int
4294 if n != max_int
4295 then (
4296 conf.pagebias <- n;
4297 state.text <- "page bias is now " ^ string_of_int n;
4300 enttext ("page bias: ", "", None, intentry, ondone)
4302 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4303 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4304 setzoom (max 0.01 (conf.zoom -. decr))
4306 | 45 | 0xffad -> (* - *)
4307 let ondone msg = state.text <- msg in
4308 enttext (
4309 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4310 optentry state.mode, ondone
4313 | 48 when ctrl -> (* ctrl-0 *)
4314 setzoom 1.0
4316 | 49 when ctrl -> (* 1 *)
4317 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4318 if zoom < 1.0
4319 then setzoom zoom
4321 | 0xffc6 -> (* f9 *)
4322 togglebirdseye ()
4324 | 57 when ctrl -> (* ctrl-9 *)
4325 togglebirdseye ()
4327 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4328 when not ctrl -> (* 0..9 *)
4329 let ondone s =
4330 let n =
4331 try int_of_string s with exc ->
4332 state.text <- Printf.sprintf "bad integer `%s': %s"
4333 s (Printexc.to_string exc);
4336 if n >= 0
4337 then (
4338 addnav ();
4339 cbput state.hists.pag (string_of_int n);
4340 gotopage1 (n + conf.pagebias - 1) 0;
4343 let pageentry text key =
4344 match Char.unsafe_chr key with
4345 | 'g' -> TEdone text
4346 | _ -> intentry text key
4348 let text = "x" in text.[0] <- Char.chr key;
4349 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4351 | 98 -> (* b *)
4352 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4353 reshape conf.winw conf.winh;
4355 | 108 -> (* l *)
4356 conf.hlinks <- not conf.hlinks;
4357 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4358 G.postRedisplay "toggle highlightlinks";
4360 | 97 -> (* a *)
4361 begin match state.autoscroll with
4362 | Some step ->
4363 conf.autoscrollstep <- step;
4364 state.autoscroll <- None
4365 | None ->
4366 if conf.autoscrollstep = 0
4367 then state.autoscroll <- Some 1
4368 else state.autoscroll <- Some conf.autoscrollstep
4371 | 80 -> (* P *)
4372 conf.presentation <- not conf.presentation;
4373 if conf.presentation
4374 then (
4375 if not conf.scrollbarinpm
4376 then state.scrollw <- 0;
4378 else
4379 state.scrollw <- conf.scrollbw;
4381 showtext ' ' ("presentation mode " ^
4382 if conf.presentation then "on" else "off");
4383 state.anchor <- getanchor ();
4384 represent ()
4386 | 102 -> (* f *)
4387 begin match state.fullscreen with
4388 | None ->
4389 state.fullscreen <- Some (conf.winw, conf.winh);
4390 Wsi.fullscreen ()
4391 | Some (w, h) ->
4392 state.fullscreen <- None;
4393 doreshape w h
4396 | 103 -> (* g *)
4397 gotoy_and_clear_text 0
4399 | 71 -> (* G *)
4400 gotopage1 (state.pagecount - 1) 0
4402 | 112 | 78 -> (* p|N *)
4403 search state.searchpattern false
4405 | 110 | 0xffc0 -> (* n|F3 *)
4406 search state.searchpattern true
4408 | 116 -> (* t *)
4409 begin match state.layout with
4410 | [] -> ()
4411 | l :: _ ->
4412 gotoy_and_clear_text (getpagey l.pageno)
4415 | 32 -> (* ' ' *)
4416 begin match List.rev state.layout with
4417 | [] -> ()
4418 | l :: _ ->
4419 let pageno = min (l.pageno+1) (state.pagecount-1) in
4420 gotoy_and_clear_text (getpagey pageno)
4423 | 0xff9f | 0xffff -> (* delete *)
4424 begin match state.layout with
4425 | [] -> ()
4426 | l :: _ ->
4427 let pageno = max 0 (l.pageno-1) in
4428 gotoy_and_clear_text (getpagey pageno)
4431 | 61 -> (* = *)
4432 showtext ' ' (describe_location ());
4434 | 119 -> (* w *)
4435 begin match state.layout with
4436 | [] -> ()
4437 | l :: _ ->
4438 doreshape (l.pagew + state.scrollw) l.pageh;
4439 G.postRedisplay "w"
4442 | 39 -> (* ' *)
4443 enterbookmarkmode ()
4445 | 104 | 0xffbe -> (* h|F1 *)
4446 enterhelpmode ()
4448 | 105 -> (* i *)
4449 enterinfomode ()
4451 | 101 when conf.redirectstderr -> (* e *)
4452 entermsgsmode ()
4454 | 109 -> (* m *)
4455 let ondone s =
4456 match state.layout with
4457 | l :: _ ->
4458 state.bookmarks <-
4459 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4460 :: state.bookmarks
4461 | _ -> ()
4463 enttext ("bookmark: ", "", None, textentry, ondone)
4465 | 126 -> (* ~ *)
4466 quickbookmark ();
4467 showtext ' ' "Quick bookmark added";
4469 | 122 -> (* z *)
4470 begin match state.layout with
4471 | l :: _ ->
4472 let rect = getpdimrect l.pagedimno in
4473 let w, h =
4474 if conf.crophack
4475 then
4476 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4477 truncate (1.2 *. (rect.(3) -. rect.(0))))
4478 else
4479 (truncate (rect.(1) -. rect.(0)),
4480 truncate (rect.(3) -. rect.(0)))
4482 let w = truncate ((float w)*.conf.zoom)
4483 and h = truncate ((float h)*.conf.zoom) in
4484 if w != 0 && h != 0
4485 then (
4486 state.anchor <- getanchor ();
4487 doreshape (w + state.scrollw) (h + conf.interpagespace)
4489 G.postRedisplay "z";
4491 | [] -> ()
4494 | 50 when ctrl -> (* ctrl-2 *)
4495 let maxw = getmaxw () in
4496 if maxw > 0.0
4497 then setzoom (maxw /. float conf.winw)
4499 | 60 | 62 -> (* < > *)
4500 reqlayout (conf.angle + (if key = 60 then 30 else -30)) conf.proportional
4502 | 91 | 93 -> (* [ ] *)
4503 conf.colorscale <-
4504 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4506 G.postRedisplay "brightness";
4508 | 107 | 0xff52 -> (* k up *)
4509 begin match state.autoscroll with
4510 | None ->
4511 begin match state.mode with
4512 | Birdseye beye -> upbirdseye 1 beye
4513 | _ ->
4514 if ctrl
4515 then gotoy (clamp ~-(conf.winh/2))
4516 else gotoy (clamp (-conf.scrollstep))
4518 | Some n ->
4519 setautoscrollspeed n false
4522 | 106 | 0xff54 -> (* j down *)
4523 begin match state.autoscroll with
4524 | None ->
4525 begin match state.mode with
4526 | Birdseye beye -> downbirdseye 1 beye
4527 | _ ->
4528 if ctrl
4529 then gotoy_and_clear_text (clamp (conf.winh/2))
4530 else gotoy_and_clear_text (clamp conf.scrollstep)
4532 | Some n ->
4533 setautoscrollspeed n true
4536 | 0xff51 | 0xff53 when conf.zoom > 1.0 -> (* left / right *)
4537 if conf.zoom > 1.0
4538 then
4539 let dx =
4540 if ctrl
4541 then conf.winw / 2
4542 else 10
4544 let dx = if key = 0xff51 then dx else -dx in
4545 state.x <- state.x + dx;
4546 gotoy_and_clear_text state.y
4547 else (
4548 state.text <- "";
4549 G.postRedisplay "lef/right"
4552 | 0xff55 -> (* prior *)
4553 let y =
4554 if ctrl
4555 then
4556 match state.layout with
4557 | [] -> state.y
4558 | l :: _ -> state.y - l.pagey
4559 else
4560 clamp (-conf.winh)
4562 gotoghyll y
4564 | 0xff56 -> (* next *)
4565 let y =
4566 if ctrl
4567 then
4568 match List.rev state.layout with
4569 | [] -> state.y
4570 | l :: _ -> getpagey l.pageno
4571 else
4572 clamp conf.winh
4574 gotoghyll y
4576 | 0xff50 -> gotoghyll 0
4577 | 0xff57 -> gotoghyll (clamp state.maxy)
4578 | 0xff53 when Wsi.withalt mask ->
4579 gotoghyll (getnav ~-1)
4580 | 0xff51 when Wsi.withalt mask ->
4581 gotoghyll (getnav 1)
4583 | 114 -> (* r *)
4584 state.anchor <- getanchor ();
4585 opendoc state.path state.password
4587 | 76 -> (* L *)
4588 launchpath ()
4590 | 118 when conf.debug -> (* v *)
4591 state.rects <- [];
4592 List.iter (fun l ->
4593 match getopaque l.pageno with
4594 | None -> ()
4595 | Some opaque ->
4596 let x0, y0, x1, y1 = pagebbox opaque in
4597 let a,b = float x0, float y0 in
4598 let c,d = float x1, float y0 in
4599 let e,f = float x1, float y1 in
4600 let h,j = float x0, float y1 in
4601 let rect = (a,b,c,d,e,f,h,j) in
4602 debugrect rect;
4603 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4604 ) state.layout;
4605 G.postRedisplay "v";
4607 | _ ->
4608 vlog "huh? %d" key
4611 let keyboard key mask =
4612 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4613 then wcmd "interrupt" []
4614 else state.uioh <- state.uioh#key key mask
4617 let birdseyekeyboard key mask
4618 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4619 let incr =
4620 match conf.columns with
4621 | None -> 1
4622 | Some ((c, _, _), _) -> c
4624 match key with
4625 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4626 let y, h = getpageyh pageno in
4627 let top = (conf.winh - h) / 2 in
4628 gotoy (max 0 (y - top))
4629 | 0xff0d -> leavebirdseye beye false
4630 | 0xff1b -> leavebirdseye beye true (* escape *)
4631 | 0xff52 -> upbirdseye incr beye (* prior *)
4632 | 0xff54 -> downbirdseye incr beye (* next *)
4633 | 0xff51 -> upbirdseye 1 beye (* up *)
4634 | 0xff53 -> downbirdseye 1 beye (* down *)
4636 | 0xff55 ->
4637 begin match state.layout with
4638 | l :: _ ->
4639 if l.pagey != 0
4640 then (
4641 state.mode <- Birdseye (
4642 oconf, leftx, l.pageno, hooverpageno, anchor
4644 gotopage1 l.pageno 0;
4646 else (
4647 let layout = layout (state.y-conf.winh) conf.winh in
4648 match layout with
4649 | [] -> gotoy (clamp (-conf.winh))
4650 | l :: _ ->
4651 state.mode <- Birdseye (
4652 oconf, leftx, l.pageno, hooverpageno, anchor
4654 gotopage1 l.pageno 0
4657 | [] -> gotoy (clamp (-conf.winh))
4658 end;
4660 | 0xff56 ->
4661 begin match List.rev state.layout with
4662 | l :: _ ->
4663 let layout = layout (state.y + conf.winh) conf.winh in
4664 begin match layout with
4665 | [] ->
4666 let incr = l.pageh - l.pagevh in
4667 if incr = 0
4668 then (
4669 state.mode <-
4670 Birdseye (
4671 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4673 G.postRedisplay "birdseye pagedown";
4675 else gotoy (clamp (incr + conf.interpagespace*2));
4677 | l :: _ ->
4678 state.mode <-
4679 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4680 gotopage1 l.pageno 0;
4683 | [] -> gotoy (clamp conf.winh)
4684 end;
4686 | 0xff50 ->
4687 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4688 gotopage1 0 0
4690 | 0xff57 ->
4691 let pageno = state.pagecount - 1 in
4692 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4693 if not (pagevisible state.layout pageno)
4694 then
4695 let h =
4696 match List.rev state.pdims with
4697 | [] -> conf.winh
4698 | (_, _, h, _) :: _ -> h
4700 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4701 else G.postRedisplay "birdseye end";
4702 | _ -> viewkeyboard key mask
4705 let drawpage l =
4706 let color =
4707 match state.mode with
4708 | Textentry _ -> scalecolor 0.4
4709 | View -> scalecolor 1.0
4710 | Birdseye (_, _, pageno, hooverpageno, _) ->
4711 if l.pageno = hooverpageno
4712 then scalecolor 0.9
4713 else (
4714 if l.pageno = pageno
4715 then scalecolor 1.0
4716 else scalecolor 0.8
4719 drawtiles l color;
4720 begin match getopaque l.pageno with
4721 | Some opaque ->
4722 if tileready l l.pagex l.pagey
4723 then
4724 let x = l.pagedispx - l.pagex
4725 and y = l.pagedispy - l.pagey in
4726 postprocess opaque conf.hlinks x y;
4728 | _ -> ()
4729 end;
4732 let scrollindicator () =
4733 let sbw, ph, sh = state.uioh#scrollph in
4734 let sbh, pw, sw = state.uioh#scrollpw in
4736 GlDraw.color (0.64, 0.64, 0.64);
4737 GlDraw.rect
4738 (float (conf.winw - sbw), 0.)
4739 (float conf.winw, float conf.winh)
4741 GlDraw.rect
4742 (0., float (conf.winh - sbh))
4743 (float (conf.winw - state.scrollw - 1), float conf.winh)
4745 GlDraw.color (0.0, 0.0, 0.0);
4747 GlDraw.rect
4748 (float (conf.winw - sbw), ph)
4749 (float conf.winw, ph +. sh)
4751 GlDraw.rect
4752 (pw, float (conf.winh - sbh))
4753 (pw +. sw, float conf.winh)
4757 let showsel () =
4758 match state.mstate with
4759 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4762 | Msel ((x0, y0), (x1, y1)) ->
4763 let rec loop = function
4764 | l :: ls ->
4765 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4766 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4767 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4768 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4769 then
4770 match getopaque l.pageno with
4771 | Some opaque ->
4772 let dx, dy = pagetranslatepoint l 0 0 in
4773 let x0 = x0 + dx
4774 and y0 = y0 + dy
4775 and x1 = x1 + dx
4776 and y1 = y1 + dy in
4777 GlMat.mode `modelview;
4778 GlMat.push ();
4779 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4780 seltext opaque (x0, y0, x1, y1);
4781 GlMat.pop ();
4782 | _ -> ()
4783 else loop ls
4784 | [] -> ()
4786 loop state.layout
4789 let showrects () =
4790 Gl.enable `blend;
4791 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4792 GlDraw.polygon_mode `both `fill;
4793 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4794 List.iter
4795 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4796 List.iter (fun l ->
4797 if l.pageno = pageno
4798 then (
4799 let dx = float (l.pagedispx - l.pagex) in
4800 let dy = float (l.pagedispy - l.pagey) in
4801 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4802 GlDraw.begins `quads;
4804 GlDraw.vertex2 (x0+.dx, y0+.dy);
4805 GlDraw.vertex2 (x1+.dx, y1+.dy);
4806 GlDraw.vertex2 (x2+.dx, y2+.dy);
4807 GlDraw.vertex2 (x3+.dx, y3+.dy);
4809 GlDraw.ends ();
4811 ) state.layout
4812 ) state.rects
4814 Gl.disable `blend;
4817 let display () =
4818 GlClear.color (scalecolor2 conf.bgcolor);
4819 GlClear.clear [`color];
4820 List.iter drawpage state.layout;
4821 showrects ();
4822 showsel ();
4823 state.uioh#display;
4824 scrollindicator ();
4825 begin match state.mstate with
4826 | Mzoomrect ((x0, y0), (x1, y1)) ->
4827 Gl.enable `blend;
4828 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4829 GlDraw.polygon_mode `both `fill;
4830 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4831 GlDraw.rect (float x0, float y0)
4832 (float x1, float y1);
4833 Gl.disable `blend;
4834 | _ -> ()
4835 end;
4836 enttext ();
4837 if conf.updatecurs
4838 then (
4839 let mx, my = state.mpos in
4840 updateunder mx my;
4842 Wsi.swapb ();
4845 let zoomrect x y x1 y1 =
4846 let x0 = min x x1
4847 and x1 = max x x1
4848 and y0 = min y y1 in
4849 gotoy (state.y + y0);
4850 state.anchor <- getanchor ();
4851 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4852 let margin =
4853 if state.w < conf.winw - state.scrollw
4854 then (conf.winw - state.scrollw - state.w) / 2
4855 else 0
4857 state.x <- (state.x + margin) - x0;
4858 setzoom zoom;
4859 Wsi.setcursor Wsi.CURSOR_INHERIT;
4860 state.mstate <- Mnone;
4863 let scrollx x =
4864 let winw = conf.winw - state.scrollw - 1 in
4865 let s = float x /. float winw in
4866 let destx = truncate (float (state.w + winw) *. s) in
4867 state.x <- winw - destx;
4868 gotoy_and_clear_text state.y;
4869 state.mstate <- Mscrollx;
4872 let scrolly y =
4873 let s = float y /. float conf.winh in
4874 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4875 gotoy_and_clear_text desty;
4876 state.mstate <- Mscrolly;
4879 let viewmouse button down x y mask =
4880 match button with
4881 | n when (n == 4 || n == 5) && not down ->
4882 if Wsi.withctrl mask
4883 then (
4884 match state.mstate with
4885 | Mzoom (oldn, i) ->
4886 if oldn = n
4887 then (
4888 if i = 2
4889 then
4890 let incr =
4891 match n with
4892 | 5 ->
4893 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4894 | _ ->
4895 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4897 let zoom = conf.zoom -. incr in
4898 setzoom zoom;
4899 state.mstate <- Mzoom (n, 0);
4900 else
4901 state.mstate <- Mzoom (n, i+1);
4903 else state.mstate <- Mzoom (n, 0)
4905 | _ -> state.mstate <- Mzoom (n, 0)
4907 else (
4908 match state.autoscroll with
4909 | Some step -> setautoscrollspeed step (n=4)
4910 | None ->
4911 let incr =
4912 if n = 4
4913 then -conf.scrollstep
4914 else conf.scrollstep
4916 let incr = incr * 2 in
4917 let y = clamp incr in
4918 gotoy_and_clear_text y
4921 | 1 when Wsi.withctrl mask ->
4922 if down
4923 then (
4924 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
4925 state.mstate <- Mpan (x, y)
4927 else
4928 state.mstate <- Mnone
4930 | 3 ->
4931 if down
4932 then (
4933 Wsi.setcursor Wsi.CURSOR_CYCLE;
4934 let p = (x, y) in
4935 state.mstate <- Mzoomrect (p, p)
4937 else (
4938 match state.mstate with
4939 | Mzoomrect ((x0, y0), _) ->
4940 if abs (x-x0) > 10 && abs (y - y0) > 10
4941 then zoomrect x0 y0 x y
4942 else (
4943 state.mstate <- Mnone;
4944 Wsi.setcursor Wsi.CURSOR_INHERIT;
4945 G.postRedisplay "kill accidental zoom rect";
4947 | _ ->
4948 Wsi.setcursor Wsi.CURSOR_INHERIT;
4949 state.mstate <- Mnone
4952 | 1 when x > conf.winw - state.scrollw ->
4953 if down
4954 then
4955 let _, position, sh = state.uioh#scrollph in
4956 if y > truncate position && y < truncate (position +. sh)
4957 then state.mstate <- Mscrolly
4958 else scrolly y
4959 else
4960 state.mstate <- Mnone
4962 | 1 when y > conf.winh - state.hscrollh ->
4963 if down
4964 then
4965 let _, position, sw = state.uioh#scrollpw in
4966 if x > truncate position && x < truncate (position +. sw)
4967 then state.mstate <- Mscrollx
4968 else scrollx x
4969 else
4970 state.mstate <- Mnone
4972 | 1 ->
4973 let dest = if down then getunder x y else Unone in
4974 begin match dest with
4975 | Ulinkgoto (pageno, top) ->
4976 if pageno >= 0
4977 then (
4978 addnav ();
4979 gotopage1 pageno top;
4982 | Ulinkuri s ->
4983 gotouri s
4985 | Uremote (filename, pageno) ->
4986 let path =
4987 if Sys.file_exists filename
4988 then filename
4989 else
4990 let dir = Filename.dirname state.path in
4991 let path = Filename.concat dir filename in
4992 if Sys.file_exists path
4993 then path
4994 else ""
4996 if String.length path > 0
4997 then (
4998 let anchor = getanchor () in
4999 let ranchor = state.path, state.password, anchor in
5000 state.anchor <- (pageno, 0.0);
5001 state.ranchors <- ranchor :: state.ranchors;
5002 opendoc path "";
5004 else showtext '!' ("Could not find " ^ filename)
5006 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
5008 | Unone when down ->
5009 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5010 state.mstate <- Mpan (x, y);
5012 | Unone | Utext _ ->
5013 if down
5014 then (
5015 if conf.angle mod 360 = 0
5016 then (
5017 state.mstate <- Msel ((x, y), (x, y));
5018 G.postRedisplay "mouse select";
5021 else (
5022 match state.mstate with
5023 | Mnone -> ()
5025 | Mzoom _ | Mscrollx | Mscrolly ->
5026 state.mstate <- Mnone
5028 | Mzoomrect ((x0, y0), _) ->
5029 zoomrect x0 y0 x y
5031 | Mpan _ ->
5032 Wsi.setcursor Wsi.CURSOR_INHERIT;
5033 state.mstate <- Mnone
5035 | Msel ((_, y0), (_, y1)) ->
5036 let rec loop = function
5037 | [] -> ()
5038 | l :: rest ->
5039 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5040 || ((y1 >= l.pagedispy
5041 && y1 <= (l.pagedispy + l.pagevh)))
5042 then
5043 match getopaque l.pageno with
5044 | Some opaque ->
5045 copysel conf.selcmd opaque;
5046 G.postRedisplay "copysel"
5047 | _ -> ()
5048 else loop rest
5050 loop state.layout;
5051 Wsi.setcursor Wsi.CURSOR_INHERIT;
5052 state.mstate <- Mnone;
5056 | _ -> ()
5059 let birdseyemouse button down x y mask
5060 (conf, leftx, _, hooverpageno, anchor) =
5061 match button with
5062 | 1 when down ->
5063 let rec loop = function
5064 | [] -> ()
5065 | l :: rest ->
5066 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5067 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5068 then (
5069 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5071 else loop rest
5073 loop state.layout
5074 | 3 -> ()
5075 | _ -> viewmouse button down x y mask
5078 let mouse button down x y mask =
5079 state.uioh <- state.uioh#button button down x y mask;
5082 let motion ~x ~y =
5083 state.uioh <- state.uioh#motion x y
5086 let pmotion ~x ~y =
5087 state.uioh <- state.uioh#pmotion x y;
5090 let uioh = object
5091 method display = ()
5093 method key key mask =
5094 begin match state.mode with
5095 | Textentry textentry -> textentrykeyboard key mask textentry
5096 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5097 | View -> viewkeyboard key mask
5098 end;
5099 state.uioh
5101 method button button bstate x y mask =
5102 begin match state.mode with
5103 | View -> viewmouse button bstate x y mask
5104 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5105 | Textentry _ -> ()
5106 end;
5107 state.uioh
5109 method motion x y =
5110 begin match state.mode with
5111 | Textentry _ -> ()
5112 | View | Birdseye _ ->
5113 match state.mstate with
5114 | Mzoom _ | Mnone -> ()
5116 | Mpan (x0, y0) ->
5117 let dx = x - x0
5118 and dy = y0 - y in
5119 state.mstate <- Mpan (x, y);
5120 if conf.zoom > 1.0 then state.x <- state.x + dx;
5121 let y = clamp dy in
5122 gotoy_and_clear_text y
5124 | Msel (a, _) ->
5125 state.mstate <- Msel (a, (x, y));
5126 G.postRedisplay "motion select";
5128 | Mscrolly ->
5129 let y = min conf.winh (max 0 y) in
5130 scrolly y
5132 | Mscrollx ->
5133 let x = min conf.winw (max 0 x) in
5134 scrollx x
5136 | Mzoomrect (p0, _) ->
5137 state.mstate <- Mzoomrect (p0, (x, y));
5138 G.postRedisplay "motion zoomrect";
5139 end;
5140 state.uioh
5142 method pmotion x y =
5143 begin match state.mode with
5144 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5145 let rec loop = function
5146 | [] ->
5147 if hooverpageno != -1
5148 then (
5149 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5150 G.postRedisplay "pmotion birdseye no hoover";
5152 | l :: rest ->
5153 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5154 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5155 then (
5156 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5157 G.postRedisplay "pmotion birdseye hoover";
5159 else loop rest
5161 loop state.layout
5163 | Textentry _ -> ()
5165 | View ->
5166 match state.mstate with
5167 | Mnone -> updateunder x y
5168 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5170 end;
5171 state.uioh
5173 method infochanged _ = ()
5175 method scrollph =
5176 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5177 let p, h = scrollph state.y maxy in
5178 state.scrollw, p, h
5180 method scrollpw =
5181 let winw = conf.winw - state.scrollw - 1 in
5182 let fwinw = float winw in
5183 let sw =
5184 let sw = fwinw /. float state.w in
5185 let sw = fwinw *. sw in
5186 max sw (float conf.scrollh)
5188 let position, sw =
5189 let f = state.w+winw in
5190 let r = float (winw-state.x) /. float f in
5191 let p = fwinw *. r in
5192 p-.sw/.2., sw
5194 let sw =
5195 if position +. sw > fwinw
5196 then fwinw -. position
5197 else sw
5199 state.hscrollh, position, sw
5201 method modehash =
5202 let modename =
5203 match state.mode with
5204 | Textentry _ -> "textentry"
5205 | Birdseye _ -> "birdseye"
5206 | View -> "global"
5208 findkeyhash conf modename
5209 end;;
5211 module Config =
5212 struct
5213 open Parser
5215 let fontpath = ref "";;
5217 module KeyMap =
5218 Map.Make (struct type t = (int * int) let compare = compare end);;
5220 let unent s =
5221 let l = String.length s in
5222 let b = Buffer.create l in
5223 unent b s 0 l;
5224 Buffer.contents b;
5227 let home =
5228 try Sys.getenv "HOME"
5229 with exn ->
5230 prerr_endline
5231 ("Can not determine home directory location: " ^
5232 Printexc.to_string exn);
5236 let modifier_of_string = function
5237 | "alt" -> Wsi.altmask
5238 | "shift" -> Wsi.shiftmask
5239 | "ctrl" | "control" -> Wsi.ctrlmask
5240 | "meta" -> Wsi.metamask
5241 | _ -> 0
5244 let key_of_string =
5245 let r = Str.regexp "-" in
5246 fun s ->
5247 let elems = Str.full_split r s in
5248 let f n k m =
5249 let g s =
5250 let m1 = modifier_of_string s in
5251 if m1 = 0
5252 then (Wsi.namekey s, m)
5253 else (k, m lor m1)
5254 in function
5255 | Str.Delim s when n land 1 = 0 -> g s
5256 | Str.Text s -> g s
5257 | Str.Delim _ -> (k, m)
5259 let rec loop n k m = function
5260 | [] -> (k, m)
5261 | x :: xs ->
5262 let k, m = f n k m x in
5263 loop (n+1) k m xs
5265 loop 0 0 0 elems
5268 let keys_of_string =
5269 let r = Str.regexp "[ \t]" in
5270 fun s ->
5271 let elems = Str.split r s in
5272 List.map key_of_string elems
5275 let copykeyhashes c =
5276 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5279 let config_of c attrs =
5280 let apply c k v =
5282 match k with
5283 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5284 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5285 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5286 | "preload" -> { c with preload = bool_of_string v }
5287 | "page-bias" -> { c with pagebias = int_of_string v }
5288 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5289 | "auto-scroll-step" ->
5290 { c with autoscrollstep = max 0 (int_of_string v) }
5291 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5292 | "crop-hack" -> { c with crophack = bool_of_string v }
5293 | "throttle" ->
5294 let mw =
5295 match String.lowercase v with
5296 | "true" -> Some infinity
5297 | "false" -> None
5298 | f -> Some (float_of_string f)
5300 { c with maxwait = mw}
5301 | "highlight-links" -> { c with hlinks = bool_of_string v }
5302 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5303 | "vertical-margin" ->
5304 { c with interpagespace = max 0 (int_of_string v) }
5305 | "zoom" ->
5306 let zoom = float_of_string v /. 100. in
5307 let zoom = max zoom 0.0 in
5308 { c with zoom = zoom }
5309 | "presentation" -> { c with presentation = bool_of_string v }
5310 | "rotation-angle" -> { c with angle = int_of_string v }
5311 | "width" -> { c with winw = max 20 (int_of_string v) }
5312 | "height" -> { c with winh = max 20 (int_of_string v) }
5313 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5314 | "proportional-display" -> { c with proportional = bool_of_string v }
5315 | "pixmap-cache-size" ->
5316 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5317 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5318 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5319 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5320 | "persistent-location" -> { c with jumpback = bool_of_string v }
5321 | "background-color" -> { c with bgcolor = color_of_string v }
5322 | "scrollbar-in-presentation" ->
5323 { c with scrollbarinpm = bool_of_string v }
5324 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5325 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5326 | "mupdf-store-size" ->
5327 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5328 | "checkers" -> { c with checkers = bool_of_string v }
5329 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5330 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5331 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5332 | "uri-launcher" -> { c with urilauncher = unent v }
5333 | "path-launcher" -> { c with pathlauncher = unent v }
5334 | "color-space" -> { c with colorspace = colorspace_of_string v }
5335 | "invert-colors" -> { c with invert = bool_of_string v }
5336 | "brightness" -> { c with colorscale = float_of_string v }
5337 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5338 | "ghyllscroll" ->
5339 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5340 | "columns" ->
5341 let nab = columns_of_string v in
5342 { c with columns = Some (nab, [||]) }
5343 | "birds-eye-columns" ->
5344 { c with beyecolumns = Some (max (int_of_string v) 2) }
5345 | "selection-command" -> { c with selcmd = unent v }
5346 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5347 | _ -> c
5348 with exn ->
5349 prerr_endline ("Error processing attribute (`" ^
5350 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5353 let rec fold c = function
5354 | [] -> c
5355 | (k, v) :: rest ->
5356 let c = apply c k v in
5357 fold c rest
5359 fold { c with keyhashes = copykeyhashes c } attrs;
5362 let fromstring f pos n v d =
5363 try f v
5364 with exn ->
5365 dolog "Error processing attribute (%S=%S) at %d\n%s"
5366 n v pos (Printexc.to_string exn)
5371 let bookmark_of attrs =
5372 let rec fold title page rely = function
5373 | ("title", v) :: rest -> fold v page rely rest
5374 | ("page", v) :: rest -> fold title v rely rest
5375 | ("rely", v) :: rest -> fold title page v rest
5376 | _ :: rest -> fold title page rely rest
5377 | [] -> title, page, rely
5379 fold "invalid" "0" "0" attrs
5382 let doc_of attrs =
5383 let rec fold path page rely pan = function
5384 | ("path", v) :: rest -> fold v page rely pan rest
5385 | ("page", v) :: rest -> fold path v rely pan rest
5386 | ("rely", v) :: rest -> fold path page v pan rest
5387 | ("pan", v) :: rest -> fold path page rely v rest
5388 | _ :: rest -> fold path page rely pan rest
5389 | [] -> path, page, rely, pan
5391 fold "" "0" "0" "0" attrs
5394 let map_of attrs =
5395 let rec fold rs ls = function
5396 | ("out", v) :: rest -> fold v ls rest
5397 | ("in", v) :: rest -> fold rs v rest
5398 | _ :: rest -> fold ls rs rest
5399 | [] -> ls, rs
5401 fold "" "" attrs
5404 let setconf dst src =
5405 dst.scrollbw <- src.scrollbw;
5406 dst.scrollh <- src.scrollh;
5407 dst.icase <- src.icase;
5408 dst.preload <- src.preload;
5409 dst.pagebias <- src.pagebias;
5410 dst.verbose <- src.verbose;
5411 dst.scrollstep <- src.scrollstep;
5412 dst.maxhfit <- src.maxhfit;
5413 dst.crophack <- src.crophack;
5414 dst.autoscrollstep <- src.autoscrollstep;
5415 dst.maxwait <- src.maxwait;
5416 dst.hlinks <- src.hlinks;
5417 dst.underinfo <- src.underinfo;
5418 dst.interpagespace <- src.interpagespace;
5419 dst.zoom <- src.zoom;
5420 dst.presentation <- src.presentation;
5421 dst.angle <- src.angle;
5422 dst.winw <- src.winw;
5423 dst.winh <- src.winh;
5424 dst.savebmarks <- src.savebmarks;
5425 dst.memlimit <- src.memlimit;
5426 dst.proportional <- src.proportional;
5427 dst.texcount <- src.texcount;
5428 dst.sliceheight <- src.sliceheight;
5429 dst.thumbw <- src.thumbw;
5430 dst.jumpback <- src.jumpback;
5431 dst.bgcolor <- src.bgcolor;
5432 dst.scrollbarinpm <- src.scrollbarinpm;
5433 dst.tilew <- src.tilew;
5434 dst.tileh <- src.tileh;
5435 dst.mustoresize <- src.mustoresize;
5436 dst.checkers <- src.checkers;
5437 dst.aalevel <- src.aalevel;
5438 dst.trimmargins <- src.trimmargins;
5439 dst.trimfuzz <- src.trimfuzz;
5440 dst.urilauncher <- src.urilauncher;
5441 dst.colorspace <- src.colorspace;
5442 dst.invert <- src.invert;
5443 dst.colorscale <- src.colorscale;
5444 dst.redirectstderr <- src.redirectstderr;
5445 dst.ghyllscroll <- src.ghyllscroll;
5446 dst.columns <- src.columns;
5447 dst.beyecolumns <- src.beyecolumns;
5448 dst.selcmd <- src.selcmd;
5449 dst.updatecurs <- src.updatecurs;
5450 dst.pathlauncher <- src.pathlauncher;
5451 dst.keyhashes <- copykeyhashes src;
5454 let get s =
5455 let h = Hashtbl.create 10 in
5456 let dc = { defconf with angle = defconf.angle } in
5457 let rec toplevel v t spos _ =
5458 match t with
5459 | Vdata | Vcdata | Vend -> v
5460 | Vopen ("llppconfig", _, closed) ->
5461 if closed
5462 then v
5463 else { v with f = llppconfig }
5464 | Vopen _ ->
5465 error "unexpected subelement at top level" s spos
5466 | Vclose _ -> error "unexpected close at top level" s spos
5468 and llppconfig v t spos _ =
5469 match t with
5470 | Vdata | Vcdata -> v
5471 | Vend -> error "unexpected end of input in llppconfig" s spos
5472 | Vopen ("defaults", attrs, closed) ->
5473 let c = config_of dc attrs in
5474 setconf dc c;
5475 if closed
5476 then v
5477 else { v with f = defaults }
5479 | Vopen ("ui-font", attrs, closed) ->
5480 let rec getsize size = function
5481 | [] -> size
5482 | ("size", v) :: rest ->
5483 let size =
5484 fromstring int_of_string spos "size" v fstate.fontsize in
5485 getsize size rest
5486 | l -> getsize size l
5488 fstate.fontsize <- getsize fstate.fontsize attrs;
5489 if closed
5490 then v
5491 else { v with f = uifont (Buffer.create 10) }
5493 | Vopen ("doc", attrs, closed) ->
5494 let pathent, spage, srely, span = doc_of attrs in
5495 let path = unent pathent
5496 and pageno = fromstring int_of_string spos "page" spage 0
5497 and rely = fromstring float_of_string spos "rely" srely 0.0
5498 and pan = fromstring int_of_string spos "pan" span 0 in
5499 let c = config_of dc attrs in
5500 let anchor = (pageno, rely) in
5501 if closed
5502 then (Hashtbl.add h path (c, [], pan, anchor); v)
5503 else { v with f = doc path pan anchor c [] }
5505 | Vopen _ ->
5506 error "unexpected subelement in llppconfig" s spos
5508 | Vclose "llppconfig" -> { v with f = toplevel }
5509 | Vclose _ -> error "unexpected close in llppconfig" s spos
5511 and defaults v t spos _ =
5512 match t with
5513 | Vdata | Vcdata -> v
5514 | Vend -> error "unexpected end of input in defaults" s spos
5515 | Vopen ("keymap", attrs, closed) ->
5516 let modename =
5517 try List.assoc "mode" attrs
5518 with Not_found -> "global" in
5519 if closed
5520 then v
5521 else
5522 let ret keymap =
5523 let h = findkeyhash dc modename in
5524 KeyMap.iter (Hashtbl.replace h) keymap;
5525 defaults
5527 { v with f = pkeymap ret KeyMap.empty }
5529 | Vopen (_, _, _) ->
5530 error "unexpected subelement in defaults" s spos
5532 | Vclose "defaults" ->
5533 { v with f = llppconfig }
5535 | Vclose _ -> error "unexpected close in defaults" s spos
5537 and uifont b v t spos epos =
5538 match t with
5539 | Vdata | Vcdata ->
5540 Buffer.add_substring b s spos (epos - spos);
5542 | Vopen (_, _, _) ->
5543 error "unexpected subelement in ui-font" s spos
5544 | Vclose "ui-font" ->
5545 if String.length !fontpath = 0
5546 then fontpath := Buffer.contents b;
5547 { v with f = llppconfig }
5548 | Vclose _ -> error "unexpected close in ui-font" s spos
5549 | Vend -> error "unexpected end of input in ui-font" s spos
5551 and doc path pan anchor c bookmarks v t spos _ =
5552 match t with
5553 | Vdata | Vcdata -> v
5554 | Vend -> error "unexpected end of input in doc" s spos
5555 | Vopen ("bookmarks", _, closed) ->
5556 if closed
5557 then v
5558 else { v with f = pbookmarks path pan anchor c bookmarks }
5560 | Vopen ("keymap", attrs, closed) ->
5561 let modename =
5562 try List.assoc "mode" attrs
5563 with Not_found -> "global"
5565 if closed
5566 then v
5567 else
5568 let ret keymap =
5569 let h = findkeyhash c modename in
5570 KeyMap.iter (Hashtbl.replace h) keymap;
5571 doc path pan anchor c bookmarks
5573 { v with f = pkeymap ret KeyMap.empty }
5575 | Vopen (_, _, _) ->
5576 error "unexpected subelement in doc" s spos
5578 | Vclose "doc" ->
5579 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5580 { v with f = llppconfig }
5582 | Vclose _ -> error "unexpected close in doc" s spos
5584 and pkeymap ret keymap v t spos _ =
5585 match t with
5586 | Vdata | Vcdata -> v
5587 | Vend -> error "unexpected end of input in keymap" s spos
5588 | Vopen ("map", attrs, closed) ->
5589 let r, l = map_of attrs in
5590 let kss = fromstring keys_of_string spos "in" r [] in
5591 let lss = fromstring keys_of_string spos "out" l [] in
5592 let keymap =
5593 match kss with
5594 | [] -> keymap
5595 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5596 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5598 if closed
5599 then { v with f = pkeymap ret keymap }
5600 else
5601 let f () = v in
5602 { v with f = skip "map" f }
5604 | Vopen _ ->
5605 error "unexpected subelement in keymap" s spos
5607 | Vclose "keymap" ->
5608 { v with f = ret keymap }
5610 | Vclose _ -> error "unexpected close in keymap" s spos
5612 and pbookmarks path pan anchor c bookmarks v t spos _ =
5613 match t with
5614 | Vdata | Vcdata -> v
5615 | Vend -> error "unexpected end of input in bookmarks" s spos
5616 | Vopen ("item", attrs, closed) ->
5617 let titleent, spage, srely = bookmark_of attrs in
5618 let page = fromstring int_of_string spos "page" spage 0
5619 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5620 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5621 if closed
5622 then { v with f = pbookmarks path pan anchor c bookmarks }
5623 else
5624 let f () = v in
5625 { v with f = skip "item" f }
5627 | Vopen _ ->
5628 error "unexpected subelement in bookmarks" s spos
5630 | Vclose "bookmarks" ->
5631 { v with f = doc path pan anchor c bookmarks }
5633 | Vclose _ -> error "unexpected close in bookmarks" s spos
5635 and skip tag f v t spos _ =
5636 match t with
5637 | Vdata | Vcdata -> v
5638 | Vend ->
5639 error ("unexpected end of input in skipped " ^ tag) s spos
5640 | Vopen (tag', _, closed) ->
5641 if closed
5642 then v
5643 else
5644 let f' () = { v with f = skip tag f } in
5645 { v with f = skip tag' f' }
5646 | Vclose ctag ->
5647 if tag = ctag
5648 then f ()
5649 else error ("unexpected close in skipped " ^ tag) s spos
5652 parse { f = toplevel; accu = () } s;
5653 h, dc;
5656 let do_load f ic =
5658 let len = in_channel_length ic in
5659 let s = String.create len in
5660 really_input ic s 0 len;
5661 f s;
5662 with
5663 | Parse_error (msg, s, pos) ->
5664 let subs = subs s pos in
5665 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5666 failwith ("parse error: " ^ s)
5668 | exn ->
5669 failwith ("config load error: " ^ Printexc.to_string exn)
5672 let defconfpath =
5673 let dir =
5675 let dir = Filename.concat home ".config" in
5676 if Sys.is_directory dir then dir else home
5677 with _ -> home
5679 Filename.concat dir "llpp.conf"
5682 let confpath = ref defconfpath;;
5684 let load1 f =
5685 if Sys.file_exists !confpath
5686 then
5687 match
5688 (try Some (open_in_bin !confpath)
5689 with exn ->
5690 prerr_endline
5691 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5692 Printexc.to_string exn);
5693 None
5695 with
5696 | Some ic ->
5697 begin try
5698 f (do_load get ic)
5699 with exn ->
5700 prerr_endline
5701 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5702 Printexc.to_string exn);
5703 end;
5704 close_in ic;
5706 | None -> ()
5707 else
5708 f (Hashtbl.create 0, defconf)
5711 let load () =
5712 let f (h, dc) =
5713 let pc, pb, px, pa =
5715 Hashtbl.find h (Filename.basename state.path)
5716 with Not_found -> dc, [], 0, (0, 0.0)
5718 setconf defconf dc;
5719 setconf conf pc;
5720 state.bookmarks <- pb;
5721 state.x <- px;
5722 state.scrollw <- conf.scrollbw;
5723 if conf.jumpback
5724 then state.anchor <- pa;
5725 cbput state.hists.nav pa;
5727 load1 f
5730 let add_attrs bb always dc c =
5731 let ob s a b =
5732 if always || a != b
5733 then Printf.bprintf bb "\n %s='%b'" s a
5734 and oi s a b =
5735 if always || a != b
5736 then Printf.bprintf bb "\n %s='%d'" s a
5737 and oI s a b =
5738 if always || a != b
5739 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5740 and oz s a b =
5741 if always || a <> b
5742 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5743 and oF s a b =
5744 if always || a <> b
5745 then Printf.bprintf bb "\n %s='%f'" s a
5746 and oc s a b =
5747 if always || a <> b
5748 then
5749 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5750 and oC s a b =
5751 if always || a <> b
5752 then
5753 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5754 and oR s a b =
5755 if always || a <> b
5756 then
5757 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5758 and os s a b =
5759 if always || a <> b
5760 then
5761 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5762 and og s a b =
5763 if always || a <> b
5764 then
5765 match a with
5766 | None -> ()
5767 | Some (_N, _A, _B) ->
5768 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5769 and oW s a b =
5770 if always || a <> b
5771 then
5772 let v =
5773 match a with
5774 | None -> "false"
5775 | Some f ->
5776 if f = infinity
5777 then "true"
5778 else string_of_float f
5780 Printf.bprintf bb "\n %s='%s'" s v
5781 and oco s a b =
5782 if always || a <> b
5783 then
5784 match a with
5785 | Some ((n, a, b), _) when n > 1 ->
5786 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5787 | _ -> ()
5788 and obeco s a b =
5789 if always || a <> b
5790 then
5791 match a with
5792 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5793 | _ -> ()
5795 let w, h =
5796 if always
5797 then dc.winw, dc.winh
5798 else
5799 match state.fullscreen with
5800 | Some wh -> wh
5801 | None -> c.winw, c.winh
5803 let zoom, presentation, interpagespace, maxwait =
5804 if always
5805 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5806 else
5807 match state.mode with
5808 | Birdseye (bc, _, _, _, _) ->
5809 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5810 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5812 oi "width" w dc.winw;
5813 oi "height" h dc.winh;
5814 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5815 oi "scroll-handle-height" c.scrollh dc.scrollh;
5816 ob "case-insensitive-search" c.icase dc.icase;
5817 ob "preload" c.preload dc.preload;
5818 oi "page-bias" c.pagebias dc.pagebias;
5819 oi "scroll-step" c.scrollstep dc.scrollstep;
5820 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5821 ob "max-height-fit" c.maxhfit dc.maxhfit;
5822 ob "crop-hack" c.crophack dc.crophack;
5823 oW "throttle" maxwait dc.maxwait;
5824 ob "highlight-links" c.hlinks dc.hlinks;
5825 ob "under-cursor-info" c.underinfo dc.underinfo;
5826 oi "vertical-margin" interpagespace dc.interpagespace;
5827 oz "zoom" zoom dc.zoom;
5828 ob "presentation" presentation dc.presentation;
5829 oi "rotation-angle" c.angle dc.angle;
5830 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5831 ob "proportional-display" c.proportional dc.proportional;
5832 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5833 oi "tex-count" c.texcount dc.texcount;
5834 oi "slice-height" c.sliceheight dc.sliceheight;
5835 oi "thumbnail-width" c.thumbw dc.thumbw;
5836 ob "persistent-location" c.jumpback dc.jumpback;
5837 oc "background-color" c.bgcolor dc.bgcolor;
5838 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5839 oi "tile-width" c.tilew dc.tilew;
5840 oi "tile-height" c.tileh dc.tileh;
5841 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5842 ob "checkers" c.checkers dc.checkers;
5843 oi "aalevel" c.aalevel dc.aalevel;
5844 ob "trim-margins" c.trimmargins dc.trimmargins;
5845 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5846 os "uri-launcher" c.urilauncher dc.urilauncher;
5847 os "path-launcher" c.pathlauncher dc.pathlauncher;
5848 oC "color-space" c.colorspace dc.colorspace;
5849 ob "invert-colors" c.invert dc.invert;
5850 oF "brightness" c.colorscale dc.colorscale;
5851 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5852 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
5853 oco "columns" c.columns dc.columns;
5854 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
5855 os "selection-command" c.selcmd dc.selcmd;
5856 ob "update-cursor" c.updatecurs dc.updatecurs;
5859 let keymapsbuf always dc c =
5860 let bb = Buffer.create 16 in
5861 List.iter (fun (modename, h) ->
5862 let dh = findkeyhash dc modename in
5863 if always || h <> dh
5864 then (
5865 if Hashtbl.length h > 0
5866 then (
5867 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
5868 Hashtbl.iter (fun i o ->
5869 let isdifferent = always ||
5871 let dO = Hashtbl.find dh i in
5872 dO <> o
5873 with Not_found -> true
5875 if isdifferent
5876 then
5877 let addkm (k, m) =
5878 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
5879 if Wsi.withalt m then Buffer.add_string bb "alt-";
5880 if Wsi.withshift m then Buffer.add_string bb "shift-";
5881 if Wsi.withmeta m then Buffer.add_string bb "meta-";
5882 Buffer.add_string bb (Wsi.keyname k);
5884 let addkms l =
5885 let rec loop = function
5886 | [] -> ()
5887 | km :: [] -> addkm km
5888 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
5890 loop l
5892 Buffer.add_string bb "<map in='";
5893 addkm i;
5894 match o with
5895 | KMinsrt km ->
5896 Buffer.add_char bb '\'';
5897 Buffer.add_string bb " out='";
5898 addkm km;
5899 Buffer.add_string bb "'/>\n"
5901 | KMinsrl kms ->
5902 Buffer.add_char bb '\'';
5903 Buffer.add_string bb " out='";
5904 addkms kms;
5905 Buffer.add_string bb "'/>\n"
5907 | KMmulti (ins, kms) ->
5908 Buffer.add_char bb ' ';
5909 addkms ins;
5910 Buffer.add_char bb '\'';
5911 Buffer.add_string bb " out='";
5912 addkms kms;
5913 Buffer.add_string bb "'/>\n"
5914 ) h;
5915 Buffer.add_string bb "</keymap>";
5918 ) c.keyhashes;
5922 let save () =
5923 let uifontsize = fstate.fontsize in
5924 let bb = Buffer.create 32768 in
5925 let f (h, dc) =
5926 let dc = if conf.bedefault then conf else dc in
5927 Buffer.add_string bb "<llppconfig>\n";
5929 if String.length !fontpath > 0
5930 then
5931 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5932 uifontsize
5933 !fontpath
5934 else (
5935 if uifontsize <> 14
5936 then
5937 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5940 Buffer.add_string bb "<defaults ";
5941 add_attrs bb true dc dc;
5942 let kb = keymapsbuf true dc dc in
5943 if Buffer.length kb > 0
5944 then (
5945 Buffer.add_string bb ">\n";
5946 Buffer.add_buffer bb kb;
5947 Buffer.add_string bb "\n</defaults>\n";
5949 else Buffer.add_string bb "/>\n";
5951 let adddoc path pan anchor c bookmarks =
5952 if bookmarks == [] && c = dc && anchor = emptyanchor
5953 then ()
5954 else (
5955 Printf.bprintf bb "<doc path='%s'"
5956 (enent path 0 (String.length path));
5958 if anchor <> emptyanchor
5959 then (
5960 let n, y = anchor in
5961 Printf.bprintf bb " page='%d'" n;
5962 if y > 1e-6
5963 then
5964 Printf.bprintf bb " rely='%f'" y
5968 if pan != 0
5969 then Printf.bprintf bb " pan='%d'" pan;
5971 add_attrs bb false dc c;
5972 let kb = keymapsbuf false dc c in
5974 begin match bookmarks with
5975 | [] ->
5976 if Buffer.length kb > 0
5977 then (
5978 Buffer.add_string bb ">\n";
5979 Buffer.add_buffer bb kb;
5980 Buffer.add_string bb "</doc>\n";
5982 else Buffer.add_string bb "/>\n"
5983 | _ ->
5984 Buffer.add_string bb ">\n<bookmarks>\n";
5985 List.iter (fun (title, _level, (page, rely)) ->
5986 Printf.bprintf bb
5987 "<item title='%s' page='%d'"
5988 (enent title 0 (String.length title))
5989 page
5991 if rely > 1e-6
5992 then
5993 Printf.bprintf bb " rely='%f'" rely
5995 Buffer.add_string bb "/>\n";
5996 ) bookmarks;
5997 Buffer.add_string bb "</bookmarks>";
5998 if Buffer.length kb > 0
5999 then (
6000 Buffer.add_string bb "\n";
6001 Buffer.add_buffer bb kb;
6003 Buffer.add_string bb "\n</doc>\n";
6004 end;
6008 let pan, conf =
6009 match state.mode with
6010 | Birdseye (c, pan, _, _, _) ->
6011 let beyecolumns =
6012 match conf.columns with
6013 | Some ((c, _, _), _) -> Some c
6014 | None -> None
6015 and columns =
6016 match c.columns with
6017 | Some (c, _) -> Some (c, [||])
6018 | None -> None
6020 pan, { c with beyecolumns = beyecolumns; columns = columns }
6021 | _ -> state.x, conf
6023 let basename = Filename.basename state.path in
6024 adddoc basename pan (getanchor ())
6025 { conf with
6026 autoscrollstep =
6027 match state.autoscroll with
6028 | Some step -> step
6029 | None -> conf.autoscrollstep }
6030 (if conf.savebmarks then state.bookmarks else []);
6032 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6033 if basename <> path
6034 then adddoc path x y c bookmarks
6035 ) h;
6036 Buffer.add_string bb "</llppconfig>";
6038 load1 f;
6039 if Buffer.length bb > 0
6040 then
6042 let tmp = !confpath ^ ".tmp" in
6043 let oc = open_out_bin tmp in
6044 Buffer.output_buffer oc bb;
6045 close_out oc;
6046 Unix.rename tmp !confpath;
6047 with exn ->
6048 prerr_endline
6049 ("error while saving configuration: " ^ Printexc.to_string exn)
6051 end;;
6053 let () =
6054 Arg.parse
6055 (Arg.align
6056 [("-p", Arg.String (fun s -> state.password <- s) ,
6057 "<password> Set password");
6059 ("-f", Arg.String (fun s -> Config.fontpath := s),
6060 "<path> Set path to the user interface font");
6062 ("-c", Arg.String (fun s -> Config.confpath := s),
6063 "<path> Set path to the configuration file");
6065 ("-v", Arg.Unit (fun () ->
6066 Printf.printf
6067 "%s\nconfiguration path: %s\n"
6068 (version ())
6069 Config.defconfpath
6071 exit 0), " Print version and exit");
6074 (fun s -> state.path <- s)
6075 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6077 if String.length state.path = 0
6078 then (prerr_endline "file name missing"; exit 1);
6080 Config.load ();
6082 let globalkeyhash = findkeyhash conf "global" in
6083 state.wsfd <- Wsi.init (object
6084 method display = display ()
6085 method reshape w h = reshape w h
6086 method mouse b d x y m = mouse b d x y m
6087 method motion x y = state.mpos <- (x, y); motion x y
6088 method pmotion x y = state.mpos <- (x, y); pmotion x y
6089 method key k m =
6090 match state.keystate with
6091 | KSnone ->
6092 let km = k, m in
6093 begin
6094 match
6095 try Hashtbl.find globalkeyhash km
6096 with Not_found ->
6097 let modehash = state.uioh#modehash in
6098 try Hashtbl.find modehash km
6099 with Not_found -> KMinsrt (k, m)
6100 with
6101 | KMinsrt (k, m) -> keyboard k m
6102 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6103 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6105 | KSinto ((k', m') :: [], insrt) when k'=k && m' land m = m' ->
6106 List.iter (fun (k, m) -> keyboard k m) insrt;
6107 state.keystate <- KSnone
6108 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land m = m' ->
6109 state.keystate <- KSinto (keys, insrt)
6110 | _ ->
6111 state.keystate <- KSnone
6113 method enter x y = state.mpos <- (x, y); pmotion x y
6114 method leave = state.mpos <- (-1, -1)
6115 end) conf.winw conf.winh;
6117 if not (
6118 List.exists GlMisc.check_extension
6119 [ "GL_ARB_texture_rectangle"
6120 ; "GL_EXT_texture_recangle"
6121 ; "GL_NV_texture_rectangle" ]
6123 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6125 let cr, sw = Unix.pipe ()
6126 and sr, cw = Unix.pipe () in
6128 cloexec cr;
6129 cloexec sw;
6130 cloexec sr;
6131 cloexec cw;
6133 setcheckers conf.checkers;
6134 redirectstderr ();
6136 init (cr, cw) (
6137 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6138 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6139 !Config.fontpath
6141 state.sr <- sr;
6142 state.sw <- sw;
6143 state.text <- "Opening " ^ state.path;
6144 setaalevel conf.aalevel;
6145 writeopen state.path state.password;
6146 state.uioh <- uioh;
6147 setfontsize fstate.fontsize;
6148 doreshape conf.winw conf.winh;
6150 let rec loop deadline =
6151 let r =
6152 match state.errfd with
6153 | None -> [state.sr; state.wsfd]
6154 | Some fd -> [state.sr; state.wsfd; fd]
6156 if state.redisplay
6157 then (
6158 state.redisplay <- false;
6159 display ();
6161 let timeout =
6162 let now = now () in
6163 if deadline > now
6164 then (
6165 if deadline = infinity
6166 then ~-.1.0
6167 else max 0.0 (deadline -. now)
6169 else 0.0
6171 let r, _, _ =
6172 try Unix.select r [] [] timeout
6173 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6175 begin match r with
6176 | [] ->
6177 state.ghyll None;
6178 let newdeadline =
6179 if state.ghyll == noghyll
6180 then
6181 match state.autoscroll with
6182 | Some step when step != 0 ->
6183 let y = state.y + step in
6184 let y =
6185 if y < 0
6186 then state.maxy
6187 else if y >= state.maxy then 0 else y
6189 gotoy y;
6190 if state.mode = View
6191 then state.text <- "";
6192 deadline +. 0.01
6193 | _ -> infinity
6194 else deadline +. 0.01
6196 loop newdeadline
6198 | l ->
6199 let rec checkfds = function
6200 | [] -> ()
6201 | fd :: rest when fd = state.sr ->
6202 let cmd = readcmd state.sr in
6203 act cmd;
6204 checkfds rest
6206 | fd :: rest when fd = state.wsfd ->
6207 Wsi.readresp fd;
6208 checkfds rest
6210 | fd :: rest ->
6211 let s = String.create 80 in
6212 let n = Unix.read fd s 0 80 in
6213 if conf.redirectstderr
6214 then (
6215 Buffer.add_substring state.errmsgs s 0 n;
6216 state.newerrmsgs <- true;
6217 state.redisplay <- true;
6219 else (
6220 prerr_string (String.sub s 0 n);
6221 flush stderr;
6223 checkfds rest
6225 checkfds l;
6226 let newdeadline =
6227 let deadline1 =
6228 if deadline = infinity
6229 then now () +. 0.01
6230 else deadline
6232 match state.autoscroll with
6233 | Some step when step != 0 -> deadline1
6234 | _ -> if state.ghyll == noghyll then infinity else deadline1
6236 loop newdeadline
6237 end;
6240 loop infinity;
6241 with Wsi.Quit ->
6242 wcmd "quit" [];
6243 Config.save ();
6244 exit 0;