buildall.sh: allow overriding mupdfrev
[llpp.git] / main.ml
blob889a343633fe40ba5e1a1568d468feea0a229010
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of launchcommand
12 | Unamed of destname
13 | Uremote of (filename * pageno)
14 | Uremotedest of (filename * destname)
15 and facename = string
16 and launchcommand = string
17 and filename = string
18 and pageno = int
19 and destname = string;;
21 type mark =
22 | Mark_page
23 | Mark_block
24 | Mark_line
25 | Mark_word
28 module Opaque :
29 sig
30 type t = private string
31 val of_string : string -> t
32 val to_string : t -> string
33 end
35 struct
36 type t = string
37 let of_string s = s
38 let to_string t = t
39 end
42 let (~<) = Opaque.of_string;;
43 let (~>) = Opaque.to_string;;
45 type params = (angle * fitmodel * trimparams
46 * texcount * sliceheight * memsize
47 * colorspace * fontpath * trimcachepath
48 * haspbo)
49 and width = int
50 and height = int
51 and leftx = int
52 and opaque = Opaque.t
53 and recttype = int
54 and pixmapsize = int
55 and angle = int
56 and trimmargins = bool
57 and interpagespace = int
58 and texcount = int
59 and sliceheight = int
60 and gen = int
61 and top = float
62 and dtop = float
63 and fontpath = string
64 and trimcachepath = string
65 and memsize = int
66 and aalevel = int
67 and irect = (int * int * int * int)
68 and trimparams = (trimmargins * irect)
69 and colorspace = | Rgb | Bgr | Gray
70 and fitmodel = | FitWidth | FitProportional | FitPage
71 and haspbo = bool
72 and uri = string
73 and caption = string
76 type x = int
77 and y = int
78 and tilex = int
79 and tiley = int
80 and tileparams = (x * y * width * height * tilex * tiley)
83 type link =
84 | Lnotfound
85 | Lfound of int
86 and linkdir =
87 | LDfirst
88 | LDlast
89 | LDfirstvisible of (int * int * int)
90 | LDleft of int
91 | LDright of int
92 | LDdown of int
93 | LDup of int
96 type pagewithlinks =
97 | Pwlnotfound
98 | Pwl of int
101 type keymap =
102 | KMinsrt of key
103 | KMinsrl of key list
104 | KMmulti of key list * key list
105 and key = int * int
106 and keyhash = (key, keymap) Hashtbl.t
107 and keystate =
108 | KSnone
109 | KSinto of (key list * key list)
112 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
113 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
115 type pipe = (Unix.file_descr * Unix.file_descr);;
117 external init : pipe -> params -> unit = "ml_init";;
118 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
119 external hassel : opaque -> bool = "ml_hassel";;
120 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
121 external getpdimrect : int -> float array = "ml_getpdimrect";;
122 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
123 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
124 external clearmark : opaque -> unit = "ml_clearmark";;
125 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
126 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
127 external measurestr : int -> string -> float = "ml_measure_string";;
128 external postprocess :
129 opaque -> int -> int -> int -> (int * string * int) -> int
130 = "ml_postprocess";;
131 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
132 external platform : unit -> platform = "ml_platform";;
133 external setaalevel : int -> unit = "ml_setaalevel";;
134 external realloctexts : int -> bool = "ml_realloctexts";;
135 external findlink : opaque -> linkdir -> link = "ml_findlink";;
136 external getlink : opaque -> int -> under = "ml_getlink";;
137 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
138 external getlinkcount : opaque -> int = "ml_getlinkcount";;
139 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
140 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
141 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
142 external freepbo : opaque -> unit = "ml_freepbo";;
143 external unmappbo : opaque -> unit = "ml_unmappbo";;
144 external pbousable : unit -> bool = "ml_pbo_usable";;
145 external unproject : opaque -> int -> int -> (int * int) option
146 = "ml_unproject";;
147 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
148 external rectofblock : opaque -> int -> int -> float array option
149 = "ml_rectofblock";;
150 external fz_version : unit -> string = "ml_fz_version";;
151 external begintiles : unit -> unit = "ml_begintiles";;
152 external endtiles : unit -> unit = "ml_endtiles";;
154 let platform_to_string = function
155 | Punknown -> "unknown"
156 | Plinux -> "Linux"
157 | Posx -> "OSX"
158 | Psun -> "Sun"
159 | Pfreebsd -> "FreeBSD"
160 | Pdragonflybsd -> "DragonflyBSD"
161 | Popenbsd -> "OpenBSD"
162 | Pnetbsd -> "NetBSD"
163 | Pcygwin -> "Cygwin"
166 let platform = platform ();;
168 let now = Unix.gettimeofday;;
170 let selfexec = ref "";;
172 let popen cmd fda =
173 if platform = Pcygwin
174 then (
175 let sh = "/bin/sh" in
176 let args = [|sh; "-c"; cmd|] in
177 let rec std si so se = function
178 | [] -> si, so, se
179 | (fd, 0) :: rest -> std fd so se rest
180 | (fd, -1) :: rest ->
181 Unix.set_close_on_exec fd;
182 std si so se rest
183 | (_, n) :: _ ->
184 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
186 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
187 ignore (Unix.create_process sh args si so se)
189 else popen cmd fda;
192 type mpos = int * int
193 and mstate =
194 | Msel of (mpos * mpos)
195 | Mpan of mpos
196 | Mscrolly | Mscrollx
197 | Mzoom of (int * int)
198 | Mzoomrect of (mpos * mpos)
199 | Mnone
202 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
203 and onkey = string -> int -> te
204 and ondone = string -> unit
205 and histcancel = unit -> unit
206 and onhist = ((histcmd -> string) * histcancel)
207 and histcmd = HCnext | HCprev | HCfirst | HClast
208 and cancelonempty = bool
209 and te =
210 | TEstop
211 | TEdone of string
212 | TEcont of string
213 | TEswitch of textentry
216 type 'a circbuf =
217 { store : 'a array
218 ; mutable rc : int
219 ; mutable wc : int
220 ; mutable len : int
224 let bound v minv maxv =
225 max minv (min maxv v);
228 let cbnew n v =
229 { store = Array.create n v
230 ; rc = 0
231 ; wc = 0
232 ; len = 0
236 let cbcap b = Array.length b.store;;
238 let cbput b v =
239 let cap = cbcap b in
240 b.store.(b.wc) <- v;
241 b.wc <- (b.wc + 1) mod cap;
242 b.rc <- b.wc;
243 b.len <- min (b.len + 1) cap;
246 let cbempty b = b.len = 0;;
248 let cbgetg b circular dir =
249 if cbempty b
250 then b.store.(0)
251 else
252 let rc = b.rc + dir in
253 let rc =
254 if circular
255 then (
256 if rc = -1
257 then b.len-1
258 else (
259 if rc >= b.len
260 then 0
261 else rc
264 else bound rc 0 (b.len-1)
266 b.rc <- rc;
267 b.store.(rc);
270 let cbget b = cbgetg b false;;
271 let cbgetc b = cbgetg b true;;
273 let drawstring size x y s =
274 Gl.enable `blend;
275 Gl.enable `texture_2d;
276 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
277 ignore (drawstr size x y s);
278 Gl.disable `blend;
279 Gl.disable `texture_2d;
282 let drawstring1 size x y s =
283 drawstr size x y s;
286 let drawstring2 size x y fmt =
287 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
290 type page =
291 { pageno : int
292 ; pagedimno : int
293 ; pagew : int
294 ; pageh : int
295 ; pagex : int
296 ; pagey : int
297 ; pagevw : int
298 ; pagevh : int
299 ; pagedispx : int
300 ; pagedispy : int
301 ; pagecol : int
305 let debugl l =
306 dolog "l %d dim=%d {" l.pageno l.pagedimno;
307 dolog " WxH %dx%d" l.pagew l.pageh;
308 dolog " vWxH %dx%d" l.pagevw l.pagevh;
309 dolog " pagex,y %d,%d" l.pagex l.pagey;
310 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
311 dolog " column %d" l.pagecol;
312 dolog "}";
315 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
316 dolog "rect {";
317 dolog " x0,y0=(% f, % f)" x0 y0;
318 dolog " x1,y1=(% f, % f)" x1 y1;
319 dolog " x2,y2=(% f, % f)" x2 y2;
320 dolog " x3,y3=(% f, % f)" x3 y3;
321 dolog "}";
324 type multicolumns = multicol * pagegeom
325 and singlecolumn = pagegeom
326 and splitcolumns = columncount * pagegeom
327 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
328 and multicol = columncount * covercount * covercount
329 and pdimno = int
330 and columncount = int
331 and covercount = int;;
333 type scrollb = int;;
334 let scrollbvv = 1;;
335 let scrollbhv = 2;;
337 type conf =
338 { mutable scrollbw : int
339 ; mutable scrollh : int
340 ; mutable scrollb : scrollb
341 ; mutable icase : bool
342 ; mutable preload : bool
343 ; mutable pagebias : int
344 ; mutable verbose : bool
345 ; mutable debug : bool
346 ; mutable scrollstep : int
347 ; mutable hscrollstep : int
348 ; mutable maxhfit : bool
349 ; mutable crophack : bool
350 ; mutable autoscrollstep : int
351 ; mutable maxwait : float option
352 ; mutable hlinks : bool
353 ; mutable underinfo : bool
354 ; mutable interpagespace : interpagespace
355 ; mutable zoom : float
356 ; mutable presentation : bool
357 ; mutable angle : angle
358 ; mutable cwinw : int
359 ; mutable cwinh : int
360 ; mutable savebmarks : bool
361 ; mutable fitmodel : fitmodel
362 ; mutable trimmargins : trimmargins
363 ; mutable trimfuzz : irect
364 ; mutable memlimit : memsize
365 ; mutable texcount : texcount
366 ; mutable sliceheight : sliceheight
367 ; mutable thumbw : width
368 ; mutable jumpback : bool
369 ; mutable bgcolor : (float * float * float)
370 ; mutable bedefault : bool
371 ; mutable tilew : int
372 ; mutable tileh : int
373 ; mutable mustoresize : memsize
374 ; mutable checkers : bool
375 ; mutable aalevel : int
376 ; mutable urilauncher : string
377 ; mutable pathlauncher : string
378 ; mutable colorspace : colorspace
379 ; mutable invert : bool
380 ; mutable colorscale : float
381 ; mutable redirectstderr : bool
382 ; mutable ghyllscroll : (int * int * int) option
383 ; mutable columns : columns
384 ; mutable beyecolumns : columncount option
385 ; mutable selcmd : string
386 ; mutable paxcmd : string
387 ; mutable updatecurs : bool
388 ; mutable keyhashes : (string * keyhash) list
389 ; mutable hfsize : int
390 ; mutable pgscale : float
391 ; mutable usepbo : bool
392 ; mutable wheelbypage : bool
393 ; mutable stcmd : string
394 ; mutable riani : bool
395 ; mutable pax : (float * int * int) ref option
396 ; mutable paxmark : mark
398 and columns =
399 | Csingle of singlecolumn
400 | Cmulti of multicolumns
401 | Csplit of splitcolumns
404 type anchor = pageno * top * dtop;;
406 type outlinekind =
407 | Onone
408 | Oanchor of anchor
409 | Ouri of uri
410 | Olaunch of launchcommand
411 | Oremote of (filename * pageno)
412 | Oremotedest of (filename * destname)
413 and outline = (caption * outlinelevel * outlinekind)
414 and outlinelevel = int
417 type rect = float * float * float * float * float * float * float * float;;
419 type tile = opaque * pixmapsize * elapsed
420 and elapsed = float;;
421 type pagemapkey = pageno * gen;;
422 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
423 and row = int
424 and col = int;;
426 let emptyanchor = (0, 0.0, 0.0);;
428 type infochange = | Memused | Docinfo | Pdim;;
430 class type uioh = object
431 method display : unit
432 method key : int -> int -> uioh
433 method button : int -> bool -> int -> int -> int -> uioh
434 method multiclick : int -> int -> int -> int -> uioh
435 method motion : int -> int -> uioh
436 method pmotion : int -> int -> uioh
437 method infochanged : infochange -> unit
438 method scrollpw : (int * float * float)
439 method scrollph : (int * float * float)
440 method modehash : keyhash
441 method eformsgs : bool
442 end;;
444 type mode =
445 | Birdseye of (conf * leftx * pageno * pageno * anchor)
446 | Textentry of (textentry * onleave)
447 | View
448 | LinkNav of linktarget
449 and onleave = leavetextentrystatus -> unit
450 and leavetextentrystatus = | Cancel | Confirm
451 and helpitem = string * int * action
452 and action =
453 | Noaction
454 | Action of (uioh -> uioh)
455 and linktarget =
456 | Ltexact of (pageno * int)
457 | Ltgendir of int
460 let isbirdseye = function Birdseye _ -> true | _ -> false;;
461 let istextentry = function Textentry _ -> true | _ -> false;;
463 type currently =
464 | Idle
465 | Loading of (page * gen)
466 | Tiling of (
467 page * opaque * colorspace * angle * gen * col * row * width * height
469 | Outlining of outline list
472 let emptykeyhash = Hashtbl.create 0;;
473 let nouioh : uioh = object (self)
474 method display = ()
475 method key _ _ = self
476 method multiclick _ _ _ _ = self
477 method button _ _ _ _ _ = self
478 method motion _ _ = self
479 method pmotion _ _ = self
480 method infochanged _ = ()
481 method scrollpw = (0, nan, nan)
482 method scrollph = (0, nan, nan)
483 method modehash = emptykeyhash
484 method eformsgs = false
485 end;;
487 type state =
488 { mutable sr : Unix.file_descr
489 ; mutable sw : Unix.file_descr
490 ; mutable wsfd : Unix.file_descr
491 ; mutable errfd : Unix.file_descr option
492 ; mutable stderr : Unix.file_descr
493 ; mutable errmsgs : Buffer.t
494 ; mutable newerrmsgs : bool
495 ; mutable w : int
496 ; mutable x : int
497 ; mutable y : int
498 ; mutable anchor : anchor
499 ; mutable ranchors : (string * string * anchor * string) list
500 ; mutable maxy : int
501 ; mutable layout : page list
502 ; pagemap : (pagemapkey, opaque) Hashtbl.t
503 ; tilemap : (tilemapkey, tile) Hashtbl.t
504 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
505 ; mutable pdims : (pageno * width * height * leftx) list
506 ; mutable pagecount : int
507 ; mutable currently : currently
508 ; mutable mstate : mstate
509 ; mutable searchpattern : string
510 ; mutable rects : (pageno * recttype * rect) list
511 ; mutable rects1 : (pageno * recttype * rect) list
512 ; mutable text : string
513 ; mutable winstate : Wsi.winstate list
514 ; mutable mode : mode
515 ; mutable uioh : uioh
516 ; mutable outlines : outline array
517 ; mutable bookmarks : outline list
518 ; mutable path : string
519 ; mutable password : string
520 ; mutable nameddest : string
521 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
522 ; mutable memused : memsize
523 ; mutable gen : gen
524 ; mutable throttle : (page list * int * float) option
525 ; mutable autoscroll : int option
526 ; mutable ghyll : (int option -> unit)
527 ; mutable help : helpitem array
528 ; mutable docinfo : (int * string) list
529 ; mutable texid : GlTex.texture_id option
530 ; hists : hists
531 ; mutable prevzoom : (float * int)
532 ; mutable progress : float
533 ; mutable redisplay : bool
534 ; mutable mpos : mpos
535 ; mutable keystate : keystate
536 ; mutable glinks : bool
537 ; mutable prevcolumns : (columns * float) option
538 ; mutable winw : int
539 ; mutable winh : int
540 ; mutable reprf : (unit -> unit)
541 ; mutable origin : string
542 ; mutable roam : (unit -> unit)
543 ; mutable bzoom : bool
544 ; mutable traw : [`float] Raw.t
545 ; mutable vraw : [`float] Raw.t
547 and hists =
548 { pat : string circbuf
549 ; pag : string circbuf
550 ; nav : anchor circbuf
551 ; sel : string circbuf
555 let defconf =
556 { scrollbw = 7
557 ; scrollh = 12
558 ; scrollb = scrollbhv lor scrollbvv
559 ; icase = true
560 ; preload = true
561 ; pagebias = 0
562 ; verbose = false
563 ; debug = false
564 ; scrollstep = 24
565 ; hscrollstep = 24
566 ; maxhfit = true
567 ; crophack = false
568 ; autoscrollstep = 2
569 ; maxwait = None
570 ; hlinks = false
571 ; underinfo = false
572 ; interpagespace = 2
573 ; zoom = 1.0
574 ; presentation = false
575 ; angle = 0
576 ; cwinw = 900
577 ; cwinh = 900
578 ; savebmarks = true
579 ; fitmodel = FitProportional
580 ; trimmargins = false
581 ; trimfuzz = (0,0,0,0)
582 ; memlimit = 32 lsl 20
583 ; texcount = 256
584 ; sliceheight = 24
585 ; thumbw = 76
586 ; jumpback = true
587 ; bgcolor = (0.5, 0.5, 0.5)
588 ; bedefault = false
589 ; tilew = 2048
590 ; tileh = 2048
591 ; mustoresize = 256 lsl 20
592 ; checkers = true
593 ; aalevel = 8
594 ; urilauncher =
595 (match platform with
596 | Plinux | Pfreebsd | Pdragonflybsd
597 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
598 | Posx -> "open \"%s\""
599 | Pcygwin -> "cygstart \"%s\""
600 | Punknown -> "echo %s")
601 ; pathlauncher = "lp \"%s\""
602 ; selcmd =
603 (match platform with
604 | Plinux | Pfreebsd | Pdragonflybsd
605 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
606 | Posx -> "pbcopy"
607 | Pcygwin -> "wsel"
608 | Punknown -> "cat")
609 ; paxcmd = "cat"
610 ; colorspace = Rgb
611 ; invert = false
612 ; colorscale = 1.0
613 ; redirectstderr = false
614 ; ghyllscroll = None
615 ; columns = Csingle [||]
616 ; beyecolumns = None
617 ; updatecurs = false
618 ; hfsize = 12
619 ; pgscale = 1.0
620 ; usepbo = false
621 ; wheelbypage = false
622 ; stcmd = "echo SyncTex"
623 ; riani = false
624 ; pax = None
625 ; paxmark = Mark_word
626 ; keyhashes =
627 let mk n = (n, Hashtbl.create 1) in
628 [ mk "global"
629 ; mk "info"
630 ; mk "help"
631 ; mk "outline"
632 ; mk "listview"
633 ; mk "birdseye"
634 ; mk "textentry"
635 ; mk "links"
636 ; mk "view"
641 let wtmode = ref false;;
642 let cxack = ref false;;
644 let findkeyhash c name =
645 try List.assoc name c.keyhashes
646 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
649 let conf = { defconf with angle = defconf.angle };;
651 let pgscale h = truncate (float h *. conf.pgscale);;
653 type fontstate =
654 { mutable fontsize : int
655 ; mutable wwidth : float
656 ; mutable maxrows : int
660 let fstate =
661 { fontsize = 14
662 ; wwidth = nan
663 ; maxrows = -1
667 let geturl s =
668 let colonpos = try String.index s ':' with Not_found -> -1 in
669 let len = String.length s in
670 if colonpos >= 0 && colonpos + 3 < len
671 then (
672 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
673 then
674 let schemestartpos =
675 try String.rindex_from s colonpos ' '
676 with Not_found -> -1
678 let scheme =
679 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
681 match scheme with
682 | "http" | "ftp" | "mailto" ->
683 let epos =
684 try String.index_from s colonpos ' '
685 with Not_found -> len
687 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
688 | _ -> ""
689 else ""
691 else ""
694 let gotouri uri =
695 if emptystr conf.urilauncher
696 then print_endline uri
697 else (
698 let url = geturl uri in
699 if emptystr url
700 then Printf.eprintf "obtained empty url from uri %S" uri
701 else
702 let re = Str.regexp "%s" in
703 let command = Str.global_replace re url conf.urilauncher in
704 try popen command []
705 with exn ->
706 Printf.eprintf
707 "failed to execute `%s': %s\n" command (exntos exn);
708 flush stderr;
712 let version () =
713 Printf.sprintf "llpp version %s, fitz %s, ocaml %s (%s/%dbit)"
714 Help.version (fz_version ()) Sys.ocaml_version
715 (platform_to_string platform) Sys.word_size
718 let makehelp () =
719 let strings = version () :: "" :: Help.keys in
720 Array.of_list (
721 List.map (fun s ->
722 let url = geturl s in
723 if nonemptystr url
724 then (s, 0, Action (fun u -> gotouri url; u))
725 else (s, 0, Noaction)
726 ) strings);
729 let noghyll _ = ();;
730 let firstgeomcmds = "", [];;
731 let noreprf () = ();;
732 let noroam () = ();;
734 let state =
735 { sr = Unix.stdin
736 ; sw = Unix.stdin
737 ; wsfd = Unix.stdin
738 ; errfd = None
739 ; stderr = Unix.stderr
740 ; errmsgs = Buffer.create 0
741 ; newerrmsgs = false
742 ; x = 0
743 ; y = 0
744 ; w = 0
745 ; anchor = emptyanchor
746 ; ranchors = []
747 ; layout = []
748 ; maxy = max_int
749 ; tilelru = Queue.create ()
750 ; pagemap = Hashtbl.create 10
751 ; tilemap = Hashtbl.create 10
752 ; pdims = []
753 ; pagecount = 0
754 ; currently = Idle
755 ; mstate = Mnone
756 ; rects = []
757 ; rects1 = []
758 ; text = ""
759 ; mode = View
760 ; winstate = []
761 ; searchpattern = ""
762 ; outlines = [||]
763 ; bookmarks = []
764 ; path = ""
765 ; password = ""
766 ; nameddest = ""
767 ; geomcmds = firstgeomcmds
768 ; hists =
769 { nav = cbnew 10 emptyanchor
770 ; pat = cbnew 10 ""
771 ; pag = cbnew 10 ""
772 ; sel = cbnew 10 ""
774 ; memused = 0
775 ; gen = 0
776 ; throttle = None
777 ; autoscroll = None
778 ; ghyll = noghyll
779 ; help = makehelp ()
780 ; docinfo = []
781 ; texid = None
782 ; prevzoom = (1.0, 0)
783 ; progress = -1.0
784 ; uioh = nouioh
785 ; redisplay = true
786 ; mpos = (-1, -1)
787 ; keystate = KSnone
788 ; glinks = false
789 ; prevcolumns = None
790 ; winw = -1
791 ; winh = -1
792 ; reprf = noreprf
793 ; origin = ""
794 ; roam = noroam
795 ; bzoom = false
796 ; traw = Raw.create_static `float 8
797 ; vraw = Raw.create_static `float 8
801 let hscrollh () =
802 if (conf.scrollb land scrollbhv = 0)
803 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
804 then 0
805 else conf.scrollbw
808 let vscrollw () =
809 if (conf.scrollb land scrollbvv = 0)
810 then 0
811 else conf.scrollbw
814 let wadjsb w = w - vscrollw ();;
816 let setfontsize n =
817 fstate.fontsize <- n;
818 fstate.wwidth <- measurestr fstate.fontsize "w";
819 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
822 let vlog fmt =
823 if conf.verbose
824 then
825 Printf.kprintf prerr_endline fmt
826 else
827 Printf.kprintf ignore fmt
830 let launchpath () =
831 if emptystr conf.pathlauncher
832 then print_endline state.path
833 else (
834 let re = Str.regexp "%s" in
835 let command = Str.global_replace re state.path conf.pathlauncher in
836 try popen command []
837 with exn ->
838 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
839 flush stderr;
843 module Ne = struct
844 type 'a t = | Res of 'a | Exn of exn;;
846 let res f =
847 try Res (f ())
848 with exn -> Exn exn
851 let clo fd f =
852 try tempfailureretry Unix.close fd
853 with exn -> f (exntos exn)
856 let dup fd =
857 try Res (tempfailureretry Unix.dup fd)
858 with exn -> Exn exn
861 let dup2 fd1 fd2 =
862 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
863 with exn -> Exn exn
865 end;;
867 let redirectstderr () =
868 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
869 if conf.redirectstderr
870 then
871 match Ne.res Unix.pipe with
872 | Ne.Exn exn ->
873 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
875 | Ne.Res (r, w) ->
876 begin match Ne.dup Unix.stderr with
877 | Ne.Exn exn ->
878 dolog "failed to dup stderr: %s" (exntos exn);
879 Ne.clo r (clofail "pipe/r");
880 Ne.clo w (clofail "pipe/w");
882 | Ne.Res dupstderr ->
883 begin match Ne.dup2 w Unix.stderr with
884 | Ne.Exn exn ->
885 dolog "failed to dup2 to stderr: %s" (exntos exn);
886 Ne.clo dupstderr (clofail "stderr duplicate");
887 Ne.clo r (clofail "redir pipe/r");
888 Ne.clo w (clofail "redir pipe/w");
890 | Ne.Res () ->
891 state.stderr <- dupstderr;
892 state.errfd <- Some r;
893 end;
895 else (
896 state.newerrmsgs <- false;
897 begin match state.errfd with
898 | Some fd ->
899 begin match Ne.dup2 state.stderr Unix.stderr with
900 | Ne.Exn exn ->
901 dolog "failed to dup2 original stderr: %s" (exntos exn)
902 | Ne.Res () ->
903 Ne.clo fd (clofail "dup of stderr");
904 state.errfd <- None;
905 end;
906 | None -> ()
907 end;
908 prerr_string (Buffer.contents state.errmsgs);
909 flush stderr;
910 Buffer.clear state.errmsgs;
914 module G =
915 struct
916 let postRedisplay who =
917 if conf.verbose
918 then prerr_endline ("redisplay for " ^ who);
919 state.redisplay <- true;
921 end;;
923 let getopaque pageno =
924 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
925 with Not_found -> None
928 let putopaque pageno opaque =
929 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
932 let pagetranslatepoint l x y =
933 let dy = y - l.pagedispy in
934 let y = dy + l.pagey in
935 let dx = x - l.pagedispx in
936 let x = dx + l.pagex in
937 (x, y);
940 let onppundermouse g x y d =
941 let rec f = function
942 | l :: rest ->
943 begin match getopaque l.pageno with
944 | Some opaque ->
945 let x0 = l.pagedispx in
946 let x1 = x0 + l.pagevw in
947 let y0 = l.pagedispy in
948 let y1 = y0 + l.pagevh in
949 if y >= y0 && y <= y1 && x >= x0 && x <= x1
950 then
951 let px, py = pagetranslatepoint l x y in
952 match g opaque l px py with
953 | Some res -> res
954 | None -> f rest
955 else f rest
956 | _ ->
957 f rest
959 | [] -> d
961 f state.layout
964 let getunder x y =
965 let g opaque l px py =
966 if state.bzoom
967 then (
968 match rectofblock opaque px py with
969 | Some a ->
970 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
971 state.rects <- [l.pageno, l.pageno mod 3, rect];
972 G.postRedisplay "getunder";
973 | None -> ()
975 match whatsunder opaque px py with
976 | Unone -> None
977 | under -> Some under
979 onppundermouse g x y Unone
982 let unproject x y =
983 let g opaque l x y =
984 match unproject opaque x y with
985 | Some (x, y) -> Some (Some (l.pageno, x, y))
986 | None -> None
988 onppundermouse g x y None;
991 let showtext c s =
992 state.text <- Printf.sprintf "%c%s" c s;
993 G.postRedisplay "showtext";
996 let pipesel opaque cmd =
997 if hassel opaque
998 then
999 match Ne.res Unix.pipe with
1000 | Ne.Exn exn ->
1001 showtext '!'
1002 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
1003 | Ne.Res (r, w) ->
1004 let doclose what fd =
1005 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
1007 let popened =
1008 try popen cmd [r, 0; w, -1]; true
1009 with exn ->
1010 dolog "can not execute %S: %s" cmd (exntos exn);
1011 false
1013 if popened
1014 then (
1015 copysel w opaque;
1016 G.postRedisplay "pipesel";
1018 else doclose "pipesel pipe/w" w;
1019 doclose "pipesel pipe/r" r;
1022 let paxunder x y =
1023 let g opaque l px py =
1024 if markunder opaque px py conf.paxmark
1025 then (
1026 Some (fun () ->
1027 match getopaque l.pageno with
1028 | None -> ()
1029 | Some opaque -> pipesel opaque conf.paxcmd
1032 else None
1034 G.postRedisplay "paxunder";
1035 if conf.paxmark = Mark_page
1036 then
1037 List.iter (fun l ->
1038 match getopaque l.pageno with
1039 | None -> ()
1040 | Some opaque -> clearmark opaque) state.layout;
1041 state.roam <-
1042 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
1045 let selstring s =
1046 match Ne.res Unix.pipe with
1047 | Ne.Exn exn ->
1048 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
1049 | Ne.Res (r, w) ->
1050 let clo cap fd =
1051 Ne.clo fd (fun msg ->
1052 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1055 let popened =
1056 try popen conf.selcmd [r, 0; w, -1]; true
1057 with exn ->
1058 showtext '!'
1059 (Printf.sprintf "failed to execute %s: %s"
1060 conf.selcmd (exntos exn));
1061 false
1063 if popened
1064 then (
1066 let l = String.length s in
1067 let n = tempfailureretry (Unix.write w s 0) l in
1068 if n != l
1069 then
1070 showtext '!'
1071 (Printf.sprintf
1072 "failed to write %d characters to sel pipe, wrote %d"
1075 with exn ->
1076 showtext '!'
1077 (Printf.sprintf "failed to write to sel pipe: %s"
1078 (exntos exn)
1081 else dolog "%s" s;
1082 clo "selstring pipe/r" r;
1083 clo "selstring pipe/w" w;
1086 let undertext = function
1087 | Unone -> "none"
1088 | Ulinkuri s -> s
1089 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1090 | Utext s -> "font: " ^ s
1091 | Uunexpected s -> "unexpected: " ^ s
1092 | Ulaunch s -> "launch: " ^ s
1093 | Unamed s -> "named: " ^ s
1094 | Uremote (filename, pageno) ->
1095 Printf.sprintf "%s: page %d" filename (pageno+1)
1096 | Uremotedest (filename, destname) ->
1097 Printf.sprintf "%s: destination %S" filename destname
1100 let updateunder x y =
1101 match getunder x y with
1102 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1103 | Ulinkuri uri ->
1104 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1105 Wsi.setcursor Wsi.CURSOR_INFO
1106 | Ulinkgoto (pageno, _) ->
1107 if conf.underinfo
1108 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1109 Wsi.setcursor Wsi.CURSOR_INFO
1110 | Utext s ->
1111 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1112 Wsi.setcursor Wsi.CURSOR_TEXT
1113 | Uunexpected s ->
1114 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1115 Wsi.setcursor Wsi.CURSOR_INHERIT
1116 | Ulaunch s ->
1117 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1118 Wsi.setcursor Wsi.CURSOR_INHERIT
1119 | Unamed s ->
1120 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1121 Wsi.setcursor Wsi.CURSOR_INHERIT
1122 | Uremote (filename, pageno) ->
1123 if conf.underinfo then showtext 'r'
1124 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1125 Wsi.setcursor Wsi.CURSOR_INFO
1126 | Uremotedest (filename, destname) ->
1127 if conf.underinfo then showtext 'r'
1128 (Printf.sprintf "emote destination: %s (%S)" filename destname);
1129 Wsi.setcursor Wsi.CURSOR_INFO
1132 let showlinktype under =
1133 if conf.underinfo
1134 then
1135 match under with
1136 | Unone -> ()
1137 | under ->
1138 let s = undertext under in
1139 showtext ' ' s
1142 let addchar s c =
1143 let b = Buffer.create (String.length s + 1) in
1144 Buffer.add_string b s;
1145 Buffer.add_char b c;
1146 Buffer.contents b;
1149 module type TextEnumType =
1151 type t
1152 val name : string
1153 val names : string array
1154 end;;
1156 module TextEnumMake (Ten : TextEnumType) =
1157 struct
1158 let names = Ten.names;;
1159 let to_int (t : Ten.t) = Obj.magic t;;
1160 let to_string t = names.(to_int t);;
1161 let of_int n : Ten.t = Obj.magic n;;
1162 let of_string s =
1163 let rec find i =
1164 if i = Array.length names
1165 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1166 else (
1167 if Ten.names.(i) = s
1168 then of_int i
1169 else find (i+1)
1171 in find 0;;
1172 end;;
1174 module CSTE = TextEnumMake (struct
1175 type t = colorspace;;
1176 let name = "colorspace";;
1177 let names = [|"rgb"; "bgr"; "gray"|];;
1178 end);;
1180 module MTE = TextEnumMake (struct
1181 type t = mark;;
1182 let name = "mark";;
1183 let names = [|"page"; "block"; "line"; "word"|];;
1184 end);;
1186 module FMTE = TextEnumMake (struct
1187 type t = fitmodel;;
1188 let name = "fitmodel";;
1189 let names = [|"width"; "proportional"; "page"|];;
1190 end);;
1192 let intentry_with_suffix text key =
1193 let c =
1194 if key >= 32 && key < 127
1195 then Char.chr key
1196 else '\000'
1198 match Char.lowercase c with
1199 | '0' .. '9' ->
1200 let text = addchar text c in
1201 TEcont text
1203 | 'k' | 'm' | 'g' ->
1204 let text = addchar text c in
1205 TEcont text
1207 | _ ->
1208 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1209 TEcont text
1212 let multicolumns_to_string (n, a, b) =
1213 if a = 0 && b = 0
1214 then Printf.sprintf "%d" n
1215 else Printf.sprintf "%d,%d,%d" n a b;
1218 let multicolumns_of_string s =
1220 (int_of_string s, 0, 0)
1221 with _ ->
1222 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1223 if a > 1 || b > 1
1224 then failwith "subtly broken"; (n, a, b)
1228 let readcmd fd =
1229 let s = "xxxx" in
1230 let n = tempfailureretry (Unix.read fd s 0) 4 in
1231 if n != 4 then error "incomplete read(len) = %d" n;
1232 let len = 0
1233 lor (Char.code s.[0] lsl 24)
1234 lor (Char.code s.[1] lsl 16)
1235 lor (Char.code s.[2] lsl 8)
1236 lor (Char.code s.[3] lsl 0)
1238 let s = String.create len in
1239 let n = tempfailureretry (Unix.read fd s 0) len in
1240 if n != len then error "incomplete read(data) %d vs %d" n len;
1244 let btod b = if b then 1 else 0;;
1246 let wcmd fmt =
1247 let b = Buffer.create 16 in
1248 Buffer.add_string b "llll";
1249 Printf.kbprintf
1250 (fun b ->
1251 let s = Buffer.contents b in
1252 let n = String.length s in
1253 let len = n - 4 in
1254 (* dolog "wcmd %S" (String.sub s 4 len); *)
1255 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1256 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1257 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1258 s.[3] <- Char.chr (len land 0xff);
1259 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1260 if n' != n then error "write failed %d vs %d" n' n;
1261 ) b fmt;
1264 let calcips h =
1265 let d = state.winh - h in
1266 max conf.interpagespace ((d + 1) / 2)
1269 let rowyh (c, coverA, coverB) b n =
1270 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1271 then
1272 let _, _, vy, (_, _, h, _) = b.(n) in
1273 (vy, h)
1274 else
1275 let n' = n - coverA in
1276 let d = n' mod c in
1277 let s = n - d in
1278 let e = min state.pagecount (s + c) in
1279 let rec find m miny maxh = if m = e then miny, maxh else
1280 let _, _, y, (_, _, h, _) = b.(m) in
1281 let miny = min miny y in
1282 let maxh = max maxh h in
1283 find (m+1) miny maxh
1284 in find s max_int 0
1287 let calcheight () =
1288 match conf.columns with
1289 | Cmulti ((_, _, _) as cl, b) ->
1290 if Array.length b > 0
1291 then
1292 let y, h = rowyh cl b (Array.length b - 1) in
1293 y + h + (if conf.presentation then calcips h else 0)
1294 else 0
1295 | Csingle b ->
1296 if Array.length b > 0
1297 then
1298 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1299 y + h + (if conf.presentation then calcips h else 0)
1300 else 0
1301 | Csplit (_, b) ->
1302 if Array.length b > 0
1303 then
1304 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1305 y + h
1306 else 0
1309 let getpageywh pageno =
1310 let pageno = bound pageno 0 (state.pagecount-1) in
1311 match conf.columns with
1312 | Csingle b ->
1313 if Array.length b = 0
1314 then 0, 0, 0
1315 else
1316 let (_, _, y, (_, w, h, _)) = b.(pageno) in
1317 let y =
1318 if conf.presentation
1319 then y - calcips h
1320 else y
1322 y, w, h
1323 | Cmulti (cl, b) ->
1324 if Array.length b = 0
1325 then 0, 0, 0
1326 else
1327 let y, h = rowyh cl b pageno in
1328 let (_, _, _, (_, w, _, _)) = b.(pageno) in
1329 let y =
1330 if conf.presentation
1331 then y - calcips h
1332 else y
1334 y, w, h
1335 | Csplit (c, b) ->
1336 if Array.length b = 0
1337 then 0, 0, 0
1338 else
1339 let n = pageno*c in
1340 let (_, _, y, (_, w, h, _)) = b.(n) in
1341 y, w / c, h
1344 let getpageyh pageno =
1345 let y,_,h = getpageywh pageno in
1346 y, h;
1349 let getpagedim pageno =
1350 let rec f ppdim l =
1351 match l with
1352 | (n, _, _, _) as pdim :: rest ->
1353 if n >= pageno
1354 then (if n = pageno then pdim else ppdim)
1355 else f pdim rest
1357 | [] -> ppdim
1359 f (-1, -1, -1, -1) state.pdims
1362 let getpagey pageno = fst (getpageyh pageno);;
1364 let nogeomcmds cmds =
1365 match cmds with
1366 | s, [] -> emptystr s
1367 | _ -> false
1370 let page_of_y y =
1371 let ((c, coverA, coverB) as cl), b =
1372 match conf.columns with
1373 | Csingle b -> (1, 0, 0), b
1374 | Cmulti (c, b) -> c, b
1375 | Csplit (_, b) -> (1, 0, 0), b
1377 if Array.length b = 0
1378 then -1
1379 else
1380 let rec bsearch nmin nmax =
1381 if nmin > nmax
1382 then bound nmin 0 (state.pagecount-1)
1383 else
1384 let n = (nmax + nmin) / 2 in
1385 let vy, h = rowyh cl b n in
1386 let y0, y1 =
1387 if conf.presentation
1388 then
1389 let ips = calcips h in
1390 let y0 = vy - ips in
1391 let y1 = vy + h + ips in
1392 y0, y1
1393 else (
1394 if n = 0
1395 then 0, vy + h + conf.interpagespace
1396 else
1397 let y0 = vy - conf.interpagespace in
1398 y0, y0 + h + conf.interpagespace
1401 if y >= y0 && y < y1
1402 then (
1403 if c = 1
1404 then n
1405 else (
1406 if n > coverA
1407 then
1408 if n < state.pagecount - coverB
1409 then ((n-coverA)/c)*c + coverA
1410 else n
1411 else n
1414 else (
1415 if y > y0
1416 then bsearch (n+1) nmax
1417 else bsearch nmin (n-1)
1420 bsearch 0 (state.pagecount-1);
1423 let layoutN ((columns, coverA, coverB), b) y sh =
1424 let sh = sh - (hscrollh ()) in
1425 let rec fold accu n =
1426 if n = Array.length b
1427 then accu
1428 else
1429 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1430 if (vy - y) > sh &&
1431 (n = coverA - 1
1432 || n = state.pagecount - coverB
1433 || (n - coverA) mod columns = columns - 1)
1434 then accu
1435 else
1436 let accu =
1437 if vy + h > y
1438 then
1439 let pagey = max 0 (y - vy) in
1440 let pagedispy = if pagey > 0 then 0 else vy - y in
1441 let pagedispx, pagex =
1442 let pdx =
1443 if n = coverA - 1 || n = state.pagecount - coverB
1444 then state.x + (wadjsb state.winw - w) / 2
1445 else dx + xoff + state.x
1447 if pdx < 0
1448 then 0, -pdx
1449 else pdx, 0
1451 let pagevw =
1452 let vw = wadjsb state.winw - pagedispx in
1453 let pw = w - pagex in
1454 min vw pw
1456 let pagevh = min (h - pagey) (sh - pagedispy) in
1457 if pagevw > 0 && pagevh > 0
1458 then
1459 let e =
1460 { pageno = n
1461 ; pagedimno = pdimno
1462 ; pagew = w
1463 ; pageh = h
1464 ; pagex = pagex
1465 ; pagey = pagey
1466 ; pagevw = pagevw
1467 ; pagevh = pagevh
1468 ; pagedispx = pagedispx
1469 ; pagedispy = pagedispy
1470 ; pagecol = 0
1473 e :: accu
1474 else
1475 accu
1476 else
1477 accu
1479 fold accu (n+1)
1481 if Array.length b = 0
1482 then []
1483 else List.rev (fold [] (page_of_y y))
1486 let layoutS (columns, b) y sh =
1487 let sh = sh - hscrollh () in
1488 let rec fold accu n =
1489 if n = Array.length b
1490 then accu
1491 else
1492 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1493 if (vy - y) > sh
1494 then accu
1495 else
1496 let accu =
1497 if vy + pageh > y
1498 then
1499 let x = xoff + state.x in
1500 let pagey = max 0 (y - vy) in
1501 let pagedispy = if pagey > 0 then 0 else vy - y in
1502 let pagedispx, pagex =
1503 if px = 0
1504 then (
1505 if x < 0
1506 then 0, -x
1507 else x, 0
1509 else (
1510 let px = px - x in
1511 if px < 0
1512 then -px, 0
1513 else 0, px
1516 let pagecolw = pagew/columns in
1517 let pagedispx =
1518 if pagecolw < state.winw
1519 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1520 else pagedispx
1522 let pagevw =
1523 let vw = wadjsb state.winw - pagedispx in
1524 let pw = pagew - pagex in
1525 min vw pw
1527 let pagevw = min pagevw pagecolw in
1528 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1529 if pagevw > 0 && pagevh > 0
1530 then
1531 let e =
1532 { pageno = n/columns
1533 ; pagedimno = pdimno
1534 ; pagew = pagew
1535 ; pageh = pageh
1536 ; pagex = pagex
1537 ; pagey = pagey
1538 ; pagevw = pagevw
1539 ; pagevh = pagevh
1540 ; pagedispx = pagedispx
1541 ; pagedispy = pagedispy
1542 ; pagecol = n mod columns
1545 e :: accu
1546 else
1547 accu
1548 else
1549 accu
1551 fold accu (n+1)
1553 List.rev (fold [] 0)
1556 let layout y sh =
1557 if nogeomcmds state.geomcmds
1558 then
1559 match conf.columns with
1560 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1561 | Cmulti c -> layoutN c y sh
1562 | Csplit s -> layoutS s y sh
1563 else []
1566 let clamp incr =
1567 let y = state.y + incr in
1568 let y = max 0 y in
1569 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1573 let itertiles l f =
1574 let tilex = l.pagex mod conf.tilew in
1575 let tiley = l.pagey mod conf.tileh in
1577 let col = l.pagex / conf.tilew in
1578 let row = l.pagey / conf.tileh in
1580 let rec rowloop row y0 dispy h =
1581 if h = 0
1582 then ()
1583 else (
1584 let dh = conf.tileh - y0 in
1585 let dh = min h dh in
1586 let rec colloop col x0 dispx w =
1587 if w = 0
1588 then ()
1589 else (
1590 let dw = conf.tilew - x0 in
1591 let dw = min w dw in
1593 f col row dispx dispy x0 y0 dw dh;
1594 colloop (col+1) 0 (dispx+dw) (w-dw)
1597 colloop col tilex l.pagedispx l.pagevw;
1598 rowloop (row+1) 0 (dispy+dh) (h-dh)
1601 if l.pagevw > 0 && l.pagevh > 0
1602 then rowloop row tiley l.pagedispy l.pagevh;
1605 let gettileopaque l col row =
1606 let key =
1607 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1609 try Some (Hashtbl.find state.tilemap key)
1610 with Not_found -> None
1613 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1614 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1615 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1618 let filledrect x0 y0 x1 y1 =
1619 GlArray.disable `texture_coord;
1620 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1621 GlArray.vertex `two state.vraw;
1622 GlArray.draw_arrays `triangle_strip 0 4;
1623 GlArray.enable `texture_coord;
1626 let linerect x0 y0 x1 y1 =
1627 GlArray.disable `texture_coord;
1628 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
1629 GlArray.vertex `two state.vraw;
1630 GlArray.draw_arrays `line_loop 0 4;
1631 GlArray.enable `texture_coord;
1634 let drawtiles l color =
1635 GlDraw.color color;
1636 begintiles ();
1637 let f col row x y tilex tiley w h =
1638 match gettileopaque l col row with
1639 | Some (opaque, _, t) ->
1640 let params = x, y, w, h, tilex, tiley in
1641 if conf.invert
1642 then GlTex.env (`mode `blend);
1643 drawtile params opaque;
1644 if conf.invert
1645 then GlTex.env (`mode `modulate);
1646 if conf.debug
1647 then (
1648 endtiles ();
1649 let s = Printf.sprintf
1650 "%d[%d,%d] %f sec"
1651 l.pageno col row t
1653 let w = measurestr fstate.fontsize s in
1654 GlDraw.color (0.0, 0.0, 0.0);
1655 filledrect (float (x-2))
1656 (float (y-2))
1657 (float (x+2) +. w)
1658 (float (y + fstate.fontsize + 2));
1659 GlDraw.color (1.0, 1.0, 1.0);
1660 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1661 begintiles ();
1664 | None ->
1665 endtiles ();
1666 let w =
1667 let lw = wadjsb state.winw - x in
1668 min lw w
1669 and h =
1670 let lh = state.winh - y in
1671 min lh h
1673 if conf.invert
1674 then GlTex.env (`mode `blend);
1675 begin match state.texid with
1676 | Some id ->
1677 Gl.enable `texture_2d;
1678 GlTex.bind_texture `texture_2d id;
1679 let x0 = float x
1680 and y0 = float y
1681 and x1 = float (x+w)
1682 and y1 = float (y+h) in
1684 let tw = float w /. 16.0
1685 and th = float h /. 16.0 in
1686 let tx0 = float tilex /. 16.0
1687 and ty0 = float tiley /. 16.0 in
1688 let tx1 = tx0 +. tw
1689 and ty1 = ty0 +. th in
1690 Raw.sets_float state.vraw ~pos:0
1691 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1692 Raw.sets_float state.traw ~pos:0
1693 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
1694 GlArray.vertex `two state.vraw;
1695 GlArray.tex_coord `two state.traw;
1696 GlArray.draw_arrays `triangle_strip 0 4;
1698 | None ->
1699 GlDraw.color (1.0, 1.0, 1.0);
1700 filledrect (float x) (float y) (float (x+w)) (float (y+h));
1701 end;
1702 if conf.invert
1703 then GlTex.env (`mode `modulate);
1704 if w > 128 && h > fstate.fontsize + 10
1705 then (
1706 let c = if conf.invert then 1.0 else 0.0 in
1707 GlDraw.color (c, c, c);
1708 let c, r =
1709 if conf.verbose
1710 then (col*conf.tilew, row*conf.tileh)
1711 else col, row
1713 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1715 GlDraw.color color;
1716 begintiles ();
1718 itertiles l f;
1719 endtiles ();
1722 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1724 let tilevisible1 l x y =
1725 let ax0 = l.pagex
1726 and ax1 = l.pagex + l.pagevw
1727 and ay0 = l.pagey
1728 and ay1 = l.pagey + l.pagevh in
1730 let bx0 = x
1731 and by0 = y in
1732 let bx1 = min (bx0 + conf.tilew) l.pagew
1733 and by1 = min (by0 + conf.tileh) l.pageh in
1735 let rx0 = max ax0 bx0
1736 and ry0 = max ay0 by0
1737 and rx1 = min ax1 bx1
1738 and ry1 = min ay1 by1 in
1740 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1741 nonemptyintersection
1744 let tilevisible layout n x y =
1745 let rec findpageinlayout m = function
1746 | l :: rest when l.pageno = n ->
1747 tilevisible1 l x y || (
1748 match conf.columns with
1749 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1750 | _ -> false
1752 | _ :: rest -> findpageinlayout 0 rest
1753 | [] -> false
1755 findpageinlayout 0 layout;
1758 let tileready l x y =
1759 tilevisible1 l x y &&
1760 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1763 let tilepage n p layout =
1764 let rec loop = function
1765 | l :: rest ->
1766 if l.pageno = n
1767 then
1768 let f col row _ _ _ _ _ _ =
1769 if state.currently = Idle
1770 then
1771 match gettileopaque l col row with
1772 | Some _ -> ()
1773 | None ->
1774 let x = col*conf.tilew
1775 and y = row*conf.tileh in
1776 let w =
1777 let w = l.pagew - x in
1778 min w conf.tilew
1780 let h =
1781 let h = l.pageh - y in
1782 min h conf.tileh
1784 let pbo =
1785 if conf.usepbo
1786 then getpbo w h conf.colorspace
1787 else ~< "0"
1789 wcmd "tile %s %d %d %d %d %s"
1790 (~> p) x y w h (~> pbo);
1791 state.currently <-
1792 Tiling (
1793 l, p, conf.colorspace, conf.angle,
1794 state.gen, col, row, conf.tilew, conf.tileh
1797 itertiles l f;
1798 else
1799 loop rest
1801 | [] -> ()
1803 if nogeomcmds state.geomcmds
1804 then loop layout;
1807 let preloadlayout y =
1808 let y = if y < state.winh then 0 else y - state.winh in
1809 let h = state.winh*3 in
1810 layout y h;
1813 let load pages =
1814 let rec loop pages =
1815 if state.currently != Idle
1816 then ()
1817 else
1818 match pages with
1819 | l :: rest ->
1820 begin match getopaque l.pageno with
1821 | None ->
1822 wcmd "page %d %d" l.pageno l.pagedimno;
1823 state.currently <- Loading (l, state.gen);
1824 | Some opaque ->
1825 tilepage l.pageno opaque pages;
1826 loop rest
1827 end;
1828 | _ -> ()
1830 if nogeomcmds state.geomcmds
1831 then loop pages
1834 let preload pages =
1835 load pages;
1836 if conf.preload && state.currently = Idle
1837 then load (preloadlayout state.y);
1840 let layoutready layout =
1841 let rec fold all ls =
1842 all && match ls with
1843 | l :: rest ->
1844 let seen = ref false in
1845 let allvisible = ref true in
1846 let foo col row _ _ _ _ _ _ =
1847 seen := true;
1848 allvisible := !allvisible &&
1849 begin match gettileopaque l col row with
1850 | Some _ -> true
1851 | None -> false
1854 itertiles l foo;
1855 fold (!seen && !allvisible) rest
1856 | [] -> true
1858 let alltilesvisible = fold true layout in
1859 alltilesvisible;
1862 let gotoy y =
1863 let y = bound y 0 state.maxy in
1864 let y, layout, proceed =
1865 match conf.maxwait with
1866 | Some time when state.ghyll == noghyll ->
1867 begin match state.throttle with
1868 | None ->
1869 let layout = layout y state.winh in
1870 let ready = layoutready layout in
1871 if not ready
1872 then (
1873 load layout;
1874 state.throttle <- Some (layout, y, now ());
1876 else G.postRedisplay "gotoy showall (None)";
1877 y, layout, ready
1878 | Some (_, _, started) ->
1879 let dt = now () -. started in
1880 if dt > time
1881 then (
1882 state.throttle <- None;
1883 let layout = layout y state.winh in
1884 load layout;
1885 G.postRedisplay "maxwait";
1886 y, layout, true
1888 else -1, [], false
1891 | _ ->
1892 let layout = layout y state.winh in
1893 if not !wtmode || layoutready layout
1894 then G.postRedisplay "gotoy ready";
1895 y, layout, true
1897 if proceed
1898 then (
1899 state.y <- y;
1900 state.layout <- layout;
1901 begin match state.mode with
1902 | LinkNav (Ltexact (pageno, linkno)) ->
1903 let rec loop = function
1904 | [] ->
1905 state.mode <- LinkNav (Ltgendir 0)
1906 | l :: _ when l.pageno = pageno ->
1907 begin match getopaque pageno with
1908 | None ->
1909 state.mode <- LinkNav (Ltgendir 0)
1910 | Some opaque ->
1911 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1912 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1913 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1914 then state.mode <- LinkNav (Ltgendir 0)
1916 | _ :: rest -> loop rest
1918 loop layout
1919 | _ -> ()
1920 end;
1921 begin match state.mode with
1922 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1923 if not (pagevisible layout pageno)
1924 then (
1925 match state.layout with
1926 | [] -> ()
1927 | l :: _ ->
1928 state.mode <- Birdseye (
1929 conf, leftx, l.pageno, hooverpageno, anchor
1932 | LinkNav (Ltgendir dir as lt) ->
1933 let linknav =
1934 let rec loop = function
1935 | [] -> lt
1936 | l :: rest ->
1937 match getopaque l.pageno with
1938 | None -> loop rest
1939 | Some opaque ->
1940 let link =
1941 let ld =
1942 if dir = 0
1943 then LDfirstvisible (l.pagex, l.pagey, dir)
1944 else (
1945 if dir > 0 then LDfirst else LDlast
1948 findlink opaque ld
1950 match link with
1951 | Lnotfound -> loop rest
1952 | Lfound n ->
1953 showlinktype (getlink opaque n);
1954 Ltexact (l.pageno, n)
1956 loop state.layout
1958 state.mode <- LinkNav linknav
1959 | _ -> ()
1960 end;
1961 preload layout;
1963 state.ghyll <- noghyll;
1964 if conf.updatecurs
1965 then (
1966 let mx, my = state.mpos in
1967 updateunder mx my;
1971 let conttiling pageno opaque =
1972 tilepage pageno opaque
1973 (if conf.preload then preloadlayout state.y else state.layout)
1976 let gotoy_and_clear_text y =
1977 if not conf.verbose then state.text <- "";
1978 gotoy y;
1981 let getanchor1 l =
1982 let top =
1983 let coloff = l.pagecol * l.pageh in
1984 float (l.pagey + coloff) /. float l.pageh
1986 let dtop =
1987 if l.pagedispy = 0
1988 then
1990 else (
1991 if conf.presentation
1992 then float l.pagedispy /. float (calcips l.pageh)
1993 else float l.pagedispy /. float conf.interpagespace
1996 (l.pageno, top, dtop)
1999 let getanchor () =
2000 match state.layout with
2001 | l :: _ -> getanchor1 l
2002 | [] ->
2003 let n = page_of_y state.y in
2004 if n = -1
2005 then state.anchor
2006 else
2007 let y, h = getpageyh n in
2008 let dy = y - state.y in
2009 let dtop =
2010 if conf.presentation
2011 then
2012 let ips = calcips h in
2013 float (dy + ips) /. float ips
2014 else
2015 float dy /. float conf.interpagespace
2017 (n, 0.0, dtop)
2020 let getanchory (n, top, dtop) =
2021 let y, h = getpageyh n in
2022 if conf.presentation
2023 then
2024 let ips = calcips h in
2025 y + truncate (top*.float h -. dtop*.float ips) + ips;
2026 else
2027 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
2030 let gotoanchor anchor =
2031 gotoy (getanchory anchor);
2034 let addnav () =
2035 cbput state.hists.nav (getanchor ());
2038 let getnav dir =
2039 let anchor = cbgetc state.hists.nav dir in
2040 getanchory anchor;
2043 let gotoghyll y =
2044 let scroll f n a b =
2045 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
2046 let snake f a b =
2047 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
2048 if f < a
2049 then s (float f /. float a)
2050 else (
2051 if f > b
2052 then 1.0 -. s ((float (f-b) /. float (n-b)))
2053 else 1.0
2056 snake f a b
2057 and summa n a b =
2058 let ins = float a *. 0.5
2059 and outs = float (n-b) *. 0.5 in
2060 let ones = b - a in
2061 ins +. outs +. float ones
2063 let rec set (_N, _A, _B) y sy =
2064 let sum = summa _N _A _B in
2065 let dy = float (y - sy) in
2066 state.ghyll <- (
2067 let rec gf n y1 o =
2068 if n >= _N
2069 then state.ghyll <- noghyll
2070 else
2071 let go n =
2072 let s = scroll n _N _A _B in
2073 let y1 = y1 +. ((s *. dy) /. sum) in
2074 gotoy_and_clear_text (truncate y1);
2075 state.ghyll <- gf (n+1) y1;
2077 match o with
2078 | None -> go n
2079 | Some y' -> set (_N/2, 1, 1) y' state.y
2081 gf 0 (float state.y)
2084 match conf.ghyllscroll with
2085 | Some nab when not conf.presentation ->
2086 if state.ghyll == noghyll
2087 then set nab y state.y
2088 else state.ghyll (Some y)
2089 | _ ->
2090 gotoy_and_clear_text y
2093 let gotopage n top =
2094 let y, h = getpageyh n in
2095 let y = y + (truncate (top *. float h)) in
2096 gotoghyll y
2099 let gotopage1 n top =
2100 let y = getpagey n in
2101 let y = y + top in
2102 gotoghyll y
2105 let invalidate s f =
2106 state.layout <- [];
2107 state.pdims <- [];
2108 state.rects <- [];
2109 state.rects1 <- [];
2110 match state.geomcmds with
2111 | ps, [] when emptystr ps ->
2112 f ();
2113 state.geomcmds <- s, [];
2115 | ps, [] ->
2116 state.geomcmds <- ps, [s, f];
2118 | ps, (s', _) :: rest when s' = s ->
2119 state.geomcmds <- ps, ((s, f) :: rest);
2121 | ps, cmds ->
2122 state.geomcmds <- ps, ((s, f) :: cmds);
2125 let flushpages () =
2126 Hashtbl.iter (fun _ opaque ->
2127 wcmd "freepage %s" (~> opaque);
2128 ) state.pagemap;
2129 Hashtbl.clear state.pagemap;
2132 let flushtiles () =
2133 if not (Queue.is_empty state.tilelru)
2134 then (
2135 Queue.iter (fun (k, p, s) ->
2136 wcmd "freetile %s" (~> p);
2137 state.memused <- state.memused - s;
2138 Hashtbl.remove state.tilemap k;
2139 ) state.tilelru;
2140 state.uioh#infochanged Memused;
2141 Queue.clear state.tilelru;
2143 load state.layout;
2146 let stateh h =
2147 let h = truncate (float h*.conf.zoom) in
2148 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2149 h - d
2152 let opendoc path password =
2153 state.path <- path;
2154 state.password <- password;
2155 state.gen <- state.gen + 1;
2156 state.docinfo <- [];
2158 flushpages ();
2159 setaalevel conf.aalevel;
2160 let titlepath =
2161 if emptystr state.origin
2162 then path
2163 else state.origin
2165 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2166 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2167 invalidate "reqlayout"
2168 (fun () ->
2169 wcmd "reqlayout %d %d %d %s\000"
2170 conf.angle (FMTE.to_int conf.fitmodel)
2171 (stateh state.winh) state.nameddest
2175 let reload () =
2176 state.anchor <- getanchor ();
2177 opendoc state.path state.password;
2180 let scalecolor c =
2181 let c = c *. conf.colorscale in
2182 (c, c, c);
2185 let scalecolor2 (r, g, b) =
2186 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2189 let docolumns = function
2190 | Csingle _ ->
2191 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2192 let rec loop pageno pdimno pdim y ph pdims =
2193 if pageno = state.pagecount
2194 then ()
2195 else
2196 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2197 match pdims with
2198 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2199 pdimno+1, pdim, rest
2200 | _ ->
2201 pdimno, pdim, pdims
2203 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2204 let y = y +
2205 (if conf.presentation
2206 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2207 else (if pageno = 0 then 0 else conf.interpagespace)
2210 a.(pageno) <- (pdimno, x, y, pdim);
2211 loop (pageno+1) pdimno pdim (y + h) h pdims
2213 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2214 conf.columns <- Csingle a;
2216 | Cmulti ((columns, coverA, coverB), _) ->
2217 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2218 let rec loop pageno pdimno pdim x y rowh pdims =
2219 let rec fixrow m = if m = pageno then () else
2220 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2221 if h < rowh
2222 then (
2223 let y = y + (rowh - h) / 2 in
2224 a.(m) <- (pdimno, x, y, pdim);
2226 fixrow (m+1)
2228 if pageno = state.pagecount
2229 then fixrow (((pageno - 1) / columns) * columns)
2230 else
2231 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2232 match pdims with
2233 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2234 pdimno+1, pdim, rest
2235 | _ ->
2236 pdimno, pdim, pdims
2238 let x, y, rowh' =
2239 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2240 then (
2241 let x = (wadjsb state.winw - w) / 2 in
2242 let ips =
2243 if conf.presentation then calcips h else conf.interpagespace in
2244 x, y + ips + rowh, h
2246 else (
2247 if (pageno - coverA) mod columns = 0
2248 then (
2249 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2250 let y =
2251 if conf.presentation
2252 then
2253 let ips = calcips h in
2254 y + (if pageno = 0 then 0 else calcips rowh + ips)
2255 else
2256 y + (if pageno = 0 then 0 else conf.interpagespace)
2258 x, y + rowh, h
2260 else x, y, max rowh h
2263 let y =
2264 if pageno > 1 && (pageno - coverA) mod columns = 0
2265 then (
2266 let y =
2267 if pageno = columns && conf.presentation
2268 then (
2269 let ips = calcips rowh in
2270 for i = 0 to pred columns
2272 let (pdimno, x, y, pdim) = a.(i) in
2273 a.(i) <- (pdimno, x, y+ips, pdim)
2274 done;
2275 y+ips;
2277 else y
2279 fixrow (pageno - columns);
2282 else y
2284 a.(pageno) <- (pdimno, x, y, pdim);
2285 let x = x + w + xoff*2 + conf.interpagespace in
2286 loop (pageno+1) pdimno pdim x y rowh' pdims
2288 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2289 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2291 | Csplit (c, _) ->
2292 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2293 let rec loop pageno pdimno pdim y pdims =
2294 if pageno = state.pagecount
2295 then ()
2296 else
2297 let pdimno, ((_, w, h, _) as pdim), pdims =
2298 match pdims with
2299 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2300 pdimno+1, pdim, rest
2301 | _ ->
2302 pdimno, pdim, pdims
2304 let cw = w / c in
2305 let rec loop1 n x y =
2306 if n = c then y else (
2307 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2308 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2311 let y = loop1 0 0 y in
2312 loop (pageno+1) pdimno pdim y pdims
2314 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2315 conf.columns <- Csplit (c, a);
2318 let represent () =
2319 docolumns conf.columns;
2320 state.maxy <- calcheight ();
2321 if state.reprf == noreprf
2322 then (
2323 match state.mode with
2324 | Birdseye (_, _, pageno, _, _) ->
2325 let y, h = getpageyh pageno in
2326 let top = (state.winh - h) / 2 in
2327 gotoy (max 0 (y - top))
2328 | _ -> gotoanchor state.anchor
2330 else (
2331 state.reprf ();
2332 state.reprf <- noreprf;
2336 let reshape w h =
2337 GlDraw.viewport 0 0 w h;
2338 let firsttime = state.geomcmds == firstgeomcmds in
2339 if not firsttime && nogeomcmds state.geomcmds
2340 then state.anchor <- getanchor ();
2342 state.winw <- w;
2343 let w = wadjsb (truncate (float w *. conf.zoom)) in
2344 let w = max w 2 in
2345 state.winh <- h;
2346 setfontsize fstate.fontsize;
2347 GlMat.mode `modelview;
2348 GlMat.load_identity ();
2350 GlMat.mode `projection;
2351 GlMat.load_identity ();
2352 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2353 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2354 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2356 let relx =
2357 if conf.zoom <= 1.0
2358 then 0.0
2359 else float state.x /. float state.w
2361 invalidate "geometry"
2362 (fun () ->
2363 state.w <- w;
2364 if not firsttime
2365 then state.x <- truncate (relx *. float w);
2366 let w =
2367 match conf.columns with
2368 | Csingle _ -> w
2369 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2370 | Csplit (c, _) -> w * c
2372 wcmd "geometry %d %d %d"
2373 w (stateh h) (FMTE.to_int conf.fitmodel)
2377 let enttext () =
2378 let len = String.length state.text in
2379 let drawstring s =
2380 let hscrollh =
2381 match state.mode with
2382 | Textentry _ | View | LinkNav _ ->
2383 let h, _, _ = state.uioh#scrollpw in
2385 | _ -> 0
2387 let rect x w =
2388 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2389 (x+.w) (float (state.winh - hscrollh))
2392 let w = float (wadjsb state.winw - 1) in
2393 if state.progress >= 0.0 && state.progress < 1.0
2394 then (
2395 GlDraw.color (0.3, 0.3, 0.3);
2396 let w1 = w *. state.progress in
2397 rect 0.0 w1;
2398 GlDraw.color (0.0, 0.0, 0.0);
2399 rect w1 (w-.w1)
2401 else (
2402 GlDraw.color (0.0, 0.0, 0.0);
2403 rect 0.0 w;
2406 GlDraw.color (1.0, 1.0, 1.0);
2407 drawstring fstate.fontsize
2408 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2410 let s =
2411 match state.mode with
2412 | Textentry ((prefix, text, _, _, _, _), _) ->
2413 let s =
2414 if len > 0
2415 then
2416 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2417 else
2418 Printf.sprintf "%s%s_" prefix text
2422 | _ -> state.text
2424 let s =
2425 if state.newerrmsgs
2426 then (
2427 if not (istextentry state.mode) && state.uioh#eformsgs
2428 then
2429 let s1 = "(press 'e' to review error messasges)" in
2430 if nonemptystr s then s ^ " " ^ s1 else s1
2431 else s
2433 else s
2435 if nonemptystr s
2436 then drawstring s
2439 let gctiles () =
2440 let len = Queue.length state.tilelru in
2441 let layout = lazy (
2442 match state.throttle with
2443 | None ->
2444 if conf.preload
2445 then preloadlayout state.y
2446 else state.layout
2447 | Some (layout, _, _) ->
2448 layout
2449 ) in
2450 let rec loop qpos =
2451 if state.memused <= conf.memlimit
2452 then ()
2453 else (
2454 if qpos < len
2455 then
2456 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2457 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2458 let (_, pw, ph, _) = getpagedim n in
2460 gen = state.gen
2461 && colorspace = conf.colorspace
2462 && angle = conf.angle
2463 && pagew = pw
2464 && pageh = ph
2465 && (
2466 let x = col*conf.tilew
2467 and y = row*conf.tileh in
2468 tilevisible (Lazy.force_val layout) n x y
2470 then Queue.push lruitem state.tilelru
2471 else (
2472 freepbo p;
2473 wcmd "freetile %s" (~> p);
2474 state.memused <- state.memused - s;
2475 state.uioh#infochanged Memused;
2476 Hashtbl.remove state.tilemap k;
2478 loop (qpos+1)
2481 loop 0
2484 let logcurrently = function
2485 | Idle -> dolog "Idle"
2486 | Loading (l, gen) ->
2487 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2488 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2489 dolog
2490 "Tiling %d[%d,%d] page=%s cs=%s angle"
2491 l.pageno col row (~> pageopaque)
2492 (CSTE.to_string colorspace)
2494 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2495 angle gen conf.angle state.gen
2496 tilew tileh
2497 conf.tilew conf.tileh
2499 | Outlining _ ->
2500 dolog "outlining"
2503 let splitatspace =
2504 let r = Str.regexp " " in
2505 fun s -> Str.bounded_split r s 2;
2508 let onpagerect pageno f =
2509 let b =
2510 match conf.columns with
2511 | Cmulti (_, b) -> b
2512 | Csingle b -> b
2513 | Csplit (_, b) -> b
2515 if pageno >= 0 && pageno < Array.length b
2516 then
2517 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2518 f w h
2521 let gotopagexy1 pageno x y =
2522 let _,w1,h1,leftx = getpagedim pageno in
2523 let top = y /. (float h1) in
2524 let left = x /. (float w1) in
2525 let py, w, h = getpageywh pageno in
2526 let wh = state.winh - hscrollh () in
2527 let x = left *. (float w) in
2528 let x = leftx + state.x + truncate x in
2529 let sx =
2530 if x < 0 || x >= wadjsb state.winw
2531 then state.x - x
2532 else state.x
2534 let pdy = truncate (top *. float h) in
2535 let y' = py + pdy in
2536 let dy = y' - state.y in
2537 let sy =
2538 if x != state.x || not (dy > 0 && dy < wh)
2539 then (
2540 if conf.presentation
2541 then
2542 if abs (py - y') > wh
2543 then y'
2544 else py
2545 else y';
2547 else state.y
2549 if state.x != sx || state.y != sy
2550 then (
2551 let x, y =
2552 if !wtmode
2553 then (
2554 let ww = wadjsb state.winw in
2555 let qx = sx / ww
2556 and qy = pdy / wh in
2557 let x = qx * ww
2558 and y = py + qy * wh in
2559 let x = if -x + ww > w1 then -(w1-ww) else x
2560 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2561 let y =
2562 if conf.presentation
2563 then
2564 if abs (py - y') > wh
2565 then y'
2566 else py
2567 else y';
2569 (x, y)
2571 else (sx, sy)
2573 state.x <- x;
2574 gotoy_and_clear_text y;
2576 else gotoy_and_clear_text state.y;
2579 let gotopagexy pageno x y =
2580 match state.mode with
2581 | Birdseye _ -> gotopage pageno 0.0
2582 | _ -> gotopagexy1 pageno x y
2585 let act cmds =
2586 (* dolog "%S" cmds; *)
2587 let cl = splitatspace cmds in
2588 let scan s fmt f =
2589 try Scanf.sscanf s fmt f
2590 with exn ->
2591 dolog "error processing '%S': %s" cmds (exntos exn);
2592 exit 1
2594 let addoutline outline =
2595 match state.currently with
2596 | Outlining outlines ->
2597 state.currently <- Outlining (outline :: outlines)
2598 | Idle -> state.currently <- Outlining [outline]
2599 | currently ->
2600 dolog "invalid outlining state";
2601 logcurrently currently
2603 match cl with
2604 | "clear" :: [] ->
2605 state.uioh#infochanged Pdim;
2606 state.pdims <- [];
2608 | "clearrects" :: [] ->
2609 state.rects <- state.rects1;
2610 G.postRedisplay "clearrects";
2612 | "continue" :: args :: [] ->
2613 let n = scan args "%u" (fun n -> n) in
2614 state.pagecount <- n;
2615 begin match state.currently with
2616 | Outlining l ->
2617 state.currently <- Idle;
2618 state.outlines <- Array.of_list (List.rev l)
2619 | _ -> ()
2620 end;
2622 let cur, cmds = state.geomcmds in
2623 if emptystr cur
2624 then failwith "umpossible";
2626 begin match List.rev cmds with
2627 | [] ->
2628 state.geomcmds <- "", [];
2629 state.throttle <- None;
2630 represent ();
2631 | (s, f) :: rest ->
2632 f ();
2633 state.geomcmds <- s, List.rev rest;
2634 end;
2635 if conf.maxwait = None && not !wtmode
2636 then G.postRedisplay "continue";
2638 | "title" :: args :: [] ->
2639 Wsi.settitle args
2641 | "msg" :: args :: [] ->
2642 showtext ' ' args
2644 | "vmsg" :: args :: [] ->
2645 if conf.verbose
2646 then showtext ' ' args
2648 | "emsg" :: args :: [] ->
2649 Buffer.add_string state.errmsgs args;
2650 state.newerrmsgs <- true;
2651 G.postRedisplay "error message"
2653 | "progress" :: args :: [] ->
2654 let progress, text =
2655 scan args "%f %n"
2656 (fun f pos ->
2657 f, String.sub args pos (String.length args - pos))
2659 state.text <- text;
2660 state.progress <- progress;
2661 G.postRedisplay "progress"
2663 | "firstmatch" :: args :: [] ->
2664 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2665 scan args "%u %d %f %f %f %f %f %f %f %f"
2666 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2667 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2669 let y = (getpagey pageno) + truncate y0 in
2670 addnav ();
2671 gotoy y;
2672 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2674 | "match" :: args :: [] ->
2675 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2676 scan args "%u %d %f %f %f %f %f %f %f %f"
2677 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2678 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2680 state.rects1 <-
2681 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2683 | "page" :: args :: [] ->
2684 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
2685 let pageopaque = ~< pageopaques in
2686 begin match state.currently with
2687 | Loading (l, gen) ->
2688 vlog "page %d took %f sec" l.pageno t;
2689 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2690 begin match state.throttle with
2691 | None ->
2692 let preloadedpages =
2693 if conf.preload
2694 then preloadlayout state.y
2695 else state.layout
2697 let evict () =
2698 let set =
2699 List.fold_left (fun s l -> IntSet.add l.pageno s)
2700 IntSet.empty preloadedpages
2702 let evictedpages =
2703 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2704 if not (IntSet.mem pageno set)
2705 then (
2706 wcmd "freepage %s" (~> opaque);
2707 key :: accu
2709 else accu
2710 ) state.pagemap []
2712 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2714 evict ();
2715 state.currently <- Idle;
2716 if gen = state.gen
2717 then (
2718 tilepage l.pageno pageopaque state.layout;
2719 load state.layout;
2720 load preloadedpages;
2721 if pagevisible state.layout l.pageno
2722 && layoutready state.layout
2723 then G.postRedisplay "page";
2726 | Some (layout, _, _) ->
2727 state.currently <- Idle;
2728 tilepage l.pageno pageopaque layout;
2729 load state.layout
2730 end;
2732 | _ ->
2733 dolog "Inconsistent loading state";
2734 logcurrently state.currently;
2735 exit 1
2738 | "tile" :: args :: [] ->
2739 let (x, y, opaques, size, t) =
2740 scan args "%u %u %s %u %f"
2741 (fun x y p size t -> (x, y, p, size, t))
2743 let opaque = ~< opaques in
2744 begin match state.currently with
2745 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2746 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2748 unmappbo opaque;
2749 if tilew != conf.tilew || tileh != conf.tileh
2750 then (
2751 wcmd "freetile %s" (~> opaque);
2752 state.currently <- Idle;
2753 load state.layout;
2755 else (
2756 puttileopaque l col row gen cs angle opaque size t;
2757 state.memused <- state.memused + size;
2758 state.uioh#infochanged Memused;
2759 gctiles ();
2760 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2761 opaque, size) state.tilelru;
2763 let layout =
2764 match state.throttle with
2765 | None -> state.layout
2766 | Some (layout, _, _) -> layout
2769 state.currently <- Idle;
2770 if gen = state.gen
2771 && conf.colorspace = cs
2772 && conf.angle = angle
2773 && tilevisible layout l.pageno x y
2774 then conttiling l.pageno pageopaque;
2776 begin match state.throttle with
2777 | None ->
2778 preload state.layout;
2779 if gen = state.gen
2780 && conf.colorspace = cs
2781 && conf.angle = angle
2782 && tilevisible state.layout l.pageno x y
2783 && (not !wtmode || layoutready state.layout)
2784 then G.postRedisplay "tile nothrottle";
2786 | Some (layout, y, _) ->
2787 let ready = layoutready layout in
2788 if ready
2789 then (
2790 state.y <- y;
2791 state.layout <- layout;
2792 state.throttle <- None;
2793 G.postRedisplay "throttle";
2795 else load layout;
2796 end;
2799 | _ ->
2800 dolog "Inconsistent tiling state";
2801 logcurrently state.currently;
2802 exit 1
2805 | "pdim" :: args :: [] ->
2806 let (n, w, h, _) as pdim =
2807 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2809 let pdim =
2810 match conf.fitmodel, conf.columns with
2811 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2812 | _ -> pdim
2814 state.uioh#infochanged Pdim;
2815 state.pdims <- pdim :: state.pdims
2817 | "o" :: args :: [] ->
2818 let (l, n, t, h, pos) =
2819 scan args "%u %u %d %u %n"
2820 (fun l n t h pos -> l, n, t, h, pos)
2822 let s = String.sub args pos (String.length args - pos) in
2823 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
2825 | "ou" :: args :: [] ->
2826 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
2827 let s = String.sub args pos len in
2828 let pos2 = pos + len + 1 in
2829 let uri = String.sub args pos2 (String.length args - pos2) in
2830 addoutline (s, l, Ouri uri)
2832 | "on" :: args :: [] ->
2833 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
2834 let s = String.sub args pos (String.length args - pos) in
2835 addoutline (s, l, Onone)
2837 | "a" :: args :: [] ->
2838 let (n, l, t) =
2839 scan args "%u %d %d" (fun n l t -> n, l, t)
2841 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2843 | "info" :: args :: [] ->
2844 state.docinfo <- (1, args) :: state.docinfo
2846 | "infoend" :: [] ->
2847 state.uioh#infochanged Docinfo;
2848 state.docinfo <- List.rev state.docinfo
2850 | _ ->
2851 error "unknown cmd `%S'" cmds
2854 let onhist cb =
2855 let rc = cb.rc in
2856 let action = function
2857 | HCprev -> cbget cb ~-1
2858 | HCnext -> cbget cb 1
2859 | HCfirst -> cbget cb ~-(cb.rc)
2860 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2861 and cancel () = cb.rc <- rc
2862 in (action, cancel)
2865 let search pattern forward =
2866 match conf.columns with
2867 | Csplit _ ->
2868 showtext '!' "searching does not work properly in split columns mode"
2869 | _ ->
2870 if nonemptystr pattern
2871 then
2872 let pn, py =
2873 match state.layout with
2874 | [] -> 0, 0
2875 | l :: _ ->
2876 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2878 wcmd "search %d %d %d %d,%s\000"
2879 (btod conf.icase) pn py (btod forward) pattern;
2882 let intentry text key =
2883 let c =
2884 if key >= 32 && key < 127
2885 then Char.chr key
2886 else '\000'
2888 match c with
2889 | '0' .. '9' ->
2890 let text = addchar text c in
2891 TEcont text
2893 | _ ->
2894 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2895 TEcont text
2898 let linknentry text key =
2899 let c =
2900 if key >= 32 && key < 127
2901 then Char.chr key
2902 else '\000'
2904 match c with
2905 | 'a' .. 'z' ->
2906 let text = addchar text c in
2907 TEcont text
2909 | _ ->
2910 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2911 TEcont text
2914 let linkndone f s =
2915 if nonemptystr s
2916 then (
2917 let n =
2918 let l = String.length s in
2919 let rec loop pos n = if pos = l then n else
2920 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2921 loop (pos+1) (n*26 + m)
2922 in loop 0 0
2924 let rec loop n = function
2925 | [] -> ()
2926 | l :: rest ->
2927 match getopaque l.pageno with
2928 | None -> loop n rest
2929 | Some opaque ->
2930 let m = getlinkcount opaque in
2931 if n < m
2932 then (
2933 let under = getlink opaque n in
2934 f under
2936 else loop (n-m) rest
2938 loop n state.layout;
2942 let textentry text key =
2943 if key land 0xff00 = 0xff00
2944 then TEcont text
2945 else TEcont (text ^ toutf8 key)
2948 let reqlayout angle fitmodel =
2949 match state.throttle with
2950 | None ->
2951 if nogeomcmds state.geomcmds
2952 then state.anchor <- getanchor ();
2953 conf.angle <- angle mod 360;
2954 if conf.angle != 0
2955 then (
2956 match state.mode with
2957 | LinkNav _ -> state.mode <- View
2958 | _ -> ()
2960 conf.fitmodel <- fitmodel;
2961 invalidate "reqlayout"
2962 (fun () ->
2963 wcmd "reqlayout %d %d %d"
2964 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2966 | _ -> ()
2969 let settrim trimmargins trimfuzz =
2970 if nogeomcmds state.geomcmds
2971 then state.anchor <- getanchor ();
2972 conf.trimmargins <- trimmargins;
2973 conf.trimfuzz <- trimfuzz;
2974 let x0, y0, x1, y1 = trimfuzz in
2975 invalidate "settrim"
2976 (fun () ->
2977 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2978 flushpages ();
2981 let setzoom zoom =
2982 match state.throttle with
2983 | None ->
2984 let zoom = max 0.0001 zoom in
2985 if zoom <> conf.zoom
2986 then (
2987 state.prevzoom <- (conf.zoom, state.x);
2988 conf.zoom <- zoom;
2989 reshape state.winw state.winh;
2990 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2993 | Some (layout, y, started) ->
2994 let time =
2995 match conf.maxwait with
2996 | None -> 0.0
2997 | Some t -> t
2999 let dt = now () -. started in
3000 if dt > time
3001 then (
3002 state.y <- y;
3003 load layout;
3007 let setcolumns mode columns coverA coverB =
3008 state.prevcolumns <- Some (conf.columns, conf.zoom);
3009 if columns < 0
3010 then (
3011 if isbirdseye mode
3012 then showtext '!' "split mode doesn't work in bird's eye"
3013 else (
3014 conf.columns <- Csplit (-columns, [||]);
3015 state.x <- 0;
3016 conf.zoom <- 1.0;
3019 else (
3020 if columns < 2
3021 then (
3022 conf.columns <- Csingle [||];
3023 state.x <- 0;
3024 setzoom 1.0;
3026 else (
3027 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
3028 conf.zoom <- 1.0;
3031 reshape state.winw state.winh;
3034 let resetmstate () =
3035 state.mstate <- Mnone;
3036 Wsi.setcursor Wsi.CURSOR_INHERIT;
3039 let enterbirdseye () =
3040 let zoom = float conf.thumbw /. float state.winw in
3041 let birdseyepageno =
3042 let cy = state.winh / 2 in
3043 let fold = function
3044 | [] -> 0
3045 | l :: rest ->
3046 let rec fold best = function
3047 | [] -> best.pageno
3048 | l :: rest ->
3049 let d = cy - (l.pagedispy + l.pagevh/2)
3050 and dbest = cy - (best.pagedispy + best.pagevh/2) in
3051 if abs d < abs dbest
3052 then fold l rest
3053 else best.pageno
3054 in fold l rest
3056 fold state.layout
3058 state.mode <- Birdseye (
3059 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
3061 resetmstate ();
3062 conf.zoom <- zoom;
3063 conf.presentation <- false;
3064 conf.interpagespace <- 10;
3065 conf.hlinks <- false;
3066 conf.fitmodel <- FitProportional;
3067 state.x <- 0;
3068 conf.maxwait <- None;
3069 conf.columns <- (
3070 match conf.beyecolumns with
3071 | Some c ->
3072 conf.zoom <- 1.0;
3073 Cmulti ((c, 0, 0), [||])
3074 | None -> Csingle [||]
3076 if conf.verbose
3077 then
3078 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3079 (100.0*.zoom)
3080 else
3081 state.text <- ""
3083 reshape state.winw state.winh;
3086 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3087 state.mode <- View;
3088 conf.zoom <- c.zoom;
3089 conf.presentation <- c.presentation;
3090 conf.interpagespace <- c.interpagespace;
3091 conf.maxwait <- c.maxwait;
3092 conf.hlinks <- c.hlinks;
3093 conf.fitmodel <- c.fitmodel;
3094 conf.beyecolumns <- (
3095 match conf.columns with
3096 | Cmulti ((c, _, _), _) -> Some c
3097 | Csingle _ -> None
3098 | Csplit _ -> failwith "leaving bird's eye split mode"
3100 conf.columns <- (
3101 match c.columns with
3102 | Cmulti (c, _) -> Cmulti (c, [||])
3103 | Csingle _ -> Csingle [||]
3104 | Csplit (c, _) -> Csplit (c, [||])
3106 if conf.verbose
3107 then
3108 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3109 (100.0*.conf.zoom)
3111 reshape state.winw state.winh;
3112 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3113 state.x <- leftx;
3116 let togglebirdseye () =
3117 match state.mode with
3118 | Birdseye vals -> leavebirdseye vals true
3119 | View -> enterbirdseye ()
3120 | _ -> ()
3123 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3124 let pageno = max 0 (pageno - incr) in
3125 let rec loop = function
3126 | [] -> gotopage1 pageno 0
3127 | l :: _ when l.pageno = pageno ->
3128 if l.pagedispy >= 0 && l.pagey = 0
3129 then G.postRedisplay "upbirdseye"
3130 else gotopage1 pageno 0
3131 | _ :: rest -> loop rest
3133 loop state.layout;
3134 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3137 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3138 let pageno = min (state.pagecount - 1) (pageno + incr) in
3139 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3140 let rec loop = function
3141 | [] ->
3142 let y, h = getpageyh pageno in
3143 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3144 gotoy (clamp dy)
3145 | l :: _ when l.pageno = pageno ->
3146 if l.pagevh != l.pageh
3147 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3148 else G.postRedisplay "downbirdseye"
3149 | _ :: rest -> loop rest
3151 loop state.layout
3154 let boundastep h step =
3155 if step < 0
3156 then bound step ~-h 0
3157 else bound step 0 h
3160 let optentry mode _ key =
3161 let btos b = if b then "on" else "off" in
3162 if key >= 32 && key < 127
3163 then
3164 let c = Char.chr key in
3165 match c with
3166 | 's' ->
3167 let ondone s =
3168 try conf.scrollstep <- int_of_string s with exc ->
3169 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3171 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3173 | 'A' ->
3174 let ondone s =
3176 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
3177 if state.autoscroll <> None
3178 then state.autoscroll <- Some conf.autoscrollstep
3179 with exc ->
3180 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3182 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3184 | 'C' ->
3185 let ondone s =
3187 let n, a, b = multicolumns_of_string s in
3188 setcolumns mode n a b;
3189 with exc ->
3190 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3192 TEswitch ("columns: ", "", None, textentry, ondone, true)
3194 | 'Z' ->
3195 let ondone s =
3197 let zoom = float (int_of_string s) /. 100.0 in
3198 setzoom zoom
3199 with exc ->
3200 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3202 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3204 | 't' ->
3205 let ondone s =
3207 conf.thumbw <- bound (int_of_string s) 2 4096;
3208 state.text <-
3209 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3210 begin match mode with
3211 | Birdseye beye ->
3212 leavebirdseye beye false;
3213 enterbirdseye ();
3214 | _ -> ();
3216 with exc ->
3217 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3219 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3221 | 'R' ->
3222 let ondone s =
3223 match try
3224 Some (int_of_string s)
3225 with exc ->
3226 state.text <- Printf.sprintf "bad integer `%s': %s"
3227 s (exntos exc);
3228 None
3229 with
3230 | Some angle -> reqlayout angle conf.fitmodel
3231 | None -> ()
3233 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3235 | 'i' ->
3236 conf.icase <- not conf.icase;
3237 TEdone ("case insensitive search " ^ (btos conf.icase))
3239 | 'p' ->
3240 conf.preload <- not conf.preload;
3241 gotoy state.y;
3242 TEdone ("preload " ^ (btos conf.preload))
3244 | 'v' ->
3245 conf.verbose <- not conf.verbose;
3246 TEdone ("verbose " ^ (btos conf.verbose))
3248 | 'd' ->
3249 conf.debug <- not conf.debug;
3250 TEdone ("debug " ^ (btos conf.debug))
3252 | 'h' ->
3253 conf.maxhfit <- not conf.maxhfit;
3254 state.maxy <- calcheight ();
3255 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3257 | 'c' ->
3258 conf.crophack <- not conf.crophack;
3259 TEdone ("crophack " ^ btos conf.crophack)
3261 | 'a' ->
3262 let s =
3263 match conf.maxwait with
3264 | None ->
3265 conf.maxwait <- Some infinity;
3266 "always wait for page to complete"
3267 | Some _ ->
3268 conf.maxwait <- None;
3269 "show placeholder if page is not ready"
3271 TEdone s
3273 | 'f' ->
3274 conf.underinfo <- not conf.underinfo;
3275 TEdone ("underinfo " ^ btos conf.underinfo)
3277 | 'P' ->
3278 conf.savebmarks <- not conf.savebmarks;
3279 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3281 | 'S' ->
3282 let ondone s =
3284 let pageno, py =
3285 match state.layout with
3286 | [] -> 0, 0
3287 | l :: _ ->
3288 l.pageno, l.pagey
3290 conf.interpagespace <- int_of_string s;
3291 docolumns conf.columns;
3292 state.maxy <- calcheight ();
3293 let y = getpagey pageno in
3294 gotoy (y + py)
3295 with exc ->
3296 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3298 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3300 | 'l' ->
3301 let fm =
3302 match conf.fitmodel with
3303 | FitProportional -> FitWidth
3304 | _ -> FitProportional
3306 reqlayout conf.angle fm;
3307 TEdone ("proportional display " ^ btos (fm == FitProportional))
3309 | 'T' ->
3310 settrim (not conf.trimmargins) conf.trimfuzz;
3311 TEdone ("trim margins " ^ btos conf.trimmargins)
3313 | 'I' ->
3314 conf.invert <- not conf.invert;
3315 TEdone ("invert colors " ^ btos conf.invert)
3317 | 'x' ->
3318 let ondone s =
3319 cbput state.hists.sel s;
3320 conf.selcmd <- s;
3322 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3323 textentry, ondone, true)
3325 | 'M' ->
3326 if conf.pax == None
3327 then conf.pax <- Some (ref (0.0, 0, 0))
3328 else conf.pax <- None;
3329 TEdone ("PAX " ^ btos (conf.pax != None))
3331 | _ ->
3332 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3333 TEstop
3334 else
3335 TEcont state.text
3338 class type lvsource = object
3339 method getitemcount : int
3340 method getitem : int -> (string * int)
3341 method hasaction : int -> bool
3342 method exit :
3343 uioh:uioh ->
3344 cancel:bool ->
3345 active:int ->
3346 first:int ->
3347 pan:int ->
3348 uioh option
3349 method getactive : int
3350 method getfirst : int
3351 method getpan : int
3352 method getminfo : (int * int) array
3353 end;;
3355 class virtual lvsourcebase = object
3356 val mutable m_active = 0
3357 val mutable m_first = 0
3358 val mutable m_pan = 0
3359 method getactive = m_active
3360 method getfirst = m_first
3361 method getpan = m_pan
3362 method getminfo : (int * int) array = [||]
3363 end;;
3365 let withoutlastutf8 s =
3366 let len = String.length s in
3367 if len = 0
3368 then s
3369 else
3370 let rec find pos =
3371 if pos = 0
3372 then pos
3373 else
3374 let b = Char.code s.[pos] in
3375 if b land 0b11000000 = 0b11000000
3376 then pos
3377 else find (pos-1)
3379 let first =
3380 if Char.code s.[len-1] land 0x80 = 0
3381 then len-1
3382 else find (len-1)
3384 String.sub s 0 first;
3387 let textentrykeyboard
3388 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3389 let key =
3390 if key >= 0xffb0 && key <= 0xffb9
3391 then key - 0xffb0 + 48 else key
3393 let enttext te =
3394 state.mode <- Textentry (te, onleave);
3395 state.text <- "";
3396 enttext ();
3397 G.postRedisplay "textentrykeyboard enttext";
3399 let histaction cmd =
3400 match opthist with
3401 | None -> ()
3402 | Some (action, _) ->
3403 state.mode <- Textentry (
3404 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3406 G.postRedisplay "textentry histaction"
3408 match key with
3409 | 0xff08 -> (* backspace *)
3410 if emptystr text && cancelonempty
3411 then (
3412 onleave Cancel;
3413 G.postRedisplay "textentrykeyboard after cancel";
3415 else
3416 let s = withoutlastutf8 text in
3417 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3419 | 0xff0d | 0xff8d -> (* (kp) enter *)
3420 ondone text;
3421 onleave Confirm;
3422 G.postRedisplay "textentrykeyboard after confirm"
3424 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3425 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3426 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3427 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3429 | 0xff1b -> (* escape*)
3430 if emptystr text
3431 then (
3432 begin match opthist with
3433 | None -> ()
3434 | Some (_, onhistcancel) -> onhistcancel ()
3435 end;
3436 onleave Cancel;
3437 state.text <- "";
3438 G.postRedisplay "textentrykeyboard after cancel2"
3440 else (
3441 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3444 | 0xff9f | 0xffff -> () (* delete *)
3446 | _ when key != 0
3447 && key land 0xff00 != 0xff00 (* keyboard *)
3448 && key land 0xfe00 != 0xfe00 (* xkb *)
3449 && key land 0xfd00 != 0xfd00 (* 3270 *)
3451 begin match onkey text key with
3452 | TEdone text ->
3453 ondone text;
3454 onleave Confirm;
3455 G.postRedisplay "textentrykeyboard after confirm2";
3457 | TEcont text ->
3458 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3460 | TEstop ->
3461 onleave Cancel;
3462 G.postRedisplay "textentrykeyboard after cancel3"
3464 | TEswitch te ->
3465 state.mode <- Textentry (te, onleave);
3466 G.postRedisplay "textentrykeyboard switch";
3467 end;
3469 | _ ->
3470 vlog "unhandled key %s" (Wsi.keyname key)
3473 let firstof first active =
3474 if first > active || abs (first - active) > fstate.maxrows - 1
3475 then max 0 (active - (fstate.maxrows/2))
3476 else first
3479 let calcfirst first active =
3480 if active > first
3481 then
3482 let rows = active - first in
3483 if rows > fstate.maxrows then active - fstate.maxrows else first
3484 else active
3487 let scrollph y maxy =
3488 let sh = float (maxy + state.winh) /. float state.winh in
3489 let sh = float state.winh /. sh in
3490 let sh = max sh (float conf.scrollh) in
3492 let percent = float y /. float maxy in
3493 let position = (float state.winh -. sh) *. percent in
3495 let position =
3496 if position +. sh > float state.winh
3497 then float state.winh -. sh
3498 else position
3500 position, sh;
3503 let coe s = (s :> uioh);;
3505 class listview ~(source:lvsource) ~trusted ~modehash =
3506 object (self)
3507 val m_pan = source#getpan
3508 val m_first = source#getfirst
3509 val m_active = source#getactive
3510 val m_qsearch = ""
3511 val m_prev_uioh = state.uioh
3513 method private elemunder y =
3514 let n = y / (fstate.fontsize+1) in
3515 if m_first + n < source#getitemcount
3516 then (
3517 if source#hasaction (m_first + n)
3518 then Some (m_first + n)
3519 else None
3521 else None
3523 method display =
3524 Gl.enable `blend;
3525 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3526 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3527 filledrect 0. 0. (float state.winw) (float state.winh);
3528 GlDraw.color (1., 1., 1.);
3529 Gl.enable `texture_2d;
3530 let fs = fstate.fontsize in
3531 let nfs = fs + 1 in
3532 let ww = fstate.wwidth in
3533 let tabw = 30.0*.ww in
3534 let itemcount = source#getitemcount in
3535 let minfo = source#getminfo in
3536 let rec loop row =
3537 if (row - m_first) > fstate.maxrows
3538 then ()
3539 else (
3540 if row >= 0 && row < itemcount
3541 then (
3542 let (s, level) = source#getitem row in
3543 let y = (row - m_first) * nfs in
3544 let x = 5.0 +. float (level + m_pan) *. ww in
3545 if row = m_active
3546 then (
3547 Gl.disable `texture_2d;
3548 let alpha = if source#hasaction row then 0.9 else 0.3 in
3549 GlDraw.color (1., 1., 1.) ~alpha;
3550 linerect 1. (float (y + 1))
3551 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3552 Gl.enable `texture_2d;
3553 GlDraw.color (1., 1., 1.);
3555 let drawtabularstring s =
3556 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3557 if trusted
3558 then
3559 let tabpos = try String.index s '\t' with Not_found -> -1 in
3560 if tabpos > 0
3561 then
3562 let len = String.length s - tabpos - 1 in
3563 let s1 = String.sub s 0 tabpos
3564 and s2 = String.sub s (tabpos + 1) len in
3565 let nx = drawstr x s1 in
3566 let sw = nx -. x in
3567 let x = x +. (max tabw sw) in
3568 drawstr x s2
3569 else
3570 drawstr x s
3571 else
3572 drawstr x s
3574 let _ = drawtabularstring s in
3575 loop (row+1)
3579 loop m_first;
3580 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
3581 let rec loop row =
3582 if (row - m_first) > fstate.maxrows
3583 then ()
3584 else (
3585 if row >= 0 && row < itemcount
3586 then (
3587 let (s, level) = source#getitem row in
3588 let y = (row - m_first) * nfs in
3589 let x = 5.0 +. float (level + m_pan) *. ww in
3590 let (first, last) = minfo.(row) in
3591 let prefix = String.sub s 0 first in
3592 let suffix = String.sub s first (last - first) in
3593 let w1 = measurestr fstate.fontsize prefix in
3594 let w2 = measurestr fstate.fontsize suffix in
3595 let x0 = x +. w1
3596 and y0 = (float (y+2)) in
3597 let x1 = x0 +. w2
3598 and y1 = (float (y+fs+3)) in
3599 filledrect x0 y0 x1 y1;
3600 loop (row+1)
3604 Gl.disable `texture_2d;
3605 if Array.length minfo > 0 then loop m_first;
3606 Gl.disable `blend;
3608 method updownlevel incr =
3609 let len = source#getitemcount in
3610 let curlevel =
3611 if m_active >= 0 && m_active < len
3612 then snd (source#getitem m_active)
3613 else -1
3615 let rec flow i =
3616 if i = len then i-1 else if i = -1 then 0 else
3617 let _, l = source#getitem i in
3618 if l != curlevel then i else flow (i+incr)
3620 let active = flow m_active in
3621 let first = calcfirst m_first active in
3622 G.postRedisplay "outline updownlevel";
3623 {< m_active = active; m_first = first >}
3625 method private key1 key mask =
3626 let set1 active first qsearch =
3627 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3629 let search active pattern incr =
3630 let active = if active = -1 then m_first else active in
3631 let dosearch re =
3632 let rec loop n =
3633 if n >= 0 && n < source#getitemcount
3634 then (
3635 let s, _ = source#getitem n in
3637 (try ignore (Str.search_forward re s 0); true
3638 with Not_found -> false)
3639 then Some n
3640 else loop (n + incr)
3642 else None
3644 loop active
3647 let re = Str.regexp_case_fold pattern in
3648 dosearch re
3649 with Failure s ->
3650 state.text <- s;
3651 None
3653 let itemcount = source#getitemcount in
3654 let find start incr =
3655 let rec find i =
3656 if i = -1 || i = itemcount
3657 then -1
3658 else (
3659 if source#hasaction i
3660 then i
3661 else find (i + incr)
3664 find start
3666 let set active first =
3667 let first = bound first 0 (itemcount - fstate.maxrows) in
3668 state.text <- "";
3669 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3671 let navigate incr =
3672 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3673 let active, first =
3674 let incr1 = if incr > 0 then 1 else -1 in
3675 if isvisible m_first m_active
3676 then
3677 let next =
3678 let next = m_active + incr in
3679 let next =
3680 if next < 0 || next >= itemcount
3681 then -1
3682 else find next incr1
3684 if abs (m_active - next) > fstate.maxrows
3685 then -1
3686 else next
3688 if next = -1
3689 then
3690 let first = m_first + incr in
3691 let first = bound first 0 (itemcount - fstate.maxrows) in
3692 let next =
3693 let next = m_active + incr in
3694 let next = bound next 0 (itemcount - 1) in
3695 find next ~-incr1
3697 let active =
3698 if next = -1
3699 then m_active
3700 else (
3701 if isvisible first next
3702 then next
3703 else m_active
3706 active, first
3707 else
3708 let first = min next m_first in
3709 let first =
3710 if abs (next - first) > fstate.maxrows
3711 then first + incr
3712 else first
3714 next, first
3715 else
3716 let first = m_first + incr in
3717 let first = bound first 0 (itemcount - 1) in
3718 let active =
3719 let next = m_active + incr in
3720 let next = bound next 0 (itemcount - 1) in
3721 let next = find next incr1 in
3722 let active =
3723 if next = -1 || abs (m_active - first) > fstate.maxrows
3724 then (
3725 let active = if m_active = -1 then next else m_active in
3726 active
3728 else next
3730 if isvisible first active
3731 then active
3732 else -1
3734 active, first
3736 G.postRedisplay "listview navigate";
3737 set active first;
3739 match key with
3740 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3741 let incr = if key = 0x72 then -1 else 1 in
3742 let active, first =
3743 match search (m_active + incr) m_qsearch incr with
3744 | None ->
3745 state.text <- m_qsearch ^ " [not found]";
3746 m_active, m_first
3747 | Some active ->
3748 state.text <- m_qsearch;
3749 active, firstof m_first active
3751 G.postRedisplay "listview ctrl-r/s";
3752 set1 active first m_qsearch;
3754 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3755 if m_active >= 0 && m_active < source#getitemcount
3756 then (
3757 let s, _ = source#getitem m_active in
3758 selstring s;
3760 coe self
3762 | 0xff08 -> (* backspace *)
3763 if emptystr m_qsearch
3764 then coe self
3765 else (
3766 let qsearch = withoutlastutf8 m_qsearch in
3767 if emptystr qsearch
3768 then (
3769 state.text <- "";
3770 G.postRedisplay "listview empty qsearch";
3771 set1 m_active m_first "";
3773 else
3774 let active, first =
3775 match search m_active qsearch ~-1 with
3776 | None ->
3777 state.text <- qsearch ^ " [not found]";
3778 m_active, m_first
3779 | Some active ->
3780 state.text <- qsearch;
3781 active, firstof m_first active
3783 G.postRedisplay "listview backspace qsearch";
3784 set1 active first qsearch
3787 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3788 let pattern = m_qsearch ^ toutf8 key in
3789 let active, first =
3790 match search m_active pattern 1 with
3791 | None ->
3792 state.text <- pattern ^ " [not found]";
3793 m_active, m_first
3794 | Some active ->
3795 state.text <- pattern;
3796 active, firstof m_first active
3798 G.postRedisplay "listview qsearch add";
3799 set1 active first pattern;
3801 | 0xff1b -> (* escape *)
3802 state.text <- "";
3803 if emptystr m_qsearch
3804 then (
3805 G.postRedisplay "list view escape";
3806 begin
3807 match
3808 source#exit (coe self) true m_active m_first m_pan
3809 with
3810 | None -> m_prev_uioh
3811 | Some uioh -> uioh
3814 else (
3815 G.postRedisplay "list view kill qsearch";
3816 coe {< m_qsearch = "" >}
3819 | 0xff0d | 0xff8d -> (* (kp) enter *)
3820 state.text <- "";
3821 let self = {< m_qsearch = "" >} in
3822 let opt =
3823 G.postRedisplay "listview enter";
3824 if m_active >= 0 && m_active < source#getitemcount
3825 then (
3826 source#exit (coe self) false m_active m_first m_pan;
3828 else (
3829 source#exit (coe self) true m_active m_first m_pan;
3832 begin match opt with
3833 | None -> m_prev_uioh
3834 | Some uioh -> uioh
3837 | 0xff9f | 0xffff -> (* (kp) delete *)
3838 coe self
3840 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3841 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3842 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3843 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3845 | 0xff53 | 0xff98 -> (* (kp) right *)
3846 state.text <- "";
3847 G.postRedisplay "listview right";
3848 coe {< m_pan = m_pan - 1 >}
3850 | 0xff51 | 0xff96 -> (* (kp) left *)
3851 state.text <- "";
3852 G.postRedisplay "listview left";
3853 coe {< m_pan = m_pan + 1 >}
3855 | 0xff50 | 0xff95 -> (* (kp) home *)
3856 let active = find 0 1 in
3857 G.postRedisplay "listview home";
3858 set active 0;
3860 | 0xff57 | 0xff9c -> (* (kp) end *)
3861 let first = max 0 (itemcount - fstate.maxrows) in
3862 let active = find (itemcount - 1) ~-1 in
3863 G.postRedisplay "listview end";
3864 set active first;
3866 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3867 coe self
3869 | _ ->
3870 dolog "listview unknown key %#x" key; coe self
3872 method key key mask =
3873 match state.mode with
3874 | Textentry te -> textentrykeyboard key mask te; coe self
3875 | _ -> self#key1 key mask
3877 method button button down x y _ =
3878 let opt =
3879 match button with
3880 | 1 when x > state.winw - conf.scrollbw ->
3881 G.postRedisplay "listview scroll";
3882 if down
3883 then
3884 let _, position, sh = self#scrollph in
3885 if y > truncate position && y < truncate (position +. sh)
3886 then (
3887 state.mstate <- Mscrolly;
3888 Some (coe self)
3890 else
3891 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3892 let first = truncate (s *. float source#getitemcount) in
3893 let first = min source#getitemcount first in
3894 Some (coe {< m_first = first; m_active = first >})
3895 else (
3896 state.mstate <- Mnone;
3897 Some (coe self);
3899 | 1 when not down ->
3900 begin match self#elemunder y with
3901 | Some n ->
3902 G.postRedisplay "listview click";
3903 source#exit (coe {< m_active = n >}) false n m_first m_pan
3904 | _ ->
3905 Some (coe self)
3907 | n when (n == 4 || n == 5) && not down ->
3908 let len = source#getitemcount in
3909 let first =
3910 if n = 5 && m_first + fstate.maxrows >= len
3911 then
3912 m_first
3913 else
3914 let first = m_first + (if n == 4 then -1 else 1) in
3915 bound first 0 (len - 1)
3917 G.postRedisplay "listview wheel";
3918 Some (coe {< m_first = first >})
3919 | n when (n = 6 || n = 7) && not down ->
3920 let inc = if n = 7 then -1 else 1 in
3921 G.postRedisplay "listview hwheel";
3922 Some (coe {< m_pan = m_pan + inc >})
3923 | _ ->
3924 Some (coe self)
3926 match opt with
3927 | None -> m_prev_uioh
3928 | Some uioh -> uioh
3930 method multiclick _ x y = self#button 1 true x y
3932 method motion _ y =
3933 match state.mstate with
3934 | Mscrolly ->
3935 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3936 let first = truncate (s *. float source#getitemcount) in
3937 let first = min source#getitemcount first in
3938 G.postRedisplay "listview motion";
3939 coe {< m_first = first; m_active = first >}
3940 | _ -> coe self
3942 method pmotion x y =
3943 if x < state.winw - conf.scrollbw
3944 then
3945 let n =
3946 match self#elemunder y with
3947 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3948 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3950 let o =
3951 if n != m_active
3952 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3953 else self
3955 coe o
3956 else (
3957 Wsi.setcursor Wsi.CURSOR_INHERIT;
3958 coe self
3961 method infochanged _ = ()
3963 method scrollpw = (0, 0.0, 0.0)
3964 method scrollph =
3965 let nfs = fstate.fontsize + 1 in
3966 let y = m_first * nfs in
3967 let itemcount = source#getitemcount in
3968 let maxi = max 0 (itemcount - fstate.maxrows) in
3969 let maxy = maxi * nfs in
3970 let p, h = scrollph y maxy in
3971 conf.scrollbw, p, h
3973 method modehash = modehash
3974 method eformsgs = false
3975 end;;
3977 class outlinelistview ~source =
3978 let settext autonarrow s =
3979 if autonarrow
3980 then
3981 let ss = source#statestr in
3982 state.text <-
3983 if emptystr ss
3984 then "[" ^ s ^ "]"
3985 else "{" ^ ss ^ "} [" ^ s ^ "]"
3986 else state.text <- s
3988 object (self)
3989 inherit listview
3990 ~source:(source :> lvsource)
3991 ~trusted:false
3992 ~modehash:(findkeyhash conf "outline")
3993 as super
3995 val m_autonarrow = false
3997 method display = super#display
3999 method key key mask =
4000 let maxrows =
4001 if emptystr state.text
4002 then fstate.maxrows
4003 else fstate.maxrows - 2
4005 let calcfirst first active =
4006 if active > first
4007 then
4008 let rows = active - first in
4009 if rows > maxrows then active - maxrows else first
4010 else active
4012 let navigate incr =
4013 let active = m_active + incr in
4014 let active = bound active 0 (source#getitemcount - 1) in
4015 let first = calcfirst m_first active in
4016 G.postRedisplay "outline navigate";
4017 coe {< m_active = active; m_first = first >}
4019 let navscroll first =
4020 let active =
4021 let dist = m_active - first in
4022 if dist < 0
4023 then first
4024 else (
4025 if dist < maxrows
4026 then m_active
4027 else first + maxrows
4030 G.postRedisplay "outline navscroll";
4031 coe {< m_first = first; m_active = active >}
4033 let ctrl = Wsi.withctrl mask in
4034 match key with
4035 | 97 when ctrl -> (* ctrl-a *)
4036 let text =
4037 if m_autonarrow
4038 then (source#denarrow; "")
4039 else (
4040 let pattern = source#renarrow in
4041 if nonemptystr m_qsearch
4042 then (source#narrow m_qsearch; m_qsearch)
4043 else pattern
4046 settext (not m_autonarrow) text;
4047 G.postRedisplay "toggle auto narrowing";
4048 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
4050 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
4051 settext true "";
4052 G.postRedisplay "toggle auto narrowing";
4053 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
4055 | 110 when ctrl -> (* ctrl-n *)
4056 source#narrow m_qsearch;
4057 if not m_autonarrow
4058 then source#add_narrow_pattern m_qsearch;
4059 G.postRedisplay "outline ctrl-n";
4060 coe {< m_first = 0; m_active = 0 >}
4062 | 83 when ctrl -> (* ctrl-S *)
4063 let active = source#calcactive (getanchor ()) in
4064 let first = firstof m_first active in
4065 G.postRedisplay "outline ctrl-s";
4066 coe {< m_first = first; m_active = active >}
4068 | 117 when ctrl -> (* ctrl-u *)
4069 G.postRedisplay "outline ctrl-u";
4070 if m_autonarrow && nonemptystr m_qsearch
4071 then (
4072 ignore (source#renarrow);
4073 settext m_autonarrow "";
4074 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4076 else (
4077 source#del_narrow_pattern;
4078 let pattern = source#renarrow in
4079 let text =
4080 if emptystr pattern then "" else "Narrowed to " ^ pattern
4082 settext m_autonarrow text;
4083 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4086 | 108 when ctrl -> (* ctrl-l *)
4087 let first = max 0 (m_active - (fstate.maxrows / 2)) in
4088 G.postRedisplay "outline ctrl-l";
4089 coe {< m_first = first >}
4091 | 0xff09 when m_autonarrow -> (* tab *)
4092 if nonemptystr m_qsearch
4093 then (
4094 G.postRedisplay "outline list view tab";
4095 source#add_narrow_pattern m_qsearch;
4096 settext true "";
4097 coe {< m_qsearch = "" >}
4099 else coe self
4101 | 0xff1b when m_autonarrow -> (* escape *)
4102 if nonemptystr m_qsearch
4103 then source#add_narrow_pattern m_qsearch;
4104 super#key key mask
4106 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
4107 if nonemptystr m_qsearch
4108 then source#add_narrow_pattern m_qsearch;
4109 super#key key mask
4111 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
4112 let pattern = m_qsearch ^ toutf8 key in
4113 G.postRedisplay "outlinelistview autonarrow add";
4114 source#narrow pattern;
4115 settext true pattern;
4116 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4118 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4119 if emptystr m_qsearch
4120 then coe self
4121 else
4122 let pattern = withoutlastutf8 m_qsearch in
4123 G.postRedisplay "outlinelistview autonarrow backspace";
4124 ignore (source#renarrow);
4125 source#narrow pattern;
4126 settext true pattern;
4127 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4129 | 0xff9f | 0xffff -> (* (kp) delete *)
4130 source#remove m_active;
4131 G.postRedisplay "outline delete";
4132 let active = max 0 (m_active-1) in
4133 coe {< m_first = firstof m_first active;
4134 m_active = active >}
4136 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4137 navscroll (max 0 (m_first - 1))
4139 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4140 navscroll (min (source#getitemcount - 1) (m_first + 1))
4142 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4143 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4144 | 0xff55 | 0xff9a -> (* (kp) prior *)
4145 navigate ~-(fstate.maxrows)
4146 | 0xff56 | 0xff9b -> (* (kp) next *)
4147 navigate fstate.maxrows
4149 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4150 let o =
4151 if ctrl
4152 then (
4153 G.postRedisplay "outline ctrl right";
4154 {< m_pan = m_pan + 1 >}
4156 else self#updownlevel 1
4158 coe o
4160 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4161 let o =
4162 if ctrl
4163 then (
4164 G.postRedisplay "outline ctrl left";
4165 {< m_pan = m_pan - 1 >}
4167 else self#updownlevel ~-1
4169 coe o
4171 | 0xff50 | 0xff95 -> (* (kp) home *)
4172 G.postRedisplay "outline home";
4173 coe {< m_first = 0; m_active = 0 >}
4175 | 0xff57 | 0xff9c -> (* (kp) end *)
4176 let active = source#getitemcount - 1 in
4177 let first = max 0 (active - fstate.maxrows) in
4178 G.postRedisplay "outline end";
4179 coe {< m_active = active; m_first = first >}
4181 | _ -> super#key key mask
4184 let gotounder under =
4185 let getpath filename =
4186 let path =
4187 if nonemptystr filename
4188 then
4189 if Filename.is_relative filename
4190 then
4191 let dir = Filename.dirname state.path in
4192 let dir =
4193 if Filename.is_implicit dir
4194 then Filename.concat (Sys.getcwd ()) dir
4195 else dir
4197 Filename.concat dir filename
4198 else filename
4199 else ""
4201 if Sys.file_exists path
4202 then path
4203 else ""
4205 match under with
4206 | Ulinkgoto (pageno, top) ->
4207 if pageno >= 0
4208 then (
4209 addnav ();
4210 gotopage1 pageno top;
4213 | Ulinkuri s ->
4214 gotouri s
4216 | Uremote (filename, pageno) ->
4217 let path = getpath filename in
4218 if nonemptystr path
4219 then (
4220 if conf.riani
4221 then
4222 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4223 try popen command []
4224 with exn ->
4225 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4226 flush stderr;
4227 else
4228 let anchor = getanchor () in
4229 let ranchor = state.path, state.password, anchor, state.origin in
4230 state.origin <- "";
4231 state.anchor <- (pageno, 0.0, 0.0);
4232 state.ranchors <- ranchor :: state.ranchors;
4233 opendoc path "";
4235 else showtext '!' ("Could not find " ^ filename)
4237 | Uremotedest (filename, destname) ->
4238 let path = getpath filename in
4239 if nonemptystr path
4240 then (
4241 if conf.riani
4242 then
4243 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4244 try popen command []
4245 with exn ->
4246 Printf.eprintf
4247 "failed to execute `%s': %s\n" command (exntos exn);
4248 flush stderr;
4249 else
4250 let anchor = getanchor () in
4251 let ranchor = state.path, state.password, anchor, state.origin in
4252 state.origin <- "";
4253 state.nameddest <- destname;
4254 state.ranchors <- ranchor :: state.ranchors;
4255 opendoc path "";
4257 else showtext '!' ("Could not find " ^ filename)
4259 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4262 let gotooutline (_, _, kind) =
4263 match kind with
4264 | Onone -> ()
4265 | Oanchor anchor ->
4266 let (pageno, y, _) = anchor in
4267 let y = getanchory
4268 (if conf.presentation then (pageno, y, 1.0) else anchor)
4270 addnav ();
4271 gotoghyll y
4272 | Ouri uri -> gotounder (Ulinkuri uri)
4273 | Olaunch cmd -> gotounder (Ulaunch cmd)
4274 | Oremote remote -> gotounder (Uremote remote)
4275 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4278 let outlinesource usebookmarks =
4279 let empty = [||] in
4280 (object (self)
4281 inherit lvsourcebase
4282 val mutable m_items = empty
4283 val mutable m_minfo = empty
4284 val mutable m_orig_items = empty
4285 val mutable m_orig_minfo = empty
4286 val mutable m_narrow_patterns = []
4287 val mutable m_hadremovals = false
4288 val mutable m_gen = -1
4290 method getitemcount =
4291 Array.length m_items + (if m_hadremovals then 1 else 0)
4293 method getitem n =
4294 if n == Array.length m_items && m_hadremovals
4295 then
4296 ("[Confirm removal]", 0)
4297 else
4298 let s, n, _ = m_items.(n) in
4299 (s, n)
4301 method exit ~uioh ~cancel ~active ~first ~pan =
4302 ignore (uioh, first);
4303 let confrimremoval = m_hadremovals && active = Array.length m_items in
4304 let items, minfo =
4305 if m_narrow_patterns = []
4306 then m_orig_items, m_orig_minfo
4307 else m_items, m_minfo
4309 if not cancel
4310 then (
4311 if not confrimremoval
4312 then (
4313 gotooutline m_items.(active);
4314 m_items <- items;
4315 m_minfo <- minfo;
4317 else (
4318 state.bookmarks <- Array.to_list m_items;
4319 m_orig_items <- m_items;
4320 m_orig_minfo <- m_minfo;
4323 else (
4324 m_items <- items;
4325 m_minfo <- minfo;
4327 m_pan <- pan;
4328 None
4330 method hasaction _ = true
4332 method greetmsg =
4333 if Array.length m_items != Array.length m_orig_items
4334 then
4335 let s =
4336 match m_narrow_patterns with
4337 | one :: [] -> one
4338 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4340 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4341 else ""
4343 method statestr =
4344 match m_narrow_patterns with
4345 | [] -> ""
4346 | one :: [] -> one
4347 | head :: _ -> "\xe2\x80\xa6" ^ head
4349 method narrow pattern =
4350 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4351 match reopt with
4352 | None -> ()
4353 | Some re ->
4354 let rec loop accu minfo n =
4355 if n = -1
4356 then (
4357 m_items <- Array.of_list accu;
4358 m_minfo <- Array.of_list minfo;
4360 else
4361 let (s, _, _) as o = m_items.(n) in
4362 let accu, minfo =
4363 let first =
4364 try Str.search_forward re s 0
4365 with Not_found -> -1
4367 if first >= 0
4368 then o :: accu, (first, Str.match_end ()) :: minfo
4369 else accu, minfo
4371 loop accu minfo (n-1)
4373 loop [] [] (Array.length m_items - 1)
4375 method getminfo = m_minfo
4377 method denarrow =
4378 m_orig_items <- (
4379 if usebookmarks
4380 then Array.of_list state.bookmarks
4381 else state.outlines
4383 m_minfo <- m_orig_minfo;
4384 m_items <- m_orig_items
4386 method remove m =
4387 if usebookmarks
4388 then
4389 if m >= 0 && m < Array.length m_items
4390 then (
4391 m_hadremovals <- true;
4392 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4393 let n = if n >= m then n+1 else n in
4394 m_items.(n)
4398 method add_narrow_pattern pattern =
4399 m_narrow_patterns <- pattern :: m_narrow_patterns
4401 method del_narrow_pattern =
4402 match m_narrow_patterns with
4403 | _ :: rest -> m_narrow_patterns <- rest
4404 | [] -> ()
4406 method renarrow =
4407 self#denarrow;
4408 match m_narrow_patterns with
4409 | pattern :: [] -> self#narrow pattern; pattern
4410 | list ->
4411 List.fold_left (fun accu pattern ->
4412 self#narrow pattern;
4413 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4415 method calcactive anchor =
4416 let rely = getanchory anchor in
4417 let rec loop n best bestd =
4418 if n = Array.length m_items
4419 then best
4420 else
4421 let _, _, kind = m_items.(n) in
4422 match kind with
4423 | Oanchor anchor ->
4424 let orely = getanchory anchor in
4425 let d = abs (orely - rely) in
4426 if d < bestd
4427 then loop (n+1) n d
4428 else loop (n+1) best bestd
4429 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
4430 loop (n+1) best bestd
4432 loop 0 ~-1 max_int
4434 method reset anchor items =
4435 m_hadremovals <- false;
4436 if state.gen != m_gen
4437 then (
4438 m_orig_items <- items;
4439 m_items <- items;
4440 m_narrow_patterns <- [];
4441 m_minfo <- empty;
4442 m_orig_minfo <- empty;
4443 m_gen <- state.gen;
4445 else (
4446 if items != m_orig_items
4447 then (
4448 m_orig_items <- items;
4449 if m_narrow_patterns == []
4450 then m_items <- items;
4453 let active = self#calcactive anchor in
4454 m_active <- active;
4455 m_first <- firstof m_first active
4456 end)
4459 let enterselector usebookmarks =
4460 resetmstate ();
4461 let source = outlinesource usebookmarks in
4462 fun errmsg ->
4463 let outlines =
4464 if usebookmarks
4465 then Array.of_list state.bookmarks
4466 else state.outlines
4468 if Array.length outlines = 0
4469 then (
4470 showtext ' ' errmsg;
4472 else (
4473 state.text <- source#greetmsg;
4474 Wsi.setcursor Wsi.CURSOR_INHERIT;
4475 let anchor = getanchor () in
4476 source#reset anchor outlines;
4477 state.uioh <- coe (new outlinelistview ~source);
4478 G.postRedisplay "enter selector";
4482 let enteroutlinemode =
4483 let f = enterselector false in
4484 fun ()-> f "Document has no outline";
4487 let enterbookmarkmode =
4488 let f = enterselector true in
4489 fun () -> f "Document has no bookmarks (yet)";
4492 let color_of_string s =
4493 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4494 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4498 let color_to_string (r, g, b) =
4499 let r = truncate (r *. 256.0)
4500 and g = truncate (g *. 256.0)
4501 and b = truncate (b *. 256.0) in
4502 Printf.sprintf "%d/%d/%d" r g b
4505 let irect_of_string s =
4506 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4509 let irect_to_string (x0,y0,x1,y1) =
4510 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4513 let makecheckers () =
4514 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4515 following to say:
4516 converted by Issac Trotts. July 25, 2002 *)
4517 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4518 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4519 let id = GlTex.gen_texture () in
4520 GlTex.bind_texture `texture_2d id;
4521 GlPix.store (`unpack_alignment 1);
4522 GlTex.image2d image;
4523 List.iter (GlTex.parameter ~target:`texture_2d)
4524 [ `mag_filter `nearest; `min_filter `nearest ];
4528 let setcheckers enabled =
4529 match state.texid with
4530 | None ->
4531 if enabled then state.texid <- Some (makecheckers ())
4533 | Some texid ->
4534 if not enabled
4535 then (
4536 GlTex.delete_texture texid;
4537 state.texid <- None;
4541 let int_of_string_with_suffix s =
4542 let l = String.length s in
4543 let s1, shift =
4544 if l > 1
4545 then
4546 let suffix = Char.lowercase s.[l-1] in
4547 match suffix with
4548 | 'k' -> String.sub s 0 (l-1), 10
4549 | 'm' -> String.sub s 0 (l-1), 20
4550 | 'g' -> String.sub s 0 (l-1), 30
4551 | _ -> s, 0
4552 else s, 0
4554 let n = int_of_string s1 in
4555 let m = n lsl shift in
4556 if m < 0 || m < n
4557 then raise (Failure "value too large")
4558 else m
4561 let string_with_suffix_of_int n =
4562 if n = 0
4563 then "0"
4564 else
4565 let units = [(30, "G"); (20, "M"); (10, "K")] in
4566 let prettyint n =
4567 let rec loop s n =
4568 let h = n mod 1000 in
4569 let n = n / 1000 in
4570 if n = 0
4571 then string_of_int h ^ s
4572 else (
4573 let s = Printf.sprintf "_%03d%s" h s in
4574 loop s n
4577 loop "" n
4579 let rec find = function
4580 | [] -> prettyint n
4581 | (shift, suffix) :: rest ->
4582 if (n land ((1 lsl shift) - 1)) = 0
4583 then prettyint (n lsr shift) ^ suffix
4584 else find rest
4586 find units
4589 let defghyllscroll = (40, 8, 32);;
4590 let fastghyllscroll = (5,1,2);;
4591 let neatghyllscroll = (10,1,9);;
4592 let ghyllscroll_of_string s =
4593 match s with
4594 | "default" -> Some defghyllscroll
4595 | "fast" -> Some (5,1,2)
4596 | "neat" -> Some (10,1,9)
4597 | "" | "none" -> None
4598 | _ ->
4599 let (n,a,b) as nab =
4600 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4601 if n <= a || n <= b || a >= b
4602 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4603 n a b;
4604 Some nab
4607 let ghyllscroll_to_string ((n, a, b) as nab) =
4608 (**) if nab = defghyllscroll then "default"
4609 else if nab = fastghyllscroll then "fast"
4610 else if nab = neatghyllscroll then "neat"
4611 else Printf.sprintf "%d,%d,%d" n a b;
4614 let describe_location () =
4615 let fn = page_of_y state.y in
4616 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4617 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4618 let percent =
4619 if maxy <= 0
4620 then 100.
4621 else (100. *. (float state.y /. float maxy))
4623 if fn = ln
4624 then
4625 Printf.sprintf "page %d of %d [%.2f%%]"
4626 (fn+1) state.pagecount percent
4627 else
4628 Printf.sprintf
4629 "pages %d-%d of %d [%.2f%%]"
4630 (fn+1) (ln+1) state.pagecount percent
4633 let setpresentationmode v =
4634 let n = page_of_y state.y in
4635 state.anchor <- (n, 0.0, 1.0);
4636 conf.presentation <- v;
4637 if conf.fitmodel = FitPage
4638 then reqlayout conf.angle conf.fitmodel;
4639 represent ();
4642 let enterinfomode =
4643 let btos b = if b then "\xe2\x88\x9a" else "" in
4644 let showextended = ref false in
4645 let leave mode = function
4646 | Confirm -> state.mode <- mode
4647 | Cancel -> state.mode <- mode in
4648 let src =
4649 (object
4650 val mutable m_first_time = true
4651 val mutable m_l = []
4652 val mutable m_a = [||]
4653 val mutable m_prev_uioh = nouioh
4654 val mutable m_prev_mode = View
4656 inherit lvsourcebase
4658 method reset prev_mode prev_uioh =
4659 m_a <- Array.of_list (List.rev m_l);
4660 m_l <- [];
4661 m_prev_mode <- prev_mode;
4662 m_prev_uioh <- prev_uioh;
4663 if m_first_time
4664 then (
4665 let rec loop n =
4666 if n >= Array.length m_a
4667 then ()
4668 else
4669 match m_a.(n) with
4670 | _, _, _, Action _ -> m_active <- n
4671 | _ -> loop (n+1)
4673 loop 0;
4674 m_first_time <- false;
4677 method int name get set =
4678 m_l <-
4679 (name, `int get, 1, Action (
4680 fun u ->
4681 let ondone s =
4682 try set (int_of_string s)
4683 with exn ->
4684 state.text <- Printf.sprintf "bad integer `%s': %s"
4685 s (exntos exn)
4687 state.text <- "";
4688 let te = name ^ ": ", "", None, intentry, ondone, true in
4689 state.mode <- Textentry (te, leave m_prev_mode);
4691 )) :: m_l
4693 method int_with_suffix name get set =
4694 m_l <-
4695 (name, `intws get, 1, Action (
4696 fun u ->
4697 let ondone s =
4698 try set (int_of_string_with_suffix s)
4699 with exn ->
4700 state.text <- Printf.sprintf "bad integer `%s': %s"
4701 s (exntos exn)
4703 state.text <- "";
4704 let te =
4705 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4707 state.mode <- Textentry (te, leave m_prev_mode);
4709 )) :: m_l
4711 method bool ?(offset=1) ?(btos=btos) name get set =
4712 m_l <-
4713 (name, `bool (btos, get), offset, Action (
4714 fun u ->
4715 let v = get () in
4716 set (not v);
4718 )) :: m_l
4720 method color name get set =
4721 m_l <-
4722 (name, `color get, 1, Action (
4723 fun u ->
4724 let invalid = (nan, nan, nan) in
4725 let ondone s =
4726 let c =
4727 try color_of_string s
4728 with exn ->
4729 state.text <- Printf.sprintf "bad color `%s': %s"
4730 s (exntos exn);
4731 invalid
4733 if c <> invalid
4734 then set c;
4736 let te = name ^ ": ", "", None, textentry, ondone, true in
4737 state.text <- color_to_string (get ());
4738 state.mode <- Textentry (te, leave m_prev_mode);
4740 )) :: m_l
4742 method string name get set =
4743 m_l <-
4744 (name, `string get, 1, Action (
4745 fun u ->
4746 let ondone s = set s in
4747 let te = name ^ ": ", "", None, textentry, ondone, true in
4748 state.mode <- Textentry (te, leave m_prev_mode);
4750 )) :: m_l
4752 method colorspace name get set =
4753 m_l <-
4754 (name, `string get, 1, Action (
4755 fun _ ->
4756 let source =
4757 (object
4758 inherit lvsourcebase
4760 initializer
4761 m_active <- CSTE.to_int conf.colorspace;
4762 m_first <- 0;
4764 method getitemcount =
4765 Array.length CSTE.names
4766 method getitem n =
4767 (CSTE.names.(n), 0)
4768 method exit ~uioh ~cancel ~active ~first ~pan =
4769 ignore (uioh, first, pan);
4770 if not cancel then set active;
4771 None
4772 method hasaction _ = true
4773 end)
4775 state.text <- "";
4776 let modehash = findkeyhash conf "info" in
4777 coe (new listview ~source ~trusted:true ~modehash)
4778 )) :: m_l
4780 method paxmark name get set =
4781 m_l <-
4782 (name, `string get, 1, Action (
4783 fun _ ->
4784 let source =
4785 (object
4786 inherit lvsourcebase
4788 initializer
4789 m_active <- MTE.to_int conf.paxmark;
4790 m_first <- 0;
4792 method getitemcount = Array.length MTE.names
4793 method getitem n = (MTE.names.(n), 0)
4794 method exit ~uioh ~cancel ~active ~first ~pan =
4795 ignore (uioh, first, pan);
4796 if not cancel then set active;
4797 None
4798 method hasaction _ = true
4799 end)
4801 state.text <- "";
4802 let modehash = findkeyhash conf "info" in
4803 coe (new listview ~source ~trusted:true ~modehash)
4804 )) :: m_l
4806 method fitmodel name get set =
4807 m_l <-
4808 (name, `string get, 1, Action (
4809 fun _ ->
4810 let source =
4811 (object
4812 inherit lvsourcebase
4814 initializer
4815 m_active <- FMTE.to_int conf.fitmodel;
4816 m_first <- 0;
4818 method getitemcount = Array.length FMTE.names
4819 method getitem n = (FMTE.names.(n), 0)
4820 method exit ~uioh ~cancel ~active ~first ~pan =
4821 ignore (uioh, first, pan);
4822 if not cancel then set active;
4823 None
4824 method hasaction _ = true
4825 end)
4827 state.text <- "";
4828 let modehash = findkeyhash conf "info" in
4829 coe (new listview ~source ~trusted:true ~modehash)
4830 )) :: m_l
4832 method caption s offset =
4833 m_l <- (s, `empty, offset, Noaction) :: m_l
4835 method caption2 s f offset =
4836 m_l <- (s, `string f, offset, Noaction) :: m_l
4838 method getitemcount = Array.length m_a
4840 method getitem n =
4841 let tostr = function
4842 | `int f -> string_of_int (f ())
4843 | `intws f -> string_with_suffix_of_int (f ())
4844 | `string f -> f ()
4845 | `color f -> color_to_string (f ())
4846 | `bool (btos, f) -> btos (f ())
4847 | `empty -> ""
4849 let name, t, offset, _ = m_a.(n) in
4850 ((let s = tostr t in
4851 if nonemptystr s
4852 then Printf.sprintf "%s\t%s" name s
4853 else name),
4854 offset)
4856 method exit ~uioh ~cancel ~active ~first ~pan =
4857 let uiohopt =
4858 if not cancel
4859 then (
4860 let uioh =
4861 match m_a.(active) with
4862 | _, _, _, Action f -> f uioh
4863 | _ -> uioh
4865 Some uioh
4867 else None
4869 m_active <- active;
4870 m_first <- first;
4871 m_pan <- pan;
4872 uiohopt
4874 method hasaction n =
4875 match m_a.(n) with
4876 | _, _, _, Action _ -> true
4877 | _ -> false
4878 end)
4880 let rec fillsrc prevmode prevuioh =
4881 let sep () = src#caption "" 0 in
4882 let colorp name get set =
4883 src#string name
4884 (fun () -> color_to_string (get ()))
4885 (fun v ->
4887 let c = color_of_string v in
4888 set c
4889 with exn ->
4890 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4893 let oldmode = state.mode in
4894 let birdseye = isbirdseye state.mode in
4896 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4898 src#bool "presentation mode"
4899 (fun () -> conf.presentation)
4900 (fun v -> setpresentationmode v);
4902 src#bool "ignore case in searches"
4903 (fun () -> conf.icase)
4904 (fun v -> conf.icase <- v);
4906 src#bool "preload"
4907 (fun () -> conf.preload)
4908 (fun v -> conf.preload <- v);
4910 src#bool "highlight links"
4911 (fun () -> conf.hlinks)
4912 (fun v -> conf.hlinks <- v);
4914 src#bool "under info"
4915 (fun () -> conf.underinfo)
4916 (fun v -> conf.underinfo <- v);
4918 src#bool "persistent bookmarks"
4919 (fun () -> conf.savebmarks)
4920 (fun v -> conf.savebmarks <- v);
4922 src#fitmodel "fit model"
4923 (fun () -> FMTE.to_string conf.fitmodel)
4924 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4926 src#bool "trim margins"
4927 (fun () -> conf.trimmargins)
4928 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4930 src#bool "persistent location"
4931 (fun () -> conf.jumpback)
4932 (fun v -> conf.jumpback <- v);
4934 sep ();
4935 src#int "inter-page space"
4936 (fun () -> conf.interpagespace)
4937 (fun n ->
4938 conf.interpagespace <- n;
4939 docolumns conf.columns;
4940 let pageno, py =
4941 match state.layout with
4942 | [] -> 0, 0
4943 | l :: _ ->
4944 l.pageno, l.pagey
4946 state.maxy <- calcheight ();
4947 let y = getpagey pageno in
4948 gotoy (y + py)
4951 src#int "page bias"
4952 (fun () -> conf.pagebias)
4953 (fun v -> conf.pagebias <- v);
4955 src#int "scroll step"
4956 (fun () -> conf.scrollstep)
4957 (fun n -> conf.scrollstep <- n);
4959 src#int "horizontal scroll step"
4960 (fun () -> conf.hscrollstep)
4961 (fun v -> conf.hscrollstep <- v);
4963 src#int "auto scroll step"
4964 (fun () ->
4965 match state.autoscroll with
4966 | Some step -> step
4967 | _ -> conf.autoscrollstep)
4968 (fun n ->
4969 let n = boundastep state.winh n in
4970 if state.autoscroll <> None
4971 then state.autoscroll <- Some n;
4972 conf.autoscrollstep <- n);
4974 src#int "zoom"
4975 (fun () -> truncate (conf.zoom *. 100.))
4976 (fun v -> setzoom ((float v) /. 100.));
4978 src#int "rotation"
4979 (fun () -> conf.angle)
4980 (fun v -> reqlayout v conf.fitmodel);
4982 src#int "scroll bar width"
4983 (fun () -> conf.scrollbw)
4984 (fun v ->
4985 conf.scrollbw <- v;
4986 reshape state.winw state.winh;
4989 src#int "scroll handle height"
4990 (fun () -> conf.scrollh)
4991 (fun v -> conf.scrollh <- v;);
4993 src#int "thumbnail width"
4994 (fun () -> conf.thumbw)
4995 (fun v ->
4996 conf.thumbw <- min 4096 v;
4997 match oldmode with
4998 | Birdseye beye ->
4999 leavebirdseye beye false;
5000 enterbirdseye ()
5001 | _ -> ()
5004 let mode = state.mode in
5005 src#string "columns"
5006 (fun () ->
5007 match conf.columns with
5008 | Csingle _ -> "1"
5009 | Cmulti (multi, _) -> multicolumns_to_string multi
5010 | Csplit (count, _) -> "-" ^ string_of_int count
5012 (fun v ->
5013 let n, a, b = multicolumns_of_string v in
5014 setcolumns mode n a b);
5016 sep ();
5017 src#caption "Pixmap cache" 0;
5018 src#int_with_suffix "size (advisory)"
5019 (fun () -> conf.memlimit)
5020 (fun v -> conf.memlimit <- v);
5022 src#caption2 "used"
5023 (fun () -> Printf.sprintf "%s bytes, %d tiles"
5024 (string_with_suffix_of_int state.memused)
5025 (Hashtbl.length state.tilemap)) 1;
5027 sep ();
5028 src#caption "Layout" 0;
5029 src#caption2 "Dimension"
5030 (fun () ->
5031 Printf.sprintf "%dx%d (virtual %dx%d)"
5032 state.winw state.winh
5033 state.w state.maxy)
5035 if conf.debug
5036 then
5037 src#caption2 "Position" (fun () ->
5038 Printf.sprintf "%dx%d" state.x state.y
5040 else
5041 src#caption2 "Position" (fun () -> describe_location ()) 1
5044 sep ();
5045 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
5046 "Save these parameters as global defaults at exit"
5047 (fun () -> conf.bedefault)
5048 (fun v -> conf.bedefault <- v)
5051 sep ();
5052 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
5053 src#bool ~offset:0 ~btos "Extended parameters"
5054 (fun () -> !showextended)
5055 (fun v -> showextended := v; fillsrc prevmode prevuioh);
5056 if !showextended
5057 then (
5058 src#bool "checkers"
5059 (fun () -> conf.checkers)
5060 (fun v -> conf.checkers <- v; setcheckers v);
5061 src#bool "update cursor"
5062 (fun () -> conf.updatecurs)
5063 (fun v -> conf.updatecurs <- v);
5064 src#bool "verbose"
5065 (fun () -> conf.verbose)
5066 (fun v -> conf.verbose <- v);
5067 src#bool "invert colors"
5068 (fun () -> conf.invert)
5069 (fun v -> conf.invert <- v);
5070 src#bool "max fit"
5071 (fun () -> conf.maxhfit)
5072 (fun v -> conf.maxhfit <- v);
5073 src#bool "redirect stderr"
5074 (fun () -> conf.redirectstderr)
5075 (fun v -> conf.redirectstderr <- v; redirectstderr ());
5076 src#bool "pax mode"
5077 (fun () -> conf.pax != None)
5078 (fun v ->
5079 if v
5080 then conf.pax <- Some (ref (now (), 0, 0))
5081 else conf.pax <- None);
5082 src#string "uri launcher"
5083 (fun () -> conf.urilauncher)
5084 (fun v -> conf.urilauncher <- v);
5085 src#string "path launcher"
5086 (fun () -> conf.pathlauncher)
5087 (fun v -> conf.pathlauncher <- v);
5088 src#string "tile size"
5089 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
5090 (fun v ->
5092 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
5093 conf.tilew <- max 64 w;
5094 conf.tileh <- max 64 h;
5095 flushtiles ();
5096 with exn ->
5097 state.text <- Printf.sprintf "bad tile size `%s': %s"
5098 v (exntos exn)
5100 src#int "texture count"
5101 (fun () -> conf.texcount)
5102 (fun v ->
5103 if realloctexts v
5104 then conf.texcount <- v
5105 else showtext '!' " Failed to set texture count please retry later"
5107 src#int "slice height"
5108 (fun () -> conf.sliceheight)
5109 (fun v ->
5110 conf.sliceheight <- v;
5111 wcmd "sliceh %d" conf.sliceheight;
5113 src#int "anti-aliasing level"
5114 (fun () -> conf.aalevel)
5115 (fun v ->
5116 conf.aalevel <- bound v 0 8;
5117 state.anchor <- getanchor ();
5118 opendoc state.path state.password;
5120 src#string "page scroll scaling factor"
5121 (fun () -> string_of_float conf.pgscale)
5122 (fun v ->
5124 let s = float_of_string v in
5125 conf.pgscale <- s
5126 with exn ->
5127 state.text <- Printf.sprintf
5128 "bad page scroll scaling factor `%s': %s" v (exntos exn)
5131 src#int "ui font size"
5132 (fun () -> fstate.fontsize)
5133 (fun v -> setfontsize (bound v 5 100));
5134 src#int "hint font size"
5135 (fun () -> conf.hfsize)
5136 (fun v -> conf.hfsize <- bound v 5 100);
5137 colorp "background color"
5138 (fun () -> conf.bgcolor)
5139 (fun v -> conf.bgcolor <- v);
5140 src#bool "crop hack"
5141 (fun () -> conf.crophack)
5142 (fun v -> conf.crophack <- v);
5143 src#string "trim fuzz"
5144 (fun () -> irect_to_string conf.trimfuzz)
5145 (fun v ->
5147 conf.trimfuzz <- irect_of_string v;
5148 if conf.trimmargins
5149 then settrim true conf.trimfuzz;
5150 with exn ->
5151 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
5153 src#string "throttle"
5154 (fun () ->
5155 match conf.maxwait with
5156 | None -> "show place holder if page is not ready"
5157 | Some time ->
5158 if time = infinity
5159 then "wait for page to fully render"
5160 else
5161 "wait " ^ string_of_float time
5162 ^ " seconds before showing placeholder"
5164 (fun v ->
5166 let f = float_of_string v in
5167 if f <= 0.0
5168 then conf.maxwait <- None
5169 else conf.maxwait <- Some f
5170 with exn ->
5171 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
5173 src#string "ghyll scroll"
5174 (fun () ->
5175 match conf.ghyllscroll with
5176 | None -> ""
5177 | Some nab -> ghyllscroll_to_string nab
5179 (fun v ->
5180 try conf.ghyllscroll <- ghyllscroll_of_string v
5181 with exn ->
5182 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
5184 src#string "selection command"
5185 (fun () -> conf.selcmd)
5186 (fun v -> conf.selcmd <- v);
5187 src#string "synctex command"
5188 (fun () -> conf.stcmd)
5189 (fun v -> conf.stcmd <- v);
5190 src#string "pax command"
5191 (fun () -> conf.paxcmd)
5192 (fun v -> conf.paxcmd <- v);
5193 src#colorspace "color space"
5194 (fun () -> CSTE.to_string conf.colorspace)
5195 (fun v ->
5196 conf.colorspace <- CSTE.of_int v;
5197 wcmd "cs %d" v;
5198 load state.layout;
5200 src#paxmark "pax mark method"
5201 (fun () -> MTE.to_string conf.paxmark)
5202 (fun v -> conf.paxmark <- MTE.of_int v);
5203 if pbousable ()
5204 then
5205 src#bool "use PBO"
5206 (fun () -> conf.usepbo)
5207 (fun v -> conf.usepbo <- v);
5208 src#bool "mouse wheel scrolls pages"
5209 (fun () -> conf.wheelbypage)
5210 (fun v -> conf.wheelbypage <- v);
5211 src#bool "open remote links in a new instance"
5212 (fun () -> conf.riani)
5213 (fun v -> conf.riani <- v);
5216 sep ();
5217 src#caption "Document" 0;
5218 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
5219 src#caption2 "Pages"
5220 (fun () -> string_of_int state.pagecount) 1;
5221 src#caption2 "Dimensions"
5222 (fun () -> string_of_int (List.length state.pdims)) 1;
5223 if conf.trimmargins
5224 then (
5225 sep ();
5226 src#caption "Trimmed margins" 0;
5227 src#caption2 "Dimensions"
5228 (fun () -> string_of_int (List.length state.pdims)) 1;
5231 sep ();
5232 src#caption "OpenGL" 0;
5233 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
5234 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
5236 sep ();
5237 src#caption "Location" 0;
5238 if nonemptystr state.origin
5239 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
5240 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5242 src#reset prevmode prevuioh;
5244 fun () ->
5245 state.text <- "";
5246 resetmstate ();
5247 let prevmode = state.mode
5248 and prevuioh = state.uioh in
5249 fillsrc prevmode prevuioh;
5250 let source = (src :> lvsource) in
5251 let modehash = findkeyhash conf "info" in
5252 state.uioh <- coe (object (self)
5253 inherit listview ~source ~trusted:true ~modehash as super
5254 val mutable m_prevmemused = 0
5255 method infochanged = function
5256 | Memused ->
5257 if m_prevmemused != state.memused
5258 then (
5259 m_prevmemused <- state.memused;
5260 G.postRedisplay "memusedchanged";
5262 | Pdim -> G.postRedisplay "pdimchanged"
5263 | Docinfo -> fillsrc prevmode prevuioh
5265 method key key mask =
5266 if not (Wsi.withctrl mask)
5267 then
5268 match key with
5269 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5270 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5271 | _ -> super#key key mask
5272 else super#key key mask
5273 end);
5274 G.postRedisplay "info";
5277 let enterhelpmode =
5278 let source =
5279 (object
5280 inherit lvsourcebase
5281 method getitemcount = Array.length state.help
5282 method getitem n =
5283 let s, l, _ = state.help.(n) in
5284 (s, l)
5286 method exit ~uioh ~cancel ~active ~first ~pan =
5287 let optuioh =
5288 if not cancel
5289 then (
5290 match state.help.(active) with
5291 | _, _, Action f -> Some (f uioh)
5292 | _ -> Some (uioh)
5294 else None
5296 m_active <- active;
5297 m_first <- first;
5298 m_pan <- pan;
5299 optuioh
5301 method hasaction n =
5302 match state.help.(n) with
5303 | _, _, Action _ -> true
5304 | _ -> false
5306 initializer
5307 m_active <- -1
5308 end)
5309 in fun () ->
5310 let modehash = findkeyhash conf "help" in
5311 resetmstate ();
5312 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5313 G.postRedisplay "help";
5316 let entermsgsmode =
5317 let msgsource =
5318 let re = Str.regexp "[\r\n]" in
5319 (object
5320 inherit lvsourcebase
5321 val mutable m_items = [||]
5323 method getitemcount = 1 + Array.length m_items
5325 method getitem n =
5326 if n = 0
5327 then "[Clear]", 0
5328 else m_items.(n-1), 0
5330 method exit ~uioh ~cancel ~active ~first ~pan =
5331 ignore uioh;
5332 if not cancel
5333 then (
5334 if active = 0
5335 then Buffer.clear state.errmsgs;
5337 m_active <- active;
5338 m_first <- first;
5339 m_pan <- pan;
5340 None
5342 method hasaction n =
5343 n = 0
5345 method reset =
5346 state.newerrmsgs <- false;
5347 let l = Str.split re (Buffer.contents state.errmsgs) in
5348 m_items <- Array.of_list l
5350 initializer
5351 m_active <- 0
5352 end)
5353 in fun () ->
5354 state.text <- "";
5355 resetmstate ();
5356 msgsource#reset;
5357 let source = (msgsource :> lvsource) in
5358 let modehash = findkeyhash conf "listview" in
5359 state.uioh <- coe (object
5360 inherit listview ~source ~trusted:false ~modehash as super
5361 method display =
5362 if state.newerrmsgs
5363 then msgsource#reset;
5364 super#display
5365 end);
5366 G.postRedisplay "msgs";
5369 let quickbookmark ?title () =
5370 match state.layout with
5371 | [] -> ()
5372 | l :: _ ->
5373 let title =
5374 match title with
5375 | None ->
5376 let sec = Unix.gettimeofday () in
5377 let tm = Unix.localtime sec in
5378 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5379 (l.pageno+1)
5380 tm.Unix.tm_mday
5381 tm.Unix.tm_mon
5382 (tm.Unix.tm_year + 1900)
5383 tm.Unix.tm_hour
5384 tm.Unix.tm_min
5385 | Some title -> title
5387 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5390 let setautoscrollspeed step goingdown =
5391 let incr = max 1 ((abs step) / 2) in
5392 let incr = if goingdown then incr else -incr in
5393 let astep = boundastep state.winh (step + incr) in
5394 state.autoscroll <- Some astep;
5397 let canpan () =
5398 match conf.columns with
5399 | Csplit _ -> true
5400 | _ -> state.x != 0 || conf.zoom > 1.0
5403 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5405 let existsinrow pageno (columns, coverA, coverB) p =
5406 let last = ((pageno - coverA) mod columns) + columns in
5407 let rec any = function
5408 | [] -> false
5409 | l :: rest ->
5410 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5411 then p l
5412 else (
5413 if not (p l)
5414 then (if l.pageno = last then false else any rest)
5415 else true
5418 any state.layout
5421 let nextpage () =
5422 match state.layout with
5423 | [] ->
5424 let pageno = page_of_y state.y in
5425 gotoghyll (getpagey (pageno+1))
5426 | l :: rest ->
5427 match conf.columns with
5428 | Csingle _ ->
5429 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5430 then
5431 let y = clamp (pgscale state.winh) in
5432 gotoghyll y
5433 else
5434 let pageno = min (l.pageno+1) (state.pagecount-1) in
5435 gotoghyll (getpagey pageno)
5436 | Cmulti ((c, _, _) as cl, _) ->
5437 if conf.presentation
5438 && (existsinrow l.pageno cl
5439 (fun l -> l.pageh > l.pagey + l.pagevh))
5440 then
5441 let y = clamp (pgscale state.winh) in
5442 gotoghyll y
5443 else
5444 let pageno = min (l.pageno+c) (state.pagecount-1) in
5445 gotoghyll (getpagey pageno)
5446 | Csplit (n, _) ->
5447 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5448 then
5449 let pagey, pageh = getpageyh l.pageno in
5450 let pagey = pagey + pageh * l.pagecol in
5451 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5452 gotoghyll (pagey + pageh + ips)
5455 let prevpage () =
5456 match state.layout with
5457 | [] ->
5458 let pageno = page_of_y state.y in
5459 gotoghyll (getpagey (pageno-1))
5460 | l :: _ ->
5461 match conf.columns with
5462 | Csingle _ ->
5463 if conf.presentation && l.pagey != 0
5464 then
5465 gotoghyll (clamp (pgscale ~-(state.winh)))
5466 else
5467 let pageno = max 0 (l.pageno-1) in
5468 gotoghyll (getpagey pageno)
5469 | Cmulti ((c, _, coverB) as cl, _) ->
5470 if conf.presentation &&
5471 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5472 then
5473 gotoghyll (clamp (pgscale ~-(state.winh)))
5474 else
5475 let decr =
5476 if l.pageno = state.pagecount - coverB
5477 then 1
5478 else c
5480 let pageno = max 0 (l.pageno-decr) in
5481 gotoghyll (getpagey pageno)
5482 | Csplit (n, _) ->
5483 let y =
5484 if l.pagecol = 0
5485 then
5486 if l.pageno = 0
5487 then l.pagey
5488 else
5489 let pageno = max 0 (l.pageno-1) in
5490 let pagey, pageh = getpageyh pageno in
5491 pagey + (n-1)*pageh
5492 else
5493 let pagey, pageh = getpageyh l.pageno in
5494 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5496 gotoghyll y
5499 let viewkeyboard key mask =
5500 let enttext te =
5501 let mode = state.mode in
5502 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5503 state.text <- "";
5504 enttext ();
5505 G.postRedisplay "view:enttext"
5507 let ctrl = Wsi.withctrl mask in
5508 let key =
5509 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5511 match key with
5512 | 81 -> (* Q *)
5513 exit 0
5515 | 0xff63 -> (* insert *)
5516 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5517 then (
5518 state.mode <- LinkNav (Ltgendir 0);
5519 gotoy state.y;
5521 else showtext '!' "Keyboard link navigation does not work under rotation"
5523 | 0xff1b | 113 -> (* escape / q *)
5524 begin match state.mstate with
5525 | Mzoomrect _ ->
5526 resetmstate ();
5527 G.postRedisplay "kill zoom rect";
5528 | _ ->
5529 begin match state.mode with
5530 | LinkNav _ ->
5531 state.mode <- View;
5532 G.postRedisplay "esc leave linknav"
5533 | _ ->
5534 match state.ranchors with
5535 | [] -> raise Quit
5536 | (path, password, anchor, origin) :: rest ->
5537 state.ranchors <- rest;
5538 state.anchor <- anchor;
5539 state.origin <- origin;
5540 state.nameddest <- "";
5541 opendoc path password
5542 end;
5543 end;
5545 | 0xff08 -> (* backspace *)
5546 gotoghyll (getnav ~-1)
5548 | 111 -> (* o *)
5549 enteroutlinemode ()
5551 | 117 -> (* u *)
5552 state.rects <- [];
5553 state.text <- "";
5554 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
5555 G.postRedisplay "dehighlight";
5557 | 47 | 63 -> (* / ? *)
5558 let ondone isforw s =
5559 cbput state.hists.pat s;
5560 state.searchpattern <- s;
5561 search s isforw
5563 let s = String.create 1 in
5564 s.[0] <- Char.chr key;
5565 enttext (s, "", Some (onhist state.hists.pat),
5566 textentry, ondone (key = 47), true)
5568 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5569 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5570 setzoom (conf.zoom +. incr)
5572 | 43 | 0xffab -> (* + *)
5573 let ondone s =
5574 let n =
5575 try int_of_string s with exc ->
5576 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5577 max_int
5579 if n != max_int
5580 then (
5581 conf.pagebias <- n;
5582 state.text <- "page bias is now " ^ string_of_int n;
5585 enttext ("page bias: ", "", None, intentry, ondone, true)
5587 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5588 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5589 setzoom (max 0.01 (conf.zoom -. decr))
5591 | 45 | 0xffad -> (* - *)
5592 let ondone msg = state.text <- msg in
5593 enttext (
5594 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5595 optentry state.mode, ondone, true
5598 | 48 when ctrl -> (* ctrl-0 *)
5599 if conf.zoom = 1.0
5600 then (
5601 state.x <- 0;
5602 gotoy state.y
5604 else setzoom 1.0
5606 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5607 let cols =
5608 match conf.columns with
5609 | Csingle _ | Cmulti _ -> 1
5610 | Csplit (n, _) -> n
5612 let h = state.winh -
5613 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5615 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5616 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5617 then setzoom zoom
5619 | 51 when ctrl -> (* ctrl-3 *)
5620 let fm =
5621 match conf.fitmodel with
5622 | FitWidth -> FitProportional
5623 | FitProportional -> FitPage
5624 | FitPage -> FitWidth
5626 state.text <- "fit model: " ^ FMTE.to_string fm;
5627 reqlayout conf.angle fm
5629 | 0xffc6 -> (* f9 *)
5630 togglebirdseye ()
5632 | 57 when ctrl -> (* ctrl-9 *)
5633 togglebirdseye ()
5635 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5636 when not ctrl -> (* 0..9 *)
5637 let ondone s =
5638 let n =
5639 try int_of_string s with exc ->
5640 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5643 if n >= 0
5644 then (
5645 addnav ();
5646 cbput state.hists.pag (string_of_int n);
5647 gotopage1 (n + conf.pagebias - 1) 0;
5650 let pageentry text key =
5651 match Char.unsafe_chr key with
5652 | 'g' -> TEdone text
5653 | _ -> intentry text key
5655 let text = "x" in text.[0] <- Char.chr key;
5656 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5658 | 98 -> (* b *)
5659 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5660 reshape state.winw state.winh;
5662 | 66 -> (* B *)
5663 state.bzoom <- not state.bzoom;
5664 state.rects <- [];
5665 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5667 | 108 -> (* l *)
5668 conf.hlinks <- not conf.hlinks;
5669 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5670 G.postRedisplay "toggle highlightlinks";
5672 | 70 -> (* F *)
5673 state.glinks <- true;
5674 let mode = state.mode in
5675 state.mode <- Textentry (
5676 (":", "", None, linknentry, linkndone gotounder, false),
5677 (fun _ ->
5678 state.glinks <- false;
5679 state.mode <- mode)
5681 state.text <- "";
5682 G.postRedisplay "view:linkent(F)"
5684 | 121 -> (* y *)
5685 state.glinks <- true;
5686 let mode = state.mode in
5687 state.mode <- Textentry (
5689 ":", "", None, linknentry, linkndone (fun under ->
5690 selstring (undertext under);
5691 ), false
5693 fun _ ->
5694 state.glinks <- false;
5695 state.mode <- mode
5697 state.text <- "";
5698 G.postRedisplay "view:linkent"
5700 | 97 -> (* a *)
5701 begin match state.autoscroll with
5702 | Some step ->
5703 conf.autoscrollstep <- step;
5704 state.autoscroll <- None
5705 | None ->
5706 if conf.autoscrollstep = 0
5707 then state.autoscroll <- Some 1
5708 else state.autoscroll <- Some conf.autoscrollstep
5711 | 112 when ctrl -> (* ctrl-p *)
5712 launchpath ()
5714 | 80 -> (* P *)
5715 setpresentationmode (not conf.presentation);
5716 showtext ' ' ("presentation mode " ^
5717 if conf.presentation then "on" else "off");
5719 | 102 -> (* f *)
5720 if List.mem Wsi.Fullscreen state.winstate
5721 then Wsi.reshape conf.cwinw conf.cwinh
5722 else Wsi.fullscreen ()
5724 | 112 | 78 -> (* p|N *)
5725 search state.searchpattern false
5727 | 110 | 0xffc0 -> (* n|F3 *)
5728 search state.searchpattern true
5730 | 116 -> (* t *)
5731 begin match state.layout with
5732 | [] -> ()
5733 | l :: _ ->
5734 gotoghyll (getpagey l.pageno)
5737 | 32 -> (* space *)
5738 nextpage ()
5740 | 0xff9f | 0xffff -> (* delete *)
5741 prevpage ()
5743 | 61 -> (* = *)
5744 showtext ' ' (describe_location ());
5746 | 119 -> (* w *)
5747 begin match state.layout with
5748 | [] -> ()
5749 | l :: _ ->
5750 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5751 G.postRedisplay "w"
5754 | 39 -> (* ' *)
5755 enterbookmarkmode ()
5757 | 104 | 0xffbe -> (* h|F1 *)
5758 enterhelpmode ()
5760 | 105 -> (* i *)
5761 enterinfomode ()
5763 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5764 entermsgsmode ()
5766 | 109 -> (* m *)
5767 let ondone s =
5768 match state.layout with
5769 | l :: _ ->
5770 if nonemptystr s
5771 then
5772 state.bookmarks <-
5773 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5774 | _ -> ()
5776 enttext ("bookmark: ", "", None, textentry, ondone, true)
5778 | 126 -> (* ~ *)
5779 quickbookmark ();
5780 showtext ' ' "Quick bookmark added";
5782 | 122 -> (* z *)
5783 begin match state.layout with
5784 | l :: _ ->
5785 let rect = getpdimrect l.pagedimno in
5786 let w, h =
5787 if conf.crophack
5788 then
5789 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5790 truncate (1.2 *. (rect.(3) -. rect.(0))))
5791 else
5792 (truncate (rect.(1) -. rect.(0)),
5793 truncate (rect.(3) -. rect.(0)))
5795 let w = truncate ((float w)*.conf.zoom)
5796 and h = truncate ((float h)*.conf.zoom) in
5797 if w != 0 && h != 0
5798 then (
5799 state.anchor <- getanchor ();
5800 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5802 G.postRedisplay "z";
5804 | [] -> ()
5807 | 120 -> (* x *)
5808 state.roam ()
5809 | 60 | 62 -> (* < > *)
5810 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5812 | 91 | 93 -> (* [ ] *)
5813 conf.colorscale <-
5814 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5816 G.postRedisplay "brightness";
5818 | 99 when state.mode = View -> (* [alt-]c *)
5819 if Wsi.withalt mask
5820 then (
5821 if conf.zoom > 1.0
5822 then
5823 let m = (wadjsb state.winw - state.w) / 2 in
5824 state.x <- m;
5825 gotoy_and_clear_text state.y
5827 else
5828 let (c, a, b), z =
5829 match state.prevcolumns with
5830 | None -> (1, 0, 0), 1.0
5831 | Some (columns, z) ->
5832 let cab =
5833 match columns with
5834 | Csplit (c, _) -> -c, 0, 0
5835 | Cmulti ((c, a, b), _) -> c, a, b
5836 | Csingle _ -> 1, 0, 0
5838 cab, z
5840 setcolumns View c a b;
5841 setzoom z
5843 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5844 -> (* ctrl-shift- (kp) [up|down] *)
5845 let zoom, x = state.prevzoom in
5846 setzoom zoom;
5847 state.x <- x;
5849 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5850 begin match state.autoscroll with
5851 | None ->
5852 begin match state.mode with
5853 | Birdseye beye -> upbirdseye 1 beye
5854 | _ ->
5855 if ctrl
5856 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5857 else (
5858 if not (Wsi.withshift mask) && conf.presentation
5859 then prevpage ()
5860 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5863 | Some n ->
5864 setautoscrollspeed n false
5867 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5868 begin match state.autoscroll with
5869 | None ->
5870 begin match state.mode with
5871 | Birdseye beye -> downbirdseye 1 beye
5872 | _ ->
5873 if ctrl
5874 then gotoy_and_clear_text (clamp (state.winh/2))
5875 else (
5876 if not (Wsi.withshift mask) && conf.presentation
5877 then nextpage ()
5878 else gotoy_and_clear_text (clamp conf.scrollstep)
5881 | Some n ->
5882 setautoscrollspeed n true
5885 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5886 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5887 if canpan ()
5888 then
5889 let dx =
5890 if ctrl
5891 then state.winw / 2
5892 else conf.hscrollstep
5894 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5895 state.x <- panbound (state.x + dx);
5896 gotoy_and_clear_text state.y
5897 else (
5898 state.text <- "";
5899 G.postRedisplay "left/right"
5902 | 0xff55 | 0xff9a -> (* (kp) prior *)
5903 let y =
5904 if ctrl
5905 then
5906 match state.layout with
5907 | [] -> state.y
5908 | l :: _ -> state.y - l.pagey
5909 else
5910 clamp (pgscale (-state.winh))
5912 gotoghyll y
5914 | 0xff56 | 0xff9b -> (* (kp) next *)
5915 let y =
5916 if ctrl
5917 then
5918 match List.rev state.layout with
5919 | [] -> state.y
5920 | l :: _ -> getpagey l.pageno
5921 else
5922 clamp (pgscale state.winh)
5924 gotoghyll y
5926 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5927 gotoghyll 0
5928 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5929 gotoghyll (clamp state.maxy)
5931 | 0xff53 | 0xff98
5932 when Wsi.withalt mask -> (* alt-(kp) right *)
5933 gotoghyll (getnav 1)
5934 | 0xff51 | 0xff96
5935 when Wsi.withalt mask -> (* alt-(kp) left *)
5936 gotoghyll (getnav ~-1)
5938 | 114 -> (* r *)
5939 reload ()
5941 | 118 when conf.debug -> (* v *)
5942 state.rects <- [];
5943 List.iter (fun l ->
5944 match getopaque l.pageno with
5945 | None -> ()
5946 | Some opaque ->
5947 let x0, y0, x1, y1 = pagebbox opaque in
5948 let a,b = float x0, float y0 in
5949 let c,d = float x1, float y0 in
5950 let e,f = float x1, float y1 in
5951 let h,j = float x0, float y1 in
5952 let rect = (a,b,c,d,e,f,h,j) in
5953 debugrect rect;
5954 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5955 ) state.layout;
5956 G.postRedisplay "v";
5958 | 124 -> (* | *)
5959 let mode = state.mode in
5960 let cmd = ref "" in
5961 let onleave = function
5962 | Cancel -> state.mode <- mode
5963 | Confirm ->
5964 List.iter (fun l ->
5965 match getopaque l.pageno with
5966 | Some opaque -> pipesel opaque !cmd
5967 | None -> ()) state.layout;
5968 state.mode <- mode
5970 let ondone s =
5971 cbput state.hists.sel s;
5972 cmd := s
5974 let te =
5975 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5977 G.postRedisplay "|";
5978 state.mode <- Textentry (te, onleave);
5980 | _ ->
5981 vlog "huh? %s" (Wsi.keyname key)
5984 let linknavkeyboard key mask linknav =
5985 let getpage pageno =
5986 let rec loop = function
5987 | [] -> None
5988 | l :: _ when l.pageno = pageno -> Some l
5989 | _ :: rest -> loop rest
5990 in loop state.layout
5992 let doexact (pageno, n) =
5993 match getopaque pageno, getpage pageno with
5994 | Some opaque, Some l ->
5995 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5996 then
5997 let under = getlink opaque n in
5998 G.postRedisplay "link gotounder";
5999 gotounder under;
6000 state.mode <- View;
6001 else
6002 let opt, dir =
6003 match key with
6004 | 0xff50 -> (* home *)
6005 Some (findlink opaque LDfirst), -1
6007 | 0xff57 -> (* end *)
6008 Some (findlink opaque LDlast), 1
6010 | 0xff51 -> (* left *)
6011 Some (findlink opaque (LDleft n)), -1
6013 | 0xff53 -> (* right *)
6014 Some (findlink opaque (LDright n)), 1
6016 | 0xff52 -> (* up *)
6017 Some (findlink opaque (LDup n)), -1
6019 | 0xff54 -> (* down *)
6020 Some (findlink opaque (LDdown n)), 1
6022 | _ -> None, 0
6024 let pwl l dir =
6025 begin match findpwl l.pageno dir with
6026 | Pwlnotfound -> ()
6027 | Pwl pageno ->
6028 let notfound dir =
6029 state.mode <- LinkNav (Ltgendir dir);
6030 let y, h = getpageyh pageno in
6031 let y =
6032 if dir < 0
6033 then y + h - state.winh
6034 else y
6036 gotoy y
6038 begin match getopaque pageno, getpage pageno with
6039 | Some opaque, Some _ ->
6040 let link =
6041 let ld = if dir > 0 then LDfirst else LDlast in
6042 findlink opaque ld
6044 begin match link with
6045 | Lfound m ->
6046 showlinktype (getlink opaque m);
6047 state.mode <- LinkNav (Ltexact (pageno, m));
6048 G.postRedisplay "linknav jpage";
6049 | _ -> notfound dir
6050 end;
6051 | _ -> notfound dir
6052 end;
6053 end;
6055 begin match opt with
6056 | Some Lnotfound -> pwl l dir;
6057 | Some (Lfound m) ->
6058 if m = n
6059 then pwl l dir
6060 else (
6061 let _, y0, _, y1 = getlinkrect opaque m in
6062 if y0 < l.pagey
6063 then gotopage1 l.pageno y0
6064 else (
6065 let d = fstate.fontsize + 1 in
6066 if y1 - l.pagey > l.pagevh - d
6067 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
6068 else G.postRedisplay "linknav";
6070 showlinktype (getlink opaque m);
6071 state.mode <- LinkNav (Ltexact (l.pageno, m));
6074 | None -> viewkeyboard key mask
6075 end;
6076 | _ -> viewkeyboard key mask
6078 if key = 0xff63
6079 then (
6080 state.mode <- View;
6081 G.postRedisplay "leave linknav"
6083 else
6084 match linknav with
6085 | Ltgendir _ -> viewkeyboard key mask
6086 | Ltexact exact -> doexact exact
6089 let keyboard key mask =
6090 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
6091 then wcmd "interrupt"
6092 else state.uioh <- state.uioh#key key mask
6095 let birdseyekeyboard key mask
6096 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
6097 let incr =
6098 match conf.columns with
6099 | Csingle _ -> 1
6100 | Cmulti ((c, _, _), _) -> c
6101 | Csplit _ -> failwith "bird's eye split mode"
6103 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
6104 match key with
6105 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
6106 let y, h = getpageyh pageno in
6107 let top = (state.winh - h) / 2 in
6108 gotoy (max 0 (y - top))
6109 | 0xff0d (* enter *)
6110 | 0xff8d -> leavebirdseye beye false (* kp enter *)
6111 | 0xff1b -> leavebirdseye beye true (* escape *)
6112 | 0xff52 -> upbirdseye incr beye (* up *)
6113 | 0xff54 -> downbirdseye incr beye (* down *)
6114 | 0xff51 -> upbirdseye 1 beye (* left *)
6115 | 0xff53 -> downbirdseye 1 beye (* right *)
6117 | 0xff55 -> (* prior *)
6118 begin match state.layout with
6119 | l :: _ ->
6120 if l.pagey != 0
6121 then (
6122 state.mode <- Birdseye (
6123 oconf, leftx, l.pageno, hooverpageno, anchor
6125 gotopage1 l.pageno 0;
6127 else (
6128 let layout = layout (state.y-state.winh) (pgh state.layout) in
6129 match layout with
6130 | [] -> gotoy (clamp (-state.winh))
6131 | l :: _ ->
6132 state.mode <- Birdseye (
6133 oconf, leftx, l.pageno, hooverpageno, anchor
6135 gotopage1 l.pageno 0
6138 | [] -> gotoy (clamp (-state.winh))
6139 end;
6141 | 0xff56 -> (* next *)
6142 begin match List.rev state.layout with
6143 | l :: _ ->
6144 let layout = layout (state.y + (pgh state.layout)) state.winh in
6145 begin match layout with
6146 | [] ->
6147 let incr = l.pageh - l.pagevh in
6148 if incr = 0
6149 then (
6150 state.mode <-
6151 Birdseye (
6152 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
6154 G.postRedisplay "birdseye pagedown";
6156 else gotoy (clamp (incr + conf.interpagespace*2));
6158 | l :: _ ->
6159 state.mode <-
6160 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
6161 gotopage1 l.pageno 0;
6164 | [] -> gotoy (clamp state.winh)
6165 end;
6167 | 0xff50 -> (* home *)
6168 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
6169 gotopage1 0 0
6171 | 0xff57 -> (* end *)
6172 let pageno = state.pagecount - 1 in
6173 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
6174 if not (pagevisible state.layout pageno)
6175 then
6176 let h =
6177 match List.rev state.pdims with
6178 | [] -> state.winh
6179 | (_, _, h, _) :: _ -> h
6181 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
6182 else G.postRedisplay "birdseye end";
6183 | _ -> viewkeyboard key mask
6186 let drawpage l =
6187 let color =
6188 match state.mode with
6189 | Textentry _ -> scalecolor 0.4
6190 | LinkNav _
6191 | View -> scalecolor 1.0
6192 | Birdseye (_, _, pageno, hooverpageno, _) ->
6193 if l.pageno = hooverpageno
6194 then scalecolor 0.9
6195 else (
6196 if l.pageno = pageno
6197 then scalecolor 1.0
6198 else scalecolor 0.8
6201 drawtiles l color;
6204 let postdrawpage l linkindexbase =
6205 match getopaque l.pageno with
6206 | Some opaque ->
6207 if tileready l l.pagex l.pagey
6208 then
6209 let x = l.pagedispx - l.pagex
6210 and y = l.pagedispy - l.pagey in
6211 let hlmask =
6212 match conf.columns with
6213 | Csingle _ | Cmulti _ ->
6214 (if conf.hlinks then 1 else 0)
6215 + (if state.glinks
6216 && not (isbirdseye state.mode) then 2 else 0)
6217 | _ -> 0
6219 let s =
6220 match state.mode with
6221 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6222 | _ -> ""
6224 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6225 else 0
6226 | _ -> 0
6229 let scrollindicator () =
6230 let sbw, ph, sh = state.uioh#scrollph in
6231 let sbh, pw, sw = state.uioh#scrollpw in
6233 GlDraw.color (0.64, 0.64, 0.64);
6234 filledrect
6235 (float (state.winw - sbw)) 0.
6236 (float state.winw) (float state.winh)
6238 filledrect
6239 0. (float (state.winh - sbh))
6240 (float (wadjsb state.winw - 1)) (float state.winh)
6242 GlDraw.color (0.0, 0.0, 0.0);
6244 filledrect
6245 (float (state.winw - sbw)) ph
6246 (float state.winw) (ph +. sh)
6248 filledrect
6249 pw (float (state.winh - sbh))
6250 (pw +. sw) (float state.winh)
6254 let showsel () =
6255 match state.mstate with
6256 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6259 | Msel ((x0, y0), (x1, y1)) ->
6260 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
6261 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< "", -1, 0, 0) in
6262 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< "", -1, 0, 0) in
6263 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
6266 let showrects = function [] -> () | rects ->
6267 Gl.enable `blend;
6268 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6269 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6270 List.iter
6271 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6272 List.iter (fun l ->
6273 if l.pageno = pageno
6274 then (
6275 let dx = float (l.pagedispx - l.pagex) in
6276 let dy = float (l.pagedispy - l.pagey) in
6277 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6278 Raw.sets_float state.vraw ~pos:0
6279 [| x0+.dx; y0+.dy;
6280 x1+.dx; y1+.dy;
6281 x3+.dx; y3+.dy;
6282 x2+.dx; y2+.dy |];
6283 GlArray.vertex `two state.vraw;
6284 GlArray.draw_arrays `triangle_strip 0 4;
6286 ) state.layout
6287 ) rects
6289 Gl.disable `blend;
6292 let display () =
6293 GlClear.color (scalecolor2 conf.bgcolor);
6294 GlClear.clear [`color];
6295 List.iter drawpage state.layout;
6296 let rects =
6297 match state.mode with
6298 | LinkNav (Ltexact (pageno, linkno)) ->
6299 begin match getopaque pageno with
6300 | Some opaque ->
6301 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6302 (pageno, 5, (
6303 float x0, float y0,
6304 float x1, float y0,
6305 float x1, float y1,
6306 float x0, float y1)
6307 ) :: state.rects
6308 | None -> state.rects
6310 | _ -> state.rects
6312 showrects rects;
6313 let rec postloop linkindexbase = function
6314 | l :: rest ->
6315 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6316 postloop linkindexbase rest
6317 | [] -> ()
6319 showsel ();
6320 postloop 0 state.layout;
6321 state.uioh#display;
6322 begin match state.mstate with
6323 | Mzoomrect ((x0, y0), (x1, y1)) ->
6324 Gl.enable `blend;
6325 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6326 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6327 filledrect (float x0) (float y0) (float x1) (float y1);
6328 Gl.disable `blend;
6329 | _ -> ()
6330 end;
6331 enttext ();
6332 scrollindicator ();
6333 Wsi.swapb ();
6336 let zoomrect x y x1 y1 =
6337 let x0 = min x x1
6338 and x1 = max x x1
6339 and y0 = min y y1 in
6340 gotoy (state.y + y0);
6341 state.anchor <- getanchor ();
6342 let zoom = (float state.w) /. float (x1 - x0) in
6343 let margin =
6344 match conf.fitmodel, conf.columns with
6345 | FitPage, Csplit _ ->
6346 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6348 | _, _ ->
6349 let adjw = wadjsb state.winw in
6350 if state.w < adjw
6351 then (adjw - state.w) / 2
6352 else 0
6354 state.x <- (state.x + margin) - x0;
6355 setzoom zoom;
6356 resetmstate ();
6359 let zoomblock x y =
6360 let g opaque l px py =
6361 match rectofblock opaque px py with
6362 | Some a ->
6363 let x0 = a.(0) -. 20. in
6364 let x1 = a.(1) +. 20. in
6365 let y0 = a.(2) -. 20. in
6366 let zoom = (float state.w) /. (x1 -. x0) in
6367 let pagey = getpagey l.pageno in
6368 gotoy_and_clear_text (pagey + truncate y0);
6369 state.anchor <- getanchor ();
6370 let margin = (state.w - l.pagew)/2 in
6371 state.x <- -truncate x0 - margin;
6372 setzoom zoom;
6373 None
6374 | None -> None
6376 match conf.columns with
6377 | Csplit _ ->
6378 showtext '!' "block zooming does not work properly in split columns mode"
6379 | _ -> onppundermouse g x y ()
6382 let scrollx x =
6383 let winw = wadjsb state.winw - 1 in
6384 let s = float x /. float winw in
6385 let destx = truncate (float (state.w + winw) *. s) in
6386 state.x <- winw - destx;
6387 gotoy_and_clear_text state.y;
6388 state.mstate <- Mscrollx;
6391 let scrolly y =
6392 let s = float y /. float state.winh in
6393 let desty = truncate (float (state.maxy - state.winh) *. s) in
6394 gotoy_and_clear_text desty;
6395 state.mstate <- Mscrolly;
6398 let viewmulticlick clicks x y mask =
6399 let g opaque l px py =
6400 let mark =
6401 match clicks with
6402 | 2 -> Mark_word
6403 | 3 -> Mark_line
6404 | 4 -> Mark_block
6405 | _ -> Mark_page
6407 if markunder opaque px py mark
6408 then (
6409 Some (fun () ->
6410 let dopipe cmd =
6411 match getopaque l.pageno with
6412 | None -> ()
6413 | Some opaque -> pipesel opaque cmd
6415 state.roam <- (fun () -> dopipe conf.paxcmd);
6416 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
6419 else None
6421 G.postRedisplay "viewmulticlick";
6422 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
6425 let canselect () =
6426 match conf.columns with
6427 | Csplit _ -> false
6428 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
6431 let viewmouse button down x y mask =
6432 match button with
6433 | n when (n == 4 || n == 5) && not down ->
6434 if Wsi.withctrl mask
6435 then (
6436 match state.mstate with
6437 | Mzoom (oldn, i) ->
6438 if oldn = n
6439 then (
6440 if i = 2
6441 then
6442 let incr =
6443 match n with
6444 | 5 ->
6445 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6446 | _ ->
6447 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6449 let zoom = conf.zoom -. incr in
6450 setzoom zoom;
6451 state.mstate <- Mzoom (n, 0);
6452 else
6453 state.mstate <- Mzoom (n, i+1);
6455 else state.mstate <- Mzoom (n, 0)
6457 | _ -> state.mstate <- Mzoom (n, 0)
6459 else (
6460 match state.autoscroll with
6461 | Some step -> setautoscrollspeed step (n=4)
6462 | None ->
6463 if conf.wheelbypage || conf.presentation
6464 then (
6465 if n = 4
6466 then prevpage ()
6467 else nextpage ()
6469 else
6470 let incr =
6471 if n = 4
6472 then -conf.scrollstep
6473 else conf.scrollstep
6475 let incr = incr * 2 in
6476 let y = clamp incr in
6477 gotoy_and_clear_text y
6480 | n when (n = 6 || n = 7) && not down && canpan () ->
6481 state.x <-
6482 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6483 gotoy_and_clear_text state.y
6485 | 1 when Wsi.withshift mask ->
6486 state.mstate <- Mnone;
6487 if not down
6488 then (
6489 match unproject x y with
6490 | Some (pageno, ux, uy) ->
6491 let cmd = Printf.sprintf
6492 "%s %s %d %d %d"
6493 conf.stcmd state.path pageno ux uy
6495 popen cmd []
6496 | None -> ()
6499 | 1 when Wsi.withctrl mask ->
6500 if down
6501 then (
6502 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6503 state.mstate <- Mpan (x, y)
6505 else
6506 state.mstate <- Mnone
6508 | 3 ->
6509 if down
6510 then (
6511 Wsi.setcursor Wsi.CURSOR_CYCLE;
6512 let p = (x, y) in
6513 state.mstate <- Mzoomrect (p, p)
6515 else (
6516 match state.mstate with
6517 | Mzoomrect ((x0, y0), _) ->
6518 if abs (x-x0) > 10 && abs (y - y0) > 10
6519 then zoomrect x0 y0 x y
6520 else (
6521 resetmstate ();
6522 G.postRedisplay "kill accidental zoom rect";
6524 | _ ->
6525 resetmstate ()
6528 | 1 when x > state.winw - vscrollw () ->
6529 if down
6530 then
6531 let _, position, sh = state.uioh#scrollph in
6532 if y > truncate position && y < truncate (position +. sh)
6533 then state.mstate <- Mscrolly
6534 else scrolly y
6535 else
6536 state.mstate <- Mnone
6538 | 1 when y > state.winh - hscrollh () ->
6539 if down
6540 then
6541 let _, position, sw = state.uioh#scrollpw in
6542 if x > truncate position && x < truncate (position +. sw)
6543 then state.mstate <- Mscrollx
6544 else scrollx x
6545 else
6546 state.mstate <- Mnone
6548 | 1 when state.bzoom -> if not down then zoomblock x y
6550 | 1 ->
6551 let dest = if down then getunder x y else Unone in
6552 begin match dest with
6553 | Ulinkgoto _
6554 | Ulinkuri _
6555 | Uremote _ | Uremotedest _
6556 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6557 gotounder dest
6559 | Unone when down ->
6560 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6561 state.mstate <- Mpan (x, y);
6563 | Unone | Utext _ ->
6564 if down
6565 then (
6566 if canselect ()
6567 then (
6568 state.mstate <- Msel ((x, y), (x, y));
6569 G.postRedisplay "mouse select";
6572 else (
6573 match state.mstate with
6574 | Mnone -> ()
6576 | Mzoom _ | Mscrollx | Mscrolly ->
6577 state.mstate <- Mnone
6579 | Mzoomrect ((x0, y0), _) ->
6580 zoomrect x0 y0 x y
6582 | Mpan _ ->
6583 Wsi.setcursor Wsi.CURSOR_INHERIT;
6584 state.mstate <- Mnone
6586 | Msel ((x0, y0), (x1, y1)) ->
6587 let rec loop = function
6588 | [] -> ()
6589 | l :: rest ->
6590 let inside =
6591 let a0 = l.pagedispy in
6592 let a1 = a0 + l.pagevh in
6593 let b0 = l.pagedispx in
6594 let b1 = b0 + l.pagevw in
6595 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6596 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6598 if inside
6599 then
6600 match getopaque l.pageno with
6601 | Some opaque ->
6602 let dosel cmd () =
6603 match Ne.res Unix.pipe with
6604 | Ne.Exn exn ->
6605 showtext '!'
6606 (Printf.sprintf
6607 "can not create sel pipe: %s"
6608 (exntos exn));
6609 | Ne.Res (r, w) ->
6610 let clo what fd =
6611 Ne.clo fd (fun msg ->
6612 dolog "%s close failed: %s" what msg)
6614 let popened =
6615 try popen cmd [r, 0; w, -1]; true
6616 with exn ->
6617 dolog "can not execute %S: %s"
6618 cmd (exntos exn);
6619 false
6621 if popened
6622 then (
6623 copysel w opaque;
6624 G.postRedisplay "copysel";
6626 else clo "Msel pipe/w" w;
6627 clo "Msel pipe/r" r;
6629 dosel conf.selcmd ();
6630 state.roam <- dosel conf.paxcmd;
6631 | None -> ()
6632 else loop rest
6634 loop state.layout;
6635 resetmstate ();
6639 | _ -> ()
6642 let birdseyemouse button down x y mask
6643 (conf, leftx, _, hooverpageno, anchor) =
6644 match button with
6645 | 1 when down ->
6646 let rec loop = function
6647 | [] -> ()
6648 | l :: rest ->
6649 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6650 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6651 then (
6652 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6654 else loop rest
6656 loop state.layout
6657 | 3 -> ()
6658 | _ -> viewmouse button down x y mask
6661 let uioh = object
6662 method display = ()
6664 method key key mask =
6665 begin match state.mode with
6666 | Textentry textentry -> textentrykeyboard key mask textentry
6667 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6668 | View -> viewkeyboard key mask
6669 | LinkNav linknav -> linknavkeyboard key mask linknav
6670 end;
6671 state.uioh
6673 method button button bstate x y mask =
6674 begin match state.mode with
6675 | LinkNav _
6676 | View -> viewmouse button bstate x y mask
6677 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6678 | Textentry _ -> ()
6679 end;
6680 state.uioh
6682 method multiclick clicks x y mask =
6683 begin match state.mode with
6684 | LinkNav _
6685 | View -> viewmulticlick clicks x y mask
6686 | Birdseye _
6687 | Textentry _ -> ()
6688 end;
6689 state.uioh
6691 method motion x y =
6692 begin match state.mode with
6693 | Textentry _ -> ()
6694 | View | Birdseye _ | LinkNav _ ->
6695 match state.mstate with
6696 | Mzoom _ | Mnone -> ()
6698 | Mpan (x0, y0) ->
6699 let dx = x - x0
6700 and dy = y0 - y in
6701 state.mstate <- Mpan (x, y);
6702 if canpan ()
6703 then state.x <- panbound (state.x + dx);
6704 let y = clamp dy in
6705 gotoy_and_clear_text y
6707 | Msel (a, _) ->
6708 state.mstate <- Msel (a, (x, y));
6709 G.postRedisplay "motion select";
6711 | Mscrolly ->
6712 let y = min state.winh (max 0 y) in
6713 scrolly y
6715 | Mscrollx ->
6716 let x = min state.winw (max 0 x) in
6717 scrollx x
6719 | Mzoomrect (p0, _) ->
6720 state.mstate <- Mzoomrect (p0, (x, y));
6721 G.postRedisplay "motion zoomrect";
6722 end;
6723 state.uioh
6725 method pmotion x y =
6726 begin match state.mode with
6727 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6728 let rec loop = function
6729 | [] ->
6730 if hooverpageno != -1
6731 then (
6732 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6733 G.postRedisplay "pmotion birdseye no hoover";
6735 | l :: rest ->
6736 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6737 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6738 then (
6739 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6740 G.postRedisplay "pmotion birdseye hoover";
6742 else loop rest
6744 loop state.layout
6746 | Textentry _ -> ()
6748 | LinkNav _
6749 | View ->
6750 match state.mstate with
6751 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6753 | Mnone ->
6754 updateunder x y;
6755 if canselect ()
6756 then
6757 match conf.pax with
6758 | None -> ()
6759 | Some r ->
6760 let past, _, _ = !r in
6761 let now = now () in
6762 let delta = now -. past in
6763 if delta > 0.01
6764 then paxunder x y
6765 else r := (now, x, y)
6766 end;
6767 state.uioh
6769 method infochanged _ = ()
6771 method scrollph =
6772 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6773 let p, h =
6774 if maxy = 0
6775 then 0.0, float state.winh
6776 else scrollph state.y maxy
6778 vscrollw (), p, h
6780 method scrollpw =
6781 let winw = wadjsb state.winw in
6782 let fwinw = float winw in
6783 let sw =
6784 let sw = fwinw /. float state.w in
6785 let sw = fwinw *. sw in
6786 max sw (float conf.scrollh)
6788 let position =
6789 let maxx = state.w + winw in
6790 let x = winw - state.x in
6791 let percent = float x /. float maxx in
6792 (fwinw -. sw) *. percent
6794 hscrollh (), position, sw
6796 method modehash =
6797 let modename =
6798 match state.mode with
6799 | LinkNav _ -> "links"
6800 | Textentry _ -> "textentry"
6801 | Birdseye _ -> "birdseye"
6802 | View -> "view"
6804 findkeyhash conf modename
6806 method eformsgs = true
6807 end;;
6809 module Config =
6810 struct
6811 open Parser
6813 let fontpath = ref "";;
6815 module KeyMap =
6816 Map.Make (struct type t = (int * int) let compare = compare end);;
6818 let unent s =
6819 let l = String.length s in
6820 let b = Buffer.create l in
6821 unent b s 0 l;
6822 Buffer.contents b;
6825 let home =
6826 try Sys.getenv "HOME"
6827 with exn ->
6828 prerr_endline
6829 ("Can not determine home directory location: " ^ exntos exn);
6833 let modifier_of_string = function
6834 | "alt" -> Wsi.altmask
6835 | "shift" -> Wsi.shiftmask
6836 | "ctrl" | "control" -> Wsi.ctrlmask
6837 | "meta" -> Wsi.metamask
6838 | _ -> 0
6841 let key_of_string =
6842 let r = Str.regexp "-" in
6843 fun s ->
6844 let elems = Str.full_split r s in
6845 let f n k m =
6846 let g s =
6847 let m1 = modifier_of_string s in
6848 if m1 = 0
6849 then (Wsi.namekey s, m)
6850 else (k, m lor m1)
6851 in function
6852 | Str.Delim s when n land 1 = 0 -> g s
6853 | Str.Text s -> g s
6854 | Str.Delim _ -> (k, m)
6856 let rec loop n k m = function
6857 | [] -> (k, m)
6858 | x :: xs ->
6859 let k, m = f n k m x in
6860 loop (n+1) k m xs
6862 loop 0 0 0 elems
6865 let keys_of_string =
6866 let r = Str.regexp "[ \t]" in
6867 fun s ->
6868 let elems = Str.split r s in
6869 List.map key_of_string elems
6872 let copykeyhashes c =
6873 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6876 let config_of c attrs =
6877 let apply c k v =
6879 match k with
6880 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6881 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6882 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6883 | "preload" -> { c with preload = bool_of_string v }
6884 | "page-bias" -> { c with pagebias = int_of_string v }
6885 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6886 | "horizontal-scroll-step" ->
6887 { c with hscrollstep = max (int_of_string v) 1 }
6888 | "auto-scroll-step" ->
6889 { c with autoscrollstep = max 0 (int_of_string v) }
6890 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6891 | "crop-hack" -> { c with crophack = bool_of_string v }
6892 | "throttle" ->
6893 let mw =
6894 match String.lowercase v with
6895 | "true" -> Some infinity
6896 | "false" -> None
6897 | f -> Some (float_of_string f)
6899 { c with maxwait = mw}
6900 | "highlight-links" -> { c with hlinks = bool_of_string v }
6901 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6902 | "vertical-margin" ->
6903 { c with interpagespace = max 0 (int_of_string v) }
6904 | "zoom" ->
6905 let zoom = float_of_string v /. 100. in
6906 let zoom = max zoom 0.0 in
6907 { c with zoom = zoom }
6908 | "presentation" -> { c with presentation = bool_of_string v }
6909 | "rotation-angle" -> { c with angle = int_of_string v }
6910 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6911 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6912 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6913 | "proportional-display" ->
6914 let fm =
6915 if bool_of_string v
6916 then FitProportional
6917 else FitWidth
6919 { c with fitmodel = fm }
6920 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6921 | "pixmap-cache-size" ->
6922 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6923 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6924 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6925 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6926 | "persistent-location" -> { c with jumpback = bool_of_string v }
6927 | "background-color" -> { c with bgcolor = color_of_string v }
6928 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6929 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6930 | "mupdf-store-size" ->
6931 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6932 | "checkers" -> { c with checkers = bool_of_string v }
6933 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6934 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6935 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6936 | "uri-launcher" -> { c with urilauncher = unent v }
6937 | "path-launcher" -> { c with pathlauncher = unent v }
6938 | "color-space" -> { c with colorspace = CSTE.of_string v }
6939 | "invert-colors" -> { c with invert = bool_of_string v }
6940 | "brightness" -> { c with colorscale = float_of_string v }
6941 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6942 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6943 | "columns" ->
6944 let (n, _, _) as nab = multicolumns_of_string v in
6945 if n < 0
6946 then { c with columns = Csplit (-n, [||]) }
6947 else { c with columns = Cmulti (nab, [||]) }
6948 | "birds-eye-columns" ->
6949 { c with beyecolumns = Some (max (int_of_string v) 2) }
6950 | "selection-command" -> { c with selcmd = unent v }
6951 | "synctex-command" -> { c with stcmd = unent v }
6952 | "pax-command" -> { c with paxcmd = unent v }
6953 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6954 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6955 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6956 | "use-pbo" -> { c with usepbo = bool_of_string v }
6957 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6958 | "horizontal-scrollbar-visible" ->
6959 let b =
6960 if bool_of_string v
6961 then c.scrollb lor scrollbhv
6962 else c.scrollb land (lnot scrollbhv)
6964 { c with scrollb = b }
6965 | "vertical-scrollbar-visible" ->
6966 let b =
6967 if bool_of_string v
6968 then c.scrollb lor scrollbvv
6969 else c.scrollb land (lnot scrollbvv)
6971 { c with scrollb = b }
6972 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6973 | "point-and-x" ->
6974 { c with pax =
6975 if bool_of_string v
6976 then Some (ref (0.0, 0, 0))
6977 else None }
6978 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6979 | _ -> c
6980 with exn ->
6981 prerr_endline ("Error processing attribute (`" ^
6982 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6985 let rec fold c = function
6986 | [] -> c
6987 | (k, v) :: rest ->
6988 let c = apply c k v in
6989 fold c rest
6991 fold { c with keyhashes = copykeyhashes c } attrs;
6994 let fromstring f pos n v d =
6995 try f v
6996 with exn ->
6997 dolog "Error processing attribute (%S=%S) at %d\n%s"
6998 n v pos (exntos exn)
7003 let bookmark_of attrs =
7004 let rec fold title page rely visy = function
7005 | ("title", v) :: rest -> fold v page rely visy rest
7006 | ("page", v) :: rest -> fold title v rely visy rest
7007 | ("rely", v) :: rest -> fold title page v visy rest
7008 | ("visy", v) :: rest -> fold title page rely v rest
7009 | _ :: rest -> fold title page rely visy rest
7010 | [] -> title, page, rely, visy
7012 fold "invalid" "0" "0" "0" attrs
7015 let doc_of attrs =
7016 let rec fold path page rely pan visy = function
7017 | ("path", v) :: rest -> fold v page rely pan visy rest
7018 | ("page", v) :: rest -> fold path v rely pan visy rest
7019 | ("rely", v) :: rest -> fold path page v pan visy rest
7020 | ("pan", v) :: rest -> fold path page rely v visy rest
7021 | ("visy", v) :: rest -> fold path page rely pan v rest
7022 | _ :: rest -> fold path page rely pan visy rest
7023 | [] -> path, page, rely, pan, visy
7025 fold "" "0" "0" "0" "0" attrs
7028 let map_of attrs =
7029 let rec fold rs ls = function
7030 | ("out", v) :: rest -> fold v ls rest
7031 | ("in", v) :: rest -> fold rs v rest
7032 | _ :: rest -> fold ls rs rest
7033 | [] -> ls, rs
7035 fold "" "" attrs
7038 let setconf dst src =
7039 dst.scrollbw <- src.scrollbw;
7040 dst.scrollh <- src.scrollh;
7041 dst.icase <- src.icase;
7042 dst.preload <- src.preload;
7043 dst.pagebias <- src.pagebias;
7044 dst.verbose <- src.verbose;
7045 dst.scrollstep <- src.scrollstep;
7046 dst.maxhfit <- src.maxhfit;
7047 dst.crophack <- src.crophack;
7048 dst.autoscrollstep <- src.autoscrollstep;
7049 dst.maxwait <- src.maxwait;
7050 dst.hlinks <- src.hlinks;
7051 dst.underinfo <- src.underinfo;
7052 dst.interpagespace <- src.interpagespace;
7053 dst.zoom <- src.zoom;
7054 dst.presentation <- src.presentation;
7055 dst.angle <- src.angle;
7056 dst.cwinw <- src.cwinw;
7057 dst.cwinh <- src.cwinh;
7058 dst.savebmarks <- src.savebmarks;
7059 dst.memlimit <- src.memlimit;
7060 dst.fitmodel <- src.fitmodel;
7061 dst.texcount <- src.texcount;
7062 dst.sliceheight <- src.sliceheight;
7063 dst.thumbw <- src.thumbw;
7064 dst.jumpback <- src.jumpback;
7065 dst.bgcolor <- src.bgcolor;
7066 dst.tilew <- src.tilew;
7067 dst.tileh <- src.tileh;
7068 dst.mustoresize <- src.mustoresize;
7069 dst.checkers <- src.checkers;
7070 dst.aalevel <- src.aalevel;
7071 dst.trimmargins <- src.trimmargins;
7072 dst.trimfuzz <- src.trimfuzz;
7073 dst.urilauncher <- src.urilauncher;
7074 dst.colorspace <- src.colorspace;
7075 dst.invert <- src.invert;
7076 dst.colorscale <- src.colorscale;
7077 dst.redirectstderr <- src.redirectstderr;
7078 dst.ghyllscroll <- src.ghyllscroll;
7079 dst.columns <- src.columns;
7080 dst.beyecolumns <- src.beyecolumns;
7081 dst.selcmd <- src.selcmd;
7082 dst.updatecurs <- src.updatecurs;
7083 dst.pathlauncher <- src.pathlauncher;
7084 dst.keyhashes <- copykeyhashes src;
7085 dst.hfsize <- src.hfsize;
7086 dst.hscrollstep <- src.hscrollstep;
7087 dst.pgscale <- src.pgscale;
7088 dst.usepbo <- src.usepbo;
7089 dst.wheelbypage <- src.wheelbypage;
7090 dst.stcmd <- src.stcmd;
7091 dst.paxcmd <- src.paxcmd;
7092 dst.scrollb <- src.scrollb;
7093 dst.riani <- src.riani;
7094 dst.paxmark <- src.paxmark;
7095 dst.pax <-
7096 if src.pax = None
7097 then None
7098 else Some ((ref (0.0, 0, 0)));
7101 let get s =
7102 let h = Hashtbl.create 10 in
7103 let dc = { defconf with angle = defconf.angle } in
7104 let rec toplevel v t spos _ =
7105 match t with
7106 | Vdata | Vcdata | Vend -> v
7107 | Vopen ("llppconfig", _, closed) ->
7108 if closed
7109 then v
7110 else { v with f = llppconfig }
7111 | Vopen _ ->
7112 error "unexpected subelement at top level" s spos
7113 | Vclose _ -> error "unexpected close at top level" s spos
7115 and llppconfig v t spos _ =
7116 match t with
7117 | Vdata | Vcdata -> v
7118 | Vend -> error "unexpected end of input in llppconfig" s spos
7119 | Vopen ("defaults", attrs, closed) ->
7120 let c = config_of dc attrs in
7121 setconf dc c;
7122 if closed
7123 then v
7124 else { v with f = defaults }
7126 | Vopen ("ui-font", attrs, closed) ->
7127 let rec getsize size = function
7128 | [] -> size
7129 | ("size", v) :: rest ->
7130 let size =
7131 fromstring int_of_string spos "size" v fstate.fontsize in
7132 getsize size rest
7133 | l -> getsize size l
7135 fstate.fontsize <- getsize fstate.fontsize attrs;
7136 if closed
7137 then v
7138 else { v with f = uifont (Buffer.create 10) }
7140 | Vopen ("doc", attrs, closed) ->
7141 let pathent, spage, srely, span, svisy = doc_of attrs in
7142 let path = unent pathent
7143 and pageno = fromstring int_of_string spos "page" spage 0
7144 and rely = fromstring float_of_string spos "rely" srely 0.0
7145 and pan = fromstring int_of_string spos "pan" span 0
7146 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7147 let c = config_of dc attrs in
7148 let anchor = (pageno, rely, visy) in
7149 if closed
7150 then (Hashtbl.add h path (c, [], pan, anchor); v)
7151 else { v with f = doc path pan anchor c [] }
7153 | Vopen _ ->
7154 error "unexpected subelement in llppconfig" s spos
7156 | Vclose "llppconfig" -> { v with f = toplevel }
7157 | Vclose _ -> error "unexpected close in llppconfig" s spos
7159 and defaults v t spos _ =
7160 match t with
7161 | Vdata | Vcdata -> v
7162 | Vend -> error "unexpected end of input in defaults" s spos
7163 | Vopen ("keymap", attrs, closed) ->
7164 let modename =
7165 try List.assoc "mode" attrs
7166 with Not_found -> "global" in
7167 if closed
7168 then v
7169 else
7170 let ret keymap =
7171 let h = findkeyhash dc modename in
7172 KeyMap.iter (Hashtbl.replace h) keymap;
7173 defaults
7175 { v with f = pkeymap ret KeyMap.empty }
7177 | Vopen (_, _, _) ->
7178 error "unexpected subelement in defaults" s spos
7180 | Vclose "defaults" ->
7181 { v with f = llppconfig }
7183 | Vclose _ -> error "unexpected close in defaults" s spos
7185 and uifont b v t spos epos =
7186 match t with
7187 | Vdata | Vcdata ->
7188 Buffer.add_substring b s spos (epos - spos);
7190 | Vopen (_, _, _) ->
7191 error "unexpected subelement in ui-font" s spos
7192 | Vclose "ui-font" ->
7193 if emptystr !fontpath
7194 then fontpath := Buffer.contents b;
7195 { v with f = llppconfig }
7196 | Vclose _ -> error "unexpected close in ui-font" s spos
7197 | Vend -> error "unexpected end of input in ui-font" s spos
7199 and doc path pan anchor c bookmarks v t spos _ =
7200 match t with
7201 | Vdata | Vcdata -> v
7202 | Vend -> error "unexpected end of input in doc" s spos
7203 | Vopen ("bookmarks", _, closed) ->
7204 if closed
7205 then v
7206 else { v with f = pbookmarks path pan anchor c bookmarks }
7208 | Vopen ("keymap", attrs, closed) ->
7209 let modename =
7210 try List.assoc "mode" attrs
7211 with Not_found -> "global"
7213 if closed
7214 then v
7215 else
7216 let ret keymap =
7217 let h = findkeyhash c modename in
7218 KeyMap.iter (Hashtbl.replace h) keymap;
7219 doc path pan anchor c bookmarks
7221 { v with f = pkeymap ret KeyMap.empty }
7223 | Vopen (_, _, _) ->
7224 error "unexpected subelement in doc" s spos
7226 | Vclose "doc" ->
7227 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7228 { v with f = llppconfig }
7230 | Vclose _ -> error "unexpected close in doc" s spos
7232 and pkeymap ret keymap v t spos _ =
7233 match t with
7234 | Vdata | Vcdata -> v
7235 | Vend -> error "unexpected end of input in keymap" s spos
7236 | Vopen ("map", attrs, closed) ->
7237 let r, l = map_of attrs in
7238 let kss = fromstring keys_of_string spos "in" r [] in
7239 let lss = fromstring keys_of_string spos "out" l [] in
7240 let keymap =
7241 match kss with
7242 | [] -> keymap
7243 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7244 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7246 if closed
7247 then { v with f = pkeymap ret keymap }
7248 else
7249 let f () = v in
7250 { v with f = skip "map" f }
7252 | Vopen _ ->
7253 error "unexpected subelement in keymap" s spos
7255 | Vclose "keymap" ->
7256 { v with f = ret keymap }
7258 | Vclose _ -> error "unexpected close in keymap" s spos
7260 and pbookmarks path pan anchor c bookmarks v t spos _ =
7261 match t with
7262 | Vdata | Vcdata -> v
7263 | Vend -> error "unexpected end of input in bookmarks" s spos
7264 | Vopen ("item", attrs, closed) ->
7265 let titleent, spage, srely, svisy = bookmark_of attrs in
7266 let page = fromstring int_of_string spos "page" spage 0
7267 and rely = fromstring float_of_string spos "rely" srely 0.0
7268 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7269 let bookmarks =
7270 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
7272 if closed
7273 then { v with f = pbookmarks path pan anchor c bookmarks }
7274 else
7275 let f () = v in
7276 { v with f = skip "item" f }
7278 | Vopen _ ->
7279 error "unexpected subelement in bookmarks" s spos
7281 | Vclose "bookmarks" ->
7282 { v with f = doc path pan anchor c bookmarks }
7284 | Vclose _ -> error "unexpected close in bookmarks" s spos
7286 and skip tag f v t spos _ =
7287 match t with
7288 | Vdata | Vcdata -> v
7289 | Vend ->
7290 error ("unexpected end of input in skipped " ^ tag) s spos
7291 | Vopen (tag', _, closed) ->
7292 if closed
7293 then v
7294 else
7295 let f' () = { v with f = skip tag f } in
7296 { v with f = skip tag' f' }
7297 | Vclose ctag ->
7298 if tag = ctag
7299 then f ()
7300 else error ("unexpected close in skipped " ^ tag) s spos
7303 parse { f = toplevel; accu = () } s;
7304 h, dc;
7307 let do_load f ic =
7309 let len = in_channel_length ic in
7310 let s = String.create len in
7311 really_input ic s 0 len;
7312 f s;
7313 with
7314 | Parse_error (msg, s, pos) ->
7315 let subs = subs s pos in
7316 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7318 | exn ->
7319 failwith ("config load error: " ^ exntos exn)
7322 let defconfpath =
7323 let dir =
7325 let dir = Filename.concat home ".config" in
7326 if Sys.is_directory dir then dir else home
7327 with _ -> home
7329 Filename.concat dir "llpp.conf"
7332 let confpath = ref defconfpath;;
7334 let load1 f =
7335 if Sys.file_exists !confpath
7336 then
7337 match
7338 (try Some (open_in_bin !confpath)
7339 with exn ->
7340 prerr_endline
7341 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7342 exntos exn);
7343 None
7345 with
7346 | Some ic ->
7347 let success =
7349 f (do_load get ic)
7350 with exn ->
7351 prerr_endline
7352 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7353 exntos exn);
7354 false
7356 close_in ic;
7357 success
7359 | None -> false
7360 else
7361 f (Hashtbl.create 0, defconf)
7364 let load () =
7365 let f (h, dc) =
7366 let pc, pb, px, pa =
7368 let key =
7369 if emptystr state.origin
7370 then state.path
7371 else state.origin
7373 Hashtbl.find h (Filename.basename key)
7374 with Not_found -> dc, [], 0, emptyanchor
7376 setconf defconf dc;
7377 setconf conf pc;
7378 state.bookmarks <- pb;
7379 state.x <- px;
7380 if conf.jumpback
7381 then state.anchor <- pa;
7382 cbput state.hists.nav pa;
7383 true
7385 load1 f
7388 let add_attrs bb always dc c =
7389 let ob s a b =
7390 if always || a != b
7391 then Printf.bprintf bb "\n %s='%b'" s a
7392 and op s a b =
7393 if always || a <> b
7394 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7395 and oi s a b =
7396 if always || a != b
7397 then Printf.bprintf bb "\n %s='%d'" s a
7398 and oI s a b =
7399 if always || a != b
7400 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7401 and oz s a b =
7402 if always || a <> b
7403 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7404 and oF s a b =
7405 if always || a <> b
7406 then Printf.bprintf bb "\n %s='%f'" s a
7407 and oc s a b =
7408 if always || a <> b
7409 then
7410 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7411 and oC s a b =
7412 if always || a <> b
7413 then
7414 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7415 and oR s a b =
7416 if always || a <> b
7417 then
7418 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7419 and os s a b =
7420 if always || a <> b
7421 then
7422 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7423 and og s a b =
7424 if always || a <> b
7425 then
7426 match a with
7427 | Some (_N, _A, _B) ->
7428 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7429 | None ->
7430 match b with
7431 | None -> ()
7432 | _ ->
7433 Printf.bprintf bb "\n %s='none'" s
7434 and oW s a b =
7435 if always || a <> b
7436 then
7437 let v =
7438 match a with
7439 | None -> "false"
7440 | Some f ->
7441 if f = infinity
7442 then "true"
7443 else string_of_float f
7445 Printf.bprintf bb "\n %s='%s'" s v
7446 and oco s a b =
7447 if always || a <> b
7448 then
7449 match a with
7450 | Cmulti ((n, a, b), _) when n > 1 ->
7451 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7452 | Csplit (n, _) when n > 1 ->
7453 Printf.bprintf bb "\n %s='%d'" s ~-n
7454 | _ -> ()
7455 and obeco s a b =
7456 if always || a <> b
7457 then
7458 match a with
7459 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7460 | _ -> ()
7461 and oFm s a b =
7462 if always || a <> b
7463 then
7464 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7465 and oSv s a b m =
7466 if always || a <> b
7467 then
7468 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7469 and oPm s a b =
7470 if always || a <> b
7471 then
7472 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7474 oi "width" c.cwinw dc.cwinw;
7475 oi "height" c.cwinh dc.cwinh;
7476 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7477 oi "scroll-handle-height" c.scrollh dc.scrollh;
7478 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7479 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7480 ob "case-insensitive-search" c.icase dc.icase;
7481 ob "preload" c.preload dc.preload;
7482 oi "page-bias" c.pagebias dc.pagebias;
7483 oi "scroll-step" c.scrollstep dc.scrollstep;
7484 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7485 ob "max-height-fit" c.maxhfit dc.maxhfit;
7486 ob "crop-hack" c.crophack dc.crophack;
7487 oW "throttle" c.maxwait dc.maxwait;
7488 ob "highlight-links" c.hlinks dc.hlinks;
7489 ob "under-cursor-info" c.underinfo dc.underinfo;
7490 oi "vertical-margin" c.interpagespace dc.interpagespace;
7491 oz "zoom" c.zoom dc.zoom;
7492 ob "presentation" c.presentation dc.presentation;
7493 oi "rotation-angle" c.angle dc.angle;
7494 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7495 oFm "fit-model" c.fitmodel dc.fitmodel;
7496 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7497 oi "tex-count" c.texcount dc.texcount;
7498 oi "slice-height" c.sliceheight dc.sliceheight;
7499 oi "thumbnail-width" c.thumbw dc.thumbw;
7500 ob "persistent-location" c.jumpback dc.jumpback;
7501 oc "background-color" c.bgcolor dc.bgcolor;
7502 oi "tile-width" c.tilew dc.tilew;
7503 oi "tile-height" c.tileh dc.tileh;
7504 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7505 ob "checkers" c.checkers dc.checkers;
7506 oi "aalevel" c.aalevel dc.aalevel;
7507 ob "trim-margins" c.trimmargins dc.trimmargins;
7508 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7509 os "uri-launcher" c.urilauncher dc.urilauncher;
7510 os "path-launcher" c.pathlauncher dc.pathlauncher;
7511 oC "color-space" c.colorspace dc.colorspace;
7512 ob "invert-colors" c.invert dc.invert;
7513 oF "brightness" c.colorscale dc.colorscale;
7514 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7515 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7516 oco "columns" c.columns dc.columns;
7517 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7518 os "selection-command" c.selcmd dc.selcmd;
7519 os "synctex-command" c.stcmd dc.stcmd;
7520 os "pax-command" c.paxcmd dc.paxcmd;
7521 ob "update-cursor" c.updatecurs dc.updatecurs;
7522 oi "hint-font-size" c.hfsize dc.hfsize;
7523 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7524 oF "page-scroll-scale" c.pgscale dc.pgscale;
7525 ob "use-pbo" c.usepbo dc.usepbo;
7526 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7527 ob "remote-in-a-new-instance" c.riani dc.riani;
7528 op "point-and-x" c.pax dc.pax;
7529 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7532 let keymapsbuf always dc c =
7533 let bb = Buffer.create 16 in
7534 let rec loop = function
7535 | [] -> ()
7536 | (modename, h) :: rest ->
7537 let dh = findkeyhash dc modename in
7538 if always || h <> dh
7539 then (
7540 if Hashtbl.length h > 0
7541 then (
7542 if Buffer.length bb > 0
7543 then Buffer.add_char bb '\n';
7544 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7545 Hashtbl.iter (fun i o ->
7546 let isdifferent = always ||
7548 let dO = Hashtbl.find dh i in
7549 dO <> o
7550 with Not_found -> true
7552 if isdifferent
7553 then
7554 let addkm (k, m) =
7555 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7556 if Wsi.withalt m then Buffer.add_string bb "alt-";
7557 if Wsi.withshift m then Buffer.add_string bb "shift-";
7558 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7559 Buffer.add_string bb (Wsi.keyname k);
7561 let addkms l =
7562 let rec loop = function
7563 | [] -> ()
7564 | km :: [] -> addkm km
7565 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7567 loop l
7569 Buffer.add_string bb "<map in='";
7570 addkm i;
7571 match o with
7572 | KMinsrt km ->
7573 Buffer.add_string bb "' out='";
7574 addkm km;
7575 Buffer.add_string bb "'/>\n"
7577 | KMinsrl kms ->
7578 Buffer.add_string bb "' out='";
7579 addkms kms;
7580 Buffer.add_string bb "'/>\n"
7582 | KMmulti (ins, kms) ->
7583 Buffer.add_char bb ' ';
7584 addkms ins;
7585 Buffer.add_string bb "' out='";
7586 addkms kms;
7587 Buffer.add_string bb "'/>\n"
7588 ) h;
7589 Buffer.add_string bb "</keymap>";
7592 loop rest
7594 loop c.keyhashes;
7598 let save () =
7599 let uifontsize = fstate.fontsize in
7600 let bb = Buffer.create 32768 in
7601 let relx = float state.x /. float state.winw in
7602 let w, h, x =
7603 let cx w = truncate (relx *. float w) in
7604 List.fold_left
7605 (fun (w, h, x) ws ->
7606 match ws with
7607 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7608 | Wsi.MaxVert -> (w, conf.cwinh, x)
7609 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7611 (state.winw, state.winh, state.x) state.winstate
7613 conf.cwinw <- w;
7614 conf.cwinh <- h;
7615 let f (h, dc) =
7616 let dc = if conf.bedefault then conf else dc in
7617 Buffer.add_string bb "<llppconfig>\n";
7619 if nonemptystr !fontpath
7620 then
7621 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7622 uifontsize
7623 !fontpath
7624 else (
7625 if uifontsize <> 14
7626 then
7627 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7630 Buffer.add_string bb "<defaults";
7631 add_attrs bb true dc dc;
7632 let kb = keymapsbuf true dc dc in
7633 if Buffer.length kb > 0
7634 then (
7635 Buffer.add_string bb ">\n";
7636 Buffer.add_buffer bb kb;
7637 Buffer.add_string bb "\n</defaults>\n";
7639 else Buffer.add_string bb "/>\n";
7641 let adddoc path pan anchor c bookmarks =
7642 if bookmarks == [] && c = dc && anchor = emptyanchor
7643 then ()
7644 else (
7645 Printf.bprintf bb "<doc path='%s'"
7646 (enent path 0 (String.length path));
7648 if anchor <> emptyanchor
7649 then (
7650 let n, rely, visy = anchor in
7651 Printf.bprintf bb " page='%d'" n;
7652 if rely > 1e-6
7653 then
7654 Printf.bprintf bb " rely='%f'" rely
7656 if abs_float visy > 1e-6
7657 then
7658 Printf.bprintf bb " visy='%f'" visy
7662 if pan != 0
7663 then Printf.bprintf bb " pan='%d'" pan;
7665 add_attrs bb false dc c;
7666 let kb = keymapsbuf false dc c in
7668 begin match bookmarks with
7669 | [] ->
7670 if Buffer.length kb > 0
7671 then (
7672 Buffer.add_string bb ">\n";
7673 Buffer.add_buffer bb kb;
7674 Buffer.add_string bb "\n</doc>\n";
7676 else Buffer.add_string bb "/>\n"
7677 | _ ->
7678 Buffer.add_string bb ">\n<bookmarks>\n";
7679 List.iter (fun (title, _, kind) ->
7680 begin match kind with
7681 | Oanchor (page, rely, visy) ->
7682 Printf.bprintf bb
7683 "<item title='%s' page='%d'"
7684 (enent title 0 (String.length title))
7685 page
7687 if rely > 1e-6
7688 then
7689 Printf.bprintf bb " rely='%f'" rely
7691 if abs_float visy > 1e-6
7692 then
7693 Printf.bprintf bb " visy='%f'" visy
7695 | Onone | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
7696 failwith "unexpected link in bookmarks"
7697 end;
7698 Buffer.add_string bb "/>\n";
7699 ) bookmarks;
7700 Buffer.add_string bb "</bookmarks>";
7701 if Buffer.length kb > 0
7702 then (
7703 Buffer.add_string bb "\n";
7704 Buffer.add_buffer bb kb;
7706 Buffer.add_string bb "\n</doc>\n";
7707 end;
7711 let pan, conf =
7712 match state.mode with
7713 | Birdseye (c, pan, _, _, _) ->
7714 let beyecolumns =
7715 match conf.columns with
7716 | Cmulti ((c, _, _), _) -> Some c
7717 | Csingle _ -> None
7718 | Csplit _ -> None
7719 and columns =
7720 match c.columns with
7721 | Cmulti (c, _) -> Cmulti (c, [||])
7722 | Csingle _ -> Csingle [||]
7723 | Csplit _ -> failwith "quit from bird's eye while split"
7725 pan, { c with beyecolumns = beyecolumns; columns = columns }
7726 | _ -> x, conf
7728 let basename = Filename.basename
7729 (if emptystr state.origin then state.path else state.origin)
7731 adddoc basename pan (getanchor ())
7732 (let autoscrollstep =
7733 match state.autoscroll with
7734 | Some step -> step
7735 | None -> conf.autoscrollstep
7736 in begin match state.mode with
7737 | Birdseye beye -> leavebirdseye beye true;
7738 | _ -> ()
7739 end;
7740 { conf with autoscrollstep = autoscrollstep })
7741 (if conf.savebmarks then state.bookmarks else []);
7743 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7744 if basename <> path
7745 then adddoc path x anchor c bookmarks
7746 ) h;
7747 Buffer.add_string bb "</llppconfig>\n";
7748 true;
7750 if load1 f && Buffer.length bb > 0
7751 then
7753 let tmp = !confpath ^ ".tmp" in
7754 let oc = open_out_bin tmp in
7755 Buffer.output_buffer oc bb;
7756 close_out oc;
7757 Unix.rename tmp !confpath;
7758 with exn ->
7759 prerr_endline
7760 ("error while saving configuration: " ^ exntos exn)
7762 end;;
7764 let adderrmsg src msg =
7765 Buffer.add_string state.errmsgs msg;
7766 state.newerrmsgs <- true;
7767 G.postRedisplay src
7770 let adderrfmt src fmt =
7771 Format.kprintf (fun s -> adderrmsg src s) fmt;
7774 let ract cmds =
7775 let cl = splitatspace cmds in
7776 let scan s fmt f =
7777 try Scanf.sscanf s fmt f
7778 with exn ->
7779 adderrfmt "remote exec"
7780 "error processing '%S': %s\n" cmds (exntos exn)
7782 match cl with
7783 | "reload" :: [] -> reload ()
7784 | "goto" :: args :: [] ->
7785 scan args "%u %f %f"
7786 (fun pageno x y ->
7787 let cmd, _ = state.geomcmds in
7788 if emptystr cmd
7789 then gotopagexy pageno x y
7790 else
7791 let f prevf () =
7792 gotopagexy pageno x y;
7793 prevf ()
7795 state.reprf <- f state.reprf
7797 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7798 | "gotor" :: args :: [] ->
7799 scan args "%S %u"
7800 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
7801 | "gotord" :: args :: [] ->
7802 scan args "%S %S"
7803 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
7804 | "rect" :: args :: [] ->
7805 scan args "%u %u %f %f %f %f"
7806 (fun pageno color x0 y0 x1 y1 ->
7807 onpagerect pageno (fun w h ->
7808 let _,w1,h1,_ = getpagedim pageno in
7809 let sw = float w1 /. float w
7810 and sh = float h1 /. float h in
7811 let x0s = x0 *. sw
7812 and x1s = x1 *. sw
7813 and y0s = y0 *. sh
7814 and y1s = y1 *. sh in
7815 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7816 debugrect rect;
7817 state.rects <- (pageno, color, rect) :: state.rects;
7818 G.postRedisplay "rect";
7821 | "activatewin" :: [] -> Wsi.activatewin ()
7822 | "quit" :: [] -> raise Quit
7823 | _ ->
7824 adderrfmt "remote command"
7825 "error processing remote command: %S\n" cmds;
7828 let remote =
7829 let scratch = String.create 80 in
7830 let buf = Buffer.create 80 in
7831 fun fd ->
7832 let rec tempfr () =
7833 try Some (Unix.read fd scratch 0 80)
7834 with
7835 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7836 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7837 | exn -> raise exn
7839 match tempfr () with
7840 | None -> Some fd
7841 | Some n ->
7842 if n = 0
7843 then (
7844 Unix.close fd;
7845 if Buffer.length buf > 0
7846 then (
7847 let s = Buffer.contents buf in
7848 Buffer.clear buf;
7849 ract s;
7851 None
7853 else
7854 let rec eat ppos =
7855 let nlpos =
7857 let pos = String.index_from scratch ppos '\n' in
7858 if pos >= n then -1 else pos
7859 with Not_found -> -1
7861 if nlpos >= 0
7862 then (
7863 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7864 let s = Buffer.contents buf in
7865 Buffer.clear buf;
7866 ract s;
7867 eat (nlpos+1);
7869 else (
7870 Buffer.add_substring buf scratch ppos (n-ppos);
7871 Some fd
7873 in eat 0
7876 let remoteopen path =
7877 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7878 with exn ->
7879 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7880 None
7883 let () =
7884 let trimcachepath = ref "" in
7885 let rcmdpath = ref "" in
7886 let pageno = ref None in
7887 selfexec := Sys.executable_name;
7888 Arg.parse
7889 (Arg.align
7890 [("-p", Arg.String (fun s -> state.password <- s),
7891 "<password> Set password");
7893 ("-f", Arg.String
7894 (fun s ->
7895 Config.fontpath := s;
7896 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7898 "<path> Set path to the user interface font");
7900 ("-c", Arg.String
7901 (fun s ->
7902 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7903 Config.confpath := s),
7904 "<path> Set path to the configuration file");
7906 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
7907 "<page-number> Jump to page");
7909 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7910 "<path> Set path to the trim cache file");
7912 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7913 "<named-destination> Set named destination");
7915 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7916 ("-cxack", Arg.Set cxack, " Cut corners");
7918 ("-remote", Arg.String (fun s -> rcmdpath := s),
7919 "<path> Set path to the remote commands source");
7921 ("-origin", Arg.String (fun s -> state.origin <- s),
7922 "<original-path> Set original path");
7924 ("-v", Arg.Unit (fun () ->
7925 Printf.printf
7926 "%s\nconfiguration path: %s\n"
7927 (version ())
7928 Config.defconfpath
7930 exit 0), " Print version and exit");
7933 (fun s -> state.path <- s)
7934 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7936 if !wtmode
7937 then selfexec := !selfexec ^ " -wtmode";
7939 if emptystr state.path
7940 then (prerr_endline "file name missing"; exit 1);
7942 if not (Config.load ())
7943 then prerr_endline "failed to load configuration";
7944 begin match !pageno with
7945 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
7946 | None -> ()
7947 end;
7949 let wsfd, winw, winh = Wsi.init (object (self)
7950 val mutable m_hack = false
7951 val mutable m_clicks = 0
7952 val mutable m_click_x = 0
7953 val mutable m_click_y = 0
7954 val mutable m_lastclicktime = infinity
7956 method private cleanup =
7957 state.roam <- noroam;
7958 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
7959 method expose = if not m_hack then G.postRedisplay "expose"
7960 method visible = G.postRedisplay "visible"
7961 method display = m_hack <- false; display ()
7962 method reshape w h =
7963 self#cleanup;
7964 m_hack <- w < state.winw && h < state.winh;
7965 reshape w h
7966 method mouse b d x y m =
7967 if d && canselect ()
7968 then (
7969 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
7970 m_click_x <- x;
7971 m_click_y <- y;
7972 if b = 1
7973 then (
7974 let t = now () in
7975 if abs x - m_click_x > 10
7976 || abs y - m_click_y > 10
7977 || abs_float (t -. m_lastclicktime) > 0.3
7978 then m_clicks <- 0;
7979 m_clicks <- m_clicks + 1;
7980 m_lastclicktime <- t;
7981 if m_clicks = 1
7982 then (
7983 self#cleanup;
7984 G.postRedisplay "cleanup";
7985 state.uioh <- state.uioh#button b d x y m;
7987 else state.uioh <- state.uioh#multiclick m_clicks x y m
7989 else (
7990 self#cleanup;
7991 m_clicks <- 0;
7992 m_lastclicktime <- infinity;
7993 state.uioh <- state.uioh#button b d x y m
7996 else (
7997 state.uioh <- state.uioh#button b d x y m
7999 method motion x y =
8000 state.mpos <- (x, y);
8001 state.uioh <- state.uioh#motion x y
8002 method pmotion x y =
8003 state.mpos <- (x, y);
8004 state.uioh <- state.uioh#pmotion x y
8005 method key k m =
8006 let mascm = m land (
8007 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
8008 ) in
8009 let keyboard k m =
8010 let x = state.x and y = state.y in
8011 keyboard k m;
8012 if x != state.x || y != state.y then self#cleanup
8014 match state.keystate with
8015 | KSnone ->
8016 let km = k, mascm in
8017 begin
8018 match
8019 let modehash = state.uioh#modehash in
8020 try Hashtbl.find modehash km
8021 with Not_found ->
8022 try Hashtbl.find (findkeyhash conf "global") km
8023 with Not_found -> KMinsrt (k, m)
8024 with
8025 | KMinsrt (k, m) -> keyboard k m
8026 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
8027 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
8029 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
8030 List.iter (fun (k, m) -> keyboard k m) insrt;
8031 state.keystate <- KSnone
8032 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
8033 state.keystate <- KSinto (keys, insrt)
8034 | _ ->
8035 state.keystate <- KSnone
8037 method enter x y =
8038 state.mpos <- (x, y);
8039 state.uioh <- state.uioh#pmotion x y
8040 method leave = state.mpos <- (-1, -1)
8041 method winstate wsl = state.winstate <- wsl; m_hack <- false
8042 method quit = raise Quit
8043 end) conf.cwinw conf.cwinh (platform = Posx) in
8045 state.wsfd <- wsfd;
8047 if not (
8048 List.exists GlMisc.check_extension
8049 [ "GL_ARB_texture_rectangle"
8050 ; "GL_EXT_texture_recangle"
8051 ; "GL_NV_texture_rectangle" ]
8053 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
8055 if (
8056 let r = GlMisc.get_string `renderer in
8057 let p = "Mesa DRI Intel(" in
8058 let l = String.length p in
8059 String.length r > l && String.sub r 0 l = p
8061 then (
8062 defconf.sliceheight <- 1024;
8063 defconf.texcount <- 32;
8064 defconf.usepbo <- true;
8067 let cr, sw =
8068 match Ne.res Unix.pipe with
8069 | Ne.Exn exn ->
8070 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
8071 exit 1
8072 | Ne.Res rw -> rw
8073 and sr, cw =
8074 match Ne.res Unix.pipe with
8075 | Ne.Exn exn ->
8076 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
8077 exit 1
8078 | Ne.Res rw -> rw
8081 cloexec cr;
8082 cloexec sw;
8083 cloexec sr;
8084 cloexec cw;
8086 setcheckers conf.checkers;
8087 redirectstderr ();
8088 if conf.redirectstderr
8089 then
8090 at_exit (fun () ->
8091 let s = Buffer.contents state.errmsgs ^
8092 (match state.errfd with
8093 | Some fd ->
8094 let s = String.create (80*24) in
8095 let n =
8097 let r, _, _ = Unix.select [fd] [] [] 0.0 in
8098 if List.mem fd r
8099 then Unix.read fd s 0 (String.length s)
8100 else 0
8101 with _ -> 0
8103 if n = 0
8104 then ""
8105 else String.sub s 0 n
8106 | None -> ""
8109 try ignore (Unix.write state.stderr s 0 (String.length s))
8110 with exn -> print_endline (exntos exn)
8114 init (cr, cw) (
8115 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
8116 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
8117 !Config.fontpath, !trimcachepath,
8118 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
8120 List.iter GlArray.enable [`texture_coord; `vertex];
8121 state.sr <- sr;
8122 state.sw <- sw;
8123 state.text <- "Opening " ^ (mbtoutf8 state.path);
8124 reshape winw winh;
8125 opendoc state.path state.password;
8126 state.uioh <- uioh;
8127 display ();
8128 Wsi.mapwin ();
8129 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
8130 let optrfd =
8131 ref (
8132 if nonemptystr !rcmdpath
8133 then remoteopen !rcmdpath
8134 else None
8138 let rec loop deadline =
8139 let r =
8140 match state.errfd with
8141 | None -> [state.sr; state.wsfd]
8142 | Some fd -> [state.sr; state.wsfd; fd]
8144 let r =
8145 match !optrfd with
8146 | None -> r
8147 | Some fd -> fd :: r
8149 if state.redisplay
8150 then (
8151 state.redisplay <- false;
8152 display ();
8154 let timeout =
8155 let now = now () in
8156 if deadline > now
8157 then (
8158 if deadline = infinity
8159 then ~-.1.0
8160 else max 0.0 (deadline -. now)
8162 else 0.0
8164 let r, _, _ =
8165 try Unix.select r [] [] timeout
8166 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
8168 begin match r with
8169 | [] ->
8170 state.ghyll None;
8171 let newdeadline =
8172 if state.ghyll == noghyll
8173 then
8174 match state.autoscroll with
8175 | Some step when step != 0 ->
8176 let y = state.y + step in
8177 let y =
8178 if y < 0
8179 then state.maxy
8180 else if y >= state.maxy then 0 else y
8182 gotoy y;
8183 if state.mode = View
8184 then state.text <- "";
8185 deadline +. 0.01
8186 | _ -> infinity
8187 else deadline +. 0.01
8189 loop newdeadline
8191 | l ->
8192 let rec checkfds = function
8193 | [] -> ()
8194 | fd :: rest when fd = state.sr ->
8195 let cmd = readcmd state.sr in
8196 act cmd;
8197 checkfds rest
8199 | fd :: rest when fd = state.wsfd ->
8200 Wsi.readresp fd;
8201 checkfds rest
8203 | fd :: rest when Some fd = !optrfd ->
8204 begin match remote fd with
8205 | None -> optrfd := remoteopen !rcmdpath;
8206 | opt -> optrfd := opt
8207 end;
8208 checkfds rest
8210 | fd :: rest ->
8211 let s = String.create 80 in
8212 let n = tempfailureretry (Unix.read fd s 0) 80 in
8213 if conf.redirectstderr
8214 then (
8215 Buffer.add_substring state.errmsgs s 0 n;
8216 state.newerrmsgs <- true;
8217 state.redisplay <- true;
8219 else (
8220 prerr_string (String.sub s 0 n);
8221 flush stderr;
8223 checkfds rest
8225 checkfds l;
8226 let newdeadline =
8227 let deadline1 =
8228 if deadline = infinity
8229 then now () +. 0.01
8230 else deadline
8232 match state.autoscroll with
8233 | Some step when step != 0 -> deadline1
8234 | _ -> if state.ghyll == noghyll then infinity else deadline1
8236 loop newdeadline
8237 end;
8240 loop infinity;
8241 with Quit ->
8242 Config.save ();