Explicitly set blend funtion when drawing link frames
[llpp.git] / main.ml
blob60af4927cad63ddd6e82418224d93baad42e90ca
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 -> bool -> 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.set_close_on_exec 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 pipe () =
829 try Res (Unix.pipe ())
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.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 paxunder x y =
979 let g opaque l px py =
980 if markunder opaque px py conf.paxmark
981 then (
982 Some (fun () ->
983 match getopaque l.pageno with
984 | None -> ()
985 | Some opaque ->
986 match Ne.pipe () with
987 | Ne.Exn exn ->
988 showtext '!'
989 (Printf.sprintf
990 "can not create mark pipe: %s"
991 (exntos exn));
992 | Ne.Res (r, w) ->
993 let doclose what fd =
994 Ne.clo fd (fun msg ->
995 dolog "%s close failed: %s" what msg)
998 popen conf.paxcmd [r, 0; w, -1];
999 copysel w opaque false;
1000 doclose "pipe/r" r;
1001 G.postRedisplay "paxunder";
1002 with exn ->
1003 dolog "can not execute %S: %s"
1004 conf.paxcmd (exntos exn);
1005 doclose "pipe/r" r;
1006 doclose "pipe/w" w;
1009 else None
1011 G.postRedisplay "paxunder";
1012 if conf.paxmark = Mark_page
1013 then
1014 List.iter (fun l ->
1015 match getopaque l.pageno with
1016 | None -> ()
1017 | Some opaque -> clearmark opaque) state.layout;
1018 state.roam <-
1019 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
1022 let selstring s =
1023 match Ne.pipe () with
1024 | Ne.Exn exn ->
1025 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
1026 | Ne.Res (r, w) ->
1027 let popened =
1028 try popen conf.selcmd [r, 0; w, -1]; true
1029 with exn ->
1030 showtext '!'
1031 (Printf.sprintf "failed to execute %s: %s"
1032 conf.selcmd (exntos exn));
1033 false
1035 let clo cap fd =
1036 Ne.clo fd (fun msg ->
1037 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
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 "pipe/r" r;
1060 clo "pipe/w" w;
1063 let undertext = function
1064 | Unone -> "none"
1065 | Ulinkuri s -> s
1066 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1067 | Utext s -> "font: " ^ s
1068 | Uunexpected s -> "unexpected: " ^ s
1069 | Ulaunch s -> "launch: " ^ s
1070 | Unamed s -> "named: " ^ s
1071 | Uremote (filename, pageno) ->
1072 Printf.sprintf "%s: page %d" filename (pageno+1)
1073 | Uremotedest (filename, destname) ->
1074 Printf.sprintf "%s: destination %S" filename destname
1077 let updateunder x y =
1078 match getunder x y with
1079 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1080 | Ulinkuri uri ->
1081 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1082 Wsi.setcursor Wsi.CURSOR_INFO
1083 | Ulinkgoto (pageno, _) ->
1084 if conf.underinfo
1085 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1086 Wsi.setcursor Wsi.CURSOR_INFO
1087 | Utext s ->
1088 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1089 Wsi.setcursor Wsi.CURSOR_TEXT
1090 | Uunexpected s ->
1091 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1092 Wsi.setcursor Wsi.CURSOR_INHERIT
1093 | Ulaunch s ->
1094 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1095 Wsi.setcursor Wsi.CURSOR_INHERIT
1096 | Unamed s ->
1097 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1098 Wsi.setcursor Wsi.CURSOR_INHERIT
1099 | Uremote (filename, pageno) ->
1100 if conf.underinfo then showtext 'r'
1101 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1102 Wsi.setcursor Wsi.CURSOR_INFO
1103 | Uremotedest (filename, destname) ->
1104 if conf.underinfo then showtext 'r'
1105 (Printf.sprintf "emote destination: %s (%S)" filename destname);
1106 Wsi.setcursor Wsi.CURSOR_INFO
1109 let showlinktype under =
1110 if conf.underinfo
1111 then
1112 match under with
1113 | Unone -> ()
1114 | under ->
1115 let s = undertext under in
1116 showtext ' ' s
1119 let addchar s c =
1120 let b = Buffer.create (String.length s + 1) in
1121 Buffer.add_string b s;
1122 Buffer.add_char b c;
1123 Buffer.contents b;
1126 module type TextEnumType =
1128 type t
1129 val name : string
1130 val names : string array
1131 end;;
1133 module TextEnumMake (Ten : TextEnumType) =
1134 struct
1135 let names = Ten.names;;
1136 let to_int (t : Ten.t) = Obj.magic t;;
1137 let to_string t = names.(to_int t);;
1138 let of_int n : Ten.t = Obj.magic n;;
1139 let of_string s =
1140 let rec find i =
1141 if i = Array.length names
1142 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1143 else (
1144 if Ten.names.(i) = s
1145 then of_int i
1146 else find (i+1)
1148 in find 0;;
1149 end;;
1151 module CSTE = TextEnumMake (struct
1152 type t = colorspace;;
1153 let name = "colorspace";;
1154 let names = [|"rgb"; "bgr"; "gray"|];;
1155 end);;
1157 module MTE = TextEnumMake (struct
1158 type t = mark;;
1159 let name = "mark";;
1160 let names = [|"page"; "block"; "line"; "word"|];;
1161 end);;
1163 module FMTE = TextEnumMake (struct
1164 type t= fitmodel;;
1165 let name = "fitmodel";;
1166 let names = [|"width"; "proportional"; "page"|];;
1167 end);;
1169 let intentry_with_suffix text key =
1170 let c =
1171 if key >= 32 && key < 127
1172 then Char.chr key
1173 else '\000'
1175 match Char.lowercase c with
1176 | '0' .. '9' ->
1177 let text = addchar text c in
1178 TEcont text
1180 | 'k' | 'm' | 'g' ->
1181 let text = addchar text c in
1182 TEcont text
1184 | _ ->
1185 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1186 TEcont text
1189 let multicolumns_to_string (n, a, b) =
1190 if a = 0 && b = 0
1191 then Printf.sprintf "%d" n
1192 else Printf.sprintf "%d,%d,%d" n a b;
1195 let multicolumns_of_string s =
1197 (int_of_string s, 0, 0)
1198 with _ ->
1199 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1200 if a > 1 || b > 1
1201 then failwith "subtly broken"; (n, a, b)
1205 let readcmd fd =
1206 let s = "xxxx" in
1207 let n = tempfailureretry (Unix.read fd s 0) 4 in
1208 if n != 4 then error "incomplete read(len) = %d" n;
1209 let len = 0
1210 lor (Char.code s.[0] lsl 24)
1211 lor (Char.code s.[1] lsl 16)
1212 lor (Char.code s.[2] lsl 8)
1213 lor (Char.code s.[3] lsl 0)
1215 let s = String.create len in
1216 let n = tempfailureretry (Unix.read fd s 0) len in
1217 if n != len then error "incomplete read(data) %d vs %d" n len;
1221 let btod b = if b then 1 else 0;;
1223 let wcmd fmt =
1224 let b = Buffer.create 16 in
1225 Buffer.add_string b "llll";
1226 Printf.kbprintf
1227 (fun b ->
1228 let s = Buffer.contents b in
1229 let n = String.length s in
1230 let len = n - 4 in
1231 (* dolog "wcmd %S" (String.sub s 4 len); *)
1232 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1233 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1234 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1235 s.[3] <- Char.chr (len land 0xff);
1236 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1237 if n' != n then error "write failed %d vs %d" n' n;
1238 ) b fmt;
1241 let calcips h =
1242 let d = state.winh - h in
1243 max conf.interpagespace ((d + 1) / 2)
1246 let rowyh (c, coverA, coverB) b n =
1247 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1248 then
1249 let _, _, vy, (_, _, h, _) = b.(n) in
1250 (vy, h)
1251 else
1252 let n' = n - coverA in
1253 let d = n' mod c in
1254 let s = n - d in
1255 let e = min state.pagecount (s + c) in
1256 let rec find m miny maxh = if m = e then miny, maxh else
1257 let _, _, y, (_, _, h, _) = b.(m) in
1258 let miny = min miny y in
1259 let maxh = max maxh h in
1260 find (m+1) miny maxh
1261 in find s max_int 0
1264 let calcheight () =
1265 match conf.columns with
1266 | Cmulti ((_, _, _) as cl, b) ->
1267 if Array.length b > 0
1268 then
1269 let y, h = rowyh cl b (Array.length b - 1) in
1270 y + h + (if conf.presentation then calcips h else 0)
1271 else 0
1272 | Csingle b ->
1273 if Array.length b > 0
1274 then
1275 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1276 y + h + (if conf.presentation then calcips h else 0)
1277 else 0
1278 | Csplit (_, b) ->
1279 if Array.length b > 0
1280 then
1281 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1282 y + h
1283 else 0
1286 let getpageywh pageno =
1287 let pageno = bound pageno 0 (state.pagecount-1) in
1288 match conf.columns with
1289 | Csingle b ->
1290 if Array.length b = 0
1291 then 0, 0, 0
1292 else
1293 let (_, _, y, (_, w, h, _)) = b.(pageno) in
1294 let y =
1295 if conf.presentation
1296 then y - calcips h
1297 else y
1299 y, w, h
1300 | Cmulti (cl, b) ->
1301 if Array.length b = 0
1302 then 0, 0, 0
1303 else
1304 let y, h = rowyh cl b pageno in
1305 let (_, _, _, (_, w, _, _)) = b.(pageno) in
1306 let y =
1307 if conf.presentation
1308 then y - calcips h
1309 else y
1311 y, w, h
1312 | Csplit (c, b) ->
1313 if Array.length b = 0
1314 then 0, 0, 0
1315 else
1316 let n = pageno*c in
1317 let (_, _, y, (_, w, h, _)) = b.(n) in
1318 y, w / c, h
1321 let getpageyh pageno =
1322 let y,_,h = getpageywh pageno in
1323 y, h;
1326 let getpagedim pageno =
1327 let rec f ppdim l =
1328 match l with
1329 | (n, _, _, _) as pdim :: rest ->
1330 if n >= pageno
1331 then (if n = pageno then pdim else ppdim)
1332 else f pdim rest
1334 | [] -> ppdim
1336 f (-1, -1, -1, -1) state.pdims
1339 let getpagey pageno = fst (getpageyh pageno);;
1341 let nogeomcmds cmds =
1342 match cmds with
1343 | s, [] -> emptystr s
1344 | _ -> false
1347 let page_of_y y =
1348 let ((c, coverA, coverB) as cl), b =
1349 match conf.columns with
1350 | Csingle b -> (1, 0, 0), b
1351 | Cmulti (c, b) -> c, b
1352 | Csplit (_, b) -> (1, 0, 0), b
1354 if Array.length b = 0
1355 then -1
1356 else
1357 let rec bsearch nmin nmax =
1358 if nmin > nmax
1359 then bound nmin 0 (state.pagecount-1)
1360 else
1361 let n = (nmax + nmin) / 2 in
1362 let vy, h = rowyh cl b n in
1363 let y0, y1 =
1364 if conf.presentation
1365 then
1366 let ips = calcips h in
1367 let y0 = vy - ips in
1368 let y1 = vy + h + ips in
1369 y0, y1
1370 else (
1371 if n = 0
1372 then 0, vy + h + conf.interpagespace
1373 else
1374 let y0 = vy - conf.interpagespace in
1375 y0, y0 + h + conf.interpagespace
1378 if y >= y0 && y < y1
1379 then (
1380 if c = 1
1381 then n
1382 else (
1383 if n > coverA
1384 then
1385 if n < state.pagecount - coverB
1386 then ((n-coverA)/c)*c + coverA
1387 else n
1388 else n
1391 else (
1392 if y > y0
1393 then bsearch (n+1) nmax
1394 else bsearch nmin (n-1)
1397 bsearch 0 (state.pagecount-1);
1400 let layoutN ((columns, coverA, coverB), b) y sh =
1401 let sh = sh - (hscrollh ()) in
1402 let rec fold accu n =
1403 if n = Array.length b
1404 then accu
1405 else
1406 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1407 if (vy - y) > sh &&
1408 (n = coverA - 1
1409 || n = state.pagecount - coverB
1410 || (n - coverA) mod columns = columns - 1)
1411 then accu
1412 else
1413 let accu =
1414 if vy + h > y
1415 then
1416 let pagey = max 0 (y - vy) in
1417 let pagedispy = if pagey > 0 then 0 else vy - y in
1418 let pagedispx, pagex =
1419 let pdx =
1420 if n = coverA - 1 || n = state.pagecount - coverB
1421 then state.x + (wadjsb state.winw - w) / 2
1422 else dx + xoff + state.x
1424 if pdx < 0
1425 then 0, -pdx
1426 else pdx, 0
1428 let pagevw =
1429 let vw = wadjsb state.winw - pagedispx in
1430 let pw = w - pagex in
1431 min vw pw
1433 let pagevh = min (h - pagey) (sh - pagedispy) in
1434 if pagevw > 0 && pagevh > 0
1435 then
1436 let e =
1437 { pageno = n
1438 ; pagedimno = pdimno
1439 ; pagew = w
1440 ; pageh = h
1441 ; pagex = pagex
1442 ; pagey = pagey
1443 ; pagevw = pagevw
1444 ; pagevh = pagevh
1445 ; pagedispx = pagedispx
1446 ; pagedispy = pagedispy
1447 ; pagecol = 0
1450 e :: accu
1451 else
1452 accu
1453 else
1454 accu
1456 fold accu (n+1)
1458 if Array.length b = 0
1459 then []
1460 else List.rev (fold [] (page_of_y y))
1463 let layoutS (columns, b) y sh =
1464 let sh = sh - hscrollh () in
1465 let rec fold accu n =
1466 if n = Array.length b
1467 then accu
1468 else
1469 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1470 if (vy - y) > sh
1471 then accu
1472 else
1473 let accu =
1474 if vy + pageh > y
1475 then
1476 let x = xoff + state.x in
1477 let pagey = max 0 (y - vy) in
1478 let pagedispy = if pagey > 0 then 0 else vy - y in
1479 let pagedispx, pagex =
1480 if px = 0
1481 then (
1482 if x < 0
1483 then 0, -x
1484 else x, 0
1486 else (
1487 let px = px - x in
1488 if px < 0
1489 then -px, 0
1490 else 0, px
1493 let pagecolw = pagew/columns in
1494 let pagedispx =
1495 if pagecolw < state.winw
1496 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1497 else pagedispx
1499 let pagevw =
1500 let vw = wadjsb state.winw - pagedispx in
1501 let pw = pagew - pagex in
1502 min vw pw
1504 let pagevw = min pagevw pagecolw in
1505 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1506 if pagevw > 0 && pagevh > 0
1507 then
1508 let e =
1509 { pageno = n/columns
1510 ; pagedimno = pdimno
1511 ; pagew = pagew
1512 ; pageh = pageh
1513 ; pagex = pagex
1514 ; pagey = pagey
1515 ; pagevw = pagevw
1516 ; pagevh = pagevh
1517 ; pagedispx = pagedispx
1518 ; pagedispy = pagedispy
1519 ; pagecol = n mod columns
1522 e :: accu
1523 else
1524 accu
1525 else
1526 accu
1528 fold accu (n+1)
1530 List.rev (fold [] 0)
1533 let layout y sh =
1534 if nogeomcmds state.geomcmds
1535 then
1536 match conf.columns with
1537 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1538 | Cmulti c -> layoutN c y sh
1539 | Csplit s -> layoutS s y sh
1540 else []
1543 let clamp incr =
1544 let y = state.y + incr in
1545 let y = max 0 y in
1546 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1550 let itertiles l f =
1551 let tilex = l.pagex mod conf.tilew in
1552 let tiley = l.pagey mod conf.tileh in
1554 let col = l.pagex / conf.tilew in
1555 let row = l.pagey / conf.tileh in
1557 let rec rowloop row y0 dispy h =
1558 if h = 0
1559 then ()
1560 else (
1561 let dh = conf.tileh - y0 in
1562 let dh = min h dh in
1563 let rec colloop col x0 dispx w =
1564 if w = 0
1565 then ()
1566 else (
1567 let dw = conf.tilew - x0 in
1568 let dw = min w dw in
1570 f col row dispx dispy x0 y0 dw dh;
1571 colloop (col+1) 0 (dispx+dw) (w-dw)
1574 colloop col tilex l.pagedispx l.pagevw;
1575 rowloop (row+1) 0 (dispy+dh) (h-dh)
1578 if l.pagevw > 0 && l.pagevh > 0
1579 then rowloop row tiley l.pagedispy l.pagevh;
1582 let gettileopaque l col row =
1583 let key =
1584 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1586 try Some (Hashtbl.find state.tilemap key)
1587 with Not_found -> None
1590 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1591 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1592 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1595 let filledrect x0 y0 x1 y1 =
1596 GlArray.disable `texture_coord;
1597 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1598 GlArray.vertex `two state.vraw;
1599 GlArray.draw_arrays `triangle_strip 0 4;
1600 GlArray.enable `texture_coord;
1603 let linerect x0 y0 x1 y1 =
1604 GlArray.disable `texture_coord;
1605 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
1606 GlArray.vertex `two state.vraw;
1607 GlArray.draw_arrays `line_loop 0 4;
1608 GlArray.enable `texture_coord;
1611 let drawtiles l color =
1612 GlDraw.color color;
1613 begintiles ();
1614 let f col row x y tilex tiley w h =
1615 match gettileopaque l col row with
1616 | Some (opaque, _, t) ->
1617 let params = x, y, w, h, tilex, tiley in
1618 if conf.invert
1619 then (
1620 Gl.enable `blend;
1621 GlFunc.blend_func `zero `one_minus_src_color;
1623 drawtile params opaque;
1624 if conf.invert
1625 then Gl.disable `blend;
1626 if conf.debug
1627 then (
1628 endtiles ();
1629 let s = Printf.sprintf
1630 "%d[%d,%d] %f sec"
1631 l.pageno col row t
1633 let w = measurestr fstate.fontsize s in
1634 GlDraw.color (0.0, 0.0, 0.0);
1635 filledrect (float (x-2))
1636 (float (y-2))
1637 (float (x+2) +. w)
1638 (float (y + fstate.fontsize + 2));
1639 GlDraw.color (1.0, 1.0, 1.0);
1640 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1641 begintiles ();
1644 | None ->
1645 endtiles ();
1646 let w =
1647 let lw = wadjsb state.winw - x in
1648 min lw w
1649 and h =
1650 let lh = state.winh - y in
1651 min lh h
1653 if conf.invert
1654 then (
1655 Gl.enable `blend;
1656 GlFunc.blend_func `zero `one_minus_src_color;
1658 begin match state.texid with
1659 | Some id ->
1660 Gl.enable `texture_2d;
1661 GlTex.bind_texture `texture_2d id;
1662 let x0 = float x
1663 and y0 = float y
1664 and x1 = float (x+w)
1665 and y1 = float (y+h) in
1667 let tw = float w /. 16.0
1668 and th = float h /. 16.0 in
1669 let tx0 = float tilex /. 16.0
1670 and ty0 = float tiley /. 16.0 in
1671 let tx1 = tx0 +. tw
1672 and ty1 = ty0 +. th in
1673 Raw.sets_float state.vraw ~pos:0
1674 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1675 Raw.sets_float state.traw ~pos:0
1676 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
1677 GlArray.vertex `two state.vraw;
1678 GlArray.tex_coord `two state.traw;
1679 GlArray.draw_arrays `triangle_strip 0 4;
1681 | None ->
1682 GlDraw.color (1.0, 1.0, 1.0);
1683 filledrect (float x) (float y) (float (x+w)) (float (y+h));
1684 end;
1685 if w > 128 && h > fstate.fontsize + 10
1686 then (
1687 let c = if conf.invert then 1.0 else 0.0 in
1688 GlDraw.color (c, c, c);
1689 let c, r =
1690 if conf.verbose
1691 then (col*conf.tilew, row*conf.tileh)
1692 else col, row
1694 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1696 GlDraw.color color;
1697 begintiles ();
1699 itertiles l f;
1700 endtiles ();
1703 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1705 let tilevisible1 l x y =
1706 let ax0 = l.pagex
1707 and ax1 = l.pagex + l.pagevw
1708 and ay0 = l.pagey
1709 and ay1 = l.pagey + l.pagevh in
1711 let bx0 = x
1712 and by0 = y in
1713 let bx1 = min (bx0 + conf.tilew) l.pagew
1714 and by1 = min (by0 + conf.tileh) l.pageh in
1716 let rx0 = max ax0 bx0
1717 and ry0 = max ay0 by0
1718 and rx1 = min ax1 bx1
1719 and ry1 = min ay1 by1 in
1721 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1722 nonemptyintersection
1725 let tilevisible layout n x y =
1726 let rec findpageinlayout m = function
1727 | l :: rest when l.pageno = n ->
1728 tilevisible1 l x y || (
1729 match conf.columns with
1730 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1731 | _ -> false
1733 | _ :: rest -> findpageinlayout 0 rest
1734 | [] -> false
1736 findpageinlayout 0 layout;
1739 let tileready l x y =
1740 tilevisible1 l x y &&
1741 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1744 let tilepage n p layout =
1745 let rec loop = function
1746 | l :: rest ->
1747 if l.pageno = n
1748 then
1749 let f col row _ _ _ _ _ _ =
1750 if state.currently = Idle
1751 then
1752 match gettileopaque l col row with
1753 | Some _ -> ()
1754 | None ->
1755 let x = col*conf.tilew
1756 and y = row*conf.tileh in
1757 let w =
1758 let w = l.pagew - x in
1759 min w conf.tilew
1761 let h =
1762 let h = l.pageh - y in
1763 min h conf.tileh
1765 let pbo =
1766 if conf.usepbo
1767 then getpbo w h conf.colorspace
1768 else "0"
1770 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1771 state.currently <-
1772 Tiling (
1773 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1774 conf.tilew, conf.tileh
1777 itertiles l f;
1778 else
1779 loop rest
1781 | [] -> ()
1783 if nogeomcmds state.geomcmds
1784 then loop layout;
1787 let preloadlayout y =
1788 let y = if y < state.winh then 0 else y - state.winh in
1789 let h = state.winh*3 in
1790 layout y h;
1793 let load pages =
1794 let rec loop pages =
1795 if state.currently != Idle
1796 then ()
1797 else
1798 match pages with
1799 | l :: rest ->
1800 begin match getopaque l.pageno with
1801 | None ->
1802 wcmd "page %d %d" l.pageno l.pagedimno;
1803 state.currently <- Loading (l, state.gen);
1804 | Some opaque ->
1805 tilepage l.pageno opaque pages;
1806 loop rest
1807 end;
1808 | _ -> ()
1810 if nogeomcmds state.geomcmds
1811 then loop pages
1814 let preload pages =
1815 load pages;
1816 if conf.preload && state.currently = Idle
1817 then load (preloadlayout state.y);
1820 let layoutready layout =
1821 let rec fold all ls =
1822 all && match ls with
1823 | l :: rest ->
1824 let seen = ref false in
1825 let allvisible = ref true in
1826 let foo col row _ _ _ _ _ _ =
1827 seen := true;
1828 allvisible := !allvisible &&
1829 begin match gettileopaque l col row with
1830 | Some _ -> true
1831 | None -> false
1834 itertiles l foo;
1835 fold (!seen && !allvisible) rest
1836 | [] -> true
1838 let alltilesvisible = fold true layout in
1839 alltilesvisible;
1842 let gotoy y =
1843 let y = bound y 0 state.maxy in
1844 let y, layout, proceed =
1845 match conf.maxwait with
1846 | Some time when state.ghyll == noghyll ->
1847 begin match state.throttle with
1848 | None ->
1849 let layout = layout y state.winh in
1850 let ready = layoutready layout in
1851 if not ready
1852 then (
1853 load layout;
1854 state.throttle <- Some (layout, y, now ());
1856 else G.postRedisplay "gotoy showall (None)";
1857 y, layout, ready
1858 | Some (_, _, started) ->
1859 let dt = now () -. started in
1860 if dt > time
1861 then (
1862 state.throttle <- None;
1863 let layout = layout y state.winh in
1864 load layout;
1865 G.postRedisplay "maxwait";
1866 y, layout, true
1868 else -1, [], false
1871 | _ ->
1872 let layout = layout y state.winh in
1873 if not !wtmode || layoutready layout
1874 then G.postRedisplay "gotoy ready";
1875 y, layout, true
1877 if proceed
1878 then (
1879 state.y <- y;
1880 state.layout <- layout;
1881 begin match state.mode with
1882 | LinkNav (Ltexact (pageno, linkno)) ->
1883 let rec loop = function
1884 | [] ->
1885 state.mode <- LinkNav (Ltgendir 0)
1886 | l :: _ when l.pageno = pageno ->
1887 begin match getopaque pageno with
1888 | None ->
1889 state.mode <- LinkNav (Ltgendir 0)
1890 | Some opaque ->
1891 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1892 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1893 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1894 then state.mode <- LinkNav (Ltgendir 0)
1896 | _ :: rest -> loop rest
1898 loop layout
1899 | _ -> ()
1900 end;
1901 begin match state.mode with
1902 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1903 if not (pagevisible layout pageno)
1904 then (
1905 match state.layout with
1906 | [] -> ()
1907 | l :: _ ->
1908 state.mode <- Birdseye (
1909 conf, leftx, l.pageno, hooverpageno, anchor
1912 | LinkNav (Ltgendir dir as lt) ->
1913 let linknav =
1914 let rec loop = function
1915 | [] -> lt
1916 | l :: rest ->
1917 match getopaque l.pageno with
1918 | None -> loop rest
1919 | Some opaque ->
1920 let link =
1921 let ld =
1922 if dir = 0
1923 then LDfirstvisible (l.pagex, l.pagey, dir)
1924 else (
1925 if dir > 0 then LDfirst else LDlast
1928 findlink opaque ld
1930 match link with
1931 | Lnotfound -> loop rest
1932 | Lfound n ->
1933 showlinktype (getlink opaque n);
1934 Ltexact (l.pageno, n)
1936 loop state.layout
1938 state.mode <- LinkNav linknav
1939 | _ -> ()
1940 end;
1941 preload layout;
1943 state.ghyll <- noghyll;
1944 if conf.updatecurs
1945 then (
1946 let mx, my = state.mpos in
1947 updateunder mx my;
1951 let conttiling pageno opaque =
1952 tilepage pageno opaque
1953 (if conf.preload then preloadlayout state.y else state.layout)
1956 let gotoy_and_clear_text y =
1957 if not conf.verbose then state.text <- "";
1958 gotoy y;
1961 let getanchor1 l =
1962 let top =
1963 let coloff = l.pagecol * l.pageh in
1964 float (l.pagey + coloff) /. float l.pageh
1966 let dtop =
1967 if l.pagedispy = 0
1968 then
1970 else (
1971 if conf.presentation
1972 then float l.pagedispy /. float (calcips l.pageh)
1973 else float l.pagedispy /. float conf.interpagespace
1976 (l.pageno, top, dtop)
1979 let getanchor () =
1980 match state.layout with
1981 | l :: _ -> getanchor1 l
1982 | [] ->
1983 let n = page_of_y state.y in
1984 if n = -1
1985 then state.anchor
1986 else
1987 let y, h = getpageyh n in
1988 let dy = y - state.y in
1989 let dtop =
1990 if conf.presentation
1991 then
1992 let ips = calcips h in
1993 float (dy + ips) /. float ips
1994 else
1995 float dy /. float conf.interpagespace
1997 (n, 0.0, dtop)
2000 let getanchory (n, top, dtop) =
2001 let y, h = getpageyh n in
2002 if conf.presentation
2003 then
2004 let ips = calcips h in
2005 y + truncate (top*.float h -. dtop*.float ips) + ips;
2006 else
2007 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
2010 let gotoanchor anchor =
2011 gotoy (getanchory anchor);
2014 let addnav () =
2015 cbput state.hists.nav (getanchor ());
2018 let getnav dir =
2019 let anchor = cbgetc state.hists.nav dir in
2020 getanchory anchor;
2023 let gotoghyll y =
2024 let scroll f n a b =
2025 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
2026 let snake f a b =
2027 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
2028 if f < a
2029 then s (float f /. float a)
2030 else (
2031 if f > b
2032 then 1.0 -. s ((float (f-b) /. float (n-b)))
2033 else 1.0
2036 snake f a b
2037 and summa n a b =
2038 let ins = float a *. 0.5
2039 and outs = float (n-b) *. 0.5 in
2040 let ones = b - a in
2041 ins +. outs +. float ones
2043 let rec set (_N, _A, _B) y sy =
2044 let sum = summa _N _A _B in
2045 let dy = float (y - sy) in
2046 state.ghyll <- (
2047 let rec gf n y1 o =
2048 if n >= _N
2049 then state.ghyll <- noghyll
2050 else
2051 let go n =
2052 let s = scroll n _N _A _B in
2053 let y1 = y1 +. ((s *. dy) /. sum) in
2054 gotoy_and_clear_text (truncate y1);
2055 state.ghyll <- gf (n+1) y1;
2057 match o with
2058 | None -> go n
2059 | Some y' -> set (_N/2, 1, 1) y' state.y
2061 gf 0 (float state.y)
2064 match conf.ghyllscroll with
2065 | None ->
2066 gotoy_and_clear_text y
2067 | Some nab ->
2068 if state.ghyll == noghyll
2069 then set nab y state.y
2070 else state.ghyll (Some y)
2073 let gotopage n top =
2074 let y, h = getpageyh n in
2075 let y = y + (truncate (top *. float h)) in
2076 gotoghyll y
2079 let gotopage1 n top =
2080 let y = getpagey n in
2081 let y = y + top in
2082 gotoghyll y
2085 let invalidate s f =
2086 state.layout <- [];
2087 state.pdims <- [];
2088 state.rects <- [];
2089 state.rects1 <- [];
2090 match state.geomcmds with
2091 | ps, [] when emptystr ps ->
2092 f ();
2093 state.geomcmds <- s, [];
2095 | ps, [] ->
2096 state.geomcmds <- ps, [s, f];
2098 | ps, (s', _) :: rest when s' = s ->
2099 state.geomcmds <- ps, ((s, f) :: rest);
2101 | ps, cmds ->
2102 state.geomcmds <- ps, ((s, f) :: cmds);
2105 let flushpages () =
2106 Hashtbl.iter (fun _ opaque ->
2107 wcmd "freepage %s" opaque;
2108 ) state.pagemap;
2109 Hashtbl.clear state.pagemap;
2112 let flushtiles () =
2113 if not (Queue.is_empty state.tilelru)
2114 then (
2115 Queue.iter (fun (k, p, s) ->
2116 wcmd "freetile %s" p;
2117 state.memused <- state.memused - s;
2118 Hashtbl.remove state.tilemap k;
2119 ) state.tilelru;
2120 state.uioh#infochanged Memused;
2121 Queue.clear state.tilelru;
2123 load state.layout;
2126 let stateh h =
2127 let h = truncate (float h*.conf.zoom) in
2128 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2129 h - d
2132 let opendoc path password =
2133 state.path <- path;
2134 state.password <- password;
2135 state.gen <- state.gen + 1;
2136 state.docinfo <- [];
2138 flushpages ();
2139 setaalevel conf.aalevel;
2140 let titlepath =
2141 if emptystr state.origin
2142 then path
2143 else state.origin
2145 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2146 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2147 invalidate "reqlayout"
2148 (fun () ->
2149 wcmd "reqlayout %d %d %d %s\000"
2150 conf.angle (FMTE.to_int conf.fitmodel)
2151 (stateh state.winh) state.nameddest
2155 let reload () =
2156 state.anchor <- getanchor ();
2157 opendoc state.path state.password;
2160 let scalecolor c =
2161 let c = c *. conf.colorscale in
2162 (c, c, c);
2165 let scalecolor2 (r, g, b) =
2166 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2169 let docolumns = function
2170 | Csingle _ ->
2171 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2172 let rec loop pageno pdimno pdim y ph pdims =
2173 if pageno = state.pagecount
2174 then ()
2175 else
2176 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2177 match pdims with
2178 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2179 pdimno+1, pdim, rest
2180 | _ ->
2181 pdimno, pdim, pdims
2183 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2184 let y = y +
2185 (if conf.presentation
2186 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2187 else (if pageno = 0 then 0 else conf.interpagespace)
2190 a.(pageno) <- (pdimno, x, y, pdim);
2191 loop (pageno+1) pdimno pdim (y + h) h pdims
2193 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2194 conf.columns <- Csingle a;
2196 | Cmulti ((columns, coverA, coverB), _) ->
2197 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2198 let rec loop pageno pdimno pdim x y rowh pdims =
2199 let rec fixrow m = if m = pageno then () else
2200 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2201 if h < rowh
2202 then (
2203 let y = y + (rowh - h) / 2 in
2204 a.(m) <- (pdimno, x, y, pdim);
2206 fixrow (m+1)
2208 if pageno = state.pagecount
2209 then fixrow (((pageno - 1) / columns) * columns)
2210 else
2211 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2212 match pdims with
2213 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2214 pdimno+1, pdim, rest
2215 | _ ->
2216 pdimno, pdim, pdims
2218 let x, y, rowh' =
2219 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2220 then (
2221 let x = (wadjsb state.winw - w) / 2 in
2222 let ips =
2223 if conf.presentation then calcips h else conf.interpagespace in
2224 x, y + ips + rowh, h
2226 else (
2227 if (pageno - coverA) mod columns = 0
2228 then (
2229 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2230 let y =
2231 if conf.presentation
2232 then
2233 let ips = calcips h in
2234 y + (if pageno = 0 then 0 else calcips rowh + ips)
2235 else
2236 y + (if pageno = 0 then 0 else conf.interpagespace)
2238 x, y + rowh, h
2240 else x, y, max rowh h
2243 let y =
2244 if pageno > 1 && (pageno - coverA) mod columns = 0
2245 then (
2246 let y =
2247 if pageno = columns && conf.presentation
2248 then (
2249 let ips = calcips rowh in
2250 for i = 0 to pred columns
2252 let (pdimno, x, y, pdim) = a.(i) in
2253 a.(i) <- (pdimno, x, y+ips, pdim)
2254 done;
2255 y+ips;
2257 else y
2259 fixrow (pageno - columns);
2262 else y
2264 a.(pageno) <- (pdimno, x, y, pdim);
2265 let x = x + w + xoff*2 + conf.interpagespace in
2266 loop (pageno+1) pdimno pdim x y rowh' pdims
2268 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2269 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2271 | Csplit (c, _) ->
2272 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2273 let rec loop pageno pdimno pdim y pdims =
2274 if pageno = state.pagecount
2275 then ()
2276 else
2277 let pdimno, ((_, w, h, _) as pdim), pdims =
2278 match pdims with
2279 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2280 pdimno+1, pdim, rest
2281 | _ ->
2282 pdimno, pdim, pdims
2284 let cw = w / c in
2285 let rec loop1 n x y =
2286 if n = c then y else (
2287 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2288 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2291 let y = loop1 0 0 y in
2292 loop (pageno+1) pdimno pdim y pdims
2294 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2295 conf.columns <- Csplit (c, a);
2298 let represent () =
2299 docolumns conf.columns;
2300 state.maxy <- calcheight ();
2301 if state.reprf == noreprf
2302 then (
2303 match state.mode with
2304 | Birdseye (_, _, pageno, _, _) ->
2305 let y, h = getpageyh pageno in
2306 let top = (state.winh - h) / 2 in
2307 gotoy (max 0 (y - top))
2308 | _ -> gotoanchor state.anchor
2310 else (
2311 state.reprf ();
2312 state.reprf <- noreprf;
2316 let reshape w h =
2317 GlDraw.viewport 0 0 w h;
2318 let firsttime = state.geomcmds == firstgeomcmds in
2319 if not firsttime && nogeomcmds state.geomcmds
2320 then state.anchor <- getanchor ();
2322 state.winw <- w;
2323 let w = wadjsb (truncate (float w *. conf.zoom)) in
2324 let w = max w 2 in
2325 state.winh <- h;
2326 setfontsize fstate.fontsize;
2327 GlMat.mode `modelview;
2328 GlMat.load_identity ();
2330 GlMat.mode `projection;
2331 GlMat.load_identity ();
2332 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2333 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2334 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2336 let relx =
2337 if conf.zoom <= 1.0
2338 then 0.0
2339 else float state.x /. float state.w
2341 invalidate "geometry"
2342 (fun () ->
2343 state.w <- w;
2344 if not firsttime
2345 then state.x <- truncate (relx *. float w);
2346 let w =
2347 match conf.columns with
2348 | Csingle _ -> w
2349 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2350 | Csplit (c, _) -> w * c
2352 wcmd "geometry %d %d %d"
2353 w (stateh h) (FMTE.to_int conf.fitmodel)
2357 let enttext () =
2358 let len = String.length state.text in
2359 let drawstring s =
2360 let hscrollh =
2361 match state.mode with
2362 | Textentry _ | View | LinkNav _ ->
2363 let h, _, _ = state.uioh#scrollpw in
2365 | _ -> 0
2367 let rect x w =
2368 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2369 (x+.w) (float (state.winh - hscrollh))
2372 let w = float (wadjsb state.winw - 1) in
2373 if state.progress >= 0.0 && state.progress < 1.0
2374 then (
2375 GlDraw.color (0.3, 0.3, 0.3);
2376 let w1 = w *. state.progress in
2377 rect 0.0 w1;
2378 GlDraw.color (0.0, 0.0, 0.0);
2379 rect w1 (w-.w1)
2381 else (
2382 GlDraw.color (0.0, 0.0, 0.0);
2383 rect 0.0 w;
2386 GlDraw.color (1.0, 1.0, 1.0);
2387 drawstring fstate.fontsize
2388 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2390 let s =
2391 match state.mode with
2392 | Textentry ((prefix, text, _, _, _, _), _) ->
2393 let s =
2394 if len > 0
2395 then
2396 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2397 else
2398 Printf.sprintf "%s%s_" prefix text
2402 | _ -> state.text
2404 let s =
2405 if state.newerrmsgs
2406 then (
2407 if not (istextentry state.mode) && state.uioh#eformsgs
2408 then
2409 let s1 = "(press 'e' to review error messasges)" in
2410 if nonemptystr s then s ^ " " ^ s1 else s1
2411 else s
2413 else s
2415 if nonemptystr s
2416 then drawstring s
2419 let gctiles () =
2420 let len = Queue.length state.tilelru in
2421 let layout = lazy (
2422 match state.throttle with
2423 | None ->
2424 if conf.preload
2425 then preloadlayout state.y
2426 else state.layout
2427 | Some (layout, _, _) ->
2428 layout
2429 ) in
2430 let rec loop qpos =
2431 if state.memused <= conf.memlimit
2432 then ()
2433 else (
2434 if qpos < len
2435 then
2436 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2437 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2438 let (_, pw, ph, _) = getpagedim n in
2440 gen = state.gen
2441 && colorspace = conf.colorspace
2442 && angle = conf.angle
2443 && pagew = pw
2444 && pageh = ph
2445 && (
2446 let x = col*conf.tilew
2447 and y = row*conf.tileh in
2448 tilevisible (Lazy.force_val layout) n x y
2450 then Queue.push lruitem state.tilelru
2451 else (
2452 freepbo p;
2453 wcmd "freetile %s" p;
2454 state.memused <- state.memused - s;
2455 state.uioh#infochanged Memused;
2456 Hashtbl.remove state.tilemap k;
2458 loop (qpos+1)
2461 loop 0
2464 let logcurrently = function
2465 | Idle -> dolog "Idle"
2466 | Loading (l, gen) ->
2467 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2468 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2469 dolog
2470 "Tiling %d[%d,%d] page=%s cs=%s angle"
2471 l.pageno col row pageopaque
2472 (CSTE.to_string colorspace)
2474 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2475 angle gen conf.angle state.gen
2476 tilew tileh
2477 conf.tilew conf.tileh
2479 | Outlining _ ->
2480 dolog "outlining"
2483 let splitatspace =
2484 let r = Str.regexp " " in
2485 fun s -> Str.bounded_split r s 2;
2488 let onpagerect pageno f =
2489 let b =
2490 match conf.columns with
2491 | Cmulti (_, b) -> b
2492 | Csingle b -> b
2493 | Csplit (_, b) -> b
2495 if pageno >= 0 && pageno < Array.length b
2496 then
2497 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2498 f w h
2501 let gotopagexy1 pageno x y =
2502 let _,w1,h1,leftx = getpagedim pageno in
2503 let top = y /. (float h1) in
2504 let left = x /. (float w1) in
2505 let py, w, h = getpageywh pageno in
2506 let wh = state.winh - hscrollh () in
2507 let x = left *. (float w) in
2508 let x = leftx + state.x + truncate x in
2509 let sx =
2510 if x < 0 || x >= wadjsb state.winw
2511 then state.x - x
2512 else state.x
2514 let pdy = truncate (top *. float h) in
2515 let y' = py + pdy in
2516 let dy = y' - state.y in
2517 let sy =
2518 if x != state.x || not (dy > 0 && dy < wh)
2519 then (
2520 if conf.presentation
2521 then
2522 if abs (py - y') > wh
2523 then y'
2524 else py
2525 else y';
2527 else state.y
2529 if state.x != sx || state.y != sy
2530 then (
2531 let x, y =
2532 if !wtmode
2533 then (
2534 let ww = wadjsb state.winw in
2535 let qx = sx / ww
2536 and qy = pdy / wh in
2537 let x = qx * ww
2538 and y = py + qy * wh in
2539 let x = if -x + ww > w1 then -(w1-ww) else x
2540 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2541 let y =
2542 if conf.presentation
2543 then
2544 if abs (py - y') > wh
2545 then y'
2546 else py
2547 else y';
2549 (x, y)
2551 else (sx, sy)
2553 state.x <- x;
2554 gotoy_and_clear_text y;
2556 else gotoy_and_clear_text state.y;
2559 let gotopagexy pageno x y =
2560 match state.mode with
2561 | Birdseye _ -> gotopage pageno 0.0
2562 | _ -> gotopagexy1 pageno x y
2565 let act cmds =
2566 (* dolog "%S" cmds; *)
2567 let cl = splitatspace cmds in
2568 let scan s fmt f =
2569 try Scanf.sscanf s fmt f
2570 with exn ->
2571 dolog "error processing '%S': %s" cmds (exntos exn);
2572 exit 1
2574 let addoutline outline =
2575 match state.currently with
2576 | Outlining outlines ->
2577 state.currently <- Outlining (outline :: outlines)
2578 | Idle -> state.currently <- Outlining [outline]
2579 | currently ->
2580 dolog "invalid outlining state";
2581 logcurrently currently
2583 match cl with
2584 | "clear" :: [] ->
2585 state.uioh#infochanged Pdim;
2586 state.pdims <- [];
2588 | "clearrects" :: [] ->
2589 state.rects <- state.rects1;
2590 G.postRedisplay "clearrects";
2592 | "continue" :: args :: [] ->
2593 let n = scan args "%u" (fun n -> n) in
2594 state.pagecount <- n;
2595 begin match state.currently with
2596 | Outlining l ->
2597 state.currently <- Idle;
2598 state.outlines <- Array.of_list (List.rev l)
2599 | _ -> ()
2600 end;
2602 let cur, cmds = state.geomcmds in
2603 if emptystr cur
2604 then failwith "umpossible";
2606 begin match List.rev cmds with
2607 | [] ->
2608 state.geomcmds <- "", [];
2609 represent ();
2610 | (s, f) :: rest ->
2611 f ();
2612 state.geomcmds <- s, List.rev rest;
2613 end;
2614 if conf.maxwait = None && not !wtmode
2615 then G.postRedisplay "continue";
2617 | "title" :: args :: [] ->
2618 Wsi.settitle args
2620 | "msg" :: args :: [] ->
2621 showtext ' ' args
2623 | "vmsg" :: args :: [] ->
2624 if conf.verbose
2625 then showtext ' ' args
2627 | "emsg" :: args :: [] ->
2628 Buffer.add_string state.errmsgs args;
2629 state.newerrmsgs <- true;
2630 G.postRedisplay "error message"
2632 | "progress" :: args :: [] ->
2633 let progress, text =
2634 scan args "%f %n"
2635 (fun f pos ->
2636 f, String.sub args pos (String.length args - pos))
2638 state.text <- text;
2639 state.progress <- progress;
2640 G.postRedisplay "progress"
2642 | "firstmatch" :: args :: [] ->
2643 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2644 scan args "%u %d %f %f %f %f %f %f %f %f"
2645 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2646 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2648 let y = (getpagey pageno) + truncate y0 in
2649 addnav ();
2650 gotoy y;
2651 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2653 | "match" :: args :: [] ->
2654 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2655 scan args "%u %d %f %f %f %f %f %f %f %f"
2656 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2657 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2659 state.rects1 <-
2660 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2662 | "page" :: args :: [] ->
2663 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2664 begin match state.currently with
2665 | Loading (l, gen) ->
2666 vlog "page %d took %f sec" l.pageno t;
2667 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2668 begin match state.throttle with
2669 | None ->
2670 let preloadedpages =
2671 if conf.preload
2672 then preloadlayout state.y
2673 else state.layout
2675 let evict () =
2676 let set =
2677 List.fold_left (fun s l -> IntSet.add l.pageno s)
2678 IntSet.empty preloadedpages
2680 let evictedpages =
2681 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2682 if not (IntSet.mem pageno set)
2683 then (
2684 wcmd "freepage %s" opaque;
2685 key :: accu
2687 else accu
2688 ) state.pagemap []
2690 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2692 evict ();
2693 state.currently <- Idle;
2694 if gen = state.gen
2695 then (
2696 tilepage l.pageno pageopaque state.layout;
2697 load state.layout;
2698 load preloadedpages;
2699 if pagevisible state.layout l.pageno
2700 && layoutready state.layout
2701 then G.postRedisplay "page";
2704 | Some (layout, _, _) ->
2705 state.currently <- Idle;
2706 tilepage l.pageno pageopaque layout;
2707 load state.layout
2708 end;
2710 | _ ->
2711 dolog "Inconsistent loading state";
2712 logcurrently state.currently;
2713 exit 1
2716 | "tile" :: args :: [] ->
2717 let (x, y, opaque, size, t) =
2718 scan args "%u %u %s %u %f"
2719 (fun x y p size t -> (x, y, p, size, t))
2721 begin match state.currently with
2722 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2723 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2725 unmappbo opaque;
2726 if tilew != conf.tilew || tileh != conf.tileh
2727 then (
2728 wcmd "freetile %s" opaque;
2729 state.currently <- Idle;
2730 load state.layout;
2732 else (
2733 puttileopaque l col row gen cs angle opaque size t;
2734 state.memused <- state.memused + size;
2735 state.uioh#infochanged Memused;
2736 gctiles ();
2737 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2738 opaque, size) state.tilelru;
2740 let layout =
2741 match state.throttle with
2742 | None -> state.layout
2743 | Some (layout, _, _) -> layout
2746 state.currently <- Idle;
2747 if gen = state.gen
2748 && conf.colorspace = cs
2749 && conf.angle = angle
2750 && tilevisible layout l.pageno x y
2751 then conttiling l.pageno pageopaque;
2753 begin match state.throttle with
2754 | None ->
2755 preload state.layout;
2756 if gen = state.gen
2757 && conf.colorspace = cs
2758 && conf.angle = angle
2759 && tilevisible state.layout l.pageno x y
2760 && (not !wtmode || layoutready state.layout)
2761 then G.postRedisplay "tile nothrottle";
2763 | Some (layout, y, _) ->
2764 let ready = layoutready layout in
2765 if ready
2766 then (
2767 state.y <- y;
2768 state.layout <- layout;
2769 state.throttle <- None;
2770 G.postRedisplay "throttle";
2772 else load layout;
2773 end;
2776 | _ ->
2777 dolog "Inconsistent tiling state";
2778 logcurrently state.currently;
2779 exit 1
2782 | "pdim" :: args :: [] ->
2783 let (n, w, h, _) as pdim =
2784 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2786 let pdim =
2787 match conf.fitmodel, conf.columns with
2788 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2789 | _ -> pdim
2791 state.uioh#infochanged Pdim;
2792 state.pdims <- pdim :: state.pdims
2794 | "o" :: args :: [] ->
2795 let (l, n, t, h, pos) =
2796 scan args "%u %u %d %u %n"
2797 (fun l n t h pos -> l, n, t, h, pos)
2799 let s = String.sub args pos (String.length args - pos) in
2800 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
2802 | "ou" :: args :: [] ->
2803 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
2804 let s = String.sub args pos len in
2805 let pos2 = pos + len + 1 in
2806 let uri = String.sub args pos2 (String.length args - pos2) in
2807 addoutline (s, l, Ouri uri)
2809 | "on" :: args :: [] ->
2810 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
2811 let s = String.sub args pos (String.length args - pos) in
2812 addoutline (s, l, Onone)
2814 | "a" :: args :: [] ->
2815 let (n, l, t) =
2816 scan args "%u %d %d" (fun n l t -> n, l, t)
2818 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2820 | "info" :: args :: [] ->
2821 state.docinfo <- (1, args) :: state.docinfo
2823 | "infoend" :: [] ->
2824 state.uioh#infochanged Docinfo;
2825 state.docinfo <- List.rev state.docinfo
2827 | _ ->
2828 error "unknown cmd `%S'" cmds
2831 let onhist cb =
2832 let rc = cb.rc in
2833 let action = function
2834 | HCprev -> cbget cb ~-1
2835 | HCnext -> cbget cb 1
2836 | HCfirst -> cbget cb ~-(cb.rc)
2837 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2838 and cancel () = cb.rc <- rc
2839 in (action, cancel)
2842 let search pattern forward =
2843 match conf.columns with
2844 | Csplit _ ->
2845 showtext '!' "searching does not work properly in split columns mode"
2846 | _ ->
2847 if nonemptystr pattern
2848 then
2849 let pn, py =
2850 match state.layout with
2851 | [] -> 0, 0
2852 | l :: _ ->
2853 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2855 wcmd "search %d %d %d %d,%s\000"
2856 (btod conf.icase) pn py (btod forward) pattern;
2859 let intentry text key =
2860 let c =
2861 if key >= 32 && key < 127
2862 then Char.chr key
2863 else '\000'
2865 match c with
2866 | '0' .. '9' ->
2867 let text = addchar text c in
2868 TEcont text
2870 | _ ->
2871 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2872 TEcont text
2875 let linknentry text key =
2876 let c =
2877 if key >= 32 && key < 127
2878 then Char.chr key
2879 else '\000'
2881 match c with
2882 | 'a' .. 'z' ->
2883 let text = addchar text c in
2884 TEcont text
2886 | _ ->
2887 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2888 TEcont text
2891 let linkndone f s =
2892 if nonemptystr s
2893 then (
2894 let n =
2895 let l = String.length s in
2896 let rec loop pos n = if pos = l then n else
2897 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2898 loop (pos+1) (n*26 + m)
2899 in loop 0 0
2901 let rec loop n = function
2902 | [] -> ()
2903 | l :: rest ->
2904 match getopaque l.pageno with
2905 | None -> loop n rest
2906 | Some opaque ->
2907 let m = getlinkcount opaque in
2908 if n < m
2909 then (
2910 let under = getlink opaque n in
2911 f under
2913 else loop (n-m) rest
2915 loop n state.layout;
2919 let textentry text key =
2920 if key land 0xff00 = 0xff00
2921 then TEcont text
2922 else TEcont (text ^ toutf8 key)
2925 let reqlayout angle fitmodel =
2926 match state.throttle with
2927 | None ->
2928 if nogeomcmds state.geomcmds
2929 then state.anchor <- getanchor ();
2930 conf.angle <- angle mod 360;
2931 if conf.angle != 0
2932 then (
2933 match state.mode with
2934 | LinkNav _ -> state.mode <- View
2935 | _ -> ()
2937 conf.fitmodel <- fitmodel;
2938 invalidate "reqlayout"
2939 (fun () ->
2940 wcmd "reqlayout %d %d %d"
2941 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2943 | _ -> ()
2946 let settrim trimmargins trimfuzz =
2947 if nogeomcmds state.geomcmds
2948 then state.anchor <- getanchor ();
2949 conf.trimmargins <- trimmargins;
2950 conf.trimfuzz <- trimfuzz;
2951 let x0, y0, x1, y1 = trimfuzz in
2952 invalidate "settrim"
2953 (fun () ->
2954 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2955 flushpages ();
2958 let setzoom zoom =
2959 match state.throttle with
2960 | None ->
2961 let zoom = max 0.0001 zoom in
2962 if zoom <> conf.zoom
2963 then (
2964 state.prevzoom <- (conf.zoom, state.x);
2965 conf.zoom <- zoom;
2966 reshape state.winw state.winh;
2967 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2970 | Some (layout, y, started) ->
2971 let time =
2972 match conf.maxwait with
2973 | None -> 0.0
2974 | Some t -> t
2976 let dt = now () -. started in
2977 if dt > time
2978 then (
2979 state.y <- y;
2980 load layout;
2984 let setcolumns mode columns coverA coverB =
2985 state.prevcolumns <- Some (conf.columns, conf.zoom);
2986 if columns < 0
2987 then (
2988 if isbirdseye mode
2989 then showtext '!' "split mode doesn't work in bird's eye"
2990 else (
2991 conf.columns <- Csplit (-columns, [||]);
2992 state.x <- 0;
2993 conf.zoom <- 1.0;
2996 else (
2997 if columns < 2
2998 then (
2999 conf.columns <- Csingle [||];
3000 state.x <- 0;
3001 setzoom 1.0;
3003 else (
3004 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
3005 conf.zoom <- 1.0;
3008 reshape state.winw state.winh;
3011 let enterbirdseye () =
3012 let zoom = float conf.thumbw /. float state.winw in
3013 let birdseyepageno =
3014 let cy = state.winh / 2 in
3015 let fold = function
3016 | [] -> 0
3017 | l :: rest ->
3018 let rec fold best = function
3019 | [] -> best.pageno
3020 | l :: rest ->
3021 let d = cy - (l.pagedispy + l.pagevh/2)
3022 and dbest = cy - (best.pagedispy + best.pagevh/2) in
3023 if abs d < abs dbest
3024 then fold l rest
3025 else best.pageno
3026 in fold l rest
3028 fold state.layout
3030 state.mode <- Birdseye (
3031 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
3033 conf.zoom <- zoom;
3034 conf.presentation <- false;
3035 conf.interpagespace <- 10;
3036 conf.hlinks <- false;
3037 conf.fitmodel <- FitProportional;
3038 state.x <- 0;
3039 state.mstate <- Mnone;
3040 conf.maxwait <- None;
3041 conf.columns <- (
3042 match conf.beyecolumns with
3043 | Some c ->
3044 conf.zoom <- 1.0;
3045 Cmulti ((c, 0, 0), [||])
3046 | None -> Csingle [||]
3048 Wsi.setcursor Wsi.CURSOR_INHERIT;
3049 if conf.verbose
3050 then
3051 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3052 (100.0*.zoom)
3053 else
3054 state.text <- ""
3056 reshape state.winw state.winh;
3059 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3060 state.mode <- View;
3061 conf.zoom <- c.zoom;
3062 conf.presentation <- c.presentation;
3063 conf.interpagespace <- c.interpagespace;
3064 conf.maxwait <- c.maxwait;
3065 conf.hlinks <- c.hlinks;
3066 conf.fitmodel <- c.fitmodel;
3067 conf.beyecolumns <- (
3068 match conf.columns with
3069 | Cmulti ((c, _, _), _) -> Some c
3070 | Csingle _ -> None
3071 | Csplit _ -> failwith "leaving bird's eye split mode"
3073 conf.columns <- (
3074 match c.columns with
3075 | Cmulti (c, _) -> Cmulti (c, [||])
3076 | Csingle _ -> Csingle [||]
3077 | Csplit (c, _) -> Csplit (c, [||])
3079 if conf.verbose
3080 then
3081 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3082 (100.0*.conf.zoom)
3084 reshape state.winw state.winh;
3085 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3086 state.x <- leftx;
3089 let togglebirdseye () =
3090 match state.mode with
3091 | Birdseye vals -> leavebirdseye vals true
3092 | View -> enterbirdseye ()
3093 | _ -> ()
3096 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3097 let pageno = max 0 (pageno - incr) in
3098 let rec loop = function
3099 | [] -> gotopage1 pageno 0
3100 | l :: _ when l.pageno = pageno ->
3101 if l.pagedispy >= 0 && l.pagey = 0
3102 then G.postRedisplay "upbirdseye"
3103 else gotopage1 pageno 0
3104 | _ :: rest -> loop rest
3106 loop state.layout;
3107 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3110 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3111 let pageno = min (state.pagecount - 1) (pageno + incr) in
3112 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3113 let rec loop = function
3114 | [] ->
3115 let y, h = getpageyh pageno in
3116 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3117 gotoy (clamp dy)
3118 | l :: _ when l.pageno = pageno ->
3119 if l.pagevh != l.pageh
3120 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3121 else G.postRedisplay "downbirdseye"
3122 | _ :: rest -> loop rest
3124 loop state.layout
3127 let boundastep h step =
3128 if step < 0
3129 then bound step ~-h 0
3130 else bound step 0 h
3133 let optentry mode _ key =
3134 let btos b = if b then "on" else "off" in
3135 if key >= 32 && key < 127
3136 then
3137 let c = Char.chr key in
3138 match c with
3139 | 's' ->
3140 let ondone s =
3141 try conf.scrollstep <- int_of_string s with exc ->
3142 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3144 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3146 | 'A' ->
3147 let ondone s =
3149 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
3150 if state.autoscroll <> None
3151 then state.autoscroll <- Some conf.autoscrollstep
3152 with exc ->
3153 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3155 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3157 | 'C' ->
3158 let ondone s =
3160 let n, a, b = multicolumns_of_string s in
3161 setcolumns mode n a b;
3162 with exc ->
3163 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3165 TEswitch ("columns: ", "", None, textentry, ondone, true)
3167 | 'Z' ->
3168 let ondone s =
3170 let zoom = float (int_of_string s) /. 100.0 in
3171 setzoom zoom
3172 with exc ->
3173 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3175 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3177 | 't' ->
3178 let ondone s =
3180 conf.thumbw <- bound (int_of_string s) 2 4096;
3181 state.text <-
3182 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3183 begin match mode with
3184 | Birdseye beye ->
3185 leavebirdseye beye false;
3186 enterbirdseye ();
3187 | _ -> ();
3189 with exc ->
3190 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3192 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3194 | 'R' ->
3195 let ondone s =
3196 match try
3197 Some (int_of_string s)
3198 with exc ->
3199 state.text <- Printf.sprintf "bad integer `%s': %s"
3200 s (exntos exc);
3201 None
3202 with
3203 | Some angle -> reqlayout angle conf.fitmodel
3204 | None -> ()
3206 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3208 | 'i' ->
3209 conf.icase <- not conf.icase;
3210 TEdone ("case insensitive search " ^ (btos conf.icase))
3212 | 'p' ->
3213 conf.preload <- not conf.preload;
3214 gotoy state.y;
3215 TEdone ("preload " ^ (btos conf.preload))
3217 | 'v' ->
3218 conf.verbose <- not conf.verbose;
3219 TEdone ("verbose " ^ (btos conf.verbose))
3221 | 'd' ->
3222 conf.debug <- not conf.debug;
3223 TEdone ("debug " ^ (btos conf.debug))
3225 | 'h' ->
3226 conf.maxhfit <- not conf.maxhfit;
3227 state.maxy <- calcheight ();
3228 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3230 | 'c' ->
3231 conf.crophack <- not conf.crophack;
3232 TEdone ("crophack " ^ btos conf.crophack)
3234 | 'a' ->
3235 let s =
3236 match conf.maxwait with
3237 | None ->
3238 conf.maxwait <- Some infinity;
3239 "always wait for page to complete"
3240 | Some _ ->
3241 conf.maxwait <- None;
3242 "show placeholder if page is not ready"
3244 TEdone s
3246 | 'f' ->
3247 conf.underinfo <- not conf.underinfo;
3248 TEdone ("underinfo " ^ btos conf.underinfo)
3250 | 'P' ->
3251 conf.savebmarks <- not conf.savebmarks;
3252 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3254 | 'S' ->
3255 let ondone s =
3257 let pageno, py =
3258 match state.layout with
3259 | [] -> 0, 0
3260 | l :: _ ->
3261 l.pageno, l.pagey
3263 conf.interpagespace <- int_of_string s;
3264 docolumns conf.columns;
3265 state.maxy <- calcheight ();
3266 let y = getpagey pageno in
3267 gotoy (y + py)
3268 with exc ->
3269 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3271 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3273 | 'l' ->
3274 let fm =
3275 match conf.fitmodel with
3276 | FitProportional -> FitWidth
3277 | _ -> FitProportional
3279 reqlayout conf.angle fm;
3280 TEdone ("proportional display " ^ btos (fm == FitProportional))
3282 | 'T' ->
3283 settrim (not conf.trimmargins) conf.trimfuzz;
3284 TEdone ("trim margins " ^ btos conf.trimmargins)
3286 | 'I' ->
3287 conf.invert <- not conf.invert;
3288 TEdone ("invert colors " ^ btos conf.invert)
3290 | 'x' ->
3291 let ondone s =
3292 cbput state.hists.sel s;
3293 conf.selcmd <- s;
3295 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3296 textentry, ondone, true)
3298 | 'M' ->
3299 if conf.pax == None
3300 then conf.pax <- Some (ref (0.0, 0, 0))
3301 else conf.pax <- None;
3302 TEdone ("PAX " ^ btos (conf.pax != None))
3304 | _ ->
3305 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3306 TEstop
3307 else
3308 TEcont state.text
3311 class type lvsource = object
3312 method getitemcount : int
3313 method getitem : int -> (string * int)
3314 method hasaction : int -> bool
3315 method exit :
3316 uioh:uioh ->
3317 cancel:bool ->
3318 active:int ->
3319 first:int ->
3320 pan:int ->
3321 qsearch:string ->
3322 uioh option
3323 method getactive : int
3324 method getfirst : int
3325 method getqsearch : string
3326 method setqsearch : string -> unit
3327 method getpan : int
3328 end;;
3330 class virtual lvsourcebase = object
3331 val mutable m_active = 0
3332 val mutable m_first = 0
3333 val mutable m_qsearch = ""
3334 val mutable m_pan = 0
3335 method getactive = m_active
3336 method getfirst = m_first
3337 method getqsearch = m_qsearch
3338 method getpan = m_pan
3339 method setqsearch s = m_qsearch <- s
3340 end;;
3342 let withoutlastutf8 s =
3343 let len = String.length s in
3344 if len = 0
3345 then s
3346 else
3347 let rec find pos =
3348 if pos = 0
3349 then pos
3350 else
3351 let b = Char.code s.[pos] in
3352 if b land 0b11000000 = 0b11000000
3353 then pos
3354 else find (pos-1)
3356 let first =
3357 if Char.code s.[len-1] land 0x80 = 0
3358 then len-1
3359 else find (len-1)
3361 String.sub s 0 first;
3364 let textentrykeyboard
3365 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3366 let key =
3367 if key >= 0xffb0 && key <= 0xffb9
3368 then key - 0xffb0 + 48 else key
3370 let enttext te =
3371 state.mode <- Textentry (te, onleave);
3372 state.text <- "";
3373 enttext ();
3374 G.postRedisplay "textentrykeyboard enttext";
3376 let histaction cmd =
3377 match opthist with
3378 | None -> ()
3379 | Some (action, _) ->
3380 state.mode <- Textentry (
3381 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3383 G.postRedisplay "textentry histaction"
3385 match key with
3386 | 0xff08 -> (* backspace *)
3387 if emptystr text && cancelonempty
3388 then (
3389 onleave Cancel;
3390 G.postRedisplay "textentrykeyboard after cancel";
3392 else
3393 let s = withoutlastutf8 text in
3394 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3396 | 0xff0d | 0xff8d -> (* (kp) enter *)
3397 ondone text;
3398 onleave Confirm;
3399 G.postRedisplay "textentrykeyboard after confirm"
3401 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3402 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3403 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3404 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3406 | 0xff1b -> (* escape*)
3407 if emptystr text
3408 then (
3409 begin match opthist with
3410 | None -> ()
3411 | Some (_, onhistcancel) -> onhistcancel ()
3412 end;
3413 onleave Cancel;
3414 state.text <- "";
3415 G.postRedisplay "textentrykeyboard after cancel2"
3417 else (
3418 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3421 | 0xff9f | 0xffff -> () (* delete *)
3423 | _ when key != 0
3424 && key land 0xff00 != 0xff00 (* keyboard *)
3425 && key land 0xfe00 != 0xfe00 (* xkb *)
3426 && key land 0xfd00 != 0xfd00 (* 3270 *)
3428 begin match onkey text key with
3429 | TEdone text ->
3430 ondone text;
3431 onleave Confirm;
3432 G.postRedisplay "textentrykeyboard after confirm2";
3434 | TEcont text ->
3435 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3437 | TEstop ->
3438 onleave Cancel;
3439 G.postRedisplay "textentrykeyboard after cancel3"
3441 | TEswitch te ->
3442 state.mode <- Textentry (te, onleave);
3443 G.postRedisplay "textentrykeyboard switch";
3444 end;
3446 | _ ->
3447 vlog "unhandled key %s" (Wsi.keyname key)
3450 let firstof first active =
3451 if first > active || abs (first - active) > fstate.maxrows - 1
3452 then max 0 (active - (fstate.maxrows/2))
3453 else first
3456 let calcfirst first active =
3457 if active > first
3458 then
3459 let rows = active - first in
3460 if rows > fstate.maxrows then active - fstate.maxrows else first
3461 else active
3464 let scrollph y maxy =
3465 let sh = float (maxy + state.winh) /. float state.winh in
3466 let sh = float state.winh /. sh in
3467 let sh = max sh (float conf.scrollh) in
3469 let percent = float y /. float maxy in
3470 let position = (float state.winh -. sh) *. percent in
3472 let position =
3473 if position +. sh > float state.winh
3474 then float state.winh -. sh
3475 else position
3477 position, sh;
3480 let coe s = (s :> uioh);;
3482 class listview ~(source:lvsource) ~trusted ~modehash =
3483 object (self)
3484 val m_pan = source#getpan
3485 val m_first = source#getfirst
3486 val m_active = source#getactive
3487 val m_qsearch = source#getqsearch
3488 val m_prev_uioh = state.uioh
3490 method private elemunder y =
3491 let n = y / (fstate.fontsize+1) in
3492 if m_first + n < source#getitemcount
3493 then (
3494 if source#hasaction (m_first + n)
3495 then Some (m_first + n)
3496 else None
3498 else None
3500 method display =
3501 Gl.enable `blend;
3502 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3503 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3504 filledrect 0. 0. (float state.winw) (float state.winh);
3505 GlDraw.color (1., 1., 1.);
3506 Gl.enable `texture_2d;
3507 let fs = fstate.fontsize in
3508 let nfs = fs + 1 in
3509 let ww = fstate.wwidth in
3510 let tabw = 30.0*.ww in
3511 let itemcount = source#getitemcount in
3512 let rec loop row =
3513 if (row - m_first) > fstate.maxrows
3514 then ()
3515 else (
3516 if row >= 0 && row < itemcount
3517 then (
3518 let (s, level) = source#getitem row in
3519 let y = (row - m_first) * nfs in
3520 let x = 5.0 +. float (level + m_pan) *. ww in
3521 if row = m_active
3522 then (
3523 Gl.disable `texture_2d;
3524 let alpha = if source#hasaction row then 0.9 else 0.3 in
3525 GlDraw.color (1., 1., 1.) ~alpha;
3526 linerect 1. (float (y + 1))
3527 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3528 GlDraw.color (1., 1., 1.);
3529 Gl.enable `texture_2d;
3532 let drawtabularstring s =
3533 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3534 if trusted
3535 then
3536 let tabpos = try String.index s '\t' with Not_found -> -1 in
3537 if tabpos > 0
3538 then
3539 let len = String.length s - tabpos - 1 in
3540 let s1 = String.sub s 0 tabpos
3541 and s2 = String.sub s (tabpos + 1) len in
3542 let nx = drawstr x s1 in
3543 let sw = nx -. x in
3544 let x = x +. (max tabw sw) in
3545 drawstr x s2
3546 else
3547 drawstr x s
3548 else
3549 drawstr x s
3551 let _ = drawtabularstring s in
3552 loop (row+1)
3556 loop m_first;
3557 Gl.disable `blend;
3558 Gl.disable `texture_2d;
3560 method updownlevel incr =
3561 let len = source#getitemcount in
3562 let curlevel =
3563 if m_active >= 0 && m_active < len
3564 then snd (source#getitem m_active)
3565 else -1
3567 let rec flow i =
3568 if i = len then i-1 else if i = -1 then 0 else
3569 let _, l = source#getitem i in
3570 if l != curlevel then i else flow (i+incr)
3572 let active = flow m_active in
3573 let first = calcfirst m_first active in
3574 G.postRedisplay "outline updownlevel";
3575 {< m_active = active; m_first = first >}
3577 method private key1 key mask =
3578 let set1 active first qsearch =
3579 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3581 let search active pattern incr =
3582 let active = if active = -1 then m_first else active in
3583 let dosearch re =
3584 let rec loop n =
3585 if n >= 0 && n < source#getitemcount
3586 then (
3587 let s, _ = source#getitem n in
3589 (try ignore (Str.search_forward re s 0); true
3590 with Not_found -> false)
3591 then Some n
3592 else loop (n + incr)
3594 else None
3596 loop active
3599 let re = Str.regexp_case_fold pattern in
3600 dosearch re
3601 with Failure s ->
3602 state.text <- s;
3603 None
3605 let itemcount = source#getitemcount in
3606 let find start incr =
3607 let rec find i =
3608 if i = -1 || i = itemcount
3609 then -1
3610 else (
3611 if source#hasaction i
3612 then i
3613 else find (i + incr)
3616 find start
3618 let set active first =
3619 let first = bound first 0 (itemcount - fstate.maxrows) in
3620 state.text <- "";
3621 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3623 let navigate incr =
3624 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3625 let active, first =
3626 let incr1 = if incr > 0 then 1 else -1 in
3627 if isvisible m_first m_active
3628 then
3629 let next =
3630 let next = m_active + incr in
3631 let next =
3632 if next < 0 || next >= itemcount
3633 then -1
3634 else find next incr1
3636 if abs (m_active - next) > fstate.maxrows
3637 then -1
3638 else next
3640 if next = -1
3641 then
3642 let first = m_first + incr in
3643 let first = bound first 0 (itemcount - fstate.maxrows) in
3644 let next =
3645 let next = m_active + incr in
3646 let next = bound next 0 (itemcount - 1) in
3647 find next ~-incr1
3649 let active =
3650 if next = -1
3651 then m_active
3652 else (
3653 if isvisible first next
3654 then next
3655 else m_active
3658 active, first
3659 else
3660 let first = min next m_first in
3661 let first =
3662 if abs (next - first) > fstate.maxrows
3663 then first + incr
3664 else first
3666 next, first
3667 else
3668 let first = m_first + incr in
3669 let first = bound first 0 (itemcount - 1) in
3670 let active =
3671 let next = m_active + incr in
3672 let next = bound next 0 (itemcount - 1) in
3673 let next = find next incr1 in
3674 let active =
3675 if next = -1 || abs (m_active - first) > fstate.maxrows
3676 then (
3677 let active = if m_active = -1 then next else m_active in
3678 active
3680 else next
3682 if isvisible first active
3683 then active
3684 else -1
3686 active, first
3688 G.postRedisplay "listview navigate";
3689 set active first;
3691 match key with
3692 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3693 let incr = if key = 0x72 then -1 else 1 in
3694 let active, first =
3695 match search (m_active + incr) m_qsearch incr with
3696 | None ->
3697 state.text <- m_qsearch ^ " [not found]";
3698 m_active, m_first
3699 | Some active ->
3700 state.text <- m_qsearch;
3701 active, firstof m_first active
3703 G.postRedisplay "listview ctrl-r/s";
3704 set1 active first m_qsearch;
3706 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3707 if m_active >= 0 && m_active < source#getitemcount
3708 then (
3709 let s, _ = source#getitem m_active in
3710 selstring s;
3712 coe self
3714 | 0xff08 -> (* backspace *)
3715 if emptystr m_qsearch
3716 then coe self
3717 else (
3718 let qsearch = withoutlastutf8 m_qsearch in
3719 if emptystr qsearch
3720 then (
3721 state.text <- "";
3722 G.postRedisplay "listview empty qsearch";
3723 set1 m_active m_first "";
3725 else
3726 let active, first =
3727 match search m_active qsearch ~-1 with
3728 | None ->
3729 state.text <- qsearch ^ " [not found]";
3730 m_active, m_first
3731 | Some active ->
3732 state.text <- qsearch;
3733 active, firstof m_first active
3735 G.postRedisplay "listview backspace qsearch";
3736 set1 active first qsearch
3739 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3740 let pattern = m_qsearch ^ toutf8 key in
3741 let active, first =
3742 match search m_active pattern 1 with
3743 | None ->
3744 state.text <- pattern ^ " [not found]";
3745 m_active, m_first
3746 | Some active ->
3747 state.text <- pattern;
3748 active, firstof m_first active
3750 G.postRedisplay "listview qsearch add";
3751 set1 active first pattern;
3753 | 0xff1b -> (* escape *)
3754 state.text <- "";
3755 if emptystr m_qsearch
3756 then (
3757 G.postRedisplay "list view escape";
3758 begin
3759 match
3760 source#exit (coe self) true m_active m_first m_pan m_qsearch
3761 with
3762 | None -> m_prev_uioh
3763 | Some uioh -> uioh
3766 else (
3767 G.postRedisplay "list view kill qsearch";
3768 source#setqsearch "";
3769 coe {< m_qsearch = "" >}
3772 | 0xff0d | 0xff8d -> (* (kp) enter *)
3773 state.text <- "";
3774 let self = {< m_qsearch = "" >} in
3775 source#setqsearch "";
3776 let opt =
3777 G.postRedisplay "listview enter";
3778 if m_active >= 0 && m_active < source#getitemcount
3779 then (
3780 source#exit (coe self) false m_active m_first m_pan "";
3782 else (
3783 source#exit (coe self) true m_active m_first m_pan "";
3786 begin match opt with
3787 | None -> m_prev_uioh
3788 | Some uioh -> uioh
3791 | 0xff9f | 0xffff -> (* (kp) delete *)
3792 coe self
3794 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3795 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3796 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3797 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3799 | 0xff53 | 0xff98 -> (* (kp) right *)
3800 state.text <- "";
3801 G.postRedisplay "listview right";
3802 coe {< m_pan = m_pan - 1 >}
3804 | 0xff51 | 0xff96 -> (* (kp) left *)
3805 state.text <- "";
3806 G.postRedisplay "listview left";
3807 coe {< m_pan = m_pan + 1 >}
3809 | 0xff50 | 0xff95 -> (* (kp) home *)
3810 let active = find 0 1 in
3811 G.postRedisplay "listview home";
3812 set active 0;
3814 | 0xff57 | 0xff9c -> (* (kp) end *)
3815 let first = max 0 (itemcount - fstate.maxrows) in
3816 let active = find (itemcount - 1) ~-1 in
3817 G.postRedisplay "listview end";
3818 set active first;
3820 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3821 coe self
3823 | _ ->
3824 dolog "listview unknown key %#x" key; coe self
3826 method key key mask =
3827 match state.mode with
3828 | Textentry te -> textentrykeyboard key mask te; coe self
3829 | _ -> self#key1 key mask
3831 method button button down x y _ =
3832 let opt =
3833 match button with
3834 | 1 when x > state.winw - conf.scrollbw ->
3835 G.postRedisplay "listview scroll";
3836 if down
3837 then
3838 let _, position, sh = self#scrollph in
3839 if y > truncate position && y < truncate (position +. sh)
3840 then (
3841 state.mstate <- Mscrolly;
3842 Some (coe self)
3844 else
3845 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3846 let first = truncate (s *. float source#getitemcount) in
3847 let first = min source#getitemcount first in
3848 Some (coe {< m_first = first; m_active = first >})
3849 else (
3850 state.mstate <- Mnone;
3851 Some (coe self);
3853 | 1 when not down ->
3854 begin match self#elemunder y with
3855 | Some n ->
3856 G.postRedisplay "listview click";
3857 source#exit
3858 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3859 | _ ->
3860 Some (coe self)
3862 | n when (n == 4 || n == 5) && not down ->
3863 let len = source#getitemcount in
3864 let first =
3865 if n = 5 && m_first + fstate.maxrows >= len
3866 then
3867 m_first
3868 else
3869 let first = m_first + (if n == 4 then -1 else 1) in
3870 bound first 0 (len - 1)
3872 G.postRedisplay "listview wheel";
3873 Some (coe {< m_first = first >})
3874 | n when (n = 6 || n = 7) && not down ->
3875 let inc = if n = 7 then -1 else 1 in
3876 G.postRedisplay "listview hwheel";
3877 Some (coe {< m_pan = m_pan + inc >})
3878 | _ ->
3879 Some (coe self)
3881 match opt with
3882 | None -> m_prev_uioh
3883 | Some uioh -> uioh
3885 method multiclick _ x y = self#button 1 true x y
3887 method motion _ y =
3888 match state.mstate with
3889 | Mscrolly ->
3890 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3891 let first = truncate (s *. float source#getitemcount) in
3892 let first = min source#getitemcount first in
3893 G.postRedisplay "listview motion";
3894 coe {< m_first = first; m_active = first >}
3895 | _ -> coe self
3897 method pmotion x y =
3898 if x < state.winw - conf.scrollbw
3899 then
3900 let n =
3901 match self#elemunder y with
3902 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3903 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3905 let o =
3906 if n != m_active
3907 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3908 else self
3910 coe o
3911 else (
3912 Wsi.setcursor Wsi.CURSOR_INHERIT;
3913 coe self
3916 method infochanged _ = ()
3918 method scrollpw = (0, 0.0, 0.0)
3919 method scrollph =
3920 let nfs = fstate.fontsize + 1 in
3921 let y = m_first * nfs in
3922 let itemcount = source#getitemcount in
3923 let maxi = max 0 (itemcount - fstate.maxrows) in
3924 let maxy = maxi * nfs in
3925 let p, h = scrollph y maxy in
3926 conf.scrollbw, p, h
3928 method modehash = modehash
3929 method eformsgs = false
3930 end;;
3932 class outlinelistview ~source =
3933 let settext autonarrow s =
3934 if autonarrow
3935 then state.text <- "[" ^ s ^ "]"
3936 else state.text <- s
3938 object (self)
3939 inherit listview
3940 ~source:(source :> lvsource)
3941 ~trusted:false
3942 ~modehash:(findkeyhash conf "outline")
3943 as super
3945 val m_autonarrow = false
3947 method key key mask =
3948 let maxrows =
3949 if emptystr state.text
3950 then fstate.maxrows
3951 else fstate.maxrows - 2
3953 let calcfirst first active =
3954 if active > first
3955 then
3956 let rows = active - first in
3957 if rows > maxrows then active - maxrows else first
3958 else active
3960 let navigate incr =
3961 let active = m_active + incr in
3962 let active = bound active 0 (source#getitemcount - 1) in
3963 let first = calcfirst m_first active in
3964 G.postRedisplay "outline navigate";
3965 coe {< m_active = active; m_first = first >}
3967 let navscroll first =
3968 let active =
3969 let dist = m_active - first in
3970 if dist < 0
3971 then first
3972 else (
3973 if dist < maxrows
3974 then m_active
3975 else first + maxrows
3978 G.postRedisplay "outline navscroll";
3979 coe {< m_first = first; m_active = active >}
3981 let ctrl = Wsi.withctrl mask in
3982 match key with
3983 | 97 when ctrl -> (* ctrl-a *)
3984 if m_autonarrow
3985 then source#denarrow
3986 else source#narrow m_qsearch;
3987 settext (not m_autonarrow) m_qsearch;
3988 G.postRedisplay "toggle auto narrowing";
3989 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3991 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3992 settext true "";
3993 G.postRedisplay "toggle auto narrowing";
3994 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3996 | 110 when ctrl -> (* ctrl-n *)
3997 source#narrow m_qsearch;
3998 if not m_autonarrow
3999 then source#add_narrow_pattern m_qsearch;
4000 G.postRedisplay "outline ctrl-n";
4001 coe {< m_first = 0; m_active = 0 >}
4003 | 115 when ctrl -> (* ctrl-s *)
4004 let active = source#calcactive (getanchor ()) in
4005 let first = firstof m_first active in
4006 G.postRedisplay "outline ctrl-s";
4007 coe {< m_first = first; m_active = active >}
4009 | 117 when ctrl -> (* ctrl-u *)
4010 source#del_narrow_pattern;
4011 let pattern = source#renarrow in
4012 G.postRedisplay "outline ctrl-u";
4013 let text =
4014 if emptystr pattern then "" else "Narrowed to " ^ pattern
4016 settext m_autonarrow text;
4017 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4019 | 108 when ctrl -> (* ctrl-l *)
4020 let first = max 0 (m_active - (fstate.maxrows / 2)) in
4021 G.postRedisplay "outline ctrl-l";
4022 coe {< m_first = first >}
4024 | 0xff1b -> (* escape *)
4025 let o = super#key key mask in
4026 if m_autonarrow
4027 then (
4028 if nonemptystr m_qsearch
4029 then (
4030 source#add_narrow_pattern m_qsearch;
4031 settext true "";
4036 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
4037 if nonemptystr m_qsearch
4038 then source#add_narrow_pattern m_qsearch;
4039 super#key key mask
4041 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
4042 let pattern = m_qsearch ^ toutf8 key in
4043 G.postRedisplay "outlinelistview autonarrow add";
4044 source#narrow pattern;
4045 settext true pattern;
4046 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4048 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4049 if emptystr m_qsearch
4050 then coe self
4051 else
4052 let pattern = withoutlastutf8 m_qsearch in
4053 G.postRedisplay "outlinelistview autonarrow backspace";
4054 ignore (source#renarrow);
4055 source#narrow pattern;
4056 settext true pattern;
4057 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4059 | 0xff9f | 0xffff -> (* (kp) delete *)
4060 source#remove m_active;
4061 G.postRedisplay "outline delete";
4062 let active = max 0 (m_active-1) in
4063 coe {< m_first = firstof m_first active;
4064 m_active = active >}
4066 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4067 navscroll (max 0 (m_first - 1))
4069 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4070 navscroll (min (source#getitemcount - 1) (m_first + 1))
4072 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4073 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4074 | 0xff55 | 0xff9a -> (* (kp) prior *)
4075 navigate ~-(fstate.maxrows)
4076 | 0xff56 | 0xff9b -> (* (kp) next *)
4077 navigate fstate.maxrows
4079 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4080 let o =
4081 if ctrl
4082 then (
4083 G.postRedisplay "outline ctrl right";
4084 {< m_pan = m_pan + 1 >}
4086 else self#updownlevel 1
4088 coe o
4090 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4091 let o =
4092 if ctrl
4093 then (
4094 G.postRedisplay "outline ctrl left";
4095 {< m_pan = m_pan - 1 >}
4097 else self#updownlevel ~-1
4099 coe o
4101 | 0xff50 | 0xff95 -> (* (kp) home *)
4102 G.postRedisplay "outline home";
4103 coe {< m_first = 0; m_active = 0 >}
4105 | 0xff57 | 0xff9c -> (* (kp) end *)
4106 let active = source#getitemcount - 1 in
4107 let first = max 0 (active - fstate.maxrows) in
4108 G.postRedisplay "outline end";
4109 coe {< m_active = active; m_first = first >}
4111 | _ -> super#key key mask
4114 let gotounder under =
4115 let getpath filename =
4116 let path =
4117 if nonemptystr filename
4118 then
4119 if Filename.is_relative filename
4120 then
4121 let dir = Filename.dirname state.path in
4122 let dir =
4123 if Filename.is_implicit dir
4124 then Filename.concat (Sys.getcwd ()) dir
4125 else dir
4127 Filename.concat dir filename
4128 else filename
4129 else ""
4131 if Sys.file_exists path
4132 then path
4133 else ""
4135 match under with
4136 | Ulinkgoto (pageno, top) ->
4137 if pageno >= 0
4138 then (
4139 addnav ();
4140 gotopage1 pageno top;
4143 | Ulinkuri s ->
4144 gotouri s
4146 | Uremote (filename, pageno) ->
4147 let path = getpath filename in
4148 if nonemptystr path
4149 then (
4150 if conf.riani
4151 then
4152 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4153 try popen command []
4154 with exn ->
4155 Printf.eprintf
4156 "failed to execute `%s': %s\n" command (exntos exn);
4157 flush stderr;
4158 else
4159 let anchor = getanchor () in
4160 let ranchor = state.path, state.password, anchor, state.origin in
4161 state.origin <- "";
4162 state.anchor <- (pageno, 0.0, 0.0);
4163 state.ranchors <- ranchor :: state.ranchors;
4164 opendoc path "";
4166 else showtext '!' ("Could not find " ^ filename)
4168 | Uremotedest (filename, destname) ->
4169 let path = getpath filename in
4170 if nonemptystr path
4171 then (
4172 if conf.riani
4173 then
4174 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4175 try popen command []
4176 with exn ->
4177 Printf.eprintf
4178 "failed to execute `%s': %s\n" command (exntos exn);
4179 flush stderr;
4180 else
4181 let anchor = getanchor () in
4182 let ranchor = state.path, state.password, anchor, state.origin in
4183 state.origin <- "";
4184 state.nameddest <- destname;
4185 state.ranchors <- ranchor :: state.ranchors;
4186 opendoc path "";
4188 else showtext '!' ("Could not find " ^ filename)
4190 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4193 let gotooutline (_, _, kind) =
4194 match kind with
4195 | Onone -> ()
4196 | Oanchor anchor ->
4197 let (pageno, y, _) = anchor in
4198 let y = getanchory
4199 (if conf.presentation then (pageno, y, 1.0) else anchor)
4201 gotoghyll y
4202 | Ouri uri -> gotounder (Ulinkuri uri)
4203 | Olaunch cmd -> gotounder (Ulaunch cmd)
4204 | Oremote remote -> gotounder (Uremote remote)
4205 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4208 let outlinesource usebookmarks =
4209 let empty = [||] in
4210 (object (self)
4211 inherit lvsourcebase
4212 val mutable m_items = empty
4213 val mutable m_orig_items = empty
4214 val mutable m_narrow_patterns = []
4215 val mutable m_hadremovals = false
4217 method getitemcount =
4218 Array.length m_items + (if m_hadremovals then 1 else 0)
4220 method getitem n =
4221 if n == Array.length m_items && m_hadremovals
4222 then
4223 ("[Confirm removal]", 0)
4224 else
4225 let s, n, _ = m_items.(n) in
4226 (s, n)
4228 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4229 ignore (uioh, first, qsearch);
4230 let confrimremoval = m_hadremovals && active = Array.length m_items in
4231 let items =
4232 if m_narrow_patterns = []
4233 then m_orig_items
4234 else m_items
4236 if not cancel
4237 then (
4238 if not confrimremoval
4239 then (
4240 gotooutline m_items.(active);
4241 m_items <- items;
4243 else (
4244 state.bookmarks <- Array.to_list m_items;
4245 m_orig_items <- m_items;
4248 else m_items <- items;
4249 m_pan <- pan;
4250 None
4252 method hasaction _ = true
4254 method greetmsg =
4255 if Array.length m_items != Array.length m_orig_items
4256 then
4257 let s =
4258 match m_narrow_patterns with
4259 | one :: [] -> one
4260 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4262 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4263 else ""
4265 method narrow pattern =
4266 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4267 match reopt with
4268 | None -> ()
4269 | Some re ->
4270 let rec loop accu n =
4271 if n = -1
4272 then m_items <- Array.of_list accu
4273 else
4274 let (s, _, _) as o = m_items.(n) in
4275 let accu =
4276 if (try ignore (Str.search_forward re s 0); true
4277 with Not_found -> false)
4278 then o :: accu
4279 else accu
4281 loop accu (n-1)
4283 loop [] (Array.length m_items - 1)
4285 method denarrow =
4286 m_orig_items <- (
4287 if usebookmarks
4288 then Array.of_list state.bookmarks
4289 else state.outlines
4291 m_items <- m_orig_items
4293 method remove m =
4294 if usebookmarks
4295 then
4296 if m >= 0 && m < Array.length m_items
4297 then (
4298 m_hadremovals <- true;
4299 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4300 let n = if n >= m then n+1 else n in
4301 m_items.(n)
4305 method add_narrow_pattern pattern =
4306 m_narrow_patterns <- pattern :: m_narrow_patterns
4308 method del_narrow_pattern =
4309 match m_narrow_patterns with
4310 | _ :: rest -> m_narrow_patterns <- rest
4311 | [] -> ()
4313 method renarrow =
4314 self#denarrow;
4315 match m_narrow_patterns with
4316 | pattern :: [] -> self#narrow pattern; pattern
4317 | list ->
4318 List.fold_left (fun accu pattern ->
4319 self#narrow pattern;
4320 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4322 method calcactive anchor =
4323 let rely = getanchory anchor in
4324 let rec loop n best bestd =
4325 if n = Array.length m_items
4326 then best
4327 else
4328 let _, _, kind = m_items.(n) in
4329 match kind with
4330 | Oanchor anchor ->
4331 let orely = getanchory anchor in
4332 let d = abs (orely - rely) in
4333 if d < bestd
4334 then loop (n+1) n d
4335 else loop (n+1) best bestd
4336 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
4337 loop (n+1) best bestd
4339 loop 0 ~-1 max_int
4341 method reset anchor items =
4342 m_hadremovals <- false;
4343 if m_orig_items == empty
4344 then (
4345 m_orig_items <- items;
4346 if m_narrow_patterns == []
4347 then m_items <- items;
4349 let active = self#calcactive anchor in
4350 m_active <- active;
4351 m_first <- firstof m_first active
4352 end)
4355 let enterselector usebookmarks =
4356 let source = outlinesource usebookmarks in
4357 fun errmsg ->
4358 let outlines =
4359 if usebookmarks
4360 then Array.of_list state.bookmarks
4361 else state.outlines
4363 if Array.length outlines = 0
4364 then (
4365 showtext ' ' errmsg;
4367 else (
4368 state.text <- source#greetmsg;
4369 Wsi.setcursor Wsi.CURSOR_INHERIT;
4370 let anchor = getanchor () in
4371 source#reset anchor outlines;
4372 state.uioh <- coe (new outlinelistview ~source);
4373 G.postRedisplay "enter selector";
4377 let enteroutlinemode =
4378 let f = enterselector false in
4379 fun ()-> f "Document has no outline";
4382 let enterbookmarkmode =
4383 let f = enterselector true in
4384 fun () -> f "Document has no bookmarks (yet)";
4387 let color_of_string s =
4388 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4389 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4393 let color_to_string (r, g, b) =
4394 let r = truncate (r *. 256.0)
4395 and g = truncate (g *. 256.0)
4396 and b = truncate (b *. 256.0) in
4397 Printf.sprintf "%d/%d/%d" r g b
4400 let irect_of_string s =
4401 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4404 let irect_to_string (x0,y0,x1,y1) =
4405 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4408 let makecheckers () =
4409 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4410 following to say:
4411 converted by Issac Trotts. July 25, 2002 *)
4412 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4413 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4414 let id = GlTex.gen_texture () in
4415 GlTex.bind_texture `texture_2d id;
4416 GlPix.store (`unpack_alignment 1);
4417 GlTex.image2d image;
4418 List.iter (GlTex.parameter ~target:`texture_2d)
4419 [ `mag_filter `nearest; `min_filter `nearest ];
4423 let setcheckers enabled =
4424 match state.texid with
4425 | None ->
4426 if enabled then state.texid <- Some (makecheckers ())
4428 | Some texid ->
4429 if not enabled
4430 then (
4431 GlTex.delete_texture texid;
4432 state.texid <- None;
4436 let int_of_string_with_suffix s =
4437 let l = String.length s in
4438 let s1, shift =
4439 if l > 1
4440 then
4441 let suffix = Char.lowercase s.[l-1] in
4442 match suffix with
4443 | 'k' -> String.sub s 0 (l-1), 10
4444 | 'm' -> String.sub s 0 (l-1), 20
4445 | 'g' -> String.sub s 0 (l-1), 30
4446 | _ -> s, 0
4447 else s, 0
4449 let n = int_of_string s1 in
4450 let m = n lsl shift in
4451 if m < 0 || m < n
4452 then raise (Failure "value too large")
4453 else m
4456 let string_with_suffix_of_int n =
4457 if n = 0
4458 then "0"
4459 else
4460 let units = [(30, "G"); (20, "M"); (10, "K")] in
4461 let prettyint n =
4462 let rec loop s n =
4463 let h = n mod 1000 in
4464 let n = n / 1000 in
4465 if n = 0
4466 then string_of_int h ^ s
4467 else (
4468 let s = Printf.sprintf "_%03d%s" h s in
4469 loop s n
4472 loop "" n
4474 let rec find = function
4475 | [] -> prettyint n
4476 | (shift, suffix) :: rest ->
4477 if (n land ((1 lsl shift) - 1)) = 0
4478 then prettyint (n lsr shift) ^ suffix
4479 else find rest
4481 find units
4484 let defghyllscroll = (40, 8, 32);;
4485 let fastghyllscroll = (5,1,2);;
4486 let neatghyllscroll = (10,1,9);;
4487 let ghyllscroll_of_string s =
4488 match s with
4489 | "default" -> Some defghyllscroll
4490 | "fast" -> Some (5,1,2)
4491 | "neat" -> Some (10,1,9)
4492 | "" | "none" -> None
4493 | _ ->
4494 let (n,a,b) as nab =
4495 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4496 if n <= a || n <= b || a >= b
4497 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4498 n a b;
4499 Some nab
4502 let ghyllscroll_to_string ((n, a, b) as nab) =
4503 (**) if nab = defghyllscroll then "default"
4504 else if nab = fastghyllscroll then "fast"
4505 else if nab = neatghyllscroll then "neat"
4506 else Printf.sprintf "%d,%d,%d" n a b;
4509 let describe_location () =
4510 let fn = page_of_y state.y in
4511 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4512 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4513 let percent =
4514 if maxy <= 0
4515 then 100.
4516 else (100. *. (float state.y /. float maxy))
4518 if fn = ln
4519 then
4520 Printf.sprintf "page %d of %d [%.2f%%]"
4521 (fn+1) state.pagecount percent
4522 else
4523 Printf.sprintf
4524 "pages %d-%d of %d [%.2f%%]"
4525 (fn+1) (ln+1) state.pagecount percent
4528 let setpresentationmode v =
4529 let n = page_of_y state.y in
4530 state.anchor <- (n, 0.0, 1.0);
4531 conf.presentation <- v;
4532 if conf.fitmodel = FitPage
4533 then reqlayout conf.angle conf.fitmodel;
4534 represent ();
4537 let enterinfomode =
4538 let btos b = if b then "\xe2\x88\x9a" else "" in
4539 let showextended = ref false in
4540 let leave mode = function
4541 | Confirm -> state.mode <- mode
4542 | Cancel -> state.mode <- mode in
4543 let src =
4544 (object
4545 val mutable m_first_time = true
4546 val mutable m_l = []
4547 val mutable m_a = [||]
4548 val mutable m_prev_uioh = nouioh
4549 val mutable m_prev_mode = View
4551 inherit lvsourcebase
4553 method reset prev_mode prev_uioh =
4554 m_a <- Array.of_list (List.rev m_l);
4555 m_l <- [];
4556 m_prev_mode <- prev_mode;
4557 m_prev_uioh <- prev_uioh;
4558 if m_first_time
4559 then (
4560 let rec loop n =
4561 if n >= Array.length m_a
4562 then ()
4563 else
4564 match m_a.(n) with
4565 | _, _, _, Action _ -> m_active <- n
4566 | _ -> loop (n+1)
4568 loop 0;
4569 m_first_time <- false;
4572 method int name get set =
4573 m_l <-
4574 (name, `int get, 1, Action (
4575 fun u ->
4576 let ondone s =
4577 try set (int_of_string s)
4578 with exn ->
4579 state.text <- Printf.sprintf "bad integer `%s': %s"
4580 s (exntos exn)
4582 state.text <- "";
4583 let te = name ^ ": ", "", None, intentry, ondone, true in
4584 state.mode <- Textentry (te, leave m_prev_mode);
4586 )) :: m_l
4588 method int_with_suffix name get set =
4589 m_l <-
4590 (name, `intws get, 1, Action (
4591 fun u ->
4592 let ondone s =
4593 try set (int_of_string_with_suffix s)
4594 with exn ->
4595 state.text <- Printf.sprintf "bad integer `%s': %s"
4596 s (exntos exn)
4598 state.text <- "";
4599 let te =
4600 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4602 state.mode <- Textentry (te, leave m_prev_mode);
4604 )) :: m_l
4606 method bool ?(offset=1) ?(btos=btos) name get set =
4607 m_l <-
4608 (name, `bool (btos, get), offset, Action (
4609 fun u ->
4610 let v = get () in
4611 set (not v);
4613 )) :: m_l
4615 method color name get set =
4616 m_l <-
4617 (name, `color get, 1, Action (
4618 fun u ->
4619 let invalid = (nan, nan, nan) in
4620 let ondone s =
4621 let c =
4622 try color_of_string s
4623 with exn ->
4624 state.text <- Printf.sprintf "bad color `%s': %s"
4625 s (exntos exn);
4626 invalid
4628 if c <> invalid
4629 then set c;
4631 let te = name ^ ": ", "", None, textentry, ondone, true in
4632 state.text <- color_to_string (get ());
4633 state.mode <- Textentry (te, leave m_prev_mode);
4635 )) :: m_l
4637 method string name get set =
4638 m_l <-
4639 (name, `string get, 1, Action (
4640 fun u ->
4641 let ondone s = set s in
4642 let te = name ^ ": ", "", None, textentry, ondone, true in
4643 state.mode <- Textentry (te, leave m_prev_mode);
4645 )) :: m_l
4647 method colorspace name get set =
4648 m_l <-
4649 (name, `string get, 1, Action (
4650 fun _ ->
4651 let source =
4652 (object
4653 inherit lvsourcebase
4655 initializer
4656 m_active <- CSTE.to_int conf.colorspace;
4657 m_first <- 0;
4659 method getitemcount =
4660 Array.length CSTE.names
4661 method getitem n =
4662 (CSTE.names.(n), 0)
4663 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4664 ignore (uioh, first, pan, qsearch);
4665 if not cancel then set active;
4666 None
4667 method hasaction _ = true
4668 end)
4670 state.text <- "";
4671 let modehash = findkeyhash conf "info" in
4672 coe (new listview ~source ~trusted:true ~modehash)
4673 )) :: m_l
4675 method paxmark name get set =
4676 m_l <-
4677 (name, `string get, 1, Action (
4678 fun _ ->
4679 let source =
4680 (object
4681 inherit lvsourcebase
4683 initializer
4684 m_active <- MTE.to_int conf.paxmark;
4685 m_first <- 0;
4687 method getitemcount = Array.length MTE.names
4688 method getitem n = (MTE.names.(n), 0)
4689 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4690 ignore (uioh, first, pan, qsearch);
4691 if not cancel then set active;
4692 None
4693 method hasaction _ = true
4694 end)
4696 state.text <- "";
4697 let modehash = findkeyhash conf "info" in
4698 coe (new listview ~source ~trusted:true ~modehash)
4699 )) :: m_l
4701 method fitmodel name get set =
4702 m_l <-
4703 (name, `string get, 1, Action (
4704 fun _ ->
4705 let source =
4706 (object
4707 inherit lvsourcebase
4709 initializer
4710 m_active <- FMTE.to_int conf.fitmodel;
4711 m_first <- 0;
4713 method getitemcount = Array.length FMTE.names
4714 method getitem n = (FMTE.names.(n), 0)
4715 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4716 ignore (uioh, first, pan, qsearch);
4717 if not cancel then set active;
4718 None
4719 method hasaction _ = true
4720 end)
4722 state.text <- "";
4723 let modehash = findkeyhash conf "info" in
4724 coe (new listview ~source ~trusted:true ~modehash)
4725 )) :: m_l
4727 method caption s offset =
4728 m_l <- (s, `empty, offset, Noaction) :: m_l
4730 method caption2 s f offset =
4731 m_l <- (s, `string f, offset, Noaction) :: m_l
4733 method getitemcount = Array.length m_a
4735 method getitem n =
4736 let tostr = function
4737 | `int f -> string_of_int (f ())
4738 | `intws f -> string_with_suffix_of_int (f ())
4739 | `string f -> f ()
4740 | `color f -> color_to_string (f ())
4741 | `bool (btos, f) -> btos (f ())
4742 | `empty -> ""
4744 let name, t, offset, _ = m_a.(n) in
4745 ((let s = tostr t in
4746 if nonemptystr s
4747 then Printf.sprintf "%s\t%s" name s
4748 else name),
4749 offset)
4751 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4752 let uiohopt =
4753 if not cancel
4754 then (
4755 m_qsearch <- qsearch;
4756 let uioh =
4757 match m_a.(active) with
4758 | _, _, _, Action f -> f uioh
4759 | _ -> uioh
4761 Some uioh
4763 else None
4765 m_active <- active;
4766 m_first <- first;
4767 m_pan <- pan;
4768 uiohopt
4770 method hasaction n =
4771 match m_a.(n) with
4772 | _, _, _, Action _ -> true
4773 | _ -> false
4774 end)
4776 let rec fillsrc prevmode prevuioh =
4777 let sep () = src#caption "" 0 in
4778 let colorp name get set =
4779 src#string name
4780 (fun () -> color_to_string (get ()))
4781 (fun v ->
4783 let c = color_of_string v in
4784 set c
4785 with exn ->
4786 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4789 let oldmode = state.mode in
4790 let birdseye = isbirdseye state.mode in
4792 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4794 src#bool "presentation mode"
4795 (fun () -> conf.presentation)
4796 (fun v -> setpresentationmode v);
4798 src#bool "ignore case in searches"
4799 (fun () -> conf.icase)
4800 (fun v -> conf.icase <- v);
4802 src#bool "preload"
4803 (fun () -> conf.preload)
4804 (fun v -> conf.preload <- v);
4806 src#bool "highlight links"
4807 (fun () -> conf.hlinks)
4808 (fun v -> conf.hlinks <- v);
4810 src#bool "under info"
4811 (fun () -> conf.underinfo)
4812 (fun v -> conf.underinfo <- v);
4814 src#bool "persistent bookmarks"
4815 (fun () -> conf.savebmarks)
4816 (fun v -> conf.savebmarks <- v);
4818 src#fitmodel "fit model"
4819 (fun () -> FMTE.to_string conf.fitmodel)
4820 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4822 src#bool "trim margins"
4823 (fun () -> conf.trimmargins)
4824 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4826 src#bool "persistent location"
4827 (fun () -> conf.jumpback)
4828 (fun v -> conf.jumpback <- v);
4830 sep ();
4831 src#int "inter-page space"
4832 (fun () -> conf.interpagespace)
4833 (fun n ->
4834 conf.interpagespace <- n;
4835 docolumns conf.columns;
4836 let pageno, py =
4837 match state.layout with
4838 | [] -> 0, 0
4839 | l :: _ ->
4840 l.pageno, l.pagey
4842 state.maxy <- calcheight ();
4843 let y = getpagey pageno in
4844 gotoy (y + py)
4847 src#int "page bias"
4848 (fun () -> conf.pagebias)
4849 (fun v -> conf.pagebias <- v);
4851 src#int "scroll step"
4852 (fun () -> conf.scrollstep)
4853 (fun n -> conf.scrollstep <- n);
4855 src#int "horizontal scroll step"
4856 (fun () -> conf.hscrollstep)
4857 (fun v -> conf.hscrollstep <- v);
4859 src#int "auto scroll step"
4860 (fun () ->
4861 match state.autoscroll with
4862 | Some step -> step
4863 | _ -> conf.autoscrollstep)
4864 (fun n ->
4865 let n = boundastep state.winh n in
4866 if state.autoscroll <> None
4867 then state.autoscroll <- Some n;
4868 conf.autoscrollstep <- n);
4870 src#int "zoom"
4871 (fun () -> truncate (conf.zoom *. 100.))
4872 (fun v -> setzoom ((float v) /. 100.));
4874 src#int "rotation"
4875 (fun () -> conf.angle)
4876 (fun v -> reqlayout v conf.fitmodel);
4878 src#int "scroll bar width"
4879 (fun () -> conf.scrollbw)
4880 (fun v ->
4881 conf.scrollbw <- v;
4882 reshape state.winw state.winh;
4885 src#int "scroll handle height"
4886 (fun () -> conf.scrollh)
4887 (fun v -> conf.scrollh <- v;);
4889 src#int "thumbnail width"
4890 (fun () -> conf.thumbw)
4891 (fun v ->
4892 conf.thumbw <- min 4096 v;
4893 match oldmode with
4894 | Birdseye beye ->
4895 leavebirdseye beye false;
4896 enterbirdseye ()
4897 | _ -> ()
4900 let mode = state.mode in
4901 src#string "columns"
4902 (fun () ->
4903 match conf.columns with
4904 | Csingle _ -> "1"
4905 | Cmulti (multi, _) -> multicolumns_to_string multi
4906 | Csplit (count, _) -> "-" ^ string_of_int count
4908 (fun v ->
4909 let n, a, b = multicolumns_of_string v in
4910 setcolumns mode n a b);
4912 sep ();
4913 src#caption "Pixmap cache" 0;
4914 src#int_with_suffix "size (advisory)"
4915 (fun () -> conf.memlimit)
4916 (fun v -> conf.memlimit <- v);
4918 src#caption2 "used"
4919 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4920 (string_with_suffix_of_int state.memused)
4921 (Hashtbl.length state.tilemap)) 1;
4923 sep ();
4924 src#caption "Layout" 0;
4925 src#caption2 "Dimension"
4926 (fun () ->
4927 Printf.sprintf "%dx%d (virtual %dx%d)"
4928 state.winw state.winh
4929 state.w state.maxy)
4931 if conf.debug
4932 then
4933 src#caption2 "Position" (fun () ->
4934 Printf.sprintf "%dx%d" state.x state.y
4936 else
4937 src#caption2 "Position" (fun () -> describe_location ()) 1
4940 sep ();
4941 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4942 "Save these parameters as global defaults at exit"
4943 (fun () -> conf.bedefault)
4944 (fun v -> conf.bedefault <- v)
4947 sep ();
4948 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4949 src#bool ~offset:0 ~btos "Extended parameters"
4950 (fun () -> !showextended)
4951 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4952 if !showextended
4953 then (
4954 src#bool "checkers"
4955 (fun () -> conf.checkers)
4956 (fun v -> conf.checkers <- v; setcheckers v);
4957 src#bool "update cursor"
4958 (fun () -> conf.updatecurs)
4959 (fun v -> conf.updatecurs <- v);
4960 src#bool "verbose"
4961 (fun () -> conf.verbose)
4962 (fun v -> conf.verbose <- v);
4963 src#bool "invert colors"
4964 (fun () -> conf.invert)
4965 (fun v -> conf.invert <- v);
4966 src#bool "max fit"
4967 (fun () -> conf.maxhfit)
4968 (fun v -> conf.maxhfit <- v);
4969 src#bool "redirect stderr"
4970 (fun () -> conf.redirectstderr)
4971 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4972 src#bool "pax mode"
4973 (fun () -> conf.pax != None)
4974 (fun v ->
4975 if v
4976 then conf.pax <- Some (ref (now (), 0, 0))
4977 else conf.pax <- None);
4978 src#string "uri launcher"
4979 (fun () -> conf.urilauncher)
4980 (fun v -> conf.urilauncher <- v);
4981 src#string "path launcher"
4982 (fun () -> conf.pathlauncher)
4983 (fun v -> conf.pathlauncher <- v);
4984 src#string "tile size"
4985 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4986 (fun v ->
4988 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4989 conf.tilew <- max 64 w;
4990 conf.tileh <- max 64 h;
4991 flushtiles ();
4992 with exn ->
4993 state.text <- Printf.sprintf "bad tile size `%s': %s"
4994 v (exntos exn)
4996 src#int "texture count"
4997 (fun () -> conf.texcount)
4998 (fun v ->
4999 if realloctexts v
5000 then conf.texcount <- v
5001 else showtext '!' " Failed to set texture count please retry later"
5003 src#int "slice height"
5004 (fun () -> conf.sliceheight)
5005 (fun v ->
5006 conf.sliceheight <- v;
5007 wcmd "sliceh %d" conf.sliceheight;
5009 src#int "anti-aliasing level"
5010 (fun () -> conf.aalevel)
5011 (fun v ->
5012 conf.aalevel <- bound v 0 8;
5013 state.anchor <- getanchor ();
5014 opendoc state.path state.password;
5016 src#string "page scroll scaling factor"
5017 (fun () -> string_of_float conf.pgscale)
5018 (fun v ->
5020 let s = float_of_string v in
5021 conf.pgscale <- s
5022 with exn ->
5023 state.text <- Printf.sprintf
5024 "bad page scroll scaling factor `%s': %s" v (exntos exn)
5027 src#int "ui font size"
5028 (fun () -> fstate.fontsize)
5029 (fun v -> setfontsize (bound v 5 100));
5030 src#int "hint font size"
5031 (fun () -> conf.hfsize)
5032 (fun v -> conf.hfsize <- bound v 5 100);
5033 colorp "background color"
5034 (fun () -> conf.bgcolor)
5035 (fun v -> conf.bgcolor <- v);
5036 src#bool "crop hack"
5037 (fun () -> conf.crophack)
5038 (fun v -> conf.crophack <- v);
5039 src#string "trim fuzz"
5040 (fun () -> irect_to_string conf.trimfuzz)
5041 (fun v ->
5043 conf.trimfuzz <- irect_of_string v;
5044 if conf.trimmargins
5045 then settrim true conf.trimfuzz;
5046 with exn ->
5047 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
5049 src#string "throttle"
5050 (fun () ->
5051 match conf.maxwait with
5052 | None -> "show place holder if page is not ready"
5053 | Some time ->
5054 if time = infinity
5055 then "wait for page to fully render"
5056 else
5057 "wait " ^ string_of_float time
5058 ^ " seconds before showing placeholder"
5060 (fun v ->
5062 let f = float_of_string v in
5063 if f <= 0.0
5064 then conf.maxwait <- None
5065 else conf.maxwait <- Some f
5066 with exn ->
5067 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
5069 src#string "ghyll scroll"
5070 (fun () ->
5071 match conf.ghyllscroll with
5072 | None -> ""
5073 | Some nab -> ghyllscroll_to_string nab
5075 (fun v ->
5076 try conf.ghyllscroll <- ghyllscroll_of_string v
5077 with exn ->
5078 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
5080 src#string "selection command"
5081 (fun () -> conf.selcmd)
5082 (fun v -> conf.selcmd <- v);
5083 src#string "synctex command"
5084 (fun () -> conf.stcmd)
5085 (fun v -> conf.stcmd <- v);
5086 src#string "pax command"
5087 (fun () -> conf.paxcmd)
5088 (fun v -> conf.paxcmd <- v);
5089 src#colorspace "color space"
5090 (fun () -> CSTE.to_string conf.colorspace)
5091 (fun v ->
5092 conf.colorspace <- CSTE.of_int v;
5093 wcmd "cs %d" v;
5094 load state.layout;
5096 src#paxmark "pax mark method"
5097 (fun () -> MTE.to_string conf.paxmark)
5098 (fun v -> conf.paxmark <- MTE.of_int v);
5099 if pbousable ()
5100 then
5101 src#bool "use PBO"
5102 (fun () -> conf.usepbo)
5103 (fun v -> conf.usepbo <- v);
5104 src#bool "mouse wheel scrolls pages"
5105 (fun () -> conf.wheelbypage)
5106 (fun v -> conf.wheelbypage <- v);
5107 src#bool "open remote links in a new instance"
5108 (fun () -> conf.riani)
5109 (fun v -> conf.riani <- v);
5112 sep ();
5113 src#caption "Document" 0;
5114 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
5115 src#caption2 "Pages"
5116 (fun () -> string_of_int state.pagecount) 1;
5117 src#caption2 "Dimensions"
5118 (fun () -> string_of_int (List.length state.pdims)) 1;
5119 if conf.trimmargins
5120 then (
5121 sep ();
5122 src#caption "Trimmed margins" 0;
5123 src#caption2 "Dimensions"
5124 (fun () -> string_of_int (List.length state.pdims)) 1;
5127 sep ();
5128 src#caption "OpenGL" 0;
5129 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
5130 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
5132 sep ();
5133 src#caption "Location" 0;
5134 if nonemptystr state.origin
5135 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
5136 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5138 src#reset prevmode prevuioh;
5140 fun () ->
5141 state.text <- "";
5142 let prevmode = state.mode
5143 and prevuioh = state.uioh in
5144 fillsrc prevmode prevuioh;
5145 let source = (src :> lvsource) in
5146 let modehash = findkeyhash conf "info" in
5147 state.uioh <- coe (object (self)
5148 inherit listview ~source ~trusted:true ~modehash as super
5149 val mutable m_prevmemused = 0
5150 method infochanged = function
5151 | Memused ->
5152 if m_prevmemused != state.memused
5153 then (
5154 m_prevmemused <- state.memused;
5155 G.postRedisplay "memusedchanged";
5157 | Pdim -> G.postRedisplay "pdimchanged"
5158 | Docinfo -> fillsrc prevmode prevuioh
5160 method key key mask =
5161 if not (Wsi.withctrl mask)
5162 then
5163 match key with
5164 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5165 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5166 | _ -> super#key key mask
5167 else super#key key mask
5168 end);
5169 G.postRedisplay "info";
5172 let enterhelpmode =
5173 let source =
5174 (object
5175 inherit lvsourcebase
5176 method getitemcount = Array.length state.help
5177 method getitem n =
5178 let s, l, _ = state.help.(n) in
5179 (s, l)
5181 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5182 let optuioh =
5183 if not cancel
5184 then (
5185 m_qsearch <- qsearch;
5186 match state.help.(active) with
5187 | _, _, Action f -> Some (f uioh)
5188 | _ -> Some (uioh)
5190 else None
5192 m_active <- active;
5193 m_first <- first;
5194 m_pan <- pan;
5195 optuioh
5197 method hasaction n =
5198 match state.help.(n) with
5199 | _, _, Action _ -> true
5200 | _ -> false
5202 initializer
5203 m_active <- -1
5204 end)
5205 in fun () ->
5206 let modehash = findkeyhash conf "help" in
5207 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5208 G.postRedisplay "help";
5211 let entermsgsmode =
5212 let msgsource =
5213 let re = Str.regexp "[\r\n]" in
5214 (object
5215 inherit lvsourcebase
5216 val mutable m_items = [||]
5218 method getitemcount = 1 + Array.length m_items
5220 method getitem n =
5221 if n = 0
5222 then "[Clear]", 0
5223 else m_items.(n-1), 0
5225 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5226 ignore uioh;
5227 if not cancel
5228 then (
5229 if active = 0
5230 then Buffer.clear state.errmsgs;
5231 m_qsearch <- qsearch;
5233 m_active <- active;
5234 m_first <- first;
5235 m_pan <- pan;
5236 None
5238 method hasaction n =
5239 n = 0
5241 method reset =
5242 state.newerrmsgs <- false;
5243 let l = Str.split re (Buffer.contents state.errmsgs) in
5244 m_items <- Array.of_list l
5246 initializer
5247 m_active <- 0
5248 end)
5249 in fun () ->
5250 state.text <- "";
5251 msgsource#reset;
5252 let source = (msgsource :> lvsource) in
5253 let modehash = findkeyhash conf "listview" in
5254 state.uioh <- coe (object
5255 inherit listview ~source ~trusted:false ~modehash as super
5256 method display =
5257 if state.newerrmsgs
5258 then msgsource#reset;
5259 super#display
5260 end);
5261 G.postRedisplay "msgs";
5264 let quickbookmark ?title () =
5265 match state.layout with
5266 | [] -> ()
5267 | l :: _ ->
5268 let title =
5269 match title with
5270 | None ->
5271 let sec = Unix.gettimeofday () in
5272 let tm = Unix.localtime sec in
5273 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5274 (l.pageno+1)
5275 tm.Unix.tm_mday
5276 tm.Unix.tm_mon
5277 (tm.Unix.tm_year + 1900)
5278 tm.Unix.tm_hour
5279 tm.Unix.tm_min
5280 | Some title -> title
5282 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5285 let setautoscrollspeed step goingdown =
5286 let incr = max 1 ((abs step) / 2) in
5287 let incr = if goingdown then incr else -incr in
5288 let astep = boundastep state.winh (step + incr) in
5289 state.autoscroll <- Some astep;
5292 let canpan () =
5293 match conf.columns with
5294 | Csplit _ -> true
5295 | _ -> state.x != 0 || conf.zoom > 1.0
5298 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5300 let existsinrow pageno (columns, coverA, coverB) p =
5301 let last = ((pageno - coverA) mod columns) + columns in
5302 let rec any = function
5303 | [] -> false
5304 | l :: rest ->
5305 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5306 then p l
5307 else (
5308 if not (p l)
5309 then (if l.pageno = last then false else any rest)
5310 else true
5313 any state.layout
5316 let nextpage () =
5317 match state.layout with
5318 | [] ->
5319 let pageno = page_of_y state.y in
5320 gotoghyll (getpagey (pageno+1))
5321 | l :: rest ->
5322 match conf.columns with
5323 | Csingle _ ->
5324 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5325 then
5326 let y = clamp (pgscale state.winh) in
5327 gotoghyll y
5328 else
5329 let pageno = min (l.pageno+1) (state.pagecount-1) in
5330 gotoghyll (getpagey pageno)
5331 | Cmulti ((c, _, _) as cl, _) ->
5332 if conf.presentation
5333 && (existsinrow l.pageno cl
5334 (fun l -> l.pageh > l.pagey + l.pagevh))
5335 then
5336 let y = clamp (pgscale state.winh) in
5337 gotoghyll y
5338 else
5339 let pageno = min (l.pageno+c) (state.pagecount-1) in
5340 gotoghyll (getpagey pageno)
5341 | Csplit (n, _) ->
5342 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5343 then
5344 let pagey, pageh = getpageyh l.pageno in
5345 let pagey = pagey + pageh * l.pagecol in
5346 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5347 gotoghyll (pagey + pageh + ips)
5350 let prevpage () =
5351 match state.layout with
5352 | [] ->
5353 let pageno = page_of_y state.y in
5354 gotoghyll (getpagey (pageno-1))
5355 | l :: _ ->
5356 match conf.columns with
5357 | Csingle _ ->
5358 if conf.presentation && l.pagey != 0
5359 then
5360 gotoghyll (clamp (pgscale ~-(state.winh)))
5361 else
5362 let pageno = max 0 (l.pageno-1) in
5363 gotoghyll (getpagey pageno)
5364 | Cmulti ((c, _, coverB) as cl, _) ->
5365 if conf.presentation &&
5366 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5367 then
5368 gotoghyll (clamp (pgscale ~-(state.winh)))
5369 else
5370 let decr =
5371 if l.pageno = state.pagecount - coverB
5372 then 1
5373 else c
5375 let pageno = max 0 (l.pageno-decr) in
5376 gotoghyll (getpagey pageno)
5377 | Csplit (n, _) ->
5378 let y =
5379 if l.pagecol = 0
5380 then
5381 if l.pageno = 0
5382 then l.pagey
5383 else
5384 let pageno = max 0 (l.pageno-1) in
5385 let pagey, pageh = getpageyh pageno in
5386 pagey + (n-1)*pageh
5387 else
5388 let pagey, pageh = getpageyh l.pageno in
5389 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5391 gotoghyll y
5394 let viewkeyboard key mask =
5395 let enttext te =
5396 let mode = state.mode in
5397 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5398 state.text <- "";
5399 enttext ();
5400 G.postRedisplay "view:enttext"
5402 let ctrl = Wsi.withctrl mask in
5403 let key =
5404 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5406 match key with
5407 | 81 -> (* Q *)
5408 exit 0
5410 | 0xff63 -> (* insert *)
5411 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5412 then (
5413 state.mode <- LinkNav (Ltgendir 0);
5414 gotoy state.y;
5416 else showtext '!' "Keyboard link navigation does not work under rotation"
5418 | 0xff1b | 113 -> (* escape / q *)
5419 begin match state.mstate with
5420 | Mzoomrect _ ->
5421 state.mstate <- Mnone;
5422 Wsi.setcursor Wsi.CURSOR_INHERIT;
5423 G.postRedisplay "kill zoom rect";
5424 | _ ->
5425 begin match state.mode with
5426 | LinkNav _ ->
5427 state.mode <- View;
5428 G.postRedisplay "esc leave linknav"
5429 | _ ->
5430 match state.ranchors with
5431 | [] -> raise Quit
5432 | (path, password, anchor, origin) :: rest ->
5433 state.ranchors <- rest;
5434 state.anchor <- anchor;
5435 state.origin <- origin;
5436 state.nameddest <- "";
5437 opendoc path password
5438 end;
5439 end;
5441 | 0xff08 -> (* backspace *)
5442 gotoghyll (getnav ~-1)
5444 | 111 -> (* o *)
5445 enteroutlinemode ()
5447 | 117 -> (* u *)
5448 state.rects <- [];
5449 state.text <- "";
5450 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
5451 G.postRedisplay "dehighlight";
5453 | 47 | 63 -> (* / ? *)
5454 let ondone isforw s =
5455 cbput state.hists.pat s;
5456 state.searchpattern <- s;
5457 search s isforw
5459 let s = String.create 1 in
5460 s.[0] <- Char.chr key;
5461 enttext (s, "", Some (onhist state.hists.pat),
5462 textentry, ondone (key = 47), true)
5464 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5465 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5466 setzoom (conf.zoom +. incr)
5468 | 43 | 0xffab -> (* + *)
5469 let ondone s =
5470 let n =
5471 try int_of_string s with exc ->
5472 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5473 max_int
5475 if n != max_int
5476 then (
5477 conf.pagebias <- n;
5478 state.text <- "page bias is now " ^ string_of_int n;
5481 enttext ("page bias: ", "", None, intentry, ondone, true)
5483 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5484 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5485 setzoom (max 0.01 (conf.zoom -. decr))
5487 | 45 | 0xffad -> (* - *)
5488 let ondone msg = state.text <- msg in
5489 enttext (
5490 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5491 optentry state.mode, ondone, true
5494 | 48 when ctrl -> (* ctrl-0 *)
5495 if conf.zoom = 1.0
5496 then (
5497 state.x <- 0;
5498 gotoy state.y
5500 else setzoom 1.0
5502 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5503 let cols =
5504 match conf.columns with
5505 | Csingle _ | Cmulti _ -> 1
5506 | Csplit (n, _) -> n
5508 let h = state.winh -
5509 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5511 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5512 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5513 then setzoom zoom
5515 | 51 when ctrl -> (* ctrl-3 *)
5516 let fm =
5517 match conf.fitmodel with
5518 | FitWidth -> FitProportional
5519 | FitProportional -> FitPage
5520 | FitPage -> FitWidth
5522 state.text <- "fit model: " ^ FMTE.to_string fm;
5523 reqlayout conf.angle fm
5525 | 0xffc6 -> (* f9 *)
5526 togglebirdseye ()
5528 | 57 when ctrl -> (* ctrl-9 *)
5529 togglebirdseye ()
5531 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5532 when not ctrl -> (* 0..9 *)
5533 let ondone s =
5534 let n =
5535 try int_of_string s with exc ->
5536 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5539 if n >= 0
5540 then (
5541 addnav ();
5542 cbput state.hists.pag (string_of_int n);
5543 gotopage1 (n + conf.pagebias - 1) 0;
5546 let pageentry text key =
5547 match Char.unsafe_chr key with
5548 | 'g' -> TEdone text
5549 | _ -> intentry text key
5551 let text = "x" in text.[0] <- Char.chr key;
5552 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5554 | 98 -> (* b *)
5555 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5556 reshape state.winw state.winh;
5558 | 66 -> (* B *)
5559 state.bzoom <- not state.bzoom;
5560 state.rects <- [];
5561 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5563 | 108 -> (* l *)
5564 conf.hlinks <- not conf.hlinks;
5565 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5566 G.postRedisplay "toggle highlightlinks";
5568 | 70 -> (* F *)
5569 state.glinks <- true;
5570 let mode = state.mode in
5571 state.mode <- Textentry (
5572 (":", "", None, linknentry, linkndone gotounder, false),
5573 (fun _ ->
5574 state.glinks <- false;
5575 state.mode <- mode)
5577 state.text <- "";
5578 G.postRedisplay "view:linkent(F)"
5580 | 121 -> (* y *)
5581 state.glinks <- true;
5582 let mode = state.mode in
5583 state.mode <- Textentry (
5585 ":", "", None, linknentry, linkndone (fun under ->
5586 selstring (undertext under);
5587 ), false
5589 fun _ ->
5590 state.glinks <- false;
5591 state.mode <- mode
5593 state.text <- "";
5594 G.postRedisplay "view:linkent"
5596 | 97 -> (* a *)
5597 begin match state.autoscroll with
5598 | Some step ->
5599 conf.autoscrollstep <- step;
5600 state.autoscroll <- None
5601 | None ->
5602 if conf.autoscrollstep = 0
5603 then state.autoscroll <- Some 1
5604 else state.autoscroll <- Some conf.autoscrollstep
5607 | 112 when ctrl -> (* ctrl-p *)
5608 launchpath ()
5610 | 80 -> (* P *)
5611 setpresentationmode (not conf.presentation);
5612 showtext ' ' ("presentation mode " ^
5613 if conf.presentation then "on" else "off");
5615 | 102 -> (* f *)
5616 if List.mem Wsi.Fullscreen state.winstate
5617 then Wsi.reshape conf.cwinw conf.cwinh
5618 else Wsi.fullscreen ()
5620 | 112 | 78 -> (* p|N *)
5621 search state.searchpattern false
5623 | 110 | 0xffc0 -> (* n|F3 *)
5624 search state.searchpattern true
5626 | 116 -> (* t *)
5627 begin match state.layout with
5628 | [] -> ()
5629 | l :: _ ->
5630 gotoghyll (getpagey l.pageno)
5633 | 32 -> (* space *)
5634 nextpage ()
5636 | 0xff9f | 0xffff -> (* delete *)
5637 prevpage ()
5639 | 61 -> (* = *)
5640 showtext ' ' (describe_location ());
5642 | 119 -> (* w *)
5643 begin match state.layout with
5644 | [] -> ()
5645 | l :: _ ->
5646 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5647 G.postRedisplay "w"
5650 | 39 -> (* ' *)
5651 enterbookmarkmode ()
5653 | 104 | 0xffbe -> (* h|F1 *)
5654 enterhelpmode ()
5656 | 105 -> (* i *)
5657 enterinfomode ()
5659 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5660 entermsgsmode ()
5662 | 109 -> (* m *)
5663 let ondone s =
5664 match state.layout with
5665 | l :: _ ->
5666 if nonemptystr s
5667 then
5668 state.bookmarks <-
5669 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5670 | _ -> ()
5672 enttext ("bookmark: ", "", None, textentry, ondone, true)
5674 | 126 -> (* ~ *)
5675 quickbookmark ();
5676 showtext ' ' "Quick bookmark added";
5678 | 122 -> (* z *)
5679 begin match state.layout with
5680 | l :: _ ->
5681 let rect = getpdimrect l.pagedimno in
5682 let w, h =
5683 if conf.crophack
5684 then
5685 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5686 truncate (1.2 *. (rect.(3) -. rect.(0))))
5687 else
5688 (truncate (rect.(1) -. rect.(0)),
5689 truncate (rect.(3) -. rect.(0)))
5691 let w = truncate ((float w)*.conf.zoom)
5692 and h = truncate ((float h)*.conf.zoom) in
5693 if w != 0 && h != 0
5694 then (
5695 state.anchor <- getanchor ();
5696 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5698 G.postRedisplay "z";
5700 | [] -> ()
5703 | 120 -> (* x *)
5704 state.roam ();
5705 state.roam <- noroam;
5706 | 60 | 62 -> (* < > *)
5707 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5709 | 91 | 93 -> (* [ ] *)
5710 conf.colorscale <-
5711 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5713 G.postRedisplay "brightness";
5715 | 99 when state.mode = View -> (* [alt-]c *)
5716 if Wsi.withalt mask
5717 then (
5718 if conf.zoom > 1.0
5719 then
5720 let m = (wadjsb state.winw - state.w) / 2 in
5721 state.x <- m;
5722 gotoy_and_clear_text state.y
5724 else
5725 let (c, a, b), z =
5726 match state.prevcolumns with
5727 | None -> (1, 0, 0), 1.0
5728 | Some (columns, z) ->
5729 let cab =
5730 match columns with
5731 | Csplit (c, _) -> -c, 0, 0
5732 | Cmulti ((c, a, b), _) -> c, a, b
5733 | Csingle _ -> 1, 0, 0
5735 cab, z
5737 setcolumns View c a b;
5738 setzoom z
5740 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5741 -> (* ctrl-shift- (kp) [up|down] *)
5742 let zoom, x = state.prevzoom in
5743 setzoom zoom;
5744 state.x <- x;
5746 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5747 begin match state.autoscroll with
5748 | None ->
5749 begin match state.mode with
5750 | Birdseye beye -> upbirdseye 1 beye
5751 | _ ->
5752 if ctrl
5753 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5754 else (
5755 if not (Wsi.withshift mask) && conf.presentation
5756 then prevpage ()
5757 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5760 | Some n ->
5761 setautoscrollspeed n false
5764 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5765 begin match state.autoscroll with
5766 | None ->
5767 begin match state.mode with
5768 | Birdseye beye -> downbirdseye 1 beye
5769 | _ ->
5770 if ctrl
5771 then gotoy_and_clear_text (clamp (state.winh/2))
5772 else (
5773 if not (Wsi.withshift mask) && conf.presentation
5774 then nextpage ()
5775 else gotoy_and_clear_text (clamp conf.scrollstep)
5778 | Some n ->
5779 setautoscrollspeed n true
5782 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5783 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5784 if canpan ()
5785 then
5786 let dx =
5787 if ctrl
5788 then state.winw / 2
5789 else conf.hscrollstep
5791 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5792 state.x <- panbound (state.x + dx);
5793 gotoy_and_clear_text state.y
5794 else (
5795 state.text <- "";
5796 G.postRedisplay "left/right"
5799 | 0xff55 | 0xff9a -> (* (kp) prior *)
5800 let y =
5801 if ctrl
5802 then
5803 match state.layout with
5804 | [] -> state.y
5805 | l :: _ -> state.y - l.pagey
5806 else
5807 clamp (pgscale (-state.winh))
5809 gotoghyll y
5811 | 0xff56 | 0xff9b -> (* (kp) next *)
5812 let y =
5813 if ctrl
5814 then
5815 match List.rev state.layout with
5816 | [] -> state.y
5817 | l :: _ -> getpagey l.pageno
5818 else
5819 clamp (pgscale state.winh)
5821 gotoghyll y
5823 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5824 gotoghyll 0
5825 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5826 gotoghyll (clamp state.maxy)
5828 | 0xff53 | 0xff98
5829 when Wsi.withalt mask -> (* alt-(kp) right *)
5830 gotoghyll (getnav 1)
5831 | 0xff51 | 0xff96
5832 when Wsi.withalt mask -> (* alt-(kp) left *)
5833 gotoghyll (getnav ~-1)
5835 | 114 -> (* r *)
5836 reload ()
5838 | 118 when conf.debug -> (* v *)
5839 state.rects <- [];
5840 List.iter (fun l ->
5841 match getopaque l.pageno with
5842 | None -> ()
5843 | Some opaque ->
5844 let x0, y0, x1, y1 = pagebbox opaque in
5845 let a,b = float x0, float y0 in
5846 let c,d = float x1, float y0 in
5847 let e,f = float x1, float y1 in
5848 let h,j = float x0, float y1 in
5849 let rect = (a,b,c,d,e,f,h,j) in
5850 debugrect rect;
5851 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5852 ) state.layout;
5853 G.postRedisplay "v";
5855 | _ ->
5856 vlog "huh? %s" (Wsi.keyname key)
5859 let linknavkeyboard key mask linknav =
5860 let getpage pageno =
5861 let rec loop = function
5862 | [] -> None
5863 | l :: _ when l.pageno = pageno -> Some l
5864 | _ :: rest -> loop rest
5865 in loop state.layout
5867 let doexact (pageno, n) =
5868 match getopaque pageno, getpage pageno with
5869 | Some opaque, Some l ->
5870 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5871 then
5872 let under = getlink opaque n in
5873 G.postRedisplay "link gotounder";
5874 gotounder under;
5875 state.mode <- View;
5876 else
5877 let opt, dir =
5878 match key with
5879 | 0xff50 -> (* home *)
5880 Some (findlink opaque LDfirst), -1
5882 | 0xff57 -> (* end *)
5883 Some (findlink opaque LDlast), 1
5885 | 0xff51 -> (* left *)
5886 Some (findlink opaque (LDleft n)), -1
5888 | 0xff53 -> (* right *)
5889 Some (findlink opaque (LDright n)), 1
5891 | 0xff52 -> (* up *)
5892 Some (findlink opaque (LDup n)), -1
5894 | 0xff54 -> (* down *)
5895 Some (findlink opaque (LDdown n)), 1
5897 | _ -> None, 0
5899 let pwl l dir =
5900 begin match findpwl l.pageno dir with
5901 | Pwlnotfound -> ()
5902 | Pwl pageno ->
5903 let notfound dir =
5904 state.mode <- LinkNav (Ltgendir dir);
5905 let y, h = getpageyh pageno in
5906 let y =
5907 if dir < 0
5908 then y + h - state.winh
5909 else y
5911 gotoy y
5913 begin match getopaque pageno, getpage pageno with
5914 | Some opaque, Some _ ->
5915 let link =
5916 let ld = if dir > 0 then LDfirst else LDlast in
5917 findlink opaque ld
5919 begin match link with
5920 | Lfound m ->
5921 showlinktype (getlink opaque m);
5922 state.mode <- LinkNav (Ltexact (pageno, m));
5923 G.postRedisplay "linknav jpage";
5924 | _ -> notfound dir
5925 end;
5926 | _ -> notfound dir
5927 end;
5928 end;
5930 begin match opt with
5931 | Some Lnotfound -> pwl l dir;
5932 | Some (Lfound m) ->
5933 if m = n
5934 then pwl l dir
5935 else (
5936 let _, y0, _, y1 = getlinkrect opaque m in
5937 if y0 < l.pagey
5938 then gotopage1 l.pageno y0
5939 else (
5940 let d = fstate.fontsize + 1 in
5941 if y1 - l.pagey > l.pagevh - d
5942 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5943 else G.postRedisplay "linknav";
5945 showlinktype (getlink opaque m);
5946 state.mode <- LinkNav (Ltexact (l.pageno, m));
5949 | None -> viewkeyboard key mask
5950 end;
5951 | _ -> viewkeyboard key mask
5953 if key = 0xff63
5954 then (
5955 state.mode <- View;
5956 G.postRedisplay "leave linknav"
5958 else
5959 match linknav with
5960 | Ltgendir _ -> viewkeyboard key mask
5961 | Ltexact exact -> doexact exact
5964 let keyboard key mask =
5965 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5966 then wcmd "interrupt"
5967 else state.uioh <- state.uioh#key key mask
5970 let birdseyekeyboard key mask
5971 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5972 let incr =
5973 match conf.columns with
5974 | Csingle _ -> 1
5975 | Cmulti ((c, _, _), _) -> c
5976 | Csplit _ -> failwith "bird's eye split mode"
5978 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5979 match key with
5980 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5981 let y, h = getpageyh pageno in
5982 let top = (state.winh - h) / 2 in
5983 gotoy (max 0 (y - top))
5984 | 0xff0d (* enter *)
5985 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5986 | 0xff1b -> leavebirdseye beye true (* escape *)
5987 | 0xff52 -> upbirdseye incr beye (* up *)
5988 | 0xff54 -> downbirdseye incr beye (* down *)
5989 | 0xff51 -> upbirdseye 1 beye (* left *)
5990 | 0xff53 -> downbirdseye 1 beye (* right *)
5992 | 0xff55 -> (* prior *)
5993 begin match state.layout with
5994 | l :: _ ->
5995 if l.pagey != 0
5996 then (
5997 state.mode <- Birdseye (
5998 oconf, leftx, l.pageno, hooverpageno, anchor
6000 gotopage1 l.pageno 0;
6002 else (
6003 let layout = layout (state.y-state.winh) (pgh state.layout) in
6004 match layout with
6005 | [] -> gotoy (clamp (-state.winh))
6006 | l :: _ ->
6007 state.mode <- Birdseye (
6008 oconf, leftx, l.pageno, hooverpageno, anchor
6010 gotopage1 l.pageno 0
6013 | [] -> gotoy (clamp (-state.winh))
6014 end;
6016 | 0xff56 -> (* next *)
6017 begin match List.rev state.layout with
6018 | l :: _ ->
6019 let layout = layout (state.y + (pgh state.layout)) state.winh in
6020 begin match layout with
6021 | [] ->
6022 let incr = l.pageh - l.pagevh in
6023 if incr = 0
6024 then (
6025 state.mode <-
6026 Birdseye (
6027 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
6029 G.postRedisplay "birdseye pagedown";
6031 else gotoy (clamp (incr + conf.interpagespace*2));
6033 | l :: _ ->
6034 state.mode <-
6035 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
6036 gotopage1 l.pageno 0;
6039 | [] -> gotoy (clamp state.winh)
6040 end;
6042 | 0xff50 -> (* home *)
6043 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
6044 gotopage1 0 0
6046 | 0xff57 -> (* end *)
6047 let pageno = state.pagecount - 1 in
6048 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
6049 if not (pagevisible state.layout pageno)
6050 then
6051 let h =
6052 match List.rev state.pdims with
6053 | [] -> state.winh
6054 | (_, _, h, _) :: _ -> h
6056 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
6057 else G.postRedisplay "birdseye end";
6058 | _ -> viewkeyboard key mask
6061 let drawpage l =
6062 let color =
6063 match state.mode with
6064 | Textentry _ -> scalecolor 0.4
6065 | LinkNav _
6066 | View -> scalecolor 1.0
6067 | Birdseye (_, _, pageno, hooverpageno, _) ->
6068 if l.pageno = hooverpageno
6069 then scalecolor 0.9
6070 else (
6071 if l.pageno = pageno
6072 then scalecolor 1.0
6073 else scalecolor 0.8
6076 drawtiles l color;
6079 let postdrawpage l linkindexbase =
6080 match getopaque l.pageno with
6081 | Some opaque ->
6082 if tileready l l.pagex l.pagey
6083 then
6084 let x = l.pagedispx - l.pagex
6085 and y = l.pagedispy - l.pagey in
6086 let hlmask =
6087 match conf.columns with
6088 | Csingle _ | Cmulti _ ->
6089 (if conf.hlinks then 1 else 0)
6090 + (if state.glinks
6091 && not (isbirdseye state.mode) then 2 else 0)
6092 | _ -> 0
6094 let s =
6095 match state.mode with
6096 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6097 | _ -> ""
6099 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6100 else 0
6101 | _ -> 0
6104 let scrollindicator () =
6105 let sbw, ph, sh = state.uioh#scrollph in
6106 let sbh, pw, sw = state.uioh#scrollpw in
6108 GlDraw.color (0.64, 0.64, 0.64);
6109 filledrect
6110 (float (state.winw - sbw)) 0.
6111 (float state.winw) (float state.winh)
6113 filledrect
6114 0. (float (state.winh - sbh))
6115 (float (wadjsb state.winw - 1)) (float state.winh)
6117 GlDraw.color (0.0, 0.0, 0.0);
6119 filledrect
6120 (float (state.winw - sbw)) ph
6121 (float state.winw) (ph +. sh)
6123 filledrect
6124 pw (float (state.winh - sbh))
6125 (pw +. sw) (float state.winh)
6129 let showsel () =
6130 match state.mstate with
6131 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6134 | Msel ((x0, y0), (x1, y1)) ->
6135 let rec loop = function
6136 | l :: ls ->
6137 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
6138 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
6139 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
6140 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
6141 then
6142 match getopaque l.pageno with
6143 | Some opaque ->
6144 let x0, y0 = pagetranslatepoint l x0 y0 in
6145 let x1, y1 = pagetranslatepoint l x1 y1 in
6146 seltext opaque (x0, y0, x1, y1);
6147 | _ -> ()
6148 else loop ls
6149 | [] -> ()
6151 loop state.layout
6154 let showrects = function [] -> () | rects ->
6155 Gl.enable `blend;
6156 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6157 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6158 List.iter
6159 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6160 List.iter (fun l ->
6161 if l.pageno = pageno
6162 then (
6163 let dx = float (l.pagedispx - l.pagex) in
6164 let dy = float (l.pagedispy - l.pagey) in
6165 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6166 Raw.sets_float state.vraw ~pos:0
6167 [| x0+.dx; y0+.dy;
6168 x1+.dx; y1+.dy;
6169 x3+.dx; y3+.dy;
6170 x2+.dx; y2+.dy |];
6171 GlArray.vertex `two state.vraw;
6172 GlArray.draw_arrays `triangle_strip 0 4;
6174 ) state.layout
6175 ) rects
6177 Gl.disable `blend;
6180 let display () =
6181 GlClear.color (scalecolor2 conf.bgcolor);
6182 GlClear.clear [`color];
6183 List.iter drawpage state.layout;
6184 let rects =
6185 match state.mode with
6186 | LinkNav (Ltexact (pageno, linkno)) ->
6187 begin match getopaque pageno with
6188 | Some opaque ->
6189 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6190 (pageno, 5, (
6191 float x0, float y0,
6192 float x1, float y0,
6193 float x1, float y1,
6194 float x0, float y1)
6195 ) :: state.rects
6196 | None -> state.rects
6198 | _ -> state.rects
6200 showrects rects;
6201 let rec postloop linkindexbase = function
6202 | l :: rest ->
6203 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6204 postloop linkindexbase rest
6205 | [] -> ()
6207 showsel ();
6208 postloop 0 state.layout;
6209 state.uioh#display;
6210 begin match state.mstate with
6211 | Mzoomrect ((x0, y0), (x1, y1)) ->
6212 Gl.enable `blend;
6213 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6214 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6215 filledrect (float x0) (float y0) (float x1) (float y1);
6216 Gl.disable `blend;
6217 | _ -> ()
6218 end;
6219 enttext ();
6220 scrollindicator ();
6221 Wsi.swapb ();
6224 let zoomrect x y x1 y1 =
6225 let x0 = min x x1
6226 and x1 = max x x1
6227 and y0 = min y y1 in
6228 gotoy (state.y + y0);
6229 state.anchor <- getanchor ();
6230 let zoom = (float state.w) /. float (x1 - x0) in
6231 let margin =
6232 match conf.fitmodel, conf.columns with
6233 | FitPage, Csplit _ ->
6234 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6236 | _, _ ->
6237 let adjw = wadjsb state.winw in
6238 if state.w < adjw
6239 then (adjw - state.w) / 2
6240 else 0
6242 state.x <- (state.x + margin) - x0;
6243 setzoom zoom;
6244 Wsi.setcursor Wsi.CURSOR_INHERIT;
6245 state.mstate <- Mnone;
6248 let zoomblock x y =
6249 let g opaque l px py =
6250 match rectofblock opaque px py with
6251 | Some a ->
6252 let x0 = a.(0) -. 20. in
6253 let x1 = a.(1) +. 20. in
6254 let y0 = a.(2) -. 20. in
6255 let zoom = (float state.w) /. (x1 -. x0) in
6256 let pagey = getpagey l.pageno in
6257 gotoy_and_clear_text (pagey + truncate y0);
6258 state.anchor <- getanchor ();
6259 let margin = (state.w - l.pagew)/2 in
6260 state.x <- -truncate x0 - margin;
6261 setzoom zoom;
6262 None
6263 | None -> None
6265 match conf.columns with
6266 | Csplit _ ->
6267 showtext '!' "block zooming does not work properly in split columns mode"
6268 | _ -> onppundermouse g x y ()
6271 let scrollx x =
6272 let winw = wadjsb state.winw - 1 in
6273 let s = float x /. float winw in
6274 let destx = truncate (float (state.w + winw) *. s) in
6275 state.x <- winw - destx;
6276 gotoy_and_clear_text state.y;
6277 state.mstate <- Mscrollx;
6280 let scrolly y =
6281 let s = float y /. float state.winh in
6282 let desty = truncate (float (state.maxy - state.winh) *. s) in
6283 gotoy_and_clear_text desty;
6284 state.mstate <- Mscrolly;
6287 let viewmulticlick clicks x y mask =
6288 let g opaque l px py =
6289 let mark =
6290 match clicks with
6291 | 2 -> Mark_word
6292 | 3 -> Mark_line
6293 | 4 -> Mark_block
6294 | _ -> Mark_page
6296 if markunder opaque px py mark
6297 then (
6298 Some (fun () ->
6299 match getopaque l.pageno with
6300 | None -> ()
6301 | Some opaque ->
6302 match Ne.pipe () with
6303 | Ne.Exn exn ->
6304 showtext '!'
6305 (Printf.sprintf
6306 "can not create mark pipe: %s"
6307 (exntos exn));
6308 | Ne.Res (r, w) ->
6309 let dopipe cmd =
6310 let doclose what fd =
6311 Ne.clo fd (fun msg ->
6312 dolog "%s close failed: %s" what msg)
6315 popen cmd [r, 0; w, -1];
6316 copysel w opaque false;
6317 doclose "pipe/r" r;
6318 G.postRedisplay "viewmulticlick";
6319 with exn ->
6320 dolog "can not execute %S: %s" cmd (exntos exn);
6321 doclose "pipe/r" r;
6322 doclose "pipe/w" w;
6324 if Wsi.withctrl mask
6325 then state.roam <- (fun () -> dopipe conf.paxcmd)
6326 else dopipe conf.selcmd
6329 else None
6331 G.postRedisplay "viewmulticlick";
6332 onppundermouse g x y (fun () -> showtext '!' "Daisy whoopsie") ();
6335 let viewmouse button down x y mask =
6336 match button with
6337 | n when (n == 4 || n == 5) && not down ->
6338 if Wsi.withctrl mask
6339 then (
6340 match state.mstate with
6341 | Mzoom (oldn, i) ->
6342 if oldn = n
6343 then (
6344 if i = 2
6345 then
6346 let incr =
6347 match n with
6348 | 5 ->
6349 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6350 | _ ->
6351 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6353 let zoom = conf.zoom -. incr in
6354 setzoom zoom;
6355 state.mstate <- Mzoom (n, 0);
6356 else
6357 state.mstate <- Mzoom (n, i+1);
6359 else state.mstate <- Mzoom (n, 0)
6361 | _ -> state.mstate <- Mzoom (n, 0)
6363 else (
6364 match state.autoscroll with
6365 | Some step -> setautoscrollspeed step (n=4)
6366 | None ->
6367 if conf.wheelbypage || conf.presentation
6368 then (
6369 if n = 4
6370 then prevpage ()
6371 else nextpage ()
6373 else
6374 let incr =
6375 if n = 4
6376 then -conf.scrollstep
6377 else conf.scrollstep
6379 let incr = incr * 2 in
6380 let y = clamp incr in
6381 gotoy_and_clear_text y
6384 | n when (n = 6 || n = 7) && not down && canpan () ->
6385 state.x <-
6386 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6387 gotoy_and_clear_text state.y
6389 | 1 when Wsi.withshift mask ->
6390 state.mstate <- Mnone;
6391 if not down
6392 then (
6393 match unproject x y with
6394 | Some (pageno, ux, uy) ->
6395 let cmd = Printf.sprintf
6396 "%s %s %d %d %d"
6397 conf.stcmd state.path pageno ux uy
6399 popen cmd []
6400 | None -> ()
6403 | 1 when Wsi.withctrl mask ->
6404 if down
6405 then (
6406 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6407 state.mstate <- Mpan (x, y)
6409 else
6410 state.mstate <- Mnone
6412 | 3 ->
6413 if down
6414 then (
6415 Wsi.setcursor Wsi.CURSOR_CYCLE;
6416 let p = (x, y) in
6417 state.mstate <- Mzoomrect (p, p)
6419 else (
6420 match state.mstate with
6421 | Mzoomrect ((x0, y0), _) ->
6422 if abs (x-x0) > 10 && abs (y - y0) > 10
6423 then zoomrect x0 y0 x y
6424 else (
6425 state.mstate <- Mnone;
6426 Wsi.setcursor Wsi.CURSOR_INHERIT;
6427 G.postRedisplay "kill accidental zoom rect";
6429 | _ ->
6430 Wsi.setcursor Wsi.CURSOR_INHERIT;
6431 state.mstate <- Mnone
6434 | 1 when x > state.winw - vscrollw () ->
6435 if down
6436 then
6437 let _, position, sh = state.uioh#scrollph in
6438 if y > truncate position && y < truncate (position +. sh)
6439 then state.mstate <- Mscrolly
6440 else scrolly y
6441 else
6442 state.mstate <- Mnone
6444 | 1 when y > state.winh - hscrollh () ->
6445 if down
6446 then
6447 let _, position, sw = state.uioh#scrollpw in
6448 if x > truncate position && x < truncate (position +. sw)
6449 then state.mstate <- Mscrollx
6450 else scrollx x
6451 else
6452 state.mstate <- Mnone
6454 | 1 when state.bzoom -> if not down then zoomblock x y
6456 | 1 ->
6457 let dest = if down then getunder x y else Unone in
6458 begin match dest with
6459 | Ulinkgoto _
6460 | Ulinkuri _
6461 | Uremote _ | Uremotedest _
6462 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6463 gotounder dest
6465 | Unone when down ->
6466 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6467 state.mstate <- Mpan (x, y);
6469 | Unone | Utext _ ->
6470 if down
6471 then (
6472 if conf.angle mod 360 = 0
6473 then (
6474 state.mstate <- Msel ((x, y), (x, y));
6475 G.postRedisplay "mouse select";
6478 else (
6479 match state.mstate with
6480 | Mnone -> ()
6482 | Mzoom _ | Mscrollx | Mscrolly ->
6483 state.mstate <- Mnone
6485 | Mzoomrect ((x0, y0), _) ->
6486 zoomrect x0 y0 x y
6488 | Mpan _ ->
6489 Wsi.setcursor Wsi.CURSOR_INHERIT;
6490 state.mstate <- Mnone
6492 | Msel ((x0, y0), (x1, y1)) ->
6493 let rec loop = function
6494 | [] -> ()
6495 | l :: rest ->
6496 let inside =
6497 let a0 = l.pagedispy in
6498 let a1 = a0 + l.pagevh in
6499 let b0 = l.pagedispx in
6500 let b1 = b0 + l.pagevw in
6501 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6502 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6504 if inside
6505 then
6506 match getopaque l.pageno with
6507 | Some opaque ->
6508 begin
6509 match Ne.pipe () with
6510 | Ne.Exn exn ->
6511 showtext '!'
6512 (Printf.sprintf
6513 "can not create sel pipe: %s"
6514 (exntos exn));
6515 | Ne.Res (r, w) ->
6516 let doclose what fd =
6517 Ne.clo fd (fun msg ->
6518 dolog "%s close failed: %s" what msg)
6521 popen conf.selcmd [r, 0; w, -1];
6522 copysel w opaque true;
6523 doclose "pipe/r" r;
6524 G.postRedisplay "copysel";
6525 with exn ->
6526 dolog "can not execute %S: %s"
6527 conf.selcmd (exntos exn);
6528 doclose "pipe/r" r;
6529 doclose "pipe/w" w;
6531 | None -> ()
6532 else loop rest
6534 loop state.layout;
6535 Wsi.setcursor Wsi.CURSOR_INHERIT;
6536 state.mstate <- Mnone;
6540 | _ -> ()
6543 let birdseyemouse button down x y mask
6544 (conf, leftx, _, hooverpageno, anchor) =
6545 match button with
6546 | 1 when down ->
6547 let rec loop = function
6548 | [] -> ()
6549 | l :: rest ->
6550 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6551 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6552 then (
6553 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6555 else loop rest
6557 loop state.layout
6558 | 3 -> ()
6559 | _ -> viewmouse button down x y mask
6562 let uioh = object
6563 method display = ()
6565 method key key mask =
6566 begin match state.mode with
6567 | Textentry textentry -> textentrykeyboard key mask textentry
6568 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6569 | View -> viewkeyboard key mask
6570 | LinkNav linknav -> linknavkeyboard key mask linknav
6571 end;
6572 state.uioh
6574 method button button bstate x y mask =
6575 begin match state.mode with
6576 | LinkNav _
6577 | View -> viewmouse button bstate x y mask
6578 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6579 | Textentry _ -> ()
6580 end;
6581 state.uioh
6583 method multiclick clicks x y mask =
6584 begin match state.mode with
6585 | LinkNav _
6586 | View -> viewmulticlick clicks x y mask
6587 | Birdseye _
6588 | Textentry _ -> ()
6589 end;
6590 state.uioh
6592 method motion x y =
6593 begin match state.mode with
6594 | Textentry _ -> ()
6595 | View | Birdseye _ | LinkNav _ ->
6596 match state.mstate with
6597 | Mzoom _ | Mnone -> ()
6599 | Mpan (x0, y0) ->
6600 let dx = x - x0
6601 and dy = y0 - y in
6602 state.mstate <- Mpan (x, y);
6603 if canpan ()
6604 then state.x <- panbound (state.x + dx);
6605 let y = clamp dy in
6606 gotoy_and_clear_text y
6608 | Msel (a, _) ->
6609 state.mstate <- Msel (a, (x, y));
6610 G.postRedisplay "motion select";
6612 | Mscrolly ->
6613 let y = min state.winh (max 0 y) in
6614 scrolly y
6616 | Mscrollx ->
6617 let x = min state.winw (max 0 x) in
6618 scrollx x
6620 | Mzoomrect (p0, _) ->
6621 state.mstate <- Mzoomrect (p0, (x, y));
6622 G.postRedisplay "motion zoomrect";
6623 end;
6624 state.uioh
6626 method pmotion x y =
6627 begin match state.mode with
6628 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6629 let rec loop = function
6630 | [] ->
6631 if hooverpageno != -1
6632 then (
6633 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6634 G.postRedisplay "pmotion birdseye no hoover";
6636 | l :: rest ->
6637 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6638 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6639 then (
6640 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6641 G.postRedisplay "pmotion birdseye hoover";
6643 else loop rest
6645 loop state.layout
6647 | Textentry _ -> ()
6649 | LinkNav _
6650 | View ->
6651 match state.mstate with
6652 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6654 | Mnone ->
6655 updateunder x y;
6656 match conf.pax with
6657 | None -> ()
6658 | Some r ->
6659 let past, _, _ = !r in
6660 let now = now () in
6661 let delta = now -. past in
6662 if delta > 0.01
6663 then paxunder x y
6664 else r := (now, x, y)
6665 end;
6666 state.uioh
6668 method infochanged _ = ()
6670 method scrollph =
6671 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6672 let p, h =
6673 if maxy = 0
6674 then 0.0, float state.winh
6675 else scrollph state.y maxy
6677 vscrollw (), p, h
6679 method scrollpw =
6680 let winw = wadjsb state.winw in
6681 let fwinw = float winw in
6682 let sw =
6683 let sw = fwinw /. float state.w in
6684 let sw = fwinw *. sw in
6685 max sw (float conf.scrollh)
6687 let position =
6688 let maxx = state.w + winw in
6689 let x = winw - state.x in
6690 let percent = float x /. float maxx in
6691 (fwinw -. sw) *. percent
6693 hscrollh (), position, sw
6695 method modehash =
6696 let modename =
6697 match state.mode with
6698 | LinkNav _ -> "links"
6699 | Textentry _ -> "textentry"
6700 | Birdseye _ -> "birdseye"
6701 | View -> "view"
6703 findkeyhash conf modename
6705 method eformsgs = true
6706 end;;
6708 module Config =
6709 struct
6710 open Parser
6712 let fontpath = ref "";;
6714 module KeyMap =
6715 Map.Make (struct type t = (int * int) let compare = compare end);;
6717 let unent s =
6718 let l = String.length s in
6719 let b = Buffer.create l in
6720 unent b s 0 l;
6721 Buffer.contents b;
6724 let home =
6725 try Sys.getenv "HOME"
6726 with exn ->
6727 prerr_endline
6728 ("Can not determine home directory location: " ^ exntos exn);
6732 let modifier_of_string = function
6733 | "alt" -> Wsi.altmask
6734 | "shift" -> Wsi.shiftmask
6735 | "ctrl" | "control" -> Wsi.ctrlmask
6736 | "meta" -> Wsi.metamask
6737 | _ -> 0
6740 let key_of_string =
6741 let r = Str.regexp "-" in
6742 fun s ->
6743 let elems = Str.full_split r s in
6744 let f n k m =
6745 let g s =
6746 let m1 = modifier_of_string s in
6747 if m1 = 0
6748 then (Wsi.namekey s, m)
6749 else (k, m lor m1)
6750 in function
6751 | Str.Delim s when n land 1 = 0 -> g s
6752 | Str.Text s -> g s
6753 | Str.Delim _ -> (k, m)
6755 let rec loop n k m = function
6756 | [] -> (k, m)
6757 | x :: xs ->
6758 let k, m = f n k m x in
6759 loop (n+1) k m xs
6761 loop 0 0 0 elems
6764 let keys_of_string =
6765 let r = Str.regexp "[ \t]" in
6766 fun s ->
6767 let elems = Str.split r s in
6768 List.map key_of_string elems
6771 let copykeyhashes c =
6772 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6775 let config_of c attrs =
6776 let apply c k v =
6778 match k with
6779 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6780 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6781 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6782 | "preload" -> { c with preload = bool_of_string v }
6783 | "page-bias" -> { c with pagebias = int_of_string v }
6784 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6785 | "horizontal-scroll-step" ->
6786 { c with hscrollstep = max (int_of_string v) 1 }
6787 | "auto-scroll-step" ->
6788 { c with autoscrollstep = max 0 (int_of_string v) }
6789 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6790 | "crop-hack" -> { c with crophack = bool_of_string v }
6791 | "throttle" ->
6792 let mw =
6793 match String.lowercase v with
6794 | "true" -> Some infinity
6795 | "false" -> None
6796 | f -> Some (float_of_string f)
6798 { c with maxwait = mw}
6799 | "highlight-links" -> { c with hlinks = bool_of_string v }
6800 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6801 | "vertical-margin" ->
6802 { c with interpagespace = max 0 (int_of_string v) }
6803 | "zoom" ->
6804 let zoom = float_of_string v /. 100. in
6805 let zoom = max zoom 0.0 in
6806 { c with zoom = zoom }
6807 | "presentation" -> { c with presentation = bool_of_string v }
6808 | "rotation-angle" -> { c with angle = int_of_string v }
6809 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6810 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6811 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6812 | "proportional-display" ->
6813 let fm =
6814 if bool_of_string v
6815 then FitProportional
6816 else FitWidth
6818 { c with fitmodel = fm }
6819 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6820 | "pixmap-cache-size" ->
6821 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6822 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6823 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6824 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6825 | "persistent-location" -> { c with jumpback = bool_of_string v }
6826 | "background-color" -> { c with bgcolor = color_of_string v }
6827 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6828 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6829 | "mupdf-store-size" ->
6830 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6831 | "checkers" -> { c with checkers = bool_of_string v }
6832 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6833 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6834 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6835 | "uri-launcher" -> { c with urilauncher = unent v }
6836 | "path-launcher" -> { c with pathlauncher = unent v }
6837 | "color-space" -> { c with colorspace = CSTE.of_string v }
6838 | "invert-colors" -> { c with invert = bool_of_string v }
6839 | "brightness" -> { c with colorscale = float_of_string v }
6840 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6841 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6842 | "columns" ->
6843 let (n, _, _) as nab = multicolumns_of_string v in
6844 if n < 0
6845 then { c with columns = Csplit (-n, [||]) }
6846 else { c with columns = Cmulti (nab, [||]) }
6847 | "birds-eye-columns" ->
6848 { c with beyecolumns = Some (max (int_of_string v) 2) }
6849 | "selection-command" -> { c with selcmd = unent v }
6850 | "synctex-command" -> { c with stcmd = unent v }
6851 | "pax-command" -> { c with paxcmd = unent v }
6852 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6853 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6854 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6855 | "use-pbo" -> { c with usepbo = bool_of_string v }
6856 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6857 | "horizontal-scrollbar-visible" ->
6858 let b =
6859 if bool_of_string v
6860 then c.scrollb lor scrollbhv
6861 else c.scrollb land (lnot scrollbhv)
6863 { c with scrollb = b }
6864 | "vertical-scrollbar-visible" ->
6865 let b =
6866 if bool_of_string v
6867 then c.scrollb lor scrollbvv
6868 else c.scrollb land (lnot scrollbvv)
6870 { c with scrollb = b }
6871 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6872 | "point-and-x" ->
6873 { c with pax =
6874 if bool_of_string v
6875 then Some (ref (0.0, 0, 0))
6876 else None }
6877 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6878 | _ -> c
6879 with exn ->
6880 prerr_endline ("Error processing attribute (`" ^
6881 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6884 let rec fold c = function
6885 | [] -> c
6886 | (k, v) :: rest ->
6887 let c = apply c k v in
6888 fold c rest
6890 fold { c with keyhashes = copykeyhashes c } attrs;
6893 let fromstring f pos n v d =
6894 try f v
6895 with exn ->
6896 dolog "Error processing attribute (%S=%S) at %d\n%s"
6897 n v pos (exntos exn)
6902 let bookmark_of attrs =
6903 let rec fold title page rely visy = function
6904 | ("title", v) :: rest -> fold v page rely visy rest
6905 | ("page", v) :: rest -> fold title v rely visy rest
6906 | ("rely", v) :: rest -> fold title page v visy rest
6907 | ("visy", v) :: rest -> fold title page rely v rest
6908 | _ :: rest -> fold title page rely visy rest
6909 | [] -> title, page, rely, visy
6911 fold "invalid" "0" "0" "0" attrs
6914 let doc_of attrs =
6915 let rec fold path page rely pan visy = function
6916 | ("path", v) :: rest -> fold v page rely pan visy rest
6917 | ("page", v) :: rest -> fold path v rely pan visy rest
6918 | ("rely", v) :: rest -> fold path page v pan visy rest
6919 | ("pan", v) :: rest -> fold path page rely v visy rest
6920 | ("visy", v) :: rest -> fold path page rely pan v rest
6921 | _ :: rest -> fold path page rely pan visy rest
6922 | [] -> path, page, rely, pan, visy
6924 fold "" "0" "0" "0" "0" attrs
6927 let map_of attrs =
6928 let rec fold rs ls = function
6929 | ("out", v) :: rest -> fold v ls rest
6930 | ("in", v) :: rest -> fold rs v rest
6931 | _ :: rest -> fold ls rs rest
6932 | [] -> ls, rs
6934 fold "" "" attrs
6937 let setconf dst src =
6938 dst.scrollbw <- src.scrollbw;
6939 dst.scrollh <- src.scrollh;
6940 dst.icase <- src.icase;
6941 dst.preload <- src.preload;
6942 dst.pagebias <- src.pagebias;
6943 dst.verbose <- src.verbose;
6944 dst.scrollstep <- src.scrollstep;
6945 dst.maxhfit <- src.maxhfit;
6946 dst.crophack <- src.crophack;
6947 dst.autoscrollstep <- src.autoscrollstep;
6948 dst.maxwait <- src.maxwait;
6949 dst.hlinks <- src.hlinks;
6950 dst.underinfo <- src.underinfo;
6951 dst.interpagespace <- src.interpagespace;
6952 dst.zoom <- src.zoom;
6953 dst.presentation <- src.presentation;
6954 dst.angle <- src.angle;
6955 dst.cwinw <- src.cwinw;
6956 dst.cwinh <- src.cwinh;
6957 dst.savebmarks <- src.savebmarks;
6958 dst.memlimit <- src.memlimit;
6959 dst.fitmodel <- src.fitmodel;
6960 dst.texcount <- src.texcount;
6961 dst.sliceheight <- src.sliceheight;
6962 dst.thumbw <- src.thumbw;
6963 dst.jumpback <- src.jumpback;
6964 dst.bgcolor <- src.bgcolor;
6965 dst.tilew <- src.tilew;
6966 dst.tileh <- src.tileh;
6967 dst.mustoresize <- src.mustoresize;
6968 dst.checkers <- src.checkers;
6969 dst.aalevel <- src.aalevel;
6970 dst.trimmargins <- src.trimmargins;
6971 dst.trimfuzz <- src.trimfuzz;
6972 dst.urilauncher <- src.urilauncher;
6973 dst.colorspace <- src.colorspace;
6974 dst.invert <- src.invert;
6975 dst.colorscale <- src.colorscale;
6976 dst.redirectstderr <- src.redirectstderr;
6977 dst.ghyllscroll <- src.ghyllscroll;
6978 dst.columns <- src.columns;
6979 dst.beyecolumns <- src.beyecolumns;
6980 dst.selcmd <- src.selcmd;
6981 dst.updatecurs <- src.updatecurs;
6982 dst.pathlauncher <- src.pathlauncher;
6983 dst.keyhashes <- copykeyhashes src;
6984 dst.hfsize <- src.hfsize;
6985 dst.hscrollstep <- src.hscrollstep;
6986 dst.pgscale <- src.pgscale;
6987 dst.usepbo <- src.usepbo;
6988 dst.wheelbypage <- src.wheelbypage;
6989 dst.stcmd <- src.stcmd;
6990 dst.paxcmd <- src.paxcmd;
6991 dst.scrollb <- src.scrollb;
6992 dst.riani <- src.riani;
6993 dst.paxmark <- src.paxmark;
6994 dst.pax <-
6995 if src.pax = None
6996 then None
6997 else Some ((ref (0.0, 0, 0)));
7000 let get s =
7001 let h = Hashtbl.create 10 in
7002 let dc = { defconf with angle = defconf.angle } in
7003 let rec toplevel v t spos _ =
7004 match t with
7005 | Vdata | Vcdata | Vend -> v
7006 | Vopen ("llppconfig", _, closed) ->
7007 if closed
7008 then v
7009 else { v with f = llppconfig }
7010 | Vopen _ ->
7011 error "unexpected subelement at top level" s spos
7012 | Vclose _ -> error "unexpected close at top level" s spos
7014 and llppconfig v t spos _ =
7015 match t with
7016 | Vdata | Vcdata -> v
7017 | Vend -> error "unexpected end of input in llppconfig" s spos
7018 | Vopen ("defaults", attrs, closed) ->
7019 let c = config_of dc attrs in
7020 setconf dc c;
7021 if closed
7022 then v
7023 else { v with f = defaults }
7025 | Vopen ("ui-font", attrs, closed) ->
7026 let rec getsize size = function
7027 | [] -> size
7028 | ("size", v) :: rest ->
7029 let size =
7030 fromstring int_of_string spos "size" v fstate.fontsize in
7031 getsize size rest
7032 | l -> getsize size l
7034 fstate.fontsize <- getsize fstate.fontsize attrs;
7035 if closed
7036 then v
7037 else { v with f = uifont (Buffer.create 10) }
7039 | Vopen ("doc", attrs, closed) ->
7040 let pathent, spage, srely, span, svisy = doc_of attrs in
7041 let path = unent pathent
7042 and pageno = fromstring int_of_string spos "page" spage 0
7043 and rely = fromstring float_of_string spos "rely" srely 0.0
7044 and pan = fromstring int_of_string spos "pan" span 0
7045 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7046 let c = config_of dc attrs in
7047 let anchor = (pageno, rely, visy) in
7048 if closed
7049 then (Hashtbl.add h path (c, [], pan, anchor); v)
7050 else { v with f = doc path pan anchor c [] }
7052 | Vopen _ ->
7053 error "unexpected subelement in llppconfig" s spos
7055 | Vclose "llppconfig" -> { v with f = toplevel }
7056 | Vclose _ -> error "unexpected close in llppconfig" s spos
7058 and defaults v t spos _ =
7059 match t with
7060 | Vdata | Vcdata -> v
7061 | Vend -> error "unexpected end of input in defaults" s spos
7062 | Vopen ("keymap", attrs, closed) ->
7063 let modename =
7064 try List.assoc "mode" attrs
7065 with Not_found -> "global" in
7066 if closed
7067 then v
7068 else
7069 let ret keymap =
7070 let h = findkeyhash dc modename in
7071 KeyMap.iter (Hashtbl.replace h) keymap;
7072 defaults
7074 { v with f = pkeymap ret KeyMap.empty }
7076 | Vopen (_, _, _) ->
7077 error "unexpected subelement in defaults" s spos
7079 | Vclose "defaults" ->
7080 { v with f = llppconfig }
7082 | Vclose _ -> error "unexpected close in defaults" s spos
7084 and uifont b v t spos epos =
7085 match t with
7086 | Vdata | Vcdata ->
7087 Buffer.add_substring b s spos (epos - spos);
7089 | Vopen (_, _, _) ->
7090 error "unexpected subelement in ui-font" s spos
7091 | Vclose "ui-font" ->
7092 if emptystr !fontpath
7093 then fontpath := Buffer.contents b;
7094 { v with f = llppconfig }
7095 | Vclose _ -> error "unexpected close in ui-font" s spos
7096 | Vend -> error "unexpected end of input in ui-font" s spos
7098 and doc path pan anchor c bookmarks v t spos _ =
7099 match t with
7100 | Vdata | Vcdata -> v
7101 | Vend -> error "unexpected end of input in doc" s spos
7102 | Vopen ("bookmarks", _, closed) ->
7103 if closed
7104 then v
7105 else { v with f = pbookmarks path pan anchor c bookmarks }
7107 | Vopen ("keymap", attrs, closed) ->
7108 let modename =
7109 try List.assoc "mode" attrs
7110 with Not_found -> "global"
7112 if closed
7113 then v
7114 else
7115 let ret keymap =
7116 let h = findkeyhash c modename in
7117 KeyMap.iter (Hashtbl.replace h) keymap;
7118 doc path pan anchor c bookmarks
7120 { v with f = pkeymap ret KeyMap.empty }
7122 | Vopen (_, _, _) ->
7123 error "unexpected subelement in doc" s spos
7125 | Vclose "doc" ->
7126 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7127 { v with f = llppconfig }
7129 | Vclose _ -> error "unexpected close in doc" s spos
7131 and pkeymap ret keymap v t spos _ =
7132 match t with
7133 | Vdata | Vcdata -> v
7134 | Vend -> error "unexpected end of input in keymap" s spos
7135 | Vopen ("map", attrs, closed) ->
7136 let r, l = map_of attrs in
7137 let kss = fromstring keys_of_string spos "in" r [] in
7138 let lss = fromstring keys_of_string spos "out" l [] in
7139 let keymap =
7140 match kss with
7141 | [] -> keymap
7142 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7143 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7145 if closed
7146 then { v with f = pkeymap ret keymap }
7147 else
7148 let f () = v in
7149 { v with f = skip "map" f }
7151 | Vopen _ ->
7152 error "unexpected subelement in keymap" s spos
7154 | Vclose "keymap" ->
7155 { v with f = ret keymap }
7157 | Vclose _ -> error "unexpected close in keymap" s spos
7159 and pbookmarks path pan anchor c bookmarks v t spos _ =
7160 match t with
7161 | Vdata | Vcdata -> v
7162 | Vend -> error "unexpected end of input in bookmarks" s spos
7163 | Vopen ("item", attrs, closed) ->
7164 let titleent, spage, srely, svisy = bookmark_of attrs in
7165 let page = fromstring int_of_string spos "page" spage 0
7166 and rely = fromstring float_of_string spos "rely" srely 0.0
7167 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7168 let bookmarks =
7169 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
7171 if closed
7172 then { v with f = pbookmarks path pan anchor c bookmarks }
7173 else
7174 let f () = v in
7175 { v with f = skip "item" f }
7177 | Vopen _ ->
7178 error "unexpected subelement in bookmarks" s spos
7180 | Vclose "bookmarks" ->
7181 { v with f = doc path pan anchor c bookmarks }
7183 | Vclose _ -> error "unexpected close in bookmarks" s spos
7185 and skip tag f v t spos _ =
7186 match t with
7187 | Vdata | Vcdata -> v
7188 | Vend ->
7189 error ("unexpected end of input in skipped " ^ tag) s spos
7190 | Vopen (tag', _, closed) ->
7191 if closed
7192 then v
7193 else
7194 let f' () = { v with f = skip tag f } in
7195 { v with f = skip tag' f' }
7196 | Vclose ctag ->
7197 if tag = ctag
7198 then f ()
7199 else error ("unexpected close in skipped " ^ tag) s spos
7202 parse { f = toplevel; accu = () } s;
7203 h, dc;
7206 let do_load f ic =
7208 let len = in_channel_length ic in
7209 let s = String.create len in
7210 really_input ic s 0 len;
7211 f s;
7212 with
7213 | Parse_error (msg, s, pos) ->
7214 let subs = subs s pos in
7215 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7217 | exn ->
7218 failwith ("config load error: " ^ exntos exn)
7221 let defconfpath =
7222 let dir =
7224 let dir = Filename.concat home ".config" in
7225 if Sys.is_directory dir then dir else home
7226 with _ -> home
7228 Filename.concat dir "llpp.conf"
7231 let confpath = ref defconfpath;;
7233 let load1 f =
7234 if Sys.file_exists !confpath
7235 then
7236 match
7237 (try Some (open_in_bin !confpath)
7238 with exn ->
7239 prerr_endline
7240 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7241 exntos exn);
7242 None
7244 with
7245 | Some ic ->
7246 let success =
7248 f (do_load get ic)
7249 with exn ->
7250 prerr_endline
7251 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7252 exntos exn);
7253 false
7255 close_in ic;
7256 success
7258 | None -> false
7259 else
7260 f (Hashtbl.create 0, defconf)
7263 let load () =
7264 let f (h, dc) =
7265 let pc, pb, px, pa =
7267 let key =
7268 if emptystr state.origin
7269 then state.path
7270 else state.origin
7272 Hashtbl.find h (Filename.basename key)
7273 with Not_found -> dc, [], 0, emptyanchor
7275 setconf defconf dc;
7276 setconf conf pc;
7277 state.bookmarks <- pb;
7278 state.x <- px;
7279 if conf.jumpback
7280 then state.anchor <- pa;
7281 cbput state.hists.nav pa;
7282 true
7284 load1 f
7287 let add_attrs bb always dc c =
7288 let ob s a b =
7289 if always || a != b
7290 then Printf.bprintf bb "\n %s='%b'" s a
7291 and op s a b =
7292 if always || a <> b
7293 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7294 and oi s a b =
7295 if always || a != b
7296 then Printf.bprintf bb "\n %s='%d'" s a
7297 and oI s a b =
7298 if always || a != b
7299 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7300 and oz s a b =
7301 if always || a <> b
7302 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7303 and oF s a b =
7304 if always || a <> b
7305 then Printf.bprintf bb "\n %s='%f'" s a
7306 and oc s a b =
7307 if always || a <> b
7308 then
7309 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7310 and oC s a b =
7311 if always || a <> b
7312 then
7313 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7314 and oR s a b =
7315 if always || a <> b
7316 then
7317 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7318 and os s a b =
7319 if always || a <> b
7320 then
7321 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7322 and og s a b =
7323 if always || a <> b
7324 then
7325 match a with
7326 | Some (_N, _A, _B) ->
7327 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7328 | None ->
7329 match b with
7330 | None -> ()
7331 | _ ->
7332 Printf.bprintf bb "\n %s='none'" s
7333 and oW s a b =
7334 if always || a <> b
7335 then
7336 let v =
7337 match a with
7338 | None -> "false"
7339 | Some f ->
7340 if f = infinity
7341 then "true"
7342 else string_of_float f
7344 Printf.bprintf bb "\n %s='%s'" s v
7345 and oco s a b =
7346 if always || a <> b
7347 then
7348 match a with
7349 | Cmulti ((n, a, b), _) when n > 1 ->
7350 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7351 | Csplit (n, _) when n > 1 ->
7352 Printf.bprintf bb "\n %s='%d'" s ~-n
7353 | _ -> ()
7354 and obeco s a b =
7355 if always || a <> b
7356 then
7357 match a with
7358 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7359 | _ -> ()
7360 and oFm s a b =
7361 if always || a <> b
7362 then
7363 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7364 and oSv s a b m =
7365 if always || a <> b
7366 then
7367 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7368 and oPm s a b =
7369 if always || a <> b
7370 then
7371 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7373 oi "width" c.cwinw dc.cwinw;
7374 oi "height" c.cwinh dc.cwinh;
7375 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7376 oi "scroll-handle-height" c.scrollh dc.scrollh;
7377 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7378 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7379 ob "case-insensitive-search" c.icase dc.icase;
7380 ob "preload" c.preload dc.preload;
7381 oi "page-bias" c.pagebias dc.pagebias;
7382 oi "scroll-step" c.scrollstep dc.scrollstep;
7383 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7384 ob "max-height-fit" c.maxhfit dc.maxhfit;
7385 ob "crop-hack" c.crophack dc.crophack;
7386 oW "throttle" c.maxwait dc.maxwait;
7387 ob "highlight-links" c.hlinks dc.hlinks;
7388 ob "under-cursor-info" c.underinfo dc.underinfo;
7389 oi "vertical-margin" c.interpagespace dc.interpagespace;
7390 oz "zoom" c.zoom dc.zoom;
7391 ob "presentation" c.presentation dc.presentation;
7392 oi "rotation-angle" c.angle dc.angle;
7393 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7394 oFm "fit-model" c.fitmodel dc.fitmodel;
7395 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7396 oi "tex-count" c.texcount dc.texcount;
7397 oi "slice-height" c.sliceheight dc.sliceheight;
7398 oi "thumbnail-width" c.thumbw dc.thumbw;
7399 ob "persistent-location" c.jumpback dc.jumpback;
7400 oc "background-color" c.bgcolor dc.bgcolor;
7401 oi "tile-width" c.tilew dc.tilew;
7402 oi "tile-height" c.tileh dc.tileh;
7403 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7404 ob "checkers" c.checkers dc.checkers;
7405 oi "aalevel" c.aalevel dc.aalevel;
7406 ob "trim-margins" c.trimmargins dc.trimmargins;
7407 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7408 os "uri-launcher" c.urilauncher dc.urilauncher;
7409 os "path-launcher" c.pathlauncher dc.pathlauncher;
7410 oC "color-space" c.colorspace dc.colorspace;
7411 ob "invert-colors" c.invert dc.invert;
7412 oF "brightness" c.colorscale dc.colorscale;
7413 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7414 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7415 oco "columns" c.columns dc.columns;
7416 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7417 os "selection-command" c.selcmd dc.selcmd;
7418 os "synctex-command" c.stcmd dc.stcmd;
7419 os "pax-command" c.paxcmd dc.paxcmd;
7420 ob "update-cursor" c.updatecurs dc.updatecurs;
7421 oi "hint-font-size" c.hfsize dc.hfsize;
7422 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7423 oF "page-scroll-scale" c.pgscale dc.pgscale;
7424 ob "use-pbo" c.usepbo dc.usepbo;
7425 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7426 ob "remote-in-a-new-instance" c.riani dc.riani;
7427 op "point-and-x" c.pax dc.pax;
7428 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7431 let keymapsbuf always dc c =
7432 let bb = Buffer.create 16 in
7433 let rec loop = function
7434 | [] -> ()
7435 | (modename, h) :: rest ->
7436 let dh = findkeyhash dc modename in
7437 if always || h <> dh
7438 then (
7439 if Hashtbl.length h > 0
7440 then (
7441 if Buffer.length bb > 0
7442 then Buffer.add_char bb '\n';
7443 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7444 Hashtbl.iter (fun i o ->
7445 let isdifferent = always ||
7447 let dO = Hashtbl.find dh i in
7448 dO <> o
7449 with Not_found -> true
7451 if isdifferent
7452 then
7453 let addkm (k, m) =
7454 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7455 if Wsi.withalt m then Buffer.add_string bb "alt-";
7456 if Wsi.withshift m then Buffer.add_string bb "shift-";
7457 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7458 Buffer.add_string bb (Wsi.keyname k);
7460 let addkms l =
7461 let rec loop = function
7462 | [] -> ()
7463 | km :: [] -> addkm km
7464 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7466 loop l
7468 Buffer.add_string bb "<map in='";
7469 addkm i;
7470 match o with
7471 | KMinsrt km ->
7472 Buffer.add_string bb "' out='";
7473 addkm km;
7474 Buffer.add_string bb "'/>\n"
7476 | KMinsrl kms ->
7477 Buffer.add_string bb "' out='";
7478 addkms kms;
7479 Buffer.add_string bb "'/>\n"
7481 | KMmulti (ins, kms) ->
7482 Buffer.add_char bb ' ';
7483 addkms ins;
7484 Buffer.add_string bb "' out='";
7485 addkms kms;
7486 Buffer.add_string bb "'/>\n"
7487 ) h;
7488 Buffer.add_string bb "</keymap>";
7491 loop rest
7493 loop c.keyhashes;
7497 let save () =
7498 let uifontsize = fstate.fontsize in
7499 let bb = Buffer.create 32768 in
7500 let relx = float state.x /. float state.winw in
7501 let w, h, x =
7502 let cx w = truncate (relx *. float w) in
7503 List.fold_left
7504 (fun (w, h, x) ws ->
7505 match ws with
7506 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7507 | Wsi.MaxVert -> (w, conf.cwinh, x)
7508 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7510 (state.winw, state.winh, state.x) state.winstate
7512 conf.cwinw <- w;
7513 conf.cwinh <- h;
7514 let f (h, dc) =
7515 let dc = if conf.bedefault then conf else dc in
7516 Buffer.add_string bb "<llppconfig>\n";
7518 if nonemptystr !fontpath
7519 then
7520 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7521 uifontsize
7522 !fontpath
7523 else (
7524 if uifontsize <> 14
7525 then
7526 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7529 Buffer.add_string bb "<defaults";
7530 add_attrs bb true dc dc;
7531 let kb = keymapsbuf true dc dc in
7532 if Buffer.length kb > 0
7533 then (
7534 Buffer.add_string bb ">\n";
7535 Buffer.add_buffer bb kb;
7536 Buffer.add_string bb "\n</defaults>\n";
7538 else Buffer.add_string bb "/>\n";
7540 let adddoc path pan anchor c bookmarks =
7541 if bookmarks == [] && c = dc && anchor = emptyanchor
7542 then ()
7543 else (
7544 Printf.bprintf bb "<doc path='%s'"
7545 (enent path 0 (String.length path));
7547 if anchor <> emptyanchor
7548 then (
7549 let n, rely, visy = anchor in
7550 Printf.bprintf bb " page='%d'" n;
7551 if rely > 1e-6
7552 then
7553 Printf.bprintf bb " rely='%f'" rely
7555 if abs_float visy > 1e-6
7556 then
7557 Printf.bprintf bb " visy='%f'" visy
7561 if pan != 0
7562 then Printf.bprintf bb " pan='%d'" pan;
7564 add_attrs bb false dc c;
7565 let kb = keymapsbuf false dc c in
7567 begin match bookmarks with
7568 | [] ->
7569 if Buffer.length kb > 0
7570 then (
7571 Buffer.add_string bb ">\n";
7572 Buffer.add_buffer bb kb;
7573 Buffer.add_string bb "\n</doc>\n";
7575 else Buffer.add_string bb "/>\n"
7576 | _ ->
7577 Buffer.add_string bb ">\n<bookmarks>\n";
7578 List.iter (fun (title, _, kind) ->
7579 begin match kind with
7580 | Oanchor (page, rely, visy) ->
7581 Printf.bprintf bb
7582 "<item title='%s' page='%d'"
7583 (enent title 0 (String.length title))
7584 page
7586 if rely > 1e-6
7587 then
7588 Printf.bprintf bb " rely='%f'" rely
7590 if abs_float visy > 1e-6
7591 then
7592 Printf.bprintf bb " visy='%f'" visy
7594 | Onone | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
7595 failwith "unexpected link in bookmarks"
7596 end;
7597 Buffer.add_string bb "/>\n";
7598 ) bookmarks;
7599 Buffer.add_string bb "</bookmarks>";
7600 if Buffer.length kb > 0
7601 then (
7602 Buffer.add_string bb "\n";
7603 Buffer.add_buffer bb kb;
7605 Buffer.add_string bb "\n</doc>\n";
7606 end;
7610 let pan, conf =
7611 match state.mode with
7612 | Birdseye (c, pan, _, _, _) ->
7613 let beyecolumns =
7614 match conf.columns with
7615 | Cmulti ((c, _, _), _) -> Some c
7616 | Csingle _ -> None
7617 | Csplit _ -> None
7618 and columns =
7619 match c.columns with
7620 | Cmulti (c, _) -> Cmulti (c, [||])
7621 | Csingle _ -> Csingle [||]
7622 | Csplit _ -> failwith "quit from bird's eye while split"
7624 pan, { c with beyecolumns = beyecolumns; columns = columns }
7625 | _ -> x, conf
7627 let basename = Filename.basename
7628 (if emptystr state.origin then state.path else state.origin)
7630 adddoc basename pan (getanchor ())
7631 (let conf =
7632 let autoscrollstep =
7633 match state.autoscroll with
7634 | Some step -> step
7635 | None -> conf.autoscrollstep
7637 match state.mode with
7638 | Birdseye (bc, _, _, _, _) ->
7639 { conf with
7640 zoom = bc.zoom;
7641 presentation = bc.presentation;
7642 interpagespace = bc.interpagespace;
7643 maxwait = bc.maxwait;
7644 autoscrollstep = autoscrollstep }
7645 | _ -> { conf with autoscrollstep = autoscrollstep }
7646 in conf)
7647 (if conf.savebmarks then state.bookmarks else []);
7649 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7650 if basename <> path
7651 then adddoc path x anchor c bookmarks
7652 ) h;
7653 Buffer.add_string bb "</llppconfig>\n";
7654 true;
7656 if load1 f && Buffer.length bb > 0
7657 then
7659 let tmp = !confpath ^ ".tmp" in
7660 let oc = open_out_bin tmp in
7661 Buffer.output_buffer oc bb;
7662 close_out oc;
7663 Unix.rename tmp !confpath;
7664 with exn ->
7665 prerr_endline
7666 ("error while saving configuration: " ^ exntos exn)
7668 end;;
7670 let adderrmsg src msg =
7671 Buffer.add_string state.errmsgs msg;
7672 state.newerrmsgs <- true;
7673 G.postRedisplay src
7676 let adderrfmt src fmt =
7677 Format.kprintf (fun s -> adderrmsg src s) fmt;
7680 let ract cmds =
7681 let cl = splitatspace cmds in
7682 let scan s fmt f =
7683 try Scanf.sscanf s fmt f
7684 with exn ->
7685 adderrfmt "remote exec"
7686 "error processing '%S': %s\n" cmds (exntos exn)
7688 match cl with
7689 | "reload" :: [] -> reload ()
7690 | "goto" :: args :: [] ->
7691 scan args "%u %f %f"
7692 (fun pageno x y ->
7693 let cmd, _ = state.geomcmds in
7694 if emptystr cmd
7695 then gotopagexy pageno x y
7696 else
7697 let f prevf () =
7698 gotopagexy pageno x y;
7699 prevf ()
7701 state.reprf <- f state.reprf
7703 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7704 | "gotor" :: args :: [] ->
7705 scan args "%S %u"
7706 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
7707 | "gotord" :: args :: [] ->
7708 scan args "%S %S"
7709 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
7710 | "rect" :: args :: [] ->
7711 scan args "%u %u %f %f %f %f"
7712 (fun pageno color x0 y0 x1 y1 ->
7713 onpagerect pageno (fun w h ->
7714 let _,w1,h1,_ = getpagedim pageno in
7715 let sw = float w1 /. float w
7716 and sh = float h1 /. float h in
7717 let x0s = x0 *. sw
7718 and x1s = x1 *. sw
7719 and y0s = y0 *. sh
7720 and y1s = y1 *. sh in
7721 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7722 debugrect rect;
7723 state.rects <- (pageno, color, rect) :: state.rects;
7724 G.postRedisplay "rect";
7727 | "activatewin" :: [] -> Wsi.activatewin ()
7728 | "quit" :: [] -> raise Quit
7729 | _ ->
7730 adderrfmt "remote command"
7731 "error processing remote command: %S\n" cmds;
7734 let remote =
7735 let scratch = String.create 80 in
7736 let buf = Buffer.create 80 in
7737 fun fd ->
7738 let rec tempfr () =
7739 try Some (Unix.read fd scratch 0 80)
7740 with
7741 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7742 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7743 | exn -> raise exn
7745 match tempfr () with
7746 | None -> Some fd
7747 | Some n ->
7748 if n = 0
7749 then (
7750 Unix.close fd;
7751 if Buffer.length buf > 0
7752 then (
7753 let s = Buffer.contents buf in
7754 Buffer.clear buf;
7755 ract s;
7757 None
7759 else
7760 let rec eat ppos =
7761 let nlpos =
7763 let pos = String.index_from scratch ppos '\n' in
7764 if pos >= n then -1 else pos
7765 with Not_found -> -1
7767 if nlpos >= 0
7768 then (
7769 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7770 let s = Buffer.contents buf in
7771 Buffer.clear buf;
7772 ract s;
7773 eat (nlpos+1);
7775 else (
7776 Buffer.add_substring buf scratch ppos (n-ppos);
7777 Some fd
7779 in eat 0
7782 let remoteopen path =
7783 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7784 with exn ->
7785 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7786 None
7789 let () =
7790 let trimcachepath = ref "" in
7791 let rcmdpath = ref "" in
7792 let pageno = ref None in
7793 selfexec := Sys.executable_name;
7794 Arg.parse
7795 (Arg.align
7796 [("-p", Arg.String (fun s -> state.password <- s),
7797 "<password> Set password");
7799 ("-f", Arg.String
7800 (fun s ->
7801 Config.fontpath := s;
7802 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7804 "<path> Set path to the user interface font");
7806 ("-c", Arg.String
7807 (fun s ->
7808 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7809 Config.confpath := s),
7810 "<path> Set path to the configuration file");
7812 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
7813 "<page-number> Jump to page");
7815 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7816 "<path> Set path to the trim cache file");
7818 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7819 "<named-destination> Set named destination");
7821 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7822 ("-cxack", Arg.Set cxack, " Cut corners");
7824 ("-remote", Arg.String (fun s -> rcmdpath := s),
7825 "<path> Set path to the remote commands source");
7827 ("-origin", Arg.String (fun s -> state.origin <- s),
7828 "<original-path> Set original path");
7830 ("-v", Arg.Unit (fun () ->
7831 Printf.printf
7832 "%s\nconfiguration path: %s\n"
7833 (version ())
7834 Config.defconfpath
7836 exit 0), " Print version and exit");
7839 (fun s -> state.path <- s)
7840 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7842 if !wtmode
7843 then selfexec := !selfexec ^ " -wtmode";
7845 if emptystr state.path
7846 then (prerr_endline "file name missing"; exit 1);
7848 if not (Config.load ())
7849 then prerr_endline "failed to load configuration";
7850 begin match !pageno with
7851 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
7852 | None -> ()
7853 end;
7855 let wsfd, winw, winh = Wsi.init (object
7856 val mutable m_hack = false
7857 val mutable m_clicks = 0
7858 val mutable m_click_x = 0
7859 val mutable m_click_y = 0
7860 val mutable m_lastclicktime = infinity
7862 method expose = if not m_hack then G.postRedisplay "expose"
7863 method visible = G.postRedisplay "visible"
7864 method display = m_hack <- false; display ()
7865 method reshape w h =
7866 m_hack <- w < state.winw && h < state.winh;
7867 reshape w h
7868 method mouse b d x y m =
7869 if d
7870 then (
7871 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
7872 m_click_x <- x;
7873 m_click_y <- y;
7874 if b = 1
7875 then (
7876 let t = now () in
7877 if abs x - m_click_x > 10
7878 || abs y - m_click_y > 10
7879 || abs_float (t -. m_lastclicktime) > 0.3
7880 then m_clicks <- 0;
7881 m_clicks <- m_clicks + 1;
7882 m_lastclicktime <- t;
7883 if m_clicks = 1
7884 then state.uioh <- state.uioh#button b d x y m
7885 else state.uioh <- state.uioh#multiclick m_clicks x y m
7887 else (
7888 m_lastclicktime <- infinity;
7889 state.uioh <- state.uioh#button b d x y m
7892 else (
7893 state.uioh <- state.uioh#button b d x y m
7895 method motion x y =
7896 state.mpos <- (x, y);
7897 state.uioh <- state.uioh#motion x y
7898 method pmotion x y =
7899 state.mpos <- (x, y);
7900 state.uioh <- state.uioh#pmotion x y
7901 method key k m =
7902 let mascm = m land (
7903 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7904 ) in
7905 match state.keystate with
7906 | KSnone ->
7907 let km = k, mascm in
7908 begin
7909 match
7910 let modehash = state.uioh#modehash in
7911 try Hashtbl.find modehash km
7912 with Not_found ->
7913 try Hashtbl.find (findkeyhash conf "global") km
7914 with Not_found -> KMinsrt (k, m)
7915 with
7916 | KMinsrt (k, m) -> keyboard k m
7917 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7918 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7920 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7921 List.iter (fun (k, m) -> keyboard k m) insrt;
7922 state.keystate <- KSnone
7923 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7924 state.keystate <- KSinto (keys, insrt)
7925 | _ ->
7926 state.keystate <- KSnone
7928 method enter x y =
7929 state.mpos <- (x, y);
7930 state.uioh <- state.uioh#pmotion x y
7931 method leave = state.mpos <- (-1, -1)
7932 method winstate wsl = state.winstate <- wsl; m_hack <- false
7933 method quit = raise Quit
7934 end) conf.cwinw conf.cwinh (platform = Posx) in
7936 state.wsfd <- wsfd;
7938 if not (
7939 List.exists GlMisc.check_extension
7940 [ "GL_ARB_texture_rectangle"
7941 ; "GL_EXT_texture_recangle"
7942 ; "GL_NV_texture_rectangle" ]
7944 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7946 if (
7947 let r = GlMisc.get_string `renderer in
7948 let p = "Mesa DRI Intel(" in
7949 let l = String.length p in
7950 String.length r > l && String.sub r 0 l = p
7952 then (
7953 defconf.sliceheight <- 1024;
7954 defconf.texcount <- 32;
7955 defconf.usepbo <- true;
7958 let cr, sw =
7959 match Ne.pipe () with
7960 | Ne.Exn exn ->
7961 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7962 exit 1
7963 | Ne.Res rw -> rw
7964 and sr, cw =
7965 match Ne.pipe () with
7966 | Ne.Exn exn ->
7967 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7968 exit 1
7969 | Ne.Res rw -> rw
7972 cloexec cr;
7973 cloexec sw;
7974 cloexec sr;
7975 cloexec cw;
7977 setcheckers conf.checkers;
7978 redirectstderr ();
7979 if conf.redirectstderr
7980 then
7981 at_exit (fun () ->
7982 let s = Buffer.contents state.errmsgs ^
7983 (match state.errfd with
7984 | Some fd ->
7985 let s = String.create (80*24) in
7986 let n =
7988 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7989 if List.mem fd r
7990 then Unix.read fd s 0 (String.length s)
7991 else 0
7992 with _ -> 0
7994 if n = 0
7995 then ""
7996 else String.sub s 0 n
7997 | None -> ""
8000 try ignore (Unix.write state.stderr s 0 (String.length s))
8001 with exn -> print_endline (exntos exn)
8005 init (cr, cw) (
8006 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
8007 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
8008 !Config.fontpath, !trimcachepath,
8009 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
8011 List.iter GlArray.enable [`texture_coord; `vertex];
8012 state.sr <- sr;
8013 state.sw <- sw;
8014 state.text <- "Opening " ^ (mbtoutf8 state.path);
8015 reshape winw winh;
8016 opendoc state.path state.password;
8017 state.uioh <- uioh;
8018 display ();
8019 Wsi.mapwin ();
8020 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
8021 let optrfd =
8022 ref (
8023 if nonemptystr !rcmdpath
8024 then remoteopen !rcmdpath
8025 else None
8029 let rec loop deadline =
8030 let r =
8031 match state.errfd with
8032 | None -> [state.sr; state.wsfd]
8033 | Some fd -> [state.sr; state.wsfd; fd]
8035 let r =
8036 match !optrfd with
8037 | None -> r
8038 | Some fd -> fd :: r
8040 if state.redisplay
8041 then (
8042 state.redisplay <- false;
8043 display ();
8045 let timeout =
8046 let now = now () in
8047 if deadline > now
8048 then (
8049 if deadline = infinity
8050 then ~-.1.0
8051 else max 0.0 (deadline -. now)
8053 else 0.0
8055 let r, _, _ =
8056 try Unix.select r [] [] timeout
8057 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
8059 begin match r with
8060 | [] ->
8061 state.ghyll None;
8062 let newdeadline =
8063 if state.ghyll == noghyll
8064 then
8065 match state.autoscroll with
8066 | Some step when step != 0 ->
8067 let y = state.y + step in
8068 let y =
8069 if y < 0
8070 then state.maxy
8071 else if y >= state.maxy then 0 else y
8073 gotoy y;
8074 if state.mode = View
8075 then state.text <- "";
8076 deadline +. 0.01
8077 | _ -> infinity
8078 else deadline +. 0.01
8080 loop newdeadline
8082 | l ->
8083 let rec checkfds = function
8084 | [] -> ()
8085 | fd :: rest when fd = state.sr ->
8086 let cmd = readcmd state.sr in
8087 act cmd;
8088 checkfds rest
8090 | fd :: rest when fd = state.wsfd ->
8091 Wsi.readresp fd;
8092 checkfds rest
8094 | fd :: rest when Some fd = !optrfd ->
8095 begin match remote fd with
8096 | None -> optrfd := remoteopen !rcmdpath;
8097 | opt -> optrfd := opt
8098 end;
8099 checkfds rest
8101 | fd :: rest ->
8102 let s = String.create 80 in
8103 let n = tempfailureretry (Unix.read fd s 0) 80 in
8104 if conf.redirectstderr
8105 then (
8106 Buffer.add_substring state.errmsgs s 0 n;
8107 state.newerrmsgs <- true;
8108 state.redisplay <- true;
8110 else (
8111 prerr_string (String.sub s 0 n);
8112 flush stderr;
8114 checkfds rest
8116 checkfds l;
8117 let newdeadline =
8118 let deadline1 =
8119 if deadline = infinity
8120 then now () +. 0.01
8121 else deadline
8123 match state.autoscroll with
8124 | Some step when step != 0 -> deadline1
8125 | _ -> if state.ghyll == noghyll then infinity else deadline1
8127 loop newdeadline
8128 end;
8131 loop infinity;
8132 with Quit ->
8133 Config.save ();