Optional fontconfig support
[llpp.git] / main.ml
blob2cf079e866657a6536695bf3332af3a78aab152f
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\n" 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;
1697 Gl.disable `texture_2d;
1699 | None ->
1700 GlDraw.color (1.0, 1.0, 1.0);
1701 filledrect (float x) (float y) (float (x+w)) (float (y+h));
1702 end;
1703 if conf.invert
1704 then GlTex.env (`mode `modulate);
1705 if w > 128 && h > fstate.fontsize + 10
1706 then (
1707 let c = if conf.invert then 1.0 else 0.0 in
1708 GlDraw.color (c, c, c);
1709 let c, r =
1710 if conf.verbose
1711 then (col*conf.tilew, row*conf.tileh)
1712 else col, row
1714 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1716 GlDraw.color color;
1717 begintiles ();
1719 itertiles l f;
1720 endtiles ();
1723 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1725 let tilevisible1 l x y =
1726 let ax0 = l.pagex
1727 and ax1 = l.pagex + l.pagevw
1728 and ay0 = l.pagey
1729 and ay1 = l.pagey + l.pagevh in
1731 let bx0 = x
1732 and by0 = y in
1733 let bx1 = min (bx0 + conf.tilew) l.pagew
1734 and by1 = min (by0 + conf.tileh) l.pageh in
1736 let rx0 = max ax0 bx0
1737 and ry0 = max ay0 by0
1738 and rx1 = min ax1 bx1
1739 and ry1 = min ay1 by1 in
1741 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1742 nonemptyintersection
1745 let tilevisible layout n x y =
1746 let rec findpageinlayout m = function
1747 | l :: rest when l.pageno = n ->
1748 tilevisible1 l x y || (
1749 match conf.columns with
1750 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1751 | _ -> false
1753 | _ :: rest -> findpageinlayout 0 rest
1754 | [] -> false
1756 findpageinlayout 0 layout;
1759 let tileready l x y =
1760 tilevisible1 l x y &&
1761 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1764 let tilepage n p layout =
1765 let rec loop = function
1766 | l :: rest ->
1767 if l.pageno = n
1768 then
1769 let f col row _ _ _ _ _ _ =
1770 if state.currently = Idle
1771 then
1772 match gettileopaque l col row with
1773 | Some _ -> ()
1774 | None ->
1775 let x = col*conf.tilew
1776 and y = row*conf.tileh in
1777 let w =
1778 let w = l.pagew - x in
1779 min w conf.tilew
1781 let h =
1782 let h = l.pageh - y in
1783 min h conf.tileh
1785 let pbo =
1786 if conf.usepbo
1787 then getpbo w h conf.colorspace
1788 else ~< "0"
1790 wcmd "tile %s %d %d %d %d %s"
1791 (~> p) x y w h (~> pbo);
1792 state.currently <-
1793 Tiling (
1794 l, p, conf.colorspace, conf.angle,
1795 state.gen, col, row, conf.tilew, conf.tileh
1798 itertiles l f;
1799 else
1800 loop rest
1802 | [] -> ()
1804 if nogeomcmds state.geomcmds
1805 then loop layout;
1808 let preloadlayout y =
1809 let y = if y < state.winh then 0 else y - state.winh in
1810 let h = state.winh*3 in
1811 layout y h;
1814 let load pages =
1815 let rec loop pages =
1816 if state.currently != Idle
1817 then ()
1818 else
1819 match pages with
1820 | l :: rest ->
1821 begin match getopaque l.pageno with
1822 | None ->
1823 wcmd "page %d %d" l.pageno l.pagedimno;
1824 state.currently <- Loading (l, state.gen);
1825 | Some opaque ->
1826 tilepage l.pageno opaque pages;
1827 loop rest
1828 end;
1829 | _ -> ()
1831 if nogeomcmds state.geomcmds
1832 then loop pages
1835 let preload pages =
1836 load pages;
1837 if conf.preload && state.currently = Idle
1838 then load (preloadlayout state.y);
1841 let layoutready layout =
1842 let rec fold all ls =
1843 all && match ls with
1844 | l :: rest ->
1845 let seen = ref false in
1846 let allvisible = ref true in
1847 let foo col row _ _ _ _ _ _ =
1848 seen := true;
1849 allvisible := !allvisible &&
1850 begin match gettileopaque l col row with
1851 | Some _ -> true
1852 | None -> false
1855 itertiles l foo;
1856 fold (!seen && !allvisible) rest
1857 | [] -> true
1859 let alltilesvisible = fold true layout in
1860 alltilesvisible;
1863 let gotoy y =
1864 let y = bound y 0 state.maxy in
1865 let y, layout, proceed =
1866 match conf.maxwait with
1867 | Some time when state.ghyll == noghyll ->
1868 begin match state.throttle with
1869 | None ->
1870 let layout = layout y state.winh in
1871 let ready = layoutready layout in
1872 if not ready
1873 then (
1874 load layout;
1875 state.throttle <- Some (layout, y, now ());
1877 else G.postRedisplay "gotoy showall (None)";
1878 y, layout, ready
1879 | Some (_, _, started) ->
1880 let dt = now () -. started in
1881 if dt > time
1882 then (
1883 state.throttle <- None;
1884 let layout = layout y state.winh in
1885 load layout;
1886 G.postRedisplay "maxwait";
1887 y, layout, true
1889 else -1, [], false
1892 | _ ->
1893 let layout = layout y state.winh in
1894 if not !wtmode || layoutready layout
1895 then G.postRedisplay "gotoy ready";
1896 y, layout, true
1898 if proceed
1899 then (
1900 state.y <- y;
1901 state.layout <- layout;
1902 begin match state.mode with
1903 | LinkNav (Ltexact (pageno, linkno)) ->
1904 let rec loop = function
1905 | [] ->
1906 state.mode <- LinkNav (Ltgendir 0)
1907 | l :: _ when l.pageno = pageno ->
1908 begin match getopaque pageno with
1909 | None ->
1910 state.mode <- LinkNav (Ltgendir 0)
1911 | Some opaque ->
1912 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1913 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1914 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1915 then state.mode <- LinkNav (Ltgendir 0)
1917 | _ :: rest -> loop rest
1919 loop layout
1920 | _ -> ()
1921 end;
1922 begin match state.mode with
1923 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1924 if not (pagevisible layout pageno)
1925 then (
1926 match state.layout with
1927 | [] -> ()
1928 | l :: _ ->
1929 state.mode <- Birdseye (
1930 conf, leftx, l.pageno, hooverpageno, anchor
1933 | LinkNav (Ltgendir dir as lt) ->
1934 let linknav =
1935 let rec loop = function
1936 | [] -> lt
1937 | l :: rest ->
1938 match getopaque l.pageno with
1939 | None -> loop rest
1940 | Some opaque ->
1941 let link =
1942 let ld =
1943 if dir = 0
1944 then LDfirstvisible (l.pagex, l.pagey, dir)
1945 else (
1946 if dir > 0 then LDfirst else LDlast
1949 findlink opaque ld
1951 match link with
1952 | Lnotfound -> loop rest
1953 | Lfound n ->
1954 showlinktype (getlink opaque n);
1955 Ltexact (l.pageno, n)
1957 loop state.layout
1959 state.mode <- LinkNav linknav
1960 | _ -> ()
1961 end;
1962 preload layout;
1964 state.ghyll <- noghyll;
1965 if conf.updatecurs
1966 then (
1967 let mx, my = state.mpos in
1968 updateunder mx my;
1972 let conttiling pageno opaque =
1973 tilepage pageno opaque
1974 (if conf.preload then preloadlayout state.y else state.layout)
1977 let gotoy_and_clear_text y =
1978 if not conf.verbose then state.text <- "";
1979 gotoy y;
1982 let getanchor1 l =
1983 let top =
1984 let coloff = l.pagecol * l.pageh in
1985 float (l.pagey + coloff) /. float l.pageh
1987 let dtop =
1988 if l.pagedispy = 0
1989 then
1991 else (
1992 if conf.presentation
1993 then float l.pagedispy /. float (calcips l.pageh)
1994 else float l.pagedispy /. float conf.interpagespace
1997 (l.pageno, top, dtop)
2000 let getanchor () =
2001 match state.layout with
2002 | l :: _ -> getanchor1 l
2003 | [] ->
2004 let n = page_of_y state.y in
2005 if n = -1
2006 then state.anchor
2007 else
2008 let y, h = getpageyh n in
2009 let dy = y - state.y in
2010 let dtop =
2011 if conf.presentation
2012 then
2013 let ips = calcips h in
2014 float (dy + ips) /. float ips
2015 else
2016 float dy /. float conf.interpagespace
2018 (n, 0.0, dtop)
2021 let getanchory (n, top, dtop) =
2022 let y, h = getpageyh n in
2023 if conf.presentation
2024 then
2025 let ips = calcips h in
2026 y + truncate (top*.float h -. dtop*.float ips) + ips;
2027 else
2028 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
2031 let gotoanchor anchor =
2032 gotoy (getanchory anchor);
2035 let addnav () =
2036 cbput state.hists.nav (getanchor ());
2039 let getnav dir =
2040 let anchor = cbgetc state.hists.nav dir in
2041 getanchory anchor;
2044 let gotoghyll1 single y =
2045 let scroll f n a b =
2046 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
2047 let snake f a b =
2048 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
2049 if f < a
2050 then s (float f /. float a)
2051 else (
2052 if f > b
2053 then 1.0 -. s ((float (f-b) /. float (n-b)))
2054 else 1.0
2057 snake f a b
2058 and summa n a b =
2059 let ins = float a *. 0.5
2060 and outs = float (n-b) *. 0.5 in
2061 let ones = b - a in
2062 ins +. outs +. float ones
2064 let rec set nab y sy =
2065 let (_N, _A, _B) = if single then (5,1,4) else nab in
2066 let sum = summa _N _A _B in
2067 let dy = float (y - sy) in
2068 state.ghyll <- (
2069 let rec gf n y1 o =
2070 if n >= _N
2071 then state.ghyll <- noghyll
2072 else
2073 let go n =
2074 let s = scroll n _N _A _B in
2075 let y1 = y1 +. ((s *. dy) /. sum) in
2076 gotoy_and_clear_text (truncate y1);
2077 state.ghyll <- gf (n+1) y1;
2079 match o with
2080 | None -> go n
2081 | Some y' when single -> set nab y' state.y
2082 | Some y' -> set (_N/2, 1, 1) y' state.y
2084 gf 0 (float state.y)
2087 match conf.ghyllscroll with
2088 | Some nab when not conf.presentation ->
2089 if state.ghyll == noghyll
2090 then set nab y state.y
2091 else state.ghyll (Some y)
2092 | _ ->
2093 gotoy_and_clear_text y
2096 let gotoghyll = gotoghyll1 false;;
2098 let gotopage n top =
2099 let y, h = getpageyh n in
2100 let y = y + (truncate (top *. float h)) in
2101 gotoghyll y
2104 let gotopage1 n top =
2105 let y = getpagey n in
2106 let y = y + top in
2107 gotoghyll y
2110 let invalidate s f =
2111 state.layout <- [];
2112 state.pdims <- [];
2113 state.rects <- [];
2114 state.rects1 <- [];
2115 match state.geomcmds with
2116 | ps, [] when emptystr ps ->
2117 f ();
2118 state.geomcmds <- s, [];
2120 | ps, [] ->
2121 state.geomcmds <- ps, [s, f];
2123 | ps, (s', _) :: rest when s' = s ->
2124 state.geomcmds <- ps, ((s, f) :: rest);
2126 | ps, cmds ->
2127 state.geomcmds <- ps, ((s, f) :: cmds);
2130 let flushpages () =
2131 Hashtbl.iter (fun _ opaque ->
2132 wcmd "freepage %s" (~> opaque);
2133 ) state.pagemap;
2134 Hashtbl.clear state.pagemap;
2137 let flushtiles () =
2138 if not (Queue.is_empty state.tilelru)
2139 then (
2140 Queue.iter (fun (k, p, s) ->
2141 wcmd "freetile %s" (~> p);
2142 state.memused <- state.memused - s;
2143 Hashtbl.remove state.tilemap k;
2144 ) state.tilelru;
2145 state.uioh#infochanged Memused;
2146 Queue.clear state.tilelru;
2148 load state.layout;
2151 let stateh h =
2152 let h = truncate (float h*.conf.zoom) in
2153 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2154 h - d
2157 let opendoc path password =
2158 state.path <- path;
2159 state.password <- password;
2160 state.gen <- state.gen + 1;
2161 state.docinfo <- [];
2163 flushpages ();
2164 setaalevel conf.aalevel;
2165 let titlepath =
2166 if emptystr state.origin
2167 then path
2168 else state.origin
2170 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2171 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2172 invalidate "reqlayout"
2173 (fun () ->
2174 wcmd "reqlayout %d %d %d %s\000"
2175 conf.angle (FMTE.to_int conf.fitmodel)
2176 (stateh state.winh) state.nameddest
2180 let reload () =
2181 state.anchor <- getanchor ();
2182 opendoc state.path state.password;
2185 let scalecolor c =
2186 let c = c *. conf.colorscale in
2187 (c, c, c);
2190 let scalecolor2 (r, g, b) =
2191 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2194 let docolumns = function
2195 | Csingle _ ->
2196 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2197 let rec loop pageno pdimno pdim y ph pdims =
2198 if pageno = state.pagecount
2199 then ()
2200 else
2201 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2202 match pdims with
2203 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2204 pdimno+1, pdim, rest
2205 | _ ->
2206 pdimno, pdim, pdims
2208 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2209 let y = y +
2210 (if conf.presentation
2211 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2212 else (if pageno = 0 then 0 else conf.interpagespace)
2215 a.(pageno) <- (pdimno, x, y, pdim);
2216 loop (pageno+1) pdimno pdim (y + h) h pdims
2218 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2219 conf.columns <- Csingle a;
2221 | Cmulti ((columns, coverA, coverB), _) ->
2222 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2223 let rec loop pageno pdimno pdim x y rowh pdims =
2224 let rec fixrow m = if m = pageno then () else
2225 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2226 if h < rowh
2227 then (
2228 let y = y + (rowh - h) / 2 in
2229 a.(m) <- (pdimno, x, y, pdim);
2231 fixrow (m+1)
2233 if pageno = state.pagecount
2234 then fixrow (((pageno - 1) / columns) * columns)
2235 else
2236 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2237 match pdims with
2238 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2239 pdimno+1, pdim, rest
2240 | _ ->
2241 pdimno, pdim, pdims
2243 let x, y, rowh' =
2244 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2245 then (
2246 let x = (wadjsb state.winw - w) / 2 in
2247 let ips =
2248 if conf.presentation then calcips h else conf.interpagespace in
2249 x, y + ips + rowh, h
2251 else (
2252 if (pageno - coverA) mod columns = 0
2253 then (
2254 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2255 let y =
2256 if conf.presentation
2257 then
2258 let ips = calcips h in
2259 y + (if pageno = 0 then 0 else calcips rowh + ips)
2260 else
2261 y + (if pageno = 0 then 0 else conf.interpagespace)
2263 x, y + rowh, h
2265 else x, y, max rowh h
2268 let y =
2269 if pageno > 1 && (pageno - coverA) mod columns = 0
2270 then (
2271 let y =
2272 if pageno = columns && conf.presentation
2273 then (
2274 let ips = calcips rowh in
2275 for i = 0 to pred columns
2277 let (pdimno, x, y, pdim) = a.(i) in
2278 a.(i) <- (pdimno, x, y+ips, pdim)
2279 done;
2280 y+ips;
2282 else y
2284 fixrow (pageno - columns);
2287 else y
2289 a.(pageno) <- (pdimno, x, y, pdim);
2290 let x = x + w + xoff*2 + conf.interpagespace in
2291 loop (pageno+1) pdimno pdim x y rowh' pdims
2293 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2294 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2296 | Csplit (c, _) ->
2297 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2298 let rec loop pageno pdimno pdim y pdims =
2299 if pageno = state.pagecount
2300 then ()
2301 else
2302 let pdimno, ((_, w, h, _) as pdim), pdims =
2303 match pdims with
2304 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2305 pdimno+1, pdim, rest
2306 | _ ->
2307 pdimno, pdim, pdims
2309 let cw = w / c in
2310 let rec loop1 n x y =
2311 if n = c then y else (
2312 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2313 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2316 let y = loop1 0 0 y in
2317 loop (pageno+1) pdimno pdim y pdims
2319 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2320 conf.columns <- Csplit (c, a);
2323 let represent () =
2324 docolumns conf.columns;
2325 state.maxy <- calcheight ();
2326 if state.reprf == noreprf
2327 then (
2328 match state.mode with
2329 | Birdseye (_, _, pageno, _, _) ->
2330 let y, h = getpageyh pageno in
2331 let top = (state.winh - h) / 2 in
2332 gotoy (max 0 (y - top))
2333 | _ -> gotoanchor state.anchor
2335 else (
2336 state.reprf ();
2337 state.reprf <- noreprf;
2341 let reshape w h =
2342 GlDraw.viewport 0 0 w h;
2343 let firsttime = state.geomcmds == firstgeomcmds in
2344 if not firsttime && nogeomcmds state.geomcmds
2345 then state.anchor <- getanchor ();
2347 state.winw <- w;
2348 let w = wadjsb (truncate (float w *. conf.zoom)) in
2349 let w = max w 2 in
2350 state.winh <- h;
2351 setfontsize fstate.fontsize;
2352 GlMat.mode `modelview;
2353 GlMat.load_identity ();
2355 GlMat.mode `projection;
2356 GlMat.load_identity ();
2357 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2358 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2359 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2361 let relx =
2362 if conf.zoom <= 1.0
2363 then 0.0
2364 else float state.x /. float state.w
2366 invalidate "geometry"
2367 (fun () ->
2368 state.w <- w;
2369 if not firsttime
2370 then state.x <- truncate (relx *. float w);
2371 let w =
2372 match conf.columns with
2373 | Csingle _ -> w
2374 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2375 | Csplit (c, _) -> w * c
2377 wcmd "geometry %d %d %d"
2378 w (stateh h) (FMTE.to_int conf.fitmodel)
2382 let enttext () =
2383 let len = String.length state.text in
2384 let drawstring s =
2385 let hscrollh =
2386 match state.mode with
2387 | Textentry _ | View | LinkNav _ ->
2388 let h, _, _ = state.uioh#scrollpw in
2390 | _ -> 0
2392 let rect x w =
2393 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2394 (x+.w) (float (state.winh - hscrollh))
2397 let w = float (wadjsb state.winw - 1) in
2398 if state.progress >= 0.0 && state.progress < 1.0
2399 then (
2400 GlDraw.color (0.3, 0.3, 0.3);
2401 let w1 = w *. state.progress in
2402 rect 0.0 w1;
2403 GlDraw.color (0.0, 0.0, 0.0);
2404 rect w1 (w-.w1)
2406 else (
2407 GlDraw.color (0.0, 0.0, 0.0);
2408 rect 0.0 w;
2411 GlDraw.color (1.0, 1.0, 1.0);
2412 drawstring fstate.fontsize
2413 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2415 let s =
2416 match state.mode with
2417 | Textentry ((prefix, text, _, _, _, _), _) ->
2418 let s =
2419 if len > 0
2420 then
2421 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2422 else
2423 Printf.sprintf "%s%s_" prefix text
2427 | _ -> state.text
2429 let s =
2430 if state.newerrmsgs
2431 then (
2432 if not (istextentry state.mode) && state.uioh#eformsgs
2433 then
2434 let s1 = "(press 'e' to review error messasges)" in
2435 if nonemptystr s then s ^ " " ^ s1 else s1
2436 else s
2438 else s
2440 if nonemptystr s
2441 then drawstring s
2444 let gctiles () =
2445 let len = Queue.length state.tilelru in
2446 let layout = lazy (
2447 match state.throttle with
2448 | None ->
2449 if conf.preload
2450 then preloadlayout state.y
2451 else state.layout
2452 | Some (layout, _, _) ->
2453 layout
2454 ) in
2455 let rec loop qpos =
2456 if state.memused <= conf.memlimit
2457 then ()
2458 else (
2459 if qpos < len
2460 then
2461 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2462 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2463 let (_, pw, ph, _) = getpagedim n in
2465 gen = state.gen
2466 && colorspace = conf.colorspace
2467 && angle = conf.angle
2468 && pagew = pw
2469 && pageh = ph
2470 && (
2471 let x = col*conf.tilew
2472 and y = row*conf.tileh in
2473 tilevisible (Lazy.force_val layout) n x y
2475 then Queue.push lruitem state.tilelru
2476 else (
2477 freepbo p;
2478 wcmd "freetile %s" (~> p);
2479 state.memused <- state.memused - s;
2480 state.uioh#infochanged Memused;
2481 Hashtbl.remove state.tilemap k;
2483 loop (qpos+1)
2486 loop 0
2489 let logcurrently = function
2490 | Idle -> dolog "Idle"
2491 | Loading (l, gen) ->
2492 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2493 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2494 dolog
2495 "Tiling %d[%d,%d] page=%s cs=%s angle"
2496 l.pageno col row (~> pageopaque)
2497 (CSTE.to_string colorspace)
2499 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2500 angle gen conf.angle state.gen
2501 tilew tileh
2502 conf.tilew conf.tileh
2504 | Outlining _ ->
2505 dolog "outlining"
2508 let splitatspace =
2509 let r = Str.regexp " " in
2510 fun s -> Str.bounded_split r s 2;
2513 let onpagerect pageno f =
2514 let b =
2515 match conf.columns with
2516 | Cmulti (_, b) -> b
2517 | Csingle b -> b
2518 | Csplit (_, b) -> b
2520 if pageno >= 0 && pageno < Array.length b
2521 then
2522 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2523 f w h
2526 let gotopagexy1 pageno x y =
2527 let _,w1,h1,leftx = getpagedim pageno in
2528 let top = y /. (float h1) in
2529 let left = x /. (float w1) in
2530 let py, w, h = getpageywh pageno in
2531 let wh = state.winh - hscrollh () in
2532 let x = left *. (float w) in
2533 let x = leftx + state.x + truncate x in
2534 let sx =
2535 if x < 0 || x >= wadjsb state.winw
2536 then state.x - x
2537 else state.x
2539 let pdy = truncate (top *. float h) in
2540 let y' = py + pdy in
2541 let dy = y' - state.y in
2542 let sy =
2543 if x != state.x || not (dy > 0 && dy < wh)
2544 then (
2545 if conf.presentation
2546 then
2547 if abs (py - y') > wh
2548 then y'
2549 else py
2550 else y';
2552 else state.y
2554 if state.x != sx || state.y != sy
2555 then (
2556 let x, y =
2557 if !wtmode
2558 then (
2559 let ww = wadjsb state.winw in
2560 let qx = sx / ww
2561 and qy = pdy / wh in
2562 let x = qx * ww
2563 and y = py + qy * wh in
2564 let x = if -x + ww > w1 then -(w1-ww) else x
2565 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2566 let y =
2567 if conf.presentation
2568 then
2569 if abs (py - y') > wh
2570 then y'
2571 else py
2572 else y';
2574 (x, y)
2576 else (sx, sy)
2578 state.x <- x;
2579 gotoy_and_clear_text y;
2581 else gotoy_and_clear_text state.y;
2584 let gotopagexy pageno x y =
2585 match state.mode with
2586 | Birdseye _ -> gotopage pageno 0.0
2587 | _ -> gotopagexy1 pageno x y
2590 let act cmds =
2591 (* dolog "%S" cmds; *)
2592 let cl = splitatspace cmds in
2593 let scan s fmt f =
2594 try Scanf.sscanf s fmt f
2595 with exn ->
2596 dolog "error processing '%S': %s" cmds (exntos exn);
2597 exit 1
2599 let addoutline outline =
2600 match state.currently with
2601 | Outlining outlines ->
2602 state.currently <- Outlining (outline :: outlines)
2603 | Idle -> state.currently <- Outlining [outline]
2604 | currently ->
2605 dolog "invalid outlining state";
2606 logcurrently currently
2608 match cl with
2609 | "clear" :: [] ->
2610 state.uioh#infochanged Pdim;
2611 state.pdims <- [];
2613 | "clearrects" :: [] ->
2614 state.rects <- state.rects1;
2615 G.postRedisplay "clearrects";
2617 | "continue" :: args :: [] ->
2618 let n = scan args "%u" (fun n -> n) in
2619 state.pagecount <- n;
2620 begin match state.currently with
2621 | Outlining l ->
2622 state.currently <- Idle;
2623 state.outlines <- Array.of_list (List.rev l)
2624 | _ -> ()
2625 end;
2627 let cur, cmds = state.geomcmds in
2628 if emptystr cur
2629 then failwith "umpossible";
2631 begin match List.rev cmds with
2632 | [] ->
2633 state.geomcmds <- "", [];
2634 state.throttle <- None;
2635 represent ();
2636 | (s, f) :: rest ->
2637 f ();
2638 state.geomcmds <- s, List.rev rest;
2639 end;
2640 if conf.maxwait = None && not !wtmode
2641 then G.postRedisplay "continue";
2643 | "title" :: args :: [] ->
2644 Wsi.settitle args
2646 | "msg" :: args :: [] ->
2647 showtext ' ' args
2649 | "vmsg" :: args :: [] ->
2650 if conf.verbose
2651 then showtext ' ' args
2653 | "emsg" :: args :: [] ->
2654 Buffer.add_string state.errmsgs args;
2655 state.newerrmsgs <- true;
2656 G.postRedisplay "error message"
2658 | "progress" :: args :: [] ->
2659 let progress, text =
2660 scan args "%f %n"
2661 (fun f pos ->
2662 f, String.sub args pos (String.length args - pos))
2664 state.text <- text;
2665 state.progress <- progress;
2666 G.postRedisplay "progress"
2668 | "firstmatch" :: args :: [] ->
2669 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2670 scan args "%u %d %f %f %f %f %f %f %f %f"
2671 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2672 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2674 let y = (getpagey pageno) + truncate y0 in
2675 addnav ();
2676 gotoy y;
2677 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2679 | "match" :: args :: [] ->
2680 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2681 scan args "%u %d %f %f %f %f %f %f %f %f"
2682 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2683 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2685 state.rects1 <-
2686 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2688 | "page" :: args :: [] ->
2689 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
2690 let pageopaque = ~< pageopaques in
2691 begin match state.currently with
2692 | Loading (l, gen) ->
2693 vlog "page %d took %f sec" l.pageno t;
2694 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2695 begin match state.throttle with
2696 | None ->
2697 let preloadedpages =
2698 if conf.preload
2699 then preloadlayout state.y
2700 else state.layout
2702 let evict () =
2703 let set =
2704 List.fold_left (fun s l -> IntSet.add l.pageno s)
2705 IntSet.empty preloadedpages
2707 let evictedpages =
2708 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2709 if not (IntSet.mem pageno set)
2710 then (
2711 wcmd "freepage %s" (~> opaque);
2712 key :: accu
2714 else accu
2715 ) state.pagemap []
2717 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2719 evict ();
2720 state.currently <- Idle;
2721 if gen = state.gen
2722 then (
2723 tilepage l.pageno pageopaque state.layout;
2724 load state.layout;
2725 load preloadedpages;
2726 if pagevisible state.layout l.pageno
2727 && layoutready state.layout
2728 then G.postRedisplay "page";
2731 | Some (layout, _, _) ->
2732 state.currently <- Idle;
2733 tilepage l.pageno pageopaque layout;
2734 load state.layout
2735 end;
2737 | _ ->
2738 dolog "Inconsistent loading state";
2739 logcurrently state.currently;
2740 exit 1
2743 | "tile" :: args :: [] ->
2744 let (x, y, opaques, size, t) =
2745 scan args "%u %u %s %u %f"
2746 (fun x y p size t -> (x, y, p, size, t))
2748 let opaque = ~< opaques in
2749 begin match state.currently with
2750 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2751 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2753 unmappbo opaque;
2754 if tilew != conf.tilew || tileh != conf.tileh
2755 then (
2756 wcmd "freetile %s" (~> opaque);
2757 state.currently <- Idle;
2758 load state.layout;
2760 else (
2761 puttileopaque l col row gen cs angle opaque size t;
2762 state.memused <- state.memused + size;
2763 state.uioh#infochanged Memused;
2764 gctiles ();
2765 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2766 opaque, size) state.tilelru;
2768 let layout =
2769 match state.throttle with
2770 | None -> state.layout
2771 | Some (layout, _, _) -> layout
2774 state.currently <- Idle;
2775 if gen = state.gen
2776 && conf.colorspace = cs
2777 && conf.angle = angle
2778 && tilevisible layout l.pageno x y
2779 then conttiling l.pageno pageopaque;
2781 begin match state.throttle with
2782 | None ->
2783 preload state.layout;
2784 if gen = state.gen
2785 && conf.colorspace = cs
2786 && conf.angle = angle
2787 && tilevisible state.layout l.pageno x y
2788 && (not !wtmode || layoutready state.layout)
2789 then G.postRedisplay "tile nothrottle";
2791 | Some (layout, y, _) ->
2792 let ready = layoutready layout in
2793 if ready
2794 then (
2795 state.y <- y;
2796 state.layout <- layout;
2797 state.throttle <- None;
2798 G.postRedisplay "throttle";
2800 else load layout;
2801 end;
2804 | _ ->
2805 dolog "Inconsistent tiling state";
2806 logcurrently state.currently;
2807 exit 1
2810 | "pdim" :: args :: [] ->
2811 let (n, w, h, _) as pdim =
2812 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2814 let pdim =
2815 match conf.fitmodel, conf.columns with
2816 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2817 | _ -> pdim
2819 state.uioh#infochanged Pdim;
2820 state.pdims <- pdim :: state.pdims
2822 | "o" :: args :: [] ->
2823 let (l, n, t, h, pos) =
2824 scan args "%u %u %d %u %n"
2825 (fun l n t h pos -> l, n, t, h, pos)
2827 let s = String.sub args pos (String.length args - pos) in
2828 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
2830 | "ou" :: args :: [] ->
2831 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
2832 let s = String.sub args pos len in
2833 let pos2 = pos + len + 1 in
2834 let uri = String.sub args pos2 (String.length args - pos2) in
2835 addoutline (s, l, Ouri uri)
2837 | "on" :: args :: [] ->
2838 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
2839 let s = String.sub args pos (String.length args - pos) in
2840 addoutline (s, l, Onone)
2842 | "a" :: args :: [] ->
2843 let (n, l, t) =
2844 scan args "%u %d %d" (fun n l t -> n, l, t)
2846 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2848 | "info" :: args :: [] ->
2849 state.docinfo <- (1, args) :: state.docinfo
2851 | "infoend" :: [] ->
2852 state.uioh#infochanged Docinfo;
2853 state.docinfo <- List.rev state.docinfo
2855 | _ ->
2856 error "unknown cmd `%S'" cmds
2859 let onhist cb =
2860 let rc = cb.rc in
2861 let action = function
2862 | HCprev -> cbget cb ~-1
2863 | HCnext -> cbget cb 1
2864 | HCfirst -> cbget cb ~-(cb.rc)
2865 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2866 and cancel () = cb.rc <- rc
2867 in (action, cancel)
2870 let search pattern forward =
2871 match conf.columns with
2872 | Csplit _ ->
2873 showtext '!' "searching does not work properly in split columns mode"
2874 | _ ->
2875 if nonemptystr pattern
2876 then
2877 let pn, py =
2878 match state.layout with
2879 | [] -> 0, 0
2880 | l :: _ ->
2881 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2883 wcmd "search %d %d %d %d,%s\000"
2884 (btod conf.icase) pn py (btod forward) pattern;
2887 let intentry text key =
2888 let c =
2889 if key >= 32 && key < 127
2890 then Char.chr key
2891 else '\000'
2893 match c with
2894 | '0' .. '9' ->
2895 let text = addchar text c in
2896 TEcont text
2898 | _ ->
2899 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2900 TEcont text
2903 let linknentry text key =
2904 let c =
2905 if key >= 32 && key < 127
2906 then Char.chr key
2907 else '\000'
2909 match c with
2910 | 'a' .. 'z' ->
2911 let text = addchar text c in
2912 TEcont text
2914 | _ ->
2915 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2916 TEcont text
2919 let linkndone f s =
2920 if nonemptystr s
2921 then (
2922 let n =
2923 let l = String.length s in
2924 let rec loop pos n = if pos = l then n else
2925 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2926 loop (pos+1) (n*26 + m)
2927 in loop 0 0
2929 let rec loop n = function
2930 | [] -> ()
2931 | l :: rest ->
2932 match getopaque l.pageno with
2933 | None -> loop n rest
2934 | Some opaque ->
2935 let m = getlinkcount opaque in
2936 if n < m
2937 then (
2938 let under = getlink opaque n in
2939 f under
2941 else loop (n-m) rest
2943 loop n state.layout;
2947 let textentry text key =
2948 if key land 0xff00 = 0xff00
2949 then TEcont text
2950 else TEcont (text ^ toutf8 key)
2953 let reqlayout angle fitmodel =
2954 match state.throttle with
2955 | None ->
2956 if nogeomcmds state.geomcmds
2957 then state.anchor <- getanchor ();
2958 conf.angle <- angle mod 360;
2959 if conf.angle != 0
2960 then (
2961 match state.mode with
2962 | LinkNav _ -> state.mode <- View
2963 | _ -> ()
2965 conf.fitmodel <- fitmodel;
2966 invalidate "reqlayout"
2967 (fun () ->
2968 wcmd "reqlayout %d %d %d"
2969 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2971 | _ -> ()
2974 let settrim trimmargins trimfuzz =
2975 if nogeomcmds state.geomcmds
2976 then state.anchor <- getanchor ();
2977 conf.trimmargins <- trimmargins;
2978 conf.trimfuzz <- trimfuzz;
2979 let x0, y0, x1, y1 = trimfuzz in
2980 invalidate "settrim"
2981 (fun () ->
2982 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2983 flushpages ();
2986 let setzoom zoom =
2987 match state.throttle with
2988 | None ->
2989 let zoom = max 0.0001 zoom in
2990 if zoom <> conf.zoom
2991 then (
2992 state.prevzoom <- (conf.zoom, state.x);
2993 conf.zoom <- zoom;
2994 reshape state.winw state.winh;
2995 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2998 | Some (layout, y, started) ->
2999 let time =
3000 match conf.maxwait with
3001 | None -> 0.0
3002 | Some t -> t
3004 let dt = now () -. started in
3005 if dt > time
3006 then (
3007 state.y <- y;
3008 load layout;
3012 let setcolumns mode columns coverA coverB =
3013 state.prevcolumns <- Some (conf.columns, conf.zoom);
3014 if columns < 0
3015 then (
3016 if isbirdseye mode
3017 then showtext '!' "split mode doesn't work in bird's eye"
3018 else (
3019 conf.columns <- Csplit (-columns, [||]);
3020 state.x <- 0;
3021 conf.zoom <- 1.0;
3024 else (
3025 if columns < 2
3026 then (
3027 conf.columns <- Csingle [||];
3028 state.x <- 0;
3029 setzoom 1.0;
3031 else (
3032 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
3033 conf.zoom <- 1.0;
3036 reshape state.winw state.winh;
3039 let resetmstate () =
3040 state.mstate <- Mnone;
3041 Wsi.setcursor Wsi.CURSOR_INHERIT;
3044 let enterbirdseye () =
3045 let zoom = float conf.thumbw /. float state.winw in
3046 let birdseyepageno =
3047 let cy = state.winh / 2 in
3048 let fold = function
3049 | [] -> 0
3050 | l :: rest ->
3051 let rec fold best = function
3052 | [] -> best.pageno
3053 | l :: rest ->
3054 let d = cy - (l.pagedispy + l.pagevh/2)
3055 and dbest = cy - (best.pagedispy + best.pagevh/2) in
3056 if abs d < abs dbest
3057 then fold l rest
3058 else best.pageno
3059 in fold l rest
3061 fold state.layout
3063 state.mode <- Birdseye (
3064 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
3066 resetmstate ();
3067 conf.zoom <- zoom;
3068 conf.presentation <- false;
3069 conf.interpagespace <- 10;
3070 conf.hlinks <- false;
3071 conf.fitmodel <- FitProportional;
3072 state.x <- 0;
3073 conf.maxwait <- None;
3074 conf.columns <- (
3075 match conf.beyecolumns with
3076 | Some c ->
3077 conf.zoom <- 1.0;
3078 Cmulti ((c, 0, 0), [||])
3079 | None -> Csingle [||]
3081 if conf.verbose
3082 then
3083 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3084 (100.0*.zoom)
3085 else
3086 state.text <- ""
3088 reshape state.winw state.winh;
3091 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3092 state.mode <- View;
3093 conf.zoom <- c.zoom;
3094 conf.presentation <- c.presentation;
3095 conf.interpagespace <- c.interpagespace;
3096 conf.maxwait <- c.maxwait;
3097 conf.hlinks <- c.hlinks;
3098 conf.fitmodel <- c.fitmodel;
3099 conf.beyecolumns <- (
3100 match conf.columns with
3101 | Cmulti ((c, _, _), _) -> Some c
3102 | Csingle _ -> None
3103 | Csplit _ -> failwith "leaving bird's eye split mode"
3105 conf.columns <- (
3106 match c.columns with
3107 | Cmulti (c, _) -> Cmulti (c, [||])
3108 | Csingle _ -> Csingle [||]
3109 | Csplit (c, _) -> Csplit (c, [||])
3111 if conf.verbose
3112 then
3113 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3114 (100.0*.conf.zoom)
3116 reshape state.winw state.winh;
3117 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3118 state.x <- leftx;
3121 let togglebirdseye () =
3122 match state.mode with
3123 | Birdseye vals -> leavebirdseye vals true
3124 | View -> enterbirdseye ()
3125 | _ -> ()
3128 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3129 let pageno = max 0 (pageno - incr) in
3130 let rec loop = function
3131 | [] -> gotopage1 pageno 0
3132 | l :: _ when l.pageno = pageno ->
3133 if l.pagedispy >= 0 && l.pagey = 0
3134 then G.postRedisplay "upbirdseye"
3135 else gotopage1 pageno 0
3136 | _ :: rest -> loop rest
3138 loop state.layout;
3139 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3142 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3143 let pageno = min (state.pagecount - 1) (pageno + incr) in
3144 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3145 let rec loop = function
3146 | [] ->
3147 let y, h = getpageyh pageno in
3148 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3149 gotoy (clamp dy)
3150 | l :: _ when l.pageno = pageno ->
3151 if l.pagevh != l.pageh
3152 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3153 else G.postRedisplay "downbirdseye"
3154 | _ :: rest -> loop rest
3156 loop state.layout
3159 let boundastep h step =
3160 if step < 0
3161 then bound step ~-h 0
3162 else bound step 0 h
3165 let optentry mode _ key =
3166 let btos b = if b then "on" else "off" in
3167 if key >= 32 && key < 127
3168 then
3169 let c = Char.chr key in
3170 match c with
3171 | 's' ->
3172 let ondone s =
3173 try conf.scrollstep <- int_of_string s with exc ->
3174 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3176 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3178 | 'A' ->
3179 let ondone s =
3181 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
3182 if state.autoscroll <> None
3183 then state.autoscroll <- Some conf.autoscrollstep
3184 with exc ->
3185 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3187 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3189 | 'C' ->
3190 let ondone s =
3192 let n, a, b = multicolumns_of_string s in
3193 setcolumns mode n a b;
3194 with exc ->
3195 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3197 TEswitch ("columns: ", "", None, textentry, ondone, true)
3199 | 'Z' ->
3200 let ondone s =
3202 let zoom = float (int_of_string s) /. 100.0 in
3203 setzoom zoom
3204 with exc ->
3205 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3207 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3209 | 't' ->
3210 let ondone s =
3212 conf.thumbw <- bound (int_of_string s) 2 4096;
3213 state.text <-
3214 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3215 begin match mode with
3216 | Birdseye beye ->
3217 leavebirdseye beye false;
3218 enterbirdseye ();
3219 | _ -> ();
3221 with exc ->
3222 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3224 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3226 | 'R' ->
3227 let ondone s =
3228 match try
3229 Some (int_of_string s)
3230 with exc ->
3231 state.text <- Printf.sprintf "bad integer `%s': %s"
3232 s (exntos exc);
3233 None
3234 with
3235 | Some angle -> reqlayout angle conf.fitmodel
3236 | None -> ()
3238 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3240 | 'i' ->
3241 conf.icase <- not conf.icase;
3242 TEdone ("case insensitive search " ^ (btos conf.icase))
3244 | 'p' ->
3245 conf.preload <- not conf.preload;
3246 gotoy state.y;
3247 TEdone ("preload " ^ (btos conf.preload))
3249 | 'v' ->
3250 conf.verbose <- not conf.verbose;
3251 TEdone ("verbose " ^ (btos conf.verbose))
3253 | 'd' ->
3254 conf.debug <- not conf.debug;
3255 TEdone ("debug " ^ (btos conf.debug))
3257 | 'h' ->
3258 conf.maxhfit <- not conf.maxhfit;
3259 state.maxy <- calcheight ();
3260 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3262 | 'c' ->
3263 conf.crophack <- not conf.crophack;
3264 TEdone ("crophack " ^ btos conf.crophack)
3266 | 'a' ->
3267 let s =
3268 match conf.maxwait with
3269 | None ->
3270 conf.maxwait <- Some infinity;
3271 "always wait for page to complete"
3272 | Some _ ->
3273 conf.maxwait <- None;
3274 "show placeholder if page is not ready"
3276 TEdone s
3278 | 'f' ->
3279 conf.underinfo <- not conf.underinfo;
3280 TEdone ("underinfo " ^ btos conf.underinfo)
3282 | 'P' ->
3283 conf.savebmarks <- not conf.savebmarks;
3284 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3286 | 'S' ->
3287 let ondone s =
3289 let pageno, py =
3290 match state.layout with
3291 | [] -> 0, 0
3292 | l :: _ ->
3293 l.pageno, l.pagey
3295 conf.interpagespace <- int_of_string s;
3296 docolumns conf.columns;
3297 state.maxy <- calcheight ();
3298 let y = getpagey pageno in
3299 gotoy (y + py)
3300 with exc ->
3301 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3303 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3305 | 'l' ->
3306 let fm =
3307 match conf.fitmodel with
3308 | FitProportional -> FitWidth
3309 | _ -> FitProportional
3311 reqlayout conf.angle fm;
3312 TEdone ("proportional display " ^ btos (fm == FitProportional))
3314 | 'T' ->
3315 settrim (not conf.trimmargins) conf.trimfuzz;
3316 TEdone ("trim margins " ^ btos conf.trimmargins)
3318 | 'I' ->
3319 conf.invert <- not conf.invert;
3320 TEdone ("invert colors " ^ btos conf.invert)
3322 | 'x' ->
3323 let ondone s =
3324 cbput state.hists.sel s;
3325 conf.selcmd <- s;
3327 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3328 textentry, ondone, true)
3330 | 'M' ->
3331 if conf.pax == None
3332 then conf.pax <- Some (ref (0.0, 0, 0))
3333 else conf.pax <- None;
3334 TEdone ("PAX " ^ btos (conf.pax != None))
3336 | _ ->
3337 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3338 TEstop
3339 else
3340 TEcont state.text
3343 class type lvsource = object
3344 method getitemcount : int
3345 method getitem : int -> (string * int)
3346 method hasaction : int -> bool
3347 method exit :
3348 uioh:uioh ->
3349 cancel:bool ->
3350 active:int ->
3351 first:int ->
3352 pan:int ->
3353 uioh option
3354 method getactive : int
3355 method getfirst : int
3356 method getpan : int
3357 method getminfo : (int * int) array
3358 end;;
3360 class virtual lvsourcebase = object
3361 val mutable m_active = 0
3362 val mutable m_first = 0
3363 val mutable m_pan = 0
3364 method getactive = m_active
3365 method getfirst = m_first
3366 method getpan = m_pan
3367 method getminfo : (int * int) array = [||]
3368 end;;
3370 let withoutlastutf8 s =
3371 let len = String.length s in
3372 if len = 0
3373 then s
3374 else
3375 let rec find pos =
3376 if pos = 0
3377 then pos
3378 else
3379 let b = Char.code s.[pos] in
3380 if b land 0b11000000 = 0b11000000
3381 then pos
3382 else find (pos-1)
3384 let first =
3385 if Char.code s.[len-1] land 0x80 = 0
3386 then len-1
3387 else find (len-1)
3389 String.sub s 0 first;
3392 let textentrykeyboard
3393 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3394 let key =
3395 if key >= 0xffb0 && key <= 0xffb9
3396 then key - 0xffb0 + 48 else key
3398 let enttext te =
3399 state.mode <- Textentry (te, onleave);
3400 state.text <- "";
3401 enttext ();
3402 G.postRedisplay "textentrykeyboard enttext";
3404 let histaction cmd =
3405 match opthist with
3406 | None -> ()
3407 | Some (action, _) ->
3408 state.mode <- Textentry (
3409 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3411 G.postRedisplay "textentry histaction"
3413 match key with
3414 | 0xff08 -> (* backspace *)
3415 if emptystr text && cancelonempty
3416 then (
3417 onleave Cancel;
3418 G.postRedisplay "textentrykeyboard after cancel";
3420 else
3421 let s = withoutlastutf8 text in
3422 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3424 | 0xff0d | 0xff8d -> (* (kp) enter *)
3425 ondone text;
3426 onleave Confirm;
3427 G.postRedisplay "textentrykeyboard after confirm"
3429 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3430 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3431 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3432 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3434 | 0xff1b -> (* escape*)
3435 if emptystr text
3436 then (
3437 begin match opthist with
3438 | None -> ()
3439 | Some (_, onhistcancel) -> onhistcancel ()
3440 end;
3441 onleave Cancel;
3442 state.text <- "";
3443 G.postRedisplay "textentrykeyboard after cancel2"
3445 else (
3446 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3449 | 0xff9f | 0xffff -> () (* delete *)
3451 | _ when key != 0
3452 && key land 0xff00 != 0xff00 (* keyboard *)
3453 && key land 0xfe00 != 0xfe00 (* xkb *)
3454 && key land 0xfd00 != 0xfd00 (* 3270 *)
3456 begin match onkey text key with
3457 | TEdone text ->
3458 ondone text;
3459 onleave Confirm;
3460 G.postRedisplay "textentrykeyboard after confirm2";
3462 | TEcont text ->
3463 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3465 | TEstop ->
3466 onleave Cancel;
3467 G.postRedisplay "textentrykeyboard after cancel3"
3469 | TEswitch te ->
3470 state.mode <- Textentry (te, onleave);
3471 G.postRedisplay "textentrykeyboard switch";
3472 end;
3474 | _ ->
3475 vlog "unhandled key %s" (Wsi.keyname key)
3478 let firstof first active =
3479 if first > active || abs (first - active) > fstate.maxrows - 1
3480 then max 0 (active - (fstate.maxrows/2))
3481 else first
3484 let calcfirst first active =
3485 if active > first
3486 then
3487 let rows = active - first in
3488 if rows > fstate.maxrows then active - fstate.maxrows else first
3489 else active
3492 let scrollph y maxy =
3493 let sh = float (maxy + state.winh) /. float state.winh in
3494 let sh = float state.winh /. sh in
3495 let sh = max sh (float conf.scrollh) in
3497 let percent = float y /. float maxy in
3498 let position = (float state.winh -. sh) *. percent in
3500 let position =
3501 if position +. sh > float state.winh
3502 then float state.winh -. sh
3503 else position
3505 position, sh;
3508 let coe s = (s :> uioh);;
3510 class listview ~(source:lvsource) ~trusted ~modehash =
3511 object (self)
3512 val m_pan = source#getpan
3513 val m_first = source#getfirst
3514 val m_active = source#getactive
3515 val m_qsearch = ""
3516 val m_prev_uioh = state.uioh
3518 method private elemunder y =
3519 if y < 0
3520 then None
3521 else
3522 let n = y / (fstate.fontsize+1) in
3523 if m_first + n < source#getitemcount
3524 then (
3525 if source#hasaction (m_first + n)
3526 then Some (m_first + n)
3527 else None
3529 else None
3531 method display =
3532 Gl.enable `blend;
3533 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3534 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3535 filledrect 0. 0. (float state.winw) (float state.winh);
3536 GlDraw.color (1., 1., 1.);
3537 Gl.enable `texture_2d;
3538 let fs = fstate.fontsize in
3539 let nfs = fs + 1 in
3540 let ww = fstate.wwidth in
3541 let tabw = 30.0*.ww in
3542 let itemcount = source#getitemcount in
3543 let minfo = source#getminfo in
3544 let rec loop row =
3545 if (row - m_first) > fstate.maxrows
3546 then ()
3547 else (
3548 if row >= 0 && row < itemcount
3549 then (
3550 let (s, level) = source#getitem row in
3551 let y = (row - m_first) * nfs in
3552 let x = 5.0 +. float (level + m_pan) *. ww in
3553 if row = m_active
3554 then (
3555 Gl.disable `texture_2d;
3556 let alpha = if source#hasaction row then 0.9 else 0.3 in
3557 GlDraw.color (1., 1., 1.) ~alpha;
3558 linerect 1. (float (y + 1))
3559 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3560 Gl.enable `texture_2d;
3561 GlDraw.color (1., 1., 1.);
3563 let drawtabularstring s =
3564 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3565 if trusted
3566 then
3567 let tabpos = try String.index s '\t' with Not_found -> -1 in
3568 if tabpos > 0
3569 then
3570 let len = String.length s - tabpos - 1 in
3571 let s1 = String.sub s 0 tabpos
3572 and s2 = String.sub s (tabpos + 1) len in
3573 let nx = drawstr x s1 in
3574 let sw = nx -. x in
3575 let x = x +. (max tabw sw) in
3576 drawstr x s2
3577 else
3578 drawstr x s
3579 else
3580 drawstr x s
3582 let _ = drawtabularstring s in
3583 loop (row+1)
3587 loop m_first;
3588 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
3589 let rec loop row =
3590 if (row - m_first) > fstate.maxrows
3591 then ()
3592 else (
3593 if row >= 0 && row < itemcount
3594 then (
3595 let (s, level) = source#getitem row in
3596 let y = (row - m_first) * nfs in
3597 let x = 5.0 +. float (level + m_pan) *. ww in
3598 let (first, last) = minfo.(row) in
3599 let prefix = String.sub s 0 first in
3600 let suffix = String.sub s first (last - first) in
3601 let w1 = measurestr fstate.fontsize prefix in
3602 let w2 = measurestr fstate.fontsize suffix in
3603 let x0 = x +. w1
3604 and y0 = (float (y+2)) in
3605 let x1 = x0 +. w2
3606 and y1 = (float (y+fs+3)) in
3607 filledrect x0 y0 x1 y1;
3608 loop (row+1)
3612 Gl.disable `texture_2d;
3613 if Array.length minfo > 0 then loop m_first;
3614 Gl.disable `blend;
3616 method updownlevel incr =
3617 let len = source#getitemcount in
3618 let curlevel =
3619 if m_active >= 0 && m_active < len
3620 then snd (source#getitem m_active)
3621 else -1
3623 let rec flow i =
3624 if i = len then i-1 else if i = -1 then 0 else
3625 let _, l = source#getitem i in
3626 if l != curlevel then i else flow (i+incr)
3628 let active = flow m_active in
3629 let first = calcfirst m_first active in
3630 G.postRedisplay "outline updownlevel";
3631 {< m_active = active; m_first = first >}
3633 method private key1 key mask =
3634 let set1 active first qsearch =
3635 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3637 let search active pattern incr =
3638 let active = if active = -1 then m_first else active in
3639 let dosearch re =
3640 let rec loop n =
3641 if n >= 0 && n < source#getitemcount
3642 then (
3643 let s, _ = source#getitem n in
3645 (try ignore (Str.search_forward re s 0); true
3646 with Not_found -> false)
3647 then Some n
3648 else loop (n + incr)
3650 else None
3652 loop active
3655 let re = Str.regexp_case_fold pattern in
3656 dosearch re
3657 with Failure s ->
3658 state.text <- s;
3659 None
3661 let itemcount = source#getitemcount in
3662 let find start incr =
3663 let rec find i =
3664 if i = -1 || i = itemcount
3665 then -1
3666 else (
3667 if source#hasaction i
3668 then i
3669 else find (i + incr)
3672 find start
3674 let set active first =
3675 let first = bound first 0 (itemcount - fstate.maxrows) in
3676 state.text <- "";
3677 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3679 let navigate incr =
3680 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3681 let active, first =
3682 let incr1 = if incr > 0 then 1 else -1 in
3683 if isvisible m_first m_active
3684 then
3685 let next =
3686 let next = m_active + incr in
3687 let next =
3688 if next < 0 || next >= itemcount
3689 then -1
3690 else find next incr1
3692 if abs (m_active - next) > fstate.maxrows
3693 then -1
3694 else next
3696 if next = -1
3697 then
3698 let first = m_first + incr in
3699 let first = bound first 0 (itemcount - fstate.maxrows) in
3700 let next =
3701 let next = m_active + incr in
3702 let next = bound next 0 (itemcount - 1) in
3703 find next ~-incr1
3705 let active =
3706 if next = -1
3707 then m_active
3708 else (
3709 if isvisible first next
3710 then next
3711 else m_active
3714 active, first
3715 else
3716 let first = min next m_first in
3717 let first =
3718 if abs (next - first) > fstate.maxrows
3719 then first + incr
3720 else first
3722 next, first
3723 else
3724 let first = m_first + incr in
3725 let first = bound first 0 (itemcount - 1) in
3726 let active =
3727 let next = m_active + incr in
3728 let next = bound next 0 (itemcount - 1) in
3729 let next = find next incr1 in
3730 let active =
3731 if next = -1 || abs (m_active - first) > fstate.maxrows
3732 then (
3733 let active = if m_active = -1 then next else m_active in
3734 active
3736 else next
3738 if isvisible first active
3739 then active
3740 else -1
3742 active, first
3744 G.postRedisplay "listview navigate";
3745 set active first;
3747 match key with
3748 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3749 let incr = if key = 0x72 then -1 else 1 in
3750 let active, first =
3751 match search (m_active + incr) m_qsearch incr with
3752 | None ->
3753 state.text <- m_qsearch ^ " [not found]";
3754 m_active, m_first
3755 | Some active ->
3756 state.text <- m_qsearch;
3757 active, firstof m_first active
3759 G.postRedisplay "listview ctrl-r/s";
3760 set1 active first m_qsearch;
3762 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3763 if m_active >= 0 && m_active < source#getitemcount
3764 then (
3765 let s, _ = source#getitem m_active in
3766 selstring s;
3768 coe self
3770 | 0xff08 -> (* backspace *)
3771 if emptystr m_qsearch
3772 then coe self
3773 else (
3774 let qsearch = withoutlastutf8 m_qsearch in
3775 if emptystr qsearch
3776 then (
3777 state.text <- "";
3778 G.postRedisplay "listview empty qsearch";
3779 set1 m_active m_first "";
3781 else
3782 let active, first =
3783 match search m_active qsearch ~-1 with
3784 | None ->
3785 state.text <- qsearch ^ " [not found]";
3786 m_active, m_first
3787 | Some active ->
3788 state.text <- qsearch;
3789 active, firstof m_first active
3791 G.postRedisplay "listview backspace qsearch";
3792 set1 active first qsearch
3795 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3796 let pattern = m_qsearch ^ toutf8 key in
3797 let active, first =
3798 match search m_active pattern 1 with
3799 | None ->
3800 state.text <- pattern ^ " [not found]";
3801 m_active, m_first
3802 | Some active ->
3803 state.text <- pattern;
3804 active, firstof m_first active
3806 G.postRedisplay "listview qsearch add";
3807 set1 active first pattern;
3809 | 0xff1b -> (* escape *)
3810 state.text <- "";
3811 if emptystr m_qsearch
3812 then (
3813 G.postRedisplay "list view escape";
3814 begin
3815 match
3816 source#exit (coe self) true m_active m_first m_pan
3817 with
3818 | None -> m_prev_uioh
3819 | Some uioh -> uioh
3822 else (
3823 G.postRedisplay "list view kill qsearch";
3824 coe {< m_qsearch = "" >}
3827 | 0xff0d | 0xff8d -> (* (kp) enter *)
3828 state.text <- "";
3829 let self = {< m_qsearch = "" >} in
3830 let opt =
3831 G.postRedisplay "listview enter";
3832 if m_active >= 0 && m_active < source#getitemcount
3833 then (
3834 source#exit (coe self) false m_active m_first m_pan;
3836 else (
3837 source#exit (coe self) true m_active m_first m_pan;
3840 begin match opt with
3841 | None -> m_prev_uioh
3842 | Some uioh -> uioh
3845 | 0xff9f | 0xffff -> (* (kp) delete *)
3846 coe self
3848 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3849 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3850 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3851 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3853 | 0xff53 | 0xff98 -> (* (kp) right *)
3854 state.text <- "";
3855 G.postRedisplay "listview right";
3856 coe {< m_pan = m_pan - 1 >}
3858 | 0xff51 | 0xff96 -> (* (kp) left *)
3859 state.text <- "";
3860 G.postRedisplay "listview left";
3861 coe {< m_pan = m_pan + 1 >}
3863 | 0xff50 | 0xff95 -> (* (kp) home *)
3864 let active = find 0 1 in
3865 G.postRedisplay "listview home";
3866 set active 0;
3868 | 0xff57 | 0xff9c -> (* (kp) end *)
3869 let first = max 0 (itemcount - fstate.maxrows) in
3870 let active = find (itemcount - 1) ~-1 in
3871 G.postRedisplay "listview end";
3872 set active first;
3874 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3875 coe self
3877 | _ ->
3878 dolog "listview unknown key %#x" key; coe self
3880 method key key mask =
3881 match state.mode with
3882 | Textentry te -> textentrykeyboard key mask te; coe self
3883 | _ -> self#key1 key mask
3885 method button button down x y _ =
3886 let opt =
3887 match button with
3888 | 1 when x > state.winw - conf.scrollbw ->
3889 G.postRedisplay "listview scroll";
3890 if down
3891 then
3892 let _, position, sh = self#scrollph in
3893 if y > truncate position && y < truncate (position +. sh)
3894 then (
3895 state.mstate <- Mscrolly;
3896 Some (coe self)
3898 else
3899 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3900 let first = truncate (s *. float source#getitemcount) in
3901 let first = min source#getitemcount first in
3902 Some (coe {< m_first = first; m_active = first >})
3903 else (
3904 state.mstate <- Mnone;
3905 Some (coe self);
3907 | 1 when not down ->
3908 begin match self#elemunder y with
3909 | Some n ->
3910 G.postRedisplay "listview click";
3911 source#exit (coe {< m_active = n >}) false n m_first m_pan
3912 | _ ->
3913 Some (coe self)
3915 | n when (n == 4 || n == 5) && not down ->
3916 let len = source#getitemcount in
3917 let first =
3918 if n = 5 && m_first + fstate.maxrows >= len
3919 then
3920 m_first
3921 else
3922 let first = m_first + (if n == 4 then -1 else 1) in
3923 bound first 0 (len - 1)
3925 G.postRedisplay "listview wheel";
3926 Some (coe {< m_first = first >})
3927 | n when (n = 6 || n = 7) && not down ->
3928 let inc = if n = 7 then -1 else 1 in
3929 G.postRedisplay "listview hwheel";
3930 Some (coe {< m_pan = m_pan + inc >})
3931 | _ ->
3932 Some (coe self)
3934 match opt with
3935 | None -> m_prev_uioh
3936 | Some uioh -> uioh
3938 method multiclick _ x y = self#button 1 true x y
3940 method motion _ y =
3941 match state.mstate with
3942 | Mscrolly ->
3943 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3944 let first = truncate (s *. float source#getitemcount) in
3945 let first = min source#getitemcount first in
3946 G.postRedisplay "listview motion";
3947 coe {< m_first = first; m_active = first >}
3948 | _ -> coe self
3950 method pmotion x y =
3951 if x < state.winw - conf.scrollbw
3952 then
3953 let n =
3954 match self#elemunder y with
3955 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3956 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3958 let o =
3959 if n != m_active
3960 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3961 else self
3963 coe o
3964 else (
3965 Wsi.setcursor Wsi.CURSOR_INHERIT;
3966 coe self
3969 method infochanged _ = ()
3971 method scrollpw = (0, 0.0, 0.0)
3972 method scrollph =
3973 let nfs = fstate.fontsize + 1 in
3974 let y = m_first * nfs in
3975 let itemcount = source#getitemcount in
3976 let maxi = max 0 (itemcount - fstate.maxrows) in
3977 let maxy = maxi * nfs in
3978 let p, h = scrollph y maxy in
3979 conf.scrollbw, p, h
3981 method modehash = modehash
3982 method eformsgs = false
3983 end;;
3985 class outlinelistview ~source =
3986 let settext autonarrow s =
3987 if autonarrow
3988 then
3989 let ss = source#statestr in
3990 state.text <-
3991 if emptystr ss
3992 then "[" ^ s ^ "]"
3993 else "{" ^ ss ^ "} [" ^ s ^ "]"
3994 else state.text <- s
3996 object (self)
3997 inherit listview
3998 ~source:(source :> lvsource)
3999 ~trusted:false
4000 ~modehash:(findkeyhash conf "outline")
4001 as super
4003 val m_autonarrow = false
4005 method display = super#display
4007 method key key mask =
4008 let maxrows =
4009 if emptystr state.text
4010 then fstate.maxrows
4011 else fstate.maxrows - 2
4013 let calcfirst first active =
4014 if active > first
4015 then
4016 let rows = active - first in
4017 if rows > maxrows then active - maxrows else first
4018 else active
4020 let navigate incr =
4021 let active = m_active + incr in
4022 let active = bound active 0 (source#getitemcount - 1) in
4023 let first = calcfirst m_first active in
4024 G.postRedisplay "outline navigate";
4025 coe {< m_active = active; m_first = first >}
4027 let navscroll first =
4028 let active =
4029 let dist = m_active - first in
4030 if dist < 0
4031 then first
4032 else (
4033 if dist < maxrows
4034 then m_active
4035 else first + maxrows
4038 G.postRedisplay "outline navscroll";
4039 coe {< m_first = first; m_active = active >}
4041 let ctrl = Wsi.withctrl mask in
4042 match key with
4043 | 97 when ctrl -> (* ctrl-a *)
4044 let text =
4045 if m_autonarrow
4046 then (source#denarrow; "")
4047 else (
4048 let pattern = source#renarrow in
4049 if nonemptystr m_qsearch
4050 then (source#narrow m_qsearch; m_qsearch)
4051 else pattern
4054 settext (not m_autonarrow) text;
4055 G.postRedisplay "toggle auto narrowing";
4056 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
4058 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
4059 settext true "";
4060 G.postRedisplay "toggle auto narrowing";
4061 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
4063 | 110 when ctrl -> (* ctrl-n *)
4064 source#narrow m_qsearch;
4065 if not m_autonarrow
4066 then source#add_narrow_pattern m_qsearch;
4067 G.postRedisplay "outline ctrl-n";
4068 coe {< m_first = 0; m_active = 0 >}
4070 | 83 when ctrl -> (* ctrl-S *)
4071 let active = source#calcactive (getanchor ()) in
4072 let first = firstof m_first active in
4073 G.postRedisplay "outline ctrl-s";
4074 coe {< m_first = first; m_active = active >}
4076 | 117 when ctrl -> (* ctrl-u *)
4077 G.postRedisplay "outline ctrl-u";
4078 if m_autonarrow && nonemptystr m_qsearch
4079 then (
4080 ignore (source#renarrow);
4081 settext m_autonarrow "";
4082 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4084 else (
4085 source#del_narrow_pattern;
4086 let pattern = source#renarrow in
4087 let text =
4088 if emptystr pattern then "" else "Narrowed to " ^ pattern
4090 settext m_autonarrow text;
4091 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4094 | 108 when ctrl -> (* ctrl-l *)
4095 let first = max 0 (m_active - (fstate.maxrows / 2)) in
4096 G.postRedisplay "outline ctrl-l";
4097 coe {< m_first = first >}
4099 | 0xff09 when m_autonarrow -> (* tab *)
4100 if nonemptystr m_qsearch
4101 then (
4102 G.postRedisplay "outline list view tab";
4103 source#add_narrow_pattern m_qsearch;
4104 settext true "";
4105 coe {< m_qsearch = "" >}
4107 else coe self
4109 | 0xff1b when m_autonarrow -> (* escape *)
4110 if nonemptystr m_qsearch
4111 then source#add_narrow_pattern m_qsearch;
4112 super#key key mask
4114 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
4115 if nonemptystr m_qsearch
4116 then source#add_narrow_pattern m_qsearch;
4117 super#key key mask
4119 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
4120 let pattern = m_qsearch ^ toutf8 key in
4121 G.postRedisplay "outlinelistview autonarrow add";
4122 source#narrow pattern;
4123 settext true pattern;
4124 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4126 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4127 if emptystr m_qsearch
4128 then coe self
4129 else
4130 let pattern = withoutlastutf8 m_qsearch in
4131 G.postRedisplay "outlinelistview autonarrow backspace";
4132 ignore (source#renarrow);
4133 source#narrow pattern;
4134 settext true pattern;
4135 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4137 | 0xff9f | 0xffff -> (* (kp) delete *)
4138 source#remove m_active;
4139 G.postRedisplay "outline delete";
4140 let active = max 0 (m_active-1) in
4141 coe {< m_first = firstof m_first active;
4142 m_active = active >}
4144 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4145 navscroll (max 0 (m_first - 1))
4147 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4148 navscroll (min (source#getitemcount - 1) (m_first + 1))
4150 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4151 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4152 | 0xff55 | 0xff9a -> (* (kp) prior *)
4153 navigate ~-(fstate.maxrows)
4154 | 0xff56 | 0xff9b -> (* (kp) next *)
4155 navigate fstate.maxrows
4157 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4158 let o =
4159 if ctrl
4160 then (
4161 G.postRedisplay "outline ctrl right";
4162 {< m_pan = m_pan + 1 >}
4164 else self#updownlevel 1
4166 coe o
4168 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4169 let o =
4170 if ctrl
4171 then (
4172 G.postRedisplay "outline ctrl left";
4173 {< m_pan = m_pan - 1 >}
4175 else self#updownlevel ~-1
4177 coe o
4179 | 0xff50 | 0xff95 -> (* (kp) home *)
4180 G.postRedisplay "outline home";
4181 coe {< m_first = 0; m_active = 0 >}
4183 | 0xff57 | 0xff9c -> (* (kp) end *)
4184 let active = source#getitemcount - 1 in
4185 let first = max 0 (active - fstate.maxrows) in
4186 G.postRedisplay "outline end";
4187 coe {< m_active = active; m_first = first >}
4189 | _ -> super#key key mask
4192 let gotounder under =
4193 let getpath filename =
4194 let path =
4195 if nonemptystr filename
4196 then
4197 if Filename.is_relative filename
4198 then
4199 let dir = Filename.dirname state.path in
4200 let dir =
4201 if Filename.is_implicit dir
4202 then Filename.concat (Sys.getcwd ()) dir
4203 else dir
4205 Filename.concat dir filename
4206 else filename
4207 else ""
4209 if Sys.file_exists path
4210 then path
4211 else ""
4213 match under with
4214 | Ulinkgoto (pageno, top) ->
4215 if pageno >= 0
4216 then (
4217 addnav ();
4218 gotopage1 pageno top;
4221 | Ulinkuri s ->
4222 gotouri s
4224 | Uremote (filename, pageno) ->
4225 let path = getpath filename in
4226 if nonemptystr path
4227 then (
4228 if conf.riani
4229 then
4230 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4231 try popen command []
4232 with exn ->
4233 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4234 flush stderr;
4235 else
4236 let anchor = getanchor () in
4237 let ranchor = state.path, state.password, anchor, state.origin in
4238 state.origin <- "";
4239 state.anchor <- (pageno, 0.0, 0.0);
4240 state.ranchors <- ranchor :: state.ranchors;
4241 opendoc path "";
4243 else showtext '!' ("Could not find " ^ filename)
4245 | Uremotedest (filename, destname) ->
4246 let path = getpath filename in
4247 if nonemptystr path
4248 then (
4249 if conf.riani
4250 then
4251 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4252 try popen command []
4253 with exn ->
4254 Printf.eprintf
4255 "failed to execute `%s': %s\n" command (exntos exn);
4256 flush stderr;
4257 else
4258 let anchor = getanchor () in
4259 let ranchor = state.path, state.password, anchor, state.origin in
4260 state.origin <- "";
4261 state.nameddest <- destname;
4262 state.ranchors <- ranchor :: state.ranchors;
4263 opendoc path "";
4265 else showtext '!' ("Could not find " ^ filename)
4267 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4270 let gotooutline (_, _, kind) =
4271 match kind with
4272 | Onone -> ()
4273 | Oanchor anchor ->
4274 let (pageno, y, _) = anchor in
4275 let y = getanchory
4276 (if conf.presentation then (pageno, y, 1.0) else anchor)
4278 addnav ();
4279 gotoghyll y
4280 | Ouri uri -> gotounder (Ulinkuri uri)
4281 | Olaunch cmd -> gotounder (Ulaunch cmd)
4282 | Oremote remote -> gotounder (Uremote remote)
4283 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4286 let outlinesource usebookmarks =
4287 let empty = [||] in
4288 (object (self)
4289 inherit lvsourcebase
4290 val mutable m_items = empty
4291 val mutable m_minfo = empty
4292 val mutable m_orig_items = empty
4293 val mutable m_orig_minfo = empty
4294 val mutable m_narrow_patterns = []
4295 val mutable m_hadremovals = false
4296 val mutable m_gen = -1
4298 method getitemcount =
4299 Array.length m_items + (if m_hadremovals then 1 else 0)
4301 method getitem n =
4302 if n == Array.length m_items && m_hadremovals
4303 then
4304 ("[Confirm removal]", 0)
4305 else
4306 let s, n, _ = m_items.(n) in
4307 (s, n)
4309 method exit ~uioh ~cancel ~active ~first ~pan =
4310 ignore (uioh, first);
4311 let confrimremoval = m_hadremovals && active = Array.length m_items in
4312 let items, minfo =
4313 if m_narrow_patterns = []
4314 then m_orig_items, m_orig_minfo
4315 else m_items, m_minfo
4317 if not cancel
4318 then (
4319 if not confrimremoval
4320 then (
4321 gotooutline m_items.(active);
4322 m_items <- items;
4323 m_minfo <- minfo;
4325 else (
4326 state.bookmarks <- Array.to_list m_items;
4327 m_orig_items <- m_items;
4328 m_orig_minfo <- m_minfo;
4331 else (
4332 m_items <- items;
4333 m_minfo <- minfo;
4335 m_pan <- pan;
4336 None
4338 method hasaction _ = true
4340 method greetmsg =
4341 if Array.length m_items != Array.length m_orig_items
4342 then
4343 let s =
4344 match m_narrow_patterns with
4345 | one :: [] -> one
4346 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4348 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4349 else ""
4351 method statestr =
4352 match m_narrow_patterns with
4353 | [] -> ""
4354 | one :: [] -> one
4355 | head :: _ -> "\xe2\x80\xa6" ^ head
4357 method narrow pattern =
4358 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4359 match reopt with
4360 | None -> ()
4361 | Some re ->
4362 let rec loop accu minfo n =
4363 if n = -1
4364 then (
4365 m_items <- Array.of_list accu;
4366 m_minfo <- Array.of_list minfo;
4368 else
4369 let (s, _, _) as o = m_items.(n) in
4370 let accu, minfo =
4371 let first =
4372 try Str.search_forward re s 0
4373 with Not_found -> -1
4375 if first >= 0
4376 then o :: accu, (first, Str.match_end ()) :: minfo
4377 else accu, minfo
4379 loop accu minfo (n-1)
4381 loop [] [] (Array.length m_items - 1)
4383 method getminfo = m_minfo
4385 method denarrow =
4386 m_orig_items <- (
4387 if usebookmarks
4388 then Array.of_list state.bookmarks
4389 else state.outlines
4391 m_minfo <- m_orig_minfo;
4392 m_items <- m_orig_items
4394 method remove m =
4395 if usebookmarks
4396 then
4397 if m >= 0 && m < Array.length m_items
4398 then (
4399 m_hadremovals <- true;
4400 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4401 let n = if n >= m then n+1 else n in
4402 m_items.(n)
4406 method add_narrow_pattern pattern =
4407 m_narrow_patterns <- pattern :: m_narrow_patterns
4409 method del_narrow_pattern =
4410 match m_narrow_patterns with
4411 | _ :: rest -> m_narrow_patterns <- rest
4412 | [] -> ()
4414 method renarrow =
4415 self#denarrow;
4416 match m_narrow_patterns with
4417 | pattern :: [] -> self#narrow pattern; pattern
4418 | list ->
4419 List.fold_left (fun accu pattern ->
4420 self#narrow pattern;
4421 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4423 method calcactive anchor =
4424 let rely = getanchory anchor in
4425 let rec loop n best bestd =
4426 if n = Array.length m_items
4427 then best
4428 else
4429 let _, _, kind = m_items.(n) in
4430 match kind with
4431 | Oanchor anchor ->
4432 let orely = getanchory anchor in
4433 let d = abs (orely - rely) in
4434 if d < bestd
4435 then loop (n+1) n d
4436 else loop (n+1) best bestd
4437 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
4438 loop (n+1) best bestd
4440 loop 0 ~-1 max_int
4442 method reset anchor items =
4443 m_hadremovals <- false;
4444 if state.gen != m_gen
4445 then (
4446 m_orig_items <- items;
4447 m_items <- items;
4448 m_narrow_patterns <- [];
4449 m_minfo <- empty;
4450 m_orig_minfo <- empty;
4451 m_gen <- state.gen;
4453 else (
4454 if items != m_orig_items
4455 then (
4456 m_orig_items <- items;
4457 if m_narrow_patterns == []
4458 then m_items <- items;
4461 let active = self#calcactive anchor in
4462 m_active <- active;
4463 m_first <- firstof m_first active
4464 end)
4467 let enterselector usebookmarks =
4468 resetmstate ();
4469 let source = outlinesource usebookmarks in
4470 fun errmsg ->
4471 let outlines =
4472 if usebookmarks
4473 then Array.of_list state.bookmarks
4474 else state.outlines
4476 if Array.length outlines = 0
4477 then (
4478 showtext ' ' errmsg;
4480 else (
4481 state.text <- source#greetmsg;
4482 Wsi.setcursor Wsi.CURSOR_INHERIT;
4483 let anchor = getanchor () in
4484 source#reset anchor outlines;
4485 state.uioh <- coe (new outlinelistview ~source);
4486 G.postRedisplay "enter selector";
4490 let enteroutlinemode =
4491 let f = enterselector false in
4492 fun ()-> f "Document has no outline";
4495 let enterbookmarkmode =
4496 let f = enterselector true in
4497 fun () -> f "Document has no bookmarks (yet)";
4500 let color_of_string s =
4501 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4502 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4506 let color_to_string (r, g, b) =
4507 let r = truncate (r *. 256.0)
4508 and g = truncate (g *. 256.0)
4509 and b = truncate (b *. 256.0) in
4510 Printf.sprintf "%d/%d/%d" r g b
4513 let irect_of_string s =
4514 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4517 let irect_to_string (x0,y0,x1,y1) =
4518 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4521 let makecheckers () =
4522 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4523 following to say:
4524 converted by Issac Trotts. July 25, 2002 *)
4525 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4526 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4527 let id = GlTex.gen_texture () in
4528 GlTex.bind_texture `texture_2d id;
4529 GlPix.store (`unpack_alignment 1);
4530 GlTex.image2d image;
4531 List.iter (GlTex.parameter ~target:`texture_2d)
4532 [ `mag_filter `nearest; `min_filter `nearest ];
4536 let setcheckers enabled =
4537 match state.texid with
4538 | None ->
4539 if enabled then state.texid <- Some (makecheckers ())
4541 | Some texid ->
4542 if not enabled
4543 then (
4544 GlTex.delete_texture texid;
4545 state.texid <- None;
4549 let int_of_string_with_suffix s =
4550 let l = String.length s in
4551 let s1, shift =
4552 if l > 1
4553 then
4554 let suffix = Char.lowercase s.[l-1] in
4555 match suffix with
4556 | 'k' -> String.sub s 0 (l-1), 10
4557 | 'm' -> String.sub s 0 (l-1), 20
4558 | 'g' -> String.sub s 0 (l-1), 30
4559 | _ -> s, 0
4560 else s, 0
4562 let n = int_of_string s1 in
4563 let m = n lsl shift in
4564 if m < 0 || m < n
4565 then raise (Failure "value too large")
4566 else m
4569 let string_with_suffix_of_int n =
4570 if n = 0
4571 then "0"
4572 else
4573 let units = [(30, "G"); (20, "M"); (10, "K")] in
4574 let prettyint n =
4575 let rec loop s n =
4576 let h = n mod 1000 in
4577 let n = n / 1000 in
4578 if n = 0
4579 then string_of_int h ^ s
4580 else (
4581 let s = Printf.sprintf "_%03d%s" h s in
4582 loop s n
4585 loop "" n
4587 let rec find = function
4588 | [] -> prettyint n
4589 | (shift, suffix) :: rest ->
4590 if (n land ((1 lsl shift) - 1)) = 0
4591 then prettyint (n lsr shift) ^ suffix
4592 else find rest
4594 find units
4597 let defghyllscroll = (40,8,32);;
4598 let fastghyllscroll = (5,1,2);;
4599 let neatghyllscroll = (10,1,9);;
4600 let ghyllscroll_of_string s =
4601 match s with
4602 | "default" -> Some defghyllscroll
4603 | "fast" -> Some (5,1,2)
4604 | "neat" -> Some (10,1,9)
4605 | "" | "none" -> None
4606 | _ ->
4607 let (n,a,b) as nab =
4608 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4609 if n <= a || n <= b || a >= b
4610 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4611 n a b;
4612 Some nab
4615 let ghyllscroll_to_string ((n, a, b) as nab) =
4616 (**) if nab = defghyllscroll then "default"
4617 else if nab = fastghyllscroll then "fast"
4618 else if nab = neatghyllscroll then "neat"
4619 else Printf.sprintf "%d,%d,%d" n a b;
4622 let describe_location () =
4623 let fn = page_of_y state.y in
4624 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4625 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4626 let percent =
4627 if maxy <= 0
4628 then 100.
4629 else (100. *. (float state.y /. float maxy))
4631 if fn = ln
4632 then
4633 Printf.sprintf "page %d of %d [%.2f%%]"
4634 (fn+1) state.pagecount percent
4635 else
4636 Printf.sprintf
4637 "pages %d-%d of %d [%.2f%%]"
4638 (fn+1) (ln+1) state.pagecount percent
4641 let setpresentationmode v =
4642 let n = page_of_y state.y in
4643 state.anchor <- (n, 0.0, 1.0);
4644 conf.presentation <- v;
4645 if conf.fitmodel = FitPage
4646 then reqlayout conf.angle conf.fitmodel;
4647 represent ();
4650 let enterinfomode =
4651 let btos b = if b then "\xe2\x88\x9a" else "" in
4652 let showextended = ref false in
4653 let leave mode = function
4654 | Confirm -> state.mode <- mode
4655 | Cancel -> state.mode <- mode in
4656 let src =
4657 (object
4658 val mutable m_first_time = true
4659 val mutable m_l = []
4660 val mutable m_a = [||]
4661 val mutable m_prev_uioh = nouioh
4662 val mutable m_prev_mode = View
4664 inherit lvsourcebase
4666 method reset prev_mode prev_uioh =
4667 m_a <- Array.of_list (List.rev m_l);
4668 m_l <- [];
4669 m_prev_mode <- prev_mode;
4670 m_prev_uioh <- prev_uioh;
4671 if m_first_time
4672 then (
4673 let rec loop n =
4674 if n >= Array.length m_a
4675 then ()
4676 else
4677 match m_a.(n) with
4678 | _, _, _, Action _ -> m_active <- n
4679 | _ -> loop (n+1)
4681 loop 0;
4682 m_first_time <- false;
4685 method int name get set =
4686 m_l <-
4687 (name, `int get, 1, Action (
4688 fun u ->
4689 let ondone s =
4690 try set (int_of_string s)
4691 with exn ->
4692 state.text <- Printf.sprintf "bad integer `%s': %s"
4693 s (exntos exn)
4695 state.text <- "";
4696 let te = name ^ ": ", "", None, intentry, ondone, true in
4697 state.mode <- Textentry (te, leave m_prev_mode);
4699 )) :: m_l
4701 method int_with_suffix name get set =
4702 m_l <-
4703 (name, `intws get, 1, Action (
4704 fun u ->
4705 let ondone s =
4706 try set (int_of_string_with_suffix s)
4707 with exn ->
4708 state.text <- Printf.sprintf "bad integer `%s': %s"
4709 s (exntos exn)
4711 state.text <- "";
4712 let te =
4713 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4715 state.mode <- Textentry (te, leave m_prev_mode);
4717 )) :: m_l
4719 method bool ?(offset=1) ?(btos=btos) name get set =
4720 m_l <-
4721 (name, `bool (btos, get), offset, Action (
4722 fun u ->
4723 let v = get () in
4724 set (not v);
4726 )) :: m_l
4728 method color name get set =
4729 m_l <-
4730 (name, `color get, 1, Action (
4731 fun u ->
4732 let invalid = (nan, nan, nan) in
4733 let ondone s =
4734 let c =
4735 try color_of_string s
4736 with exn ->
4737 state.text <- Printf.sprintf "bad color `%s': %s"
4738 s (exntos exn);
4739 invalid
4741 if c <> invalid
4742 then set c;
4744 let te = name ^ ": ", "", None, textentry, ondone, true in
4745 state.text <- color_to_string (get ());
4746 state.mode <- Textentry (te, leave m_prev_mode);
4748 )) :: m_l
4750 method string name get set =
4751 m_l <-
4752 (name, `string get, 1, Action (
4753 fun u ->
4754 let ondone s = set s in
4755 let te = name ^ ": ", "", None, textentry, ondone, true in
4756 state.mode <- Textentry (te, leave m_prev_mode);
4758 )) :: m_l
4760 method colorspace name get set =
4761 m_l <-
4762 (name, `string get, 1, Action (
4763 fun _ ->
4764 let source =
4765 (object
4766 inherit lvsourcebase
4768 initializer
4769 m_active <- CSTE.to_int conf.colorspace;
4770 m_first <- 0;
4772 method getitemcount =
4773 Array.length CSTE.names
4774 method getitem n =
4775 (CSTE.names.(n), 0)
4776 method exit ~uioh ~cancel ~active ~first ~pan =
4777 ignore (uioh, first, pan);
4778 if not cancel then set active;
4779 None
4780 method hasaction _ = true
4781 end)
4783 state.text <- "";
4784 let modehash = findkeyhash conf "info" in
4785 coe (new listview ~source ~trusted:true ~modehash)
4786 )) :: m_l
4788 method paxmark name get set =
4789 m_l <-
4790 (name, `string get, 1, Action (
4791 fun _ ->
4792 let source =
4793 (object
4794 inherit lvsourcebase
4796 initializer
4797 m_active <- MTE.to_int conf.paxmark;
4798 m_first <- 0;
4800 method getitemcount = Array.length MTE.names
4801 method getitem n = (MTE.names.(n), 0)
4802 method exit ~uioh ~cancel ~active ~first ~pan =
4803 ignore (uioh, first, pan);
4804 if not cancel then set active;
4805 None
4806 method hasaction _ = true
4807 end)
4809 state.text <- "";
4810 let modehash = findkeyhash conf "info" in
4811 coe (new listview ~source ~trusted:true ~modehash)
4812 )) :: m_l
4814 method fitmodel name get set =
4815 m_l <-
4816 (name, `string get, 1, Action (
4817 fun _ ->
4818 let source =
4819 (object
4820 inherit lvsourcebase
4822 initializer
4823 m_active <- FMTE.to_int conf.fitmodel;
4824 m_first <- 0;
4826 method getitemcount = Array.length FMTE.names
4827 method getitem n = (FMTE.names.(n), 0)
4828 method exit ~uioh ~cancel ~active ~first ~pan =
4829 ignore (uioh, first, pan);
4830 if not cancel then set active;
4831 None
4832 method hasaction _ = true
4833 end)
4835 state.text <- "";
4836 let modehash = findkeyhash conf "info" in
4837 coe (new listview ~source ~trusted:true ~modehash)
4838 )) :: m_l
4840 method caption s offset =
4841 m_l <- (s, `empty, offset, Noaction) :: m_l
4843 method caption2 s f offset =
4844 m_l <- (s, `string f, offset, Noaction) :: m_l
4846 method getitemcount = Array.length m_a
4848 method getitem n =
4849 let tostr = function
4850 | `int f -> string_of_int (f ())
4851 | `intws f -> string_with_suffix_of_int (f ())
4852 | `string f -> f ()
4853 | `color f -> color_to_string (f ())
4854 | `bool (btos, f) -> btos (f ())
4855 | `empty -> ""
4857 let name, t, offset, _ = m_a.(n) in
4858 ((let s = tostr t in
4859 if nonemptystr s
4860 then Printf.sprintf "%s\t%s" name s
4861 else name),
4862 offset)
4864 method exit ~uioh ~cancel ~active ~first ~pan =
4865 let uiohopt =
4866 if not cancel
4867 then (
4868 let uioh =
4869 match m_a.(active) with
4870 | _, _, _, Action f -> f uioh
4871 | _ -> uioh
4873 Some uioh
4875 else None
4877 m_active <- active;
4878 m_first <- first;
4879 m_pan <- pan;
4880 uiohopt
4882 method hasaction n =
4883 match m_a.(n) with
4884 | _, _, _, Action _ -> true
4885 | _ -> false
4886 end)
4888 let rec fillsrc prevmode prevuioh =
4889 let sep () = src#caption "" 0 in
4890 let colorp name get set =
4891 src#string name
4892 (fun () -> color_to_string (get ()))
4893 (fun v ->
4895 let c = color_of_string v in
4896 set c
4897 with exn ->
4898 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4901 let oldmode = state.mode in
4902 let birdseye = isbirdseye state.mode in
4904 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4906 src#bool "presentation mode"
4907 (fun () -> conf.presentation)
4908 (fun v -> setpresentationmode v);
4910 src#bool "ignore case in searches"
4911 (fun () -> conf.icase)
4912 (fun v -> conf.icase <- v);
4914 src#bool "preload"
4915 (fun () -> conf.preload)
4916 (fun v -> conf.preload <- v);
4918 src#bool "highlight links"
4919 (fun () -> conf.hlinks)
4920 (fun v -> conf.hlinks <- v);
4922 src#bool "under info"
4923 (fun () -> conf.underinfo)
4924 (fun v -> conf.underinfo <- v);
4926 src#bool "persistent bookmarks"
4927 (fun () -> conf.savebmarks)
4928 (fun v -> conf.savebmarks <- v);
4930 src#fitmodel "fit model"
4931 (fun () -> FMTE.to_string conf.fitmodel)
4932 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4934 src#bool "trim margins"
4935 (fun () -> conf.trimmargins)
4936 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4938 src#bool "persistent location"
4939 (fun () -> conf.jumpback)
4940 (fun v -> conf.jumpback <- v);
4942 sep ();
4943 src#int "inter-page space"
4944 (fun () -> conf.interpagespace)
4945 (fun n ->
4946 conf.interpagespace <- n;
4947 docolumns conf.columns;
4948 let pageno, py =
4949 match state.layout with
4950 | [] -> 0, 0
4951 | l :: _ ->
4952 l.pageno, l.pagey
4954 state.maxy <- calcheight ();
4955 let y = getpagey pageno in
4956 gotoy (y + py)
4959 src#int "page bias"
4960 (fun () -> conf.pagebias)
4961 (fun v -> conf.pagebias <- v);
4963 src#int "scroll step"
4964 (fun () -> conf.scrollstep)
4965 (fun n -> conf.scrollstep <- n);
4967 src#int "horizontal scroll step"
4968 (fun () -> conf.hscrollstep)
4969 (fun v -> conf.hscrollstep <- v);
4971 src#int "auto scroll step"
4972 (fun () ->
4973 match state.autoscroll with
4974 | Some step -> step
4975 | _ -> conf.autoscrollstep)
4976 (fun n ->
4977 let n = boundastep state.winh n in
4978 if state.autoscroll <> None
4979 then state.autoscroll <- Some n;
4980 conf.autoscrollstep <- n);
4982 src#int "zoom"
4983 (fun () -> truncate (conf.zoom *. 100.))
4984 (fun v -> setzoom ((float v) /. 100.));
4986 src#int "rotation"
4987 (fun () -> conf.angle)
4988 (fun v -> reqlayout v conf.fitmodel);
4990 src#int "scroll bar width"
4991 (fun () -> conf.scrollbw)
4992 (fun v ->
4993 conf.scrollbw <- v;
4994 reshape state.winw state.winh;
4997 src#int "scroll handle height"
4998 (fun () -> conf.scrollh)
4999 (fun v -> conf.scrollh <- v;);
5001 src#int "thumbnail width"
5002 (fun () -> conf.thumbw)
5003 (fun v ->
5004 conf.thumbw <- min 4096 v;
5005 match oldmode with
5006 | Birdseye beye ->
5007 leavebirdseye beye false;
5008 enterbirdseye ()
5009 | _ -> ()
5012 let mode = state.mode in
5013 src#string "columns"
5014 (fun () ->
5015 match conf.columns with
5016 | Csingle _ -> "1"
5017 | Cmulti (multi, _) -> multicolumns_to_string multi
5018 | Csplit (count, _) -> "-" ^ string_of_int count
5020 (fun v ->
5021 let n, a, b = multicolumns_of_string v in
5022 setcolumns mode n a b);
5024 sep ();
5025 src#caption "Pixmap cache" 0;
5026 src#int_with_suffix "size (advisory)"
5027 (fun () -> conf.memlimit)
5028 (fun v -> conf.memlimit <- v);
5030 src#caption2 "used"
5031 (fun () -> Printf.sprintf "%s bytes, %d tiles"
5032 (string_with_suffix_of_int state.memused)
5033 (Hashtbl.length state.tilemap)) 1;
5035 sep ();
5036 src#caption "Layout" 0;
5037 src#caption2 "Dimension"
5038 (fun () ->
5039 Printf.sprintf "%dx%d (virtual %dx%d)"
5040 state.winw state.winh
5041 state.w state.maxy)
5043 if conf.debug
5044 then
5045 src#caption2 "Position" (fun () ->
5046 Printf.sprintf "%dx%d" state.x state.y
5048 else
5049 src#caption2 "Position" (fun () -> describe_location ()) 1
5052 sep ();
5053 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
5054 "Save these parameters as global defaults at exit"
5055 (fun () -> conf.bedefault)
5056 (fun v -> conf.bedefault <- v)
5059 sep ();
5060 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
5061 src#bool ~offset:0 ~btos "Extended parameters"
5062 (fun () -> !showextended)
5063 (fun v -> showextended := v; fillsrc prevmode prevuioh);
5064 if !showextended
5065 then (
5066 src#bool "checkers"
5067 (fun () -> conf.checkers)
5068 (fun v -> conf.checkers <- v; setcheckers v);
5069 src#bool "update cursor"
5070 (fun () -> conf.updatecurs)
5071 (fun v -> conf.updatecurs <- v);
5072 src#bool "verbose"
5073 (fun () -> conf.verbose)
5074 (fun v -> conf.verbose <- v);
5075 src#bool "invert colors"
5076 (fun () -> conf.invert)
5077 (fun v -> conf.invert <- v);
5078 src#bool "max fit"
5079 (fun () -> conf.maxhfit)
5080 (fun v -> conf.maxhfit <- v);
5081 src#bool "redirect stderr"
5082 (fun () -> conf.redirectstderr)
5083 (fun v -> conf.redirectstderr <- v; redirectstderr ());
5084 src#bool "pax mode"
5085 (fun () -> conf.pax != None)
5086 (fun v ->
5087 if v
5088 then conf.pax <- Some (ref (now (), 0, 0))
5089 else conf.pax <- None);
5090 src#string "uri launcher"
5091 (fun () -> conf.urilauncher)
5092 (fun v -> conf.urilauncher <- v);
5093 src#string "path launcher"
5094 (fun () -> conf.pathlauncher)
5095 (fun v -> conf.pathlauncher <- v);
5096 src#string "tile size"
5097 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
5098 (fun v ->
5100 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
5101 conf.tilew <- max 64 w;
5102 conf.tileh <- max 64 h;
5103 flushtiles ();
5104 with exn ->
5105 state.text <- Printf.sprintf "bad tile size `%s': %s"
5106 v (exntos exn)
5108 src#int "texture count"
5109 (fun () -> conf.texcount)
5110 (fun v ->
5111 if realloctexts v
5112 then conf.texcount <- v
5113 else showtext '!' " Failed to set texture count please retry later"
5115 src#int "slice height"
5116 (fun () -> conf.sliceheight)
5117 (fun v ->
5118 conf.sliceheight <- v;
5119 wcmd "sliceh %d" conf.sliceheight;
5121 src#int "anti-aliasing level"
5122 (fun () -> conf.aalevel)
5123 (fun v ->
5124 conf.aalevel <- bound v 0 8;
5125 state.anchor <- getanchor ();
5126 opendoc state.path state.password;
5128 src#string "page scroll scaling factor"
5129 (fun () -> string_of_float conf.pgscale)
5130 (fun v ->
5132 let s = float_of_string v in
5133 conf.pgscale <- s
5134 with exn ->
5135 state.text <- Printf.sprintf
5136 "bad page scroll scaling factor `%s': %s" v (exntos exn)
5139 src#int "ui font size"
5140 (fun () -> fstate.fontsize)
5141 (fun v -> setfontsize (bound v 5 100));
5142 src#int "hint font size"
5143 (fun () -> conf.hfsize)
5144 (fun v -> conf.hfsize <- bound v 5 100);
5145 colorp "background color"
5146 (fun () -> conf.bgcolor)
5147 (fun v -> conf.bgcolor <- v);
5148 src#bool "crop hack"
5149 (fun () -> conf.crophack)
5150 (fun v -> conf.crophack <- v);
5151 src#string "trim fuzz"
5152 (fun () -> irect_to_string conf.trimfuzz)
5153 (fun v ->
5155 conf.trimfuzz <- irect_of_string v;
5156 if conf.trimmargins
5157 then settrim true conf.trimfuzz;
5158 with exn ->
5159 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
5161 src#string "throttle"
5162 (fun () ->
5163 match conf.maxwait with
5164 | None -> "show place holder if page is not ready"
5165 | Some time ->
5166 if time = infinity
5167 then "wait for page to fully render"
5168 else
5169 "wait " ^ string_of_float time
5170 ^ " seconds before showing placeholder"
5172 (fun v ->
5174 let f = float_of_string v in
5175 if f <= 0.0
5176 then conf.maxwait <- None
5177 else conf.maxwait <- Some f
5178 with exn ->
5179 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
5181 src#string "ghyll scroll"
5182 (fun () ->
5183 match conf.ghyllscroll with
5184 | None -> ""
5185 | Some nab -> ghyllscroll_to_string nab
5187 (fun v ->
5188 try conf.ghyllscroll <- ghyllscroll_of_string v
5189 with exn ->
5190 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
5192 src#string "selection command"
5193 (fun () -> conf.selcmd)
5194 (fun v -> conf.selcmd <- v);
5195 src#string "synctex command"
5196 (fun () -> conf.stcmd)
5197 (fun v -> conf.stcmd <- v);
5198 src#string "pax command"
5199 (fun () -> conf.paxcmd)
5200 (fun v -> conf.paxcmd <- v);
5201 src#colorspace "color space"
5202 (fun () -> CSTE.to_string conf.colorspace)
5203 (fun v ->
5204 conf.colorspace <- CSTE.of_int v;
5205 wcmd "cs %d" v;
5206 load state.layout;
5208 src#paxmark "pax mark method"
5209 (fun () -> MTE.to_string conf.paxmark)
5210 (fun v -> conf.paxmark <- MTE.of_int v);
5211 if pbousable ()
5212 then
5213 src#bool "use PBO"
5214 (fun () -> conf.usepbo)
5215 (fun v -> conf.usepbo <- v);
5216 src#bool "mouse wheel scrolls pages"
5217 (fun () -> conf.wheelbypage)
5218 (fun v -> conf.wheelbypage <- v);
5219 src#bool "open remote links in a new instance"
5220 (fun () -> conf.riani)
5221 (fun v -> conf.riani <- v);
5224 sep ();
5225 src#caption "Document" 0;
5226 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
5227 src#caption2 "Pages"
5228 (fun () -> string_of_int state.pagecount) 1;
5229 src#caption2 "Dimensions"
5230 (fun () -> string_of_int (List.length state.pdims)) 1;
5231 if conf.trimmargins
5232 then (
5233 sep ();
5234 src#caption "Trimmed margins" 0;
5235 src#caption2 "Dimensions"
5236 (fun () -> string_of_int (List.length state.pdims)) 1;
5239 sep ();
5240 src#caption "OpenGL" 0;
5241 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
5242 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
5244 sep ();
5245 src#caption "Location" 0;
5246 if nonemptystr state.origin
5247 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
5248 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5250 src#reset prevmode prevuioh;
5252 fun () ->
5253 state.text <- "";
5254 resetmstate ();
5255 let prevmode = state.mode
5256 and prevuioh = state.uioh in
5257 fillsrc prevmode prevuioh;
5258 let source = (src :> lvsource) in
5259 let modehash = findkeyhash conf "info" in
5260 state.uioh <- coe (object (self)
5261 inherit listview ~source ~trusted:true ~modehash as super
5262 val mutable m_prevmemused = 0
5263 method infochanged = function
5264 | Memused ->
5265 if m_prevmemused != state.memused
5266 then (
5267 m_prevmemused <- state.memused;
5268 G.postRedisplay "memusedchanged";
5270 | Pdim -> G.postRedisplay "pdimchanged"
5271 | Docinfo -> fillsrc prevmode prevuioh
5273 method key key mask =
5274 if not (Wsi.withctrl mask)
5275 then
5276 match key with
5277 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5278 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5279 | _ -> super#key key mask
5280 else super#key key mask
5281 end);
5282 G.postRedisplay "info";
5285 let enterhelpmode =
5286 let source =
5287 (object
5288 inherit lvsourcebase
5289 method getitemcount = Array.length state.help
5290 method getitem n =
5291 let s, l, _ = state.help.(n) in
5292 (s, l)
5294 method exit ~uioh ~cancel ~active ~first ~pan =
5295 let optuioh =
5296 if not cancel
5297 then (
5298 match state.help.(active) with
5299 | _, _, Action f -> Some (f uioh)
5300 | _ -> Some (uioh)
5302 else None
5304 m_active <- active;
5305 m_first <- first;
5306 m_pan <- pan;
5307 optuioh
5309 method hasaction n =
5310 match state.help.(n) with
5311 | _, _, Action _ -> true
5312 | _ -> false
5314 initializer
5315 m_active <- -1
5316 end)
5317 in fun () ->
5318 let modehash = findkeyhash conf "help" in
5319 resetmstate ();
5320 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5321 G.postRedisplay "help";
5324 let entermsgsmode =
5325 let msgsource =
5326 let re = Str.regexp "[\r\n]" in
5327 (object
5328 inherit lvsourcebase
5329 val mutable m_items = [||]
5331 method getitemcount = 1 + Array.length m_items
5333 method getitem n =
5334 if n = 0
5335 then "[Clear]", 0
5336 else m_items.(n-1), 0
5338 method exit ~uioh ~cancel ~active ~first ~pan =
5339 ignore uioh;
5340 if not cancel
5341 then (
5342 if active = 0
5343 then Buffer.clear state.errmsgs;
5345 m_active <- active;
5346 m_first <- first;
5347 m_pan <- pan;
5348 None
5350 method hasaction n =
5351 n = 0
5353 method reset =
5354 state.newerrmsgs <- false;
5355 let l = Str.split re (Buffer.contents state.errmsgs) in
5356 m_items <- Array.of_list l
5358 initializer
5359 m_active <- 0
5360 end)
5361 in fun () ->
5362 state.text <- "";
5363 resetmstate ();
5364 msgsource#reset;
5365 let source = (msgsource :> lvsource) in
5366 let modehash = findkeyhash conf "listview" in
5367 state.uioh <- coe (object
5368 inherit listview ~source ~trusted:false ~modehash as super
5369 method display =
5370 if state.newerrmsgs
5371 then msgsource#reset;
5372 super#display
5373 end);
5374 G.postRedisplay "msgs";
5377 let quickbookmark ?title () =
5378 match state.layout with
5379 | [] -> ()
5380 | l :: _ ->
5381 let title =
5382 match title with
5383 | None ->
5384 let sec = Unix.gettimeofday () in
5385 let tm = Unix.localtime sec in
5386 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5387 (l.pageno+1)
5388 tm.Unix.tm_mday
5389 tm.Unix.tm_mon
5390 (tm.Unix.tm_year + 1900)
5391 tm.Unix.tm_hour
5392 tm.Unix.tm_min
5393 | Some title -> title
5395 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5398 let setautoscrollspeed step goingdown =
5399 let incr = max 1 ((abs step) / 2) in
5400 let incr = if goingdown then incr else -incr in
5401 let astep = boundastep state.winh (step + incr) in
5402 state.autoscroll <- Some astep;
5405 let canpan () =
5406 match conf.columns with
5407 | Csplit _ -> true
5408 | _ -> state.x != 0 || conf.zoom > 1.0
5411 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5413 let existsinrow pageno (columns, coverA, coverB) p =
5414 let last = ((pageno - coverA) mod columns) + columns in
5415 let rec any = function
5416 | [] -> false
5417 | l :: rest ->
5418 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5419 then p l
5420 else (
5421 if not (p l)
5422 then (if l.pageno = last then false else any rest)
5423 else true
5426 any state.layout
5429 let nextpage () =
5430 match state.layout with
5431 | [] ->
5432 let pageno = page_of_y state.y in
5433 gotoghyll (getpagey (pageno+1))
5434 | l :: rest ->
5435 match conf.columns with
5436 | Csingle _ ->
5437 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5438 then
5439 let y = clamp (pgscale state.winh) in
5440 gotoghyll y
5441 else
5442 let pageno = min (l.pageno+1) (state.pagecount-1) in
5443 gotoghyll (getpagey pageno)
5444 | Cmulti ((c, _, _) as cl, _) ->
5445 if conf.presentation
5446 && (existsinrow l.pageno cl
5447 (fun l -> l.pageh > l.pagey + l.pagevh))
5448 then
5449 let y = clamp (pgscale state.winh) in
5450 gotoghyll y
5451 else
5452 let pageno = min (l.pageno+c) (state.pagecount-1) in
5453 gotoghyll (getpagey pageno)
5454 | Csplit (n, _) ->
5455 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5456 then
5457 let pagey, pageh = getpageyh l.pageno in
5458 let pagey = pagey + pageh * l.pagecol in
5459 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5460 gotoghyll (pagey + pageh + ips)
5463 let prevpage () =
5464 match state.layout with
5465 | [] ->
5466 let pageno = page_of_y state.y in
5467 gotoghyll (getpagey (pageno-1))
5468 | l :: _ ->
5469 match conf.columns with
5470 | Csingle _ ->
5471 if conf.presentation && l.pagey != 0
5472 then
5473 gotoghyll (clamp (pgscale ~-(state.winh)))
5474 else
5475 let pageno = max 0 (l.pageno-1) in
5476 gotoghyll (getpagey pageno)
5477 | Cmulti ((c, _, coverB) as cl, _) ->
5478 if conf.presentation &&
5479 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5480 then
5481 gotoghyll (clamp (pgscale ~-(state.winh)))
5482 else
5483 let decr =
5484 if l.pageno = state.pagecount - coverB
5485 then 1
5486 else c
5488 let pageno = max 0 (l.pageno-decr) in
5489 gotoghyll (getpagey pageno)
5490 | Csplit (n, _) ->
5491 let y =
5492 if l.pagecol = 0
5493 then
5494 if l.pageno = 0
5495 then l.pagey
5496 else
5497 let pageno = max 0 (l.pageno-1) in
5498 let pagey, pageh = getpageyh pageno in
5499 pagey + (n-1)*pageh
5500 else
5501 let pagey, pageh = getpageyh l.pageno in
5502 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5504 gotoghyll y
5507 let viewkeyboard key mask =
5508 let enttext te =
5509 let mode = state.mode in
5510 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5511 state.text <- "";
5512 enttext ();
5513 G.postRedisplay "view:enttext"
5515 let ctrl = Wsi.withctrl mask in
5516 let key =
5517 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5519 match key with
5520 | 81 -> (* Q *)
5521 exit 0
5523 | 0xff63 -> (* insert *)
5524 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5525 then (
5526 state.mode <- LinkNav (Ltgendir 0);
5527 gotoy state.y;
5529 else showtext '!' "Keyboard link navigation does not work under rotation"
5531 | 0xff1b | 113 -> (* escape / q *)
5532 begin match state.mstate with
5533 | Mzoomrect _ ->
5534 resetmstate ();
5535 G.postRedisplay "kill zoom rect";
5536 | _ ->
5537 begin match state.mode with
5538 | LinkNav _ ->
5539 state.mode <- View;
5540 G.postRedisplay "esc leave linknav"
5541 | _ ->
5542 match state.ranchors with
5543 | [] -> raise Quit
5544 | (path, password, anchor, origin) :: rest ->
5545 state.ranchors <- rest;
5546 state.anchor <- anchor;
5547 state.origin <- origin;
5548 state.nameddest <- "";
5549 opendoc path password
5550 end;
5551 end;
5553 | 0xff08 -> (* backspace *)
5554 gotoghyll (getnav ~-1)
5556 | 111 -> (* o *)
5557 enteroutlinemode ()
5559 | 117 -> (* u *)
5560 state.rects <- [];
5561 state.text <- "";
5562 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
5563 G.postRedisplay "dehighlight";
5565 | 47 | 63 -> (* / ? *)
5566 let ondone isforw s =
5567 cbput state.hists.pat s;
5568 state.searchpattern <- s;
5569 search s isforw
5571 let s = String.create 1 in
5572 s.[0] <- Char.chr key;
5573 enttext (s, "", Some (onhist state.hists.pat),
5574 textentry, ondone (key = 47), true)
5576 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5577 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5578 setzoom (conf.zoom +. incr)
5580 | 43 | 0xffab -> (* + *)
5581 let ondone s =
5582 let n =
5583 try int_of_string s with exc ->
5584 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5585 max_int
5587 if n != max_int
5588 then (
5589 conf.pagebias <- n;
5590 state.text <- "page bias is now " ^ string_of_int n;
5593 enttext ("page bias: ", "", None, intentry, ondone, true)
5595 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5596 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5597 setzoom (max 0.01 (conf.zoom -. decr))
5599 | 45 | 0xffad -> (* - *)
5600 let ondone msg = state.text <- msg in
5601 enttext (
5602 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5603 optentry state.mode, ondone, true
5606 | 48 when ctrl -> (* ctrl-0 *)
5607 if conf.zoom = 1.0
5608 then (
5609 state.x <- 0;
5610 gotoy state.y
5612 else setzoom 1.0
5614 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5615 let cols =
5616 match conf.columns with
5617 | Csingle _ | Cmulti _ -> 1
5618 | Csplit (n, _) -> n
5620 let h = state.winh -
5621 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5623 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5624 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5625 then setzoom zoom
5627 | 51 when ctrl -> (* ctrl-3 *)
5628 let fm =
5629 match conf.fitmodel with
5630 | FitWidth -> FitProportional
5631 | FitProportional -> FitPage
5632 | FitPage -> FitWidth
5634 state.text <- "fit model: " ^ FMTE.to_string fm;
5635 reqlayout conf.angle fm
5637 | 0xffc6 -> (* f9 *)
5638 togglebirdseye ()
5640 | 57 when ctrl -> (* ctrl-9 *)
5641 togglebirdseye ()
5643 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5644 when not ctrl -> (* 0..9 *)
5645 let ondone s =
5646 let n =
5647 try int_of_string s with exc ->
5648 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5651 if n >= 0
5652 then (
5653 addnav ();
5654 cbput state.hists.pag (string_of_int n);
5655 gotopage1 (n + conf.pagebias - 1) 0;
5658 let pageentry text key =
5659 match Char.unsafe_chr key with
5660 | 'g' -> TEdone text
5661 | _ -> intentry text key
5663 let text = "x" in text.[0] <- Char.chr key;
5664 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5666 | 98 -> (* b *)
5667 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5668 reshape state.winw state.winh;
5670 | 66 -> (* B *)
5671 state.bzoom <- not state.bzoom;
5672 state.rects <- [];
5673 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5675 | 108 -> (* l *)
5676 conf.hlinks <- not conf.hlinks;
5677 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5678 G.postRedisplay "toggle highlightlinks";
5680 | 70 -> (* F *)
5681 state.glinks <- true;
5682 let mode = state.mode in
5683 state.mode <- Textentry (
5684 (":", "", None, linknentry, linkndone gotounder, false),
5685 (fun _ ->
5686 state.glinks <- false;
5687 state.mode <- mode)
5689 state.text <- "";
5690 G.postRedisplay "view:linkent(F)"
5692 | 121 -> (* y *)
5693 state.glinks <- true;
5694 let mode = state.mode in
5695 state.mode <- Textentry (
5697 ":", "", None, linknentry, linkndone (fun under ->
5698 selstring (undertext under);
5699 ), false
5701 fun _ ->
5702 state.glinks <- false;
5703 state.mode <- mode
5705 state.text <- "";
5706 G.postRedisplay "view:linkent"
5708 | 97 -> (* a *)
5709 begin match state.autoscroll with
5710 | Some step ->
5711 conf.autoscrollstep <- step;
5712 state.autoscroll <- None
5713 | None ->
5714 if conf.autoscrollstep = 0
5715 then state.autoscroll <- Some 1
5716 else state.autoscroll <- Some conf.autoscrollstep
5719 | 112 when ctrl -> (* ctrl-p *)
5720 launchpath ()
5722 | 80 -> (* P *)
5723 setpresentationmode (not conf.presentation);
5724 showtext ' ' ("presentation mode " ^
5725 if conf.presentation then "on" else "off");
5727 | 102 -> (* f *)
5728 if List.mem Wsi.Fullscreen state.winstate
5729 then Wsi.reshape conf.cwinw conf.cwinh
5730 else Wsi.fullscreen ()
5732 | 112 | 78 -> (* p|N *)
5733 search state.searchpattern false
5735 | 110 | 0xffc0 -> (* n|F3 *)
5736 search state.searchpattern true
5738 | 116 -> (* t *)
5739 begin match state.layout with
5740 | [] -> ()
5741 | l :: _ ->
5742 gotoghyll (getpagey l.pageno)
5745 | 32 -> (* space *)
5746 nextpage ()
5748 | 0xff9f | 0xffff -> (* delete *)
5749 prevpage ()
5751 | 61 -> (* = *)
5752 showtext ' ' (describe_location ());
5754 | 119 -> (* w *)
5755 begin match state.layout with
5756 | [] -> ()
5757 | l :: _ ->
5758 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5759 G.postRedisplay "w"
5762 | 39 -> (* ' *)
5763 enterbookmarkmode ()
5765 | 104 | 0xffbe -> (* h|F1 *)
5766 enterhelpmode ()
5768 | 105 -> (* i *)
5769 enterinfomode ()
5771 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5772 entermsgsmode ()
5774 | 109 -> (* m *)
5775 let ondone s =
5776 match state.layout with
5777 | l :: _ ->
5778 if nonemptystr s
5779 then
5780 state.bookmarks <-
5781 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5782 | _ -> ()
5784 enttext ("bookmark: ", "", None, textentry, ondone, true)
5786 | 126 -> (* ~ *)
5787 quickbookmark ();
5788 showtext ' ' "Quick bookmark added";
5790 | 122 -> (* z *)
5791 begin match state.layout with
5792 | l :: _ ->
5793 let rect = getpdimrect l.pagedimno in
5794 let w, h =
5795 if conf.crophack
5796 then
5797 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5798 truncate (1.2 *. (rect.(3) -. rect.(0))))
5799 else
5800 (truncate (rect.(1) -. rect.(0)),
5801 truncate (rect.(3) -. rect.(0)))
5803 let w = truncate ((float w)*.conf.zoom)
5804 and h = truncate ((float h)*.conf.zoom) in
5805 if w != 0 && h != 0
5806 then (
5807 state.anchor <- getanchor ();
5808 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5810 G.postRedisplay "z";
5812 | [] -> ()
5815 | 120 -> (* x *)
5816 state.roam ()
5817 | 60 | 62 -> (* < > *)
5818 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5820 | 91 | 93 -> (* [ ] *)
5821 conf.colorscale <-
5822 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5824 G.postRedisplay "brightness";
5826 | 99 when state.mode = View -> (* [alt-]c *)
5827 if Wsi.withalt mask
5828 then (
5829 if conf.zoom > 1.0
5830 then
5831 let m = (wadjsb state.winw - state.w) / 2 in
5832 state.x <- m;
5833 gotoy_and_clear_text state.y
5835 else
5836 let (c, a, b), z =
5837 match state.prevcolumns with
5838 | None -> (1, 0, 0), 1.0
5839 | Some (columns, z) ->
5840 let cab =
5841 match columns with
5842 | Csplit (c, _) -> -c, 0, 0
5843 | Cmulti ((c, a, b), _) -> c, a, b
5844 | Csingle _ -> 1, 0, 0
5846 cab, z
5848 setcolumns View c a b;
5849 setzoom z
5851 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5852 -> (* ctrl-shift- (kp) [up|down] *)
5853 let zoom, x = state.prevzoom in
5854 setzoom zoom;
5855 state.x <- x;
5857 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5858 begin match state.autoscroll with
5859 | None ->
5860 begin match state.mode with
5861 | Birdseye beye -> upbirdseye 1 beye
5862 | _ ->
5863 if ctrl
5864 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5865 else (
5866 if not (Wsi.withshift mask) && conf.presentation
5867 then prevpage ()
5868 else gotoghyll1 true (clamp (-conf.scrollstep))
5871 | Some n ->
5872 setautoscrollspeed n false
5875 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5876 begin match state.autoscroll with
5877 | None ->
5878 begin match state.mode with
5879 | Birdseye beye -> downbirdseye 1 beye
5880 | _ ->
5881 if ctrl
5882 then gotoy_and_clear_text (clamp (state.winh/2))
5883 else (
5884 if not (Wsi.withshift mask) && conf.presentation
5885 then nextpage ()
5886 else gotoghyll1 true (clamp (conf.scrollstep))
5889 | Some n ->
5890 setautoscrollspeed n true
5893 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5894 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5895 if canpan ()
5896 then
5897 let dx =
5898 if ctrl
5899 then state.winw / 2
5900 else conf.hscrollstep
5902 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5903 state.x <- panbound (state.x + dx);
5904 gotoy_and_clear_text state.y
5905 else (
5906 state.text <- "";
5907 G.postRedisplay "left/right"
5910 | 0xff55 | 0xff9a -> (* (kp) prior *)
5911 let y =
5912 if ctrl
5913 then
5914 match state.layout with
5915 | [] -> state.y
5916 | l :: _ -> state.y - l.pagey
5917 else
5918 clamp (pgscale (-state.winh))
5920 gotoghyll y
5922 | 0xff56 | 0xff9b -> (* (kp) next *)
5923 let y =
5924 if ctrl
5925 then
5926 match List.rev state.layout with
5927 | [] -> state.y
5928 | l :: _ -> getpagey l.pageno
5929 else
5930 clamp (pgscale state.winh)
5932 gotoghyll y
5934 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5935 gotoghyll 0
5936 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5937 gotoghyll (clamp state.maxy)
5939 | 0xff53 | 0xff98
5940 when Wsi.withalt mask -> (* alt-(kp) right *)
5941 gotoghyll (getnav 1)
5942 | 0xff51 | 0xff96
5943 when Wsi.withalt mask -> (* alt-(kp) left *)
5944 gotoghyll (getnav ~-1)
5946 | 114 -> (* r *)
5947 reload ()
5949 | 118 when conf.debug -> (* v *)
5950 state.rects <- [];
5951 List.iter (fun l ->
5952 match getopaque l.pageno with
5953 | None -> ()
5954 | Some opaque ->
5955 let x0, y0, x1, y1 = pagebbox opaque in
5956 let a,b = float x0, float y0 in
5957 let c,d = float x1, float y0 in
5958 let e,f = float x1, float y1 in
5959 let h,j = float x0, float y1 in
5960 let rect = (a,b,c,d,e,f,h,j) in
5961 debugrect rect;
5962 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5963 ) state.layout;
5964 G.postRedisplay "v";
5966 | 124 -> (* | *)
5967 let mode = state.mode in
5968 let cmd = ref "" in
5969 let onleave = function
5970 | Cancel -> state.mode <- mode
5971 | Confirm ->
5972 List.iter (fun l ->
5973 match getopaque l.pageno with
5974 | Some opaque -> pipesel opaque !cmd
5975 | None -> ()) state.layout;
5976 state.mode <- mode
5978 let ondone s =
5979 cbput state.hists.sel s;
5980 cmd := s
5982 let te =
5983 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5985 G.postRedisplay "|";
5986 state.mode <- Textentry (te, onleave);
5988 | _ ->
5989 vlog "huh? %s" (Wsi.keyname key)
5992 let linknavkeyboard key mask linknav =
5993 let getpage pageno =
5994 let rec loop = function
5995 | [] -> None
5996 | l :: _ when l.pageno = pageno -> Some l
5997 | _ :: rest -> loop rest
5998 in loop state.layout
6000 let doexact (pageno, n) =
6001 match getopaque pageno, getpage pageno with
6002 | Some opaque, Some l ->
6003 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
6004 then
6005 let under = getlink opaque n in
6006 G.postRedisplay "link gotounder";
6007 gotounder under;
6008 state.mode <- View;
6009 else
6010 let opt, dir =
6011 match key with
6012 | 0xff50 -> (* home *)
6013 Some (findlink opaque LDfirst), -1
6015 | 0xff57 -> (* end *)
6016 Some (findlink opaque LDlast), 1
6018 | 0xff51 -> (* left *)
6019 Some (findlink opaque (LDleft n)), -1
6021 | 0xff53 -> (* right *)
6022 Some (findlink opaque (LDright n)), 1
6024 | 0xff52 -> (* up *)
6025 Some (findlink opaque (LDup n)), -1
6027 | 0xff54 -> (* down *)
6028 Some (findlink opaque (LDdown n)), 1
6030 | _ -> None, 0
6032 let pwl l dir =
6033 begin match findpwl l.pageno dir with
6034 | Pwlnotfound -> ()
6035 | Pwl pageno ->
6036 let notfound dir =
6037 state.mode <- LinkNav (Ltgendir dir);
6038 let y, h = getpageyh pageno in
6039 let y =
6040 if dir < 0
6041 then y + h - state.winh
6042 else y
6044 gotoy y
6046 begin match getopaque pageno, getpage pageno with
6047 | Some opaque, Some _ ->
6048 let link =
6049 let ld = if dir > 0 then LDfirst else LDlast in
6050 findlink opaque ld
6052 begin match link with
6053 | Lfound m ->
6054 showlinktype (getlink opaque m);
6055 state.mode <- LinkNav (Ltexact (pageno, m));
6056 G.postRedisplay "linknav jpage";
6057 | _ -> notfound dir
6058 end;
6059 | _ -> notfound dir
6060 end;
6061 end;
6063 begin match opt with
6064 | Some Lnotfound -> pwl l dir;
6065 | Some (Lfound m) ->
6066 if m = n
6067 then pwl l dir
6068 else (
6069 let _, y0, _, y1 = getlinkrect opaque m in
6070 if y0 < l.pagey
6071 then gotopage1 l.pageno y0
6072 else (
6073 let d = fstate.fontsize + 1 in
6074 if y1 - l.pagey > l.pagevh - d
6075 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
6076 else G.postRedisplay "linknav";
6078 showlinktype (getlink opaque m);
6079 state.mode <- LinkNav (Ltexact (l.pageno, m));
6082 | None -> viewkeyboard key mask
6083 end;
6084 | _ -> viewkeyboard key mask
6086 if key = 0xff63
6087 then (
6088 state.mode <- View;
6089 G.postRedisplay "leave linknav"
6091 else
6092 match linknav with
6093 | Ltgendir _ -> viewkeyboard key mask
6094 | Ltexact exact -> doexact exact
6097 let keyboard key mask =
6098 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
6099 then wcmd "interrupt"
6100 else state.uioh <- state.uioh#key key mask
6103 let birdseyekeyboard key mask
6104 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
6105 let incr =
6106 match conf.columns with
6107 | Csingle _ -> 1
6108 | Cmulti ((c, _, _), _) -> c
6109 | Csplit _ -> failwith "bird's eye split mode"
6111 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
6112 match key with
6113 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
6114 let y, h = getpageyh pageno in
6115 let top = (state.winh - h) / 2 in
6116 gotoy (max 0 (y - top))
6117 | 0xff0d (* enter *)
6118 | 0xff8d -> leavebirdseye beye false (* kp enter *)
6119 | 0xff1b -> leavebirdseye beye true (* escape *)
6120 | 0xff52 -> upbirdseye incr beye (* up *)
6121 | 0xff54 -> downbirdseye incr beye (* down *)
6122 | 0xff51 -> upbirdseye 1 beye (* left *)
6123 | 0xff53 -> downbirdseye 1 beye (* right *)
6125 | 0xff55 -> (* prior *)
6126 begin match state.layout with
6127 | l :: _ ->
6128 if l.pagey != 0
6129 then (
6130 state.mode <- Birdseye (
6131 oconf, leftx, l.pageno, hooverpageno, anchor
6133 gotopage1 l.pageno 0;
6135 else (
6136 let layout = layout (state.y-state.winh) (pgh state.layout) in
6137 match layout with
6138 | [] -> gotoy (clamp (-state.winh))
6139 | l :: _ ->
6140 state.mode <- Birdseye (
6141 oconf, leftx, l.pageno, hooverpageno, anchor
6143 gotopage1 l.pageno 0
6146 | [] -> gotoy (clamp (-state.winh))
6147 end;
6149 | 0xff56 -> (* next *)
6150 begin match List.rev state.layout with
6151 | l :: _ ->
6152 let layout = layout (state.y + (pgh state.layout)) state.winh in
6153 begin match layout with
6154 | [] ->
6155 let incr = l.pageh - l.pagevh in
6156 if incr = 0
6157 then (
6158 state.mode <-
6159 Birdseye (
6160 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
6162 G.postRedisplay "birdseye pagedown";
6164 else gotoy (clamp (incr + conf.interpagespace*2));
6166 | l :: _ ->
6167 state.mode <-
6168 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
6169 gotopage1 l.pageno 0;
6172 | [] -> gotoy (clamp state.winh)
6173 end;
6175 | 0xff50 -> (* home *)
6176 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
6177 gotopage1 0 0
6179 | 0xff57 -> (* end *)
6180 let pageno = state.pagecount - 1 in
6181 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
6182 if not (pagevisible state.layout pageno)
6183 then
6184 let h =
6185 match List.rev state.pdims with
6186 | [] -> state.winh
6187 | (_, _, h, _) :: _ -> h
6189 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
6190 else G.postRedisplay "birdseye end";
6191 | _ -> viewkeyboard key mask
6194 let drawpage l =
6195 let color =
6196 match state.mode with
6197 | Textentry _ -> scalecolor 0.4
6198 | LinkNav _
6199 | View -> scalecolor 1.0
6200 | Birdseye (_, _, pageno, hooverpageno, _) ->
6201 if l.pageno = hooverpageno
6202 then scalecolor 0.9
6203 else (
6204 if l.pageno = pageno
6205 then scalecolor 1.0
6206 else scalecolor 0.8
6209 drawtiles l color;
6212 let postdrawpage l linkindexbase =
6213 match getopaque l.pageno with
6214 | Some opaque ->
6215 if tileready l l.pagex l.pagey
6216 then
6217 let x = l.pagedispx - l.pagex
6218 and y = l.pagedispy - l.pagey in
6219 let hlmask =
6220 match conf.columns with
6221 | Csingle _ | Cmulti _ ->
6222 (if conf.hlinks then 1 else 0)
6223 + (if state.glinks
6224 && not (isbirdseye state.mode) then 2 else 0)
6225 | _ -> 0
6227 let s =
6228 match state.mode with
6229 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6230 | _ -> ""
6232 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6233 else 0
6234 | _ -> 0
6237 let scrollindicator () =
6238 let sbw, ph, sh = state.uioh#scrollph in
6239 let sbh, pw, sw = state.uioh#scrollpw in
6241 GlDraw.color (0.64, 0.64, 0.64);
6242 filledrect
6243 (float (state.winw - sbw)) 0.
6244 (float state.winw) (float state.winh)
6246 filledrect
6247 0. (float (state.winh - sbh))
6248 (float (wadjsb state.winw - 1)) (float state.winh)
6250 GlDraw.color (0.0, 0.0, 0.0);
6252 filledrect
6253 (float (state.winw - sbw)) ph
6254 (float state.winw) (ph +. sh)
6256 filledrect
6257 pw (float (state.winh - sbh))
6258 (pw +. sw) (float state.winh)
6262 let showsel () =
6263 match state.mstate with
6264 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6267 | Msel ((x0, y0), (x1, y1)) ->
6268 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
6269 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< "", -1, 0, 0) in
6270 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< "", -1, 0, 0) in
6271 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
6274 let showrects = function [] -> () | rects ->
6275 Gl.enable `blend;
6276 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6277 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6278 List.iter
6279 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6280 List.iter (fun l ->
6281 if l.pageno = pageno
6282 then (
6283 let dx = float (l.pagedispx - l.pagex) in
6284 let dy = float (l.pagedispy - l.pagey) in
6285 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6286 Raw.sets_float state.vraw ~pos:0
6287 [| x0+.dx; y0+.dy;
6288 x1+.dx; y1+.dy;
6289 x3+.dx; y3+.dy;
6290 x2+.dx; y2+.dy |];
6291 GlArray.vertex `two state.vraw;
6292 GlArray.draw_arrays `triangle_strip 0 4;
6294 ) state.layout
6295 ) rects
6297 Gl.disable `blend;
6300 let display () =
6301 GlClear.color (scalecolor2 conf.bgcolor);
6302 GlClear.clear [`color];
6303 List.iter drawpage state.layout;
6304 let rects =
6305 match state.mode with
6306 | LinkNav (Ltexact (pageno, linkno)) ->
6307 begin match getopaque pageno with
6308 | Some opaque ->
6309 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6310 (pageno, 5, (
6311 float x0, float y0,
6312 float x1, float y0,
6313 float x1, float y1,
6314 float x0, float y1)
6315 ) :: state.rects
6316 | None -> state.rects
6318 | _ -> state.rects
6320 showrects rects;
6321 let rec postloop linkindexbase = function
6322 | l :: rest ->
6323 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6324 postloop linkindexbase rest
6325 | [] -> ()
6327 showsel ();
6328 postloop 0 state.layout;
6329 state.uioh#display;
6330 begin match state.mstate with
6331 | Mzoomrect ((x0, y0), (x1, y1)) ->
6332 Gl.enable `blend;
6333 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6334 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6335 filledrect (float x0) (float y0) (float x1) (float y1);
6336 Gl.disable `blend;
6337 | _ -> ()
6338 end;
6339 enttext ();
6340 scrollindicator ();
6341 Wsi.swapb ();
6344 let zoomrect x y x1 y1 =
6345 let x0 = min x x1
6346 and x1 = max x x1
6347 and y0 = min y y1 in
6348 gotoy (state.y + y0);
6349 state.anchor <- getanchor ();
6350 let zoom = (float state.w) /. float (x1 - x0) in
6351 let margin =
6352 match conf.fitmodel, conf.columns with
6353 | FitPage, Csplit _ ->
6354 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6356 | _, _ ->
6357 let adjw = wadjsb state.winw in
6358 if state.w < adjw
6359 then (adjw - state.w) / 2
6360 else 0
6362 state.x <- (state.x + margin) - x0;
6363 setzoom zoom;
6364 resetmstate ();
6367 let zoomblock x y =
6368 let g opaque l px py =
6369 match rectofblock opaque px py with
6370 | Some a ->
6371 let x0 = a.(0) -. 20. in
6372 let x1 = a.(1) +. 20. in
6373 let y0 = a.(2) -. 20. in
6374 let zoom = (float state.w) /. (x1 -. x0) in
6375 let pagey = getpagey l.pageno in
6376 gotoy_and_clear_text (pagey + truncate y0);
6377 state.anchor <- getanchor ();
6378 let margin = (state.w - l.pagew)/2 in
6379 state.x <- -truncate x0 - margin;
6380 setzoom zoom;
6381 None
6382 | None -> None
6384 match conf.columns with
6385 | Csplit _ ->
6386 showtext '!' "block zooming does not work properly in split columns mode"
6387 | _ -> onppundermouse g x y ()
6390 let scrollx x =
6391 let winw = wadjsb state.winw - 1 in
6392 let s = float x /. float winw in
6393 let destx = truncate (float (state.w + winw) *. s) in
6394 state.x <- winw - destx;
6395 gotoy_and_clear_text state.y;
6396 state.mstate <- Mscrollx;
6399 let scrolly y =
6400 let s = float y /. float state.winh in
6401 let desty = truncate (float (state.maxy - state.winh) *. s) in
6402 gotoy_and_clear_text desty;
6403 state.mstate <- Mscrolly;
6406 let viewmulticlick clicks x y mask =
6407 let g opaque l px py =
6408 let mark =
6409 match clicks with
6410 | 2 -> Mark_word
6411 | 3 -> Mark_line
6412 | 4 -> Mark_block
6413 | _ -> Mark_page
6415 if markunder opaque px py mark
6416 then (
6417 Some (fun () ->
6418 let dopipe cmd =
6419 match getopaque l.pageno with
6420 | None -> ()
6421 | Some opaque -> pipesel opaque cmd
6423 state.roam <- (fun () -> dopipe conf.paxcmd);
6424 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
6427 else None
6429 G.postRedisplay "viewmulticlick";
6430 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
6433 let canselect () =
6434 match conf.columns with
6435 | Csplit _ -> false
6436 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
6439 let viewmouse button down x y mask =
6440 match button with
6441 | n when (n == 4 || n == 5) && not down ->
6442 if Wsi.withctrl mask
6443 then (
6444 match state.mstate with
6445 | Mzoom (oldn, i) ->
6446 if oldn = n
6447 then (
6448 if i = 2
6449 then
6450 let incr =
6451 match n with
6452 | 5 ->
6453 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6454 | _ ->
6455 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6457 let zoom = conf.zoom -. incr in
6458 setzoom zoom;
6459 state.mstate <- Mzoom (n, 0);
6460 else
6461 state.mstate <- Mzoom (n, i+1);
6463 else state.mstate <- Mzoom (n, 0)
6465 | _ -> state.mstate <- Mzoom (n, 0)
6467 else (
6468 match state.autoscroll with
6469 | Some step -> setautoscrollspeed step (n=4)
6470 | None ->
6471 if conf.wheelbypage || conf.presentation
6472 then (
6473 if n = 4
6474 then prevpage ()
6475 else nextpage ()
6477 else
6478 let incr =
6479 if n = 4
6480 then -conf.scrollstep
6481 else conf.scrollstep
6483 let incr = incr * 2 in
6484 let y = clamp incr in
6485 gotoy_and_clear_text y
6488 | n when (n = 6 || n = 7) && not down && canpan () ->
6489 state.x <-
6490 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6491 gotoy_and_clear_text state.y
6493 | 1 when Wsi.withshift mask ->
6494 state.mstate <- Mnone;
6495 if not down
6496 then (
6497 match unproject x y with
6498 | Some (pageno, ux, uy) ->
6499 let cmd = Printf.sprintf
6500 "%s %s %d %d %d"
6501 conf.stcmd state.path pageno ux uy
6503 popen cmd []
6504 | None -> ()
6507 | 1 when Wsi.withctrl mask ->
6508 if down
6509 then (
6510 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6511 state.mstate <- Mpan (x, y)
6513 else
6514 state.mstate <- Mnone
6516 | 3 ->
6517 if down
6518 then (
6519 Wsi.setcursor Wsi.CURSOR_CYCLE;
6520 let p = (x, y) in
6521 state.mstate <- Mzoomrect (p, p)
6523 else (
6524 match state.mstate with
6525 | Mzoomrect ((x0, y0), _) ->
6526 if abs (x-x0) > 10 && abs (y - y0) > 10
6527 then zoomrect x0 y0 x y
6528 else (
6529 resetmstate ();
6530 G.postRedisplay "kill accidental zoom rect";
6532 | _ ->
6533 resetmstate ()
6536 | 1 when x > state.winw - vscrollw () ->
6537 if down
6538 then
6539 let _, position, sh = state.uioh#scrollph in
6540 if y > truncate position && y < truncate (position +. sh)
6541 then state.mstate <- Mscrolly
6542 else scrolly y
6543 else
6544 state.mstate <- Mnone
6546 | 1 when y > state.winh - hscrollh () ->
6547 if down
6548 then
6549 let _, position, sw = state.uioh#scrollpw in
6550 if x > truncate position && x < truncate (position +. sw)
6551 then state.mstate <- Mscrollx
6552 else scrollx x
6553 else
6554 state.mstate <- Mnone
6556 | 1 when state.bzoom -> if not down then zoomblock x y
6558 | 1 ->
6559 let dest = if down then getunder x y else Unone in
6560 begin match dest with
6561 | Ulinkgoto _
6562 | Ulinkuri _
6563 | Uremote _ | Uremotedest _
6564 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6565 gotounder dest
6567 | Unone when down ->
6568 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6569 state.mstate <- Mpan (x, y);
6571 | Unone | Utext _ ->
6572 if down
6573 then (
6574 if canselect ()
6575 then (
6576 state.mstate <- Msel ((x, y), (x, y));
6577 G.postRedisplay "mouse select";
6580 else (
6581 match state.mstate with
6582 | Mnone -> ()
6584 | Mzoom _ | Mscrollx | Mscrolly ->
6585 state.mstate <- Mnone
6587 | Mzoomrect ((x0, y0), _) ->
6588 zoomrect x0 y0 x y
6590 | Mpan _ ->
6591 Wsi.setcursor Wsi.CURSOR_INHERIT;
6592 state.mstate <- Mnone
6594 | Msel ((x0, y0), (x1, y1)) ->
6595 let rec loop = function
6596 | [] -> ()
6597 | l :: rest ->
6598 let inside =
6599 let a0 = l.pagedispy in
6600 let a1 = a0 + l.pagevh in
6601 let b0 = l.pagedispx in
6602 let b1 = b0 + l.pagevw in
6603 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6604 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6606 if inside
6607 then
6608 match getopaque l.pageno with
6609 | Some opaque ->
6610 let dosel cmd () =
6611 match Ne.res Unix.pipe with
6612 | Ne.Exn exn ->
6613 showtext '!'
6614 (Printf.sprintf
6615 "can not create sel pipe: %s"
6616 (exntos exn));
6617 | Ne.Res (r, w) ->
6618 let clo what fd =
6619 Ne.clo fd (fun msg ->
6620 dolog "%s close failed: %s" what msg)
6622 let popened =
6623 try popen cmd [r, 0; w, -1]; true
6624 with exn ->
6625 dolog "can not execute %S: %s"
6626 cmd (exntos exn);
6627 false
6629 if popened
6630 then (
6631 copysel w opaque;
6632 G.postRedisplay "copysel";
6634 else clo "Msel pipe/w" w;
6635 clo "Msel pipe/r" r;
6637 dosel conf.selcmd ();
6638 state.roam <- dosel conf.paxcmd;
6639 | None -> ()
6640 else loop rest
6642 loop state.layout;
6643 resetmstate ();
6647 | _ -> ()
6650 let birdseyemouse button down x y mask
6651 (conf, leftx, _, hooverpageno, anchor) =
6652 match button with
6653 | 1 when down ->
6654 let rec loop = function
6655 | [] -> ()
6656 | l :: rest ->
6657 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6658 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6659 then (
6660 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6662 else loop rest
6664 loop state.layout
6665 | 3 -> ()
6666 | _ -> viewmouse button down x y mask
6669 let uioh = object
6670 method display = ()
6672 method key key mask =
6673 begin match state.mode with
6674 | Textentry textentry -> textentrykeyboard key mask textentry
6675 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6676 | View -> viewkeyboard key mask
6677 | LinkNav linknav -> linknavkeyboard key mask linknav
6678 end;
6679 state.uioh
6681 method button button bstate x y mask =
6682 begin match state.mode with
6683 | LinkNav _
6684 | View -> viewmouse button bstate x y mask
6685 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6686 | Textentry _ -> ()
6687 end;
6688 state.uioh
6690 method multiclick clicks x y mask =
6691 begin match state.mode with
6692 | LinkNav _
6693 | View -> viewmulticlick clicks x y mask
6694 | Birdseye _
6695 | Textentry _ -> ()
6696 end;
6697 state.uioh
6699 method motion x y =
6700 begin match state.mode with
6701 | Textentry _ -> ()
6702 | View | Birdseye _ | LinkNav _ ->
6703 match state.mstate with
6704 | Mzoom _ | Mnone -> ()
6706 | Mpan (x0, y0) ->
6707 let dx = x - x0
6708 and dy = y0 - y in
6709 state.mstate <- Mpan (x, y);
6710 if canpan ()
6711 then state.x <- panbound (state.x + dx);
6712 let y = clamp dy in
6713 gotoy_and_clear_text y
6715 | Msel (a, _) ->
6716 state.mstate <- Msel (a, (x, y));
6717 G.postRedisplay "motion select";
6719 | Mscrolly ->
6720 let y = min state.winh (max 0 y) in
6721 scrolly y
6723 | Mscrollx ->
6724 let x = min state.winw (max 0 x) in
6725 scrollx x
6727 | Mzoomrect (p0, _) ->
6728 state.mstate <- Mzoomrect (p0, (x, y));
6729 G.postRedisplay "motion zoomrect";
6730 end;
6731 state.uioh
6733 method pmotion x y =
6734 begin match state.mode with
6735 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6736 let rec loop = function
6737 | [] ->
6738 if hooverpageno != -1
6739 then (
6740 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6741 G.postRedisplay "pmotion birdseye no hoover";
6743 | l :: rest ->
6744 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6745 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6746 then (
6747 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6748 G.postRedisplay "pmotion birdseye hoover";
6750 else loop rest
6752 loop state.layout
6754 | Textentry _ -> ()
6756 | LinkNav _
6757 | View ->
6758 match state.mstate with
6759 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6761 | Mnone ->
6762 updateunder x y;
6763 if canselect ()
6764 then
6765 match conf.pax with
6766 | None -> ()
6767 | Some r ->
6768 let past, _, _ = !r in
6769 let now = now () in
6770 let delta = now -. past in
6771 if delta > 0.01
6772 then paxunder x y
6773 else r := (now, x, y)
6774 end;
6775 state.uioh
6777 method infochanged _ = ()
6779 method scrollph =
6780 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6781 let p, h =
6782 if maxy = 0
6783 then 0.0, float state.winh
6784 else scrollph state.y maxy
6786 vscrollw (), p, h
6788 method scrollpw =
6789 let winw = wadjsb state.winw in
6790 let fwinw = float winw in
6791 let sw =
6792 let sw = fwinw /. float state.w in
6793 let sw = fwinw *. sw in
6794 max sw (float conf.scrollh)
6796 let position =
6797 let maxx = state.w + winw in
6798 let x = winw - state.x in
6799 let percent = float x /. float maxx in
6800 (fwinw -. sw) *. percent
6802 hscrollh (), position, sw
6804 method modehash =
6805 let modename =
6806 match state.mode with
6807 | LinkNav _ -> "links"
6808 | Textentry _ -> "textentry"
6809 | Birdseye _ -> "birdseye"
6810 | View -> "view"
6812 findkeyhash conf modename
6814 method eformsgs = true
6815 end;;
6817 module Config =
6818 struct
6819 open Parser
6821 let fontpath = ref "";;
6823 module KeyMap =
6824 Map.Make (struct type t = (int * int) let compare = compare end);;
6826 let unent s =
6827 let l = String.length s in
6828 let b = Buffer.create l in
6829 unent b s 0 l;
6830 Buffer.contents b;
6833 let home =
6834 try Sys.getenv "HOME"
6835 with exn ->
6836 prerr_endline
6837 ("Can not determine home directory location: " ^ exntos exn);
6841 let modifier_of_string = function
6842 | "alt" -> Wsi.altmask
6843 | "shift" -> Wsi.shiftmask
6844 | "ctrl" | "control" -> Wsi.ctrlmask
6845 | "meta" -> Wsi.metamask
6846 | _ -> 0
6849 let key_of_string =
6850 let r = Str.regexp "-" in
6851 fun s ->
6852 let elems = Str.full_split r s in
6853 let f n k m =
6854 let g s =
6855 let m1 = modifier_of_string s in
6856 if m1 = 0
6857 then (Wsi.namekey s, m)
6858 else (k, m lor m1)
6859 in function
6860 | Str.Delim s when n land 1 = 0 -> g s
6861 | Str.Text s -> g s
6862 | Str.Delim _ -> (k, m)
6864 let rec loop n k m = function
6865 | [] -> (k, m)
6866 | x :: xs ->
6867 let k, m = f n k m x in
6868 loop (n+1) k m xs
6870 loop 0 0 0 elems
6873 let keys_of_string =
6874 let r = Str.regexp "[ \t]" in
6875 fun s ->
6876 let elems = Str.split r s in
6877 List.map key_of_string elems
6880 let copykeyhashes c =
6881 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6884 let config_of c attrs =
6885 let apply c k v =
6887 match k with
6888 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6889 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6890 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6891 | "preload" -> { c with preload = bool_of_string v }
6892 | "page-bias" -> { c with pagebias = int_of_string v }
6893 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6894 | "horizontal-scroll-step" ->
6895 { c with hscrollstep = max (int_of_string v) 1 }
6896 | "auto-scroll-step" ->
6897 { c with autoscrollstep = max 0 (int_of_string v) }
6898 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6899 | "crop-hack" -> { c with crophack = bool_of_string v }
6900 | "throttle" ->
6901 let mw =
6902 match String.lowercase v with
6903 | "true" -> Some infinity
6904 | "false" -> None
6905 | f -> Some (float_of_string f)
6907 { c with maxwait = mw}
6908 | "highlight-links" -> { c with hlinks = bool_of_string v }
6909 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6910 | "vertical-margin" ->
6911 { c with interpagespace = max 0 (int_of_string v) }
6912 | "zoom" ->
6913 let zoom = float_of_string v /. 100. in
6914 let zoom = max zoom 0.0 in
6915 { c with zoom = zoom }
6916 | "presentation" -> { c with presentation = bool_of_string v }
6917 | "rotation-angle" -> { c with angle = int_of_string v }
6918 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6919 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6920 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6921 | "proportional-display" ->
6922 let fm =
6923 if bool_of_string v
6924 then FitProportional
6925 else FitWidth
6927 { c with fitmodel = fm }
6928 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6929 | "pixmap-cache-size" ->
6930 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6931 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6932 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6933 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6934 | "persistent-location" -> { c with jumpback = bool_of_string v }
6935 | "background-color" -> { c with bgcolor = color_of_string v }
6936 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6937 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6938 | "mupdf-store-size" ->
6939 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6940 | "checkers" -> { c with checkers = bool_of_string v }
6941 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6942 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6943 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6944 | "uri-launcher" -> { c with urilauncher = unent v }
6945 | "path-launcher" -> { c with pathlauncher = unent v }
6946 | "color-space" -> { c with colorspace = CSTE.of_string v }
6947 | "invert-colors" -> { c with invert = bool_of_string v }
6948 | "brightness" -> { c with colorscale = float_of_string v }
6949 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6950 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6951 | "columns" ->
6952 let (n, _, _) as nab = multicolumns_of_string v in
6953 if n < 0
6954 then { c with columns = Csplit (-n, [||]) }
6955 else { c with columns = Cmulti (nab, [||]) }
6956 | "birds-eye-columns" ->
6957 { c with beyecolumns = Some (max (int_of_string v) 2) }
6958 | "selection-command" -> { c with selcmd = unent v }
6959 | "synctex-command" -> { c with stcmd = unent v }
6960 | "pax-command" -> { c with paxcmd = unent v }
6961 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6962 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6963 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6964 | "use-pbo" -> { c with usepbo = bool_of_string v }
6965 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6966 | "horizontal-scrollbar-visible" ->
6967 let b =
6968 if bool_of_string v
6969 then c.scrollb lor scrollbhv
6970 else c.scrollb land (lnot scrollbhv)
6972 { c with scrollb = b }
6973 | "vertical-scrollbar-visible" ->
6974 let b =
6975 if bool_of_string v
6976 then c.scrollb lor scrollbvv
6977 else c.scrollb land (lnot scrollbvv)
6979 { c with scrollb = b }
6980 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6981 | "point-and-x" ->
6982 { c with pax =
6983 if bool_of_string v
6984 then Some (ref (0.0, 0, 0))
6985 else None }
6986 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6987 | _ -> c
6988 with exn ->
6989 prerr_endline ("Error processing attribute (`" ^
6990 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6993 let rec fold c = function
6994 | [] -> c
6995 | (k, v) :: rest ->
6996 let c = apply c k v in
6997 fold c rest
6999 fold { c with keyhashes = copykeyhashes c } attrs;
7002 let fromstring f pos n v d =
7003 try f v
7004 with exn ->
7005 dolog "Error processing attribute (%S=%S) at %d\n%s"
7006 n v pos (exntos exn)
7011 let bookmark_of attrs =
7012 let rec fold title page rely visy = function
7013 | ("title", v) :: rest -> fold v page rely visy rest
7014 | ("page", v) :: rest -> fold title v rely visy rest
7015 | ("rely", v) :: rest -> fold title page v visy rest
7016 | ("visy", v) :: rest -> fold title page rely v rest
7017 | _ :: rest -> fold title page rely visy rest
7018 | [] -> title, page, rely, visy
7020 fold "invalid" "0" "0" "0" attrs
7023 let doc_of attrs =
7024 let rec fold path page rely pan visy = function
7025 | ("path", v) :: rest -> fold v page rely pan visy rest
7026 | ("page", v) :: rest -> fold path v rely pan visy rest
7027 | ("rely", v) :: rest -> fold path page v pan visy rest
7028 | ("pan", v) :: rest -> fold path page rely v visy rest
7029 | ("visy", v) :: rest -> fold path page rely pan v rest
7030 | _ :: rest -> fold path page rely pan visy rest
7031 | [] -> path, page, rely, pan, visy
7033 fold "" "0" "0" "0" "0" attrs
7036 let map_of attrs =
7037 let rec fold rs ls = function
7038 | ("out", v) :: rest -> fold v ls rest
7039 | ("in", v) :: rest -> fold rs v rest
7040 | _ :: rest -> fold ls rs rest
7041 | [] -> ls, rs
7043 fold "" "" attrs
7046 let setconf dst src =
7047 dst.scrollbw <- src.scrollbw;
7048 dst.scrollh <- src.scrollh;
7049 dst.icase <- src.icase;
7050 dst.preload <- src.preload;
7051 dst.pagebias <- src.pagebias;
7052 dst.verbose <- src.verbose;
7053 dst.scrollstep <- src.scrollstep;
7054 dst.maxhfit <- src.maxhfit;
7055 dst.crophack <- src.crophack;
7056 dst.autoscrollstep <- src.autoscrollstep;
7057 dst.maxwait <- src.maxwait;
7058 dst.hlinks <- src.hlinks;
7059 dst.underinfo <- src.underinfo;
7060 dst.interpagespace <- src.interpagespace;
7061 dst.zoom <- src.zoom;
7062 dst.presentation <- src.presentation;
7063 dst.angle <- src.angle;
7064 dst.cwinw <- src.cwinw;
7065 dst.cwinh <- src.cwinh;
7066 dst.savebmarks <- src.savebmarks;
7067 dst.memlimit <- src.memlimit;
7068 dst.fitmodel <- src.fitmodel;
7069 dst.texcount <- src.texcount;
7070 dst.sliceheight <- src.sliceheight;
7071 dst.thumbw <- src.thumbw;
7072 dst.jumpback <- src.jumpback;
7073 dst.bgcolor <- src.bgcolor;
7074 dst.tilew <- src.tilew;
7075 dst.tileh <- src.tileh;
7076 dst.mustoresize <- src.mustoresize;
7077 dst.checkers <- src.checkers;
7078 dst.aalevel <- src.aalevel;
7079 dst.trimmargins <- src.trimmargins;
7080 dst.trimfuzz <- src.trimfuzz;
7081 dst.urilauncher <- src.urilauncher;
7082 dst.colorspace <- src.colorspace;
7083 dst.invert <- src.invert;
7084 dst.colorscale <- src.colorscale;
7085 dst.redirectstderr <- src.redirectstderr;
7086 dst.ghyllscroll <- src.ghyllscroll;
7087 dst.columns <- src.columns;
7088 dst.beyecolumns <- src.beyecolumns;
7089 dst.selcmd <- src.selcmd;
7090 dst.updatecurs <- src.updatecurs;
7091 dst.pathlauncher <- src.pathlauncher;
7092 dst.keyhashes <- copykeyhashes src;
7093 dst.hfsize <- src.hfsize;
7094 dst.hscrollstep <- src.hscrollstep;
7095 dst.pgscale <- src.pgscale;
7096 dst.usepbo <- src.usepbo;
7097 dst.wheelbypage <- src.wheelbypage;
7098 dst.stcmd <- src.stcmd;
7099 dst.paxcmd <- src.paxcmd;
7100 dst.scrollb <- src.scrollb;
7101 dst.riani <- src.riani;
7102 dst.paxmark <- src.paxmark;
7103 dst.pax <-
7104 if src.pax = None
7105 then None
7106 else Some ((ref (0.0, 0, 0)));
7109 let get s =
7110 let h = Hashtbl.create 10 in
7111 let dc = { defconf with angle = defconf.angle } in
7112 let rec toplevel v t spos _ =
7113 match t with
7114 | Vdata | Vcdata | Vend -> v
7115 | Vopen ("llppconfig", _, closed) ->
7116 if closed
7117 then v
7118 else { v with f = llppconfig }
7119 | Vopen _ ->
7120 error "unexpected subelement at top level" s spos
7121 | Vclose _ -> error "unexpected close at top level" s spos
7123 and llppconfig v t spos _ =
7124 match t with
7125 | Vdata | Vcdata -> v
7126 | Vend -> error "unexpected end of input in llppconfig" s spos
7127 | Vopen ("defaults", attrs, closed) ->
7128 let c = config_of dc attrs in
7129 setconf dc c;
7130 if closed
7131 then v
7132 else { v with f = defaults }
7134 | Vopen ("ui-font", attrs, closed) ->
7135 let rec getsize size = function
7136 | [] -> size
7137 | ("size", v) :: rest ->
7138 let size =
7139 fromstring int_of_string spos "size" v fstate.fontsize in
7140 getsize size rest
7141 | l -> getsize size l
7143 fstate.fontsize <- getsize fstate.fontsize attrs;
7144 if closed
7145 then v
7146 else { v with f = uifont (Buffer.create 10) }
7148 | Vopen ("doc", attrs, closed) ->
7149 let pathent, spage, srely, span, svisy = doc_of attrs in
7150 let path = unent pathent
7151 and pageno = fromstring int_of_string spos "page" spage 0
7152 and rely = fromstring float_of_string spos "rely" srely 0.0
7153 and pan = fromstring int_of_string spos "pan" span 0
7154 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7155 let c = config_of dc attrs in
7156 let anchor = (pageno, rely, visy) in
7157 if closed
7158 then (Hashtbl.add h path (c, [], pan, anchor); v)
7159 else { v with f = doc path pan anchor c [] }
7161 | Vopen _ ->
7162 error "unexpected subelement in llppconfig" s spos
7164 | Vclose "llppconfig" -> { v with f = toplevel }
7165 | Vclose _ -> error "unexpected close in llppconfig" s spos
7167 and defaults v t spos _ =
7168 match t with
7169 | Vdata | Vcdata -> v
7170 | Vend -> error "unexpected end of input in defaults" s spos
7171 | Vopen ("keymap", attrs, closed) ->
7172 let modename =
7173 try List.assoc "mode" attrs
7174 with Not_found -> "global" in
7175 if closed
7176 then v
7177 else
7178 let ret keymap =
7179 let h = findkeyhash dc modename in
7180 KeyMap.iter (Hashtbl.replace h) keymap;
7181 defaults
7183 { v with f = pkeymap ret KeyMap.empty }
7185 | Vopen (_, _, _) ->
7186 error "unexpected subelement in defaults" s spos
7188 | Vclose "defaults" ->
7189 { v with f = llppconfig }
7191 | Vclose _ -> error "unexpected close in defaults" s spos
7193 and uifont b v t spos epos =
7194 match t with
7195 | Vdata | Vcdata ->
7196 Buffer.add_substring b s spos (epos - spos);
7198 | Vopen (_, _, _) ->
7199 error "unexpected subelement in ui-font" s spos
7200 | Vclose "ui-font" ->
7201 if emptystr !fontpath
7202 then fontpath := Buffer.contents b;
7203 { v with f = llppconfig }
7204 | Vclose _ -> error "unexpected close in ui-font" s spos
7205 | Vend -> error "unexpected end of input in ui-font" s spos
7207 and doc path pan anchor c bookmarks v t spos _ =
7208 match t with
7209 | Vdata | Vcdata -> v
7210 | Vend -> error "unexpected end of input in doc" s spos
7211 | Vopen ("bookmarks", _, closed) ->
7212 if closed
7213 then v
7214 else { v with f = pbookmarks path pan anchor c bookmarks }
7216 | Vopen ("keymap", attrs, closed) ->
7217 let modename =
7218 try List.assoc "mode" attrs
7219 with Not_found -> "global"
7221 if closed
7222 then v
7223 else
7224 let ret keymap =
7225 let h = findkeyhash c modename in
7226 KeyMap.iter (Hashtbl.replace h) keymap;
7227 doc path pan anchor c bookmarks
7229 { v with f = pkeymap ret KeyMap.empty }
7231 | Vopen (_, _, _) ->
7232 error "unexpected subelement in doc" s spos
7234 | Vclose "doc" ->
7235 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7236 { v with f = llppconfig }
7238 | Vclose _ -> error "unexpected close in doc" s spos
7240 and pkeymap ret keymap v t spos _ =
7241 match t with
7242 | Vdata | Vcdata -> v
7243 | Vend -> error "unexpected end of input in keymap" s spos
7244 | Vopen ("map", attrs, closed) ->
7245 let r, l = map_of attrs in
7246 let kss = fromstring keys_of_string spos "in" r [] in
7247 let lss = fromstring keys_of_string spos "out" l [] in
7248 let keymap =
7249 match kss with
7250 | [] -> keymap
7251 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7252 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7254 if closed
7255 then { v with f = pkeymap ret keymap }
7256 else
7257 let f () = v in
7258 { v with f = skip "map" f }
7260 | Vopen _ ->
7261 error "unexpected subelement in keymap" s spos
7263 | Vclose "keymap" ->
7264 { v with f = ret keymap }
7266 | Vclose _ -> error "unexpected close in keymap" s spos
7268 and pbookmarks path pan anchor c bookmarks v t spos _ =
7269 match t with
7270 | Vdata | Vcdata -> v
7271 | Vend -> error "unexpected end of input in bookmarks" s spos
7272 | Vopen ("item", attrs, closed) ->
7273 let titleent, spage, srely, svisy = bookmark_of attrs in
7274 let page = fromstring int_of_string spos "page" spage 0
7275 and rely = fromstring float_of_string spos "rely" srely 0.0
7276 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7277 let bookmarks =
7278 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
7280 if closed
7281 then { v with f = pbookmarks path pan anchor c bookmarks }
7282 else
7283 let f () = v in
7284 { v with f = skip "item" f }
7286 | Vopen _ ->
7287 error "unexpected subelement in bookmarks" s spos
7289 | Vclose "bookmarks" ->
7290 { v with f = doc path pan anchor c bookmarks }
7292 | Vclose _ -> error "unexpected close in bookmarks" s spos
7294 and skip tag f v t spos _ =
7295 match t with
7296 | Vdata | Vcdata -> v
7297 | Vend ->
7298 error ("unexpected end of input in skipped " ^ tag) s spos
7299 | Vopen (tag', _, closed) ->
7300 if closed
7301 then v
7302 else
7303 let f' () = { v with f = skip tag f } in
7304 { v with f = skip tag' f' }
7305 | Vclose ctag ->
7306 if tag = ctag
7307 then f ()
7308 else error ("unexpected close in skipped " ^ tag) s spos
7311 parse { f = toplevel; accu = () } s;
7312 h, dc;
7315 let do_load f ic =
7317 let len = in_channel_length ic in
7318 let s = String.create len in
7319 really_input ic s 0 len;
7320 f s;
7321 with
7322 | Parse_error (msg, s, pos) ->
7323 let subs = subs s pos in
7324 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7326 | exn ->
7327 failwith ("config load error: " ^ exntos exn)
7330 let defconfpath =
7331 let dir =
7333 let dir = Filename.concat home ".config" in
7334 if Sys.is_directory dir then dir else home
7335 with _ -> home
7337 Filename.concat dir "llpp.conf"
7340 let confpath = ref defconfpath;;
7342 let load1 f =
7343 if Sys.file_exists !confpath
7344 then
7345 match
7346 (try Some (open_in_bin !confpath)
7347 with exn ->
7348 prerr_endline
7349 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7350 exntos exn);
7351 None
7353 with
7354 | Some ic ->
7355 let success =
7357 f (do_load get ic)
7358 with exn ->
7359 prerr_endline
7360 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7361 exntos exn);
7362 false
7364 close_in ic;
7365 success
7367 | None -> false
7368 else
7369 f (Hashtbl.create 0, defconf)
7372 let load () =
7373 let f (h, dc) =
7374 let pc, pb, px, pa =
7376 let key =
7377 if emptystr state.origin
7378 then state.path
7379 else state.origin
7381 Hashtbl.find h (Filename.basename key)
7382 with Not_found -> dc, [], 0, emptyanchor
7384 setconf defconf dc;
7385 setconf conf pc;
7386 state.bookmarks <- pb;
7387 state.x <- px;
7388 if conf.jumpback
7389 then state.anchor <- pa;
7390 cbput state.hists.nav pa;
7391 true
7393 load1 f
7396 let add_attrs bb always dc c =
7397 let ob s a b =
7398 if always || a != b
7399 then Printf.bprintf bb "\n %s='%b'" s a
7400 and op s a b =
7401 if always || a <> b
7402 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7403 and oi s a b =
7404 if always || a != b
7405 then Printf.bprintf bb "\n %s='%d'" s a
7406 and oI s a b =
7407 if always || a != b
7408 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7409 and oz s a b =
7410 if always || a <> b
7411 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7412 and oF s a b =
7413 if always || a <> b
7414 then Printf.bprintf bb "\n %s='%f'" s a
7415 and oc s a b =
7416 if always || a <> b
7417 then
7418 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7419 and oC s a b =
7420 if always || a <> b
7421 then
7422 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7423 and oR s a b =
7424 if always || a <> b
7425 then
7426 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7427 and os s a b =
7428 if always || a <> b
7429 then
7430 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7431 and og s a b =
7432 if always || a <> b
7433 then
7434 match a with
7435 | Some (_N, _A, _B) ->
7436 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7437 | None ->
7438 match b with
7439 | None -> ()
7440 | _ ->
7441 Printf.bprintf bb "\n %s='none'" s
7442 and oW s a b =
7443 if always || a <> b
7444 then
7445 let v =
7446 match a with
7447 | None -> "false"
7448 | Some f ->
7449 if f = infinity
7450 then "true"
7451 else string_of_float f
7453 Printf.bprintf bb "\n %s='%s'" s v
7454 and oco s a b =
7455 if always || a <> b
7456 then
7457 match a with
7458 | Cmulti ((n, a, b), _) when n > 1 ->
7459 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7460 | Csplit (n, _) when n > 1 ->
7461 Printf.bprintf bb "\n %s='%d'" s ~-n
7462 | _ -> ()
7463 and obeco s a b =
7464 if always || a <> b
7465 then
7466 match a with
7467 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7468 | _ -> ()
7469 and oFm s a b =
7470 if always || a <> b
7471 then
7472 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7473 and oSv s a b m =
7474 if always || a <> b
7475 then
7476 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7477 and oPm s a b =
7478 if always || a <> b
7479 then
7480 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7482 oi "width" c.cwinw dc.cwinw;
7483 oi "height" c.cwinh dc.cwinh;
7484 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7485 oi "scroll-handle-height" c.scrollh dc.scrollh;
7486 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7487 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7488 ob "case-insensitive-search" c.icase dc.icase;
7489 ob "preload" c.preload dc.preload;
7490 oi "page-bias" c.pagebias dc.pagebias;
7491 oi "scroll-step" c.scrollstep dc.scrollstep;
7492 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7493 ob "max-height-fit" c.maxhfit dc.maxhfit;
7494 ob "crop-hack" c.crophack dc.crophack;
7495 oW "throttle" c.maxwait dc.maxwait;
7496 ob "highlight-links" c.hlinks dc.hlinks;
7497 ob "under-cursor-info" c.underinfo dc.underinfo;
7498 oi "vertical-margin" c.interpagespace dc.interpagespace;
7499 oz "zoom" c.zoom dc.zoom;
7500 ob "presentation" c.presentation dc.presentation;
7501 oi "rotation-angle" c.angle dc.angle;
7502 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7503 oFm "fit-model" c.fitmodel dc.fitmodel;
7504 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7505 oi "tex-count" c.texcount dc.texcount;
7506 oi "slice-height" c.sliceheight dc.sliceheight;
7507 oi "thumbnail-width" c.thumbw dc.thumbw;
7508 ob "persistent-location" c.jumpback dc.jumpback;
7509 oc "background-color" c.bgcolor dc.bgcolor;
7510 oi "tile-width" c.tilew dc.tilew;
7511 oi "tile-height" c.tileh dc.tileh;
7512 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7513 ob "checkers" c.checkers dc.checkers;
7514 oi "aalevel" c.aalevel dc.aalevel;
7515 ob "trim-margins" c.trimmargins dc.trimmargins;
7516 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7517 os "uri-launcher" c.urilauncher dc.urilauncher;
7518 os "path-launcher" c.pathlauncher dc.pathlauncher;
7519 oC "color-space" c.colorspace dc.colorspace;
7520 ob "invert-colors" c.invert dc.invert;
7521 oF "brightness" c.colorscale dc.colorscale;
7522 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7523 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7524 oco "columns" c.columns dc.columns;
7525 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7526 os "selection-command" c.selcmd dc.selcmd;
7527 os "synctex-command" c.stcmd dc.stcmd;
7528 os "pax-command" c.paxcmd dc.paxcmd;
7529 ob "update-cursor" c.updatecurs dc.updatecurs;
7530 oi "hint-font-size" c.hfsize dc.hfsize;
7531 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7532 oF "page-scroll-scale" c.pgscale dc.pgscale;
7533 ob "use-pbo" c.usepbo dc.usepbo;
7534 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7535 ob "remote-in-a-new-instance" c.riani dc.riani;
7536 op "point-and-x" c.pax dc.pax;
7537 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7540 let keymapsbuf always dc c =
7541 let bb = Buffer.create 16 in
7542 let rec loop = function
7543 | [] -> ()
7544 | (modename, h) :: rest ->
7545 let dh = findkeyhash dc modename in
7546 if always || h <> dh
7547 then (
7548 if Hashtbl.length h > 0
7549 then (
7550 if Buffer.length bb > 0
7551 then Buffer.add_char bb '\n';
7552 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7553 Hashtbl.iter (fun i o ->
7554 let isdifferent = always ||
7556 let dO = Hashtbl.find dh i in
7557 dO <> o
7558 with Not_found -> true
7560 if isdifferent
7561 then
7562 let addkm (k, m) =
7563 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7564 if Wsi.withalt m then Buffer.add_string bb "alt-";
7565 if Wsi.withshift m then Buffer.add_string bb "shift-";
7566 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7567 Buffer.add_string bb (Wsi.keyname k);
7569 let addkms l =
7570 let rec loop = function
7571 | [] -> ()
7572 | km :: [] -> addkm km
7573 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7575 loop l
7577 Buffer.add_string bb "<map in='";
7578 addkm i;
7579 match o with
7580 | KMinsrt km ->
7581 Buffer.add_string bb "' out='";
7582 addkm km;
7583 Buffer.add_string bb "'/>\n"
7585 | KMinsrl kms ->
7586 Buffer.add_string bb "' out='";
7587 addkms kms;
7588 Buffer.add_string bb "'/>\n"
7590 | KMmulti (ins, kms) ->
7591 Buffer.add_char bb ' ';
7592 addkms ins;
7593 Buffer.add_string bb "' out='";
7594 addkms kms;
7595 Buffer.add_string bb "'/>\n"
7596 ) h;
7597 Buffer.add_string bb "</keymap>";
7600 loop rest
7602 loop c.keyhashes;
7606 let save () =
7607 let uifontsize = fstate.fontsize in
7608 let bb = Buffer.create 32768 in
7609 let relx = float state.x /. float state.winw in
7610 let w, h, x =
7611 let cx w = truncate (relx *. float w) in
7612 List.fold_left
7613 (fun (w, h, x) ws ->
7614 match ws with
7615 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7616 | Wsi.MaxVert -> (w, conf.cwinh, x)
7617 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7619 (state.winw, state.winh, state.x) state.winstate
7621 conf.cwinw <- w;
7622 conf.cwinh <- h;
7623 let f (h, dc) =
7624 let dc = if conf.bedefault then conf else dc in
7625 Buffer.add_string bb "<llppconfig>\n";
7627 if nonemptystr !fontpath
7628 then
7629 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7630 uifontsize
7631 !fontpath
7632 else (
7633 if uifontsize <> 14
7634 then
7635 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7638 Buffer.add_string bb "<defaults";
7639 add_attrs bb true dc dc;
7640 let kb = keymapsbuf true dc dc in
7641 if Buffer.length kb > 0
7642 then (
7643 Buffer.add_string bb ">\n";
7644 Buffer.add_buffer bb kb;
7645 Buffer.add_string bb "\n</defaults>\n";
7647 else Buffer.add_string bb "/>\n";
7649 let adddoc path pan anchor c bookmarks =
7650 if bookmarks == [] && c = dc && anchor = emptyanchor
7651 then ()
7652 else (
7653 Printf.bprintf bb "<doc path='%s'"
7654 (enent path 0 (String.length path));
7656 if anchor <> emptyanchor
7657 then (
7658 let n, rely, visy = anchor in
7659 Printf.bprintf bb " page='%d'" n;
7660 if rely > 1e-6
7661 then
7662 Printf.bprintf bb " rely='%f'" rely
7664 if abs_float visy > 1e-6
7665 then
7666 Printf.bprintf bb " visy='%f'" visy
7670 if pan != 0
7671 then Printf.bprintf bb " pan='%d'" pan;
7673 add_attrs bb false dc c;
7674 let kb = keymapsbuf false dc c in
7676 begin match bookmarks with
7677 | [] ->
7678 if Buffer.length kb > 0
7679 then (
7680 Buffer.add_string bb ">\n";
7681 Buffer.add_buffer bb kb;
7682 Buffer.add_string bb "\n</doc>\n";
7684 else Buffer.add_string bb "/>\n"
7685 | _ ->
7686 Buffer.add_string bb ">\n<bookmarks>\n";
7687 List.iter (fun (title, _, kind) ->
7688 begin match kind with
7689 | Oanchor (page, rely, visy) ->
7690 Printf.bprintf bb
7691 "<item title='%s' page='%d'"
7692 (enent title 0 (String.length title))
7693 page
7695 if rely > 1e-6
7696 then
7697 Printf.bprintf bb " rely='%f'" rely
7699 if abs_float visy > 1e-6
7700 then
7701 Printf.bprintf bb " visy='%f'" visy
7703 | Onone | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
7704 failwith "unexpected link in bookmarks"
7705 end;
7706 Buffer.add_string bb "/>\n";
7707 ) bookmarks;
7708 Buffer.add_string bb "</bookmarks>";
7709 if Buffer.length kb > 0
7710 then (
7711 Buffer.add_string bb "\n";
7712 Buffer.add_buffer bb kb;
7714 Buffer.add_string bb "\n</doc>\n";
7715 end;
7719 let pan, conf =
7720 match state.mode with
7721 | Birdseye (c, pan, _, _, _) ->
7722 let beyecolumns =
7723 match conf.columns with
7724 | Cmulti ((c, _, _), _) -> Some c
7725 | Csingle _ -> None
7726 | Csplit _ -> None
7727 and columns =
7728 match c.columns with
7729 | Cmulti (c, _) -> Cmulti (c, [||])
7730 | Csingle _ -> Csingle [||]
7731 | Csplit _ -> failwith "quit from bird's eye while split"
7733 pan, { c with beyecolumns = beyecolumns; columns = columns }
7734 | _ -> x, conf
7736 let basename = Filename.basename
7737 (if emptystr state.origin then state.path else state.origin)
7739 adddoc basename pan (getanchor ())
7740 (let autoscrollstep =
7741 match state.autoscroll with
7742 | Some step -> step
7743 | None -> conf.autoscrollstep
7744 in begin match state.mode with
7745 | Birdseye beye -> leavebirdseye beye true;
7746 | _ -> ()
7747 end;
7748 { conf with autoscrollstep = autoscrollstep })
7749 (if conf.savebmarks then state.bookmarks else []);
7751 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7752 if basename <> path
7753 then adddoc path x anchor c bookmarks
7754 ) h;
7755 Buffer.add_string bb "</llppconfig>\n";
7756 true;
7758 if load1 f && Buffer.length bb > 0
7759 then
7761 let tmp = !confpath ^ ".tmp" in
7762 let oc = open_out_bin tmp in
7763 Buffer.output_buffer oc bb;
7764 close_out oc;
7765 Unix.rename tmp !confpath;
7766 with exn ->
7767 prerr_endline
7768 ("error while saving configuration: " ^ exntos exn)
7770 end;;
7772 let adderrmsg src msg =
7773 Buffer.add_string state.errmsgs msg;
7774 state.newerrmsgs <- true;
7775 G.postRedisplay src
7778 let adderrfmt src fmt =
7779 Format.kprintf (fun s -> adderrmsg src s) fmt;
7782 let ract cmds =
7783 let cl = splitatspace cmds in
7784 let scan s fmt f =
7785 try Scanf.sscanf s fmt f
7786 with exn ->
7787 adderrfmt "remote exec"
7788 "error processing '%S': %s\n" cmds (exntos exn)
7790 match cl with
7791 | "reload" :: [] -> reload ()
7792 | "goto" :: args :: [] ->
7793 scan args "%u %f %f"
7794 (fun pageno x y ->
7795 let cmd, _ = state.geomcmds in
7796 if emptystr cmd
7797 then gotopagexy pageno x y
7798 else
7799 let f prevf () =
7800 gotopagexy pageno x y;
7801 prevf ()
7803 state.reprf <- f state.reprf
7805 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7806 | "gotor" :: args :: [] ->
7807 scan args "%S %u"
7808 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
7809 | "gotord" :: args :: [] ->
7810 scan args "%S %S"
7811 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
7812 | "rect" :: args :: [] ->
7813 scan args "%u %u %f %f %f %f"
7814 (fun pageno color x0 y0 x1 y1 ->
7815 onpagerect pageno (fun w h ->
7816 let _,w1,h1,_ = getpagedim pageno in
7817 let sw = float w1 /. float w
7818 and sh = float h1 /. float h in
7819 let x0s = x0 *. sw
7820 and x1s = x1 *. sw
7821 and y0s = y0 *. sh
7822 and y1s = y1 *. sh in
7823 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7824 debugrect rect;
7825 state.rects <- (pageno, color, rect) :: state.rects;
7826 G.postRedisplay "rect";
7829 | "activatewin" :: [] -> Wsi.activatewin ()
7830 | "quit" :: [] -> raise Quit
7831 | _ ->
7832 adderrfmt "remote command"
7833 "error processing remote command: %S\n" cmds;
7836 let remote =
7837 let scratch = String.create 80 in
7838 let buf = Buffer.create 80 in
7839 fun fd ->
7840 let rec tempfr () =
7841 try Some (Unix.read fd scratch 0 80)
7842 with
7843 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7844 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7845 | exn -> raise exn
7847 match tempfr () with
7848 | None -> Some fd
7849 | Some n ->
7850 if n = 0
7851 then (
7852 Unix.close fd;
7853 if Buffer.length buf > 0
7854 then (
7855 let s = Buffer.contents buf in
7856 Buffer.clear buf;
7857 ract s;
7859 None
7861 else
7862 let rec eat ppos =
7863 let nlpos =
7865 let pos = String.index_from scratch ppos '\n' in
7866 if pos >= n then -1 else pos
7867 with Not_found -> -1
7869 if nlpos >= 0
7870 then (
7871 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7872 let s = Buffer.contents buf in
7873 Buffer.clear buf;
7874 ract s;
7875 eat (nlpos+1);
7877 else (
7878 Buffer.add_substring buf scratch ppos (n-ppos);
7879 Some fd
7881 in eat 0
7884 let remoteopen path =
7885 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7886 with exn ->
7887 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7888 None
7891 let () =
7892 let trimcachepath = ref "" in
7893 let rcmdpath = ref "" in
7894 let pageno = ref None in
7895 selfexec := Sys.executable_name;
7896 Arg.parse
7897 (Arg.align
7898 [("-p", Arg.String (fun s -> state.password <- s),
7899 "<password> Set password");
7901 ("-f", Arg.String
7902 (fun s ->
7903 Config.fontpath := s;
7904 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7906 "<path> Set path to the user interface font");
7908 ("-c", Arg.String
7909 (fun s ->
7910 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7911 Config.confpath := s),
7912 "<path> Set path to the configuration file");
7914 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
7915 "<page-number> Jump to page");
7917 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7918 "<path> Set path to the trim cache file");
7920 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7921 "<named-destination> Set named destination");
7923 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7924 ("-cxack", Arg.Set cxack, " Cut corners");
7926 ("-remote", Arg.String (fun s -> rcmdpath := s),
7927 "<path> Set path to the remote commands source");
7929 ("-origin", Arg.String (fun s -> state.origin <- s),
7930 "<original-path> Set original path");
7932 ("-v", Arg.Unit (fun () ->
7933 Printf.printf
7934 "%s\nconfiguration path: %s\n"
7935 (version ())
7936 Config.defconfpath
7938 exit 0), " Print version and exit");
7941 (fun s -> state.path <- s)
7942 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7944 if !wtmode
7945 then selfexec := !selfexec ^ " -wtmode";
7947 if emptystr state.path
7948 then (prerr_endline "file name missing"; exit 1);
7950 if not (Config.load ())
7951 then prerr_endline "failed to load configuration";
7952 begin match !pageno with
7953 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
7954 | None -> ()
7955 end;
7957 let wsfd, winw, winh = Wsi.init (object (self)
7958 val mutable m_hack = false
7959 val mutable m_clicks = 0
7960 val mutable m_click_x = 0
7961 val mutable m_click_y = 0
7962 val mutable m_lastclicktime = infinity
7964 method private cleanup =
7965 state.roam <- noroam;
7966 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
7967 method expose = if not m_hack then G.postRedisplay "expose"
7968 method visible = G.postRedisplay "visible"
7969 method display = m_hack <- false; display ()
7970 method reshape w h =
7971 self#cleanup;
7972 m_hack <- w < state.winw && h < state.winh;
7973 reshape w h
7974 method mouse b d x y m =
7975 if d && canselect ()
7976 then (
7977 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
7978 m_click_x <- x;
7979 m_click_y <- y;
7980 if b = 1
7981 then (
7982 let t = now () in
7983 if abs x - m_click_x > 10
7984 || abs y - m_click_y > 10
7985 || abs_float (t -. m_lastclicktime) > 0.3
7986 then m_clicks <- 0;
7987 m_clicks <- m_clicks + 1;
7988 m_lastclicktime <- t;
7989 if m_clicks = 1
7990 then (
7991 self#cleanup;
7992 G.postRedisplay "cleanup";
7993 state.uioh <- state.uioh#button b d x y m;
7995 else state.uioh <- state.uioh#multiclick m_clicks x y m
7997 else (
7998 self#cleanup;
7999 m_clicks <- 0;
8000 m_lastclicktime <- infinity;
8001 state.uioh <- state.uioh#button b d x y m
8004 else (
8005 state.uioh <- state.uioh#button b d x y m
8007 method motion x y =
8008 state.mpos <- (x, y);
8009 state.uioh <- state.uioh#motion x y
8010 method pmotion x y =
8011 state.mpos <- (x, y);
8012 state.uioh <- state.uioh#pmotion x y
8013 method key k m =
8014 let mascm = m land (
8015 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
8016 ) in
8017 let keyboard k m =
8018 let x = state.x and y = state.y in
8019 keyboard k m;
8020 if x != state.x || y != state.y then self#cleanup
8022 match state.keystate with
8023 | KSnone ->
8024 let km = k, mascm in
8025 begin
8026 match
8027 let modehash = state.uioh#modehash in
8028 try Hashtbl.find modehash km
8029 with Not_found ->
8030 try Hashtbl.find (findkeyhash conf "global") km
8031 with Not_found -> KMinsrt (k, m)
8032 with
8033 | KMinsrt (k, m) -> keyboard k m
8034 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
8035 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
8037 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
8038 List.iter (fun (k, m) -> keyboard k m) insrt;
8039 state.keystate <- KSnone
8040 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
8041 state.keystate <- KSinto (keys, insrt)
8042 | _ ->
8043 state.keystate <- KSnone
8045 method enter x y =
8046 state.mpos <- (x, y);
8047 state.uioh <- state.uioh#pmotion x y
8048 method leave = state.mpos <- (-1, -1)
8049 method winstate wsl = state.winstate <- wsl; m_hack <- false
8050 method quit = raise Quit
8051 end) conf.cwinw conf.cwinh (platform = Posx) in
8053 state.wsfd <- wsfd;
8055 if not (
8056 List.exists GlMisc.check_extension
8057 [ "GL_ARB_texture_rectangle"
8058 ; "GL_EXT_texture_recangle"
8059 ; "GL_NV_texture_rectangle" ]
8061 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
8063 if (
8064 let r = GlMisc.get_string `renderer in
8065 let p = "Mesa DRI Intel(" in
8066 let l = String.length p in
8067 String.length r > l && String.sub r 0 l = p
8069 then (
8070 defconf.sliceheight <- 1024;
8071 defconf.texcount <- 32;
8072 defconf.usepbo <- true;
8075 let cr, sw =
8076 match Ne.res Unix.pipe with
8077 | Ne.Exn exn ->
8078 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
8079 exit 1
8080 | Ne.Res rw -> rw
8081 and sr, cw =
8082 match Ne.res Unix.pipe with
8083 | Ne.Exn exn ->
8084 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
8085 exit 1
8086 | Ne.Res rw -> rw
8089 cloexec cr;
8090 cloexec sw;
8091 cloexec sr;
8092 cloexec cw;
8094 setcheckers conf.checkers;
8095 redirectstderr ();
8096 if conf.redirectstderr
8097 then
8098 at_exit (fun () ->
8099 let s = Buffer.contents state.errmsgs ^
8100 (match state.errfd with
8101 | Some fd ->
8102 let s = String.create (80*24) in
8103 let n =
8105 let r, _, _ = Unix.select [fd] [] [] 0.0 in
8106 if List.mem fd r
8107 then Unix.read fd s 0 (String.length s)
8108 else 0
8109 with _ -> 0
8111 if n = 0
8112 then ""
8113 else String.sub s 0 n
8114 | None -> ""
8117 try ignore (Unix.write state.stderr s 0 (String.length s))
8118 with exn -> print_endline (exntos exn)
8122 init (cr, cw) (
8123 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
8124 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
8125 !Config.fontpath, !trimcachepath,
8126 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
8128 List.iter GlArray.enable [`texture_coord; `vertex];
8129 state.sr <- sr;
8130 state.sw <- sw;
8131 state.text <- "Opening " ^ (mbtoutf8 state.path);
8132 reshape winw winh;
8133 opendoc state.path state.password;
8134 state.uioh <- uioh;
8135 display ();
8136 Wsi.mapwin ();
8137 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
8138 let optrfd =
8139 ref (
8140 if nonemptystr !rcmdpath
8141 then remoteopen !rcmdpath
8142 else None
8146 let rec loop deadline =
8147 let r =
8148 match state.errfd with
8149 | None -> [state.sr; state.wsfd]
8150 | Some fd -> [state.sr; state.wsfd; fd]
8152 let r =
8153 match !optrfd with
8154 | None -> r
8155 | Some fd -> fd :: r
8157 if state.redisplay
8158 then (
8159 state.redisplay <- false;
8160 display ();
8162 let timeout =
8163 let now = now () in
8164 if deadline > now
8165 then (
8166 if deadline = infinity
8167 then ~-.1.0
8168 else max 0.0 (deadline -. now)
8170 else 0.0
8172 let r, _, _ =
8173 try Unix.select r [] [] timeout
8174 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
8176 begin match r with
8177 | [] ->
8178 state.ghyll None;
8179 let newdeadline =
8180 if state.ghyll == noghyll
8181 then
8182 match state.autoscroll with
8183 | Some step when step != 0 ->
8184 let y = state.y + step in
8185 let y =
8186 if y < 0
8187 then state.maxy
8188 else if y >= state.maxy then 0 else y
8190 gotoy y;
8191 if state.mode = View
8192 then state.text <- "";
8193 deadline +. 0.01
8194 | _ -> infinity
8195 else deadline +. 0.01
8197 loop newdeadline
8199 | l ->
8200 let rec checkfds = function
8201 | [] -> ()
8202 | fd :: rest when fd = state.sr ->
8203 let cmd = readcmd state.sr in
8204 act cmd;
8205 checkfds rest
8207 | fd :: rest when fd = state.wsfd ->
8208 Wsi.readresp fd;
8209 checkfds rest
8211 | fd :: rest when Some fd = !optrfd ->
8212 begin match remote fd with
8213 | None -> optrfd := remoteopen !rcmdpath;
8214 | opt -> optrfd := opt
8215 end;
8216 checkfds rest
8218 | fd :: rest ->
8219 let s = String.create 80 in
8220 let n = tempfailureretry (Unix.read fd s 0) 80 in
8221 if conf.redirectstderr
8222 then (
8223 Buffer.add_substring state.errmsgs s 0 n;
8224 state.newerrmsgs <- true;
8225 state.redisplay <- true;
8227 else (
8228 prerr_string (String.sub s 0 n);
8229 flush stderr;
8231 checkfds rest
8233 checkfds l;
8234 let newdeadline =
8235 let deadline1 =
8236 if deadline = infinity
8237 then now () +. 0.01
8238 else deadline
8240 match state.autoscroll with
8241 | Some step when step != 0 -> deadline1
8242 | _ -> if state.ghyll == noghyll then infinity else deadline1
8244 loop newdeadline
8245 end;
8248 loop infinity;
8249 with Quit ->
8250 Config.save ();