Deal with documents with empty mediabox and non empty cropbox
[llpp.git] / main.ml
blob1374dd52240a97f0b357a89e431293a4ad7074f1
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 type params = (angle * fitmodel * trimparams
29 * texcount * sliceheight * memsize
30 * colorspace * fontpath * trimcachepath
31 * haspbo)
32 and width = int
33 and height = int
34 and leftx = int
35 and opaque = string
36 and recttype = int
37 and pixmapsize = int
38 and angle = int
39 and trimmargins = bool
40 and interpagespace = int
41 and texcount = int
42 and sliceheight = int
43 and gen = int
44 and top = float
45 and dtop = float
46 and fontpath = string
47 and trimcachepath = string
48 and memsize = int
49 and aalevel = int
50 and irect = (int * int * int * int)
51 and trimparams = (trimmargins * irect)
52 and colorspace = | Rgb | Bgr | Gray
53 and fitmodel = | FitWidth | FitProportional | FitPage
54 and haspbo = bool
55 and uri = string
56 and caption = string
59 type x = int
60 and y = int
61 and tilex = int
62 and tiley = int
63 and tileparams = (x * y * width * height * tilex * tiley)
66 type link =
67 | Lnotfound
68 | Lfound of int
69 and linkdir =
70 | LDfirst
71 | LDlast
72 | LDfirstvisible of (int * int * int)
73 | LDleft of int
74 | LDright of int
75 | LDdown of int
76 | LDup of int
79 type pagewithlinks =
80 | Pwlnotfound
81 | Pwl of int
84 type keymap =
85 | KMinsrt of key
86 | KMinsrl of key list
87 | KMmulti of key list * key list
88 and key = int * int
89 and keyhash = (key, keymap) Hashtbl.t
90 and keystate =
91 | KSnone
92 | KSinto of (key list * key list)
95 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
96 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
98 type pipe = (Unix.file_descr * Unix.file_descr);;
100 external init : pipe -> params -> unit = "ml_init";;
101 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
102 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
103 external getpdimrect : int -> float array = "ml_getpdimrect";;
104 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
105 external markunder : string -> int -> int -> mark -> bool = "ml_markunder";;
106 external clearmark : string -> unit = "ml_clearmark";;
107 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
108 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
109 external measurestr : int -> string -> float = "ml_measure_string";;
110 external postprocess :
111 opaque -> int -> int -> int -> (int * string * int) -> int
112 = "ml_postprocess";;
113 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
114 external platform : unit -> platform = "ml_platform";;
115 external setaalevel : int -> unit = "ml_setaalevel";;
116 external realloctexts : int -> bool = "ml_realloctexts";;
117 external findlink : opaque -> linkdir -> link = "ml_findlink";;
118 external getlink : opaque -> int -> under = "ml_getlink";;
119 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
120 external getlinkcount : opaque -> int = "ml_getlinkcount";;
121 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
122 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
123 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
124 external freepbo : string -> unit = "ml_freepbo";;
125 external unmappbo : string -> unit = "ml_unmappbo";;
126 external pbousable : unit -> bool = "ml_pbo_usable";;
127 external unproject : opaque -> int -> int -> (int * int) option
128 = "ml_unproject";;
129 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
130 external rectofblock : opaque -> int -> int -> float array option
131 = "ml_rectofblock";;
132 external fz_version : unit -> string = "ml_fz_version";;
133 external begintiles : unit -> unit = "ml_begintiles";;
134 external endtiles : unit -> unit = "ml_endtiles";;
136 let platform_to_string = function
137 | Punknown -> "unknown"
138 | Plinux -> "Linux"
139 | Posx -> "OSX"
140 | Psun -> "Sun"
141 | Pfreebsd -> "FreeBSD"
142 | Pdragonflybsd -> "DragonflyBSD"
143 | Popenbsd -> "OpenBSD"
144 | Pnetbsd -> "NetBSD"
145 | Pcygwin -> "Cygwin"
148 let platform = platform ();;
150 let now = Unix.gettimeofday;;
152 let selfexec = ref "";;
154 let popen cmd fda =
155 if platform = Pcygwin
156 then (
157 let sh = "/bin/sh" in
158 let args = [|sh; "-c"; cmd|] in
159 let rec std si so se = function
160 | [] -> si, so, se
161 | (fd, 0) :: rest -> std fd so se rest
162 | (fd, -1) :: rest ->
163 Unix.close fd;
164 std si so se rest
165 | (_, n) :: _ ->
166 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
168 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
169 ignore (Unix.create_process sh args si so se)
171 else popen cmd fda;
174 type mpos = int * int
175 and mstate =
176 | Msel of (mpos * mpos)
177 | Mpan of mpos
178 | Mscrolly | Mscrollx
179 | Mzoom of (int * int)
180 | Mzoomrect of (mpos * mpos)
181 | Mnone
184 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
185 and onkey = string -> int -> te
186 and ondone = string -> unit
187 and histcancel = unit -> unit
188 and onhist = ((histcmd -> string) * histcancel)
189 and histcmd = HCnext | HCprev | HCfirst | HClast
190 and cancelonempty = bool
191 and te =
192 | TEstop
193 | TEdone of string
194 | TEcont of string
195 | TEswitch of textentry
198 type 'a circbuf =
199 { store : 'a array
200 ; mutable rc : int
201 ; mutable wc : int
202 ; mutable len : int
206 let bound v minv maxv =
207 max minv (min maxv v);
210 let cbnew n v =
211 { store = Array.create n v
212 ; rc = 0
213 ; wc = 0
214 ; len = 0
218 let cbcap b = Array.length b.store;;
220 let cbput b v =
221 let cap = cbcap b in
222 b.store.(b.wc) <- v;
223 b.wc <- (b.wc + 1) mod cap;
224 b.rc <- b.wc;
225 b.len <- min (b.len + 1) cap;
228 let cbempty b = b.len = 0;;
230 let cbgetg b circular dir =
231 if cbempty b
232 then b.store.(0)
233 else
234 let rc = b.rc + dir in
235 let rc =
236 if circular
237 then (
238 if rc = -1
239 then b.len-1
240 else (
241 if rc >= b.len
242 then 0
243 else rc
246 else bound rc 0 (b.len-1)
248 b.rc <- rc;
249 b.store.(rc);
252 let cbget b = cbgetg b false;;
253 let cbgetc b = cbgetg b true;;
255 let drawstring size x y s =
256 Gl.enable `blend;
257 Gl.enable `texture_2d;
258 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
259 ignore (drawstr size x y s);
260 Gl.disable `blend;
261 Gl.disable `texture_2d;
264 let drawstring1 size x y s =
265 drawstr size x y s;
268 let drawstring2 size x y fmt =
269 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
272 type page =
273 { pageno : int
274 ; pagedimno : int
275 ; pagew : int
276 ; pageh : int
277 ; pagex : int
278 ; pagey : int
279 ; pagevw : int
280 ; pagevh : int
281 ; pagedispx : int
282 ; pagedispy : int
283 ; pagecol : int
287 let debugl l =
288 dolog "l %d dim=%d {" l.pageno l.pagedimno;
289 dolog " WxH %dx%d" l.pagew l.pageh;
290 dolog " vWxH %dx%d" l.pagevw l.pagevh;
291 dolog " pagex,y %d,%d" l.pagex l.pagey;
292 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
293 dolog " column %d" l.pagecol;
294 dolog "}";
297 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
298 dolog "rect {";
299 dolog " x0,y0=(% f, % f)" x0 y0;
300 dolog " x1,y1=(% f, % f)" x1 y1;
301 dolog " x2,y2=(% f, % f)" x2 y2;
302 dolog " x3,y3=(% f, % f)" x3 y3;
303 dolog "}";
306 type multicolumns = multicol * pagegeom
307 and singlecolumn = pagegeom
308 and splitcolumns = columncount * pagegeom
309 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
310 and multicol = columncount * covercount * covercount
311 and pdimno = int
312 and columncount = int
313 and covercount = int;;
315 type scrollb = int;;
316 let scrollbvv = 1;;
317 let scrollbhv = 2;;
319 type conf =
320 { mutable scrollbw : int
321 ; mutable scrollh : int
322 ; mutable scrollb : scrollb
323 ; mutable icase : bool
324 ; mutable preload : bool
325 ; mutable pagebias : int
326 ; mutable verbose : bool
327 ; mutable debug : bool
328 ; mutable scrollstep : int
329 ; mutable hscrollstep : int
330 ; mutable maxhfit : bool
331 ; mutable crophack : bool
332 ; mutable autoscrollstep : int
333 ; mutable maxwait : float option
334 ; mutable hlinks : bool
335 ; mutable underinfo : bool
336 ; mutable interpagespace : interpagespace
337 ; mutable zoom : float
338 ; mutable presentation : bool
339 ; mutable angle : angle
340 ; mutable cwinw : int
341 ; mutable cwinh : int
342 ; mutable savebmarks : bool
343 ; mutable fitmodel : fitmodel
344 ; mutable trimmargins : trimmargins
345 ; mutable trimfuzz : irect
346 ; mutable memlimit : memsize
347 ; mutable texcount : texcount
348 ; mutable sliceheight : sliceheight
349 ; mutable thumbw : width
350 ; mutable jumpback : bool
351 ; mutable bgcolor : (float * float * float)
352 ; mutable bedefault : bool
353 ; mutable tilew : int
354 ; mutable tileh : int
355 ; mutable mustoresize : memsize
356 ; mutable checkers : bool
357 ; mutable aalevel : int
358 ; mutable urilauncher : string
359 ; mutable pathlauncher : string
360 ; mutable colorspace : colorspace
361 ; mutable invert : bool
362 ; mutable colorscale : float
363 ; mutable redirectstderr : bool
364 ; mutable ghyllscroll : (int * int * int) option
365 ; mutable columns : columns
366 ; mutable beyecolumns : columncount option
367 ; mutable selcmd : string
368 ; mutable paxcmd : string
369 ; mutable updatecurs : bool
370 ; mutable keyhashes : (string * keyhash) list
371 ; mutable hfsize : int
372 ; mutable pgscale : float
373 ; mutable usepbo : bool
374 ; mutable wheelbypage : bool
375 ; mutable stcmd : string
376 ; mutable riani : bool
377 ; mutable pax : (float * int * int) ref option
378 ; mutable paxmark : mark
380 and columns =
381 | Csingle of singlecolumn
382 | Cmulti of multicolumns
383 | Csplit of splitcolumns
386 type anchor = pageno * top * dtop;;
388 type outlinekind =
389 | Onone
390 | Oanchor of anchor
391 | Ouri of uri
392 | Olaunch of launchcommand
393 | Oremote of (filename * pageno)
394 | Oremotedest of (filename * destname)
395 and outline = (caption * outlinelevel * outlinekind)
396 and outlinelevel = int
399 type rect = float * float * float * float * float * float * float * float;;
401 type tile = opaque * pixmapsize * elapsed
402 and elapsed = float;;
403 type pagemapkey = pageno * gen;;
404 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
405 and row = int
406 and col = int;;
408 let emptyanchor = (0, 0.0, 0.0);;
410 type infochange = | Memused | Docinfo | Pdim;;
412 class type uioh = object
413 method display : unit
414 method key : int -> int -> uioh
415 method button : int -> bool -> int -> int -> int -> uioh
416 method multiclick : int -> int -> int -> int -> uioh
417 method motion : int -> int -> uioh
418 method pmotion : int -> int -> uioh
419 method infochanged : infochange -> unit
420 method scrollpw : (int * float * float)
421 method scrollph : (int * float * float)
422 method modehash : keyhash
423 method eformsgs : bool
424 end;;
426 type mode =
427 | Birdseye of (conf * leftx * pageno * pageno * anchor)
428 | Textentry of (textentry * onleave)
429 | View
430 | LinkNav of linktarget
431 and onleave = leavetextentrystatus -> unit
432 and leavetextentrystatus = | Cancel | Confirm
433 and helpitem = string * int * action
434 and action =
435 | Noaction
436 | Action of (uioh -> uioh)
437 and linktarget =
438 | Ltexact of (pageno * int)
439 | Ltgendir of int
442 let isbirdseye = function Birdseye _ -> true | _ -> false;;
443 let istextentry = function Textentry _ -> true | _ -> false;;
445 type currently =
446 | Idle
447 | Loading of (page * gen)
448 | Tiling of (
449 page * opaque * colorspace * angle * gen * col * row * width * height
451 | Outlining of outline list
454 let emptykeyhash = Hashtbl.create 0;;
455 let nouioh : uioh = object (self)
456 method display = ()
457 method key _ _ = self
458 method multiclick _ _ _ _ = self
459 method button _ _ _ _ _ = self
460 method motion _ _ = self
461 method pmotion _ _ = self
462 method infochanged _ = ()
463 method scrollpw = (0, nan, nan)
464 method scrollph = (0, nan, nan)
465 method modehash = emptykeyhash
466 method eformsgs = false
467 end;;
469 type state =
470 { mutable sr : Unix.file_descr
471 ; mutable sw : Unix.file_descr
472 ; mutable wsfd : Unix.file_descr
473 ; mutable errfd : Unix.file_descr option
474 ; mutable stderr : Unix.file_descr
475 ; mutable errmsgs : Buffer.t
476 ; mutable newerrmsgs : bool
477 ; mutable w : int
478 ; mutable x : int
479 ; mutable y : int
480 ; mutable anchor : anchor
481 ; mutable ranchors : (string * string * anchor * string) list
482 ; mutable maxy : int
483 ; mutable layout : page list
484 ; pagemap : (pagemapkey, opaque) Hashtbl.t
485 ; tilemap : (tilemapkey, tile) Hashtbl.t
486 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
487 ; mutable pdims : (pageno * width * height * leftx) list
488 ; mutable pagecount : int
489 ; mutable currently : currently
490 ; mutable mstate : mstate
491 ; mutable searchpattern : string
492 ; mutable rects : (pageno * recttype * rect) list
493 ; mutable rects1 : (pageno * recttype * rect) list
494 ; mutable text : string
495 ; mutable winstate : Wsi.winstate list
496 ; mutable mode : mode
497 ; mutable uioh : uioh
498 ; mutable outlines : outline array
499 ; mutable bookmarks : outline list
500 ; mutable path : string
501 ; mutable password : string
502 ; mutable nameddest : string
503 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
504 ; mutable memused : memsize
505 ; mutable gen : gen
506 ; mutable throttle : (page list * int * float) option
507 ; mutable autoscroll : int option
508 ; mutable ghyll : (int option -> unit)
509 ; mutable help : helpitem array
510 ; mutable docinfo : (int * string) list
511 ; mutable texid : GlTex.texture_id option
512 ; hists : hists
513 ; mutable prevzoom : (float * int)
514 ; mutable progress : float
515 ; mutable redisplay : bool
516 ; mutable mpos : mpos
517 ; mutable keystate : keystate
518 ; mutable glinks : bool
519 ; mutable prevcolumns : (columns * float) option
520 ; mutable winw : int
521 ; mutable winh : int
522 ; mutable reprf : (unit -> unit)
523 ; mutable origin : string
524 ; mutable roam : (unit -> unit)
525 ; mutable bzoom : bool
526 ; mutable traw : [`float] Raw.t
527 ; mutable vraw : [`float] Raw.t
529 and hists =
530 { pat : string circbuf
531 ; pag : string circbuf
532 ; nav : anchor circbuf
533 ; sel : string circbuf
537 let defconf =
538 { scrollbw = 7
539 ; scrollh = 12
540 ; scrollb = scrollbhv lor scrollbvv
541 ; icase = true
542 ; preload = true
543 ; pagebias = 0
544 ; verbose = false
545 ; debug = false
546 ; scrollstep = 24
547 ; hscrollstep = 24
548 ; maxhfit = true
549 ; crophack = false
550 ; autoscrollstep = 2
551 ; maxwait = None
552 ; hlinks = false
553 ; underinfo = false
554 ; interpagespace = 2
555 ; zoom = 1.0
556 ; presentation = false
557 ; angle = 0
558 ; cwinw = 900
559 ; cwinh = 900
560 ; savebmarks = true
561 ; fitmodel = FitProportional
562 ; trimmargins = false
563 ; trimfuzz = (0,0,0,0)
564 ; memlimit = 32 lsl 20
565 ; texcount = 256
566 ; sliceheight = 24
567 ; thumbw = 76
568 ; jumpback = true
569 ; bgcolor = (0.5, 0.5, 0.5)
570 ; bedefault = false
571 ; tilew = 2048
572 ; tileh = 2048
573 ; mustoresize = 256 lsl 20
574 ; checkers = true
575 ; aalevel = 8
576 ; urilauncher =
577 (match platform with
578 | Plinux | Pfreebsd | Pdragonflybsd
579 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
580 | Posx -> "open \"%s\""
581 | Pcygwin -> "cygstart \"%s\""
582 | Punknown -> "echo %s")
583 ; pathlauncher = "lp \"%s\""
584 ; selcmd =
585 (match platform with
586 | Plinux | Pfreebsd | Pdragonflybsd
587 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
588 | Posx -> "pbcopy"
589 | Pcygwin -> "wsel"
590 | Punknown -> "cat")
591 ; paxcmd = "cat"
592 ; colorspace = Rgb
593 ; invert = false
594 ; colorscale = 1.0
595 ; redirectstderr = false
596 ; ghyllscroll = None
597 ; columns = Csingle [||]
598 ; beyecolumns = None
599 ; updatecurs = false
600 ; hfsize = 12
601 ; pgscale = 1.0
602 ; usepbo = false
603 ; wheelbypage = false
604 ; stcmd = "echo SyncTex"
605 ; riani = false
606 ; pax = None
607 ; paxmark = Mark_word
608 ; keyhashes =
609 let mk n = (n, Hashtbl.create 1) in
610 [ mk "global"
611 ; mk "info"
612 ; mk "help"
613 ; mk "outline"
614 ; mk "listview"
615 ; mk "birdseye"
616 ; mk "textentry"
617 ; mk "links"
618 ; mk "view"
623 let wtmode = ref false;;
624 let cxack = ref false;;
626 let findkeyhash c name =
627 try List.assoc name c.keyhashes
628 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
631 let conf = { defconf with angle = defconf.angle };;
633 let pgscale h = truncate (float h *. conf.pgscale);;
635 type fontstate =
636 { mutable fontsize : int
637 ; mutable wwidth : float
638 ; mutable maxrows : int
642 let fstate =
643 { fontsize = 14
644 ; wwidth = nan
645 ; maxrows = -1
649 let geturl s =
650 let colonpos = try String.index s ':' with Not_found -> -1 in
651 let len = String.length s in
652 if colonpos >= 0 && colonpos + 3 < len
653 then (
654 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
655 then
656 let schemestartpos =
657 try String.rindex_from s colonpos ' '
658 with Not_found -> -1
660 let scheme =
661 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
663 match scheme with
664 | "http" | "ftp" | "mailto" ->
665 let epos =
666 try String.index_from s colonpos ' '
667 with Not_found -> len
669 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
670 | _ -> ""
671 else ""
673 else ""
676 let gotouri uri =
677 if emptystr conf.urilauncher
678 then print_endline uri
679 else (
680 let url = geturl uri in
681 if emptystr url
682 then Printf.eprintf "obtained empty url from uri %S" uri
683 else
684 let re = Str.regexp "%s" in
685 let command = Str.global_replace re url conf.urilauncher in
686 try popen command []
687 with exn ->
688 Printf.eprintf
689 "failed to execute `%s': %s\n" command (exntos exn);
690 flush stderr;
694 let version () =
695 Printf.sprintf "llpp version %s, fitz %s, ocaml %s (%s/%dbit)"
696 Help.version (fz_version ()) Sys.ocaml_version
697 (platform_to_string platform) Sys.word_size
700 let makehelp () =
701 let strings = version () :: "" :: Help.keys in
702 Array.of_list (
703 List.map (fun s ->
704 let url = geturl s in
705 if nonemptystr url
706 then (s, 0, Action (fun u -> gotouri url; u))
707 else (s, 0, Noaction)
708 ) strings);
711 let noghyll _ = ();;
712 let firstgeomcmds = "", [];;
713 let noreprf () = ();;
714 let noroam () = ();;
716 let state =
717 { sr = Unix.stdin
718 ; sw = Unix.stdin
719 ; wsfd = Unix.stdin
720 ; errfd = None
721 ; stderr = Unix.stderr
722 ; errmsgs = Buffer.create 0
723 ; newerrmsgs = false
724 ; x = 0
725 ; y = 0
726 ; w = 0
727 ; anchor = emptyanchor
728 ; ranchors = []
729 ; layout = []
730 ; maxy = max_int
731 ; tilelru = Queue.create ()
732 ; pagemap = Hashtbl.create 10
733 ; tilemap = Hashtbl.create 10
734 ; pdims = []
735 ; pagecount = 0
736 ; currently = Idle
737 ; mstate = Mnone
738 ; rects = []
739 ; rects1 = []
740 ; text = ""
741 ; mode = View
742 ; winstate = []
743 ; searchpattern = ""
744 ; outlines = [||]
745 ; bookmarks = []
746 ; path = ""
747 ; password = ""
748 ; nameddest = ""
749 ; geomcmds = firstgeomcmds
750 ; hists =
751 { nav = cbnew 10 emptyanchor
752 ; pat = cbnew 10 ""
753 ; pag = cbnew 10 ""
754 ; sel = cbnew 10 ""
756 ; memused = 0
757 ; gen = 0
758 ; throttle = None
759 ; autoscroll = None
760 ; ghyll = noghyll
761 ; help = makehelp ()
762 ; docinfo = []
763 ; texid = None
764 ; prevzoom = (1.0, 0)
765 ; progress = -1.0
766 ; uioh = nouioh
767 ; redisplay = true
768 ; mpos = (-1, -1)
769 ; keystate = KSnone
770 ; glinks = false
771 ; prevcolumns = None
772 ; winw = -1
773 ; winh = -1
774 ; reprf = noreprf
775 ; origin = ""
776 ; roam = noroam
777 ; bzoom = false
778 ; traw = Raw.create_static `float 8
779 ; vraw = Raw.create_static `float 8
783 let hscrollh () =
784 if (conf.scrollb land scrollbhv = 0)
785 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
786 then 0
787 else conf.scrollbw
790 let vscrollw () =
791 if (conf.scrollb land scrollbvv = 0)
792 then 0
793 else conf.scrollbw
796 let wadjsb w = w - vscrollw ();;
798 let setfontsize n =
799 fstate.fontsize <- n;
800 fstate.wwidth <- measurestr fstate.fontsize "w";
801 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
804 let vlog fmt =
805 if conf.verbose
806 then
807 Printf.kprintf prerr_endline fmt
808 else
809 Printf.kprintf ignore fmt
812 let launchpath () =
813 if emptystr conf.pathlauncher
814 then print_endline state.path
815 else (
816 let re = Str.regexp "%s" in
817 let command = Str.global_replace re state.path conf.pathlauncher in
818 try popen command []
819 with exn ->
820 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
821 flush stderr;
825 module Ne = struct
826 type 'a t = | Res of 'a | Exn of exn;;
828 let res f =
829 try Res (f ())
830 with exn -> Exn exn
833 let clo fd f =
834 try tempfailureretry Unix.close fd
835 with exn -> f (exntos exn)
838 let dup fd =
839 try Res (tempfailureretry Unix.dup fd)
840 with exn -> Exn exn
843 let dup2 fd1 fd2 =
844 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
845 with exn -> Exn exn
847 end;;
849 let redirectstderr () =
850 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
851 if conf.redirectstderr
852 then
853 match Ne.res Unix.pipe with
854 | Ne.Exn exn ->
855 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
857 | Ne.Res (r, w) ->
858 begin match Ne.dup Unix.stderr with
859 | Ne.Exn exn ->
860 dolog "failed to dup stderr: %s" (exntos exn);
861 Ne.clo r (clofail "pipe/r");
862 Ne.clo w (clofail "pipe/w");
864 | Ne.Res dupstderr ->
865 begin match Ne.dup2 w Unix.stderr with
866 | Ne.Exn exn ->
867 dolog "failed to dup2 to stderr: %s" (exntos exn);
868 Ne.clo dupstderr (clofail "stderr duplicate");
869 Ne.clo r (clofail "redir pipe/r");
870 Ne.clo w (clofail "redir pipe/w");
872 | Ne.Res () ->
873 state.stderr <- dupstderr;
874 state.errfd <- Some r;
875 end;
877 else (
878 state.newerrmsgs <- false;
879 begin match state.errfd with
880 | Some fd ->
881 begin match Ne.dup2 state.stderr Unix.stderr with
882 | Ne.Exn exn ->
883 dolog "failed to dup2 original stderr: %s" (exntos exn)
884 | Ne.Res () ->
885 Ne.clo fd (clofail "dup of stderr");
886 state.errfd <- None;
887 end;
888 | None -> ()
889 end;
890 prerr_string (Buffer.contents state.errmsgs);
891 flush stderr;
892 Buffer.clear state.errmsgs;
896 module G =
897 struct
898 let postRedisplay who =
899 if conf.verbose
900 then prerr_endline ("redisplay for " ^ who);
901 state.redisplay <- true;
903 end;;
905 let getopaque pageno =
906 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
907 with Not_found -> None
910 let putopaque pageno opaque =
911 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
914 let pagetranslatepoint l x y =
915 let dy = y - l.pagedispy in
916 let y = dy + l.pagey in
917 let dx = x - l.pagedispx in
918 let x = dx + l.pagex in
919 (x, y);
922 let onppundermouse g x y d =
923 let rec f = function
924 | l :: rest ->
925 begin match getopaque l.pageno with
926 | Some opaque ->
927 let x0 = l.pagedispx in
928 let x1 = x0 + l.pagevw in
929 let y0 = l.pagedispy in
930 let y1 = y0 + l.pagevh in
931 if y >= y0 && y <= y1 && x >= x0 && x <= x1
932 then
933 let px, py = pagetranslatepoint l x y in
934 match g opaque l px py with
935 | Some res -> res
936 | None -> f rest
937 else f rest
938 | _ ->
939 f rest
941 | [] -> d
943 f state.layout
946 let getunder x y =
947 let g opaque l px py =
948 if state.bzoom
949 then (
950 match rectofblock opaque px py with
951 | Some a ->
952 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
953 state.rects <- [l.pageno, l.pageno mod 3, rect];
954 G.postRedisplay "getunder";
955 | None -> ()
957 match whatsunder opaque px py with
958 | Unone -> None
959 | under -> Some under
961 onppundermouse g x y Unone
964 let unproject x y =
965 let g opaque l x y =
966 match unproject opaque x y with
967 | Some (x, y) -> Some (Some (l.pageno, x, y))
968 | None -> None
970 onppundermouse g x y None;
973 let showtext c s =
974 state.text <- Printf.sprintf "%c%s" c s;
975 G.postRedisplay "showtext";
978 let pipesel opaque cmd =
979 match Ne.res Unix.pipe with
980 | Ne.Exn exn ->
981 showtext '!'
982 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
983 | Ne.Res (r, w) ->
984 let doclose what fd =
985 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
987 begin try
988 popen cmd [r, 0; w, -1];
989 with exn ->
990 doclose "pipesel pipe/w" w;
991 dolog "can not execute %S: %s" cmd (exntos exn);
992 end;
993 copysel w opaque;
994 G.postRedisplay "pipesel";
995 doclose "pipesel pipe/r" r;
998 let paxunder x y =
999 let g opaque l px py =
1000 if markunder opaque px py conf.paxmark
1001 then (
1002 Some (fun () ->
1003 match getopaque l.pageno with
1004 | None -> ()
1005 | Some opaque -> pipesel conf.paxcmd opaque
1008 else None
1010 G.postRedisplay "paxunder";
1011 if conf.paxmark = Mark_page
1012 then
1013 List.iter (fun l ->
1014 match getopaque l.pageno with
1015 | None -> ()
1016 | Some opaque -> clearmark opaque) state.layout;
1017 state.roam <-
1018 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
1021 let selstring s =
1022 match Ne.res Unix.pipe with
1023 | Ne.Exn exn ->
1024 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
1025 | Ne.Res (r, w) ->
1026 let clo cap fd =
1027 Ne.clo fd (fun msg ->
1028 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1031 let popened =
1032 try popen conf.selcmd [r, 0; w, -1]; true
1033 with exn ->
1034 clo "selstring pipe/w" w;
1035 showtext '!'
1036 (Printf.sprintf "failed to execute %s: %s"
1037 conf.selcmd (exntos exn));
1038 false
1040 if popened
1041 then (
1043 let l = String.length s in
1044 let n = tempfailureretry (Unix.write w s 0) l in
1045 if n != l
1046 then
1047 showtext '!'
1048 (Printf.sprintf
1049 "failed to write %d characters to sel pipe, wrote %d"
1052 with exn ->
1053 showtext '!'
1054 (Printf.sprintf "failed to write to sel pipe: %s"
1055 (exntos exn)
1058 else dolog "%s" s;
1059 clo "selstring pipe/r" r;
1062 let undertext = function
1063 | Unone -> "none"
1064 | Ulinkuri s -> s
1065 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1066 | Utext s -> "font: " ^ s
1067 | Uunexpected s -> "unexpected: " ^ s
1068 | Ulaunch s -> "launch: " ^ s
1069 | Unamed s -> "named: " ^ s
1070 | Uremote (filename, pageno) ->
1071 Printf.sprintf "%s: page %d" filename (pageno+1)
1072 | Uremotedest (filename, destname) ->
1073 Printf.sprintf "%s: destination %S" filename destname
1076 let updateunder x y =
1077 match getunder x y with
1078 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1079 | Ulinkuri uri ->
1080 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1081 Wsi.setcursor Wsi.CURSOR_INFO
1082 | Ulinkgoto (pageno, _) ->
1083 if conf.underinfo
1084 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1085 Wsi.setcursor Wsi.CURSOR_INFO
1086 | Utext s ->
1087 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1088 Wsi.setcursor Wsi.CURSOR_TEXT
1089 | Uunexpected s ->
1090 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1091 Wsi.setcursor Wsi.CURSOR_INHERIT
1092 | Ulaunch s ->
1093 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1094 Wsi.setcursor Wsi.CURSOR_INHERIT
1095 | Unamed s ->
1096 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1097 Wsi.setcursor Wsi.CURSOR_INHERIT
1098 | Uremote (filename, pageno) ->
1099 if conf.underinfo then showtext 'r'
1100 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1101 Wsi.setcursor Wsi.CURSOR_INFO
1102 | Uremotedest (filename, destname) ->
1103 if conf.underinfo then showtext 'r'
1104 (Printf.sprintf "emote destination: %s (%S)" filename destname);
1105 Wsi.setcursor Wsi.CURSOR_INFO
1108 let showlinktype under =
1109 if conf.underinfo
1110 then
1111 match under with
1112 | Unone -> ()
1113 | under ->
1114 let s = undertext under in
1115 showtext ' ' s
1118 let addchar s c =
1119 let b = Buffer.create (String.length s + 1) in
1120 Buffer.add_string b s;
1121 Buffer.add_char b c;
1122 Buffer.contents b;
1125 module type TextEnumType =
1127 type t
1128 val name : string
1129 val names : string array
1130 end;;
1132 module TextEnumMake (Ten : TextEnumType) =
1133 struct
1134 let names = Ten.names;;
1135 let to_int (t : Ten.t) = Obj.magic t;;
1136 let to_string t = names.(to_int t);;
1137 let of_int n : Ten.t = Obj.magic n;;
1138 let of_string s =
1139 let rec find i =
1140 if i = Array.length names
1141 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1142 else (
1143 if Ten.names.(i) = s
1144 then of_int i
1145 else find (i+1)
1147 in find 0;;
1148 end;;
1150 module CSTE = TextEnumMake (struct
1151 type t = colorspace;;
1152 let name = "colorspace";;
1153 let names = [|"rgb"; "bgr"; "gray"|];;
1154 end);;
1156 module MTE = TextEnumMake (struct
1157 type t = mark;;
1158 let name = "mark";;
1159 let names = [|"page"; "block"; "line"; "word"|];;
1160 end);;
1162 module FMTE = TextEnumMake (struct
1163 type t= fitmodel;;
1164 let name = "fitmodel";;
1165 let names = [|"width"; "proportional"; "page"|];;
1166 end);;
1168 let intentry_with_suffix text key =
1169 let c =
1170 if key >= 32 && key < 127
1171 then Char.chr key
1172 else '\000'
1174 match Char.lowercase c with
1175 | '0' .. '9' ->
1176 let text = addchar text c in
1177 TEcont text
1179 | 'k' | 'm' | 'g' ->
1180 let text = addchar text c in
1181 TEcont text
1183 | _ ->
1184 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1185 TEcont text
1188 let multicolumns_to_string (n, a, b) =
1189 if a = 0 && b = 0
1190 then Printf.sprintf "%d" n
1191 else Printf.sprintf "%d,%d,%d" n a b;
1194 let multicolumns_of_string s =
1196 (int_of_string s, 0, 0)
1197 with _ ->
1198 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1199 if a > 1 || b > 1
1200 then failwith "subtly broken"; (n, a, b)
1204 let readcmd fd =
1205 let s = "xxxx" in
1206 let n = tempfailureretry (Unix.read fd s 0) 4 in
1207 if n != 4 then error "incomplete read(len) = %d" n;
1208 let len = 0
1209 lor (Char.code s.[0] lsl 24)
1210 lor (Char.code s.[1] lsl 16)
1211 lor (Char.code s.[2] lsl 8)
1212 lor (Char.code s.[3] lsl 0)
1214 let s = String.create len in
1215 let n = tempfailureretry (Unix.read fd s 0) len in
1216 if n != len then error "incomplete read(data) %d vs %d" n len;
1220 let btod b = if b then 1 else 0;;
1222 let wcmd fmt =
1223 let b = Buffer.create 16 in
1224 Buffer.add_string b "llll";
1225 Printf.kbprintf
1226 (fun b ->
1227 let s = Buffer.contents b in
1228 let n = String.length s in
1229 let len = n - 4 in
1230 (* dolog "wcmd %S" (String.sub s 4 len); *)
1231 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1232 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1233 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1234 s.[3] <- Char.chr (len land 0xff);
1235 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1236 if n' != n then error "write failed %d vs %d" n' n;
1237 ) b fmt;
1240 let calcips h =
1241 let d = state.winh - h in
1242 max conf.interpagespace ((d + 1) / 2)
1245 let rowyh (c, coverA, coverB) b n =
1246 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1247 then
1248 let _, _, vy, (_, _, h, _) = b.(n) in
1249 (vy, h)
1250 else
1251 let n' = n - coverA in
1252 let d = n' mod c in
1253 let s = n - d in
1254 let e = min state.pagecount (s + c) in
1255 let rec find m miny maxh = if m = e then miny, maxh else
1256 let _, _, y, (_, _, h, _) = b.(m) in
1257 let miny = min miny y in
1258 let maxh = max maxh h in
1259 find (m+1) miny maxh
1260 in find s max_int 0
1263 let calcheight () =
1264 match conf.columns with
1265 | Cmulti ((_, _, _) as cl, b) ->
1266 if Array.length b > 0
1267 then
1268 let y, h = rowyh cl b (Array.length b - 1) in
1269 y + h + (if conf.presentation then calcips h else 0)
1270 else 0
1271 | Csingle b ->
1272 if Array.length b > 0
1273 then
1274 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1275 y + h + (if conf.presentation then calcips h else 0)
1276 else 0
1277 | Csplit (_, b) ->
1278 if Array.length b > 0
1279 then
1280 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1281 y + h
1282 else 0
1285 let getpageywh pageno =
1286 let pageno = bound pageno 0 (state.pagecount-1) in
1287 match conf.columns with
1288 | Csingle b ->
1289 if Array.length b = 0
1290 then 0, 0, 0
1291 else
1292 let (_, _, y, (_, w, h, _)) = b.(pageno) in
1293 let y =
1294 if conf.presentation
1295 then y - calcips h
1296 else y
1298 y, w, h
1299 | Cmulti (cl, b) ->
1300 if Array.length b = 0
1301 then 0, 0, 0
1302 else
1303 let y, h = rowyh cl b pageno in
1304 let (_, _, _, (_, w, _, _)) = b.(pageno) in
1305 let y =
1306 if conf.presentation
1307 then y - calcips h
1308 else y
1310 y, w, h
1311 | Csplit (c, b) ->
1312 if Array.length b = 0
1313 then 0, 0, 0
1314 else
1315 let n = pageno*c in
1316 let (_, _, y, (_, w, h, _)) = b.(n) in
1317 y, w / c, h
1320 let getpageyh pageno =
1321 let y,_,h = getpageywh pageno in
1322 y, h;
1325 let getpagedim pageno =
1326 let rec f ppdim l =
1327 match l with
1328 | (n, _, _, _) as pdim :: rest ->
1329 if n >= pageno
1330 then (if n = pageno then pdim else ppdim)
1331 else f pdim rest
1333 | [] -> ppdim
1335 f (-1, -1, -1, -1) state.pdims
1338 let getpagey pageno = fst (getpageyh pageno);;
1340 let nogeomcmds cmds =
1341 match cmds with
1342 | s, [] -> emptystr s
1343 | _ -> false
1346 let page_of_y y =
1347 let ((c, coverA, coverB) as cl), b =
1348 match conf.columns with
1349 | Csingle b -> (1, 0, 0), b
1350 | Cmulti (c, b) -> c, b
1351 | Csplit (_, b) -> (1, 0, 0), b
1353 if Array.length b = 0
1354 then -1
1355 else
1356 let rec bsearch nmin nmax =
1357 if nmin > nmax
1358 then bound nmin 0 (state.pagecount-1)
1359 else
1360 let n = (nmax + nmin) / 2 in
1361 let vy, h = rowyh cl b n in
1362 let y0, y1 =
1363 if conf.presentation
1364 then
1365 let ips = calcips h in
1366 let y0 = vy - ips in
1367 let y1 = vy + h + ips in
1368 y0, y1
1369 else (
1370 if n = 0
1371 then 0, vy + h + conf.interpagespace
1372 else
1373 let y0 = vy - conf.interpagespace in
1374 y0, y0 + h + conf.interpagespace
1377 if y >= y0 && y < y1
1378 then (
1379 if c = 1
1380 then n
1381 else (
1382 if n > coverA
1383 then
1384 if n < state.pagecount - coverB
1385 then ((n-coverA)/c)*c + coverA
1386 else n
1387 else n
1390 else (
1391 if y > y0
1392 then bsearch (n+1) nmax
1393 else bsearch nmin (n-1)
1396 bsearch 0 (state.pagecount-1);
1399 let layoutN ((columns, coverA, coverB), b) y sh =
1400 let sh = sh - (hscrollh ()) in
1401 let rec fold accu n =
1402 if n = Array.length b
1403 then accu
1404 else
1405 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1406 if (vy - y) > sh &&
1407 (n = coverA - 1
1408 || n = state.pagecount - coverB
1409 || (n - coverA) mod columns = columns - 1)
1410 then accu
1411 else
1412 let accu =
1413 if vy + h > y
1414 then
1415 let pagey = max 0 (y - vy) in
1416 let pagedispy = if pagey > 0 then 0 else vy - y in
1417 let pagedispx, pagex =
1418 let pdx =
1419 if n = coverA - 1 || n = state.pagecount - coverB
1420 then state.x + (wadjsb state.winw - w) / 2
1421 else dx + xoff + state.x
1423 if pdx < 0
1424 then 0, -pdx
1425 else pdx, 0
1427 let pagevw =
1428 let vw = wadjsb state.winw - pagedispx in
1429 let pw = w - pagex in
1430 min vw pw
1432 let pagevh = min (h - pagey) (sh - pagedispy) in
1433 if pagevw > 0 && pagevh > 0
1434 then
1435 let e =
1436 { pageno = n
1437 ; pagedimno = pdimno
1438 ; pagew = w
1439 ; pageh = h
1440 ; pagex = pagex
1441 ; pagey = pagey
1442 ; pagevw = pagevw
1443 ; pagevh = pagevh
1444 ; pagedispx = pagedispx
1445 ; pagedispy = pagedispy
1446 ; pagecol = 0
1449 e :: accu
1450 else
1451 accu
1452 else
1453 accu
1455 fold accu (n+1)
1457 if Array.length b = 0
1458 then []
1459 else List.rev (fold [] (page_of_y y))
1462 let layoutS (columns, b) y sh =
1463 let sh = sh - hscrollh () in
1464 let rec fold accu n =
1465 if n = Array.length b
1466 then accu
1467 else
1468 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1469 if (vy - y) > sh
1470 then accu
1471 else
1472 let accu =
1473 if vy + pageh > y
1474 then
1475 let x = xoff + state.x in
1476 let pagey = max 0 (y - vy) in
1477 let pagedispy = if pagey > 0 then 0 else vy - y in
1478 let pagedispx, pagex =
1479 if px = 0
1480 then (
1481 if x < 0
1482 then 0, -x
1483 else x, 0
1485 else (
1486 let px = px - x in
1487 if px < 0
1488 then -px, 0
1489 else 0, px
1492 let pagecolw = pagew/columns in
1493 let pagedispx =
1494 if pagecolw < state.winw
1495 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1496 else pagedispx
1498 let pagevw =
1499 let vw = wadjsb state.winw - pagedispx in
1500 let pw = pagew - pagex in
1501 min vw pw
1503 let pagevw = min pagevw pagecolw in
1504 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1505 if pagevw > 0 && pagevh > 0
1506 then
1507 let e =
1508 { pageno = n/columns
1509 ; pagedimno = pdimno
1510 ; pagew = pagew
1511 ; pageh = pageh
1512 ; pagex = pagex
1513 ; pagey = pagey
1514 ; pagevw = pagevw
1515 ; pagevh = pagevh
1516 ; pagedispx = pagedispx
1517 ; pagedispy = pagedispy
1518 ; pagecol = n mod columns
1521 e :: accu
1522 else
1523 accu
1524 else
1525 accu
1527 fold accu (n+1)
1529 List.rev (fold [] 0)
1532 let layout y sh =
1533 if nogeomcmds state.geomcmds
1534 then
1535 match conf.columns with
1536 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1537 | Cmulti c -> layoutN c y sh
1538 | Csplit s -> layoutS s y sh
1539 else []
1542 let clamp incr =
1543 let y = state.y + incr in
1544 let y = max 0 y in
1545 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1549 let itertiles l f =
1550 let tilex = l.pagex mod conf.tilew in
1551 let tiley = l.pagey mod conf.tileh in
1553 let col = l.pagex / conf.tilew in
1554 let row = l.pagey / conf.tileh in
1556 let rec rowloop row y0 dispy h =
1557 if h = 0
1558 then ()
1559 else (
1560 let dh = conf.tileh - y0 in
1561 let dh = min h dh in
1562 let rec colloop col x0 dispx w =
1563 if w = 0
1564 then ()
1565 else (
1566 let dw = conf.tilew - x0 in
1567 let dw = min w dw in
1569 f col row dispx dispy x0 y0 dw dh;
1570 colloop (col+1) 0 (dispx+dw) (w-dw)
1573 colloop col tilex l.pagedispx l.pagevw;
1574 rowloop (row+1) 0 (dispy+dh) (h-dh)
1577 if l.pagevw > 0 && l.pagevh > 0
1578 then rowloop row tiley l.pagedispy l.pagevh;
1581 let gettileopaque l col row =
1582 let key =
1583 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1585 try Some (Hashtbl.find state.tilemap key)
1586 with Not_found -> None
1589 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1590 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1591 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1594 let filledrect x0 y0 x1 y1 =
1595 GlArray.disable `texture_coord;
1596 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1597 GlArray.vertex `two state.vraw;
1598 GlArray.draw_arrays `triangle_strip 0 4;
1599 GlArray.enable `texture_coord;
1602 let linerect x0 y0 x1 y1 =
1603 GlArray.disable `texture_coord;
1604 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
1605 GlArray.vertex `two state.vraw;
1606 GlArray.draw_arrays `line_loop 0 4;
1607 GlArray.enable `texture_coord;
1610 let drawtiles l color =
1611 GlDraw.color color;
1612 begintiles ();
1613 let f col row x y tilex tiley w h =
1614 match gettileopaque l col row with
1615 | Some (opaque, _, t) ->
1616 let params = x, y, w, h, tilex, tiley in
1617 if conf.invert
1618 then (
1619 Gl.enable `blend;
1620 GlFunc.blend_func `zero `one_minus_src_color;
1622 drawtile params opaque;
1623 if conf.invert
1624 then Gl.disable `blend;
1625 if conf.debug
1626 then (
1627 endtiles ();
1628 let s = Printf.sprintf
1629 "%d[%d,%d] %f sec"
1630 l.pageno col row t
1632 let w = measurestr fstate.fontsize s in
1633 GlDraw.color (0.0, 0.0, 0.0);
1634 filledrect (float (x-2))
1635 (float (y-2))
1636 (float (x+2) +. w)
1637 (float (y + fstate.fontsize + 2));
1638 GlDraw.color (1.0, 1.0, 1.0);
1639 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1640 begintiles ();
1643 | None ->
1644 endtiles ();
1645 let w =
1646 let lw = wadjsb state.winw - x in
1647 min lw w
1648 and h =
1649 let lh = state.winh - y in
1650 min lh h
1652 if conf.invert
1653 then (
1654 Gl.enable `blend;
1655 GlFunc.blend_func `zero `one_minus_src_color;
1657 begin match state.texid with
1658 | Some id ->
1659 Gl.enable `texture_2d;
1660 GlTex.bind_texture `texture_2d id;
1661 let x0 = float x
1662 and y0 = float y
1663 and x1 = float (x+w)
1664 and y1 = float (y+h) in
1666 let tw = float w /. 16.0
1667 and th = float h /. 16.0 in
1668 let tx0 = float tilex /. 16.0
1669 and ty0 = float tiley /. 16.0 in
1670 let tx1 = tx0 +. tw
1671 and ty1 = ty0 +. th in
1672 Raw.sets_float state.vraw ~pos:0
1673 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1674 Raw.sets_float state.traw ~pos:0
1675 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
1676 GlArray.vertex `two state.vraw;
1677 GlArray.tex_coord `two state.traw;
1678 GlArray.draw_arrays `triangle_strip 0 4;
1680 | None ->
1681 GlDraw.color (1.0, 1.0, 1.0);
1682 filledrect (float x) (float y) (float (x+w)) (float (y+h));
1683 end;
1684 if w > 128 && h > fstate.fontsize + 10
1685 then (
1686 let c = if conf.invert then 1.0 else 0.0 in
1687 GlDraw.color (c, c, c);
1688 let c, r =
1689 if conf.verbose
1690 then (col*conf.tilew, row*conf.tileh)
1691 else col, row
1693 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1695 GlDraw.color color;
1696 begintiles ();
1698 itertiles l f;
1699 endtiles ();
1702 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1704 let tilevisible1 l x y =
1705 let ax0 = l.pagex
1706 and ax1 = l.pagex + l.pagevw
1707 and ay0 = l.pagey
1708 and ay1 = l.pagey + l.pagevh in
1710 let bx0 = x
1711 and by0 = y in
1712 let bx1 = min (bx0 + conf.tilew) l.pagew
1713 and by1 = min (by0 + conf.tileh) l.pageh in
1715 let rx0 = max ax0 bx0
1716 and ry0 = max ay0 by0
1717 and rx1 = min ax1 bx1
1718 and ry1 = min ay1 by1 in
1720 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1721 nonemptyintersection
1724 let tilevisible layout n x y =
1725 let rec findpageinlayout m = function
1726 | l :: rest when l.pageno = n ->
1727 tilevisible1 l x y || (
1728 match conf.columns with
1729 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1730 | _ -> false
1732 | _ :: rest -> findpageinlayout 0 rest
1733 | [] -> false
1735 findpageinlayout 0 layout;
1738 let tileready l x y =
1739 tilevisible1 l x y &&
1740 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1743 let tilepage n p layout =
1744 let rec loop = function
1745 | l :: rest ->
1746 if l.pageno = n
1747 then
1748 let f col row _ _ _ _ _ _ =
1749 if state.currently = Idle
1750 then
1751 match gettileopaque l col row with
1752 | Some _ -> ()
1753 | None ->
1754 let x = col*conf.tilew
1755 and y = row*conf.tileh in
1756 let w =
1757 let w = l.pagew - x in
1758 min w conf.tilew
1760 let h =
1761 let h = l.pageh - y in
1762 min h conf.tileh
1764 let pbo =
1765 if conf.usepbo
1766 then getpbo w h conf.colorspace
1767 else "0"
1769 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1770 state.currently <-
1771 Tiling (
1772 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1773 conf.tilew, conf.tileh
1776 itertiles l f;
1777 else
1778 loop rest
1780 | [] -> ()
1782 if nogeomcmds state.geomcmds
1783 then loop layout;
1786 let preloadlayout y =
1787 let y = if y < state.winh then 0 else y - state.winh in
1788 let h = state.winh*3 in
1789 layout y h;
1792 let load pages =
1793 let rec loop pages =
1794 if state.currently != Idle
1795 then ()
1796 else
1797 match pages with
1798 | l :: rest ->
1799 begin match getopaque l.pageno with
1800 | None ->
1801 wcmd "page %d %d" l.pageno l.pagedimno;
1802 state.currently <- Loading (l, state.gen);
1803 | Some opaque ->
1804 tilepage l.pageno opaque pages;
1805 loop rest
1806 end;
1807 | _ -> ()
1809 if nogeomcmds state.geomcmds
1810 then loop pages
1813 let preload pages =
1814 load pages;
1815 if conf.preload && state.currently = Idle
1816 then load (preloadlayout state.y);
1819 let layoutready layout =
1820 let rec fold all ls =
1821 all && match ls with
1822 | l :: rest ->
1823 let seen = ref false in
1824 let allvisible = ref true in
1825 let foo col row _ _ _ _ _ _ =
1826 seen := true;
1827 allvisible := !allvisible &&
1828 begin match gettileopaque l col row with
1829 | Some _ -> true
1830 | None -> false
1833 itertiles l foo;
1834 fold (!seen && !allvisible) rest
1835 | [] -> true
1837 let alltilesvisible = fold true layout in
1838 alltilesvisible;
1841 let gotoy y =
1842 let y = bound y 0 state.maxy in
1843 let y, layout, proceed =
1844 match conf.maxwait with
1845 | Some time when state.ghyll == noghyll ->
1846 begin match state.throttle with
1847 | None ->
1848 let layout = layout y state.winh in
1849 let ready = layoutready layout in
1850 if not ready
1851 then (
1852 load layout;
1853 state.throttle <- Some (layout, y, now ());
1855 else G.postRedisplay "gotoy showall (None)";
1856 y, layout, ready
1857 | Some (_, _, started) ->
1858 let dt = now () -. started in
1859 if dt > time
1860 then (
1861 state.throttle <- None;
1862 let layout = layout y state.winh in
1863 load layout;
1864 G.postRedisplay "maxwait";
1865 y, layout, true
1867 else -1, [], false
1870 | _ ->
1871 let layout = layout y state.winh in
1872 if not !wtmode || layoutready layout
1873 then G.postRedisplay "gotoy ready";
1874 y, layout, true
1876 if proceed
1877 then (
1878 state.y <- y;
1879 state.layout <- layout;
1880 begin match state.mode with
1881 | LinkNav (Ltexact (pageno, linkno)) ->
1882 let rec loop = function
1883 | [] ->
1884 state.mode <- LinkNav (Ltgendir 0)
1885 | l :: _ when l.pageno = pageno ->
1886 begin match getopaque pageno with
1887 | None ->
1888 state.mode <- LinkNav (Ltgendir 0)
1889 | Some opaque ->
1890 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1891 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1892 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1893 then state.mode <- LinkNav (Ltgendir 0)
1895 | _ :: rest -> loop rest
1897 loop layout
1898 | _ -> ()
1899 end;
1900 begin match state.mode with
1901 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1902 if not (pagevisible layout pageno)
1903 then (
1904 match state.layout with
1905 | [] -> ()
1906 | l :: _ ->
1907 state.mode <- Birdseye (
1908 conf, leftx, l.pageno, hooverpageno, anchor
1911 | LinkNav (Ltgendir dir as lt) ->
1912 let linknav =
1913 let rec loop = function
1914 | [] -> lt
1915 | l :: rest ->
1916 match getopaque l.pageno with
1917 | None -> loop rest
1918 | Some opaque ->
1919 let link =
1920 let ld =
1921 if dir = 0
1922 then LDfirstvisible (l.pagex, l.pagey, dir)
1923 else (
1924 if dir > 0 then LDfirst else LDlast
1927 findlink opaque ld
1929 match link with
1930 | Lnotfound -> loop rest
1931 | Lfound n ->
1932 showlinktype (getlink opaque n);
1933 Ltexact (l.pageno, n)
1935 loop state.layout
1937 state.mode <- LinkNav linknav
1938 | _ -> ()
1939 end;
1940 preload layout;
1942 state.ghyll <- noghyll;
1943 if conf.updatecurs
1944 then (
1945 let mx, my = state.mpos in
1946 updateunder mx my;
1950 let conttiling pageno opaque =
1951 tilepage pageno opaque
1952 (if conf.preload then preloadlayout state.y else state.layout)
1955 let gotoy_and_clear_text y =
1956 if not conf.verbose then state.text <- "";
1957 gotoy y;
1960 let getanchor1 l =
1961 let top =
1962 let coloff = l.pagecol * l.pageh in
1963 float (l.pagey + coloff) /. float l.pageh
1965 let dtop =
1966 if l.pagedispy = 0
1967 then
1969 else (
1970 if conf.presentation
1971 then float l.pagedispy /. float (calcips l.pageh)
1972 else float l.pagedispy /. float conf.interpagespace
1975 (l.pageno, top, dtop)
1978 let getanchor () =
1979 match state.layout with
1980 | l :: _ -> getanchor1 l
1981 | [] ->
1982 let n = page_of_y state.y in
1983 if n = -1
1984 then state.anchor
1985 else
1986 let y, h = getpageyh n in
1987 let dy = y - state.y in
1988 let dtop =
1989 if conf.presentation
1990 then
1991 let ips = calcips h in
1992 float (dy + ips) /. float ips
1993 else
1994 float dy /. float conf.interpagespace
1996 (n, 0.0, dtop)
1999 let getanchory (n, top, dtop) =
2000 let y, h = getpageyh n in
2001 if conf.presentation
2002 then
2003 let ips = calcips h in
2004 y + truncate (top*.float h -. dtop*.float ips) + ips;
2005 else
2006 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
2009 let gotoanchor anchor =
2010 gotoy (getanchory anchor);
2013 let addnav () =
2014 cbput state.hists.nav (getanchor ());
2017 let getnav dir =
2018 let anchor = cbgetc state.hists.nav dir in
2019 getanchory anchor;
2022 let gotoghyll y =
2023 let scroll f n a b =
2024 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
2025 let snake f a b =
2026 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
2027 if f < a
2028 then s (float f /. float a)
2029 else (
2030 if f > b
2031 then 1.0 -. s ((float (f-b) /. float (n-b)))
2032 else 1.0
2035 snake f a b
2036 and summa n a b =
2037 let ins = float a *. 0.5
2038 and outs = float (n-b) *. 0.5 in
2039 let ones = b - a in
2040 ins +. outs +. float ones
2042 let rec set (_N, _A, _B) y sy =
2043 let sum = summa _N _A _B in
2044 let dy = float (y - sy) in
2045 state.ghyll <- (
2046 let rec gf n y1 o =
2047 if n >= _N
2048 then state.ghyll <- noghyll
2049 else
2050 let go n =
2051 let s = scroll n _N _A _B in
2052 let y1 = y1 +. ((s *. dy) /. sum) in
2053 gotoy_and_clear_text (truncate y1);
2054 state.ghyll <- gf (n+1) y1;
2056 match o with
2057 | None -> go n
2058 | Some y' -> set (_N/2, 1, 1) y' state.y
2060 gf 0 (float state.y)
2063 match conf.ghyllscroll with
2064 | None ->
2065 gotoy_and_clear_text y
2066 | Some nab ->
2067 if state.ghyll == noghyll
2068 then set nab y state.y
2069 else state.ghyll (Some y)
2072 let gotopage n top =
2073 let y, h = getpageyh n in
2074 let y = y + (truncate (top *. float h)) in
2075 gotoghyll y
2078 let gotopage1 n top =
2079 let y = getpagey n in
2080 let y = y + top in
2081 gotoghyll y
2084 let invalidate s f =
2085 state.layout <- [];
2086 state.pdims <- [];
2087 state.rects <- [];
2088 state.rects1 <- [];
2089 match state.geomcmds with
2090 | ps, [] when emptystr ps ->
2091 f ();
2092 state.geomcmds <- s, [];
2094 | ps, [] ->
2095 state.geomcmds <- ps, [s, f];
2097 | ps, (s', _) :: rest when s' = s ->
2098 state.geomcmds <- ps, ((s, f) :: rest);
2100 | ps, cmds ->
2101 state.geomcmds <- ps, ((s, f) :: cmds);
2104 let flushpages () =
2105 Hashtbl.iter (fun _ opaque ->
2106 wcmd "freepage %s" opaque;
2107 ) state.pagemap;
2108 Hashtbl.clear state.pagemap;
2111 let flushtiles () =
2112 if not (Queue.is_empty state.tilelru)
2113 then (
2114 Queue.iter (fun (k, p, s) ->
2115 wcmd "freetile %s" p;
2116 state.memused <- state.memused - s;
2117 Hashtbl.remove state.tilemap k;
2118 ) state.tilelru;
2119 state.uioh#infochanged Memused;
2120 Queue.clear state.tilelru;
2122 load state.layout;
2125 let stateh h =
2126 let h = truncate (float h*.conf.zoom) in
2127 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2128 h - d
2131 let opendoc path password =
2132 state.path <- path;
2133 state.password <- password;
2134 state.gen <- state.gen + 1;
2135 state.docinfo <- [];
2137 flushpages ();
2138 setaalevel conf.aalevel;
2139 let titlepath =
2140 if emptystr state.origin
2141 then path
2142 else state.origin
2144 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2145 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2146 invalidate "reqlayout"
2147 (fun () ->
2148 wcmd "reqlayout %d %d %d %s\000"
2149 conf.angle (FMTE.to_int conf.fitmodel)
2150 (stateh state.winh) state.nameddest
2154 let reload () =
2155 state.anchor <- getanchor ();
2156 opendoc state.path state.password;
2159 let scalecolor c =
2160 let c = c *. conf.colorscale in
2161 (c, c, c);
2164 let scalecolor2 (r, g, b) =
2165 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2168 let docolumns = function
2169 | Csingle _ ->
2170 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2171 let rec loop pageno pdimno pdim y ph pdims =
2172 if pageno = state.pagecount
2173 then ()
2174 else
2175 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2176 match pdims with
2177 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2178 pdimno+1, pdim, rest
2179 | _ ->
2180 pdimno, pdim, pdims
2182 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2183 let y = y +
2184 (if conf.presentation
2185 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2186 else (if pageno = 0 then 0 else conf.interpagespace)
2189 a.(pageno) <- (pdimno, x, y, pdim);
2190 loop (pageno+1) pdimno pdim (y + h) h pdims
2192 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2193 conf.columns <- Csingle a;
2195 | Cmulti ((columns, coverA, coverB), _) ->
2196 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2197 let rec loop pageno pdimno pdim x y rowh pdims =
2198 let rec fixrow m = if m = pageno then () else
2199 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2200 if h < rowh
2201 then (
2202 let y = y + (rowh - h) / 2 in
2203 a.(m) <- (pdimno, x, y, pdim);
2205 fixrow (m+1)
2207 if pageno = state.pagecount
2208 then fixrow (((pageno - 1) / columns) * columns)
2209 else
2210 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2211 match pdims with
2212 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2213 pdimno+1, pdim, rest
2214 | _ ->
2215 pdimno, pdim, pdims
2217 let x, y, rowh' =
2218 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2219 then (
2220 let x = (wadjsb state.winw - w) / 2 in
2221 let ips =
2222 if conf.presentation then calcips h else conf.interpagespace in
2223 x, y + ips + rowh, h
2225 else (
2226 if (pageno - coverA) mod columns = 0
2227 then (
2228 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2229 let y =
2230 if conf.presentation
2231 then
2232 let ips = calcips h in
2233 y + (if pageno = 0 then 0 else calcips rowh + ips)
2234 else
2235 y + (if pageno = 0 then 0 else conf.interpagespace)
2237 x, y + rowh, h
2239 else x, y, max rowh h
2242 let y =
2243 if pageno > 1 && (pageno - coverA) mod columns = 0
2244 then (
2245 let y =
2246 if pageno = columns && conf.presentation
2247 then (
2248 let ips = calcips rowh in
2249 for i = 0 to pred columns
2251 let (pdimno, x, y, pdim) = a.(i) in
2252 a.(i) <- (pdimno, x, y+ips, pdim)
2253 done;
2254 y+ips;
2256 else y
2258 fixrow (pageno - columns);
2261 else y
2263 a.(pageno) <- (pdimno, x, y, pdim);
2264 let x = x + w + xoff*2 + conf.interpagespace in
2265 loop (pageno+1) pdimno pdim x y rowh' pdims
2267 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2268 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2270 | Csplit (c, _) ->
2271 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2272 let rec loop pageno pdimno pdim y pdims =
2273 if pageno = state.pagecount
2274 then ()
2275 else
2276 let pdimno, ((_, w, h, _) as pdim), pdims =
2277 match pdims with
2278 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2279 pdimno+1, pdim, rest
2280 | _ ->
2281 pdimno, pdim, pdims
2283 let cw = w / c in
2284 let rec loop1 n x y =
2285 if n = c then y else (
2286 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2287 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2290 let y = loop1 0 0 y in
2291 loop (pageno+1) pdimno pdim y pdims
2293 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2294 conf.columns <- Csplit (c, a);
2297 let represent () =
2298 docolumns conf.columns;
2299 state.maxy <- calcheight ();
2300 if state.reprf == noreprf
2301 then (
2302 match state.mode with
2303 | Birdseye (_, _, pageno, _, _) ->
2304 let y, h = getpageyh pageno in
2305 let top = (state.winh - h) / 2 in
2306 gotoy (max 0 (y - top))
2307 | _ -> gotoanchor state.anchor
2309 else (
2310 state.reprf ();
2311 state.reprf <- noreprf;
2315 let reshape w h =
2316 GlDraw.viewport 0 0 w h;
2317 let firsttime = state.geomcmds == firstgeomcmds in
2318 if not firsttime && nogeomcmds state.geomcmds
2319 then state.anchor <- getanchor ();
2321 state.winw <- w;
2322 let w = wadjsb (truncate (float w *. conf.zoom)) in
2323 let w = max w 2 in
2324 state.winh <- h;
2325 setfontsize fstate.fontsize;
2326 GlMat.mode `modelview;
2327 GlMat.load_identity ();
2329 GlMat.mode `projection;
2330 GlMat.load_identity ();
2331 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2332 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2333 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2335 let relx =
2336 if conf.zoom <= 1.0
2337 then 0.0
2338 else float state.x /. float state.w
2340 invalidate "geometry"
2341 (fun () ->
2342 state.w <- w;
2343 if not firsttime
2344 then state.x <- truncate (relx *. float w);
2345 let w =
2346 match conf.columns with
2347 | Csingle _ -> w
2348 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2349 | Csplit (c, _) -> w * c
2351 wcmd "geometry %d %d %d"
2352 w (stateh h) (FMTE.to_int conf.fitmodel)
2356 let enttext () =
2357 let len = String.length state.text in
2358 let drawstring s =
2359 let hscrollh =
2360 match state.mode with
2361 | Textentry _ | View | LinkNav _ ->
2362 let h, _, _ = state.uioh#scrollpw in
2364 | _ -> 0
2366 let rect x w =
2367 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2368 (x+.w) (float (state.winh - hscrollh))
2371 let w = float (wadjsb state.winw - 1) in
2372 if state.progress >= 0.0 && state.progress < 1.0
2373 then (
2374 GlDraw.color (0.3, 0.3, 0.3);
2375 let w1 = w *. state.progress in
2376 rect 0.0 w1;
2377 GlDraw.color (0.0, 0.0, 0.0);
2378 rect w1 (w-.w1)
2380 else (
2381 GlDraw.color (0.0, 0.0, 0.0);
2382 rect 0.0 w;
2385 GlDraw.color (1.0, 1.0, 1.0);
2386 drawstring fstate.fontsize
2387 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2389 let s =
2390 match state.mode with
2391 | Textentry ((prefix, text, _, _, _, _), _) ->
2392 let s =
2393 if len > 0
2394 then
2395 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2396 else
2397 Printf.sprintf "%s%s_" prefix text
2401 | _ -> state.text
2403 let s =
2404 if state.newerrmsgs
2405 then (
2406 if not (istextentry state.mode) && state.uioh#eformsgs
2407 then
2408 let s1 = "(press 'e' to review error messasges)" in
2409 if nonemptystr s then s ^ " " ^ s1 else s1
2410 else s
2412 else s
2414 if nonemptystr s
2415 then drawstring s
2418 let gctiles () =
2419 let len = Queue.length state.tilelru in
2420 let layout = lazy (
2421 match state.throttle with
2422 | None ->
2423 if conf.preload
2424 then preloadlayout state.y
2425 else state.layout
2426 | Some (layout, _, _) ->
2427 layout
2428 ) in
2429 let rec loop qpos =
2430 if state.memused <= conf.memlimit
2431 then ()
2432 else (
2433 if qpos < len
2434 then
2435 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2436 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2437 let (_, pw, ph, _) = getpagedim n in
2439 gen = state.gen
2440 && colorspace = conf.colorspace
2441 && angle = conf.angle
2442 && pagew = pw
2443 && pageh = ph
2444 && (
2445 let x = col*conf.tilew
2446 and y = row*conf.tileh in
2447 tilevisible (Lazy.force_val layout) n x y
2449 then Queue.push lruitem state.tilelru
2450 else (
2451 freepbo p;
2452 wcmd "freetile %s" p;
2453 state.memused <- state.memused - s;
2454 state.uioh#infochanged Memused;
2455 Hashtbl.remove state.tilemap k;
2457 loop (qpos+1)
2460 loop 0
2463 let logcurrently = function
2464 | Idle -> dolog "Idle"
2465 | Loading (l, gen) ->
2466 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2467 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2468 dolog
2469 "Tiling %d[%d,%d] page=%s cs=%s angle"
2470 l.pageno col row pageopaque
2471 (CSTE.to_string colorspace)
2473 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2474 angle gen conf.angle state.gen
2475 tilew tileh
2476 conf.tilew conf.tileh
2478 | Outlining _ ->
2479 dolog "outlining"
2482 let splitatspace =
2483 let r = Str.regexp " " in
2484 fun s -> Str.bounded_split r s 2;
2487 let onpagerect pageno f =
2488 let b =
2489 match conf.columns with
2490 | Cmulti (_, b) -> b
2491 | Csingle b -> b
2492 | Csplit (_, b) -> b
2494 if pageno >= 0 && pageno < Array.length b
2495 then
2496 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2497 f w h
2500 let gotopagexy1 pageno x y =
2501 let _,w1,h1,leftx = getpagedim pageno in
2502 let top = y /. (float h1) in
2503 let left = x /. (float w1) in
2504 let py, w, h = getpageywh pageno in
2505 let wh = state.winh - hscrollh () in
2506 let x = left *. (float w) in
2507 let x = leftx + state.x + truncate x in
2508 let sx =
2509 if x < 0 || x >= wadjsb state.winw
2510 then state.x - x
2511 else state.x
2513 let pdy = truncate (top *. float h) in
2514 let y' = py + pdy in
2515 let dy = y' - state.y in
2516 let sy =
2517 if x != state.x || not (dy > 0 && dy < wh)
2518 then (
2519 if conf.presentation
2520 then
2521 if abs (py - y') > wh
2522 then y'
2523 else py
2524 else y';
2526 else state.y
2528 if state.x != sx || state.y != sy
2529 then (
2530 let x, y =
2531 if !wtmode
2532 then (
2533 let ww = wadjsb state.winw in
2534 let qx = sx / ww
2535 and qy = pdy / wh in
2536 let x = qx * ww
2537 and y = py + qy * wh in
2538 let x = if -x + ww > w1 then -(w1-ww) else x
2539 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2540 let y =
2541 if conf.presentation
2542 then
2543 if abs (py - y') > wh
2544 then y'
2545 else py
2546 else y';
2548 (x, y)
2550 else (sx, sy)
2552 state.x <- x;
2553 gotoy_and_clear_text y;
2555 else gotoy_and_clear_text state.y;
2558 let gotopagexy pageno x y =
2559 match state.mode with
2560 | Birdseye _ -> gotopage pageno 0.0
2561 | _ -> gotopagexy1 pageno x y
2564 let act cmds =
2565 (* dolog "%S" cmds; *)
2566 let cl = splitatspace cmds in
2567 let scan s fmt f =
2568 try Scanf.sscanf s fmt f
2569 with exn ->
2570 dolog "error processing '%S': %s" cmds (exntos exn);
2571 exit 1
2573 let addoutline outline =
2574 match state.currently with
2575 | Outlining outlines ->
2576 state.currently <- Outlining (outline :: outlines)
2577 | Idle -> state.currently <- Outlining [outline]
2578 | currently ->
2579 dolog "invalid outlining state";
2580 logcurrently currently
2582 match cl with
2583 | "clear" :: [] ->
2584 state.uioh#infochanged Pdim;
2585 state.pdims <- [];
2587 | "clearrects" :: [] ->
2588 state.rects <- state.rects1;
2589 G.postRedisplay "clearrects";
2591 | "continue" :: args :: [] ->
2592 let n = scan args "%u" (fun n -> n) in
2593 state.pagecount <- n;
2594 begin match state.currently with
2595 | Outlining l ->
2596 state.currently <- Idle;
2597 state.outlines <- Array.of_list (List.rev l)
2598 | _ -> ()
2599 end;
2601 let cur, cmds = state.geomcmds in
2602 if emptystr cur
2603 then failwith "umpossible";
2605 begin match List.rev cmds with
2606 | [] ->
2607 state.geomcmds <- "", [];
2608 represent ();
2609 | (s, f) :: rest ->
2610 f ();
2611 state.geomcmds <- s, List.rev rest;
2612 end;
2613 if conf.maxwait = None && not !wtmode
2614 then G.postRedisplay "continue";
2616 | "title" :: args :: [] ->
2617 Wsi.settitle args
2619 | "msg" :: args :: [] ->
2620 showtext ' ' args
2622 | "vmsg" :: args :: [] ->
2623 if conf.verbose
2624 then showtext ' ' args
2626 | "emsg" :: args :: [] ->
2627 Buffer.add_string state.errmsgs args;
2628 state.newerrmsgs <- true;
2629 G.postRedisplay "error message"
2631 | "progress" :: args :: [] ->
2632 let progress, text =
2633 scan args "%f %n"
2634 (fun f pos ->
2635 f, String.sub args pos (String.length args - pos))
2637 state.text <- text;
2638 state.progress <- progress;
2639 G.postRedisplay "progress"
2641 | "firstmatch" :: args :: [] ->
2642 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2643 scan args "%u %d %f %f %f %f %f %f %f %f"
2644 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2645 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2647 let y = (getpagey pageno) + truncate y0 in
2648 addnav ();
2649 gotoy y;
2650 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2652 | "match" :: args :: [] ->
2653 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2654 scan args "%u %d %f %f %f %f %f %f %f %f"
2655 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2656 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2658 state.rects1 <-
2659 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2661 | "page" :: args :: [] ->
2662 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2663 begin match state.currently with
2664 | Loading (l, gen) ->
2665 vlog "page %d took %f sec" l.pageno t;
2666 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2667 begin match state.throttle with
2668 | None ->
2669 let preloadedpages =
2670 if conf.preload
2671 then preloadlayout state.y
2672 else state.layout
2674 let evict () =
2675 let set =
2676 List.fold_left (fun s l -> IntSet.add l.pageno s)
2677 IntSet.empty preloadedpages
2679 let evictedpages =
2680 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2681 if not (IntSet.mem pageno set)
2682 then (
2683 wcmd "freepage %s" opaque;
2684 key :: accu
2686 else accu
2687 ) state.pagemap []
2689 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2691 evict ();
2692 state.currently <- Idle;
2693 if gen = state.gen
2694 then (
2695 tilepage l.pageno pageopaque state.layout;
2696 load state.layout;
2697 load preloadedpages;
2698 if pagevisible state.layout l.pageno
2699 && layoutready state.layout
2700 then G.postRedisplay "page";
2703 | Some (layout, _, _) ->
2704 state.currently <- Idle;
2705 tilepage l.pageno pageopaque layout;
2706 load state.layout
2707 end;
2709 | _ ->
2710 dolog "Inconsistent loading state";
2711 logcurrently state.currently;
2712 exit 1
2715 | "tile" :: args :: [] ->
2716 let (x, y, opaque, size, t) =
2717 scan args "%u %u %s %u %f"
2718 (fun x y p size t -> (x, y, p, size, t))
2720 begin match state.currently with
2721 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2722 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2724 unmappbo opaque;
2725 if tilew != conf.tilew || tileh != conf.tileh
2726 then (
2727 wcmd "freetile %s" opaque;
2728 state.currently <- Idle;
2729 load state.layout;
2731 else (
2732 puttileopaque l col row gen cs angle opaque size t;
2733 state.memused <- state.memused + size;
2734 state.uioh#infochanged Memused;
2735 gctiles ();
2736 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2737 opaque, size) state.tilelru;
2739 let layout =
2740 match state.throttle with
2741 | None -> state.layout
2742 | Some (layout, _, _) -> layout
2745 state.currently <- Idle;
2746 if gen = state.gen
2747 && conf.colorspace = cs
2748 && conf.angle = angle
2749 && tilevisible layout l.pageno x y
2750 then conttiling l.pageno pageopaque;
2752 begin match state.throttle with
2753 | None ->
2754 preload state.layout;
2755 if gen = state.gen
2756 && conf.colorspace = cs
2757 && conf.angle = angle
2758 && tilevisible state.layout l.pageno x y
2759 && (not !wtmode || layoutready state.layout)
2760 then G.postRedisplay "tile nothrottle";
2762 | Some (layout, y, _) ->
2763 let ready = layoutready layout in
2764 if ready
2765 then (
2766 state.y <- y;
2767 state.layout <- layout;
2768 state.throttle <- None;
2769 G.postRedisplay "throttle";
2771 else load layout;
2772 end;
2775 | _ ->
2776 dolog "Inconsistent tiling state";
2777 logcurrently state.currently;
2778 exit 1
2781 | "pdim" :: args :: [] ->
2782 let (n, w, h, _) as pdim =
2783 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2785 let pdim =
2786 match conf.fitmodel, conf.columns with
2787 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2788 | _ -> pdim
2790 state.uioh#infochanged Pdim;
2791 state.pdims <- pdim :: state.pdims
2793 | "o" :: args :: [] ->
2794 let (l, n, t, h, pos) =
2795 scan args "%u %u %d %u %n"
2796 (fun l n t h pos -> l, n, t, h, pos)
2798 let s = String.sub args pos (String.length args - pos) in
2799 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
2801 | "ou" :: args :: [] ->
2802 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
2803 let s = String.sub args pos len in
2804 let pos2 = pos + len + 1 in
2805 let uri = String.sub args pos2 (String.length args - pos2) in
2806 addoutline (s, l, Ouri uri)
2808 | "on" :: args :: [] ->
2809 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
2810 let s = String.sub args pos (String.length args - pos) in
2811 addoutline (s, l, Onone)
2813 | "a" :: args :: [] ->
2814 let (n, l, t) =
2815 scan args "%u %d %d" (fun n l t -> n, l, t)
2817 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2819 | "info" :: args :: [] ->
2820 state.docinfo <- (1, args) :: state.docinfo
2822 | "infoend" :: [] ->
2823 state.uioh#infochanged Docinfo;
2824 state.docinfo <- List.rev state.docinfo
2826 | _ ->
2827 error "unknown cmd `%S'" cmds
2830 let onhist cb =
2831 let rc = cb.rc in
2832 let action = function
2833 | HCprev -> cbget cb ~-1
2834 | HCnext -> cbget cb 1
2835 | HCfirst -> cbget cb ~-(cb.rc)
2836 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2837 and cancel () = cb.rc <- rc
2838 in (action, cancel)
2841 let search pattern forward =
2842 match conf.columns with
2843 | Csplit _ ->
2844 showtext '!' "searching does not work properly in split columns mode"
2845 | _ ->
2846 if nonemptystr pattern
2847 then
2848 let pn, py =
2849 match state.layout with
2850 | [] -> 0, 0
2851 | l :: _ ->
2852 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2854 wcmd "search %d %d %d %d,%s\000"
2855 (btod conf.icase) pn py (btod forward) pattern;
2858 let intentry text key =
2859 let c =
2860 if key >= 32 && key < 127
2861 then Char.chr key
2862 else '\000'
2864 match c with
2865 | '0' .. '9' ->
2866 let text = addchar text c in
2867 TEcont text
2869 | _ ->
2870 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2871 TEcont text
2874 let linknentry text key =
2875 let c =
2876 if key >= 32 && key < 127
2877 then Char.chr key
2878 else '\000'
2880 match c with
2881 | 'a' .. 'z' ->
2882 let text = addchar text c in
2883 TEcont text
2885 | _ ->
2886 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2887 TEcont text
2890 let linkndone f s =
2891 if nonemptystr s
2892 then (
2893 let n =
2894 let l = String.length s in
2895 let rec loop pos n = if pos = l then n else
2896 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2897 loop (pos+1) (n*26 + m)
2898 in loop 0 0
2900 let rec loop n = function
2901 | [] -> ()
2902 | l :: rest ->
2903 match getopaque l.pageno with
2904 | None -> loop n rest
2905 | Some opaque ->
2906 let m = getlinkcount opaque in
2907 if n < m
2908 then (
2909 let under = getlink opaque n in
2910 f under
2912 else loop (n-m) rest
2914 loop n state.layout;
2918 let textentry text key =
2919 if key land 0xff00 = 0xff00
2920 then TEcont text
2921 else TEcont (text ^ toutf8 key)
2924 let reqlayout angle fitmodel =
2925 match state.throttle with
2926 | None ->
2927 if nogeomcmds state.geomcmds
2928 then state.anchor <- getanchor ();
2929 conf.angle <- angle mod 360;
2930 if conf.angle != 0
2931 then (
2932 match state.mode with
2933 | LinkNav _ -> state.mode <- View
2934 | _ -> ()
2936 conf.fitmodel <- fitmodel;
2937 invalidate "reqlayout"
2938 (fun () ->
2939 wcmd "reqlayout %d %d %d"
2940 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2942 | _ -> ()
2945 let settrim trimmargins trimfuzz =
2946 if nogeomcmds state.geomcmds
2947 then state.anchor <- getanchor ();
2948 conf.trimmargins <- trimmargins;
2949 conf.trimfuzz <- trimfuzz;
2950 let x0, y0, x1, y1 = trimfuzz in
2951 invalidate "settrim"
2952 (fun () ->
2953 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2954 flushpages ();
2957 let setzoom zoom =
2958 match state.throttle with
2959 | None ->
2960 let zoom = max 0.0001 zoom in
2961 if zoom <> conf.zoom
2962 then (
2963 state.prevzoom <- (conf.zoom, state.x);
2964 conf.zoom <- zoom;
2965 reshape state.winw state.winh;
2966 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2969 | Some (layout, y, started) ->
2970 let time =
2971 match conf.maxwait with
2972 | None -> 0.0
2973 | Some t -> t
2975 let dt = now () -. started in
2976 if dt > time
2977 then (
2978 state.y <- y;
2979 load layout;
2983 let setcolumns mode columns coverA coverB =
2984 state.prevcolumns <- Some (conf.columns, conf.zoom);
2985 if columns < 0
2986 then (
2987 if isbirdseye mode
2988 then showtext '!' "split mode doesn't work in bird's eye"
2989 else (
2990 conf.columns <- Csplit (-columns, [||]);
2991 state.x <- 0;
2992 conf.zoom <- 1.0;
2995 else (
2996 if columns < 2
2997 then (
2998 conf.columns <- Csingle [||];
2999 state.x <- 0;
3000 setzoom 1.0;
3002 else (
3003 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
3004 conf.zoom <- 1.0;
3007 reshape state.winw state.winh;
3010 let enterbirdseye () =
3011 let zoom = float conf.thumbw /. float state.winw in
3012 let birdseyepageno =
3013 let cy = state.winh / 2 in
3014 let fold = function
3015 | [] -> 0
3016 | l :: rest ->
3017 let rec fold best = function
3018 | [] -> best.pageno
3019 | l :: rest ->
3020 let d = cy - (l.pagedispy + l.pagevh/2)
3021 and dbest = cy - (best.pagedispy + best.pagevh/2) in
3022 if abs d < abs dbest
3023 then fold l rest
3024 else best.pageno
3025 in fold l rest
3027 fold state.layout
3029 state.mode <- Birdseye (
3030 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
3032 conf.zoom <- zoom;
3033 conf.presentation <- false;
3034 conf.interpagespace <- 10;
3035 conf.hlinks <- false;
3036 conf.fitmodel <- FitProportional;
3037 state.x <- 0;
3038 state.mstate <- Mnone;
3039 conf.maxwait <- None;
3040 conf.columns <- (
3041 match conf.beyecolumns with
3042 | Some c ->
3043 conf.zoom <- 1.0;
3044 Cmulti ((c, 0, 0), [||])
3045 | None -> Csingle [||]
3047 Wsi.setcursor Wsi.CURSOR_INHERIT;
3048 if conf.verbose
3049 then
3050 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3051 (100.0*.zoom)
3052 else
3053 state.text <- ""
3055 reshape state.winw state.winh;
3058 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3059 state.mode <- View;
3060 conf.zoom <- c.zoom;
3061 conf.presentation <- c.presentation;
3062 conf.interpagespace <- c.interpagespace;
3063 conf.maxwait <- c.maxwait;
3064 conf.hlinks <- c.hlinks;
3065 conf.fitmodel <- c.fitmodel;
3066 conf.beyecolumns <- (
3067 match conf.columns with
3068 | Cmulti ((c, _, _), _) -> Some c
3069 | Csingle _ -> None
3070 | Csplit _ -> failwith "leaving bird's eye split mode"
3072 conf.columns <- (
3073 match c.columns with
3074 | Cmulti (c, _) -> Cmulti (c, [||])
3075 | Csingle _ -> Csingle [||]
3076 | Csplit (c, _) -> Csplit (c, [||])
3078 if conf.verbose
3079 then
3080 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3081 (100.0*.conf.zoom)
3083 reshape state.winw state.winh;
3084 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3085 state.x <- leftx;
3088 let togglebirdseye () =
3089 match state.mode with
3090 | Birdseye vals -> leavebirdseye vals true
3091 | View -> enterbirdseye ()
3092 | _ -> ()
3095 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3096 let pageno = max 0 (pageno - incr) in
3097 let rec loop = function
3098 | [] -> gotopage1 pageno 0
3099 | l :: _ when l.pageno = pageno ->
3100 if l.pagedispy >= 0 && l.pagey = 0
3101 then G.postRedisplay "upbirdseye"
3102 else gotopage1 pageno 0
3103 | _ :: rest -> loop rest
3105 loop state.layout;
3106 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3109 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3110 let pageno = min (state.pagecount - 1) (pageno + incr) in
3111 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3112 let rec loop = function
3113 | [] ->
3114 let y, h = getpageyh pageno in
3115 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3116 gotoy (clamp dy)
3117 | l :: _ when l.pageno = pageno ->
3118 if l.pagevh != l.pageh
3119 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3120 else G.postRedisplay "downbirdseye"
3121 | _ :: rest -> loop rest
3123 loop state.layout
3126 let boundastep h step =
3127 if step < 0
3128 then bound step ~-h 0
3129 else bound step 0 h
3132 let optentry mode _ key =
3133 let btos b = if b then "on" else "off" in
3134 if key >= 32 && key < 127
3135 then
3136 let c = Char.chr key in
3137 match c with
3138 | 's' ->
3139 let ondone s =
3140 try conf.scrollstep <- int_of_string s with exc ->
3141 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3143 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3145 | 'A' ->
3146 let ondone s =
3148 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
3149 if state.autoscroll <> None
3150 then state.autoscroll <- Some conf.autoscrollstep
3151 with exc ->
3152 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3154 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3156 | 'C' ->
3157 let ondone s =
3159 let n, a, b = multicolumns_of_string s in
3160 setcolumns mode n a b;
3161 with exc ->
3162 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3164 TEswitch ("columns: ", "", None, textentry, ondone, true)
3166 | 'Z' ->
3167 let ondone s =
3169 let zoom = float (int_of_string s) /. 100.0 in
3170 setzoom zoom
3171 with exc ->
3172 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3174 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3176 | 't' ->
3177 let ondone s =
3179 conf.thumbw <- bound (int_of_string s) 2 4096;
3180 state.text <-
3181 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3182 begin match mode with
3183 | Birdseye beye ->
3184 leavebirdseye beye false;
3185 enterbirdseye ();
3186 | _ -> ();
3188 with exc ->
3189 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3191 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3193 | 'R' ->
3194 let ondone s =
3195 match try
3196 Some (int_of_string s)
3197 with exc ->
3198 state.text <- Printf.sprintf "bad integer `%s': %s"
3199 s (exntos exc);
3200 None
3201 with
3202 | Some angle -> reqlayout angle conf.fitmodel
3203 | None -> ()
3205 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3207 | 'i' ->
3208 conf.icase <- not conf.icase;
3209 TEdone ("case insensitive search " ^ (btos conf.icase))
3211 | 'p' ->
3212 conf.preload <- not conf.preload;
3213 gotoy state.y;
3214 TEdone ("preload " ^ (btos conf.preload))
3216 | 'v' ->
3217 conf.verbose <- not conf.verbose;
3218 TEdone ("verbose " ^ (btos conf.verbose))
3220 | 'd' ->
3221 conf.debug <- not conf.debug;
3222 TEdone ("debug " ^ (btos conf.debug))
3224 | 'h' ->
3225 conf.maxhfit <- not conf.maxhfit;
3226 state.maxy <- calcheight ();
3227 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3229 | 'c' ->
3230 conf.crophack <- not conf.crophack;
3231 TEdone ("crophack " ^ btos conf.crophack)
3233 | 'a' ->
3234 let s =
3235 match conf.maxwait with
3236 | None ->
3237 conf.maxwait <- Some infinity;
3238 "always wait for page to complete"
3239 | Some _ ->
3240 conf.maxwait <- None;
3241 "show placeholder if page is not ready"
3243 TEdone s
3245 | 'f' ->
3246 conf.underinfo <- not conf.underinfo;
3247 TEdone ("underinfo " ^ btos conf.underinfo)
3249 | 'P' ->
3250 conf.savebmarks <- not conf.savebmarks;
3251 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3253 | 'S' ->
3254 let ondone s =
3256 let pageno, py =
3257 match state.layout with
3258 | [] -> 0, 0
3259 | l :: _ ->
3260 l.pageno, l.pagey
3262 conf.interpagespace <- int_of_string s;
3263 docolumns conf.columns;
3264 state.maxy <- calcheight ();
3265 let y = getpagey pageno in
3266 gotoy (y + py)
3267 with exc ->
3268 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3270 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3272 | 'l' ->
3273 let fm =
3274 match conf.fitmodel with
3275 | FitProportional -> FitWidth
3276 | _ -> FitProportional
3278 reqlayout conf.angle fm;
3279 TEdone ("proportional display " ^ btos (fm == FitProportional))
3281 | 'T' ->
3282 settrim (not conf.trimmargins) conf.trimfuzz;
3283 TEdone ("trim margins " ^ btos conf.trimmargins)
3285 | 'I' ->
3286 conf.invert <- not conf.invert;
3287 TEdone ("invert colors " ^ btos conf.invert)
3289 | 'x' ->
3290 let ondone s =
3291 cbput state.hists.sel s;
3292 conf.selcmd <- s;
3294 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3295 textentry, ondone, true)
3297 | 'M' ->
3298 if conf.pax == None
3299 then conf.pax <- Some (ref (0.0, 0, 0))
3300 else conf.pax <- None;
3301 TEdone ("PAX " ^ btos (conf.pax != None))
3303 | _ ->
3304 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3305 TEstop
3306 else
3307 TEcont state.text
3310 class type lvsource = object
3311 method getitemcount : int
3312 method getitem : int -> (string * int)
3313 method hasaction : int -> bool
3314 method exit :
3315 uioh:uioh ->
3316 cancel:bool ->
3317 active:int ->
3318 first:int ->
3319 pan:int ->
3320 qsearch:string ->
3321 uioh option
3322 method getactive : int
3323 method getfirst : int
3324 method getqsearch : string
3325 method setqsearch : string -> unit
3326 method getpan : int
3327 end;;
3329 class virtual lvsourcebase = object
3330 val mutable m_active = 0
3331 val mutable m_first = 0
3332 val mutable m_qsearch = ""
3333 val mutable m_pan = 0
3334 method getactive = m_active
3335 method getfirst = m_first
3336 method getqsearch = m_qsearch
3337 method getpan = m_pan
3338 method setqsearch s = m_qsearch <- s
3339 end;;
3341 let withoutlastutf8 s =
3342 let len = String.length s in
3343 if len = 0
3344 then s
3345 else
3346 let rec find pos =
3347 if pos = 0
3348 then pos
3349 else
3350 let b = Char.code s.[pos] in
3351 if b land 0b11000000 = 0b11000000
3352 then pos
3353 else find (pos-1)
3355 let first =
3356 if Char.code s.[len-1] land 0x80 = 0
3357 then len-1
3358 else find (len-1)
3360 String.sub s 0 first;
3363 let textentrykeyboard
3364 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3365 let key =
3366 if key >= 0xffb0 && key <= 0xffb9
3367 then key - 0xffb0 + 48 else key
3369 let enttext te =
3370 state.mode <- Textentry (te, onleave);
3371 state.text <- "";
3372 enttext ();
3373 G.postRedisplay "textentrykeyboard enttext";
3375 let histaction cmd =
3376 match opthist with
3377 | None -> ()
3378 | Some (action, _) ->
3379 state.mode <- Textentry (
3380 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3382 G.postRedisplay "textentry histaction"
3384 match key with
3385 | 0xff08 -> (* backspace *)
3386 if emptystr text && cancelonempty
3387 then (
3388 onleave Cancel;
3389 G.postRedisplay "textentrykeyboard after cancel";
3391 else
3392 let s = withoutlastutf8 text in
3393 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3395 | 0xff0d | 0xff8d -> (* (kp) enter *)
3396 ondone text;
3397 onleave Confirm;
3398 G.postRedisplay "textentrykeyboard after confirm"
3400 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3401 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3402 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3403 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3405 | 0xff1b -> (* escape*)
3406 if emptystr text
3407 then (
3408 begin match opthist with
3409 | None -> ()
3410 | Some (_, onhistcancel) -> onhistcancel ()
3411 end;
3412 onleave Cancel;
3413 state.text <- "";
3414 G.postRedisplay "textentrykeyboard after cancel2"
3416 else (
3417 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3420 | 0xff9f | 0xffff -> () (* delete *)
3422 | _ when key != 0
3423 && key land 0xff00 != 0xff00 (* keyboard *)
3424 && key land 0xfe00 != 0xfe00 (* xkb *)
3425 && key land 0xfd00 != 0xfd00 (* 3270 *)
3427 begin match onkey text key with
3428 | TEdone text ->
3429 ondone text;
3430 onleave Confirm;
3431 G.postRedisplay "textentrykeyboard after confirm2";
3433 | TEcont text ->
3434 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3436 | TEstop ->
3437 onleave Cancel;
3438 G.postRedisplay "textentrykeyboard after cancel3"
3440 | TEswitch te ->
3441 state.mode <- Textentry (te, onleave);
3442 G.postRedisplay "textentrykeyboard switch";
3443 end;
3445 | _ ->
3446 vlog "unhandled key %s" (Wsi.keyname key)
3449 let firstof first active =
3450 if first > active || abs (first - active) > fstate.maxrows - 1
3451 then max 0 (active - (fstate.maxrows/2))
3452 else first
3455 let calcfirst first active =
3456 if active > first
3457 then
3458 let rows = active - first in
3459 if rows > fstate.maxrows then active - fstate.maxrows else first
3460 else active
3463 let scrollph y maxy =
3464 let sh = float (maxy + state.winh) /. float state.winh in
3465 let sh = float state.winh /. sh in
3466 let sh = max sh (float conf.scrollh) in
3468 let percent = float y /. float maxy in
3469 let position = (float state.winh -. sh) *. percent in
3471 let position =
3472 if position +. sh > float state.winh
3473 then float state.winh -. sh
3474 else position
3476 position, sh;
3479 let coe s = (s :> uioh);;
3481 class listview ~(source:lvsource) ~trusted ~modehash =
3482 object (self)
3483 val m_pan = source#getpan
3484 val m_first = source#getfirst
3485 val m_active = source#getactive
3486 val m_qsearch = source#getqsearch
3487 val m_prev_uioh = state.uioh
3489 method private elemunder y =
3490 let n = y / (fstate.fontsize+1) in
3491 if m_first + n < source#getitemcount
3492 then (
3493 if source#hasaction (m_first + n)
3494 then Some (m_first + n)
3495 else None
3497 else None
3499 method display =
3500 Gl.enable `blend;
3501 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3502 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3503 filledrect 0. 0. (float state.winw) (float state.winh);
3504 GlDraw.color (1., 1., 1.);
3505 Gl.enable `texture_2d;
3506 let fs = fstate.fontsize in
3507 let nfs = fs + 1 in
3508 let ww = fstate.wwidth in
3509 let tabw = 30.0*.ww in
3510 let itemcount = source#getitemcount in
3511 let rec loop row =
3512 if (row - m_first) > fstate.maxrows
3513 then ()
3514 else (
3515 if row >= 0 && row < itemcount
3516 then (
3517 let (s, level) = source#getitem row in
3518 let y = (row - m_first) * nfs in
3519 let x = 5.0 +. float (level + m_pan) *. ww in
3520 if row = m_active
3521 then (
3522 Gl.disable `texture_2d;
3523 let alpha = if source#hasaction row then 0.9 else 0.3 in
3524 GlDraw.color (1., 1., 1.) ~alpha;
3525 linerect 1. (float (y + 1))
3526 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3527 GlDraw.color (1., 1., 1.);
3528 Gl.enable `texture_2d;
3531 let drawtabularstring s =
3532 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3533 if trusted
3534 then
3535 let tabpos = try String.index s '\t' with Not_found -> -1 in
3536 if tabpos > 0
3537 then
3538 let len = String.length s - tabpos - 1 in
3539 let s1 = String.sub s 0 tabpos
3540 and s2 = String.sub s (tabpos + 1) len in
3541 let nx = drawstr x s1 in
3542 let sw = nx -. x in
3543 let x = x +. (max tabw sw) in
3544 drawstr x s2
3545 else
3546 drawstr x s
3547 else
3548 drawstr x s
3550 let _ = drawtabularstring s in
3551 loop (row+1)
3555 loop m_first;
3556 Gl.disable `blend;
3557 Gl.disable `texture_2d;
3559 method updownlevel incr =
3560 let len = source#getitemcount in
3561 let curlevel =
3562 if m_active >= 0 && m_active < len
3563 then snd (source#getitem m_active)
3564 else -1
3566 let rec flow i =
3567 if i = len then i-1 else if i = -1 then 0 else
3568 let _, l = source#getitem i in
3569 if l != curlevel then i else flow (i+incr)
3571 let active = flow m_active in
3572 let first = calcfirst m_first active in
3573 G.postRedisplay "outline updownlevel";
3574 {< m_active = active; m_first = first >}
3576 method private key1 key mask =
3577 let set1 active first qsearch =
3578 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3580 let search active pattern incr =
3581 let active = if active = -1 then m_first else active in
3582 let dosearch re =
3583 let rec loop n =
3584 if n >= 0 && n < source#getitemcount
3585 then (
3586 let s, _ = source#getitem n in
3588 (try ignore (Str.search_forward re s 0); true
3589 with Not_found -> false)
3590 then Some n
3591 else loop (n + incr)
3593 else None
3595 loop active
3598 let re = Str.regexp_case_fold pattern in
3599 dosearch re
3600 with Failure s ->
3601 state.text <- s;
3602 None
3604 let itemcount = source#getitemcount in
3605 let find start incr =
3606 let rec find i =
3607 if i = -1 || i = itemcount
3608 then -1
3609 else (
3610 if source#hasaction i
3611 then i
3612 else find (i + incr)
3615 find start
3617 let set active first =
3618 let first = bound first 0 (itemcount - fstate.maxrows) in
3619 state.text <- "";
3620 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3622 let navigate incr =
3623 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3624 let active, first =
3625 let incr1 = if incr > 0 then 1 else -1 in
3626 if isvisible m_first m_active
3627 then
3628 let next =
3629 let next = m_active + incr in
3630 let next =
3631 if next < 0 || next >= itemcount
3632 then -1
3633 else find next incr1
3635 if abs (m_active - next) > fstate.maxrows
3636 then -1
3637 else next
3639 if next = -1
3640 then
3641 let first = m_first + incr in
3642 let first = bound first 0 (itemcount - fstate.maxrows) in
3643 let next =
3644 let next = m_active + incr in
3645 let next = bound next 0 (itemcount - 1) in
3646 find next ~-incr1
3648 let active =
3649 if next = -1
3650 then m_active
3651 else (
3652 if isvisible first next
3653 then next
3654 else m_active
3657 active, first
3658 else
3659 let first = min next m_first in
3660 let first =
3661 if abs (next - first) > fstate.maxrows
3662 then first + incr
3663 else first
3665 next, first
3666 else
3667 let first = m_first + incr in
3668 let first = bound first 0 (itemcount - 1) in
3669 let active =
3670 let next = m_active + incr in
3671 let next = bound next 0 (itemcount - 1) in
3672 let next = find next incr1 in
3673 let active =
3674 if next = -1 || abs (m_active - first) > fstate.maxrows
3675 then (
3676 let active = if m_active = -1 then next else m_active in
3677 active
3679 else next
3681 if isvisible first active
3682 then active
3683 else -1
3685 active, first
3687 G.postRedisplay "listview navigate";
3688 set active first;
3690 match key with
3691 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3692 let incr = if key = 0x72 then -1 else 1 in
3693 let active, first =
3694 match search (m_active + incr) m_qsearch incr with
3695 | None ->
3696 state.text <- m_qsearch ^ " [not found]";
3697 m_active, m_first
3698 | Some active ->
3699 state.text <- m_qsearch;
3700 active, firstof m_first active
3702 G.postRedisplay "listview ctrl-r/s";
3703 set1 active first m_qsearch;
3705 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3706 if m_active >= 0 && m_active < source#getitemcount
3707 then (
3708 let s, _ = source#getitem m_active in
3709 selstring s;
3711 coe self
3713 | 0xff08 -> (* backspace *)
3714 if emptystr m_qsearch
3715 then coe self
3716 else (
3717 let qsearch = withoutlastutf8 m_qsearch in
3718 if emptystr qsearch
3719 then (
3720 state.text <- "";
3721 G.postRedisplay "listview empty qsearch";
3722 set1 m_active m_first "";
3724 else
3725 let active, first =
3726 match search m_active qsearch ~-1 with
3727 | None ->
3728 state.text <- qsearch ^ " [not found]";
3729 m_active, m_first
3730 | Some active ->
3731 state.text <- qsearch;
3732 active, firstof m_first active
3734 G.postRedisplay "listview backspace qsearch";
3735 set1 active first qsearch
3738 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3739 let pattern = m_qsearch ^ toutf8 key in
3740 let active, first =
3741 match search m_active pattern 1 with
3742 | None ->
3743 state.text <- pattern ^ " [not found]";
3744 m_active, m_first
3745 | Some active ->
3746 state.text <- pattern;
3747 active, firstof m_first active
3749 G.postRedisplay "listview qsearch add";
3750 set1 active first pattern;
3752 | 0xff1b -> (* escape *)
3753 state.text <- "";
3754 if emptystr m_qsearch
3755 then (
3756 G.postRedisplay "list view escape";
3757 begin
3758 match
3759 source#exit (coe self) true m_active m_first m_pan m_qsearch
3760 with
3761 | None -> m_prev_uioh
3762 | Some uioh -> uioh
3765 else (
3766 G.postRedisplay "list view kill qsearch";
3767 source#setqsearch "";
3768 coe {< m_qsearch = "" >}
3771 | 0xff0d | 0xff8d -> (* (kp) enter *)
3772 state.text <- "";
3773 let self = {< m_qsearch = "" >} in
3774 source#setqsearch "";
3775 let opt =
3776 G.postRedisplay "listview enter";
3777 if m_active >= 0 && m_active < source#getitemcount
3778 then (
3779 source#exit (coe self) false m_active m_first m_pan "";
3781 else (
3782 source#exit (coe self) true m_active m_first m_pan "";
3785 begin match opt with
3786 | None -> m_prev_uioh
3787 | Some uioh -> uioh
3790 | 0xff9f | 0xffff -> (* (kp) delete *)
3791 coe self
3793 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3794 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3795 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3796 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3798 | 0xff53 | 0xff98 -> (* (kp) right *)
3799 state.text <- "";
3800 G.postRedisplay "listview right";
3801 coe {< m_pan = m_pan - 1 >}
3803 | 0xff51 | 0xff96 -> (* (kp) left *)
3804 state.text <- "";
3805 G.postRedisplay "listview left";
3806 coe {< m_pan = m_pan + 1 >}
3808 | 0xff50 | 0xff95 -> (* (kp) home *)
3809 let active = find 0 1 in
3810 G.postRedisplay "listview home";
3811 set active 0;
3813 | 0xff57 | 0xff9c -> (* (kp) end *)
3814 let first = max 0 (itemcount - fstate.maxrows) in
3815 let active = find (itemcount - 1) ~-1 in
3816 G.postRedisplay "listview end";
3817 set active first;
3819 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3820 coe self
3822 | _ ->
3823 dolog "listview unknown key %#x" key; coe self
3825 method key key mask =
3826 match state.mode with
3827 | Textentry te -> textentrykeyboard key mask te; coe self
3828 | _ -> self#key1 key mask
3830 method button button down x y _ =
3831 let opt =
3832 match button with
3833 | 1 when x > state.winw - conf.scrollbw ->
3834 G.postRedisplay "listview scroll";
3835 if down
3836 then
3837 let _, position, sh = self#scrollph in
3838 if y > truncate position && y < truncate (position +. sh)
3839 then (
3840 state.mstate <- Mscrolly;
3841 Some (coe self)
3843 else
3844 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3845 let first = truncate (s *. float source#getitemcount) in
3846 let first = min source#getitemcount first in
3847 Some (coe {< m_first = first; m_active = first >})
3848 else (
3849 state.mstate <- Mnone;
3850 Some (coe self);
3852 | 1 when not down ->
3853 begin match self#elemunder y with
3854 | Some n ->
3855 G.postRedisplay "listview click";
3856 source#exit
3857 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3858 | _ ->
3859 Some (coe self)
3861 | n when (n == 4 || n == 5) && not down ->
3862 let len = source#getitemcount in
3863 let first =
3864 if n = 5 && m_first + fstate.maxrows >= len
3865 then
3866 m_first
3867 else
3868 let first = m_first + (if n == 4 then -1 else 1) in
3869 bound first 0 (len - 1)
3871 G.postRedisplay "listview wheel";
3872 Some (coe {< m_first = first >})
3873 | n when (n = 6 || n = 7) && not down ->
3874 let inc = if n = 7 then -1 else 1 in
3875 G.postRedisplay "listview hwheel";
3876 Some (coe {< m_pan = m_pan + inc >})
3877 | _ ->
3878 Some (coe self)
3880 match opt with
3881 | None -> m_prev_uioh
3882 | Some uioh -> uioh
3884 method multiclick _ x y = self#button 1 true x y
3886 method motion _ y =
3887 match state.mstate with
3888 | Mscrolly ->
3889 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3890 let first = truncate (s *. float source#getitemcount) in
3891 let first = min source#getitemcount first in
3892 G.postRedisplay "listview motion";
3893 coe {< m_first = first; m_active = first >}
3894 | _ -> coe self
3896 method pmotion x y =
3897 if x < state.winw - conf.scrollbw
3898 then
3899 let n =
3900 match self#elemunder y with
3901 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3902 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3904 let o =
3905 if n != m_active
3906 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3907 else self
3909 coe o
3910 else (
3911 Wsi.setcursor Wsi.CURSOR_INHERIT;
3912 coe self
3915 method infochanged _ = ()
3917 method scrollpw = (0, 0.0, 0.0)
3918 method scrollph =
3919 let nfs = fstate.fontsize + 1 in
3920 let y = m_first * nfs in
3921 let itemcount = source#getitemcount in
3922 let maxi = max 0 (itemcount - fstate.maxrows) in
3923 let maxy = maxi * nfs in
3924 let p, h = scrollph y maxy in
3925 conf.scrollbw, p, h
3927 method modehash = modehash
3928 method eformsgs = false
3929 end;;
3931 class outlinelistview ~source =
3932 let settext autonarrow s =
3933 if autonarrow
3934 then state.text <- "[" ^ s ^ "]"
3935 else state.text <- s
3937 object (self)
3938 inherit listview
3939 ~source:(source :> lvsource)
3940 ~trusted:false
3941 ~modehash:(findkeyhash conf "outline")
3942 as super
3944 val m_autonarrow = false
3946 method key key mask =
3947 let maxrows =
3948 if emptystr state.text
3949 then fstate.maxrows
3950 else fstate.maxrows - 2
3952 let calcfirst first active =
3953 if active > first
3954 then
3955 let rows = active - first in
3956 if rows > maxrows then active - maxrows else first
3957 else active
3959 let navigate incr =
3960 let active = m_active + incr in
3961 let active = bound active 0 (source#getitemcount - 1) in
3962 let first = calcfirst m_first active in
3963 G.postRedisplay "outline navigate";
3964 coe {< m_active = active; m_first = first >}
3966 let navscroll first =
3967 let active =
3968 let dist = m_active - first in
3969 if dist < 0
3970 then first
3971 else (
3972 if dist < maxrows
3973 then m_active
3974 else first + maxrows
3977 G.postRedisplay "outline navscroll";
3978 coe {< m_first = first; m_active = active >}
3980 let ctrl = Wsi.withctrl mask in
3981 match key with
3982 | 97 when ctrl -> (* ctrl-a *)
3983 if m_autonarrow
3984 then source#denarrow
3985 else source#narrow m_qsearch;
3986 settext (not m_autonarrow) m_qsearch;
3987 G.postRedisplay "toggle auto narrowing";
3988 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3990 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3991 settext true "";
3992 G.postRedisplay "toggle auto narrowing";
3993 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3995 | 110 when ctrl -> (* ctrl-n *)
3996 source#narrow m_qsearch;
3997 if not m_autonarrow
3998 then source#add_narrow_pattern m_qsearch;
3999 G.postRedisplay "outline ctrl-n";
4000 coe {< m_first = 0; m_active = 0 >}
4002 | 83 when ctrl -> (* ctrl-S *)
4003 let active = source#calcactive (getanchor ()) in
4004 let first = firstof m_first active in
4005 G.postRedisplay "outline ctrl-s";
4006 coe {< m_first = first; m_active = active >}
4008 | 117 when ctrl -> (* ctrl-u *)
4009 source#del_narrow_pattern;
4010 let pattern = source#renarrow in
4011 G.postRedisplay "outline ctrl-u";
4012 let text =
4013 if emptystr pattern then "" else "Narrowed to " ^ pattern
4015 settext m_autonarrow text;
4016 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4018 | 108 when ctrl -> (* ctrl-l *)
4019 let first = max 0 (m_active - (fstate.maxrows / 2)) in
4020 G.postRedisplay "outline ctrl-l";
4021 coe {< m_first = first >}
4023 | 0xff1b -> (* escape *)
4024 let o = super#key key mask in
4025 if m_autonarrow
4026 then (
4027 if nonemptystr m_qsearch
4028 then (
4029 source#add_narrow_pattern m_qsearch;
4030 settext true "";
4035 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
4036 if nonemptystr m_qsearch
4037 then source#add_narrow_pattern m_qsearch;
4038 super#key key mask
4040 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
4041 let pattern = m_qsearch ^ toutf8 key in
4042 G.postRedisplay "outlinelistview autonarrow add";
4043 source#narrow pattern;
4044 settext true pattern;
4045 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4047 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4048 if emptystr m_qsearch
4049 then coe self
4050 else
4051 let pattern = withoutlastutf8 m_qsearch in
4052 G.postRedisplay "outlinelistview autonarrow backspace";
4053 ignore (source#renarrow);
4054 source#narrow pattern;
4055 settext true pattern;
4056 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4058 | 0xff9f | 0xffff -> (* (kp) delete *)
4059 source#remove m_active;
4060 G.postRedisplay "outline delete";
4061 let active = max 0 (m_active-1) in
4062 coe {< m_first = firstof m_first active;
4063 m_active = active >}
4065 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4066 navscroll (max 0 (m_first - 1))
4068 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4069 navscroll (min (source#getitemcount - 1) (m_first + 1))
4071 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4072 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4073 | 0xff55 | 0xff9a -> (* (kp) prior *)
4074 navigate ~-(fstate.maxrows)
4075 | 0xff56 | 0xff9b -> (* (kp) next *)
4076 navigate fstate.maxrows
4078 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4079 let o =
4080 if ctrl
4081 then (
4082 G.postRedisplay "outline ctrl right";
4083 {< m_pan = m_pan + 1 >}
4085 else self#updownlevel 1
4087 coe o
4089 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4090 let o =
4091 if ctrl
4092 then (
4093 G.postRedisplay "outline ctrl left";
4094 {< m_pan = m_pan - 1 >}
4096 else self#updownlevel ~-1
4098 coe o
4100 | 0xff50 | 0xff95 -> (* (kp) home *)
4101 G.postRedisplay "outline home";
4102 coe {< m_first = 0; m_active = 0 >}
4104 | 0xff57 | 0xff9c -> (* (kp) end *)
4105 let active = source#getitemcount - 1 in
4106 let first = max 0 (active - fstate.maxrows) in
4107 G.postRedisplay "outline end";
4108 coe {< m_active = active; m_first = first >}
4110 | _ -> super#key key mask
4113 let gotounder under =
4114 let getpath filename =
4115 let path =
4116 if nonemptystr filename
4117 then
4118 if Filename.is_relative filename
4119 then
4120 let dir = Filename.dirname state.path in
4121 let dir =
4122 if Filename.is_implicit dir
4123 then Filename.concat (Sys.getcwd ()) dir
4124 else dir
4126 Filename.concat dir filename
4127 else filename
4128 else ""
4130 if Sys.file_exists path
4131 then path
4132 else ""
4134 match under with
4135 | Ulinkgoto (pageno, top) ->
4136 if pageno >= 0
4137 then (
4138 addnav ();
4139 gotopage1 pageno top;
4142 | Ulinkuri s ->
4143 gotouri s
4145 | Uremote (filename, pageno) ->
4146 let path = getpath filename in
4147 if nonemptystr path
4148 then (
4149 if conf.riani
4150 then
4151 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4152 try popen command []
4153 with exn ->
4154 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4155 flush stderr;
4156 else
4157 let anchor = getanchor () in
4158 let ranchor = state.path, state.password, anchor, state.origin in
4159 state.origin <- "";
4160 state.anchor <- (pageno, 0.0, 0.0);
4161 state.ranchors <- ranchor :: state.ranchors;
4162 opendoc path "";
4164 else showtext '!' ("Could not find " ^ filename)
4166 | Uremotedest (filename, destname) ->
4167 let path = getpath filename in
4168 if nonemptystr path
4169 then (
4170 if conf.riani
4171 then
4172 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4173 try popen command []
4174 with exn ->
4175 Printf.eprintf
4176 "failed to execute `%s': %s\n" command (exntos exn);
4177 flush stderr;
4178 else
4179 let anchor = getanchor () in
4180 let ranchor = state.path, state.password, anchor, state.origin in
4181 state.origin <- "";
4182 state.nameddest <- destname;
4183 state.ranchors <- ranchor :: state.ranchors;
4184 opendoc path "";
4186 else showtext '!' ("Could not find " ^ filename)
4188 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4191 let gotooutline (_, _, kind) =
4192 match kind with
4193 | Onone -> ()
4194 | Oanchor anchor ->
4195 let (pageno, y, _) = anchor in
4196 let y = getanchory
4197 (if conf.presentation then (pageno, y, 1.0) else anchor)
4199 gotoghyll y
4200 | Ouri uri -> gotounder (Ulinkuri uri)
4201 | Olaunch cmd -> gotounder (Ulaunch cmd)
4202 | Oremote remote -> gotounder (Uremote remote)
4203 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4206 let outlinesource usebookmarks =
4207 let empty = [||] in
4208 (object (self)
4209 inherit lvsourcebase
4210 val mutable m_items = empty
4211 val mutable m_orig_items = empty
4212 val mutable m_narrow_patterns = []
4213 val mutable m_hadremovals = false
4215 method getitemcount =
4216 Array.length m_items + (if m_hadremovals then 1 else 0)
4218 method getitem n =
4219 if n == Array.length m_items && m_hadremovals
4220 then
4221 ("[Confirm removal]", 0)
4222 else
4223 let s, n, _ = m_items.(n) in
4224 (s, n)
4226 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4227 ignore (uioh, first, qsearch);
4228 let confrimremoval = m_hadremovals && active = Array.length m_items in
4229 let items =
4230 if m_narrow_patterns = []
4231 then m_orig_items
4232 else m_items
4234 if not cancel
4235 then (
4236 if not confrimremoval
4237 then (
4238 gotooutline m_items.(active);
4239 m_items <- items;
4241 else (
4242 state.bookmarks <- Array.to_list m_items;
4243 m_orig_items <- m_items;
4246 else m_items <- items;
4247 m_pan <- pan;
4248 None
4250 method hasaction _ = true
4252 method greetmsg =
4253 if Array.length m_items != Array.length m_orig_items
4254 then
4255 let s =
4256 match m_narrow_patterns with
4257 | one :: [] -> one
4258 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4260 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4261 else ""
4263 method narrow pattern =
4264 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4265 match reopt with
4266 | None -> ()
4267 | Some re ->
4268 let rec loop accu n =
4269 if n = -1
4270 then m_items <- Array.of_list accu
4271 else
4272 let (s, _, _) as o = m_items.(n) in
4273 let accu =
4274 if (try ignore (Str.search_forward re s 0); true
4275 with Not_found -> false)
4276 then o :: accu
4277 else accu
4279 loop accu (n-1)
4281 loop [] (Array.length m_items - 1)
4283 method denarrow =
4284 m_orig_items <- (
4285 if usebookmarks
4286 then Array.of_list state.bookmarks
4287 else state.outlines
4289 m_items <- m_orig_items
4291 method remove m =
4292 if usebookmarks
4293 then
4294 if m >= 0 && m < Array.length m_items
4295 then (
4296 m_hadremovals <- true;
4297 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4298 let n = if n >= m then n+1 else n in
4299 m_items.(n)
4303 method add_narrow_pattern pattern =
4304 m_narrow_patterns <- pattern :: m_narrow_patterns
4306 method del_narrow_pattern =
4307 match m_narrow_patterns with
4308 | _ :: rest -> m_narrow_patterns <- rest
4309 | [] -> ()
4311 method renarrow =
4312 self#denarrow;
4313 match m_narrow_patterns with
4314 | pattern :: [] -> self#narrow pattern; pattern
4315 | list ->
4316 List.fold_left (fun accu pattern ->
4317 self#narrow pattern;
4318 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4320 method calcactive anchor =
4321 let rely = getanchory anchor in
4322 let rec loop n best bestd =
4323 if n = Array.length m_items
4324 then best
4325 else
4326 let _, _, kind = m_items.(n) in
4327 match kind with
4328 | Oanchor anchor ->
4329 let orely = getanchory anchor in
4330 let d = abs (orely - rely) in
4331 if d < bestd
4332 then loop (n+1) n d
4333 else loop (n+1) best bestd
4334 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
4335 loop (n+1) best bestd
4337 loop 0 ~-1 max_int
4339 method reset anchor items =
4340 m_hadremovals <- false;
4341 if m_orig_items == empty
4342 then (
4343 m_orig_items <- items;
4344 if m_narrow_patterns == []
4345 then m_items <- items;
4347 let active = self#calcactive anchor in
4348 m_active <- active;
4349 m_first <- firstof m_first active
4350 end)
4353 let enterselector usebookmarks =
4354 let source = outlinesource usebookmarks in
4355 fun errmsg ->
4356 let outlines =
4357 if usebookmarks
4358 then Array.of_list state.bookmarks
4359 else state.outlines
4361 if Array.length outlines = 0
4362 then (
4363 showtext ' ' errmsg;
4365 else (
4366 state.text <- source#greetmsg;
4367 Wsi.setcursor Wsi.CURSOR_INHERIT;
4368 let anchor = getanchor () in
4369 source#reset anchor outlines;
4370 state.uioh <- coe (new outlinelistview ~source);
4371 G.postRedisplay "enter selector";
4375 let enteroutlinemode =
4376 let f = enterselector false in
4377 fun ()-> f "Document has no outline";
4380 let enterbookmarkmode =
4381 let f = enterselector true in
4382 fun () -> f "Document has no bookmarks (yet)";
4385 let color_of_string s =
4386 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4387 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4391 let color_to_string (r, g, b) =
4392 let r = truncate (r *. 256.0)
4393 and g = truncate (g *. 256.0)
4394 and b = truncate (b *. 256.0) in
4395 Printf.sprintf "%d/%d/%d" r g b
4398 let irect_of_string s =
4399 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4402 let irect_to_string (x0,y0,x1,y1) =
4403 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4406 let makecheckers () =
4407 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4408 following to say:
4409 converted by Issac Trotts. July 25, 2002 *)
4410 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4411 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4412 let id = GlTex.gen_texture () in
4413 GlTex.bind_texture `texture_2d id;
4414 GlPix.store (`unpack_alignment 1);
4415 GlTex.image2d image;
4416 List.iter (GlTex.parameter ~target:`texture_2d)
4417 [ `mag_filter `nearest; `min_filter `nearest ];
4421 let setcheckers enabled =
4422 match state.texid with
4423 | None ->
4424 if enabled then state.texid <- Some (makecheckers ())
4426 | Some texid ->
4427 if not enabled
4428 then (
4429 GlTex.delete_texture texid;
4430 state.texid <- None;
4434 let int_of_string_with_suffix s =
4435 let l = String.length s in
4436 let s1, shift =
4437 if l > 1
4438 then
4439 let suffix = Char.lowercase s.[l-1] in
4440 match suffix with
4441 | 'k' -> String.sub s 0 (l-1), 10
4442 | 'm' -> String.sub s 0 (l-1), 20
4443 | 'g' -> String.sub s 0 (l-1), 30
4444 | _ -> s, 0
4445 else s, 0
4447 let n = int_of_string s1 in
4448 let m = n lsl shift in
4449 if m < 0 || m < n
4450 then raise (Failure "value too large")
4451 else m
4454 let string_with_suffix_of_int n =
4455 if n = 0
4456 then "0"
4457 else
4458 let units = [(30, "G"); (20, "M"); (10, "K")] in
4459 let prettyint n =
4460 let rec loop s n =
4461 let h = n mod 1000 in
4462 let n = n / 1000 in
4463 if n = 0
4464 then string_of_int h ^ s
4465 else (
4466 let s = Printf.sprintf "_%03d%s" h s in
4467 loop s n
4470 loop "" n
4472 let rec find = function
4473 | [] -> prettyint n
4474 | (shift, suffix) :: rest ->
4475 if (n land ((1 lsl shift) - 1)) = 0
4476 then prettyint (n lsr shift) ^ suffix
4477 else find rest
4479 find units
4482 let defghyllscroll = (40, 8, 32);;
4483 let fastghyllscroll = (5,1,2);;
4484 let neatghyllscroll = (10,1,9);;
4485 let ghyllscroll_of_string s =
4486 match s with
4487 | "default" -> Some defghyllscroll
4488 | "fast" -> Some (5,1,2)
4489 | "neat" -> Some (10,1,9)
4490 | "" | "none" -> None
4491 | _ ->
4492 let (n,a,b) as nab =
4493 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4494 if n <= a || n <= b || a >= b
4495 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4496 n a b;
4497 Some nab
4500 let ghyllscroll_to_string ((n, a, b) as nab) =
4501 (**) if nab = defghyllscroll then "default"
4502 else if nab = fastghyllscroll then "fast"
4503 else if nab = neatghyllscroll then "neat"
4504 else Printf.sprintf "%d,%d,%d" n a b;
4507 let describe_location () =
4508 let fn = page_of_y state.y in
4509 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4510 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4511 let percent =
4512 if maxy <= 0
4513 then 100.
4514 else (100. *. (float state.y /. float maxy))
4516 if fn = ln
4517 then
4518 Printf.sprintf "page %d of %d [%.2f%%]"
4519 (fn+1) state.pagecount percent
4520 else
4521 Printf.sprintf
4522 "pages %d-%d of %d [%.2f%%]"
4523 (fn+1) (ln+1) state.pagecount percent
4526 let setpresentationmode v =
4527 let n = page_of_y state.y in
4528 state.anchor <- (n, 0.0, 1.0);
4529 conf.presentation <- v;
4530 if conf.fitmodel = FitPage
4531 then reqlayout conf.angle conf.fitmodel;
4532 represent ();
4535 let enterinfomode =
4536 let btos b = if b then "\xe2\x88\x9a" else "" in
4537 let showextended = ref false in
4538 let leave mode = function
4539 | Confirm -> state.mode <- mode
4540 | Cancel -> state.mode <- mode in
4541 let src =
4542 (object
4543 val mutable m_first_time = true
4544 val mutable m_l = []
4545 val mutable m_a = [||]
4546 val mutable m_prev_uioh = nouioh
4547 val mutable m_prev_mode = View
4549 inherit lvsourcebase
4551 method reset prev_mode prev_uioh =
4552 m_a <- Array.of_list (List.rev m_l);
4553 m_l <- [];
4554 m_prev_mode <- prev_mode;
4555 m_prev_uioh <- prev_uioh;
4556 if m_first_time
4557 then (
4558 let rec loop n =
4559 if n >= Array.length m_a
4560 then ()
4561 else
4562 match m_a.(n) with
4563 | _, _, _, Action _ -> m_active <- n
4564 | _ -> loop (n+1)
4566 loop 0;
4567 m_first_time <- false;
4570 method int name get set =
4571 m_l <-
4572 (name, `int get, 1, Action (
4573 fun u ->
4574 let ondone s =
4575 try set (int_of_string s)
4576 with exn ->
4577 state.text <- Printf.sprintf "bad integer `%s': %s"
4578 s (exntos exn)
4580 state.text <- "";
4581 let te = name ^ ": ", "", None, intentry, ondone, true in
4582 state.mode <- Textentry (te, leave m_prev_mode);
4584 )) :: m_l
4586 method int_with_suffix name get set =
4587 m_l <-
4588 (name, `intws get, 1, Action (
4589 fun u ->
4590 let ondone s =
4591 try set (int_of_string_with_suffix s)
4592 with exn ->
4593 state.text <- Printf.sprintf "bad integer `%s': %s"
4594 s (exntos exn)
4596 state.text <- "";
4597 let te =
4598 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4600 state.mode <- Textentry (te, leave m_prev_mode);
4602 )) :: m_l
4604 method bool ?(offset=1) ?(btos=btos) name get set =
4605 m_l <-
4606 (name, `bool (btos, get), offset, Action (
4607 fun u ->
4608 let v = get () in
4609 set (not v);
4611 )) :: m_l
4613 method color name get set =
4614 m_l <-
4615 (name, `color get, 1, Action (
4616 fun u ->
4617 let invalid = (nan, nan, nan) in
4618 let ondone s =
4619 let c =
4620 try color_of_string s
4621 with exn ->
4622 state.text <- Printf.sprintf "bad color `%s': %s"
4623 s (exntos exn);
4624 invalid
4626 if c <> invalid
4627 then set c;
4629 let te = name ^ ": ", "", None, textentry, ondone, true in
4630 state.text <- color_to_string (get ());
4631 state.mode <- Textentry (te, leave m_prev_mode);
4633 )) :: m_l
4635 method string name get set =
4636 m_l <-
4637 (name, `string get, 1, Action (
4638 fun u ->
4639 let ondone s = set s in
4640 let te = name ^ ": ", "", None, textentry, ondone, true in
4641 state.mode <- Textentry (te, leave m_prev_mode);
4643 )) :: m_l
4645 method colorspace name get set =
4646 m_l <-
4647 (name, `string get, 1, Action (
4648 fun _ ->
4649 let source =
4650 (object
4651 inherit lvsourcebase
4653 initializer
4654 m_active <- CSTE.to_int conf.colorspace;
4655 m_first <- 0;
4657 method getitemcount =
4658 Array.length CSTE.names
4659 method getitem n =
4660 (CSTE.names.(n), 0)
4661 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4662 ignore (uioh, first, pan, qsearch);
4663 if not cancel then set active;
4664 None
4665 method hasaction _ = true
4666 end)
4668 state.text <- "";
4669 let modehash = findkeyhash conf "info" in
4670 coe (new listview ~source ~trusted:true ~modehash)
4671 )) :: m_l
4673 method paxmark name get set =
4674 m_l <-
4675 (name, `string get, 1, Action (
4676 fun _ ->
4677 let source =
4678 (object
4679 inherit lvsourcebase
4681 initializer
4682 m_active <- MTE.to_int conf.paxmark;
4683 m_first <- 0;
4685 method getitemcount = Array.length MTE.names
4686 method getitem n = (MTE.names.(n), 0)
4687 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4688 ignore (uioh, first, pan, qsearch);
4689 if not cancel then set active;
4690 None
4691 method hasaction _ = true
4692 end)
4694 state.text <- "";
4695 let modehash = findkeyhash conf "info" in
4696 coe (new listview ~source ~trusted:true ~modehash)
4697 )) :: m_l
4699 method fitmodel name get set =
4700 m_l <-
4701 (name, `string get, 1, Action (
4702 fun _ ->
4703 let source =
4704 (object
4705 inherit lvsourcebase
4707 initializer
4708 m_active <- FMTE.to_int conf.fitmodel;
4709 m_first <- 0;
4711 method getitemcount = Array.length FMTE.names
4712 method getitem n = (FMTE.names.(n), 0)
4713 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4714 ignore (uioh, first, pan, qsearch);
4715 if not cancel then set active;
4716 None
4717 method hasaction _ = true
4718 end)
4720 state.text <- "";
4721 let modehash = findkeyhash conf "info" in
4722 coe (new listview ~source ~trusted:true ~modehash)
4723 )) :: m_l
4725 method caption s offset =
4726 m_l <- (s, `empty, offset, Noaction) :: m_l
4728 method caption2 s f offset =
4729 m_l <- (s, `string f, offset, Noaction) :: m_l
4731 method getitemcount = Array.length m_a
4733 method getitem n =
4734 let tostr = function
4735 | `int f -> string_of_int (f ())
4736 | `intws f -> string_with_suffix_of_int (f ())
4737 | `string f -> f ()
4738 | `color f -> color_to_string (f ())
4739 | `bool (btos, f) -> btos (f ())
4740 | `empty -> ""
4742 let name, t, offset, _ = m_a.(n) in
4743 ((let s = tostr t in
4744 if nonemptystr s
4745 then Printf.sprintf "%s\t%s" name s
4746 else name),
4747 offset)
4749 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4750 let uiohopt =
4751 if not cancel
4752 then (
4753 m_qsearch <- qsearch;
4754 let uioh =
4755 match m_a.(active) with
4756 | _, _, _, Action f -> f uioh
4757 | _ -> uioh
4759 Some uioh
4761 else None
4763 m_active <- active;
4764 m_first <- first;
4765 m_pan <- pan;
4766 uiohopt
4768 method hasaction n =
4769 match m_a.(n) with
4770 | _, _, _, Action _ -> true
4771 | _ -> false
4772 end)
4774 let rec fillsrc prevmode prevuioh =
4775 let sep () = src#caption "" 0 in
4776 let colorp name get set =
4777 src#string name
4778 (fun () -> color_to_string (get ()))
4779 (fun v ->
4781 let c = color_of_string v in
4782 set c
4783 with exn ->
4784 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4787 let oldmode = state.mode in
4788 let birdseye = isbirdseye state.mode in
4790 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4792 src#bool "presentation mode"
4793 (fun () -> conf.presentation)
4794 (fun v -> setpresentationmode v);
4796 src#bool "ignore case in searches"
4797 (fun () -> conf.icase)
4798 (fun v -> conf.icase <- v);
4800 src#bool "preload"
4801 (fun () -> conf.preload)
4802 (fun v -> conf.preload <- v);
4804 src#bool "highlight links"
4805 (fun () -> conf.hlinks)
4806 (fun v -> conf.hlinks <- v);
4808 src#bool "under info"
4809 (fun () -> conf.underinfo)
4810 (fun v -> conf.underinfo <- v);
4812 src#bool "persistent bookmarks"
4813 (fun () -> conf.savebmarks)
4814 (fun v -> conf.savebmarks <- v);
4816 src#fitmodel "fit model"
4817 (fun () -> FMTE.to_string conf.fitmodel)
4818 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4820 src#bool "trim margins"
4821 (fun () -> conf.trimmargins)
4822 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4824 src#bool "persistent location"
4825 (fun () -> conf.jumpback)
4826 (fun v -> conf.jumpback <- v);
4828 sep ();
4829 src#int "inter-page space"
4830 (fun () -> conf.interpagespace)
4831 (fun n ->
4832 conf.interpagespace <- n;
4833 docolumns conf.columns;
4834 let pageno, py =
4835 match state.layout with
4836 | [] -> 0, 0
4837 | l :: _ ->
4838 l.pageno, l.pagey
4840 state.maxy <- calcheight ();
4841 let y = getpagey pageno in
4842 gotoy (y + py)
4845 src#int "page bias"
4846 (fun () -> conf.pagebias)
4847 (fun v -> conf.pagebias <- v);
4849 src#int "scroll step"
4850 (fun () -> conf.scrollstep)
4851 (fun n -> conf.scrollstep <- n);
4853 src#int "horizontal scroll step"
4854 (fun () -> conf.hscrollstep)
4855 (fun v -> conf.hscrollstep <- v);
4857 src#int "auto scroll step"
4858 (fun () ->
4859 match state.autoscroll with
4860 | Some step -> step
4861 | _ -> conf.autoscrollstep)
4862 (fun n ->
4863 let n = boundastep state.winh n in
4864 if state.autoscroll <> None
4865 then state.autoscroll <- Some n;
4866 conf.autoscrollstep <- n);
4868 src#int "zoom"
4869 (fun () -> truncate (conf.zoom *. 100.))
4870 (fun v -> setzoom ((float v) /. 100.));
4872 src#int "rotation"
4873 (fun () -> conf.angle)
4874 (fun v -> reqlayout v conf.fitmodel);
4876 src#int "scroll bar width"
4877 (fun () -> conf.scrollbw)
4878 (fun v ->
4879 conf.scrollbw <- v;
4880 reshape state.winw state.winh;
4883 src#int "scroll handle height"
4884 (fun () -> conf.scrollh)
4885 (fun v -> conf.scrollh <- v;);
4887 src#int "thumbnail width"
4888 (fun () -> conf.thumbw)
4889 (fun v ->
4890 conf.thumbw <- min 4096 v;
4891 match oldmode with
4892 | Birdseye beye ->
4893 leavebirdseye beye false;
4894 enterbirdseye ()
4895 | _ -> ()
4898 let mode = state.mode in
4899 src#string "columns"
4900 (fun () ->
4901 match conf.columns with
4902 | Csingle _ -> "1"
4903 | Cmulti (multi, _) -> multicolumns_to_string multi
4904 | Csplit (count, _) -> "-" ^ string_of_int count
4906 (fun v ->
4907 let n, a, b = multicolumns_of_string v in
4908 setcolumns mode n a b);
4910 sep ();
4911 src#caption "Pixmap cache" 0;
4912 src#int_with_suffix "size (advisory)"
4913 (fun () -> conf.memlimit)
4914 (fun v -> conf.memlimit <- v);
4916 src#caption2 "used"
4917 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4918 (string_with_suffix_of_int state.memused)
4919 (Hashtbl.length state.tilemap)) 1;
4921 sep ();
4922 src#caption "Layout" 0;
4923 src#caption2 "Dimension"
4924 (fun () ->
4925 Printf.sprintf "%dx%d (virtual %dx%d)"
4926 state.winw state.winh
4927 state.w state.maxy)
4929 if conf.debug
4930 then
4931 src#caption2 "Position" (fun () ->
4932 Printf.sprintf "%dx%d" state.x state.y
4934 else
4935 src#caption2 "Position" (fun () -> describe_location ()) 1
4938 sep ();
4939 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4940 "Save these parameters as global defaults at exit"
4941 (fun () -> conf.bedefault)
4942 (fun v -> conf.bedefault <- v)
4945 sep ();
4946 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4947 src#bool ~offset:0 ~btos "Extended parameters"
4948 (fun () -> !showextended)
4949 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4950 if !showextended
4951 then (
4952 src#bool "checkers"
4953 (fun () -> conf.checkers)
4954 (fun v -> conf.checkers <- v; setcheckers v);
4955 src#bool "update cursor"
4956 (fun () -> conf.updatecurs)
4957 (fun v -> conf.updatecurs <- v);
4958 src#bool "verbose"
4959 (fun () -> conf.verbose)
4960 (fun v -> conf.verbose <- v);
4961 src#bool "invert colors"
4962 (fun () -> conf.invert)
4963 (fun v -> conf.invert <- v);
4964 src#bool "max fit"
4965 (fun () -> conf.maxhfit)
4966 (fun v -> conf.maxhfit <- v);
4967 src#bool "redirect stderr"
4968 (fun () -> conf.redirectstderr)
4969 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4970 src#bool "pax mode"
4971 (fun () -> conf.pax != None)
4972 (fun v ->
4973 if v
4974 then conf.pax <- Some (ref (now (), 0, 0))
4975 else conf.pax <- None);
4976 src#string "uri launcher"
4977 (fun () -> conf.urilauncher)
4978 (fun v -> conf.urilauncher <- v);
4979 src#string "path launcher"
4980 (fun () -> conf.pathlauncher)
4981 (fun v -> conf.pathlauncher <- v);
4982 src#string "tile size"
4983 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4984 (fun v ->
4986 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4987 conf.tilew <- max 64 w;
4988 conf.tileh <- max 64 h;
4989 flushtiles ();
4990 with exn ->
4991 state.text <- Printf.sprintf "bad tile size `%s': %s"
4992 v (exntos exn)
4994 src#int "texture count"
4995 (fun () -> conf.texcount)
4996 (fun v ->
4997 if realloctexts v
4998 then conf.texcount <- v
4999 else showtext '!' " Failed to set texture count please retry later"
5001 src#int "slice height"
5002 (fun () -> conf.sliceheight)
5003 (fun v ->
5004 conf.sliceheight <- v;
5005 wcmd "sliceh %d" conf.sliceheight;
5007 src#int "anti-aliasing level"
5008 (fun () -> conf.aalevel)
5009 (fun v ->
5010 conf.aalevel <- bound v 0 8;
5011 state.anchor <- getanchor ();
5012 opendoc state.path state.password;
5014 src#string "page scroll scaling factor"
5015 (fun () -> string_of_float conf.pgscale)
5016 (fun v ->
5018 let s = float_of_string v in
5019 conf.pgscale <- s
5020 with exn ->
5021 state.text <- Printf.sprintf
5022 "bad page scroll scaling factor `%s': %s" v (exntos exn)
5025 src#int "ui font size"
5026 (fun () -> fstate.fontsize)
5027 (fun v -> setfontsize (bound v 5 100));
5028 src#int "hint font size"
5029 (fun () -> conf.hfsize)
5030 (fun v -> conf.hfsize <- bound v 5 100);
5031 colorp "background color"
5032 (fun () -> conf.bgcolor)
5033 (fun v -> conf.bgcolor <- v);
5034 src#bool "crop hack"
5035 (fun () -> conf.crophack)
5036 (fun v -> conf.crophack <- v);
5037 src#string "trim fuzz"
5038 (fun () -> irect_to_string conf.trimfuzz)
5039 (fun v ->
5041 conf.trimfuzz <- irect_of_string v;
5042 if conf.trimmargins
5043 then settrim true conf.trimfuzz;
5044 with exn ->
5045 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
5047 src#string "throttle"
5048 (fun () ->
5049 match conf.maxwait with
5050 | None -> "show place holder if page is not ready"
5051 | Some time ->
5052 if time = infinity
5053 then "wait for page to fully render"
5054 else
5055 "wait " ^ string_of_float time
5056 ^ " seconds before showing placeholder"
5058 (fun v ->
5060 let f = float_of_string v in
5061 if f <= 0.0
5062 then conf.maxwait <- None
5063 else conf.maxwait <- Some f
5064 with exn ->
5065 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
5067 src#string "ghyll scroll"
5068 (fun () ->
5069 match conf.ghyllscroll with
5070 | None -> ""
5071 | Some nab -> ghyllscroll_to_string nab
5073 (fun v ->
5074 try conf.ghyllscroll <- ghyllscroll_of_string v
5075 with exn ->
5076 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
5078 src#string "selection command"
5079 (fun () -> conf.selcmd)
5080 (fun v -> conf.selcmd <- v);
5081 src#string "synctex command"
5082 (fun () -> conf.stcmd)
5083 (fun v -> conf.stcmd <- v);
5084 src#string "pax command"
5085 (fun () -> conf.paxcmd)
5086 (fun v -> conf.paxcmd <- v);
5087 src#colorspace "color space"
5088 (fun () -> CSTE.to_string conf.colorspace)
5089 (fun v ->
5090 conf.colorspace <- CSTE.of_int v;
5091 wcmd "cs %d" v;
5092 load state.layout;
5094 src#paxmark "pax mark method"
5095 (fun () -> MTE.to_string conf.paxmark)
5096 (fun v -> conf.paxmark <- MTE.of_int v);
5097 if pbousable ()
5098 then
5099 src#bool "use PBO"
5100 (fun () -> conf.usepbo)
5101 (fun v -> conf.usepbo <- v);
5102 src#bool "mouse wheel scrolls pages"
5103 (fun () -> conf.wheelbypage)
5104 (fun v -> conf.wheelbypage <- v);
5105 src#bool "open remote links in a new instance"
5106 (fun () -> conf.riani)
5107 (fun v -> conf.riani <- v);
5110 sep ();
5111 src#caption "Document" 0;
5112 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
5113 src#caption2 "Pages"
5114 (fun () -> string_of_int state.pagecount) 1;
5115 src#caption2 "Dimensions"
5116 (fun () -> string_of_int (List.length state.pdims)) 1;
5117 if conf.trimmargins
5118 then (
5119 sep ();
5120 src#caption "Trimmed margins" 0;
5121 src#caption2 "Dimensions"
5122 (fun () -> string_of_int (List.length state.pdims)) 1;
5125 sep ();
5126 src#caption "OpenGL" 0;
5127 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
5128 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
5130 sep ();
5131 src#caption "Location" 0;
5132 if nonemptystr state.origin
5133 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
5134 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5136 src#reset prevmode prevuioh;
5138 fun () ->
5139 state.text <- "";
5140 let prevmode = state.mode
5141 and prevuioh = state.uioh in
5142 fillsrc prevmode prevuioh;
5143 let source = (src :> lvsource) in
5144 let modehash = findkeyhash conf "info" in
5145 state.uioh <- coe (object (self)
5146 inherit listview ~source ~trusted:true ~modehash as super
5147 val mutable m_prevmemused = 0
5148 method infochanged = function
5149 | Memused ->
5150 if m_prevmemused != state.memused
5151 then (
5152 m_prevmemused <- state.memused;
5153 G.postRedisplay "memusedchanged";
5155 | Pdim -> G.postRedisplay "pdimchanged"
5156 | Docinfo -> fillsrc prevmode prevuioh
5158 method key key mask =
5159 if not (Wsi.withctrl mask)
5160 then
5161 match key with
5162 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5163 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5164 | _ -> super#key key mask
5165 else super#key key mask
5166 end);
5167 G.postRedisplay "info";
5170 let enterhelpmode =
5171 let source =
5172 (object
5173 inherit lvsourcebase
5174 method getitemcount = Array.length state.help
5175 method getitem n =
5176 let s, l, _ = state.help.(n) in
5177 (s, l)
5179 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5180 let optuioh =
5181 if not cancel
5182 then (
5183 m_qsearch <- qsearch;
5184 match state.help.(active) with
5185 | _, _, Action f -> Some (f uioh)
5186 | _ -> Some (uioh)
5188 else None
5190 m_active <- active;
5191 m_first <- first;
5192 m_pan <- pan;
5193 optuioh
5195 method hasaction n =
5196 match state.help.(n) with
5197 | _, _, Action _ -> true
5198 | _ -> false
5200 initializer
5201 m_active <- -1
5202 end)
5203 in fun () ->
5204 let modehash = findkeyhash conf "help" in
5205 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5206 G.postRedisplay "help";
5209 let entermsgsmode =
5210 let msgsource =
5211 let re = Str.regexp "[\r\n]" in
5212 (object
5213 inherit lvsourcebase
5214 val mutable m_items = [||]
5216 method getitemcount = 1 + Array.length m_items
5218 method getitem n =
5219 if n = 0
5220 then "[Clear]", 0
5221 else m_items.(n-1), 0
5223 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5224 ignore uioh;
5225 if not cancel
5226 then (
5227 if active = 0
5228 then Buffer.clear state.errmsgs;
5229 m_qsearch <- qsearch;
5231 m_active <- active;
5232 m_first <- first;
5233 m_pan <- pan;
5234 None
5236 method hasaction n =
5237 n = 0
5239 method reset =
5240 state.newerrmsgs <- false;
5241 let l = Str.split re (Buffer.contents state.errmsgs) in
5242 m_items <- Array.of_list l
5244 initializer
5245 m_active <- 0
5246 end)
5247 in fun () ->
5248 state.text <- "";
5249 msgsource#reset;
5250 let source = (msgsource :> lvsource) in
5251 let modehash = findkeyhash conf "listview" in
5252 state.uioh <- coe (object
5253 inherit listview ~source ~trusted:false ~modehash as super
5254 method display =
5255 if state.newerrmsgs
5256 then msgsource#reset;
5257 super#display
5258 end);
5259 G.postRedisplay "msgs";
5262 let quickbookmark ?title () =
5263 match state.layout with
5264 | [] -> ()
5265 | l :: _ ->
5266 let title =
5267 match title with
5268 | None ->
5269 let sec = Unix.gettimeofday () in
5270 let tm = Unix.localtime sec in
5271 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5272 (l.pageno+1)
5273 tm.Unix.tm_mday
5274 tm.Unix.tm_mon
5275 (tm.Unix.tm_year + 1900)
5276 tm.Unix.tm_hour
5277 tm.Unix.tm_min
5278 | Some title -> title
5280 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5283 let setautoscrollspeed step goingdown =
5284 let incr = max 1 ((abs step) / 2) in
5285 let incr = if goingdown then incr else -incr in
5286 let astep = boundastep state.winh (step + incr) in
5287 state.autoscroll <- Some astep;
5290 let canpan () =
5291 match conf.columns with
5292 | Csplit _ -> true
5293 | _ -> state.x != 0 || conf.zoom > 1.0
5296 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5298 let existsinrow pageno (columns, coverA, coverB) p =
5299 let last = ((pageno - coverA) mod columns) + columns in
5300 let rec any = function
5301 | [] -> false
5302 | l :: rest ->
5303 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5304 then p l
5305 else (
5306 if not (p l)
5307 then (if l.pageno = last then false else any rest)
5308 else true
5311 any state.layout
5314 let nextpage () =
5315 match state.layout with
5316 | [] ->
5317 let pageno = page_of_y state.y in
5318 gotoghyll (getpagey (pageno+1))
5319 | l :: rest ->
5320 match conf.columns with
5321 | Csingle _ ->
5322 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5323 then
5324 let y = clamp (pgscale state.winh) in
5325 gotoghyll y
5326 else
5327 let pageno = min (l.pageno+1) (state.pagecount-1) in
5328 gotoghyll (getpagey pageno)
5329 | Cmulti ((c, _, _) as cl, _) ->
5330 if conf.presentation
5331 && (existsinrow l.pageno cl
5332 (fun l -> l.pageh > l.pagey + l.pagevh))
5333 then
5334 let y = clamp (pgscale state.winh) in
5335 gotoghyll y
5336 else
5337 let pageno = min (l.pageno+c) (state.pagecount-1) in
5338 gotoghyll (getpagey pageno)
5339 | Csplit (n, _) ->
5340 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5341 then
5342 let pagey, pageh = getpageyh l.pageno in
5343 let pagey = pagey + pageh * l.pagecol in
5344 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5345 gotoghyll (pagey + pageh + ips)
5348 let prevpage () =
5349 match state.layout with
5350 | [] ->
5351 let pageno = page_of_y state.y in
5352 gotoghyll (getpagey (pageno-1))
5353 | l :: _ ->
5354 match conf.columns with
5355 | Csingle _ ->
5356 if conf.presentation && l.pagey != 0
5357 then
5358 gotoghyll (clamp (pgscale ~-(state.winh)))
5359 else
5360 let pageno = max 0 (l.pageno-1) in
5361 gotoghyll (getpagey pageno)
5362 | Cmulti ((c, _, coverB) as cl, _) ->
5363 if conf.presentation &&
5364 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5365 then
5366 gotoghyll (clamp (pgscale ~-(state.winh)))
5367 else
5368 let decr =
5369 if l.pageno = state.pagecount - coverB
5370 then 1
5371 else c
5373 let pageno = max 0 (l.pageno-decr) in
5374 gotoghyll (getpagey pageno)
5375 | Csplit (n, _) ->
5376 let y =
5377 if l.pagecol = 0
5378 then
5379 if l.pageno = 0
5380 then l.pagey
5381 else
5382 let pageno = max 0 (l.pageno-1) in
5383 let pagey, pageh = getpageyh pageno in
5384 pagey + (n-1)*pageh
5385 else
5386 let pagey, pageh = getpageyh l.pageno in
5387 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5389 gotoghyll y
5392 let viewkeyboard key mask =
5393 let enttext te =
5394 let mode = state.mode in
5395 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5396 state.text <- "";
5397 enttext ();
5398 G.postRedisplay "view:enttext"
5400 let ctrl = Wsi.withctrl mask in
5401 let key =
5402 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5404 match key with
5405 | 81 -> (* Q *)
5406 exit 0
5408 | 0xff63 -> (* insert *)
5409 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5410 then (
5411 state.mode <- LinkNav (Ltgendir 0);
5412 gotoy state.y;
5414 else showtext '!' "Keyboard link navigation does not work under rotation"
5416 | 0xff1b | 113 -> (* escape / q *)
5417 begin match state.mstate with
5418 | Mzoomrect _ ->
5419 state.mstate <- Mnone;
5420 Wsi.setcursor Wsi.CURSOR_INHERIT;
5421 G.postRedisplay "kill zoom rect";
5422 | _ ->
5423 begin match state.mode with
5424 | LinkNav _ ->
5425 state.mode <- View;
5426 G.postRedisplay "esc leave linknav"
5427 | _ ->
5428 match state.ranchors with
5429 | [] -> raise Quit
5430 | (path, password, anchor, origin) :: rest ->
5431 state.ranchors <- rest;
5432 state.anchor <- anchor;
5433 state.origin <- origin;
5434 state.nameddest <- "";
5435 opendoc path password
5436 end;
5437 end;
5439 | 0xff08 -> (* backspace *)
5440 gotoghyll (getnav ~-1)
5442 | 111 -> (* o *)
5443 enteroutlinemode ()
5445 | 117 -> (* u *)
5446 state.rects <- [];
5447 state.text <- "";
5448 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
5449 G.postRedisplay "dehighlight";
5451 | 47 | 63 -> (* / ? *)
5452 let ondone isforw s =
5453 cbput state.hists.pat s;
5454 state.searchpattern <- s;
5455 search s isforw
5457 let s = String.create 1 in
5458 s.[0] <- Char.chr key;
5459 enttext (s, "", Some (onhist state.hists.pat),
5460 textentry, ondone (key = 47), true)
5462 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5463 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5464 setzoom (conf.zoom +. incr)
5466 | 43 | 0xffab -> (* + *)
5467 let ondone s =
5468 let n =
5469 try int_of_string s with exc ->
5470 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5471 max_int
5473 if n != max_int
5474 then (
5475 conf.pagebias <- n;
5476 state.text <- "page bias is now " ^ string_of_int n;
5479 enttext ("page bias: ", "", None, intentry, ondone, true)
5481 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5482 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5483 setzoom (max 0.01 (conf.zoom -. decr))
5485 | 45 | 0xffad -> (* - *)
5486 let ondone msg = state.text <- msg in
5487 enttext (
5488 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5489 optentry state.mode, ondone, true
5492 | 48 when ctrl -> (* ctrl-0 *)
5493 if conf.zoom = 1.0
5494 then (
5495 state.x <- 0;
5496 gotoy state.y
5498 else setzoom 1.0
5500 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5501 let cols =
5502 match conf.columns with
5503 | Csingle _ | Cmulti _ -> 1
5504 | Csplit (n, _) -> n
5506 let h = state.winh -
5507 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5509 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5510 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5511 then setzoom zoom
5513 | 51 when ctrl -> (* ctrl-3 *)
5514 let fm =
5515 match conf.fitmodel with
5516 | FitWidth -> FitProportional
5517 | FitProportional -> FitPage
5518 | FitPage -> FitWidth
5520 state.text <- "fit model: " ^ FMTE.to_string fm;
5521 reqlayout conf.angle fm
5523 | 0xffc6 -> (* f9 *)
5524 togglebirdseye ()
5526 | 57 when ctrl -> (* ctrl-9 *)
5527 togglebirdseye ()
5529 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5530 when not ctrl -> (* 0..9 *)
5531 let ondone s =
5532 let n =
5533 try int_of_string s with exc ->
5534 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5537 if n >= 0
5538 then (
5539 addnav ();
5540 cbput state.hists.pag (string_of_int n);
5541 gotopage1 (n + conf.pagebias - 1) 0;
5544 let pageentry text key =
5545 match Char.unsafe_chr key with
5546 | 'g' -> TEdone text
5547 | _ -> intentry text key
5549 let text = "x" in text.[0] <- Char.chr key;
5550 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5552 | 98 -> (* b *)
5553 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5554 reshape state.winw state.winh;
5556 | 66 -> (* B *)
5557 state.bzoom <- not state.bzoom;
5558 state.rects <- [];
5559 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5561 | 108 -> (* l *)
5562 conf.hlinks <- not conf.hlinks;
5563 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5564 G.postRedisplay "toggle highlightlinks";
5566 | 70 -> (* F *)
5567 state.glinks <- true;
5568 let mode = state.mode in
5569 state.mode <- Textentry (
5570 (":", "", None, linknentry, linkndone gotounder, false),
5571 (fun _ ->
5572 state.glinks <- false;
5573 state.mode <- mode)
5575 state.text <- "";
5576 G.postRedisplay "view:linkent(F)"
5578 | 121 -> (* y *)
5579 state.glinks <- true;
5580 let mode = state.mode in
5581 state.mode <- Textentry (
5583 ":", "", None, linknentry, linkndone (fun under ->
5584 selstring (undertext under);
5585 ), false
5587 fun _ ->
5588 state.glinks <- false;
5589 state.mode <- mode
5591 state.text <- "";
5592 G.postRedisplay "view:linkent"
5594 | 97 -> (* a *)
5595 begin match state.autoscroll with
5596 | Some step ->
5597 conf.autoscrollstep <- step;
5598 state.autoscroll <- None
5599 | None ->
5600 if conf.autoscrollstep = 0
5601 then state.autoscroll <- Some 1
5602 else state.autoscroll <- Some conf.autoscrollstep
5605 | 112 when ctrl -> (* ctrl-p *)
5606 launchpath ()
5608 | 80 -> (* P *)
5609 setpresentationmode (not conf.presentation);
5610 showtext ' ' ("presentation mode " ^
5611 if conf.presentation then "on" else "off");
5613 | 102 -> (* f *)
5614 if List.mem Wsi.Fullscreen state.winstate
5615 then Wsi.reshape conf.cwinw conf.cwinh
5616 else Wsi.fullscreen ()
5618 | 112 | 78 -> (* p|N *)
5619 search state.searchpattern false
5621 | 110 | 0xffc0 -> (* n|F3 *)
5622 search state.searchpattern true
5624 | 116 -> (* t *)
5625 begin match state.layout with
5626 | [] -> ()
5627 | l :: _ ->
5628 gotoghyll (getpagey l.pageno)
5631 | 32 -> (* space *)
5632 nextpage ()
5634 | 0xff9f | 0xffff -> (* delete *)
5635 prevpage ()
5637 | 61 -> (* = *)
5638 showtext ' ' (describe_location ());
5640 | 119 -> (* w *)
5641 begin match state.layout with
5642 | [] -> ()
5643 | l :: _ ->
5644 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5645 G.postRedisplay "w"
5648 | 39 -> (* ' *)
5649 enterbookmarkmode ()
5651 | 104 | 0xffbe -> (* h|F1 *)
5652 enterhelpmode ()
5654 | 105 -> (* i *)
5655 enterinfomode ()
5657 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5658 entermsgsmode ()
5660 | 109 -> (* m *)
5661 let ondone s =
5662 match state.layout with
5663 | l :: _ ->
5664 if nonemptystr s
5665 then
5666 state.bookmarks <-
5667 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5668 | _ -> ()
5670 enttext ("bookmark: ", "", None, textentry, ondone, true)
5672 | 126 -> (* ~ *)
5673 quickbookmark ();
5674 showtext ' ' "Quick bookmark added";
5676 | 122 -> (* z *)
5677 begin match state.layout with
5678 | l :: _ ->
5679 let rect = getpdimrect l.pagedimno in
5680 let w, h =
5681 if conf.crophack
5682 then
5683 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5684 truncate (1.2 *. (rect.(3) -. rect.(0))))
5685 else
5686 (truncate (rect.(1) -. rect.(0)),
5687 truncate (rect.(3) -. rect.(0)))
5689 let w = truncate ((float w)*.conf.zoom)
5690 and h = truncate ((float h)*.conf.zoom) in
5691 if w != 0 && h != 0
5692 then (
5693 state.anchor <- getanchor ();
5694 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5696 G.postRedisplay "z";
5698 | [] -> ()
5701 | 120 -> (* x *)
5702 state.roam ()
5703 | 60 | 62 -> (* < > *)
5704 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5706 | 91 | 93 -> (* [ ] *)
5707 conf.colorscale <-
5708 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5710 G.postRedisplay "brightness";
5712 | 99 when state.mode = View -> (* [alt-]c *)
5713 if Wsi.withalt mask
5714 then (
5715 if conf.zoom > 1.0
5716 then
5717 let m = (wadjsb state.winw - state.w) / 2 in
5718 state.x <- m;
5719 gotoy_and_clear_text state.y
5721 else
5722 let (c, a, b), z =
5723 match state.prevcolumns with
5724 | None -> (1, 0, 0), 1.0
5725 | Some (columns, z) ->
5726 let cab =
5727 match columns with
5728 | Csplit (c, _) -> -c, 0, 0
5729 | Cmulti ((c, a, b), _) -> c, a, b
5730 | Csingle _ -> 1, 0, 0
5732 cab, z
5734 setcolumns View c a b;
5735 setzoom z
5737 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5738 -> (* ctrl-shift- (kp) [up|down] *)
5739 let zoom, x = state.prevzoom in
5740 setzoom zoom;
5741 state.x <- x;
5743 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5744 begin match state.autoscroll with
5745 | None ->
5746 begin match state.mode with
5747 | Birdseye beye -> upbirdseye 1 beye
5748 | _ ->
5749 if ctrl
5750 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5751 else (
5752 if not (Wsi.withshift mask) && conf.presentation
5753 then prevpage ()
5754 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5757 | Some n ->
5758 setautoscrollspeed n false
5761 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5762 begin match state.autoscroll with
5763 | None ->
5764 begin match state.mode with
5765 | Birdseye beye -> downbirdseye 1 beye
5766 | _ ->
5767 if ctrl
5768 then gotoy_and_clear_text (clamp (state.winh/2))
5769 else (
5770 if not (Wsi.withshift mask) && conf.presentation
5771 then nextpage ()
5772 else gotoy_and_clear_text (clamp conf.scrollstep)
5775 | Some n ->
5776 setautoscrollspeed n true
5779 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5780 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5781 if canpan ()
5782 then
5783 let dx =
5784 if ctrl
5785 then state.winw / 2
5786 else conf.hscrollstep
5788 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5789 state.x <- panbound (state.x + dx);
5790 gotoy_and_clear_text state.y
5791 else (
5792 state.text <- "";
5793 G.postRedisplay "left/right"
5796 | 0xff55 | 0xff9a -> (* (kp) prior *)
5797 let y =
5798 if ctrl
5799 then
5800 match state.layout with
5801 | [] -> state.y
5802 | l :: _ -> state.y - l.pagey
5803 else
5804 clamp (pgscale (-state.winh))
5806 gotoghyll y
5808 | 0xff56 | 0xff9b -> (* (kp) next *)
5809 let y =
5810 if ctrl
5811 then
5812 match List.rev state.layout with
5813 | [] -> state.y
5814 | l :: _ -> getpagey l.pageno
5815 else
5816 clamp (pgscale state.winh)
5818 gotoghyll y
5820 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5821 gotoghyll 0
5822 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5823 gotoghyll (clamp state.maxy)
5825 | 0xff53 | 0xff98
5826 when Wsi.withalt mask -> (* alt-(kp) right *)
5827 gotoghyll (getnav 1)
5828 | 0xff51 | 0xff96
5829 when Wsi.withalt mask -> (* alt-(kp) left *)
5830 gotoghyll (getnav ~-1)
5832 | 114 -> (* r *)
5833 reload ()
5835 | 118 when conf.debug -> (* v *)
5836 state.rects <- [];
5837 List.iter (fun l ->
5838 match getopaque l.pageno with
5839 | None -> ()
5840 | Some opaque ->
5841 let x0, y0, x1, y1 = pagebbox opaque in
5842 let a,b = float x0, float y0 in
5843 let c,d = float x1, float y0 in
5844 let e,f = float x1, float y1 in
5845 let h,j = float x0, float y1 in
5846 let rect = (a,b,c,d,e,f,h,j) in
5847 debugrect rect;
5848 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5849 ) state.layout;
5850 G.postRedisplay "v";
5852 | 124 -> (* | *)
5853 let mode = state.mode in
5854 let cmd = ref "" in
5855 let onleave = function
5856 | Cancel -> state.mode <- mode
5857 | Confirm ->
5858 List.iter (fun l ->
5859 match getopaque l.pageno with
5860 | Some opaque -> pipesel opaque !cmd
5861 | None -> ()) state.layout;
5862 state.mode <- mode
5864 let ondone s = cmd := s in
5865 let te =
5866 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5868 G.postRedisplay "|";
5869 state.mode <- Textentry (te, onleave);
5871 | _ ->
5872 vlog "huh? %s" (Wsi.keyname key)
5875 let linknavkeyboard key mask linknav =
5876 let getpage pageno =
5877 let rec loop = function
5878 | [] -> None
5879 | l :: _ when l.pageno = pageno -> Some l
5880 | _ :: rest -> loop rest
5881 in loop state.layout
5883 let doexact (pageno, n) =
5884 match getopaque pageno, getpage pageno with
5885 | Some opaque, Some l ->
5886 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5887 then
5888 let under = getlink opaque n in
5889 G.postRedisplay "link gotounder";
5890 gotounder under;
5891 state.mode <- View;
5892 else
5893 let opt, dir =
5894 match key with
5895 | 0xff50 -> (* home *)
5896 Some (findlink opaque LDfirst), -1
5898 | 0xff57 -> (* end *)
5899 Some (findlink opaque LDlast), 1
5901 | 0xff51 -> (* left *)
5902 Some (findlink opaque (LDleft n)), -1
5904 | 0xff53 -> (* right *)
5905 Some (findlink opaque (LDright n)), 1
5907 | 0xff52 -> (* up *)
5908 Some (findlink opaque (LDup n)), -1
5910 | 0xff54 -> (* down *)
5911 Some (findlink opaque (LDdown n)), 1
5913 | _ -> None, 0
5915 let pwl l dir =
5916 begin match findpwl l.pageno dir with
5917 | Pwlnotfound -> ()
5918 | Pwl pageno ->
5919 let notfound dir =
5920 state.mode <- LinkNav (Ltgendir dir);
5921 let y, h = getpageyh pageno in
5922 let y =
5923 if dir < 0
5924 then y + h - state.winh
5925 else y
5927 gotoy y
5929 begin match getopaque pageno, getpage pageno with
5930 | Some opaque, Some _ ->
5931 let link =
5932 let ld = if dir > 0 then LDfirst else LDlast in
5933 findlink opaque ld
5935 begin match link with
5936 | Lfound m ->
5937 showlinktype (getlink opaque m);
5938 state.mode <- LinkNav (Ltexact (pageno, m));
5939 G.postRedisplay "linknav jpage";
5940 | _ -> notfound dir
5941 end;
5942 | _ -> notfound dir
5943 end;
5944 end;
5946 begin match opt with
5947 | Some Lnotfound -> pwl l dir;
5948 | Some (Lfound m) ->
5949 if m = n
5950 then pwl l dir
5951 else (
5952 let _, y0, _, y1 = getlinkrect opaque m in
5953 if y0 < l.pagey
5954 then gotopage1 l.pageno y0
5955 else (
5956 let d = fstate.fontsize + 1 in
5957 if y1 - l.pagey > l.pagevh - d
5958 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5959 else G.postRedisplay "linknav";
5961 showlinktype (getlink opaque m);
5962 state.mode <- LinkNav (Ltexact (l.pageno, m));
5965 | None -> viewkeyboard key mask
5966 end;
5967 | _ -> viewkeyboard key mask
5969 if key = 0xff63
5970 then (
5971 state.mode <- View;
5972 G.postRedisplay "leave linknav"
5974 else
5975 match linknav with
5976 | Ltgendir _ -> viewkeyboard key mask
5977 | Ltexact exact -> doexact exact
5980 let keyboard key mask =
5981 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5982 then wcmd "interrupt"
5983 else state.uioh <- state.uioh#key key mask
5986 let birdseyekeyboard key mask
5987 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5988 let incr =
5989 match conf.columns with
5990 | Csingle _ -> 1
5991 | Cmulti ((c, _, _), _) -> c
5992 | Csplit _ -> failwith "bird's eye split mode"
5994 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5995 match key with
5996 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5997 let y, h = getpageyh pageno in
5998 let top = (state.winh - h) / 2 in
5999 gotoy (max 0 (y - top))
6000 | 0xff0d (* enter *)
6001 | 0xff8d -> leavebirdseye beye false (* kp enter *)
6002 | 0xff1b -> leavebirdseye beye true (* escape *)
6003 | 0xff52 -> upbirdseye incr beye (* up *)
6004 | 0xff54 -> downbirdseye incr beye (* down *)
6005 | 0xff51 -> upbirdseye 1 beye (* left *)
6006 | 0xff53 -> downbirdseye 1 beye (* right *)
6008 | 0xff55 -> (* prior *)
6009 begin match state.layout with
6010 | l :: _ ->
6011 if l.pagey != 0
6012 then (
6013 state.mode <- Birdseye (
6014 oconf, leftx, l.pageno, hooverpageno, anchor
6016 gotopage1 l.pageno 0;
6018 else (
6019 let layout = layout (state.y-state.winh) (pgh state.layout) in
6020 match layout with
6021 | [] -> gotoy (clamp (-state.winh))
6022 | l :: _ ->
6023 state.mode <- Birdseye (
6024 oconf, leftx, l.pageno, hooverpageno, anchor
6026 gotopage1 l.pageno 0
6029 | [] -> gotoy (clamp (-state.winh))
6030 end;
6032 | 0xff56 -> (* next *)
6033 begin match List.rev state.layout with
6034 | l :: _ ->
6035 let layout = layout (state.y + (pgh state.layout)) state.winh in
6036 begin match layout with
6037 | [] ->
6038 let incr = l.pageh - l.pagevh in
6039 if incr = 0
6040 then (
6041 state.mode <-
6042 Birdseye (
6043 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
6045 G.postRedisplay "birdseye pagedown";
6047 else gotoy (clamp (incr + conf.interpagespace*2));
6049 | l :: _ ->
6050 state.mode <-
6051 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
6052 gotopage1 l.pageno 0;
6055 | [] -> gotoy (clamp state.winh)
6056 end;
6058 | 0xff50 -> (* home *)
6059 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
6060 gotopage1 0 0
6062 | 0xff57 -> (* end *)
6063 let pageno = state.pagecount - 1 in
6064 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
6065 if not (pagevisible state.layout pageno)
6066 then
6067 let h =
6068 match List.rev state.pdims with
6069 | [] -> state.winh
6070 | (_, _, h, _) :: _ -> h
6072 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
6073 else G.postRedisplay "birdseye end";
6074 | _ -> viewkeyboard key mask
6077 let drawpage l =
6078 let color =
6079 match state.mode with
6080 | Textentry _ -> scalecolor 0.4
6081 | LinkNav _
6082 | View -> scalecolor 1.0
6083 | Birdseye (_, _, pageno, hooverpageno, _) ->
6084 if l.pageno = hooverpageno
6085 then scalecolor 0.9
6086 else (
6087 if l.pageno = pageno
6088 then scalecolor 1.0
6089 else scalecolor 0.8
6092 drawtiles l color;
6095 let postdrawpage l linkindexbase =
6096 match getopaque l.pageno with
6097 | Some opaque ->
6098 if tileready l l.pagex l.pagey
6099 then
6100 let x = l.pagedispx - l.pagex
6101 and y = l.pagedispy - l.pagey in
6102 let hlmask =
6103 match conf.columns with
6104 | Csingle _ | Cmulti _ ->
6105 (if conf.hlinks then 1 else 0)
6106 + (if state.glinks
6107 && not (isbirdseye state.mode) then 2 else 0)
6108 | _ -> 0
6110 let s =
6111 match state.mode with
6112 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6113 | _ -> ""
6115 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6116 else 0
6117 | _ -> 0
6120 let scrollindicator () =
6121 let sbw, ph, sh = state.uioh#scrollph in
6122 let sbh, pw, sw = state.uioh#scrollpw in
6124 GlDraw.color (0.64, 0.64, 0.64);
6125 filledrect
6126 (float (state.winw - sbw)) 0.
6127 (float state.winw) (float state.winh)
6129 filledrect
6130 0. (float (state.winh - sbh))
6131 (float (wadjsb state.winw - 1)) (float state.winh)
6133 GlDraw.color (0.0, 0.0, 0.0);
6135 filledrect
6136 (float (state.winw - sbw)) ph
6137 (float state.winw) (ph +. sh)
6139 filledrect
6140 pw (float (state.winh - sbh))
6141 (pw +. sw) (float state.winh)
6145 let showsel () =
6146 match state.mstate with
6147 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6150 | Msel ((x0, y0), (x1, y1)) ->
6151 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
6152 let o0,n0,px0,py0 = onppundermouse identify x0 y0 ("", -1, 0, 0) in
6153 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 ("", -1, 0, 0) in
6154 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
6157 let showrects = function [] -> () | rects ->
6158 Gl.enable `blend;
6159 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6160 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6161 List.iter
6162 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6163 List.iter (fun l ->
6164 if l.pageno = pageno
6165 then (
6166 let dx = float (l.pagedispx - l.pagex) in
6167 let dy = float (l.pagedispy - l.pagey) in
6168 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6169 Raw.sets_float state.vraw ~pos:0
6170 [| x0+.dx; y0+.dy;
6171 x1+.dx; y1+.dy;
6172 x3+.dx; y3+.dy;
6173 x2+.dx; y2+.dy |];
6174 GlArray.vertex `two state.vraw;
6175 GlArray.draw_arrays `triangle_strip 0 4;
6177 ) state.layout
6178 ) rects
6180 Gl.disable `blend;
6183 let display () =
6184 GlClear.color (scalecolor2 conf.bgcolor);
6185 GlClear.clear [`color];
6186 List.iter drawpage state.layout;
6187 let rects =
6188 match state.mode with
6189 | LinkNav (Ltexact (pageno, linkno)) ->
6190 begin match getopaque pageno with
6191 | Some opaque ->
6192 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6193 (pageno, 5, (
6194 float x0, float y0,
6195 float x1, float y0,
6196 float x1, float y1,
6197 float x0, float y1)
6198 ) :: state.rects
6199 | None -> state.rects
6201 | _ -> state.rects
6203 showrects rects;
6204 let rec postloop linkindexbase = function
6205 | l :: rest ->
6206 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6207 postloop linkindexbase rest
6208 | [] -> ()
6210 showsel ();
6211 postloop 0 state.layout;
6212 state.uioh#display;
6213 begin match state.mstate with
6214 | Mzoomrect ((x0, y0), (x1, y1)) ->
6215 Gl.enable `blend;
6216 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6217 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6218 filledrect (float x0) (float y0) (float x1) (float y1);
6219 Gl.disable `blend;
6220 | _ -> ()
6221 end;
6222 enttext ();
6223 scrollindicator ();
6224 Wsi.swapb ();
6227 let zoomrect x y x1 y1 =
6228 let x0 = min x x1
6229 and x1 = max x x1
6230 and y0 = min y y1 in
6231 gotoy (state.y + y0);
6232 state.anchor <- getanchor ();
6233 let zoom = (float state.w) /. float (x1 - x0) in
6234 let margin =
6235 match conf.fitmodel, conf.columns with
6236 | FitPage, Csplit _ ->
6237 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6239 | _, _ ->
6240 let adjw = wadjsb state.winw in
6241 if state.w < adjw
6242 then (adjw - state.w) / 2
6243 else 0
6245 state.x <- (state.x + margin) - x0;
6246 setzoom zoom;
6247 Wsi.setcursor Wsi.CURSOR_INHERIT;
6248 state.mstate <- Mnone;
6251 let zoomblock x y =
6252 let g opaque l px py =
6253 match rectofblock opaque px py with
6254 | Some a ->
6255 let x0 = a.(0) -. 20. in
6256 let x1 = a.(1) +. 20. in
6257 let y0 = a.(2) -. 20. in
6258 let zoom = (float state.w) /. (x1 -. x0) in
6259 let pagey = getpagey l.pageno in
6260 gotoy_and_clear_text (pagey + truncate y0);
6261 state.anchor <- getanchor ();
6262 let margin = (state.w - l.pagew)/2 in
6263 state.x <- -truncate x0 - margin;
6264 setzoom zoom;
6265 None
6266 | None -> None
6268 match conf.columns with
6269 | Csplit _ ->
6270 showtext '!' "block zooming does not work properly in split columns mode"
6271 | _ -> onppundermouse g x y ()
6274 let scrollx x =
6275 let winw = wadjsb state.winw - 1 in
6276 let s = float x /. float winw in
6277 let destx = truncate (float (state.w + winw) *. s) in
6278 state.x <- winw - destx;
6279 gotoy_and_clear_text state.y;
6280 state.mstate <- Mscrollx;
6283 let scrolly y =
6284 let s = float y /. float state.winh in
6285 let desty = truncate (float (state.maxy - state.winh) *. s) in
6286 gotoy_and_clear_text desty;
6287 state.mstate <- Mscrolly;
6290 let viewmulticlick clicks x y mask =
6291 let g opaque l px py =
6292 let mark =
6293 match clicks with
6294 | 2 -> Mark_word
6295 | 3 -> Mark_line
6296 | 4 -> Mark_block
6297 | _ -> Mark_page
6299 if markunder opaque px py mark
6300 then (
6301 Some (fun () ->
6302 let dopipe cmd =
6303 match getopaque l.pageno with
6304 | None -> ()
6305 | Some opaque -> pipesel opaque cmd
6307 state.roam <- (fun () -> dopipe conf.paxcmd);
6308 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
6311 else None
6313 G.postRedisplay "viewmulticlick";
6314 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
6317 let viewmouse button down x y mask =
6318 match button with
6319 | n when (n == 4 || n == 5) && not down ->
6320 if Wsi.withctrl mask
6321 then (
6322 match state.mstate with
6323 | Mzoom (oldn, i) ->
6324 if oldn = n
6325 then (
6326 if i = 2
6327 then
6328 let incr =
6329 match n with
6330 | 5 ->
6331 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6332 | _ ->
6333 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6335 let zoom = conf.zoom -. incr in
6336 setzoom zoom;
6337 state.mstate <- Mzoom (n, 0);
6338 else
6339 state.mstate <- Mzoom (n, i+1);
6341 else state.mstate <- Mzoom (n, 0)
6343 | _ -> state.mstate <- Mzoom (n, 0)
6345 else (
6346 match state.autoscroll with
6347 | Some step -> setautoscrollspeed step (n=4)
6348 | None ->
6349 if conf.wheelbypage || conf.presentation
6350 then (
6351 if n = 4
6352 then prevpage ()
6353 else nextpage ()
6355 else
6356 let incr =
6357 if n = 4
6358 then -conf.scrollstep
6359 else conf.scrollstep
6361 let incr = incr * 2 in
6362 let y = clamp incr in
6363 gotoy_and_clear_text y
6366 | n when (n = 6 || n = 7) && not down && canpan () ->
6367 state.x <-
6368 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6369 gotoy_and_clear_text state.y
6371 | 1 when Wsi.withshift mask ->
6372 state.mstate <- Mnone;
6373 if not down
6374 then (
6375 match unproject x y with
6376 | Some (pageno, ux, uy) ->
6377 let cmd = Printf.sprintf
6378 "%s %s %d %d %d"
6379 conf.stcmd state.path pageno ux uy
6381 popen cmd []
6382 | None -> ()
6385 | 1 when Wsi.withctrl mask ->
6386 if down
6387 then (
6388 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6389 state.mstate <- Mpan (x, y)
6391 else
6392 state.mstate <- Mnone
6394 | 3 ->
6395 if down
6396 then (
6397 Wsi.setcursor Wsi.CURSOR_CYCLE;
6398 let p = (x, y) in
6399 state.mstate <- Mzoomrect (p, p)
6401 else (
6402 match state.mstate with
6403 | Mzoomrect ((x0, y0), _) ->
6404 if abs (x-x0) > 10 && abs (y - y0) > 10
6405 then zoomrect x0 y0 x y
6406 else (
6407 state.mstate <- Mnone;
6408 Wsi.setcursor Wsi.CURSOR_INHERIT;
6409 G.postRedisplay "kill accidental zoom rect";
6411 | _ ->
6412 Wsi.setcursor Wsi.CURSOR_INHERIT;
6413 state.mstate <- Mnone
6416 | 1 when x > state.winw - vscrollw () ->
6417 if down
6418 then
6419 let _, position, sh = state.uioh#scrollph in
6420 if y > truncate position && y < truncate (position +. sh)
6421 then state.mstate <- Mscrolly
6422 else scrolly y
6423 else
6424 state.mstate <- Mnone
6426 | 1 when y > state.winh - hscrollh () ->
6427 if down
6428 then
6429 let _, position, sw = state.uioh#scrollpw in
6430 if x > truncate position && x < truncate (position +. sw)
6431 then state.mstate <- Mscrollx
6432 else scrollx x
6433 else
6434 state.mstate <- Mnone
6436 | 1 when state.bzoom -> if not down then zoomblock x y
6438 | 1 ->
6439 let dest = if down then getunder x y else Unone in
6440 begin match dest with
6441 | Ulinkgoto _
6442 | Ulinkuri _
6443 | Uremote _ | Uremotedest _
6444 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6445 gotounder dest
6447 | Unone when down ->
6448 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6449 state.mstate <- Mpan (x, y);
6451 | Unone | Utext _ ->
6452 if down
6453 then (
6454 if conf.angle mod 360 = 0
6455 then (
6456 state.mstate <- Msel ((x, y), (x, y));
6457 G.postRedisplay "mouse select";
6460 else (
6461 match state.mstate with
6462 | Mnone -> ()
6464 | Mzoom _ | Mscrollx | Mscrolly ->
6465 state.mstate <- Mnone
6467 | Mzoomrect ((x0, y0), _) ->
6468 zoomrect x0 y0 x y
6470 | Mpan _ ->
6471 Wsi.setcursor Wsi.CURSOR_INHERIT;
6472 state.mstate <- Mnone
6474 | Msel ((x0, y0), (x1, y1)) ->
6475 let rec loop = function
6476 | [] -> ()
6477 | l :: rest ->
6478 let inside =
6479 let a0 = l.pagedispy in
6480 let a1 = a0 + l.pagevh in
6481 let b0 = l.pagedispx in
6482 let b1 = b0 + l.pagevw in
6483 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6484 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6486 if inside
6487 then
6488 match getopaque l.pageno with
6489 | Some opaque ->
6490 let dosel cmd () =
6491 match Ne.res Unix.pipe with
6492 | Ne.Exn exn ->
6493 showtext '!'
6494 (Printf.sprintf
6495 "can not create sel pipe: %s"
6496 (exntos exn));
6497 | Ne.Res (r, w) ->
6498 let doclose what fd =
6499 Ne.clo fd (fun msg ->
6500 dolog "%s close failed: %s" what msg)
6502 begin try
6503 popen cmd [r, 0; w, -1];
6504 with exn ->
6505 dolog "can not execute %S: %s"
6506 cmd (exntos exn);
6507 doclose "Msel pipe/w" w;
6508 end;
6509 copysel w opaque;
6510 G.postRedisplay "copysel";
6511 doclose "Msel pipe/r" r;
6513 dosel conf.selcmd ();
6514 state.roam <- dosel conf.paxcmd;
6515 | None -> ()
6516 else loop rest
6518 loop state.layout;
6519 Wsi.setcursor Wsi.CURSOR_INHERIT;
6520 state.mstate <- Mnone;
6524 | _ -> ()
6527 let birdseyemouse button down x y mask
6528 (conf, leftx, _, hooverpageno, anchor) =
6529 match button with
6530 | 1 when down ->
6531 let rec loop = function
6532 | [] -> ()
6533 | l :: rest ->
6534 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6535 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6536 then (
6537 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6539 else loop rest
6541 loop state.layout
6542 | 3 -> ()
6543 | _ -> viewmouse button down x y mask
6546 let uioh = object
6547 method display = ()
6549 method key key mask =
6550 begin match state.mode with
6551 | Textentry textentry -> textentrykeyboard key mask textentry
6552 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6553 | View -> viewkeyboard key mask
6554 | LinkNav linknav -> linknavkeyboard key mask linknav
6555 end;
6556 state.uioh
6558 method button button bstate x y mask =
6559 begin match state.mode with
6560 | LinkNav _
6561 | View -> viewmouse button bstate x y mask
6562 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6563 | Textentry _ -> ()
6564 end;
6565 state.uioh
6567 method multiclick clicks x y mask =
6568 begin match state.mode with
6569 | LinkNav _
6570 | View -> viewmulticlick clicks x y mask
6571 | Birdseye _
6572 | Textentry _ -> ()
6573 end;
6574 state.uioh
6576 method motion x y =
6577 begin match state.mode with
6578 | Textentry _ -> ()
6579 | View | Birdseye _ | LinkNav _ ->
6580 match state.mstate with
6581 | Mzoom _ | Mnone -> ()
6583 | Mpan (x0, y0) ->
6584 let dx = x - x0
6585 and dy = y0 - y in
6586 state.mstate <- Mpan (x, y);
6587 if canpan ()
6588 then state.x <- panbound (state.x + dx);
6589 let y = clamp dy in
6590 gotoy_and_clear_text y
6592 | Msel (a, _) ->
6593 state.mstate <- Msel (a, (x, y));
6594 G.postRedisplay "motion select";
6596 | Mscrolly ->
6597 let y = min state.winh (max 0 y) in
6598 scrolly y
6600 | Mscrollx ->
6601 let x = min state.winw (max 0 x) in
6602 scrollx x
6604 | Mzoomrect (p0, _) ->
6605 state.mstate <- Mzoomrect (p0, (x, y));
6606 G.postRedisplay "motion zoomrect";
6607 end;
6608 state.uioh
6610 method pmotion x y =
6611 begin match state.mode with
6612 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6613 let rec loop = function
6614 | [] ->
6615 if hooverpageno != -1
6616 then (
6617 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6618 G.postRedisplay "pmotion birdseye no hoover";
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 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6625 G.postRedisplay "pmotion birdseye hoover";
6627 else loop rest
6629 loop state.layout
6631 | Textentry _ -> ()
6633 | LinkNav _
6634 | View ->
6635 match state.mstate with
6636 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6638 | Mnone ->
6639 updateunder x y;
6640 match conf.pax with
6641 | None -> ()
6642 | Some r ->
6643 let past, _, _ = !r in
6644 let now = now () in
6645 let delta = now -. past in
6646 if delta > 0.01
6647 then paxunder x y
6648 else r := (now, x, y)
6649 end;
6650 state.uioh
6652 method infochanged _ = ()
6654 method scrollph =
6655 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6656 let p, h =
6657 if maxy = 0
6658 then 0.0, float state.winh
6659 else scrollph state.y maxy
6661 vscrollw (), p, h
6663 method scrollpw =
6664 let winw = wadjsb state.winw in
6665 let fwinw = float winw in
6666 let sw =
6667 let sw = fwinw /. float state.w in
6668 let sw = fwinw *. sw in
6669 max sw (float conf.scrollh)
6671 let position =
6672 let maxx = state.w + winw in
6673 let x = winw - state.x in
6674 let percent = float x /. float maxx in
6675 (fwinw -. sw) *. percent
6677 hscrollh (), position, sw
6679 method modehash =
6680 let modename =
6681 match state.mode with
6682 | LinkNav _ -> "links"
6683 | Textentry _ -> "textentry"
6684 | Birdseye _ -> "birdseye"
6685 | View -> "view"
6687 findkeyhash conf modename
6689 method eformsgs = true
6690 end;;
6692 module Config =
6693 struct
6694 open Parser
6696 let fontpath = ref "";;
6698 module KeyMap =
6699 Map.Make (struct type t = (int * int) let compare = compare end);;
6701 let unent s =
6702 let l = String.length s in
6703 let b = Buffer.create l in
6704 unent b s 0 l;
6705 Buffer.contents b;
6708 let home =
6709 try Sys.getenv "HOME"
6710 with exn ->
6711 prerr_endline
6712 ("Can not determine home directory location: " ^ exntos exn);
6716 let modifier_of_string = function
6717 | "alt" -> Wsi.altmask
6718 | "shift" -> Wsi.shiftmask
6719 | "ctrl" | "control" -> Wsi.ctrlmask
6720 | "meta" -> Wsi.metamask
6721 | _ -> 0
6724 let key_of_string =
6725 let r = Str.regexp "-" in
6726 fun s ->
6727 let elems = Str.full_split r s in
6728 let f n k m =
6729 let g s =
6730 let m1 = modifier_of_string s in
6731 if m1 = 0
6732 then (Wsi.namekey s, m)
6733 else (k, m lor m1)
6734 in function
6735 | Str.Delim s when n land 1 = 0 -> g s
6736 | Str.Text s -> g s
6737 | Str.Delim _ -> (k, m)
6739 let rec loop n k m = function
6740 | [] -> (k, m)
6741 | x :: xs ->
6742 let k, m = f n k m x in
6743 loop (n+1) k m xs
6745 loop 0 0 0 elems
6748 let keys_of_string =
6749 let r = Str.regexp "[ \t]" in
6750 fun s ->
6751 let elems = Str.split r s in
6752 List.map key_of_string elems
6755 let copykeyhashes c =
6756 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6759 let config_of c attrs =
6760 let apply c k v =
6762 match k with
6763 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6764 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6765 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6766 | "preload" -> { c with preload = bool_of_string v }
6767 | "page-bias" -> { c with pagebias = int_of_string v }
6768 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6769 | "horizontal-scroll-step" ->
6770 { c with hscrollstep = max (int_of_string v) 1 }
6771 | "auto-scroll-step" ->
6772 { c with autoscrollstep = max 0 (int_of_string v) }
6773 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6774 | "crop-hack" -> { c with crophack = bool_of_string v }
6775 | "throttle" ->
6776 let mw =
6777 match String.lowercase v with
6778 | "true" -> Some infinity
6779 | "false" -> None
6780 | f -> Some (float_of_string f)
6782 { c with maxwait = mw}
6783 | "highlight-links" -> { c with hlinks = bool_of_string v }
6784 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6785 | "vertical-margin" ->
6786 { c with interpagespace = max 0 (int_of_string v) }
6787 | "zoom" ->
6788 let zoom = float_of_string v /. 100. in
6789 let zoom = max zoom 0.0 in
6790 { c with zoom = zoom }
6791 | "presentation" -> { c with presentation = bool_of_string v }
6792 | "rotation-angle" -> { c with angle = int_of_string v }
6793 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6794 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6795 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6796 | "proportional-display" ->
6797 let fm =
6798 if bool_of_string v
6799 then FitProportional
6800 else FitWidth
6802 { c with fitmodel = fm }
6803 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6804 | "pixmap-cache-size" ->
6805 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6806 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6807 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6808 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6809 | "persistent-location" -> { c with jumpback = bool_of_string v }
6810 | "background-color" -> { c with bgcolor = color_of_string v }
6811 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6812 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6813 | "mupdf-store-size" ->
6814 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6815 | "checkers" -> { c with checkers = bool_of_string v }
6816 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6817 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6818 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6819 | "uri-launcher" -> { c with urilauncher = unent v }
6820 | "path-launcher" -> { c with pathlauncher = unent v }
6821 | "color-space" -> { c with colorspace = CSTE.of_string v }
6822 | "invert-colors" -> { c with invert = bool_of_string v }
6823 | "brightness" -> { c with colorscale = float_of_string v }
6824 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6825 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6826 | "columns" ->
6827 let (n, _, _) as nab = multicolumns_of_string v in
6828 if n < 0
6829 then { c with columns = Csplit (-n, [||]) }
6830 else { c with columns = Cmulti (nab, [||]) }
6831 | "birds-eye-columns" ->
6832 { c with beyecolumns = Some (max (int_of_string v) 2) }
6833 | "selection-command" -> { c with selcmd = unent v }
6834 | "synctex-command" -> { c with stcmd = unent v }
6835 | "pax-command" -> { c with paxcmd = unent v }
6836 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6837 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6838 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6839 | "use-pbo" -> { c with usepbo = bool_of_string v }
6840 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6841 | "horizontal-scrollbar-visible" ->
6842 let b =
6843 if bool_of_string v
6844 then c.scrollb lor scrollbhv
6845 else c.scrollb land (lnot scrollbhv)
6847 { c with scrollb = b }
6848 | "vertical-scrollbar-visible" ->
6849 let b =
6850 if bool_of_string v
6851 then c.scrollb lor scrollbvv
6852 else c.scrollb land (lnot scrollbvv)
6854 { c with scrollb = b }
6855 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6856 | "point-and-x" ->
6857 { c with pax =
6858 if bool_of_string v
6859 then Some (ref (0.0, 0, 0))
6860 else None }
6861 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6862 | _ -> c
6863 with exn ->
6864 prerr_endline ("Error processing attribute (`" ^
6865 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6868 let rec fold c = function
6869 | [] -> c
6870 | (k, v) :: rest ->
6871 let c = apply c k v in
6872 fold c rest
6874 fold { c with keyhashes = copykeyhashes c } attrs;
6877 let fromstring f pos n v d =
6878 try f v
6879 with exn ->
6880 dolog "Error processing attribute (%S=%S) at %d\n%s"
6881 n v pos (exntos exn)
6886 let bookmark_of attrs =
6887 let rec fold title page rely visy = function
6888 | ("title", v) :: rest -> fold v page rely visy rest
6889 | ("page", v) :: rest -> fold title v rely visy rest
6890 | ("rely", v) :: rest -> fold title page v visy rest
6891 | ("visy", v) :: rest -> fold title page rely v rest
6892 | _ :: rest -> fold title page rely visy rest
6893 | [] -> title, page, rely, visy
6895 fold "invalid" "0" "0" "0" attrs
6898 let doc_of attrs =
6899 let rec fold path page rely pan visy = function
6900 | ("path", v) :: rest -> fold v page rely pan visy rest
6901 | ("page", v) :: rest -> fold path v rely pan visy rest
6902 | ("rely", v) :: rest -> fold path page v pan visy rest
6903 | ("pan", v) :: rest -> fold path page rely v visy rest
6904 | ("visy", v) :: rest -> fold path page rely pan v rest
6905 | _ :: rest -> fold path page rely pan visy rest
6906 | [] -> path, page, rely, pan, visy
6908 fold "" "0" "0" "0" "0" attrs
6911 let map_of attrs =
6912 let rec fold rs ls = function
6913 | ("out", v) :: rest -> fold v ls rest
6914 | ("in", v) :: rest -> fold rs v rest
6915 | _ :: rest -> fold ls rs rest
6916 | [] -> ls, rs
6918 fold "" "" attrs
6921 let setconf dst src =
6922 dst.scrollbw <- src.scrollbw;
6923 dst.scrollh <- src.scrollh;
6924 dst.icase <- src.icase;
6925 dst.preload <- src.preload;
6926 dst.pagebias <- src.pagebias;
6927 dst.verbose <- src.verbose;
6928 dst.scrollstep <- src.scrollstep;
6929 dst.maxhfit <- src.maxhfit;
6930 dst.crophack <- src.crophack;
6931 dst.autoscrollstep <- src.autoscrollstep;
6932 dst.maxwait <- src.maxwait;
6933 dst.hlinks <- src.hlinks;
6934 dst.underinfo <- src.underinfo;
6935 dst.interpagespace <- src.interpagespace;
6936 dst.zoom <- src.zoom;
6937 dst.presentation <- src.presentation;
6938 dst.angle <- src.angle;
6939 dst.cwinw <- src.cwinw;
6940 dst.cwinh <- src.cwinh;
6941 dst.savebmarks <- src.savebmarks;
6942 dst.memlimit <- src.memlimit;
6943 dst.fitmodel <- src.fitmodel;
6944 dst.texcount <- src.texcount;
6945 dst.sliceheight <- src.sliceheight;
6946 dst.thumbw <- src.thumbw;
6947 dst.jumpback <- src.jumpback;
6948 dst.bgcolor <- src.bgcolor;
6949 dst.tilew <- src.tilew;
6950 dst.tileh <- src.tileh;
6951 dst.mustoresize <- src.mustoresize;
6952 dst.checkers <- src.checkers;
6953 dst.aalevel <- src.aalevel;
6954 dst.trimmargins <- src.trimmargins;
6955 dst.trimfuzz <- src.trimfuzz;
6956 dst.urilauncher <- src.urilauncher;
6957 dst.colorspace <- src.colorspace;
6958 dst.invert <- src.invert;
6959 dst.colorscale <- src.colorscale;
6960 dst.redirectstderr <- src.redirectstderr;
6961 dst.ghyllscroll <- src.ghyllscroll;
6962 dst.columns <- src.columns;
6963 dst.beyecolumns <- src.beyecolumns;
6964 dst.selcmd <- src.selcmd;
6965 dst.updatecurs <- src.updatecurs;
6966 dst.pathlauncher <- src.pathlauncher;
6967 dst.keyhashes <- copykeyhashes src;
6968 dst.hfsize <- src.hfsize;
6969 dst.hscrollstep <- src.hscrollstep;
6970 dst.pgscale <- src.pgscale;
6971 dst.usepbo <- src.usepbo;
6972 dst.wheelbypage <- src.wheelbypage;
6973 dst.stcmd <- src.stcmd;
6974 dst.paxcmd <- src.paxcmd;
6975 dst.scrollb <- src.scrollb;
6976 dst.riani <- src.riani;
6977 dst.paxmark <- src.paxmark;
6978 dst.pax <-
6979 if src.pax = None
6980 then None
6981 else Some ((ref (0.0, 0, 0)));
6984 let get s =
6985 let h = Hashtbl.create 10 in
6986 let dc = { defconf with angle = defconf.angle } in
6987 let rec toplevel v t spos _ =
6988 match t with
6989 | Vdata | Vcdata | Vend -> v
6990 | Vopen ("llppconfig", _, closed) ->
6991 if closed
6992 then v
6993 else { v with f = llppconfig }
6994 | Vopen _ ->
6995 error "unexpected subelement at top level" s spos
6996 | Vclose _ -> error "unexpected close at top level" s spos
6998 and llppconfig v t spos _ =
6999 match t with
7000 | Vdata | Vcdata -> v
7001 | Vend -> error "unexpected end of input in llppconfig" s spos
7002 | Vopen ("defaults", attrs, closed) ->
7003 let c = config_of dc attrs in
7004 setconf dc c;
7005 if closed
7006 then v
7007 else { v with f = defaults }
7009 | Vopen ("ui-font", attrs, closed) ->
7010 let rec getsize size = function
7011 | [] -> size
7012 | ("size", v) :: rest ->
7013 let size =
7014 fromstring int_of_string spos "size" v fstate.fontsize in
7015 getsize size rest
7016 | l -> getsize size l
7018 fstate.fontsize <- getsize fstate.fontsize attrs;
7019 if closed
7020 then v
7021 else { v with f = uifont (Buffer.create 10) }
7023 | Vopen ("doc", attrs, closed) ->
7024 let pathent, spage, srely, span, svisy = doc_of attrs in
7025 let path = unent pathent
7026 and pageno = fromstring int_of_string spos "page" spage 0
7027 and rely = fromstring float_of_string spos "rely" srely 0.0
7028 and pan = fromstring int_of_string spos "pan" span 0
7029 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7030 let c = config_of dc attrs in
7031 let anchor = (pageno, rely, visy) in
7032 if closed
7033 then (Hashtbl.add h path (c, [], pan, anchor); v)
7034 else { v with f = doc path pan anchor c [] }
7036 | Vopen _ ->
7037 error "unexpected subelement in llppconfig" s spos
7039 | Vclose "llppconfig" -> { v with f = toplevel }
7040 | Vclose _ -> error "unexpected close in llppconfig" s spos
7042 and defaults v t spos _ =
7043 match t with
7044 | Vdata | Vcdata -> v
7045 | Vend -> error "unexpected end of input in defaults" s spos
7046 | Vopen ("keymap", attrs, closed) ->
7047 let modename =
7048 try List.assoc "mode" attrs
7049 with Not_found -> "global" in
7050 if closed
7051 then v
7052 else
7053 let ret keymap =
7054 let h = findkeyhash dc modename in
7055 KeyMap.iter (Hashtbl.replace h) keymap;
7056 defaults
7058 { v with f = pkeymap ret KeyMap.empty }
7060 | Vopen (_, _, _) ->
7061 error "unexpected subelement in defaults" s spos
7063 | Vclose "defaults" ->
7064 { v with f = llppconfig }
7066 | Vclose _ -> error "unexpected close in defaults" s spos
7068 and uifont b v t spos epos =
7069 match t with
7070 | Vdata | Vcdata ->
7071 Buffer.add_substring b s spos (epos - spos);
7073 | Vopen (_, _, _) ->
7074 error "unexpected subelement in ui-font" s spos
7075 | Vclose "ui-font" ->
7076 if emptystr !fontpath
7077 then fontpath := Buffer.contents b;
7078 { v with f = llppconfig }
7079 | Vclose _ -> error "unexpected close in ui-font" s spos
7080 | Vend -> error "unexpected end of input in ui-font" s spos
7082 and doc path pan anchor c bookmarks v t spos _ =
7083 match t with
7084 | Vdata | Vcdata -> v
7085 | Vend -> error "unexpected end of input in doc" s spos
7086 | Vopen ("bookmarks", _, closed) ->
7087 if closed
7088 then v
7089 else { v with f = pbookmarks path pan anchor c bookmarks }
7091 | Vopen ("keymap", attrs, closed) ->
7092 let modename =
7093 try List.assoc "mode" attrs
7094 with Not_found -> "global"
7096 if closed
7097 then v
7098 else
7099 let ret keymap =
7100 let h = findkeyhash c modename in
7101 KeyMap.iter (Hashtbl.replace h) keymap;
7102 doc path pan anchor c bookmarks
7104 { v with f = pkeymap ret KeyMap.empty }
7106 | Vopen (_, _, _) ->
7107 error "unexpected subelement in doc" s spos
7109 | Vclose "doc" ->
7110 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7111 { v with f = llppconfig }
7113 | Vclose _ -> error "unexpected close in doc" s spos
7115 and pkeymap ret keymap v t spos _ =
7116 match t with
7117 | Vdata | Vcdata -> v
7118 | Vend -> error "unexpected end of input in keymap" s spos
7119 | Vopen ("map", attrs, closed) ->
7120 let r, l = map_of attrs in
7121 let kss = fromstring keys_of_string spos "in" r [] in
7122 let lss = fromstring keys_of_string spos "out" l [] in
7123 let keymap =
7124 match kss with
7125 | [] -> keymap
7126 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7127 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7129 if closed
7130 then { v with f = pkeymap ret keymap }
7131 else
7132 let f () = v in
7133 { v with f = skip "map" f }
7135 | Vopen _ ->
7136 error "unexpected subelement in keymap" s spos
7138 | Vclose "keymap" ->
7139 { v with f = ret keymap }
7141 | Vclose _ -> error "unexpected close in keymap" s spos
7143 and pbookmarks path pan anchor c bookmarks v t spos _ =
7144 match t with
7145 | Vdata | Vcdata -> v
7146 | Vend -> error "unexpected end of input in bookmarks" s spos
7147 | Vopen ("item", attrs, closed) ->
7148 let titleent, spage, srely, svisy = bookmark_of attrs in
7149 let page = fromstring int_of_string spos "page" spage 0
7150 and rely = fromstring float_of_string spos "rely" srely 0.0
7151 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7152 let bookmarks =
7153 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
7155 if closed
7156 then { v with f = pbookmarks path pan anchor c bookmarks }
7157 else
7158 let f () = v in
7159 { v with f = skip "item" f }
7161 | Vopen _ ->
7162 error "unexpected subelement in bookmarks" s spos
7164 | Vclose "bookmarks" ->
7165 { v with f = doc path pan anchor c bookmarks }
7167 | Vclose _ -> error "unexpected close in bookmarks" s spos
7169 and skip tag f v t spos _ =
7170 match t with
7171 | Vdata | Vcdata -> v
7172 | Vend ->
7173 error ("unexpected end of input in skipped " ^ tag) s spos
7174 | Vopen (tag', _, closed) ->
7175 if closed
7176 then v
7177 else
7178 let f' () = { v with f = skip tag f } in
7179 { v with f = skip tag' f' }
7180 | Vclose ctag ->
7181 if tag = ctag
7182 then f ()
7183 else error ("unexpected close in skipped " ^ tag) s spos
7186 parse { f = toplevel; accu = () } s;
7187 h, dc;
7190 let do_load f ic =
7192 let len = in_channel_length ic in
7193 let s = String.create len in
7194 really_input ic s 0 len;
7195 f s;
7196 with
7197 | Parse_error (msg, s, pos) ->
7198 let subs = subs s pos in
7199 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7201 | exn ->
7202 failwith ("config load error: " ^ exntos exn)
7205 let defconfpath =
7206 let dir =
7208 let dir = Filename.concat home ".config" in
7209 if Sys.is_directory dir then dir else home
7210 with _ -> home
7212 Filename.concat dir "llpp.conf"
7215 let confpath = ref defconfpath;;
7217 let load1 f =
7218 if Sys.file_exists !confpath
7219 then
7220 match
7221 (try Some (open_in_bin !confpath)
7222 with exn ->
7223 prerr_endline
7224 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7225 exntos exn);
7226 None
7228 with
7229 | Some ic ->
7230 let success =
7232 f (do_load get ic)
7233 with exn ->
7234 prerr_endline
7235 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7236 exntos exn);
7237 false
7239 close_in ic;
7240 success
7242 | None -> false
7243 else
7244 f (Hashtbl.create 0, defconf)
7247 let load () =
7248 let f (h, dc) =
7249 let pc, pb, px, pa =
7251 let key =
7252 if emptystr state.origin
7253 then state.path
7254 else state.origin
7256 Hashtbl.find h (Filename.basename key)
7257 with Not_found -> dc, [], 0, emptyanchor
7259 setconf defconf dc;
7260 setconf conf pc;
7261 state.bookmarks <- pb;
7262 state.x <- px;
7263 if conf.jumpback
7264 then state.anchor <- pa;
7265 cbput state.hists.nav pa;
7266 true
7268 load1 f
7271 let add_attrs bb always dc c =
7272 let ob s a b =
7273 if always || a != b
7274 then Printf.bprintf bb "\n %s='%b'" s a
7275 and op s a b =
7276 if always || a <> b
7277 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7278 and oi s a b =
7279 if always || a != b
7280 then Printf.bprintf bb "\n %s='%d'" s a
7281 and oI s a b =
7282 if always || a != b
7283 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7284 and oz s a b =
7285 if always || a <> b
7286 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7287 and oF s a b =
7288 if always || a <> b
7289 then Printf.bprintf bb "\n %s='%f'" s a
7290 and oc s a b =
7291 if always || a <> b
7292 then
7293 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7294 and oC s a b =
7295 if always || a <> b
7296 then
7297 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7298 and oR s a b =
7299 if always || a <> b
7300 then
7301 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7302 and os s a b =
7303 if always || a <> b
7304 then
7305 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7306 and og s a b =
7307 if always || a <> b
7308 then
7309 match a with
7310 | Some (_N, _A, _B) ->
7311 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7312 | None ->
7313 match b with
7314 | None -> ()
7315 | _ ->
7316 Printf.bprintf bb "\n %s='none'" s
7317 and oW s a b =
7318 if always || a <> b
7319 then
7320 let v =
7321 match a with
7322 | None -> "false"
7323 | Some f ->
7324 if f = infinity
7325 then "true"
7326 else string_of_float f
7328 Printf.bprintf bb "\n %s='%s'" s v
7329 and oco s a b =
7330 if always || a <> b
7331 then
7332 match a with
7333 | Cmulti ((n, a, b), _) when n > 1 ->
7334 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7335 | Csplit (n, _) when n > 1 ->
7336 Printf.bprintf bb "\n %s='%d'" s ~-n
7337 | _ -> ()
7338 and obeco s a b =
7339 if always || a <> b
7340 then
7341 match a with
7342 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7343 | _ -> ()
7344 and oFm s a b =
7345 if always || a <> b
7346 then
7347 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7348 and oSv s a b m =
7349 if always || a <> b
7350 then
7351 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7352 and oPm s a b =
7353 if always || a <> b
7354 then
7355 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7357 oi "width" c.cwinw dc.cwinw;
7358 oi "height" c.cwinh dc.cwinh;
7359 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7360 oi "scroll-handle-height" c.scrollh dc.scrollh;
7361 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7362 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7363 ob "case-insensitive-search" c.icase dc.icase;
7364 ob "preload" c.preload dc.preload;
7365 oi "page-bias" c.pagebias dc.pagebias;
7366 oi "scroll-step" c.scrollstep dc.scrollstep;
7367 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7368 ob "max-height-fit" c.maxhfit dc.maxhfit;
7369 ob "crop-hack" c.crophack dc.crophack;
7370 oW "throttle" c.maxwait dc.maxwait;
7371 ob "highlight-links" c.hlinks dc.hlinks;
7372 ob "under-cursor-info" c.underinfo dc.underinfo;
7373 oi "vertical-margin" c.interpagespace dc.interpagespace;
7374 oz "zoom" c.zoom dc.zoom;
7375 ob "presentation" c.presentation dc.presentation;
7376 oi "rotation-angle" c.angle dc.angle;
7377 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7378 oFm "fit-model" c.fitmodel dc.fitmodel;
7379 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7380 oi "tex-count" c.texcount dc.texcount;
7381 oi "slice-height" c.sliceheight dc.sliceheight;
7382 oi "thumbnail-width" c.thumbw dc.thumbw;
7383 ob "persistent-location" c.jumpback dc.jumpback;
7384 oc "background-color" c.bgcolor dc.bgcolor;
7385 oi "tile-width" c.tilew dc.tilew;
7386 oi "tile-height" c.tileh dc.tileh;
7387 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7388 ob "checkers" c.checkers dc.checkers;
7389 oi "aalevel" c.aalevel dc.aalevel;
7390 ob "trim-margins" c.trimmargins dc.trimmargins;
7391 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7392 os "uri-launcher" c.urilauncher dc.urilauncher;
7393 os "path-launcher" c.pathlauncher dc.pathlauncher;
7394 oC "color-space" c.colorspace dc.colorspace;
7395 ob "invert-colors" c.invert dc.invert;
7396 oF "brightness" c.colorscale dc.colorscale;
7397 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7398 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7399 oco "columns" c.columns dc.columns;
7400 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7401 os "selection-command" c.selcmd dc.selcmd;
7402 os "synctex-command" c.stcmd dc.stcmd;
7403 os "pax-command" c.paxcmd dc.paxcmd;
7404 ob "update-cursor" c.updatecurs dc.updatecurs;
7405 oi "hint-font-size" c.hfsize dc.hfsize;
7406 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7407 oF "page-scroll-scale" c.pgscale dc.pgscale;
7408 ob "use-pbo" c.usepbo dc.usepbo;
7409 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7410 ob "remote-in-a-new-instance" c.riani dc.riani;
7411 op "point-and-x" c.pax dc.pax;
7412 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7415 let keymapsbuf always dc c =
7416 let bb = Buffer.create 16 in
7417 let rec loop = function
7418 | [] -> ()
7419 | (modename, h) :: rest ->
7420 let dh = findkeyhash dc modename in
7421 if always || h <> dh
7422 then (
7423 if Hashtbl.length h > 0
7424 then (
7425 if Buffer.length bb > 0
7426 then Buffer.add_char bb '\n';
7427 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7428 Hashtbl.iter (fun i o ->
7429 let isdifferent = always ||
7431 let dO = Hashtbl.find dh i in
7432 dO <> o
7433 with Not_found -> true
7435 if isdifferent
7436 then
7437 let addkm (k, m) =
7438 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7439 if Wsi.withalt m then Buffer.add_string bb "alt-";
7440 if Wsi.withshift m then Buffer.add_string bb "shift-";
7441 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7442 Buffer.add_string bb (Wsi.keyname k);
7444 let addkms l =
7445 let rec loop = function
7446 | [] -> ()
7447 | km :: [] -> addkm km
7448 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7450 loop l
7452 Buffer.add_string bb "<map in='";
7453 addkm i;
7454 match o with
7455 | KMinsrt km ->
7456 Buffer.add_string bb "' out='";
7457 addkm km;
7458 Buffer.add_string bb "'/>\n"
7460 | KMinsrl kms ->
7461 Buffer.add_string bb "' out='";
7462 addkms kms;
7463 Buffer.add_string bb "'/>\n"
7465 | KMmulti (ins, kms) ->
7466 Buffer.add_char bb ' ';
7467 addkms ins;
7468 Buffer.add_string bb "' out='";
7469 addkms kms;
7470 Buffer.add_string bb "'/>\n"
7471 ) h;
7472 Buffer.add_string bb "</keymap>";
7475 loop rest
7477 loop c.keyhashes;
7481 let save () =
7482 let uifontsize = fstate.fontsize in
7483 let bb = Buffer.create 32768 in
7484 let relx = float state.x /. float state.winw in
7485 let w, h, x =
7486 let cx w = truncate (relx *. float w) in
7487 List.fold_left
7488 (fun (w, h, x) ws ->
7489 match ws with
7490 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7491 | Wsi.MaxVert -> (w, conf.cwinh, x)
7492 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7494 (state.winw, state.winh, state.x) state.winstate
7496 conf.cwinw <- w;
7497 conf.cwinh <- h;
7498 let f (h, dc) =
7499 let dc = if conf.bedefault then conf else dc in
7500 Buffer.add_string bb "<llppconfig>\n";
7502 if nonemptystr !fontpath
7503 then
7504 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7505 uifontsize
7506 !fontpath
7507 else (
7508 if uifontsize <> 14
7509 then
7510 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7513 Buffer.add_string bb "<defaults";
7514 add_attrs bb true dc dc;
7515 let kb = keymapsbuf true dc dc in
7516 if Buffer.length kb > 0
7517 then (
7518 Buffer.add_string bb ">\n";
7519 Buffer.add_buffer bb kb;
7520 Buffer.add_string bb "\n</defaults>\n";
7522 else Buffer.add_string bb "/>\n";
7524 let adddoc path pan anchor c bookmarks =
7525 if bookmarks == [] && c = dc && anchor = emptyanchor
7526 then ()
7527 else (
7528 Printf.bprintf bb "<doc path='%s'"
7529 (enent path 0 (String.length path));
7531 if anchor <> emptyanchor
7532 then (
7533 let n, rely, visy = anchor in
7534 Printf.bprintf bb " page='%d'" n;
7535 if rely > 1e-6
7536 then
7537 Printf.bprintf bb " rely='%f'" rely
7539 if abs_float visy > 1e-6
7540 then
7541 Printf.bprintf bb " visy='%f'" visy
7545 if pan != 0
7546 then Printf.bprintf bb " pan='%d'" pan;
7548 add_attrs bb false dc c;
7549 let kb = keymapsbuf false dc c in
7551 begin match bookmarks with
7552 | [] ->
7553 if Buffer.length kb > 0
7554 then (
7555 Buffer.add_string bb ">\n";
7556 Buffer.add_buffer bb kb;
7557 Buffer.add_string bb "\n</doc>\n";
7559 else Buffer.add_string bb "/>\n"
7560 | _ ->
7561 Buffer.add_string bb ">\n<bookmarks>\n";
7562 List.iter (fun (title, _, kind) ->
7563 begin match kind with
7564 | Oanchor (page, rely, visy) ->
7565 Printf.bprintf bb
7566 "<item title='%s' page='%d'"
7567 (enent title 0 (String.length title))
7568 page
7570 if rely > 1e-6
7571 then
7572 Printf.bprintf bb " rely='%f'" rely
7574 if abs_float visy > 1e-6
7575 then
7576 Printf.bprintf bb " visy='%f'" visy
7578 | Onone | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
7579 failwith "unexpected link in bookmarks"
7580 end;
7581 Buffer.add_string bb "/>\n";
7582 ) bookmarks;
7583 Buffer.add_string bb "</bookmarks>";
7584 if Buffer.length kb > 0
7585 then (
7586 Buffer.add_string bb "\n";
7587 Buffer.add_buffer bb kb;
7589 Buffer.add_string bb "\n</doc>\n";
7590 end;
7594 let pan, conf =
7595 match state.mode with
7596 | Birdseye (c, pan, _, _, _) ->
7597 let beyecolumns =
7598 match conf.columns with
7599 | Cmulti ((c, _, _), _) -> Some c
7600 | Csingle _ -> None
7601 | Csplit _ -> None
7602 and columns =
7603 match c.columns with
7604 | Cmulti (c, _) -> Cmulti (c, [||])
7605 | Csingle _ -> Csingle [||]
7606 | Csplit _ -> failwith "quit from bird's eye while split"
7608 pan, { c with beyecolumns = beyecolumns; columns = columns }
7609 | _ -> x, conf
7611 let basename = Filename.basename
7612 (if emptystr state.origin then state.path else state.origin)
7614 adddoc basename pan (getanchor ())
7615 (let conf =
7616 let autoscrollstep =
7617 match state.autoscroll with
7618 | Some step -> step
7619 | None -> conf.autoscrollstep
7621 match state.mode with
7622 | Birdseye (bc, _, _, _, _) ->
7623 { conf with
7624 zoom = bc.zoom;
7625 presentation = bc.presentation;
7626 interpagespace = bc.interpagespace;
7627 maxwait = bc.maxwait;
7628 autoscrollstep = autoscrollstep }
7629 | _ -> { conf with autoscrollstep = autoscrollstep }
7630 in conf)
7631 (if conf.savebmarks then state.bookmarks else []);
7633 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7634 if basename <> path
7635 then adddoc path x anchor c bookmarks
7636 ) h;
7637 Buffer.add_string bb "</llppconfig>\n";
7638 true;
7640 if load1 f && Buffer.length bb > 0
7641 then
7643 let tmp = !confpath ^ ".tmp" in
7644 let oc = open_out_bin tmp in
7645 Buffer.output_buffer oc bb;
7646 close_out oc;
7647 Unix.rename tmp !confpath;
7648 with exn ->
7649 prerr_endline
7650 ("error while saving configuration: " ^ exntos exn)
7652 end;;
7654 let adderrmsg src msg =
7655 Buffer.add_string state.errmsgs msg;
7656 state.newerrmsgs <- true;
7657 G.postRedisplay src
7660 let adderrfmt src fmt =
7661 Format.kprintf (fun s -> adderrmsg src s) fmt;
7664 let ract cmds =
7665 let cl = splitatspace cmds in
7666 let scan s fmt f =
7667 try Scanf.sscanf s fmt f
7668 with exn ->
7669 adderrfmt "remote exec"
7670 "error processing '%S': %s\n" cmds (exntos exn)
7672 match cl with
7673 | "reload" :: [] -> reload ()
7674 | "goto" :: args :: [] ->
7675 scan args "%u %f %f"
7676 (fun pageno x y ->
7677 let cmd, _ = state.geomcmds in
7678 if emptystr cmd
7679 then gotopagexy pageno x y
7680 else
7681 let f prevf () =
7682 gotopagexy pageno x y;
7683 prevf ()
7685 state.reprf <- f state.reprf
7687 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7688 | "gotor" :: args :: [] ->
7689 scan args "%S %u"
7690 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
7691 | "gotord" :: args :: [] ->
7692 scan args "%S %S"
7693 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
7694 | "rect" :: args :: [] ->
7695 scan args "%u %u %f %f %f %f"
7696 (fun pageno color x0 y0 x1 y1 ->
7697 onpagerect pageno (fun w h ->
7698 let _,w1,h1,_ = getpagedim pageno in
7699 let sw = float w1 /. float w
7700 and sh = float h1 /. float h in
7701 let x0s = x0 *. sw
7702 and x1s = x1 *. sw
7703 and y0s = y0 *. sh
7704 and y1s = y1 *. sh in
7705 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7706 debugrect rect;
7707 state.rects <- (pageno, color, rect) :: state.rects;
7708 G.postRedisplay "rect";
7711 | "activatewin" :: [] -> Wsi.activatewin ()
7712 | "quit" :: [] -> raise Quit
7713 | _ ->
7714 adderrfmt "remote command"
7715 "error processing remote command: %S\n" cmds;
7718 let remote =
7719 let scratch = String.create 80 in
7720 let buf = Buffer.create 80 in
7721 fun fd ->
7722 let rec tempfr () =
7723 try Some (Unix.read fd scratch 0 80)
7724 with
7725 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7726 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7727 | exn -> raise exn
7729 match tempfr () with
7730 | None -> Some fd
7731 | Some n ->
7732 if n = 0
7733 then (
7734 Unix.close fd;
7735 if Buffer.length buf > 0
7736 then (
7737 let s = Buffer.contents buf in
7738 Buffer.clear buf;
7739 ract s;
7741 None
7743 else
7744 let rec eat ppos =
7745 let nlpos =
7747 let pos = String.index_from scratch ppos '\n' in
7748 if pos >= n then -1 else pos
7749 with Not_found -> -1
7751 if nlpos >= 0
7752 then (
7753 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7754 let s = Buffer.contents buf in
7755 Buffer.clear buf;
7756 ract s;
7757 eat (nlpos+1);
7759 else (
7760 Buffer.add_substring buf scratch ppos (n-ppos);
7761 Some fd
7763 in eat 0
7766 let remoteopen path =
7767 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7768 with exn ->
7769 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7770 None
7773 let () =
7774 let trimcachepath = ref "" in
7775 let rcmdpath = ref "" in
7776 let pageno = ref None in
7777 selfexec := Sys.executable_name;
7778 Arg.parse
7779 (Arg.align
7780 [("-p", Arg.String (fun s -> state.password <- s),
7781 "<password> Set password");
7783 ("-f", Arg.String
7784 (fun s ->
7785 Config.fontpath := s;
7786 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7788 "<path> Set path to the user interface font");
7790 ("-c", Arg.String
7791 (fun s ->
7792 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7793 Config.confpath := s),
7794 "<path> Set path to the configuration file");
7796 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
7797 "<page-number> Jump to page");
7799 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7800 "<path> Set path to the trim cache file");
7802 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7803 "<named-destination> Set named destination");
7805 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7806 ("-cxack", Arg.Set cxack, " Cut corners");
7808 ("-remote", Arg.String (fun s -> rcmdpath := s),
7809 "<path> Set path to the remote commands source");
7811 ("-origin", Arg.String (fun s -> state.origin <- s),
7812 "<original-path> Set original path");
7814 ("-v", Arg.Unit (fun () ->
7815 Printf.printf
7816 "%s\nconfiguration path: %s\n"
7817 (version ())
7818 Config.defconfpath
7820 exit 0), " Print version and exit");
7823 (fun s -> state.path <- s)
7824 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7826 if !wtmode
7827 then selfexec := !selfexec ^ " -wtmode";
7829 if emptystr state.path
7830 then (prerr_endline "file name missing"; exit 1);
7832 if not (Config.load ())
7833 then prerr_endline "failed to load configuration";
7834 begin match !pageno with
7835 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
7836 | None -> ()
7837 end;
7839 let wsfd, winw, winh = Wsi.init (object (self)
7840 val mutable m_hack = false
7841 val mutable m_clicks = 0
7842 val mutable m_click_x = 0
7843 val mutable m_click_y = 0
7844 val mutable m_lastclicktime = infinity
7846 method private cleanup =
7847 state.roam <- noroam;
7848 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
7849 method expose = if not m_hack then G.postRedisplay "expose"
7850 method visible = G.postRedisplay "visible"
7851 method display = m_hack <- false; display ()
7852 method reshape w h =
7853 self#cleanup;
7854 m_hack <- w < state.winw && h < state.winh;
7855 reshape w h
7856 method mouse b d x y m =
7857 if d
7858 then (
7859 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
7860 m_click_x <- x;
7861 m_click_y <- y;
7862 if b = 1
7863 then (
7864 let t = now () in
7865 if abs x - m_click_x > 10
7866 || abs y - m_click_y > 10
7867 || abs_float (t -. m_lastclicktime) > 0.3
7868 then m_clicks <- 0;
7869 m_clicks <- m_clicks + 1;
7870 m_lastclicktime <- t;
7871 if m_clicks = 1
7872 then (
7873 self#cleanup;
7874 G.postRedisplay "cleanup";
7875 state.uioh <- state.uioh#button b d x y m;
7877 else state.uioh <- state.uioh#multiclick m_clicks x y m
7879 else (
7880 self#cleanup;
7881 m_clicks <- 0;
7882 m_lastclicktime <- infinity;
7883 state.uioh <- state.uioh#button b d x y m
7886 else (
7887 state.uioh <- state.uioh#button b d x y m
7889 method motion x y =
7890 state.mpos <- (x, y);
7891 state.uioh <- state.uioh#motion x y
7892 method pmotion x y =
7893 state.mpos <- (x, y);
7894 state.uioh <- state.uioh#pmotion x y
7895 method key k m =
7896 let mascm = m land (
7897 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7898 ) in
7899 let keyboard k m =
7900 let x = state.x and y = state.y in
7901 keyboard k m;
7902 if x != state.x || y != state.y then self#cleanup
7904 match state.keystate with
7905 | KSnone ->
7906 let km = k, mascm in
7907 begin
7908 match
7909 let modehash = state.uioh#modehash in
7910 try Hashtbl.find modehash km
7911 with Not_found ->
7912 try Hashtbl.find (findkeyhash conf "global") km
7913 with Not_found -> KMinsrt (k, m)
7914 with
7915 | KMinsrt (k, m) -> keyboard k m
7916 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7917 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7919 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7920 List.iter (fun (k, m) -> keyboard k m) insrt;
7921 state.keystate <- KSnone
7922 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7923 state.keystate <- KSinto (keys, insrt)
7924 | _ ->
7925 state.keystate <- KSnone
7927 method enter x y =
7928 state.mpos <- (x, y);
7929 state.uioh <- state.uioh#pmotion x y
7930 method leave = state.mpos <- (-1, -1)
7931 method winstate wsl = state.winstate <- wsl; m_hack <- false
7932 method quit = raise Quit
7933 end) conf.cwinw conf.cwinh (platform = Posx) in
7935 state.wsfd <- wsfd;
7937 if not (
7938 List.exists GlMisc.check_extension
7939 [ "GL_ARB_texture_rectangle"
7940 ; "GL_EXT_texture_recangle"
7941 ; "GL_NV_texture_rectangle" ]
7943 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7945 if (
7946 let r = GlMisc.get_string `renderer in
7947 let p = "Mesa DRI Intel(" in
7948 let l = String.length p in
7949 String.length r > l && String.sub r 0 l = p
7951 then (
7952 defconf.sliceheight <- 1024;
7953 defconf.texcount <- 32;
7954 defconf.usepbo <- true;
7957 let cr, sw =
7958 match Ne.res Unix.pipe with
7959 | Ne.Exn exn ->
7960 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7961 exit 1
7962 | Ne.Res rw -> rw
7963 and sr, cw =
7964 match Ne.res Unix.pipe with
7965 | Ne.Exn exn ->
7966 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7967 exit 1
7968 | Ne.Res rw -> rw
7971 cloexec cr;
7972 cloexec sw;
7973 cloexec sr;
7974 cloexec cw;
7976 setcheckers conf.checkers;
7977 redirectstderr ();
7978 if conf.redirectstderr
7979 then
7980 at_exit (fun () ->
7981 let s = Buffer.contents state.errmsgs ^
7982 (match state.errfd with
7983 | Some fd ->
7984 let s = String.create (80*24) in
7985 let n =
7987 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7988 if List.mem fd r
7989 then Unix.read fd s 0 (String.length s)
7990 else 0
7991 with _ -> 0
7993 if n = 0
7994 then ""
7995 else String.sub s 0 n
7996 | None -> ""
7999 try ignore (Unix.write state.stderr s 0 (String.length s))
8000 with exn -> print_endline (exntos exn)
8004 init (cr, cw) (
8005 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
8006 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
8007 !Config.fontpath, !trimcachepath,
8008 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
8010 List.iter GlArray.enable [`texture_coord; `vertex];
8011 state.sr <- sr;
8012 state.sw <- sw;
8013 state.text <- "Opening " ^ (mbtoutf8 state.path);
8014 reshape winw winh;
8015 opendoc state.path state.password;
8016 state.uioh <- uioh;
8017 display ();
8018 Wsi.mapwin ();
8019 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
8020 let optrfd =
8021 ref (
8022 if nonemptystr !rcmdpath
8023 then remoteopen !rcmdpath
8024 else None
8028 let rec loop deadline =
8029 let r =
8030 match state.errfd with
8031 | None -> [state.sr; state.wsfd]
8032 | Some fd -> [state.sr; state.wsfd; fd]
8034 let r =
8035 match !optrfd with
8036 | None -> r
8037 | Some fd -> fd :: r
8039 if state.redisplay
8040 then (
8041 state.redisplay <- false;
8042 display ();
8044 let timeout =
8045 let now = now () in
8046 if deadline > now
8047 then (
8048 if deadline = infinity
8049 then ~-.1.0
8050 else max 0.0 (deadline -. now)
8052 else 0.0
8054 let r, _, _ =
8055 try Unix.select r [] [] timeout
8056 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
8058 begin match r with
8059 | [] ->
8060 state.ghyll None;
8061 let newdeadline =
8062 if state.ghyll == noghyll
8063 then
8064 match state.autoscroll with
8065 | Some step when step != 0 ->
8066 let y = state.y + step in
8067 let y =
8068 if y < 0
8069 then state.maxy
8070 else if y >= state.maxy then 0 else y
8072 gotoy y;
8073 if state.mode = View
8074 then state.text <- "";
8075 deadline +. 0.01
8076 | _ -> infinity
8077 else deadline +. 0.01
8079 loop newdeadline
8081 | l ->
8082 let rec checkfds = function
8083 | [] -> ()
8084 | fd :: rest when fd = state.sr ->
8085 let cmd = readcmd state.sr in
8086 act cmd;
8087 checkfds rest
8089 | fd :: rest when fd = state.wsfd ->
8090 Wsi.readresp fd;
8091 checkfds rest
8093 | fd :: rest when Some fd = !optrfd ->
8094 begin match remote fd with
8095 | None -> optrfd := remoteopen !rcmdpath;
8096 | opt -> optrfd := opt
8097 end;
8098 checkfds rest
8100 | fd :: rest ->
8101 let s = String.create 80 in
8102 let n = tempfailureretry (Unix.read fd s 0) 80 in
8103 if conf.redirectstderr
8104 then (
8105 Buffer.add_substring state.errmsgs s 0 n;
8106 state.newerrmsgs <- true;
8107 state.redisplay <- true;
8109 else (
8110 prerr_string (String.sub s 0 n);
8111 flush stderr;
8113 checkfds rest
8115 checkfds l;
8116 let newdeadline =
8117 let deadline1 =
8118 if deadline = infinity
8119 then now () +. 0.01
8120 else deadline
8122 match state.autoscroll with
8123 | Some step when step != 0 -> deadline1
8124 | _ -> if state.ghyll == noghyll then infinity else deadline1
8126 loop newdeadline
8127 end;
8130 loop infinity;
8131 with Quit ->
8132 Config.save ();