Update cursor when entering the window
[llpp.git] / main.ml
bloba85def7ecbc1b23e2d57ead096ae0bb5d9785edd
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 colorspace : colorspace
260 ; mutable invert : bool
261 ; mutable colorscale : float
262 ; mutable redirectstderr : bool
263 ; mutable ghyllscroll : (int * int * int) option
264 ; mutable columns : columns option
265 ; mutable beyecolumns : columncount option
266 ; mutable selcmd : string
267 ; mutable updatecurs : bool
271 type anchor = pageno * top;;
273 type outline = string * int * anchor;;
275 type rect = float * float * float * float * float * float * float * float;;
277 type tile = opaque * pixmapsize * elapsed
278 and elapsed = float;;
279 type pagemapkey = pageno * gen;;
280 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
281 and row = int
282 and col = int;;
284 let emptyanchor = (0, 0.0);;
286 type infochange = | Memused | Docinfo | Pdim;;
288 class type uioh = object
289 method display : unit
290 method key : int -> int -> uioh
291 method button : int -> bool -> int -> int -> int -> uioh
292 method motion : int -> int -> uioh
293 method pmotion : int -> int -> uioh
294 method infochanged : infochange -> unit
295 method scrollpw : (int * float * float)
296 method scrollph : (int * float * float)
297 end;;
299 type mode =
300 | Birdseye of (conf * leftx * pageno * pageno * anchor)
301 | Textentry of (textentry * onleave)
302 | View
303 and onleave = leavetextentrystatus -> unit
304 and leavetextentrystatus = | Cancel | Confirm
305 and helpitem = string * int * action
306 and action =
307 | Noaction
308 | Action of (uioh -> uioh)
311 let isbirdseye = function Birdseye _ -> true | _ -> false;;
312 let istextentry = function Textentry _ -> true | _ -> false;;
314 type currently =
315 | Idle
316 | Loading of (page * gen)
317 | Tiling of (
318 page * opaque * colorspace * angle * gen * col * row * width * height
320 | Outlining of outline list
323 let nouioh : uioh = object (self)
324 method display = ()
325 method key _ _ = self
326 method button _ _ _ _ _ = self
327 method motion _ _ = self
328 method pmotion _ _ = self
329 method infochanged _ = ()
330 method scrollpw = (0, nan, nan)
331 method scrollph = (0, nan, nan)
332 end;;
334 type state =
335 { mutable sr : Unix.file_descr
336 ; mutable sw : Unix.file_descr
337 ; mutable wsfd : Unix.file_descr
338 ; mutable errfd : Unix.file_descr option
339 ; mutable stderr : Unix.file_descr
340 ; mutable errmsgs : Buffer.t
341 ; mutable newerrmsgs : bool
342 ; mutable w : int
343 ; mutable x : int
344 ; mutable y : int
345 ; mutable scrollw : int
346 ; mutable hscrollh : int
347 ; mutable anchor : anchor
348 ; mutable ranchors : (string * string * anchor) list
349 ; mutable maxy : int
350 ; mutable layout : page list
351 ; pagemap : (pagemapkey, opaque) Hashtbl.t
352 ; tilemap : (tilemapkey, tile) Hashtbl.t
353 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
354 ; mutable pdims : (pageno * width * height * leftx) list
355 ; mutable pagecount : int
356 ; mutable currently : currently
357 ; mutable mstate : mstate
358 ; mutable searchpattern : string
359 ; mutable rects : (pageno * recttype * rect) list
360 ; mutable rects1 : (pageno * recttype * rect) list
361 ; mutable text : string
362 ; mutable fullscreen : (width * height) option
363 ; mutable mode : mode
364 ; mutable uioh : uioh
365 ; mutable outlines : outline array
366 ; mutable bookmarks : outline list
367 ; mutable path : string
368 ; mutable password : string
369 ; mutable invalidated : int
370 ; mutable memused : memsize
371 ; mutable gen : gen
372 ; mutable throttle : (page list * int * float) option
373 ; mutable autoscroll : int option
374 ; mutable ghyll : (int option -> unit)
375 ; mutable help : helpitem array
376 ; mutable docinfo : (int * string) list
377 ; mutable texid : GlTex.texture_id option
378 ; hists : hists
379 ; mutable prevzoom : float
380 ; mutable progress : float
381 ; mutable redisplay : bool
382 ; mutable mpos : mpos
384 and hists =
385 { pat : string circbuf
386 ; pag : string circbuf
387 ; nav : anchor circbuf
388 ; sel : string circbuf
392 let defconf =
393 { scrollbw = 7
394 ; scrollh = 12
395 ; icase = true
396 ; preload = true
397 ; pagebias = 0
398 ; verbose = false
399 ; debug = false
400 ; scrollstep = 24
401 ; maxhfit = true
402 ; crophack = false
403 ; autoscrollstep = 2
404 ; maxwait = None
405 ; hlinks = false
406 ; underinfo = false
407 ; interpagespace = 2
408 ; zoom = 1.0
409 ; presentation = false
410 ; angle = 0
411 ; winw = 900
412 ; winh = 900
413 ; savebmarks = true
414 ; proportional = true
415 ; trimmargins = false
416 ; trimfuzz = (0,0,0,0)
417 ; memlimit = 32 lsl 20
418 ; texcount = 256
419 ; sliceheight = 24
420 ; thumbw = 76
421 ; jumpback = true
422 ; bgcolor = (0.5, 0.5, 0.5)
423 ; bedefault = false
424 ; scrollbarinpm = true
425 ; tilew = 2048
426 ; tileh = 2048
427 ; mustoresize = 128 lsl 20
428 ; checkers = true
429 ; aalevel = 8
430 ; urilauncher =
431 (match platform with
432 | Plinux | Pfreebsd | Pdragonflybsd
433 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
434 | Posx -> "open \"%s\""
435 | Pcygwin -> "cygstart %s"
436 | Punknown -> "echo %s")
437 ; selcmd =
438 (match platform with
439 | Plinux | Pfreebsd | Pdragonflybsd
440 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
441 | Posx -> "pbcopy"
442 | Pcygwin -> "wsel"
443 | Punknown -> "cat")
444 ; colorspace = Rgb
445 ; invert = false
446 ; colorscale = 1.0
447 ; redirectstderr = false
448 ; ghyllscroll = None
449 ; columns = None
450 ; beyecolumns = None
451 ; updatecurs = false
455 let conf = { defconf with angle = defconf.angle };;
457 type fontstate =
458 { mutable fontsize : int
459 ; mutable wwidth : float
460 ; mutable maxrows : int
464 let fstate =
465 { fontsize = 14
466 ; wwidth = nan
467 ; maxrows = -1
471 let setfontsize n =
472 fstate.fontsize <- n;
473 fstate.wwidth <- measurestr fstate.fontsize "w";
474 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
477 let geturl s =
478 let colonpos = try String.index s ':' with Not_found -> -1 in
479 let len = String.length s in
480 if colonpos >= 0 && colonpos + 3 < len
481 then (
482 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
483 then
484 let schemestartpos =
485 try String.rindex_from s colonpos ' '
486 with Not_found -> -1
488 let scheme =
489 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
491 match scheme with
492 | "http" | "ftp" | "mailto" ->
493 let epos =
494 try String.index_from s colonpos ' '
495 with Not_found -> len
497 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
498 | _ -> ""
499 else ""
501 else ""
504 let popen =
505 let shell, farg = "/bin/sh", "-c" in
506 fun s ->
507 let args = [|shell; farg; s|] in
508 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
511 let gotouri uri =
512 if String.length conf.urilauncher = 0
513 then print_endline uri
514 else (
515 let url = geturl uri in
516 if String.length url = 0
517 then print_endline uri
518 else
519 let re = Str.regexp "%s" in
520 let command = Str.global_replace re url conf.urilauncher in
521 try popen command
522 with exn ->
523 Printf.eprintf
524 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
525 flush stderr;
529 let version () =
530 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
531 (platform_to_string platform) Sys.word_size Sys.ocaml_version
534 let makehelp () =
535 let strings = version () :: "" :: Help.keys in
536 Array.of_list (
537 List.map (fun s ->
538 let url = geturl s in
539 if String.length url > 0
540 then (s, 0, Action (fun u -> gotouri url; u))
541 else (s, 0, Noaction)
542 ) strings);
545 let noghyll _ = ();;
547 let state =
548 { sr = Unix.stdin
549 ; sw = Unix.stdin
550 ; wsfd = Unix.stdin
551 ; errfd = None
552 ; stderr = Unix.stderr
553 ; errmsgs = Buffer.create 0
554 ; newerrmsgs = false
555 ; x = 0
556 ; y = 0
557 ; w = 0
558 ; scrollw = 0
559 ; hscrollh = 0
560 ; anchor = emptyanchor
561 ; ranchors = []
562 ; layout = []
563 ; maxy = max_int
564 ; tilelru = Queue.create ()
565 ; pagemap = Hashtbl.create 10
566 ; tilemap = Hashtbl.create 10
567 ; pdims = []
568 ; pagecount = 0
569 ; currently = Idle
570 ; mstate = Mnone
571 ; rects = []
572 ; rects1 = []
573 ; text = ""
574 ; mode = View
575 ; fullscreen = None
576 ; searchpattern = ""
577 ; outlines = [||]
578 ; bookmarks = []
579 ; path = ""
580 ; password = ""
581 ; invalidated = 0
582 ; hists =
583 { nav = cbnew 10 (0, 0.0)
584 ; pat = cbnew 10 ""
585 ; pag = cbnew 10 ""
586 ; sel = cbnew 10 ""
588 ; memused = 0
589 ; gen = 0
590 ; throttle = None
591 ; autoscroll = None
592 ; ghyll = noghyll
593 ; help = makehelp ()
594 ; docinfo = []
595 ; texid = None
596 ; prevzoom = 1.0
597 ; progress = -1.0
598 ; uioh = nouioh
599 ; redisplay = false
600 ; mpos = (-1, -1)
604 let vlog fmt =
605 if conf.verbose
606 then
607 Printf.kprintf prerr_endline fmt
608 else
609 Printf.kprintf ignore fmt
612 let redirectstderr () =
613 if conf.redirectstderr
614 then
615 let rfd, wfd = Unix.pipe () in
616 state.stderr <- Unix.dup Unix.stderr;
617 state.errfd <- Some rfd;
618 Unix.dup2 wfd Unix.stderr;
619 else (
620 state.newerrmsgs <- false;
621 begin match state.errfd with
622 | Some fd ->
623 Unix.close fd;
624 Unix.dup2 state.stderr Unix.stderr;
625 state.errfd <- None;
626 | None -> ()
627 end;
628 prerr_string (Buffer.contents state.errmsgs);
629 flush stderr;
630 Buffer.clear state.errmsgs;
634 module G =
635 struct
636 let postRedisplay who =
637 if conf.verbose
638 then prerr_endline ("redisplay for " ^ who);
639 state.redisplay <- true;
641 end;;
643 let getopaque pageno =
644 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
645 with Not_found -> None
648 let putopaque pageno opaque =
649 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
652 let pagetranslatepoint l x y =
653 let dy = y - l.pagedispy in
654 let y = dy + l.pagey in
655 let dx = x - l.pagedispx in
656 let x = dx + l.pagex in
657 (x, y);
660 let getunder x y =
661 let rec f = function
662 | l :: rest ->
663 begin match getopaque l.pageno with
664 | Some opaque ->
665 let x0 = l.pagedispx in
666 let x1 = x0 + l.pagevw in
667 let y0 = l.pagedispy in
668 let y1 = y0 + l.pagevh in
669 if y >= y0 && y <= y1 && x >= x0 && x <= x1
670 then
671 let px, py = pagetranslatepoint l x y in
672 match whatsunder opaque px py with
673 | Unone -> f rest
674 | under -> under
675 else f rest
676 | _ ->
677 f rest
679 | [] -> Unone
681 f state.layout
684 let showtext c s =
685 state.text <- Printf.sprintf "%c%s" c s;
686 G.postRedisplay "showtext";
689 let updateunder x y =
690 match getunder x y with
691 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
692 | Ulinkuri uri ->
693 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
694 Wsi.setcursor Wsi.CURSOR_INFO
695 | Ulinkgoto (page, _) ->
696 if conf.underinfo
697 then showtext 'p' ("age: " ^ string_of_int (page+1));
698 Wsi.setcursor Wsi.CURSOR_INFO
699 | Utext s ->
700 if conf.underinfo then showtext 'f' ("ont: " ^ s);
701 Wsi.setcursor Wsi.CURSOR_TEXT
702 | Uunexpected s ->
703 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
704 Wsi.setcursor Wsi.CURSOR_INHERIT
705 | Ulaunch s ->
706 if conf.underinfo then showtext 'l' ("launch: " ^ s);
707 Wsi.setcursor Wsi.CURSOR_INHERIT
708 | Unamed s ->
709 if conf.underinfo then showtext 'n' ("named: " ^ s);
710 Wsi.setcursor Wsi.CURSOR_INHERIT
711 | Uremote (filename, pageno) ->
712 if conf.underinfo then showtext 'r'
713 (Printf.sprintf "emote: %s (%d)" filename pageno);
714 Wsi.setcursor Wsi.CURSOR_INFO
717 let addchar s c =
718 let b = Buffer.create (String.length s + 1) in
719 Buffer.add_string b s;
720 Buffer.add_char b c;
721 Buffer.contents b;
724 let colorspace_of_string s =
725 match String.lowercase s with
726 | "rgb" -> Rgb
727 | "bgr" -> Bgr
728 | "gray" -> Gray
729 | _ -> failwith "invalid colorspace"
732 let int_of_colorspace = function
733 | Rgb -> 0
734 | Bgr -> 1
735 | Gray -> 2
738 let colorspace_of_int = function
739 | 0 -> Rgb
740 | 1 -> Bgr
741 | 2 -> Gray
742 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
745 let colorspace_to_string = function
746 | Rgb -> "rgb"
747 | Bgr -> "bgr"
748 | Gray -> "gray"
751 let intentry_with_suffix text key =
752 let c =
753 if key >= 32 && key < 127
754 then Char.chr key
755 else '\000'
757 match Char.lowercase c with
758 | '0' .. '9' ->
759 let text = addchar text c in
760 TEcont text
762 | 'k' | 'm' | 'g' ->
763 let text = addchar text c in
764 TEcont text
766 | _ ->
767 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
768 TEcont text
771 let columns_to_string (n, a, b) =
772 if a = 0 && b = 0
773 then Printf.sprintf "%d" n
774 else Printf.sprintf "%d,%d,%d" n a b;
777 let columns_of_string s =
779 (int_of_string s, 0, 0)
780 with _ ->
781 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
784 let writecmd fd s =
785 let len = String.length s in
786 let n = 4 + len in
787 let b = Buffer.create n in
788 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
789 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
790 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
791 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
792 Buffer.add_string b s;
793 let s' = Buffer.contents b in
794 let n' = Unix.write fd s' 0 n in
795 if n' != n then failwith "write failed";
798 let readcmd fd =
799 let s = "xxxx" in
800 let n = Unix.read fd s 0 4 in
801 if n != 4 then failwith "incomplete read(len)";
802 let len = 0
803 lor (Char.code s.[0] lsl 24)
804 lor (Char.code s.[1] lsl 16)
805 lor (Char.code s.[2] lsl 8)
806 lor (Char.code s.[3] lsl 0)
808 let s = String.create len in
809 let n = Unix.read fd s 0 len in
810 if n != len then failwith "incomplete read(data)";
814 let makecmd s l =
815 let b = Buffer.create 10 in
816 Buffer.add_string b s;
817 let rec combine = function
818 | [] -> b
819 | x :: xs ->
820 Buffer.add_char b ' ';
821 let s =
822 match x with
823 | `b b -> if b then "1" else "0"
824 | `s s -> s
825 | `i i -> string_of_int i
826 | `f f -> string_of_float f
827 | `I f -> string_of_int (truncate f)
829 Buffer.add_string b s;
830 combine xs;
832 combine l;
835 let wcmd s l =
836 let cmd = Buffer.contents (makecmd s l) in
837 writecmd state.sw cmd;
840 let calcips h =
841 if conf.presentation
842 then
843 let d = conf.winh - h in
844 max 0 ((d + 1) / 2)
845 else
846 conf.interpagespace
849 let calcheight () =
850 let rec f pn ph pi fh l =
851 match l with
852 | (n, _, h, _) :: rest ->
853 let ips = calcips h in
854 let fh =
855 if conf.presentation
856 then fh+ips
857 else (
858 if isbirdseye state.mode && pn = 0
859 then fh + ips
860 else fh
863 let fh = fh + ((n - pn) * (ph + pi)) in
864 f n h ips fh rest;
866 | [] ->
867 let inc =
868 if conf.presentation || (isbirdseye state.mode && pn = 0)
869 then 0
870 else -pi
872 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
873 max 0 fh
875 let fh = f 0 0 0 0 state.pdims in
879 let calcheight () =
880 match conf.columns with
881 | None -> calcheight ()
882 | Some (_, b) ->
883 if Array.length b > 0
884 then
885 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
886 y + h
887 else 0
890 let getpageyh pageno =
891 let rec f pn ph pi y l =
892 match l with
893 | (n, _, h, _) :: rest ->
894 let ips = calcips h in
895 if n >= pageno
896 then
897 let h = if n = pageno then h else ph in
898 if conf.presentation && n = pageno
899 then
900 y + (pageno - pn) * (ph + pi) + pi, h
901 else
902 y + (pageno - pn) * (ph + pi), h
903 else
904 let y = y + (if conf.presentation then pi else 0) in
905 let y = y + (n - pn) * (ph + pi) in
906 f n h ips y rest
908 | [] ->
909 y + (pageno - pn) * (ph + pi), ph
911 f 0 0 0 0 state.pdims
914 let getpageyh pageno =
915 match conf.columns with
916 | None -> getpageyh pageno
917 | Some (_, b) ->
918 let (_, _, y, (_, _, h, _)) = b.(pageno) in
919 y, h
922 let getpagedim pageno =
923 let rec f ppdim l =
924 match l with
925 | (n, _, _, _) as pdim :: rest ->
926 if n >= pageno
927 then (if n = pageno then pdim else ppdim)
928 else f pdim rest
930 | [] -> ppdim
932 f (-1, -1, -1, -1) state.pdims
935 let getpagey pageno = fst (getpageyh pageno);;
937 let layout1 y sh =
938 let sh = sh - state.hscrollh in
939 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
940 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
941 match pdims with
942 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
943 let ips = calcips h in
944 let yinc =
945 if conf.presentation || (isbirdseye state.mode && pageno = 0)
946 then ips
947 else 0
949 (w, h, ips, xoff), rest, pdimno + 1, yinc
950 | _ ->
951 prev, pdims, pdimno, 0
953 let dy = dy + yinc in
954 let py = py + yinc in
955 if pageno = state.pagecount || dy >= sh
956 then
957 accu
958 else
959 let vy = y + dy in
960 if py + h <= vy - yinc
961 then
962 let py = py + h + ips in
963 let dy = max 0 (py - y) in
964 f ~pageno:(pageno+1)
965 ~pdimno
966 ~prev:curr
969 ~pdims:rest
970 ~accu
971 else
972 let pagey = vy - py in
973 let pagevh = h - pagey in
974 let pagevh = min (sh - dy) pagevh in
975 let off = if yinc > 0 then py - vy else 0 in
976 let py = py + h + ips in
977 let pagex, dx =
978 let xoff = xoff +
979 if state.w < conf.winw - state.scrollw
980 then (conf.winw - state.scrollw - state.w) / 2
981 else 0
983 let dispx = xoff + state.x in
984 if dispx < 0
985 then (-dispx, 0)
986 else (0, dispx)
988 let pagevw =
989 let lw = w - pagex in
990 min lw (conf.winw - state.scrollw)
992 let e =
993 { pageno = pageno
994 ; pagedimno = pdimno
995 ; pagew = w
996 ; pageh = h
997 ; pagex = pagex
998 ; pagey = pagey + off
999 ; pagevw = pagevw
1000 ; pagevh = pagevh - off
1001 ; pagedispx = dx
1002 ; pagedispy = dy + off
1005 let accu = e :: accu in
1006 f ~pageno:(pageno+1)
1007 ~pdimno
1008 ~prev:curr
1010 ~dy:(dy+pagevh+ips)
1011 ~pdims:rest
1012 ~accu
1014 if state.invalidated = 0
1015 then (
1016 let accu =
1018 ~pageno:0
1019 ~pdimno:~-1
1020 ~prev:(0,0,0,0)
1021 ~py:0
1022 ~dy:0
1023 ~pdims:state.pdims
1024 ~accu:[]
1026 List.rev accu
1028 else
1032 let layoutN ((columns, coverA, coverB), b) y sh =
1033 let sh = sh - state.hscrollh in
1034 let rec fold accu n =
1035 if n = Array.length b
1036 then accu
1037 else
1038 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1039 if (vy - y) > sh &&
1040 (n = coverA - 1
1041 || n = state.pagecount - coverB
1042 || (n - coverA) mod columns = columns - 1)
1043 then accu
1044 else
1045 let accu =
1046 if vy + h > y
1047 then
1048 let pagey = max 0 (y - vy) in
1049 let pagedispy = if pagey > 0 then 0 else vy - y in
1050 let pagedispx, pagex, pagevw =
1051 let pdx =
1052 if n = coverA - 1 || n = state.pagecount - coverB
1053 then state.x + (conf.winw - state.scrollw - w) / 2
1054 else dx + xoff + state.x
1056 if pdx < 0
1057 then 0, -pdx, w + pdx
1058 else pdx, 0, min (conf.winw - state.scrollw) w
1060 let pagevh = min (h - pagey) (sh - pagedispy) in
1061 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1062 then
1063 let e =
1064 { pageno = n
1065 ; pagedimno = pdimno
1066 ; pagew = w
1067 ; pageh = h
1068 ; pagex = pagex
1069 ; pagey = pagey
1070 ; pagevw = pagevw
1071 ; pagevh = pagevh
1072 ; pagedispx = pagedispx
1073 ; pagedispy = pagedispy
1076 e :: accu
1077 else
1078 accu
1079 else
1080 accu
1082 fold accu (n+1)
1084 if state.invalidated = 0
1085 then List.rev (fold [] 0)
1086 else []
1089 let layout y sh =
1090 match conf.columns with
1091 | None -> layout1 y sh
1092 | Some c -> layoutN c y sh
1095 let clamp incr =
1096 let y = state.y + incr in
1097 let y = max 0 y in
1098 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1102 let itertiles l f =
1103 let tilex = l.pagex mod conf.tilew in
1104 let tiley = l.pagey mod conf.tileh in
1106 let col = l.pagex / conf.tilew in
1107 let row = l.pagey / conf.tileh in
1109 let vw =
1110 let a = l.pagew - l.pagex in
1111 let b = conf.winw - state.scrollw in
1112 min a b
1113 and vh = l.pagevh in
1115 let rec rowloop row y0 dispy h =
1116 if h = 0
1117 then ()
1118 else (
1119 let dh = conf.tileh - y0 in
1120 let dh = min h dh in
1121 let rec colloop col x0 dispx w =
1122 if w = 0
1123 then ()
1124 else (
1125 let dw = conf.tilew - x0 in
1126 let dw = min w dw in
1128 f col row dispx dispy x0 y0 dw dh;
1129 colloop (col+1) 0 (dispx+dw) (w-dw)
1132 colloop col tilex l.pagedispx vw;
1133 rowloop (row+1) 0 (dispy+dh) (h-dh)
1136 if vw > 0 && vh > 0
1137 then rowloop row tiley l.pagedispy vh;
1140 let gettileopaque l col row =
1141 let key =
1142 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1144 try Some (Hashtbl.find state.tilemap key)
1145 with Not_found -> None
1148 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1149 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1150 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1153 let drawtiles l color =
1154 GlDraw.color color;
1155 let f col row x y tilex tiley w h =
1156 match gettileopaque l col row with
1157 | Some (opaque, _, t) ->
1158 let params = x, y, w, h, tilex, tiley in
1159 if conf.invert
1160 then (
1161 Gl.enable `blend;
1162 GlFunc.blend_func `zero `one_minus_src_color;
1164 drawtile params opaque;
1165 if conf.invert
1166 then Gl.disable `blend;
1167 if conf.debug
1168 then (
1169 let s = Printf.sprintf
1170 "%d[%d,%d] %f sec"
1171 l.pageno col row t
1173 let w = measurestr fstate.fontsize s in
1174 GlMisc.push_attrib [`current];
1175 GlDraw.color (0.0, 0.0, 0.0);
1176 GlDraw.rect
1177 (float (x-2), float (y-2))
1178 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1179 GlDraw.color (1.0, 1.0, 1.0);
1180 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1181 GlMisc.pop_attrib ();
1184 | _ ->
1185 let w =
1186 let lw = conf.winw - state.scrollw - x in
1187 min lw w
1188 and h =
1189 let lh = conf.winh - y in
1190 min lh h
1192 Gl.enable `texture_2d;
1193 begin match state.texid with
1194 | Some id ->
1195 GlTex.bind_texture `texture_2d id;
1196 let x0 = float x
1197 and y0 = float y
1198 and x1 = float (x+w)
1199 and y1 = float (y+h) in
1201 let tw = float w /. 64.0
1202 and th = float h /. 64.0 in
1203 let tx0 = float tilex /. 64.0
1204 and ty0 = float tiley /. 64.0 in
1205 let tx1 = tx0 +. tw
1206 and ty1 = ty0 +. th in
1207 GlDraw.begins `quads;
1208 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1209 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1210 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1211 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1212 GlDraw.ends ();
1214 Gl.disable `texture_2d;
1215 | None ->
1216 GlDraw.color (1.0, 1.0, 1.0);
1217 GlDraw.rect
1218 (float x, float y)
1219 (float (x+w), float (y+h));
1220 end;
1221 if w > 128 && h > fstate.fontsize + 10
1222 then (
1223 GlDraw.color (0.0, 0.0, 0.0);
1224 let c, r =
1225 if conf.verbose
1226 then (col*conf.tilew, row*conf.tileh)
1227 else col, row
1229 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1231 GlDraw.color color;
1233 itertiles l f
1236 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1238 let tilevisible1 l x y =
1239 let ax0 = l.pagex
1240 and ax1 = l.pagex + l.pagevw
1241 and ay0 = l.pagey
1242 and ay1 = l.pagey + l.pagevh in
1244 let bx0 = x
1245 and by0 = y in
1246 let bx1 = min (bx0 + conf.tilew) l.pagew
1247 and by1 = min (by0 + conf.tileh) l.pageh in
1249 let rx0 = max ax0 bx0
1250 and ry0 = max ay0 by0
1251 and rx1 = min ax1 bx1
1252 and ry1 = min ay1 by1 in
1254 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1255 nonemptyintersection
1258 let tilevisible layout n x y =
1259 let rec findpageinlayout = function
1260 | l :: _ when l.pageno = n -> tilevisible1 l x y
1261 | _ :: rest -> findpageinlayout rest
1262 | [] -> false
1264 findpageinlayout layout
1267 let tileready l x y =
1268 tilevisible1 l x y &&
1269 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1272 let tilepage n p layout =
1273 let rec loop = function
1274 | l :: rest ->
1275 if l.pageno = n
1276 then
1277 let f col row _ _ _ _ _ _ =
1278 if state.currently = Idle
1279 then
1280 match gettileopaque l col row with
1281 | Some _ -> ()
1282 | None ->
1283 let x = col*conf.tilew
1284 and y = row*conf.tileh in
1285 let w =
1286 let w = l.pagew - x in
1287 min w conf.tilew
1289 let h =
1290 let h = l.pageh - y in
1291 min h conf.tileh
1293 wcmd "tile"
1294 [`s p
1295 ;`i x
1296 ;`i y
1297 ;`i w
1298 ;`i h
1300 state.currently <-
1301 Tiling (
1302 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1303 conf.tilew, conf.tileh
1306 itertiles l f;
1307 else
1308 loop rest
1310 | [] -> ()
1312 if state.invalidated = 0 then loop layout;
1315 let preloadlayout visiblepages =
1316 let presentation = conf.presentation in
1317 let interpagespace = conf.interpagespace in
1318 let maxy = state.maxy in
1319 conf.presentation <- false;
1320 conf.interpagespace <- 0;
1321 state.maxy <- calcheight ();
1322 let y =
1323 match visiblepages with
1324 | [] -> 0
1325 | l :: _ -> getpagey l.pageno + l.pagey
1327 let y = if y < conf.winh then 0 else y - conf.winh in
1328 let h = state.y - y + conf.winh*3 in
1329 let pages = layout y h in
1330 conf.presentation <- presentation;
1331 conf.interpagespace <- interpagespace;
1332 state.maxy <- maxy;
1333 pages;
1336 let load pages =
1337 let rec loop pages =
1338 if state.currently != Idle
1339 then ()
1340 else
1341 match pages with
1342 | l :: rest ->
1343 begin match getopaque l.pageno with
1344 | None ->
1345 wcmd "page" [`i l.pageno; `i l.pagedimno];
1346 state.currently <- Loading (l, state.gen);
1347 | Some opaque ->
1348 tilepage l.pageno opaque pages;
1349 loop rest
1350 end;
1351 | _ -> ()
1353 if state.invalidated = 0 then loop pages
1356 let preload pages =
1357 load pages;
1358 if conf.preload && state.currently = Idle
1359 then load (preloadlayout pages);
1362 let layoutready layout =
1363 let rec fold all ls =
1364 all && match ls with
1365 | l :: rest ->
1366 let seen = ref false in
1367 let allvisible = ref true in
1368 let foo col row _ _ _ _ _ _ =
1369 seen := true;
1370 allvisible := !allvisible &&
1371 begin match gettileopaque l col row with
1372 | Some _ -> true
1373 | None -> false
1376 itertiles l foo;
1377 fold (!seen && !allvisible) rest
1378 | [] -> true
1380 let alltilesvisible = fold true layout in
1381 alltilesvisible;
1384 let gotoy y =
1385 let y = bound y 0 state.maxy in
1386 let y, layout, proceed =
1387 match conf.maxwait with
1388 | Some time when state.ghyll == noghyll ->
1389 begin match state.throttle with
1390 | None ->
1391 let layout = layout y conf.winh in
1392 let ready = layoutready layout in
1393 if not ready
1394 then (
1395 load layout;
1396 state.throttle <- Some (layout, y, now ());
1398 else G.postRedisplay "gotoy showall (None)";
1399 y, layout, ready
1400 | Some (_, _, started) ->
1401 let dt = now () -. started in
1402 if dt > time
1403 then (
1404 state.throttle <- None;
1405 let layout = layout y conf.winh in
1406 load layout;
1407 G.postRedisplay "maxwait";
1408 y, layout, true
1410 else -1, [], false
1413 | _ ->
1414 let layout = layout y conf.winh in
1415 if true || layoutready layout
1416 then G.postRedisplay "gotoy ready";
1417 y, layout, true
1419 if proceed
1420 then (
1421 state.y <- y;
1422 state.layout <- layout;
1423 begin match state.mode with
1424 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1425 if not (pagevisible layout pageno)
1426 then (
1427 match state.layout with
1428 | [] -> ()
1429 | l :: _ ->
1430 state.mode <- Birdseye (
1431 conf, leftx, l.pageno, hooverpageno, anchor
1434 | _ -> ()
1435 end;
1436 preload layout;
1438 state.ghyll <- noghyll;
1441 let conttiling pageno opaque =
1442 tilepage pageno opaque
1443 (if conf.preload then preloadlayout state.layout else state.layout)
1446 let gotoy_and_clear_text y =
1447 gotoy y;
1448 if not conf.verbose then state.text <- "";
1451 let getanchor () =
1452 match state.layout with
1453 | [] -> emptyanchor
1454 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1457 let getanchory (n, top) =
1458 let y, h = getpageyh n in
1459 y + (truncate (top *. float h));
1462 let gotoanchor anchor =
1463 gotoy (getanchory anchor);
1466 let addnav () =
1467 cbput state.hists.nav (getanchor ());
1470 let getnav dir =
1471 let anchor = cbgetc state.hists.nav dir in
1472 getanchory anchor;
1475 let gotoghyll y =
1476 let rec scroll f n a b =
1477 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1478 let snake f a b =
1479 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1480 if f < a
1481 then s (float f /. float a)
1482 else (
1483 if f > b
1484 then 1.0 -. s ((float (f-b) /. float (n-b)))
1485 else 1.0
1488 snake f a b
1489 and summa f n a b =
1490 (* courtesy:
1491 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1492 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1493 let iv1 = iv f in
1494 let ins = float a *. iv1
1495 and outs = float (n-b) *. iv1 in
1496 let ones = b - a in
1497 ins +. outs +. float ones
1499 let rec set (_N, _A, _B) y sy =
1500 let sum = summa 1.0 _N _A _B in
1501 let dy = float (y - sy) in
1502 state.ghyll <- (
1503 let rec gf n y1 o =
1504 if n >= _N
1505 then state.ghyll <- noghyll
1506 else
1507 let go n =
1508 let s = scroll n _N _A _B in
1509 let y1 = y1 +. ((s *. dy) /. sum) in
1510 gotoy_and_clear_text (truncate y1);
1511 state.ghyll <- gf (n+1) y1;
1513 match o with
1514 | None -> go n
1515 | Some y' -> set (_N/2, 0, 0) y' state.y
1517 gf 0 (float state.y)
1520 match conf.ghyllscroll with
1521 | None ->
1522 gotoy_and_clear_text y
1523 | Some nab ->
1524 if state.ghyll == noghyll
1525 then set nab y state.y
1526 else state.ghyll (Some y)
1529 let gotopage n top =
1530 let y, h = getpageyh n in
1531 let y = y + (truncate (top *. float h)) in
1532 gotoghyll y
1535 let gotopage1 n top =
1536 let y = getpagey n in
1537 let y = y + top in
1538 gotoghyll y
1541 let invalidate () =
1542 state.layout <- [];
1543 state.pdims <- [];
1544 state.rects <- [];
1545 state.rects1 <- [];
1546 state.invalidated <- state.invalidated + 1;
1549 let writeopen path password =
1550 writecmd state.sw ("open " ^ path ^ "\000" ^ password ^ "\000");
1553 let opendoc path password =
1554 invalidate ();
1555 state.path <- path;
1556 state.password <- password;
1557 state.gen <- state.gen + 1;
1558 state.docinfo <- [];
1560 setaalevel conf.aalevel;
1561 writeopen path password;
1562 Wsi.settitle ("llpp " ^ Filename.basename path);
1563 wcmd "geometry" [`i state.w; `i conf.winh];
1566 let scalecolor c =
1567 let c = c *. conf.colorscale in
1568 (c, c, c);
1571 let scalecolor2 (r, g, b) =
1572 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1575 let represent () =
1576 let docolumns = function
1577 | None -> ()
1578 | Some ((columns, coverA, coverB), _) ->
1579 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1580 let rec loop pageno pdimno pdim x y rowh pdims =
1581 if pageno = state.pagecount
1582 then ()
1583 else
1584 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1585 match pdims with
1586 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1587 pdimno+1, pdim, rest
1588 | _ ->
1589 pdimno, pdim, pdims
1591 let x, y, rowh' =
1592 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1593 then (
1594 (conf.winw - state.scrollw - w) / 2,
1595 y + rowh + conf.interpagespace, h
1597 else (
1598 if (pageno - coverA) mod columns = 0
1599 then 0, y + rowh + conf.interpagespace, h
1600 else x, y, max rowh h
1603 let rec fixrow m = if m = pageno then () else
1604 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1605 if h < rowh
1606 then (
1607 let y = y + (rowh - h) / 2 in
1608 a.(m) <- (pdimno, x, y, pdim);
1610 fixrow (m+1)
1612 if pageno > 1 && (pageno - coverA) mod columns = 0
1613 then fixrow (pageno - columns);
1614 a.(pageno) <- (pdimno, x, y, pdim);
1615 let x = x + w + xoff*2 + conf.interpagespace in
1616 loop (pageno+1) pdimno pdim x y rowh' pdims
1618 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1619 conf.columns <- Some ((columns, coverA, coverB), a);
1621 docolumns conf.columns;
1622 state.maxy <- calcheight ();
1623 state.hscrollh <-
1624 if state.w <= conf.winw - state.scrollw
1625 then 0
1626 else state.scrollw
1628 match state.mode with
1629 | Birdseye (_, _, pageno, _, _) ->
1630 let y, h = getpageyh pageno in
1631 let top = (conf.winh - h) / 2 in
1632 gotoy (max 0 (y - top))
1633 | _ -> gotoanchor state.anchor
1636 let reshape =
1637 let firsttime = ref true in
1638 fun ~w ~h ->
1639 GlDraw.viewport 0 0 w h;
1640 if state.invalidated = 0 && not !firsttime
1641 then state.anchor <- getanchor ();
1643 firsttime := false;
1644 conf.winw <- w;
1645 let w = truncate (float w *. conf.zoom) - state.scrollw in
1646 let w = max w 2 in
1647 state.w <- w;
1648 conf.winh <- h;
1649 setfontsize fstate.fontsize;
1650 GlMat.mode `modelview;
1651 GlMat.load_identity ();
1653 GlMat.mode `projection;
1654 GlMat.load_identity ();
1655 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1656 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1657 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1659 let w =
1660 match conf.columns with
1661 | None -> w
1662 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1664 invalidate ();
1665 wcmd "geometry" [`i w; `i h];
1668 let enttext () =
1669 let len = String.length state.text in
1670 let drawstring s =
1671 let hscrollh =
1672 match state.mode with
1673 | View -> state.hscrollh
1674 | _ -> 0
1676 let rect x w =
1677 GlDraw.rect
1678 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1679 (x+.w, float (conf.winh - hscrollh))
1682 let w = float (conf.winw - state.scrollw - 1) in
1683 if state.progress >= 0.0 && state.progress < 1.0
1684 then (
1685 GlDraw.color (0.3, 0.3, 0.3);
1686 let w1 = w *. state.progress in
1687 rect 0.0 w1;
1688 GlDraw.color (0.0, 0.0, 0.0);
1689 rect w1 (w-.w1)
1691 else (
1692 GlDraw.color (0.0, 0.0, 0.0);
1693 rect 0.0 w;
1696 GlDraw.color (1.0, 1.0, 1.0);
1697 drawstring fstate.fontsize
1698 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1700 let s =
1701 match state.mode with
1702 | Textentry ((prefix, text, _, _, _), _) ->
1703 let s =
1704 if len > 0
1705 then
1706 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1707 else
1708 Printf.sprintf "%s%s_" prefix text
1712 | _ -> state.text
1714 let s =
1715 if state.newerrmsgs
1716 then (
1717 if not (istextentry state.mode)
1718 then
1719 let s1 = "(press 'e' to review error messasges)" in
1720 if String.length s > 0 then s ^ " " ^ s1 else s1
1721 else s
1723 else s
1725 if String.length s > 0
1726 then drawstring s
1729 let gctiles () =
1730 let len = Queue.length state.tilelru in
1731 let rec loop qpos =
1732 if state.memused <= conf.memlimit
1733 then ()
1734 else (
1735 if qpos < len
1736 then
1737 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1738 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1739 let (_, pw, ph, _) = getpagedim n in
1741 gen = state.gen
1742 && colorspace = conf.colorspace
1743 && angle = conf.angle
1744 && pagew = pw
1745 && pageh = ph
1746 && (
1747 let layout =
1748 match state.throttle with
1749 | None ->
1750 if conf.preload
1751 then preloadlayout state.layout
1752 else state.layout
1753 | Some (layout, _, _) ->
1754 layout
1756 let x = col*conf.tilew
1757 and y = row*conf.tileh in
1758 tilevisible layout n x y
1760 then Queue.push lruitem state.tilelru
1761 else (
1762 wcmd "freetile" [`s p];
1763 state.memused <- state.memused - s;
1764 state.uioh#infochanged Memused;
1765 Hashtbl.remove state.tilemap k;
1767 loop (qpos+1)
1770 loop 0
1773 let flushtiles () =
1774 Queue.iter (fun (k, p, s) ->
1775 wcmd "freetile" [`s p];
1776 state.memused <- state.memused - s;
1777 state.uioh#infochanged Memused;
1778 Hashtbl.remove state.tilemap k;
1779 ) state.tilelru;
1780 Queue.clear state.tilelru;
1781 load state.layout;
1784 let logcurrently = function
1785 | Idle -> dolog "Idle"
1786 | Loading (l, gen) ->
1787 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1788 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1789 dolog
1790 "Tiling %d[%d,%d] page=%s cs=%s angle"
1791 l.pageno col row pageopaque
1792 (colorspace_to_string colorspace)
1794 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1795 angle gen conf.angle state.gen
1796 tilew tileh
1797 conf.tilew conf.tileh
1799 | Outlining _ ->
1800 dolog "outlining"
1803 let act cmds =
1804 (* dolog "%S" cmds; *)
1805 let op, args =
1806 let spacepos =
1807 try String.index cmds ' '
1808 with Not_found -> -1
1810 if spacepos = -1
1811 then cmds, ""
1812 else
1813 let l = String.length cmds in
1814 let op = String.sub cmds 0 spacepos in
1815 op, begin
1816 if l - spacepos < 2 then ""
1817 else String.sub cmds (spacepos+1) (l-spacepos-1)
1820 match op with
1821 | "clear" ->
1822 state.uioh#infochanged Pdim;
1823 state.pdims <- [];
1825 | "clearrects" ->
1826 state.rects <- state.rects1;
1827 G.postRedisplay "clearrects";
1829 | "continue" ->
1830 let n =
1831 try Scanf.sscanf args "%u" (fun n -> n)
1832 with exn ->
1833 dolog "error processing 'continue' %S: %s"
1834 cmds (Printexc.to_string exn);
1835 exit 1;
1837 state.pagecount <- n;
1838 state.invalidated <- state.invalidated - 1;
1839 begin match state.currently with
1840 | Outlining l ->
1841 state.currently <- Idle;
1842 state.outlines <- Array.of_list (List.rev l)
1843 | _ -> ()
1844 end;
1845 if state.invalidated = 0
1846 then represent ();
1847 if conf.maxwait = None
1848 then G.postRedisplay "continue";
1850 | "title" ->
1851 Wsi.settitle args
1853 | "msg" ->
1854 showtext ' ' args
1856 | "vmsg" ->
1857 if conf.verbose
1858 then showtext ' ' args
1860 | "progress" ->
1861 let progress, text =
1863 Scanf.sscanf args "%f %n"
1864 (fun f pos ->
1865 f, String.sub args pos (String.length args - pos))
1866 with exn ->
1867 dolog "error processing 'progress' %S: %s"
1868 cmds (Printexc.to_string exn);
1869 exit 1;
1871 state.text <- text;
1872 state.progress <- progress;
1873 G.postRedisplay "progress"
1875 | "firstmatch" ->
1876 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1878 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1879 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1880 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1881 with exn ->
1882 dolog "error processing 'firstmatch' %S: %s"
1883 cmds (Printexc.to_string exn);
1884 exit 1;
1886 let y = (getpagey pageno) + truncate y0 in
1887 addnav ();
1888 gotoy y;
1889 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1891 | "match" ->
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 'match' %S: %s"
1899 cmds (Printexc.to_string exn);
1900 exit 1;
1902 state.rects1 <-
1903 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1905 | "page" ->
1906 let pageopaque, t =
1908 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1909 with exn ->
1910 dolog "error processing 'page' %S: %s"
1911 cmds (Printexc.to_string exn);
1912 exit 1;
1914 begin match state.currently with
1915 | Loading (l, gen) ->
1916 vlog "page %d took %f sec" l.pageno t;
1917 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1918 begin match state.throttle with
1919 | None ->
1920 let preloadedpages =
1921 if conf.preload
1922 then preloadlayout state.layout
1923 else state.layout
1925 let evict () =
1926 let module IntSet =
1927 Set.Make (struct type t = int let compare = (-) end) in
1928 let set =
1929 List.fold_left (fun s l -> IntSet.add l.pageno s)
1930 IntSet.empty preloadedpages
1932 let evictedpages =
1933 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1934 if not (IntSet.mem pageno set)
1935 then (
1936 wcmd "freepage" [`s opaque];
1937 key :: accu
1939 else accu
1940 ) state.pagemap []
1942 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1944 evict ();
1945 state.currently <- Idle;
1946 if gen = state.gen
1947 then (
1948 tilepage l.pageno pageopaque state.layout;
1949 load state.layout;
1950 load preloadedpages;
1951 if pagevisible state.layout l.pageno
1952 && layoutready state.layout
1953 then G.postRedisplay "page";
1956 | Some (layout, _, _) ->
1957 state.currently <- Idle;
1958 tilepage l.pageno pageopaque layout;
1959 load state.layout
1960 end;
1962 | _ ->
1963 dolog "Inconsistent loading state";
1964 logcurrently state.currently;
1965 exit 1
1968 | "tile" ->
1969 let (x, y, opaque, size, t) =
1971 Scanf.sscanf args "%u %u %s %u %f"
1972 (fun x y p size t -> (x, y, p, size, t))
1973 with exn ->
1974 dolog "error processing 'tile' %S: %s"
1975 cmds (Printexc.to_string exn);
1976 exit 1;
1978 begin match state.currently with
1979 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1980 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1982 if tilew != conf.tilew || tileh != conf.tileh
1983 then (
1984 wcmd "freetile" [`s opaque];
1985 state.currently <- Idle;
1986 load state.layout;
1988 else (
1989 puttileopaque l col row gen cs angle opaque size t;
1990 state.memused <- state.memused + size;
1991 state.uioh#infochanged Memused;
1992 gctiles ();
1993 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1994 opaque, size) state.tilelru;
1996 let layout =
1997 match state.throttle with
1998 | None -> state.layout
1999 | Some (layout, _, _) -> layout
2002 state.currently <- Idle;
2003 if gen = state.gen
2004 && conf.colorspace = cs
2005 && conf.angle = angle
2006 && tilevisible layout l.pageno x y
2007 then conttiling l.pageno pageopaque;
2009 begin match state.throttle with
2010 | None ->
2011 preload state.layout;
2012 if gen = state.gen
2013 && conf.colorspace = cs
2014 && conf.angle = angle
2015 && tilevisible state.layout l.pageno x y
2016 then G.postRedisplay "tile nothrottle";
2018 | Some (layout, y, _) ->
2019 let ready = layoutready layout in
2020 if ready
2021 then (
2022 state.y <- y;
2023 state.layout <- layout;
2024 state.throttle <- None;
2025 G.postRedisplay "throttle";
2027 else load layout;
2028 end;
2031 | _ ->
2032 dolog "Inconsistent tiling state";
2033 logcurrently state.currently;
2034 exit 1
2037 | "pdim" ->
2038 let pdim =
2040 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2041 with exn ->
2042 dolog "error processing 'pdim' %S: %s"
2043 cmds (Printexc.to_string exn);
2044 exit 1;
2046 state.uioh#infochanged Pdim;
2047 state.pdims <- pdim :: state.pdims
2049 | "o" ->
2050 let (l, n, t, h, pos) =
2052 Scanf.sscanf args "%u %u %d %u %n"
2053 (fun l n t h pos -> l, n, t, h, pos)
2054 with exn ->
2055 dolog "error processing 'o' %S: %s"
2056 cmds (Printexc.to_string exn);
2057 exit 1;
2059 let s = String.sub args pos (String.length args - pos) in
2060 let outline = (s, l, (n, float t /. float h)) in
2061 begin match state.currently with
2062 | Outlining outlines ->
2063 state.currently <- Outlining (outline :: outlines)
2064 | Idle ->
2065 state.currently <- Outlining [outline]
2066 | currently ->
2067 dolog "invalid outlining state";
2068 logcurrently currently
2071 | "info" ->
2072 state.docinfo <- (1, args) :: state.docinfo
2074 | "infoend" ->
2075 state.uioh#infochanged Docinfo;
2076 state.docinfo <- List.rev state.docinfo
2078 | _ ->
2079 dolog "unknown cmd `%S'" cmds
2082 let onhist cb =
2083 let rc = cb.rc in
2084 let action = function
2085 | HCprev -> cbget cb ~-1
2086 | HCnext -> cbget cb 1
2087 | HCfirst -> cbget cb ~-(cb.rc)
2088 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2089 and cancel () = cb.rc <- rc
2090 in (action, cancel)
2093 let search pattern forward =
2094 if String.length pattern > 0
2095 then
2096 let pn, py =
2097 match state.layout with
2098 | [] -> 0, 0
2099 | l :: _ ->
2100 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2102 let cmd =
2103 let b = makecmd "search"
2104 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
2106 Buffer.add_char b ',';
2107 Buffer.add_string b pattern;
2108 Buffer.add_char b '\000';
2109 Buffer.contents b;
2111 writecmd state.sw cmd;
2114 let intentry text key =
2115 let c =
2116 if key >= 32 && key < 127
2117 then Char.chr key
2118 else '\000'
2120 match c with
2121 | '0' .. '9' ->
2122 let text = addchar text c in
2123 TEcont text
2125 | _ ->
2126 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2127 TEcont text
2130 let textentry text key =
2131 if key land 0xff00 = 0xff00
2132 then TEcont text
2133 else TEcont (text ^ Wsi.toutf8 key)
2136 let reqlayout angle proportional =
2137 match state.throttle with
2138 | None ->
2139 if state.invalidated = 0 then state.anchor <- getanchor ();
2140 conf.angle <- angle mod 360;
2141 conf.proportional <- proportional;
2142 invalidate ();
2143 wcmd "reqlayout" [`i conf.angle; `b proportional];
2144 | _ -> ()
2147 let settrim trimmargins trimfuzz =
2148 if state.invalidated = 0 then state.anchor <- getanchor ();
2149 conf.trimmargins <- trimmargins;
2150 conf.trimfuzz <- trimfuzz;
2151 let x0, y0, x1, y1 = trimfuzz in
2152 invalidate ();
2153 wcmd "settrim" [
2154 `b conf.trimmargins;
2155 `i x0;
2156 `i y0;
2157 `i x1;
2158 `i y1;
2160 Hashtbl.iter (fun _ opaque ->
2161 wcmd "freepage" [`s opaque];
2162 ) state.pagemap;
2163 Hashtbl.clear state.pagemap;
2166 let setzoom zoom =
2167 match state.throttle with
2168 | None ->
2169 let zoom = max 0.01 zoom in
2170 if zoom <> conf.zoom
2171 then (
2172 state.prevzoom <- conf.zoom;
2173 let relx =
2174 if zoom <= 1.0
2175 then (state.x <- 0; 0.0)
2176 else float state.x /. float state.w
2178 conf.zoom <- zoom;
2179 reshape conf.winw conf.winh;
2180 if zoom > 1.0
2181 then (
2182 let x = relx *. float state.w in
2183 state.x <- truncate x;
2185 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2188 | Some (layout, y, started) ->
2189 let time =
2190 match conf.maxwait with
2191 | None -> 0.0
2192 | Some t -> t
2194 let dt = now () -. started in
2195 if dt > time
2196 then (
2197 state.y <- y;
2198 load layout;
2202 let setcolumns columns coverA coverB =
2203 if columns < 2
2204 then (
2205 conf.columns <- None;
2206 state.x <- 0;
2207 setzoom 1.0;
2209 else (
2210 conf.columns <- Some ((columns, coverA, coverB), [||]);
2211 conf.zoom <- 1.0;
2213 reshape conf.winw conf.winh;
2216 let enterbirdseye () =
2217 let zoom = float conf.thumbw /. float conf.winw in
2218 let birdseyepageno =
2219 let cy = conf.winh / 2 in
2220 let fold = function
2221 | [] -> 0
2222 | l :: rest ->
2223 let rec fold best = function
2224 | [] -> best.pageno
2225 | l :: rest ->
2226 let d = cy - (l.pagedispy + l.pagevh/2)
2227 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2228 if abs d < abs dbest
2229 then fold l rest
2230 else best.pageno
2231 in fold l rest
2233 fold state.layout
2235 state.mode <- Birdseye (
2236 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2238 conf.zoom <- zoom;
2239 conf.presentation <- false;
2240 conf.interpagespace <- 10;
2241 conf.hlinks <- false;
2242 state.x <- 0;
2243 state.mstate <- Mnone;
2244 conf.maxwait <- None;
2245 conf.columns <- (
2246 match conf.beyecolumns with
2247 | Some c ->
2248 conf.zoom <- 1.0;
2249 Some ((c, 0, 0), [||])
2250 | None -> None
2252 Wsi.setcursor Wsi.CURSOR_INHERIT;
2253 if conf.verbose
2254 then
2255 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2256 (100.0*.zoom)
2257 else
2258 state.text <- ""
2260 reshape conf.winw conf.winh;
2263 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2264 state.mode <- View;
2265 conf.zoom <- c.zoom;
2266 conf.presentation <- c.presentation;
2267 conf.interpagespace <- c.interpagespace;
2268 conf.maxwait <- c.maxwait;
2269 conf.hlinks <- c.hlinks;
2270 conf.beyecolumns <- (
2271 match conf.columns with
2272 | Some ((c, _, _), _) -> Some c
2273 | None -> None
2275 conf.columns <- (
2276 match c.columns with
2277 | Some (c, _) -> Some (c, [||])
2278 | None -> None
2280 state.x <- leftx;
2281 if conf.verbose
2282 then
2283 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2284 (100.0*.conf.zoom)
2286 reshape conf.winw conf.winh;
2287 state.anchor <- if goback then anchor else (pageno, 0.0);
2290 let togglebirdseye () =
2291 match state.mode with
2292 | Birdseye vals -> leavebirdseye vals true
2293 | View -> enterbirdseye ()
2294 | _ -> ()
2297 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2298 let pageno = max 0 (pageno - incr) in
2299 let rec loop = function
2300 | [] -> gotopage1 pageno 0
2301 | l :: _ when l.pageno = pageno ->
2302 if l.pagedispy >= 0 && l.pagey = 0
2303 then G.postRedisplay "upbirdseye"
2304 else gotopage1 pageno 0
2305 | _ :: rest -> loop rest
2307 loop state.layout;
2308 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2311 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2312 let pageno = min (state.pagecount - 1) (pageno + incr) in
2313 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2314 let rec loop = function
2315 | [] ->
2316 let y, h = getpageyh pageno in
2317 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2318 gotoy (clamp dy)
2319 | l :: _ when l.pageno = pageno ->
2320 if l.pagevh != l.pageh
2321 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2322 else G.postRedisplay "downbirdseye"
2323 | _ :: rest -> loop rest
2325 loop state.layout
2328 let optentry mode _ key =
2329 let btos b = if b then "on" else "off" in
2330 if key >= 32 && key < 127
2331 then
2332 let c = Char.chr key in
2333 match c with
2334 | 's' ->
2335 let ondone s =
2336 try conf.scrollstep <- int_of_string s with exc ->
2337 state.text <- Printf.sprintf "bad integer `%s': %s"
2338 s (Printexc.to_string exc)
2340 TEswitch ("scroll step: ", "", None, intentry, ondone)
2342 | 'A' ->
2343 let ondone s =
2345 conf.autoscrollstep <- int_of_string s;
2346 if state.autoscroll <> None
2347 then state.autoscroll <- Some conf.autoscrollstep
2348 with exc ->
2349 state.text <- Printf.sprintf "bad integer `%s': %s"
2350 s (Printexc.to_string exc)
2352 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2354 | 'C' ->
2355 let ondone s =
2357 let n, a, b = columns_of_string s in
2358 setcolumns n a b;
2359 with exc ->
2360 state.text <- Printf.sprintf "bad columns `%s': %s"
2361 s (Printexc.to_string exc)
2363 TEswitch ("columns: ", "", None, textentry, ondone)
2365 | 'Z' ->
2366 let ondone s =
2368 let zoom = float (int_of_string s) /. 100.0 in
2369 setzoom zoom
2370 with exc ->
2371 state.text <- Printf.sprintf "bad integer `%s': %s"
2372 s (Printexc.to_string exc)
2374 TEswitch ("zoom: ", "", None, intentry, ondone)
2376 | 't' ->
2377 let ondone s =
2379 conf.thumbw <- bound (int_of_string s) 2 4096;
2380 state.text <-
2381 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2382 begin match mode with
2383 | Birdseye beye ->
2384 leavebirdseye beye false;
2385 enterbirdseye ();
2386 | _ -> ();
2388 with exc ->
2389 state.text <- Printf.sprintf "bad integer `%s': %s"
2390 s (Printexc.to_string exc)
2392 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2394 | 'R' ->
2395 let ondone s =
2396 match try
2397 Some (int_of_string s)
2398 with exc ->
2399 state.text <- Printf.sprintf "bad integer `%s': %s"
2400 s (Printexc.to_string exc);
2401 None
2402 with
2403 | Some angle -> reqlayout angle conf.proportional
2404 | None -> ()
2406 TEswitch ("rotation: ", "", None, intentry, ondone)
2408 | 'i' ->
2409 conf.icase <- not conf.icase;
2410 TEdone ("case insensitive search " ^ (btos conf.icase))
2412 | 'p' ->
2413 conf.preload <- not conf.preload;
2414 gotoy state.y;
2415 TEdone ("preload " ^ (btos conf.preload))
2417 | 'v' ->
2418 conf.verbose <- not conf.verbose;
2419 TEdone ("verbose " ^ (btos conf.verbose))
2421 | 'd' ->
2422 conf.debug <- not conf.debug;
2423 TEdone ("debug " ^ (btos conf.debug))
2425 | 'h' ->
2426 conf.maxhfit <- not conf.maxhfit;
2427 state.maxy <-
2428 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2429 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2431 | 'c' ->
2432 conf.crophack <- not conf.crophack;
2433 TEdone ("crophack " ^ btos conf.crophack)
2435 | 'a' ->
2436 let s =
2437 match conf.maxwait with
2438 | None ->
2439 conf.maxwait <- Some infinity;
2440 "always wait for page to complete"
2441 | Some _ ->
2442 conf.maxwait <- None;
2443 "show placeholder if page is not ready"
2445 TEdone s
2447 | 'f' ->
2448 conf.underinfo <- not conf.underinfo;
2449 TEdone ("underinfo " ^ btos conf.underinfo)
2451 | 'P' ->
2452 conf.savebmarks <- not conf.savebmarks;
2453 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2455 | 'S' ->
2456 let ondone s =
2458 let pageno, py =
2459 match state.layout with
2460 | [] -> 0, 0
2461 | l :: _ ->
2462 l.pageno, l.pagey
2464 conf.interpagespace <- int_of_string s;
2465 state.maxy <- calcheight ();
2466 let y = getpagey pageno in
2467 gotoy (y + py)
2468 with exc ->
2469 state.text <- Printf.sprintf "bad integer `%s': %s"
2470 s (Printexc.to_string exc)
2472 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2474 | 'l' ->
2475 reqlayout conf.angle (not conf.proportional);
2476 TEdone ("proportional display " ^ btos conf.proportional)
2478 | 'T' ->
2479 settrim (not conf.trimmargins) conf.trimfuzz;
2480 TEdone ("trim margins " ^ btos conf.trimmargins)
2482 | 'I' ->
2483 conf.invert <- not conf.invert;
2484 TEdone ("invert colors " ^ btos conf.invert)
2486 | 'x' ->
2487 let ondone s =
2488 cbput state.hists.sel s;
2489 conf.selcmd <- s;
2491 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2492 textentry, ondone)
2494 | _ ->
2495 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2496 TEstop
2497 else
2498 TEcont state.text
2501 class type lvsource = object
2502 method getitemcount : int
2503 method getitem : int -> (string * int)
2504 method hasaction : int -> bool
2505 method exit :
2506 uioh:uioh ->
2507 cancel:bool ->
2508 active:int ->
2509 first:int ->
2510 pan:int ->
2511 qsearch:string ->
2512 uioh option
2513 method getactive : int
2514 method getfirst : int
2515 method getqsearch : string
2516 method setqsearch : string -> unit
2517 method getpan : int
2518 end;;
2520 class virtual lvsourcebase = object
2521 val mutable m_active = 0
2522 val mutable m_first = 0
2523 val mutable m_qsearch = ""
2524 val mutable m_pan = 0
2525 method getactive = m_active
2526 method getfirst = m_first
2527 method getqsearch = m_qsearch
2528 method getpan = m_pan
2529 method setqsearch s = m_qsearch <- s
2530 end;;
2532 let withoutlastutf8 s =
2533 let len = String.length s in
2534 if len = 0
2535 then s
2536 else
2537 let rec find pos =
2538 if pos = 0
2539 then pos
2540 else
2541 let b = Char.code s.[pos] in
2542 if b land 0b110000 = 0b11000000
2543 then find (pos-1)
2544 else pos-1
2546 let first =
2547 if Char.code s.[len-1] land 0x80 = 0
2548 then len-1
2549 else find (len-1)
2551 String.sub s 0 first;
2554 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2555 let enttext te =
2556 state.mode <- Textentry (te, onleave);
2557 state.text <- "";
2558 enttext ();
2559 G.postRedisplay "textentrykeyboard enttext";
2561 let histaction cmd =
2562 match opthist with
2563 | None -> ()
2564 | Some (action, _) ->
2565 state.mode <- Textentry (
2566 (c, action cmd, opthist, onkey, ondone), onleave
2568 G.postRedisplay "textentry histaction"
2570 match key with
2571 | 0xff08 -> (* backspace *)
2572 let s = withoutlastutf8 text in
2573 let len = String.length s in
2574 if len = 0
2575 then (
2576 onleave Cancel;
2577 G.postRedisplay "textentrykeyboard after cancel";
2579 else (
2580 enttext (c, s, opthist, onkey, ondone)
2583 | 0xff0d ->
2584 ondone text;
2585 onleave Confirm;
2586 G.postRedisplay "textentrykeyboard after confirm"
2588 | 0xff52 -> histaction HCprev
2589 | 0xff54 -> histaction HCnext
2590 | 0xff50 -> histaction HCfirst
2591 | 0xff57 -> histaction HClast
2593 | 0xff1b -> (* escape*)
2594 if String.length text = 0
2595 then (
2596 begin match opthist with
2597 | None -> ()
2598 | Some (_, onhistcancel) -> onhistcancel ()
2599 end;
2600 onleave Cancel;
2601 state.text <- "";
2602 G.postRedisplay "textentrykeyboard after cancel2"
2604 else (
2605 enttext (c, "", opthist, onkey, ondone)
2608 | 0xff9f | 0xffff -> () (* delete *)
2610 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2611 begin match onkey text key with
2612 | TEdone text ->
2613 ondone text;
2614 onleave Confirm;
2615 G.postRedisplay "textentrykeyboard after confirm2";
2617 | TEcont text ->
2618 enttext (c, text, opthist, onkey, ondone);
2620 | TEstop ->
2621 onleave Cancel;
2622 G.postRedisplay "textentrykeyboard after cancel3"
2624 | TEswitch te ->
2625 state.mode <- Textentry (te, onleave);
2626 G.postRedisplay "textentrykeyboard switch";
2627 end;
2629 | _ ->
2630 vlog "unhandled key %#x" key
2633 let firstof first active =
2634 if first > active || abs (first - active) > fstate.maxrows - 1
2635 then max 0 (active - (fstate.maxrows/2))
2636 else first
2639 let calcfirst first active =
2640 if active > first
2641 then
2642 let rows = active - first in
2643 if rows > fstate.maxrows then active - fstate.maxrows else first
2644 else active
2647 let scrollph y maxy =
2648 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2649 let sh = float conf.winh /. sh in
2650 let sh = max sh (float conf.scrollh) in
2652 let percent =
2653 if y = state.maxy
2654 then 1.0
2655 else float y /. float maxy
2657 let position = (float conf.winh -. sh) *. percent in
2659 let position =
2660 if position +. sh > float conf.winh
2661 then float conf.winh -. sh
2662 else position
2664 position, sh;
2667 let coe s = (s :> uioh);;
2669 class listview ~(source:lvsource) ~trusted =
2670 object (self)
2671 val m_pan = source#getpan
2672 val m_first = source#getfirst
2673 val m_active = source#getactive
2674 val m_qsearch = source#getqsearch
2675 val m_prev_uioh = state.uioh
2677 method private elemunder y =
2678 let n = y / (fstate.fontsize+1) in
2679 if m_first + n < source#getitemcount
2680 then (
2681 if source#hasaction (m_first + n)
2682 then Some (m_first + n)
2683 else None
2685 else None
2687 method display =
2688 Gl.enable `blend;
2689 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2690 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2691 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2692 GlDraw.color (1., 1., 1.);
2693 Gl.enable `texture_2d;
2694 let fs = fstate.fontsize in
2695 let nfs = fs + 1 in
2696 let ww = fstate.wwidth in
2697 let tabw = 30.0*.ww in
2698 let itemcount = source#getitemcount in
2699 let rec loop row =
2700 if (row - m_first) * nfs > conf.winh
2701 then ()
2702 else (
2703 if row >= 0 && row < itemcount
2704 then (
2705 let (s, level) = source#getitem row in
2706 let y = (row - m_first) * nfs in
2707 let x = 5.0 +. float (level + m_pan) *. ww in
2708 if row = m_active
2709 then (
2710 Gl.disable `texture_2d;
2711 GlDraw.polygon_mode `both `line;
2712 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2713 GlDraw.rect (1., float (y + 1))
2714 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2715 GlDraw.polygon_mode `both `fill;
2716 GlDraw.color (1., 1., 1.);
2717 Gl.enable `texture_2d;
2720 let drawtabularstring s =
2721 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2722 if trusted
2723 then
2724 let tabpos = try String.index s '\t' with Not_found -> -1 in
2725 if tabpos > 0
2726 then
2727 let len = String.length s - tabpos - 1 in
2728 let s1 = String.sub s 0 tabpos
2729 and s2 = String.sub s (tabpos + 1) len in
2730 let nx = drawstr x s1 in
2731 let sw = nx -. x in
2732 let x = x +. (max tabw sw) in
2733 drawstr x s2
2734 else
2735 drawstr x s
2736 else
2737 drawstr x s
2739 let _ = drawtabularstring s in
2740 loop (row+1)
2744 loop m_first;
2745 Gl.disable `blend;
2746 Gl.disable `texture_2d;
2748 method updownlevel incr =
2749 let len = source#getitemcount in
2750 let curlevel =
2751 if m_active >= 0 && m_active < len
2752 then snd (source#getitem m_active)
2753 else -1
2755 let rec flow i =
2756 if i = len then i-1 else if i = -1 then 0 else
2757 let _, l = source#getitem i in
2758 if l != curlevel then i else flow (i+incr)
2760 let active = flow m_active in
2761 let first = calcfirst m_first active in
2762 G.postRedisplay "outline updownlevel";
2763 {< m_active = active; m_first = first >}
2765 method private key1 key mask =
2766 let set1 active first qsearch =
2767 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2769 let search active pattern incr =
2770 let dosearch re =
2771 let rec loop n =
2772 if n >= 0 && n < source#getitemcount
2773 then (
2774 let s, _ = source#getitem n in
2776 (try ignore (Str.search_forward re s 0); true
2777 with Not_found -> false)
2778 then Some n
2779 else loop (n + incr)
2781 else None
2783 loop active
2786 let re = Str.regexp_case_fold pattern in
2787 dosearch re
2788 with Failure s ->
2789 state.text <- s;
2790 None
2792 let itemcount = source#getitemcount in
2793 let find start incr =
2794 let rec find i =
2795 if i = -1 || i = itemcount
2796 then -1
2797 else (
2798 if source#hasaction i
2799 then i
2800 else find (i + incr)
2803 find start
2805 let set active first =
2806 let first = bound first 0 (itemcount - fstate.maxrows) in
2807 state.text <- "";
2808 coe {< m_active = active; m_first = first >}
2810 let navigate incr =
2811 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2812 let active, first =
2813 let incr1 = if incr > 0 then 1 else -1 in
2814 if isvisible m_first m_active
2815 then
2816 let next =
2817 let next = m_active + incr in
2818 let next =
2819 if next < 0 || next >= itemcount
2820 then -1
2821 else find next incr1
2823 if next = -1 || abs (m_active - next) > fstate.maxrows
2824 then -1
2825 else next
2827 if next = -1
2828 then
2829 let first = m_first + incr in
2830 let first = bound first 0 (itemcount - 1) in
2831 let next =
2832 let next = m_active + incr in
2833 let next = bound next 0 (itemcount - 1) in
2834 find next ~-incr1
2836 let active = if next = -1 then m_active else next in
2837 active, first
2838 else
2839 let first = min next m_first in
2840 let first =
2841 if abs (next - first) > fstate.maxrows
2842 then first + incr
2843 else first
2845 next, first
2846 else
2847 let first = m_first + incr in
2848 let first = bound first 0 (itemcount - 1) in
2849 let active =
2850 let next = m_active + incr in
2851 let next = bound next 0 (itemcount - 1) in
2852 let next = find next incr1 in
2853 let active =
2854 if next = -1 || abs (m_active - first) > fstate.maxrows
2855 then (
2856 let active = if m_active = -1 then next else m_active in
2857 active
2859 else next
2861 if isvisible first active
2862 then active
2863 else -1
2865 active, first
2867 G.postRedisplay "listview navigate";
2868 set active first;
2870 match key with
2871 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2872 let incr = if key = 0x72 then -1 else 1 in
2873 let active, first =
2874 match search (m_active + incr) m_qsearch incr with
2875 | None ->
2876 state.text <- m_qsearch ^ " [not found]";
2877 m_active, m_first
2878 | Some active ->
2879 state.text <- m_qsearch;
2880 active, firstof m_first active
2882 G.postRedisplay "listview ctrl-r/s";
2883 set1 active first m_qsearch;
2885 | 0xff08 -> (* backspace *)
2886 if String.length m_qsearch = 0
2887 then coe self
2888 else (
2889 let qsearch = withoutlastutf8 m_qsearch in
2890 let len = String.length qsearch in
2891 if len = 0
2892 then (
2893 state.text <- "";
2894 G.postRedisplay "listview empty qsearch";
2895 set1 m_active m_first "";
2897 else
2898 let active, first =
2899 match search m_active qsearch ~-1 with
2900 | None ->
2901 state.text <- qsearch ^ " [not found]";
2902 m_active, m_first
2903 | Some active ->
2904 state.text <- qsearch;
2905 active, firstof m_first active
2907 G.postRedisplay "listview backspace qsearch";
2908 set1 active first qsearch
2911 | key when ((key >= 32 && key < 127)
2912 || (key != 0 && key land 0xff00 != 0xff00)) ->
2913 let pattern =
2914 if key >= 32 && key < 127
2915 then addchar m_qsearch (Char.chr key)
2916 else m_qsearch ^ Wsi.toutf8 key
2918 let active, first =
2919 match search m_active pattern 1 with
2920 | None ->
2921 state.text <- pattern ^ " [not found]";
2922 m_active, m_first
2923 | Some active ->
2924 state.text <- pattern;
2925 active, firstof m_first active
2927 G.postRedisplay "listview qsearch add";
2928 set1 active first pattern;
2930 | 0xff1b -> (* escape *)
2931 state.text <- "";
2932 if String.length m_qsearch = 0
2933 then (
2934 G.postRedisplay "list view escape";
2935 begin
2936 match
2937 source#exit (coe self) true m_active m_first m_pan m_qsearch
2938 with
2939 | None -> m_prev_uioh
2940 | Some uioh -> uioh
2943 else (
2944 G.postRedisplay "list view kill qsearch";
2945 source#setqsearch "";
2946 coe {< m_qsearch = "" >}
2949 | 0xff0d -> (* return *)
2950 state.text <- "";
2951 let self = {< m_qsearch = "" >} in
2952 source#setqsearch "";
2953 let opt =
2954 G.postRedisplay "listview enter";
2955 if m_active >= 0 && m_active < source#getitemcount
2956 then (
2957 source#exit (coe self) false m_active m_first m_pan "";
2959 else (
2960 source#exit (coe self) true m_active m_first m_pan "";
2963 begin match opt with
2964 | None -> m_prev_uioh
2965 | Some uioh -> uioh
2968 | 0xff9f | 0xffff -> (* delete *)
2969 coe self
2971 | 0xff52 -> navigate ~-1 (* up *)
2972 | 0xff54 -> navigate 1 (* down *)
2973 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
2974 | 0xff56 -> navigate fstate.maxrows (* next *)
2976 | 0xff53 -> (* right *)
2977 state.text <- "";
2978 G.postRedisplay "listview right";
2979 coe {< m_pan = m_pan - 1 >}
2981 | 0xff51 -> (* left *)
2982 state.text <- "";
2983 G.postRedisplay "listview left";
2984 coe {< m_pan = m_pan + 1 >}
2986 | 0xff50 -> (* home *)
2987 let active = find 0 1 in
2988 G.postRedisplay "listview home";
2989 set active 0;
2991 | 0xff57 -> (* end *)
2992 let first = max 0 (itemcount - fstate.maxrows) in
2993 let active = find (itemcount - 1) ~-1 in
2994 G.postRedisplay "listview end";
2995 set active first;
2997 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2998 coe self
3000 | _ ->
3001 dolog "listview unknown key %#x" key; coe self
3003 method key key mask =
3004 match state.mode with
3005 | Textentry te -> textentrykeyboard key mask te; coe self
3006 | _ -> self#key1 key mask
3008 method button button down x y _ =
3009 let opt =
3010 match button with
3011 | 1 when x > conf.winw - conf.scrollbw ->
3012 G.postRedisplay "listview scroll";
3013 if down
3014 then
3015 let _, position, sh = self#scrollph in
3016 if y > truncate position && y < truncate (position +. sh)
3017 then (
3018 state.mstate <- Mscrolly;
3019 Some (coe self)
3021 else
3022 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3023 let first = truncate (s *. float source#getitemcount) in
3024 let first = min source#getitemcount first in
3025 Some (coe {< m_first = first; m_active = first >})
3026 else (
3027 state.mstate <- Mnone;
3028 Some (coe self);
3030 | 1 when not down ->
3031 begin match self#elemunder y with
3032 | Some n ->
3033 G.postRedisplay "listview click";
3034 source#exit
3035 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3036 | _ ->
3037 Some (coe self)
3039 | n when (n == 4 || n == 5) && not down ->
3040 let len = source#getitemcount in
3041 let first =
3042 if n = 5 && m_first + fstate.maxrows >= len
3043 then
3044 m_first
3045 else
3046 let first = m_first + (if n == 3 then -1 else 1) in
3047 bound first 0 (len - 1)
3049 G.postRedisplay "listview wheel";
3050 Some (coe {< m_first = first >})
3051 | _ ->
3052 Some (coe self)
3054 match opt with
3055 | None -> m_prev_uioh
3056 | Some uioh -> uioh
3058 method motion _ y =
3059 match state.mstate with
3060 | Mscrolly ->
3061 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3062 let first = truncate (s *. float source#getitemcount) in
3063 let first = min source#getitemcount first in
3064 G.postRedisplay "listview motion";
3065 coe {< m_first = first; m_active = first >}
3066 | _ -> coe self
3068 method pmotion x y =
3069 if x < conf.winw - conf.scrollbw
3070 then
3071 let n =
3072 match self#elemunder y with
3073 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3074 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3076 let o =
3077 if n != m_active
3078 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3079 else self
3081 coe o
3082 else (
3083 Wsi.setcursor Wsi.CURSOR_INHERIT;
3084 coe self
3087 method infochanged _ = ()
3089 method scrollpw = (0, 0.0, 0.0)
3090 method scrollph =
3091 let nfs = fstate.fontsize + 1 in
3092 let y = m_first * nfs in
3093 let itemcount = source#getitemcount in
3094 let maxi = max 0 (itemcount - fstate.maxrows) in
3095 let maxy = maxi * nfs in
3096 let p, h = scrollph y maxy in
3097 conf.scrollbw, p, h
3098 end;;
3100 class outlinelistview ~source =
3101 object (self)
3102 inherit listview ~source:(source :> lvsource) ~trusted:false as super
3104 method key key mask =
3105 let calcfirst first active =
3106 if active > first
3107 then
3108 let rows = active - first in
3109 if rows > fstate.maxrows then active - fstate.maxrows else first
3110 else active
3112 let navigate incr =
3113 let active = m_active + incr in
3114 let active = bound active 0 (source#getitemcount - 1) in
3115 let first = calcfirst m_first active in
3116 G.postRedisplay "outline navigate";
3117 coe {< m_active = active; m_first = first >}
3119 match key with
3120 | 110 when Wsi.withctrl mask -> (* ctrl-n *)
3121 source#narrow m_qsearch;
3122 G.postRedisplay "outline ctrl-n";
3123 coe {< m_first = 0; m_active = 0 >}
3125 | 117 when Wsi.withctrl mask -> (* ctrl-u *)
3126 source#denarrow;
3127 G.postRedisplay "outline ctrl-u";
3128 state.text <- "";
3129 coe {< m_first = 0; m_active = 0 >}
3131 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
3132 let first = m_active - (fstate.maxrows / 2) in
3133 G.postRedisplay "outline ctrl-l";
3134 coe {< m_first = first >}
3136 | 0xff9f | 0xffff -> (* delete *)
3137 source#remove m_active;
3138 G.postRedisplay "outline delete";
3139 let active = max 0 (m_active-1) in
3140 coe {< m_first = firstof m_first active;
3141 m_active = active >}
3143 | 0xff52 -> navigate ~-1 (* up *)
3144 | 0xff54 -> navigate 1 (* down *)
3145 | 0xff55 -> (* prior *)
3146 navigate ~-(fstate.maxrows)
3147 | 0xff56 -> (* next *)
3148 navigate fstate.maxrows
3150 | 0xff53 -> (* [ctrl-]right *)
3151 let o =
3152 if Wsi.withctrl mask
3153 then (
3154 G.postRedisplay "outline ctrl right";
3155 {< m_pan = m_pan + 1 >}
3157 else self#updownlevel 1
3159 coe o
3161 | 0xff51 -> (* [ctrl-]left *)
3162 let o =
3163 if Wsi.withctrl mask
3164 then (
3165 G.postRedisplay "outline ctrl left";
3166 {< m_pan = m_pan - 1 >}
3168 else self#updownlevel ~-1
3170 coe o
3172 | 0xff50 -> (* home *)
3173 G.postRedisplay "outline home";
3174 coe {< m_first = 0; m_active = 0 >}
3176 | 0xff57 -> (* end *)
3177 let active = source#getitemcount - 1 in
3178 let first = max 0 (active - fstate.maxrows) in
3179 G.postRedisplay "outline end";
3180 coe {< m_active = active; m_first = first >}
3182 | _ -> super#key key mask
3185 let outlinesource usebookmarks =
3186 let empty = [||] in
3187 (object
3188 inherit lvsourcebase
3189 val mutable m_items = empty
3190 val mutable m_orig_items = empty
3191 val mutable m_prev_items = empty
3192 val mutable m_narrow_pattern = ""
3193 val mutable m_hadremovals = false
3195 method getitemcount =
3196 Array.length m_items + (if m_hadremovals then 1 else 0)
3198 method getitem n =
3199 if n == Array.length m_items && m_hadremovals
3200 then
3201 ("[Confirm removal]", 0)
3202 else
3203 let s, n, _ = m_items.(n) in
3204 (s, n)
3206 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3207 ignore (uioh, first, qsearch);
3208 let confrimremoval = m_hadremovals && active = Array.length m_items in
3209 let items =
3210 if String.length m_narrow_pattern = 0
3211 then m_orig_items
3212 else m_items
3214 if not cancel
3215 then (
3216 if not confrimremoval
3217 then(
3218 let _, _, anchor = m_items.(active) in
3219 gotoanchor anchor;
3220 m_items <- items;
3222 else (
3223 state.bookmarks <- Array.to_list m_items;
3224 m_orig_items <- m_items;
3227 else m_items <- items;
3228 m_pan <- pan;
3229 None
3231 method hasaction _ = true
3233 method greetmsg =
3234 if Array.length m_items != Array.length m_orig_items
3235 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3236 else ""
3238 method narrow pattern =
3239 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3240 match reopt with
3241 | None -> ()
3242 | Some re ->
3243 let rec loop accu n =
3244 if n = -1
3245 then (
3246 m_narrow_pattern <- pattern;
3247 m_items <- Array.of_list accu
3249 else
3250 let (s, _, _) as o = m_items.(n) in
3251 let accu =
3252 if (try ignore (Str.search_forward re s 0); true
3253 with Not_found -> false)
3254 then o :: accu
3255 else accu
3257 loop accu (n-1)
3259 loop [] (Array.length m_items - 1)
3261 method denarrow =
3262 m_orig_items <- (
3263 if usebookmarks
3264 then Array.of_list state.bookmarks
3265 else state.outlines
3267 m_items <- m_orig_items
3269 method remove m =
3270 if usebookmarks
3271 then
3272 if m >= 0 && m < Array.length m_items
3273 then (
3274 m_hadremovals <- true;
3275 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3276 let n = if n >= m then n+1 else n in
3277 m_items.(n)
3281 method reset anchor items =
3282 m_hadremovals <- false;
3283 if m_orig_items == empty || m_prev_items != items
3284 then (
3285 m_orig_items <- items;
3286 if String.length m_narrow_pattern = 0
3287 then m_items <- items;
3289 m_prev_items <- items;
3290 let rely = getanchory anchor in
3291 let active =
3292 let rec loop n best bestd =
3293 if n = Array.length m_items
3294 then best
3295 else
3296 let (_, _, anchor) = m_items.(n) in
3297 let orely = getanchory anchor in
3298 let d = abs (orely - rely) in
3299 if d < bestd
3300 then loop (n+1) n d
3301 else loop (n+1) best bestd
3303 loop 0 ~-1 max_int
3305 m_active <- active;
3306 m_first <- firstof m_first active
3307 end)
3310 let enterselector usebookmarks =
3311 let source = outlinesource usebookmarks in
3312 fun errmsg ->
3313 let outlines =
3314 if usebookmarks
3315 then Array.of_list state.bookmarks
3316 else state.outlines
3318 if Array.length outlines = 0
3319 then (
3320 showtext ' ' errmsg;
3322 else (
3323 state.text <- source#greetmsg;
3324 Wsi.setcursor Wsi.CURSOR_INHERIT;
3325 let anchor = getanchor () in
3326 source#reset anchor outlines;
3327 state.uioh <- coe (new outlinelistview ~source);
3328 G.postRedisplay "enter selector";
3332 let enteroutlinemode =
3333 let f = enterselector false in
3334 fun ()-> f "Document has no outline";
3337 let enterbookmarkmode =
3338 let f = enterselector true in
3339 fun () -> f "Document has no bookmarks (yet)";
3342 let color_of_string s =
3343 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3344 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3348 let color_to_string (r, g, b) =
3349 let r = truncate (r *. 256.0)
3350 and g = truncate (g *. 256.0)
3351 and b = truncate (b *. 256.0) in
3352 Printf.sprintf "%d/%d/%d" r g b
3355 let irect_of_string s =
3356 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3359 let irect_to_string (x0,y0,x1,y1) =
3360 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3363 let makecheckers () =
3364 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3365 following to say:
3366 converted by Issac Trotts. July 25, 2002 *)
3367 let image_height = 64
3368 and image_width = 64 in
3370 let make_image () =
3371 let image =
3372 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3374 for i = 0 to image_width - 1 do
3375 for j = 0 to image_height - 1 do
3376 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3377 (if (i land 8 ) lxor (j land 8) = 0
3378 then [|255;255;255|] else [|200;200;200|])
3379 done
3380 done;
3381 image
3383 let image = make_image () in
3384 let id = GlTex.gen_texture () in
3385 GlTex.bind_texture `texture_2d id;
3386 GlPix.store (`unpack_alignment 1);
3387 GlTex.image2d image;
3388 List.iter (GlTex.parameter ~target:`texture_2d)
3389 [ `wrap_s `repeat;
3390 `wrap_t `repeat;
3391 `mag_filter `nearest;
3392 `min_filter `nearest ];
3396 let setcheckers enabled =
3397 match state.texid with
3398 | None ->
3399 if enabled then state.texid <- Some (makecheckers ())
3401 | Some texid ->
3402 if not enabled
3403 then (
3404 GlTex.delete_texture texid;
3405 state.texid <- None;
3409 let int_of_string_with_suffix s =
3410 let l = String.length s in
3411 let s1, shift =
3412 if l > 1
3413 then
3414 let suffix = Char.lowercase s.[l-1] in
3415 match suffix with
3416 | 'k' -> String.sub s 0 (l-1), 10
3417 | 'm' -> String.sub s 0 (l-1), 20
3418 | 'g' -> String.sub s 0 (l-1), 30
3419 | _ -> s, 0
3420 else s, 0
3422 let n = int_of_string s1 in
3423 let m = n lsl shift in
3424 if m < 0 || m < n
3425 then raise (Failure "value too large")
3426 else m
3429 let string_with_suffix_of_int n =
3430 if n = 0
3431 then "0"
3432 else
3433 let n, s =
3434 if n = 0
3435 then 0, ""
3436 else (
3437 if n land ((1 lsl 20) - 1) = 0
3438 then n lsr 20, "M"
3439 else (
3440 if n land ((1 lsl 10) - 1) = 0
3441 then n lsr 10, "K"
3442 else n, ""
3446 let rec loop s n =
3447 let h = n mod 1000 in
3448 let n = n / 1000 in
3449 if n = 0
3450 then string_of_int h ^ s
3451 else (
3452 let s = Printf.sprintf "_%03d%s" h s in
3453 loop s n
3456 loop "" n ^ s;
3459 let defghyllscroll = (40, 8, 32);;
3460 let ghyllscroll_of_string s =
3461 let (n, a, b) as nab =
3462 if s = "default"
3463 then defghyllscroll
3464 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3466 if n <= a || n <= b || a >= b
3467 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3468 nab;
3471 let ghyllscroll_to_string ((n, a, b) as nab) =
3472 if nab = defghyllscroll
3473 then "default"
3474 else Printf.sprintf "%d,%d,%d" n a b;
3477 let describe_location () =
3478 let f (fn, _) l =
3479 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3481 let fn, ln = List.fold_left f (-1, -1) state.layout in
3482 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3483 let percent =
3484 if maxy <= 0
3485 then 100.
3486 else (100. *. (float state.y /. float maxy))
3488 if fn = ln
3489 then
3490 Printf.sprintf "page %d of %d [%.2f%%]"
3491 (fn+1) state.pagecount percent
3492 else
3493 Printf.sprintf
3494 "pages %d-%d of %d [%.2f%%]"
3495 (fn+1) (ln+1) state.pagecount percent
3498 let enterinfomode =
3499 let btos b = if b then "\xe2\x88\x9a" else "" in
3500 let showextended = ref false in
3501 let leave mode = function
3502 | Confirm -> state.mode <- mode
3503 | Cancel -> state.mode <- mode in
3504 let src =
3505 (object
3506 val mutable m_first_time = true
3507 val mutable m_l = []
3508 val mutable m_a = [||]
3509 val mutable m_prev_uioh = nouioh
3510 val mutable m_prev_mode = View
3512 inherit lvsourcebase
3514 method reset prev_mode prev_uioh =
3515 m_a <- Array.of_list (List.rev m_l);
3516 m_l <- [];
3517 m_prev_mode <- prev_mode;
3518 m_prev_uioh <- prev_uioh;
3519 if m_first_time
3520 then (
3521 let rec loop n =
3522 if n >= Array.length m_a
3523 then ()
3524 else
3525 match m_a.(n) with
3526 | _, _, _, Action _ -> m_active <- n
3527 | _ -> loop (n+1)
3529 loop 0;
3530 m_first_time <- false;
3533 method int name get set =
3534 m_l <-
3535 (name, `int get, 1, Action (
3536 fun u ->
3537 let ondone s =
3538 try set (int_of_string s)
3539 with exn ->
3540 state.text <- Printf.sprintf "bad integer `%s': %s"
3541 s (Printexc.to_string exn)
3543 state.text <- "";
3544 let te = name ^ ": ", "", None, intentry, ondone in
3545 state.mode <- Textentry (te, leave m_prev_mode);
3547 )) :: m_l
3549 method int_with_suffix name get set =
3550 m_l <-
3551 (name, `intws get, 1, Action (
3552 fun u ->
3553 let ondone s =
3554 try set (int_of_string_with_suffix 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 =
3561 name ^ ": ", "", None, intentry_with_suffix, ondone
3563 state.mode <- Textentry (te, leave m_prev_mode);
3565 )) :: m_l
3567 method bool ?(offset=1) ?(btos=btos) name get set =
3568 m_l <-
3569 (name, `bool (btos, get), offset, Action (
3570 fun u ->
3571 let v = get () in
3572 set (not v);
3574 )) :: m_l
3576 method color name get set =
3577 m_l <-
3578 (name, `color get, 1, Action (
3579 fun u ->
3580 let invalid = (nan, nan, nan) in
3581 let ondone s =
3582 let c =
3583 try color_of_string s
3584 with exn ->
3585 state.text <- Printf.sprintf "bad color `%s': %s"
3586 s (Printexc.to_string exn);
3587 invalid
3589 if c <> invalid
3590 then set c;
3592 let te = name ^ ": ", "", None, textentry, ondone in
3593 state.text <- color_to_string (get ());
3594 state.mode <- Textentry (te, leave m_prev_mode);
3596 )) :: m_l
3598 method string name get set =
3599 m_l <-
3600 (name, `string get, 1, Action (
3601 fun u ->
3602 let ondone s = set s in
3603 let te = name ^ ": ", "", None, textentry, ondone in
3604 state.mode <- Textentry (te, leave m_prev_mode);
3606 )) :: m_l
3608 method colorspace name get set =
3609 m_l <-
3610 (name, `string get, 1, Action (
3611 fun _ ->
3612 let source =
3613 let vals = [| "rgb"; "bgr"; "gray" |] in
3614 (object
3615 inherit lvsourcebase
3617 initializer
3618 m_active <- int_of_colorspace conf.colorspace;
3619 m_first <- 0;
3621 method getitemcount = Array.length vals
3622 method getitem n = (vals.(n), 0)
3623 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3624 ignore (uioh, first, pan, qsearch);
3625 if not cancel then set active;
3626 None
3627 method hasaction _ = true
3628 end)
3630 state.text <- "";
3631 coe (new listview ~source ~trusted:true)
3632 )) :: m_l
3634 method caption s offset =
3635 m_l <- (s, `empty, offset, Noaction) :: m_l
3637 method caption2 s f offset =
3638 m_l <- (s, `string f, offset, Noaction) :: m_l
3640 method getitemcount = Array.length m_a
3642 method getitem n =
3643 let tostr = function
3644 | `int f -> string_of_int (f ())
3645 | `intws f -> string_with_suffix_of_int (f ())
3646 | `string f -> f ()
3647 | `color f -> color_to_string (f ())
3648 | `bool (btos, f) -> btos (f ())
3649 | `empty -> ""
3651 let name, t, offset, _ = m_a.(n) in
3652 ((let s = tostr t in
3653 if String.length s > 0
3654 then Printf.sprintf "%s\t%s" name s
3655 else name),
3656 offset)
3658 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3659 let uiohopt =
3660 if not cancel
3661 then (
3662 m_qsearch <- qsearch;
3663 let uioh =
3664 match m_a.(active) with
3665 | _, _, _, Action f -> f uioh
3666 | _ -> uioh
3668 Some uioh
3670 else None
3672 m_active <- active;
3673 m_first <- first;
3674 m_pan <- pan;
3675 uiohopt
3677 method hasaction n =
3678 match m_a.(n) with
3679 | _, _, _, Action _ -> true
3680 | _ -> false
3681 end)
3683 let rec fillsrc prevmode prevuioh =
3684 let sep () = src#caption "" 0 in
3685 let colorp name get set =
3686 src#string name
3687 (fun () -> color_to_string (get ()))
3688 (fun v ->
3690 let c = color_of_string v in
3691 set c
3692 with exn ->
3693 state.text <- Printf.sprintf "bad color `%s': %s"
3694 v (Printexc.to_string exn);
3697 let oldmode = state.mode in
3698 let birdseye = isbirdseye state.mode in
3700 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3702 src#bool "presentation mode"
3703 (fun () -> conf.presentation)
3704 (fun v ->
3705 conf.presentation <- v;
3706 state.anchor <- getanchor ();
3707 represent ());
3709 src#bool "ignore case in searches"
3710 (fun () -> conf.icase)
3711 (fun v -> conf.icase <- v);
3713 src#bool "preload"
3714 (fun () -> conf.preload)
3715 (fun v -> conf.preload <- v);
3717 src#bool "highlight links"
3718 (fun () -> conf.hlinks)
3719 (fun v -> conf.hlinks <- v);
3721 src#bool "under info"
3722 (fun () -> conf.underinfo)
3723 (fun v -> conf.underinfo <- v);
3725 src#bool "persistent bookmarks"
3726 (fun () -> conf.savebmarks)
3727 (fun v -> conf.savebmarks <- v);
3729 src#bool "proportional display"
3730 (fun () -> conf.proportional)
3731 (fun v -> reqlayout conf.angle v);
3733 src#bool "trim margins"
3734 (fun () -> conf.trimmargins)
3735 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3737 src#bool "persistent location"
3738 (fun () -> conf.jumpback)
3739 (fun v -> conf.jumpback <- v);
3741 sep ();
3742 src#int "inter-page space"
3743 (fun () -> conf.interpagespace)
3744 (fun n ->
3745 conf.interpagespace <- n;
3746 let pageno, py =
3747 match state.layout with
3748 | [] -> 0, 0
3749 | l :: _ ->
3750 l.pageno, l.pagey
3752 state.maxy <- calcheight ();
3753 let y = getpagey pageno in
3754 gotoy (y + py)
3757 src#int "page bias"
3758 (fun () -> conf.pagebias)
3759 (fun v -> conf.pagebias <- v);
3761 src#int "scroll step"
3762 (fun () -> conf.scrollstep)
3763 (fun n -> conf.scrollstep <- n);
3765 src#int "auto scroll step"
3766 (fun () ->
3767 match state.autoscroll with
3768 | Some step -> step
3769 | _ -> conf.autoscrollstep)
3770 (fun n ->
3771 if state.autoscroll <> None
3772 then state.autoscroll <- Some n;
3773 conf.autoscrollstep <- n);
3775 src#int "zoom"
3776 (fun () -> truncate (conf.zoom *. 100.))
3777 (fun v -> setzoom ((float v) /. 100.));
3779 src#int "rotation"
3780 (fun () -> conf.angle)
3781 (fun v -> reqlayout v conf.proportional);
3783 src#int "scroll bar width"
3784 (fun () -> state.scrollw)
3785 (fun v ->
3786 state.scrollw <- v;
3787 conf.scrollbw <- v;
3788 reshape conf.winw conf.winh;
3791 src#int "scroll handle height"
3792 (fun () -> conf.scrollh)
3793 (fun v -> conf.scrollh <- v;);
3795 src#int "thumbnail width"
3796 (fun () -> conf.thumbw)
3797 (fun v ->
3798 conf.thumbw <- min 4096 v;
3799 match oldmode with
3800 | Birdseye beye ->
3801 leavebirdseye beye false;
3802 enterbirdseye ()
3803 | _ -> ()
3806 src#string "columns"
3807 (fun () ->
3808 match conf.columns with
3809 | None -> "1"
3810 | Some (multicol, _) -> columns_to_string multicol)
3811 (fun v ->
3812 let n, a, b = columns_of_string v in
3813 setcolumns n a b);
3815 sep ();
3816 src#caption "Presentation mode" 0;
3817 src#bool "scrollbar visible"
3818 (fun () -> conf.scrollbarinpm)
3819 (fun v ->
3820 if v != conf.scrollbarinpm
3821 then (
3822 conf.scrollbarinpm <- v;
3823 if conf.presentation
3824 then (
3825 state.scrollw <- if v then conf.scrollbw else 0;
3826 reshape conf.winw conf.winh;
3831 sep ();
3832 src#caption "Pixmap cache" 0;
3833 src#int_with_suffix "size (advisory)"
3834 (fun () -> conf.memlimit)
3835 (fun v -> conf.memlimit <- v);
3837 src#caption2 "used"
3838 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3839 (string_with_suffix_of_int state.memused)
3840 (Hashtbl.length state.tilemap)) 1;
3842 sep ();
3843 src#caption "Layout" 0;
3844 src#caption2 "Dimension"
3845 (fun () ->
3846 Printf.sprintf "%dx%d (virtual %dx%d)"
3847 conf.winw conf.winh
3848 state.w state.maxy)
3850 if conf.debug
3851 then
3852 src#caption2 "Position" (fun () ->
3853 Printf.sprintf "%dx%d" state.x state.y
3855 else
3856 src#caption2 "Visible" (fun () -> describe_location ()) 1
3859 sep ();
3860 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3861 "Save these parameters as global defaults at exit"
3862 (fun () -> conf.bedefault)
3863 (fun v -> conf.bedefault <- v)
3866 sep ();
3867 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3868 src#bool ~offset:0 ~btos "Extended parameters"
3869 (fun () -> !showextended)
3870 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3871 if !showextended
3872 then (
3873 src#bool "checkers"
3874 (fun () -> conf.checkers)
3875 (fun v -> conf.checkers <- v; setcheckers v);
3876 src#bool "verbose"
3877 (fun () -> conf.verbose)
3878 (fun v -> conf.verbose <- v);
3879 src#bool "invert colors"
3880 (fun () -> conf.invert)
3881 (fun v -> conf.invert <- v);
3882 src#bool "max fit"
3883 (fun () -> conf.maxhfit)
3884 (fun v -> conf.maxhfit <- v);
3885 src#bool "redirect stderr"
3886 (fun () -> conf.redirectstderr)
3887 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3888 src#string "uri launcher"
3889 (fun () -> conf.urilauncher)
3890 (fun v -> conf.urilauncher <- v);
3891 src#string "tile size"
3892 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3893 (fun v ->
3895 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3896 conf.tileh <- max 64 w;
3897 conf.tilew <- max 64 h;
3898 flushtiles ();
3899 with exn ->
3900 state.text <- Printf.sprintf "bad tile size `%s': %s"
3901 v (Printexc.to_string exn));
3902 src#int "texture count"
3903 (fun () -> conf.texcount)
3904 (fun v ->
3905 if realloctexts v
3906 then conf.texcount <- v
3907 else showtext '!' " Failed to set texture count please retry later"
3909 src#int "slice height"
3910 (fun () -> conf.sliceheight)
3911 (fun v ->
3912 conf.sliceheight <- v;
3913 wcmd "sliceh" [`i conf.sliceheight];
3915 src#int "anti-aliasing level"
3916 (fun () -> conf.aalevel)
3917 (fun v ->
3918 conf.aalevel <- bound v 0 8;
3919 state.anchor <- getanchor ();
3920 opendoc state.path state.password;
3922 src#int "ui font size"
3923 (fun () -> fstate.fontsize)
3924 (fun v -> setfontsize (bound v 5 100));
3925 colorp "background color"
3926 (fun () -> conf.bgcolor)
3927 (fun v -> conf.bgcolor <- v);
3928 src#bool "crop hack"
3929 (fun () -> conf.crophack)
3930 (fun v -> conf.crophack <- v);
3931 src#string "trim fuzz"
3932 (fun () -> irect_to_string conf.trimfuzz)
3933 (fun v ->
3935 conf.trimfuzz <- irect_of_string v;
3936 if conf.trimmargins
3937 then settrim true conf.trimfuzz;
3938 with exn ->
3939 state.text <- Printf.sprintf "bad irect `%s': %s"
3940 v (Printexc.to_string exn)
3942 src#string "throttle"
3943 (fun () ->
3944 match conf.maxwait with
3945 | None -> "show place holder if page is not ready"
3946 | Some time ->
3947 if time = infinity
3948 then "wait for page to fully render"
3949 else
3950 "wait " ^ string_of_float time
3951 ^ " seconds before showing placeholder"
3953 (fun v ->
3955 let f = float_of_string v in
3956 if f <= 0.0
3957 then conf.maxwait <- None
3958 else conf.maxwait <- Some f
3959 with exn ->
3960 state.text <- Printf.sprintf "bad time `%s': %s"
3961 v (Printexc.to_string exn)
3963 src#string "ghyll scroll"
3964 (fun () ->
3965 match conf.ghyllscroll with
3966 | None -> ""
3967 | Some nab -> ghyllscroll_to_string nab
3969 (fun v ->
3971 let gs =
3972 if String.length v = 0
3973 then None
3974 else Some (ghyllscroll_of_string v)
3976 conf.ghyllscroll <- gs
3977 with exn ->
3978 state.text <- Printf.sprintf "bad ghyll `%s': %s"
3979 v (Printexc.to_string exn)
3981 src#string "selection command"
3982 (fun () -> conf.selcmd)
3983 (fun v -> conf.selcmd <- v);
3984 src#colorspace "color space"
3985 (fun () -> colorspace_to_string conf.colorspace)
3986 (fun v ->
3987 conf.colorspace <- colorspace_of_int v;
3988 wcmd "cs" [`i v];
3989 load state.layout;
3993 sep ();
3994 src#caption "Document" 0;
3995 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3996 src#caption2 "Pages"
3997 (fun () -> string_of_int state.pagecount) 1;
3998 src#caption2 "Dimensions"
3999 (fun () -> string_of_int (List.length state.pdims)) 1;
4000 if conf.trimmargins
4001 then (
4002 sep ();
4003 src#caption "Trimmed margins" 0;
4004 src#caption2 "Dimensions"
4005 (fun () -> string_of_int (List.length state.pdims)) 1;
4008 src#reset prevmode prevuioh;
4010 fun () ->
4011 state.text <- "";
4012 let prevmode = state.mode
4013 and prevuioh = state.uioh in
4014 fillsrc prevmode prevuioh;
4015 let source = (src :> lvsource) in
4016 state.uioh <- coe (object (self)
4017 inherit listview ~source ~trusted:true as super
4018 val mutable m_prevmemused = 0
4019 method infochanged = function
4020 | Memused ->
4021 if m_prevmemused != state.memused
4022 then (
4023 m_prevmemused <- state.memused;
4024 G.postRedisplay "memusedchanged";
4026 | Pdim -> G.postRedisplay "pdimchanged"
4027 | Docinfo -> fillsrc prevmode prevuioh
4029 method key key mask =
4030 if not (Wsi.withctrl mask)
4031 then
4032 match key with
4033 | 0xff51 -> coe (self#updownlevel ~-1)
4034 | 0xff53 -> coe (self#updownlevel 1)
4035 | _ -> super#key key mask
4036 else super#key key mask
4037 end);
4038 G.postRedisplay "info";
4041 let enterhelpmode =
4042 let source =
4043 (object
4044 inherit lvsourcebase
4045 method getitemcount = Array.length state.help
4046 method getitem n =
4047 let s, n, _ = state.help.(n) in
4048 (s, n)
4050 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4051 let optuioh =
4052 if not cancel
4053 then (
4054 m_qsearch <- qsearch;
4055 match state.help.(active) with
4056 | _, _, Action f -> Some (f uioh)
4057 | _ -> Some (uioh)
4059 else None
4061 m_active <- active;
4062 m_first <- first;
4063 m_pan <- pan;
4064 optuioh
4066 method hasaction n =
4067 match state.help.(n) with
4068 | _, _, Action _ -> true
4069 | _ -> false
4071 initializer
4072 m_active <- -1
4073 end)
4074 in fun () ->
4075 state.uioh <- coe (new listview ~source ~trusted:true);
4076 G.postRedisplay "help";
4079 let entermsgsmode =
4080 let msgsource =
4081 let re = Str.regexp "[\r\n]" in
4082 (object
4083 inherit lvsourcebase
4084 val mutable m_items = [||]
4086 method getitemcount = 1 + Array.length m_items
4088 method getitem n =
4089 if n = 0
4090 then "[Clear]", 0
4091 else m_items.(n-1), 0
4093 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4094 ignore uioh;
4095 if not cancel
4096 then (
4097 if active = 0
4098 then Buffer.clear state.errmsgs;
4099 m_qsearch <- qsearch;
4101 m_active <- active;
4102 m_first <- first;
4103 m_pan <- pan;
4104 None
4106 method hasaction n =
4107 n = 0
4109 method reset =
4110 state.newerrmsgs <- false;
4111 let l = Str.split re (Buffer.contents state.errmsgs) in
4112 m_items <- Array.of_list l
4114 initializer
4115 m_active <- 0
4116 end)
4117 in fun () ->
4118 state.text <- "";
4119 msgsource#reset;
4120 let source = (msgsource :> lvsource) in
4121 state.uioh <- coe (object
4122 inherit listview ~source ~trusted:false as super
4123 method display =
4124 if state.newerrmsgs
4125 then msgsource#reset;
4126 super#display
4127 end);
4128 G.postRedisplay "msgs";
4131 let quickbookmark ?title () =
4132 match state.layout with
4133 | [] -> ()
4134 | l :: _ ->
4135 let title =
4136 match title with
4137 | None ->
4138 let sec = Unix.gettimeofday () in
4139 let tm = Unix.localtime sec in
4140 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4141 (l.pageno+1)
4142 tm.Unix.tm_mday
4143 tm.Unix.tm_mon
4144 (tm.Unix.tm_year + 1900)
4145 tm.Unix.tm_hour
4146 tm.Unix.tm_min
4147 | Some title -> title
4149 state.bookmarks <-
4150 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4151 :: state.bookmarks
4154 let doreshape w h =
4155 state.fullscreen <- None;
4156 Wsi.reshape w h;
4159 let setautoscrollspeed step goingdown =
4160 let incr = max 1 ((abs step) / 2) in
4161 let incr = if goingdown then incr else -incr in
4162 let astep = step + incr in
4163 state.autoscroll <- Some astep;
4166 let viewkeyboard key mask =
4167 let enttext te =
4168 let mode = state.mode in
4169 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4170 state.text <- "";
4171 enttext ();
4172 G.postRedisplay "view:enttext"
4174 match key with
4175 | 81 -> (* Q *)
4176 exit 0
4178 | 0xff1b | 113 -> (* escape / q *)
4179 begin match state.mstate with
4180 | Mzoomrect _ ->
4181 state.mstate <- Mnone;
4182 Wsi.setcursor Wsi.CURSOR_INHERIT;
4183 G.postRedisplay "kill zoom rect";
4184 | _ ->
4185 match state.ranchors with
4186 | [] -> raise Wsi.Quit
4187 | (path, password, anchor) :: rest ->
4188 state.ranchors <- rest;
4189 state.anchor <- anchor;
4190 opendoc path password
4191 end;
4193 | 0xff08 -> (* backspace *)
4194 let y = getnav ~-1 in
4195 gotoy_and_clear_text y
4197 | 111 -> (* o *)
4198 enteroutlinemode ()
4200 | 117 -> (* u *)
4201 state.rects <- [];
4202 state.text <- "";
4203 G.postRedisplay "dehighlight";
4205 | 47 | 63 -> (* / ? *)
4206 let ondone isforw s =
4207 cbput state.hists.pat s;
4208 state.searchpattern <- s;
4209 search s isforw
4211 let s = String.create 1 in
4212 s.[0] <- Char.chr key;
4213 enttext (s, "", Some (onhist state.hists.pat),
4214 textentry, ondone (key = 47))
4216 | 43 | 0xffab when Wsi.withctrl mask -> (* + *)
4217 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4218 setzoom (conf.zoom +. incr)
4220 | 43 | 0xffab -> (* + *)
4221 let ondone s =
4222 let n =
4223 try int_of_string s with exc ->
4224 state.text <- Printf.sprintf "bad integer `%s': %s"
4225 s (Printexc.to_string exc);
4226 max_int
4228 if n != max_int
4229 then (
4230 conf.pagebias <- n;
4231 state.text <- "page bias is now " ^ string_of_int n;
4234 enttext ("page bias: ", "", None, intentry, ondone)
4236 | 45 | 0xffad when Wsi.withctrl mask -> (* - *)
4237 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4238 setzoom (max 0.01 (conf.zoom -. decr))
4240 | 45 | 0xffad -> (* - *)
4241 let ondone msg = state.text <- msg in
4242 enttext (
4243 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4244 optentry state.mode, ondone
4247 | 48 when Wsi.withctrl mask -> (* 0 *)
4248 setzoom 1.0
4250 | 49 when Wsi.withctrl mask -> (* 1 *)
4251 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4252 if zoom < 1.0
4253 then setzoom zoom
4255 | 0xffc6 -> (* f9 *)
4256 togglebirdseye ()
4258 | 57 when Wsi.withctrl mask -> (* ctrl-9 *)
4259 togglebirdseye ()
4261 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4262 when not (Wsi.withctrl mask) -> (* 0..9 *)
4263 let ondone s =
4264 let n =
4265 try int_of_string s with exc ->
4266 state.text <- Printf.sprintf "bad integer `%s': %s"
4267 s (Printexc.to_string exc);
4270 if n >= 0
4271 then (
4272 addnav ();
4273 cbput state.hists.pag (string_of_int n);
4274 gotopage1 (n + conf.pagebias - 1) 0;
4277 let pageentry text key =
4278 match Char.unsafe_chr key with
4279 | 'g' -> TEdone text
4280 | _ -> intentry text key
4282 let text = "x" in text.[0] <- Char.chr key;
4283 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4285 | 98 -> (* b *)
4286 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4287 reshape conf.winw conf.winh;
4289 | 108 -> (* l *)
4290 conf.hlinks <- not conf.hlinks;
4291 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4292 G.postRedisplay "toggle highlightlinks";
4294 | 97 -> (* a *)
4295 begin match state.autoscroll with
4296 | Some step ->
4297 conf.autoscrollstep <- step;
4298 state.autoscroll <- None
4299 | None ->
4300 if conf.autoscrollstep = 0
4301 then state.autoscroll <- Some 1
4302 else state.autoscroll <- Some conf.autoscrollstep
4305 | 80 -> (* P *)
4306 conf.presentation <- not conf.presentation;
4307 if conf.presentation
4308 then (
4309 if not conf.scrollbarinpm
4310 then state.scrollw <- 0;
4312 else
4313 state.scrollw <- conf.scrollbw;
4315 showtext ' ' ("presentation mode " ^
4316 if conf.presentation then "on" else "off");
4317 state.anchor <- getanchor ();
4318 represent ()
4320 | 102 -> (* f *)
4321 begin match state.fullscreen with
4322 | None ->
4323 state.fullscreen <- Some (conf.winw, conf.winh);
4324 Wsi.fullscreen ()
4325 | Some (w, h) ->
4326 state.fullscreen <- None;
4327 doreshape w h
4330 | 103 -> (* g *)
4331 gotoy_and_clear_text 0
4333 | 71 -> (* G *)
4334 gotopage1 (state.pagecount - 1) 0
4336 | 112 | 78 -> (* p|N *)
4337 search state.searchpattern false
4339 | 110 -> (* n *)
4340 search state.searchpattern true
4342 | 116 -> (* t *)
4343 begin match state.layout with
4344 | [] -> ()
4345 | l :: _ ->
4346 gotoy_and_clear_text (getpagey l.pageno)
4349 | 32 -> (* ' ' *)
4350 begin match List.rev state.layout with
4351 | [] -> ()
4352 | l :: _ ->
4353 let pageno = min (l.pageno+1) (state.pagecount-1) in
4354 gotoy_and_clear_text (getpagey pageno)
4357 | 0xff9f | 0xffff -> (* delete *)
4358 begin match state.layout with
4359 | [] -> ()
4360 | l :: _ ->
4361 let pageno = max 0 (l.pageno-1) in
4362 gotoy_and_clear_text (getpagey pageno)
4365 | 61 -> (* = *)
4366 showtext ' ' (describe_location ());
4368 | 119 -> (* w *)
4369 begin match state.layout with
4370 | [] -> ()
4371 | l :: _ ->
4372 doreshape (l.pagew + state.scrollw) l.pageh;
4373 G.postRedisplay "w"
4376 | 39 -> (* ' *)
4377 enterbookmarkmode ()
4379 | 104 -> (* h *)
4380 enterhelpmode ()
4382 | 105 -> (* i *)
4383 enterinfomode ()
4385 | 101 when conf.redirectstderr -> (* e *)
4386 entermsgsmode ()
4388 | 109 -> (* m *)
4389 let ondone s =
4390 match state.layout with
4391 | l :: _ ->
4392 state.bookmarks <-
4393 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4394 :: state.bookmarks
4395 | _ -> ()
4397 enttext ("bookmark: ", "", None, textentry, ondone)
4399 | 126 -> (* ~ *)
4400 quickbookmark ();
4401 showtext ' ' "Quick bookmark added";
4403 | 122 -> (* z *)
4404 begin match state.layout with
4405 | l :: _ ->
4406 let rect = getpdimrect l.pagedimno in
4407 let w, h =
4408 if conf.crophack
4409 then
4410 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4411 truncate (1.2 *. (rect.(3) -. rect.(0))))
4412 else
4413 (truncate (rect.(1) -. rect.(0)),
4414 truncate (rect.(3) -. rect.(0)))
4416 let w = truncate ((float w)*.conf.zoom)
4417 and h = truncate ((float h)*.conf.zoom) in
4418 if w != 0 && h != 0
4419 then (
4420 state.anchor <- getanchor ();
4421 doreshape (w + state.scrollw) (h + conf.interpagespace)
4423 G.postRedisplay "z";
4425 | [] -> ()
4428 | 50 when Wsi.withctrl mask -> (* ctrl-2 *)
4429 let maxw = getmaxw () in
4430 if maxw > 0.0
4431 then setzoom (maxw /. float conf.winw)
4433 | 60 | 62 -> (* < > *)
4434 reqlayout (conf.angle + (if key = 60 then 30 else -30)) conf.proportional
4436 | 91 | 93 -> (* [ ] *)
4437 conf.colorscale <-
4438 bound (conf.colorscale +. (if key = 92 then 0.1 else -0.1)) 0.0 1.0
4440 G.postRedisplay "brightness";
4442 | 107 | 0xff52 -> (* k UP *)
4443 begin match state.autoscroll with
4444 | None ->
4445 begin match state.mode with
4446 | Birdseye beye -> upbirdseye 1 beye
4447 | _ ->
4448 if Wsi.withctrl mask
4449 then gotoy (-clamp (conf.winh/2))
4450 else gotoy (clamp (-conf.scrollstep))
4452 | Some n ->
4453 setautoscrollspeed n false
4456 | 106 | 0xff54 -> (* j DOWN *)
4457 begin match state.autoscroll with
4458 | None ->
4459 begin match state.mode with
4460 | Birdseye beye -> downbirdseye 1 beye
4461 | _ ->
4462 if Wsi.withctrl mask
4463 then gotoy (clamp (conf.winh/2))
4464 else gotoy (clamp conf.scrollstep)
4466 | Some n ->
4467 setautoscrollspeed n true
4470 | 0xff51 | 0xff53 (* left / right *)
4471 when conf.zoom > 1.0->
4472 let dx =
4473 if Wsi.withctrl mask
4474 then conf.winw / 2
4475 else 10
4477 let dx = if key = 0xff51 then dx else -dx in
4478 state.x <- state.x + dx;
4479 gotoy state.y
4481 | 0xff55 -> (* prior *)
4482 let y =
4483 if Wsi.withctrl mask
4484 then
4485 match state.layout with
4486 | [] -> state.y
4487 | l :: _ -> state.y - l.pagey
4488 else
4489 clamp (-conf.winh)
4491 gotoghyll y
4493 | 0xff56 -> (* next *)
4494 let y =
4495 if Wsi.withctrl mask
4496 then
4497 match List.rev state.layout with
4498 | [] -> state.y
4499 | l :: _ -> getpagey l.pageno
4500 else
4501 clamp conf.winh
4503 gotoghyll y
4505 | 0xff50 -> gotoghyll 0
4506 | 0xff57 -> gotoghyll (clamp state.maxy)
4507 | 0xff53 when Wsi.withalt mask ->
4508 gotoghyll (getnav ~-1)
4509 | 0xff51 when Wsi.withalt mask ->
4510 gotoghyll (getnav 1)
4512 | 114 -> (* r *)
4513 state.anchor <- getanchor ();
4514 opendoc state.path state.password
4516 | 118 when conf.debug -> (* v *)
4517 state.rects <- [];
4518 List.iter (fun l ->
4519 match getopaque l.pageno with
4520 | None -> ()
4521 | Some opaque ->
4522 let x0, y0, x1, y1 = pagebbox opaque in
4523 let a,b = float x0, float y0 in
4524 let c,d = float x1, float y0 in
4525 let e,f = float x1, float y1 in
4526 let h,j = float x0, float y1 in
4527 let rect = (a,b,c,d,e,f,h,j) in
4528 debugrect rect;
4529 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4530 ) state.layout;
4531 G.postRedisplay "v";
4533 | _ ->
4534 vlog "huh? %d" key
4537 let keyboard key mask =
4538 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4539 then wcmd "interrupt" []
4540 else state.uioh <- state.uioh#key key mask
4543 let birdseyekeyboard key mask
4544 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4545 let incr =
4546 match conf.columns with
4547 | None -> 1
4548 | Some ((c, _, _), _) -> c
4550 match key with
4551 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4552 let y, h = getpageyh pageno in
4553 let top = (conf.winh - h) / 2 in
4554 gotoy (max 0 (y - top))
4555 | 0xff0d -> leavebirdseye beye false
4556 | 0xff1b -> leavebirdseye beye true (* escape *)
4557 | 0xff52 -> upbirdseye incr beye (* prior *)
4558 | 0xff54 -> downbirdseye incr beye (* next *)
4559 | 0xff51 -> upbirdseye 1 beye (* up *)
4560 | 0xff53 -> downbirdseye 1 beye (* down *)
4562 | 0xff55 ->
4563 begin match state.layout with
4564 | l :: _ ->
4565 if l.pagey != 0
4566 then (
4567 state.mode <- Birdseye (
4568 oconf, leftx, l.pageno, hooverpageno, anchor
4570 gotopage1 l.pageno 0;
4572 else (
4573 let layout = layout (state.y-conf.winh) conf.winh in
4574 match layout with
4575 | [] -> gotoy (clamp (-conf.winh))
4576 | l :: _ ->
4577 state.mode <- Birdseye (
4578 oconf, leftx, l.pageno, hooverpageno, anchor
4580 gotopage1 l.pageno 0
4583 | [] -> gotoy (clamp (-conf.winh))
4584 end;
4586 | 0xff56 ->
4587 begin match List.rev state.layout with
4588 | l :: _ ->
4589 let layout = layout (state.y + conf.winh) conf.winh in
4590 begin match layout with
4591 | [] ->
4592 let incr = l.pageh - l.pagevh in
4593 if incr = 0
4594 then (
4595 state.mode <-
4596 Birdseye (
4597 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4599 G.postRedisplay "birdseye pagedown";
4601 else gotoy (clamp (incr + conf.interpagespace*2));
4603 | l :: _ ->
4604 state.mode <-
4605 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4606 gotopage1 l.pageno 0;
4609 | [] -> gotoy (clamp conf.winh)
4610 end;
4612 | 0xff50 ->
4613 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4614 gotopage1 0 0
4616 | 0xff57 ->
4617 let pageno = state.pagecount - 1 in
4618 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4619 if not (pagevisible state.layout pageno)
4620 then
4621 let h =
4622 match List.rev state.pdims with
4623 | [] -> conf.winh
4624 | (_, _, h, _) :: _ -> h
4626 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4627 else G.postRedisplay "birdseye end";
4628 | _ -> viewkeyboard key mask
4631 let drawpage l =
4632 let color =
4633 match state.mode with
4634 | Textentry _ -> scalecolor 0.4
4635 | View -> scalecolor 1.0
4636 | Birdseye (_, _, pageno, hooverpageno, _) ->
4637 if l.pageno = hooverpageno
4638 then scalecolor 0.9
4639 else (
4640 if l.pageno = pageno
4641 then scalecolor 1.0
4642 else scalecolor 0.8
4645 drawtiles l color;
4646 begin match getopaque l.pageno with
4647 | Some opaque ->
4648 if tileready l l.pagex l.pagey
4649 then
4650 let x = l.pagedispx - l.pagex
4651 and y = l.pagedispy - l.pagey in
4652 postprocess opaque conf.hlinks x y;
4654 | _ -> ()
4655 end;
4658 let scrollindicator () =
4659 let sbw, ph, sh = state.uioh#scrollph in
4660 let sbh, pw, sw = state.uioh#scrollpw in
4662 GlDraw.color (0.64, 0.64, 0.64);
4663 GlDraw.rect
4664 (float (conf.winw - sbw), 0.)
4665 (float conf.winw, float conf.winh)
4667 GlDraw.rect
4668 (0., float (conf.winh - sbh))
4669 (float (conf.winw - state.scrollw - 1), float conf.winh)
4671 GlDraw.color (0.0, 0.0, 0.0);
4673 GlDraw.rect
4674 (float (conf.winw - sbw), ph)
4675 (float conf.winw, ph +. sh)
4677 GlDraw.rect
4678 (pw, float (conf.winh - sbh))
4679 (pw +. sw, float conf.winh)
4683 let showsel () =
4684 match state.mstate with
4685 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4688 | Msel ((x0, y0), (x1, y1)) ->
4689 let rec loop = function
4690 | l :: ls ->
4691 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4692 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4693 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4694 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4695 then
4696 match getopaque l.pageno with
4697 | Some opaque ->
4698 let dx, dy = pagetranslatepoint l 0 0 in
4699 let x0 = x0 + dx
4700 and y0 = y0 + dy
4701 and x1 = x1 + dx
4702 and y1 = y1 + dy in
4703 GlMat.mode `modelview;
4704 GlMat.push ();
4705 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4706 seltext opaque (x0, y0, x1, y1);
4707 GlMat.pop ();
4708 | _ -> ()
4709 else loop ls
4710 | [] -> ()
4712 loop state.layout
4715 let showrects () =
4716 Gl.enable `blend;
4717 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4718 GlDraw.polygon_mode `both `fill;
4719 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4720 List.iter
4721 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4722 List.iter (fun l ->
4723 if l.pageno = pageno
4724 then (
4725 let dx = float (l.pagedispx - l.pagex) in
4726 let dy = float (l.pagedispy - l.pagey) in
4727 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4728 GlDraw.begins `quads;
4730 GlDraw.vertex2 (x0+.dx, y0+.dy);
4731 GlDraw.vertex2 (x1+.dx, y1+.dy);
4732 GlDraw.vertex2 (x2+.dx, y2+.dy);
4733 GlDraw.vertex2 (x3+.dx, y3+.dy);
4735 GlDraw.ends ();
4737 ) state.layout
4738 ) state.rects
4740 Gl.disable `blend;
4743 let display () =
4744 GlClear.color (scalecolor2 conf.bgcolor);
4745 GlClear.clear [`color];
4746 List.iter drawpage state.layout;
4747 showrects ();
4748 showsel ();
4749 state.uioh#display;
4750 scrollindicator ();
4751 begin match state.mstate with
4752 | Mzoomrect ((x0, y0), (x1, y1)) ->
4753 Gl.enable `blend;
4754 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4755 GlDraw.polygon_mode `both `fill;
4756 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4757 GlDraw.rect (float x0, float y0)
4758 (float x1, float y1);
4759 Gl.disable `blend;
4760 | _ -> ()
4761 end;
4762 enttext ();
4763 if conf.updatecurs
4764 then (
4765 let mx, my = state.mpos in
4766 updateunder mx my;
4768 Wsi.swapb ();
4771 let zoomrect x y x1 y1 =
4772 let x0 = min x x1
4773 and x1 = max x x1
4774 and y0 = min y y1 in
4775 gotoy (state.y + y0);
4776 state.anchor <- getanchor ();
4777 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4778 let margin =
4779 if state.w < conf.winw - state.scrollw
4780 then (conf.winw - state.scrollw - state.w) / 2
4781 else 0
4783 state.x <- (state.x + margin) - x0;
4784 setzoom zoom;
4785 Wsi.setcursor Wsi.CURSOR_INHERIT;
4786 state.mstate <- Mnone;
4789 let scrollx x =
4790 let winw = conf.winw - state.scrollw - 1 in
4791 let s = float x /. float winw in
4792 let destx = truncate (float (state.w + winw) *. s) in
4793 state.x <- winw - destx;
4794 gotoy_and_clear_text state.y;
4795 state.mstate <- Mscrollx;
4798 let scrolly y =
4799 let s = float y /. float conf.winh in
4800 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4801 gotoy_and_clear_text desty;
4802 state.mstate <- Mscrolly;
4805 let viewmouse button down x y mask =
4806 match button with
4807 | n when (n == 4 || n == 5) && not down ->
4808 if Wsi.withctrl mask
4809 then (
4810 match state.mstate with
4811 | Mzoom (oldn, i) ->
4812 if oldn = n
4813 then (
4814 if i = 2
4815 then
4816 let incr =
4817 match n with
4818 | 5 ->
4819 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4820 | _ ->
4821 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4823 let zoom = conf.zoom -. incr in
4824 setzoom zoom;
4825 state.mstate <- Mzoom (n, 0);
4826 else
4827 state.mstate <- Mzoom (n, i+1);
4829 else state.mstate <- Mzoom (n, 0)
4831 | _ -> state.mstate <- Mzoom (n, 0)
4833 else (
4834 match state.autoscroll with
4835 | Some step -> setautoscrollspeed step (n=4)
4836 | None ->
4837 let incr =
4838 if n = 4
4839 then -conf.scrollstep
4840 else conf.scrollstep
4842 let incr = incr * 2 in
4843 let y = clamp incr in
4844 gotoy_and_clear_text y
4847 | 1 when Wsi.withctrl mask ->
4848 if down
4849 then (
4850 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
4851 state.mstate <- Mpan (x, y)
4853 else
4854 state.mstate <- Mnone
4856 | 3 ->
4857 if down
4858 then (
4859 Wsi.setcursor Wsi.CURSOR_CYCLE;
4860 let p = (x, y) in
4861 state.mstate <- Mzoomrect (p, p)
4863 else (
4864 match state.mstate with
4865 | Mzoomrect ((x0, y0), _) ->
4866 if abs (x-x0) > 10 && abs (y - y0) > 10
4867 then zoomrect x0 y0 x y
4868 else (
4869 state.mstate <- Mnone;
4870 Wsi.setcursor Wsi.CURSOR_INHERIT;
4871 G.postRedisplay "kill accidental zoom rect";
4873 | _ ->
4874 Wsi.setcursor Wsi.CURSOR_INHERIT;
4875 state.mstate <- Mnone
4878 | 1 when x > conf.winw - state.scrollw ->
4879 if down
4880 then
4881 let _, position, sh = state.uioh#scrollph in
4882 if y > truncate position && y < truncate (position +. sh)
4883 then state.mstate <- Mscrolly
4884 else scrolly y
4885 else
4886 state.mstate <- Mnone
4888 | 1 when y > conf.winh - state.hscrollh ->
4889 if down
4890 then
4891 let _, position, sw = state.uioh#scrollpw in
4892 if x > truncate position && x < truncate (position +. sw)
4893 then state.mstate <- Mscrollx
4894 else scrollx x
4895 else
4896 state.mstate <- Mnone
4898 | 1 ->
4899 let dest = if down then getunder x y else Unone in
4900 begin match dest with
4901 | Ulinkgoto (pageno, top) ->
4902 if pageno >= 0
4903 then (
4904 addnav ();
4905 gotopage1 pageno top;
4908 | Ulinkuri s ->
4909 gotouri s
4911 | Uremote (filename, pageno) ->
4912 let path =
4913 if Sys.file_exists filename
4914 then filename
4915 else
4916 let dir = Filename.dirname state.path in
4917 let path = Filename.concat dir filename in
4918 if Sys.file_exists path
4919 then path
4920 else ""
4922 if String.length path > 0
4923 then (
4924 let anchor = getanchor () in
4925 let ranchor = state.path, state.password, anchor in
4926 state.anchor <- (pageno, 0.0);
4927 state.ranchors <- ranchor :: state.ranchors;
4928 opendoc path "";
4930 else showtext '!' ("Could not find " ^ filename)
4932 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
4934 | Unone when down ->
4935 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
4936 state.mstate <- Mpan (x, y);
4938 | Unone | Utext _ ->
4939 if down
4940 then (
4941 if conf.angle mod 360 = 0
4942 then (
4943 state.mstate <- Msel ((x, y), (x, y));
4944 G.postRedisplay "mouse select";
4947 else (
4948 match state.mstate with
4949 | Mnone -> ()
4951 | Mzoom _ | Mscrollx | Mscrolly ->
4952 state.mstate <- Mnone
4954 | Mzoomrect ((x0, y0), _) ->
4955 zoomrect x0 y0 x y
4957 | Mpan _ ->
4958 Wsi.setcursor Wsi.CURSOR_INHERIT;
4959 state.mstate <- Mnone
4961 | Msel ((_, y0), (_, y1)) ->
4962 let rec loop = function
4963 | [] -> ()
4964 | l :: rest ->
4965 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4966 || ((y1 >= l.pagedispy
4967 && y1 <= (l.pagedispy + l.pagevh)))
4968 then
4969 match getopaque l.pageno with
4970 | Some opaque ->
4971 copysel conf.selcmd opaque;
4972 G.postRedisplay "copysel"
4973 | _ -> ()
4974 else loop rest
4976 loop state.layout;
4977 Wsi.setcursor Wsi.CURSOR_INHERIT;
4978 state.mstate <- Mnone;
4982 | _ -> ()
4985 let birdseyemouse button down x y mask
4986 (conf, leftx, _, hooverpageno, anchor) =
4987 match button with
4988 | 1 when down ->
4989 let rec loop = function
4990 | [] -> ()
4991 | l :: rest ->
4992 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4993 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4994 then (
4995 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4997 else loop rest
4999 loop state.layout
5000 | 3 -> ()
5001 | _ -> viewmouse button down x y mask
5004 let mouse button down x y mask =
5005 state.uioh <- state.uioh#button button down x y mask;
5008 let motion ~x ~y =
5009 state.uioh <- state.uioh#motion x y
5012 let pmotion ~x ~y =
5013 state.uioh <- state.uioh#pmotion x y;
5016 let uioh = object
5017 method display = ()
5019 method key key mask =
5020 begin match state.mode with
5021 | Textentry textentry -> textentrykeyboard key mask textentry
5022 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5023 | View -> viewkeyboard key mask
5024 end;
5025 state.uioh
5027 method button button bstate x y mask =
5028 begin match state.mode with
5029 | View -> viewmouse button bstate x y mask
5030 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5031 | Textentry _ -> ()
5032 end;
5033 state.uioh
5035 method motion x y =
5036 begin match state.mode with
5037 | Textentry _ -> ()
5038 | View | Birdseye _ ->
5039 match state.mstate with
5040 | Mzoom _ | Mnone -> ()
5042 | Mpan (x0, y0) ->
5043 let dx = x - x0
5044 and dy = y0 - y in
5045 state.mstate <- Mpan (x, y);
5046 if conf.zoom > 1.0 then state.x <- state.x + dx;
5047 let y = clamp dy in
5048 gotoy_and_clear_text y
5050 | Msel (a, _) ->
5051 state.mstate <- Msel (a, (x, y));
5052 G.postRedisplay "motion select";
5054 | Mscrolly ->
5055 let y = min conf.winh (max 0 y) in
5056 scrolly y
5058 | Mscrollx ->
5059 let x = min conf.winw (max 0 x) in
5060 scrollx x
5062 | Mzoomrect (p0, _) ->
5063 state.mstate <- Mzoomrect (p0, (x, y));
5064 G.postRedisplay "motion zoomrect";
5065 end;
5066 state.uioh
5068 method pmotion x y =
5069 begin match state.mode with
5070 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5071 let rec loop = function
5072 | [] ->
5073 if hooverpageno != -1
5074 then (
5075 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5076 G.postRedisplay "pmotion birdseye no hoover";
5078 | l :: rest ->
5079 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5080 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5081 then (
5082 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5083 G.postRedisplay "pmotion birdseye hoover";
5085 else loop rest
5087 loop state.layout
5089 | Textentry _ -> ()
5091 | View ->
5092 match state.mstate with
5093 | Mnone -> updateunder x y
5094 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5096 end;
5097 state.uioh
5099 method infochanged _ = ()
5101 method scrollph =
5102 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5103 let p, h = scrollph state.y maxy in
5104 state.scrollw, p, h
5106 method scrollpw =
5107 let winw = conf.winw - state.scrollw - 1 in
5108 let fwinw = float winw in
5109 let sw =
5110 let sw = fwinw /. float state.w in
5111 let sw = fwinw *. sw in
5112 max sw (float conf.scrollh)
5114 let position, sw =
5115 let f = state.w+winw in
5116 let r = float (winw-state.x) /. float f in
5117 let p = fwinw *. r in
5118 p-.sw/.2., sw
5120 let sw =
5121 if position +. sw > fwinw
5122 then fwinw -. position
5123 else sw
5125 state.hscrollh, position, sw
5126 end;;
5128 module Config =
5129 struct
5130 open Parser
5132 let fontpath = ref "";;
5134 let unent s =
5135 let l = String.length s in
5136 let b = Buffer.create l in
5137 unent b s 0 l;
5138 Buffer.contents b;
5141 let home =
5142 try Sys.getenv "HOME"
5143 with exn ->
5144 prerr_endline
5145 ("Can not determine home directory location: " ^
5146 Printexc.to_string exn);
5150 let config_of c attrs =
5151 let apply c k v =
5153 match k with
5154 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5155 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5156 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5157 | "preload" -> { c with preload = bool_of_string v }
5158 | "page-bias" -> { c with pagebias = int_of_string v }
5159 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5160 | "auto-scroll-step" ->
5161 { c with autoscrollstep = max 0 (int_of_string v) }
5162 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5163 | "crop-hack" -> { c with crophack = bool_of_string v }
5164 | "throttle" ->
5165 let mw =
5166 match String.lowercase v with
5167 | "true" -> Some infinity
5168 | "false" -> None
5169 | f -> Some (float_of_string f)
5171 { c with maxwait = mw}
5172 | "highlight-links" -> { c with hlinks = bool_of_string v }
5173 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5174 | "vertical-margin" ->
5175 { c with interpagespace = max 0 (int_of_string v) }
5176 | "zoom" ->
5177 let zoom = float_of_string v /. 100. in
5178 let zoom = max zoom 0.0 in
5179 { c with zoom = zoom }
5180 | "presentation" -> { c with presentation = bool_of_string v }
5181 | "rotation-angle" -> { c with angle = int_of_string v }
5182 | "width" -> { c with winw = max 20 (int_of_string v) }
5183 | "height" -> { c with winh = max 20 (int_of_string v) }
5184 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5185 | "proportional-display" -> { c with proportional = bool_of_string v }
5186 | "pixmap-cache-size" ->
5187 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5188 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5189 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5190 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5191 | "persistent-location" -> { c with jumpback = bool_of_string v }
5192 | "background-color" -> { c with bgcolor = color_of_string v }
5193 | "scrollbar-in-presentation" ->
5194 { c with scrollbarinpm = bool_of_string v }
5195 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5196 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5197 | "mupdf-store-size" ->
5198 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5199 | "checkers" -> { c with checkers = bool_of_string v }
5200 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5201 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5202 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5203 | "uri-launcher" -> { c with urilauncher = unent v }
5204 | "color-space" -> { c with colorspace = colorspace_of_string v }
5205 | "invert-colors" -> { c with invert = bool_of_string v }
5206 | "brightness" -> { c with colorscale = float_of_string v }
5207 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5208 | "ghyllscroll" ->
5209 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5210 | "columns" ->
5211 let nab = columns_of_string v in
5212 { c with columns = Some (nab, [||]) }
5213 | "birds-eye-columns" ->
5214 { c with beyecolumns = Some (max (int_of_string v) 2) }
5215 | "selection-command" -> { c with selcmd = unent v }
5216 | _ -> c
5217 with exn ->
5218 prerr_endline ("Error processing attribute (`" ^
5219 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5222 let rec fold c = function
5223 | [] -> c
5224 | (k, v) :: rest ->
5225 let c = apply c k v in
5226 fold c rest
5228 fold c attrs;
5231 let fromstring f pos n v d =
5232 try f v
5233 with exn ->
5234 dolog "Error processing attribute (%S=%S) at %d\n%s"
5235 n v pos (Printexc.to_string exn)
5240 let bookmark_of attrs =
5241 let rec fold title page rely = function
5242 | ("title", v) :: rest -> fold v page rely rest
5243 | ("page", v) :: rest -> fold title v rely rest
5244 | ("rely", v) :: rest -> fold title page v rest
5245 | _ :: rest -> fold title page rely rest
5246 | [] -> title, page, rely
5248 fold "invalid" "0" "0" attrs
5251 let doc_of attrs =
5252 let rec fold path page rely pan = function
5253 | ("path", v) :: rest -> fold v page rely pan rest
5254 | ("page", v) :: rest -> fold path v rely pan rest
5255 | ("rely", v) :: rest -> fold path page v pan rest
5256 | ("pan", v) :: rest -> fold path page rely v rest
5257 | _ :: rest -> fold path page rely pan rest
5258 | [] -> path, page, rely, pan
5260 fold "" "0" "0" "0" attrs
5263 let setconf dst src =
5264 dst.scrollbw <- src.scrollbw;
5265 dst.scrollh <- src.scrollh;
5266 dst.icase <- src.icase;
5267 dst.preload <- src.preload;
5268 dst.pagebias <- src.pagebias;
5269 dst.verbose <- src.verbose;
5270 dst.scrollstep <- src.scrollstep;
5271 dst.maxhfit <- src.maxhfit;
5272 dst.crophack <- src.crophack;
5273 dst.autoscrollstep <- src.autoscrollstep;
5274 dst.maxwait <- src.maxwait;
5275 dst.hlinks <- src.hlinks;
5276 dst.underinfo <- src.underinfo;
5277 dst.interpagespace <- src.interpagespace;
5278 dst.zoom <- src.zoom;
5279 dst.presentation <- src.presentation;
5280 dst.angle <- src.angle;
5281 dst.winw <- src.winw;
5282 dst.winh <- src.winh;
5283 dst.savebmarks <- src.savebmarks;
5284 dst.memlimit <- src.memlimit;
5285 dst.proportional <- src.proportional;
5286 dst.texcount <- src.texcount;
5287 dst.sliceheight <- src.sliceheight;
5288 dst.thumbw <- src.thumbw;
5289 dst.jumpback <- src.jumpback;
5290 dst.bgcolor <- src.bgcolor;
5291 dst.scrollbarinpm <- src.scrollbarinpm;
5292 dst.tilew <- src.tilew;
5293 dst.tileh <- src.tileh;
5294 dst.mustoresize <- src.mustoresize;
5295 dst.checkers <- src.checkers;
5296 dst.aalevel <- src.aalevel;
5297 dst.trimmargins <- src.trimmargins;
5298 dst.trimfuzz <- src.trimfuzz;
5299 dst.urilauncher <- src.urilauncher;
5300 dst.colorspace <- src.colorspace;
5301 dst.invert <- src.invert;
5302 dst.colorscale <- src.colorscale;
5303 dst.redirectstderr <- src.redirectstderr;
5304 dst.ghyllscroll <- src.ghyllscroll;
5305 dst.columns <- src.columns;
5306 dst.beyecolumns <- src.beyecolumns;
5307 dst.selcmd <- src.selcmd;
5310 let get s =
5311 let h = Hashtbl.create 10 in
5312 let dc = { defconf with angle = defconf.angle } in
5313 let rec toplevel v t spos _ =
5314 match t with
5315 | Vdata | Vcdata | Vend -> v
5316 | Vopen ("llppconfig", _, closed) ->
5317 if closed
5318 then v
5319 else { v with f = llppconfig }
5320 | Vopen _ ->
5321 error "unexpected subelement at top level" s spos
5322 | Vclose _ -> error "unexpected close at top level" s spos
5324 and llppconfig v t spos _ =
5325 match t with
5326 | Vdata | Vcdata -> v
5327 | Vend -> error "unexpected end of input in llppconfig" s spos
5328 | Vopen ("defaults", attrs, closed) ->
5329 let c = config_of dc attrs in
5330 setconf dc c;
5331 if closed
5332 then v
5333 else { v with f = skip "defaults" (fun () -> v) }
5335 | Vopen ("ui-font", attrs, closed) ->
5336 let rec getsize size = function
5337 | [] -> size
5338 | ("size", v) :: rest ->
5339 let size =
5340 fromstring int_of_string spos "size" v fstate.fontsize in
5341 getsize size rest
5342 | l -> getsize size l
5344 fstate.fontsize <- getsize fstate.fontsize attrs;
5345 if closed
5346 then v
5347 else { v with f = uifont (Buffer.create 10) }
5349 | Vopen ("doc", attrs, closed) ->
5350 let pathent, spage, srely, span = doc_of attrs in
5351 let path = unent pathent
5352 and pageno = fromstring int_of_string spos "page" spage 0
5353 and rely = fromstring float_of_string spos "rely" srely 0.0
5354 and pan = fromstring int_of_string spos "pan" span 0 in
5355 let c = config_of dc attrs in
5356 let anchor = (pageno, rely) in
5357 if closed
5358 then (Hashtbl.add h path (c, [], pan, anchor); v)
5359 else { v with f = doc path pan anchor c [] }
5361 | Vopen _ ->
5362 error "unexpected subelement in llppconfig" s spos
5364 | Vclose "llppconfig" -> { v with f = toplevel }
5365 | Vclose _ -> error "unexpected close in llppconfig" s spos
5367 and uifont b v t spos epos =
5368 match t with
5369 | Vdata | Vcdata ->
5370 Buffer.add_substring b s spos (epos - spos);
5372 | Vopen (_, _, _) ->
5373 error "unexpected subelement in ui-font" s spos
5374 | Vclose "ui-font" ->
5375 if String.length !fontpath = 0
5376 then fontpath := Buffer.contents b;
5377 { v with f = llppconfig }
5378 | Vclose _ -> error "unexpected close in ui-font" s spos
5379 | Vend -> error "unexpected end of input in ui-font" s spos
5381 and doc path pan anchor c bookmarks v t spos _ =
5382 match t with
5383 | Vdata | Vcdata -> v
5384 | Vend -> error "unexpected end of input in doc" s spos
5385 | Vopen ("bookmarks", _, closed) ->
5386 if closed
5387 then v
5388 else { v with f = pbookmarks path pan anchor c bookmarks }
5390 | Vopen (_, _, _) ->
5391 error "unexpected subelement in doc" s spos
5393 | Vclose "doc" ->
5394 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5395 { v with f = llppconfig }
5397 | Vclose _ -> error "unexpected close in doc" s spos
5399 and pbookmarks path pan anchor c bookmarks v t spos _ =
5400 match t with
5401 | Vdata | Vcdata -> v
5402 | Vend -> error "unexpected end of input in bookmarks" s spos
5403 | Vopen ("item", attrs, closed) ->
5404 let titleent, spage, srely = bookmark_of attrs in
5405 let page = fromstring int_of_string spos "page" spage 0
5406 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5407 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5408 if closed
5409 then { v with f = pbookmarks path pan anchor c bookmarks }
5410 else
5411 let f () = v in
5412 { v with f = skip "item" f }
5414 | Vopen _ ->
5415 error "unexpected subelement in bookmarks" s spos
5417 | Vclose "bookmarks" ->
5418 { v with f = doc path pan anchor c bookmarks }
5420 | Vclose _ -> error "unexpected close in bookmarks" s spos
5422 and skip tag f v t spos _ =
5423 match t with
5424 | Vdata | Vcdata -> v
5425 | Vend ->
5426 error ("unexpected end of input in skipped " ^ tag) s spos
5427 | Vopen (tag', _, closed) ->
5428 if closed
5429 then v
5430 else
5431 let f' () = { v with f = skip tag f } in
5432 { v with f = skip tag' f' }
5433 | Vclose ctag ->
5434 if tag = ctag
5435 then f ()
5436 else error ("unexpected close in skipped " ^ tag) s spos
5439 parse { f = toplevel; accu = () } s;
5440 h, dc;
5443 let do_load f ic =
5445 let len = in_channel_length ic in
5446 let s = String.create len in
5447 really_input ic s 0 len;
5448 f s;
5449 with
5450 | Parse_error (msg, s, pos) ->
5451 let subs = subs s pos in
5452 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5453 failwith ("parse error: " ^ s)
5455 | exn ->
5456 failwith ("config load error: " ^ Printexc.to_string exn)
5459 let defconfpath =
5460 let dir =
5462 let dir = Filename.concat home ".config" in
5463 if Sys.is_directory dir then dir else home
5464 with _ -> home
5466 Filename.concat dir "llpp.conf"
5469 let confpath = ref defconfpath;;
5471 let load1 f =
5472 if Sys.file_exists !confpath
5473 then
5474 match
5475 (try Some (open_in_bin !confpath)
5476 with exn ->
5477 prerr_endline
5478 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5479 Printexc.to_string exn);
5480 None
5482 with
5483 | Some ic ->
5484 begin try
5485 f (do_load get ic)
5486 with exn ->
5487 prerr_endline
5488 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5489 Printexc.to_string exn);
5490 end;
5491 close_in ic;
5493 | None -> ()
5494 else
5495 f (Hashtbl.create 0, defconf)
5498 let load () =
5499 let f (h, dc) =
5500 let pc, pb, px, pa =
5502 Hashtbl.find h (Filename.basename state.path)
5503 with Not_found -> dc, [], 0, (0, 0.0)
5505 setconf defconf dc;
5506 setconf conf pc;
5507 state.bookmarks <- pb;
5508 state.x <- px;
5509 state.scrollw <- conf.scrollbw;
5510 if conf.jumpback
5511 then state.anchor <- pa;
5512 cbput state.hists.nav pa;
5514 load1 f
5517 let add_attrs bb always dc c =
5518 let ob s a b =
5519 if always || a != b
5520 then Printf.bprintf bb "\n %s='%b'" s a
5521 and oi s a b =
5522 if always || a != b
5523 then Printf.bprintf bb "\n %s='%d'" s a
5524 and oI s a b =
5525 if always || a != b
5526 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5527 and oz s a b =
5528 if always || a <> b
5529 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5530 and oF s a b =
5531 if always || a <> b
5532 then Printf.bprintf bb "\n %s='%f'" s a
5533 and oc s a b =
5534 if always || a <> b
5535 then
5536 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5537 and oC s a b =
5538 if always || a <> b
5539 then
5540 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5541 and oR s a b =
5542 if always || a <> b
5543 then
5544 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5545 and os s a b =
5546 if always || a <> b
5547 then
5548 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5549 and og s a b =
5550 if always || a <> b
5551 then
5552 match a with
5553 | None -> ()
5554 | Some (_N, _A, _B) ->
5555 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5556 and oW s a b =
5557 if always || a <> b
5558 then
5559 let v =
5560 match a with
5561 | None -> "false"
5562 | Some f ->
5563 if f = infinity
5564 then "true"
5565 else string_of_float f
5567 Printf.bprintf bb "\n %s='%s'" s v
5568 and oco s a b =
5569 if always || a <> b
5570 then
5571 match a with
5572 | Some ((n, a, b), _) when n > 1 ->
5573 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5574 | _ -> ()
5575 and obeco s a b =
5576 if always || a <> b
5577 then
5578 match a with
5579 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5580 | _ -> ()
5582 let w, h =
5583 if always
5584 then dc.winw, dc.winh
5585 else
5586 match state.fullscreen with
5587 | Some wh -> wh
5588 | None -> c.winw, c.winh
5590 let zoom, presentation, interpagespace, maxwait =
5591 if always
5592 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5593 else
5594 match state.mode with
5595 | Birdseye (bc, _, _, _, _) ->
5596 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5597 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5599 oi "width" w dc.winw;
5600 oi "height" h dc.winh;
5601 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5602 oi "scroll-handle-height" c.scrollh dc.scrollh;
5603 ob "case-insensitive-search" c.icase dc.icase;
5604 ob "preload" c.preload dc.preload;
5605 oi "page-bias" c.pagebias dc.pagebias;
5606 oi "scroll-step" c.scrollstep dc.scrollstep;
5607 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5608 ob "max-height-fit" c.maxhfit dc.maxhfit;
5609 ob "crop-hack" c.crophack dc.crophack;
5610 oW "throttle" maxwait dc.maxwait;
5611 ob "highlight-links" c.hlinks dc.hlinks;
5612 ob "under-cursor-info" c.underinfo dc.underinfo;
5613 oi "vertical-margin" interpagespace dc.interpagespace;
5614 oz "zoom" zoom dc.zoom;
5615 ob "presentation" presentation dc.presentation;
5616 oi "rotation-angle" c.angle dc.angle;
5617 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5618 ob "proportional-display" c.proportional dc.proportional;
5619 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5620 oi "tex-count" c.texcount dc.texcount;
5621 oi "slice-height" c.sliceheight dc.sliceheight;
5622 oi "thumbnail-width" c.thumbw dc.thumbw;
5623 ob "persistent-location" c.jumpback dc.jumpback;
5624 oc "background-color" c.bgcolor dc.bgcolor;
5625 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5626 oi "tile-width" c.tilew dc.tilew;
5627 oi "tile-height" c.tileh dc.tileh;
5628 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5629 ob "checkers" c.checkers dc.checkers;
5630 oi "aalevel" c.aalevel dc.aalevel;
5631 ob "trim-margins" c.trimmargins dc.trimmargins;
5632 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5633 os "uri-launcher" c.urilauncher dc.urilauncher;
5634 oC "color-space" c.colorspace dc.colorspace;
5635 ob "invert-colors" c.invert dc.invert;
5636 oF "brightness" c.colorscale dc.colorscale;
5637 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5638 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
5639 oco "columns" c.columns dc.columns;
5640 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
5641 os "selection-command" c.selcmd dc.selcmd;
5644 let save () =
5645 let uifontsize = fstate.fontsize in
5646 let bb = Buffer.create 32768 in
5647 let f (h, dc) =
5648 let dc = if conf.bedefault then conf else dc in
5649 Buffer.add_string bb "<llppconfig>\n";
5651 if String.length !fontpath > 0
5652 then
5653 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5654 uifontsize
5655 !fontpath
5656 else (
5657 if uifontsize <> 14
5658 then
5659 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5662 Buffer.add_string bb "<defaults ";
5663 add_attrs bb true dc dc;
5664 Buffer.add_string bb "/>\n";
5666 let adddoc path pan anchor c bookmarks =
5667 if bookmarks == [] && c = dc && anchor = emptyanchor
5668 then ()
5669 else (
5670 Printf.bprintf bb "<doc path='%s'"
5671 (enent path 0 (String.length path));
5673 if anchor <> emptyanchor
5674 then (
5675 let n, y = anchor in
5676 Printf.bprintf bb " page='%d'" n;
5677 if y > 1e-6
5678 then
5679 Printf.bprintf bb " rely='%f'" y
5683 if pan != 0
5684 then Printf.bprintf bb " pan='%d'" pan;
5686 add_attrs bb false dc c;
5688 begin match bookmarks with
5689 | [] -> Buffer.add_string bb "/>\n"
5690 | _ ->
5691 Buffer.add_string bb ">\n<bookmarks>\n";
5692 List.iter (fun (title, _level, (page, rely)) ->
5693 Printf.bprintf bb
5694 "<item title='%s' page='%d'"
5695 (enent title 0 (String.length title))
5696 page
5698 if rely > 1e-6
5699 then
5700 Printf.bprintf bb " rely='%f'" rely
5702 Buffer.add_string bb "/>\n";
5703 ) bookmarks;
5704 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5705 end;
5709 let pan, conf =
5710 match state.mode with
5711 | Birdseye (c, pan, _, _, _) ->
5712 let beyecolumns =
5713 match conf.columns with
5714 | Some ((c, _, _), _) -> Some c
5715 | None -> None
5716 and columns =
5717 match c.columns with
5718 | Some (c, _) -> Some (c, [||])
5719 | None -> None
5721 pan, { c with beyecolumns = beyecolumns; columns = columns }
5722 | _ -> state.x, conf
5724 let basename = Filename.basename state.path in
5725 adddoc basename pan (getanchor ())
5726 { conf with
5727 autoscrollstep =
5728 match state.autoscroll with
5729 | Some step -> step
5730 | None -> conf.autoscrollstep }
5731 (if conf.savebmarks then state.bookmarks else []);
5733 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5734 if basename <> path
5735 then adddoc path x y c bookmarks
5736 ) h;
5737 Buffer.add_string bb "</llppconfig>";
5739 load1 f;
5740 if Buffer.length bb > 0
5741 then
5743 let tmp = !confpath ^ ".tmp" in
5744 let oc = open_out_bin tmp in
5745 Buffer.output_buffer oc bb;
5746 close_out oc;
5747 Unix.rename tmp !confpath;
5748 with exn ->
5749 prerr_endline
5750 ("error while saving configuration: " ^ Printexc.to_string exn)
5752 end;;
5754 let () =
5755 Arg.parse
5756 (Arg.align
5757 [("-p", Arg.String (fun s -> state.password <- s) ,
5758 "<password> Set password");
5760 ("-f", Arg.String (fun s -> Config.fontpath := s),
5761 "<path> Set path to the user interface font");
5763 ("-c", Arg.String (fun s -> Config.confpath := s),
5764 "<path> Set path to the configuration file");
5766 ("-v", Arg.Unit (fun () ->
5767 Printf.printf
5768 "%s\nconfiguration path: %s\n"
5769 (version ())
5770 Config.defconfpath
5772 exit 0), " Print version and exit");
5775 (fun s -> state.path <- s)
5776 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5778 if String.length state.path = 0
5779 then (prerr_endline "file name missing"; exit 1);
5781 Config.load ();
5783 state.wsfd <- Wsi.init (object
5784 method display = display ()
5785 method reshape w h = reshape w h
5786 method mouse b d x y m = mouse b d x y m
5787 method motion x y = state.mpos <- (x, y); motion x y
5788 method pmotion x y = state.mpos <- (x, y); pmotion x y
5789 method key c m = keyboard c m
5790 method enter x y = state.mpos <- (x, y); pmotion x y
5791 method leave = state.mpos <- (-1, -1)
5792 end);
5794 if not (
5795 List.exists GlMisc.check_extension
5796 [ "GL_ARB_texture_rectangle"
5797 ; "GL_EXT_texture_recangle"
5798 ; "GL_NV_texture_rectangle" ]
5800 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5802 let cr, sw = Unix.pipe ()
5803 and sr, cw = Unix.pipe () in
5805 cloexec cr;
5806 cloexec sw;
5807 cloexec sr;
5808 cloexec cw;
5810 setcheckers conf.checkers;
5811 redirectstderr ();
5813 init (cr, cw) (
5814 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5815 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5816 !Config.fontpath
5818 state.sr <- sr;
5819 state.sw <- sw;
5820 state.text <- "Opening " ^ state.path;
5821 setaalevel conf.aalevel;
5822 writeopen state.path state.password;
5823 state.uioh <- uioh;
5824 setfontsize fstate.fontsize;
5825 doreshape conf.winw conf.winh;
5827 let rec loop deadline =
5828 let r =
5829 match state.errfd with
5830 | None -> [state.sr; state.wsfd]
5831 | Some fd -> [state.sr; state.wsfd; fd]
5833 if state.redisplay
5834 then (
5835 state.redisplay <- false;
5836 display ();
5838 let timeout =
5839 let now = now () in
5840 if deadline > now
5841 then (
5842 if deadline = infinity
5843 then ~-.1.0
5844 else max 0.0 (deadline -. now)
5846 else 0.0
5848 let r, _, _ =
5849 try Unix.select r [] [] timeout
5850 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
5852 begin match r with
5853 | [] ->
5854 state.ghyll None;
5855 let newdeadline =
5856 match state.autoscroll with
5857 | Some step when step != 0 ->
5858 let y = state.y + step in
5859 let y =
5860 if y < 0
5861 then state.maxy
5862 else if y >= state.maxy then 0 else y
5864 gotoy y;
5865 if state.mode = View
5866 then state.text <- "";
5867 deadline +. 0.01
5868 | _ ->
5869 if state.ghyll == noghyll then infinity else deadline +. 0.01
5871 loop newdeadline
5873 | l ->
5874 let rec checkfds = function
5875 | [] -> ()
5876 | fd :: rest when fd = state.sr ->
5877 let cmd = readcmd state.sr in
5878 act cmd;
5879 checkfds rest
5881 | fd :: rest when fd = state.wsfd ->
5882 Wsi.readresp fd;
5883 checkfds rest
5885 | fd :: rest ->
5886 let s = String.create 80 in
5887 let n = Unix.read fd s 0 80 in
5888 if conf.redirectstderr
5889 then (
5890 Buffer.add_substring state.errmsgs s 0 n;
5891 state.newerrmsgs <- true;
5892 state.redisplay <- true;
5894 else (
5895 prerr_string (String.sub s 0 n);
5896 flush stderr;
5898 checkfds rest
5900 checkfds l;
5901 let newdeadline =
5902 let deadline1 =
5903 if deadline = infinity
5904 then now () +. 0.01
5905 else deadline
5907 match state.autoscroll with
5908 | Some step when step != 0 -> deadline1
5909 | _ -> if state.ghyll == noghyll then infinity else deadline1
5911 loop newdeadline
5912 end;
5915 loop infinity;
5916 with Wsi.Quit ->
5917 wcmd "quit" [];
5918 Config.save ();
5919 exit 0;