Uhm
[llpp.git] / main.ml
blob2cd701103aa440f897c0daaa1995a7db42960ed4
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), y =
2066 if single
2067 then
2068 let scl = if y > sy then 2 else -2 in
2069 let _N, _, _ = nab in
2070 (_N,0,_N), y+conf.scrollstep*scl
2071 else nab,y in
2072 let sum = summa _N _A _B in
2073 let dy = float (y - sy) in
2074 state.ghyll <- (
2075 let rec gf n y1 o =
2076 if n >= _N
2077 then state.ghyll <- noghyll
2078 else
2079 let go n =
2080 let s = scroll n _N _A _B in
2081 let y1 = y1 +. ((s *. dy) /. sum) in
2082 gotoy_and_clear_text (truncate y1);
2083 state.ghyll <- gf (n+1) y1;
2085 match o with
2086 | None -> go n
2087 | Some y' when single -> set nab y' state.y
2088 | Some y' -> set (_N/2, 1, 1) y' state.y
2090 gf 0 (float state.y)
2093 match conf.ghyllscroll with
2094 | Some nab when not conf.presentation ->
2095 if state.ghyll == noghyll
2096 then set nab y state.y
2097 else state.ghyll (Some y)
2098 | _ ->
2099 gotoy_and_clear_text y
2102 let gotoghyll = gotoghyll1 false;;
2104 let gotopage n top =
2105 let y, h = getpageyh n in
2106 let y = y + (truncate (top *. float h)) in
2107 gotoghyll y
2110 let gotopage1 n top =
2111 let y = getpagey n in
2112 let y = y + top in
2113 gotoghyll y
2116 let invalidate s f =
2117 state.layout <- [];
2118 state.pdims <- [];
2119 state.rects <- [];
2120 state.rects1 <- [];
2121 match state.geomcmds with
2122 | ps, [] when emptystr ps ->
2123 f ();
2124 state.geomcmds <- s, [];
2126 | ps, [] ->
2127 state.geomcmds <- ps, [s, f];
2129 | ps, (s', _) :: rest when s' = s ->
2130 state.geomcmds <- ps, ((s, f) :: rest);
2132 | ps, cmds ->
2133 state.geomcmds <- ps, ((s, f) :: cmds);
2136 let flushpages () =
2137 Hashtbl.iter (fun _ opaque ->
2138 wcmd "freepage %s" (~> opaque);
2139 ) state.pagemap;
2140 Hashtbl.clear state.pagemap;
2143 let flushtiles () =
2144 if not (Queue.is_empty state.tilelru)
2145 then (
2146 Queue.iter (fun (k, p, s) ->
2147 wcmd "freetile %s" (~> p);
2148 state.memused <- state.memused - s;
2149 Hashtbl.remove state.tilemap k;
2150 ) state.tilelru;
2151 state.uioh#infochanged Memused;
2152 Queue.clear state.tilelru;
2154 load state.layout;
2157 let stateh h =
2158 let h = truncate (float h*.conf.zoom) in
2159 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2160 h - d
2163 let opendoc path password =
2164 state.path <- path;
2165 state.password <- password;
2166 state.gen <- state.gen + 1;
2167 state.docinfo <- [];
2169 flushpages ();
2170 setaalevel conf.aalevel;
2171 let titlepath =
2172 if emptystr state.origin
2173 then path
2174 else state.origin
2176 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2177 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2178 invalidate "reqlayout"
2179 (fun () ->
2180 wcmd "reqlayout %d %d %d %s\000"
2181 conf.angle (FMTE.to_int conf.fitmodel)
2182 (stateh state.winh) state.nameddest
2186 let reload () =
2187 state.anchor <- getanchor ();
2188 opendoc state.path state.password;
2191 let scalecolor c =
2192 let c = c *. conf.colorscale in
2193 (c, c, c);
2196 let scalecolor2 (r, g, b) =
2197 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2200 let docolumns = function
2201 | Csingle _ ->
2202 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2203 let rec loop pageno pdimno pdim y ph pdims =
2204 if pageno = state.pagecount
2205 then ()
2206 else
2207 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2208 match pdims with
2209 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2210 pdimno+1, pdim, rest
2211 | _ ->
2212 pdimno, pdim, pdims
2214 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2215 let y = y +
2216 (if conf.presentation
2217 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2218 else (if pageno = 0 then 0 else conf.interpagespace)
2221 a.(pageno) <- (pdimno, x, y, pdim);
2222 loop (pageno+1) pdimno pdim (y + h) h pdims
2224 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2225 conf.columns <- Csingle a;
2227 | Cmulti ((columns, coverA, coverB), _) ->
2228 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2229 let rec loop pageno pdimno pdim x y rowh pdims =
2230 let rec fixrow m = if m = pageno then () else
2231 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2232 if h < rowh
2233 then (
2234 let y = y + (rowh - h) / 2 in
2235 a.(m) <- (pdimno, x, y, pdim);
2237 fixrow (m+1)
2239 if pageno = state.pagecount
2240 then fixrow (((pageno - 1) / columns) * columns)
2241 else
2242 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2243 match pdims with
2244 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2245 pdimno+1, pdim, rest
2246 | _ ->
2247 pdimno, pdim, pdims
2249 let x, y, rowh' =
2250 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2251 then (
2252 let x = (wadjsb state.winw - w) / 2 in
2253 let ips =
2254 if conf.presentation then calcips h else conf.interpagespace in
2255 x, y + ips + rowh, h
2257 else (
2258 if (pageno - coverA) mod columns = 0
2259 then (
2260 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2261 let y =
2262 if conf.presentation
2263 then
2264 let ips = calcips h in
2265 y + (if pageno = 0 then 0 else calcips rowh + ips)
2266 else
2267 y + (if pageno = 0 then 0 else conf.interpagespace)
2269 x, y + rowh, h
2271 else x, y, max rowh h
2274 let y =
2275 if pageno > 1 && (pageno - coverA) mod columns = 0
2276 then (
2277 let y =
2278 if pageno = columns && conf.presentation
2279 then (
2280 let ips = calcips rowh in
2281 for i = 0 to pred columns
2283 let (pdimno, x, y, pdim) = a.(i) in
2284 a.(i) <- (pdimno, x, y+ips, pdim)
2285 done;
2286 y+ips;
2288 else y
2290 fixrow (pageno - columns);
2293 else y
2295 a.(pageno) <- (pdimno, x, y, pdim);
2296 let x = x + w + xoff*2 + conf.interpagespace in
2297 loop (pageno+1) pdimno pdim x y rowh' pdims
2299 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2300 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2302 | Csplit (c, _) ->
2303 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2304 let rec loop pageno pdimno pdim y pdims =
2305 if pageno = state.pagecount
2306 then ()
2307 else
2308 let pdimno, ((_, w, h, _) as pdim), pdims =
2309 match pdims with
2310 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2311 pdimno+1, pdim, rest
2312 | _ ->
2313 pdimno, pdim, pdims
2315 let cw = w / c in
2316 let rec loop1 n x y =
2317 if n = c then y else (
2318 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2319 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2322 let y = loop1 0 0 y in
2323 loop (pageno+1) pdimno pdim y pdims
2325 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2326 conf.columns <- Csplit (c, a);
2329 let represent () =
2330 docolumns conf.columns;
2331 state.maxy <- calcheight ();
2332 if state.reprf == noreprf
2333 then (
2334 match state.mode with
2335 | Birdseye (_, _, pageno, _, _) ->
2336 let y, h = getpageyh pageno in
2337 let top = (state.winh - h) / 2 in
2338 gotoy (max 0 (y - top))
2339 | _ -> gotoanchor state.anchor
2341 else (
2342 state.reprf ();
2343 state.reprf <- noreprf;
2347 let reshape w h =
2348 GlDraw.viewport 0 0 w h;
2349 let firsttime = state.geomcmds == firstgeomcmds in
2350 if not firsttime && nogeomcmds state.geomcmds
2351 then state.anchor <- getanchor ();
2353 state.winw <- w;
2354 let w = wadjsb (truncate (float w *. conf.zoom)) in
2355 let w = max w 2 in
2356 state.winh <- h;
2357 setfontsize fstate.fontsize;
2358 GlMat.mode `modelview;
2359 GlMat.load_identity ();
2361 GlMat.mode `projection;
2362 GlMat.load_identity ();
2363 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2364 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2365 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2367 let relx =
2368 if conf.zoom <= 1.0
2369 then 0.0
2370 else float state.x /. float state.w
2372 invalidate "geometry"
2373 (fun () ->
2374 state.w <- w;
2375 if not firsttime
2376 then state.x <- truncate (relx *. float w);
2377 let w =
2378 match conf.columns with
2379 | Csingle _ -> w
2380 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2381 | Csplit (c, _) -> w * c
2383 wcmd "geometry %d %d %d"
2384 w (stateh h) (FMTE.to_int conf.fitmodel)
2388 let enttext () =
2389 let len = String.length state.text in
2390 let drawstring s =
2391 let hscrollh =
2392 match state.mode with
2393 | Textentry _ | View | LinkNav _ ->
2394 let h, _, _ = state.uioh#scrollpw in
2396 | _ -> 0
2398 let rect x w =
2399 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2400 (x+.w) (float (state.winh - hscrollh))
2403 let w = float (wadjsb state.winw - 1) in
2404 if state.progress >= 0.0 && state.progress < 1.0
2405 then (
2406 GlDraw.color (0.3, 0.3, 0.3);
2407 let w1 = w *. state.progress in
2408 rect 0.0 w1;
2409 GlDraw.color (0.0, 0.0, 0.0);
2410 rect w1 (w-.w1)
2412 else (
2413 GlDraw.color (0.0, 0.0, 0.0);
2414 rect 0.0 w;
2417 GlDraw.color (1.0, 1.0, 1.0);
2418 drawstring fstate.fontsize
2419 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2421 let s =
2422 match state.mode with
2423 | Textentry ((prefix, text, _, _, _, _), _) ->
2424 let s =
2425 if len > 0
2426 then
2427 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2428 else
2429 Printf.sprintf "%s%s_" prefix text
2433 | _ -> state.text
2435 let s =
2436 if state.newerrmsgs
2437 then (
2438 if not (istextentry state.mode) && state.uioh#eformsgs
2439 then
2440 let s1 = "(press 'e' to review error messasges)" in
2441 if nonemptystr s then s ^ " " ^ s1 else s1
2442 else s
2444 else s
2446 if nonemptystr s
2447 then drawstring s
2450 let gctiles () =
2451 let len = Queue.length state.tilelru in
2452 let layout = lazy (
2453 match state.throttle with
2454 | None ->
2455 if conf.preload
2456 then preloadlayout state.y
2457 else state.layout
2458 | Some (layout, _, _) ->
2459 layout
2460 ) in
2461 let rec loop qpos =
2462 if state.memused <= conf.memlimit
2463 then ()
2464 else (
2465 if qpos < len
2466 then
2467 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2468 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2469 let (_, pw, ph, _) = getpagedim n in
2471 gen = state.gen
2472 && colorspace = conf.colorspace
2473 && angle = conf.angle
2474 && pagew = pw
2475 && pageh = ph
2476 && (
2477 let x = col*conf.tilew
2478 and y = row*conf.tileh in
2479 tilevisible (Lazy.force_val layout) n x y
2481 then Queue.push lruitem state.tilelru
2482 else (
2483 freepbo p;
2484 wcmd "freetile %s" (~> p);
2485 state.memused <- state.memused - s;
2486 state.uioh#infochanged Memused;
2487 Hashtbl.remove state.tilemap k;
2489 loop (qpos+1)
2492 loop 0
2495 let logcurrently = function
2496 | Idle -> dolog "Idle"
2497 | Loading (l, gen) ->
2498 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2499 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2500 dolog
2501 "Tiling %d[%d,%d] page=%s cs=%s angle"
2502 l.pageno col row (~> pageopaque)
2503 (CSTE.to_string colorspace)
2505 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2506 angle gen conf.angle state.gen
2507 tilew tileh
2508 conf.tilew conf.tileh
2510 | Outlining _ ->
2511 dolog "outlining"
2514 let splitatspace =
2515 let r = Str.regexp " " in
2516 fun s -> Str.bounded_split r s 2;
2519 let onpagerect pageno f =
2520 let b =
2521 match conf.columns with
2522 | Cmulti (_, b) -> b
2523 | Csingle b -> b
2524 | Csplit (_, b) -> b
2526 if pageno >= 0 && pageno < Array.length b
2527 then
2528 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2529 f w h
2532 let gotopagexy1 pageno x y =
2533 let _,w1,h1,leftx = getpagedim pageno in
2534 let top = y /. (float h1) in
2535 let left = x /. (float w1) in
2536 let py, w, h = getpageywh pageno in
2537 let wh = state.winh - hscrollh () in
2538 let x = left *. (float w) in
2539 let x = leftx + state.x + truncate x in
2540 let sx =
2541 if x < 0 || x >= wadjsb state.winw
2542 then state.x - x
2543 else state.x
2545 let pdy = truncate (top *. float h) in
2546 let y' = py + pdy in
2547 let dy = y' - state.y in
2548 let sy =
2549 if x != state.x || not (dy > 0 && dy < wh)
2550 then (
2551 if conf.presentation
2552 then
2553 if abs (py - y') > wh
2554 then y'
2555 else py
2556 else y';
2558 else state.y
2560 if state.x != sx || state.y != sy
2561 then (
2562 let x, y =
2563 if !wtmode
2564 then (
2565 let ww = wadjsb state.winw in
2566 let qx = sx / ww
2567 and qy = pdy / wh in
2568 let x = qx * ww
2569 and y = py + qy * wh in
2570 let x = if -x + ww > w1 then -(w1-ww) else x
2571 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2572 let y =
2573 if conf.presentation
2574 then
2575 if abs (py - y') > wh
2576 then y'
2577 else py
2578 else y';
2580 (x, y)
2582 else (sx, sy)
2584 state.x <- x;
2585 gotoy_and_clear_text y;
2587 else gotoy_and_clear_text state.y;
2590 let gotopagexy pageno x y =
2591 match state.mode with
2592 | Birdseye _ -> gotopage pageno 0.0
2593 | _ -> gotopagexy1 pageno x y
2596 let act cmds =
2597 (* dolog "%S" cmds; *)
2598 let cl = splitatspace cmds in
2599 let scan s fmt f =
2600 try Scanf.sscanf s fmt f
2601 with exn ->
2602 dolog "error processing '%S': %s" cmds (exntos exn);
2603 exit 1
2605 let addoutline outline =
2606 match state.currently with
2607 | Outlining outlines ->
2608 state.currently <- Outlining (outline :: outlines)
2609 | Idle -> state.currently <- Outlining [outline]
2610 | currently ->
2611 dolog "invalid outlining state";
2612 logcurrently currently
2614 match cl with
2615 | "clear" :: [] ->
2616 state.uioh#infochanged Pdim;
2617 state.pdims <- [];
2619 | "clearrects" :: [] ->
2620 state.rects <- state.rects1;
2621 G.postRedisplay "clearrects";
2623 | "continue" :: args :: [] ->
2624 let n = scan args "%u" (fun n -> n) in
2625 state.pagecount <- n;
2626 begin match state.currently with
2627 | Outlining l ->
2628 state.currently <- Idle;
2629 state.outlines <- Array.of_list (List.rev l)
2630 | _ -> ()
2631 end;
2633 let cur, cmds = state.geomcmds in
2634 if emptystr cur
2635 then failwith "umpossible";
2637 begin match List.rev cmds with
2638 | [] ->
2639 state.geomcmds <- "", [];
2640 state.throttle <- None;
2641 represent ();
2642 | (s, f) :: rest ->
2643 f ();
2644 state.geomcmds <- s, List.rev rest;
2645 end;
2646 if conf.maxwait = None && not !wtmode
2647 then G.postRedisplay "continue";
2649 | "title" :: args :: [] ->
2650 Wsi.settitle args
2652 | "msg" :: args :: [] ->
2653 showtext ' ' args
2655 | "vmsg" :: args :: [] ->
2656 if conf.verbose
2657 then showtext ' ' args
2659 | "emsg" :: args :: [] ->
2660 Buffer.add_string state.errmsgs args;
2661 state.newerrmsgs <- true;
2662 G.postRedisplay "error message"
2664 | "progress" :: args :: [] ->
2665 let progress, text =
2666 scan args "%f %n"
2667 (fun f pos ->
2668 f, String.sub args pos (String.length args - pos))
2670 state.text <- text;
2671 state.progress <- progress;
2672 G.postRedisplay "progress"
2674 | "firstmatch" :: args :: [] ->
2675 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2676 scan args "%u %d %f %f %f %f %f %f %f %f"
2677 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2678 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2680 let y = (getpagey pageno) + truncate y0 in
2681 addnav ();
2682 gotoy y;
2683 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2685 | "match" :: args :: [] ->
2686 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2687 scan args "%u %d %f %f %f %f %f %f %f %f"
2688 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2689 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2691 state.rects1 <-
2692 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2694 | "page" :: args :: [] ->
2695 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
2696 let pageopaque = ~< pageopaques in
2697 begin match state.currently with
2698 | Loading (l, gen) ->
2699 vlog "page %d took %f sec" l.pageno t;
2700 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2701 begin match state.throttle with
2702 | None ->
2703 let preloadedpages =
2704 if conf.preload
2705 then preloadlayout state.y
2706 else state.layout
2708 let evict () =
2709 let set =
2710 List.fold_left (fun s l -> IntSet.add l.pageno s)
2711 IntSet.empty preloadedpages
2713 let evictedpages =
2714 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2715 if not (IntSet.mem pageno set)
2716 then (
2717 wcmd "freepage %s" (~> opaque);
2718 key :: accu
2720 else accu
2721 ) state.pagemap []
2723 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2725 evict ();
2726 state.currently <- Idle;
2727 if gen = state.gen
2728 then (
2729 tilepage l.pageno pageopaque state.layout;
2730 load state.layout;
2731 load preloadedpages;
2732 if pagevisible state.layout l.pageno
2733 && layoutready state.layout
2734 then G.postRedisplay "page";
2737 | Some (layout, _, _) ->
2738 state.currently <- Idle;
2739 tilepage l.pageno pageopaque layout;
2740 load state.layout
2741 end;
2743 | _ ->
2744 dolog "Inconsistent loading state";
2745 logcurrently state.currently;
2746 exit 1
2749 | "tile" :: args :: [] ->
2750 let (x, y, opaques, size, t) =
2751 scan args "%u %u %s %u %f"
2752 (fun x y p size t -> (x, y, p, size, t))
2754 let opaque = ~< opaques in
2755 begin match state.currently with
2756 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2757 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2759 unmappbo opaque;
2760 if tilew != conf.tilew || tileh != conf.tileh
2761 then (
2762 wcmd "freetile %s" (~> opaque);
2763 state.currently <- Idle;
2764 load state.layout;
2766 else (
2767 puttileopaque l col row gen cs angle opaque size t;
2768 state.memused <- state.memused + size;
2769 state.uioh#infochanged Memused;
2770 gctiles ();
2771 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2772 opaque, size) state.tilelru;
2774 let layout =
2775 match state.throttle with
2776 | None -> state.layout
2777 | Some (layout, _, _) -> layout
2780 state.currently <- Idle;
2781 if gen = state.gen
2782 && conf.colorspace = cs
2783 && conf.angle = angle
2784 && tilevisible layout l.pageno x y
2785 then conttiling l.pageno pageopaque;
2787 begin match state.throttle with
2788 | None ->
2789 preload state.layout;
2790 if gen = state.gen
2791 && conf.colorspace = cs
2792 && conf.angle = angle
2793 && tilevisible state.layout l.pageno x y
2794 && (not !wtmode || layoutready state.layout)
2795 then G.postRedisplay "tile nothrottle";
2797 | Some (layout, y, _) ->
2798 let ready = layoutready layout in
2799 if ready
2800 then (
2801 state.y <- y;
2802 state.layout <- layout;
2803 state.throttle <- None;
2804 G.postRedisplay "throttle";
2806 else load layout;
2807 end;
2810 | _ ->
2811 dolog "Inconsistent tiling state";
2812 logcurrently state.currently;
2813 exit 1
2816 | "pdim" :: args :: [] ->
2817 let (n, w, h, _) as pdim =
2818 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2820 let pdim =
2821 match conf.fitmodel, conf.columns with
2822 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2823 | _ -> pdim
2825 state.uioh#infochanged Pdim;
2826 state.pdims <- pdim :: state.pdims
2828 | "o" :: args :: [] ->
2829 let (l, n, t, h, pos) =
2830 scan args "%u %u %d %u %n"
2831 (fun l n t h pos -> l, n, t, h, pos)
2833 let s = String.sub args pos (String.length args - pos) in
2834 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
2836 | "ou" :: args :: [] ->
2837 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
2838 let s = String.sub args pos len in
2839 let pos2 = pos + len + 1 in
2840 let uri = String.sub args pos2 (String.length args - pos2) in
2841 addoutline (s, l, Ouri uri)
2843 | "on" :: args :: [] ->
2844 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
2845 let s = String.sub args pos (String.length args - pos) in
2846 addoutline (s, l, Onone)
2848 | "a" :: args :: [] ->
2849 let (n, l, t) =
2850 scan args "%u %d %d" (fun n l t -> n, l, t)
2852 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2854 | "info" :: args :: [] ->
2855 state.docinfo <- (1, args) :: state.docinfo
2857 | "infoend" :: [] ->
2858 state.uioh#infochanged Docinfo;
2859 state.docinfo <- List.rev state.docinfo
2861 | _ ->
2862 error "unknown cmd `%S'" cmds
2865 let onhist cb =
2866 let rc = cb.rc in
2867 let action = function
2868 | HCprev -> cbget cb ~-1
2869 | HCnext -> cbget cb 1
2870 | HCfirst -> cbget cb ~-(cb.rc)
2871 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2872 and cancel () = cb.rc <- rc
2873 in (action, cancel)
2876 let search pattern forward =
2877 match conf.columns with
2878 | Csplit _ ->
2879 showtext '!' "searching does not work properly in split columns mode"
2880 | _ ->
2881 if nonemptystr pattern
2882 then
2883 let pn, py =
2884 match state.layout with
2885 | [] -> 0, 0
2886 | l :: _ ->
2887 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2889 wcmd "search %d %d %d %d,%s\000"
2890 (btod conf.icase) pn py (btod forward) pattern;
2893 let intentry text key =
2894 let c =
2895 if key >= 32 && key < 127
2896 then Char.chr key
2897 else '\000'
2899 match c with
2900 | '0' .. '9' ->
2901 let text = addchar text c in
2902 TEcont text
2904 | _ ->
2905 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2906 TEcont text
2909 let linknentry text key =
2910 let c =
2911 if key >= 32 && key < 127
2912 then Char.chr key
2913 else '\000'
2915 match c with
2916 | 'a' .. 'z' ->
2917 let text = addchar text c in
2918 TEcont text
2920 | _ ->
2921 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2922 TEcont text
2925 let linkndone f s =
2926 if nonemptystr s
2927 then (
2928 let n =
2929 let l = String.length s in
2930 let rec loop pos n = if pos = l then n else
2931 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2932 loop (pos+1) (n*26 + m)
2933 in loop 0 0
2935 let rec loop n = function
2936 | [] -> ()
2937 | l :: rest ->
2938 match getopaque l.pageno with
2939 | None -> loop n rest
2940 | Some opaque ->
2941 let m = getlinkcount opaque in
2942 if n < m
2943 then (
2944 let under = getlink opaque n in
2945 f under
2947 else loop (n-m) rest
2949 loop n state.layout;
2953 let textentry text key =
2954 if key land 0xff00 = 0xff00
2955 then TEcont text
2956 else TEcont (text ^ toutf8 key)
2959 let reqlayout angle fitmodel =
2960 match state.throttle with
2961 | None ->
2962 if nogeomcmds state.geomcmds
2963 then state.anchor <- getanchor ();
2964 conf.angle <- angle mod 360;
2965 if conf.angle != 0
2966 then (
2967 match state.mode with
2968 | LinkNav _ -> state.mode <- View
2969 | _ -> ()
2971 conf.fitmodel <- fitmodel;
2972 invalidate "reqlayout"
2973 (fun () ->
2974 wcmd "reqlayout %d %d %d"
2975 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2977 | _ -> ()
2980 let settrim trimmargins trimfuzz =
2981 if nogeomcmds state.geomcmds
2982 then state.anchor <- getanchor ();
2983 conf.trimmargins <- trimmargins;
2984 conf.trimfuzz <- trimfuzz;
2985 let x0, y0, x1, y1 = trimfuzz in
2986 invalidate "settrim"
2987 (fun () ->
2988 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2989 flushpages ();
2992 let setzoom zoom =
2993 match state.throttle with
2994 | None ->
2995 let zoom = max 0.0001 zoom in
2996 if zoom <> conf.zoom
2997 then (
2998 state.prevzoom <- (conf.zoom, state.x);
2999 conf.zoom <- zoom;
3000 reshape state.winw state.winh;
3001 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
3004 | Some (layout, y, started) ->
3005 let time =
3006 match conf.maxwait with
3007 | None -> 0.0
3008 | Some t -> t
3010 let dt = now () -. started in
3011 if dt > time
3012 then (
3013 state.y <- y;
3014 load layout;
3018 let setcolumns mode columns coverA coverB =
3019 state.prevcolumns <- Some (conf.columns, conf.zoom);
3020 if columns < 0
3021 then (
3022 if isbirdseye mode
3023 then showtext '!' "split mode doesn't work in bird's eye"
3024 else (
3025 conf.columns <- Csplit (-columns, [||]);
3026 state.x <- 0;
3027 conf.zoom <- 1.0;
3030 else (
3031 if columns < 2
3032 then (
3033 conf.columns <- Csingle [||];
3034 state.x <- 0;
3035 setzoom 1.0;
3037 else (
3038 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
3039 conf.zoom <- 1.0;
3042 reshape state.winw state.winh;
3045 let resetmstate () =
3046 state.mstate <- Mnone;
3047 Wsi.setcursor Wsi.CURSOR_INHERIT;
3050 let enterbirdseye () =
3051 let zoom = float conf.thumbw /. float state.winw in
3052 let birdseyepageno =
3053 let cy = state.winh / 2 in
3054 let fold = function
3055 | [] -> 0
3056 | l :: rest ->
3057 let rec fold best = function
3058 | [] -> best.pageno
3059 | l :: rest ->
3060 let d = cy - (l.pagedispy + l.pagevh/2)
3061 and dbest = cy - (best.pagedispy + best.pagevh/2) in
3062 if abs d < abs dbest
3063 then fold l rest
3064 else best.pageno
3065 in fold l rest
3067 fold state.layout
3069 state.mode <- Birdseye (
3070 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
3072 resetmstate ();
3073 conf.zoom <- zoom;
3074 conf.presentation <- false;
3075 conf.interpagespace <- 10;
3076 conf.hlinks <- false;
3077 conf.fitmodel <- FitProportional;
3078 state.x <- 0;
3079 conf.maxwait <- None;
3080 conf.columns <- (
3081 match conf.beyecolumns with
3082 | Some c ->
3083 conf.zoom <- 1.0;
3084 Cmulti ((c, 0, 0), [||])
3085 | None -> Csingle [||]
3087 if conf.verbose
3088 then
3089 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3090 (100.0*.zoom)
3091 else
3092 state.text <- ""
3094 reshape state.winw state.winh;
3097 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3098 state.mode <- View;
3099 conf.zoom <- c.zoom;
3100 conf.presentation <- c.presentation;
3101 conf.interpagespace <- c.interpagespace;
3102 conf.maxwait <- c.maxwait;
3103 conf.hlinks <- c.hlinks;
3104 conf.fitmodel <- c.fitmodel;
3105 conf.beyecolumns <- (
3106 match conf.columns with
3107 | Cmulti ((c, _, _), _) -> Some c
3108 | Csingle _ -> None
3109 | Csplit _ -> failwith "leaving bird's eye split mode"
3111 conf.columns <- (
3112 match c.columns with
3113 | Cmulti (c, _) -> Cmulti (c, [||])
3114 | Csingle _ -> Csingle [||]
3115 | Csplit (c, _) -> Csplit (c, [||])
3117 if conf.verbose
3118 then
3119 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3120 (100.0*.conf.zoom)
3122 reshape state.winw state.winh;
3123 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3124 state.x <- leftx;
3127 let togglebirdseye () =
3128 match state.mode with
3129 | Birdseye vals -> leavebirdseye vals true
3130 | View -> enterbirdseye ()
3131 | _ -> ()
3134 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3135 let pageno = max 0 (pageno - incr) in
3136 let rec loop = function
3137 | [] -> gotopage1 pageno 0
3138 | l :: _ when l.pageno = pageno ->
3139 if l.pagedispy >= 0 && l.pagey = 0
3140 then G.postRedisplay "upbirdseye"
3141 else gotopage1 pageno 0
3142 | _ :: rest -> loop rest
3144 loop state.layout;
3145 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3148 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3149 let pageno = min (state.pagecount - 1) (pageno + incr) in
3150 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3151 let rec loop = function
3152 | [] ->
3153 let y, h = getpageyh pageno in
3154 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3155 gotoy (clamp dy)
3156 | l :: _ when l.pageno = pageno ->
3157 if l.pagevh != l.pageh
3158 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3159 else G.postRedisplay "downbirdseye"
3160 | _ :: rest -> loop rest
3162 loop state.layout
3165 let boundastep h step =
3166 if step < 0
3167 then bound step ~-h 0
3168 else bound step 0 h
3171 let optentry mode _ key =
3172 let btos b = if b then "on" else "off" in
3173 if key >= 32 && key < 127
3174 then
3175 let c = Char.chr key in
3176 match c with
3177 | 's' ->
3178 let ondone s =
3179 try conf.scrollstep <- int_of_string s with exc ->
3180 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3182 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3184 | 'A' ->
3185 let ondone s =
3187 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
3188 if state.autoscroll <> None
3189 then state.autoscroll <- Some conf.autoscrollstep
3190 with exc ->
3191 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3193 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3195 | 'C' ->
3196 let ondone s =
3198 let n, a, b = multicolumns_of_string s in
3199 setcolumns mode n a b;
3200 with exc ->
3201 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3203 TEswitch ("columns: ", "", None, textentry, ondone, true)
3205 | 'Z' ->
3206 let ondone s =
3208 let zoom = float (int_of_string s) /. 100.0 in
3209 setzoom zoom
3210 with exc ->
3211 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3213 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3215 | 't' ->
3216 let ondone s =
3218 conf.thumbw <- bound (int_of_string s) 2 4096;
3219 state.text <-
3220 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3221 begin match mode with
3222 | Birdseye beye ->
3223 leavebirdseye beye false;
3224 enterbirdseye ();
3225 | _ -> ();
3227 with exc ->
3228 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3230 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3232 | 'R' ->
3233 let ondone s =
3234 match try
3235 Some (int_of_string s)
3236 with exc ->
3237 state.text <- Printf.sprintf "bad integer `%s': %s"
3238 s (exntos exc);
3239 None
3240 with
3241 | Some angle -> reqlayout angle conf.fitmodel
3242 | None -> ()
3244 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3246 | 'i' ->
3247 conf.icase <- not conf.icase;
3248 TEdone ("case insensitive search " ^ (btos conf.icase))
3250 | 'p' ->
3251 conf.preload <- not conf.preload;
3252 gotoy state.y;
3253 TEdone ("preload " ^ (btos conf.preload))
3255 | 'v' ->
3256 conf.verbose <- not conf.verbose;
3257 TEdone ("verbose " ^ (btos conf.verbose))
3259 | 'd' ->
3260 conf.debug <- not conf.debug;
3261 TEdone ("debug " ^ (btos conf.debug))
3263 | 'h' ->
3264 conf.maxhfit <- not conf.maxhfit;
3265 state.maxy <- calcheight ();
3266 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3268 | 'c' ->
3269 conf.crophack <- not conf.crophack;
3270 TEdone ("crophack " ^ btos conf.crophack)
3272 | 'a' ->
3273 let s =
3274 match conf.maxwait with
3275 | None ->
3276 conf.maxwait <- Some infinity;
3277 "always wait for page to complete"
3278 | Some _ ->
3279 conf.maxwait <- None;
3280 "show placeholder if page is not ready"
3282 TEdone s
3284 | 'f' ->
3285 conf.underinfo <- not conf.underinfo;
3286 TEdone ("underinfo " ^ btos conf.underinfo)
3288 | 'P' ->
3289 conf.savebmarks <- not conf.savebmarks;
3290 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3292 | 'S' ->
3293 let ondone s =
3295 let pageno, py =
3296 match state.layout with
3297 | [] -> 0, 0
3298 | l :: _ ->
3299 l.pageno, l.pagey
3301 conf.interpagespace <- int_of_string s;
3302 docolumns conf.columns;
3303 state.maxy <- calcheight ();
3304 let y = getpagey pageno in
3305 gotoy (y + py)
3306 with exc ->
3307 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3309 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3311 | 'l' ->
3312 let fm =
3313 match conf.fitmodel with
3314 | FitProportional -> FitWidth
3315 | _ -> FitProportional
3317 reqlayout conf.angle fm;
3318 TEdone ("proportional display " ^ btos (fm == FitProportional))
3320 | 'T' ->
3321 settrim (not conf.trimmargins) conf.trimfuzz;
3322 TEdone ("trim margins " ^ btos conf.trimmargins)
3324 | 'I' ->
3325 conf.invert <- not conf.invert;
3326 TEdone ("invert colors " ^ btos conf.invert)
3328 | 'x' ->
3329 let ondone s =
3330 cbput state.hists.sel s;
3331 conf.selcmd <- s;
3333 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3334 textentry, ondone, true)
3336 | 'M' ->
3337 if conf.pax == None
3338 then conf.pax <- Some (ref (0.0, 0, 0))
3339 else conf.pax <- None;
3340 TEdone ("PAX " ^ btos (conf.pax != None))
3342 | _ ->
3343 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3344 TEstop
3345 else
3346 TEcont state.text
3349 class type lvsource = object
3350 method getitemcount : int
3351 method getitem : int -> (string * int)
3352 method hasaction : int -> bool
3353 method exit :
3354 uioh:uioh ->
3355 cancel:bool ->
3356 active:int ->
3357 first:int ->
3358 pan:int ->
3359 uioh option
3360 method getactive : int
3361 method getfirst : int
3362 method getpan : int
3363 method getminfo : (int * int) array
3364 end;;
3366 class virtual lvsourcebase = object
3367 val mutable m_active = 0
3368 val mutable m_first = 0
3369 val mutable m_pan = 0
3370 method getactive = m_active
3371 method getfirst = m_first
3372 method getpan = m_pan
3373 method getminfo : (int * int) array = [||]
3374 end;;
3376 let withoutlastutf8 s =
3377 let len = String.length s in
3378 if len = 0
3379 then s
3380 else
3381 let rec find pos =
3382 if pos = 0
3383 then pos
3384 else
3385 let b = Char.code s.[pos] in
3386 if b land 0b11000000 = 0b11000000
3387 then pos
3388 else find (pos-1)
3390 let first =
3391 if Char.code s.[len-1] land 0x80 = 0
3392 then len-1
3393 else find (len-1)
3395 String.sub s 0 first;
3398 let textentrykeyboard
3399 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3400 let key =
3401 if key >= 0xffb0 && key <= 0xffb9
3402 then key - 0xffb0 + 48 else key
3404 let enttext te =
3405 state.mode <- Textentry (te, onleave);
3406 state.text <- "";
3407 enttext ();
3408 G.postRedisplay "textentrykeyboard enttext";
3410 let histaction cmd =
3411 match opthist with
3412 | None -> ()
3413 | Some (action, _) ->
3414 state.mode <- Textentry (
3415 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3417 G.postRedisplay "textentry histaction"
3419 match key with
3420 | 0xff08 -> (* backspace *)
3421 if emptystr text && cancelonempty
3422 then (
3423 onleave Cancel;
3424 G.postRedisplay "textentrykeyboard after cancel";
3426 else
3427 let s = withoutlastutf8 text in
3428 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3430 | 0xff0d | 0xff8d -> (* (kp) enter *)
3431 ondone text;
3432 onleave Confirm;
3433 G.postRedisplay "textentrykeyboard after confirm"
3435 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3436 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3437 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3438 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3440 | 0xff1b -> (* escape*)
3441 if emptystr text
3442 then (
3443 begin match opthist with
3444 | None -> ()
3445 | Some (_, onhistcancel) -> onhistcancel ()
3446 end;
3447 onleave Cancel;
3448 state.text <- "";
3449 G.postRedisplay "textentrykeyboard after cancel2"
3451 else (
3452 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3455 | 0xff9f | 0xffff -> () (* delete *)
3457 | _ when key != 0
3458 && key land 0xff00 != 0xff00 (* keyboard *)
3459 && key land 0xfe00 != 0xfe00 (* xkb *)
3460 && key land 0xfd00 != 0xfd00 (* 3270 *)
3462 begin match onkey text key with
3463 | TEdone text ->
3464 ondone text;
3465 onleave Confirm;
3466 G.postRedisplay "textentrykeyboard after confirm2";
3468 | TEcont text ->
3469 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3471 | TEstop ->
3472 onleave Cancel;
3473 G.postRedisplay "textentrykeyboard after cancel3"
3475 | TEswitch te ->
3476 state.mode <- Textentry (te, onleave);
3477 G.postRedisplay "textentrykeyboard switch";
3478 end;
3480 | _ ->
3481 vlog "unhandled key %s" (Wsi.keyname key)
3484 let firstof first active =
3485 if first > active || abs (first - active) > fstate.maxrows - 1
3486 then max 0 (active - (fstate.maxrows/2))
3487 else first
3490 let calcfirst first active =
3491 if active > first
3492 then
3493 let rows = active - first in
3494 if rows > fstate.maxrows then active - fstate.maxrows else first
3495 else active
3498 let scrollph y maxy =
3499 let sh = float (maxy + state.winh) /. float state.winh in
3500 let sh = float state.winh /. sh in
3501 let sh = max sh (float conf.scrollh) in
3503 let percent = float y /. float maxy in
3504 let position = (float state.winh -. sh) *. percent in
3506 let position =
3507 if position +. sh > float state.winh
3508 then float state.winh -. sh
3509 else position
3511 position, sh;
3514 let coe s = (s :> uioh);;
3516 class listview ~(source:lvsource) ~trusted ~modehash =
3517 object (self)
3518 val m_pan = source#getpan
3519 val m_first = source#getfirst
3520 val m_active = source#getactive
3521 val m_qsearch = ""
3522 val m_prev_uioh = state.uioh
3524 method private elemunder y =
3525 if y < 0
3526 then None
3527 else
3528 let n = y / (fstate.fontsize+1) in
3529 if m_first + n < source#getitemcount
3530 then (
3531 if source#hasaction (m_first + n)
3532 then Some (m_first + n)
3533 else None
3535 else None
3537 method display =
3538 Gl.enable `blend;
3539 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3540 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3541 filledrect 0. 0. (float state.winw) (float state.winh);
3542 GlDraw.color (1., 1., 1.);
3543 Gl.enable `texture_2d;
3544 let fs = fstate.fontsize in
3545 let nfs = fs + 1 in
3546 let ww = fstate.wwidth in
3547 let tabw = 30.0*.ww in
3548 let itemcount = source#getitemcount in
3549 let minfo = source#getminfo in
3550 let helphack = ref false in
3551 let rec loop row =
3552 if (row - m_first) > fstate.maxrows
3553 then ()
3554 else (
3555 if row >= 0 && row < itemcount
3556 then (
3557 let (s, level) = source#getitem row in
3558 let y = (row - m_first) * nfs in
3559 let x = 5.0 +. float (level + m_pan) *. ww in
3560 if row = m_active
3561 then (
3562 Gl.disable `texture_2d;
3563 let alpha = if source#hasaction row then 0.9 else 0.3 in
3564 GlDraw.color (1., 1., 1.) ~alpha;
3565 linerect 1. (float (y + 1))
3566 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3567 Gl.enable `texture_2d;
3568 GlDraw.color (1., 1., 1.);
3570 let drawtabularstring s =
3571 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3572 if trusted
3573 then
3574 let x = if !helphack then x +. ww else x in
3575 let tabpos = try String.index s '\t' with Not_found -> -1 in
3576 if tabpos > 0
3577 then
3578 let len = String.length s - tabpos - 1 in
3579 let s1 = String.sub s 0 tabpos
3580 and s2 = String.sub s (tabpos + 1) len in
3581 let nx = drawstr x s1 in
3582 let sw = nx -. x in
3583 let x = x +. (max tabw sw) in
3584 drawstr x s2
3585 else
3586 let len = String.length s - 2 in
3587 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
3588 then
3589 let s = String.sub s 2 len in
3590 let x =
3591 if not !helphack
3592 then (
3593 helphack := true;
3594 x +. ww
3596 else x
3598 GlDraw.color (1.2, 1.2, 1.2);
3599 let vinc = drawstring1 (fs+fs/4)
3600 (truncate (x -. ww)) (y+nfs) s in
3601 GlDraw.color (1., 1., 1.);
3602 vinc +. (float fs *. 0.8)
3603 else
3604 drawstr x s
3605 else
3606 drawstr x s
3608 let _ = drawtabularstring s in
3609 loop (row+1)
3613 loop m_first;
3614 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
3615 let rec loop row =
3616 if (row - m_first) > fstate.maxrows
3617 then ()
3618 else (
3619 if row >= 0 && row < itemcount
3620 then (
3621 let (s, level) = source#getitem row in
3622 let y = (row - m_first) * nfs in
3623 let x = 5.0 +. float (level + m_pan) *. ww in
3624 let (first, last) = minfo.(row) in
3625 let prefix = String.sub s 0 first in
3626 let suffix = String.sub s first (last - first) in
3627 let w1 = measurestr fstate.fontsize prefix in
3628 let w2 = measurestr fstate.fontsize suffix in
3629 let x0 = x +. w1
3630 and y0 = (float (y+2)) in
3631 let x1 = x0 +. w2
3632 and y1 = (float (y+fs+3)) in
3633 filledrect x0 y0 x1 y1;
3634 loop (row+1)
3638 Gl.disable `texture_2d;
3639 if Array.length minfo > 0 then loop m_first;
3640 Gl.disable `blend;
3642 method updownlevel incr =
3643 let len = source#getitemcount in
3644 let curlevel =
3645 if m_active >= 0 && m_active < len
3646 then snd (source#getitem m_active)
3647 else -1
3649 let rec flow i =
3650 if i = len then i-1 else if i = -1 then 0 else
3651 let _, l = source#getitem i in
3652 if l != curlevel then i else flow (i+incr)
3654 let active = flow m_active in
3655 let first = calcfirst m_first active in
3656 G.postRedisplay "outline updownlevel";
3657 {< m_active = active; m_first = first >}
3659 method private key1 key mask =
3660 let set1 active first qsearch =
3661 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3663 let search active pattern incr =
3664 let active = if active = -1 then m_first else active in
3665 let dosearch re =
3666 let rec loop n =
3667 if n >= 0 && n < source#getitemcount
3668 then (
3669 let s, _ = source#getitem n in
3671 (try ignore (Str.search_forward re s 0); true
3672 with Not_found -> false)
3673 then Some n
3674 else loop (n + incr)
3676 else None
3678 loop active
3681 let re = Str.regexp_case_fold pattern in
3682 dosearch re
3683 with Failure s ->
3684 state.text <- s;
3685 None
3687 let itemcount = source#getitemcount in
3688 let find start incr =
3689 let rec find i =
3690 if i = -1 || i = itemcount
3691 then -1
3692 else (
3693 if source#hasaction i
3694 then i
3695 else find (i + incr)
3698 find start
3700 let set active first =
3701 let first = bound first 0 (itemcount - fstate.maxrows) in
3702 state.text <- "";
3703 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3705 let navigate incr =
3706 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3707 let active, first =
3708 let incr1 = if incr > 0 then 1 else -1 in
3709 if isvisible m_first m_active
3710 then
3711 let next =
3712 let next = m_active + incr in
3713 let next =
3714 if next < 0 || next >= itemcount
3715 then -1
3716 else find next incr1
3718 if abs (m_active - next) > fstate.maxrows
3719 then -1
3720 else next
3722 if next = -1
3723 then
3724 let first = m_first + incr in
3725 let first = bound first 0 (itemcount - fstate.maxrows) in
3726 let next =
3727 let next = m_active + incr in
3728 let next = bound next 0 (itemcount - 1) in
3729 find next ~-incr1
3731 let active =
3732 if next = -1
3733 then m_active
3734 else (
3735 if isvisible first next
3736 then next
3737 else m_active
3740 active, first
3741 else
3742 let first = min next m_first in
3743 let first =
3744 if abs (next - first) > fstate.maxrows
3745 then first + incr
3746 else first
3748 next, first
3749 else
3750 let first = m_first + incr in
3751 let first = bound first 0 (itemcount - 1) in
3752 let active =
3753 let next = m_active + incr in
3754 let next = bound next 0 (itemcount - 1) in
3755 let next = find next incr1 in
3756 let active =
3757 if next = -1 || abs (m_active - first) > fstate.maxrows
3758 then (
3759 let active = if m_active = -1 then next else m_active in
3760 active
3762 else next
3764 if isvisible first active
3765 then active
3766 else -1
3768 active, first
3770 G.postRedisplay "listview navigate";
3771 set active first;
3773 match key with
3774 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3775 let incr = if key = 0x72 then -1 else 1 in
3776 let active, first =
3777 match search (m_active + incr) m_qsearch incr with
3778 | None ->
3779 state.text <- m_qsearch ^ " [not found]";
3780 m_active, m_first
3781 | Some active ->
3782 state.text <- m_qsearch;
3783 active, firstof m_first active
3785 G.postRedisplay "listview ctrl-r/s";
3786 set1 active first m_qsearch;
3788 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3789 if m_active >= 0 && m_active < source#getitemcount
3790 then (
3791 let s, _ = source#getitem m_active in
3792 selstring s;
3794 coe self
3796 | 0xff08 -> (* backspace *)
3797 if emptystr m_qsearch
3798 then coe self
3799 else (
3800 let qsearch = withoutlastutf8 m_qsearch in
3801 if emptystr qsearch
3802 then (
3803 state.text <- "";
3804 G.postRedisplay "listview empty qsearch";
3805 set1 m_active m_first "";
3807 else
3808 let active, first =
3809 match search m_active qsearch ~-1 with
3810 | None ->
3811 state.text <- qsearch ^ " [not found]";
3812 m_active, m_first
3813 | Some active ->
3814 state.text <- qsearch;
3815 active, firstof m_first active
3817 G.postRedisplay "listview backspace qsearch";
3818 set1 active first qsearch
3821 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3822 let pattern = m_qsearch ^ toutf8 key in
3823 let active, first =
3824 match search m_active pattern 1 with
3825 | None ->
3826 state.text <- pattern ^ " [not found]";
3827 m_active, m_first
3828 | Some active ->
3829 state.text <- pattern;
3830 active, firstof m_first active
3832 G.postRedisplay "listview qsearch add";
3833 set1 active first pattern;
3835 | 0xff1b -> (* escape *)
3836 state.text <- "";
3837 if emptystr m_qsearch
3838 then (
3839 G.postRedisplay "list view escape";
3840 begin
3841 match
3842 source#exit (coe self) true m_active m_first m_pan
3843 with
3844 | None -> m_prev_uioh
3845 | Some uioh -> uioh
3848 else (
3849 G.postRedisplay "list view kill qsearch";
3850 coe {< m_qsearch = "" >}
3853 | 0xff0d | 0xff8d -> (* (kp) enter *)
3854 state.text <- "";
3855 let self = {< m_qsearch = "" >} in
3856 let opt =
3857 G.postRedisplay "listview enter";
3858 if m_active >= 0 && m_active < source#getitemcount
3859 then (
3860 source#exit (coe self) false m_active m_first m_pan;
3862 else (
3863 source#exit (coe self) true m_active m_first m_pan;
3866 begin match opt with
3867 | None -> m_prev_uioh
3868 | Some uioh -> uioh
3871 | 0xff9f | 0xffff -> (* (kp) delete *)
3872 coe self
3874 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3875 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3876 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3877 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3879 | 0xff53 | 0xff98 -> (* (kp) right *)
3880 state.text <- "";
3881 G.postRedisplay "listview right";
3882 coe {< m_pan = m_pan - 1 >}
3884 | 0xff51 | 0xff96 -> (* (kp) left *)
3885 state.text <- "";
3886 G.postRedisplay "listview left";
3887 coe {< m_pan = m_pan + 1 >}
3889 | 0xff50 | 0xff95 -> (* (kp) home *)
3890 let active = find 0 1 in
3891 G.postRedisplay "listview home";
3892 set active 0;
3894 | 0xff57 | 0xff9c -> (* (kp) end *)
3895 let first = max 0 (itemcount - fstate.maxrows) in
3896 let active = find (itemcount - 1) ~-1 in
3897 G.postRedisplay "listview end";
3898 set active first;
3900 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3901 coe self
3903 | _ ->
3904 dolog "listview unknown key %#x" key; coe self
3906 method key key mask =
3907 match state.mode with
3908 | Textentry te -> textentrykeyboard key mask te; coe self
3909 | _ -> self#key1 key mask
3911 method button button down x y _ =
3912 let opt =
3913 match button with
3914 | 1 when x > state.winw - conf.scrollbw ->
3915 G.postRedisplay "listview scroll";
3916 if down
3917 then
3918 let _, position, sh = self#scrollph in
3919 if y > truncate position && y < truncate (position +. sh)
3920 then (
3921 state.mstate <- Mscrolly;
3922 Some (coe self)
3924 else
3925 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3926 let first = truncate (s *. float source#getitemcount) in
3927 let first = min source#getitemcount first in
3928 Some (coe {< m_first = first; m_active = first >})
3929 else (
3930 state.mstate <- Mnone;
3931 Some (coe self);
3933 | 1 when not down ->
3934 begin match self#elemunder y with
3935 | Some n ->
3936 G.postRedisplay "listview click";
3937 source#exit (coe {< m_active = n >}) false n m_first m_pan
3938 | _ ->
3939 Some (coe self)
3941 | n when (n == 4 || n == 5) && not down ->
3942 let len = source#getitemcount in
3943 let first =
3944 if n = 5 && m_first + fstate.maxrows >= len
3945 then
3946 m_first
3947 else
3948 let first = m_first + (if n == 4 then -1 else 1) in
3949 bound first 0 (len - 1)
3951 G.postRedisplay "listview wheel";
3952 Some (coe {< m_first = first >})
3953 | n when (n = 6 || n = 7) && not down ->
3954 let inc = if n = 7 then -1 else 1 in
3955 G.postRedisplay "listview hwheel";
3956 Some (coe {< m_pan = m_pan + inc >})
3957 | _ ->
3958 Some (coe self)
3960 match opt with
3961 | None -> m_prev_uioh
3962 | Some uioh -> uioh
3964 method multiclick _ x y = self#button 1 true x y
3966 method motion _ y =
3967 match state.mstate with
3968 | Mscrolly ->
3969 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3970 let first = truncate (s *. float source#getitemcount) in
3971 let first = min source#getitemcount first in
3972 G.postRedisplay "listview motion";
3973 coe {< m_first = first; m_active = first >}
3974 | _ -> coe self
3976 method pmotion x y =
3977 if x < state.winw - conf.scrollbw
3978 then
3979 let n =
3980 match self#elemunder y with
3981 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3982 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3984 let o =
3985 if n != m_active
3986 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3987 else self
3989 coe o
3990 else (
3991 Wsi.setcursor Wsi.CURSOR_INHERIT;
3992 coe self
3995 method infochanged _ = ()
3997 method scrollpw = (0, 0.0, 0.0)
3998 method scrollph =
3999 let nfs = fstate.fontsize + 1 in
4000 let y = m_first * nfs in
4001 let itemcount = source#getitemcount in
4002 let maxi = max 0 (itemcount - fstate.maxrows) in
4003 let maxy = maxi * nfs in
4004 let p, h = scrollph y maxy in
4005 conf.scrollbw, p, h
4007 method modehash = modehash
4008 method eformsgs = false
4009 end;;
4011 class outlinelistview ~source =
4012 let settext autonarrow s =
4013 if autonarrow
4014 then
4015 let ss = source#statestr in
4016 state.text <-
4017 if emptystr ss
4018 then "[" ^ s ^ "]"
4019 else "{" ^ ss ^ "} [" ^ s ^ "]"
4020 else state.text <- s
4022 object (self)
4023 inherit listview
4024 ~source:(source :> lvsource)
4025 ~trusted:false
4026 ~modehash:(findkeyhash conf "outline")
4027 as super
4029 val m_autonarrow = false
4031 method display = super#display
4033 method key key mask =
4034 let maxrows =
4035 if emptystr state.text
4036 then fstate.maxrows
4037 else fstate.maxrows - 2
4039 let calcfirst first active =
4040 if active > first
4041 then
4042 let rows = active - first in
4043 if rows > maxrows then active - maxrows else first
4044 else active
4046 let navigate incr =
4047 let active = m_active + incr in
4048 let active = bound active 0 (source#getitemcount - 1) in
4049 let first = calcfirst m_first active in
4050 G.postRedisplay "outline navigate";
4051 coe {< m_active = active; m_first = first >}
4053 let navscroll first =
4054 let active =
4055 let dist = m_active - first in
4056 if dist < 0
4057 then first
4058 else (
4059 if dist < maxrows
4060 then m_active
4061 else first + maxrows
4064 G.postRedisplay "outline navscroll";
4065 coe {< m_first = first; m_active = active >}
4067 let ctrl = Wsi.withctrl mask in
4068 match key with
4069 | 97 when ctrl -> (* ctrl-a *)
4070 let text =
4071 if m_autonarrow
4072 then (source#denarrow; "")
4073 else (
4074 let pattern = source#renarrow in
4075 if nonemptystr m_qsearch
4076 then (source#narrow m_qsearch; m_qsearch)
4077 else pattern
4080 settext (not m_autonarrow) text;
4081 G.postRedisplay "toggle auto narrowing";
4082 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
4084 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
4085 settext true "";
4086 G.postRedisplay "toggle auto narrowing";
4087 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
4089 | 110 when ctrl -> (* ctrl-n *)
4090 source#narrow m_qsearch;
4091 if not m_autonarrow
4092 then source#add_narrow_pattern m_qsearch;
4093 G.postRedisplay "outline ctrl-n";
4094 coe {< m_first = 0; m_active = 0 >}
4096 | 83 when ctrl -> (* ctrl-S *)
4097 let active = source#calcactive (getanchor ()) in
4098 let first = firstof m_first active in
4099 G.postRedisplay "outline ctrl-s";
4100 coe {< m_first = first; m_active = active >}
4102 | 117 when ctrl -> (* ctrl-u *)
4103 G.postRedisplay "outline ctrl-u";
4104 if m_autonarrow && nonemptystr m_qsearch
4105 then (
4106 ignore (source#renarrow);
4107 settext m_autonarrow "";
4108 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4110 else (
4111 source#del_narrow_pattern;
4112 let pattern = source#renarrow in
4113 let text =
4114 if emptystr pattern then "" else "Narrowed to " ^ pattern
4116 settext m_autonarrow text;
4117 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4120 | 108 when ctrl -> (* ctrl-l *)
4121 let first = max 0 (m_active - (fstate.maxrows / 2)) in
4122 G.postRedisplay "outline ctrl-l";
4123 coe {< m_first = first >}
4125 | 0xff09 when m_autonarrow -> (* tab *)
4126 if nonemptystr m_qsearch
4127 then (
4128 G.postRedisplay "outline list view tab";
4129 source#add_narrow_pattern m_qsearch;
4130 settext true "";
4131 coe {< m_qsearch = "" >}
4133 else coe self
4135 | 0xff1b when m_autonarrow -> (* escape *)
4136 if nonemptystr m_qsearch
4137 then source#add_narrow_pattern m_qsearch;
4138 super#key key mask
4140 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
4141 if nonemptystr m_qsearch
4142 then source#add_narrow_pattern m_qsearch;
4143 super#key key mask
4145 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
4146 let pattern = m_qsearch ^ toutf8 key in
4147 G.postRedisplay "outlinelistview autonarrow add";
4148 source#narrow pattern;
4149 settext true pattern;
4150 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4152 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4153 if emptystr m_qsearch
4154 then coe self
4155 else
4156 let pattern = withoutlastutf8 m_qsearch in
4157 G.postRedisplay "outlinelistview autonarrow backspace";
4158 ignore (source#renarrow);
4159 source#narrow pattern;
4160 settext true pattern;
4161 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4163 | 0xff9f | 0xffff -> (* (kp) delete *)
4164 source#remove m_active;
4165 G.postRedisplay "outline delete";
4166 let active = max 0 (m_active-1) in
4167 coe {< m_first = firstof m_first active;
4168 m_active = active >}
4170 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4171 navscroll (max 0 (m_first - 1))
4173 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4174 navscroll (min (source#getitemcount - 1) (m_first + 1))
4176 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4177 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4178 | 0xff55 | 0xff9a -> (* (kp) prior *)
4179 navigate ~-(fstate.maxrows)
4180 | 0xff56 | 0xff9b -> (* (kp) next *)
4181 navigate fstate.maxrows
4183 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4184 let o =
4185 if ctrl
4186 then (
4187 G.postRedisplay "outline ctrl right";
4188 {< m_pan = m_pan + 1 >}
4190 else self#updownlevel 1
4192 coe o
4194 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4195 let o =
4196 if ctrl
4197 then (
4198 G.postRedisplay "outline ctrl left";
4199 {< m_pan = m_pan - 1 >}
4201 else self#updownlevel ~-1
4203 coe o
4205 | 0xff50 | 0xff95 -> (* (kp) home *)
4206 G.postRedisplay "outline home";
4207 coe {< m_first = 0; m_active = 0 >}
4209 | 0xff57 | 0xff9c -> (* (kp) end *)
4210 let active = source#getitemcount - 1 in
4211 let first = max 0 (active - fstate.maxrows) in
4212 G.postRedisplay "outline end";
4213 coe {< m_active = active; m_first = first >}
4215 | _ -> super#key key mask
4218 let gotounder under =
4219 let getpath filename =
4220 let path =
4221 if nonemptystr filename
4222 then
4223 if Filename.is_relative filename
4224 then
4225 let dir = Filename.dirname state.path in
4226 let dir =
4227 if Filename.is_implicit dir
4228 then Filename.concat (Sys.getcwd ()) dir
4229 else dir
4231 Filename.concat dir filename
4232 else filename
4233 else ""
4235 if Sys.file_exists path
4236 then path
4237 else ""
4239 match under with
4240 | Ulinkgoto (pageno, top) ->
4241 if pageno >= 0
4242 then (
4243 addnav ();
4244 gotopage1 pageno top;
4247 | Ulinkuri s ->
4248 gotouri s
4250 | Uremote (filename, pageno) ->
4251 let path = getpath filename in
4252 if nonemptystr path
4253 then (
4254 if conf.riani
4255 then
4256 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4257 try popen command []
4258 with exn ->
4259 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4260 flush stderr;
4261 else
4262 let anchor = getanchor () in
4263 let ranchor = state.path, state.password, anchor, state.origin in
4264 state.origin <- "";
4265 state.anchor <- (pageno, 0.0, 0.0);
4266 state.ranchors <- ranchor :: state.ranchors;
4267 opendoc path "";
4269 else showtext '!' ("Could not find " ^ filename)
4271 | Uremotedest (filename, destname) ->
4272 let path = getpath filename in
4273 if nonemptystr path
4274 then (
4275 if conf.riani
4276 then
4277 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4278 try popen command []
4279 with exn ->
4280 Printf.eprintf
4281 "failed to execute `%s': %s\n" command (exntos exn);
4282 flush stderr;
4283 else
4284 let anchor = getanchor () in
4285 let ranchor = state.path, state.password, anchor, state.origin in
4286 state.origin <- "";
4287 state.nameddest <- destname;
4288 state.ranchors <- ranchor :: state.ranchors;
4289 opendoc path "";
4291 else showtext '!' ("Could not find " ^ filename)
4293 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4296 let gotooutline (_, _, kind) =
4297 match kind with
4298 | Onone -> ()
4299 | Oanchor anchor ->
4300 let (pageno, y, _) = anchor in
4301 let y = getanchory
4302 (if conf.presentation then (pageno, y, 1.0) else anchor)
4304 addnav ();
4305 gotoghyll y
4306 | Ouri uri -> gotounder (Ulinkuri uri)
4307 | Olaunch cmd -> gotounder (Ulaunch cmd)
4308 | Oremote remote -> gotounder (Uremote remote)
4309 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4312 let outlinesource usebookmarks =
4313 let empty = [||] in
4314 (object (self)
4315 inherit lvsourcebase
4316 val mutable m_items = empty
4317 val mutable m_minfo = empty
4318 val mutable m_orig_items = empty
4319 val mutable m_orig_minfo = empty
4320 val mutable m_narrow_patterns = []
4321 val mutable m_hadremovals = false
4322 val mutable m_gen = -1
4324 method getitemcount =
4325 Array.length m_items + (if m_hadremovals then 1 else 0)
4327 method getitem n =
4328 if n == Array.length m_items && m_hadremovals
4329 then
4330 ("[Confirm removal]", 0)
4331 else
4332 let s, n, _ = m_items.(n) in
4333 (s, n)
4335 method exit ~uioh ~cancel ~active ~first ~pan =
4336 ignore (uioh, first);
4337 let confrimremoval = m_hadremovals && active = Array.length m_items in
4338 let items, minfo =
4339 if m_narrow_patterns = []
4340 then m_orig_items, m_orig_minfo
4341 else m_items, m_minfo
4343 if not cancel
4344 then (
4345 if not confrimremoval
4346 then (
4347 gotooutline m_items.(active);
4348 m_items <- items;
4349 m_minfo <- minfo;
4351 else (
4352 state.bookmarks <- Array.to_list m_items;
4353 m_orig_items <- m_items;
4354 m_orig_minfo <- m_minfo;
4357 else (
4358 m_items <- items;
4359 m_minfo <- minfo;
4361 m_pan <- pan;
4362 None
4364 method hasaction _ = true
4366 method greetmsg =
4367 if Array.length m_items != Array.length m_orig_items
4368 then
4369 let s =
4370 match m_narrow_patterns with
4371 | one :: [] -> one
4372 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4374 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4375 else ""
4377 method statestr =
4378 match m_narrow_patterns with
4379 | [] -> ""
4380 | one :: [] -> one
4381 | head :: _ -> "\xe2\x80\xa6" ^ head
4383 method narrow pattern =
4384 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4385 match reopt with
4386 | None -> ()
4387 | Some re ->
4388 let rec loop accu minfo n =
4389 if n = -1
4390 then (
4391 m_items <- Array.of_list accu;
4392 m_minfo <- Array.of_list minfo;
4394 else
4395 let (s, _, _) as o = m_items.(n) in
4396 let accu, minfo =
4397 let first =
4398 try Str.search_forward re s 0
4399 with Not_found -> -1
4401 if first >= 0
4402 then o :: accu, (first, Str.match_end ()) :: minfo
4403 else accu, minfo
4405 loop accu minfo (n-1)
4407 loop [] [] (Array.length m_items - 1)
4409 method getminfo = m_minfo
4411 method denarrow =
4412 m_orig_items <- (
4413 if usebookmarks
4414 then Array.of_list state.bookmarks
4415 else state.outlines
4417 m_minfo <- m_orig_minfo;
4418 m_items <- m_orig_items
4420 method remove m =
4421 if usebookmarks
4422 then
4423 if m >= 0 && m < Array.length m_items
4424 then (
4425 m_hadremovals <- true;
4426 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4427 let n = if n >= m then n+1 else n in
4428 m_items.(n)
4432 method add_narrow_pattern pattern =
4433 m_narrow_patterns <- pattern :: m_narrow_patterns
4435 method del_narrow_pattern =
4436 match m_narrow_patterns with
4437 | _ :: rest -> m_narrow_patterns <- rest
4438 | [] -> ()
4440 method renarrow =
4441 self#denarrow;
4442 match m_narrow_patterns with
4443 | pattern :: [] -> self#narrow pattern; pattern
4444 | list ->
4445 List.fold_left (fun accu pattern ->
4446 self#narrow pattern;
4447 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4449 method calcactive anchor =
4450 let rely = getanchory anchor in
4451 let rec loop n best bestd =
4452 if n = Array.length m_items
4453 then best
4454 else
4455 let _, _, kind = m_items.(n) in
4456 match kind with
4457 | Oanchor anchor ->
4458 let orely = getanchory anchor in
4459 let d = abs (orely - rely) in
4460 if d < bestd
4461 then loop (n+1) n d
4462 else loop (n+1) best bestd
4463 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
4464 loop (n+1) best bestd
4466 loop 0 ~-1 max_int
4468 method reset anchor items =
4469 m_hadremovals <- false;
4470 if state.gen != m_gen
4471 then (
4472 m_orig_items <- items;
4473 m_items <- items;
4474 m_narrow_patterns <- [];
4475 m_minfo <- empty;
4476 m_orig_minfo <- empty;
4477 m_gen <- state.gen;
4479 else (
4480 if items != m_orig_items
4481 then (
4482 m_orig_items <- items;
4483 if m_narrow_patterns == []
4484 then m_items <- items;
4487 let active = self#calcactive anchor in
4488 m_active <- active;
4489 m_first <- firstof m_first active
4490 end)
4493 let enterselector usebookmarks =
4494 resetmstate ();
4495 let source = outlinesource usebookmarks in
4496 fun errmsg ->
4497 let outlines =
4498 if usebookmarks
4499 then Array.of_list state.bookmarks
4500 else state.outlines
4502 if Array.length outlines = 0
4503 then (
4504 showtext ' ' errmsg;
4506 else (
4507 state.text <- source#greetmsg;
4508 Wsi.setcursor Wsi.CURSOR_INHERIT;
4509 let anchor = getanchor () in
4510 source#reset anchor outlines;
4511 state.uioh <- coe (new outlinelistview ~source);
4512 G.postRedisplay "enter selector";
4516 let enteroutlinemode =
4517 let f = enterselector false in
4518 fun () -> f "Document has no outline";
4521 let enterbookmarkmode =
4522 let f = enterselector true in
4523 fun () -> f "Document has no bookmarks (yet)";
4526 let color_of_string s =
4527 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4528 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4532 let color_to_string (r, g, b) =
4533 let r = truncate (r *. 256.0)
4534 and g = truncate (g *. 256.0)
4535 and b = truncate (b *. 256.0) in
4536 Printf.sprintf "%d/%d/%d" r g b
4539 let irect_of_string s =
4540 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4543 let irect_to_string (x0,y0,x1,y1) =
4544 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4547 let makecheckers () =
4548 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4549 following to say:
4550 converted by Issac Trotts. July 25, 2002 *)
4551 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4552 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4553 let id = GlTex.gen_texture () in
4554 GlTex.bind_texture `texture_2d id;
4555 GlPix.store (`unpack_alignment 1);
4556 GlTex.image2d image;
4557 List.iter (GlTex.parameter ~target:`texture_2d)
4558 [ `mag_filter `nearest; `min_filter `nearest ];
4562 let setcheckers enabled =
4563 match state.texid with
4564 | None ->
4565 if enabled then state.texid <- Some (makecheckers ())
4567 | Some texid ->
4568 if not enabled
4569 then (
4570 GlTex.delete_texture texid;
4571 state.texid <- None;
4575 let int_of_string_with_suffix s =
4576 let l = String.length s in
4577 let s1, shift =
4578 if l > 1
4579 then
4580 let suffix = Char.lowercase s.[l-1] in
4581 match suffix with
4582 | 'k' -> String.sub s 0 (l-1), 10
4583 | 'm' -> String.sub s 0 (l-1), 20
4584 | 'g' -> String.sub s 0 (l-1), 30
4585 | _ -> s, 0
4586 else s, 0
4588 let n = int_of_string s1 in
4589 let m = n lsl shift in
4590 if m < 0 || m < n
4591 then raise (Failure "value too large")
4592 else m
4595 let string_with_suffix_of_int n =
4596 if n = 0
4597 then "0"
4598 else
4599 let units = [(30, "G"); (20, "M"); (10, "K")] in
4600 let prettyint n =
4601 let rec loop s n =
4602 let h = n mod 1000 in
4603 let n = n / 1000 in
4604 if n = 0
4605 then string_of_int h ^ s
4606 else (
4607 let s = Printf.sprintf "_%03d%s" h s in
4608 loop s n
4611 loop "" n
4613 let rec find = function
4614 | [] -> prettyint n
4615 | (shift, suffix) :: rest ->
4616 if (n land ((1 lsl shift) - 1)) = 0
4617 then prettyint (n lsr shift) ^ suffix
4618 else find rest
4620 find units
4623 let fastghyllscroll = (5,1,2);;
4624 let neatghyllscroll = (10,1,9);;
4625 let ghyllscroll_of_string s =
4626 match s with
4627 | "fast" -> Some fastghyllscroll
4628 | "neat" -> Some (10,1,9)
4629 | "" | "none" -> None
4630 | _ ->
4631 let (n,a,b) as nab =
4632 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4633 if n <= a || n <= b || a >= b
4634 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4635 n a b;
4636 Some nab
4639 let ghyllscroll_to_string ((n, a, b) as nab) =
4640 (**) if nab = fastghyllscroll then "fast"
4641 else if nab = neatghyllscroll then "neat"
4642 else Printf.sprintf "%d,%d,%d" n a b;
4645 let describe_location () =
4646 let fn = page_of_y state.y in
4647 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4648 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4649 let percent =
4650 if maxy <= 0
4651 then 100.
4652 else (100. *. (float state.y /. float maxy))
4654 if fn = ln
4655 then
4656 Printf.sprintf "page %d of %d [%.2f%%]"
4657 (fn+1) state.pagecount percent
4658 else
4659 Printf.sprintf
4660 "pages %d-%d of %d [%.2f%%]"
4661 (fn+1) (ln+1) state.pagecount percent
4664 let setpresentationmode v =
4665 let n = page_of_y state.y in
4666 state.anchor <- (n, 0.0, 1.0);
4667 conf.presentation <- v;
4668 if conf.fitmodel = FitPage
4669 then reqlayout conf.angle conf.fitmodel;
4670 represent ();
4673 let enterinfomode =
4674 let btos b = if b then "\xe2\x88\x9a" else "" in
4675 let showextended = ref false in
4676 let leave mode = function
4677 | Confirm -> state.mode <- mode
4678 | Cancel -> state.mode <- mode in
4679 let src =
4680 (object
4681 val mutable m_first_time = true
4682 val mutable m_l = []
4683 val mutable m_a = [||]
4684 val mutable m_prev_uioh = nouioh
4685 val mutable m_prev_mode = View
4687 inherit lvsourcebase
4689 method reset prev_mode prev_uioh =
4690 m_a <- Array.of_list (List.rev m_l);
4691 m_l <- [];
4692 m_prev_mode <- prev_mode;
4693 m_prev_uioh <- prev_uioh;
4694 if m_first_time
4695 then (
4696 let rec loop n =
4697 if n >= Array.length m_a
4698 then ()
4699 else
4700 match m_a.(n) with
4701 | _, _, _, Action _ -> m_active <- n
4702 | _ -> loop (n+1)
4704 loop 0;
4705 m_first_time <- false;
4708 method int name get set =
4709 m_l <-
4710 (name, `int get, 1, Action (
4711 fun u ->
4712 let ondone s =
4713 try set (int_of_string s)
4714 with exn ->
4715 state.text <- Printf.sprintf "bad integer `%s': %s"
4716 s (exntos exn)
4718 state.text <- "";
4719 let te = name ^ ": ", "", None, intentry, ondone, true in
4720 state.mode <- Textentry (te, leave m_prev_mode);
4722 )) :: m_l
4724 method int_with_suffix name get set =
4725 m_l <-
4726 (name, `intws get, 1, Action (
4727 fun u ->
4728 let ondone s =
4729 try set (int_of_string_with_suffix s)
4730 with exn ->
4731 state.text <- Printf.sprintf "bad integer `%s': %s"
4732 s (exntos exn)
4734 state.text <- "";
4735 let te =
4736 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4738 state.mode <- Textentry (te, leave m_prev_mode);
4740 )) :: m_l
4742 method bool ?(offset=1) ?(btos=btos) name get set =
4743 m_l <-
4744 (name, `bool (btos, get), offset, Action (
4745 fun u ->
4746 let v = get () in
4747 set (not v);
4749 )) :: m_l
4751 method color name get set =
4752 m_l <-
4753 (name, `color get, 1, Action (
4754 fun u ->
4755 let invalid = (nan, nan, nan) in
4756 let ondone s =
4757 let c =
4758 try color_of_string s
4759 with exn ->
4760 state.text <- Printf.sprintf "bad color `%s': %s"
4761 s (exntos exn);
4762 invalid
4764 if c <> invalid
4765 then set c;
4767 let te = name ^ ": ", "", None, textentry, ondone, true in
4768 state.text <- color_to_string (get ());
4769 state.mode <- Textentry (te, leave m_prev_mode);
4771 )) :: m_l
4773 method string name get set =
4774 m_l <-
4775 (name, `string get, 1, Action (
4776 fun u ->
4777 let ondone s = set s in
4778 let te = name ^ ": ", "", None, textentry, ondone, true in
4779 state.mode <- Textentry (te, leave m_prev_mode);
4781 )) :: m_l
4783 method colorspace name get set =
4784 m_l <-
4785 (name, `string get, 1, Action (
4786 fun _ ->
4787 let source =
4788 (object
4789 inherit lvsourcebase
4791 initializer
4792 m_active <- CSTE.to_int conf.colorspace;
4793 m_first <- 0;
4795 method getitemcount =
4796 Array.length CSTE.names
4797 method getitem n =
4798 (CSTE.names.(n), 0)
4799 method exit ~uioh ~cancel ~active ~first ~pan =
4800 ignore (uioh, first, pan);
4801 if not cancel then set active;
4802 None
4803 method hasaction _ = true
4804 end)
4806 state.text <- "";
4807 let modehash = findkeyhash conf "info" in
4808 coe (new listview ~source ~trusted:true ~modehash)
4809 )) :: m_l
4811 method paxmark name get set =
4812 m_l <-
4813 (name, `string get, 1, Action (
4814 fun _ ->
4815 let source =
4816 (object
4817 inherit lvsourcebase
4819 initializer
4820 m_active <- MTE.to_int conf.paxmark;
4821 m_first <- 0;
4823 method getitemcount = Array.length MTE.names
4824 method getitem n = (MTE.names.(n), 0)
4825 method exit ~uioh ~cancel ~active ~first ~pan =
4826 ignore (uioh, first, pan);
4827 if not cancel then set active;
4828 None
4829 method hasaction _ = true
4830 end)
4832 state.text <- "";
4833 let modehash = findkeyhash conf "info" in
4834 coe (new listview ~source ~trusted:true ~modehash)
4835 )) :: m_l
4837 method fitmodel name get set =
4838 m_l <-
4839 (name, `string get, 1, Action (
4840 fun _ ->
4841 let source =
4842 (object
4843 inherit lvsourcebase
4845 initializer
4846 m_active <- FMTE.to_int conf.fitmodel;
4847 m_first <- 0;
4849 method getitemcount = Array.length FMTE.names
4850 method getitem n = (FMTE.names.(n), 0)
4851 method exit ~uioh ~cancel ~active ~first ~pan =
4852 ignore (uioh, first, pan);
4853 if not cancel then set active;
4854 None
4855 method hasaction _ = true
4856 end)
4858 state.text <- "";
4859 let modehash = findkeyhash conf "info" in
4860 coe (new listview ~source ~trusted:true ~modehash)
4861 )) :: m_l
4863 method caption s offset =
4864 m_l <- (s, `empty, offset, Noaction) :: m_l
4866 method caption2 s f offset =
4867 m_l <- (s, `string f, offset, Noaction) :: m_l
4869 method getitemcount = Array.length m_a
4871 method getitem n =
4872 let tostr = function
4873 | `int f -> string_of_int (f ())
4874 | `intws f -> string_with_suffix_of_int (f ())
4875 | `string f -> f ()
4876 | `color f -> color_to_string (f ())
4877 | `bool (btos, f) -> btos (f ())
4878 | `empty -> ""
4880 let name, t, offset, _ = m_a.(n) in
4881 ((let s = tostr t in
4882 if nonemptystr s
4883 then Printf.sprintf "%s\t%s" name s
4884 else name),
4885 offset)
4887 method exit ~uioh ~cancel ~active ~first ~pan =
4888 let uiohopt =
4889 if not cancel
4890 then (
4891 let uioh =
4892 match m_a.(active) with
4893 | _, _, _, Action f -> f uioh
4894 | _ -> uioh
4896 Some uioh
4898 else None
4900 m_active <- active;
4901 m_first <- first;
4902 m_pan <- pan;
4903 uiohopt
4905 method hasaction n =
4906 match m_a.(n) with
4907 | _, _, _, Action _ -> true
4908 | _ -> false
4909 end)
4911 let rec fillsrc prevmode prevuioh =
4912 let sep () = src#caption "" 0 in
4913 let colorp name get set =
4914 src#string name
4915 (fun () -> color_to_string (get ()))
4916 (fun v ->
4918 let c = color_of_string v in
4919 set c
4920 with exn ->
4921 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4924 let oldmode = state.mode in
4925 let birdseye = isbirdseye state.mode in
4927 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4929 src#bool "presentation mode"
4930 (fun () -> conf.presentation)
4931 (fun v -> setpresentationmode v);
4933 src#bool "ignore case in searches"
4934 (fun () -> conf.icase)
4935 (fun v -> conf.icase <- v);
4937 src#bool "preload"
4938 (fun () -> conf.preload)
4939 (fun v -> conf.preload <- v);
4941 src#bool "highlight links"
4942 (fun () -> conf.hlinks)
4943 (fun v -> conf.hlinks <- v);
4945 src#bool "under info"
4946 (fun () -> conf.underinfo)
4947 (fun v -> conf.underinfo <- v);
4949 src#bool "persistent bookmarks"
4950 (fun () -> conf.savebmarks)
4951 (fun v -> conf.savebmarks <- v);
4953 src#fitmodel "fit model"
4954 (fun () -> FMTE.to_string conf.fitmodel)
4955 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4957 src#bool "trim margins"
4958 (fun () -> conf.trimmargins)
4959 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4961 src#bool "persistent location"
4962 (fun () -> conf.jumpback)
4963 (fun v -> conf.jumpback <- v);
4965 sep ();
4966 src#int "inter-page space"
4967 (fun () -> conf.interpagespace)
4968 (fun n ->
4969 conf.interpagespace <- n;
4970 docolumns conf.columns;
4971 let pageno, py =
4972 match state.layout with
4973 | [] -> 0, 0
4974 | l :: _ ->
4975 l.pageno, l.pagey
4977 state.maxy <- calcheight ();
4978 let y = getpagey pageno in
4979 gotoy (y + py)
4982 src#int "page bias"
4983 (fun () -> conf.pagebias)
4984 (fun v -> conf.pagebias <- v);
4986 src#int "scroll step"
4987 (fun () -> conf.scrollstep)
4988 (fun n -> conf.scrollstep <- n);
4990 src#int "horizontal scroll step"
4991 (fun () -> conf.hscrollstep)
4992 (fun v -> conf.hscrollstep <- v);
4994 src#int "auto scroll step"
4995 (fun () ->
4996 match state.autoscroll with
4997 | Some step -> step
4998 | _ -> conf.autoscrollstep)
4999 (fun n ->
5000 let n = boundastep state.winh n in
5001 if state.autoscroll <> None
5002 then state.autoscroll <- Some n;
5003 conf.autoscrollstep <- n);
5005 src#int "zoom"
5006 (fun () -> truncate (conf.zoom *. 100.))
5007 (fun v -> setzoom ((float v) /. 100.));
5009 src#int "rotation"
5010 (fun () -> conf.angle)
5011 (fun v -> reqlayout v conf.fitmodel);
5013 src#int "scroll bar width"
5014 (fun () -> conf.scrollbw)
5015 (fun v ->
5016 conf.scrollbw <- v;
5017 reshape state.winw state.winh;
5020 src#int "scroll handle height"
5021 (fun () -> conf.scrollh)
5022 (fun v -> conf.scrollh <- v;);
5024 src#int "thumbnail width"
5025 (fun () -> conf.thumbw)
5026 (fun v ->
5027 conf.thumbw <- min 4096 v;
5028 match oldmode with
5029 | Birdseye beye ->
5030 leavebirdseye beye false;
5031 enterbirdseye ()
5032 | _ -> ()
5035 let mode = state.mode in
5036 src#string "columns"
5037 (fun () ->
5038 match conf.columns with
5039 | Csingle _ -> "1"
5040 | Cmulti (multi, _) -> multicolumns_to_string multi
5041 | Csplit (count, _) -> "-" ^ string_of_int count
5043 (fun v ->
5044 let n, a, b = multicolumns_of_string v in
5045 setcolumns mode n a b);
5047 sep ();
5048 src#caption "Pixmap cache" 0;
5049 src#int_with_suffix "size (advisory)"
5050 (fun () -> conf.memlimit)
5051 (fun v -> conf.memlimit <- v);
5053 src#caption2 "used"
5054 (fun () -> Printf.sprintf "%s bytes, %d tiles"
5055 (string_with_suffix_of_int state.memused)
5056 (Hashtbl.length state.tilemap)) 1;
5058 sep ();
5059 src#caption "Layout" 0;
5060 src#caption2 "Dimension"
5061 (fun () ->
5062 Printf.sprintf "%dx%d (virtual %dx%d)"
5063 state.winw state.winh
5064 state.w state.maxy)
5066 if conf.debug
5067 then
5068 src#caption2 "Position" (fun () ->
5069 Printf.sprintf "%dx%d" state.x state.y
5071 else
5072 src#caption2 "Position" (fun () -> describe_location ()) 1
5075 sep ();
5076 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
5077 "Save these parameters as global defaults at exit"
5078 (fun () -> conf.bedefault)
5079 (fun v -> conf.bedefault <- v)
5082 sep ();
5083 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
5084 src#bool ~offset:0 ~btos "Extended parameters"
5085 (fun () -> !showextended)
5086 (fun v -> showextended := v; fillsrc prevmode prevuioh);
5087 if !showextended
5088 then (
5089 src#bool "checkers"
5090 (fun () -> conf.checkers)
5091 (fun v -> conf.checkers <- v; setcheckers v);
5092 src#bool "update cursor"
5093 (fun () -> conf.updatecurs)
5094 (fun v -> conf.updatecurs <- v);
5095 src#bool "verbose"
5096 (fun () -> conf.verbose)
5097 (fun v -> conf.verbose <- v);
5098 src#bool "invert colors"
5099 (fun () -> conf.invert)
5100 (fun v -> conf.invert <- v);
5101 src#bool "max fit"
5102 (fun () -> conf.maxhfit)
5103 (fun v -> conf.maxhfit <- v);
5104 src#bool "redirect stderr"
5105 (fun () -> conf.redirectstderr)
5106 (fun v -> conf.redirectstderr <- v; redirectstderr ());
5107 src#bool "pax mode"
5108 (fun () -> conf.pax != None)
5109 (fun v ->
5110 if v
5111 then conf.pax <- Some (ref (now (), 0, 0))
5112 else conf.pax <- None);
5113 src#string "uri launcher"
5114 (fun () -> conf.urilauncher)
5115 (fun v -> conf.urilauncher <- v);
5116 src#string "path launcher"
5117 (fun () -> conf.pathlauncher)
5118 (fun v -> conf.pathlauncher <- v);
5119 src#string "tile size"
5120 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
5121 (fun v ->
5123 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
5124 conf.tilew <- max 64 w;
5125 conf.tileh <- max 64 h;
5126 flushtiles ();
5127 with exn ->
5128 state.text <- Printf.sprintf "bad tile size `%s': %s"
5129 v (exntos exn)
5131 src#int "texture count"
5132 (fun () -> conf.texcount)
5133 (fun v ->
5134 if realloctexts v
5135 then conf.texcount <- v
5136 else showtext '!' " Failed to set texture count please retry later"
5138 src#int "slice height"
5139 (fun () -> conf.sliceheight)
5140 (fun v ->
5141 conf.sliceheight <- v;
5142 wcmd "sliceh %d" conf.sliceheight;
5144 src#int "anti-aliasing level"
5145 (fun () -> conf.aalevel)
5146 (fun v ->
5147 conf.aalevel <- bound v 0 8;
5148 state.anchor <- getanchor ();
5149 opendoc state.path state.password;
5151 src#string "page scroll scaling factor"
5152 (fun () -> string_of_float conf.pgscale)
5153 (fun v ->
5155 let s = float_of_string v in
5156 conf.pgscale <- s
5157 with exn ->
5158 state.text <- Printf.sprintf
5159 "bad page scroll scaling factor `%s': %s" v (exntos exn)
5162 src#int "ui font size"
5163 (fun () -> fstate.fontsize)
5164 (fun v -> setfontsize (bound v 5 100));
5165 src#int "hint font size"
5166 (fun () -> conf.hfsize)
5167 (fun v -> conf.hfsize <- bound v 5 100);
5168 colorp "background color"
5169 (fun () -> conf.bgcolor)
5170 (fun v -> conf.bgcolor <- v);
5171 src#bool "crop hack"
5172 (fun () -> conf.crophack)
5173 (fun v -> conf.crophack <- v);
5174 src#string "trim fuzz"
5175 (fun () -> irect_to_string conf.trimfuzz)
5176 (fun v ->
5178 conf.trimfuzz <- irect_of_string v;
5179 if conf.trimmargins
5180 then settrim true conf.trimfuzz;
5181 with exn ->
5182 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
5184 src#string "throttle"
5185 (fun () ->
5186 match conf.maxwait with
5187 | None -> "show place holder if page is not ready"
5188 | Some time ->
5189 if time = infinity
5190 then "wait for page to fully render"
5191 else
5192 "wait " ^ string_of_float time
5193 ^ " seconds before showing placeholder"
5195 (fun v ->
5197 let f = float_of_string v in
5198 if f <= 0.0
5199 then conf.maxwait <- None
5200 else conf.maxwait <- Some f
5201 with exn ->
5202 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
5204 src#string "ghyll scroll"
5205 (fun () ->
5206 match conf.ghyllscroll with
5207 | None -> ""
5208 | Some nab -> ghyllscroll_to_string nab
5210 (fun v ->
5211 try conf.ghyllscroll <- ghyllscroll_of_string v
5212 with exn ->
5213 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
5215 src#string "selection command"
5216 (fun () -> conf.selcmd)
5217 (fun v -> conf.selcmd <- v);
5218 src#string "synctex command"
5219 (fun () -> conf.stcmd)
5220 (fun v -> conf.stcmd <- v);
5221 src#string "pax command"
5222 (fun () -> conf.paxcmd)
5223 (fun v -> conf.paxcmd <- v);
5224 src#colorspace "color space"
5225 (fun () -> CSTE.to_string conf.colorspace)
5226 (fun v ->
5227 conf.colorspace <- CSTE.of_int v;
5228 wcmd "cs %d" v;
5229 load state.layout;
5231 src#paxmark "pax mark method"
5232 (fun () -> MTE.to_string conf.paxmark)
5233 (fun v -> conf.paxmark <- MTE.of_int v);
5234 if pbousable ()
5235 then
5236 src#bool "use PBO"
5237 (fun () -> conf.usepbo)
5238 (fun v -> conf.usepbo <- v);
5239 src#bool "mouse wheel scrolls pages"
5240 (fun () -> conf.wheelbypage)
5241 (fun v -> conf.wheelbypage <- v);
5242 src#bool "open remote links in a new instance"
5243 (fun () -> conf.riani)
5244 (fun v -> conf.riani <- v);
5247 sep ();
5248 src#caption "Document" 0;
5249 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
5250 src#caption2 "Pages"
5251 (fun () -> string_of_int state.pagecount) 1;
5252 src#caption2 "Dimensions"
5253 (fun () -> string_of_int (List.length state.pdims)) 1;
5254 if conf.trimmargins
5255 then (
5256 sep ();
5257 src#caption "Trimmed margins" 0;
5258 src#caption2 "Dimensions"
5259 (fun () -> string_of_int (List.length state.pdims)) 1;
5262 sep ();
5263 src#caption "OpenGL" 0;
5264 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
5265 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
5267 sep ();
5268 src#caption "Location" 0;
5269 if nonemptystr state.origin
5270 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
5271 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5273 src#reset prevmode prevuioh;
5275 fun () ->
5276 state.text <- "";
5277 resetmstate ();
5278 let prevmode = state.mode
5279 and prevuioh = state.uioh in
5280 fillsrc prevmode prevuioh;
5281 let source = (src :> lvsource) in
5282 let modehash = findkeyhash conf "info" in
5283 state.uioh <- coe (object (self)
5284 inherit listview ~source ~trusted:true ~modehash as super
5285 val mutable m_prevmemused = 0
5286 method infochanged = function
5287 | Memused ->
5288 if m_prevmemused != state.memused
5289 then (
5290 m_prevmemused <- state.memused;
5291 G.postRedisplay "memusedchanged";
5293 | Pdim -> G.postRedisplay "pdimchanged"
5294 | Docinfo -> fillsrc prevmode prevuioh
5296 method key key mask =
5297 if not (Wsi.withctrl mask)
5298 then
5299 match key with
5300 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5301 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5302 | _ -> super#key key mask
5303 else super#key key mask
5304 end);
5305 G.postRedisplay "info";
5308 let enterhelpmode =
5309 let source =
5310 (object
5311 inherit lvsourcebase
5312 method getitemcount = Array.length state.help
5313 method getitem n =
5314 let s, l, _ = state.help.(n) in
5315 (s, l)
5317 method exit ~uioh ~cancel ~active ~first ~pan =
5318 let optuioh =
5319 if not cancel
5320 then (
5321 match state.help.(active) with
5322 | _, _, Action f -> Some (f uioh)
5323 | _ -> Some (uioh)
5325 else None
5327 m_active <- active;
5328 m_first <- first;
5329 m_pan <- pan;
5330 optuioh
5332 method hasaction n =
5333 match state.help.(n) with
5334 | _, _, Action _ -> true
5335 | _ -> false
5337 initializer
5338 m_active <- -1
5339 end)
5340 in fun () ->
5341 let modehash = findkeyhash conf "help" in
5342 resetmstate ();
5343 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5344 G.postRedisplay "help";
5347 let entermsgsmode =
5348 let msgsource =
5349 let re = Str.regexp "[\r\n]" in
5350 (object
5351 inherit lvsourcebase
5352 val mutable m_items = [||]
5354 method getitemcount = 1 + Array.length m_items
5356 method getitem n =
5357 if n = 0
5358 then "[Clear]", 0
5359 else m_items.(n-1), 0
5361 method exit ~uioh ~cancel ~active ~first ~pan =
5362 ignore uioh;
5363 if not cancel
5364 then (
5365 if active = 0
5366 then Buffer.clear state.errmsgs;
5368 m_active <- active;
5369 m_first <- first;
5370 m_pan <- pan;
5371 None
5373 method hasaction n =
5374 n = 0
5376 method reset =
5377 state.newerrmsgs <- false;
5378 let l = Str.split re (Buffer.contents state.errmsgs) in
5379 m_items <- Array.of_list l
5381 initializer
5382 m_active <- 0
5383 end)
5384 in fun () ->
5385 state.text <- "";
5386 resetmstate ();
5387 msgsource#reset;
5388 let source = (msgsource :> lvsource) in
5389 let modehash = findkeyhash conf "listview" in
5390 state.uioh <- coe (object
5391 inherit listview ~source ~trusted:false ~modehash as super
5392 method display =
5393 if state.newerrmsgs
5394 then msgsource#reset;
5395 super#display
5396 end);
5397 G.postRedisplay "msgs";
5400 let quickbookmark ?title () =
5401 match state.layout with
5402 | [] -> ()
5403 | l :: _ ->
5404 let title =
5405 match title with
5406 | None ->
5407 let sec = Unix.gettimeofday () in
5408 let tm = Unix.localtime sec in
5409 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5410 (l.pageno+1)
5411 tm.Unix.tm_mday
5412 tm.Unix.tm_mon
5413 (tm.Unix.tm_year + 1900)
5414 tm.Unix.tm_hour
5415 tm.Unix.tm_min
5416 | Some title -> title
5418 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5421 let setautoscrollspeed step goingdown =
5422 let incr = max 1 ((abs step) / 2) in
5423 let incr = if goingdown then incr else -incr in
5424 let astep = boundastep state.winh (step + incr) in
5425 state.autoscroll <- Some astep;
5428 let canpan () =
5429 match conf.columns with
5430 | Csplit _ -> true
5431 | _ -> state.x != 0 || conf.zoom > 1.0
5434 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5436 let existsinrow pageno (columns, coverA, coverB) p =
5437 let last = ((pageno - coverA) mod columns) + columns in
5438 let rec any = function
5439 | [] -> false
5440 | l :: rest ->
5441 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5442 then p l
5443 else (
5444 if not (p l)
5445 then (if l.pageno = last then false else any rest)
5446 else true
5449 any state.layout
5452 let nextpage () =
5453 match state.layout with
5454 | [] ->
5455 let pageno = page_of_y state.y in
5456 gotoghyll (getpagey (pageno+1))
5457 | l :: rest ->
5458 match conf.columns with
5459 | Csingle _ ->
5460 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5461 then
5462 let y = clamp (pgscale state.winh) in
5463 gotoghyll y
5464 else
5465 let pageno = min (l.pageno+1) (state.pagecount-1) in
5466 gotoghyll (getpagey pageno)
5467 | Cmulti ((c, _, _) as cl, _) ->
5468 if conf.presentation
5469 && (existsinrow l.pageno cl
5470 (fun l -> l.pageh > l.pagey + l.pagevh))
5471 then
5472 let y = clamp (pgscale state.winh) in
5473 gotoghyll y
5474 else
5475 let pageno = min (l.pageno+c) (state.pagecount-1) in
5476 gotoghyll (getpagey pageno)
5477 | Csplit (n, _) ->
5478 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5479 then
5480 let pagey, pageh = getpageyh l.pageno in
5481 let pagey = pagey + pageh * l.pagecol in
5482 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5483 gotoghyll (pagey + pageh + ips)
5486 let prevpage () =
5487 match state.layout with
5488 | [] ->
5489 let pageno = page_of_y state.y in
5490 gotoghyll (getpagey (pageno-1))
5491 | l :: _ ->
5492 match conf.columns with
5493 | Csingle _ ->
5494 if conf.presentation && l.pagey != 0
5495 then
5496 gotoghyll (clamp (pgscale ~-(state.winh)))
5497 else
5498 let pageno = max 0 (l.pageno-1) in
5499 gotoghyll (getpagey pageno)
5500 | Cmulti ((c, _, coverB) as cl, _) ->
5501 if conf.presentation &&
5502 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5503 then
5504 gotoghyll (clamp (pgscale ~-(state.winh)))
5505 else
5506 let decr =
5507 if l.pageno = state.pagecount - coverB
5508 then 1
5509 else c
5511 let pageno = max 0 (l.pageno-decr) in
5512 gotoghyll (getpagey pageno)
5513 | Csplit (n, _) ->
5514 let y =
5515 if l.pagecol = 0
5516 then
5517 if l.pageno = 0
5518 then l.pagey
5519 else
5520 let pageno = max 0 (l.pageno-1) in
5521 let pagey, pageh = getpageyh pageno in
5522 pagey + (n-1)*pageh
5523 else
5524 let pagey, pageh = getpageyh l.pageno in
5525 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5527 gotoghyll y
5530 let viewkeyboard key mask =
5531 let enttext te =
5532 let mode = state.mode in
5533 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5534 state.text <- "";
5535 enttext ();
5536 G.postRedisplay "view:enttext"
5538 let ctrl = Wsi.withctrl mask in
5539 let key =
5540 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5542 match key with
5543 | 81 -> (* Q *)
5544 exit 0
5546 | 0xff63 -> (* insert *)
5547 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5548 then (
5549 state.mode <- LinkNav (Ltgendir 0);
5550 gotoy state.y;
5552 else showtext '!' "Keyboard link navigation does not work under rotation"
5554 | 0xff1b | 113 -> (* escape / q *)
5555 begin match state.mstate with
5556 | Mzoomrect _ ->
5557 resetmstate ();
5558 G.postRedisplay "kill zoom rect";
5559 | _ ->
5560 begin match state.mode with
5561 | LinkNav _ ->
5562 state.mode <- View;
5563 G.postRedisplay "esc leave linknav"
5564 | _ ->
5565 match state.ranchors with
5566 | [] -> raise Quit
5567 | (path, password, anchor, origin) :: rest ->
5568 state.ranchors <- rest;
5569 state.anchor <- anchor;
5570 state.origin <- origin;
5571 state.nameddest <- "";
5572 opendoc path password
5573 end;
5574 end;
5576 | 0xff08 -> (* backspace *)
5577 gotoghyll (getnav ~-1)
5579 | 111 -> (* o *)
5580 enteroutlinemode ()
5582 | 117 -> (* u *)
5583 state.rects <- [];
5584 state.text <- "";
5585 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
5586 G.postRedisplay "dehighlight";
5588 | 47 | 63 -> (* / ? *)
5589 let ondone isforw s =
5590 cbput state.hists.pat s;
5591 state.searchpattern <- s;
5592 search s isforw
5594 let s = String.create 1 in
5595 s.[0] <- Char.chr key;
5596 enttext (s, "", Some (onhist state.hists.pat),
5597 textentry, ondone (key = 47), true)
5599 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5600 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5601 setzoom (conf.zoom +. incr)
5603 | 43 | 0xffab -> (* + *)
5604 let ondone s =
5605 let n =
5606 try int_of_string s with exc ->
5607 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5608 max_int
5610 if n != max_int
5611 then (
5612 conf.pagebias <- n;
5613 state.text <- "page bias is now " ^ string_of_int n;
5616 enttext ("page bias: ", "", None, intentry, ondone, true)
5618 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5619 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5620 setzoom (max 0.01 (conf.zoom -. decr))
5622 | 45 | 0xffad -> (* - *)
5623 let ondone msg = state.text <- msg in
5624 enttext (
5625 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5626 optentry state.mode, ondone, true
5629 | 48 when ctrl -> (* ctrl-0 *)
5630 if conf.zoom = 1.0
5631 then (
5632 state.x <- 0;
5633 gotoy state.y
5635 else setzoom 1.0
5637 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5638 let cols =
5639 match conf.columns with
5640 | Csingle _ | Cmulti _ -> 1
5641 | Csplit (n, _) -> n
5643 let h = state.winh -
5644 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5646 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5647 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5648 then setzoom zoom
5650 | 51 when ctrl -> (* ctrl-3 *)
5651 let fm =
5652 match conf.fitmodel with
5653 | FitWidth -> FitProportional
5654 | FitProportional -> FitPage
5655 | FitPage -> FitWidth
5657 state.text <- "fit model: " ^ FMTE.to_string fm;
5658 reqlayout conf.angle fm
5660 | 0xffc6 -> (* f9 *)
5661 togglebirdseye ()
5663 | 57 when ctrl -> (* ctrl-9 *)
5664 togglebirdseye ()
5666 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5667 when not ctrl -> (* 0..9 *)
5668 let ondone s =
5669 let n =
5670 try int_of_string s with exc ->
5671 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5674 if n >= 0
5675 then (
5676 addnav ();
5677 cbput state.hists.pag (string_of_int n);
5678 gotopage1 (n + conf.pagebias - 1) 0;
5681 let pageentry text key =
5682 match Char.unsafe_chr key with
5683 | 'g' -> TEdone text
5684 | _ -> intentry text key
5686 let text = "x" in text.[0] <- Char.chr key;
5687 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5689 | 98 -> (* b *)
5690 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5691 reshape state.winw state.winh;
5693 | 66 -> (* B *)
5694 state.bzoom <- not state.bzoom;
5695 state.rects <- [];
5696 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5698 | 108 -> (* l *)
5699 conf.hlinks <- not conf.hlinks;
5700 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5701 G.postRedisplay "toggle highlightlinks";
5703 | 70 -> (* F *)
5704 state.glinks <- true;
5705 let mode = state.mode in
5706 state.mode <- Textentry (
5707 (":", "", None, linknentry, linkndone gotounder, false),
5708 (fun _ ->
5709 state.glinks <- false;
5710 state.mode <- mode)
5712 state.text <- "";
5713 G.postRedisplay "view:linkent(F)"
5715 | 121 -> (* y *)
5716 state.glinks <- true;
5717 let mode = state.mode in
5718 state.mode <- Textentry (
5720 ":", "", None, linknentry, linkndone (fun under ->
5721 selstring (undertext under);
5722 ), false
5724 fun _ ->
5725 state.glinks <- false;
5726 state.mode <- mode
5728 state.text <- "";
5729 G.postRedisplay "view:linkent"
5731 | 97 -> (* a *)
5732 begin match state.autoscroll with
5733 | Some step ->
5734 conf.autoscrollstep <- step;
5735 state.autoscroll <- None
5736 | None ->
5737 if conf.autoscrollstep = 0
5738 then state.autoscroll <- Some 1
5739 else state.autoscroll <- Some conf.autoscrollstep
5742 | 112 when ctrl -> (* ctrl-p *)
5743 launchpath ()
5745 | 80 -> (* P *)
5746 setpresentationmode (not conf.presentation);
5747 showtext ' ' ("presentation mode " ^
5748 if conf.presentation then "on" else "off");
5750 | 102 -> (* f *)
5751 if List.mem Wsi.Fullscreen state.winstate
5752 then Wsi.reshape conf.cwinw conf.cwinh
5753 else Wsi.fullscreen ()
5755 | 112 | 78 -> (* p|N *)
5756 search state.searchpattern false
5758 | 110 | 0xffc0 -> (* n|F3 *)
5759 search state.searchpattern true
5761 | 116 -> (* t *)
5762 begin match state.layout with
5763 | [] -> ()
5764 | l :: _ ->
5765 gotoghyll (getpagey l.pageno)
5768 | 32 -> (* space *)
5769 nextpage ()
5771 | 0xff9f | 0xffff -> (* delete *)
5772 prevpage ()
5774 | 61 -> (* = *)
5775 showtext ' ' (describe_location ());
5777 | 119 -> (* w *)
5778 begin match state.layout with
5779 | [] -> ()
5780 | l :: _ ->
5781 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5782 G.postRedisplay "w"
5785 | 39 -> (* ' *)
5786 enterbookmarkmode ()
5788 | 104 | 0xffbe -> (* h|F1 *)
5789 enterhelpmode ()
5791 | 105 -> (* i *)
5792 enterinfomode ()
5794 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5795 entermsgsmode ()
5797 | 109 -> (* m *)
5798 let ondone s =
5799 match state.layout with
5800 | l :: _ ->
5801 if nonemptystr s
5802 then
5803 state.bookmarks <-
5804 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5805 | _ -> ()
5807 enttext ("bookmark: ", "", None, textentry, ondone, true)
5809 | 126 -> (* ~ *)
5810 quickbookmark ();
5811 showtext ' ' "Quick bookmark added";
5813 | 122 -> (* z *)
5814 begin match state.layout with
5815 | l :: _ ->
5816 let rect = getpdimrect l.pagedimno in
5817 let w, h =
5818 if conf.crophack
5819 then
5820 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5821 truncate (1.2 *. (rect.(3) -. rect.(0))))
5822 else
5823 (truncate (rect.(1) -. rect.(0)),
5824 truncate (rect.(3) -. rect.(0)))
5826 let w = truncate ((float w)*.conf.zoom)
5827 and h = truncate ((float h)*.conf.zoom) in
5828 if w != 0 && h != 0
5829 then (
5830 state.anchor <- getanchor ();
5831 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5833 G.postRedisplay "z";
5835 | [] -> ()
5838 | 120 -> (* x *)
5839 state.roam ()
5840 | 60 | 62 -> (* < > *)
5841 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5843 | 91 | 93 -> (* [ ] *)
5844 conf.colorscale <-
5845 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5847 G.postRedisplay "brightness";
5849 | 99 when state.mode = View -> (* [alt-]c *)
5850 if Wsi.withalt mask
5851 then (
5852 if conf.zoom > 1.0
5853 then
5854 let m = (wadjsb state.winw - state.w) / 2 in
5855 state.x <- m;
5856 gotoy_and_clear_text state.y
5858 else
5859 let (c, a, b), z =
5860 match state.prevcolumns with
5861 | None -> (1, 0, 0), 1.0
5862 | Some (columns, z) ->
5863 let cab =
5864 match columns with
5865 | Csplit (c, _) -> -c, 0, 0
5866 | Cmulti ((c, a, b), _) -> c, a, b
5867 | Csingle _ -> 1, 0, 0
5869 cab, z
5871 setcolumns View c a b;
5872 setzoom z
5874 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5875 -> (* ctrl-shift- (kp) [up|down] *)
5876 let zoom, x = state.prevzoom in
5877 setzoom zoom;
5878 state.x <- x;
5880 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5881 begin match state.autoscroll with
5882 | None ->
5883 begin match state.mode with
5884 | Birdseye beye -> upbirdseye 1 beye
5885 | _ ->
5886 if ctrl
5887 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5888 else (
5889 if not (Wsi.withshift mask) && conf.presentation
5890 then prevpage ()
5891 else gotoghyll1 true (clamp (-conf.scrollstep))
5894 | Some n ->
5895 setautoscrollspeed n false
5898 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5899 begin match state.autoscroll with
5900 | None ->
5901 begin match state.mode with
5902 | Birdseye beye -> downbirdseye 1 beye
5903 | _ ->
5904 if ctrl
5905 then gotoy_and_clear_text (clamp (state.winh/2))
5906 else (
5907 if not (Wsi.withshift mask) && conf.presentation
5908 then nextpage ()
5909 else gotoghyll1 true (clamp (conf.scrollstep))
5912 | Some n ->
5913 setautoscrollspeed n true
5916 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5917 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5918 if canpan ()
5919 then
5920 let dx =
5921 if ctrl
5922 then state.winw / 2
5923 else conf.hscrollstep
5925 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5926 state.x <- panbound (state.x + dx);
5927 gotoy_and_clear_text state.y
5928 else (
5929 state.text <- "";
5930 G.postRedisplay "left/right"
5933 | 0xff55 | 0xff9a -> (* (kp) prior *)
5934 let y =
5935 if ctrl
5936 then
5937 match state.layout with
5938 | [] -> state.y
5939 | l :: _ -> state.y - l.pagey
5940 else
5941 clamp (pgscale (-state.winh))
5943 gotoghyll y
5945 | 0xff56 | 0xff9b -> (* (kp) next *)
5946 let y =
5947 if ctrl
5948 then
5949 match List.rev state.layout with
5950 | [] -> state.y
5951 | l :: _ -> getpagey l.pageno
5952 else
5953 clamp (pgscale state.winh)
5955 gotoghyll y
5957 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5958 gotoghyll 0
5959 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5960 gotoghyll (clamp state.maxy)
5962 | 0xff53 | 0xff98
5963 when Wsi.withalt mask -> (* alt-(kp) right *)
5964 gotoghyll (getnav 1)
5965 | 0xff51 | 0xff96
5966 when Wsi.withalt mask -> (* alt-(kp) left *)
5967 gotoghyll (getnav ~-1)
5969 | 114 -> (* r *)
5970 reload ()
5972 | 118 when conf.debug -> (* v *)
5973 state.rects <- [];
5974 List.iter (fun l ->
5975 match getopaque l.pageno with
5976 | None -> ()
5977 | Some opaque ->
5978 let x0, y0, x1, y1 = pagebbox opaque in
5979 let a,b = float x0, float y0 in
5980 let c,d = float x1, float y0 in
5981 let e,f = float x1, float y1 in
5982 let h,j = float x0, float y1 in
5983 let rect = (a,b,c,d,e,f,h,j) in
5984 debugrect rect;
5985 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5986 ) state.layout;
5987 G.postRedisplay "v";
5989 | 124 -> (* | *)
5990 let mode = state.mode in
5991 let cmd = ref "" in
5992 let onleave = function
5993 | Cancel -> state.mode <- mode
5994 | Confirm ->
5995 List.iter (fun l ->
5996 match getopaque l.pageno with
5997 | Some opaque -> pipesel opaque !cmd
5998 | None -> ()) state.layout;
5999 state.mode <- mode
6001 let ondone s =
6002 cbput state.hists.sel s;
6003 cmd := s
6005 let te =
6006 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
6008 G.postRedisplay "|";
6009 state.mode <- Textentry (te, onleave);
6011 | _ ->
6012 vlog "huh? %s" (Wsi.keyname key)
6015 let linknavkeyboard key mask linknav =
6016 let getpage pageno =
6017 let rec loop = function
6018 | [] -> None
6019 | l :: _ when l.pageno = pageno -> Some l
6020 | _ :: rest -> loop rest
6021 in loop state.layout
6023 let doexact (pageno, n) =
6024 match getopaque pageno, getpage pageno with
6025 | Some opaque, Some l ->
6026 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
6027 then
6028 let under = getlink opaque n in
6029 G.postRedisplay "link gotounder";
6030 gotounder under;
6031 state.mode <- View;
6032 else
6033 let opt, dir =
6034 match key with
6035 | 0xff50 -> (* home *)
6036 Some (findlink opaque LDfirst), -1
6038 | 0xff57 -> (* end *)
6039 Some (findlink opaque LDlast), 1
6041 | 0xff51 -> (* left *)
6042 Some (findlink opaque (LDleft n)), -1
6044 | 0xff53 -> (* right *)
6045 Some (findlink opaque (LDright n)), 1
6047 | 0xff52 -> (* up *)
6048 Some (findlink opaque (LDup n)), -1
6050 | 0xff54 -> (* down *)
6051 Some (findlink opaque (LDdown n)), 1
6053 | _ -> None, 0
6055 let pwl l dir =
6056 begin match findpwl l.pageno dir with
6057 | Pwlnotfound -> ()
6058 | Pwl pageno ->
6059 let notfound dir =
6060 state.mode <- LinkNav (Ltgendir dir);
6061 let y, h = getpageyh pageno in
6062 let y =
6063 if dir < 0
6064 then y + h - state.winh
6065 else y
6067 gotoy y
6069 begin match getopaque pageno, getpage pageno with
6070 | Some opaque, Some _ ->
6071 let link =
6072 let ld = if dir > 0 then LDfirst else LDlast in
6073 findlink opaque ld
6075 begin match link with
6076 | Lfound m ->
6077 showlinktype (getlink opaque m);
6078 state.mode <- LinkNav (Ltexact (pageno, m));
6079 G.postRedisplay "linknav jpage";
6080 | _ -> notfound dir
6081 end;
6082 | _ -> notfound dir
6083 end;
6084 end;
6086 begin match opt with
6087 | Some Lnotfound -> pwl l dir;
6088 | Some (Lfound m) ->
6089 if m = n
6090 then pwl l dir
6091 else (
6092 let _, y0, _, y1 = getlinkrect opaque m in
6093 if y0 < l.pagey
6094 then gotopage1 l.pageno y0
6095 else (
6096 let d = fstate.fontsize + 1 in
6097 if y1 - l.pagey > l.pagevh - d
6098 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
6099 else G.postRedisplay "linknav";
6101 showlinktype (getlink opaque m);
6102 state.mode <- LinkNav (Ltexact (l.pageno, m));
6105 | None -> viewkeyboard key mask
6106 end;
6107 | _ -> viewkeyboard key mask
6109 if key = 0xff63
6110 then (
6111 state.mode <- View;
6112 G.postRedisplay "leave linknav"
6114 else
6115 match linknav with
6116 | Ltgendir _ -> viewkeyboard key mask
6117 | Ltexact exact -> doexact exact
6120 let keyboard key mask =
6121 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
6122 then wcmd "interrupt"
6123 else state.uioh <- state.uioh#key key mask
6126 let birdseyekeyboard key mask
6127 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
6128 let incr =
6129 match conf.columns with
6130 | Csingle _ -> 1
6131 | Cmulti ((c, _, _), _) -> c
6132 | Csplit _ -> failwith "bird's eye split mode"
6134 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
6135 match key with
6136 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
6137 let y, h = getpageyh pageno in
6138 let top = (state.winh - h) / 2 in
6139 gotoy (max 0 (y - top))
6140 | 0xff0d (* enter *)
6141 | 0xff8d -> leavebirdseye beye false (* kp enter *)
6142 | 0xff1b -> leavebirdseye beye true (* escape *)
6143 | 0xff52 -> upbirdseye incr beye (* up *)
6144 | 0xff54 -> downbirdseye incr beye (* down *)
6145 | 0xff51 -> upbirdseye 1 beye (* left *)
6146 | 0xff53 -> downbirdseye 1 beye (* right *)
6148 | 0xff55 -> (* prior *)
6149 begin match state.layout with
6150 | l :: _ ->
6151 if l.pagey != 0
6152 then (
6153 state.mode <- Birdseye (
6154 oconf, leftx, l.pageno, hooverpageno, anchor
6156 gotopage1 l.pageno 0;
6158 else (
6159 let layout = layout (state.y-state.winh) (pgh state.layout) in
6160 match layout with
6161 | [] -> gotoy (clamp (-state.winh))
6162 | l :: _ ->
6163 state.mode <- Birdseye (
6164 oconf, leftx, l.pageno, hooverpageno, anchor
6166 gotopage1 l.pageno 0
6169 | [] -> gotoy (clamp (-state.winh))
6170 end;
6172 | 0xff56 -> (* next *)
6173 begin match List.rev state.layout with
6174 | l :: _ ->
6175 let layout = layout (state.y + (pgh state.layout)) state.winh in
6176 begin match layout with
6177 | [] ->
6178 let incr = l.pageh - l.pagevh in
6179 if incr = 0
6180 then (
6181 state.mode <-
6182 Birdseye (
6183 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
6185 G.postRedisplay "birdseye pagedown";
6187 else gotoy (clamp (incr + conf.interpagespace*2));
6189 | l :: _ ->
6190 state.mode <-
6191 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
6192 gotopage1 l.pageno 0;
6195 | [] -> gotoy (clamp state.winh)
6196 end;
6198 | 0xff50 -> (* home *)
6199 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
6200 gotopage1 0 0
6202 | 0xff57 -> (* end *)
6203 let pageno = state.pagecount - 1 in
6204 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
6205 if not (pagevisible state.layout pageno)
6206 then
6207 let h =
6208 match List.rev state.pdims with
6209 | [] -> state.winh
6210 | (_, _, h, _) :: _ -> h
6212 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
6213 else G.postRedisplay "birdseye end";
6214 | _ -> viewkeyboard key mask
6217 let drawpage l =
6218 let color =
6219 match state.mode with
6220 | Textentry _ -> scalecolor 0.4
6221 | LinkNav _
6222 | View -> scalecolor 1.0
6223 | Birdseye (_, _, pageno, hooverpageno, _) ->
6224 if l.pageno = hooverpageno
6225 then scalecolor 0.9
6226 else (
6227 if l.pageno = pageno
6228 then scalecolor 1.0
6229 else scalecolor 0.8
6232 drawtiles l color;
6235 let postdrawpage l linkindexbase =
6236 match getopaque l.pageno with
6237 | Some opaque ->
6238 if tileready l l.pagex l.pagey
6239 then
6240 let x = l.pagedispx - l.pagex
6241 and y = l.pagedispy - l.pagey in
6242 let hlmask =
6243 match conf.columns with
6244 | Csingle _ | Cmulti _ ->
6245 (if conf.hlinks then 1 else 0)
6246 + (if state.glinks
6247 && not (isbirdseye state.mode) then 2 else 0)
6248 | _ -> 0
6250 let s =
6251 match state.mode with
6252 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6253 | _ -> ""
6255 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6256 else 0
6257 | _ -> 0
6260 let scrollindicator () =
6261 let sbw, ph, sh = state.uioh#scrollph in
6262 let sbh, pw, sw = state.uioh#scrollpw in
6264 GlDraw.color (0.64, 0.64, 0.64);
6265 filledrect
6266 (float (state.winw - sbw)) 0.
6267 (float state.winw) (float state.winh)
6269 filledrect
6270 0. (float (state.winh - sbh))
6271 (float (wadjsb state.winw - 1)) (float state.winh)
6273 GlDraw.color (0.0, 0.0, 0.0);
6275 filledrect
6276 (float (state.winw - sbw)) ph
6277 (float state.winw) (ph +. sh)
6279 filledrect
6280 pw (float (state.winh - sbh))
6281 (pw +. sw) (float state.winh)
6285 let showsel () =
6286 match state.mstate with
6287 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6290 | Msel ((x0, y0), (x1, y1)) ->
6291 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
6292 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< "", -1, 0, 0) in
6293 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< "", -1, 0, 0) in
6294 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
6297 let showrects = function [] -> () | rects ->
6298 Gl.enable `blend;
6299 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6300 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6301 List.iter
6302 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6303 List.iter (fun l ->
6304 if l.pageno = pageno
6305 then (
6306 let dx = float (l.pagedispx - l.pagex) in
6307 let dy = float (l.pagedispy - l.pagey) in
6308 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6309 Raw.sets_float state.vraw ~pos:0
6310 [| x0+.dx; y0+.dy;
6311 x1+.dx; y1+.dy;
6312 x3+.dx; y3+.dy;
6313 x2+.dx; y2+.dy |];
6314 GlArray.vertex `two state.vraw;
6315 GlArray.draw_arrays `triangle_strip 0 4;
6317 ) state.layout
6318 ) rects
6320 Gl.disable `blend;
6323 let display () =
6324 GlClear.color (scalecolor2 conf.bgcolor);
6325 GlClear.clear [`color];
6326 List.iter drawpage state.layout;
6327 let rects =
6328 match state.mode with
6329 | LinkNav (Ltexact (pageno, linkno)) ->
6330 begin match getopaque pageno with
6331 | Some opaque ->
6332 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6333 (pageno, 5, (
6334 float x0, float y0,
6335 float x1, float y0,
6336 float x1, float y1,
6337 float x0, float y1)
6338 ) :: state.rects
6339 | None -> state.rects
6341 | _ -> state.rects
6343 showrects rects;
6344 let rec postloop linkindexbase = function
6345 | l :: rest ->
6346 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6347 postloop linkindexbase rest
6348 | [] -> ()
6350 showsel ();
6351 postloop 0 state.layout;
6352 state.uioh#display;
6353 begin match state.mstate with
6354 | Mzoomrect ((x0, y0), (x1, y1)) ->
6355 Gl.enable `blend;
6356 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6357 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6358 filledrect (float x0) (float y0) (float x1) (float y1);
6359 Gl.disable `blend;
6360 | _ -> ()
6361 end;
6362 enttext ();
6363 scrollindicator ();
6364 Wsi.swapb ();
6367 let zoomrect x y x1 y1 =
6368 let x0 = min x x1
6369 and x1 = max x x1
6370 and y0 = min y y1 in
6371 gotoy (state.y + y0);
6372 state.anchor <- getanchor ();
6373 let zoom = (float state.w) /. float (x1 - x0) in
6374 let margin =
6375 match conf.fitmodel, conf.columns with
6376 | FitPage, Csplit _ ->
6377 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6379 | _, _ ->
6380 let adjw = wadjsb state.winw in
6381 if state.w < adjw
6382 then (adjw - state.w) / 2
6383 else 0
6385 state.x <- (state.x + margin) - x0;
6386 setzoom zoom;
6387 resetmstate ();
6390 let zoomblock x y =
6391 let g opaque l px py =
6392 match rectofblock opaque px py with
6393 | Some a ->
6394 let x0 = a.(0) -. 20. in
6395 let x1 = a.(1) +. 20. in
6396 let y0 = a.(2) -. 20. in
6397 let zoom = (float state.w) /. (x1 -. x0) in
6398 let pagey = getpagey l.pageno in
6399 gotoy_and_clear_text (pagey + truncate y0);
6400 state.anchor <- getanchor ();
6401 let margin = (state.w - l.pagew)/2 in
6402 state.x <- -truncate x0 - margin;
6403 setzoom zoom;
6404 None
6405 | None -> None
6407 match conf.columns with
6408 | Csplit _ ->
6409 showtext '!' "block zooming does not work properly in split columns mode"
6410 | _ -> onppundermouse g x y ()
6413 let scrollx x =
6414 let winw = wadjsb state.winw - 1 in
6415 let s = float x /. float winw in
6416 let destx = truncate (float (state.w + winw) *. s) in
6417 state.x <- winw - destx;
6418 gotoy_and_clear_text state.y;
6419 state.mstate <- Mscrollx;
6422 let scrolly y =
6423 let s = float y /. float state.winh in
6424 let desty = truncate (float (state.maxy - state.winh) *. s) in
6425 gotoy_and_clear_text desty;
6426 state.mstate <- Mscrolly;
6429 let viewmulticlick clicks x y mask =
6430 let g opaque l px py =
6431 let mark =
6432 match clicks with
6433 | 2 -> Mark_word
6434 | 3 -> Mark_line
6435 | 4 -> Mark_block
6436 | _ -> Mark_page
6438 if markunder opaque px py mark
6439 then (
6440 Some (fun () ->
6441 let dopipe cmd =
6442 match getopaque l.pageno with
6443 | None -> ()
6444 | Some opaque -> pipesel opaque cmd
6446 state.roam <- (fun () -> dopipe conf.paxcmd);
6447 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
6450 else None
6452 G.postRedisplay "viewmulticlick";
6453 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
6456 let canselect () =
6457 match conf.columns with
6458 | Csplit _ -> false
6459 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
6462 let viewmouse button down x y mask =
6463 match button with
6464 | n when (n == 4 || n == 5) && not down ->
6465 if Wsi.withctrl mask
6466 then (
6467 match state.mstate with
6468 | Mzoom (oldn, i) ->
6469 if oldn = n
6470 then (
6471 if i = 2
6472 then
6473 let incr =
6474 match n with
6475 | 5 ->
6476 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6477 | _ ->
6478 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6480 let zoom = conf.zoom -. incr in
6481 setzoom zoom;
6482 state.mstate <- Mzoom (n, 0);
6483 else
6484 state.mstate <- Mzoom (n, i+1);
6486 else state.mstate <- Mzoom (n, 0)
6488 | _ -> state.mstate <- Mzoom (n, 0)
6490 else (
6491 match state.autoscroll with
6492 | Some step -> setautoscrollspeed step (n=4)
6493 | None ->
6494 if conf.wheelbypage || conf.presentation
6495 then (
6496 if n = 4
6497 then prevpage ()
6498 else nextpage ()
6500 else
6501 let incr =
6502 if n = 4
6503 then -conf.scrollstep
6504 else conf.scrollstep
6506 let incr = incr * 2 in
6507 let y = clamp incr in
6508 gotoy_and_clear_text y
6511 | n when (n = 6 || n = 7) && not down && canpan () ->
6512 state.x <-
6513 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6514 gotoy_and_clear_text state.y
6516 | 1 when Wsi.withshift mask ->
6517 state.mstate <- Mnone;
6518 if not down
6519 then (
6520 match unproject x y with
6521 | Some (pageno, ux, uy) ->
6522 let cmd = Printf.sprintf
6523 "%s %s %d %d %d"
6524 conf.stcmd state.path pageno ux uy
6526 popen cmd []
6527 | None -> ()
6530 | 1 when Wsi.withctrl mask ->
6531 if down
6532 then (
6533 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6534 state.mstate <- Mpan (x, y)
6536 else
6537 state.mstate <- Mnone
6539 | 3 ->
6540 if down
6541 then (
6542 Wsi.setcursor Wsi.CURSOR_CYCLE;
6543 let p = (x, y) in
6544 state.mstate <- Mzoomrect (p, p)
6546 else (
6547 match state.mstate with
6548 | Mzoomrect ((x0, y0), _) ->
6549 if abs (x-x0) > 10 && abs (y - y0) > 10
6550 then zoomrect x0 y0 x y
6551 else (
6552 resetmstate ();
6553 G.postRedisplay "kill accidental zoom rect";
6555 | _ ->
6556 resetmstate ()
6559 | 1 when x > state.winw - vscrollw () ->
6560 if down
6561 then
6562 let _, position, sh = state.uioh#scrollph in
6563 if y > truncate position && y < truncate (position +. sh)
6564 then state.mstate <- Mscrolly
6565 else scrolly y
6566 else
6567 state.mstate <- Mnone
6569 | 1 when y > state.winh - hscrollh () ->
6570 if down
6571 then
6572 let _, position, sw = state.uioh#scrollpw in
6573 if x > truncate position && x < truncate (position +. sw)
6574 then state.mstate <- Mscrollx
6575 else scrollx x
6576 else
6577 state.mstate <- Mnone
6579 | 1 when state.bzoom -> if not down then zoomblock x y
6581 | 1 ->
6582 let dest = if down then getunder x y else Unone in
6583 begin match dest with
6584 | Ulinkgoto _
6585 | Ulinkuri _
6586 | Uremote _ | Uremotedest _
6587 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6588 gotounder dest
6590 | Unone when down ->
6591 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6592 state.mstate <- Mpan (x, y);
6594 | Unone | Utext _ ->
6595 if down
6596 then (
6597 if canselect ()
6598 then (
6599 state.mstate <- Msel ((x, y), (x, y));
6600 G.postRedisplay "mouse select";
6603 else (
6604 match state.mstate with
6605 | Mnone -> ()
6607 | Mzoom _ | Mscrollx | Mscrolly ->
6608 state.mstate <- Mnone
6610 | Mzoomrect ((x0, y0), _) ->
6611 zoomrect x0 y0 x y
6613 | Mpan _ ->
6614 Wsi.setcursor Wsi.CURSOR_INHERIT;
6615 state.mstate <- Mnone
6617 | Msel ((x0, y0), (x1, y1)) ->
6618 let rec loop = function
6619 | [] -> ()
6620 | l :: rest ->
6621 let inside =
6622 let a0 = l.pagedispy in
6623 let a1 = a0 + l.pagevh in
6624 let b0 = l.pagedispx in
6625 let b1 = b0 + l.pagevw in
6626 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6627 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6629 if inside
6630 then
6631 match getopaque l.pageno with
6632 | Some opaque ->
6633 let dosel cmd () =
6634 match Ne.res Unix.pipe with
6635 | Ne.Exn exn ->
6636 showtext '!'
6637 (Printf.sprintf
6638 "can not create sel pipe: %s"
6639 (exntos exn));
6640 | Ne.Res (r, w) ->
6641 let clo what fd =
6642 Ne.clo fd (fun msg ->
6643 dolog "%s close failed: %s" what msg)
6645 let popened =
6646 try popen cmd [r, 0; w, -1]; true
6647 with exn ->
6648 dolog "can not execute %S: %s"
6649 cmd (exntos exn);
6650 false
6652 if popened
6653 then (
6654 copysel w opaque;
6655 G.postRedisplay "copysel";
6657 else clo "Msel pipe/w" w;
6658 clo "Msel pipe/r" r;
6660 dosel conf.selcmd ();
6661 state.roam <- dosel conf.paxcmd;
6662 | None -> ()
6663 else loop rest
6665 loop state.layout;
6666 resetmstate ();
6670 | _ -> ()
6673 let birdseyemouse button down x y mask
6674 (conf, leftx, _, hooverpageno, anchor) =
6675 match button with
6676 | 1 when down ->
6677 let rec loop = function
6678 | [] -> ()
6679 | l :: rest ->
6680 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6681 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6682 then (
6683 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6685 else loop rest
6687 loop state.layout
6688 | 3 -> ()
6689 | _ -> viewmouse button down x y mask
6692 let uioh = object
6693 method display = ()
6695 method key key mask =
6696 begin match state.mode with
6697 | Textentry textentry -> textentrykeyboard key mask textentry
6698 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6699 | View -> viewkeyboard key mask
6700 | LinkNav linknav -> linknavkeyboard key mask linknav
6701 end;
6702 state.uioh
6704 method button button bstate x y mask =
6705 begin match state.mode with
6706 | LinkNav _
6707 | View -> viewmouse button bstate x y mask
6708 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6709 | Textentry _ -> ()
6710 end;
6711 state.uioh
6713 method multiclick clicks x y mask =
6714 begin match state.mode with
6715 | LinkNav _
6716 | View -> viewmulticlick clicks x y mask
6717 | Birdseye _
6718 | Textentry _ -> ()
6719 end;
6720 state.uioh
6722 method motion x y =
6723 begin match state.mode with
6724 | Textentry _ -> ()
6725 | View | Birdseye _ | LinkNav _ ->
6726 match state.mstate with
6727 | Mzoom _ | Mnone -> ()
6729 | Mpan (x0, y0) ->
6730 let dx = x - x0
6731 and dy = y0 - y in
6732 state.mstate <- Mpan (x, y);
6733 if canpan ()
6734 then state.x <- panbound (state.x + dx);
6735 let y = clamp dy in
6736 gotoy_and_clear_text y
6738 | Msel (a, _) ->
6739 state.mstate <- Msel (a, (x, y));
6740 G.postRedisplay "motion select";
6742 | Mscrolly ->
6743 let y = min state.winh (max 0 y) in
6744 scrolly y
6746 | Mscrollx ->
6747 let x = min state.winw (max 0 x) in
6748 scrollx x
6750 | Mzoomrect (p0, _) ->
6751 state.mstate <- Mzoomrect (p0, (x, y));
6752 G.postRedisplay "motion zoomrect";
6753 end;
6754 state.uioh
6756 method pmotion x y =
6757 begin match state.mode with
6758 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6759 let rec loop = function
6760 | [] ->
6761 if hooverpageno != -1
6762 then (
6763 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6764 G.postRedisplay "pmotion birdseye no hoover";
6766 | l :: rest ->
6767 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6768 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6769 then (
6770 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6771 G.postRedisplay "pmotion birdseye hoover";
6773 else loop rest
6775 loop state.layout
6777 | Textentry _ -> ()
6779 | LinkNav _
6780 | View ->
6781 match state.mstate with
6782 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6784 | Mnone ->
6785 updateunder x y;
6786 if canselect ()
6787 then
6788 match conf.pax with
6789 | None -> ()
6790 | Some r ->
6791 let past, _, _ = !r in
6792 let now = now () in
6793 let delta = now -. past in
6794 if delta > 0.01
6795 then paxunder x y
6796 else r := (now, x, y)
6797 end;
6798 state.uioh
6800 method infochanged _ = ()
6802 method scrollph =
6803 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6804 let p, h =
6805 if maxy = 0
6806 then 0.0, float state.winh
6807 else scrollph state.y maxy
6809 vscrollw (), p, h
6811 method scrollpw =
6812 let winw = wadjsb state.winw in
6813 let fwinw = float winw in
6814 let sw =
6815 let sw = fwinw /. float state.w in
6816 let sw = fwinw *. sw in
6817 max sw (float conf.scrollh)
6819 let position =
6820 let maxx = state.w + winw in
6821 let x = winw - state.x in
6822 let percent = float x /. float maxx in
6823 (fwinw -. sw) *. percent
6825 hscrollh (), position, sw
6827 method modehash =
6828 let modename =
6829 match state.mode with
6830 | LinkNav _ -> "links"
6831 | Textentry _ -> "textentry"
6832 | Birdseye _ -> "birdseye"
6833 | View -> "view"
6835 findkeyhash conf modename
6837 method eformsgs = true
6838 end;;
6840 module Config =
6841 struct
6842 open Parser
6844 let fontpath = ref "";;
6846 module KeyMap =
6847 Map.Make (struct type t = (int * int) let compare = compare end);;
6849 let unent s =
6850 let l = String.length s in
6851 let b = Buffer.create l in
6852 unent b s 0 l;
6853 Buffer.contents b;
6856 let home =
6857 try Sys.getenv "HOME"
6858 with exn ->
6859 prerr_endline
6860 ("Can not determine home directory location: " ^ exntos exn);
6864 let modifier_of_string = function
6865 | "alt" -> Wsi.altmask
6866 | "shift" -> Wsi.shiftmask
6867 | "ctrl" | "control" -> Wsi.ctrlmask
6868 | "meta" -> Wsi.metamask
6869 | _ -> 0
6872 let key_of_string =
6873 let r = Str.regexp "-" in
6874 fun s ->
6875 let elems = Str.full_split r s in
6876 let f n k m =
6877 let g s =
6878 let m1 = modifier_of_string s in
6879 if m1 = 0
6880 then (Wsi.namekey s, m)
6881 else (k, m lor m1)
6882 in function
6883 | Str.Delim s when n land 1 = 0 -> g s
6884 | Str.Text s -> g s
6885 | Str.Delim _ -> (k, m)
6887 let rec loop n k m = function
6888 | [] -> (k, m)
6889 | x :: xs ->
6890 let k, m = f n k m x in
6891 loop (n+1) k m xs
6893 loop 0 0 0 elems
6896 let keys_of_string =
6897 let r = Str.regexp "[ \t]" in
6898 fun s ->
6899 let elems = Str.split r s in
6900 List.map key_of_string elems
6903 let copykeyhashes c =
6904 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6907 let config_of c attrs =
6908 let apply c k v =
6910 match k with
6911 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6912 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6913 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6914 | "preload" -> { c with preload = bool_of_string v }
6915 | "page-bias" -> { c with pagebias = int_of_string v }
6916 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6917 | "horizontal-scroll-step" ->
6918 { c with hscrollstep = max (int_of_string v) 1 }
6919 | "auto-scroll-step" ->
6920 { c with autoscrollstep = max 0 (int_of_string v) }
6921 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6922 | "crop-hack" -> { c with crophack = bool_of_string v }
6923 | "throttle" ->
6924 let mw =
6925 match String.lowercase v with
6926 | "true" -> Some infinity
6927 | "false" -> None
6928 | f -> Some (float_of_string f)
6930 { c with maxwait = mw}
6931 | "highlight-links" -> { c with hlinks = bool_of_string v }
6932 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6933 | "vertical-margin" ->
6934 { c with interpagespace = max 0 (int_of_string v) }
6935 | "zoom" ->
6936 let zoom = float_of_string v /. 100. in
6937 let zoom = max zoom 0.0 in
6938 { c with zoom = zoom }
6939 | "presentation" -> { c with presentation = bool_of_string v }
6940 | "rotation-angle" -> { c with angle = int_of_string v }
6941 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6942 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6943 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6944 | "proportional-display" ->
6945 let fm =
6946 if bool_of_string v
6947 then FitProportional
6948 else FitWidth
6950 { c with fitmodel = fm }
6951 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6952 | "pixmap-cache-size" ->
6953 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6954 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6955 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6956 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6957 | "persistent-location" -> { c with jumpback = bool_of_string v }
6958 | "background-color" -> { c with bgcolor = color_of_string v }
6959 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6960 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6961 | "mupdf-store-size" ->
6962 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6963 | "checkers" -> { c with checkers = bool_of_string v }
6964 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6965 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6966 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6967 | "uri-launcher" -> { c with urilauncher = unent v }
6968 | "path-launcher" -> { c with pathlauncher = unent v }
6969 | "color-space" -> { c with colorspace = CSTE.of_string v }
6970 | "invert-colors" -> { c with invert = bool_of_string v }
6971 | "brightness" -> { c with colorscale = float_of_string v }
6972 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6973 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6974 | "columns" ->
6975 let (n, _, _) as nab = multicolumns_of_string v in
6976 if n < 0
6977 then { c with columns = Csplit (-n, [||]) }
6978 else { c with columns = Cmulti (nab, [||]) }
6979 | "birds-eye-columns" ->
6980 { c with beyecolumns = Some (max (int_of_string v) 2) }
6981 | "selection-command" -> { c with selcmd = unent v }
6982 | "synctex-command" -> { c with stcmd = unent v }
6983 | "pax-command" -> { c with paxcmd = unent v }
6984 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6985 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6986 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6987 | "use-pbo" -> { c with usepbo = bool_of_string v }
6988 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6989 | "horizontal-scrollbar-visible" ->
6990 let b =
6991 if bool_of_string v
6992 then c.scrollb lor scrollbhv
6993 else c.scrollb land (lnot scrollbhv)
6995 { c with scrollb = b }
6996 | "vertical-scrollbar-visible" ->
6997 let b =
6998 if bool_of_string v
6999 then c.scrollb lor scrollbvv
7000 else c.scrollb land (lnot scrollbvv)
7002 { c with scrollb = b }
7003 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
7004 | "point-and-x" ->
7005 { c with pax =
7006 if bool_of_string v
7007 then Some (ref (0.0, 0, 0))
7008 else None }
7009 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
7010 | _ -> c
7011 with exn ->
7012 prerr_endline ("Error processing attribute (`" ^
7013 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
7016 let rec fold c = function
7017 | [] -> c
7018 | (k, v) :: rest ->
7019 let c = apply c k v in
7020 fold c rest
7022 fold { c with keyhashes = copykeyhashes c } attrs;
7025 let fromstring f pos n v d =
7026 try f v
7027 with exn ->
7028 dolog "Error processing attribute (%S=%S) at %d\n%s"
7029 n v pos (exntos exn)
7034 let bookmark_of attrs =
7035 let rec fold title page rely visy = function
7036 | ("title", v) :: rest -> fold v page rely visy rest
7037 | ("page", v) :: rest -> fold title v rely visy rest
7038 | ("rely", v) :: rest -> fold title page v visy rest
7039 | ("visy", v) :: rest -> fold title page rely v rest
7040 | _ :: rest -> fold title page rely visy rest
7041 | [] -> title, page, rely, visy
7043 fold "invalid" "0" "0" "0" attrs
7046 let doc_of attrs =
7047 let rec fold path page rely pan visy = function
7048 | ("path", v) :: rest -> fold v page rely pan visy rest
7049 | ("page", v) :: rest -> fold path v rely pan visy rest
7050 | ("rely", v) :: rest -> fold path page v pan visy rest
7051 | ("pan", v) :: rest -> fold path page rely v visy rest
7052 | ("visy", v) :: rest -> fold path page rely pan v rest
7053 | _ :: rest -> fold path page rely pan visy rest
7054 | [] -> path, page, rely, pan, visy
7056 fold "" "0" "0" "0" "0" attrs
7059 let map_of attrs =
7060 let rec fold rs ls = function
7061 | ("out", v) :: rest -> fold v ls rest
7062 | ("in", v) :: rest -> fold rs v rest
7063 | _ :: rest -> fold ls rs rest
7064 | [] -> ls, rs
7066 fold "" "" attrs
7069 let setconf dst src =
7070 dst.scrollbw <- src.scrollbw;
7071 dst.scrollh <- src.scrollh;
7072 dst.icase <- src.icase;
7073 dst.preload <- src.preload;
7074 dst.pagebias <- src.pagebias;
7075 dst.verbose <- src.verbose;
7076 dst.scrollstep <- src.scrollstep;
7077 dst.maxhfit <- src.maxhfit;
7078 dst.crophack <- src.crophack;
7079 dst.autoscrollstep <- src.autoscrollstep;
7080 dst.maxwait <- src.maxwait;
7081 dst.hlinks <- src.hlinks;
7082 dst.underinfo <- src.underinfo;
7083 dst.interpagespace <- src.interpagespace;
7084 dst.zoom <- src.zoom;
7085 dst.presentation <- src.presentation;
7086 dst.angle <- src.angle;
7087 dst.cwinw <- src.cwinw;
7088 dst.cwinh <- src.cwinh;
7089 dst.savebmarks <- src.savebmarks;
7090 dst.memlimit <- src.memlimit;
7091 dst.fitmodel <- src.fitmodel;
7092 dst.texcount <- src.texcount;
7093 dst.sliceheight <- src.sliceheight;
7094 dst.thumbw <- src.thumbw;
7095 dst.jumpback <- src.jumpback;
7096 dst.bgcolor <- src.bgcolor;
7097 dst.tilew <- src.tilew;
7098 dst.tileh <- src.tileh;
7099 dst.mustoresize <- src.mustoresize;
7100 dst.checkers <- src.checkers;
7101 dst.aalevel <- src.aalevel;
7102 dst.trimmargins <- src.trimmargins;
7103 dst.trimfuzz <- src.trimfuzz;
7104 dst.urilauncher <- src.urilauncher;
7105 dst.colorspace <- src.colorspace;
7106 dst.invert <- src.invert;
7107 dst.colorscale <- src.colorscale;
7108 dst.redirectstderr <- src.redirectstderr;
7109 dst.ghyllscroll <- src.ghyllscroll;
7110 dst.columns <- src.columns;
7111 dst.beyecolumns <- src.beyecolumns;
7112 dst.selcmd <- src.selcmd;
7113 dst.updatecurs <- src.updatecurs;
7114 dst.pathlauncher <- src.pathlauncher;
7115 dst.keyhashes <- copykeyhashes src;
7116 dst.hfsize <- src.hfsize;
7117 dst.hscrollstep <- src.hscrollstep;
7118 dst.pgscale <- src.pgscale;
7119 dst.usepbo <- src.usepbo;
7120 dst.wheelbypage <- src.wheelbypage;
7121 dst.stcmd <- src.stcmd;
7122 dst.paxcmd <- src.paxcmd;
7123 dst.scrollb <- src.scrollb;
7124 dst.riani <- src.riani;
7125 dst.paxmark <- src.paxmark;
7126 dst.pax <-
7127 if src.pax = None
7128 then None
7129 else Some ((ref (0.0, 0, 0)));
7132 let get s =
7133 let h = Hashtbl.create 10 in
7134 let dc = { defconf with angle = defconf.angle } in
7135 let rec toplevel v t spos _ =
7136 match t with
7137 | Vdata | Vcdata | Vend -> v
7138 | Vopen ("llppconfig", _, closed) ->
7139 if closed
7140 then v
7141 else { v with f = llppconfig }
7142 | Vopen _ ->
7143 error "unexpected subelement at top level" s spos
7144 | Vclose _ -> error "unexpected close at top level" s spos
7146 and llppconfig v t spos _ =
7147 match t with
7148 | Vdata | Vcdata -> v
7149 | Vend -> error "unexpected end of input in llppconfig" s spos
7150 | Vopen ("defaults", attrs, closed) ->
7151 let c = config_of dc attrs in
7152 setconf dc c;
7153 if closed
7154 then v
7155 else { v with f = defaults }
7157 | Vopen ("ui-font", attrs, closed) ->
7158 let rec getsize size = function
7159 | [] -> size
7160 | ("size", v) :: rest ->
7161 let size =
7162 fromstring int_of_string spos "size" v fstate.fontsize in
7163 getsize size rest
7164 | l -> getsize size l
7166 fstate.fontsize <- getsize fstate.fontsize attrs;
7167 if closed
7168 then v
7169 else { v with f = uifont (Buffer.create 10) }
7171 | Vopen ("doc", attrs, closed) ->
7172 let pathent, spage, srely, span, svisy = doc_of attrs in
7173 let path = unent pathent
7174 and pageno = fromstring int_of_string spos "page" spage 0
7175 and rely = fromstring float_of_string spos "rely" srely 0.0
7176 and pan = fromstring int_of_string spos "pan" span 0
7177 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7178 let c = config_of dc attrs in
7179 let anchor = (pageno, rely, visy) in
7180 if closed
7181 then (Hashtbl.add h path (c, [], pan, anchor); v)
7182 else { v with f = doc path pan anchor c [] }
7184 | Vopen _ ->
7185 error "unexpected subelement in llppconfig" s spos
7187 | Vclose "llppconfig" -> { v with f = toplevel }
7188 | Vclose _ -> error "unexpected close in llppconfig" s spos
7190 and defaults v t spos _ =
7191 match t with
7192 | Vdata | Vcdata -> v
7193 | Vend -> error "unexpected end of input in defaults" s spos
7194 | Vopen ("keymap", attrs, closed) ->
7195 let modename =
7196 try List.assoc "mode" attrs
7197 with Not_found -> "global" in
7198 if closed
7199 then v
7200 else
7201 let ret keymap =
7202 let h = findkeyhash dc modename in
7203 KeyMap.iter (Hashtbl.replace h) keymap;
7204 defaults
7206 { v with f = pkeymap ret KeyMap.empty }
7208 | Vopen (_, _, _) ->
7209 error "unexpected subelement in defaults" s spos
7211 | Vclose "defaults" ->
7212 { v with f = llppconfig }
7214 | Vclose _ -> error "unexpected close in defaults" s spos
7216 and uifont b v t spos epos =
7217 match t with
7218 | Vdata | Vcdata ->
7219 Buffer.add_substring b s spos (epos - spos);
7221 | Vopen (_, _, _) ->
7222 error "unexpected subelement in ui-font" s spos
7223 | Vclose "ui-font" ->
7224 if emptystr !fontpath
7225 then fontpath := Buffer.contents b;
7226 { v with f = llppconfig }
7227 | Vclose _ -> error "unexpected close in ui-font" s spos
7228 | Vend -> error "unexpected end of input in ui-font" s spos
7230 and doc path pan anchor c bookmarks v t spos _ =
7231 match t with
7232 | Vdata | Vcdata -> v
7233 | Vend -> error "unexpected end of input in doc" s spos
7234 | Vopen ("bookmarks", _, closed) ->
7235 if closed
7236 then v
7237 else { v with f = pbookmarks path pan anchor c bookmarks }
7239 | Vopen ("keymap", attrs, closed) ->
7240 let modename =
7241 try List.assoc "mode" attrs
7242 with Not_found -> "global"
7244 if closed
7245 then v
7246 else
7247 let ret keymap =
7248 let h = findkeyhash c modename in
7249 KeyMap.iter (Hashtbl.replace h) keymap;
7250 doc path pan anchor c bookmarks
7252 { v with f = pkeymap ret KeyMap.empty }
7254 | Vopen (_, _, _) ->
7255 error "unexpected subelement in doc" s spos
7257 | Vclose "doc" ->
7258 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7259 { v with f = llppconfig }
7261 | Vclose _ -> error "unexpected close in doc" s spos
7263 and pkeymap ret keymap v t spos _ =
7264 match t with
7265 | Vdata | Vcdata -> v
7266 | Vend -> error "unexpected end of input in keymap" s spos
7267 | Vopen ("map", attrs, closed) ->
7268 let r, l = map_of attrs in
7269 let kss = fromstring keys_of_string spos "in" r [] in
7270 let lss = fromstring keys_of_string spos "out" l [] in
7271 let keymap =
7272 match kss with
7273 | [] -> keymap
7274 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7275 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7277 if closed
7278 then { v with f = pkeymap ret keymap }
7279 else
7280 let f () = v in
7281 { v with f = skip "map" f }
7283 | Vopen _ ->
7284 error "unexpected subelement in keymap" s spos
7286 | Vclose "keymap" ->
7287 { v with f = ret keymap }
7289 | Vclose _ -> error "unexpected close in keymap" s spos
7291 and pbookmarks path pan anchor c bookmarks v t spos _ =
7292 match t with
7293 | Vdata | Vcdata -> v
7294 | Vend -> error "unexpected end of input in bookmarks" s spos
7295 | Vopen ("item", attrs, closed) ->
7296 let titleent, spage, srely, svisy = bookmark_of attrs in
7297 let page = fromstring int_of_string spos "page" spage 0
7298 and rely = fromstring float_of_string spos "rely" srely 0.0
7299 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7300 let bookmarks =
7301 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
7303 if closed
7304 then { v with f = pbookmarks path pan anchor c bookmarks }
7305 else
7306 let f () = v in
7307 { v with f = skip "item" f }
7309 | Vopen _ ->
7310 error "unexpected subelement in bookmarks" s spos
7312 | Vclose "bookmarks" ->
7313 { v with f = doc path pan anchor c bookmarks }
7315 | Vclose _ -> error "unexpected close in bookmarks" s spos
7317 and skip tag f v t spos _ =
7318 match t with
7319 | Vdata | Vcdata -> v
7320 | Vend ->
7321 error ("unexpected end of input in skipped " ^ tag) s spos
7322 | Vopen (tag', _, closed) ->
7323 if closed
7324 then v
7325 else
7326 let f' () = { v with f = skip tag f } in
7327 { v with f = skip tag' f' }
7328 | Vclose ctag ->
7329 if tag = ctag
7330 then f ()
7331 else error ("unexpected close in skipped " ^ tag) s spos
7334 parse { f = toplevel; accu = () } s;
7335 h, dc;
7338 let do_load f ic =
7340 let len = in_channel_length ic in
7341 let s = String.create len in
7342 really_input ic s 0 len;
7343 f s;
7344 with
7345 | Parse_error (msg, s, pos) ->
7346 let subs = subs s pos in
7347 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7349 | exn ->
7350 failwith ("config load error: " ^ exntos exn)
7353 let defconfpath =
7354 let dir =
7356 let dir = Filename.concat home ".config" in
7357 if Sys.is_directory dir then dir else home
7358 with _ -> home
7360 Filename.concat dir "llpp.conf"
7363 let confpath = ref defconfpath;;
7365 let load1 f =
7366 if Sys.file_exists !confpath
7367 then
7368 match
7369 (try Some (open_in_bin !confpath)
7370 with exn ->
7371 prerr_endline
7372 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7373 exntos exn);
7374 None
7376 with
7377 | Some ic ->
7378 let success =
7380 f (do_load get ic)
7381 with exn ->
7382 prerr_endline
7383 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7384 exntos exn);
7385 false
7387 close_in ic;
7388 success
7390 | None -> false
7391 else
7392 f (Hashtbl.create 0, defconf)
7395 let load () =
7396 let f (h, dc) =
7397 let pc, pb, px, pa =
7399 let key =
7400 if emptystr state.origin
7401 then state.path
7402 else state.origin
7404 Hashtbl.find h (Filename.basename key)
7405 with Not_found -> dc, [], 0, emptyanchor
7407 setconf defconf dc;
7408 setconf conf pc;
7409 state.bookmarks <- pb;
7410 state.x <- px;
7411 if conf.jumpback
7412 then state.anchor <- pa;
7413 cbput state.hists.nav pa;
7414 true
7416 load1 f
7419 let add_attrs bb always dc c =
7420 let ob s a b =
7421 if always || a != b
7422 then Printf.bprintf bb "\n %s='%b'" s a
7423 and op s a b =
7424 if always || a <> b
7425 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7426 and oi s a b =
7427 if always || a != b
7428 then Printf.bprintf bb "\n %s='%d'" s a
7429 and oI s a b =
7430 if always || a != b
7431 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7432 and oz s a b =
7433 if always || a <> b
7434 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7435 and oF s a b =
7436 if always || a <> b
7437 then Printf.bprintf bb "\n %s='%f'" s a
7438 and oc s a b =
7439 if always || a <> b
7440 then
7441 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7442 and oC s a b =
7443 if always || a <> b
7444 then
7445 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7446 and oR s a b =
7447 if always || a <> b
7448 then
7449 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7450 and os s a b =
7451 if always || a <> b
7452 then
7453 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7454 and og s a b =
7455 if always || a <> b
7456 then
7457 match a with
7458 | Some (_N, _A, _B) ->
7459 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7460 | None ->
7461 match b with
7462 | None -> ()
7463 | _ ->
7464 Printf.bprintf bb "\n %s='none'" s
7465 and oW s a b =
7466 if always || a <> b
7467 then
7468 let v =
7469 match a with
7470 | None -> "false"
7471 | Some f ->
7472 if f = infinity
7473 then "true"
7474 else string_of_float f
7476 Printf.bprintf bb "\n %s='%s'" s v
7477 and oco s a b =
7478 if always || a <> b
7479 then
7480 match a with
7481 | Cmulti ((n, a, b), _) when n > 1 ->
7482 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7483 | Csplit (n, _) when n > 1 ->
7484 Printf.bprintf bb "\n %s='%d'" s ~-n
7485 | _ -> ()
7486 and obeco s a b =
7487 if always || a <> b
7488 then
7489 match a with
7490 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7491 | _ -> ()
7492 and oFm s a b =
7493 if always || a <> b
7494 then
7495 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7496 and oSv s a b m =
7497 if always || a <> b
7498 then
7499 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7500 and oPm s a b =
7501 if always || a <> b
7502 then
7503 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7505 oi "width" c.cwinw dc.cwinw;
7506 oi "height" c.cwinh dc.cwinh;
7507 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7508 oi "scroll-handle-height" c.scrollh dc.scrollh;
7509 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7510 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7511 ob "case-insensitive-search" c.icase dc.icase;
7512 ob "preload" c.preload dc.preload;
7513 oi "page-bias" c.pagebias dc.pagebias;
7514 oi "scroll-step" c.scrollstep dc.scrollstep;
7515 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7516 ob "max-height-fit" c.maxhfit dc.maxhfit;
7517 ob "crop-hack" c.crophack dc.crophack;
7518 oW "throttle" c.maxwait dc.maxwait;
7519 ob "highlight-links" c.hlinks dc.hlinks;
7520 ob "under-cursor-info" c.underinfo dc.underinfo;
7521 oi "vertical-margin" c.interpagespace dc.interpagespace;
7522 oz "zoom" c.zoom dc.zoom;
7523 ob "presentation" c.presentation dc.presentation;
7524 oi "rotation-angle" c.angle dc.angle;
7525 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7526 oFm "fit-model" c.fitmodel dc.fitmodel;
7527 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7528 oi "tex-count" c.texcount dc.texcount;
7529 oi "slice-height" c.sliceheight dc.sliceheight;
7530 oi "thumbnail-width" c.thumbw dc.thumbw;
7531 ob "persistent-location" c.jumpback dc.jumpback;
7532 oc "background-color" c.bgcolor dc.bgcolor;
7533 oi "tile-width" c.tilew dc.tilew;
7534 oi "tile-height" c.tileh dc.tileh;
7535 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7536 ob "checkers" c.checkers dc.checkers;
7537 oi "aalevel" c.aalevel dc.aalevel;
7538 ob "trim-margins" c.trimmargins dc.trimmargins;
7539 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7540 os "uri-launcher" c.urilauncher dc.urilauncher;
7541 os "path-launcher" c.pathlauncher dc.pathlauncher;
7542 oC "color-space" c.colorspace dc.colorspace;
7543 ob "invert-colors" c.invert dc.invert;
7544 oF "brightness" c.colorscale dc.colorscale;
7545 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7546 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7547 oco "columns" c.columns dc.columns;
7548 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7549 os "selection-command" c.selcmd dc.selcmd;
7550 os "synctex-command" c.stcmd dc.stcmd;
7551 os "pax-command" c.paxcmd dc.paxcmd;
7552 ob "update-cursor" c.updatecurs dc.updatecurs;
7553 oi "hint-font-size" c.hfsize dc.hfsize;
7554 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7555 oF "page-scroll-scale" c.pgscale dc.pgscale;
7556 ob "use-pbo" c.usepbo dc.usepbo;
7557 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7558 ob "remote-in-a-new-instance" c.riani dc.riani;
7559 op "point-and-x" c.pax dc.pax;
7560 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7563 let keymapsbuf always dc c =
7564 let bb = Buffer.create 16 in
7565 let rec loop = function
7566 | [] -> ()
7567 | (modename, h) :: rest ->
7568 let dh = findkeyhash dc modename in
7569 if always || h <> dh
7570 then (
7571 if Hashtbl.length h > 0
7572 then (
7573 if Buffer.length bb > 0
7574 then Buffer.add_char bb '\n';
7575 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7576 Hashtbl.iter (fun i o ->
7577 let isdifferent = always ||
7579 let dO = Hashtbl.find dh i in
7580 dO <> o
7581 with Not_found -> true
7583 if isdifferent
7584 then
7585 let addkm (k, m) =
7586 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7587 if Wsi.withalt m then Buffer.add_string bb "alt-";
7588 if Wsi.withshift m then Buffer.add_string bb "shift-";
7589 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7590 Buffer.add_string bb (Wsi.keyname k);
7592 let addkms l =
7593 let rec loop = function
7594 | [] -> ()
7595 | km :: [] -> addkm km
7596 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7598 loop l
7600 Buffer.add_string bb "<map in='";
7601 addkm i;
7602 match o with
7603 | KMinsrt km ->
7604 Buffer.add_string bb "' out='";
7605 addkm km;
7606 Buffer.add_string bb "'/>\n"
7608 | KMinsrl kms ->
7609 Buffer.add_string bb "' out='";
7610 addkms kms;
7611 Buffer.add_string bb "'/>\n"
7613 | KMmulti (ins, kms) ->
7614 Buffer.add_char bb ' ';
7615 addkms ins;
7616 Buffer.add_string bb "' out='";
7617 addkms kms;
7618 Buffer.add_string bb "'/>\n"
7619 ) h;
7620 Buffer.add_string bb "</keymap>";
7623 loop rest
7625 loop c.keyhashes;
7629 let save () =
7630 let uifontsize = fstate.fontsize in
7631 let bb = Buffer.create 32768 in
7632 let relx = float state.x /. float state.winw in
7633 let w, h, x =
7634 let cx w = truncate (relx *. float w) in
7635 List.fold_left
7636 (fun (w, h, x) ws ->
7637 match ws with
7638 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7639 | Wsi.MaxVert -> (w, conf.cwinh, x)
7640 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7642 (state.winw, state.winh, state.x) state.winstate
7644 conf.cwinw <- w;
7645 conf.cwinh <- h;
7646 let f (h, dc) =
7647 let dc = if conf.bedefault then conf else dc in
7648 Buffer.add_string bb "<llppconfig>\n";
7650 if nonemptystr !fontpath
7651 then
7652 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7653 uifontsize
7654 !fontpath
7655 else (
7656 if uifontsize <> 14
7657 then
7658 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7661 Buffer.add_string bb "<defaults";
7662 add_attrs bb true dc dc;
7663 let kb = keymapsbuf true dc dc in
7664 if Buffer.length kb > 0
7665 then (
7666 Buffer.add_string bb ">\n";
7667 Buffer.add_buffer bb kb;
7668 Buffer.add_string bb "\n</defaults>\n";
7670 else Buffer.add_string bb "/>\n";
7672 let adddoc path pan anchor c bookmarks =
7673 if bookmarks == [] && c = dc && anchor = emptyanchor
7674 then ()
7675 else (
7676 Printf.bprintf bb "<doc path='%s'"
7677 (enent path 0 (String.length path));
7679 if anchor <> emptyanchor
7680 then (
7681 let n, rely, visy = anchor in
7682 Printf.bprintf bb " page='%d'" n;
7683 if rely > 1e-6
7684 then
7685 Printf.bprintf bb " rely='%f'" rely
7687 if abs_float visy > 1e-6
7688 then
7689 Printf.bprintf bb " visy='%f'" visy
7693 if pan != 0
7694 then Printf.bprintf bb " pan='%d'" pan;
7696 add_attrs bb false dc c;
7697 let kb = keymapsbuf false dc c in
7699 begin match bookmarks with
7700 | [] ->
7701 if Buffer.length kb > 0
7702 then (
7703 Buffer.add_string bb ">\n";
7704 Buffer.add_buffer bb kb;
7705 Buffer.add_string bb "\n</doc>\n";
7707 else Buffer.add_string bb "/>\n"
7708 | _ ->
7709 Buffer.add_string bb ">\n<bookmarks>\n";
7710 List.iter (fun (title, _, kind) ->
7711 begin match kind with
7712 | Oanchor (page, rely, visy) ->
7713 Printf.bprintf bb
7714 "<item title='%s' page='%d'"
7715 (enent title 0 (String.length title))
7716 page
7718 if rely > 1e-6
7719 then
7720 Printf.bprintf bb " rely='%f'" rely
7722 if abs_float visy > 1e-6
7723 then
7724 Printf.bprintf bb " visy='%f'" visy
7726 | Onone | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
7727 failwith "unexpected link in bookmarks"
7728 end;
7729 Buffer.add_string bb "/>\n";
7730 ) bookmarks;
7731 Buffer.add_string bb "</bookmarks>";
7732 if Buffer.length kb > 0
7733 then (
7734 Buffer.add_string bb "\n";
7735 Buffer.add_buffer bb kb;
7737 Buffer.add_string bb "\n</doc>\n";
7738 end;
7742 let pan, conf =
7743 match state.mode with
7744 | Birdseye (c, pan, _, _, _) ->
7745 let beyecolumns =
7746 match conf.columns with
7747 | Cmulti ((c, _, _), _) -> Some c
7748 | Csingle _ -> None
7749 | Csplit _ -> None
7750 and columns =
7751 match c.columns with
7752 | Cmulti (c, _) -> Cmulti (c, [||])
7753 | Csingle _ -> Csingle [||]
7754 | Csplit _ -> failwith "quit from bird's eye while split"
7756 pan, { c with beyecolumns = beyecolumns; columns = columns }
7757 | _ -> x, conf
7759 let basename = Filename.basename
7760 (if emptystr state.origin then state.path else state.origin)
7762 adddoc basename pan (getanchor ())
7763 (let autoscrollstep =
7764 match state.autoscroll with
7765 | Some step -> step
7766 | None -> conf.autoscrollstep
7767 in begin match state.mode with
7768 | Birdseye beye -> leavebirdseye beye true;
7769 | _ -> ()
7770 end;
7771 { conf with autoscrollstep = autoscrollstep })
7772 (if conf.savebmarks then state.bookmarks else []);
7774 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7775 if basename <> path
7776 then adddoc path x anchor c bookmarks
7777 ) h;
7778 Buffer.add_string bb "</llppconfig>\n";
7779 true;
7781 if load1 f && Buffer.length bb > 0
7782 then
7784 let tmp = !confpath ^ ".tmp" in
7785 let oc = open_out_bin tmp in
7786 Buffer.output_buffer oc bb;
7787 close_out oc;
7788 Unix.rename tmp !confpath;
7789 with exn ->
7790 prerr_endline
7791 ("error while saving configuration: " ^ exntos exn)
7793 end;;
7795 let adderrmsg src msg =
7796 Buffer.add_string state.errmsgs msg;
7797 state.newerrmsgs <- true;
7798 G.postRedisplay src
7801 let adderrfmt src fmt =
7802 Format.kprintf (fun s -> adderrmsg src s) fmt;
7805 let ract cmds =
7806 let cl = splitatspace cmds in
7807 let scan s fmt f =
7808 try Scanf.sscanf s fmt f
7809 with exn ->
7810 adderrfmt "remote exec"
7811 "error processing '%S': %s\n" cmds (exntos exn)
7813 match cl with
7814 | "reload" :: [] -> reload ()
7815 | "goto" :: args :: [] ->
7816 scan args "%u %f %f"
7817 (fun pageno x y ->
7818 let cmd, _ = state.geomcmds in
7819 if emptystr cmd
7820 then gotopagexy pageno x y
7821 else
7822 let f prevf () =
7823 gotopagexy pageno x y;
7824 prevf ()
7826 state.reprf <- f state.reprf
7828 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7829 | "gotor" :: args :: [] ->
7830 scan args "%S %u"
7831 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
7832 | "gotord" :: args :: [] ->
7833 scan args "%S %S"
7834 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
7835 | "rect" :: args :: [] ->
7836 scan args "%u %u %f %f %f %f"
7837 (fun pageno color x0 y0 x1 y1 ->
7838 onpagerect pageno (fun w h ->
7839 let _,w1,h1,_ = getpagedim pageno in
7840 let sw = float w1 /. float w
7841 and sh = float h1 /. float h in
7842 let x0s = x0 *. sw
7843 and x1s = x1 *. sw
7844 and y0s = y0 *. sh
7845 and y1s = y1 *. sh in
7846 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7847 debugrect rect;
7848 state.rects <- (pageno, color, rect) :: state.rects;
7849 G.postRedisplay "rect";
7852 | "activatewin" :: [] -> Wsi.activatewin ()
7853 | "quit" :: [] -> raise Quit
7854 | _ ->
7855 adderrfmt "remote command"
7856 "error processing remote command: %S\n" cmds;
7859 let remote =
7860 let scratch = String.create 80 in
7861 let buf = Buffer.create 80 in
7862 fun fd ->
7863 let rec tempfr () =
7864 try Some (Unix.read fd scratch 0 80)
7865 with
7866 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7867 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7868 | exn -> raise exn
7870 match tempfr () with
7871 | None -> Some fd
7872 | Some n ->
7873 if n = 0
7874 then (
7875 Unix.close fd;
7876 if Buffer.length buf > 0
7877 then (
7878 let s = Buffer.contents buf in
7879 Buffer.clear buf;
7880 ract s;
7882 None
7884 else
7885 let rec eat ppos =
7886 let nlpos =
7888 let pos = String.index_from scratch ppos '\n' in
7889 if pos >= n then -1 else pos
7890 with Not_found -> -1
7892 if nlpos >= 0
7893 then (
7894 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7895 let s = Buffer.contents buf in
7896 Buffer.clear buf;
7897 ract s;
7898 eat (nlpos+1);
7900 else (
7901 Buffer.add_substring buf scratch ppos (n-ppos);
7902 Some fd
7904 in eat 0
7907 let remoteopen path =
7908 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7909 with exn ->
7910 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7911 None
7914 let () =
7915 let trimcachepath = ref "" in
7916 let rcmdpath = ref "" in
7917 let pageno = ref None in
7918 selfexec := Sys.executable_name;
7919 Arg.parse
7920 (Arg.align
7921 [("-p", Arg.String (fun s -> state.password <- s),
7922 "<password> Set password");
7924 ("-f", Arg.String
7925 (fun s ->
7926 Config.fontpath := s;
7927 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7929 "<path> Set path to the user interface font");
7931 ("-c", Arg.String
7932 (fun s ->
7933 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7934 Config.confpath := s),
7935 "<path> Set path to the configuration file");
7937 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
7938 "<page-number> Jump to page");
7940 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7941 "<path> Set path to the trim cache file");
7943 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7944 "<named-destination> Set named destination");
7946 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7947 ("-cxack", Arg.Set cxack, " Cut corners");
7949 ("-remote", Arg.String (fun s -> rcmdpath := s),
7950 "<path> Set path to the remote commands source");
7952 ("-origin", Arg.String (fun s -> state.origin <- s),
7953 "<original-path> Set original path");
7955 ("-v", Arg.Unit (fun () ->
7956 Printf.printf
7957 "%s\nconfiguration path: %s\n"
7958 (version ())
7959 Config.defconfpath
7961 exit 0), " Print version and exit");
7964 (fun s -> state.path <- s)
7965 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7967 if !wtmode
7968 then selfexec := !selfexec ^ " -wtmode";
7970 if emptystr state.path
7971 then (prerr_endline "file name missing"; exit 1);
7973 if not (Config.load ())
7974 then prerr_endline "failed to load configuration";
7975 begin match !pageno with
7976 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
7977 | None -> ()
7978 end;
7980 let wsfd, winw, winh = Wsi.init (object (self)
7981 val mutable m_hack = false
7982 val mutable m_clicks = 0
7983 val mutable m_click_x = 0
7984 val mutable m_click_y = 0
7985 val mutable m_lastclicktime = infinity
7987 method private cleanup =
7988 state.roam <- noroam;
7989 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
7990 method expose = if not m_hack then G.postRedisplay "expose"
7991 method visible = G.postRedisplay "visible"
7992 method display = m_hack <- false; display ()
7993 method reshape w h =
7994 self#cleanup;
7995 m_hack <- w < state.winw && h < state.winh;
7996 reshape w h
7997 method mouse b d x y m =
7998 if d && canselect ()
7999 then (
8000 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
8001 m_click_x <- x;
8002 m_click_y <- y;
8003 if b = 1
8004 then (
8005 let t = now () in
8006 if abs x - m_click_x > 10
8007 || abs y - m_click_y > 10
8008 || abs_float (t -. m_lastclicktime) > 0.3
8009 then m_clicks <- 0;
8010 m_clicks <- m_clicks + 1;
8011 m_lastclicktime <- t;
8012 if m_clicks = 1
8013 then (
8014 self#cleanup;
8015 G.postRedisplay "cleanup";
8016 state.uioh <- state.uioh#button b d x y m;
8018 else state.uioh <- state.uioh#multiclick m_clicks x y m
8020 else (
8021 self#cleanup;
8022 m_clicks <- 0;
8023 m_lastclicktime <- infinity;
8024 state.uioh <- state.uioh#button b d x y m
8027 else (
8028 state.uioh <- state.uioh#button b d x y m
8030 method motion x y =
8031 state.mpos <- (x, y);
8032 state.uioh <- state.uioh#motion x y
8033 method pmotion x y =
8034 state.mpos <- (x, y);
8035 state.uioh <- state.uioh#pmotion x y
8036 method key k m =
8037 let mascm = m land (
8038 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
8039 ) in
8040 let keyboard k m =
8041 let x = state.x and y = state.y in
8042 keyboard k m;
8043 if x != state.x || y != state.y then self#cleanup
8045 match state.keystate with
8046 | KSnone ->
8047 let km = k, mascm in
8048 begin
8049 match
8050 let modehash = state.uioh#modehash in
8051 try Hashtbl.find modehash km
8052 with Not_found ->
8053 try Hashtbl.find (findkeyhash conf "global") km
8054 with Not_found -> KMinsrt (k, m)
8055 with
8056 | KMinsrt (k, m) -> keyboard k m
8057 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
8058 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
8060 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
8061 List.iter (fun (k, m) -> keyboard k m) insrt;
8062 state.keystate <- KSnone
8063 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
8064 state.keystate <- KSinto (keys, insrt)
8065 | _ ->
8066 state.keystate <- KSnone
8068 method enter x y =
8069 state.mpos <- (x, y);
8070 state.uioh <- state.uioh#pmotion x y
8071 method leave = state.mpos <- (-1, -1)
8072 method winstate wsl = state.winstate <- wsl; m_hack <- false
8073 method quit = raise Quit
8074 end) conf.cwinw conf.cwinh (platform = Posx) in
8076 state.wsfd <- wsfd;
8078 if not (
8079 List.exists GlMisc.check_extension
8080 [ "GL_ARB_texture_rectangle"
8081 ; "GL_EXT_texture_recangle"
8082 ; "GL_NV_texture_rectangle" ]
8084 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
8086 if (
8087 let r = GlMisc.get_string `renderer in
8088 let p = "Mesa DRI Intel(" in
8089 let l = String.length p in
8090 String.length r > l && String.sub r 0 l = p
8092 then (
8093 defconf.sliceheight <- 1024;
8094 defconf.texcount <- 32;
8095 defconf.usepbo <- true;
8098 let cr, sw =
8099 match Ne.res Unix.pipe with
8100 | Ne.Exn exn ->
8101 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
8102 exit 1
8103 | Ne.Res rw -> rw
8104 and sr, cw =
8105 match Ne.res Unix.pipe with
8106 | Ne.Exn exn ->
8107 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
8108 exit 1
8109 | Ne.Res rw -> rw
8112 cloexec cr;
8113 cloexec sw;
8114 cloexec sr;
8115 cloexec cw;
8117 setcheckers conf.checkers;
8118 redirectstderr ();
8119 if conf.redirectstderr
8120 then
8121 at_exit (fun () ->
8122 let s = Buffer.contents state.errmsgs ^
8123 (match state.errfd with
8124 | Some fd ->
8125 let s = String.create (80*24) in
8126 let n =
8128 let r, _, _ = Unix.select [fd] [] [] 0.0 in
8129 if List.mem fd r
8130 then Unix.read fd s 0 (String.length s)
8131 else 0
8132 with _ -> 0
8134 if n = 0
8135 then ""
8136 else String.sub s 0 n
8137 | None -> ""
8140 try ignore (Unix.write state.stderr s 0 (String.length s))
8141 with exn -> print_endline (exntos exn)
8145 init (cr, cw) (
8146 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
8147 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
8148 !Config.fontpath, !trimcachepath,
8149 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
8151 List.iter GlArray.enable [`texture_coord; `vertex];
8152 state.sr <- sr;
8153 state.sw <- sw;
8154 state.text <- "Opening " ^ (mbtoutf8 state.path);
8155 reshape winw winh;
8156 opendoc state.path state.password;
8157 state.uioh <- uioh;
8158 display ();
8159 Wsi.mapwin ();
8160 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
8161 let optrfd =
8162 ref (
8163 if nonemptystr !rcmdpath
8164 then remoteopen !rcmdpath
8165 else None
8169 let rec loop deadline =
8170 let r =
8171 match state.errfd with
8172 | None -> [state.sr; state.wsfd]
8173 | Some fd -> [state.sr; state.wsfd; fd]
8175 let r =
8176 match !optrfd with
8177 | None -> r
8178 | Some fd -> fd :: r
8180 if state.redisplay
8181 then (
8182 state.redisplay <- false;
8183 display ();
8185 let timeout =
8186 let now = now () in
8187 if deadline > now
8188 then (
8189 if deadline = infinity
8190 then ~-.1.0
8191 else max 0.0 (deadline -. now)
8193 else 0.0
8195 let r, _, _ =
8196 try Unix.select r [] [] timeout
8197 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
8199 begin match r with
8200 | [] ->
8201 state.ghyll None;
8202 let newdeadline =
8203 if state.ghyll == noghyll
8204 then
8205 match state.autoscroll with
8206 | Some step when step != 0 ->
8207 let y = state.y + step in
8208 let y =
8209 if y < 0
8210 then state.maxy
8211 else if y >= state.maxy then 0 else y
8213 gotoy y;
8214 if state.mode = View
8215 then state.text <- "";
8216 deadline +. 0.01
8217 | _ -> infinity
8218 else deadline +. 0.01
8220 loop newdeadline
8222 | l ->
8223 let rec checkfds = function
8224 | [] -> ()
8225 | fd :: rest when fd = state.sr ->
8226 let cmd = readcmd state.sr in
8227 act cmd;
8228 checkfds rest
8230 | fd :: rest when fd = state.wsfd ->
8231 Wsi.readresp fd;
8232 checkfds rest
8234 | fd :: rest when Some fd = !optrfd ->
8235 begin match remote fd with
8236 | None -> optrfd := remoteopen !rcmdpath;
8237 | opt -> optrfd := opt
8238 end;
8239 checkfds rest
8241 | fd :: rest ->
8242 let s = String.create 80 in
8243 let n = tempfailureretry (Unix.read fd s 0) 80 in
8244 if conf.redirectstderr
8245 then (
8246 Buffer.add_substring state.errmsgs s 0 n;
8247 state.newerrmsgs <- true;
8248 state.redisplay <- true;
8250 else (
8251 prerr_string (String.sub s 0 n);
8252 flush stderr;
8254 checkfds rest
8256 checkfds l;
8257 let newdeadline =
8258 let deadline1 =
8259 if deadline = infinity
8260 then now () +. 0.01
8261 else deadline
8263 match state.autoscroll with
8264 | Some step when step != 0 -> deadline1
8265 | _ -> if state.ghyll == noghyll then infinity else deadline1
8267 loop newdeadline
8268 end;
8271 loop infinity;
8272 with Quit ->
8273 Config.save ();