Correct KEYBUTMASK processing
[llpp.git] / main.ml
blobfc2d602c0e926a0d0085e471e8eafd0f0f6169a2
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 | Uunexpected of string
7 | Ulaunch of string
8 | Unamed of string
9 | Uremote of (string * int)
10 and facename = string;;
12 let dolog fmt = Printf.kprintf prerr_endline fmt;;
13 let now = Unix.gettimeofday;;
15 type params = (angle * proportional * trimparams
16 * texcount * sliceheight * memsize
17 * colorspace * fontpath)
18 and pageno = int
19 and width = int
20 and height = int
21 and leftx = int
22 and opaque = string
23 and recttype = int
24 and pixmapsize = int
25 and angle = int
26 and proportional = bool
27 and trimmargins = bool
28 and interpagespace = int
29 and texcount = int
30 and sliceheight = int
31 and gen = int
32 and top = float
33 and fontpath = string
34 and memsize = int
35 and aalevel = int
36 and irect = (int * int * int * int)
37 and trimparams = (trimmargins * irect)
38 and colorspace = | Rgb | Bgr | Gray
41 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
42 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
44 type pipe = (Unix.file_descr * Unix.file_descr);;
46 external init : pipe -> params -> unit = "ml_init";;
47 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
48 external copysel : string -> opaque -> unit = "ml_copysel";;
49 external getpdimrect : int -> float array = "ml_getpdimrect";;
50 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
51 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
52 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
53 external measurestr : int -> string -> float = "ml_measure_string";;
54 external getmaxw : unit -> float = "ml_getmaxw";;
55 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
56 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
57 external platform : unit -> platform = "ml_platform";;
58 external setaalevel : int -> unit = "ml_setaalevel";;
59 external realloctexts : int -> bool = "ml_realloctexts";;
60 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
62 let platform_to_string = function
63 | Punknown -> "unknown"
64 | Plinux -> "Linux"
65 | Posx -> "OSX"
66 | Psun -> "Sun"
67 | Pfreebsd -> "FreeBSD"
68 | Pdragonflybsd -> "DragonflyBSD"
69 | Popenbsd -> "OpenBSD"
70 | Pnetbsd -> "NetBSD"
71 | Pcygwin -> "Cygwin"
74 let platform = platform ();;
76 type x = int
77 and y = int
78 and tilex = int
79 and tiley = int
80 and tileparams = (x * y * width * height * tilex * tiley)
83 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
85 type mpos = int * int
86 and mstate =
87 | Msel of (mpos * mpos)
88 | Mpan of mpos
89 | Mscrolly | Mscrollx
90 | Mzoom of (int * int)
91 | Mzoomrect of (mpos * mpos)
92 | Mnone
95 type textentry = string * string * onhist option * onkey * ondone
96 and onkey = string -> int -> te
97 and ondone = string -> unit
98 and histcancel = unit -> unit
99 and onhist = ((histcmd -> string) * histcancel)
100 and histcmd = HCnext | HCprev | HCfirst | HClast
101 and te =
102 | TEstop
103 | TEdone of string
104 | TEcont of string
105 | TEswitch of textentry
108 type 'a circbuf =
109 { store : 'a array
110 ; mutable rc : int
111 ; mutable wc : int
112 ; mutable len : int
116 let bound v minv maxv =
117 max minv (min maxv v);
120 let cbnew n v =
121 { store = Array.create n v
122 ; rc = 0
123 ; wc = 0
124 ; len = 0
128 let drawstring size x y s =
129 Gl.enable `blend;
130 Gl.enable `texture_2d;
131 ignore (drawstr size x y s);
132 Gl.disable `blend;
133 Gl.disable `texture_2d;
136 let drawstring1 size x y s =
137 drawstr size x y s;
140 let drawstring2 size x y fmt =
141 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
144 let cbcap b = Array.length b.store;;
146 let cbput b v =
147 let cap = cbcap b in
148 b.store.(b.wc) <- v;
149 b.wc <- (b.wc + 1) mod cap;
150 b.rc <- b.wc;
151 b.len <- min (b.len + 1) cap;
154 let cbempty b = b.len = 0;;
156 let cbgetg b circular dir =
157 if cbempty b
158 then b.store.(0)
159 else
160 let rc = b.rc + dir in
161 let rc =
162 if circular
163 then (
164 if rc = -1
165 then b.len-1
166 else (
167 if rc = b.len
168 then 0
169 else rc
172 else max 0 (min rc (b.len-1))
174 b.rc <- rc;
175 b.store.(rc);
178 let cbget b = cbgetg b false;;
179 let cbgetc b = cbgetg b true;;
181 type page =
182 { pageno : int
183 ; pagedimno : int
184 ; pagew : int
185 ; pageh : int
186 ; pagex : int
187 ; pagey : int
188 ; pagevw : int
189 ; pagevh : int
190 ; pagedispx : int
191 ; pagedispy : int
195 let debugl l =
196 dolog "l %d dim=%d {" l.pageno l.pagedimno;
197 dolog " WxH %dx%d" l.pagew l.pageh;
198 dolog " vWxH %dx%d" l.pagevw l.pagevh;
199 dolog " pagex,y %d,%d" l.pagex l.pagey;
200 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
201 dolog "}";
204 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
205 dolog "rect {";
206 dolog " x0,y0=(% f, % f)" x0 y0;
207 dolog " x1,y1=(% f, % f)" x1 y1;
208 dolog " x2,y2=(% f, % f)" x2 y2;
209 dolog " x3,y3=(% f, % f)" x3 y3;
210 dolog "}";
213 type columns =
214 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
215 and multicol = columncount * covercount * covercount
216 and pdimno = int
217 and columncount = int
218 and covercount = int;;
220 type conf =
221 { mutable scrollbw : int
222 ; mutable scrollh : int
223 ; mutable icase : bool
224 ; mutable preload : bool
225 ; mutable pagebias : int
226 ; mutable verbose : bool
227 ; mutable debug : bool
228 ; mutable scrollstep : int
229 ; mutable maxhfit : bool
230 ; mutable crophack : bool
231 ; mutable autoscrollstep : int
232 ; mutable maxwait : float option
233 ; mutable hlinks : bool
234 ; mutable underinfo : bool
235 ; mutable interpagespace : interpagespace
236 ; mutable zoom : float
237 ; mutable presentation : bool
238 ; mutable angle : angle
239 ; mutable winw : int
240 ; mutable winh : int
241 ; mutable savebmarks : bool
242 ; mutable proportional : proportional
243 ; mutable trimmargins : trimmargins
244 ; mutable trimfuzz : irect
245 ; mutable memlimit : memsize
246 ; mutable texcount : texcount
247 ; mutable sliceheight : sliceheight
248 ; mutable thumbw : width
249 ; mutable jumpback : bool
250 ; mutable bgcolor : float * float * float
251 ; mutable bedefault : bool
252 ; mutable scrollbarinpm : bool
253 ; mutable tilew : int
254 ; mutable tileh : int
255 ; mutable mustoresize : memsize
256 ; mutable checkers : bool
257 ; mutable aalevel : int
258 ; mutable urilauncher : string
259 ; mutable pathlauncher : string
260 ; mutable colorspace : colorspace
261 ; mutable invert : bool
262 ; mutable colorscale : float
263 ; mutable redirectstderr : bool
264 ; mutable ghyllscroll : (int * int * int) option
265 ; mutable columns : columns option
266 ; mutable beyecolumns : columncount option
267 ; mutable selcmd : string
268 ; mutable updatecurs : bool
272 type anchor = pageno * top;;
274 type outline = string * int * anchor;;
276 type rect = float * float * float * float * float * float * float * float;;
278 type tile = opaque * pixmapsize * elapsed
279 and elapsed = float;;
280 type pagemapkey = pageno * gen;;
281 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
282 and row = int
283 and col = int;;
285 let emptyanchor = (0, 0.0);;
287 type infochange = | Memused | Docinfo | Pdim;;
289 class type uioh = object
290 method display : unit
291 method key : int -> int -> uioh
292 method button : int -> bool -> int -> int -> int -> uioh
293 method motion : int -> int -> uioh
294 method pmotion : int -> int -> uioh
295 method infochanged : infochange -> unit
296 method scrollpw : (int * float * float)
297 method scrollph : (int * float * float)
298 end;;
300 type mode =
301 | Birdseye of (conf * leftx * pageno * pageno * anchor)
302 | Textentry of (textentry * onleave)
303 | View
304 and onleave = leavetextentrystatus -> unit
305 and leavetextentrystatus = | Cancel | Confirm
306 and helpitem = string * int * action
307 and action =
308 | Noaction
309 | Action of (uioh -> uioh)
312 let isbirdseye = function Birdseye _ -> true | _ -> false;;
313 let istextentry = function Textentry _ -> true | _ -> false;;
315 type currently =
316 | Idle
317 | Loading of (page * gen)
318 | Tiling of (
319 page * opaque * colorspace * angle * gen * col * row * width * height
321 | Outlining of outline list
324 let nouioh : uioh = object (self)
325 method display = ()
326 method key _ _ = self
327 method button _ _ _ _ _ = self
328 method motion _ _ = self
329 method pmotion _ _ = self
330 method infochanged _ = ()
331 method scrollpw = (0, nan, nan)
332 method scrollph = (0, nan, nan)
333 end;;
335 type state =
336 { mutable sr : Unix.file_descr
337 ; mutable sw : Unix.file_descr
338 ; mutable wsfd : Unix.file_descr
339 ; mutable errfd : Unix.file_descr option
340 ; mutable stderr : Unix.file_descr
341 ; mutable errmsgs : Buffer.t
342 ; mutable newerrmsgs : bool
343 ; mutable w : int
344 ; mutable x : int
345 ; mutable y : int
346 ; mutable scrollw : int
347 ; mutable hscrollh : int
348 ; mutable anchor : anchor
349 ; mutable ranchors : (string * string * anchor) list
350 ; mutable maxy : int
351 ; mutable layout : page list
352 ; pagemap : (pagemapkey, opaque) Hashtbl.t
353 ; tilemap : (tilemapkey, tile) Hashtbl.t
354 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
355 ; mutable pdims : (pageno * width * height * leftx) list
356 ; mutable pagecount : int
357 ; mutable currently : currently
358 ; mutable mstate : mstate
359 ; mutable searchpattern : string
360 ; mutable rects : (pageno * recttype * rect) list
361 ; mutable rects1 : (pageno * recttype * rect) list
362 ; mutable text : string
363 ; mutable fullscreen : (width * height) option
364 ; mutable mode : mode
365 ; mutable uioh : uioh
366 ; mutable outlines : outline array
367 ; mutable bookmarks : outline list
368 ; mutable path : string
369 ; mutable password : string
370 ; mutable invalidated : int
371 ; mutable memused : memsize
372 ; mutable gen : gen
373 ; mutable throttle : (page list * int * float) option
374 ; mutable autoscroll : int option
375 ; mutable ghyll : (int option -> unit)
376 ; mutable help : helpitem array
377 ; mutable docinfo : (int * string) list
378 ; mutable texid : GlTex.texture_id option
379 ; hists : hists
380 ; mutable prevzoom : float
381 ; mutable progress : float
382 ; mutable redisplay : bool
383 ; mutable mpos : mpos
385 and hists =
386 { pat : string circbuf
387 ; pag : string circbuf
388 ; nav : anchor circbuf
389 ; sel : string circbuf
393 let defconf =
394 { scrollbw = 7
395 ; scrollh = 12
396 ; icase = true
397 ; preload = true
398 ; pagebias = 0
399 ; verbose = false
400 ; debug = false
401 ; scrollstep = 24
402 ; maxhfit = true
403 ; crophack = false
404 ; autoscrollstep = 2
405 ; maxwait = None
406 ; hlinks = false
407 ; underinfo = false
408 ; interpagespace = 2
409 ; zoom = 1.0
410 ; presentation = false
411 ; angle = 0
412 ; winw = 900
413 ; winh = 900
414 ; savebmarks = true
415 ; proportional = true
416 ; trimmargins = false
417 ; trimfuzz = (0,0,0,0)
418 ; memlimit = 32 lsl 20
419 ; texcount = 256
420 ; sliceheight = 24
421 ; thumbw = 76
422 ; jumpback = true
423 ; bgcolor = (0.5, 0.5, 0.5)
424 ; bedefault = false
425 ; scrollbarinpm = true
426 ; tilew = 2048
427 ; tileh = 2048
428 ; mustoresize = 128 lsl 20
429 ; checkers = true
430 ; aalevel = 8
431 ; urilauncher =
432 (match platform with
433 | Plinux | Pfreebsd | Pdragonflybsd
434 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
435 | Posx -> "open \"%s\""
436 | Pcygwin -> "cygstart %s"
437 | Punknown -> "echo %s")
438 ; pathlauncher = "lp \"%s\""
439 ; selcmd =
440 (match platform with
441 | Plinux | Pfreebsd | Pdragonflybsd
442 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
443 | Posx -> "pbcopy"
444 | Pcygwin -> "wsel"
445 | Punknown -> "cat")
446 ; colorspace = Rgb
447 ; invert = false
448 ; colorscale = 1.0
449 ; redirectstderr = false
450 ; ghyllscroll = None
451 ; columns = None
452 ; beyecolumns = None
453 ; updatecurs = false
457 let conf = { defconf with angle = defconf.angle };;
459 type fontstate =
460 { mutable fontsize : int
461 ; mutable wwidth : float
462 ; mutable maxrows : int
466 let fstate =
467 { fontsize = 14
468 ; wwidth = nan
469 ; maxrows = -1
473 let setfontsize n =
474 fstate.fontsize <- n;
475 fstate.wwidth <- measurestr fstate.fontsize "w";
476 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
479 let geturl s =
480 let colonpos = try String.index s ':' with Not_found -> -1 in
481 let len = String.length s in
482 if colonpos >= 0 && colonpos + 3 < len
483 then (
484 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
485 then
486 let schemestartpos =
487 try String.rindex_from s colonpos ' '
488 with Not_found -> -1
490 let scheme =
491 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
493 match scheme with
494 | "http" | "ftp" | "mailto" ->
495 let epos =
496 try String.index_from s colonpos ' '
497 with Not_found -> len
499 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
500 | _ -> ""
501 else ""
503 else ""
506 let popen =
507 let shell, farg = "/bin/sh", "-c" in
508 fun s ->
509 let args = [|shell; farg; s|] in
510 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
513 let gotouri uri =
514 if String.length conf.urilauncher = 0
515 then print_endline uri
516 else (
517 let url = geturl uri in
518 if String.length url = 0
519 then print_endline uri
520 else
521 let re = Str.regexp "%s" in
522 let command = Str.global_replace re url conf.urilauncher in
523 try popen command
524 with exn ->
525 Printf.eprintf
526 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
527 flush stderr;
531 let version () =
532 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
533 (platform_to_string platform) Sys.word_size Sys.ocaml_version
536 let makehelp () =
537 let strings = version () :: "" :: Help.keys in
538 Array.of_list (
539 List.map (fun s ->
540 let url = geturl s in
541 if String.length url > 0
542 then (s, 0, Action (fun u -> gotouri url; u))
543 else (s, 0, Noaction)
544 ) strings);
547 let noghyll _ = ();;
549 let state =
550 { sr = Unix.stdin
551 ; sw = Unix.stdin
552 ; wsfd = Unix.stdin
553 ; errfd = None
554 ; stderr = Unix.stderr
555 ; errmsgs = Buffer.create 0
556 ; newerrmsgs = false
557 ; x = 0
558 ; y = 0
559 ; w = 0
560 ; scrollw = 0
561 ; hscrollh = 0
562 ; anchor = emptyanchor
563 ; ranchors = []
564 ; layout = []
565 ; maxy = max_int
566 ; tilelru = Queue.create ()
567 ; pagemap = Hashtbl.create 10
568 ; tilemap = Hashtbl.create 10
569 ; pdims = []
570 ; pagecount = 0
571 ; currently = Idle
572 ; mstate = Mnone
573 ; rects = []
574 ; rects1 = []
575 ; text = ""
576 ; mode = View
577 ; fullscreen = None
578 ; searchpattern = ""
579 ; outlines = [||]
580 ; bookmarks = []
581 ; path = ""
582 ; password = ""
583 ; invalidated = 0
584 ; hists =
585 { nav = cbnew 10 (0, 0.0)
586 ; pat = cbnew 10 ""
587 ; pag = cbnew 10 ""
588 ; sel = cbnew 10 ""
590 ; memused = 0
591 ; gen = 0
592 ; throttle = None
593 ; autoscroll = None
594 ; ghyll = noghyll
595 ; help = makehelp ()
596 ; docinfo = []
597 ; texid = None
598 ; prevzoom = 1.0
599 ; progress = -1.0
600 ; uioh = nouioh
601 ; redisplay = false
602 ; mpos = (-1, -1)
606 let vlog fmt =
607 if conf.verbose
608 then
609 Printf.kprintf prerr_endline fmt
610 else
611 Printf.kprintf ignore fmt
614 let launchpath () =
615 if String.length conf.pathlauncher = 0
616 then print_endline state.path
617 else (
618 let re = Str.regexp "%s" in
619 let command = Str.global_replace re state.path conf.pathlauncher in
620 try popen command
621 with exn ->
622 Printf.eprintf
623 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
624 flush stderr;
628 let redirectstderr () =
629 if conf.redirectstderr
630 then
631 let rfd, wfd = Unix.pipe () in
632 state.stderr <- Unix.dup Unix.stderr;
633 state.errfd <- Some rfd;
634 Unix.dup2 wfd Unix.stderr;
635 else (
636 state.newerrmsgs <- false;
637 begin match state.errfd with
638 | Some fd ->
639 Unix.close fd;
640 Unix.dup2 state.stderr Unix.stderr;
641 state.errfd <- None;
642 | None -> ()
643 end;
644 prerr_string (Buffer.contents state.errmsgs);
645 flush stderr;
646 Buffer.clear state.errmsgs;
650 module G =
651 struct
652 let postRedisplay who =
653 if conf.verbose
654 then prerr_endline ("redisplay for " ^ who);
655 state.redisplay <- true;
657 end;;
659 let getopaque pageno =
660 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
661 with Not_found -> None
664 let putopaque pageno opaque =
665 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
668 let pagetranslatepoint l x y =
669 let dy = y - l.pagedispy in
670 let y = dy + l.pagey in
671 let dx = x - l.pagedispx in
672 let x = dx + l.pagex in
673 (x, y);
676 let getunder x y =
677 let rec f = function
678 | l :: rest ->
679 begin match getopaque l.pageno with
680 | Some opaque ->
681 let x0 = l.pagedispx in
682 let x1 = x0 + l.pagevw in
683 let y0 = l.pagedispy in
684 let y1 = y0 + l.pagevh in
685 if y >= y0 && y <= y1 && x >= x0 && x <= x1
686 then
687 let px, py = pagetranslatepoint l x y in
688 match whatsunder opaque px py with
689 | Unone -> f rest
690 | under -> under
691 else f rest
692 | _ ->
693 f rest
695 | [] -> Unone
697 f state.layout
700 let showtext c s =
701 state.text <- Printf.sprintf "%c%s" c s;
702 G.postRedisplay "showtext";
705 let updateunder x y =
706 match getunder x y with
707 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
708 | Ulinkuri uri ->
709 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
710 Wsi.setcursor Wsi.CURSOR_INFO
711 | Ulinkgoto (page, _) ->
712 if conf.underinfo
713 then showtext 'p' ("age: " ^ string_of_int (page+1));
714 Wsi.setcursor Wsi.CURSOR_INFO
715 | Utext s ->
716 if conf.underinfo then showtext 'f' ("ont: " ^ s);
717 Wsi.setcursor Wsi.CURSOR_TEXT
718 | Uunexpected s ->
719 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
720 Wsi.setcursor Wsi.CURSOR_INHERIT
721 | Ulaunch s ->
722 if conf.underinfo then showtext 'l' ("launch: " ^ s);
723 Wsi.setcursor Wsi.CURSOR_INHERIT
724 | Unamed s ->
725 if conf.underinfo then showtext 'n' ("named: " ^ s);
726 Wsi.setcursor Wsi.CURSOR_INHERIT
727 | Uremote (filename, pageno) ->
728 if conf.underinfo then showtext 'r'
729 (Printf.sprintf "emote: %s (%d)" filename pageno);
730 Wsi.setcursor Wsi.CURSOR_INFO
733 let addchar s c =
734 let b = Buffer.create (String.length s + 1) in
735 Buffer.add_string b s;
736 Buffer.add_char b c;
737 Buffer.contents b;
740 let colorspace_of_string s =
741 match String.lowercase s with
742 | "rgb" -> Rgb
743 | "bgr" -> Bgr
744 | "gray" -> Gray
745 | _ -> failwith "invalid colorspace"
748 let int_of_colorspace = function
749 | Rgb -> 0
750 | Bgr -> 1
751 | Gray -> 2
754 let colorspace_of_int = function
755 | 0 -> Rgb
756 | 1 -> Bgr
757 | 2 -> Gray
758 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
761 let colorspace_to_string = function
762 | Rgb -> "rgb"
763 | Bgr -> "bgr"
764 | Gray -> "gray"
767 let intentry_with_suffix text key =
768 let c =
769 if key >= 32 && key < 127
770 then Char.chr key
771 else '\000'
773 match Char.lowercase c with
774 | '0' .. '9' ->
775 let text = addchar text c in
776 TEcont text
778 | 'k' | 'm' | 'g' ->
779 let text = addchar text c in
780 TEcont text
782 | _ ->
783 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
784 TEcont text
787 let columns_to_string (n, a, b) =
788 if a = 0 && b = 0
789 then Printf.sprintf "%d" n
790 else Printf.sprintf "%d,%d,%d" n a b;
793 let columns_of_string s =
795 (int_of_string s, 0, 0)
796 with _ ->
797 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
800 let writecmd fd s =
801 let len = String.length s in
802 let n = 4 + len in
803 let b = Buffer.create n in
804 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
805 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
806 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
807 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
808 Buffer.add_string b s;
809 let s' = Buffer.contents b in
810 let n' = Unix.write fd s' 0 n in
811 if n' != n then failwith "write failed";
814 let readcmd fd =
815 let s = "xxxx" in
816 let n = Unix.read fd s 0 4 in
817 if n != 4 then failwith "incomplete read(len)";
818 let len = 0
819 lor (Char.code s.[0] lsl 24)
820 lor (Char.code s.[1] lsl 16)
821 lor (Char.code s.[2] lsl 8)
822 lor (Char.code s.[3] lsl 0)
824 let s = String.create len in
825 let n = Unix.read fd s 0 len in
826 if n != len then failwith "incomplete read(data)";
830 let makecmd s l =
831 let b = Buffer.create 10 in
832 Buffer.add_string b s;
833 let rec combine = function
834 | [] -> b
835 | x :: xs ->
836 Buffer.add_char b ' ';
837 let s =
838 match x with
839 | `b b -> if b then "1" else "0"
840 | `s s -> s
841 | `i i -> string_of_int i
842 | `f f -> string_of_float f
843 | `I f -> string_of_int (truncate f)
845 Buffer.add_string b s;
846 combine xs;
848 combine l;
851 let wcmd s l =
852 let cmd = Buffer.contents (makecmd s l) in
853 writecmd state.sw cmd;
856 let calcips h =
857 if conf.presentation
858 then
859 let d = conf.winh - h in
860 max 0 ((d + 1) / 2)
861 else
862 conf.interpagespace
865 let calcheight () =
866 let rec f pn ph pi fh l =
867 match l with
868 | (n, _, h, _) :: rest ->
869 let ips = calcips h in
870 let fh =
871 if conf.presentation
872 then fh+ips
873 else (
874 if isbirdseye state.mode && pn = 0
875 then fh + ips
876 else fh
879 let fh = fh + ((n - pn) * (ph + pi)) in
880 f n h ips fh rest;
882 | [] ->
883 let inc =
884 if conf.presentation || (isbirdseye state.mode && pn = 0)
885 then 0
886 else -pi
888 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
889 max 0 fh
891 let fh = f 0 0 0 0 state.pdims in
895 let calcheight () =
896 match conf.columns with
897 | None -> calcheight ()
898 | Some (_, b) ->
899 if Array.length b > 0
900 then
901 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
902 y + h
903 else 0
906 let getpageyh pageno =
907 let rec f pn ph pi y l =
908 match l with
909 | (n, _, h, _) :: rest ->
910 let ips = calcips h in
911 if n >= pageno
912 then
913 let h = if n = pageno then h else ph in
914 if conf.presentation && n = pageno
915 then
916 y + (pageno - pn) * (ph + pi) + pi, h
917 else
918 y + (pageno - pn) * (ph + pi), h
919 else
920 let y = y + (if conf.presentation then pi else 0) in
921 let y = y + (n - pn) * (ph + pi) in
922 f n h ips y rest
924 | [] ->
925 y + (pageno - pn) * (ph + pi), ph
927 f 0 0 0 0 state.pdims
930 let getpageyh pageno =
931 match conf.columns with
932 | None -> getpageyh pageno
933 | Some (_, b) ->
934 let (_, _, y, (_, _, h, _)) = b.(pageno) in
935 y, h
938 let getpagedim pageno =
939 let rec f ppdim l =
940 match l with
941 | (n, _, _, _) as pdim :: rest ->
942 if n >= pageno
943 then (if n = pageno then pdim else ppdim)
944 else f pdim rest
946 | [] -> ppdim
948 f (-1, -1, -1, -1) state.pdims
951 let getpagey pageno = fst (getpageyh pageno);;
953 let layout1 y sh =
954 let sh = sh - state.hscrollh in
955 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
956 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
957 match pdims with
958 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
959 let ips = calcips h in
960 let yinc =
961 if conf.presentation || (isbirdseye state.mode && pageno = 0)
962 then ips
963 else 0
965 (w, h, ips, xoff), rest, pdimno + 1, yinc
966 | _ ->
967 prev, pdims, pdimno, 0
969 let dy = dy + yinc in
970 let py = py + yinc in
971 if pageno = state.pagecount || dy >= sh
972 then
973 accu
974 else
975 let vy = y + dy in
976 if py + h <= vy - yinc
977 then
978 let py = py + h + ips in
979 let dy = max 0 (py - y) in
980 f ~pageno:(pageno+1)
981 ~pdimno
982 ~prev:curr
985 ~pdims:rest
986 ~accu
987 else
988 let pagey = vy - py in
989 let pagevh = h - pagey in
990 let pagevh = min (sh - dy) pagevh in
991 let off = if yinc > 0 then py - vy else 0 in
992 let py = py + h + ips in
993 let pagex, dx =
994 let xoff = xoff +
995 if state.w < conf.winw - state.scrollw
996 then (conf.winw - state.scrollw - state.w) / 2
997 else 0
999 let dispx = xoff + state.x in
1000 if dispx < 0
1001 then (-dispx, 0)
1002 else (0, dispx)
1004 let pagevw =
1005 let lw = w - pagex in
1006 min lw (conf.winw - state.scrollw)
1008 let e =
1009 { pageno = pageno
1010 ; pagedimno = pdimno
1011 ; pagew = w
1012 ; pageh = h
1013 ; pagex = pagex
1014 ; pagey = pagey + off
1015 ; pagevw = pagevw
1016 ; pagevh = pagevh - off
1017 ; pagedispx = dx
1018 ; pagedispy = dy + off
1021 let accu = e :: accu in
1022 f ~pageno:(pageno+1)
1023 ~pdimno
1024 ~prev:curr
1026 ~dy:(dy+pagevh+ips)
1027 ~pdims:rest
1028 ~accu
1030 if state.invalidated = 0
1031 then (
1032 let accu =
1034 ~pageno:0
1035 ~pdimno:~-1
1036 ~prev:(0,0,0,0)
1037 ~py:0
1038 ~dy:0
1039 ~pdims:state.pdims
1040 ~accu:[]
1042 List.rev accu
1044 else
1048 let layoutN ((columns, coverA, coverB), b) y sh =
1049 let sh = sh - state.hscrollh in
1050 let rec fold accu n =
1051 if n = Array.length b
1052 then accu
1053 else
1054 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1055 if (vy - y) > sh &&
1056 (n = coverA - 1
1057 || n = state.pagecount - coverB
1058 || (n - coverA) mod columns = columns - 1)
1059 then accu
1060 else
1061 let accu =
1062 if vy + h > y
1063 then
1064 let pagey = max 0 (y - vy) in
1065 let pagedispy = if pagey > 0 then 0 else vy - y in
1066 let pagedispx, pagex, pagevw =
1067 let pdx =
1068 if n = coverA - 1 || n = state.pagecount - coverB
1069 then state.x + (conf.winw - state.scrollw - w) / 2
1070 else dx + xoff + state.x
1072 if pdx < 0
1073 then 0, -pdx, w + pdx
1074 else pdx, 0, min (conf.winw - state.scrollw) w
1076 let pagevh = min (h - pagey) (sh - pagedispy) in
1077 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1078 then
1079 let e =
1080 { pageno = n
1081 ; pagedimno = pdimno
1082 ; pagew = w
1083 ; pageh = h
1084 ; pagex = pagex
1085 ; pagey = pagey
1086 ; pagevw = pagevw
1087 ; pagevh = pagevh
1088 ; pagedispx = pagedispx
1089 ; pagedispy = pagedispy
1092 e :: accu
1093 else
1094 accu
1095 else
1096 accu
1098 fold accu (n+1)
1100 if state.invalidated = 0
1101 then List.rev (fold [] 0)
1102 else []
1105 let layout y sh =
1106 match conf.columns with
1107 | None -> layout1 y sh
1108 | Some c -> layoutN c y sh
1111 let clamp incr =
1112 let y = state.y + incr in
1113 let y = max 0 y in
1114 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1118 let itertiles l f =
1119 let tilex = l.pagex mod conf.tilew in
1120 let tiley = l.pagey mod conf.tileh in
1122 let col = l.pagex / conf.tilew in
1123 let row = l.pagey / conf.tileh in
1125 let vw =
1126 let a = l.pagew - l.pagex in
1127 let b = conf.winw - state.scrollw in
1128 min a b
1129 and vh = l.pagevh in
1131 let rec rowloop row y0 dispy h =
1132 if h = 0
1133 then ()
1134 else (
1135 let dh = conf.tileh - y0 in
1136 let dh = min h dh in
1137 let rec colloop col x0 dispx w =
1138 if w = 0
1139 then ()
1140 else (
1141 let dw = conf.tilew - x0 in
1142 let dw = min w dw in
1144 f col row dispx dispy x0 y0 dw dh;
1145 colloop (col+1) 0 (dispx+dw) (w-dw)
1148 colloop col tilex l.pagedispx vw;
1149 rowloop (row+1) 0 (dispy+dh) (h-dh)
1152 if vw > 0 && vh > 0
1153 then rowloop row tiley l.pagedispy vh;
1156 let gettileopaque l col row =
1157 let key =
1158 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1160 try Some (Hashtbl.find state.tilemap key)
1161 with Not_found -> None
1164 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1165 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1166 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1169 let drawtiles l color =
1170 GlDraw.color color;
1171 let f col row x y tilex tiley w h =
1172 match gettileopaque l col row with
1173 | Some (opaque, _, t) ->
1174 let params = x, y, w, h, tilex, tiley in
1175 if conf.invert
1176 then (
1177 Gl.enable `blend;
1178 GlFunc.blend_func `zero `one_minus_src_color;
1180 drawtile params opaque;
1181 if conf.invert
1182 then Gl.disable `blend;
1183 if conf.debug
1184 then (
1185 let s = Printf.sprintf
1186 "%d[%d,%d] %f sec"
1187 l.pageno col row t
1189 let w = measurestr fstate.fontsize s in
1190 GlMisc.push_attrib [`current];
1191 GlDraw.color (0.0, 0.0, 0.0);
1192 GlDraw.rect
1193 (float (x-2), float (y-2))
1194 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1195 GlDraw.color (1.0, 1.0, 1.0);
1196 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1197 GlMisc.pop_attrib ();
1200 | _ ->
1201 let w =
1202 let lw = conf.winw - state.scrollw - x in
1203 min lw w
1204 and h =
1205 let lh = conf.winh - y in
1206 min lh h
1208 Gl.enable `texture_2d;
1209 begin match state.texid with
1210 | Some id ->
1211 GlTex.bind_texture `texture_2d id;
1212 let x0 = float x
1213 and y0 = float y
1214 and x1 = float (x+w)
1215 and y1 = float (y+h) in
1217 let tw = float w /. 64.0
1218 and th = float h /. 64.0 in
1219 let tx0 = float tilex /. 64.0
1220 and ty0 = float tiley /. 64.0 in
1221 let tx1 = tx0 +. tw
1222 and ty1 = ty0 +. th in
1223 GlDraw.begins `quads;
1224 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1225 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1226 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1227 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1228 GlDraw.ends ();
1230 Gl.disable `texture_2d;
1231 | None ->
1232 GlDraw.color (1.0, 1.0, 1.0);
1233 GlDraw.rect
1234 (float x, float y)
1235 (float (x+w), float (y+h));
1236 end;
1237 if w > 128 && h > fstate.fontsize + 10
1238 then (
1239 GlDraw.color (0.0, 0.0, 0.0);
1240 let c, r =
1241 if conf.verbose
1242 then (col*conf.tilew, row*conf.tileh)
1243 else col, row
1245 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1247 GlDraw.color color;
1249 itertiles l f
1252 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1254 let tilevisible1 l x y =
1255 let ax0 = l.pagex
1256 and ax1 = l.pagex + l.pagevw
1257 and ay0 = l.pagey
1258 and ay1 = l.pagey + l.pagevh in
1260 let bx0 = x
1261 and by0 = y in
1262 let bx1 = min (bx0 + conf.tilew) l.pagew
1263 and by1 = min (by0 + conf.tileh) l.pageh in
1265 let rx0 = max ax0 bx0
1266 and ry0 = max ay0 by0
1267 and rx1 = min ax1 bx1
1268 and ry1 = min ay1 by1 in
1270 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1271 nonemptyintersection
1274 let tilevisible layout n x y =
1275 let rec findpageinlayout = function
1276 | l :: _ when l.pageno = n -> tilevisible1 l x y
1277 | _ :: rest -> findpageinlayout rest
1278 | [] -> false
1280 findpageinlayout layout
1283 let tileready l x y =
1284 tilevisible1 l x y &&
1285 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1288 let tilepage n p layout =
1289 let rec loop = function
1290 | l :: rest ->
1291 if l.pageno = n
1292 then
1293 let f col row _ _ _ _ _ _ =
1294 if state.currently = Idle
1295 then
1296 match gettileopaque l col row with
1297 | Some _ -> ()
1298 | None ->
1299 let x = col*conf.tilew
1300 and y = row*conf.tileh in
1301 let w =
1302 let w = l.pagew - x in
1303 min w conf.tilew
1305 let h =
1306 let h = l.pageh - y in
1307 min h conf.tileh
1309 wcmd "tile"
1310 [`s p
1311 ;`i x
1312 ;`i y
1313 ;`i w
1314 ;`i h
1316 state.currently <-
1317 Tiling (
1318 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1319 conf.tilew, conf.tileh
1322 itertiles l f;
1323 else
1324 loop rest
1326 | [] -> ()
1328 if state.invalidated = 0 then loop layout;
1331 let preloadlayout visiblepages =
1332 let presentation = conf.presentation in
1333 let interpagespace = conf.interpagespace in
1334 let maxy = state.maxy in
1335 conf.presentation <- false;
1336 conf.interpagespace <- 0;
1337 state.maxy <- calcheight ();
1338 let y =
1339 match visiblepages with
1340 | [] -> 0
1341 | l :: _ -> getpagey l.pageno + l.pagey
1343 let y = if y < conf.winh then 0 else y - conf.winh in
1344 let h = state.y - y + conf.winh*3 in
1345 let pages = layout y h in
1346 conf.presentation <- presentation;
1347 conf.interpagespace <- interpagespace;
1348 state.maxy <- maxy;
1349 pages;
1352 let load pages =
1353 let rec loop pages =
1354 if state.currently != Idle
1355 then ()
1356 else
1357 match pages with
1358 | l :: rest ->
1359 begin match getopaque l.pageno with
1360 | None ->
1361 wcmd "page" [`i l.pageno; `i l.pagedimno];
1362 state.currently <- Loading (l, state.gen);
1363 | Some opaque ->
1364 tilepage l.pageno opaque pages;
1365 loop rest
1366 end;
1367 | _ -> ()
1369 if state.invalidated = 0 then loop pages
1372 let preload pages =
1373 load pages;
1374 if conf.preload && state.currently = Idle
1375 then load (preloadlayout pages);
1378 let layoutready layout =
1379 let rec fold all ls =
1380 all && match ls with
1381 | l :: rest ->
1382 let seen = ref false in
1383 let allvisible = ref true in
1384 let foo col row _ _ _ _ _ _ =
1385 seen := true;
1386 allvisible := !allvisible &&
1387 begin match gettileopaque l col row with
1388 | Some _ -> true
1389 | None -> false
1392 itertiles l foo;
1393 fold (!seen && !allvisible) rest
1394 | [] -> true
1396 let alltilesvisible = fold true layout in
1397 alltilesvisible;
1400 let gotoy y =
1401 let y = bound y 0 state.maxy in
1402 let y, layout, proceed =
1403 match conf.maxwait with
1404 | Some time when state.ghyll == noghyll ->
1405 begin match state.throttle with
1406 | None ->
1407 let layout = layout y conf.winh in
1408 let ready = layoutready layout in
1409 if not ready
1410 then (
1411 load layout;
1412 state.throttle <- Some (layout, y, now ());
1414 else G.postRedisplay "gotoy showall (None)";
1415 y, layout, ready
1416 | Some (_, _, started) ->
1417 let dt = now () -. started in
1418 if dt > time
1419 then (
1420 state.throttle <- None;
1421 let layout = layout y conf.winh in
1422 load layout;
1423 G.postRedisplay "maxwait";
1424 y, layout, true
1426 else -1, [], false
1429 | _ ->
1430 let layout = layout y conf.winh in
1431 if true || layoutready layout
1432 then G.postRedisplay "gotoy ready";
1433 y, layout, true
1435 if proceed
1436 then (
1437 state.y <- y;
1438 state.layout <- layout;
1439 begin match state.mode with
1440 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1441 if not (pagevisible layout pageno)
1442 then (
1443 match state.layout with
1444 | [] -> ()
1445 | l :: _ ->
1446 state.mode <- Birdseye (
1447 conf, leftx, l.pageno, hooverpageno, anchor
1450 | _ -> ()
1451 end;
1452 preload layout;
1454 state.ghyll <- noghyll;
1457 let conttiling pageno opaque =
1458 tilepage pageno opaque
1459 (if conf.preload then preloadlayout state.layout else state.layout)
1462 let gotoy_and_clear_text y =
1463 gotoy y;
1464 if not conf.verbose then state.text <- "";
1467 let getanchor () =
1468 match state.layout with
1469 | [] -> emptyanchor
1470 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1473 let getanchory (n, top) =
1474 let y, h = getpageyh n in
1475 y + (truncate (top *. float h));
1478 let gotoanchor anchor =
1479 gotoy (getanchory anchor);
1482 let addnav () =
1483 cbput state.hists.nav (getanchor ());
1486 let getnav dir =
1487 let anchor = cbgetc state.hists.nav dir in
1488 getanchory anchor;
1491 let gotoghyll y =
1492 let rec scroll f n a b =
1493 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1494 let snake f a b =
1495 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1496 if f < a
1497 then s (float f /. float a)
1498 else (
1499 if f > b
1500 then 1.0 -. s ((float (f-b) /. float (n-b)))
1501 else 1.0
1504 snake f a b
1505 and summa f n a b =
1506 (* courtesy:
1507 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1508 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1509 let iv1 = iv f in
1510 let ins = float a *. iv1
1511 and outs = float (n-b) *. iv1 in
1512 let ones = b - a in
1513 ins +. outs +. float ones
1515 let rec set (_N, _A, _B) y sy =
1516 let sum = summa 1.0 _N _A _B in
1517 let dy = float (y - sy) in
1518 state.ghyll <- (
1519 let rec gf n y1 o =
1520 if n >= _N
1521 then state.ghyll <- noghyll
1522 else
1523 let go n =
1524 let s = scroll n _N _A _B in
1525 let y1 = y1 +. ((s *. dy) /. sum) in
1526 gotoy_and_clear_text (truncate y1);
1527 state.ghyll <- gf (n+1) y1;
1529 match o with
1530 | None -> go n
1531 | Some y' -> set (_N/2, 0, 0) y' state.y
1533 gf 0 (float state.y)
1536 match conf.ghyllscroll with
1537 | None ->
1538 gotoy_and_clear_text y
1539 | Some nab ->
1540 if state.ghyll == noghyll
1541 then set nab y state.y
1542 else state.ghyll (Some y)
1545 let gotopage n top =
1546 let y, h = getpageyh n in
1547 let y = y + (truncate (top *. float h)) in
1548 gotoghyll y
1551 let gotopage1 n top =
1552 let y = getpagey n in
1553 let y = y + top in
1554 gotoghyll y
1557 let invalidate () =
1558 state.layout <- [];
1559 state.pdims <- [];
1560 state.rects <- [];
1561 state.rects1 <- [];
1562 state.invalidated <- state.invalidated + 1;
1565 let writeopen path password =
1566 writecmd state.sw ("open " ^ path ^ "\000" ^ password ^ "\000");
1569 let opendoc path password =
1570 invalidate ();
1571 state.path <- path;
1572 state.password <- password;
1573 state.gen <- state.gen + 1;
1574 state.docinfo <- [];
1576 setaalevel conf.aalevel;
1577 writeopen path password;
1578 Wsi.settitle ("llpp " ^ Filename.basename path);
1579 wcmd "geometry" [`i state.w; `i conf.winh];
1582 let scalecolor c =
1583 let c = c *. conf.colorscale in
1584 (c, c, c);
1587 let scalecolor2 (r, g, b) =
1588 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1591 let represent () =
1592 let docolumns = function
1593 | None -> ()
1594 | Some ((columns, coverA, coverB), _) ->
1595 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1596 let rec loop pageno pdimno pdim x y rowh pdims =
1597 if pageno = state.pagecount
1598 then ()
1599 else
1600 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1601 match pdims with
1602 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1603 pdimno+1, pdim, rest
1604 | _ ->
1605 pdimno, pdim, pdims
1607 let x, y, rowh' =
1608 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1609 then (
1610 (conf.winw - state.scrollw - w) / 2,
1611 y + rowh + conf.interpagespace, h
1613 else (
1614 if (pageno - coverA) mod columns = 0
1615 then 0, y + rowh + conf.interpagespace, h
1616 else x, y, max rowh h
1619 let rec fixrow m = if m = pageno then () else
1620 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1621 if h < rowh
1622 then (
1623 let y = y + (rowh - h) / 2 in
1624 a.(m) <- (pdimno, x, y, pdim);
1626 fixrow (m+1)
1628 if pageno > 1 && (pageno - coverA) mod columns = 0
1629 then fixrow (pageno - columns);
1630 a.(pageno) <- (pdimno, x, y, pdim);
1631 let x = x + w + xoff*2 + conf.interpagespace in
1632 loop (pageno+1) pdimno pdim x y rowh' pdims
1634 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1635 conf.columns <- Some ((columns, coverA, coverB), a);
1637 docolumns conf.columns;
1638 state.maxy <- calcheight ();
1639 state.hscrollh <-
1640 if state.w <= conf.winw - state.scrollw
1641 then 0
1642 else state.scrollw
1644 match state.mode with
1645 | Birdseye (_, _, pageno, _, _) ->
1646 let y, h = getpageyh pageno in
1647 let top = (conf.winh - h) / 2 in
1648 gotoy (max 0 (y - top))
1649 | _ -> gotoanchor state.anchor
1652 let reshape =
1653 let firsttime = ref true in
1654 fun ~w ~h ->
1655 GlDraw.viewport 0 0 w h;
1656 if state.invalidated = 0 && not !firsttime
1657 then state.anchor <- getanchor ();
1659 firsttime := false;
1660 conf.winw <- w;
1661 let w = truncate (float w *. conf.zoom) - state.scrollw in
1662 let w = max w 2 in
1663 state.w <- w;
1664 conf.winh <- h;
1665 setfontsize fstate.fontsize;
1666 GlMat.mode `modelview;
1667 GlMat.load_identity ();
1669 GlMat.mode `projection;
1670 GlMat.load_identity ();
1671 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1672 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1673 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1675 let w =
1676 match conf.columns with
1677 | None -> w
1678 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1680 invalidate ();
1681 wcmd "geometry" [`i w; `i h];
1684 let enttext () =
1685 let len = String.length state.text in
1686 let drawstring s =
1687 let hscrollh =
1688 match state.mode with
1689 | View -> state.hscrollh
1690 | _ -> 0
1692 let rect x w =
1693 GlDraw.rect
1694 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1695 (x+.w, float (conf.winh - hscrollh))
1698 let w = float (conf.winw - state.scrollw - 1) in
1699 if state.progress >= 0.0 && state.progress < 1.0
1700 then (
1701 GlDraw.color (0.3, 0.3, 0.3);
1702 let w1 = w *. state.progress in
1703 rect 0.0 w1;
1704 GlDraw.color (0.0, 0.0, 0.0);
1705 rect w1 (w-.w1)
1707 else (
1708 GlDraw.color (0.0, 0.0, 0.0);
1709 rect 0.0 w;
1712 GlDraw.color (1.0, 1.0, 1.0);
1713 drawstring fstate.fontsize
1714 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1716 let s =
1717 match state.mode with
1718 | Textentry ((prefix, text, _, _, _), _) ->
1719 let s =
1720 if len > 0
1721 then
1722 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1723 else
1724 Printf.sprintf "%s%s_" prefix text
1728 | _ -> state.text
1730 let s =
1731 if state.newerrmsgs
1732 then (
1733 if not (istextentry state.mode)
1734 then
1735 let s1 = "(press 'e' to review error messasges)" in
1736 if String.length s > 0 then s ^ " " ^ s1 else s1
1737 else s
1739 else s
1741 if String.length s > 0
1742 then drawstring s
1745 let gctiles () =
1746 let len = Queue.length state.tilelru in
1747 let rec loop qpos =
1748 if state.memused <= conf.memlimit
1749 then ()
1750 else (
1751 if qpos < len
1752 then
1753 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1754 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1755 let (_, pw, ph, _) = getpagedim n in
1757 gen = state.gen
1758 && colorspace = conf.colorspace
1759 && angle = conf.angle
1760 && pagew = pw
1761 && pageh = ph
1762 && (
1763 let layout =
1764 match state.throttle with
1765 | None ->
1766 if conf.preload
1767 then preloadlayout state.layout
1768 else state.layout
1769 | Some (layout, _, _) ->
1770 layout
1772 let x = col*conf.tilew
1773 and y = row*conf.tileh in
1774 tilevisible layout n x y
1776 then Queue.push lruitem state.tilelru
1777 else (
1778 wcmd "freetile" [`s p];
1779 state.memused <- state.memused - s;
1780 state.uioh#infochanged Memused;
1781 Hashtbl.remove state.tilemap k;
1783 loop (qpos+1)
1786 loop 0
1789 let flushtiles () =
1790 Queue.iter (fun (k, p, s) ->
1791 wcmd "freetile" [`s p];
1792 state.memused <- state.memused - s;
1793 state.uioh#infochanged Memused;
1794 Hashtbl.remove state.tilemap k;
1795 ) state.tilelru;
1796 Queue.clear state.tilelru;
1797 load state.layout;
1800 let logcurrently = function
1801 | Idle -> dolog "Idle"
1802 | Loading (l, gen) ->
1803 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1804 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1805 dolog
1806 "Tiling %d[%d,%d] page=%s cs=%s angle"
1807 l.pageno col row pageopaque
1808 (colorspace_to_string colorspace)
1810 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1811 angle gen conf.angle state.gen
1812 tilew tileh
1813 conf.tilew conf.tileh
1815 | Outlining _ ->
1816 dolog "outlining"
1819 let act cmds =
1820 (* dolog "%S" cmds; *)
1821 let op, args =
1822 let spacepos =
1823 try String.index cmds ' '
1824 with Not_found -> -1
1826 if spacepos = -1
1827 then cmds, ""
1828 else
1829 let l = String.length cmds in
1830 let op = String.sub cmds 0 spacepos in
1831 op, begin
1832 if l - spacepos < 2 then ""
1833 else String.sub cmds (spacepos+1) (l-spacepos-1)
1836 match op with
1837 | "clear" ->
1838 state.uioh#infochanged Pdim;
1839 state.pdims <- [];
1841 | "clearrects" ->
1842 state.rects <- state.rects1;
1843 G.postRedisplay "clearrects";
1845 | "continue" ->
1846 let n =
1847 try Scanf.sscanf args "%u" (fun n -> n)
1848 with exn ->
1849 dolog "error processing 'continue' %S: %s"
1850 cmds (Printexc.to_string exn);
1851 exit 1;
1853 state.pagecount <- n;
1854 state.invalidated <- state.invalidated - 1;
1855 begin match state.currently with
1856 | Outlining l ->
1857 state.currently <- Idle;
1858 state.outlines <- Array.of_list (List.rev l)
1859 | _ -> ()
1860 end;
1861 if state.invalidated = 0
1862 then represent ();
1863 if conf.maxwait = None
1864 then G.postRedisplay "continue";
1866 | "title" ->
1867 Wsi.settitle args
1869 | "msg" ->
1870 showtext ' ' args
1872 | "vmsg" ->
1873 if conf.verbose
1874 then showtext ' ' args
1876 | "progress" ->
1877 let progress, text =
1879 Scanf.sscanf args "%f %n"
1880 (fun f pos ->
1881 f, String.sub args pos (String.length args - pos))
1882 with exn ->
1883 dolog "error processing 'progress' %S: %s"
1884 cmds (Printexc.to_string exn);
1885 exit 1;
1887 state.text <- text;
1888 state.progress <- progress;
1889 G.postRedisplay "progress"
1891 | "firstmatch" ->
1892 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1894 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1895 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1896 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1897 with exn ->
1898 dolog "error processing 'firstmatch' %S: %s"
1899 cmds (Printexc.to_string exn);
1900 exit 1;
1902 let y = (getpagey pageno) + truncate y0 in
1903 addnav ();
1904 gotoy y;
1905 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1907 | "match" ->
1908 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1910 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1911 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1912 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1913 with exn ->
1914 dolog "error processing 'match' %S: %s"
1915 cmds (Printexc.to_string exn);
1916 exit 1;
1918 state.rects1 <-
1919 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1921 | "page" ->
1922 let pageopaque, t =
1924 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1925 with exn ->
1926 dolog "error processing 'page' %S: %s"
1927 cmds (Printexc.to_string exn);
1928 exit 1;
1930 begin match state.currently with
1931 | Loading (l, gen) ->
1932 vlog "page %d took %f sec" l.pageno t;
1933 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1934 begin match state.throttle with
1935 | None ->
1936 let preloadedpages =
1937 if conf.preload
1938 then preloadlayout state.layout
1939 else state.layout
1941 let evict () =
1942 let module IntSet =
1943 Set.Make (struct type t = int let compare = (-) end) in
1944 let set =
1945 List.fold_left (fun s l -> IntSet.add l.pageno s)
1946 IntSet.empty preloadedpages
1948 let evictedpages =
1949 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1950 if not (IntSet.mem pageno set)
1951 then (
1952 wcmd "freepage" [`s opaque];
1953 key :: accu
1955 else accu
1956 ) state.pagemap []
1958 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1960 evict ();
1961 state.currently <- Idle;
1962 if gen = state.gen
1963 then (
1964 tilepage l.pageno pageopaque state.layout;
1965 load state.layout;
1966 load preloadedpages;
1967 if pagevisible state.layout l.pageno
1968 && layoutready state.layout
1969 then G.postRedisplay "page";
1972 | Some (layout, _, _) ->
1973 state.currently <- Idle;
1974 tilepage l.pageno pageopaque layout;
1975 load state.layout
1976 end;
1978 | _ ->
1979 dolog "Inconsistent loading state";
1980 logcurrently state.currently;
1981 exit 1
1984 | "tile" ->
1985 let (x, y, opaque, size, t) =
1987 Scanf.sscanf args "%u %u %s %u %f"
1988 (fun x y p size t -> (x, y, p, size, t))
1989 with exn ->
1990 dolog "error processing 'tile' %S: %s"
1991 cmds (Printexc.to_string exn);
1992 exit 1;
1994 begin match state.currently with
1995 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1996 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1998 if tilew != conf.tilew || tileh != conf.tileh
1999 then (
2000 wcmd "freetile" [`s opaque];
2001 state.currently <- Idle;
2002 load state.layout;
2004 else (
2005 puttileopaque l col row gen cs angle opaque size t;
2006 state.memused <- state.memused + size;
2007 state.uioh#infochanged Memused;
2008 gctiles ();
2009 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2010 opaque, size) state.tilelru;
2012 let layout =
2013 match state.throttle with
2014 | None -> state.layout
2015 | Some (layout, _, _) -> layout
2018 state.currently <- Idle;
2019 if gen = state.gen
2020 && conf.colorspace = cs
2021 && conf.angle = angle
2022 && tilevisible layout l.pageno x y
2023 then conttiling l.pageno pageopaque;
2025 begin match state.throttle with
2026 | None ->
2027 preload state.layout;
2028 if gen = state.gen
2029 && conf.colorspace = cs
2030 && conf.angle = angle
2031 && tilevisible state.layout l.pageno x y
2032 then G.postRedisplay "tile nothrottle";
2034 | Some (layout, y, _) ->
2035 let ready = layoutready layout in
2036 if ready
2037 then (
2038 state.y <- y;
2039 state.layout <- layout;
2040 state.throttle <- None;
2041 G.postRedisplay "throttle";
2043 else load layout;
2044 end;
2047 | _ ->
2048 dolog "Inconsistent tiling state";
2049 logcurrently state.currently;
2050 exit 1
2053 | "pdim" ->
2054 let pdim =
2056 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2057 with exn ->
2058 dolog "error processing 'pdim' %S: %s"
2059 cmds (Printexc.to_string exn);
2060 exit 1;
2062 state.uioh#infochanged Pdim;
2063 state.pdims <- pdim :: state.pdims
2065 | "o" ->
2066 let (l, n, t, h, pos) =
2068 Scanf.sscanf args "%u %u %d %u %n"
2069 (fun l n t h pos -> l, n, t, h, pos)
2070 with exn ->
2071 dolog "error processing 'o' %S: %s"
2072 cmds (Printexc.to_string exn);
2073 exit 1;
2075 let s = String.sub args pos (String.length args - pos) in
2076 let outline = (s, l, (n, float t /. float h)) in
2077 begin match state.currently with
2078 | Outlining outlines ->
2079 state.currently <- Outlining (outline :: outlines)
2080 | Idle ->
2081 state.currently <- Outlining [outline]
2082 | currently ->
2083 dolog "invalid outlining state";
2084 logcurrently currently
2087 | "info" ->
2088 state.docinfo <- (1, args) :: state.docinfo
2090 | "infoend" ->
2091 state.uioh#infochanged Docinfo;
2092 state.docinfo <- List.rev state.docinfo
2094 | _ ->
2095 dolog "unknown cmd `%S'" cmds
2098 let onhist cb =
2099 let rc = cb.rc in
2100 let action = function
2101 | HCprev -> cbget cb ~-1
2102 | HCnext -> cbget cb 1
2103 | HCfirst -> cbget cb ~-(cb.rc)
2104 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2105 and cancel () = cb.rc <- rc
2106 in (action, cancel)
2109 let search pattern forward =
2110 if String.length pattern > 0
2111 then
2112 let pn, py =
2113 match state.layout with
2114 | [] -> 0, 0
2115 | l :: _ ->
2116 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2118 let cmd =
2119 let b = makecmd "search"
2120 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
2122 Buffer.add_char b ',';
2123 Buffer.add_string b pattern;
2124 Buffer.add_char b '\000';
2125 Buffer.contents b;
2127 writecmd state.sw cmd;
2130 let intentry text key =
2131 let c =
2132 if key >= 32 && key < 127
2133 then Char.chr key
2134 else '\000'
2136 match c with
2137 | '0' .. '9' ->
2138 let text = addchar text c in
2139 TEcont text
2141 | _ ->
2142 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2143 TEcont text
2146 let textentry text key =
2147 if key land 0xff00 = 0xff00
2148 then TEcont text
2149 else TEcont (text ^ Wsi.toutf8 key)
2152 let reqlayout angle proportional =
2153 match state.throttle with
2154 | None ->
2155 if state.invalidated = 0 then state.anchor <- getanchor ();
2156 conf.angle <- angle mod 360;
2157 conf.proportional <- proportional;
2158 invalidate ();
2159 wcmd "reqlayout" [`i conf.angle; `b proportional];
2160 | _ -> ()
2163 let settrim trimmargins trimfuzz =
2164 if state.invalidated = 0 then state.anchor <- getanchor ();
2165 conf.trimmargins <- trimmargins;
2166 conf.trimfuzz <- trimfuzz;
2167 let x0, y0, x1, y1 = trimfuzz in
2168 invalidate ();
2169 wcmd "settrim" [
2170 `b conf.trimmargins;
2171 `i x0;
2172 `i y0;
2173 `i x1;
2174 `i y1;
2176 Hashtbl.iter (fun _ opaque ->
2177 wcmd "freepage" [`s opaque];
2178 ) state.pagemap;
2179 Hashtbl.clear state.pagemap;
2182 let setzoom zoom =
2183 match state.throttle with
2184 | None ->
2185 let zoom = max 0.01 zoom in
2186 if zoom <> conf.zoom
2187 then (
2188 state.prevzoom <- conf.zoom;
2189 let relx =
2190 if zoom <= 1.0
2191 then (state.x <- 0; 0.0)
2192 else float state.x /. float state.w
2194 conf.zoom <- zoom;
2195 reshape conf.winw conf.winh;
2196 if zoom > 1.0
2197 then (
2198 let x = relx *. float state.w in
2199 state.x <- truncate x;
2201 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2204 | Some (layout, y, started) ->
2205 let time =
2206 match conf.maxwait with
2207 | None -> 0.0
2208 | Some t -> t
2210 let dt = now () -. started in
2211 if dt > time
2212 then (
2213 state.y <- y;
2214 load layout;
2218 let setcolumns columns coverA coverB =
2219 if columns < 2
2220 then (
2221 conf.columns <- None;
2222 state.x <- 0;
2223 setzoom 1.0;
2225 else (
2226 conf.columns <- Some ((columns, coverA, coverB), [||]);
2227 conf.zoom <- 1.0;
2229 reshape conf.winw conf.winh;
2232 let enterbirdseye () =
2233 let zoom = float conf.thumbw /. float conf.winw in
2234 let birdseyepageno =
2235 let cy = conf.winh / 2 in
2236 let fold = function
2237 | [] -> 0
2238 | l :: rest ->
2239 let rec fold best = function
2240 | [] -> best.pageno
2241 | l :: rest ->
2242 let d = cy - (l.pagedispy + l.pagevh/2)
2243 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2244 if abs d < abs dbest
2245 then fold l rest
2246 else best.pageno
2247 in fold l rest
2249 fold state.layout
2251 state.mode <- Birdseye (
2252 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2254 conf.zoom <- zoom;
2255 conf.presentation <- false;
2256 conf.interpagespace <- 10;
2257 conf.hlinks <- false;
2258 state.x <- 0;
2259 state.mstate <- Mnone;
2260 conf.maxwait <- None;
2261 conf.columns <- (
2262 match conf.beyecolumns with
2263 | Some c ->
2264 conf.zoom <- 1.0;
2265 Some ((c, 0, 0), [||])
2266 | None -> None
2268 Wsi.setcursor Wsi.CURSOR_INHERIT;
2269 if conf.verbose
2270 then
2271 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2272 (100.0*.zoom)
2273 else
2274 state.text <- ""
2276 reshape conf.winw conf.winh;
2279 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2280 state.mode <- View;
2281 conf.zoom <- c.zoom;
2282 conf.presentation <- c.presentation;
2283 conf.interpagespace <- c.interpagespace;
2284 conf.maxwait <- c.maxwait;
2285 conf.hlinks <- c.hlinks;
2286 conf.beyecolumns <- (
2287 match conf.columns with
2288 | Some ((c, _, _), _) -> Some c
2289 | None -> None
2291 conf.columns <- (
2292 match c.columns with
2293 | Some (c, _) -> Some (c, [||])
2294 | None -> None
2296 state.x <- leftx;
2297 if conf.verbose
2298 then
2299 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2300 (100.0*.conf.zoom)
2302 reshape conf.winw conf.winh;
2303 state.anchor <- if goback then anchor else (pageno, 0.0);
2306 let togglebirdseye () =
2307 match state.mode with
2308 | Birdseye vals -> leavebirdseye vals true
2309 | View -> enterbirdseye ()
2310 | _ -> ()
2313 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2314 let pageno = max 0 (pageno - incr) in
2315 let rec loop = function
2316 | [] -> gotopage1 pageno 0
2317 | l :: _ when l.pageno = pageno ->
2318 if l.pagedispy >= 0 && l.pagey = 0
2319 then G.postRedisplay "upbirdseye"
2320 else gotopage1 pageno 0
2321 | _ :: rest -> loop rest
2323 loop state.layout;
2324 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2327 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2328 let pageno = min (state.pagecount - 1) (pageno + incr) in
2329 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2330 let rec loop = function
2331 | [] ->
2332 let y, h = getpageyh pageno in
2333 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2334 gotoy (clamp dy)
2335 | l :: _ when l.pageno = pageno ->
2336 if l.pagevh != l.pageh
2337 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2338 else G.postRedisplay "downbirdseye"
2339 | _ :: rest -> loop rest
2341 loop state.layout
2344 let optentry mode _ key =
2345 let btos b = if b then "on" else "off" in
2346 if key >= 32 && key < 127
2347 then
2348 let c = Char.chr key in
2349 match c with
2350 | 's' ->
2351 let ondone s =
2352 try conf.scrollstep <- int_of_string s with exc ->
2353 state.text <- Printf.sprintf "bad integer `%s': %s"
2354 s (Printexc.to_string exc)
2356 TEswitch ("scroll step: ", "", None, intentry, ondone)
2358 | 'A' ->
2359 let ondone s =
2361 conf.autoscrollstep <- int_of_string s;
2362 if state.autoscroll <> None
2363 then state.autoscroll <- Some conf.autoscrollstep
2364 with exc ->
2365 state.text <- Printf.sprintf "bad integer `%s': %s"
2366 s (Printexc.to_string exc)
2368 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2370 | 'C' ->
2371 let ondone s =
2373 let n, a, b = columns_of_string s in
2374 setcolumns n a b;
2375 with exc ->
2376 state.text <- Printf.sprintf "bad columns `%s': %s"
2377 s (Printexc.to_string exc)
2379 TEswitch ("columns: ", "", None, textentry, ondone)
2381 | 'Z' ->
2382 let ondone s =
2384 let zoom = float (int_of_string s) /. 100.0 in
2385 setzoom zoom
2386 with exc ->
2387 state.text <- Printf.sprintf "bad integer `%s': %s"
2388 s (Printexc.to_string exc)
2390 TEswitch ("zoom: ", "", None, intentry, ondone)
2392 | 't' ->
2393 let ondone s =
2395 conf.thumbw <- bound (int_of_string s) 2 4096;
2396 state.text <-
2397 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2398 begin match mode with
2399 | Birdseye beye ->
2400 leavebirdseye beye false;
2401 enterbirdseye ();
2402 | _ -> ();
2404 with exc ->
2405 state.text <- Printf.sprintf "bad integer `%s': %s"
2406 s (Printexc.to_string exc)
2408 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2410 | 'R' ->
2411 let ondone s =
2412 match try
2413 Some (int_of_string s)
2414 with exc ->
2415 state.text <- Printf.sprintf "bad integer `%s': %s"
2416 s (Printexc.to_string exc);
2417 None
2418 with
2419 | Some angle -> reqlayout angle conf.proportional
2420 | None -> ()
2422 TEswitch ("rotation: ", "", None, intentry, ondone)
2424 | 'i' ->
2425 conf.icase <- not conf.icase;
2426 TEdone ("case insensitive search " ^ (btos conf.icase))
2428 | 'p' ->
2429 conf.preload <- not conf.preload;
2430 gotoy state.y;
2431 TEdone ("preload " ^ (btos conf.preload))
2433 | 'v' ->
2434 conf.verbose <- not conf.verbose;
2435 TEdone ("verbose " ^ (btos conf.verbose))
2437 | 'd' ->
2438 conf.debug <- not conf.debug;
2439 TEdone ("debug " ^ (btos conf.debug))
2441 | 'h' ->
2442 conf.maxhfit <- not conf.maxhfit;
2443 state.maxy <-
2444 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2445 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2447 | 'c' ->
2448 conf.crophack <- not conf.crophack;
2449 TEdone ("crophack " ^ btos conf.crophack)
2451 | 'a' ->
2452 let s =
2453 match conf.maxwait with
2454 | None ->
2455 conf.maxwait <- Some infinity;
2456 "always wait for page to complete"
2457 | Some _ ->
2458 conf.maxwait <- None;
2459 "show placeholder if page is not ready"
2461 TEdone s
2463 | 'f' ->
2464 conf.underinfo <- not conf.underinfo;
2465 TEdone ("underinfo " ^ btos conf.underinfo)
2467 | 'P' ->
2468 conf.savebmarks <- not conf.savebmarks;
2469 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2471 | 'S' ->
2472 let ondone s =
2474 let pageno, py =
2475 match state.layout with
2476 | [] -> 0, 0
2477 | l :: _ ->
2478 l.pageno, l.pagey
2480 conf.interpagespace <- int_of_string s;
2481 state.maxy <- calcheight ();
2482 let y = getpagey pageno in
2483 gotoy (y + py)
2484 with exc ->
2485 state.text <- Printf.sprintf "bad integer `%s': %s"
2486 s (Printexc.to_string exc)
2488 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2490 | 'l' ->
2491 reqlayout conf.angle (not conf.proportional);
2492 TEdone ("proportional display " ^ btos conf.proportional)
2494 | 'T' ->
2495 settrim (not conf.trimmargins) conf.trimfuzz;
2496 TEdone ("trim margins " ^ btos conf.trimmargins)
2498 | 'I' ->
2499 conf.invert <- not conf.invert;
2500 TEdone ("invert colors " ^ btos conf.invert)
2502 | 'x' ->
2503 let ondone s =
2504 cbput state.hists.sel s;
2505 conf.selcmd <- s;
2507 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2508 textentry, ondone)
2510 | _ ->
2511 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2512 TEstop
2513 else
2514 TEcont state.text
2517 class type lvsource = object
2518 method getitemcount : int
2519 method getitem : int -> (string * int)
2520 method hasaction : int -> bool
2521 method exit :
2522 uioh:uioh ->
2523 cancel:bool ->
2524 active:int ->
2525 first:int ->
2526 pan:int ->
2527 qsearch:string ->
2528 uioh option
2529 method getactive : int
2530 method getfirst : int
2531 method getqsearch : string
2532 method setqsearch : string -> unit
2533 method getpan : int
2534 end;;
2536 class virtual lvsourcebase = object
2537 val mutable m_active = 0
2538 val mutable m_first = 0
2539 val mutable m_qsearch = ""
2540 val mutable m_pan = 0
2541 method getactive = m_active
2542 method getfirst = m_first
2543 method getqsearch = m_qsearch
2544 method getpan = m_pan
2545 method setqsearch s = m_qsearch <- s
2546 end;;
2548 let withoutlastutf8 s =
2549 let len = String.length s in
2550 if len = 0
2551 then s
2552 else
2553 let rec find pos =
2554 if pos = 0
2555 then pos
2556 else
2557 let b = Char.code s.[pos] in
2558 if b land 0b110000 = 0b11000000
2559 then find (pos-1)
2560 else pos-1
2562 let first =
2563 if Char.code s.[len-1] land 0x80 = 0
2564 then len-1
2565 else find (len-1)
2567 String.sub s 0 first;
2570 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2571 let enttext te =
2572 state.mode <- Textentry (te, onleave);
2573 state.text <- "";
2574 enttext ();
2575 G.postRedisplay "textentrykeyboard enttext";
2577 let histaction cmd =
2578 match opthist with
2579 | None -> ()
2580 | Some (action, _) ->
2581 state.mode <- Textentry (
2582 (c, action cmd, opthist, onkey, ondone), onleave
2584 G.postRedisplay "textentry histaction"
2586 match key with
2587 | 0xff08 -> (* backspace *)
2588 let s = withoutlastutf8 text in
2589 let len = String.length s in
2590 if len = 0
2591 then (
2592 onleave Cancel;
2593 G.postRedisplay "textentrykeyboard after cancel";
2595 else (
2596 enttext (c, s, opthist, onkey, ondone)
2599 | 0xff0d ->
2600 ondone text;
2601 onleave Confirm;
2602 G.postRedisplay "textentrykeyboard after confirm"
2604 | 0xff52 -> histaction HCprev
2605 | 0xff54 -> histaction HCnext
2606 | 0xff50 -> histaction HCfirst
2607 | 0xff57 -> histaction HClast
2609 | 0xff1b -> (* escape*)
2610 if String.length text = 0
2611 then (
2612 begin match opthist with
2613 | None -> ()
2614 | Some (_, onhistcancel) -> onhistcancel ()
2615 end;
2616 onleave Cancel;
2617 state.text <- "";
2618 G.postRedisplay "textentrykeyboard after cancel2"
2620 else (
2621 enttext (c, "", opthist, onkey, ondone)
2624 | 0xff9f | 0xffff -> () (* delete *)
2626 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2627 begin match onkey text key with
2628 | TEdone text ->
2629 ondone text;
2630 onleave Confirm;
2631 G.postRedisplay "textentrykeyboard after confirm2";
2633 | TEcont text ->
2634 enttext (c, text, opthist, onkey, ondone);
2636 | TEstop ->
2637 onleave Cancel;
2638 G.postRedisplay "textentrykeyboard after cancel3"
2640 | TEswitch te ->
2641 state.mode <- Textentry (te, onleave);
2642 G.postRedisplay "textentrykeyboard switch";
2643 end;
2645 | _ ->
2646 vlog "unhandled key %#x" key
2649 let firstof first active =
2650 if first > active || abs (first - active) > fstate.maxrows - 1
2651 then max 0 (active - (fstate.maxrows/2))
2652 else first
2655 let calcfirst first active =
2656 if active > first
2657 then
2658 let rows = active - first in
2659 if rows > fstate.maxrows then active - fstate.maxrows else first
2660 else active
2663 let scrollph y maxy =
2664 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2665 let sh = float conf.winh /. sh in
2666 let sh = max sh (float conf.scrollh) in
2668 let percent =
2669 if y = state.maxy
2670 then 1.0
2671 else float y /. float maxy
2673 let position = (float conf.winh -. sh) *. percent in
2675 let position =
2676 if position +. sh > float conf.winh
2677 then float conf.winh -. sh
2678 else position
2680 position, sh;
2683 let coe s = (s :> uioh);;
2685 class listview ~(source:lvsource) ~trusted =
2686 object (self)
2687 val m_pan = source#getpan
2688 val m_first = source#getfirst
2689 val m_active = source#getactive
2690 val m_qsearch = source#getqsearch
2691 val m_prev_uioh = state.uioh
2693 method private elemunder y =
2694 let n = y / (fstate.fontsize+1) in
2695 if m_first + n < source#getitemcount
2696 then (
2697 if source#hasaction (m_first + n)
2698 then Some (m_first + n)
2699 else None
2701 else None
2703 method display =
2704 Gl.enable `blend;
2705 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2706 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2707 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2708 GlDraw.color (1., 1., 1.);
2709 Gl.enable `texture_2d;
2710 let fs = fstate.fontsize in
2711 let nfs = fs + 1 in
2712 let ww = fstate.wwidth in
2713 let tabw = 30.0*.ww in
2714 let itemcount = source#getitemcount in
2715 let rec loop row =
2716 if (row - m_first) * nfs > conf.winh
2717 then ()
2718 else (
2719 if row >= 0 && row < itemcount
2720 then (
2721 let (s, level) = source#getitem row in
2722 let y = (row - m_first) * nfs in
2723 let x = 5.0 +. float (level + m_pan) *. ww in
2724 if row = m_active
2725 then (
2726 Gl.disable `texture_2d;
2727 GlDraw.polygon_mode `both `line;
2728 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2729 GlDraw.rect (1., float (y + 1))
2730 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2731 GlDraw.polygon_mode `both `fill;
2732 GlDraw.color (1., 1., 1.);
2733 Gl.enable `texture_2d;
2736 let drawtabularstring s =
2737 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2738 if trusted
2739 then
2740 let tabpos = try String.index s '\t' with Not_found -> -1 in
2741 if tabpos > 0
2742 then
2743 let len = String.length s - tabpos - 1 in
2744 let s1 = String.sub s 0 tabpos
2745 and s2 = String.sub s (tabpos + 1) len in
2746 let nx = drawstr x s1 in
2747 let sw = nx -. x in
2748 let x = x +. (max tabw sw) in
2749 drawstr x s2
2750 else
2751 drawstr x s
2752 else
2753 drawstr x s
2755 let _ = drawtabularstring s in
2756 loop (row+1)
2760 loop m_first;
2761 Gl.disable `blend;
2762 Gl.disable `texture_2d;
2764 method updownlevel incr =
2765 let len = source#getitemcount in
2766 let curlevel =
2767 if m_active >= 0 && m_active < len
2768 then snd (source#getitem m_active)
2769 else -1
2771 let rec flow i =
2772 if i = len then i-1 else if i = -1 then 0 else
2773 let _, l = source#getitem i in
2774 if l != curlevel then i else flow (i+incr)
2776 let active = flow m_active in
2777 let first = calcfirst m_first active in
2778 G.postRedisplay "outline updownlevel";
2779 {< m_active = active; m_first = first >}
2781 method private key1 key mask =
2782 let set1 active first qsearch =
2783 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2785 let search active pattern incr =
2786 let dosearch re =
2787 let rec loop n =
2788 if n >= 0 && n < source#getitemcount
2789 then (
2790 let s, _ = source#getitem n in
2792 (try ignore (Str.search_forward re s 0); true
2793 with Not_found -> false)
2794 then Some n
2795 else loop (n + incr)
2797 else None
2799 loop active
2802 let re = Str.regexp_case_fold pattern in
2803 dosearch re
2804 with Failure s ->
2805 state.text <- s;
2806 None
2808 let itemcount = source#getitemcount in
2809 let find start incr =
2810 let rec find i =
2811 if i = -1 || i = itemcount
2812 then -1
2813 else (
2814 if source#hasaction i
2815 then i
2816 else find (i + incr)
2819 find start
2821 let set active first =
2822 let first = bound first 0 (itemcount - fstate.maxrows) in
2823 state.text <- "";
2824 coe {< m_active = active; m_first = first >}
2826 let navigate incr =
2827 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2828 let active, first =
2829 let incr1 = if incr > 0 then 1 else -1 in
2830 if isvisible m_first m_active
2831 then
2832 let next =
2833 let next = m_active + incr in
2834 let next =
2835 if next < 0 || next >= itemcount
2836 then -1
2837 else find next incr1
2839 if next = -1 || abs (m_active - next) > fstate.maxrows
2840 then -1
2841 else next
2843 if next = -1
2844 then
2845 let first = m_first + incr in
2846 let first = bound first 0 (itemcount - 1) in
2847 let next =
2848 let next = m_active + incr in
2849 let next = bound next 0 (itemcount - 1) in
2850 find next ~-incr1
2852 let active = if next = -1 then m_active else next in
2853 active, first
2854 else
2855 let first = min next m_first in
2856 let first =
2857 if abs (next - first) > fstate.maxrows
2858 then first + incr
2859 else first
2861 next, first
2862 else
2863 let first = m_first + incr in
2864 let first = bound first 0 (itemcount - 1) in
2865 let active =
2866 let next = m_active + incr in
2867 let next = bound next 0 (itemcount - 1) in
2868 let next = find next incr1 in
2869 let active =
2870 if next = -1 || abs (m_active - first) > fstate.maxrows
2871 then (
2872 let active = if m_active = -1 then next else m_active in
2873 active
2875 else next
2877 if isvisible first active
2878 then active
2879 else -1
2881 active, first
2883 G.postRedisplay "listview navigate";
2884 set active first;
2886 match key with
2887 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2888 let incr = if key = 0x72 then -1 else 1 in
2889 let active, first =
2890 match search (m_active + incr) m_qsearch incr with
2891 | None ->
2892 state.text <- m_qsearch ^ " [not found]";
2893 m_active, m_first
2894 | Some active ->
2895 state.text <- m_qsearch;
2896 active, firstof m_first active
2898 G.postRedisplay "listview ctrl-r/s";
2899 set1 active first m_qsearch;
2901 | 0xff08 -> (* backspace *)
2902 if String.length m_qsearch = 0
2903 then coe self
2904 else (
2905 let qsearch = withoutlastutf8 m_qsearch in
2906 let len = String.length qsearch in
2907 if len = 0
2908 then (
2909 state.text <- "";
2910 G.postRedisplay "listview empty qsearch";
2911 set1 m_active m_first "";
2913 else
2914 let active, first =
2915 match search m_active qsearch ~-1 with
2916 | None ->
2917 state.text <- qsearch ^ " [not found]";
2918 m_active, m_first
2919 | Some active ->
2920 state.text <- qsearch;
2921 active, firstof m_first active
2923 G.postRedisplay "listview backspace qsearch";
2924 set1 active first qsearch
2927 | key when ((key >= 32 && key < 127)
2928 || (key != 0 && key land 0xff00 != 0xff00)) ->
2929 let pattern =
2930 if key >= 32 && key < 127
2931 then addchar m_qsearch (Char.chr key)
2932 else m_qsearch ^ Wsi.toutf8 key
2934 let active, first =
2935 match search m_active pattern 1 with
2936 | None ->
2937 state.text <- pattern ^ " [not found]";
2938 m_active, m_first
2939 | Some active ->
2940 state.text <- pattern;
2941 active, firstof m_first active
2943 G.postRedisplay "listview qsearch add";
2944 set1 active first pattern;
2946 | 0xff1b -> (* escape *)
2947 state.text <- "";
2948 if String.length m_qsearch = 0
2949 then (
2950 G.postRedisplay "list view escape";
2951 begin
2952 match
2953 source#exit (coe self) true m_active m_first m_pan m_qsearch
2954 with
2955 | None -> m_prev_uioh
2956 | Some uioh -> uioh
2959 else (
2960 G.postRedisplay "list view kill qsearch";
2961 source#setqsearch "";
2962 coe {< m_qsearch = "" >}
2965 | 0xff0d -> (* return *)
2966 state.text <- "";
2967 let self = {< m_qsearch = "" >} in
2968 source#setqsearch "";
2969 let opt =
2970 G.postRedisplay "listview enter";
2971 if m_active >= 0 && m_active < source#getitemcount
2972 then (
2973 source#exit (coe self) false m_active m_first m_pan "";
2975 else (
2976 source#exit (coe self) true m_active m_first m_pan "";
2979 begin match opt with
2980 | None -> m_prev_uioh
2981 | Some uioh -> uioh
2984 | 0xff9f | 0xffff -> (* delete *)
2985 coe self
2987 | 0xff52 -> navigate ~-1 (* up *)
2988 | 0xff54 -> navigate 1 (* down *)
2989 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
2990 | 0xff56 -> navigate fstate.maxrows (* next *)
2992 | 0xff53 -> (* right *)
2993 state.text <- "";
2994 G.postRedisplay "listview right";
2995 coe {< m_pan = m_pan - 1 >}
2997 | 0xff51 -> (* left *)
2998 state.text <- "";
2999 G.postRedisplay "listview left";
3000 coe {< m_pan = m_pan + 1 >}
3002 | 0xff50 -> (* home *)
3003 let active = find 0 1 in
3004 G.postRedisplay "listview home";
3005 set active 0;
3007 | 0xff57 -> (* end *)
3008 let first = max 0 (itemcount - fstate.maxrows) in
3009 let active = find (itemcount - 1) ~-1 in
3010 G.postRedisplay "listview end";
3011 set active first;
3013 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3014 coe self
3016 | _ ->
3017 dolog "listview unknown key %#x" key; coe self
3019 method key key mask =
3020 match state.mode with
3021 | Textentry te -> textentrykeyboard key mask te; coe self
3022 | _ -> self#key1 key mask
3024 method button button down x y _ =
3025 let opt =
3026 match button with
3027 | 1 when x > conf.winw - conf.scrollbw ->
3028 G.postRedisplay "listview scroll";
3029 if down
3030 then
3031 let _, position, sh = self#scrollph in
3032 if y > truncate position && y < truncate (position +. sh)
3033 then (
3034 state.mstate <- Mscrolly;
3035 Some (coe self)
3037 else
3038 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3039 let first = truncate (s *. float source#getitemcount) in
3040 let first = min source#getitemcount first in
3041 Some (coe {< m_first = first; m_active = first >})
3042 else (
3043 state.mstate <- Mnone;
3044 Some (coe self);
3046 | 1 when not down ->
3047 begin match self#elemunder y with
3048 | Some n ->
3049 G.postRedisplay "listview click";
3050 source#exit
3051 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3052 | _ ->
3053 Some (coe self)
3055 | n when (n == 4 || n == 5) && not down ->
3056 let len = source#getitemcount in
3057 let first =
3058 if n = 5 && m_first + fstate.maxrows >= len
3059 then
3060 m_first
3061 else
3062 let first = m_first + (if n == 4 then -1 else 1) in
3063 bound first 0 (len - 1)
3065 G.postRedisplay "listview wheel";
3066 Some (coe {< m_first = first >})
3067 | _ ->
3068 Some (coe self)
3070 match opt with
3071 | None -> m_prev_uioh
3072 | Some uioh -> uioh
3074 method motion _ y =
3075 match state.mstate with
3076 | Mscrolly ->
3077 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3078 let first = truncate (s *. float source#getitemcount) in
3079 let first = min source#getitemcount first in
3080 G.postRedisplay "listview motion";
3081 coe {< m_first = first; m_active = first >}
3082 | _ -> coe self
3084 method pmotion x y =
3085 if x < conf.winw - conf.scrollbw
3086 then
3087 let n =
3088 match self#elemunder y with
3089 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3090 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3092 let o =
3093 if n != m_active
3094 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3095 else self
3097 coe o
3098 else (
3099 Wsi.setcursor Wsi.CURSOR_INHERIT;
3100 coe self
3103 method infochanged _ = ()
3105 method scrollpw = (0, 0.0, 0.0)
3106 method scrollph =
3107 let nfs = fstate.fontsize + 1 in
3108 let y = m_first * nfs in
3109 let itemcount = source#getitemcount in
3110 let maxi = max 0 (itemcount - fstate.maxrows) in
3111 let maxy = maxi * nfs in
3112 let p, h = scrollph y maxy in
3113 conf.scrollbw, p, h
3114 end;;
3116 class outlinelistview ~source =
3117 object (self)
3118 inherit listview ~source:(source :> lvsource) ~trusted:false as super
3120 method key key mask =
3121 let calcfirst first active =
3122 if active > first
3123 then
3124 let rows = active - first in
3125 if rows > fstate.maxrows then active - fstate.maxrows else first
3126 else active
3128 let navigate incr =
3129 let active = m_active + incr in
3130 let active = bound active 0 (source#getitemcount - 1) in
3131 let first = calcfirst m_first active in
3132 G.postRedisplay "outline navigate";
3133 coe {< m_active = active; m_first = first >}
3135 match key with
3136 | 110 when Wsi.withctrl mask -> (* ctrl-n *)
3137 source#narrow m_qsearch;
3138 G.postRedisplay "outline ctrl-n";
3139 coe {< m_first = 0; m_active = 0 >}
3141 | 117 when Wsi.withctrl mask -> (* ctrl-u *)
3142 source#denarrow;
3143 G.postRedisplay "outline ctrl-u";
3144 state.text <- "";
3145 coe {< m_first = 0; m_active = 0 >}
3147 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
3148 let first = m_active - (fstate.maxrows / 2) in
3149 G.postRedisplay "outline ctrl-l";
3150 coe {< m_first = first >}
3152 | 0xff9f | 0xffff -> (* delete *)
3153 source#remove m_active;
3154 G.postRedisplay "outline delete";
3155 let active = max 0 (m_active-1) in
3156 coe {< m_first = firstof m_first active;
3157 m_active = active >}
3159 | 0xff52 -> navigate ~-1 (* up *)
3160 | 0xff54 -> navigate 1 (* down *)
3161 | 0xff55 -> (* prior *)
3162 navigate ~-(fstate.maxrows)
3163 | 0xff56 -> (* next *)
3164 navigate fstate.maxrows
3166 | 0xff53 -> (* [ctrl-]right *)
3167 let o =
3168 if Wsi.withctrl mask
3169 then (
3170 G.postRedisplay "outline ctrl right";
3171 {< m_pan = m_pan + 1 >}
3173 else self#updownlevel 1
3175 coe o
3177 | 0xff51 -> (* [ctrl-]left *)
3178 let o =
3179 if Wsi.withctrl mask
3180 then (
3181 G.postRedisplay "outline ctrl left";
3182 {< m_pan = m_pan - 1 >}
3184 else self#updownlevel ~-1
3186 coe o
3188 | 0xff50 -> (* home *)
3189 G.postRedisplay "outline home";
3190 coe {< m_first = 0; m_active = 0 >}
3192 | 0xff57 -> (* end *)
3193 let active = source#getitemcount - 1 in
3194 let first = max 0 (active - fstate.maxrows) in
3195 G.postRedisplay "outline end";
3196 coe {< m_active = active; m_first = first >}
3198 | _ -> super#key key mask
3201 let outlinesource usebookmarks =
3202 let empty = [||] in
3203 (object
3204 inherit lvsourcebase
3205 val mutable m_items = empty
3206 val mutable m_orig_items = empty
3207 val mutable m_prev_items = empty
3208 val mutable m_narrow_pattern = ""
3209 val mutable m_hadremovals = false
3211 method getitemcount =
3212 Array.length m_items + (if m_hadremovals then 1 else 0)
3214 method getitem n =
3215 if n == Array.length m_items && m_hadremovals
3216 then
3217 ("[Confirm removal]", 0)
3218 else
3219 let s, n, _ = m_items.(n) in
3220 (s, n)
3222 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3223 ignore (uioh, first, qsearch);
3224 let confrimremoval = m_hadremovals && active = Array.length m_items in
3225 let items =
3226 if String.length m_narrow_pattern = 0
3227 then m_orig_items
3228 else m_items
3230 if not cancel
3231 then (
3232 if not confrimremoval
3233 then(
3234 let _, _, anchor = m_items.(active) in
3235 gotoanchor anchor;
3236 m_items <- items;
3238 else (
3239 state.bookmarks <- Array.to_list m_items;
3240 m_orig_items <- m_items;
3243 else m_items <- items;
3244 m_pan <- pan;
3245 None
3247 method hasaction _ = true
3249 method greetmsg =
3250 if Array.length m_items != Array.length m_orig_items
3251 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3252 else ""
3254 method narrow pattern =
3255 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3256 match reopt with
3257 | None -> ()
3258 | Some re ->
3259 let rec loop accu n =
3260 if n = -1
3261 then (
3262 m_narrow_pattern <- pattern;
3263 m_items <- Array.of_list accu
3265 else
3266 let (s, _, _) as o = m_items.(n) in
3267 let accu =
3268 if (try ignore (Str.search_forward re s 0); true
3269 with Not_found -> false)
3270 then o :: accu
3271 else accu
3273 loop accu (n-1)
3275 loop [] (Array.length m_items - 1)
3277 method denarrow =
3278 m_orig_items <- (
3279 if usebookmarks
3280 then Array.of_list state.bookmarks
3281 else state.outlines
3283 m_items <- m_orig_items
3285 method remove m =
3286 if usebookmarks
3287 then
3288 if m >= 0 && m < Array.length m_items
3289 then (
3290 m_hadremovals <- true;
3291 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3292 let n = if n >= m then n+1 else n in
3293 m_items.(n)
3297 method reset anchor items =
3298 m_hadremovals <- false;
3299 if m_orig_items == empty || m_prev_items != items
3300 then (
3301 m_orig_items <- items;
3302 if String.length m_narrow_pattern = 0
3303 then m_items <- items;
3305 m_prev_items <- items;
3306 let rely = getanchory anchor in
3307 let active =
3308 let rec loop n best bestd =
3309 if n = Array.length m_items
3310 then best
3311 else
3312 let (_, _, anchor) = m_items.(n) in
3313 let orely = getanchory anchor in
3314 let d = abs (orely - rely) in
3315 if d < bestd
3316 then loop (n+1) n d
3317 else loop (n+1) best bestd
3319 loop 0 ~-1 max_int
3321 m_active <- active;
3322 m_first <- firstof m_first active
3323 end)
3326 let enterselector usebookmarks =
3327 let source = outlinesource usebookmarks in
3328 fun errmsg ->
3329 let outlines =
3330 if usebookmarks
3331 then Array.of_list state.bookmarks
3332 else state.outlines
3334 if Array.length outlines = 0
3335 then (
3336 showtext ' ' errmsg;
3338 else (
3339 state.text <- source#greetmsg;
3340 Wsi.setcursor Wsi.CURSOR_INHERIT;
3341 let anchor = getanchor () in
3342 source#reset anchor outlines;
3343 state.uioh <- coe (new outlinelistview ~source);
3344 G.postRedisplay "enter selector";
3348 let enteroutlinemode =
3349 let f = enterselector false in
3350 fun ()-> f "Document has no outline";
3353 let enterbookmarkmode =
3354 let f = enterselector true in
3355 fun () -> f "Document has no bookmarks (yet)";
3358 let color_of_string s =
3359 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3360 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3364 let color_to_string (r, g, b) =
3365 let r = truncate (r *. 256.0)
3366 and g = truncate (g *. 256.0)
3367 and b = truncate (b *. 256.0) in
3368 Printf.sprintf "%d/%d/%d" r g b
3371 let irect_of_string s =
3372 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3375 let irect_to_string (x0,y0,x1,y1) =
3376 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3379 let makecheckers () =
3380 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3381 following to say:
3382 converted by Issac Trotts. July 25, 2002 *)
3383 let image_height = 64
3384 and image_width = 64 in
3386 let make_image () =
3387 let image =
3388 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3390 for i = 0 to image_width - 1 do
3391 for j = 0 to image_height - 1 do
3392 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3393 (if (i land 8 ) lxor (j land 8) = 0
3394 then [|255;255;255|] else [|200;200;200|])
3395 done
3396 done;
3397 image
3399 let image = make_image () in
3400 let id = GlTex.gen_texture () in
3401 GlTex.bind_texture `texture_2d id;
3402 GlPix.store (`unpack_alignment 1);
3403 GlTex.image2d image;
3404 List.iter (GlTex.parameter ~target:`texture_2d)
3405 [ `wrap_s `repeat;
3406 `wrap_t `repeat;
3407 `mag_filter `nearest;
3408 `min_filter `nearest ];
3412 let setcheckers enabled =
3413 match state.texid with
3414 | None ->
3415 if enabled then state.texid <- Some (makecheckers ())
3417 | Some texid ->
3418 if not enabled
3419 then (
3420 GlTex.delete_texture texid;
3421 state.texid <- None;
3425 let int_of_string_with_suffix s =
3426 let l = String.length s in
3427 let s1, shift =
3428 if l > 1
3429 then
3430 let suffix = Char.lowercase s.[l-1] in
3431 match suffix with
3432 | 'k' -> String.sub s 0 (l-1), 10
3433 | 'm' -> String.sub s 0 (l-1), 20
3434 | 'g' -> String.sub s 0 (l-1), 30
3435 | _ -> s, 0
3436 else s, 0
3438 let n = int_of_string s1 in
3439 let m = n lsl shift in
3440 if m < 0 || m < n
3441 then raise (Failure "value too large")
3442 else m
3445 let string_with_suffix_of_int n =
3446 if n = 0
3447 then "0"
3448 else
3449 let n, s =
3450 if n = 0
3451 then 0, ""
3452 else (
3453 if n land ((1 lsl 20) - 1) = 0
3454 then n lsr 20, "M"
3455 else (
3456 if n land ((1 lsl 10) - 1) = 0
3457 then n lsr 10, "K"
3458 else n, ""
3462 let rec loop s n =
3463 let h = n mod 1000 in
3464 let n = n / 1000 in
3465 if n = 0
3466 then string_of_int h ^ s
3467 else (
3468 let s = Printf.sprintf "_%03d%s" h s in
3469 loop s n
3472 loop "" n ^ s;
3475 let defghyllscroll = (40, 8, 32);;
3476 let ghyllscroll_of_string s =
3477 let (n, a, b) as nab =
3478 if s = "default"
3479 then defghyllscroll
3480 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3482 if n <= a || n <= b || a >= b
3483 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3484 nab;
3487 let ghyllscroll_to_string ((n, a, b) as nab) =
3488 if nab = defghyllscroll
3489 then "default"
3490 else Printf.sprintf "%d,%d,%d" n a b;
3493 let describe_location () =
3494 let f (fn, _) l =
3495 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3497 let fn, ln = List.fold_left f (-1, -1) state.layout in
3498 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3499 let percent =
3500 if maxy <= 0
3501 then 100.
3502 else (100. *. (float state.y /. float maxy))
3504 if fn = ln
3505 then
3506 Printf.sprintf "page %d of %d [%.2f%%]"
3507 (fn+1) state.pagecount percent
3508 else
3509 Printf.sprintf
3510 "pages %d-%d of %d [%.2f%%]"
3511 (fn+1) (ln+1) state.pagecount percent
3514 let enterinfomode =
3515 let btos b = if b then "\xe2\x88\x9a" else "" in
3516 let showextended = ref false in
3517 let leave mode = function
3518 | Confirm -> state.mode <- mode
3519 | Cancel -> state.mode <- mode in
3520 let src =
3521 (object
3522 val mutable m_first_time = true
3523 val mutable m_l = []
3524 val mutable m_a = [||]
3525 val mutable m_prev_uioh = nouioh
3526 val mutable m_prev_mode = View
3528 inherit lvsourcebase
3530 method reset prev_mode prev_uioh =
3531 m_a <- Array.of_list (List.rev m_l);
3532 m_l <- [];
3533 m_prev_mode <- prev_mode;
3534 m_prev_uioh <- prev_uioh;
3535 if m_first_time
3536 then (
3537 let rec loop n =
3538 if n >= Array.length m_a
3539 then ()
3540 else
3541 match m_a.(n) with
3542 | _, _, _, Action _ -> m_active <- n
3543 | _ -> loop (n+1)
3545 loop 0;
3546 m_first_time <- false;
3549 method int name get set =
3550 m_l <-
3551 (name, `int get, 1, Action (
3552 fun u ->
3553 let ondone s =
3554 try set (int_of_string s)
3555 with exn ->
3556 state.text <- Printf.sprintf "bad integer `%s': %s"
3557 s (Printexc.to_string exn)
3559 state.text <- "";
3560 let te = name ^ ": ", "", None, intentry, ondone in
3561 state.mode <- Textentry (te, leave m_prev_mode);
3563 )) :: m_l
3565 method int_with_suffix name get set =
3566 m_l <-
3567 (name, `intws get, 1, Action (
3568 fun u ->
3569 let ondone s =
3570 try set (int_of_string_with_suffix s)
3571 with exn ->
3572 state.text <- Printf.sprintf "bad integer `%s': %s"
3573 s (Printexc.to_string exn)
3575 state.text <- "";
3576 let te =
3577 name ^ ": ", "", None, intentry_with_suffix, ondone
3579 state.mode <- Textentry (te, leave m_prev_mode);
3581 )) :: m_l
3583 method bool ?(offset=1) ?(btos=btos) name get set =
3584 m_l <-
3585 (name, `bool (btos, get), offset, Action (
3586 fun u ->
3587 let v = get () in
3588 set (not v);
3590 )) :: m_l
3592 method color name get set =
3593 m_l <-
3594 (name, `color get, 1, Action (
3595 fun u ->
3596 let invalid = (nan, nan, nan) in
3597 let ondone s =
3598 let c =
3599 try color_of_string s
3600 with exn ->
3601 state.text <- Printf.sprintf "bad color `%s': %s"
3602 s (Printexc.to_string exn);
3603 invalid
3605 if c <> invalid
3606 then set c;
3608 let te = name ^ ": ", "", None, textentry, ondone in
3609 state.text <- color_to_string (get ());
3610 state.mode <- Textentry (te, leave m_prev_mode);
3612 )) :: m_l
3614 method string name get set =
3615 m_l <-
3616 (name, `string get, 1, Action (
3617 fun u ->
3618 let ondone s = set s in
3619 let te = name ^ ": ", "", None, textentry, ondone in
3620 state.mode <- Textentry (te, leave m_prev_mode);
3622 )) :: m_l
3624 method colorspace name get set =
3625 m_l <-
3626 (name, `string get, 1, Action (
3627 fun _ ->
3628 let source =
3629 let vals = [| "rgb"; "bgr"; "gray" |] in
3630 (object
3631 inherit lvsourcebase
3633 initializer
3634 m_active <- int_of_colorspace conf.colorspace;
3635 m_first <- 0;
3637 method getitemcount = Array.length vals
3638 method getitem n = (vals.(n), 0)
3639 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3640 ignore (uioh, first, pan, qsearch);
3641 if not cancel then set active;
3642 None
3643 method hasaction _ = true
3644 end)
3646 state.text <- "";
3647 coe (new listview ~source ~trusted:true)
3648 )) :: m_l
3650 method caption s offset =
3651 m_l <- (s, `empty, offset, Noaction) :: m_l
3653 method caption2 s f offset =
3654 m_l <- (s, `string f, offset, Noaction) :: m_l
3656 method getitemcount = Array.length m_a
3658 method getitem n =
3659 let tostr = function
3660 | `int f -> string_of_int (f ())
3661 | `intws f -> string_with_suffix_of_int (f ())
3662 | `string f -> f ()
3663 | `color f -> color_to_string (f ())
3664 | `bool (btos, f) -> btos (f ())
3665 | `empty -> ""
3667 let name, t, offset, _ = m_a.(n) in
3668 ((let s = tostr t in
3669 if String.length s > 0
3670 then Printf.sprintf "%s\t%s" name s
3671 else name),
3672 offset)
3674 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3675 let uiohopt =
3676 if not cancel
3677 then (
3678 m_qsearch <- qsearch;
3679 let uioh =
3680 match m_a.(active) with
3681 | _, _, _, Action f -> f uioh
3682 | _ -> uioh
3684 Some uioh
3686 else None
3688 m_active <- active;
3689 m_first <- first;
3690 m_pan <- pan;
3691 uiohopt
3693 method hasaction n =
3694 match m_a.(n) with
3695 | _, _, _, Action _ -> true
3696 | _ -> false
3697 end)
3699 let rec fillsrc prevmode prevuioh =
3700 let sep () = src#caption "" 0 in
3701 let colorp name get set =
3702 src#string name
3703 (fun () -> color_to_string (get ()))
3704 (fun v ->
3706 let c = color_of_string v in
3707 set c
3708 with exn ->
3709 state.text <- Printf.sprintf "bad color `%s': %s"
3710 v (Printexc.to_string exn);
3713 let oldmode = state.mode in
3714 let birdseye = isbirdseye state.mode in
3716 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3718 src#bool "presentation mode"
3719 (fun () -> conf.presentation)
3720 (fun v ->
3721 conf.presentation <- v;
3722 state.anchor <- getanchor ();
3723 represent ());
3725 src#bool "ignore case in searches"
3726 (fun () -> conf.icase)
3727 (fun v -> conf.icase <- v);
3729 src#bool "preload"
3730 (fun () -> conf.preload)
3731 (fun v -> conf.preload <- v);
3733 src#bool "highlight links"
3734 (fun () -> conf.hlinks)
3735 (fun v -> conf.hlinks <- v);
3737 src#bool "under info"
3738 (fun () -> conf.underinfo)
3739 (fun v -> conf.underinfo <- v);
3741 src#bool "persistent bookmarks"
3742 (fun () -> conf.savebmarks)
3743 (fun v -> conf.savebmarks <- v);
3745 src#bool "proportional display"
3746 (fun () -> conf.proportional)
3747 (fun v -> reqlayout conf.angle v);
3749 src#bool "trim margins"
3750 (fun () -> conf.trimmargins)
3751 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3753 src#bool "persistent location"
3754 (fun () -> conf.jumpback)
3755 (fun v -> conf.jumpback <- v);
3757 sep ();
3758 src#int "inter-page space"
3759 (fun () -> conf.interpagespace)
3760 (fun n ->
3761 conf.interpagespace <- n;
3762 let pageno, py =
3763 match state.layout with
3764 | [] -> 0, 0
3765 | l :: _ ->
3766 l.pageno, l.pagey
3768 state.maxy <- calcheight ();
3769 let y = getpagey pageno in
3770 gotoy (y + py)
3773 src#int "page bias"
3774 (fun () -> conf.pagebias)
3775 (fun v -> conf.pagebias <- v);
3777 src#int "scroll step"
3778 (fun () -> conf.scrollstep)
3779 (fun n -> conf.scrollstep <- n);
3781 src#int "auto scroll step"
3782 (fun () ->
3783 match state.autoscroll with
3784 | Some step -> step
3785 | _ -> conf.autoscrollstep)
3786 (fun n ->
3787 if state.autoscroll <> None
3788 then state.autoscroll <- Some n;
3789 conf.autoscrollstep <- n);
3791 src#int "zoom"
3792 (fun () -> truncate (conf.zoom *. 100.))
3793 (fun v -> setzoom ((float v) /. 100.));
3795 src#int "rotation"
3796 (fun () -> conf.angle)
3797 (fun v -> reqlayout v conf.proportional);
3799 src#int "scroll bar width"
3800 (fun () -> state.scrollw)
3801 (fun v ->
3802 state.scrollw <- v;
3803 conf.scrollbw <- v;
3804 reshape conf.winw conf.winh;
3807 src#int "scroll handle height"
3808 (fun () -> conf.scrollh)
3809 (fun v -> conf.scrollh <- v;);
3811 src#int "thumbnail width"
3812 (fun () -> conf.thumbw)
3813 (fun v ->
3814 conf.thumbw <- min 4096 v;
3815 match oldmode with
3816 | Birdseye beye ->
3817 leavebirdseye beye false;
3818 enterbirdseye ()
3819 | _ -> ()
3822 src#string "columns"
3823 (fun () ->
3824 match conf.columns with
3825 | None -> "1"
3826 | Some (multicol, _) -> columns_to_string multicol)
3827 (fun v ->
3828 let n, a, b = columns_of_string v in
3829 setcolumns n a b);
3831 sep ();
3832 src#caption "Presentation mode" 0;
3833 src#bool "scrollbar visible"
3834 (fun () -> conf.scrollbarinpm)
3835 (fun v ->
3836 if v != conf.scrollbarinpm
3837 then (
3838 conf.scrollbarinpm <- v;
3839 if conf.presentation
3840 then (
3841 state.scrollw <- if v then conf.scrollbw else 0;
3842 reshape conf.winw conf.winh;
3847 sep ();
3848 src#caption "Pixmap cache" 0;
3849 src#int_with_suffix "size (advisory)"
3850 (fun () -> conf.memlimit)
3851 (fun v -> conf.memlimit <- v);
3853 src#caption2 "used"
3854 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3855 (string_with_suffix_of_int state.memused)
3856 (Hashtbl.length state.tilemap)) 1;
3858 sep ();
3859 src#caption "Layout" 0;
3860 src#caption2 "Dimension"
3861 (fun () ->
3862 Printf.sprintf "%dx%d (virtual %dx%d)"
3863 conf.winw conf.winh
3864 state.w state.maxy)
3866 if conf.debug
3867 then
3868 src#caption2 "Position" (fun () ->
3869 Printf.sprintf "%dx%d" state.x state.y
3871 else
3872 src#caption2 "Visible" (fun () -> describe_location ()) 1
3875 sep ();
3876 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3877 "Save these parameters as global defaults at exit"
3878 (fun () -> conf.bedefault)
3879 (fun v -> conf.bedefault <- v)
3882 sep ();
3883 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3884 src#bool ~offset:0 ~btos "Extended parameters"
3885 (fun () -> !showextended)
3886 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3887 if !showextended
3888 then (
3889 src#bool "checkers"
3890 (fun () -> conf.checkers)
3891 (fun v -> conf.checkers <- v; setcheckers v);
3892 src#bool "update cursor"
3893 (fun () -> conf.updatecurs)
3894 (fun v -> conf.updatecurs <- v);
3895 src#bool "verbose"
3896 (fun () -> conf.verbose)
3897 (fun v -> conf.verbose <- v);
3898 src#bool "invert colors"
3899 (fun () -> conf.invert)
3900 (fun v -> conf.invert <- v);
3901 src#bool "max fit"
3902 (fun () -> conf.maxhfit)
3903 (fun v -> conf.maxhfit <- v);
3904 src#bool "redirect stderr"
3905 (fun () -> conf.redirectstderr)
3906 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3907 src#string "uri launcher"
3908 (fun () -> conf.urilauncher)
3909 (fun v -> conf.urilauncher <- v);
3910 src#string "path launcher"
3911 (fun () -> conf.pathlauncher)
3912 (fun v -> conf.pathlauncher <- v);
3913 src#string "tile size"
3914 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3915 (fun v ->
3917 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3918 conf.tileh <- max 64 w;
3919 conf.tilew <- max 64 h;
3920 flushtiles ();
3921 with exn ->
3922 state.text <- Printf.sprintf "bad tile size `%s': %s"
3923 v (Printexc.to_string exn));
3924 src#int "texture count"
3925 (fun () -> conf.texcount)
3926 (fun v ->
3927 if realloctexts v
3928 then conf.texcount <- v
3929 else showtext '!' " Failed to set texture count please retry later"
3931 src#int "slice height"
3932 (fun () -> conf.sliceheight)
3933 (fun v ->
3934 conf.sliceheight <- v;
3935 wcmd "sliceh" [`i conf.sliceheight];
3937 src#int "anti-aliasing level"
3938 (fun () -> conf.aalevel)
3939 (fun v ->
3940 conf.aalevel <- bound v 0 8;
3941 state.anchor <- getanchor ();
3942 opendoc state.path state.password;
3944 src#int "ui font size"
3945 (fun () -> fstate.fontsize)
3946 (fun v -> setfontsize (bound v 5 100));
3947 colorp "background color"
3948 (fun () -> conf.bgcolor)
3949 (fun v -> conf.bgcolor <- v);
3950 src#bool "crop hack"
3951 (fun () -> conf.crophack)
3952 (fun v -> conf.crophack <- v);
3953 src#string "trim fuzz"
3954 (fun () -> irect_to_string conf.trimfuzz)
3955 (fun v ->
3957 conf.trimfuzz <- irect_of_string v;
3958 if conf.trimmargins
3959 then settrim true conf.trimfuzz;
3960 with exn ->
3961 state.text <- Printf.sprintf "bad irect `%s': %s"
3962 v (Printexc.to_string exn)
3964 src#string "throttle"
3965 (fun () ->
3966 match conf.maxwait with
3967 | None -> "show place holder if page is not ready"
3968 | Some time ->
3969 if time = infinity
3970 then "wait for page to fully render"
3971 else
3972 "wait " ^ string_of_float time
3973 ^ " seconds before showing placeholder"
3975 (fun v ->
3977 let f = float_of_string v in
3978 if f <= 0.0
3979 then conf.maxwait <- None
3980 else conf.maxwait <- Some f
3981 with exn ->
3982 state.text <- Printf.sprintf "bad time `%s': %s"
3983 v (Printexc.to_string exn)
3985 src#string "ghyll scroll"
3986 (fun () ->
3987 match conf.ghyllscroll with
3988 | None -> ""
3989 | Some nab -> ghyllscroll_to_string nab
3991 (fun v ->
3993 let gs =
3994 if String.length v = 0
3995 then None
3996 else Some (ghyllscroll_of_string v)
3998 conf.ghyllscroll <- gs
3999 with exn ->
4000 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4001 v (Printexc.to_string exn)
4003 src#string "selection command"
4004 (fun () -> conf.selcmd)
4005 (fun v -> conf.selcmd <- v);
4006 src#colorspace "color space"
4007 (fun () -> colorspace_to_string conf.colorspace)
4008 (fun v ->
4009 conf.colorspace <- colorspace_of_int v;
4010 wcmd "cs" [`i v];
4011 load state.layout;
4015 sep ();
4016 src#caption "Document" 0;
4017 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4018 src#caption2 "Pages"
4019 (fun () -> string_of_int state.pagecount) 1;
4020 src#caption2 "Dimensions"
4021 (fun () -> string_of_int (List.length state.pdims)) 1;
4022 if conf.trimmargins
4023 then (
4024 sep ();
4025 src#caption "Trimmed margins" 0;
4026 src#caption2 "Dimensions"
4027 (fun () -> string_of_int (List.length state.pdims)) 1;
4030 src#reset prevmode prevuioh;
4032 fun () ->
4033 state.text <- "";
4034 let prevmode = state.mode
4035 and prevuioh = state.uioh in
4036 fillsrc prevmode prevuioh;
4037 let source = (src :> lvsource) in
4038 state.uioh <- coe (object (self)
4039 inherit listview ~source ~trusted:true as super
4040 val mutable m_prevmemused = 0
4041 method infochanged = function
4042 | Memused ->
4043 if m_prevmemused != state.memused
4044 then (
4045 m_prevmemused <- state.memused;
4046 G.postRedisplay "memusedchanged";
4048 | Pdim -> G.postRedisplay "pdimchanged"
4049 | Docinfo -> fillsrc prevmode prevuioh
4051 method key key mask =
4052 if not (Wsi.withctrl mask)
4053 then
4054 match key with
4055 | 0xff51 -> coe (self#updownlevel ~-1)
4056 | 0xff53 -> coe (self#updownlevel 1)
4057 | _ -> super#key key mask
4058 else super#key key mask
4059 end);
4060 G.postRedisplay "info";
4063 let enterhelpmode =
4064 let source =
4065 (object
4066 inherit lvsourcebase
4067 method getitemcount = Array.length state.help
4068 method getitem n =
4069 let s, n, _ = state.help.(n) in
4070 (s, n)
4072 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4073 let optuioh =
4074 if not cancel
4075 then (
4076 m_qsearch <- qsearch;
4077 match state.help.(active) with
4078 | _, _, Action f -> Some (f uioh)
4079 | _ -> Some (uioh)
4081 else None
4083 m_active <- active;
4084 m_first <- first;
4085 m_pan <- pan;
4086 optuioh
4088 method hasaction n =
4089 match state.help.(n) with
4090 | _, _, Action _ -> true
4091 | _ -> false
4093 initializer
4094 m_active <- -1
4095 end)
4096 in fun () ->
4097 state.uioh <- coe (new listview ~source ~trusted:true);
4098 G.postRedisplay "help";
4101 let entermsgsmode =
4102 let msgsource =
4103 let re = Str.regexp "[\r\n]" in
4104 (object
4105 inherit lvsourcebase
4106 val mutable m_items = [||]
4108 method getitemcount = 1 + Array.length m_items
4110 method getitem n =
4111 if n = 0
4112 then "[Clear]", 0
4113 else m_items.(n-1), 0
4115 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4116 ignore uioh;
4117 if not cancel
4118 then (
4119 if active = 0
4120 then Buffer.clear state.errmsgs;
4121 m_qsearch <- qsearch;
4123 m_active <- active;
4124 m_first <- first;
4125 m_pan <- pan;
4126 None
4128 method hasaction n =
4129 n = 0
4131 method reset =
4132 state.newerrmsgs <- false;
4133 let l = Str.split re (Buffer.contents state.errmsgs) in
4134 m_items <- Array.of_list l
4136 initializer
4137 m_active <- 0
4138 end)
4139 in fun () ->
4140 state.text <- "";
4141 msgsource#reset;
4142 let source = (msgsource :> lvsource) in
4143 state.uioh <- coe (object
4144 inherit listview ~source ~trusted:false as super
4145 method display =
4146 if state.newerrmsgs
4147 then msgsource#reset;
4148 super#display
4149 end);
4150 G.postRedisplay "msgs";
4153 let quickbookmark ?title () =
4154 match state.layout with
4155 | [] -> ()
4156 | l :: _ ->
4157 let title =
4158 match title with
4159 | None ->
4160 let sec = Unix.gettimeofday () in
4161 let tm = Unix.localtime sec in
4162 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4163 (l.pageno+1)
4164 tm.Unix.tm_mday
4165 tm.Unix.tm_mon
4166 (tm.Unix.tm_year + 1900)
4167 tm.Unix.tm_hour
4168 tm.Unix.tm_min
4169 | Some title -> title
4171 state.bookmarks <-
4172 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4173 :: state.bookmarks
4176 let doreshape w h =
4177 state.fullscreen <- None;
4178 Wsi.reshape w h;
4181 let setautoscrollspeed step goingdown =
4182 let incr = max 1 ((abs step) / 2) in
4183 let incr = if goingdown then incr else -incr in
4184 let astep = step + incr in
4185 state.autoscroll <- Some astep;
4188 let viewkeyboard key mask =
4189 let enttext te =
4190 let mode = state.mode in
4191 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4192 state.text <- "";
4193 enttext ();
4194 G.postRedisplay "view:enttext"
4196 match key with
4197 | 81 -> (* Q *)
4198 exit 0
4200 | 0xff1b | 113 -> (* escape / q *)
4201 begin match state.mstate with
4202 | Mzoomrect _ ->
4203 state.mstate <- Mnone;
4204 Wsi.setcursor Wsi.CURSOR_INHERIT;
4205 G.postRedisplay "kill zoom rect";
4206 | _ ->
4207 match state.ranchors with
4208 | [] -> raise Wsi.Quit
4209 | (path, password, anchor) :: rest ->
4210 state.ranchors <- rest;
4211 state.anchor <- anchor;
4212 opendoc path password
4213 end;
4215 | 0xff08 -> (* backspace *)
4216 let y = getnav ~-1 in
4217 gotoy_and_clear_text y
4219 | 111 -> (* o *)
4220 enteroutlinemode ()
4222 | 117 -> (* u *)
4223 state.rects <- [];
4224 state.text <- "";
4225 G.postRedisplay "dehighlight";
4227 | 47 | 63 -> (* / ? *)
4228 let ondone isforw s =
4229 cbput state.hists.pat s;
4230 state.searchpattern <- s;
4231 search s isforw
4233 let s = String.create 1 in
4234 s.[0] <- Char.chr key;
4235 enttext (s, "", Some (onhist state.hists.pat),
4236 textentry, ondone (key = 47))
4238 | 43 | 0xffab when Wsi.withctrl mask -> (* + *)
4239 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4240 setzoom (conf.zoom +. incr)
4242 | 43 | 0xffab -> (* + *)
4243 let ondone s =
4244 let n =
4245 try int_of_string s with exc ->
4246 state.text <- Printf.sprintf "bad integer `%s': %s"
4247 s (Printexc.to_string exc);
4248 max_int
4250 if n != max_int
4251 then (
4252 conf.pagebias <- n;
4253 state.text <- "page bias is now " ^ string_of_int n;
4256 enttext ("page bias: ", "", None, intentry, ondone)
4258 | 45 | 0xffad when Wsi.withctrl mask -> (* - *)
4259 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4260 setzoom (max 0.01 (conf.zoom -. decr))
4262 | 45 | 0xffad -> (* - *)
4263 let ondone msg = state.text <- msg in
4264 enttext (
4265 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4266 optentry state.mode, ondone
4269 | 48 when Wsi.withctrl mask -> (* 0 *)
4270 setzoom 1.0
4272 | 49 when Wsi.withctrl mask -> (* 1 *)
4273 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4274 if zoom < 1.0
4275 then setzoom zoom
4277 | 0xffc6 -> (* f9 *)
4278 togglebirdseye ()
4280 | 57 when Wsi.withctrl mask -> (* ctrl-9 *)
4281 togglebirdseye ()
4283 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4284 when not (Wsi.withctrl mask) -> (* 0..9 *)
4285 let ondone s =
4286 let n =
4287 try int_of_string s with exc ->
4288 state.text <- Printf.sprintf "bad integer `%s': %s"
4289 s (Printexc.to_string exc);
4292 if n >= 0
4293 then (
4294 addnav ();
4295 cbput state.hists.pag (string_of_int n);
4296 gotopage1 (n + conf.pagebias - 1) 0;
4299 let pageentry text key =
4300 match Char.unsafe_chr key with
4301 | 'g' -> TEdone text
4302 | _ -> intentry text key
4304 let text = "x" in text.[0] <- Char.chr key;
4305 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4307 | 98 -> (* b *)
4308 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4309 reshape conf.winw conf.winh;
4311 | 108 -> (* l *)
4312 conf.hlinks <- not conf.hlinks;
4313 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4314 G.postRedisplay "toggle highlightlinks";
4316 | 97 -> (* a *)
4317 begin match state.autoscroll with
4318 | Some step ->
4319 conf.autoscrollstep <- step;
4320 state.autoscroll <- None
4321 | None ->
4322 if conf.autoscrollstep = 0
4323 then state.autoscroll <- Some 1
4324 else state.autoscroll <- Some conf.autoscrollstep
4327 | 80 -> (* P *)
4328 conf.presentation <- not conf.presentation;
4329 if conf.presentation
4330 then (
4331 if not conf.scrollbarinpm
4332 then state.scrollw <- 0;
4334 else
4335 state.scrollw <- conf.scrollbw;
4337 showtext ' ' ("presentation mode " ^
4338 if conf.presentation then "on" else "off");
4339 state.anchor <- getanchor ();
4340 represent ()
4342 | 102 -> (* f *)
4343 begin match state.fullscreen with
4344 | None ->
4345 state.fullscreen <- Some (conf.winw, conf.winh);
4346 Wsi.fullscreen ()
4347 | Some (w, h) ->
4348 state.fullscreen <- None;
4349 doreshape w h
4352 | 103 -> (* g *)
4353 gotoy_and_clear_text 0
4355 | 71 -> (* G *)
4356 gotopage1 (state.pagecount - 1) 0
4358 | 112 | 78 -> (* p|N *)
4359 search state.searchpattern false
4361 | 110 -> (* n *)
4362 search state.searchpattern true
4364 | 116 -> (* t *)
4365 begin match state.layout with
4366 | [] -> ()
4367 | l :: _ ->
4368 gotoy_and_clear_text (getpagey l.pageno)
4371 | 32 -> (* ' ' *)
4372 begin match List.rev state.layout with
4373 | [] -> ()
4374 | l :: _ ->
4375 let pageno = min (l.pageno+1) (state.pagecount-1) in
4376 gotoy_and_clear_text (getpagey pageno)
4379 | 0xff9f | 0xffff -> (* delete *)
4380 begin match state.layout with
4381 | [] -> ()
4382 | l :: _ ->
4383 let pageno = max 0 (l.pageno-1) in
4384 gotoy_and_clear_text (getpagey pageno)
4387 | 61 -> (* = *)
4388 showtext ' ' (describe_location ());
4390 | 119 -> (* w *)
4391 begin match state.layout with
4392 | [] -> ()
4393 | l :: _ ->
4394 doreshape (l.pagew + state.scrollw) l.pageh;
4395 G.postRedisplay "w"
4398 | 39 -> (* ' *)
4399 enterbookmarkmode ()
4401 | 104 -> (* h *)
4402 enterhelpmode ()
4404 | 105 -> (* i *)
4405 enterinfomode ()
4407 | 101 when conf.redirectstderr -> (* e *)
4408 entermsgsmode ()
4410 | 109 -> (* m *)
4411 let ondone s =
4412 match state.layout with
4413 | l :: _ ->
4414 state.bookmarks <-
4415 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4416 :: state.bookmarks
4417 | _ -> ()
4419 enttext ("bookmark: ", "", None, textentry, ondone)
4421 | 126 -> (* ~ *)
4422 quickbookmark ();
4423 showtext ' ' "Quick bookmark added";
4425 | 122 -> (* z *)
4426 begin match state.layout with
4427 | l :: _ ->
4428 let rect = getpdimrect l.pagedimno in
4429 let w, h =
4430 if conf.crophack
4431 then
4432 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4433 truncate (1.2 *. (rect.(3) -. rect.(0))))
4434 else
4435 (truncate (rect.(1) -. rect.(0)),
4436 truncate (rect.(3) -. rect.(0)))
4438 let w = truncate ((float w)*.conf.zoom)
4439 and h = truncate ((float h)*.conf.zoom) in
4440 if w != 0 && h != 0
4441 then (
4442 state.anchor <- getanchor ();
4443 doreshape (w + state.scrollw) (h + conf.interpagespace)
4445 G.postRedisplay "z";
4447 | [] -> ()
4450 | 50 when Wsi.withctrl mask -> (* ctrl-2 *)
4451 let maxw = getmaxw () in
4452 if maxw > 0.0
4453 then setzoom (maxw /. float conf.winw)
4455 | 60 | 62 -> (* < > *)
4456 reqlayout (conf.angle + (if key = 60 then 30 else -30)) conf.proportional
4458 | 91 | 93 -> (* [ ] *)
4459 conf.colorscale <-
4460 bound (conf.colorscale +. (if key = 92 then 0.1 else -0.1)) 0.0 1.0
4462 G.postRedisplay "brightness";
4464 | 107 | 0xff52 -> (* k up *)
4465 begin match state.autoscroll with
4466 | None ->
4467 begin match state.mode with
4468 | Birdseye beye -> upbirdseye 1 beye
4469 | _ ->
4470 if Wsi.withctrl mask
4471 then gotoy (-clamp (conf.winh/2))
4472 else gotoy (clamp (-conf.scrollstep))
4474 | Some n ->
4475 setautoscrollspeed n false
4478 | 106 | 0xff54 -> (* j down *)
4479 begin match state.autoscroll with
4480 | None ->
4481 begin match state.mode with
4482 | Birdseye beye -> downbirdseye 1 beye
4483 | _ ->
4484 if Wsi.withctrl mask
4485 then gotoy_and_clear_text (clamp (conf.winh/2))
4486 else gotoy_and_clear_text (clamp conf.scrollstep)
4488 | Some n ->
4489 setautoscrollspeed n true
4492 | 0xff51 | 0xff53 (* left / right *)
4493 when Wsi.withctrl mask && conf.zoom > 1.0 ->
4494 let dx =
4495 if Wsi.withctrl mask
4496 then conf.winw / 2
4497 else 10
4499 let dx = if key = 0xff51 then dx else -dx in
4500 state.x <- state.x + dx;
4501 gotoy_and_clear_text state.y
4503 | 0xff51 | 0xff53 when mask = 0 -> (* left / right *)
4504 state.text <- "";
4505 G.postRedisplay "lef/right"
4507 | 0xff55 -> (* prior *)
4508 let y =
4509 if Wsi.withctrl mask
4510 then
4511 match state.layout with
4512 | [] -> state.y
4513 | l :: _ -> state.y - l.pagey
4514 else
4515 clamp (-conf.winh)
4517 gotoghyll y
4519 | 0xff56 -> (* next *)
4520 let y =
4521 if Wsi.withctrl mask
4522 then
4523 match List.rev state.layout with
4524 | [] -> state.y
4525 | l :: _ -> getpagey l.pageno
4526 else
4527 clamp conf.winh
4529 gotoghyll y
4531 | 0xff50 -> gotoghyll 0
4532 | 0xff57 -> gotoghyll (clamp state.maxy)
4533 | 0xff53 when Wsi.withalt mask ->
4534 gotoghyll (getnav ~-1)
4535 | 0xff51 when Wsi.withalt mask ->
4536 gotoghyll (getnav 1)
4538 | 114 -> (* r *)
4539 state.anchor <- getanchor ();
4540 opendoc state.path state.password
4542 | 76 -> (* L *)
4543 launchpath ()
4545 | 118 when conf.debug -> (* v *)
4546 state.rects <- [];
4547 List.iter (fun l ->
4548 match getopaque l.pageno with
4549 | None -> ()
4550 | Some opaque ->
4551 let x0, y0, x1, y1 = pagebbox opaque in
4552 let a,b = float x0, float y0 in
4553 let c,d = float x1, float y0 in
4554 let e,f = float x1, float y1 in
4555 let h,j = float x0, float y1 in
4556 let rect = (a,b,c,d,e,f,h,j) in
4557 debugrect rect;
4558 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4559 ) state.layout;
4560 G.postRedisplay "v";
4562 | _ ->
4563 vlog "huh? %d" key
4566 let keyboard key mask =
4567 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4568 then wcmd "interrupt" []
4569 else state.uioh <- state.uioh#key key mask
4572 let birdseyekeyboard key mask
4573 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4574 let incr =
4575 match conf.columns with
4576 | None -> 1
4577 | Some ((c, _, _), _) -> c
4579 match key with
4580 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4581 let y, h = getpageyh pageno in
4582 let top = (conf.winh - h) / 2 in
4583 gotoy (max 0 (y - top))
4584 | 0xff0d -> leavebirdseye beye false
4585 | 0xff1b -> leavebirdseye beye true (* escape *)
4586 | 0xff52 -> upbirdseye incr beye (* prior *)
4587 | 0xff54 -> downbirdseye incr beye (* next *)
4588 | 0xff51 -> upbirdseye 1 beye (* up *)
4589 | 0xff53 -> downbirdseye 1 beye (* down *)
4591 | 0xff55 ->
4592 begin match state.layout with
4593 | l :: _ ->
4594 if l.pagey != 0
4595 then (
4596 state.mode <- Birdseye (
4597 oconf, leftx, l.pageno, hooverpageno, anchor
4599 gotopage1 l.pageno 0;
4601 else (
4602 let layout = layout (state.y-conf.winh) conf.winh in
4603 match layout with
4604 | [] -> gotoy (clamp (-conf.winh))
4605 | l :: _ ->
4606 state.mode <- Birdseye (
4607 oconf, leftx, l.pageno, hooverpageno, anchor
4609 gotopage1 l.pageno 0
4612 | [] -> gotoy (clamp (-conf.winh))
4613 end;
4615 | 0xff56 ->
4616 begin match List.rev state.layout with
4617 | l :: _ ->
4618 let layout = layout (state.y + conf.winh) conf.winh in
4619 begin match layout with
4620 | [] ->
4621 let incr = l.pageh - l.pagevh in
4622 if incr = 0
4623 then (
4624 state.mode <-
4625 Birdseye (
4626 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4628 G.postRedisplay "birdseye pagedown";
4630 else gotoy (clamp (incr + conf.interpagespace*2));
4632 | l :: _ ->
4633 state.mode <-
4634 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4635 gotopage1 l.pageno 0;
4638 | [] -> gotoy (clamp conf.winh)
4639 end;
4641 | 0xff50 ->
4642 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4643 gotopage1 0 0
4645 | 0xff57 ->
4646 let pageno = state.pagecount - 1 in
4647 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4648 if not (pagevisible state.layout pageno)
4649 then
4650 let h =
4651 match List.rev state.pdims with
4652 | [] -> conf.winh
4653 | (_, _, h, _) :: _ -> h
4655 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4656 else G.postRedisplay "birdseye end";
4657 | _ -> viewkeyboard key mask
4660 let drawpage l =
4661 let color =
4662 match state.mode with
4663 | Textentry _ -> scalecolor 0.4
4664 | View -> scalecolor 1.0
4665 | Birdseye (_, _, pageno, hooverpageno, _) ->
4666 if l.pageno = hooverpageno
4667 then scalecolor 0.9
4668 else (
4669 if l.pageno = pageno
4670 then scalecolor 1.0
4671 else scalecolor 0.8
4674 drawtiles l color;
4675 begin match getopaque l.pageno with
4676 | Some opaque ->
4677 if tileready l l.pagex l.pagey
4678 then
4679 let x = l.pagedispx - l.pagex
4680 and y = l.pagedispy - l.pagey in
4681 postprocess opaque conf.hlinks x y;
4683 | _ -> ()
4684 end;
4687 let scrollindicator () =
4688 let sbw, ph, sh = state.uioh#scrollph in
4689 let sbh, pw, sw = state.uioh#scrollpw in
4691 GlDraw.color (0.64, 0.64, 0.64);
4692 GlDraw.rect
4693 (float (conf.winw - sbw), 0.)
4694 (float conf.winw, float conf.winh)
4696 GlDraw.rect
4697 (0., float (conf.winh - sbh))
4698 (float (conf.winw - state.scrollw - 1), float conf.winh)
4700 GlDraw.color (0.0, 0.0, 0.0);
4702 GlDraw.rect
4703 (float (conf.winw - sbw), ph)
4704 (float conf.winw, ph +. sh)
4706 GlDraw.rect
4707 (pw, float (conf.winh - sbh))
4708 (pw +. sw, float conf.winh)
4712 let showsel () =
4713 match state.mstate with
4714 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4717 | Msel ((x0, y0), (x1, y1)) ->
4718 let rec loop = function
4719 | l :: ls ->
4720 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4721 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4722 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4723 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4724 then
4725 match getopaque l.pageno with
4726 | Some opaque ->
4727 let dx, dy = pagetranslatepoint l 0 0 in
4728 let x0 = x0 + dx
4729 and y0 = y0 + dy
4730 and x1 = x1 + dx
4731 and y1 = y1 + dy in
4732 GlMat.mode `modelview;
4733 GlMat.push ();
4734 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4735 seltext opaque (x0, y0, x1, y1);
4736 GlMat.pop ();
4737 | _ -> ()
4738 else loop ls
4739 | [] -> ()
4741 loop state.layout
4744 let showrects () =
4745 Gl.enable `blend;
4746 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4747 GlDraw.polygon_mode `both `fill;
4748 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4749 List.iter
4750 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4751 List.iter (fun l ->
4752 if l.pageno = pageno
4753 then (
4754 let dx = float (l.pagedispx - l.pagex) in
4755 let dy = float (l.pagedispy - l.pagey) in
4756 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4757 GlDraw.begins `quads;
4759 GlDraw.vertex2 (x0+.dx, y0+.dy);
4760 GlDraw.vertex2 (x1+.dx, y1+.dy);
4761 GlDraw.vertex2 (x2+.dx, y2+.dy);
4762 GlDraw.vertex2 (x3+.dx, y3+.dy);
4764 GlDraw.ends ();
4766 ) state.layout
4767 ) state.rects
4769 Gl.disable `blend;
4772 let display () =
4773 GlClear.color (scalecolor2 conf.bgcolor);
4774 GlClear.clear [`color];
4775 List.iter drawpage state.layout;
4776 showrects ();
4777 showsel ();
4778 state.uioh#display;
4779 scrollindicator ();
4780 begin match state.mstate with
4781 | Mzoomrect ((x0, y0), (x1, y1)) ->
4782 Gl.enable `blend;
4783 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4784 GlDraw.polygon_mode `both `fill;
4785 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4786 GlDraw.rect (float x0, float y0)
4787 (float x1, float y1);
4788 Gl.disable `blend;
4789 | _ -> ()
4790 end;
4791 enttext ();
4792 if conf.updatecurs
4793 then (
4794 let mx, my = state.mpos in
4795 updateunder mx my;
4797 Wsi.swapb ();
4800 let zoomrect x y x1 y1 =
4801 let x0 = min x x1
4802 and x1 = max x x1
4803 and y0 = min y y1 in
4804 gotoy (state.y + y0);
4805 state.anchor <- getanchor ();
4806 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4807 let margin =
4808 if state.w < conf.winw - state.scrollw
4809 then (conf.winw - state.scrollw - state.w) / 2
4810 else 0
4812 state.x <- (state.x + margin) - x0;
4813 setzoom zoom;
4814 Wsi.setcursor Wsi.CURSOR_INHERIT;
4815 state.mstate <- Mnone;
4818 let scrollx x =
4819 let winw = conf.winw - state.scrollw - 1 in
4820 let s = float x /. float winw in
4821 let destx = truncate (float (state.w + winw) *. s) in
4822 state.x <- winw - destx;
4823 gotoy_and_clear_text state.y;
4824 state.mstate <- Mscrollx;
4827 let scrolly y =
4828 let s = float y /. float conf.winh in
4829 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4830 gotoy_and_clear_text desty;
4831 state.mstate <- Mscrolly;
4834 let viewmouse button down x y mask =
4835 match button with
4836 | n when (n == 4 || n == 5) && not down ->
4837 if Wsi.withctrl mask
4838 then (
4839 match state.mstate with
4840 | Mzoom (oldn, i) ->
4841 if oldn = n
4842 then (
4843 if i = 2
4844 then
4845 let incr =
4846 match n with
4847 | 5 ->
4848 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4849 | _ ->
4850 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4852 let zoom = conf.zoom -. incr in
4853 setzoom zoom;
4854 state.mstate <- Mzoom (n, 0);
4855 else
4856 state.mstate <- Mzoom (n, i+1);
4858 else state.mstate <- Mzoom (n, 0)
4860 | _ -> state.mstate <- Mzoom (n, 0)
4862 else (
4863 match state.autoscroll with
4864 | Some step -> setautoscrollspeed step (n=4)
4865 | None ->
4866 let incr =
4867 if n = 4
4868 then -conf.scrollstep
4869 else conf.scrollstep
4871 let incr = incr * 2 in
4872 let y = clamp incr in
4873 gotoy_and_clear_text y
4876 | 1 when Wsi.withctrl mask ->
4877 if down
4878 then (
4879 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
4880 state.mstate <- Mpan (x, y)
4882 else
4883 state.mstate <- Mnone
4885 | 3 ->
4886 if down
4887 then (
4888 Wsi.setcursor Wsi.CURSOR_CYCLE;
4889 let p = (x, y) in
4890 state.mstate <- Mzoomrect (p, p)
4892 else (
4893 match state.mstate with
4894 | Mzoomrect ((x0, y0), _) ->
4895 if abs (x-x0) > 10 && abs (y - y0) > 10
4896 then zoomrect x0 y0 x y
4897 else (
4898 state.mstate <- Mnone;
4899 Wsi.setcursor Wsi.CURSOR_INHERIT;
4900 G.postRedisplay "kill accidental zoom rect";
4902 | _ ->
4903 Wsi.setcursor Wsi.CURSOR_INHERIT;
4904 state.mstate <- Mnone
4907 | 1 when x > conf.winw - state.scrollw ->
4908 if down
4909 then
4910 let _, position, sh = state.uioh#scrollph in
4911 if y > truncate position && y < truncate (position +. sh)
4912 then state.mstate <- Mscrolly
4913 else scrolly y
4914 else
4915 state.mstate <- Mnone
4917 | 1 when y > conf.winh - state.hscrollh ->
4918 if down
4919 then
4920 let _, position, sw = state.uioh#scrollpw in
4921 if x > truncate position && x < truncate (position +. sw)
4922 then state.mstate <- Mscrollx
4923 else scrollx x
4924 else
4925 state.mstate <- Mnone
4927 | 1 ->
4928 let dest = if down then getunder x y else Unone in
4929 begin match dest with
4930 | Ulinkgoto (pageno, top) ->
4931 if pageno >= 0
4932 then (
4933 addnav ();
4934 gotopage1 pageno top;
4937 | Ulinkuri s ->
4938 gotouri s
4940 | Uremote (filename, pageno) ->
4941 let path =
4942 if Sys.file_exists filename
4943 then filename
4944 else
4945 let dir = Filename.dirname state.path in
4946 let path = Filename.concat dir filename in
4947 if Sys.file_exists path
4948 then path
4949 else ""
4951 if String.length path > 0
4952 then (
4953 let anchor = getanchor () in
4954 let ranchor = state.path, state.password, anchor in
4955 state.anchor <- (pageno, 0.0);
4956 state.ranchors <- ranchor :: state.ranchors;
4957 opendoc path "";
4959 else showtext '!' ("Could not find " ^ filename)
4961 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
4963 | Unone when down ->
4964 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
4965 state.mstate <- Mpan (x, y);
4967 | Unone | Utext _ ->
4968 if down
4969 then (
4970 if conf.angle mod 360 = 0
4971 then (
4972 state.mstate <- Msel ((x, y), (x, y));
4973 G.postRedisplay "mouse select";
4976 else (
4977 match state.mstate with
4978 | Mnone -> ()
4980 | Mzoom _ | Mscrollx | Mscrolly ->
4981 state.mstate <- Mnone
4983 | Mzoomrect ((x0, y0), _) ->
4984 zoomrect x0 y0 x y
4986 | Mpan _ ->
4987 Wsi.setcursor Wsi.CURSOR_INHERIT;
4988 state.mstate <- Mnone
4990 | Msel ((_, y0), (_, y1)) ->
4991 let rec loop = function
4992 | [] -> ()
4993 | l :: rest ->
4994 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4995 || ((y1 >= l.pagedispy
4996 && y1 <= (l.pagedispy + l.pagevh)))
4997 then
4998 match getopaque l.pageno with
4999 | Some opaque ->
5000 copysel conf.selcmd opaque;
5001 G.postRedisplay "copysel"
5002 | _ -> ()
5003 else loop rest
5005 loop state.layout;
5006 Wsi.setcursor Wsi.CURSOR_INHERIT;
5007 state.mstate <- Mnone;
5011 | _ -> ()
5014 let birdseyemouse button down x y mask
5015 (conf, leftx, _, hooverpageno, anchor) =
5016 match button with
5017 | 1 when down ->
5018 let rec loop = function
5019 | [] -> ()
5020 | l :: rest ->
5021 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5022 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5023 then (
5024 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5026 else loop rest
5028 loop state.layout
5029 | 3 -> ()
5030 | _ -> viewmouse button down x y mask
5033 let mouse button down x y mask =
5034 state.uioh <- state.uioh#button button down x y mask;
5037 let motion ~x ~y =
5038 state.uioh <- state.uioh#motion x y
5041 let pmotion ~x ~y =
5042 state.uioh <- state.uioh#pmotion x y;
5045 let uioh = object
5046 method display = ()
5048 method key key mask =
5049 begin match state.mode with
5050 | Textentry textentry -> textentrykeyboard key mask textentry
5051 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5052 | View -> viewkeyboard key mask
5053 end;
5054 state.uioh
5056 method button button bstate x y mask =
5057 begin match state.mode with
5058 | View -> viewmouse button bstate x y mask
5059 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5060 | Textentry _ -> ()
5061 end;
5062 state.uioh
5064 method motion x y =
5065 begin match state.mode with
5066 | Textentry _ -> ()
5067 | View | Birdseye _ ->
5068 match state.mstate with
5069 | Mzoom _ | Mnone -> ()
5071 | Mpan (x0, y0) ->
5072 let dx = x - x0
5073 and dy = y0 - y in
5074 state.mstate <- Mpan (x, y);
5075 if conf.zoom > 1.0 then state.x <- state.x + dx;
5076 let y = clamp dy in
5077 gotoy_and_clear_text y
5079 | Msel (a, _) ->
5080 state.mstate <- Msel (a, (x, y));
5081 G.postRedisplay "motion select";
5083 | Mscrolly ->
5084 let y = min conf.winh (max 0 y) in
5085 scrolly y
5087 | Mscrollx ->
5088 let x = min conf.winw (max 0 x) in
5089 scrollx x
5091 | Mzoomrect (p0, _) ->
5092 state.mstate <- Mzoomrect (p0, (x, y));
5093 G.postRedisplay "motion zoomrect";
5094 end;
5095 state.uioh
5097 method pmotion x y =
5098 begin match state.mode with
5099 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5100 let rec loop = function
5101 | [] ->
5102 if hooverpageno != -1
5103 then (
5104 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5105 G.postRedisplay "pmotion birdseye no hoover";
5107 | l :: rest ->
5108 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5109 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5110 then (
5111 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5112 G.postRedisplay "pmotion birdseye hoover";
5114 else loop rest
5116 loop state.layout
5118 | Textentry _ -> ()
5120 | View ->
5121 match state.mstate with
5122 | Mnone -> updateunder x y
5123 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5125 end;
5126 state.uioh
5128 method infochanged _ = ()
5130 method scrollph =
5131 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5132 let p, h = scrollph state.y maxy in
5133 state.scrollw, p, h
5135 method scrollpw =
5136 let winw = conf.winw - state.scrollw - 1 in
5137 let fwinw = float winw in
5138 let sw =
5139 let sw = fwinw /. float state.w in
5140 let sw = fwinw *. sw in
5141 max sw (float conf.scrollh)
5143 let position, sw =
5144 let f = state.w+winw in
5145 let r = float (winw-state.x) /. float f in
5146 let p = fwinw *. r in
5147 p-.sw/.2., sw
5149 let sw =
5150 if position +. sw > fwinw
5151 then fwinw -. position
5152 else sw
5154 state.hscrollh, position, sw
5155 end;;
5157 module Config =
5158 struct
5159 open Parser
5161 let fontpath = ref "";;
5163 let unent s =
5164 let l = String.length s in
5165 let b = Buffer.create l in
5166 unent b s 0 l;
5167 Buffer.contents b;
5170 let home =
5171 try Sys.getenv "HOME"
5172 with exn ->
5173 prerr_endline
5174 ("Can not determine home directory location: " ^
5175 Printexc.to_string exn);
5179 let config_of c attrs =
5180 let apply c k v =
5182 match k with
5183 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5184 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5185 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5186 | "preload" -> { c with preload = bool_of_string v }
5187 | "page-bias" -> { c with pagebias = int_of_string v }
5188 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5189 | "auto-scroll-step" ->
5190 { c with autoscrollstep = max 0 (int_of_string v) }
5191 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5192 | "crop-hack" -> { c with crophack = bool_of_string v }
5193 | "throttle" ->
5194 let mw =
5195 match String.lowercase v with
5196 | "true" -> Some infinity
5197 | "false" -> None
5198 | f -> Some (float_of_string f)
5200 { c with maxwait = mw}
5201 | "highlight-links" -> { c with hlinks = bool_of_string v }
5202 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5203 | "vertical-margin" ->
5204 { c with interpagespace = max 0 (int_of_string v) }
5205 | "zoom" ->
5206 let zoom = float_of_string v /. 100. in
5207 let zoom = max zoom 0.0 in
5208 { c with zoom = zoom }
5209 | "presentation" -> { c with presentation = bool_of_string v }
5210 | "rotation-angle" -> { c with angle = int_of_string v }
5211 | "width" -> { c with winw = max 20 (int_of_string v) }
5212 | "height" -> { c with winh = max 20 (int_of_string v) }
5213 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5214 | "proportional-display" -> { c with proportional = bool_of_string v }
5215 | "pixmap-cache-size" ->
5216 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5217 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5218 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5219 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5220 | "persistent-location" -> { c with jumpback = bool_of_string v }
5221 | "background-color" -> { c with bgcolor = color_of_string v }
5222 | "scrollbar-in-presentation" ->
5223 { c with scrollbarinpm = bool_of_string v }
5224 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5225 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5226 | "mupdf-store-size" ->
5227 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5228 | "checkers" -> { c with checkers = bool_of_string v }
5229 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5230 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5231 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5232 | "uri-launcher" -> { c with urilauncher = unent v }
5233 | "path-launcher" -> { c with pathlauncher = unent v }
5234 | "color-space" -> { c with colorspace = colorspace_of_string v }
5235 | "invert-colors" -> { c with invert = bool_of_string v }
5236 | "brightness" -> { c with colorscale = float_of_string v }
5237 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5238 | "ghyllscroll" ->
5239 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5240 | "columns" ->
5241 let nab = columns_of_string v in
5242 { c with columns = Some (nab, [||]) }
5243 | "birds-eye-columns" ->
5244 { c with beyecolumns = Some (max (int_of_string v) 2) }
5245 | "selection-command" -> { c with selcmd = unent v }
5246 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5247 | _ -> c
5248 with exn ->
5249 prerr_endline ("Error processing attribute (`" ^
5250 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5253 let rec fold c = function
5254 | [] -> c
5255 | (k, v) :: rest ->
5256 let c = apply c k v in
5257 fold c rest
5259 fold c attrs;
5262 let fromstring f pos n v d =
5263 try f v
5264 with exn ->
5265 dolog "Error processing attribute (%S=%S) at %d\n%s"
5266 n v pos (Printexc.to_string exn)
5271 let bookmark_of attrs =
5272 let rec fold title page rely = function
5273 | ("title", v) :: rest -> fold v page rely rest
5274 | ("page", v) :: rest -> fold title v rely rest
5275 | ("rely", v) :: rest -> fold title page v rest
5276 | _ :: rest -> fold title page rely rest
5277 | [] -> title, page, rely
5279 fold "invalid" "0" "0" attrs
5282 let doc_of attrs =
5283 let rec fold path page rely pan = function
5284 | ("path", v) :: rest -> fold v page rely pan rest
5285 | ("page", v) :: rest -> fold path v rely pan rest
5286 | ("rely", v) :: rest -> fold path page v pan rest
5287 | ("pan", v) :: rest -> fold path page rely v rest
5288 | _ :: rest -> fold path page rely pan rest
5289 | [] -> path, page, rely, pan
5291 fold "" "0" "0" "0" attrs
5294 let setconf dst src =
5295 dst.scrollbw <- src.scrollbw;
5296 dst.scrollh <- src.scrollh;
5297 dst.icase <- src.icase;
5298 dst.preload <- src.preload;
5299 dst.pagebias <- src.pagebias;
5300 dst.verbose <- src.verbose;
5301 dst.scrollstep <- src.scrollstep;
5302 dst.maxhfit <- src.maxhfit;
5303 dst.crophack <- src.crophack;
5304 dst.autoscrollstep <- src.autoscrollstep;
5305 dst.maxwait <- src.maxwait;
5306 dst.hlinks <- src.hlinks;
5307 dst.underinfo <- src.underinfo;
5308 dst.interpagespace <- src.interpagespace;
5309 dst.zoom <- src.zoom;
5310 dst.presentation <- src.presentation;
5311 dst.angle <- src.angle;
5312 dst.winw <- src.winw;
5313 dst.winh <- src.winh;
5314 dst.savebmarks <- src.savebmarks;
5315 dst.memlimit <- src.memlimit;
5316 dst.proportional <- src.proportional;
5317 dst.texcount <- src.texcount;
5318 dst.sliceheight <- src.sliceheight;
5319 dst.thumbw <- src.thumbw;
5320 dst.jumpback <- src.jumpback;
5321 dst.bgcolor <- src.bgcolor;
5322 dst.scrollbarinpm <- src.scrollbarinpm;
5323 dst.tilew <- src.tilew;
5324 dst.tileh <- src.tileh;
5325 dst.mustoresize <- src.mustoresize;
5326 dst.checkers <- src.checkers;
5327 dst.aalevel <- src.aalevel;
5328 dst.trimmargins <- src.trimmargins;
5329 dst.trimfuzz <- src.trimfuzz;
5330 dst.urilauncher <- src.urilauncher;
5331 dst.colorspace <- src.colorspace;
5332 dst.invert <- src.invert;
5333 dst.colorscale <- src.colorscale;
5334 dst.redirectstderr <- src.redirectstderr;
5335 dst.ghyllscroll <- src.ghyllscroll;
5336 dst.columns <- src.columns;
5337 dst.beyecolumns <- src.beyecolumns;
5338 dst.selcmd <- src.selcmd;
5339 dst.updatecurs <- src.updatecurs;
5340 dst.pathlauncher <- src.pathlauncher;
5343 let get s =
5344 let h = Hashtbl.create 10 in
5345 let dc = { defconf with angle = defconf.angle } in
5346 let rec toplevel v t spos _ =
5347 match t with
5348 | Vdata | Vcdata | Vend -> v
5349 | Vopen ("llppconfig", _, closed) ->
5350 if closed
5351 then v
5352 else { v with f = llppconfig }
5353 | Vopen _ ->
5354 error "unexpected subelement at top level" s spos
5355 | Vclose _ -> error "unexpected close at top level" s spos
5357 and llppconfig v t spos _ =
5358 match t with
5359 | Vdata | Vcdata -> v
5360 | Vend -> error "unexpected end of input in llppconfig" s spos
5361 | Vopen ("defaults", attrs, closed) ->
5362 let c = config_of dc attrs in
5363 setconf dc c;
5364 if closed
5365 then v
5366 else { v with f = skip "defaults" (fun () -> v) }
5368 | Vopen ("ui-font", attrs, closed) ->
5369 let rec getsize size = function
5370 | [] -> size
5371 | ("size", v) :: rest ->
5372 let size =
5373 fromstring int_of_string spos "size" v fstate.fontsize in
5374 getsize size rest
5375 | l -> getsize size l
5377 fstate.fontsize <- getsize fstate.fontsize attrs;
5378 if closed
5379 then v
5380 else { v with f = uifont (Buffer.create 10) }
5382 | Vopen ("doc", attrs, closed) ->
5383 let pathent, spage, srely, span = doc_of attrs in
5384 let path = unent pathent
5385 and pageno = fromstring int_of_string spos "page" spage 0
5386 and rely = fromstring float_of_string spos "rely" srely 0.0
5387 and pan = fromstring int_of_string spos "pan" span 0 in
5388 let c = config_of dc attrs in
5389 let anchor = (pageno, rely) in
5390 if closed
5391 then (Hashtbl.add h path (c, [], pan, anchor); v)
5392 else { v with f = doc path pan anchor c [] }
5394 | Vopen _ ->
5395 error "unexpected subelement in llppconfig" s spos
5397 | Vclose "llppconfig" -> { v with f = toplevel }
5398 | Vclose _ -> error "unexpected close in llppconfig" s spos
5400 and uifont b v t spos epos =
5401 match t with
5402 | Vdata | Vcdata ->
5403 Buffer.add_substring b s spos (epos - spos);
5405 | Vopen (_, _, _) ->
5406 error "unexpected subelement in ui-font" s spos
5407 | Vclose "ui-font" ->
5408 if String.length !fontpath = 0
5409 then fontpath := Buffer.contents b;
5410 { v with f = llppconfig }
5411 | Vclose _ -> error "unexpected close in ui-font" s spos
5412 | Vend -> error "unexpected end of input in ui-font" s spos
5414 and doc path pan anchor c bookmarks v t spos _ =
5415 match t with
5416 | Vdata | Vcdata -> v
5417 | Vend -> error "unexpected end of input in doc" s spos
5418 | Vopen ("bookmarks", _, closed) ->
5419 if closed
5420 then v
5421 else { v with f = pbookmarks path pan anchor c bookmarks }
5423 | Vopen (_, _, _) ->
5424 error "unexpected subelement in doc" s spos
5426 | Vclose "doc" ->
5427 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5428 { v with f = llppconfig }
5430 | Vclose _ -> error "unexpected close in doc" s spos
5432 and pbookmarks path pan anchor c bookmarks v t spos _ =
5433 match t with
5434 | Vdata | Vcdata -> v
5435 | Vend -> error "unexpected end of input in bookmarks" s spos
5436 | Vopen ("item", attrs, closed) ->
5437 let titleent, spage, srely = bookmark_of attrs in
5438 let page = fromstring int_of_string spos "page" spage 0
5439 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5440 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5441 if closed
5442 then { v with f = pbookmarks path pan anchor c bookmarks }
5443 else
5444 let f () = v in
5445 { v with f = skip "item" f }
5447 | Vopen _ ->
5448 error "unexpected subelement in bookmarks" s spos
5450 | Vclose "bookmarks" ->
5451 { v with f = doc path pan anchor c bookmarks }
5453 | Vclose _ -> error "unexpected close in bookmarks" s spos
5455 and skip tag f v t spos _ =
5456 match t with
5457 | Vdata | Vcdata -> v
5458 | Vend ->
5459 error ("unexpected end of input in skipped " ^ tag) s spos
5460 | Vopen (tag', _, closed) ->
5461 if closed
5462 then v
5463 else
5464 let f' () = { v with f = skip tag f } in
5465 { v with f = skip tag' f' }
5466 | Vclose ctag ->
5467 if tag = ctag
5468 then f ()
5469 else error ("unexpected close in skipped " ^ tag) s spos
5472 parse { f = toplevel; accu = () } s;
5473 h, dc;
5476 let do_load f ic =
5478 let len = in_channel_length ic in
5479 let s = String.create len in
5480 really_input ic s 0 len;
5481 f s;
5482 with
5483 | Parse_error (msg, s, pos) ->
5484 let subs = subs s pos in
5485 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5486 failwith ("parse error: " ^ s)
5488 | exn ->
5489 failwith ("config load error: " ^ Printexc.to_string exn)
5492 let defconfpath =
5493 let dir =
5495 let dir = Filename.concat home ".config" in
5496 if Sys.is_directory dir then dir else home
5497 with _ -> home
5499 Filename.concat dir "llpp.conf"
5502 let confpath = ref defconfpath;;
5504 let load1 f =
5505 if Sys.file_exists !confpath
5506 then
5507 match
5508 (try Some (open_in_bin !confpath)
5509 with exn ->
5510 prerr_endline
5511 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5512 Printexc.to_string exn);
5513 None
5515 with
5516 | Some ic ->
5517 begin try
5518 f (do_load get ic)
5519 with exn ->
5520 prerr_endline
5521 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5522 Printexc.to_string exn);
5523 end;
5524 close_in ic;
5526 | None -> ()
5527 else
5528 f (Hashtbl.create 0, defconf)
5531 let load () =
5532 let f (h, dc) =
5533 let pc, pb, px, pa =
5535 Hashtbl.find h (Filename.basename state.path)
5536 with Not_found -> dc, [], 0, (0, 0.0)
5538 setconf defconf dc;
5539 setconf conf pc;
5540 state.bookmarks <- pb;
5541 state.x <- px;
5542 state.scrollw <- conf.scrollbw;
5543 if conf.jumpback
5544 then state.anchor <- pa;
5545 cbput state.hists.nav pa;
5547 load1 f
5550 let add_attrs bb always dc c =
5551 let ob s a b =
5552 if always || a != b
5553 then Printf.bprintf bb "\n %s='%b'" s a
5554 and oi s a b =
5555 if always || a != b
5556 then Printf.bprintf bb "\n %s='%d'" s a
5557 and oI s a b =
5558 if always || a != b
5559 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5560 and oz s a b =
5561 if always || a <> b
5562 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5563 and oF s a b =
5564 if always || a <> b
5565 then Printf.bprintf bb "\n %s='%f'" s a
5566 and oc s a b =
5567 if always || a <> b
5568 then
5569 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5570 and oC s a b =
5571 if always || a <> b
5572 then
5573 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5574 and oR s a b =
5575 if always || a <> b
5576 then
5577 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5578 and os s a b =
5579 if always || a <> b
5580 then
5581 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5582 and og s a b =
5583 if always || a <> b
5584 then
5585 match a with
5586 | None -> ()
5587 | Some (_N, _A, _B) ->
5588 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5589 and oW s a b =
5590 if always || a <> b
5591 then
5592 let v =
5593 match a with
5594 | None -> "false"
5595 | Some f ->
5596 if f = infinity
5597 then "true"
5598 else string_of_float f
5600 Printf.bprintf bb "\n %s='%s'" s v
5601 and oco s a b =
5602 if always || a <> b
5603 then
5604 match a with
5605 | Some ((n, a, b), _) when n > 1 ->
5606 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5607 | _ -> ()
5608 and obeco s a b =
5609 if always || a <> b
5610 then
5611 match a with
5612 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5613 | _ -> ()
5615 let w, h =
5616 if always
5617 then dc.winw, dc.winh
5618 else
5619 match state.fullscreen with
5620 | Some wh -> wh
5621 | None -> c.winw, c.winh
5623 let zoom, presentation, interpagespace, maxwait =
5624 if always
5625 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5626 else
5627 match state.mode with
5628 | Birdseye (bc, _, _, _, _) ->
5629 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5630 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5632 oi "width" w dc.winw;
5633 oi "height" h dc.winh;
5634 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5635 oi "scroll-handle-height" c.scrollh dc.scrollh;
5636 ob "case-insensitive-search" c.icase dc.icase;
5637 ob "preload" c.preload dc.preload;
5638 oi "page-bias" c.pagebias dc.pagebias;
5639 oi "scroll-step" c.scrollstep dc.scrollstep;
5640 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5641 ob "max-height-fit" c.maxhfit dc.maxhfit;
5642 ob "crop-hack" c.crophack dc.crophack;
5643 oW "throttle" maxwait dc.maxwait;
5644 ob "highlight-links" c.hlinks dc.hlinks;
5645 ob "under-cursor-info" c.underinfo dc.underinfo;
5646 oi "vertical-margin" interpagespace dc.interpagespace;
5647 oz "zoom" zoom dc.zoom;
5648 ob "presentation" presentation dc.presentation;
5649 oi "rotation-angle" c.angle dc.angle;
5650 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5651 ob "proportional-display" c.proportional dc.proportional;
5652 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5653 oi "tex-count" c.texcount dc.texcount;
5654 oi "slice-height" c.sliceheight dc.sliceheight;
5655 oi "thumbnail-width" c.thumbw dc.thumbw;
5656 ob "persistent-location" c.jumpback dc.jumpback;
5657 oc "background-color" c.bgcolor dc.bgcolor;
5658 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5659 oi "tile-width" c.tilew dc.tilew;
5660 oi "tile-height" c.tileh dc.tileh;
5661 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5662 ob "checkers" c.checkers dc.checkers;
5663 oi "aalevel" c.aalevel dc.aalevel;
5664 ob "trim-margins" c.trimmargins dc.trimmargins;
5665 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5666 os "uri-launcher" c.urilauncher dc.urilauncher;
5667 os "path-launcher" c.pathlauncher dc.pathlauncher;
5668 oC "color-space" c.colorspace dc.colorspace;
5669 ob "invert-colors" c.invert dc.invert;
5670 oF "brightness" c.colorscale dc.colorscale;
5671 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5672 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
5673 oco "columns" c.columns dc.columns;
5674 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
5675 os "selection-command" c.selcmd dc.selcmd;
5676 ob "update-cursor" c.updatecurs dc.updatecurs;
5679 let save () =
5680 let uifontsize = fstate.fontsize in
5681 let bb = Buffer.create 32768 in
5682 let f (h, dc) =
5683 let dc = if conf.bedefault then conf else dc in
5684 Buffer.add_string bb "<llppconfig>\n";
5686 if String.length !fontpath > 0
5687 then
5688 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5689 uifontsize
5690 !fontpath
5691 else (
5692 if uifontsize <> 14
5693 then
5694 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5697 Buffer.add_string bb "<defaults ";
5698 add_attrs bb true dc dc;
5699 Buffer.add_string bb "/>\n";
5701 let adddoc path pan anchor c bookmarks =
5702 if bookmarks == [] && c = dc && anchor = emptyanchor
5703 then ()
5704 else (
5705 Printf.bprintf bb "<doc path='%s'"
5706 (enent path 0 (String.length path));
5708 if anchor <> emptyanchor
5709 then (
5710 let n, y = anchor in
5711 Printf.bprintf bb " page='%d'" n;
5712 if y > 1e-6
5713 then
5714 Printf.bprintf bb " rely='%f'" y
5718 if pan != 0
5719 then Printf.bprintf bb " pan='%d'" pan;
5721 add_attrs bb false dc c;
5723 begin match bookmarks with
5724 | [] -> Buffer.add_string bb "/>\n"
5725 | _ ->
5726 Buffer.add_string bb ">\n<bookmarks>\n";
5727 List.iter (fun (title, _level, (page, rely)) ->
5728 Printf.bprintf bb
5729 "<item title='%s' page='%d'"
5730 (enent title 0 (String.length title))
5731 page
5733 if rely > 1e-6
5734 then
5735 Printf.bprintf bb " rely='%f'" rely
5737 Buffer.add_string bb "/>\n";
5738 ) bookmarks;
5739 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5740 end;
5744 let pan, conf =
5745 match state.mode with
5746 | Birdseye (c, pan, _, _, _) ->
5747 let beyecolumns =
5748 match conf.columns with
5749 | Some ((c, _, _), _) -> Some c
5750 | None -> None
5751 and columns =
5752 match c.columns with
5753 | Some (c, _) -> Some (c, [||])
5754 | None -> None
5756 pan, { c with beyecolumns = beyecolumns; columns = columns }
5757 | _ -> state.x, conf
5759 let basename = Filename.basename state.path in
5760 adddoc basename pan (getanchor ())
5761 { conf with
5762 autoscrollstep =
5763 match state.autoscroll with
5764 | Some step -> step
5765 | None -> conf.autoscrollstep }
5766 (if conf.savebmarks then state.bookmarks else []);
5768 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5769 if basename <> path
5770 then adddoc path x y c bookmarks
5771 ) h;
5772 Buffer.add_string bb "</llppconfig>";
5774 load1 f;
5775 if Buffer.length bb > 0
5776 then
5778 let tmp = !confpath ^ ".tmp" in
5779 let oc = open_out_bin tmp in
5780 Buffer.output_buffer oc bb;
5781 close_out oc;
5782 Unix.rename tmp !confpath;
5783 with exn ->
5784 prerr_endline
5785 ("error while saving configuration: " ^ Printexc.to_string exn)
5787 end;;
5789 let () =
5790 Arg.parse
5791 (Arg.align
5792 [("-p", Arg.String (fun s -> state.password <- s) ,
5793 "<password> Set password");
5795 ("-f", Arg.String (fun s -> Config.fontpath := s),
5796 "<path> Set path to the user interface font");
5798 ("-c", Arg.String (fun s -> Config.confpath := s),
5799 "<path> Set path to the configuration file");
5801 ("-v", Arg.Unit (fun () ->
5802 Printf.printf
5803 "%s\nconfiguration path: %s\n"
5804 (version ())
5805 Config.defconfpath
5807 exit 0), " Print version and exit");
5810 (fun s -> state.path <- s)
5811 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5813 if String.length state.path = 0
5814 then (prerr_endline "file name missing"; exit 1);
5816 Config.load ();
5818 state.wsfd <- Wsi.init (object
5819 method display = display ()
5820 method reshape w h = reshape w h
5821 method mouse b d x y m = mouse b d x y m
5822 method motion x y = state.mpos <- (x, y); motion x y
5823 method pmotion x y = state.mpos <- (x, y); pmotion x y
5824 method key c m = keyboard c m
5825 method enter x y = state.mpos <- (x, y); pmotion x y
5826 method leave = state.mpos <- (-1, -1)
5827 end) conf.winw conf.winh;
5829 if not (
5830 List.exists GlMisc.check_extension
5831 [ "GL_ARB_texture_rectangle"
5832 ; "GL_EXT_texture_recangle"
5833 ; "GL_NV_texture_rectangle" ]
5835 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5837 let cr, sw = Unix.pipe ()
5838 and sr, cw = Unix.pipe () in
5840 cloexec cr;
5841 cloexec sw;
5842 cloexec sr;
5843 cloexec cw;
5845 setcheckers conf.checkers;
5846 redirectstderr ();
5848 init (cr, cw) (
5849 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5850 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5851 !Config.fontpath
5853 state.sr <- sr;
5854 state.sw <- sw;
5855 state.text <- "Opening " ^ state.path;
5856 setaalevel conf.aalevel;
5857 writeopen state.path state.password;
5858 state.uioh <- uioh;
5859 setfontsize fstate.fontsize;
5860 doreshape conf.winw conf.winh;
5862 let rec loop deadline =
5863 let r =
5864 match state.errfd with
5865 | None -> [state.sr; state.wsfd]
5866 | Some fd -> [state.sr; state.wsfd; fd]
5868 if state.redisplay
5869 then (
5870 state.redisplay <- false;
5871 display ();
5873 let timeout =
5874 let now = now () in
5875 if deadline > now
5876 then (
5877 if deadline = infinity
5878 then ~-.1.0
5879 else max 0.0 (deadline -. now)
5881 else 0.0
5883 let r, _, _ =
5884 try Unix.select r [] [] timeout
5885 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
5887 begin match r with
5888 | [] ->
5889 state.ghyll None;
5890 let newdeadline =
5891 match state.autoscroll with
5892 | Some step when step != 0 ->
5893 let y = state.y + step in
5894 let y =
5895 if y < 0
5896 then state.maxy
5897 else if y >= state.maxy then 0 else y
5899 gotoy y;
5900 if state.mode = View
5901 then state.text <- "";
5902 deadline +. 0.01
5903 | _ ->
5904 if state.ghyll == noghyll then infinity else deadline +. 0.01
5906 loop newdeadline
5908 | l ->
5909 let rec checkfds = function
5910 | [] -> ()
5911 | fd :: rest when fd = state.sr ->
5912 let cmd = readcmd state.sr in
5913 act cmd;
5914 checkfds rest
5916 | fd :: rest when fd = state.wsfd ->
5917 Wsi.readresp fd;
5918 checkfds rest
5920 | fd :: rest ->
5921 let s = String.create 80 in
5922 let n = Unix.read fd s 0 80 in
5923 if conf.redirectstderr
5924 then (
5925 Buffer.add_substring state.errmsgs s 0 n;
5926 state.newerrmsgs <- true;
5927 state.redisplay <- true;
5929 else (
5930 prerr_string (String.sub s 0 n);
5931 flush stderr;
5933 checkfds rest
5935 checkfds l;
5936 let newdeadline =
5937 let deadline1 =
5938 if deadline = infinity
5939 then now () +. 0.01
5940 else deadline
5942 match state.autoscroll with
5943 | Some step when step != 0 -> deadline1
5944 | _ -> if state.ghyll == noghyll then infinity else deadline1
5946 loop newdeadline
5947 end;
5950 loop infinity;
5951 with Wsi.Quit ->
5952 wcmd "quit" [];
5953 Config.save ();
5954 exit 0;