Visual narrow cues
[llpp.git] / main.ml
blob4a3cb950ba2f4ee8397c6e8328e7beda9d5ffc1c
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of launchcommand
12 | Unamed of destname
13 | Uremote of (filename * pageno)
14 | Uremotedest of (filename * destname)
15 and facename = string
16 and launchcommand = string
17 and filename = string
18 and pageno = int
19 and destname = string;;
21 type mark =
22 | Mark_page
23 | Mark_block
24 | Mark_line
25 | Mark_word
28 module Opaque :
29 sig
30 type t = private string
31 val of_string : string -> t
32 val to_string : t -> string
33 end
35 struct
36 type t = string
37 let of_string s = s
38 let to_string t = t
39 end
42 let (~<) = Opaque.of_string;;
43 let (~>) = Opaque.to_string;;
45 type params = (angle * fitmodel * trimparams
46 * texcount * sliceheight * memsize
47 * colorspace * fontpath * trimcachepath
48 * haspbo)
49 and width = int
50 and height = int
51 and leftx = int
52 and opaque = Opaque.t
53 and recttype = int
54 and pixmapsize = int
55 and angle = int
56 and trimmargins = bool
57 and interpagespace = int
58 and texcount = int
59 and sliceheight = int
60 and gen = int
61 and top = float
62 and dtop = float
63 and fontpath = string
64 and trimcachepath = string
65 and memsize = int
66 and aalevel = int
67 and irect = (int * int * int * int)
68 and trimparams = (trimmargins * irect)
69 and colorspace = | Rgb | Bgr | Gray
70 and fitmodel = | FitWidth | FitProportional | FitPage
71 and haspbo = bool
72 and uri = string
73 and caption = string
76 type x = int
77 and y = int
78 and tilex = int
79 and tiley = int
80 and tileparams = (x * y * width * height * tilex * tiley)
83 type link =
84 | Lnotfound
85 | Lfound of int
86 and linkdir =
87 | LDfirst
88 | LDlast
89 | LDfirstvisible of (int * int * int)
90 | LDleft of int
91 | LDright of int
92 | LDdown of int
93 | LDup of int
96 type pagewithlinks =
97 | Pwlnotfound
98 | Pwl of int
101 type keymap =
102 | KMinsrt of key
103 | KMinsrl of key list
104 | KMmulti of key list * key list
105 and key = int * int
106 and keyhash = (key, keymap) Hashtbl.t
107 and keystate =
108 | KSnone
109 | KSinto of (key list * key list)
112 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
113 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
115 type pipe = (Unix.file_descr * Unix.file_descr);;
117 external init : pipe -> params -> unit = "ml_init";;
118 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
119 external hassel : opaque -> bool = "ml_hassel";;
120 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
121 external getpdimrect : int -> float array = "ml_getpdimrect";;
122 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
123 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
124 external clearmark : opaque -> unit = "ml_clearmark";;
125 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
126 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
127 external measurestr : int -> string -> float = "ml_measure_string";;
128 external postprocess :
129 opaque -> int -> int -> int -> (int * string * int) -> int
130 = "ml_postprocess";;
131 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
132 external platform : unit -> platform = "ml_platform";;
133 external setaalevel : int -> unit = "ml_setaalevel";;
134 external realloctexts : int -> bool = "ml_realloctexts";;
135 external findlink : opaque -> linkdir -> link = "ml_findlink";;
136 external getlink : opaque -> int -> under = "ml_getlink";;
137 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
138 external getlinkcount : opaque -> int = "ml_getlinkcount";;
139 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
140 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
141 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
142 external freepbo : opaque -> unit = "ml_freepbo";;
143 external unmappbo : opaque -> unit = "ml_unmappbo";;
144 external pbousable : unit -> bool = "ml_pbo_usable";;
145 external unproject : opaque -> int -> int -> (int * int) option
146 = "ml_unproject";;
147 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
148 external rectofblock : opaque -> int -> int -> float array option
149 = "ml_rectofblock";;
150 external fz_version : unit -> string = "ml_fz_version";;
151 external begintiles : unit -> unit = "ml_begintiles";;
152 external endtiles : unit -> unit = "ml_endtiles";;
154 let platform_to_string = function
155 | Punknown -> "unknown"
156 | Plinux -> "Linux"
157 | Posx -> "OSX"
158 | Psun -> "Sun"
159 | Pfreebsd -> "FreeBSD"
160 | Pdragonflybsd -> "DragonflyBSD"
161 | Popenbsd -> "OpenBSD"
162 | Pnetbsd -> "NetBSD"
163 | Pcygwin -> "Cygwin"
166 let platform = platform ();;
168 let now = Unix.gettimeofday;;
170 let selfexec = ref "";;
172 let popen cmd fda =
173 if platform = Pcygwin
174 then (
175 let sh = "/bin/sh" in
176 let args = [|sh; "-c"; cmd|] in
177 let rec std si so se = function
178 | [] -> si, so, se
179 | (fd, 0) :: rest -> std fd so se rest
180 | (fd, -1) :: rest ->
181 Unix.set_close_on_exec fd;
182 std si so se rest
183 | (_, n) :: _ ->
184 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
186 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
187 ignore (Unix.create_process sh args si so se)
189 else popen cmd fda;
192 type mpos = int * int
193 and mstate =
194 | Msel of (mpos * mpos)
195 | Mpan of mpos
196 | Mscrolly | Mscrollx
197 | Mzoom of (int * int)
198 | Mzoomrect of (mpos * mpos)
199 | Mnone
202 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
203 and onkey = string -> int -> te
204 and ondone = string -> unit
205 and histcancel = unit -> unit
206 and onhist = ((histcmd -> string) * histcancel)
207 and histcmd = HCnext | HCprev | HCfirst | HClast
208 and cancelonempty = bool
209 and te =
210 | TEstop
211 | TEdone of string
212 | TEcont of string
213 | TEswitch of textentry
216 type 'a circbuf =
217 { store : 'a array
218 ; mutable rc : int
219 ; mutable wc : int
220 ; mutable len : int
224 let bound v minv maxv =
225 max minv (min maxv v);
228 let cbnew n v =
229 { store = Array.create n v
230 ; rc = 0
231 ; wc = 0
232 ; len = 0
236 let cbcap b = Array.length b.store;;
238 let cbput b v =
239 let cap = cbcap b in
240 b.store.(b.wc) <- v;
241 b.wc <- (b.wc + 1) mod cap;
242 b.rc <- b.wc;
243 b.len <- min (b.len + 1) cap;
246 let cbempty b = b.len = 0;;
248 let cbgetg b circular dir =
249 if cbempty b
250 then b.store.(0)
251 else
252 let rc = b.rc + dir in
253 let rc =
254 if circular
255 then (
256 if rc = -1
257 then b.len-1
258 else (
259 if rc >= b.len
260 then 0
261 else rc
264 else bound rc 0 (b.len-1)
266 b.rc <- rc;
267 b.store.(rc);
270 let cbget b = cbgetg b false;;
271 let cbgetc b = cbgetg b true;;
273 let drawstring size x y s =
274 Gl.enable `blend;
275 Gl.enable `texture_2d;
276 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
277 ignore (drawstr size x y s);
278 Gl.disable `blend;
279 Gl.disable `texture_2d;
282 let drawstring1 size x y s =
283 drawstr size x y s;
286 let drawstring2 size x y fmt =
287 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
290 type page =
291 { pageno : int
292 ; pagedimno : int
293 ; pagew : int
294 ; pageh : int
295 ; pagex : int
296 ; pagey : int
297 ; pagevw : int
298 ; pagevh : int
299 ; pagedispx : int
300 ; pagedispy : int
301 ; pagecol : int
305 let debugl l =
306 dolog "l %d dim=%d {" l.pageno l.pagedimno;
307 dolog " WxH %dx%d" l.pagew l.pageh;
308 dolog " vWxH %dx%d" l.pagevw l.pagevh;
309 dolog " pagex,y %d,%d" l.pagex l.pagey;
310 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
311 dolog " column %d" l.pagecol;
312 dolog "}";
315 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
316 dolog "rect {";
317 dolog " x0,y0=(% f, % f)" x0 y0;
318 dolog " x1,y1=(% f, % f)" x1 y1;
319 dolog " x2,y2=(% f, % f)" x2 y2;
320 dolog " x3,y3=(% f, % f)" x3 y3;
321 dolog "}";
324 type multicolumns = multicol * pagegeom
325 and singlecolumn = pagegeom
326 and splitcolumns = columncount * pagegeom
327 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
328 and multicol = columncount * covercount * covercount
329 and pdimno = int
330 and columncount = int
331 and covercount = int;;
333 type scrollb = int;;
334 let scrollbvv = 1;;
335 let scrollbhv = 2;;
337 type conf =
338 { mutable scrollbw : int
339 ; mutable scrollh : int
340 ; mutable scrollb : scrollb
341 ; mutable icase : bool
342 ; mutable preload : bool
343 ; mutable pagebias : int
344 ; mutable verbose : bool
345 ; mutable debug : bool
346 ; mutable scrollstep : int
347 ; mutable hscrollstep : int
348 ; mutable maxhfit : bool
349 ; mutable crophack : bool
350 ; mutable autoscrollstep : int
351 ; mutable maxwait : float option
352 ; mutable hlinks : bool
353 ; mutable underinfo : bool
354 ; mutable interpagespace : interpagespace
355 ; mutable zoom : float
356 ; mutable presentation : bool
357 ; mutable angle : angle
358 ; mutable cwinw : int
359 ; mutable cwinh : int
360 ; mutable savebmarks : bool
361 ; mutable fitmodel : fitmodel
362 ; mutable trimmargins : trimmargins
363 ; mutable trimfuzz : irect
364 ; mutable memlimit : memsize
365 ; mutable texcount : texcount
366 ; mutable sliceheight : sliceheight
367 ; mutable thumbw : width
368 ; mutable jumpback : bool
369 ; mutable bgcolor : (float * float * float)
370 ; mutable bedefault : bool
371 ; mutable tilew : int
372 ; mutable tileh : int
373 ; mutable mustoresize : memsize
374 ; mutable checkers : bool
375 ; mutable aalevel : int
376 ; mutable urilauncher : string
377 ; mutable pathlauncher : string
378 ; mutable colorspace : colorspace
379 ; mutable invert : bool
380 ; mutable colorscale : float
381 ; mutable redirectstderr : bool
382 ; mutable ghyllscroll : (int * int * int) option
383 ; mutable columns : columns
384 ; mutable beyecolumns : columncount option
385 ; mutable selcmd : string
386 ; mutable paxcmd : string
387 ; mutable updatecurs : bool
388 ; mutable keyhashes : (string * keyhash) list
389 ; mutable hfsize : int
390 ; mutable pgscale : float
391 ; mutable usepbo : bool
392 ; mutable wheelbypage : bool
393 ; mutable stcmd : string
394 ; mutable riani : bool
395 ; mutable pax : (float * int * int) ref option
396 ; mutable paxmark : mark
398 and columns =
399 | Csingle of singlecolumn
400 | Cmulti of multicolumns
401 | Csplit of splitcolumns
404 type anchor = pageno * top * dtop;;
406 type outlinekind =
407 | Onone
408 | Oanchor of anchor
409 | Ouri of uri
410 | Olaunch of launchcommand
411 | Oremote of (filename * pageno)
412 | Oremotedest of (filename * destname)
413 and outline = (caption * outlinelevel * outlinekind)
414 and outlinelevel = int
417 type rect = float * float * float * float * float * float * float * float;;
419 type tile = opaque * pixmapsize * elapsed
420 and elapsed = float;;
421 type pagemapkey = pageno * gen;;
422 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
423 and row = int
424 and col = int;;
426 let emptyanchor = (0, 0.0, 0.0);;
428 type infochange = | Memused | Docinfo | Pdim;;
430 class type uioh = object
431 method display : unit
432 method key : int -> int -> uioh
433 method button : int -> bool -> int -> int -> int -> uioh
434 method multiclick : int -> int -> int -> int -> uioh
435 method motion : int -> int -> uioh
436 method pmotion : int -> int -> uioh
437 method infochanged : infochange -> unit
438 method scrollpw : (int * float * float)
439 method scrollph : (int * float * float)
440 method modehash : keyhash
441 method eformsgs : bool
442 end;;
444 type mode =
445 | Birdseye of (conf * leftx * pageno * pageno * anchor)
446 | Textentry of (textentry * onleave)
447 | View
448 | LinkNav of linktarget
449 and onleave = leavetextentrystatus -> unit
450 and leavetextentrystatus = | Cancel | Confirm
451 and helpitem = string * int * action
452 and action =
453 | Noaction
454 | Action of (uioh -> uioh)
455 and linktarget =
456 | Ltexact of (pageno * int)
457 | Ltgendir of int
460 let isbirdseye = function Birdseye _ -> true | _ -> false;;
461 let istextentry = function Textentry _ -> true | _ -> false;;
463 type currently =
464 | Idle
465 | Loading of (page * gen)
466 | Tiling of (
467 page * opaque * colorspace * angle * gen * col * row * width * height
469 | Outlining of outline list
472 let emptykeyhash = Hashtbl.create 0;;
473 let nouioh : uioh = object (self)
474 method display = ()
475 method key _ _ = self
476 method multiclick _ _ _ _ = self
477 method button _ _ _ _ _ = self
478 method motion _ _ = self
479 method pmotion _ _ = self
480 method infochanged _ = ()
481 method scrollpw = (0, nan, nan)
482 method scrollph = (0, nan, nan)
483 method modehash = emptykeyhash
484 method eformsgs = false
485 end;;
487 type state =
488 { mutable sr : Unix.file_descr
489 ; mutable sw : Unix.file_descr
490 ; mutable wsfd : Unix.file_descr
491 ; mutable errfd : Unix.file_descr option
492 ; mutable stderr : Unix.file_descr
493 ; mutable errmsgs : Buffer.t
494 ; mutable newerrmsgs : bool
495 ; mutable w : int
496 ; mutable x : int
497 ; mutable y : int
498 ; mutable anchor : anchor
499 ; mutable ranchors : (string * string * anchor * string) list
500 ; mutable maxy : int
501 ; mutable layout : page list
502 ; pagemap : (pagemapkey, opaque) Hashtbl.t
503 ; tilemap : (tilemapkey, tile) Hashtbl.t
504 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
505 ; mutable pdims : (pageno * width * height * leftx) list
506 ; mutable pagecount : int
507 ; mutable currently : currently
508 ; mutable mstate : mstate
509 ; mutable searchpattern : string
510 ; mutable rects : (pageno * recttype * rect) list
511 ; mutable rects1 : (pageno * recttype * rect) list
512 ; mutable text : string
513 ; mutable winstate : Wsi.winstate list
514 ; mutable mode : mode
515 ; mutable uioh : uioh
516 ; mutable outlines : outline array
517 ; mutable bookmarks : outline list
518 ; mutable path : string
519 ; mutable password : string
520 ; mutable nameddest : string
521 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
522 ; mutable memused : memsize
523 ; mutable gen : gen
524 ; mutable throttle : (page list * int * float) option
525 ; mutable autoscroll : int option
526 ; mutable ghyll : (int option -> unit)
527 ; mutable help : helpitem array
528 ; mutable docinfo : (int * string) list
529 ; mutable texid : GlTex.texture_id option
530 ; hists : hists
531 ; mutable prevzoom : (float * int)
532 ; mutable progress : float
533 ; mutable redisplay : bool
534 ; mutable mpos : mpos
535 ; mutable keystate : keystate
536 ; mutable glinks : bool
537 ; mutable prevcolumns : (columns * float) option
538 ; mutable winw : int
539 ; mutable winh : int
540 ; mutable reprf : (unit -> unit)
541 ; mutable origin : string
542 ; mutable roam : (unit -> unit)
543 ; mutable bzoom : bool
544 ; mutable traw : [`float] Raw.t
545 ; mutable vraw : [`float] Raw.t
547 and hists =
548 { pat : string circbuf
549 ; pag : string circbuf
550 ; nav : anchor circbuf
551 ; sel : string circbuf
555 let defconf =
556 { scrollbw = 7
557 ; scrollh = 12
558 ; scrollb = scrollbhv lor scrollbvv
559 ; icase = true
560 ; preload = true
561 ; pagebias = 0
562 ; verbose = false
563 ; debug = false
564 ; scrollstep = 24
565 ; hscrollstep = 24
566 ; maxhfit = true
567 ; crophack = false
568 ; autoscrollstep = 2
569 ; maxwait = None
570 ; hlinks = false
571 ; underinfo = false
572 ; interpagespace = 2
573 ; zoom = 1.0
574 ; presentation = false
575 ; angle = 0
576 ; cwinw = 900
577 ; cwinh = 900
578 ; savebmarks = true
579 ; fitmodel = FitProportional
580 ; trimmargins = false
581 ; trimfuzz = (0,0,0,0)
582 ; memlimit = 32 lsl 20
583 ; texcount = 256
584 ; sliceheight = 24
585 ; thumbw = 76
586 ; jumpback = true
587 ; bgcolor = (0.5, 0.5, 0.5)
588 ; bedefault = false
589 ; tilew = 2048
590 ; tileh = 2048
591 ; mustoresize = 256 lsl 20
592 ; checkers = true
593 ; aalevel = 8
594 ; urilauncher =
595 (match platform with
596 | Plinux | Pfreebsd | Pdragonflybsd
597 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
598 | Posx -> "open \"%s\""
599 | Pcygwin -> "cygstart \"%s\""
600 | Punknown -> "echo %s")
601 ; pathlauncher = "lp \"%s\""
602 ; selcmd =
603 (match platform with
604 | Plinux | Pfreebsd | Pdragonflybsd
605 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
606 | Posx -> "pbcopy"
607 | Pcygwin -> "wsel"
608 | Punknown -> "cat")
609 ; paxcmd = "cat"
610 ; colorspace = Rgb
611 ; invert = false
612 ; colorscale = 1.0
613 ; redirectstderr = false
614 ; ghyllscroll = None
615 ; columns = Csingle [||]
616 ; beyecolumns = None
617 ; updatecurs = false
618 ; hfsize = 12
619 ; pgscale = 1.0
620 ; usepbo = false
621 ; wheelbypage = false
622 ; stcmd = "echo SyncTex"
623 ; riani = false
624 ; pax = None
625 ; paxmark = Mark_word
626 ; keyhashes =
627 let mk n = (n, Hashtbl.create 1) in
628 [ mk "global"
629 ; mk "info"
630 ; mk "help"
631 ; mk "outline"
632 ; mk "listview"
633 ; mk "birdseye"
634 ; mk "textentry"
635 ; mk "links"
636 ; mk "view"
641 let wtmode = ref false;;
642 let cxack = ref false;;
644 let findkeyhash c name =
645 try List.assoc name c.keyhashes
646 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
649 let conf = { defconf with angle = defconf.angle };;
651 let pgscale h = truncate (float h *. conf.pgscale);;
653 type fontstate =
654 { mutable fontsize : int
655 ; mutable wwidth : float
656 ; mutable maxrows : int
660 let fstate =
661 { fontsize = 14
662 ; wwidth = nan
663 ; maxrows = -1
667 let geturl s =
668 let colonpos = try String.index s ':' with Not_found -> -1 in
669 let len = String.length s in
670 if colonpos >= 0 && colonpos + 3 < len
671 then (
672 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
673 then
674 let schemestartpos =
675 try String.rindex_from s colonpos ' '
676 with Not_found -> -1
678 let scheme =
679 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
681 match scheme with
682 | "http" | "ftp" | "mailto" ->
683 let epos =
684 try String.index_from s colonpos ' '
685 with Not_found -> len
687 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
688 | _ -> ""
689 else ""
691 else ""
694 let gotouri uri =
695 if emptystr conf.urilauncher
696 then print_endline uri
697 else (
698 let url = geturl uri in
699 if emptystr url
700 then Printf.eprintf "obtained empty url from uri %S" uri
701 else
702 let re = Str.regexp "%s" in
703 let command = Str.global_replace re url conf.urilauncher in
704 try popen command []
705 with exn ->
706 Printf.eprintf
707 "failed to execute `%s': %s\n" command (exntos exn);
708 flush stderr;
712 let version () =
713 Printf.sprintf "llpp version %s, fitz %s, ocaml %s (%s/%dbit)"
714 Help.version (fz_version ()) Sys.ocaml_version
715 (platform_to_string platform) Sys.word_size
718 let makehelp () =
719 let strings = version () :: "" :: Help.keys in
720 Array.of_list (
721 List.map (fun s ->
722 let url = geturl s in
723 if nonemptystr url
724 then (s, 0, Action (fun u -> gotouri url; u))
725 else (s, 0, Noaction)
726 ) strings);
729 let noghyll _ = ();;
730 let firstgeomcmds = "", [];;
731 let noreprf () = ();;
732 let noroam () = ();;
734 let state =
735 { sr = Unix.stdin
736 ; sw = Unix.stdin
737 ; wsfd = Unix.stdin
738 ; errfd = None
739 ; stderr = Unix.stderr
740 ; errmsgs = Buffer.create 0
741 ; newerrmsgs = false
742 ; x = 0
743 ; y = 0
744 ; w = 0
745 ; anchor = emptyanchor
746 ; ranchors = []
747 ; layout = []
748 ; maxy = max_int
749 ; tilelru = Queue.create ()
750 ; pagemap = Hashtbl.create 10
751 ; tilemap = Hashtbl.create 10
752 ; pdims = []
753 ; pagecount = 0
754 ; currently = Idle
755 ; mstate = Mnone
756 ; rects = []
757 ; rects1 = []
758 ; text = ""
759 ; mode = View
760 ; winstate = []
761 ; searchpattern = ""
762 ; outlines = [||]
763 ; bookmarks = []
764 ; path = ""
765 ; password = ""
766 ; nameddest = ""
767 ; geomcmds = firstgeomcmds
768 ; hists =
769 { nav = cbnew 10 emptyanchor
770 ; pat = cbnew 10 ""
771 ; pag = cbnew 10 ""
772 ; sel = cbnew 10 ""
774 ; memused = 0
775 ; gen = 0
776 ; throttle = None
777 ; autoscroll = None
778 ; ghyll = noghyll
779 ; help = makehelp ()
780 ; docinfo = []
781 ; texid = None
782 ; prevzoom = (1.0, 0)
783 ; progress = -1.0
784 ; uioh = nouioh
785 ; redisplay = true
786 ; mpos = (-1, -1)
787 ; keystate = KSnone
788 ; glinks = false
789 ; prevcolumns = None
790 ; winw = -1
791 ; winh = -1
792 ; reprf = noreprf
793 ; origin = ""
794 ; roam = noroam
795 ; bzoom = false
796 ; traw = Raw.create_static `float 8
797 ; vraw = Raw.create_static `float 8
801 let hscrollh () =
802 if (conf.scrollb land scrollbhv = 0)
803 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
804 then 0
805 else conf.scrollbw
808 let vscrollw () =
809 if (conf.scrollb land scrollbvv = 0)
810 then 0
811 else conf.scrollbw
814 let wadjsb w = w - vscrollw ();;
816 let setfontsize n =
817 fstate.fontsize <- n;
818 fstate.wwidth <- measurestr fstate.fontsize "w";
819 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
822 let vlog fmt =
823 if conf.verbose
824 then
825 Printf.kprintf prerr_endline fmt
826 else
827 Printf.kprintf ignore fmt
830 let launchpath () =
831 if emptystr conf.pathlauncher
832 then print_endline state.path
833 else (
834 let re = Str.regexp "%s" in
835 let command = Str.global_replace re state.path conf.pathlauncher in
836 try popen command []
837 with exn ->
838 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
839 flush stderr;
843 module Ne = struct
844 type 'a t = | Res of 'a | Exn of exn;;
846 let res f =
847 try Res (f ())
848 with exn -> Exn exn
851 let clo fd f =
852 try tempfailureretry Unix.close fd
853 with exn -> f (exntos exn)
856 let dup fd =
857 try Res (tempfailureretry Unix.dup fd)
858 with exn -> Exn exn
861 let dup2 fd1 fd2 =
862 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
863 with exn -> Exn exn
865 end;;
867 let redirectstderr () =
868 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
869 if conf.redirectstderr
870 then
871 match Ne.res Unix.pipe with
872 | Ne.Exn exn ->
873 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
875 | Ne.Res (r, w) ->
876 begin match Ne.dup Unix.stderr with
877 | Ne.Exn exn ->
878 dolog "failed to dup stderr: %s" (exntos exn);
879 Ne.clo r (clofail "pipe/r");
880 Ne.clo w (clofail "pipe/w");
882 | Ne.Res dupstderr ->
883 begin match Ne.dup2 w Unix.stderr with
884 | Ne.Exn exn ->
885 dolog "failed to dup2 to stderr: %s" (exntos exn);
886 Ne.clo dupstderr (clofail "stderr duplicate");
887 Ne.clo r (clofail "redir pipe/r");
888 Ne.clo w (clofail "redir pipe/w");
890 | Ne.Res () ->
891 state.stderr <- dupstderr;
892 state.errfd <- Some r;
893 end;
895 else (
896 state.newerrmsgs <- false;
897 begin match state.errfd with
898 | Some fd ->
899 begin match Ne.dup2 state.stderr Unix.stderr with
900 | Ne.Exn exn ->
901 dolog "failed to dup2 original stderr: %s" (exntos exn)
902 | Ne.Res () ->
903 Ne.clo fd (clofail "dup of stderr");
904 state.errfd <- None;
905 end;
906 | None -> ()
907 end;
908 prerr_string (Buffer.contents state.errmsgs);
909 flush stderr;
910 Buffer.clear state.errmsgs;
914 module G =
915 struct
916 let postRedisplay who =
917 if conf.verbose
918 then prerr_endline ("redisplay for " ^ who);
919 state.redisplay <- true;
921 end;;
923 let getopaque pageno =
924 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
925 with Not_found -> None
928 let putopaque pageno opaque =
929 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
932 let pagetranslatepoint l x y =
933 let dy = y - l.pagedispy in
934 let y = dy + l.pagey in
935 let dx = x - l.pagedispx in
936 let x = dx + l.pagex in
937 (x, y);
940 let onppundermouse g x y d =
941 let rec f = function
942 | l :: rest ->
943 begin match getopaque l.pageno with
944 | Some opaque ->
945 let x0 = l.pagedispx in
946 let x1 = x0 + l.pagevw in
947 let y0 = l.pagedispy in
948 let y1 = y0 + l.pagevh in
949 if y >= y0 && y <= y1 && x >= x0 && x <= x1
950 then
951 let px, py = pagetranslatepoint l x y in
952 match g opaque l px py with
953 | Some res -> res
954 | None -> f rest
955 else f rest
956 | _ ->
957 f rest
959 | [] -> d
961 f state.layout
964 let getunder x y =
965 let g opaque l px py =
966 if state.bzoom
967 then (
968 match rectofblock opaque px py with
969 | Some a ->
970 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
971 state.rects <- [l.pageno, l.pageno mod 3, rect];
972 G.postRedisplay "getunder";
973 | None -> ()
975 match whatsunder opaque px py with
976 | Unone -> None
977 | under -> Some under
979 onppundermouse g x y Unone
982 let unproject x y =
983 let g opaque l x y =
984 match unproject opaque x y with
985 | Some (x, y) -> Some (Some (l.pageno, x, y))
986 | None -> None
988 onppundermouse g x y None;
991 let showtext c s =
992 state.text <- Printf.sprintf "%c%s" c s;
993 G.postRedisplay "showtext";
996 let pipesel opaque cmd =
997 if hassel opaque
998 then
999 match Ne.res Unix.pipe with
1000 | Ne.Exn exn ->
1001 showtext '!'
1002 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
1003 | Ne.Res (r, w) ->
1004 let doclose what fd =
1005 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
1007 let popened =
1008 try popen cmd [r, 0; w, -1]; true
1009 with exn ->
1010 dolog "can not execute %S: %s" cmd (exntos exn);
1011 false
1013 if popened
1014 then (
1015 copysel w opaque;
1016 G.postRedisplay "pipesel";
1018 else doclose "pipesel pipe/w" w;
1019 doclose "pipesel pipe/r" r;
1022 let paxunder x y =
1023 let g opaque l px py =
1024 if markunder opaque px py conf.paxmark
1025 then (
1026 Some (fun () ->
1027 match getopaque l.pageno with
1028 | None -> ()
1029 | Some opaque -> pipesel opaque conf.paxcmd
1032 else None
1034 G.postRedisplay "paxunder";
1035 if conf.paxmark = Mark_page
1036 then
1037 List.iter (fun l ->
1038 match getopaque l.pageno with
1039 | None -> ()
1040 | Some opaque -> clearmark opaque) state.layout;
1041 state.roam <-
1042 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
1045 let selstring s =
1046 match Ne.res Unix.pipe with
1047 | Ne.Exn exn ->
1048 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
1049 | Ne.Res (r, w) ->
1050 let clo cap fd =
1051 Ne.clo fd (fun msg ->
1052 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1055 let popened =
1056 try popen conf.selcmd [r, 0; w, -1]; true
1057 with exn ->
1058 showtext '!'
1059 (Printf.sprintf "failed to execute %s: %s"
1060 conf.selcmd (exntos exn));
1061 false
1063 if popened
1064 then (
1066 let l = String.length s in
1067 let n = tempfailureretry (Unix.write w s 0) l in
1068 if n != l
1069 then
1070 showtext '!'
1071 (Printf.sprintf
1072 "failed to write %d characters to sel pipe, wrote %d"
1075 with exn ->
1076 showtext '!'
1077 (Printf.sprintf "failed to write to sel pipe: %s"
1078 (exntos exn)
1081 else dolog "%s" s;
1082 clo "selstring pipe/r" r;
1083 clo "selstring pipe/w" w;
1086 let undertext = function
1087 | Unone -> "none"
1088 | Ulinkuri s -> s
1089 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1090 | Utext s -> "font: " ^ s
1091 | Uunexpected s -> "unexpected: " ^ s
1092 | Ulaunch s -> "launch: " ^ s
1093 | Unamed s -> "named: " ^ s
1094 | Uremote (filename, pageno) ->
1095 Printf.sprintf "%s: page %d" filename (pageno+1)
1096 | Uremotedest (filename, destname) ->
1097 Printf.sprintf "%s: destination %S" filename destname
1100 let updateunder x y =
1101 match getunder x y with
1102 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1103 | Ulinkuri uri ->
1104 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1105 Wsi.setcursor Wsi.CURSOR_INFO
1106 | Ulinkgoto (pageno, _) ->
1107 if conf.underinfo
1108 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1109 Wsi.setcursor Wsi.CURSOR_INFO
1110 | Utext s ->
1111 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1112 Wsi.setcursor Wsi.CURSOR_TEXT
1113 | Uunexpected s ->
1114 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1115 Wsi.setcursor Wsi.CURSOR_INHERIT
1116 | Ulaunch s ->
1117 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1118 Wsi.setcursor Wsi.CURSOR_INHERIT
1119 | Unamed s ->
1120 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1121 Wsi.setcursor Wsi.CURSOR_INHERIT
1122 | Uremote (filename, pageno) ->
1123 if conf.underinfo then showtext 'r'
1124 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1125 Wsi.setcursor Wsi.CURSOR_INFO
1126 | Uremotedest (filename, destname) ->
1127 if conf.underinfo then showtext 'r'
1128 (Printf.sprintf "emote destination: %s (%S)" filename destname);
1129 Wsi.setcursor Wsi.CURSOR_INFO
1132 let showlinktype under =
1133 if conf.underinfo
1134 then
1135 match under with
1136 | Unone -> ()
1137 | under ->
1138 let s = undertext under in
1139 showtext ' ' s
1142 let addchar s c =
1143 let b = Buffer.create (String.length s + 1) in
1144 Buffer.add_string b s;
1145 Buffer.add_char b c;
1146 Buffer.contents b;
1149 module type TextEnumType =
1151 type t
1152 val name : string
1153 val names : string array
1154 end;;
1156 module TextEnumMake (Ten : TextEnumType) =
1157 struct
1158 let names = Ten.names;;
1159 let to_int (t : Ten.t) = Obj.magic t;;
1160 let to_string t = names.(to_int t);;
1161 let of_int n : Ten.t = Obj.magic n;;
1162 let of_string s =
1163 let rec find i =
1164 if i = Array.length names
1165 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1166 else (
1167 if Ten.names.(i) = s
1168 then of_int i
1169 else find (i+1)
1171 in find 0;;
1172 end;;
1174 module CSTE = TextEnumMake (struct
1175 type t = colorspace;;
1176 let name = "colorspace";;
1177 let names = [|"rgb"; "bgr"; "gray"|];;
1178 end);;
1180 module MTE = TextEnumMake (struct
1181 type t = mark;;
1182 let name = "mark";;
1183 let names = [|"page"; "block"; "line"; "word"|];;
1184 end);;
1186 module FMTE = TextEnumMake (struct
1187 type t = fitmodel;;
1188 let name = "fitmodel";;
1189 let names = [|"width"; "proportional"; "page"|];;
1190 end);;
1192 let intentry_with_suffix text key =
1193 let c =
1194 if key >= 32 && key < 127
1195 then Char.chr key
1196 else '\000'
1198 match Char.lowercase c with
1199 | '0' .. '9' ->
1200 let text = addchar text c in
1201 TEcont text
1203 | 'k' | 'm' | 'g' ->
1204 let text = addchar text c in
1205 TEcont text
1207 | _ ->
1208 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1209 TEcont text
1212 let multicolumns_to_string (n, a, b) =
1213 if a = 0 && b = 0
1214 then Printf.sprintf "%d" n
1215 else Printf.sprintf "%d,%d,%d" n a b;
1218 let multicolumns_of_string s =
1220 (int_of_string s, 0, 0)
1221 with _ ->
1222 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1223 if a > 1 || b > 1
1224 then failwith "subtly broken"; (n, a, b)
1228 let readcmd fd =
1229 let s = "xxxx" in
1230 let n = tempfailureretry (Unix.read fd s 0) 4 in
1231 if n != 4 then error "incomplete read(len) = %d" n;
1232 let len = 0
1233 lor (Char.code s.[0] lsl 24)
1234 lor (Char.code s.[1] lsl 16)
1235 lor (Char.code s.[2] lsl 8)
1236 lor (Char.code s.[3] lsl 0)
1238 let s = String.create len in
1239 let n = tempfailureretry (Unix.read fd s 0) len in
1240 if n != len then error "incomplete read(data) %d vs %d" n len;
1244 let btod b = if b then 1 else 0;;
1246 let wcmd fmt =
1247 let b = Buffer.create 16 in
1248 Buffer.add_string b "llll";
1249 Printf.kbprintf
1250 (fun b ->
1251 let s = Buffer.contents b in
1252 let n = String.length s in
1253 let len = n - 4 in
1254 (* dolog "wcmd %S" (String.sub s 4 len); *)
1255 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1256 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1257 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1258 s.[3] <- Char.chr (len land 0xff);
1259 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1260 if n' != n then error "write failed %d vs %d" n' n;
1261 ) b fmt;
1264 let calcips h =
1265 let d = state.winh - h in
1266 max conf.interpagespace ((d + 1) / 2)
1269 let rowyh (c, coverA, coverB) b n =
1270 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1271 then
1272 let _, _, vy, (_, _, h, _) = b.(n) in
1273 (vy, h)
1274 else
1275 let n' = n - coverA in
1276 let d = n' mod c in
1277 let s = n - d in
1278 let e = min state.pagecount (s + c) in
1279 let rec find m miny maxh = if m = e then miny, maxh else
1280 let _, _, y, (_, _, h, _) = b.(m) in
1281 let miny = min miny y in
1282 let maxh = max maxh h in
1283 find (m+1) miny maxh
1284 in find s max_int 0
1287 let calcheight () =
1288 match conf.columns with
1289 | Cmulti ((_, _, _) as cl, b) ->
1290 if Array.length b > 0
1291 then
1292 let y, h = rowyh cl b (Array.length b - 1) in
1293 y + h + (if conf.presentation then calcips h else 0)
1294 else 0
1295 | Csingle b ->
1296 if Array.length b > 0
1297 then
1298 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1299 y + h + (if conf.presentation then calcips h else 0)
1300 else 0
1301 | Csplit (_, b) ->
1302 if Array.length b > 0
1303 then
1304 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1305 y + h
1306 else 0
1309 let getpageywh pageno =
1310 let pageno = bound pageno 0 (state.pagecount-1) in
1311 match conf.columns with
1312 | Csingle b ->
1313 if Array.length b = 0
1314 then 0, 0, 0
1315 else
1316 let (_, _, y, (_, w, h, _)) = b.(pageno) in
1317 let y =
1318 if conf.presentation
1319 then y - calcips h
1320 else y
1322 y, w, h
1323 | Cmulti (cl, b) ->
1324 if Array.length b = 0
1325 then 0, 0, 0
1326 else
1327 let y, h = rowyh cl b pageno in
1328 let (_, _, _, (_, w, _, _)) = b.(pageno) in
1329 let y =
1330 if conf.presentation
1331 then y - calcips h
1332 else y
1334 y, w, h
1335 | Csplit (c, b) ->
1336 if Array.length b = 0
1337 then 0, 0, 0
1338 else
1339 let n = pageno*c in
1340 let (_, _, y, (_, w, h, _)) = b.(n) in
1341 y, w / c, h
1344 let getpageyh pageno =
1345 let y,_,h = getpageywh pageno in
1346 y, h;
1349 let getpagedim pageno =
1350 let rec f ppdim l =
1351 match l with
1352 | (n, _, _, _) as pdim :: rest ->
1353 if n >= pageno
1354 then (if n = pageno then pdim else ppdim)
1355 else f pdim rest
1357 | [] -> ppdim
1359 f (-1, -1, -1, -1) state.pdims
1362 let getpagey pageno = fst (getpageyh pageno);;
1364 let nogeomcmds cmds =
1365 match cmds with
1366 | s, [] -> emptystr s
1367 | _ -> false
1370 let page_of_y y =
1371 let ((c, coverA, coverB) as cl), b =
1372 match conf.columns with
1373 | Csingle b -> (1, 0, 0), b
1374 | Cmulti (c, b) -> c, b
1375 | Csplit (_, b) -> (1, 0, 0), b
1377 if Array.length b = 0
1378 then -1
1379 else
1380 let rec bsearch nmin nmax =
1381 if nmin > nmax
1382 then bound nmin 0 (state.pagecount-1)
1383 else
1384 let n = (nmax + nmin) / 2 in
1385 let vy, h = rowyh cl b n in
1386 let y0, y1 =
1387 if conf.presentation
1388 then
1389 let ips = calcips h in
1390 let y0 = vy - ips in
1391 let y1 = vy + h + ips in
1392 y0, y1
1393 else (
1394 if n = 0
1395 then 0, vy + h + conf.interpagespace
1396 else
1397 let y0 = vy - conf.interpagespace in
1398 y0, y0 + h + conf.interpagespace
1401 if y >= y0 && y < y1
1402 then (
1403 if c = 1
1404 then n
1405 else (
1406 if n > coverA
1407 then
1408 if n < state.pagecount - coverB
1409 then ((n-coverA)/c)*c + coverA
1410 else n
1411 else n
1414 else (
1415 if y > y0
1416 then bsearch (n+1) nmax
1417 else bsearch nmin (n-1)
1420 bsearch 0 (state.pagecount-1);
1423 let layoutN ((columns, coverA, coverB), b) y sh =
1424 let sh = sh - (hscrollh ()) in
1425 let rec fold accu n =
1426 if n = Array.length b
1427 then accu
1428 else
1429 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1430 if (vy - y) > sh &&
1431 (n = coverA - 1
1432 || n = state.pagecount - coverB
1433 || (n - coverA) mod columns = columns - 1)
1434 then accu
1435 else
1436 let accu =
1437 if vy + h > y
1438 then
1439 let pagey = max 0 (y - vy) in
1440 let pagedispy = if pagey > 0 then 0 else vy - y in
1441 let pagedispx, pagex =
1442 let pdx =
1443 if n = coverA - 1 || n = state.pagecount - coverB
1444 then state.x + (wadjsb state.winw - w) / 2
1445 else dx + xoff + state.x
1447 if pdx < 0
1448 then 0, -pdx
1449 else pdx, 0
1451 let pagevw =
1452 let vw = wadjsb state.winw - pagedispx in
1453 let pw = w - pagex in
1454 min vw pw
1456 let pagevh = min (h - pagey) (sh - pagedispy) in
1457 if pagevw > 0 && pagevh > 0
1458 then
1459 let e =
1460 { pageno = n
1461 ; pagedimno = pdimno
1462 ; pagew = w
1463 ; pageh = h
1464 ; pagex = pagex
1465 ; pagey = pagey
1466 ; pagevw = pagevw
1467 ; pagevh = pagevh
1468 ; pagedispx = pagedispx
1469 ; pagedispy = pagedispy
1470 ; pagecol = 0
1473 e :: accu
1474 else
1475 accu
1476 else
1477 accu
1479 fold accu (n+1)
1481 if Array.length b = 0
1482 then []
1483 else List.rev (fold [] (page_of_y y))
1486 let layoutS (columns, b) y sh =
1487 let sh = sh - hscrollh () in
1488 let rec fold accu n =
1489 if n = Array.length b
1490 then accu
1491 else
1492 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1493 if (vy - y) > sh
1494 then accu
1495 else
1496 let accu =
1497 if vy + pageh > y
1498 then
1499 let x = xoff + state.x in
1500 let pagey = max 0 (y - vy) in
1501 let pagedispy = if pagey > 0 then 0 else vy - y in
1502 let pagedispx, pagex =
1503 if px = 0
1504 then (
1505 if x < 0
1506 then 0, -x
1507 else x, 0
1509 else (
1510 let px = px - x in
1511 if px < 0
1512 then -px, 0
1513 else 0, px
1516 let pagecolw = pagew/columns in
1517 let pagedispx =
1518 if pagecolw < state.winw
1519 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1520 else pagedispx
1522 let pagevw =
1523 let vw = wadjsb state.winw - pagedispx in
1524 let pw = pagew - pagex in
1525 min vw pw
1527 let pagevw = min pagevw pagecolw in
1528 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1529 if pagevw > 0 && pagevh > 0
1530 then
1531 let e =
1532 { pageno = n/columns
1533 ; pagedimno = pdimno
1534 ; pagew = pagew
1535 ; pageh = pageh
1536 ; pagex = pagex
1537 ; pagey = pagey
1538 ; pagevw = pagevw
1539 ; pagevh = pagevh
1540 ; pagedispx = pagedispx
1541 ; pagedispy = pagedispy
1542 ; pagecol = n mod columns
1545 e :: accu
1546 else
1547 accu
1548 else
1549 accu
1551 fold accu (n+1)
1553 List.rev (fold [] 0)
1556 let layout y sh =
1557 if nogeomcmds state.geomcmds
1558 then
1559 match conf.columns with
1560 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1561 | Cmulti c -> layoutN c y sh
1562 | Csplit s -> layoutS s y sh
1563 else []
1566 let clamp incr =
1567 let y = state.y + incr in
1568 let y = max 0 y in
1569 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1573 let itertiles l f =
1574 let tilex = l.pagex mod conf.tilew in
1575 let tiley = l.pagey mod conf.tileh in
1577 let col = l.pagex / conf.tilew in
1578 let row = l.pagey / conf.tileh in
1580 let rec rowloop row y0 dispy h =
1581 if h = 0
1582 then ()
1583 else (
1584 let dh = conf.tileh - y0 in
1585 let dh = min h dh in
1586 let rec colloop col x0 dispx w =
1587 if w = 0
1588 then ()
1589 else (
1590 let dw = conf.tilew - x0 in
1591 let dw = min w dw in
1593 f col row dispx dispy x0 y0 dw dh;
1594 colloop (col+1) 0 (dispx+dw) (w-dw)
1597 colloop col tilex l.pagedispx l.pagevw;
1598 rowloop (row+1) 0 (dispy+dh) (h-dh)
1601 if l.pagevw > 0 && l.pagevh > 0
1602 then rowloop row tiley l.pagedispy l.pagevh;
1605 let gettileopaque l col row =
1606 let key =
1607 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1609 try Some (Hashtbl.find state.tilemap key)
1610 with Not_found -> None
1613 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1614 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1615 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1618 let filledrect x0 y0 x1 y1 =
1619 GlArray.disable `texture_coord;
1620 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1621 GlArray.vertex `two state.vraw;
1622 GlArray.draw_arrays `triangle_strip 0 4;
1623 GlArray.enable `texture_coord;
1626 let linerect x0 y0 x1 y1 =
1627 GlArray.disable `texture_coord;
1628 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
1629 GlArray.vertex `two state.vraw;
1630 GlArray.draw_arrays `line_loop 0 4;
1631 GlArray.enable `texture_coord;
1634 let drawtiles l color =
1635 GlDraw.color color;
1636 begintiles ();
1637 let f col row x y tilex tiley w h =
1638 match gettileopaque l col row with
1639 | Some (opaque, _, t) ->
1640 let params = x, y, w, h, tilex, tiley in
1641 if conf.invert
1642 then (
1643 Gl.enable `blend;
1644 GlFunc.blend_func `zero `one_minus_src_color;
1646 drawtile params opaque;
1647 if conf.invert
1648 then Gl.disable `blend;
1649 if conf.debug
1650 then (
1651 endtiles ();
1652 let s = Printf.sprintf
1653 "%d[%d,%d] %f sec"
1654 l.pageno col row t
1656 let w = measurestr fstate.fontsize s in
1657 GlDraw.color (0.0, 0.0, 0.0);
1658 filledrect (float (x-2))
1659 (float (y-2))
1660 (float (x+2) +. w)
1661 (float (y + fstate.fontsize + 2));
1662 GlDraw.color (1.0, 1.0, 1.0);
1663 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1664 begintiles ();
1667 | None ->
1668 endtiles ();
1669 let w =
1670 let lw = wadjsb state.winw - x in
1671 min lw w
1672 and h =
1673 let lh = state.winh - y in
1674 min lh h
1676 if conf.invert
1677 then (
1678 Gl.enable `blend;
1679 GlFunc.blend_func `zero `one_minus_src_color;
1681 begin match state.texid with
1682 | Some id ->
1683 Gl.enable `texture_2d;
1684 GlTex.bind_texture `texture_2d id;
1685 let x0 = float x
1686 and y0 = float y
1687 and x1 = float (x+w)
1688 and y1 = float (y+h) in
1690 let tw = float w /. 16.0
1691 and th = float h /. 16.0 in
1692 let tx0 = float tilex /. 16.0
1693 and ty0 = float tiley /. 16.0 in
1694 let tx1 = tx0 +. tw
1695 and ty1 = ty0 +. th in
1696 Raw.sets_float state.vraw ~pos:0
1697 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1698 Raw.sets_float state.traw ~pos:0
1699 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
1700 GlArray.vertex `two state.vraw;
1701 GlArray.tex_coord `two state.traw;
1702 GlArray.draw_arrays `triangle_strip 0 4;
1704 | None ->
1705 GlDraw.color (1.0, 1.0, 1.0);
1706 filledrect (float x) (float y) (float (x+w)) (float (y+h));
1707 end;
1708 if w > 128 && h > fstate.fontsize + 10
1709 then (
1710 let c = if conf.invert then 1.0 else 0.0 in
1711 GlDraw.color (c, c, c);
1712 let c, r =
1713 if conf.verbose
1714 then (col*conf.tilew, row*conf.tileh)
1715 else col, row
1717 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1719 GlDraw.color color;
1720 begintiles ();
1722 itertiles l f;
1723 endtiles ();
1726 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1728 let tilevisible1 l x y =
1729 let ax0 = l.pagex
1730 and ax1 = l.pagex + l.pagevw
1731 and ay0 = l.pagey
1732 and ay1 = l.pagey + l.pagevh in
1734 let bx0 = x
1735 and by0 = y in
1736 let bx1 = min (bx0 + conf.tilew) l.pagew
1737 and by1 = min (by0 + conf.tileh) l.pageh in
1739 let rx0 = max ax0 bx0
1740 and ry0 = max ay0 by0
1741 and rx1 = min ax1 bx1
1742 and ry1 = min ay1 by1 in
1744 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1745 nonemptyintersection
1748 let tilevisible layout n x y =
1749 let rec findpageinlayout m = function
1750 | l :: rest when l.pageno = n ->
1751 tilevisible1 l x y || (
1752 match conf.columns with
1753 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1754 | _ -> false
1756 | _ :: rest -> findpageinlayout 0 rest
1757 | [] -> false
1759 findpageinlayout 0 layout;
1762 let tileready l x y =
1763 tilevisible1 l x y &&
1764 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1767 let tilepage n p layout =
1768 let rec loop = function
1769 | l :: rest ->
1770 if l.pageno = n
1771 then
1772 let f col row _ _ _ _ _ _ =
1773 if state.currently = Idle
1774 then
1775 match gettileopaque l col row with
1776 | Some _ -> ()
1777 | None ->
1778 let x = col*conf.tilew
1779 and y = row*conf.tileh in
1780 let w =
1781 let w = l.pagew - x in
1782 min w conf.tilew
1784 let h =
1785 let h = l.pageh - y in
1786 min h conf.tileh
1788 let pbo =
1789 if conf.usepbo
1790 then getpbo w h conf.colorspace
1791 else ~< "0"
1793 wcmd "tile %s %d %d %d %d %s"
1794 (~> p) x y w h (~> pbo);
1795 state.currently <-
1796 Tiling (
1797 l, p, conf.colorspace, conf.angle,
1798 state.gen, col, row, conf.tilew, conf.tileh
1801 itertiles l f;
1802 else
1803 loop rest
1805 | [] -> ()
1807 if nogeomcmds state.geomcmds
1808 then loop layout;
1811 let preloadlayout y =
1812 let y = if y < state.winh then 0 else y - state.winh in
1813 let h = state.winh*3 in
1814 layout y h;
1817 let load pages =
1818 let rec loop pages =
1819 if state.currently != Idle
1820 then ()
1821 else
1822 match pages with
1823 | l :: rest ->
1824 begin match getopaque l.pageno with
1825 | None ->
1826 wcmd "page %d %d" l.pageno l.pagedimno;
1827 state.currently <- Loading (l, state.gen);
1828 | Some opaque ->
1829 tilepage l.pageno opaque pages;
1830 loop rest
1831 end;
1832 | _ -> ()
1834 if nogeomcmds state.geomcmds
1835 then loop pages
1838 let preload pages =
1839 load pages;
1840 if conf.preload && state.currently = Idle
1841 then load (preloadlayout state.y);
1844 let layoutready layout =
1845 let rec fold all ls =
1846 all && match ls with
1847 | l :: rest ->
1848 let seen = ref false in
1849 let allvisible = ref true in
1850 let foo col row _ _ _ _ _ _ =
1851 seen := true;
1852 allvisible := !allvisible &&
1853 begin match gettileopaque l col row with
1854 | Some _ -> true
1855 | None -> false
1858 itertiles l foo;
1859 fold (!seen && !allvisible) rest
1860 | [] -> true
1862 let alltilesvisible = fold true layout in
1863 alltilesvisible;
1866 let gotoy y =
1867 let y = bound y 0 state.maxy in
1868 let y, layout, proceed =
1869 match conf.maxwait with
1870 | Some time when state.ghyll == noghyll ->
1871 begin match state.throttle with
1872 | None ->
1873 let layout = layout y state.winh in
1874 let ready = layoutready layout in
1875 if not ready
1876 then (
1877 load layout;
1878 state.throttle <- Some (layout, y, now ());
1880 else G.postRedisplay "gotoy showall (None)";
1881 y, layout, ready
1882 | Some (_, _, started) ->
1883 let dt = now () -. started in
1884 if dt > time
1885 then (
1886 state.throttle <- None;
1887 let layout = layout y state.winh in
1888 load layout;
1889 G.postRedisplay "maxwait";
1890 y, layout, true
1892 else -1, [], false
1895 | _ ->
1896 let layout = layout y state.winh in
1897 if not !wtmode || layoutready layout
1898 then G.postRedisplay "gotoy ready";
1899 y, layout, true
1901 if proceed
1902 then (
1903 state.y <- y;
1904 state.layout <- layout;
1905 begin match state.mode with
1906 | LinkNav (Ltexact (pageno, linkno)) ->
1907 let rec loop = function
1908 | [] ->
1909 state.mode <- LinkNav (Ltgendir 0)
1910 | l :: _ when l.pageno = pageno ->
1911 begin match getopaque pageno with
1912 | None ->
1913 state.mode <- LinkNav (Ltgendir 0)
1914 | Some opaque ->
1915 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1916 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1917 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1918 then state.mode <- LinkNav (Ltgendir 0)
1920 | _ :: rest -> loop rest
1922 loop layout
1923 | _ -> ()
1924 end;
1925 begin match state.mode with
1926 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1927 if not (pagevisible layout pageno)
1928 then (
1929 match state.layout with
1930 | [] -> ()
1931 | l :: _ ->
1932 state.mode <- Birdseye (
1933 conf, leftx, l.pageno, hooverpageno, anchor
1936 | LinkNav (Ltgendir dir as lt) ->
1937 let linknav =
1938 let rec loop = function
1939 | [] -> lt
1940 | l :: rest ->
1941 match getopaque l.pageno with
1942 | None -> loop rest
1943 | Some opaque ->
1944 let link =
1945 let ld =
1946 if dir = 0
1947 then LDfirstvisible (l.pagex, l.pagey, dir)
1948 else (
1949 if dir > 0 then LDfirst else LDlast
1952 findlink opaque ld
1954 match link with
1955 | Lnotfound -> loop rest
1956 | Lfound n ->
1957 showlinktype (getlink opaque n);
1958 Ltexact (l.pageno, n)
1960 loop state.layout
1962 state.mode <- LinkNav linknav
1963 | _ -> ()
1964 end;
1965 preload layout;
1967 state.ghyll <- noghyll;
1968 if conf.updatecurs
1969 then (
1970 let mx, my = state.mpos in
1971 updateunder mx my;
1975 let conttiling pageno opaque =
1976 tilepage pageno opaque
1977 (if conf.preload then preloadlayout state.y else state.layout)
1980 let gotoy_and_clear_text y =
1981 if not conf.verbose then state.text <- "";
1982 gotoy y;
1985 let getanchor1 l =
1986 let top =
1987 let coloff = l.pagecol * l.pageh in
1988 float (l.pagey + coloff) /. float l.pageh
1990 let dtop =
1991 if l.pagedispy = 0
1992 then
1994 else (
1995 if conf.presentation
1996 then float l.pagedispy /. float (calcips l.pageh)
1997 else float l.pagedispy /. float conf.interpagespace
2000 (l.pageno, top, dtop)
2003 let getanchor () =
2004 match state.layout with
2005 | l :: _ -> getanchor1 l
2006 | [] ->
2007 let n = page_of_y state.y in
2008 if n = -1
2009 then state.anchor
2010 else
2011 let y, h = getpageyh n in
2012 let dy = y - state.y in
2013 let dtop =
2014 if conf.presentation
2015 then
2016 let ips = calcips h in
2017 float (dy + ips) /. float ips
2018 else
2019 float dy /. float conf.interpagespace
2021 (n, 0.0, dtop)
2024 let getanchory (n, top, dtop) =
2025 let y, h = getpageyh n in
2026 if conf.presentation
2027 then
2028 let ips = calcips h in
2029 y + truncate (top*.float h -. dtop*.float ips) + ips;
2030 else
2031 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
2034 let gotoanchor anchor =
2035 gotoy (getanchory anchor);
2038 let addnav () =
2039 cbput state.hists.nav (getanchor ());
2042 let getnav dir =
2043 let anchor = cbgetc state.hists.nav dir in
2044 getanchory anchor;
2047 let gotoghyll y =
2048 let scroll f n a b =
2049 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
2050 let snake f a b =
2051 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
2052 if f < a
2053 then s (float f /. float a)
2054 else (
2055 if f > b
2056 then 1.0 -. s ((float (f-b) /. float (n-b)))
2057 else 1.0
2060 snake f a b
2061 and summa n a b =
2062 let ins = float a *. 0.5
2063 and outs = float (n-b) *. 0.5 in
2064 let ones = b - a in
2065 ins +. outs +. float ones
2067 let rec set (_N, _A, _B) y sy =
2068 let sum = summa _N _A _B in
2069 let dy = float (y - sy) in
2070 state.ghyll <- (
2071 let rec gf n y1 o =
2072 if n >= _N
2073 then state.ghyll <- noghyll
2074 else
2075 let go n =
2076 let s = scroll n _N _A _B in
2077 let y1 = y1 +. ((s *. dy) /. sum) in
2078 gotoy_and_clear_text (truncate y1);
2079 state.ghyll <- gf (n+1) y1;
2081 match o with
2082 | None -> go n
2083 | Some y' -> set (_N/2, 1, 1) y' state.y
2085 gf 0 (float state.y)
2088 match conf.ghyllscroll with
2089 | None ->
2090 gotoy_and_clear_text y
2091 | Some nab ->
2092 if state.ghyll == noghyll
2093 then set nab y state.y
2094 else state.ghyll (Some y)
2097 let gotopage n top =
2098 let y, h = getpageyh n in
2099 let y = y + (truncate (top *. float h)) in
2100 gotoghyll y
2103 let gotopage1 n top =
2104 let y = getpagey n in
2105 let y = y + top in
2106 gotoghyll y
2109 let invalidate s f =
2110 state.layout <- [];
2111 state.pdims <- [];
2112 state.rects <- [];
2113 state.rects1 <- [];
2114 match state.geomcmds with
2115 | ps, [] when emptystr ps ->
2116 f ();
2117 state.geomcmds <- s, [];
2119 | ps, [] ->
2120 state.geomcmds <- ps, [s, f];
2122 | ps, (s', _) :: rest when s' = s ->
2123 state.geomcmds <- ps, ((s, f) :: rest);
2125 | ps, cmds ->
2126 state.geomcmds <- ps, ((s, f) :: cmds);
2129 let flushpages () =
2130 Hashtbl.iter (fun _ opaque ->
2131 wcmd "freepage %s" (~> opaque);
2132 ) state.pagemap;
2133 Hashtbl.clear state.pagemap;
2136 let flushtiles () =
2137 if not (Queue.is_empty state.tilelru)
2138 then (
2139 Queue.iter (fun (k, p, s) ->
2140 wcmd "freetile %s" (~> p);
2141 state.memused <- state.memused - s;
2142 Hashtbl.remove state.tilemap k;
2143 ) state.tilelru;
2144 state.uioh#infochanged Memused;
2145 Queue.clear state.tilelru;
2147 load state.layout;
2150 let stateh h =
2151 let h = truncate (float h*.conf.zoom) in
2152 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2153 h - d
2156 let opendoc path password =
2157 state.path <- path;
2158 state.password <- password;
2159 state.gen <- state.gen + 1;
2160 state.docinfo <- [];
2162 flushpages ();
2163 setaalevel conf.aalevel;
2164 let titlepath =
2165 if emptystr state.origin
2166 then path
2167 else state.origin
2169 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2170 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2171 invalidate "reqlayout"
2172 (fun () ->
2173 wcmd "reqlayout %d %d %d %s\000"
2174 conf.angle (FMTE.to_int conf.fitmodel)
2175 (stateh state.winh) state.nameddest
2179 let reload () =
2180 state.anchor <- getanchor ();
2181 opendoc state.path state.password;
2184 let scalecolor c =
2185 let c = c *. conf.colorscale in
2186 (c, c, c);
2189 let scalecolor2 (r, g, b) =
2190 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2193 let docolumns = function
2194 | Csingle _ ->
2195 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2196 let rec loop pageno pdimno pdim y ph pdims =
2197 if pageno = state.pagecount
2198 then ()
2199 else
2200 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2201 match pdims with
2202 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2203 pdimno+1, pdim, rest
2204 | _ ->
2205 pdimno, pdim, pdims
2207 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2208 let y = y +
2209 (if conf.presentation
2210 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2211 else (if pageno = 0 then 0 else conf.interpagespace)
2214 a.(pageno) <- (pdimno, x, y, pdim);
2215 loop (pageno+1) pdimno pdim (y + h) h pdims
2217 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2218 conf.columns <- Csingle a;
2220 | Cmulti ((columns, coverA, coverB), _) ->
2221 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2222 let rec loop pageno pdimno pdim x y rowh pdims =
2223 let rec fixrow m = if m = pageno then () else
2224 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2225 if h < rowh
2226 then (
2227 let y = y + (rowh - h) / 2 in
2228 a.(m) <- (pdimno, x, y, pdim);
2230 fixrow (m+1)
2232 if pageno = state.pagecount
2233 then fixrow (((pageno - 1) / columns) * columns)
2234 else
2235 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2236 match pdims with
2237 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2238 pdimno+1, pdim, rest
2239 | _ ->
2240 pdimno, pdim, pdims
2242 let x, y, rowh' =
2243 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2244 then (
2245 let x = (wadjsb state.winw - w) / 2 in
2246 let ips =
2247 if conf.presentation then calcips h else conf.interpagespace in
2248 x, y + ips + rowh, h
2250 else (
2251 if (pageno - coverA) mod columns = 0
2252 then (
2253 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2254 let y =
2255 if conf.presentation
2256 then
2257 let ips = calcips h in
2258 y + (if pageno = 0 then 0 else calcips rowh + ips)
2259 else
2260 y + (if pageno = 0 then 0 else conf.interpagespace)
2262 x, y + rowh, h
2264 else x, y, max rowh h
2267 let y =
2268 if pageno > 1 && (pageno - coverA) mod columns = 0
2269 then (
2270 let y =
2271 if pageno = columns && conf.presentation
2272 then (
2273 let ips = calcips rowh in
2274 for i = 0 to pred columns
2276 let (pdimno, x, y, pdim) = a.(i) in
2277 a.(i) <- (pdimno, x, y+ips, pdim)
2278 done;
2279 y+ips;
2281 else y
2283 fixrow (pageno - columns);
2286 else y
2288 a.(pageno) <- (pdimno, x, y, pdim);
2289 let x = x + w + xoff*2 + conf.interpagespace in
2290 loop (pageno+1) pdimno pdim x y rowh' pdims
2292 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2293 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2295 | Csplit (c, _) ->
2296 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2297 let rec loop pageno pdimno pdim y pdims =
2298 if pageno = state.pagecount
2299 then ()
2300 else
2301 let pdimno, ((_, w, h, _) as pdim), pdims =
2302 match pdims with
2303 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2304 pdimno+1, pdim, rest
2305 | _ ->
2306 pdimno, pdim, pdims
2308 let cw = w / c in
2309 let rec loop1 n x y =
2310 if n = c then y else (
2311 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2312 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2315 let y = loop1 0 0 y in
2316 loop (pageno+1) pdimno pdim y pdims
2318 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2319 conf.columns <- Csplit (c, a);
2322 let represent () =
2323 docolumns conf.columns;
2324 state.maxy <- calcheight ();
2325 if state.reprf == noreprf
2326 then (
2327 match state.mode with
2328 | Birdseye (_, _, pageno, _, _) ->
2329 let y, h = getpageyh pageno in
2330 let top = (state.winh - h) / 2 in
2331 gotoy (max 0 (y - top))
2332 | _ -> gotoanchor state.anchor
2334 else (
2335 state.reprf ();
2336 state.reprf <- noreprf;
2340 let reshape w h =
2341 GlDraw.viewport 0 0 w h;
2342 let firsttime = state.geomcmds == firstgeomcmds in
2343 if not firsttime && nogeomcmds state.geomcmds
2344 then state.anchor <- getanchor ();
2346 state.winw <- w;
2347 let w = wadjsb (truncate (float w *. conf.zoom)) in
2348 let w = max w 2 in
2349 state.winh <- h;
2350 setfontsize fstate.fontsize;
2351 GlMat.mode `modelview;
2352 GlMat.load_identity ();
2354 GlMat.mode `projection;
2355 GlMat.load_identity ();
2356 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2357 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2358 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2360 let relx =
2361 if conf.zoom <= 1.0
2362 then 0.0
2363 else float state.x /. float state.w
2365 invalidate "geometry"
2366 (fun () ->
2367 state.w <- w;
2368 if not firsttime
2369 then state.x <- truncate (relx *. float w);
2370 let w =
2371 match conf.columns with
2372 | Csingle _ -> w
2373 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2374 | Csplit (c, _) -> w * c
2376 wcmd "geometry %d %d %d"
2377 w (stateh h) (FMTE.to_int conf.fitmodel)
2381 let enttext () =
2382 let len = String.length state.text in
2383 let drawstring s =
2384 let hscrollh =
2385 match state.mode with
2386 | Textentry _ | View | LinkNav _ ->
2387 let h, _, _ = state.uioh#scrollpw in
2389 | _ -> 0
2391 let rect x w =
2392 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2393 (x+.w) (float (state.winh - hscrollh))
2396 let w = float (wadjsb state.winw - 1) in
2397 if state.progress >= 0.0 && state.progress < 1.0
2398 then (
2399 GlDraw.color (0.3, 0.3, 0.3);
2400 let w1 = w *. state.progress in
2401 rect 0.0 w1;
2402 GlDraw.color (0.0, 0.0, 0.0);
2403 rect w1 (w-.w1)
2405 else (
2406 GlDraw.color (0.0, 0.0, 0.0);
2407 rect 0.0 w;
2410 GlDraw.color (1.0, 1.0, 1.0);
2411 drawstring fstate.fontsize
2412 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2414 let s =
2415 match state.mode with
2416 | Textentry ((prefix, text, _, _, _, _), _) ->
2417 let s =
2418 if len > 0
2419 then
2420 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2421 else
2422 Printf.sprintf "%s%s_" prefix text
2426 | _ -> state.text
2428 let s =
2429 if state.newerrmsgs
2430 then (
2431 if not (istextentry state.mode) && state.uioh#eformsgs
2432 then
2433 let s1 = "(press 'e' to review error messasges)" in
2434 if nonemptystr s then s ^ " " ^ s1 else s1
2435 else s
2437 else s
2439 if nonemptystr s
2440 then drawstring s
2443 let gctiles () =
2444 let len = Queue.length state.tilelru in
2445 let layout = lazy (
2446 match state.throttle with
2447 | None ->
2448 if conf.preload
2449 then preloadlayout state.y
2450 else state.layout
2451 | Some (layout, _, _) ->
2452 layout
2453 ) in
2454 let rec loop qpos =
2455 if state.memused <= conf.memlimit
2456 then ()
2457 else (
2458 if qpos < len
2459 then
2460 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2461 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2462 let (_, pw, ph, _) = getpagedim n in
2464 gen = state.gen
2465 && colorspace = conf.colorspace
2466 && angle = conf.angle
2467 && pagew = pw
2468 && pageh = ph
2469 && (
2470 let x = col*conf.tilew
2471 and y = row*conf.tileh in
2472 tilevisible (Lazy.force_val layout) n x y
2474 then Queue.push lruitem state.tilelru
2475 else (
2476 freepbo p;
2477 wcmd "freetile %s" (~> p);
2478 state.memused <- state.memused - s;
2479 state.uioh#infochanged Memused;
2480 Hashtbl.remove state.tilemap k;
2482 loop (qpos+1)
2485 loop 0
2488 let logcurrently = function
2489 | Idle -> dolog "Idle"
2490 | Loading (l, gen) ->
2491 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2492 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2493 dolog
2494 "Tiling %d[%d,%d] page=%s cs=%s angle"
2495 l.pageno col row (~> pageopaque)
2496 (CSTE.to_string colorspace)
2498 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2499 angle gen conf.angle state.gen
2500 tilew tileh
2501 conf.tilew conf.tileh
2503 | Outlining _ ->
2504 dolog "outlining"
2507 let splitatspace =
2508 let r = Str.regexp " " in
2509 fun s -> Str.bounded_split r s 2;
2512 let onpagerect pageno f =
2513 let b =
2514 match conf.columns with
2515 | Cmulti (_, b) -> b
2516 | Csingle b -> b
2517 | Csplit (_, b) -> b
2519 if pageno >= 0 && pageno < Array.length b
2520 then
2521 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2522 f w h
2525 let gotopagexy1 pageno x y =
2526 let _,w1,h1,leftx = getpagedim pageno in
2527 let top = y /. (float h1) in
2528 let left = x /. (float w1) in
2529 let py, w, h = getpageywh pageno in
2530 let wh = state.winh - hscrollh () in
2531 let x = left *. (float w) in
2532 let x = leftx + state.x + truncate x in
2533 let sx =
2534 if x < 0 || x >= wadjsb state.winw
2535 then state.x - x
2536 else state.x
2538 let pdy = truncate (top *. float h) in
2539 let y' = py + pdy in
2540 let dy = y' - state.y in
2541 let sy =
2542 if x != state.x || not (dy > 0 && dy < wh)
2543 then (
2544 if conf.presentation
2545 then
2546 if abs (py - y') > wh
2547 then y'
2548 else py
2549 else y';
2551 else state.y
2553 if state.x != sx || state.y != sy
2554 then (
2555 let x, y =
2556 if !wtmode
2557 then (
2558 let ww = wadjsb state.winw in
2559 let qx = sx / ww
2560 and qy = pdy / wh in
2561 let x = qx * ww
2562 and y = py + qy * wh in
2563 let x = if -x + ww > w1 then -(w1-ww) else x
2564 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2565 let y =
2566 if conf.presentation
2567 then
2568 if abs (py - y') > wh
2569 then y'
2570 else py
2571 else y';
2573 (x, y)
2575 else (sx, sy)
2577 state.x <- x;
2578 gotoy_and_clear_text y;
2580 else gotoy_and_clear_text state.y;
2583 let gotopagexy pageno x y =
2584 match state.mode with
2585 | Birdseye _ -> gotopage pageno 0.0
2586 | _ -> gotopagexy1 pageno x y
2589 let act cmds =
2590 (* dolog "%S" cmds; *)
2591 let cl = splitatspace cmds in
2592 let scan s fmt f =
2593 try Scanf.sscanf s fmt f
2594 with exn ->
2595 dolog "error processing '%S': %s" cmds (exntos exn);
2596 exit 1
2598 let addoutline outline =
2599 match state.currently with
2600 | Outlining outlines ->
2601 state.currently <- Outlining (outline :: outlines)
2602 | Idle -> state.currently <- Outlining [outline]
2603 | currently ->
2604 dolog "invalid outlining state";
2605 logcurrently currently
2607 match cl with
2608 | "clear" :: [] ->
2609 state.uioh#infochanged Pdim;
2610 state.pdims <- [];
2612 | "clearrects" :: [] ->
2613 state.rects <- state.rects1;
2614 G.postRedisplay "clearrects";
2616 | "continue" :: args :: [] ->
2617 let n = scan args "%u" (fun n -> n) in
2618 state.pagecount <- n;
2619 begin match state.currently with
2620 | Outlining l ->
2621 state.currently <- Idle;
2622 state.outlines <- Array.of_list (List.rev l)
2623 | _ -> ()
2624 end;
2626 let cur, cmds = state.geomcmds in
2627 if emptystr cur
2628 then failwith "umpossible";
2630 begin match List.rev cmds with
2631 | [] ->
2632 state.geomcmds <- "", [];
2633 state.throttle <- None;
2634 represent ();
2635 | (s, f) :: rest ->
2636 f ();
2637 state.geomcmds <- s, List.rev rest;
2638 end;
2639 if conf.maxwait = None && not !wtmode
2640 then G.postRedisplay "continue";
2642 | "title" :: args :: [] ->
2643 Wsi.settitle args
2645 | "msg" :: args :: [] ->
2646 showtext ' ' args
2648 | "vmsg" :: args :: [] ->
2649 if conf.verbose
2650 then showtext ' ' args
2652 | "emsg" :: args :: [] ->
2653 Buffer.add_string state.errmsgs args;
2654 state.newerrmsgs <- true;
2655 G.postRedisplay "error message"
2657 | "progress" :: args :: [] ->
2658 let progress, text =
2659 scan args "%f %n"
2660 (fun f pos ->
2661 f, String.sub args pos (String.length args - pos))
2663 state.text <- text;
2664 state.progress <- progress;
2665 G.postRedisplay "progress"
2667 | "firstmatch" :: args :: [] ->
2668 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2669 scan args "%u %d %f %f %f %f %f %f %f %f"
2670 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2671 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2673 let y = (getpagey pageno) + truncate y0 in
2674 addnav ();
2675 gotoy y;
2676 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2678 | "match" :: args :: [] ->
2679 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2680 scan args "%u %d %f %f %f %f %f %f %f %f"
2681 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2682 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2684 state.rects1 <-
2685 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2687 | "page" :: args :: [] ->
2688 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
2689 let pageopaque = ~< pageopaques in
2690 begin match state.currently with
2691 | Loading (l, gen) ->
2692 vlog "page %d took %f sec" l.pageno t;
2693 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2694 begin match state.throttle with
2695 | None ->
2696 let preloadedpages =
2697 if conf.preload
2698 then preloadlayout state.y
2699 else state.layout
2701 let evict () =
2702 let set =
2703 List.fold_left (fun s l -> IntSet.add l.pageno s)
2704 IntSet.empty preloadedpages
2706 let evictedpages =
2707 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2708 if not (IntSet.mem pageno set)
2709 then (
2710 wcmd "freepage %s" (~> opaque);
2711 key :: accu
2713 else accu
2714 ) state.pagemap []
2716 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2718 evict ();
2719 state.currently <- Idle;
2720 if gen = state.gen
2721 then (
2722 tilepage l.pageno pageopaque state.layout;
2723 load state.layout;
2724 load preloadedpages;
2725 if pagevisible state.layout l.pageno
2726 && layoutready state.layout
2727 then G.postRedisplay "page";
2730 | Some (layout, _, _) ->
2731 state.currently <- Idle;
2732 tilepage l.pageno pageopaque layout;
2733 load state.layout
2734 end;
2736 | _ ->
2737 dolog "Inconsistent loading state";
2738 logcurrently state.currently;
2739 exit 1
2742 | "tile" :: args :: [] ->
2743 let (x, y, opaques, size, t) =
2744 scan args "%u %u %s %u %f"
2745 (fun x y p size t -> (x, y, p, size, t))
2747 let opaque = ~< opaques in
2748 begin match state.currently with
2749 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2750 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2752 unmappbo opaque;
2753 if tilew != conf.tilew || tileh != conf.tileh
2754 then (
2755 wcmd "freetile %s" (~> opaque);
2756 state.currently <- Idle;
2757 load state.layout;
2759 else (
2760 puttileopaque l col row gen cs angle opaque size t;
2761 state.memused <- state.memused + size;
2762 state.uioh#infochanged Memused;
2763 gctiles ();
2764 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2765 opaque, size) state.tilelru;
2767 let layout =
2768 match state.throttle with
2769 | None -> state.layout
2770 | Some (layout, _, _) -> layout
2773 state.currently <- Idle;
2774 if gen = state.gen
2775 && conf.colorspace = cs
2776 && conf.angle = angle
2777 && tilevisible layout l.pageno x y
2778 then conttiling l.pageno pageopaque;
2780 begin match state.throttle with
2781 | None ->
2782 preload state.layout;
2783 if gen = state.gen
2784 && conf.colorspace = cs
2785 && conf.angle = angle
2786 && tilevisible state.layout l.pageno x y
2787 && (not !wtmode || layoutready state.layout)
2788 then G.postRedisplay "tile nothrottle";
2790 | Some (layout, y, _) ->
2791 let ready = layoutready layout in
2792 if ready
2793 then (
2794 state.y <- y;
2795 state.layout <- layout;
2796 state.throttle <- None;
2797 G.postRedisplay "throttle";
2799 else load layout;
2800 end;
2803 | _ ->
2804 dolog "Inconsistent tiling state";
2805 logcurrently state.currently;
2806 exit 1
2809 | "pdim" :: args :: [] ->
2810 let (n, w, h, _) as pdim =
2811 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2813 let pdim =
2814 match conf.fitmodel, conf.columns with
2815 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2816 | _ -> pdim
2818 state.uioh#infochanged Pdim;
2819 state.pdims <- pdim :: state.pdims
2821 | "o" :: args :: [] ->
2822 let (l, n, t, h, pos) =
2823 scan args "%u %u %d %u %n"
2824 (fun l n t h pos -> l, n, t, h, pos)
2826 let s = String.sub args pos (String.length args - pos) in
2827 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
2829 | "ou" :: args :: [] ->
2830 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
2831 let s = String.sub args pos len in
2832 let pos2 = pos + len + 1 in
2833 let uri = String.sub args pos2 (String.length args - pos2) in
2834 addoutline (s, l, Ouri uri)
2836 | "on" :: args :: [] ->
2837 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
2838 let s = String.sub args pos (String.length args - pos) in
2839 addoutline (s, l, Onone)
2841 | "a" :: args :: [] ->
2842 let (n, l, t) =
2843 scan args "%u %d %d" (fun n l t -> n, l, t)
2845 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2847 | "info" :: args :: [] ->
2848 state.docinfo <- (1, args) :: state.docinfo
2850 | "infoend" :: [] ->
2851 state.uioh#infochanged Docinfo;
2852 state.docinfo <- List.rev state.docinfo
2854 | _ ->
2855 error "unknown cmd `%S'" cmds
2858 let onhist cb =
2859 let rc = cb.rc in
2860 let action = function
2861 | HCprev -> cbget cb ~-1
2862 | HCnext -> cbget cb 1
2863 | HCfirst -> cbget cb ~-(cb.rc)
2864 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2865 and cancel () = cb.rc <- rc
2866 in (action, cancel)
2869 let search pattern forward =
2870 match conf.columns with
2871 | Csplit _ ->
2872 showtext '!' "searching does not work properly in split columns mode"
2873 | _ ->
2874 if nonemptystr pattern
2875 then
2876 let pn, py =
2877 match state.layout with
2878 | [] -> 0, 0
2879 | l :: _ ->
2880 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2882 wcmd "search %d %d %d %d,%s\000"
2883 (btod conf.icase) pn py (btod forward) pattern;
2886 let intentry text key =
2887 let c =
2888 if key >= 32 && key < 127
2889 then Char.chr key
2890 else '\000'
2892 match c with
2893 | '0' .. '9' ->
2894 let text = addchar text c in
2895 TEcont text
2897 | _ ->
2898 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2899 TEcont text
2902 let linknentry text key =
2903 let c =
2904 if key >= 32 && key < 127
2905 then Char.chr key
2906 else '\000'
2908 match c with
2909 | 'a' .. 'z' ->
2910 let text = addchar text c in
2911 TEcont text
2913 | _ ->
2914 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2915 TEcont text
2918 let linkndone f s =
2919 if nonemptystr s
2920 then (
2921 let n =
2922 let l = String.length s in
2923 let rec loop pos n = if pos = l then n else
2924 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2925 loop (pos+1) (n*26 + m)
2926 in loop 0 0
2928 let rec loop n = function
2929 | [] -> ()
2930 | l :: rest ->
2931 match getopaque l.pageno with
2932 | None -> loop n rest
2933 | Some opaque ->
2934 let m = getlinkcount opaque in
2935 if n < m
2936 then (
2937 let under = getlink opaque n in
2938 f under
2940 else loop (n-m) rest
2942 loop n state.layout;
2946 let textentry text key =
2947 if key land 0xff00 = 0xff00
2948 then TEcont text
2949 else TEcont (text ^ toutf8 key)
2952 let reqlayout angle fitmodel =
2953 match state.throttle with
2954 | None ->
2955 if nogeomcmds state.geomcmds
2956 then state.anchor <- getanchor ();
2957 conf.angle <- angle mod 360;
2958 if conf.angle != 0
2959 then (
2960 match state.mode with
2961 | LinkNav _ -> state.mode <- View
2962 | _ -> ()
2964 conf.fitmodel <- fitmodel;
2965 invalidate "reqlayout"
2966 (fun () ->
2967 wcmd "reqlayout %d %d %d"
2968 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2970 | _ -> ()
2973 let settrim trimmargins trimfuzz =
2974 if nogeomcmds state.geomcmds
2975 then state.anchor <- getanchor ();
2976 conf.trimmargins <- trimmargins;
2977 conf.trimfuzz <- trimfuzz;
2978 let x0, y0, x1, y1 = trimfuzz in
2979 invalidate "settrim"
2980 (fun () ->
2981 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2982 flushpages ();
2985 let setzoom zoom =
2986 match state.throttle with
2987 | None ->
2988 let zoom = max 0.0001 zoom in
2989 if zoom <> conf.zoom
2990 then (
2991 state.prevzoom <- (conf.zoom, state.x);
2992 conf.zoom <- zoom;
2993 reshape state.winw state.winh;
2994 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2997 | Some (layout, y, started) ->
2998 let time =
2999 match conf.maxwait with
3000 | None -> 0.0
3001 | Some t -> t
3003 let dt = now () -. started in
3004 if dt > time
3005 then (
3006 state.y <- y;
3007 load layout;
3011 let setcolumns mode columns coverA coverB =
3012 state.prevcolumns <- Some (conf.columns, conf.zoom);
3013 if columns < 0
3014 then (
3015 if isbirdseye mode
3016 then showtext '!' "split mode doesn't work in bird's eye"
3017 else (
3018 conf.columns <- Csplit (-columns, [||]);
3019 state.x <- 0;
3020 conf.zoom <- 1.0;
3023 else (
3024 if columns < 2
3025 then (
3026 conf.columns <- Csingle [||];
3027 state.x <- 0;
3028 setzoom 1.0;
3030 else (
3031 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
3032 conf.zoom <- 1.0;
3035 reshape state.winw state.winh;
3038 let enterbirdseye () =
3039 let zoom = float conf.thumbw /. float state.winw in
3040 let birdseyepageno =
3041 let cy = state.winh / 2 in
3042 let fold = function
3043 | [] -> 0
3044 | l :: rest ->
3045 let rec fold best = function
3046 | [] -> best.pageno
3047 | l :: rest ->
3048 let d = cy - (l.pagedispy + l.pagevh/2)
3049 and dbest = cy - (best.pagedispy + best.pagevh/2) in
3050 if abs d < abs dbest
3051 then fold l rest
3052 else best.pageno
3053 in fold l rest
3055 fold state.layout
3057 state.mode <- Birdseye (
3058 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
3060 conf.zoom <- zoom;
3061 conf.presentation <- false;
3062 conf.interpagespace <- 10;
3063 conf.hlinks <- false;
3064 conf.fitmodel <- FitProportional;
3065 state.x <- 0;
3066 state.mstate <- Mnone;
3067 conf.maxwait <- None;
3068 conf.columns <- (
3069 match conf.beyecolumns with
3070 | Some c ->
3071 conf.zoom <- 1.0;
3072 Cmulti ((c, 0, 0), [||])
3073 | None -> Csingle [||]
3075 Wsi.setcursor Wsi.CURSOR_INHERIT;
3076 if conf.verbose
3077 then
3078 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3079 (100.0*.zoom)
3080 else
3081 state.text <- ""
3083 reshape state.winw state.winh;
3086 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3087 state.mode <- View;
3088 conf.zoom <- c.zoom;
3089 conf.presentation <- c.presentation;
3090 conf.interpagespace <- c.interpagespace;
3091 conf.maxwait <- c.maxwait;
3092 conf.hlinks <- c.hlinks;
3093 conf.fitmodel <- c.fitmodel;
3094 conf.beyecolumns <- (
3095 match conf.columns with
3096 | Cmulti ((c, _, _), _) -> Some c
3097 | Csingle _ -> None
3098 | Csplit _ -> failwith "leaving bird's eye split mode"
3100 conf.columns <- (
3101 match c.columns with
3102 | Cmulti (c, _) -> Cmulti (c, [||])
3103 | Csingle _ -> Csingle [||]
3104 | Csplit (c, _) -> Csplit (c, [||])
3106 if conf.verbose
3107 then
3108 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3109 (100.0*.conf.zoom)
3111 reshape state.winw state.winh;
3112 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3113 state.x <- leftx;
3116 let togglebirdseye () =
3117 match state.mode with
3118 | Birdseye vals -> leavebirdseye vals true
3119 | View -> enterbirdseye ()
3120 | _ -> ()
3123 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3124 let pageno = max 0 (pageno - incr) in
3125 let rec loop = function
3126 | [] -> gotopage1 pageno 0
3127 | l :: _ when l.pageno = pageno ->
3128 if l.pagedispy >= 0 && l.pagey = 0
3129 then G.postRedisplay "upbirdseye"
3130 else gotopage1 pageno 0
3131 | _ :: rest -> loop rest
3133 loop state.layout;
3134 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3137 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3138 let pageno = min (state.pagecount - 1) (pageno + incr) in
3139 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3140 let rec loop = function
3141 | [] ->
3142 let y, h = getpageyh pageno in
3143 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3144 gotoy (clamp dy)
3145 | l :: _ when l.pageno = pageno ->
3146 if l.pagevh != l.pageh
3147 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3148 else G.postRedisplay "downbirdseye"
3149 | _ :: rest -> loop rest
3151 loop state.layout
3154 let boundastep h step =
3155 if step < 0
3156 then bound step ~-h 0
3157 else bound step 0 h
3160 let optentry mode _ key =
3161 let btos b = if b then "on" else "off" in
3162 if key >= 32 && key < 127
3163 then
3164 let c = Char.chr key in
3165 match c with
3166 | 's' ->
3167 let ondone s =
3168 try conf.scrollstep <- int_of_string s with exc ->
3169 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3171 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3173 | 'A' ->
3174 let ondone s =
3176 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
3177 if state.autoscroll <> None
3178 then state.autoscroll <- Some conf.autoscrollstep
3179 with exc ->
3180 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3182 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3184 | 'C' ->
3185 let ondone s =
3187 let n, a, b = multicolumns_of_string s in
3188 setcolumns mode n a b;
3189 with exc ->
3190 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3192 TEswitch ("columns: ", "", None, textentry, ondone, true)
3194 | 'Z' ->
3195 let ondone s =
3197 let zoom = float (int_of_string s) /. 100.0 in
3198 setzoom zoom
3199 with exc ->
3200 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3202 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3204 | 't' ->
3205 let ondone s =
3207 conf.thumbw <- bound (int_of_string s) 2 4096;
3208 state.text <-
3209 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3210 begin match mode with
3211 | Birdseye beye ->
3212 leavebirdseye beye false;
3213 enterbirdseye ();
3214 | _ -> ();
3216 with exc ->
3217 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3219 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3221 | 'R' ->
3222 let ondone s =
3223 match try
3224 Some (int_of_string s)
3225 with exc ->
3226 state.text <- Printf.sprintf "bad integer `%s': %s"
3227 s (exntos exc);
3228 None
3229 with
3230 | Some angle -> reqlayout angle conf.fitmodel
3231 | None -> ()
3233 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3235 | 'i' ->
3236 conf.icase <- not conf.icase;
3237 TEdone ("case insensitive search " ^ (btos conf.icase))
3239 | 'p' ->
3240 conf.preload <- not conf.preload;
3241 gotoy state.y;
3242 TEdone ("preload " ^ (btos conf.preload))
3244 | 'v' ->
3245 conf.verbose <- not conf.verbose;
3246 TEdone ("verbose " ^ (btos conf.verbose))
3248 | 'd' ->
3249 conf.debug <- not conf.debug;
3250 TEdone ("debug " ^ (btos conf.debug))
3252 | 'h' ->
3253 conf.maxhfit <- not conf.maxhfit;
3254 state.maxy <- calcheight ();
3255 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3257 | 'c' ->
3258 conf.crophack <- not conf.crophack;
3259 TEdone ("crophack " ^ btos conf.crophack)
3261 | 'a' ->
3262 let s =
3263 match conf.maxwait with
3264 | None ->
3265 conf.maxwait <- Some infinity;
3266 "always wait for page to complete"
3267 | Some _ ->
3268 conf.maxwait <- None;
3269 "show placeholder if page is not ready"
3271 TEdone s
3273 | 'f' ->
3274 conf.underinfo <- not conf.underinfo;
3275 TEdone ("underinfo " ^ btos conf.underinfo)
3277 | 'P' ->
3278 conf.savebmarks <- not conf.savebmarks;
3279 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3281 | 'S' ->
3282 let ondone s =
3284 let pageno, py =
3285 match state.layout with
3286 | [] -> 0, 0
3287 | l :: _ ->
3288 l.pageno, l.pagey
3290 conf.interpagespace <- int_of_string s;
3291 docolumns conf.columns;
3292 state.maxy <- calcheight ();
3293 let y = getpagey pageno in
3294 gotoy (y + py)
3295 with exc ->
3296 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3298 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3300 | 'l' ->
3301 let fm =
3302 match conf.fitmodel with
3303 | FitProportional -> FitWidth
3304 | _ -> FitProportional
3306 reqlayout conf.angle fm;
3307 TEdone ("proportional display " ^ btos (fm == FitProportional))
3309 | 'T' ->
3310 settrim (not conf.trimmargins) conf.trimfuzz;
3311 TEdone ("trim margins " ^ btos conf.trimmargins)
3313 | 'I' ->
3314 conf.invert <- not conf.invert;
3315 TEdone ("invert colors " ^ btos conf.invert)
3317 | 'x' ->
3318 let ondone s =
3319 cbput state.hists.sel s;
3320 conf.selcmd <- s;
3322 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3323 textentry, ondone, true)
3325 | 'M' ->
3326 if conf.pax == None
3327 then conf.pax <- Some (ref (0.0, 0, 0))
3328 else conf.pax <- None;
3329 TEdone ("PAX " ^ btos (conf.pax != None))
3331 | _ ->
3332 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3333 TEstop
3334 else
3335 TEcont state.text
3338 class type lvsource = object
3339 method getitemcount : int
3340 method getitem : int -> (string * int)
3341 method hasaction : int -> bool
3342 method exit :
3343 uioh:uioh ->
3344 cancel:bool ->
3345 active:int ->
3346 first:int ->
3347 pan:int ->
3348 qsearch:string ->
3349 uioh option
3350 method getactive : int
3351 method getfirst : int
3352 method getqsearch : string
3353 method setqsearch : string -> unit
3354 method getpan : int
3355 method getminfo : (int * int) array
3356 end;;
3358 class virtual lvsourcebase = object
3359 val mutable m_active = 0
3360 val mutable m_first = 0
3361 val mutable m_qsearch = ""
3362 val mutable m_pan = 0
3363 method getactive = m_active
3364 method getfirst = m_first
3365 method getqsearch = m_qsearch
3366 method getpan = m_pan
3367 method setqsearch s = m_qsearch <- s
3368 method getminfo : (int * int) array = [||]
3369 end;;
3371 let withoutlastutf8 s =
3372 let len = String.length s in
3373 if len = 0
3374 then s
3375 else
3376 let rec find pos =
3377 if pos = 0
3378 then pos
3379 else
3380 let b = Char.code s.[pos] in
3381 if b land 0b11000000 = 0b11000000
3382 then pos
3383 else find (pos-1)
3385 let first =
3386 if Char.code s.[len-1] land 0x80 = 0
3387 then len-1
3388 else find (len-1)
3390 String.sub s 0 first;
3393 let textentrykeyboard
3394 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3395 let key =
3396 if key >= 0xffb0 && key <= 0xffb9
3397 then key - 0xffb0 + 48 else key
3399 let enttext te =
3400 state.mode <- Textentry (te, onleave);
3401 state.text <- "";
3402 enttext ();
3403 G.postRedisplay "textentrykeyboard enttext";
3405 let histaction cmd =
3406 match opthist with
3407 | None -> ()
3408 | Some (action, _) ->
3409 state.mode <- Textentry (
3410 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3412 G.postRedisplay "textentry histaction"
3414 match key with
3415 | 0xff08 -> (* backspace *)
3416 if emptystr text && cancelonempty
3417 then (
3418 onleave Cancel;
3419 G.postRedisplay "textentrykeyboard after cancel";
3421 else
3422 let s = withoutlastutf8 text in
3423 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3425 | 0xff0d | 0xff8d -> (* (kp) enter *)
3426 ondone text;
3427 onleave Confirm;
3428 G.postRedisplay "textentrykeyboard after confirm"
3430 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3431 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3432 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3433 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3435 | 0xff1b -> (* escape*)
3436 if emptystr text
3437 then (
3438 begin match opthist with
3439 | None -> ()
3440 | Some (_, onhistcancel) -> onhistcancel ()
3441 end;
3442 onleave Cancel;
3443 state.text <- "";
3444 G.postRedisplay "textentrykeyboard after cancel2"
3446 else (
3447 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3450 | 0xff9f | 0xffff -> () (* delete *)
3452 | _ when key != 0
3453 && key land 0xff00 != 0xff00 (* keyboard *)
3454 && key land 0xfe00 != 0xfe00 (* xkb *)
3455 && key land 0xfd00 != 0xfd00 (* 3270 *)
3457 begin match onkey text key with
3458 | TEdone text ->
3459 ondone text;
3460 onleave Confirm;
3461 G.postRedisplay "textentrykeyboard after confirm2";
3463 | TEcont text ->
3464 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3466 | TEstop ->
3467 onleave Cancel;
3468 G.postRedisplay "textentrykeyboard after cancel3"
3470 | TEswitch te ->
3471 state.mode <- Textentry (te, onleave);
3472 G.postRedisplay "textentrykeyboard switch";
3473 end;
3475 | _ ->
3476 vlog "unhandled key %s" (Wsi.keyname key)
3479 let firstof first active =
3480 if first > active || abs (first - active) > fstate.maxrows - 1
3481 then max 0 (active - (fstate.maxrows/2))
3482 else first
3485 let calcfirst first active =
3486 if active > first
3487 then
3488 let rows = active - first in
3489 if rows > fstate.maxrows then active - fstate.maxrows else first
3490 else active
3493 let scrollph y maxy =
3494 let sh = float (maxy + state.winh) /. float state.winh in
3495 let sh = float state.winh /. sh in
3496 let sh = max sh (float conf.scrollh) in
3498 let percent = float y /. float maxy in
3499 let position = (float state.winh -. sh) *. percent in
3501 let position =
3502 if position +. sh > float state.winh
3503 then float state.winh -. sh
3504 else position
3506 position, sh;
3509 let coe s = (s :> uioh);;
3511 class listview ~(source:lvsource) ~trusted ~modehash =
3512 object (self)
3513 val m_pan = source#getpan
3514 val m_first = source#getfirst
3515 val m_active = source#getactive
3516 val m_qsearch = source#getqsearch
3517 val m_prev_uioh = state.uioh
3519 method private elemunder y =
3520 let n = y / (fstate.fontsize+1) in
3521 if m_first + n < source#getitemcount
3522 then (
3523 if source#hasaction (m_first + n)
3524 then Some (m_first + n)
3525 else None
3527 else None
3529 method display =
3530 Gl.enable `blend;
3531 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3532 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3533 filledrect 0. 0. (float state.winw) (float state.winh);
3534 GlDraw.color (1., 1., 1.);
3535 Gl.enable `texture_2d;
3536 let fs = fstate.fontsize in
3537 let nfs = fs + 1 in
3538 let ww = fstate.wwidth in
3539 let tabw = 30.0*.ww in
3540 let itemcount = source#getitemcount in
3541 let minfo = source#getminfo in
3542 let rec loop row =
3543 if (row - m_first) > fstate.maxrows
3544 then ()
3545 else (
3546 if row >= 0 && row < itemcount
3547 then (
3548 let (s, level) = source#getitem row in
3549 let y = (row - m_first) * nfs in
3550 let x = 5.0 +. float (level + m_pan) *. ww in
3551 if row = m_active
3552 then (
3553 Gl.disable `texture_2d;
3554 let alpha = if source#hasaction row then 0.9 else 0.3 in
3555 GlDraw.color (1., 1., 1.) ~alpha;
3556 linerect 1. (float (y + 1))
3557 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3558 Gl.enable `texture_2d;
3559 GlDraw.color (1., 1., 1.);
3561 let drawtabularstring s =
3562 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3563 if trusted
3564 then
3565 let tabpos = try String.index s '\t' with Not_found -> -1 in
3566 if tabpos > 0
3567 then
3568 let len = String.length s - tabpos - 1 in
3569 let s1 = String.sub s 0 tabpos
3570 and s2 = String.sub s (tabpos + 1) len in
3571 let nx = drawstr x s1 in
3572 let sw = nx -. x in
3573 let x = x +. (max tabw sw) in
3574 drawstr x s2
3575 else
3576 drawstr x s
3577 else
3578 drawstr x s
3580 let _ = drawtabularstring s in
3581 loop (row+1)
3585 loop m_first;
3586 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
3587 let rec loop row =
3588 if (row - m_first) > fstate.maxrows
3589 then ()
3590 else (
3591 if row >= 0 && row < itemcount
3592 then (
3593 let (s, level) = source#getitem row in
3594 let y = (row - m_first) * nfs in
3595 let x = 5.0 +. float (level + m_pan) *. ww in
3596 let (first, last) = minfo.(row) in
3597 let prefix = String.sub s 0 first in
3598 let suffix = String.sub s first (last - first) in
3599 let w1 = measurestr fstate.fontsize prefix in
3600 let w2 = measurestr fstate.fontsize suffix in
3601 let x0 = x +. w1
3602 and y0 = (float (y+2)) in
3603 let x1 = x0 +. w2
3604 and y1 = (float (y+fs+3)) in
3605 filledrect x0 y0 x1 y1;
3606 loop (row+1)
3610 Gl.disable `texture_2d;
3611 if Array.length minfo > 0 then loop m_first;
3612 Gl.disable `blend;
3614 method updownlevel incr =
3615 let len = source#getitemcount in
3616 let curlevel =
3617 if m_active >= 0 && m_active < len
3618 then snd (source#getitem m_active)
3619 else -1
3621 let rec flow i =
3622 if i = len then i-1 else if i = -1 then 0 else
3623 let _, l = source#getitem i in
3624 if l != curlevel then i else flow (i+incr)
3626 let active = flow m_active in
3627 let first = calcfirst m_first active in
3628 G.postRedisplay "outline updownlevel";
3629 {< m_active = active; m_first = first >}
3631 method private key1 key mask =
3632 let set1 active first qsearch =
3633 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3635 let search active pattern incr =
3636 let active = if active = -1 then m_first else active in
3637 let dosearch re =
3638 let rec loop n =
3639 if n >= 0 && n < source#getitemcount
3640 then (
3641 let s, _ = source#getitem n in
3643 (try ignore (Str.search_forward re s 0); true
3644 with Not_found -> false)
3645 then Some n
3646 else loop (n + incr)
3648 else None
3650 loop active
3653 let re = Str.regexp_case_fold pattern in
3654 dosearch re
3655 with Failure s ->
3656 state.text <- s;
3657 None
3659 let itemcount = source#getitemcount in
3660 let find start incr =
3661 let rec find i =
3662 if i = -1 || i = itemcount
3663 then -1
3664 else (
3665 if source#hasaction i
3666 then i
3667 else find (i + incr)
3670 find start
3672 let set active first =
3673 let first = bound first 0 (itemcount - fstate.maxrows) in
3674 state.text <- "";
3675 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3677 let navigate incr =
3678 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3679 let active, first =
3680 let incr1 = if incr > 0 then 1 else -1 in
3681 if isvisible m_first m_active
3682 then
3683 let next =
3684 let next = m_active + incr in
3685 let next =
3686 if next < 0 || next >= itemcount
3687 then -1
3688 else find next incr1
3690 if abs (m_active - next) > fstate.maxrows
3691 then -1
3692 else next
3694 if next = -1
3695 then
3696 let first = m_first + incr in
3697 let first = bound first 0 (itemcount - fstate.maxrows) in
3698 let next =
3699 let next = m_active + incr in
3700 let next = bound next 0 (itemcount - 1) in
3701 find next ~-incr1
3703 let active =
3704 if next = -1
3705 then m_active
3706 else (
3707 if isvisible first next
3708 then next
3709 else m_active
3712 active, first
3713 else
3714 let first = min next m_first in
3715 let first =
3716 if abs (next - first) > fstate.maxrows
3717 then first + incr
3718 else first
3720 next, first
3721 else
3722 let first = m_first + incr in
3723 let first = bound first 0 (itemcount - 1) in
3724 let active =
3725 let next = m_active + incr in
3726 let next = bound next 0 (itemcount - 1) in
3727 let next = find next incr1 in
3728 let active =
3729 if next = -1 || abs (m_active - first) > fstate.maxrows
3730 then (
3731 let active = if m_active = -1 then next else m_active in
3732 active
3734 else next
3736 if isvisible first active
3737 then active
3738 else -1
3740 active, first
3742 G.postRedisplay "listview navigate";
3743 set active first;
3745 match key with
3746 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3747 let incr = if key = 0x72 then -1 else 1 in
3748 let active, first =
3749 match search (m_active + incr) m_qsearch incr with
3750 | None ->
3751 state.text <- m_qsearch ^ " [not found]";
3752 m_active, m_first
3753 | Some active ->
3754 state.text <- m_qsearch;
3755 active, firstof m_first active
3757 G.postRedisplay "listview ctrl-r/s";
3758 set1 active first m_qsearch;
3760 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3761 if m_active >= 0 && m_active < source#getitemcount
3762 then (
3763 let s, _ = source#getitem m_active in
3764 selstring s;
3766 coe self
3768 | 0xff08 -> (* backspace *)
3769 if emptystr m_qsearch
3770 then coe self
3771 else (
3772 let qsearch = withoutlastutf8 m_qsearch in
3773 if emptystr qsearch
3774 then (
3775 state.text <- "";
3776 G.postRedisplay "listview empty qsearch";
3777 set1 m_active m_first "";
3779 else
3780 let active, first =
3781 match search m_active qsearch ~-1 with
3782 | None ->
3783 state.text <- qsearch ^ " [not found]";
3784 m_active, m_first
3785 | Some active ->
3786 state.text <- qsearch;
3787 active, firstof m_first active
3789 G.postRedisplay "listview backspace qsearch";
3790 set1 active first qsearch
3793 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3794 let pattern = m_qsearch ^ toutf8 key in
3795 let active, first =
3796 match search m_active pattern 1 with
3797 | None ->
3798 state.text <- pattern ^ " [not found]";
3799 m_active, m_first
3800 | Some active ->
3801 state.text <- pattern;
3802 active, firstof m_first active
3804 G.postRedisplay "listview qsearch add";
3805 set1 active first pattern;
3807 | 0xff1b -> (* escape *)
3808 state.text <- "";
3809 if emptystr m_qsearch
3810 then (
3811 G.postRedisplay "list view escape";
3812 begin
3813 match
3814 source#exit (coe self) true m_active m_first m_pan m_qsearch
3815 with
3816 | None -> m_prev_uioh
3817 | Some uioh -> uioh
3820 else (
3821 G.postRedisplay "list view kill qsearch";
3822 source#setqsearch "";
3823 coe {< m_qsearch = "" >}
3826 | 0xff0d | 0xff8d -> (* (kp) enter *)
3827 state.text <- "";
3828 let self = {< m_qsearch = "" >} in
3829 source#setqsearch "";
3830 let opt =
3831 G.postRedisplay "listview enter";
3832 if m_active >= 0 && m_active < source#getitemcount
3833 then (
3834 source#exit (coe self) false m_active m_first m_pan "";
3836 else (
3837 source#exit (coe self) true m_active m_first m_pan "";
3840 begin match opt with
3841 | None -> m_prev_uioh
3842 | Some uioh -> uioh
3845 | 0xff9f | 0xffff -> (* (kp) delete *)
3846 coe self
3848 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3849 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3850 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3851 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3853 | 0xff53 | 0xff98 -> (* (kp) right *)
3854 state.text <- "";
3855 G.postRedisplay "listview right";
3856 coe {< m_pan = m_pan - 1 >}
3858 | 0xff51 | 0xff96 -> (* (kp) left *)
3859 state.text <- "";
3860 G.postRedisplay "listview left";
3861 coe {< m_pan = m_pan + 1 >}
3863 | 0xff50 | 0xff95 -> (* (kp) home *)
3864 let active = find 0 1 in
3865 G.postRedisplay "listview home";
3866 set active 0;
3868 | 0xff57 | 0xff9c -> (* (kp) end *)
3869 let first = max 0 (itemcount - fstate.maxrows) in
3870 let active = find (itemcount - 1) ~-1 in
3871 G.postRedisplay "listview end";
3872 set active first;
3874 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3875 coe self
3877 | _ ->
3878 dolog "listview unknown key %#x" key; coe self
3880 method key key mask =
3881 match state.mode with
3882 | Textentry te -> textentrykeyboard key mask te; coe self
3883 | _ -> self#key1 key mask
3885 method button button down x y _ =
3886 let opt =
3887 match button with
3888 | 1 when x > state.winw - conf.scrollbw ->
3889 G.postRedisplay "listview scroll";
3890 if down
3891 then
3892 let _, position, sh = self#scrollph in
3893 if y > truncate position && y < truncate (position +. sh)
3894 then (
3895 state.mstate <- Mscrolly;
3896 Some (coe self)
3898 else
3899 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3900 let first = truncate (s *. float source#getitemcount) in
3901 let first = min source#getitemcount first in
3902 Some (coe {< m_first = first; m_active = first >})
3903 else (
3904 state.mstate <- Mnone;
3905 Some (coe self);
3907 | 1 when not down ->
3908 begin match self#elemunder y with
3909 | Some n ->
3910 G.postRedisplay "listview click";
3911 source#exit
3912 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3913 | _ ->
3914 Some (coe self)
3916 | n when (n == 4 || n == 5) && not down ->
3917 let len = source#getitemcount in
3918 let first =
3919 if n = 5 && m_first + fstate.maxrows >= len
3920 then
3921 m_first
3922 else
3923 let first = m_first + (if n == 4 then -1 else 1) in
3924 bound first 0 (len - 1)
3926 G.postRedisplay "listview wheel";
3927 Some (coe {< m_first = first >})
3928 | n when (n = 6 || n = 7) && not down ->
3929 let inc = if n = 7 then -1 else 1 in
3930 G.postRedisplay "listview hwheel";
3931 Some (coe {< m_pan = m_pan + inc >})
3932 | _ ->
3933 Some (coe self)
3935 match opt with
3936 | None -> m_prev_uioh
3937 | Some uioh -> uioh
3939 method multiclick _ x y = self#button 1 true x y
3941 method motion _ y =
3942 match state.mstate with
3943 | Mscrolly ->
3944 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3945 let first = truncate (s *. float source#getitemcount) in
3946 let first = min source#getitemcount first in
3947 G.postRedisplay "listview motion";
3948 coe {< m_first = first; m_active = first >}
3949 | _ -> coe self
3951 method pmotion x y =
3952 if x < state.winw - conf.scrollbw
3953 then
3954 let n =
3955 match self#elemunder y with
3956 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3957 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3959 let o =
3960 if n != m_active
3961 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3962 else self
3964 coe o
3965 else (
3966 Wsi.setcursor Wsi.CURSOR_INHERIT;
3967 coe self
3970 method infochanged _ = ()
3972 method scrollpw = (0, 0.0, 0.0)
3973 method scrollph =
3974 let nfs = fstate.fontsize + 1 in
3975 let y = m_first * nfs in
3976 let itemcount = source#getitemcount in
3977 let maxi = max 0 (itemcount - fstate.maxrows) in
3978 let maxy = maxi * nfs in
3979 let p, h = scrollph y maxy in
3980 conf.scrollbw, p, h
3982 method modehash = modehash
3983 method eformsgs = false
3984 end;;
3986 class outlinelistview ~source =
3987 let settext autonarrow s =
3988 if autonarrow
3989 then state.text <- "[" ^ s ^ "]"
3990 else state.text <- s
3992 object (self)
3993 inherit listview
3994 ~source:(source :> lvsource)
3995 ~trusted:false
3996 ~modehash:(findkeyhash conf "outline")
3997 as super
3999 val m_autonarrow = false
4001 method display = super#display
4003 method key key mask =
4004 let maxrows =
4005 if emptystr state.text
4006 then fstate.maxrows
4007 else fstate.maxrows - 2
4009 let calcfirst first active =
4010 if active > first
4011 then
4012 let rows = active - first in
4013 if rows > maxrows then active - maxrows else first
4014 else active
4016 let navigate incr =
4017 let active = m_active + incr in
4018 let active = bound active 0 (source#getitemcount - 1) in
4019 let first = calcfirst m_first active in
4020 G.postRedisplay "outline navigate";
4021 coe {< m_active = active; m_first = first >}
4023 let navscroll first =
4024 let active =
4025 let dist = m_active - first in
4026 if dist < 0
4027 then first
4028 else (
4029 if dist < maxrows
4030 then m_active
4031 else first + maxrows
4034 G.postRedisplay "outline navscroll";
4035 coe {< m_first = first; m_active = active >}
4037 let ctrl = Wsi.withctrl mask in
4038 match key with
4039 | 97 when ctrl -> (* ctrl-a *)
4040 if m_autonarrow
4041 then source#denarrow
4042 else source#narrow m_qsearch;
4043 settext (not m_autonarrow) m_qsearch;
4044 G.postRedisplay "toggle auto narrowing";
4045 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
4047 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
4048 settext true "";
4049 G.postRedisplay "toggle auto narrowing";
4050 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
4052 | 110 when ctrl -> (* ctrl-n *)
4053 source#narrow m_qsearch;
4054 if not m_autonarrow
4055 then source#add_narrow_pattern m_qsearch;
4056 G.postRedisplay "outline ctrl-n";
4057 coe {< m_first = 0; m_active = 0 >}
4059 | 83 when ctrl -> (* ctrl-S *)
4060 let active = source#calcactive (getanchor ()) in
4061 let first = firstof m_first active in
4062 G.postRedisplay "outline ctrl-s";
4063 coe {< m_first = first; m_active = active >}
4065 | 117 when ctrl -> (* ctrl-u *)
4066 source#del_narrow_pattern;
4067 let pattern = source#renarrow in
4068 G.postRedisplay "outline ctrl-u";
4069 let text =
4070 if emptystr pattern then "" else "Narrowed to " ^ pattern
4072 settext m_autonarrow text;
4073 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4075 | 108 when ctrl -> (* ctrl-l *)
4076 let first = max 0 (m_active - (fstate.maxrows / 2)) in
4077 G.postRedisplay "outline ctrl-l";
4078 coe {< m_first = first >}
4080 | 0xff1b -> (* escape *)
4081 let o = super#key key mask in
4082 if m_autonarrow
4083 then (
4084 if nonemptystr m_qsearch
4085 then (
4086 source#add_narrow_pattern m_qsearch;
4087 settext true "";
4092 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
4093 if nonemptystr m_qsearch
4094 then source#add_narrow_pattern m_qsearch;
4095 super#key key mask
4097 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
4098 let pattern = m_qsearch ^ toutf8 key in
4099 G.postRedisplay "outlinelistview autonarrow add";
4100 source#narrow pattern;
4101 settext true pattern;
4102 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4104 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4105 if emptystr m_qsearch
4106 then coe self
4107 else
4108 let pattern = withoutlastutf8 m_qsearch in
4109 G.postRedisplay "outlinelistview autonarrow backspace";
4110 ignore (source#renarrow);
4111 source#narrow pattern;
4112 settext true pattern;
4113 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4115 | 0xff9f | 0xffff -> (* (kp) delete *)
4116 source#remove m_active;
4117 G.postRedisplay "outline delete";
4118 let active = max 0 (m_active-1) in
4119 coe {< m_first = firstof m_first active;
4120 m_active = active >}
4122 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4123 navscroll (max 0 (m_first - 1))
4125 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4126 navscroll (min (source#getitemcount - 1) (m_first + 1))
4128 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4129 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4130 | 0xff55 | 0xff9a -> (* (kp) prior *)
4131 navigate ~-(fstate.maxrows)
4132 | 0xff56 | 0xff9b -> (* (kp) next *)
4133 navigate fstate.maxrows
4135 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4136 let o =
4137 if ctrl
4138 then (
4139 G.postRedisplay "outline ctrl right";
4140 {< m_pan = m_pan + 1 >}
4142 else self#updownlevel 1
4144 coe o
4146 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4147 let o =
4148 if ctrl
4149 then (
4150 G.postRedisplay "outline ctrl left";
4151 {< m_pan = m_pan - 1 >}
4153 else self#updownlevel ~-1
4155 coe o
4157 | 0xff50 | 0xff95 -> (* (kp) home *)
4158 G.postRedisplay "outline home";
4159 coe {< m_first = 0; m_active = 0 >}
4161 | 0xff57 | 0xff9c -> (* (kp) end *)
4162 let active = source#getitemcount - 1 in
4163 let first = max 0 (active - fstate.maxrows) in
4164 G.postRedisplay "outline end";
4165 coe {< m_active = active; m_first = first >}
4167 | _ -> super#key key mask
4170 let gotounder under =
4171 let getpath filename =
4172 let path =
4173 if nonemptystr filename
4174 then
4175 if Filename.is_relative filename
4176 then
4177 let dir = Filename.dirname state.path in
4178 let dir =
4179 if Filename.is_implicit dir
4180 then Filename.concat (Sys.getcwd ()) dir
4181 else dir
4183 Filename.concat dir filename
4184 else filename
4185 else ""
4187 if Sys.file_exists path
4188 then path
4189 else ""
4191 match under with
4192 | Ulinkgoto (pageno, top) ->
4193 if pageno >= 0
4194 then (
4195 addnav ();
4196 gotopage1 pageno top;
4199 | Ulinkuri s ->
4200 gotouri s
4202 | Uremote (filename, pageno) ->
4203 let path = getpath filename in
4204 if nonemptystr path
4205 then (
4206 if conf.riani
4207 then
4208 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4209 try popen command []
4210 with exn ->
4211 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4212 flush stderr;
4213 else
4214 let anchor = getanchor () in
4215 let ranchor = state.path, state.password, anchor, state.origin in
4216 state.origin <- "";
4217 state.anchor <- (pageno, 0.0, 0.0);
4218 state.ranchors <- ranchor :: state.ranchors;
4219 opendoc path "";
4221 else showtext '!' ("Could not find " ^ filename)
4223 | Uremotedest (filename, destname) ->
4224 let path = getpath filename in
4225 if nonemptystr path
4226 then (
4227 if conf.riani
4228 then
4229 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4230 try popen command []
4231 with exn ->
4232 Printf.eprintf
4233 "failed to execute `%s': %s\n" command (exntos exn);
4234 flush stderr;
4235 else
4236 let anchor = getanchor () in
4237 let ranchor = state.path, state.password, anchor, state.origin in
4238 state.origin <- "";
4239 state.nameddest <- destname;
4240 state.ranchors <- ranchor :: state.ranchors;
4241 opendoc path "";
4243 else showtext '!' ("Could not find " ^ filename)
4245 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4248 let gotooutline (_, _, kind) =
4249 match kind with
4250 | Onone -> ()
4251 | Oanchor anchor ->
4252 let (pageno, y, _) = anchor in
4253 let y = getanchory
4254 (if conf.presentation then (pageno, y, 1.0) else anchor)
4256 addnav ();
4257 gotoghyll y
4258 | Ouri uri -> gotounder (Ulinkuri uri)
4259 | Olaunch cmd -> gotounder (Ulaunch cmd)
4260 | Oremote remote -> gotounder (Uremote remote)
4261 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4264 let outlinesource usebookmarks =
4265 let empty = [||] in
4266 (object (self)
4267 inherit lvsourcebase
4268 val mutable m_items = empty
4269 val mutable m_minfo = empty
4270 val mutable m_orig_items = empty
4271 val mutable m_orig_minfo = empty
4272 val mutable m_narrow_patterns = []
4273 val mutable m_hadremovals = false
4275 method getitemcount =
4276 Array.length m_items + (if m_hadremovals then 1 else 0)
4278 method getitem n =
4279 if n == Array.length m_items && m_hadremovals
4280 then
4281 ("[Confirm removal]", 0)
4282 else
4283 let s, n, _ = m_items.(n) in
4284 (s, n)
4286 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4287 ignore (uioh, first, qsearch);
4288 let confrimremoval = m_hadremovals && active = Array.length m_items in
4289 let items, minfo =
4290 if m_narrow_patterns = []
4291 then m_orig_items, m_orig_minfo
4292 else m_items, m_minfo
4294 if not cancel
4295 then (
4296 if not confrimremoval
4297 then (
4298 gotooutline m_items.(active);
4299 m_items <- items;
4300 m_minfo <- minfo;
4302 else (
4303 state.bookmarks <- Array.to_list m_items;
4304 m_orig_items <- m_items;
4305 m_orig_minfo <- m_minfo;
4308 else (
4309 m_items <- items;
4310 m_minfo <- minfo;
4312 m_pan <- pan;
4313 None
4315 method hasaction _ = true
4317 method greetmsg =
4318 if Array.length m_items != Array.length m_orig_items
4319 then
4320 let s =
4321 match m_narrow_patterns with
4322 | one :: [] -> one
4323 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4325 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4326 else ""
4328 method narrow pattern =
4329 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4330 match reopt with
4331 | None -> ()
4332 | Some re ->
4333 let rec loop accu minfo n =
4334 if n = -1
4335 then (
4336 m_items <- Array.of_list accu;
4337 m_minfo <- Array.of_list minfo;
4339 else
4340 let (s, _, _) as o = m_items.(n) in
4341 let accu, minfo =
4342 let first =
4343 try Str.search_forward re s 0
4344 with Not_found -> -1
4346 if first >= 0
4347 then o :: accu, (first, Str.match_end ()) :: minfo
4348 else accu, minfo
4350 loop accu minfo (n-1)
4352 loop [] [] (Array.length m_items - 1)
4354 method getminfo = m_minfo
4356 method denarrow =
4357 m_orig_items <- (
4358 if usebookmarks
4359 then Array.of_list state.bookmarks
4360 else state.outlines
4362 m_minfo <- m_orig_minfo;
4363 m_items <- m_orig_items
4365 method remove m =
4366 if usebookmarks
4367 then
4368 if m >= 0 && m < Array.length m_items
4369 then (
4370 m_hadremovals <- true;
4371 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4372 let n = if n >= m then n+1 else n in
4373 m_items.(n)
4377 method add_narrow_pattern pattern =
4378 m_narrow_patterns <- pattern :: m_narrow_patterns
4380 method del_narrow_pattern =
4381 match m_narrow_patterns with
4382 | _ :: rest -> m_narrow_patterns <- rest
4383 | [] -> ()
4385 method renarrow =
4386 self#denarrow;
4387 match m_narrow_patterns with
4388 | pattern :: [] -> self#narrow pattern; pattern
4389 | list ->
4390 List.fold_left (fun accu pattern ->
4391 self#narrow pattern;
4392 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4394 method calcactive anchor =
4395 let rely = getanchory anchor in
4396 let rec loop n best bestd =
4397 if n = Array.length m_items
4398 then best
4399 else
4400 let _, _, kind = m_items.(n) in
4401 match kind with
4402 | Oanchor anchor ->
4403 let orely = getanchory anchor in
4404 let d = abs (orely - rely) in
4405 if d < bestd
4406 then loop (n+1) n d
4407 else loop (n+1) best bestd
4408 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
4409 loop (n+1) best bestd
4411 loop 0 ~-1 max_int
4413 method reset anchor items =
4414 m_hadremovals <- false;
4415 if m_orig_items == empty
4416 then (
4417 m_orig_items <- items;
4418 if m_narrow_patterns == []
4419 then m_items <- items;
4421 let active = self#calcactive anchor in
4422 m_active <- active;
4423 m_first <- firstof m_first active
4424 end)
4427 let enterselector usebookmarks =
4428 let source = outlinesource usebookmarks in
4429 fun errmsg ->
4430 let outlines =
4431 if usebookmarks
4432 then Array.of_list state.bookmarks
4433 else state.outlines
4435 if Array.length outlines = 0
4436 then (
4437 showtext ' ' errmsg;
4439 else (
4440 state.text <- source#greetmsg;
4441 Wsi.setcursor Wsi.CURSOR_INHERIT;
4442 let anchor = getanchor () in
4443 source#reset anchor outlines;
4444 state.uioh <- coe (new outlinelistview ~source);
4445 G.postRedisplay "enter selector";
4449 let enteroutlinemode =
4450 let f = enterselector false in
4451 fun ()-> f "Document has no outline";
4454 let enterbookmarkmode =
4455 let f = enterselector true in
4456 fun () -> f "Document has no bookmarks (yet)";
4459 let color_of_string s =
4460 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4461 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4465 let color_to_string (r, g, b) =
4466 let r = truncate (r *. 256.0)
4467 and g = truncate (g *. 256.0)
4468 and b = truncate (b *. 256.0) in
4469 Printf.sprintf "%d/%d/%d" r g b
4472 let irect_of_string s =
4473 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4476 let irect_to_string (x0,y0,x1,y1) =
4477 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4480 let makecheckers () =
4481 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4482 following to say:
4483 converted by Issac Trotts. July 25, 2002 *)
4484 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4485 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4486 let id = GlTex.gen_texture () in
4487 GlTex.bind_texture `texture_2d id;
4488 GlPix.store (`unpack_alignment 1);
4489 GlTex.image2d image;
4490 List.iter (GlTex.parameter ~target:`texture_2d)
4491 [ `mag_filter `nearest; `min_filter `nearest ];
4495 let setcheckers enabled =
4496 match state.texid with
4497 | None ->
4498 if enabled then state.texid <- Some (makecheckers ())
4500 | Some texid ->
4501 if not enabled
4502 then (
4503 GlTex.delete_texture texid;
4504 state.texid <- None;
4508 let int_of_string_with_suffix s =
4509 let l = String.length s in
4510 let s1, shift =
4511 if l > 1
4512 then
4513 let suffix = Char.lowercase s.[l-1] in
4514 match suffix with
4515 | 'k' -> String.sub s 0 (l-1), 10
4516 | 'm' -> String.sub s 0 (l-1), 20
4517 | 'g' -> String.sub s 0 (l-1), 30
4518 | _ -> s, 0
4519 else s, 0
4521 let n = int_of_string s1 in
4522 let m = n lsl shift in
4523 if m < 0 || m < n
4524 then raise (Failure "value too large")
4525 else m
4528 let string_with_suffix_of_int n =
4529 if n = 0
4530 then "0"
4531 else
4532 let units = [(30, "G"); (20, "M"); (10, "K")] in
4533 let prettyint n =
4534 let rec loop s n =
4535 let h = n mod 1000 in
4536 let n = n / 1000 in
4537 if n = 0
4538 then string_of_int h ^ s
4539 else (
4540 let s = Printf.sprintf "_%03d%s" h s in
4541 loop s n
4544 loop "" n
4546 let rec find = function
4547 | [] -> prettyint n
4548 | (shift, suffix) :: rest ->
4549 if (n land ((1 lsl shift) - 1)) = 0
4550 then prettyint (n lsr shift) ^ suffix
4551 else find rest
4553 find units
4556 let defghyllscroll = (40, 8, 32);;
4557 let fastghyllscroll = (5,1,2);;
4558 let neatghyllscroll = (10,1,9);;
4559 let ghyllscroll_of_string s =
4560 match s with
4561 | "default" -> Some defghyllscroll
4562 | "fast" -> Some (5,1,2)
4563 | "neat" -> Some (10,1,9)
4564 | "" | "none" -> None
4565 | _ ->
4566 let (n,a,b) as nab =
4567 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4568 if n <= a || n <= b || a >= b
4569 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4570 n a b;
4571 Some nab
4574 let ghyllscroll_to_string ((n, a, b) as nab) =
4575 (**) if nab = defghyllscroll then "default"
4576 else if nab = fastghyllscroll then "fast"
4577 else if nab = neatghyllscroll then "neat"
4578 else Printf.sprintf "%d,%d,%d" n a b;
4581 let describe_location () =
4582 let fn = page_of_y state.y in
4583 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4584 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4585 let percent =
4586 if maxy <= 0
4587 then 100.
4588 else (100. *. (float state.y /. float maxy))
4590 if fn = ln
4591 then
4592 Printf.sprintf "page %d of %d [%.2f%%]"
4593 (fn+1) state.pagecount percent
4594 else
4595 Printf.sprintf
4596 "pages %d-%d of %d [%.2f%%]"
4597 (fn+1) (ln+1) state.pagecount percent
4600 let setpresentationmode v =
4601 let n = page_of_y state.y in
4602 state.anchor <- (n, 0.0, 1.0);
4603 conf.presentation <- v;
4604 if conf.fitmodel = FitPage
4605 then reqlayout conf.angle conf.fitmodel;
4606 represent ();
4609 let enterinfomode =
4610 let btos b = if b then "\xe2\x88\x9a" else "" in
4611 let showextended = ref false in
4612 let leave mode = function
4613 | Confirm -> state.mode <- mode
4614 | Cancel -> state.mode <- mode in
4615 let src =
4616 (object
4617 val mutable m_first_time = true
4618 val mutable m_l = []
4619 val mutable m_a = [||]
4620 val mutable m_prev_uioh = nouioh
4621 val mutable m_prev_mode = View
4623 inherit lvsourcebase
4625 method reset prev_mode prev_uioh =
4626 m_a <- Array.of_list (List.rev m_l);
4627 m_l <- [];
4628 m_prev_mode <- prev_mode;
4629 m_prev_uioh <- prev_uioh;
4630 if m_first_time
4631 then (
4632 let rec loop n =
4633 if n >= Array.length m_a
4634 then ()
4635 else
4636 match m_a.(n) with
4637 | _, _, _, Action _ -> m_active <- n
4638 | _ -> loop (n+1)
4640 loop 0;
4641 m_first_time <- false;
4644 method int name get set =
4645 m_l <-
4646 (name, `int get, 1, Action (
4647 fun u ->
4648 let ondone s =
4649 try set (int_of_string s)
4650 with exn ->
4651 state.text <- Printf.sprintf "bad integer `%s': %s"
4652 s (exntos exn)
4654 state.text <- "";
4655 let te = name ^ ": ", "", None, intentry, ondone, true in
4656 state.mode <- Textentry (te, leave m_prev_mode);
4658 )) :: m_l
4660 method int_with_suffix name get set =
4661 m_l <-
4662 (name, `intws get, 1, Action (
4663 fun u ->
4664 let ondone s =
4665 try set (int_of_string_with_suffix s)
4666 with exn ->
4667 state.text <- Printf.sprintf "bad integer `%s': %s"
4668 s (exntos exn)
4670 state.text <- "";
4671 let te =
4672 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4674 state.mode <- Textentry (te, leave m_prev_mode);
4676 )) :: m_l
4678 method bool ?(offset=1) ?(btos=btos) name get set =
4679 m_l <-
4680 (name, `bool (btos, get), offset, Action (
4681 fun u ->
4682 let v = get () in
4683 set (not v);
4685 )) :: m_l
4687 method color name get set =
4688 m_l <-
4689 (name, `color get, 1, Action (
4690 fun u ->
4691 let invalid = (nan, nan, nan) in
4692 let ondone s =
4693 let c =
4694 try color_of_string s
4695 with exn ->
4696 state.text <- Printf.sprintf "bad color `%s': %s"
4697 s (exntos exn);
4698 invalid
4700 if c <> invalid
4701 then set c;
4703 let te = name ^ ": ", "", None, textentry, ondone, true in
4704 state.text <- color_to_string (get ());
4705 state.mode <- Textentry (te, leave m_prev_mode);
4707 )) :: m_l
4709 method string name get set =
4710 m_l <-
4711 (name, `string get, 1, Action (
4712 fun u ->
4713 let ondone s = set s in
4714 let te = name ^ ": ", "", None, textentry, ondone, true in
4715 state.mode <- Textentry (te, leave m_prev_mode);
4717 )) :: m_l
4719 method colorspace name get set =
4720 m_l <-
4721 (name, `string get, 1, Action (
4722 fun _ ->
4723 let source =
4724 (object
4725 inherit lvsourcebase
4727 initializer
4728 m_active <- CSTE.to_int conf.colorspace;
4729 m_first <- 0;
4731 method getitemcount =
4732 Array.length CSTE.names
4733 method getitem n =
4734 (CSTE.names.(n), 0)
4735 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4736 ignore (uioh, first, pan, qsearch);
4737 if not cancel then set active;
4738 None
4739 method hasaction _ = true
4740 end)
4742 state.text <- "";
4743 let modehash = findkeyhash conf "info" in
4744 coe (new listview ~source ~trusted:true ~modehash)
4745 )) :: m_l
4747 method paxmark name get set =
4748 m_l <-
4749 (name, `string get, 1, Action (
4750 fun _ ->
4751 let source =
4752 (object
4753 inherit lvsourcebase
4755 initializer
4756 m_active <- MTE.to_int conf.paxmark;
4757 m_first <- 0;
4759 method getitemcount = Array.length MTE.names
4760 method getitem n = (MTE.names.(n), 0)
4761 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4762 ignore (uioh, first, pan, qsearch);
4763 if not cancel then set active;
4764 None
4765 method hasaction _ = true
4766 end)
4768 state.text <- "";
4769 let modehash = findkeyhash conf "info" in
4770 coe (new listview ~source ~trusted:true ~modehash)
4771 )) :: m_l
4773 method fitmodel name get set =
4774 m_l <-
4775 (name, `string get, 1, Action (
4776 fun _ ->
4777 let source =
4778 (object
4779 inherit lvsourcebase
4781 initializer
4782 m_active <- FMTE.to_int conf.fitmodel;
4783 m_first <- 0;
4785 method getitemcount = Array.length FMTE.names
4786 method getitem n = (FMTE.names.(n), 0)
4787 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4788 ignore (uioh, first, pan, qsearch);
4789 if not cancel then set active;
4790 None
4791 method hasaction _ = true
4792 end)
4794 state.text <- "";
4795 let modehash = findkeyhash conf "info" in
4796 coe (new listview ~source ~trusted:true ~modehash)
4797 )) :: m_l
4799 method caption s offset =
4800 m_l <- (s, `empty, offset, Noaction) :: m_l
4802 method caption2 s f offset =
4803 m_l <- (s, `string f, offset, Noaction) :: m_l
4805 method getitemcount = Array.length m_a
4807 method getitem n =
4808 let tostr = function
4809 | `int f -> string_of_int (f ())
4810 | `intws f -> string_with_suffix_of_int (f ())
4811 | `string f -> f ()
4812 | `color f -> color_to_string (f ())
4813 | `bool (btos, f) -> btos (f ())
4814 | `empty -> ""
4816 let name, t, offset, _ = m_a.(n) in
4817 ((let s = tostr t in
4818 if nonemptystr s
4819 then Printf.sprintf "%s\t%s" name s
4820 else name),
4821 offset)
4823 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4824 let uiohopt =
4825 if not cancel
4826 then (
4827 m_qsearch <- qsearch;
4828 let uioh =
4829 match m_a.(active) with
4830 | _, _, _, Action f -> f uioh
4831 | _ -> uioh
4833 Some uioh
4835 else None
4837 m_active <- active;
4838 m_first <- first;
4839 m_pan <- pan;
4840 uiohopt
4842 method hasaction n =
4843 match m_a.(n) with
4844 | _, _, _, Action _ -> true
4845 | _ -> false
4846 end)
4848 let rec fillsrc prevmode prevuioh =
4849 let sep () = src#caption "" 0 in
4850 let colorp name get set =
4851 src#string name
4852 (fun () -> color_to_string (get ()))
4853 (fun v ->
4855 let c = color_of_string v in
4856 set c
4857 with exn ->
4858 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4861 let oldmode = state.mode in
4862 let birdseye = isbirdseye state.mode in
4864 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4866 src#bool "presentation mode"
4867 (fun () -> conf.presentation)
4868 (fun v -> setpresentationmode v);
4870 src#bool "ignore case in searches"
4871 (fun () -> conf.icase)
4872 (fun v -> conf.icase <- v);
4874 src#bool "preload"
4875 (fun () -> conf.preload)
4876 (fun v -> conf.preload <- v);
4878 src#bool "highlight links"
4879 (fun () -> conf.hlinks)
4880 (fun v -> conf.hlinks <- v);
4882 src#bool "under info"
4883 (fun () -> conf.underinfo)
4884 (fun v -> conf.underinfo <- v);
4886 src#bool "persistent bookmarks"
4887 (fun () -> conf.savebmarks)
4888 (fun v -> conf.savebmarks <- v);
4890 src#fitmodel "fit model"
4891 (fun () -> FMTE.to_string conf.fitmodel)
4892 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4894 src#bool "trim margins"
4895 (fun () -> conf.trimmargins)
4896 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4898 src#bool "persistent location"
4899 (fun () -> conf.jumpback)
4900 (fun v -> conf.jumpback <- v);
4902 sep ();
4903 src#int "inter-page space"
4904 (fun () -> conf.interpagespace)
4905 (fun n ->
4906 conf.interpagespace <- n;
4907 docolumns conf.columns;
4908 let pageno, py =
4909 match state.layout with
4910 | [] -> 0, 0
4911 | l :: _ ->
4912 l.pageno, l.pagey
4914 state.maxy <- calcheight ();
4915 let y = getpagey pageno in
4916 gotoy (y + py)
4919 src#int "page bias"
4920 (fun () -> conf.pagebias)
4921 (fun v -> conf.pagebias <- v);
4923 src#int "scroll step"
4924 (fun () -> conf.scrollstep)
4925 (fun n -> conf.scrollstep <- n);
4927 src#int "horizontal scroll step"
4928 (fun () -> conf.hscrollstep)
4929 (fun v -> conf.hscrollstep <- v);
4931 src#int "auto scroll step"
4932 (fun () ->
4933 match state.autoscroll with
4934 | Some step -> step
4935 | _ -> conf.autoscrollstep)
4936 (fun n ->
4937 let n = boundastep state.winh n in
4938 if state.autoscroll <> None
4939 then state.autoscroll <- Some n;
4940 conf.autoscrollstep <- n);
4942 src#int "zoom"
4943 (fun () -> truncate (conf.zoom *. 100.))
4944 (fun v -> setzoom ((float v) /. 100.));
4946 src#int "rotation"
4947 (fun () -> conf.angle)
4948 (fun v -> reqlayout v conf.fitmodel);
4950 src#int "scroll bar width"
4951 (fun () -> conf.scrollbw)
4952 (fun v ->
4953 conf.scrollbw <- v;
4954 reshape state.winw state.winh;
4957 src#int "scroll handle height"
4958 (fun () -> conf.scrollh)
4959 (fun v -> conf.scrollh <- v;);
4961 src#int "thumbnail width"
4962 (fun () -> conf.thumbw)
4963 (fun v ->
4964 conf.thumbw <- min 4096 v;
4965 match oldmode with
4966 | Birdseye beye ->
4967 leavebirdseye beye false;
4968 enterbirdseye ()
4969 | _ -> ()
4972 let mode = state.mode in
4973 src#string "columns"
4974 (fun () ->
4975 match conf.columns with
4976 | Csingle _ -> "1"
4977 | Cmulti (multi, _) -> multicolumns_to_string multi
4978 | Csplit (count, _) -> "-" ^ string_of_int count
4980 (fun v ->
4981 let n, a, b = multicolumns_of_string v in
4982 setcolumns mode n a b);
4984 sep ();
4985 src#caption "Pixmap cache" 0;
4986 src#int_with_suffix "size (advisory)"
4987 (fun () -> conf.memlimit)
4988 (fun v -> conf.memlimit <- v);
4990 src#caption2 "used"
4991 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4992 (string_with_suffix_of_int state.memused)
4993 (Hashtbl.length state.tilemap)) 1;
4995 sep ();
4996 src#caption "Layout" 0;
4997 src#caption2 "Dimension"
4998 (fun () ->
4999 Printf.sprintf "%dx%d (virtual %dx%d)"
5000 state.winw state.winh
5001 state.w state.maxy)
5003 if conf.debug
5004 then
5005 src#caption2 "Position" (fun () ->
5006 Printf.sprintf "%dx%d" state.x state.y
5008 else
5009 src#caption2 "Position" (fun () -> describe_location ()) 1
5012 sep ();
5013 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
5014 "Save these parameters as global defaults at exit"
5015 (fun () -> conf.bedefault)
5016 (fun v -> conf.bedefault <- v)
5019 sep ();
5020 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
5021 src#bool ~offset:0 ~btos "Extended parameters"
5022 (fun () -> !showextended)
5023 (fun v -> showextended := v; fillsrc prevmode prevuioh);
5024 if !showextended
5025 then (
5026 src#bool "checkers"
5027 (fun () -> conf.checkers)
5028 (fun v -> conf.checkers <- v; setcheckers v);
5029 src#bool "update cursor"
5030 (fun () -> conf.updatecurs)
5031 (fun v -> conf.updatecurs <- v);
5032 src#bool "verbose"
5033 (fun () -> conf.verbose)
5034 (fun v -> conf.verbose <- v);
5035 src#bool "invert colors"
5036 (fun () -> conf.invert)
5037 (fun v -> conf.invert <- v);
5038 src#bool "max fit"
5039 (fun () -> conf.maxhfit)
5040 (fun v -> conf.maxhfit <- v);
5041 src#bool "redirect stderr"
5042 (fun () -> conf.redirectstderr)
5043 (fun v -> conf.redirectstderr <- v; redirectstderr ());
5044 src#bool "pax mode"
5045 (fun () -> conf.pax != None)
5046 (fun v ->
5047 if v
5048 then conf.pax <- Some (ref (now (), 0, 0))
5049 else conf.pax <- None);
5050 src#string "uri launcher"
5051 (fun () -> conf.urilauncher)
5052 (fun v -> conf.urilauncher <- v);
5053 src#string "path launcher"
5054 (fun () -> conf.pathlauncher)
5055 (fun v -> conf.pathlauncher <- v);
5056 src#string "tile size"
5057 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
5058 (fun v ->
5060 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
5061 conf.tilew <- max 64 w;
5062 conf.tileh <- max 64 h;
5063 flushtiles ();
5064 with exn ->
5065 state.text <- Printf.sprintf "bad tile size `%s': %s"
5066 v (exntos exn)
5068 src#int "texture count"
5069 (fun () -> conf.texcount)
5070 (fun v ->
5071 if realloctexts v
5072 then conf.texcount <- v
5073 else showtext '!' " Failed to set texture count please retry later"
5075 src#int "slice height"
5076 (fun () -> conf.sliceheight)
5077 (fun v ->
5078 conf.sliceheight <- v;
5079 wcmd "sliceh %d" conf.sliceheight;
5081 src#int "anti-aliasing level"
5082 (fun () -> conf.aalevel)
5083 (fun v ->
5084 conf.aalevel <- bound v 0 8;
5085 state.anchor <- getanchor ();
5086 opendoc state.path state.password;
5088 src#string "page scroll scaling factor"
5089 (fun () -> string_of_float conf.pgscale)
5090 (fun v ->
5092 let s = float_of_string v in
5093 conf.pgscale <- s
5094 with exn ->
5095 state.text <- Printf.sprintf
5096 "bad page scroll scaling factor `%s': %s" v (exntos exn)
5099 src#int "ui font size"
5100 (fun () -> fstate.fontsize)
5101 (fun v -> setfontsize (bound v 5 100));
5102 src#int "hint font size"
5103 (fun () -> conf.hfsize)
5104 (fun v -> conf.hfsize <- bound v 5 100);
5105 colorp "background color"
5106 (fun () -> conf.bgcolor)
5107 (fun v -> conf.bgcolor <- v);
5108 src#bool "crop hack"
5109 (fun () -> conf.crophack)
5110 (fun v -> conf.crophack <- v);
5111 src#string "trim fuzz"
5112 (fun () -> irect_to_string conf.trimfuzz)
5113 (fun v ->
5115 conf.trimfuzz <- irect_of_string v;
5116 if conf.trimmargins
5117 then settrim true conf.trimfuzz;
5118 with exn ->
5119 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
5121 src#string "throttle"
5122 (fun () ->
5123 match conf.maxwait with
5124 | None -> "show place holder if page is not ready"
5125 | Some time ->
5126 if time = infinity
5127 then "wait for page to fully render"
5128 else
5129 "wait " ^ string_of_float time
5130 ^ " seconds before showing placeholder"
5132 (fun v ->
5134 let f = float_of_string v in
5135 if f <= 0.0
5136 then conf.maxwait <- None
5137 else conf.maxwait <- Some f
5138 with exn ->
5139 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
5141 src#string "ghyll scroll"
5142 (fun () ->
5143 match conf.ghyllscroll with
5144 | None -> ""
5145 | Some nab -> ghyllscroll_to_string nab
5147 (fun v ->
5148 try conf.ghyllscroll <- ghyllscroll_of_string v
5149 with exn ->
5150 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
5152 src#string "selection command"
5153 (fun () -> conf.selcmd)
5154 (fun v -> conf.selcmd <- v);
5155 src#string "synctex command"
5156 (fun () -> conf.stcmd)
5157 (fun v -> conf.stcmd <- v);
5158 src#string "pax command"
5159 (fun () -> conf.paxcmd)
5160 (fun v -> conf.paxcmd <- v);
5161 src#colorspace "color space"
5162 (fun () -> CSTE.to_string conf.colorspace)
5163 (fun v ->
5164 conf.colorspace <- CSTE.of_int v;
5165 wcmd "cs %d" v;
5166 load state.layout;
5168 src#paxmark "pax mark method"
5169 (fun () -> MTE.to_string conf.paxmark)
5170 (fun v -> conf.paxmark <- MTE.of_int v);
5171 if pbousable ()
5172 then
5173 src#bool "use PBO"
5174 (fun () -> conf.usepbo)
5175 (fun v -> conf.usepbo <- v);
5176 src#bool "mouse wheel scrolls pages"
5177 (fun () -> conf.wheelbypage)
5178 (fun v -> conf.wheelbypage <- v);
5179 src#bool "open remote links in a new instance"
5180 (fun () -> conf.riani)
5181 (fun v -> conf.riani <- v);
5184 sep ();
5185 src#caption "Document" 0;
5186 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
5187 src#caption2 "Pages"
5188 (fun () -> string_of_int state.pagecount) 1;
5189 src#caption2 "Dimensions"
5190 (fun () -> string_of_int (List.length state.pdims)) 1;
5191 if conf.trimmargins
5192 then (
5193 sep ();
5194 src#caption "Trimmed margins" 0;
5195 src#caption2 "Dimensions"
5196 (fun () -> string_of_int (List.length state.pdims)) 1;
5199 sep ();
5200 src#caption "OpenGL" 0;
5201 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
5202 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
5204 sep ();
5205 src#caption "Location" 0;
5206 if nonemptystr state.origin
5207 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
5208 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5210 src#reset prevmode prevuioh;
5212 fun () ->
5213 state.text <- "";
5214 let prevmode = state.mode
5215 and prevuioh = state.uioh in
5216 fillsrc prevmode prevuioh;
5217 let source = (src :> lvsource) in
5218 let modehash = findkeyhash conf "info" in
5219 state.uioh <- coe (object (self)
5220 inherit listview ~source ~trusted:true ~modehash as super
5221 val mutable m_prevmemused = 0
5222 method infochanged = function
5223 | Memused ->
5224 if m_prevmemused != state.memused
5225 then (
5226 m_prevmemused <- state.memused;
5227 G.postRedisplay "memusedchanged";
5229 | Pdim -> G.postRedisplay "pdimchanged"
5230 | Docinfo -> fillsrc prevmode prevuioh
5232 method key key mask =
5233 if not (Wsi.withctrl mask)
5234 then
5235 match key with
5236 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5237 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5238 | _ -> super#key key mask
5239 else super#key key mask
5240 end);
5241 G.postRedisplay "info";
5244 let enterhelpmode =
5245 let source =
5246 (object
5247 inherit lvsourcebase
5248 method getitemcount = Array.length state.help
5249 method getitem n =
5250 let s, l, _ = state.help.(n) in
5251 (s, l)
5253 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5254 let optuioh =
5255 if not cancel
5256 then (
5257 m_qsearch <- qsearch;
5258 match state.help.(active) with
5259 | _, _, Action f -> Some (f uioh)
5260 | _ -> Some (uioh)
5262 else None
5264 m_active <- active;
5265 m_first <- first;
5266 m_pan <- pan;
5267 optuioh
5269 method hasaction n =
5270 match state.help.(n) with
5271 | _, _, Action _ -> true
5272 | _ -> false
5274 initializer
5275 m_active <- -1
5276 end)
5277 in fun () ->
5278 let modehash = findkeyhash conf "help" in
5279 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5280 G.postRedisplay "help";
5283 let entermsgsmode =
5284 let msgsource =
5285 let re = Str.regexp "[\r\n]" in
5286 (object
5287 inherit lvsourcebase
5288 val mutable m_items = [||]
5290 method getitemcount = 1 + Array.length m_items
5292 method getitem n =
5293 if n = 0
5294 then "[Clear]", 0
5295 else m_items.(n-1), 0
5297 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5298 ignore uioh;
5299 if not cancel
5300 then (
5301 if active = 0
5302 then Buffer.clear state.errmsgs;
5303 m_qsearch <- qsearch;
5305 m_active <- active;
5306 m_first <- first;
5307 m_pan <- pan;
5308 None
5310 method hasaction n =
5311 n = 0
5313 method reset =
5314 state.newerrmsgs <- false;
5315 let l = Str.split re (Buffer.contents state.errmsgs) in
5316 m_items <- Array.of_list l
5318 initializer
5319 m_active <- 0
5320 end)
5321 in fun () ->
5322 state.text <- "";
5323 msgsource#reset;
5324 let source = (msgsource :> lvsource) in
5325 let modehash = findkeyhash conf "listview" in
5326 state.uioh <- coe (object
5327 inherit listview ~source ~trusted:false ~modehash as super
5328 method display =
5329 if state.newerrmsgs
5330 then msgsource#reset;
5331 super#display
5332 end);
5333 G.postRedisplay "msgs";
5336 let quickbookmark ?title () =
5337 match state.layout with
5338 | [] -> ()
5339 | l :: _ ->
5340 let title =
5341 match title with
5342 | None ->
5343 let sec = Unix.gettimeofday () in
5344 let tm = Unix.localtime sec in
5345 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5346 (l.pageno+1)
5347 tm.Unix.tm_mday
5348 tm.Unix.tm_mon
5349 (tm.Unix.tm_year + 1900)
5350 tm.Unix.tm_hour
5351 tm.Unix.tm_min
5352 | Some title -> title
5354 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5357 let setautoscrollspeed step goingdown =
5358 let incr = max 1 ((abs step) / 2) in
5359 let incr = if goingdown then incr else -incr in
5360 let astep = boundastep state.winh (step + incr) in
5361 state.autoscroll <- Some astep;
5364 let canpan () =
5365 match conf.columns with
5366 | Csplit _ -> true
5367 | _ -> state.x != 0 || conf.zoom > 1.0
5370 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5372 let existsinrow pageno (columns, coverA, coverB) p =
5373 let last = ((pageno - coverA) mod columns) + columns in
5374 let rec any = function
5375 | [] -> false
5376 | l :: rest ->
5377 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5378 then p l
5379 else (
5380 if not (p l)
5381 then (if l.pageno = last then false else any rest)
5382 else true
5385 any state.layout
5388 let nextpage () =
5389 match state.layout with
5390 | [] ->
5391 let pageno = page_of_y state.y in
5392 gotoghyll (getpagey (pageno+1))
5393 | l :: rest ->
5394 match conf.columns with
5395 | Csingle _ ->
5396 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5397 then
5398 let y = clamp (pgscale state.winh) in
5399 gotoghyll y
5400 else
5401 let pageno = min (l.pageno+1) (state.pagecount-1) in
5402 gotoghyll (getpagey pageno)
5403 | Cmulti ((c, _, _) as cl, _) ->
5404 if conf.presentation
5405 && (existsinrow l.pageno cl
5406 (fun l -> l.pageh > l.pagey + l.pagevh))
5407 then
5408 let y = clamp (pgscale state.winh) in
5409 gotoghyll y
5410 else
5411 let pageno = min (l.pageno+c) (state.pagecount-1) in
5412 gotoghyll (getpagey pageno)
5413 | Csplit (n, _) ->
5414 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5415 then
5416 let pagey, pageh = getpageyh l.pageno in
5417 let pagey = pagey + pageh * l.pagecol in
5418 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5419 gotoghyll (pagey + pageh + ips)
5422 let prevpage () =
5423 match state.layout with
5424 | [] ->
5425 let pageno = page_of_y state.y in
5426 gotoghyll (getpagey (pageno-1))
5427 | l :: _ ->
5428 match conf.columns with
5429 | Csingle _ ->
5430 if conf.presentation && l.pagey != 0
5431 then
5432 gotoghyll (clamp (pgscale ~-(state.winh)))
5433 else
5434 let pageno = max 0 (l.pageno-1) in
5435 gotoghyll (getpagey pageno)
5436 | Cmulti ((c, _, coverB) as cl, _) ->
5437 if conf.presentation &&
5438 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5439 then
5440 gotoghyll (clamp (pgscale ~-(state.winh)))
5441 else
5442 let decr =
5443 if l.pageno = state.pagecount - coverB
5444 then 1
5445 else c
5447 let pageno = max 0 (l.pageno-decr) in
5448 gotoghyll (getpagey pageno)
5449 | Csplit (n, _) ->
5450 let y =
5451 if l.pagecol = 0
5452 then
5453 if l.pageno = 0
5454 then l.pagey
5455 else
5456 let pageno = max 0 (l.pageno-1) in
5457 let pagey, pageh = getpageyh pageno in
5458 pagey + (n-1)*pageh
5459 else
5460 let pagey, pageh = getpageyh l.pageno in
5461 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5463 gotoghyll y
5466 let viewkeyboard key mask =
5467 let enttext te =
5468 let mode = state.mode in
5469 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5470 state.text <- "";
5471 enttext ();
5472 G.postRedisplay "view:enttext"
5474 let ctrl = Wsi.withctrl mask in
5475 let key =
5476 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5478 match key with
5479 | 81 -> (* Q *)
5480 exit 0
5482 | 0xff63 -> (* insert *)
5483 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5484 then (
5485 state.mode <- LinkNav (Ltgendir 0);
5486 gotoy state.y;
5488 else showtext '!' "Keyboard link navigation does not work under rotation"
5490 | 0xff1b | 113 -> (* escape / q *)
5491 begin match state.mstate with
5492 | Mzoomrect _ ->
5493 state.mstate <- Mnone;
5494 Wsi.setcursor Wsi.CURSOR_INHERIT;
5495 G.postRedisplay "kill zoom rect";
5496 | _ ->
5497 begin match state.mode with
5498 | LinkNav _ ->
5499 state.mode <- View;
5500 G.postRedisplay "esc leave linknav"
5501 | _ ->
5502 match state.ranchors with
5503 | [] -> raise Quit
5504 | (path, password, anchor, origin) :: rest ->
5505 state.ranchors <- rest;
5506 state.anchor <- anchor;
5507 state.origin <- origin;
5508 state.nameddest <- "";
5509 opendoc path password
5510 end;
5511 end;
5513 | 0xff08 -> (* backspace *)
5514 gotoghyll (getnav ~-1)
5516 | 111 -> (* o *)
5517 enteroutlinemode ()
5519 | 117 -> (* u *)
5520 state.rects <- [];
5521 state.text <- "";
5522 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
5523 G.postRedisplay "dehighlight";
5525 | 47 | 63 -> (* / ? *)
5526 let ondone isforw s =
5527 cbput state.hists.pat s;
5528 state.searchpattern <- s;
5529 search s isforw
5531 let s = String.create 1 in
5532 s.[0] <- Char.chr key;
5533 enttext (s, "", Some (onhist state.hists.pat),
5534 textentry, ondone (key = 47), true)
5536 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5537 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5538 setzoom (conf.zoom +. incr)
5540 | 43 | 0xffab -> (* + *)
5541 let ondone s =
5542 let n =
5543 try int_of_string s with exc ->
5544 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5545 max_int
5547 if n != max_int
5548 then (
5549 conf.pagebias <- n;
5550 state.text <- "page bias is now " ^ string_of_int n;
5553 enttext ("page bias: ", "", None, intentry, ondone, true)
5555 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5556 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5557 setzoom (max 0.01 (conf.zoom -. decr))
5559 | 45 | 0xffad -> (* - *)
5560 let ondone msg = state.text <- msg in
5561 enttext (
5562 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5563 optentry state.mode, ondone, true
5566 | 48 when ctrl -> (* ctrl-0 *)
5567 if conf.zoom = 1.0
5568 then (
5569 state.x <- 0;
5570 gotoy state.y
5572 else setzoom 1.0
5574 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5575 let cols =
5576 match conf.columns with
5577 | Csingle _ | Cmulti _ -> 1
5578 | Csplit (n, _) -> n
5580 let h = state.winh -
5581 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5583 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5584 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5585 then setzoom zoom
5587 | 51 when ctrl -> (* ctrl-3 *)
5588 let fm =
5589 match conf.fitmodel with
5590 | FitWidth -> FitProportional
5591 | FitProportional -> FitPage
5592 | FitPage -> FitWidth
5594 state.text <- "fit model: " ^ FMTE.to_string fm;
5595 reqlayout conf.angle fm
5597 | 0xffc6 -> (* f9 *)
5598 togglebirdseye ()
5600 | 57 when ctrl -> (* ctrl-9 *)
5601 togglebirdseye ()
5603 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5604 when not ctrl -> (* 0..9 *)
5605 let ondone s =
5606 let n =
5607 try int_of_string s with exc ->
5608 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5611 if n >= 0
5612 then (
5613 addnav ();
5614 cbput state.hists.pag (string_of_int n);
5615 gotopage1 (n + conf.pagebias - 1) 0;
5618 let pageentry text key =
5619 match Char.unsafe_chr key with
5620 | 'g' -> TEdone text
5621 | _ -> intentry text key
5623 let text = "x" in text.[0] <- Char.chr key;
5624 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5626 | 98 -> (* b *)
5627 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5628 reshape state.winw state.winh;
5630 | 66 -> (* B *)
5631 state.bzoom <- not state.bzoom;
5632 state.rects <- [];
5633 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5635 | 108 -> (* l *)
5636 conf.hlinks <- not conf.hlinks;
5637 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5638 G.postRedisplay "toggle highlightlinks";
5640 | 70 -> (* F *)
5641 state.glinks <- true;
5642 let mode = state.mode in
5643 state.mode <- Textentry (
5644 (":", "", None, linknentry, linkndone gotounder, false),
5645 (fun _ ->
5646 state.glinks <- false;
5647 state.mode <- mode)
5649 state.text <- "";
5650 G.postRedisplay "view:linkent(F)"
5652 | 121 -> (* y *)
5653 state.glinks <- true;
5654 let mode = state.mode in
5655 state.mode <- Textentry (
5657 ":", "", None, linknentry, linkndone (fun under ->
5658 selstring (undertext under);
5659 ), false
5661 fun _ ->
5662 state.glinks <- false;
5663 state.mode <- mode
5665 state.text <- "";
5666 G.postRedisplay "view:linkent"
5668 | 97 -> (* a *)
5669 begin match state.autoscroll with
5670 | Some step ->
5671 conf.autoscrollstep <- step;
5672 state.autoscroll <- None
5673 | None ->
5674 if conf.autoscrollstep = 0
5675 then state.autoscroll <- Some 1
5676 else state.autoscroll <- Some conf.autoscrollstep
5679 | 112 when ctrl -> (* ctrl-p *)
5680 launchpath ()
5682 | 80 -> (* P *)
5683 setpresentationmode (not conf.presentation);
5684 showtext ' ' ("presentation mode " ^
5685 if conf.presentation then "on" else "off");
5687 | 102 -> (* f *)
5688 if List.mem Wsi.Fullscreen state.winstate
5689 then Wsi.reshape conf.cwinw conf.cwinh
5690 else Wsi.fullscreen ()
5692 | 112 | 78 -> (* p|N *)
5693 search state.searchpattern false
5695 | 110 | 0xffc0 -> (* n|F3 *)
5696 search state.searchpattern true
5698 | 116 -> (* t *)
5699 begin match state.layout with
5700 | [] -> ()
5701 | l :: _ ->
5702 gotoghyll (getpagey l.pageno)
5705 | 32 -> (* space *)
5706 nextpage ()
5708 | 0xff9f | 0xffff -> (* delete *)
5709 prevpage ()
5711 | 61 -> (* = *)
5712 showtext ' ' (describe_location ());
5714 | 119 -> (* w *)
5715 begin match state.layout with
5716 | [] -> ()
5717 | l :: _ ->
5718 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5719 G.postRedisplay "w"
5722 | 39 -> (* ' *)
5723 enterbookmarkmode ()
5725 | 104 | 0xffbe -> (* h|F1 *)
5726 enterhelpmode ()
5728 | 105 -> (* i *)
5729 enterinfomode ()
5731 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5732 entermsgsmode ()
5734 | 109 -> (* m *)
5735 let ondone s =
5736 match state.layout with
5737 | l :: _ ->
5738 if nonemptystr s
5739 then
5740 state.bookmarks <-
5741 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5742 | _ -> ()
5744 enttext ("bookmark: ", "", None, textentry, ondone, true)
5746 | 126 -> (* ~ *)
5747 quickbookmark ();
5748 showtext ' ' "Quick bookmark added";
5750 | 122 -> (* z *)
5751 begin match state.layout with
5752 | l :: _ ->
5753 let rect = getpdimrect l.pagedimno in
5754 let w, h =
5755 if conf.crophack
5756 then
5757 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5758 truncate (1.2 *. (rect.(3) -. rect.(0))))
5759 else
5760 (truncate (rect.(1) -. rect.(0)),
5761 truncate (rect.(3) -. rect.(0)))
5763 let w = truncate ((float w)*.conf.zoom)
5764 and h = truncate ((float h)*.conf.zoom) in
5765 if w != 0 && h != 0
5766 then (
5767 state.anchor <- getanchor ();
5768 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5770 G.postRedisplay "z";
5772 | [] -> ()
5775 | 120 -> (* x *)
5776 state.roam ()
5777 | 60 | 62 -> (* < > *)
5778 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5780 | 91 | 93 -> (* [ ] *)
5781 conf.colorscale <-
5782 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5784 G.postRedisplay "brightness";
5786 | 99 when state.mode = View -> (* [alt-]c *)
5787 if Wsi.withalt mask
5788 then (
5789 if conf.zoom > 1.0
5790 then
5791 let m = (wadjsb state.winw - state.w) / 2 in
5792 state.x <- m;
5793 gotoy_and_clear_text state.y
5795 else
5796 let (c, a, b), z =
5797 match state.prevcolumns with
5798 | None -> (1, 0, 0), 1.0
5799 | Some (columns, z) ->
5800 let cab =
5801 match columns with
5802 | Csplit (c, _) -> -c, 0, 0
5803 | Cmulti ((c, a, b), _) -> c, a, b
5804 | Csingle _ -> 1, 0, 0
5806 cab, z
5808 setcolumns View c a b;
5809 setzoom z
5811 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5812 -> (* ctrl-shift- (kp) [up|down] *)
5813 let zoom, x = state.prevzoom in
5814 setzoom zoom;
5815 state.x <- x;
5817 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5818 begin match state.autoscroll with
5819 | None ->
5820 begin match state.mode with
5821 | Birdseye beye -> upbirdseye 1 beye
5822 | _ ->
5823 if ctrl
5824 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5825 else (
5826 if not (Wsi.withshift mask) && conf.presentation
5827 then prevpage ()
5828 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5831 | Some n ->
5832 setautoscrollspeed n false
5835 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5836 begin match state.autoscroll with
5837 | None ->
5838 begin match state.mode with
5839 | Birdseye beye -> downbirdseye 1 beye
5840 | _ ->
5841 if ctrl
5842 then gotoy_and_clear_text (clamp (state.winh/2))
5843 else (
5844 if not (Wsi.withshift mask) && conf.presentation
5845 then nextpage ()
5846 else gotoy_and_clear_text (clamp conf.scrollstep)
5849 | Some n ->
5850 setautoscrollspeed n true
5853 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5854 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5855 if canpan ()
5856 then
5857 let dx =
5858 if ctrl
5859 then state.winw / 2
5860 else conf.hscrollstep
5862 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5863 state.x <- panbound (state.x + dx);
5864 gotoy_and_clear_text state.y
5865 else (
5866 state.text <- "";
5867 G.postRedisplay "left/right"
5870 | 0xff55 | 0xff9a -> (* (kp) prior *)
5871 let y =
5872 if ctrl
5873 then
5874 match state.layout with
5875 | [] -> state.y
5876 | l :: _ -> state.y - l.pagey
5877 else
5878 clamp (pgscale (-state.winh))
5880 gotoghyll y
5882 | 0xff56 | 0xff9b -> (* (kp) next *)
5883 let y =
5884 if ctrl
5885 then
5886 match List.rev state.layout with
5887 | [] -> state.y
5888 | l :: _ -> getpagey l.pageno
5889 else
5890 clamp (pgscale state.winh)
5892 gotoghyll y
5894 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5895 gotoghyll 0
5896 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5897 gotoghyll (clamp state.maxy)
5899 | 0xff53 | 0xff98
5900 when Wsi.withalt mask -> (* alt-(kp) right *)
5901 gotoghyll (getnav 1)
5902 | 0xff51 | 0xff96
5903 when Wsi.withalt mask -> (* alt-(kp) left *)
5904 gotoghyll (getnav ~-1)
5906 | 114 -> (* r *)
5907 reload ()
5909 | 118 when conf.debug -> (* v *)
5910 state.rects <- [];
5911 List.iter (fun l ->
5912 match getopaque l.pageno with
5913 | None -> ()
5914 | Some opaque ->
5915 let x0, y0, x1, y1 = pagebbox opaque in
5916 let a,b = float x0, float y0 in
5917 let c,d = float x1, float y0 in
5918 let e,f = float x1, float y1 in
5919 let h,j = float x0, float y1 in
5920 let rect = (a,b,c,d,e,f,h,j) in
5921 debugrect rect;
5922 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5923 ) state.layout;
5924 G.postRedisplay "v";
5926 | 124 -> (* | *)
5927 let mode = state.mode in
5928 let cmd = ref "" in
5929 let onleave = function
5930 | Cancel -> state.mode <- mode
5931 | Confirm ->
5932 List.iter (fun l ->
5933 match getopaque l.pageno with
5934 | Some opaque -> pipesel opaque !cmd
5935 | None -> ()) state.layout;
5936 state.mode <- mode
5938 let ondone s =
5939 cbput state.hists.sel s;
5940 cmd := s
5942 let te =
5943 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5945 G.postRedisplay "|";
5946 state.mode <- Textentry (te, onleave);
5948 | _ ->
5949 vlog "huh? %s" (Wsi.keyname key)
5952 let linknavkeyboard key mask linknav =
5953 let getpage pageno =
5954 let rec loop = function
5955 | [] -> None
5956 | l :: _ when l.pageno = pageno -> Some l
5957 | _ :: rest -> loop rest
5958 in loop state.layout
5960 let doexact (pageno, n) =
5961 match getopaque pageno, getpage pageno with
5962 | Some opaque, Some l ->
5963 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5964 then
5965 let under = getlink opaque n in
5966 G.postRedisplay "link gotounder";
5967 gotounder under;
5968 state.mode <- View;
5969 else
5970 let opt, dir =
5971 match key with
5972 | 0xff50 -> (* home *)
5973 Some (findlink opaque LDfirst), -1
5975 | 0xff57 -> (* end *)
5976 Some (findlink opaque LDlast), 1
5978 | 0xff51 -> (* left *)
5979 Some (findlink opaque (LDleft n)), -1
5981 | 0xff53 -> (* right *)
5982 Some (findlink opaque (LDright n)), 1
5984 | 0xff52 -> (* up *)
5985 Some (findlink opaque (LDup n)), -1
5987 | 0xff54 -> (* down *)
5988 Some (findlink opaque (LDdown n)), 1
5990 | _ -> None, 0
5992 let pwl l dir =
5993 begin match findpwl l.pageno dir with
5994 | Pwlnotfound -> ()
5995 | Pwl pageno ->
5996 let notfound dir =
5997 state.mode <- LinkNav (Ltgendir dir);
5998 let y, h = getpageyh pageno in
5999 let y =
6000 if dir < 0
6001 then y + h - state.winh
6002 else y
6004 gotoy y
6006 begin match getopaque pageno, getpage pageno with
6007 | Some opaque, Some _ ->
6008 let link =
6009 let ld = if dir > 0 then LDfirst else LDlast in
6010 findlink opaque ld
6012 begin match link with
6013 | Lfound m ->
6014 showlinktype (getlink opaque m);
6015 state.mode <- LinkNav (Ltexact (pageno, m));
6016 G.postRedisplay "linknav jpage";
6017 | _ -> notfound dir
6018 end;
6019 | _ -> notfound dir
6020 end;
6021 end;
6023 begin match opt with
6024 | Some Lnotfound -> pwl l dir;
6025 | Some (Lfound m) ->
6026 if m = n
6027 then pwl l dir
6028 else (
6029 let _, y0, _, y1 = getlinkrect opaque m in
6030 if y0 < l.pagey
6031 then gotopage1 l.pageno y0
6032 else (
6033 let d = fstate.fontsize + 1 in
6034 if y1 - l.pagey > l.pagevh - d
6035 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
6036 else G.postRedisplay "linknav";
6038 showlinktype (getlink opaque m);
6039 state.mode <- LinkNav (Ltexact (l.pageno, m));
6042 | None -> viewkeyboard key mask
6043 end;
6044 | _ -> viewkeyboard key mask
6046 if key = 0xff63
6047 then (
6048 state.mode <- View;
6049 G.postRedisplay "leave linknav"
6051 else
6052 match linknav with
6053 | Ltgendir _ -> viewkeyboard key mask
6054 | Ltexact exact -> doexact exact
6057 let keyboard key mask =
6058 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
6059 then wcmd "interrupt"
6060 else state.uioh <- state.uioh#key key mask
6063 let birdseyekeyboard key mask
6064 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
6065 let incr =
6066 match conf.columns with
6067 | Csingle _ -> 1
6068 | Cmulti ((c, _, _), _) -> c
6069 | Csplit _ -> failwith "bird's eye split mode"
6071 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
6072 match key with
6073 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
6074 let y, h = getpageyh pageno in
6075 let top = (state.winh - h) / 2 in
6076 gotoy (max 0 (y - top))
6077 | 0xff0d (* enter *)
6078 | 0xff8d -> leavebirdseye beye false (* kp enter *)
6079 | 0xff1b -> leavebirdseye beye true (* escape *)
6080 | 0xff52 -> upbirdseye incr beye (* up *)
6081 | 0xff54 -> downbirdseye incr beye (* down *)
6082 | 0xff51 -> upbirdseye 1 beye (* left *)
6083 | 0xff53 -> downbirdseye 1 beye (* right *)
6085 | 0xff55 -> (* prior *)
6086 begin match state.layout with
6087 | l :: _ ->
6088 if l.pagey != 0
6089 then (
6090 state.mode <- Birdseye (
6091 oconf, leftx, l.pageno, hooverpageno, anchor
6093 gotopage1 l.pageno 0;
6095 else (
6096 let layout = layout (state.y-state.winh) (pgh state.layout) in
6097 match layout with
6098 | [] -> gotoy (clamp (-state.winh))
6099 | l :: _ ->
6100 state.mode <- Birdseye (
6101 oconf, leftx, l.pageno, hooverpageno, anchor
6103 gotopage1 l.pageno 0
6106 | [] -> gotoy (clamp (-state.winh))
6107 end;
6109 | 0xff56 -> (* next *)
6110 begin match List.rev state.layout with
6111 | l :: _ ->
6112 let layout = layout (state.y + (pgh state.layout)) state.winh in
6113 begin match layout with
6114 | [] ->
6115 let incr = l.pageh - l.pagevh in
6116 if incr = 0
6117 then (
6118 state.mode <-
6119 Birdseye (
6120 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
6122 G.postRedisplay "birdseye pagedown";
6124 else gotoy (clamp (incr + conf.interpagespace*2));
6126 | l :: _ ->
6127 state.mode <-
6128 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
6129 gotopage1 l.pageno 0;
6132 | [] -> gotoy (clamp state.winh)
6133 end;
6135 | 0xff50 -> (* home *)
6136 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
6137 gotopage1 0 0
6139 | 0xff57 -> (* end *)
6140 let pageno = state.pagecount - 1 in
6141 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
6142 if not (pagevisible state.layout pageno)
6143 then
6144 let h =
6145 match List.rev state.pdims with
6146 | [] -> state.winh
6147 | (_, _, h, _) :: _ -> h
6149 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
6150 else G.postRedisplay "birdseye end";
6151 | _ -> viewkeyboard key mask
6154 let drawpage l =
6155 let color =
6156 match state.mode with
6157 | Textentry _ -> scalecolor 0.4
6158 | LinkNav _
6159 | View -> scalecolor 1.0
6160 | Birdseye (_, _, pageno, hooverpageno, _) ->
6161 if l.pageno = hooverpageno
6162 then scalecolor 0.9
6163 else (
6164 if l.pageno = pageno
6165 then scalecolor 1.0
6166 else scalecolor 0.8
6169 drawtiles l color;
6172 let postdrawpage l linkindexbase =
6173 match getopaque l.pageno with
6174 | Some opaque ->
6175 if tileready l l.pagex l.pagey
6176 then
6177 let x = l.pagedispx - l.pagex
6178 and y = l.pagedispy - l.pagey in
6179 let hlmask =
6180 match conf.columns with
6181 | Csingle _ | Cmulti _ ->
6182 (if conf.hlinks then 1 else 0)
6183 + (if state.glinks
6184 && not (isbirdseye state.mode) then 2 else 0)
6185 | _ -> 0
6187 let s =
6188 match state.mode with
6189 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6190 | _ -> ""
6192 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6193 else 0
6194 | _ -> 0
6197 let scrollindicator () =
6198 let sbw, ph, sh = state.uioh#scrollph in
6199 let sbh, pw, sw = state.uioh#scrollpw in
6201 GlDraw.color (0.64, 0.64, 0.64);
6202 filledrect
6203 (float (state.winw - sbw)) 0.
6204 (float state.winw) (float state.winh)
6206 filledrect
6207 0. (float (state.winh - sbh))
6208 (float (wadjsb state.winw - 1)) (float state.winh)
6210 GlDraw.color (0.0, 0.0, 0.0);
6212 filledrect
6213 (float (state.winw - sbw)) ph
6214 (float state.winw) (ph +. sh)
6216 filledrect
6217 pw (float (state.winh - sbh))
6218 (pw +. sw) (float state.winh)
6222 let showsel () =
6223 match state.mstate with
6224 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6227 | Msel ((x0, y0), (x1, y1)) ->
6228 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
6229 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< "", -1, 0, 0) in
6230 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< "", -1, 0, 0) in
6231 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
6234 let showrects = function [] -> () | rects ->
6235 Gl.enable `blend;
6236 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6237 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6238 List.iter
6239 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6240 List.iter (fun l ->
6241 if l.pageno = pageno
6242 then (
6243 let dx = float (l.pagedispx - l.pagex) in
6244 let dy = float (l.pagedispy - l.pagey) in
6245 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6246 Raw.sets_float state.vraw ~pos:0
6247 [| x0+.dx; y0+.dy;
6248 x1+.dx; y1+.dy;
6249 x3+.dx; y3+.dy;
6250 x2+.dx; y2+.dy |];
6251 GlArray.vertex `two state.vraw;
6252 GlArray.draw_arrays `triangle_strip 0 4;
6254 ) state.layout
6255 ) rects
6257 Gl.disable `blend;
6260 let display () =
6261 GlClear.color (scalecolor2 conf.bgcolor);
6262 GlClear.clear [`color];
6263 List.iter drawpage state.layout;
6264 let rects =
6265 match state.mode with
6266 | LinkNav (Ltexact (pageno, linkno)) ->
6267 begin match getopaque pageno with
6268 | Some opaque ->
6269 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6270 (pageno, 5, (
6271 float x0, float y0,
6272 float x1, float y0,
6273 float x1, float y1,
6274 float x0, float y1)
6275 ) :: state.rects
6276 | None -> state.rects
6278 | _ -> state.rects
6280 showrects rects;
6281 let rec postloop linkindexbase = function
6282 | l :: rest ->
6283 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6284 postloop linkindexbase rest
6285 | [] -> ()
6287 showsel ();
6288 postloop 0 state.layout;
6289 state.uioh#display;
6290 begin match state.mstate with
6291 | Mzoomrect ((x0, y0), (x1, y1)) ->
6292 Gl.enable `blend;
6293 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6294 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6295 filledrect (float x0) (float y0) (float x1) (float y1);
6296 Gl.disable `blend;
6297 | _ -> ()
6298 end;
6299 enttext ();
6300 scrollindicator ();
6301 Wsi.swapb ();
6304 let zoomrect x y x1 y1 =
6305 let x0 = min x x1
6306 and x1 = max x x1
6307 and y0 = min y y1 in
6308 gotoy (state.y + y0);
6309 state.anchor <- getanchor ();
6310 let zoom = (float state.w) /. float (x1 - x0) in
6311 let margin =
6312 match conf.fitmodel, conf.columns with
6313 | FitPage, Csplit _ ->
6314 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6316 | _, _ ->
6317 let adjw = wadjsb state.winw in
6318 if state.w < adjw
6319 then (adjw - state.w) / 2
6320 else 0
6322 state.x <- (state.x + margin) - x0;
6323 setzoom zoom;
6324 Wsi.setcursor Wsi.CURSOR_INHERIT;
6325 state.mstate <- Mnone;
6328 let zoomblock x y =
6329 let g opaque l px py =
6330 match rectofblock opaque px py with
6331 | Some a ->
6332 let x0 = a.(0) -. 20. in
6333 let x1 = a.(1) +. 20. in
6334 let y0 = a.(2) -. 20. in
6335 let zoom = (float state.w) /. (x1 -. x0) in
6336 let pagey = getpagey l.pageno in
6337 gotoy_and_clear_text (pagey + truncate y0);
6338 state.anchor <- getanchor ();
6339 let margin = (state.w - l.pagew)/2 in
6340 state.x <- -truncate x0 - margin;
6341 setzoom zoom;
6342 None
6343 | None -> None
6345 match conf.columns with
6346 | Csplit _ ->
6347 showtext '!' "block zooming does not work properly in split columns mode"
6348 | _ -> onppundermouse g x y ()
6351 let scrollx x =
6352 let winw = wadjsb state.winw - 1 in
6353 let s = float x /. float winw in
6354 let destx = truncate (float (state.w + winw) *. s) in
6355 state.x <- winw - destx;
6356 gotoy_and_clear_text state.y;
6357 state.mstate <- Mscrollx;
6360 let scrolly y =
6361 let s = float y /. float state.winh in
6362 let desty = truncate (float (state.maxy - state.winh) *. s) in
6363 gotoy_and_clear_text desty;
6364 state.mstate <- Mscrolly;
6367 let viewmulticlick clicks x y mask =
6368 let g opaque l px py =
6369 let mark =
6370 match clicks with
6371 | 2 -> Mark_word
6372 | 3 -> Mark_line
6373 | 4 -> Mark_block
6374 | _ -> Mark_page
6376 if markunder opaque px py mark
6377 then (
6378 Some (fun () ->
6379 let dopipe cmd =
6380 match getopaque l.pageno with
6381 | None -> ()
6382 | Some opaque -> pipesel opaque cmd
6384 state.roam <- (fun () -> dopipe conf.paxcmd);
6385 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
6388 else None
6390 G.postRedisplay "viewmulticlick";
6391 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
6394 let canselect () =
6395 match conf.columns with
6396 | Csplit _ -> false
6397 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
6400 let viewmouse button down x y mask =
6401 match button with
6402 | n when (n == 4 || n == 5) && not down ->
6403 if Wsi.withctrl mask
6404 then (
6405 match state.mstate with
6406 | Mzoom (oldn, i) ->
6407 if oldn = n
6408 then (
6409 if i = 2
6410 then
6411 let incr =
6412 match n with
6413 | 5 ->
6414 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6415 | _ ->
6416 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6418 let zoom = conf.zoom -. incr in
6419 setzoom zoom;
6420 state.mstate <- Mzoom (n, 0);
6421 else
6422 state.mstate <- Mzoom (n, i+1);
6424 else state.mstate <- Mzoom (n, 0)
6426 | _ -> state.mstate <- Mzoom (n, 0)
6428 else (
6429 match state.autoscroll with
6430 | Some step -> setautoscrollspeed step (n=4)
6431 | None ->
6432 if conf.wheelbypage || conf.presentation
6433 then (
6434 if n = 4
6435 then prevpage ()
6436 else nextpage ()
6438 else
6439 let incr =
6440 if n = 4
6441 then -conf.scrollstep
6442 else conf.scrollstep
6444 let incr = incr * 2 in
6445 let y = clamp incr in
6446 gotoy_and_clear_text y
6449 | n when (n = 6 || n = 7) && not down && canpan () ->
6450 state.x <-
6451 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6452 gotoy_and_clear_text state.y
6454 | 1 when Wsi.withshift mask ->
6455 state.mstate <- Mnone;
6456 if not down
6457 then (
6458 match unproject x y with
6459 | Some (pageno, ux, uy) ->
6460 let cmd = Printf.sprintf
6461 "%s %s %d %d %d"
6462 conf.stcmd state.path pageno ux uy
6464 popen cmd []
6465 | None -> ()
6468 | 1 when Wsi.withctrl mask ->
6469 if down
6470 then (
6471 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6472 state.mstate <- Mpan (x, y)
6474 else
6475 state.mstate <- Mnone
6477 | 3 ->
6478 if down
6479 then (
6480 Wsi.setcursor Wsi.CURSOR_CYCLE;
6481 let p = (x, y) in
6482 state.mstate <- Mzoomrect (p, p)
6484 else (
6485 match state.mstate with
6486 | Mzoomrect ((x0, y0), _) ->
6487 if abs (x-x0) > 10 && abs (y - y0) > 10
6488 then zoomrect x0 y0 x y
6489 else (
6490 state.mstate <- Mnone;
6491 Wsi.setcursor Wsi.CURSOR_INHERIT;
6492 G.postRedisplay "kill accidental zoom rect";
6494 | _ ->
6495 Wsi.setcursor Wsi.CURSOR_INHERIT;
6496 state.mstate <- Mnone
6499 | 1 when x > state.winw - vscrollw () ->
6500 if down
6501 then
6502 let _, position, sh = state.uioh#scrollph in
6503 if y > truncate position && y < truncate (position +. sh)
6504 then state.mstate <- Mscrolly
6505 else scrolly y
6506 else
6507 state.mstate <- Mnone
6509 | 1 when y > state.winh - hscrollh () ->
6510 if down
6511 then
6512 let _, position, sw = state.uioh#scrollpw in
6513 if x > truncate position && x < truncate (position +. sw)
6514 then state.mstate <- Mscrollx
6515 else scrollx x
6516 else
6517 state.mstate <- Mnone
6519 | 1 when state.bzoom -> if not down then zoomblock x y
6521 | 1 ->
6522 let dest = if down then getunder x y else Unone in
6523 begin match dest with
6524 | Ulinkgoto _
6525 | Ulinkuri _
6526 | Uremote _ | Uremotedest _
6527 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6528 gotounder dest
6530 | Unone when down ->
6531 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6532 state.mstate <- Mpan (x, y);
6534 | Unone | Utext _ ->
6535 if down
6536 then (
6537 if canselect ()
6538 then (
6539 state.mstate <- Msel ((x, y), (x, y));
6540 G.postRedisplay "mouse select";
6543 else (
6544 match state.mstate with
6545 | Mnone -> ()
6547 | Mzoom _ | Mscrollx | Mscrolly ->
6548 state.mstate <- Mnone
6550 | Mzoomrect ((x0, y0), _) ->
6551 zoomrect x0 y0 x y
6553 | Mpan _ ->
6554 Wsi.setcursor Wsi.CURSOR_INHERIT;
6555 state.mstate <- Mnone
6557 | Msel ((x0, y0), (x1, y1)) ->
6558 let rec loop = function
6559 | [] -> ()
6560 | l :: rest ->
6561 let inside =
6562 let a0 = l.pagedispy in
6563 let a1 = a0 + l.pagevh in
6564 let b0 = l.pagedispx in
6565 let b1 = b0 + l.pagevw in
6566 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6567 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6569 if inside
6570 then
6571 match getopaque l.pageno with
6572 | Some opaque ->
6573 let dosel cmd () =
6574 match Ne.res Unix.pipe with
6575 | Ne.Exn exn ->
6576 showtext '!'
6577 (Printf.sprintf
6578 "can not create sel pipe: %s"
6579 (exntos exn));
6580 | Ne.Res (r, w) ->
6581 let clo what fd =
6582 Ne.clo fd (fun msg ->
6583 dolog "%s close failed: %s" what msg)
6585 let popened =
6586 try popen cmd [r, 0; w, -1]; true
6587 with exn ->
6588 dolog "can not execute %S: %s"
6589 cmd (exntos exn);
6590 false
6592 if popened
6593 then (
6594 copysel w opaque;
6595 G.postRedisplay "copysel";
6597 else clo "Msel pipe/w" w;
6598 clo "Msel pipe/r" r;
6600 dosel conf.selcmd ();
6601 state.roam <- dosel conf.paxcmd;
6602 | None -> ()
6603 else loop rest
6605 loop state.layout;
6606 Wsi.setcursor Wsi.CURSOR_INHERIT;
6607 state.mstate <- Mnone;
6611 | _ -> ()
6614 let birdseyemouse button down x y mask
6615 (conf, leftx, _, hooverpageno, anchor) =
6616 match button with
6617 | 1 when down ->
6618 let rec loop = function
6619 | [] -> ()
6620 | l :: rest ->
6621 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6622 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6623 then (
6624 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6626 else loop rest
6628 loop state.layout
6629 | 3 -> ()
6630 | _ -> viewmouse button down x y mask
6633 let uioh = object
6634 method display = ()
6636 method key key mask =
6637 begin match state.mode with
6638 | Textentry textentry -> textentrykeyboard key mask textentry
6639 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6640 | View -> viewkeyboard key mask
6641 | LinkNav linknav -> linknavkeyboard key mask linknav
6642 end;
6643 state.uioh
6645 method button button bstate x y mask =
6646 begin match state.mode with
6647 | LinkNav _
6648 | View -> viewmouse button bstate x y mask
6649 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6650 | Textentry _ -> ()
6651 end;
6652 state.uioh
6654 method multiclick clicks x y mask =
6655 begin match state.mode with
6656 | LinkNav _
6657 | View -> viewmulticlick clicks x y mask
6658 | Birdseye _
6659 | Textentry _ -> ()
6660 end;
6661 state.uioh
6663 method motion x y =
6664 begin match state.mode with
6665 | Textentry _ -> ()
6666 | View | Birdseye _ | LinkNav _ ->
6667 match state.mstate with
6668 | Mzoom _ | Mnone -> ()
6670 | Mpan (x0, y0) ->
6671 let dx = x - x0
6672 and dy = y0 - y in
6673 state.mstate <- Mpan (x, y);
6674 if canpan ()
6675 then state.x <- panbound (state.x + dx);
6676 let y = clamp dy in
6677 gotoy_and_clear_text y
6679 | Msel (a, _) ->
6680 state.mstate <- Msel (a, (x, y));
6681 G.postRedisplay "motion select";
6683 | Mscrolly ->
6684 let y = min state.winh (max 0 y) in
6685 scrolly y
6687 | Mscrollx ->
6688 let x = min state.winw (max 0 x) in
6689 scrollx x
6691 | Mzoomrect (p0, _) ->
6692 state.mstate <- Mzoomrect (p0, (x, y));
6693 G.postRedisplay "motion zoomrect";
6694 end;
6695 state.uioh
6697 method pmotion x y =
6698 begin match state.mode with
6699 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6700 let rec loop = function
6701 | [] ->
6702 if hooverpageno != -1
6703 then (
6704 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6705 G.postRedisplay "pmotion birdseye no hoover";
6707 | l :: rest ->
6708 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6709 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6710 then (
6711 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6712 G.postRedisplay "pmotion birdseye hoover";
6714 else loop rest
6716 loop state.layout
6718 | Textentry _ -> ()
6720 | LinkNav _
6721 | View ->
6722 match state.mstate with
6723 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6725 | Mnone ->
6726 updateunder x y;
6727 if canselect ()
6728 then
6729 match conf.pax with
6730 | None -> ()
6731 | Some r ->
6732 let past, _, _ = !r in
6733 let now = now () in
6734 let delta = now -. past in
6735 if delta > 0.01
6736 then paxunder x y
6737 else r := (now, x, y)
6738 end;
6739 state.uioh
6741 method infochanged _ = ()
6743 method scrollph =
6744 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6745 let p, h =
6746 if maxy = 0
6747 then 0.0, float state.winh
6748 else scrollph state.y maxy
6750 vscrollw (), p, h
6752 method scrollpw =
6753 let winw = wadjsb state.winw in
6754 let fwinw = float winw in
6755 let sw =
6756 let sw = fwinw /. float state.w in
6757 let sw = fwinw *. sw in
6758 max sw (float conf.scrollh)
6760 let position =
6761 let maxx = state.w + winw in
6762 let x = winw - state.x in
6763 let percent = float x /. float maxx in
6764 (fwinw -. sw) *. percent
6766 hscrollh (), position, sw
6768 method modehash =
6769 let modename =
6770 match state.mode with
6771 | LinkNav _ -> "links"
6772 | Textentry _ -> "textentry"
6773 | Birdseye _ -> "birdseye"
6774 | View -> "view"
6776 findkeyhash conf modename
6778 method eformsgs = true
6779 end;;
6781 module Config =
6782 struct
6783 open Parser
6785 let fontpath = ref "";;
6787 module KeyMap =
6788 Map.Make (struct type t = (int * int) let compare = compare end);;
6790 let unent s =
6791 let l = String.length s in
6792 let b = Buffer.create l in
6793 unent b s 0 l;
6794 Buffer.contents b;
6797 let home =
6798 try Sys.getenv "HOME"
6799 with exn ->
6800 prerr_endline
6801 ("Can not determine home directory location: " ^ exntos exn);
6805 let modifier_of_string = function
6806 | "alt" -> Wsi.altmask
6807 | "shift" -> Wsi.shiftmask
6808 | "ctrl" | "control" -> Wsi.ctrlmask
6809 | "meta" -> Wsi.metamask
6810 | _ -> 0
6813 let key_of_string =
6814 let r = Str.regexp "-" in
6815 fun s ->
6816 let elems = Str.full_split r s in
6817 let f n k m =
6818 let g s =
6819 let m1 = modifier_of_string s in
6820 if m1 = 0
6821 then (Wsi.namekey s, m)
6822 else (k, m lor m1)
6823 in function
6824 | Str.Delim s when n land 1 = 0 -> g s
6825 | Str.Text s -> g s
6826 | Str.Delim _ -> (k, m)
6828 let rec loop n k m = function
6829 | [] -> (k, m)
6830 | x :: xs ->
6831 let k, m = f n k m x in
6832 loop (n+1) k m xs
6834 loop 0 0 0 elems
6837 let keys_of_string =
6838 let r = Str.regexp "[ \t]" in
6839 fun s ->
6840 let elems = Str.split r s in
6841 List.map key_of_string elems
6844 let copykeyhashes c =
6845 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6848 let config_of c attrs =
6849 let apply c k v =
6851 match k with
6852 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6853 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6854 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6855 | "preload" -> { c with preload = bool_of_string v }
6856 | "page-bias" -> { c with pagebias = int_of_string v }
6857 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6858 | "horizontal-scroll-step" ->
6859 { c with hscrollstep = max (int_of_string v) 1 }
6860 | "auto-scroll-step" ->
6861 { c with autoscrollstep = max 0 (int_of_string v) }
6862 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6863 | "crop-hack" -> { c with crophack = bool_of_string v }
6864 | "throttle" ->
6865 let mw =
6866 match String.lowercase v with
6867 | "true" -> Some infinity
6868 | "false" -> None
6869 | f -> Some (float_of_string f)
6871 { c with maxwait = mw}
6872 | "highlight-links" -> { c with hlinks = bool_of_string v }
6873 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6874 | "vertical-margin" ->
6875 { c with interpagespace = max 0 (int_of_string v) }
6876 | "zoom" ->
6877 let zoom = float_of_string v /. 100. in
6878 let zoom = max zoom 0.0 in
6879 { c with zoom = zoom }
6880 | "presentation" -> { c with presentation = bool_of_string v }
6881 | "rotation-angle" -> { c with angle = int_of_string v }
6882 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6883 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6884 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6885 | "proportional-display" ->
6886 let fm =
6887 if bool_of_string v
6888 then FitProportional
6889 else FitWidth
6891 { c with fitmodel = fm }
6892 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6893 | "pixmap-cache-size" ->
6894 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6895 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6896 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6897 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6898 | "persistent-location" -> { c with jumpback = bool_of_string v }
6899 | "background-color" -> { c with bgcolor = color_of_string v }
6900 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6901 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6902 | "mupdf-store-size" ->
6903 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6904 | "checkers" -> { c with checkers = bool_of_string v }
6905 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6906 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6907 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6908 | "uri-launcher" -> { c with urilauncher = unent v }
6909 | "path-launcher" -> { c with pathlauncher = unent v }
6910 | "color-space" -> { c with colorspace = CSTE.of_string v }
6911 | "invert-colors" -> { c with invert = bool_of_string v }
6912 | "brightness" -> { c with colorscale = float_of_string v }
6913 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6914 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6915 | "columns" ->
6916 let (n, _, _) as nab = multicolumns_of_string v in
6917 if n < 0
6918 then { c with columns = Csplit (-n, [||]) }
6919 else { c with columns = Cmulti (nab, [||]) }
6920 | "birds-eye-columns" ->
6921 { c with beyecolumns = Some (max (int_of_string v) 2) }
6922 | "selection-command" -> { c with selcmd = unent v }
6923 | "synctex-command" -> { c with stcmd = unent v }
6924 | "pax-command" -> { c with paxcmd = unent v }
6925 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6926 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6927 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6928 | "use-pbo" -> { c with usepbo = bool_of_string v }
6929 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6930 | "horizontal-scrollbar-visible" ->
6931 let b =
6932 if bool_of_string v
6933 then c.scrollb lor scrollbhv
6934 else c.scrollb land (lnot scrollbhv)
6936 { c with scrollb = b }
6937 | "vertical-scrollbar-visible" ->
6938 let b =
6939 if bool_of_string v
6940 then c.scrollb lor scrollbvv
6941 else c.scrollb land (lnot scrollbvv)
6943 { c with scrollb = b }
6944 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6945 | "point-and-x" ->
6946 { c with pax =
6947 if bool_of_string v
6948 then Some (ref (0.0, 0, 0))
6949 else None }
6950 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6951 | _ -> c
6952 with exn ->
6953 prerr_endline ("Error processing attribute (`" ^
6954 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6957 let rec fold c = function
6958 | [] -> c
6959 | (k, v) :: rest ->
6960 let c = apply c k v in
6961 fold c rest
6963 fold { c with keyhashes = copykeyhashes c } attrs;
6966 let fromstring f pos n v d =
6967 try f v
6968 with exn ->
6969 dolog "Error processing attribute (%S=%S) at %d\n%s"
6970 n v pos (exntos exn)
6975 let bookmark_of attrs =
6976 let rec fold title page rely visy = function
6977 | ("title", v) :: rest -> fold v page rely visy rest
6978 | ("page", v) :: rest -> fold title v rely visy rest
6979 | ("rely", v) :: rest -> fold title page v visy rest
6980 | ("visy", v) :: rest -> fold title page rely v rest
6981 | _ :: rest -> fold title page rely visy rest
6982 | [] -> title, page, rely, visy
6984 fold "invalid" "0" "0" "0" attrs
6987 let doc_of attrs =
6988 let rec fold path page rely pan visy = function
6989 | ("path", v) :: rest -> fold v page rely pan visy rest
6990 | ("page", v) :: rest -> fold path v rely pan visy rest
6991 | ("rely", v) :: rest -> fold path page v pan visy rest
6992 | ("pan", v) :: rest -> fold path page rely v visy rest
6993 | ("visy", v) :: rest -> fold path page rely pan v rest
6994 | _ :: rest -> fold path page rely pan visy rest
6995 | [] -> path, page, rely, pan, visy
6997 fold "" "0" "0" "0" "0" attrs
7000 let map_of attrs =
7001 let rec fold rs ls = function
7002 | ("out", v) :: rest -> fold v ls rest
7003 | ("in", v) :: rest -> fold rs v rest
7004 | _ :: rest -> fold ls rs rest
7005 | [] -> ls, rs
7007 fold "" "" attrs
7010 let setconf dst src =
7011 dst.scrollbw <- src.scrollbw;
7012 dst.scrollh <- src.scrollh;
7013 dst.icase <- src.icase;
7014 dst.preload <- src.preload;
7015 dst.pagebias <- src.pagebias;
7016 dst.verbose <- src.verbose;
7017 dst.scrollstep <- src.scrollstep;
7018 dst.maxhfit <- src.maxhfit;
7019 dst.crophack <- src.crophack;
7020 dst.autoscrollstep <- src.autoscrollstep;
7021 dst.maxwait <- src.maxwait;
7022 dst.hlinks <- src.hlinks;
7023 dst.underinfo <- src.underinfo;
7024 dst.interpagespace <- src.interpagespace;
7025 dst.zoom <- src.zoom;
7026 dst.presentation <- src.presentation;
7027 dst.angle <- src.angle;
7028 dst.cwinw <- src.cwinw;
7029 dst.cwinh <- src.cwinh;
7030 dst.savebmarks <- src.savebmarks;
7031 dst.memlimit <- src.memlimit;
7032 dst.fitmodel <- src.fitmodel;
7033 dst.texcount <- src.texcount;
7034 dst.sliceheight <- src.sliceheight;
7035 dst.thumbw <- src.thumbw;
7036 dst.jumpback <- src.jumpback;
7037 dst.bgcolor <- src.bgcolor;
7038 dst.tilew <- src.tilew;
7039 dst.tileh <- src.tileh;
7040 dst.mustoresize <- src.mustoresize;
7041 dst.checkers <- src.checkers;
7042 dst.aalevel <- src.aalevel;
7043 dst.trimmargins <- src.trimmargins;
7044 dst.trimfuzz <- src.trimfuzz;
7045 dst.urilauncher <- src.urilauncher;
7046 dst.colorspace <- src.colorspace;
7047 dst.invert <- src.invert;
7048 dst.colorscale <- src.colorscale;
7049 dst.redirectstderr <- src.redirectstderr;
7050 dst.ghyllscroll <- src.ghyllscroll;
7051 dst.columns <- src.columns;
7052 dst.beyecolumns <- src.beyecolumns;
7053 dst.selcmd <- src.selcmd;
7054 dst.updatecurs <- src.updatecurs;
7055 dst.pathlauncher <- src.pathlauncher;
7056 dst.keyhashes <- copykeyhashes src;
7057 dst.hfsize <- src.hfsize;
7058 dst.hscrollstep <- src.hscrollstep;
7059 dst.pgscale <- src.pgscale;
7060 dst.usepbo <- src.usepbo;
7061 dst.wheelbypage <- src.wheelbypage;
7062 dst.stcmd <- src.stcmd;
7063 dst.paxcmd <- src.paxcmd;
7064 dst.scrollb <- src.scrollb;
7065 dst.riani <- src.riani;
7066 dst.paxmark <- src.paxmark;
7067 dst.pax <-
7068 if src.pax = None
7069 then None
7070 else Some ((ref (0.0, 0, 0)));
7073 let get s =
7074 let h = Hashtbl.create 10 in
7075 let dc = { defconf with angle = defconf.angle } in
7076 let rec toplevel v t spos _ =
7077 match t with
7078 | Vdata | Vcdata | Vend -> v
7079 | Vopen ("llppconfig", _, closed) ->
7080 if closed
7081 then v
7082 else { v with f = llppconfig }
7083 | Vopen _ ->
7084 error "unexpected subelement at top level" s spos
7085 | Vclose _ -> error "unexpected close at top level" s spos
7087 and llppconfig v t spos _ =
7088 match t with
7089 | Vdata | Vcdata -> v
7090 | Vend -> error "unexpected end of input in llppconfig" s spos
7091 | Vopen ("defaults", attrs, closed) ->
7092 let c = config_of dc attrs in
7093 setconf dc c;
7094 if closed
7095 then v
7096 else { v with f = defaults }
7098 | Vopen ("ui-font", attrs, closed) ->
7099 let rec getsize size = function
7100 | [] -> size
7101 | ("size", v) :: rest ->
7102 let size =
7103 fromstring int_of_string spos "size" v fstate.fontsize in
7104 getsize size rest
7105 | l -> getsize size l
7107 fstate.fontsize <- getsize fstate.fontsize attrs;
7108 if closed
7109 then v
7110 else { v with f = uifont (Buffer.create 10) }
7112 | Vopen ("doc", attrs, closed) ->
7113 let pathent, spage, srely, span, svisy = doc_of attrs in
7114 let path = unent pathent
7115 and pageno = fromstring int_of_string spos "page" spage 0
7116 and rely = fromstring float_of_string spos "rely" srely 0.0
7117 and pan = fromstring int_of_string spos "pan" span 0
7118 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7119 let c = config_of dc attrs in
7120 let anchor = (pageno, rely, visy) in
7121 if closed
7122 then (Hashtbl.add h path (c, [], pan, anchor); v)
7123 else { v with f = doc path pan anchor c [] }
7125 | Vopen _ ->
7126 error "unexpected subelement in llppconfig" s spos
7128 | Vclose "llppconfig" -> { v with f = toplevel }
7129 | Vclose _ -> error "unexpected close in llppconfig" s spos
7131 and defaults v t spos _ =
7132 match t with
7133 | Vdata | Vcdata -> v
7134 | Vend -> error "unexpected end of input in defaults" s spos
7135 | Vopen ("keymap", attrs, closed) ->
7136 let modename =
7137 try List.assoc "mode" attrs
7138 with Not_found -> "global" in
7139 if closed
7140 then v
7141 else
7142 let ret keymap =
7143 let h = findkeyhash dc modename in
7144 KeyMap.iter (Hashtbl.replace h) keymap;
7145 defaults
7147 { v with f = pkeymap ret KeyMap.empty }
7149 | Vopen (_, _, _) ->
7150 error "unexpected subelement in defaults" s spos
7152 | Vclose "defaults" ->
7153 { v with f = llppconfig }
7155 | Vclose _ -> error "unexpected close in defaults" s spos
7157 and uifont b v t spos epos =
7158 match t with
7159 | Vdata | Vcdata ->
7160 Buffer.add_substring b s spos (epos - spos);
7162 | Vopen (_, _, _) ->
7163 error "unexpected subelement in ui-font" s spos
7164 | Vclose "ui-font" ->
7165 if emptystr !fontpath
7166 then fontpath := Buffer.contents b;
7167 { v with f = llppconfig }
7168 | Vclose _ -> error "unexpected close in ui-font" s spos
7169 | Vend -> error "unexpected end of input in ui-font" s spos
7171 and doc path pan anchor c bookmarks v t spos _ =
7172 match t with
7173 | Vdata | Vcdata -> v
7174 | Vend -> error "unexpected end of input in doc" s spos
7175 | Vopen ("bookmarks", _, closed) ->
7176 if closed
7177 then v
7178 else { v with f = pbookmarks path pan anchor c bookmarks }
7180 | Vopen ("keymap", attrs, closed) ->
7181 let modename =
7182 try List.assoc "mode" attrs
7183 with Not_found -> "global"
7185 if closed
7186 then v
7187 else
7188 let ret keymap =
7189 let h = findkeyhash c modename in
7190 KeyMap.iter (Hashtbl.replace h) keymap;
7191 doc path pan anchor c bookmarks
7193 { v with f = pkeymap ret KeyMap.empty }
7195 | Vopen (_, _, _) ->
7196 error "unexpected subelement in doc" s spos
7198 | Vclose "doc" ->
7199 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7200 { v with f = llppconfig }
7202 | Vclose _ -> error "unexpected close in doc" s spos
7204 and pkeymap ret keymap v t spos _ =
7205 match t with
7206 | Vdata | Vcdata -> v
7207 | Vend -> error "unexpected end of input in keymap" s spos
7208 | Vopen ("map", attrs, closed) ->
7209 let r, l = map_of attrs in
7210 let kss = fromstring keys_of_string spos "in" r [] in
7211 let lss = fromstring keys_of_string spos "out" l [] in
7212 let keymap =
7213 match kss with
7214 | [] -> keymap
7215 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7216 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7218 if closed
7219 then { v with f = pkeymap ret keymap }
7220 else
7221 let f () = v in
7222 { v with f = skip "map" f }
7224 | Vopen _ ->
7225 error "unexpected subelement in keymap" s spos
7227 | Vclose "keymap" ->
7228 { v with f = ret keymap }
7230 | Vclose _ -> error "unexpected close in keymap" s spos
7232 and pbookmarks path pan anchor c bookmarks v t spos _ =
7233 match t with
7234 | Vdata | Vcdata -> v
7235 | Vend -> error "unexpected end of input in bookmarks" s spos
7236 | Vopen ("item", attrs, closed) ->
7237 let titleent, spage, srely, svisy = bookmark_of attrs in
7238 let page = fromstring int_of_string spos "page" spage 0
7239 and rely = fromstring float_of_string spos "rely" srely 0.0
7240 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7241 let bookmarks =
7242 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
7244 if closed
7245 then { v with f = pbookmarks path pan anchor c bookmarks }
7246 else
7247 let f () = v in
7248 { v with f = skip "item" f }
7250 | Vopen _ ->
7251 error "unexpected subelement in bookmarks" s spos
7253 | Vclose "bookmarks" ->
7254 { v with f = doc path pan anchor c bookmarks }
7256 | Vclose _ -> error "unexpected close in bookmarks" s spos
7258 and skip tag f v t spos _ =
7259 match t with
7260 | Vdata | Vcdata -> v
7261 | Vend ->
7262 error ("unexpected end of input in skipped " ^ tag) s spos
7263 | Vopen (tag', _, closed) ->
7264 if closed
7265 then v
7266 else
7267 let f' () = { v with f = skip tag f } in
7268 { v with f = skip tag' f' }
7269 | Vclose ctag ->
7270 if tag = ctag
7271 then f ()
7272 else error ("unexpected close in skipped " ^ tag) s spos
7275 parse { f = toplevel; accu = () } s;
7276 h, dc;
7279 let do_load f ic =
7281 let len = in_channel_length ic in
7282 let s = String.create len in
7283 really_input ic s 0 len;
7284 f s;
7285 with
7286 | Parse_error (msg, s, pos) ->
7287 let subs = subs s pos in
7288 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7290 | exn ->
7291 failwith ("config load error: " ^ exntos exn)
7294 let defconfpath =
7295 let dir =
7297 let dir = Filename.concat home ".config" in
7298 if Sys.is_directory dir then dir else home
7299 with _ -> home
7301 Filename.concat dir "llpp.conf"
7304 let confpath = ref defconfpath;;
7306 let load1 f =
7307 if Sys.file_exists !confpath
7308 then
7309 match
7310 (try Some (open_in_bin !confpath)
7311 with exn ->
7312 prerr_endline
7313 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7314 exntos exn);
7315 None
7317 with
7318 | Some ic ->
7319 let success =
7321 f (do_load get ic)
7322 with exn ->
7323 prerr_endline
7324 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7325 exntos exn);
7326 false
7328 close_in ic;
7329 success
7331 | None -> false
7332 else
7333 f (Hashtbl.create 0, defconf)
7336 let load () =
7337 let f (h, dc) =
7338 let pc, pb, px, pa =
7340 let key =
7341 if emptystr state.origin
7342 then state.path
7343 else state.origin
7345 Hashtbl.find h (Filename.basename key)
7346 with Not_found -> dc, [], 0, emptyanchor
7348 setconf defconf dc;
7349 setconf conf pc;
7350 state.bookmarks <- pb;
7351 state.x <- px;
7352 if conf.jumpback
7353 then state.anchor <- pa;
7354 cbput state.hists.nav pa;
7355 true
7357 load1 f
7360 let add_attrs bb always dc c =
7361 let ob s a b =
7362 if always || a != b
7363 then Printf.bprintf bb "\n %s='%b'" s a
7364 and op s a b =
7365 if always || a <> b
7366 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7367 and oi s a b =
7368 if always || a != b
7369 then Printf.bprintf bb "\n %s='%d'" s a
7370 and oI s a b =
7371 if always || a != b
7372 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7373 and oz s a b =
7374 if always || a <> b
7375 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7376 and oF s a b =
7377 if always || a <> b
7378 then Printf.bprintf bb "\n %s='%f'" s a
7379 and oc s a b =
7380 if always || a <> b
7381 then
7382 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7383 and oC s a b =
7384 if always || a <> b
7385 then
7386 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7387 and oR s a b =
7388 if always || a <> b
7389 then
7390 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7391 and os s a b =
7392 if always || a <> b
7393 then
7394 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7395 and og s a b =
7396 if always || a <> b
7397 then
7398 match a with
7399 | Some (_N, _A, _B) ->
7400 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7401 | None ->
7402 match b with
7403 | None -> ()
7404 | _ ->
7405 Printf.bprintf bb "\n %s='none'" s
7406 and oW s a b =
7407 if always || a <> b
7408 then
7409 let v =
7410 match a with
7411 | None -> "false"
7412 | Some f ->
7413 if f = infinity
7414 then "true"
7415 else string_of_float f
7417 Printf.bprintf bb "\n %s='%s'" s v
7418 and oco s a b =
7419 if always || a <> b
7420 then
7421 match a with
7422 | Cmulti ((n, a, b), _) when n > 1 ->
7423 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7424 | Csplit (n, _) when n > 1 ->
7425 Printf.bprintf bb "\n %s='%d'" s ~-n
7426 | _ -> ()
7427 and obeco s a b =
7428 if always || a <> b
7429 then
7430 match a with
7431 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7432 | _ -> ()
7433 and oFm s a b =
7434 if always || a <> b
7435 then
7436 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7437 and oSv s a b m =
7438 if always || a <> b
7439 then
7440 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7441 and oPm s a b =
7442 if always || a <> b
7443 then
7444 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7446 oi "width" c.cwinw dc.cwinw;
7447 oi "height" c.cwinh dc.cwinh;
7448 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7449 oi "scroll-handle-height" c.scrollh dc.scrollh;
7450 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7451 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7452 ob "case-insensitive-search" c.icase dc.icase;
7453 ob "preload" c.preload dc.preload;
7454 oi "page-bias" c.pagebias dc.pagebias;
7455 oi "scroll-step" c.scrollstep dc.scrollstep;
7456 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7457 ob "max-height-fit" c.maxhfit dc.maxhfit;
7458 ob "crop-hack" c.crophack dc.crophack;
7459 oW "throttle" c.maxwait dc.maxwait;
7460 ob "highlight-links" c.hlinks dc.hlinks;
7461 ob "under-cursor-info" c.underinfo dc.underinfo;
7462 oi "vertical-margin" c.interpagespace dc.interpagespace;
7463 oz "zoom" c.zoom dc.zoom;
7464 ob "presentation" c.presentation dc.presentation;
7465 oi "rotation-angle" c.angle dc.angle;
7466 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7467 oFm "fit-model" c.fitmodel dc.fitmodel;
7468 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7469 oi "tex-count" c.texcount dc.texcount;
7470 oi "slice-height" c.sliceheight dc.sliceheight;
7471 oi "thumbnail-width" c.thumbw dc.thumbw;
7472 ob "persistent-location" c.jumpback dc.jumpback;
7473 oc "background-color" c.bgcolor dc.bgcolor;
7474 oi "tile-width" c.tilew dc.tilew;
7475 oi "tile-height" c.tileh dc.tileh;
7476 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7477 ob "checkers" c.checkers dc.checkers;
7478 oi "aalevel" c.aalevel dc.aalevel;
7479 ob "trim-margins" c.trimmargins dc.trimmargins;
7480 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7481 os "uri-launcher" c.urilauncher dc.urilauncher;
7482 os "path-launcher" c.pathlauncher dc.pathlauncher;
7483 oC "color-space" c.colorspace dc.colorspace;
7484 ob "invert-colors" c.invert dc.invert;
7485 oF "brightness" c.colorscale dc.colorscale;
7486 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7487 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7488 oco "columns" c.columns dc.columns;
7489 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7490 os "selection-command" c.selcmd dc.selcmd;
7491 os "synctex-command" c.stcmd dc.stcmd;
7492 os "pax-command" c.paxcmd dc.paxcmd;
7493 ob "update-cursor" c.updatecurs dc.updatecurs;
7494 oi "hint-font-size" c.hfsize dc.hfsize;
7495 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7496 oF "page-scroll-scale" c.pgscale dc.pgscale;
7497 ob "use-pbo" c.usepbo dc.usepbo;
7498 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7499 ob "remote-in-a-new-instance" c.riani dc.riani;
7500 op "point-and-x" c.pax dc.pax;
7501 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7504 let keymapsbuf always dc c =
7505 let bb = Buffer.create 16 in
7506 let rec loop = function
7507 | [] -> ()
7508 | (modename, h) :: rest ->
7509 let dh = findkeyhash dc modename in
7510 if always || h <> dh
7511 then (
7512 if Hashtbl.length h > 0
7513 then (
7514 if Buffer.length bb > 0
7515 then Buffer.add_char bb '\n';
7516 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7517 Hashtbl.iter (fun i o ->
7518 let isdifferent = always ||
7520 let dO = Hashtbl.find dh i in
7521 dO <> o
7522 with Not_found -> true
7524 if isdifferent
7525 then
7526 let addkm (k, m) =
7527 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7528 if Wsi.withalt m then Buffer.add_string bb "alt-";
7529 if Wsi.withshift m then Buffer.add_string bb "shift-";
7530 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7531 Buffer.add_string bb (Wsi.keyname k);
7533 let addkms l =
7534 let rec loop = function
7535 | [] -> ()
7536 | km :: [] -> addkm km
7537 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7539 loop l
7541 Buffer.add_string bb "<map in='";
7542 addkm i;
7543 match o with
7544 | KMinsrt km ->
7545 Buffer.add_string bb "' out='";
7546 addkm km;
7547 Buffer.add_string bb "'/>\n"
7549 | KMinsrl kms ->
7550 Buffer.add_string bb "' out='";
7551 addkms kms;
7552 Buffer.add_string bb "'/>\n"
7554 | KMmulti (ins, kms) ->
7555 Buffer.add_char bb ' ';
7556 addkms ins;
7557 Buffer.add_string bb "' out='";
7558 addkms kms;
7559 Buffer.add_string bb "'/>\n"
7560 ) h;
7561 Buffer.add_string bb "</keymap>";
7564 loop rest
7566 loop c.keyhashes;
7570 let save () =
7571 let uifontsize = fstate.fontsize in
7572 let bb = Buffer.create 32768 in
7573 let relx = float state.x /. float state.winw in
7574 let w, h, x =
7575 let cx w = truncate (relx *. float w) in
7576 List.fold_left
7577 (fun (w, h, x) ws ->
7578 match ws with
7579 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7580 | Wsi.MaxVert -> (w, conf.cwinh, x)
7581 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7583 (state.winw, state.winh, state.x) state.winstate
7585 conf.cwinw <- w;
7586 conf.cwinh <- h;
7587 let f (h, dc) =
7588 let dc = if conf.bedefault then conf else dc in
7589 Buffer.add_string bb "<llppconfig>\n";
7591 if nonemptystr !fontpath
7592 then
7593 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7594 uifontsize
7595 !fontpath
7596 else (
7597 if uifontsize <> 14
7598 then
7599 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7602 Buffer.add_string bb "<defaults";
7603 add_attrs bb true dc dc;
7604 let kb = keymapsbuf true dc dc in
7605 if Buffer.length kb > 0
7606 then (
7607 Buffer.add_string bb ">\n";
7608 Buffer.add_buffer bb kb;
7609 Buffer.add_string bb "\n</defaults>\n";
7611 else Buffer.add_string bb "/>\n";
7613 let adddoc path pan anchor c bookmarks =
7614 if bookmarks == [] && c = dc && anchor = emptyanchor
7615 then ()
7616 else (
7617 Printf.bprintf bb "<doc path='%s'"
7618 (enent path 0 (String.length path));
7620 if anchor <> emptyanchor
7621 then (
7622 let n, rely, visy = anchor in
7623 Printf.bprintf bb " page='%d'" n;
7624 if rely > 1e-6
7625 then
7626 Printf.bprintf bb " rely='%f'" rely
7628 if abs_float visy > 1e-6
7629 then
7630 Printf.bprintf bb " visy='%f'" visy
7634 if pan != 0
7635 then Printf.bprintf bb " pan='%d'" pan;
7637 add_attrs bb false dc c;
7638 let kb = keymapsbuf false dc c in
7640 begin match bookmarks with
7641 | [] ->
7642 if Buffer.length kb > 0
7643 then (
7644 Buffer.add_string bb ">\n";
7645 Buffer.add_buffer bb kb;
7646 Buffer.add_string bb "\n</doc>\n";
7648 else Buffer.add_string bb "/>\n"
7649 | _ ->
7650 Buffer.add_string bb ">\n<bookmarks>\n";
7651 List.iter (fun (title, _, kind) ->
7652 begin match kind with
7653 | Oanchor (page, rely, visy) ->
7654 Printf.bprintf bb
7655 "<item title='%s' page='%d'"
7656 (enent title 0 (String.length title))
7657 page
7659 if rely > 1e-6
7660 then
7661 Printf.bprintf bb " rely='%f'" rely
7663 if abs_float visy > 1e-6
7664 then
7665 Printf.bprintf bb " visy='%f'" visy
7667 | Onone | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
7668 failwith "unexpected link in bookmarks"
7669 end;
7670 Buffer.add_string bb "/>\n";
7671 ) bookmarks;
7672 Buffer.add_string bb "</bookmarks>";
7673 if Buffer.length kb > 0
7674 then (
7675 Buffer.add_string bb "\n";
7676 Buffer.add_buffer bb kb;
7678 Buffer.add_string bb "\n</doc>\n";
7679 end;
7683 let pan, conf =
7684 match state.mode with
7685 | Birdseye (c, pan, _, _, _) ->
7686 let beyecolumns =
7687 match conf.columns with
7688 | Cmulti ((c, _, _), _) -> Some c
7689 | Csingle _ -> None
7690 | Csplit _ -> None
7691 and columns =
7692 match c.columns with
7693 | Cmulti (c, _) -> Cmulti (c, [||])
7694 | Csingle _ -> Csingle [||]
7695 | Csplit _ -> failwith "quit from bird's eye while split"
7697 pan, { c with beyecolumns = beyecolumns; columns = columns }
7698 | _ -> x, conf
7700 let basename = Filename.basename
7701 (if emptystr state.origin then state.path else state.origin)
7703 adddoc basename pan (getanchor ())
7704 (let autoscrollstep =
7705 match state.autoscroll with
7706 | Some step -> step
7707 | None -> conf.autoscrollstep
7708 in begin match state.mode with
7709 | Birdseye beye -> leavebirdseye beye true;
7710 | _ -> ()
7711 end;
7712 { conf with autoscrollstep = autoscrollstep })
7713 (if conf.savebmarks then state.bookmarks else []);
7715 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7716 if basename <> path
7717 then adddoc path x anchor c bookmarks
7718 ) h;
7719 Buffer.add_string bb "</llppconfig>\n";
7720 true;
7722 if load1 f && Buffer.length bb > 0
7723 then
7725 let tmp = !confpath ^ ".tmp" in
7726 let oc = open_out_bin tmp in
7727 Buffer.output_buffer oc bb;
7728 close_out oc;
7729 Unix.rename tmp !confpath;
7730 with exn ->
7731 prerr_endline
7732 ("error while saving configuration: " ^ exntos exn)
7734 end;;
7736 let adderrmsg src msg =
7737 Buffer.add_string state.errmsgs msg;
7738 state.newerrmsgs <- true;
7739 G.postRedisplay src
7742 let adderrfmt src fmt =
7743 Format.kprintf (fun s -> adderrmsg src s) fmt;
7746 let ract cmds =
7747 let cl = splitatspace cmds in
7748 let scan s fmt f =
7749 try Scanf.sscanf s fmt f
7750 with exn ->
7751 adderrfmt "remote exec"
7752 "error processing '%S': %s\n" cmds (exntos exn)
7754 match cl with
7755 | "reload" :: [] -> reload ()
7756 | "goto" :: args :: [] ->
7757 scan args "%u %f %f"
7758 (fun pageno x y ->
7759 let cmd, _ = state.geomcmds in
7760 if emptystr cmd
7761 then gotopagexy pageno x y
7762 else
7763 let f prevf () =
7764 gotopagexy pageno x y;
7765 prevf ()
7767 state.reprf <- f state.reprf
7769 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7770 | "gotor" :: args :: [] ->
7771 scan args "%S %u"
7772 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
7773 | "gotord" :: args :: [] ->
7774 scan args "%S %S"
7775 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
7776 | "rect" :: args :: [] ->
7777 scan args "%u %u %f %f %f %f"
7778 (fun pageno color x0 y0 x1 y1 ->
7779 onpagerect pageno (fun w h ->
7780 let _,w1,h1,_ = getpagedim pageno in
7781 let sw = float w1 /. float w
7782 and sh = float h1 /. float h in
7783 let x0s = x0 *. sw
7784 and x1s = x1 *. sw
7785 and y0s = y0 *. sh
7786 and y1s = y1 *. sh in
7787 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7788 debugrect rect;
7789 state.rects <- (pageno, color, rect) :: state.rects;
7790 G.postRedisplay "rect";
7793 | "activatewin" :: [] -> Wsi.activatewin ()
7794 | "quit" :: [] -> raise Quit
7795 | _ ->
7796 adderrfmt "remote command"
7797 "error processing remote command: %S\n" cmds;
7800 let remote =
7801 let scratch = String.create 80 in
7802 let buf = Buffer.create 80 in
7803 fun fd ->
7804 let rec tempfr () =
7805 try Some (Unix.read fd scratch 0 80)
7806 with
7807 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7808 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7809 | exn -> raise exn
7811 match tempfr () with
7812 | None -> Some fd
7813 | Some n ->
7814 if n = 0
7815 then (
7816 Unix.close fd;
7817 if Buffer.length buf > 0
7818 then (
7819 let s = Buffer.contents buf in
7820 Buffer.clear buf;
7821 ract s;
7823 None
7825 else
7826 let rec eat ppos =
7827 let nlpos =
7829 let pos = String.index_from scratch ppos '\n' in
7830 if pos >= n then -1 else pos
7831 with Not_found -> -1
7833 if nlpos >= 0
7834 then (
7835 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7836 let s = Buffer.contents buf in
7837 Buffer.clear buf;
7838 ract s;
7839 eat (nlpos+1);
7841 else (
7842 Buffer.add_substring buf scratch ppos (n-ppos);
7843 Some fd
7845 in eat 0
7848 let remoteopen path =
7849 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7850 with exn ->
7851 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7852 None
7855 let () =
7856 let trimcachepath = ref "" in
7857 let rcmdpath = ref "" in
7858 let pageno = ref None in
7859 selfexec := Sys.executable_name;
7860 Arg.parse
7861 (Arg.align
7862 [("-p", Arg.String (fun s -> state.password <- s),
7863 "<password> Set password");
7865 ("-f", Arg.String
7866 (fun s ->
7867 Config.fontpath := s;
7868 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7870 "<path> Set path to the user interface font");
7872 ("-c", Arg.String
7873 (fun s ->
7874 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7875 Config.confpath := s),
7876 "<path> Set path to the configuration file");
7878 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
7879 "<page-number> Jump to page");
7881 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7882 "<path> Set path to the trim cache file");
7884 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7885 "<named-destination> Set named destination");
7887 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7888 ("-cxack", Arg.Set cxack, " Cut corners");
7890 ("-remote", Arg.String (fun s -> rcmdpath := s),
7891 "<path> Set path to the remote commands source");
7893 ("-origin", Arg.String (fun s -> state.origin <- s),
7894 "<original-path> Set original path");
7896 ("-v", Arg.Unit (fun () ->
7897 Printf.printf
7898 "%s\nconfiguration path: %s\n"
7899 (version ())
7900 Config.defconfpath
7902 exit 0), " Print version and exit");
7905 (fun s -> state.path <- s)
7906 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7908 if !wtmode
7909 then selfexec := !selfexec ^ " -wtmode";
7911 if emptystr state.path
7912 then (prerr_endline "file name missing"; exit 1);
7914 if not (Config.load ())
7915 then prerr_endline "failed to load configuration";
7916 begin match !pageno with
7917 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
7918 | None -> ()
7919 end;
7921 let wsfd, winw, winh = Wsi.init (object (self)
7922 val mutable m_hack = false
7923 val mutable m_clicks = 0
7924 val mutable m_click_x = 0
7925 val mutable m_click_y = 0
7926 val mutable m_lastclicktime = infinity
7928 method private cleanup =
7929 state.roam <- noroam;
7930 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
7931 method expose = if not m_hack then G.postRedisplay "expose"
7932 method visible = G.postRedisplay "visible"
7933 method display = m_hack <- false; display ()
7934 method reshape w h =
7935 self#cleanup;
7936 m_hack <- w < state.winw && h < state.winh;
7937 reshape w h
7938 method mouse b d x y m =
7939 if d && canselect ()
7940 then (
7941 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
7942 m_click_x <- x;
7943 m_click_y <- y;
7944 if b = 1
7945 then (
7946 let t = now () in
7947 if abs x - m_click_x > 10
7948 || abs y - m_click_y > 10
7949 || abs_float (t -. m_lastclicktime) > 0.3
7950 then m_clicks <- 0;
7951 m_clicks <- m_clicks + 1;
7952 m_lastclicktime <- t;
7953 if m_clicks = 1
7954 then (
7955 self#cleanup;
7956 G.postRedisplay "cleanup";
7957 state.uioh <- state.uioh#button b d x y m;
7959 else state.uioh <- state.uioh#multiclick m_clicks x y m
7961 else (
7962 self#cleanup;
7963 m_clicks <- 0;
7964 m_lastclicktime <- infinity;
7965 state.uioh <- state.uioh#button b d x y m
7968 else (
7969 state.uioh <- state.uioh#button b d x y m
7971 method motion x y =
7972 state.mpos <- (x, y);
7973 state.uioh <- state.uioh#motion x y
7974 method pmotion x y =
7975 state.mpos <- (x, y);
7976 state.uioh <- state.uioh#pmotion x y
7977 method key k m =
7978 let mascm = m land (
7979 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7980 ) in
7981 let keyboard k m =
7982 let x = state.x and y = state.y in
7983 keyboard k m;
7984 if x != state.x || y != state.y then self#cleanup
7986 match state.keystate with
7987 | KSnone ->
7988 let km = k, mascm in
7989 begin
7990 match
7991 let modehash = state.uioh#modehash in
7992 try Hashtbl.find modehash km
7993 with Not_found ->
7994 try Hashtbl.find (findkeyhash conf "global") km
7995 with Not_found -> KMinsrt (k, m)
7996 with
7997 | KMinsrt (k, m) -> keyboard k m
7998 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7999 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
8001 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
8002 List.iter (fun (k, m) -> keyboard k m) insrt;
8003 state.keystate <- KSnone
8004 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
8005 state.keystate <- KSinto (keys, insrt)
8006 | _ ->
8007 state.keystate <- KSnone
8009 method enter x y =
8010 state.mpos <- (x, y);
8011 state.uioh <- state.uioh#pmotion x y
8012 method leave = state.mpos <- (-1, -1)
8013 method winstate wsl = state.winstate <- wsl; m_hack <- false
8014 method quit = raise Quit
8015 end) conf.cwinw conf.cwinh (platform = Posx) in
8017 state.wsfd <- wsfd;
8019 if not (
8020 List.exists GlMisc.check_extension
8021 [ "GL_ARB_texture_rectangle"
8022 ; "GL_EXT_texture_recangle"
8023 ; "GL_NV_texture_rectangle" ]
8025 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
8027 if (
8028 let r = GlMisc.get_string `renderer in
8029 let p = "Mesa DRI Intel(" in
8030 let l = String.length p in
8031 String.length r > l && String.sub r 0 l = p
8033 then (
8034 defconf.sliceheight <- 1024;
8035 defconf.texcount <- 32;
8036 defconf.usepbo <- true;
8039 let cr, sw =
8040 match Ne.res Unix.pipe with
8041 | Ne.Exn exn ->
8042 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
8043 exit 1
8044 | Ne.Res rw -> rw
8045 and sr, cw =
8046 match Ne.res Unix.pipe with
8047 | Ne.Exn exn ->
8048 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
8049 exit 1
8050 | Ne.Res rw -> rw
8053 cloexec cr;
8054 cloexec sw;
8055 cloexec sr;
8056 cloexec cw;
8058 setcheckers conf.checkers;
8059 redirectstderr ();
8060 if conf.redirectstderr
8061 then
8062 at_exit (fun () ->
8063 let s = Buffer.contents state.errmsgs ^
8064 (match state.errfd with
8065 | Some fd ->
8066 let s = String.create (80*24) in
8067 let n =
8069 let r, _, _ = Unix.select [fd] [] [] 0.0 in
8070 if List.mem fd r
8071 then Unix.read fd s 0 (String.length s)
8072 else 0
8073 with _ -> 0
8075 if n = 0
8076 then ""
8077 else String.sub s 0 n
8078 | None -> ""
8081 try ignore (Unix.write state.stderr s 0 (String.length s))
8082 with exn -> print_endline (exntos exn)
8086 init (cr, cw) (
8087 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
8088 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
8089 !Config.fontpath, !trimcachepath,
8090 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
8092 List.iter GlArray.enable [`texture_coord; `vertex];
8093 state.sr <- sr;
8094 state.sw <- sw;
8095 state.text <- "Opening " ^ (mbtoutf8 state.path);
8096 reshape winw winh;
8097 opendoc state.path state.password;
8098 state.uioh <- uioh;
8099 display ();
8100 Wsi.mapwin ();
8101 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
8102 let optrfd =
8103 ref (
8104 if nonemptystr !rcmdpath
8105 then remoteopen !rcmdpath
8106 else None
8110 let rec loop deadline =
8111 let r =
8112 match state.errfd with
8113 | None -> [state.sr; state.wsfd]
8114 | Some fd -> [state.sr; state.wsfd; fd]
8116 let r =
8117 match !optrfd with
8118 | None -> r
8119 | Some fd -> fd :: r
8121 if state.redisplay
8122 then (
8123 state.redisplay <- false;
8124 display ();
8126 let timeout =
8127 let now = now () in
8128 if deadline > now
8129 then (
8130 if deadline = infinity
8131 then ~-.1.0
8132 else max 0.0 (deadline -. now)
8134 else 0.0
8136 let r, _, _ =
8137 try Unix.select r [] [] timeout
8138 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
8140 begin match r with
8141 | [] ->
8142 state.ghyll None;
8143 let newdeadline =
8144 if state.ghyll == noghyll
8145 then
8146 match state.autoscroll with
8147 | Some step when step != 0 ->
8148 let y = state.y + step in
8149 let y =
8150 if y < 0
8151 then state.maxy
8152 else if y >= state.maxy then 0 else y
8154 gotoy y;
8155 if state.mode = View
8156 then state.text <- "";
8157 deadline +. 0.01
8158 | _ -> infinity
8159 else deadline +. 0.01
8161 loop newdeadline
8163 | l ->
8164 let rec checkfds = function
8165 | [] -> ()
8166 | fd :: rest when fd = state.sr ->
8167 let cmd = readcmd state.sr in
8168 act cmd;
8169 checkfds rest
8171 | fd :: rest when fd = state.wsfd ->
8172 Wsi.readresp fd;
8173 checkfds rest
8175 | fd :: rest when Some fd = !optrfd ->
8176 begin match remote fd with
8177 | None -> optrfd := remoteopen !rcmdpath;
8178 | opt -> optrfd := opt
8179 end;
8180 checkfds rest
8182 | fd :: rest ->
8183 let s = String.create 80 in
8184 let n = tempfailureretry (Unix.read fd s 0) 80 in
8185 if conf.redirectstderr
8186 then (
8187 Buffer.add_substring state.errmsgs s 0 n;
8188 state.newerrmsgs <- true;
8189 state.redisplay <- true;
8191 else (
8192 prerr_string (String.sub s 0 n);
8193 flush stderr;
8195 checkfds rest
8197 checkfds l;
8198 let newdeadline =
8199 let deadline1 =
8200 if deadline = infinity
8201 then now () +. 0.01
8202 else deadline
8204 match state.autoscroll with
8205 | Some step when step != 0 -> deadline1
8206 | _ -> if state.ghyll == noghyll then infinity else deadline1
8208 loop newdeadline
8209 end;
8212 loop infinity;
8213 with Quit ->
8214 Config.save ();