Aproach titling differently
[llpp.git] / main.ml
blobc5e9d89ee500003213550aba20412a4f4c210ffc
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
270 type anchor = pageno * top;;
272 type outline = string * int * anchor;;
274 type rect = float * float * float * float * float * float * float * float;;
276 type tile = opaque * pixmapsize * elapsed
277 and elapsed = float;;
278 type pagemapkey = pageno * gen;;
279 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
280 and row = int
281 and col = int;;
283 let emptyanchor = (0, 0.0);;
285 type infochange = | Memused | Docinfo | Pdim;;
287 class type uioh = object
288 method display : unit
289 method key : int -> int -> uioh
290 method button : int -> bool -> int -> int -> int -> uioh
291 method motion : int -> int -> uioh
292 method pmotion : int -> int -> uioh
293 method infochanged : infochange -> unit
294 method scrollpw : (int * float * float)
295 method scrollph : (int * float * float)
296 end;;
298 type mode =
299 | Birdseye of (conf * leftx * pageno * pageno * anchor)
300 | Textentry of (textentry * onleave)
301 | View
302 and onleave = leavetextentrystatus -> unit
303 and leavetextentrystatus = | Cancel | Confirm
304 and helpitem = string * int * action
305 and action =
306 | Noaction
307 | Action of (uioh -> uioh)
310 let isbirdseye = function Birdseye _ -> true | _ -> false;;
311 let istextentry = function Textentry _ -> true | _ -> false;;
313 type currently =
314 | Idle
315 | Loading of (page * gen)
316 | Tiling of (
317 page * opaque * colorspace * angle * gen * col * row * width * height
319 | Outlining of outline list
322 let nouioh : uioh = object (self)
323 method display = ()
324 method key _ _ = self
325 method button _ _ _ _ _ = self
326 method motion _ _ = self
327 method pmotion _ _ = self
328 method infochanged _ = ()
329 method scrollpw = (0, nan, nan)
330 method scrollph = (0, nan, nan)
331 end;;
333 type state =
334 { mutable sr : Unix.file_descr
335 ; mutable sw : Unix.file_descr
336 ; mutable wsfd : Unix.file_descr
337 ; mutable errfd : Unix.file_descr option
338 ; mutable stderr : Unix.file_descr
339 ; mutable errmsgs : Buffer.t
340 ; mutable newerrmsgs : bool
341 ; mutable w : int
342 ; mutable x : int
343 ; mutable y : int
344 ; mutable scrollw : int
345 ; mutable hscrollh : int
346 ; mutable anchor : anchor
347 ; mutable ranchors : (string * string * anchor) list
348 ; mutable maxy : int
349 ; mutable layout : page list
350 ; pagemap : (pagemapkey, opaque) Hashtbl.t
351 ; tilemap : (tilemapkey, tile) Hashtbl.t
352 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
353 ; mutable pdims : (pageno * width * height * leftx) list
354 ; mutable pagecount : int
355 ; mutable currently : currently
356 ; mutable mstate : mstate
357 ; mutable searchpattern : string
358 ; mutable rects : (pageno * recttype * rect) list
359 ; mutable rects1 : (pageno * recttype * rect) list
360 ; mutable text : string
361 ; mutable fullscreen : (width * height) option
362 ; mutable mode : mode
363 ; mutable uioh : uioh
364 ; mutable outlines : outline array
365 ; mutable bookmarks : outline list
366 ; mutable path : string
367 ; mutable password : string
368 ; mutable invalidated : int
369 ; mutable memused : memsize
370 ; mutable gen : gen
371 ; mutable throttle : (page list * int * float) option
372 ; mutable autoscroll : int option
373 ; mutable ghyll : (int option -> unit)
374 ; mutable help : helpitem array
375 ; mutable docinfo : (int * string) list
376 ; mutable texid : GlTex.texture_id option
377 ; hists : hists
378 ; mutable prevzoom : float
379 ; mutable progress : float
380 ; mutable redisplay : bool
382 and hists =
383 { pat : string circbuf
384 ; pag : string circbuf
385 ; nav : anchor circbuf
386 ; sel : string circbuf
390 let defconf =
391 { scrollbw = 7
392 ; scrollh = 12
393 ; icase = true
394 ; preload = true
395 ; pagebias = 0
396 ; verbose = false
397 ; debug = false
398 ; scrollstep = 24
399 ; maxhfit = true
400 ; crophack = false
401 ; autoscrollstep = 2
402 ; maxwait = None
403 ; hlinks = false
404 ; underinfo = false
405 ; interpagespace = 2
406 ; zoom = 1.0
407 ; presentation = false
408 ; angle = 0
409 ; winw = 900
410 ; winh = 900
411 ; savebmarks = true
412 ; proportional = true
413 ; trimmargins = false
414 ; trimfuzz = (0,0,0,0)
415 ; memlimit = 32 lsl 20
416 ; texcount = 256
417 ; sliceheight = 24
418 ; thumbw = 76
419 ; jumpback = true
420 ; bgcolor = (0.5, 0.5, 0.5)
421 ; bedefault = false
422 ; scrollbarinpm = true
423 ; tilew = 2048
424 ; tileh = 2048
425 ; mustoresize = 128 lsl 20
426 ; checkers = true
427 ; aalevel = 8
428 ; urilauncher =
429 (match platform with
430 | Plinux | Pfreebsd | Pdragonflybsd
431 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
432 | Posx -> "open \"%s\""
433 | Pcygwin -> "cygstart %s"
434 | Punknown -> "echo %s")
435 ; selcmd =
436 (match platform with
437 | Plinux | Pfreebsd | Pdragonflybsd
438 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
439 | Posx -> "pbcopy"
440 | Pcygwin -> "wsel"
441 | Punknown -> "cat")
442 ; colorspace = Rgb
443 ; invert = false
444 ; colorscale = 1.0
445 ; redirectstderr = false
446 ; ghyllscroll = None
447 ; columns = None
448 ; beyecolumns = None
452 let conf = { defconf with angle = defconf.angle };;
454 type fontstate =
455 { mutable fontsize : int
456 ; mutable wwidth : float
457 ; mutable maxrows : int
461 let fstate =
462 { fontsize = 14
463 ; wwidth = nan
464 ; maxrows = -1
468 let setfontsize n =
469 fstate.fontsize <- n;
470 fstate.wwidth <- measurestr fstate.fontsize "w";
471 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
474 let geturl s =
475 let colonpos = try String.index s ':' with Not_found -> -1 in
476 let len = String.length s in
477 if colonpos >= 0 && colonpos + 3 < len
478 then (
479 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
480 then
481 let schemestartpos =
482 try String.rindex_from s colonpos ' '
483 with Not_found -> -1
485 let scheme =
486 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
488 match scheme with
489 | "http" | "ftp" | "mailto" ->
490 let epos =
491 try String.index_from s colonpos ' '
492 with Not_found -> len
494 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
495 | _ -> ""
496 else ""
498 else ""
501 let popen =
502 let shell, farg = "/bin/sh", "-c" in
503 fun s ->
504 let args = [|shell; farg; s|] in
505 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
508 let gotouri uri =
509 if String.length conf.urilauncher = 0
510 then print_endline uri
511 else (
512 let url = geturl uri in
513 if String.length url = 0
514 then print_endline uri
515 else
516 let re = Str.regexp "%s" in
517 let command = Str.global_replace re url conf.urilauncher in
518 try popen command
519 with exn ->
520 Printf.eprintf
521 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
522 flush stderr;
526 let version () =
527 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
528 (platform_to_string platform) Sys.word_size Sys.ocaml_version
531 let makehelp () =
532 let strings = version () :: "" :: Help.keys in
533 Array.of_list (
534 List.map (fun s ->
535 let url = geturl s in
536 if String.length url > 0
537 then (s, 0, Action (fun u -> gotouri url; u))
538 else (s, 0, Noaction)
539 ) strings);
542 let noghyll _ = ();;
544 let state =
545 { sr = Unix.stdin
546 ; sw = Unix.stdin
547 ; wsfd = Unix.stdin
548 ; errfd = None
549 ; stderr = Unix.stderr
550 ; errmsgs = Buffer.create 0
551 ; newerrmsgs = false
552 ; x = 0
553 ; y = 0
554 ; w = 0
555 ; scrollw = 0
556 ; hscrollh = 0
557 ; anchor = emptyanchor
558 ; ranchors = []
559 ; layout = []
560 ; maxy = max_int
561 ; tilelru = Queue.create ()
562 ; pagemap = Hashtbl.create 10
563 ; tilemap = Hashtbl.create 10
564 ; pdims = []
565 ; pagecount = 0
566 ; currently = Idle
567 ; mstate = Mnone
568 ; rects = []
569 ; rects1 = []
570 ; text = ""
571 ; mode = View
572 ; fullscreen = None
573 ; searchpattern = ""
574 ; outlines = [||]
575 ; bookmarks = []
576 ; path = ""
577 ; password = ""
578 ; invalidated = 0
579 ; hists =
580 { nav = cbnew 10 (0, 0.0)
581 ; pat = cbnew 10 ""
582 ; pag = cbnew 10 ""
583 ; sel = cbnew 10 ""
585 ; memused = 0
586 ; gen = 0
587 ; throttle = None
588 ; autoscroll = None
589 ; ghyll = noghyll
590 ; help = makehelp ()
591 ; docinfo = []
592 ; texid = None
593 ; prevzoom = 1.0
594 ; progress = -1.0
595 ; uioh = nouioh
596 ; redisplay = false
600 let vlog fmt =
601 if conf.verbose
602 then
603 Printf.kprintf prerr_endline fmt
604 else
605 Printf.kprintf ignore fmt
608 let redirectstderr () =
609 if conf.redirectstderr
610 then
611 let rfd, wfd = Unix.pipe () in
612 state.stderr <- Unix.dup Unix.stderr;
613 state.errfd <- Some rfd;
614 Unix.dup2 wfd Unix.stderr;
615 else (
616 state.newerrmsgs <- false;
617 begin match state.errfd with
618 | Some fd ->
619 Unix.close fd;
620 Unix.dup2 state.stderr Unix.stderr;
621 state.errfd <- None;
622 | None -> ()
623 end;
624 prerr_string (Buffer.contents state.errmsgs);
625 flush stderr;
626 Buffer.clear state.errmsgs;
630 module G =
631 struct
632 let postRedisplay who =
633 if conf.verbose
634 then prerr_endline ("redisplay for " ^ who);
635 state.redisplay <- true;
637 end;;
639 let addchar s c =
640 let b = Buffer.create (String.length s + 1) in
641 Buffer.add_string b s;
642 Buffer.add_char b c;
643 Buffer.contents b;
646 let colorspace_of_string s =
647 match String.lowercase s with
648 | "rgb" -> Rgb
649 | "bgr" -> Bgr
650 | "gray" -> Gray
651 | _ -> failwith "invalid colorspace"
654 let int_of_colorspace = function
655 | Rgb -> 0
656 | Bgr -> 1
657 | Gray -> 2
660 let colorspace_of_int = function
661 | 0 -> Rgb
662 | 1 -> Bgr
663 | 2 -> Gray
664 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
667 let colorspace_to_string = function
668 | Rgb -> "rgb"
669 | Bgr -> "bgr"
670 | Gray -> "gray"
673 let intentry_with_suffix text key =
674 let c =
675 if key >= 32 && key < 127
676 then Char.chr key
677 else '\000'
679 match Char.lowercase c with
680 | '0' .. '9' ->
681 let text = addchar text c in
682 TEcont text
684 | 'k' | 'm' | 'g' ->
685 let text = addchar text c in
686 TEcont text
688 | _ ->
689 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
690 TEcont text
693 let columns_to_string (n, a, b) =
694 if a = 0 && b = 0
695 then Printf.sprintf "%d" n
696 else Printf.sprintf "%d,%d,%d" n a b;
699 let columns_of_string s =
701 (int_of_string s, 0, 0)
702 with _ ->
703 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
706 let writecmd fd s =
707 let len = String.length s in
708 let n = 4 + len in
709 let b = Buffer.create n in
710 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
711 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
712 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
713 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
714 Buffer.add_string b s;
715 let s' = Buffer.contents b in
716 let n' = Unix.write fd s' 0 n in
717 if n' != n then failwith "write failed";
720 let readcmd fd =
721 let s = "xxxx" in
722 let n = Unix.read fd s 0 4 in
723 if n != 4 then failwith "incomplete read(len)";
724 let len = 0
725 lor (Char.code s.[0] lsl 24)
726 lor (Char.code s.[1] lsl 16)
727 lor (Char.code s.[2] lsl 8)
728 lor (Char.code s.[3] lsl 0)
730 let s = String.create len in
731 let n = Unix.read fd s 0 len in
732 if n != len then failwith "incomplete read(data)";
736 let makecmd s l =
737 let b = Buffer.create 10 in
738 Buffer.add_string b s;
739 let rec combine = function
740 | [] -> b
741 | x :: xs ->
742 Buffer.add_char b ' ';
743 let s =
744 match x with
745 | `b b -> if b then "1" else "0"
746 | `s s -> s
747 | `i i -> string_of_int i
748 | `f f -> string_of_float f
749 | `I f -> string_of_int (truncate f)
751 Buffer.add_string b s;
752 combine xs;
754 combine l;
757 let wcmd s l =
758 let cmd = Buffer.contents (makecmd s l) in
759 writecmd state.sw cmd;
762 let calcips h =
763 if conf.presentation
764 then
765 let d = conf.winh - h in
766 max 0 ((d + 1) / 2)
767 else
768 conf.interpagespace
771 let calcheight () =
772 let rec f pn ph pi fh l =
773 match l with
774 | (n, _, h, _) :: rest ->
775 let ips = calcips h in
776 let fh =
777 if conf.presentation
778 then fh+ips
779 else (
780 if isbirdseye state.mode && pn = 0
781 then fh + ips
782 else fh
785 let fh = fh + ((n - pn) * (ph + pi)) in
786 f n h ips fh rest;
788 | [] ->
789 let inc =
790 if conf.presentation || (isbirdseye state.mode && pn = 0)
791 then 0
792 else -pi
794 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
795 max 0 fh
797 let fh = f 0 0 0 0 state.pdims in
801 let calcheight () =
802 match conf.columns with
803 | None -> calcheight ()
804 | Some (_, b) ->
805 if Array.length b > 0
806 then
807 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
808 y + h
809 else 0
812 let getpageyh pageno =
813 let rec f pn ph pi y l =
814 match l with
815 | (n, _, h, _) :: rest ->
816 let ips = calcips h in
817 if n >= pageno
818 then
819 let h = if n = pageno then h else ph in
820 if conf.presentation && n = pageno
821 then
822 y + (pageno - pn) * (ph + pi) + pi, h
823 else
824 y + (pageno - pn) * (ph + pi), h
825 else
826 let y = y + (if conf.presentation then pi else 0) in
827 let y = y + (n - pn) * (ph + pi) in
828 f n h ips y rest
830 | [] ->
831 y + (pageno - pn) * (ph + pi), ph
833 f 0 0 0 0 state.pdims
836 let getpageyh pageno =
837 match conf.columns with
838 | None -> getpageyh pageno
839 | Some (_, b) ->
840 let (_, _, y, (_, _, h, _)) = b.(pageno) in
841 y, h
844 let getpagedim pageno =
845 let rec f ppdim l =
846 match l with
847 | (n, _, _, _) as pdim :: rest ->
848 if n >= pageno
849 then (if n = pageno then pdim else ppdim)
850 else f pdim rest
852 | [] -> ppdim
854 f (-1, -1, -1, -1) state.pdims
857 let getpagey pageno = fst (getpageyh pageno);;
859 let layout1 y sh =
860 let sh = sh - state.hscrollh in
861 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
862 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
863 match pdims with
864 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
865 let ips = calcips h in
866 let yinc =
867 if conf.presentation || (isbirdseye state.mode && pageno = 0)
868 then ips
869 else 0
871 (w, h, ips, xoff), rest, pdimno + 1, yinc
872 | _ ->
873 prev, pdims, pdimno, 0
875 let dy = dy + yinc in
876 let py = py + yinc in
877 if pageno = state.pagecount || dy >= sh
878 then
879 accu
880 else
881 let vy = y + dy in
882 if py + h <= vy - yinc
883 then
884 let py = py + h + ips in
885 let dy = max 0 (py - y) in
886 f ~pageno:(pageno+1)
887 ~pdimno
888 ~prev:curr
891 ~pdims:rest
892 ~accu
893 else
894 let pagey = vy - py in
895 let pagevh = h - pagey in
896 let pagevh = min (sh - dy) pagevh in
897 let off = if yinc > 0 then py - vy else 0 in
898 let py = py + h + ips in
899 let pagex, dx =
900 let xoff = xoff +
901 if state.w < conf.winw - state.scrollw
902 then (conf.winw - state.scrollw - state.w) / 2
903 else 0
905 let dispx = xoff + state.x in
906 if dispx < 0
907 then (-dispx, 0)
908 else (0, dispx)
910 let pagevw =
911 let lw = w - pagex in
912 min lw (conf.winw - state.scrollw)
914 let e =
915 { pageno = pageno
916 ; pagedimno = pdimno
917 ; pagew = w
918 ; pageh = h
919 ; pagex = pagex
920 ; pagey = pagey + off
921 ; pagevw = pagevw
922 ; pagevh = pagevh - off
923 ; pagedispx = dx
924 ; pagedispy = dy + off
927 let accu = e :: accu in
928 f ~pageno:(pageno+1)
929 ~pdimno
930 ~prev:curr
932 ~dy:(dy+pagevh+ips)
933 ~pdims:rest
934 ~accu
936 if state.invalidated = 0
937 then (
938 let accu =
940 ~pageno:0
941 ~pdimno:~-1
942 ~prev:(0,0,0,0)
943 ~py:0
944 ~dy:0
945 ~pdims:state.pdims
946 ~accu:[]
948 List.rev accu
950 else
954 let layoutN ((columns, coverA, coverB), b) y sh =
955 let sh = sh - state.hscrollh in
956 let rec fold accu n =
957 if n = Array.length b
958 then accu
959 else
960 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
961 if (vy - y) > sh &&
962 (n = coverA - 1
963 || n = state.pagecount - coverB
964 || (n - coverA) mod columns = columns - 1)
965 then accu
966 else
967 let accu =
968 if vy + h > y
969 then
970 let pagey = max 0 (y - vy) in
971 let pagedispy = if pagey > 0 then 0 else vy - y in
972 let pagedispx, pagex, pagevw =
973 let pdx =
974 if n = coverA - 1 || n = state.pagecount - coverB
975 then state.x + (conf.winw - state.scrollw - w) / 2
976 else dx + xoff + state.x
978 if pdx < 0
979 then 0, -pdx, w + pdx
980 else pdx, 0, min (conf.winw - state.scrollw) w
982 let pagevh = min (h - pagey) (sh - pagedispy) in
983 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
984 then
985 let e =
986 { pageno = n
987 ; pagedimno = pdimno
988 ; pagew = w
989 ; pageh = h
990 ; pagex = pagex
991 ; pagey = pagey
992 ; pagevw = pagevw
993 ; pagevh = pagevh
994 ; pagedispx = pagedispx
995 ; pagedispy = pagedispy
998 e :: accu
999 else
1000 accu
1001 else
1002 accu
1004 fold accu (n+1)
1006 if state.invalidated = 0
1007 then List.rev (fold [] 0)
1008 else []
1011 let layout y sh =
1012 match conf.columns with
1013 | None -> layout1 y sh
1014 | Some c -> layoutN c y sh
1017 let clamp incr =
1018 let y = state.y + incr in
1019 let y = max 0 y in
1020 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1024 let getopaque pageno =
1025 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
1026 with Not_found -> None
1029 let putopaque pageno opaque =
1030 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
1033 let itertiles l f =
1034 let tilex = l.pagex mod conf.tilew in
1035 let tiley = l.pagey mod conf.tileh in
1037 let col = l.pagex / conf.tilew in
1038 let row = l.pagey / conf.tileh in
1040 let vw =
1041 let a = l.pagew - l.pagex in
1042 let b = conf.winw - state.scrollw in
1043 min a b
1044 and vh = l.pagevh in
1046 let rec rowloop row y0 dispy h =
1047 if h = 0
1048 then ()
1049 else (
1050 let dh = conf.tileh - y0 in
1051 let dh = min h dh in
1052 let rec colloop col x0 dispx w =
1053 if w = 0
1054 then ()
1055 else (
1056 let dw = conf.tilew - x0 in
1057 let dw = min w dw in
1059 f col row dispx dispy x0 y0 dw dh;
1060 colloop (col+1) 0 (dispx+dw) (w-dw)
1063 colloop col tilex l.pagedispx vw;
1064 rowloop (row+1) 0 (dispy+dh) (h-dh)
1067 if vw > 0 && vh > 0
1068 then rowloop row tiley l.pagedispy vh;
1071 let gettileopaque l col row =
1072 let key =
1073 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1075 try Some (Hashtbl.find state.tilemap key)
1076 with Not_found -> None
1079 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1080 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1081 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1084 let drawtiles l color =
1085 GlDraw.color color;
1086 let f col row x y tilex tiley w h =
1087 match gettileopaque l col row with
1088 | Some (opaque, _, t) ->
1089 let params = x, y, w, h, tilex, tiley in
1090 if conf.invert
1091 then (
1092 Gl.enable `blend;
1093 GlFunc.blend_func `zero `one_minus_src_color;
1095 drawtile params opaque;
1096 if conf.invert
1097 then Gl.disable `blend;
1098 if conf.debug
1099 then (
1100 let s = Printf.sprintf
1101 "%d[%d,%d] %f sec"
1102 l.pageno col row t
1104 let w = measurestr fstate.fontsize s in
1105 GlMisc.push_attrib [`current];
1106 GlDraw.color (0.0, 0.0, 0.0);
1107 GlDraw.rect
1108 (float (x-2), float (y-2))
1109 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1110 GlDraw.color (1.0, 1.0, 1.0);
1111 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1112 GlMisc.pop_attrib ();
1115 | _ ->
1116 let w =
1117 let lw = conf.winw - state.scrollw - x in
1118 min lw w
1119 and h =
1120 let lh = conf.winh - y in
1121 min lh h
1123 Gl.enable `texture_2d;
1124 begin match state.texid with
1125 | Some id ->
1126 GlTex.bind_texture `texture_2d id;
1127 let x0 = float x
1128 and y0 = float y
1129 and x1 = float (x+w)
1130 and y1 = float (y+h) in
1132 let tw = float w /. 64.0
1133 and th = float h /. 64.0 in
1134 let tx0 = float tilex /. 64.0
1135 and ty0 = float tiley /. 64.0 in
1136 let tx1 = tx0 +. tw
1137 and ty1 = ty0 +. th in
1138 GlDraw.begins `quads;
1139 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1140 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1141 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1142 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1143 GlDraw.ends ();
1145 Gl.disable `texture_2d;
1146 | None ->
1147 GlDraw.color (1.0, 1.0, 1.0);
1148 GlDraw.rect
1149 (float x, float y)
1150 (float (x+w), float (y+h));
1151 end;
1152 if w > 128 && h > fstate.fontsize + 10
1153 then (
1154 GlDraw.color (0.0, 0.0, 0.0);
1155 let c, r =
1156 if conf.verbose
1157 then (col*conf.tilew, row*conf.tileh)
1158 else col, row
1160 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1162 GlDraw.color color;
1164 itertiles l f
1167 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1169 let tilevisible1 l x y =
1170 let ax0 = l.pagex
1171 and ax1 = l.pagex + l.pagevw
1172 and ay0 = l.pagey
1173 and ay1 = l.pagey + l.pagevh in
1175 let bx0 = x
1176 and by0 = y in
1177 let bx1 = min (bx0 + conf.tilew) l.pagew
1178 and by1 = min (by0 + conf.tileh) l.pageh in
1180 let rx0 = max ax0 bx0
1181 and ry0 = max ay0 by0
1182 and rx1 = min ax1 bx1
1183 and ry1 = min ay1 by1 in
1185 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1186 nonemptyintersection
1189 let tilevisible layout n x y =
1190 let rec findpageinlayout = function
1191 | l :: _ when l.pageno = n -> tilevisible1 l x y
1192 | _ :: rest -> findpageinlayout rest
1193 | [] -> false
1195 findpageinlayout layout
1198 let tileready l x y =
1199 tilevisible1 l x y &&
1200 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1203 let tilepage n p layout =
1204 let rec loop = function
1205 | l :: rest ->
1206 if l.pageno = n
1207 then
1208 let f col row _ _ _ _ _ _ =
1209 if state.currently = Idle
1210 then
1211 match gettileopaque l col row with
1212 | Some _ -> ()
1213 | None ->
1214 let x = col*conf.tilew
1215 and y = row*conf.tileh in
1216 let w =
1217 let w = l.pagew - x in
1218 min w conf.tilew
1220 let h =
1221 let h = l.pageh - y in
1222 min h conf.tileh
1224 wcmd "tile"
1225 [`s p
1226 ;`i x
1227 ;`i y
1228 ;`i w
1229 ;`i h
1231 state.currently <-
1232 Tiling (
1233 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1234 conf.tilew, conf.tileh
1237 itertiles l f;
1238 else
1239 loop rest
1241 | [] -> ()
1243 if state.invalidated = 0 then loop layout;
1246 let preloadlayout visiblepages =
1247 let presentation = conf.presentation in
1248 let interpagespace = conf.interpagespace in
1249 let maxy = state.maxy in
1250 conf.presentation <- false;
1251 conf.interpagespace <- 0;
1252 state.maxy <- calcheight ();
1253 let y =
1254 match visiblepages with
1255 | [] -> 0
1256 | l :: _ -> getpagey l.pageno + l.pagey
1258 let y = if y < conf.winh then 0 else y - conf.winh in
1259 let h = state.y - y + conf.winh*3 in
1260 let pages = layout y h in
1261 conf.presentation <- presentation;
1262 conf.interpagespace <- interpagespace;
1263 state.maxy <- maxy;
1264 pages;
1267 let load pages =
1268 let rec loop pages =
1269 if state.currently != Idle
1270 then ()
1271 else
1272 match pages with
1273 | l :: rest ->
1274 begin match getopaque l.pageno with
1275 | None ->
1276 wcmd "page" [`i l.pageno; `i l.pagedimno];
1277 state.currently <- Loading (l, state.gen);
1278 | Some opaque ->
1279 tilepage l.pageno opaque pages;
1280 loop rest
1281 end;
1282 | _ -> ()
1284 if state.invalidated = 0 then loop pages
1287 let preload pages =
1288 load pages;
1289 if conf.preload && state.currently = Idle
1290 then load (preloadlayout pages);
1293 let layoutready layout =
1294 let rec fold all ls =
1295 all && match ls with
1296 | l :: rest ->
1297 let seen = ref false in
1298 let allvisible = ref true in
1299 let foo col row _ _ _ _ _ _ =
1300 seen := true;
1301 allvisible := !allvisible &&
1302 begin match gettileopaque l col row with
1303 | Some _ -> true
1304 | None -> false
1307 itertiles l foo;
1308 fold (!seen && !allvisible) rest
1309 | [] -> true
1311 let alltilesvisible = fold true layout in
1312 alltilesvisible;
1315 let gotoy y =
1316 let y = bound y 0 state.maxy in
1317 let y, layout, proceed =
1318 match conf.maxwait with
1319 | Some time when state.ghyll == noghyll ->
1320 begin match state.throttle with
1321 | None ->
1322 let layout = layout y conf.winh in
1323 let ready = layoutready layout in
1324 if not ready
1325 then (
1326 load layout;
1327 state.throttle <- Some (layout, y, now ());
1329 else G.postRedisplay "gotoy showall (None)";
1330 y, layout, ready
1331 | Some (_, _, started) ->
1332 let dt = now () -. started in
1333 if dt > time
1334 then (
1335 state.throttle <- None;
1336 let layout = layout y conf.winh in
1337 load layout;
1338 G.postRedisplay "maxwait";
1339 y, layout, true
1341 else -1, [], false
1344 | _ ->
1345 let layout = layout y conf.winh in
1346 if true || layoutready layout
1347 then G.postRedisplay "gotoy ready";
1348 y, layout, true
1350 if proceed
1351 then (
1352 state.y <- y;
1353 state.layout <- layout;
1354 begin match state.mode with
1355 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1356 if not (pagevisible layout pageno)
1357 then (
1358 match state.layout with
1359 | [] -> ()
1360 | l :: _ ->
1361 state.mode <- Birdseye (
1362 conf, leftx, l.pageno, hooverpageno, anchor
1365 | _ -> ()
1366 end;
1367 preload layout;
1369 state.ghyll <- noghyll;
1372 let conttiling pageno opaque =
1373 tilepage pageno opaque
1374 (if conf.preload then preloadlayout state.layout else state.layout)
1377 let gotoy_and_clear_text y =
1378 gotoy y;
1379 if not conf.verbose then state.text <- "";
1382 let getanchor () =
1383 match state.layout with
1384 | [] -> emptyanchor
1385 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1388 let getanchory (n, top) =
1389 let y, h = getpageyh n in
1390 y + (truncate (top *. float h));
1393 let gotoanchor anchor =
1394 gotoy (getanchory anchor);
1397 let addnav () =
1398 cbput state.hists.nav (getanchor ());
1401 let getnav dir =
1402 let anchor = cbgetc state.hists.nav dir in
1403 getanchory anchor;
1406 let gotoghyll y =
1407 let rec scroll f n a b =
1408 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1409 let snake f a b =
1410 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1411 if f < a
1412 then s (float f /. float a)
1413 else (
1414 if f > b
1415 then 1.0 -. s ((float (f-b) /. float (n-b)))
1416 else 1.0
1419 snake f a b
1420 and summa f n a b =
1421 (* courtesy:
1422 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1423 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1424 let iv1 = iv f in
1425 let ins = float a *. iv1
1426 and outs = float (n-b) *. iv1 in
1427 let ones = b - a in
1428 ins +. outs +. float ones
1430 let rec set (_N, _A, _B) y sy =
1431 let sum = summa 1.0 _N _A _B in
1432 let dy = float (y - sy) in
1433 state.ghyll <- (
1434 let rec gf n y1 o =
1435 if n >= _N
1436 then state.ghyll <- noghyll
1437 else
1438 let go n =
1439 let s = scroll n _N _A _B in
1440 let y1 = y1 +. ((s *. dy) /. sum) in
1441 gotoy_and_clear_text (truncate y1);
1442 state.ghyll <- gf (n+1) y1;
1444 match o with
1445 | None -> go n
1446 | Some y' -> set (_N/2, 0, 0) y' state.y
1448 gf 0 (float state.y)
1451 match conf.ghyllscroll with
1452 | None ->
1453 gotoy_and_clear_text y
1454 | Some nab ->
1455 if state.ghyll == noghyll
1456 then set nab y state.y
1457 else state.ghyll (Some y)
1460 let gotopage n top =
1461 let y, h = getpageyh n in
1462 let y = y + (truncate (top *. float h)) in
1463 gotoghyll y
1466 let gotopage1 n top =
1467 let y = getpagey n in
1468 let y = y + top in
1469 gotoghyll y
1472 let invalidate () =
1473 state.layout <- [];
1474 state.pdims <- [];
1475 state.rects <- [];
1476 state.rects1 <- [];
1477 state.invalidated <- state.invalidated + 1;
1480 let writeopen path password =
1481 writecmd state.sw ("open " ^ path ^ "\000" ^ password ^ "\000");
1484 let opendoc path password =
1485 invalidate ();
1486 state.path <- path;
1487 state.password <- password;
1488 state.gen <- state.gen + 1;
1489 state.docinfo <- [];
1491 setaalevel conf.aalevel;
1492 writeopen path password;
1493 Wsi.settitle ("llpp " ^ Filename.basename path);
1494 wcmd "geometry" [`i state.w; `i conf.winh];
1497 let scalecolor c =
1498 let c = c *. conf.colorscale in
1499 (c, c, c);
1502 let scalecolor2 (r, g, b) =
1503 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1506 let represent () =
1507 let docolumns = function
1508 | None -> ()
1509 | Some ((columns, coverA, coverB), _) ->
1510 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1511 let rec loop pageno pdimno pdim x y rowh pdims =
1512 if pageno = state.pagecount
1513 then ()
1514 else
1515 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1516 match pdims with
1517 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1518 pdimno+1, pdim, rest
1519 | _ ->
1520 pdimno, pdim, pdims
1522 let x, y, rowh' =
1523 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1524 then (
1525 (conf.winw - state.scrollw - w) / 2,
1526 y + rowh + conf.interpagespace, h
1528 else (
1529 if (pageno - coverA) mod columns = 0
1530 then 0, y + rowh + conf.interpagespace, h
1531 else x, y, max rowh h
1534 let rec fixrow m = if m = pageno then () else
1535 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1536 if h < rowh
1537 then (
1538 let y = y + (rowh - h) / 2 in
1539 a.(m) <- (pdimno, x, y, pdim);
1541 fixrow (m+1)
1543 if pageno > 1 && (pageno - coverA) mod columns = 0
1544 then fixrow (pageno - columns);
1545 a.(pageno) <- (pdimno, x, y, pdim);
1546 let x = x + w + xoff*2 + conf.interpagespace in
1547 loop (pageno+1) pdimno pdim x y rowh' pdims
1549 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1550 conf.columns <- Some ((columns, coverA, coverB), a);
1552 docolumns conf.columns;
1553 state.maxy <- calcheight ();
1554 state.hscrollh <-
1555 if state.w <= conf.winw - state.scrollw
1556 then 0
1557 else state.scrollw
1559 match state.mode with
1560 | Birdseye (_, _, pageno, _, _) ->
1561 let y, h = getpageyh pageno in
1562 let top = (conf.winh - h) / 2 in
1563 gotoy (max 0 (y - top))
1564 | _ -> gotoanchor state.anchor
1567 let reshape =
1568 let firsttime = ref true in
1569 fun ~w ~h ->
1570 GlDraw.viewport 0 0 w h;
1571 if state.invalidated = 0 && not !firsttime
1572 then state.anchor <- getanchor ();
1574 firsttime := false;
1575 conf.winw <- w;
1576 let w = truncate (float w *. conf.zoom) - state.scrollw in
1577 let w = max w 2 in
1578 state.w <- w;
1579 conf.winh <- h;
1580 setfontsize fstate.fontsize;
1581 GlMat.mode `modelview;
1582 GlMat.load_identity ();
1584 GlMat.mode `projection;
1585 GlMat.load_identity ();
1586 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1587 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1588 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1590 let w =
1591 match conf.columns with
1592 | None -> w
1593 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1595 invalidate ();
1596 wcmd "geometry" [`i w; `i h];
1599 let enttext () =
1600 let len = String.length state.text in
1601 let drawstring s =
1602 let hscrollh =
1603 match state.mode with
1604 | View -> state.hscrollh
1605 | _ -> 0
1607 let rect x w =
1608 GlDraw.rect
1609 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1610 (x+.w, float (conf.winh - hscrollh))
1613 let w = float (conf.winw - state.scrollw - 1) in
1614 if state.progress >= 0.0 && state.progress < 1.0
1615 then (
1616 GlDraw.color (0.3, 0.3, 0.3);
1617 let w1 = w *. state.progress in
1618 rect 0.0 w1;
1619 GlDraw.color (0.0, 0.0, 0.0);
1620 rect w1 (w-.w1)
1622 else (
1623 GlDraw.color (0.0, 0.0, 0.0);
1624 rect 0.0 w;
1627 GlDraw.color (1.0, 1.0, 1.0);
1628 drawstring fstate.fontsize
1629 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1631 let s =
1632 match state.mode with
1633 | Textentry ((prefix, text, _, _, _), _) ->
1634 let s =
1635 if len > 0
1636 then
1637 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1638 else
1639 Printf.sprintf "%s%s_" prefix text
1643 | _ -> state.text
1645 let s =
1646 if state.newerrmsgs
1647 then (
1648 if not (istextentry state.mode)
1649 then
1650 let s1 = "(press 'e' to review error messasges)" in
1651 if String.length s > 0 then s ^ " " ^ s1 else s1
1652 else s
1654 else s
1656 if String.length s > 0
1657 then drawstring s
1660 let showtext c s =
1661 state.text <- Printf.sprintf "%c%s" c s;
1662 G.postRedisplay "showtext";
1665 let gctiles () =
1666 let len = Queue.length state.tilelru in
1667 let rec loop qpos =
1668 if state.memused <= conf.memlimit
1669 then ()
1670 else (
1671 if qpos < len
1672 then
1673 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1674 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1675 let (_, pw, ph, _) = getpagedim n in
1677 gen = state.gen
1678 && colorspace = conf.colorspace
1679 && angle = conf.angle
1680 && pagew = pw
1681 && pageh = ph
1682 && (
1683 let layout =
1684 match state.throttle with
1685 | None ->
1686 if conf.preload
1687 then preloadlayout state.layout
1688 else state.layout
1689 | Some (layout, _, _) ->
1690 layout
1692 let x = col*conf.tilew
1693 and y = row*conf.tileh in
1694 tilevisible layout n x y
1696 then Queue.push lruitem state.tilelru
1697 else (
1698 wcmd "freetile" [`s p];
1699 state.memused <- state.memused - s;
1700 state.uioh#infochanged Memused;
1701 Hashtbl.remove state.tilemap k;
1703 loop (qpos+1)
1706 loop 0
1709 let flushtiles () =
1710 Queue.iter (fun (k, p, s) ->
1711 wcmd "freetile" [`s p];
1712 state.memused <- state.memused - s;
1713 state.uioh#infochanged Memused;
1714 Hashtbl.remove state.tilemap k;
1715 ) state.tilelru;
1716 Queue.clear state.tilelru;
1717 load state.layout;
1720 let logcurrently = function
1721 | Idle -> dolog "Idle"
1722 | Loading (l, gen) ->
1723 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1724 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1725 dolog
1726 "Tiling %d[%d,%d] page=%s cs=%s angle"
1727 l.pageno col row pageopaque
1728 (colorspace_to_string colorspace)
1730 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1731 angle gen conf.angle state.gen
1732 tilew tileh
1733 conf.tilew conf.tileh
1735 | Outlining _ ->
1736 dolog "outlining"
1739 let act cmds =
1740 (* dolog "%S" cmds; *)
1741 let op, args =
1742 let spacepos =
1743 try String.index cmds ' '
1744 with Not_found -> -1
1746 if spacepos = -1
1747 then cmds, ""
1748 else
1749 let l = String.length cmds in
1750 let op = String.sub cmds 0 spacepos in
1751 op, begin
1752 if l - spacepos < 2 then ""
1753 else String.sub cmds (spacepos+1) (l-spacepos-1)
1756 match op with
1757 | "clear" ->
1758 state.uioh#infochanged Pdim;
1759 state.pdims <- [];
1761 | "clearrects" ->
1762 state.rects <- state.rects1;
1763 G.postRedisplay "clearrects";
1765 | "continue" ->
1766 let n =
1767 try Scanf.sscanf args "%u" (fun n -> n)
1768 with exn ->
1769 dolog "error processing 'continue' %S: %s"
1770 cmds (Printexc.to_string exn);
1771 exit 1;
1773 state.pagecount <- n;
1774 state.invalidated <- state.invalidated - 1;
1775 begin match state.currently with
1776 | Outlining l ->
1777 state.currently <- Idle;
1778 state.outlines <- Array.of_list (List.rev l)
1779 | _ -> ()
1780 end;
1781 if state.invalidated = 0
1782 then represent ();
1783 if conf.maxwait = None
1784 then G.postRedisplay "continue";
1786 | "title" ->
1787 Wsi.settitle args
1789 | "msg" ->
1790 showtext ' ' args
1792 | "vmsg" ->
1793 if conf.verbose
1794 then showtext ' ' args
1796 | "progress" ->
1797 let progress, text =
1799 Scanf.sscanf args "%f %n"
1800 (fun f pos ->
1801 f, String.sub args pos (String.length args - pos))
1802 with exn ->
1803 dolog "error processing 'progress' %S: %s"
1804 cmds (Printexc.to_string exn);
1805 exit 1;
1807 state.text <- text;
1808 state.progress <- progress;
1809 G.postRedisplay "progress"
1811 | "firstmatch" ->
1812 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1814 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1815 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1816 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1817 with exn ->
1818 dolog "error processing 'firstmatch' %S: %s"
1819 cmds (Printexc.to_string exn);
1820 exit 1;
1822 let y = (getpagey pageno) + truncate y0 in
1823 addnav ();
1824 gotoy y;
1825 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1827 | "match" ->
1828 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1830 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1831 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1832 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1833 with exn ->
1834 dolog "error processing 'match' %S: %s"
1835 cmds (Printexc.to_string exn);
1836 exit 1;
1838 state.rects1 <-
1839 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1841 | "page" ->
1842 let pageopaque, t =
1844 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1845 with exn ->
1846 dolog "error processing 'page' %S: %s"
1847 cmds (Printexc.to_string exn);
1848 exit 1;
1850 begin match state.currently with
1851 | Loading (l, gen) ->
1852 vlog "page %d took %f sec" l.pageno t;
1853 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1854 begin match state.throttle with
1855 | None ->
1856 let preloadedpages =
1857 if conf.preload
1858 then preloadlayout state.layout
1859 else state.layout
1861 let evict () =
1862 let module IntSet =
1863 Set.Make (struct type t = int let compare = (-) end) in
1864 let set =
1865 List.fold_left (fun s l -> IntSet.add l.pageno s)
1866 IntSet.empty preloadedpages
1868 let evictedpages =
1869 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1870 if not (IntSet.mem pageno set)
1871 then (
1872 wcmd "freepage" [`s opaque];
1873 key :: accu
1875 else accu
1876 ) state.pagemap []
1878 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1880 evict ();
1881 state.currently <- Idle;
1882 if gen = state.gen
1883 then (
1884 tilepage l.pageno pageopaque state.layout;
1885 load state.layout;
1886 load preloadedpages;
1887 if pagevisible state.layout l.pageno
1888 && layoutready state.layout
1889 then G.postRedisplay "page";
1892 | Some (layout, _, _) ->
1893 state.currently <- Idle;
1894 tilepage l.pageno pageopaque layout;
1895 load state.layout
1896 end;
1898 | _ ->
1899 dolog "Inconsistent loading state";
1900 logcurrently state.currently;
1901 exit 1
1904 | "tile" ->
1905 let (x, y, opaque, size, t) =
1907 Scanf.sscanf args "%u %u %s %u %f"
1908 (fun x y p size t -> (x, y, p, size, t))
1909 with exn ->
1910 dolog "error processing 'tile' %S: %s"
1911 cmds (Printexc.to_string exn);
1912 exit 1;
1914 begin match state.currently with
1915 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1916 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1918 if tilew != conf.tilew || tileh != conf.tileh
1919 then (
1920 wcmd "freetile" [`s opaque];
1921 state.currently <- Idle;
1922 load state.layout;
1924 else (
1925 puttileopaque l col row gen cs angle opaque size t;
1926 state.memused <- state.memused + size;
1927 state.uioh#infochanged Memused;
1928 gctiles ();
1929 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1930 opaque, size) state.tilelru;
1932 let layout =
1933 match state.throttle with
1934 | None -> state.layout
1935 | Some (layout, _, _) -> layout
1938 state.currently <- Idle;
1939 if gen = state.gen
1940 && conf.colorspace = cs
1941 && conf.angle = angle
1942 && tilevisible layout l.pageno x y
1943 then conttiling l.pageno pageopaque;
1945 begin match state.throttle with
1946 | None ->
1947 preload state.layout;
1948 if gen = state.gen
1949 && conf.colorspace = cs
1950 && conf.angle = angle
1951 && tilevisible state.layout l.pageno x y
1952 then G.postRedisplay "tile nothrottle";
1954 | Some (layout, y, _) ->
1955 let ready = layoutready layout in
1956 if ready
1957 then (
1958 state.y <- y;
1959 state.layout <- layout;
1960 state.throttle <- None;
1961 G.postRedisplay "throttle";
1963 else load layout;
1964 end;
1967 | _ ->
1968 dolog "Inconsistent tiling state";
1969 logcurrently state.currently;
1970 exit 1
1973 | "pdim" ->
1974 let pdim =
1976 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1977 with exn ->
1978 dolog "error processing 'pdim' %S: %s"
1979 cmds (Printexc.to_string exn);
1980 exit 1;
1982 state.uioh#infochanged Pdim;
1983 state.pdims <- pdim :: state.pdims
1985 | "o" ->
1986 let (l, n, t, h, pos) =
1988 Scanf.sscanf args "%u %u %d %u %n"
1989 (fun l n t h pos -> l, n, t, h, pos)
1990 with exn ->
1991 dolog "error processing 'o' %S: %s"
1992 cmds (Printexc.to_string exn);
1993 exit 1;
1995 let s = String.sub args pos (String.length args - pos) in
1996 let outline = (s, l, (n, float t /. float h)) in
1997 begin match state.currently with
1998 | Outlining outlines ->
1999 state.currently <- Outlining (outline :: outlines)
2000 | Idle ->
2001 state.currently <- Outlining [outline]
2002 | currently ->
2003 dolog "invalid outlining state";
2004 logcurrently currently
2007 | "info" ->
2008 state.docinfo <- (1, args) :: state.docinfo
2010 | "infoend" ->
2011 state.uioh#infochanged Docinfo;
2012 state.docinfo <- List.rev state.docinfo
2014 | _ ->
2015 dolog "unknown cmd `%S'" cmds
2018 let onhist cb =
2019 let rc = cb.rc in
2020 let action = function
2021 | HCprev -> cbget cb ~-1
2022 | HCnext -> cbget cb 1
2023 | HCfirst -> cbget cb ~-(cb.rc)
2024 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2025 and cancel () = cb.rc <- rc
2026 in (action, cancel)
2029 let search pattern forward =
2030 if String.length pattern > 0
2031 then
2032 let pn, py =
2033 match state.layout with
2034 | [] -> 0, 0
2035 | l :: _ ->
2036 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2038 let cmd =
2039 let b = makecmd "search"
2040 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
2042 Buffer.add_char b ',';
2043 Buffer.add_string b pattern;
2044 Buffer.add_char b '\000';
2045 Buffer.contents b;
2047 writecmd state.sw cmd;
2050 let intentry text key =
2051 let c =
2052 if key >= 32 && key < 127
2053 then Char.chr key
2054 else '\000'
2056 match c with
2057 | '0' .. '9' ->
2058 let text = addchar text c in
2059 TEcont text
2061 | _ ->
2062 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2063 TEcont text
2066 let textentry text key =
2067 if key land 0xff00 = 0xff00
2068 then TEcont text
2069 else TEcont (text ^ Wsi.toutf8 key)
2072 let reqlayout angle proportional =
2073 match state.throttle with
2074 | None ->
2075 if state.invalidated = 0 then state.anchor <- getanchor ();
2076 conf.angle <- angle mod 360;
2077 conf.proportional <- proportional;
2078 invalidate ();
2079 wcmd "reqlayout" [`i conf.angle; `b proportional];
2080 | _ -> ()
2083 let settrim trimmargins trimfuzz =
2084 if state.invalidated = 0 then state.anchor <- getanchor ();
2085 conf.trimmargins <- trimmargins;
2086 conf.trimfuzz <- trimfuzz;
2087 let x0, y0, x1, y1 = trimfuzz in
2088 invalidate ();
2089 wcmd "settrim" [
2090 `b conf.trimmargins;
2091 `i x0;
2092 `i y0;
2093 `i x1;
2094 `i y1;
2096 Hashtbl.iter (fun _ opaque ->
2097 wcmd "freepage" [`s opaque];
2098 ) state.pagemap;
2099 Hashtbl.clear state.pagemap;
2102 let setzoom zoom =
2103 match state.throttle with
2104 | None ->
2105 let zoom = max 0.01 zoom in
2106 if zoom <> conf.zoom
2107 then (
2108 state.prevzoom <- conf.zoom;
2109 let relx =
2110 if zoom <= 1.0
2111 then (state.x <- 0; 0.0)
2112 else float state.x /. float state.w
2114 conf.zoom <- zoom;
2115 reshape conf.winw conf.winh;
2116 if zoom > 1.0
2117 then (
2118 let x = relx *. float state.w in
2119 state.x <- truncate x;
2121 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2124 | Some (layout, y, started) ->
2125 let time =
2126 match conf.maxwait with
2127 | None -> 0.0
2128 | Some t -> t
2130 let dt = now () -. started in
2131 if dt > time
2132 then (
2133 state.y <- y;
2134 load layout;
2138 let setcolumns columns coverA coverB =
2139 if columns < 2
2140 then (
2141 conf.columns <- None;
2142 state.x <- 0;
2143 setzoom 1.0;
2145 else (
2146 conf.columns <- Some ((columns, coverA, coverB), [||]);
2147 conf.zoom <- 1.0;
2149 reshape conf.winw conf.winh;
2152 let enterbirdseye () =
2153 let zoom = float conf.thumbw /. float conf.winw in
2154 let birdseyepageno =
2155 let cy = conf.winh / 2 in
2156 let fold = function
2157 | [] -> 0
2158 | l :: rest ->
2159 let rec fold best = function
2160 | [] -> best.pageno
2161 | l :: rest ->
2162 let d = cy - (l.pagedispy + l.pagevh/2)
2163 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2164 if abs d < abs dbest
2165 then fold l rest
2166 else best.pageno
2167 in fold l rest
2169 fold state.layout
2171 state.mode <- Birdseye (
2172 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2174 conf.zoom <- zoom;
2175 conf.presentation <- false;
2176 conf.interpagespace <- 10;
2177 conf.hlinks <- false;
2178 state.x <- 0;
2179 state.mstate <- Mnone;
2180 conf.maxwait <- None;
2181 conf.columns <- (
2182 match conf.beyecolumns with
2183 | Some c ->
2184 conf.zoom <- 1.0;
2185 Some ((c, 0, 0), [||])
2186 | None -> None
2188 Wsi.setcursor Wsi.CURSOR_INHERIT;
2189 if conf.verbose
2190 then
2191 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2192 (100.0*.zoom)
2193 else
2194 state.text <- ""
2196 reshape conf.winw conf.winh;
2199 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2200 state.mode <- View;
2201 conf.zoom <- c.zoom;
2202 conf.presentation <- c.presentation;
2203 conf.interpagespace <- c.interpagespace;
2204 conf.maxwait <- c.maxwait;
2205 conf.hlinks <- c.hlinks;
2206 conf.beyecolumns <- (
2207 match conf.columns with
2208 | Some ((c, _, _), _) -> Some c
2209 | None -> None
2211 conf.columns <- (
2212 match c.columns with
2213 | Some (c, _) -> Some (c, [||])
2214 | None -> None
2216 state.x <- leftx;
2217 if conf.verbose
2218 then
2219 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2220 (100.0*.conf.zoom)
2222 reshape conf.winw conf.winh;
2223 state.anchor <- if goback then anchor else (pageno, 0.0);
2226 let togglebirdseye () =
2227 match state.mode with
2228 | Birdseye vals -> leavebirdseye vals true
2229 | View -> enterbirdseye ()
2230 | _ -> ()
2233 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2234 let pageno = max 0 (pageno - incr) in
2235 let rec loop = function
2236 | [] -> gotopage1 pageno 0
2237 | l :: _ when l.pageno = pageno ->
2238 if l.pagedispy >= 0 && l.pagey = 0
2239 then G.postRedisplay "upbirdseye"
2240 else gotopage1 pageno 0
2241 | _ :: rest -> loop rest
2243 loop state.layout;
2244 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2247 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2248 let pageno = min (state.pagecount - 1) (pageno + incr) in
2249 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2250 let rec loop = function
2251 | [] ->
2252 let y, h = getpageyh pageno in
2253 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2254 gotoy (clamp dy)
2255 | l :: _ when l.pageno = pageno ->
2256 if l.pagevh != l.pageh
2257 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2258 else G.postRedisplay "downbirdseye"
2259 | _ :: rest -> loop rest
2261 loop state.layout
2264 let optentry mode _ key =
2265 let btos b = if b then "on" else "off" in
2266 if key >= 32 && key < 127
2267 then
2268 let c = Char.chr key in
2269 match c with
2270 | 's' ->
2271 let ondone s =
2272 try conf.scrollstep <- int_of_string s with exc ->
2273 state.text <- Printf.sprintf "bad integer `%s': %s"
2274 s (Printexc.to_string exc)
2276 TEswitch ("scroll step: ", "", None, intentry, ondone)
2278 | 'A' ->
2279 let ondone s =
2281 conf.autoscrollstep <- int_of_string s;
2282 if state.autoscroll <> None
2283 then state.autoscroll <- Some conf.autoscrollstep
2284 with exc ->
2285 state.text <- Printf.sprintf "bad integer `%s': %s"
2286 s (Printexc.to_string exc)
2288 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2290 | 'C' ->
2291 let ondone s =
2293 let n, a, b = columns_of_string s in
2294 setcolumns n a b;
2295 with exc ->
2296 state.text <- Printf.sprintf "bad columns `%s': %s"
2297 s (Printexc.to_string exc)
2299 TEswitch ("columns: ", "", None, textentry, ondone)
2301 | 'Z' ->
2302 let ondone s =
2304 let zoom = float (int_of_string s) /. 100.0 in
2305 setzoom zoom
2306 with exc ->
2307 state.text <- Printf.sprintf "bad integer `%s': %s"
2308 s (Printexc.to_string exc)
2310 TEswitch ("zoom: ", "", None, intentry, ondone)
2312 | 't' ->
2313 let ondone s =
2315 conf.thumbw <- bound (int_of_string s) 2 4096;
2316 state.text <-
2317 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2318 begin match mode with
2319 | Birdseye beye ->
2320 leavebirdseye beye false;
2321 enterbirdseye ();
2322 | _ -> ();
2324 with exc ->
2325 state.text <- Printf.sprintf "bad integer `%s': %s"
2326 s (Printexc.to_string exc)
2328 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2330 | 'R' ->
2331 let ondone s =
2332 match try
2333 Some (int_of_string s)
2334 with exc ->
2335 state.text <- Printf.sprintf "bad integer `%s': %s"
2336 s (Printexc.to_string exc);
2337 None
2338 with
2339 | Some angle -> reqlayout angle conf.proportional
2340 | None -> ()
2342 TEswitch ("rotation: ", "", None, intentry, ondone)
2344 | 'i' ->
2345 conf.icase <- not conf.icase;
2346 TEdone ("case insensitive search " ^ (btos conf.icase))
2348 | 'p' ->
2349 conf.preload <- not conf.preload;
2350 gotoy state.y;
2351 TEdone ("preload " ^ (btos conf.preload))
2353 | 'v' ->
2354 conf.verbose <- not conf.verbose;
2355 TEdone ("verbose " ^ (btos conf.verbose))
2357 | 'd' ->
2358 conf.debug <- not conf.debug;
2359 TEdone ("debug " ^ (btos conf.debug))
2361 | 'h' ->
2362 conf.maxhfit <- not conf.maxhfit;
2363 state.maxy <-
2364 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2365 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2367 | 'c' ->
2368 conf.crophack <- not conf.crophack;
2369 TEdone ("crophack " ^ btos conf.crophack)
2371 | 'a' ->
2372 let s =
2373 match conf.maxwait with
2374 | None ->
2375 conf.maxwait <- Some infinity;
2376 "always wait for page to complete"
2377 | Some _ ->
2378 conf.maxwait <- None;
2379 "show placeholder if page is not ready"
2381 TEdone s
2383 | 'f' ->
2384 conf.underinfo <- not conf.underinfo;
2385 TEdone ("underinfo " ^ btos conf.underinfo)
2387 | 'P' ->
2388 conf.savebmarks <- not conf.savebmarks;
2389 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2391 | 'S' ->
2392 let ondone s =
2394 let pageno, py =
2395 match state.layout with
2396 | [] -> 0, 0
2397 | l :: _ ->
2398 l.pageno, l.pagey
2400 conf.interpagespace <- int_of_string s;
2401 state.maxy <- calcheight ();
2402 let y = getpagey pageno in
2403 gotoy (y + py)
2404 with exc ->
2405 state.text <- Printf.sprintf "bad integer `%s': %s"
2406 s (Printexc.to_string exc)
2408 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2410 | 'l' ->
2411 reqlayout conf.angle (not conf.proportional);
2412 TEdone ("proportional display " ^ btos conf.proportional)
2414 | 'T' ->
2415 settrim (not conf.trimmargins) conf.trimfuzz;
2416 TEdone ("trim margins " ^ btos conf.trimmargins)
2418 | 'I' ->
2419 conf.invert <- not conf.invert;
2420 TEdone ("invert colors " ^ btos conf.invert)
2422 | 'x' ->
2423 let ondone s =
2424 cbput state.hists.sel s;
2425 conf.selcmd <- s;
2427 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2428 textentry, ondone)
2430 | _ ->
2431 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2432 TEstop
2433 else
2434 TEcont state.text
2437 class type lvsource = object
2438 method getitemcount : int
2439 method getitem : int -> (string * int)
2440 method hasaction : int -> bool
2441 method exit :
2442 uioh:uioh ->
2443 cancel:bool ->
2444 active:int ->
2445 first:int ->
2446 pan:int ->
2447 qsearch:string ->
2448 uioh option
2449 method getactive : int
2450 method getfirst : int
2451 method getqsearch : string
2452 method setqsearch : string -> unit
2453 method getpan : int
2454 end;;
2456 class virtual lvsourcebase = object
2457 val mutable m_active = 0
2458 val mutable m_first = 0
2459 val mutable m_qsearch = ""
2460 val mutable m_pan = 0
2461 method getactive = m_active
2462 method getfirst = m_first
2463 method getqsearch = m_qsearch
2464 method getpan = m_pan
2465 method setqsearch s = m_qsearch <- s
2466 end;;
2468 let withoutlastutf8 s =
2469 let len = String.length s in
2470 if len = 0
2471 then s
2472 else
2473 let rec find pos =
2474 if pos = 0
2475 then pos
2476 else
2477 let b = Char.code s.[pos] in
2478 if b land 0b110000 = 0b11000000
2479 then find (pos-1)
2480 else pos-1
2482 let first =
2483 if Char.code s.[len-1] land 0x80 = 0
2484 then len-1
2485 else find (len-1)
2487 String.sub s 0 first;
2490 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2491 let enttext te =
2492 state.mode <- Textentry (te, onleave);
2493 state.text <- "";
2494 enttext ();
2495 G.postRedisplay "textentrykeyboard enttext";
2497 let histaction cmd =
2498 match opthist with
2499 | None -> ()
2500 | Some (action, _) ->
2501 state.mode <- Textentry (
2502 (c, action cmd, opthist, onkey, ondone), onleave
2504 G.postRedisplay "textentry histaction"
2506 match key with
2507 | 0xff08 -> (* backspace *)
2508 let s = withoutlastutf8 text in
2509 let len = String.length s in
2510 if len = 0
2511 then (
2512 onleave Cancel;
2513 G.postRedisplay "textentrykeyboard after cancel";
2515 else (
2516 enttext (c, s, opthist, onkey, ondone)
2519 | 0xff0d ->
2520 ondone text;
2521 onleave Confirm;
2522 G.postRedisplay "textentrykeyboard after confirm"
2524 | 0xff52 -> histaction HCprev
2525 | 0xff54 -> histaction HCnext
2526 | 0xff50 -> histaction HCfirst
2527 | 0xff57 -> histaction HClast
2529 | 0xff1b -> (* escape*)
2530 if String.length text = 0
2531 then (
2532 begin match opthist with
2533 | None -> ()
2534 | Some (_, onhistcancel) -> onhistcancel ()
2535 end;
2536 onleave Cancel;
2537 state.text <- "";
2538 G.postRedisplay "textentrykeyboard after cancel2"
2540 else (
2541 enttext (c, "", opthist, onkey, ondone)
2544 | 0xff9f | 0xffff -> () (* delete *)
2546 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2547 begin match onkey text key with
2548 | TEdone text ->
2549 ondone text;
2550 onleave Confirm;
2551 G.postRedisplay "textentrykeyboard after confirm2";
2553 | TEcont text ->
2554 enttext (c, text, opthist, onkey, ondone);
2556 | TEstop ->
2557 onleave Cancel;
2558 G.postRedisplay "textentrykeyboard after cancel3"
2560 | TEswitch te ->
2561 state.mode <- Textentry (te, onleave);
2562 G.postRedisplay "textentrykeyboard switch";
2563 end;
2565 | _ ->
2566 vlog "unhandled key %#x" key
2569 let firstof first active =
2570 if first > active || abs (first - active) > fstate.maxrows - 1
2571 then max 0 (active - (fstate.maxrows/2))
2572 else first
2575 let calcfirst first active =
2576 if active > first
2577 then
2578 let rows = active - first in
2579 if rows > fstate.maxrows then active - fstate.maxrows else first
2580 else active
2583 let scrollph y maxy =
2584 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2585 let sh = float conf.winh /. sh in
2586 let sh = max sh (float conf.scrollh) in
2588 let percent =
2589 if y = state.maxy
2590 then 1.0
2591 else float y /. float maxy
2593 let position = (float conf.winh -. sh) *. percent in
2595 let position =
2596 if position +. sh > float conf.winh
2597 then float conf.winh -. sh
2598 else position
2600 position, sh;
2603 let coe s = (s :> uioh);;
2605 class listview ~(source:lvsource) ~trusted =
2606 object (self)
2607 val m_pan = source#getpan
2608 val m_first = source#getfirst
2609 val m_active = source#getactive
2610 val m_qsearch = source#getqsearch
2611 val m_prev_uioh = state.uioh
2613 method private elemunder y =
2614 let n = y / (fstate.fontsize+1) in
2615 if m_first + n < source#getitemcount
2616 then (
2617 if source#hasaction (m_first + n)
2618 then Some (m_first + n)
2619 else None
2621 else None
2623 method display =
2624 Gl.enable `blend;
2625 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2626 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2627 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2628 GlDraw.color (1., 1., 1.);
2629 Gl.enable `texture_2d;
2630 let fs = fstate.fontsize in
2631 let nfs = fs + 1 in
2632 let ww = fstate.wwidth in
2633 let tabw = 30.0*.ww in
2634 let itemcount = source#getitemcount in
2635 let rec loop row =
2636 if (row - m_first) * nfs > conf.winh
2637 then ()
2638 else (
2639 if row >= 0 && row < itemcount
2640 then (
2641 let (s, level) = source#getitem row in
2642 let y = (row - m_first) * nfs in
2643 let x = 5.0 +. float (level + m_pan) *. ww in
2644 if row = m_active
2645 then (
2646 Gl.disable `texture_2d;
2647 GlDraw.polygon_mode `both `line;
2648 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2649 GlDraw.rect (1., float (y + 1))
2650 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2651 GlDraw.polygon_mode `both `fill;
2652 GlDraw.color (1., 1., 1.);
2653 Gl.enable `texture_2d;
2656 let drawtabularstring s =
2657 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2658 if trusted
2659 then
2660 let tabpos = try String.index s '\t' with Not_found -> -1 in
2661 if tabpos > 0
2662 then
2663 let len = String.length s - tabpos - 1 in
2664 let s1 = String.sub s 0 tabpos
2665 and s2 = String.sub s (tabpos + 1) len in
2666 let nx = drawstr x s1 in
2667 let sw = nx -. x in
2668 let x = x +. (max tabw sw) in
2669 drawstr x s2
2670 else
2671 drawstr x s
2672 else
2673 drawstr x s
2675 let _ = drawtabularstring s in
2676 loop (row+1)
2680 loop m_first;
2681 Gl.disable `blend;
2682 Gl.disable `texture_2d;
2684 method updownlevel incr =
2685 let len = source#getitemcount in
2686 let curlevel =
2687 if m_active >= 0 && m_active < len
2688 then snd (source#getitem m_active)
2689 else -1
2691 let rec flow i =
2692 if i = len then i-1 else if i = -1 then 0 else
2693 let _, l = source#getitem i in
2694 if l != curlevel then i else flow (i+incr)
2696 let active = flow m_active in
2697 let first = calcfirst m_first active in
2698 G.postRedisplay "outline updownlevel";
2699 {< m_active = active; m_first = first >}
2701 method private key1 key mask =
2702 let set1 active first qsearch =
2703 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2705 let search active pattern incr =
2706 let dosearch re =
2707 let rec loop n =
2708 if n >= 0 && n < source#getitemcount
2709 then (
2710 let s, _ = source#getitem n in
2712 (try ignore (Str.search_forward re s 0); true
2713 with Not_found -> false)
2714 then Some n
2715 else loop (n + incr)
2717 else None
2719 loop active
2722 let re = Str.regexp_case_fold pattern in
2723 dosearch re
2724 with Failure s ->
2725 state.text <- s;
2726 None
2728 let itemcount = source#getitemcount in
2729 let find start incr =
2730 let rec find i =
2731 if i = -1 || i = itemcount
2732 then -1
2733 else (
2734 if source#hasaction i
2735 then i
2736 else find (i + incr)
2739 find start
2741 let set active first =
2742 let first = bound first 0 (itemcount - fstate.maxrows) in
2743 state.text <- "";
2744 coe {< m_active = active; m_first = first >}
2746 let navigate incr =
2747 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2748 let active, first =
2749 let incr1 = if incr > 0 then 1 else -1 in
2750 if isvisible m_first m_active
2751 then
2752 let next =
2753 let next = m_active + incr in
2754 let next =
2755 if next < 0 || next >= itemcount
2756 then -1
2757 else find next incr1
2759 if next = -1 || abs (m_active - next) > fstate.maxrows
2760 then -1
2761 else next
2763 if next = -1
2764 then
2765 let first = m_first + incr in
2766 let first = bound first 0 (itemcount - 1) in
2767 let next =
2768 let next = m_active + incr in
2769 let next = bound next 0 (itemcount - 1) in
2770 find next ~-incr1
2772 let active = if next = -1 then m_active else next in
2773 active, first
2774 else
2775 let first = min next m_first in
2776 let first =
2777 if abs (next - first) > fstate.maxrows
2778 then first + incr
2779 else first
2781 next, first
2782 else
2783 let first = m_first + incr in
2784 let first = bound first 0 (itemcount - 1) in
2785 let active =
2786 let next = m_active + incr in
2787 let next = bound next 0 (itemcount - 1) in
2788 let next = find next incr1 in
2789 let active =
2790 if next = -1 || abs (m_active - first) > fstate.maxrows
2791 then (
2792 let active = if m_active = -1 then next else m_active in
2793 active
2795 else next
2797 if isvisible first active
2798 then active
2799 else -1
2801 active, first
2803 G.postRedisplay "listview navigate";
2804 set active first;
2806 match key with
2807 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2808 let incr = if key = 0x72 then -1 else 1 in
2809 let active, first =
2810 match search (m_active + incr) m_qsearch incr with
2811 | None ->
2812 state.text <- m_qsearch ^ " [not found]";
2813 m_active, m_first
2814 | Some active ->
2815 state.text <- m_qsearch;
2816 active, firstof m_first active
2818 G.postRedisplay "listview ctrl-r/s";
2819 set1 active first m_qsearch;
2821 | 0xff08 -> (* backspace *)
2822 if String.length m_qsearch = 0
2823 then coe self
2824 else (
2825 let qsearch = withoutlastutf8 m_qsearch in
2826 let len = String.length qsearch in
2827 if len = 0
2828 then (
2829 state.text <- "";
2830 G.postRedisplay "listview empty qsearch";
2831 set1 m_active m_first "";
2833 else
2834 let active, first =
2835 match search m_active qsearch ~-1 with
2836 | None ->
2837 state.text <- qsearch ^ " [not found]";
2838 m_active, m_first
2839 | Some active ->
2840 state.text <- qsearch;
2841 active, firstof m_first active
2843 G.postRedisplay "listview backspace qsearch";
2844 set1 active first qsearch
2847 | key when ((key >= 32 && key < 127)
2848 || (key != 0 && key land 0xff00 != 0xff00)) ->
2849 let pattern =
2850 if key >= 32 && key < 127
2851 then addchar m_qsearch (Char.chr key)
2852 else m_qsearch ^ Wsi.toutf8 key
2854 let active, first =
2855 match search m_active pattern 1 with
2856 | None ->
2857 state.text <- pattern ^ " [not found]";
2858 m_active, m_first
2859 | Some active ->
2860 state.text <- pattern;
2861 active, firstof m_first active
2863 G.postRedisplay "listview qsearch add";
2864 set1 active first pattern;
2866 | 0xff1b -> (* escape *)
2867 state.text <- "";
2868 if String.length m_qsearch = 0
2869 then (
2870 G.postRedisplay "list view escape";
2871 begin
2872 match
2873 source#exit (coe self) true m_active m_first m_pan m_qsearch
2874 with
2875 | None -> m_prev_uioh
2876 | Some uioh -> uioh
2879 else (
2880 G.postRedisplay "list view kill qsearch";
2881 source#setqsearch "";
2882 coe {< m_qsearch = "" >}
2885 | 0xff0d -> (* return *)
2886 state.text <- "";
2887 let self = {< m_qsearch = "" >} in
2888 source#setqsearch "";
2889 let opt =
2890 G.postRedisplay "listview enter";
2891 if m_active >= 0 && m_active < source#getitemcount
2892 then (
2893 source#exit (coe self) false m_active m_first m_pan "";
2895 else (
2896 source#exit (coe self) true m_active m_first m_pan "";
2899 begin match opt with
2900 | None -> m_prev_uioh
2901 | Some uioh -> uioh
2904 | 0xff9f | 0xffff -> (* delete *)
2905 coe self
2907 | 0xff52 -> navigate ~-1 (* up *)
2908 | 0xff54 -> navigate 1 (* down *)
2909 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
2910 | 0xff56 -> navigate fstate.maxrows (* next *)
2912 | 0xff53 -> (* right *)
2913 state.text <- "";
2914 G.postRedisplay "listview right";
2915 coe {< m_pan = m_pan - 1 >}
2917 | 0xff51 -> (* left *)
2918 state.text <- "";
2919 G.postRedisplay "listview left";
2920 coe {< m_pan = m_pan + 1 >}
2922 | 0xff50 -> (* home *)
2923 let active = find 0 1 in
2924 G.postRedisplay "listview home";
2925 set active 0;
2927 | 0xff57 -> (* end *)
2928 let first = max 0 (itemcount - fstate.maxrows) in
2929 let active = find (itemcount - 1) ~-1 in
2930 G.postRedisplay "listview end";
2931 set active first;
2933 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2934 coe self
2936 | _ ->
2937 dolog "listview unknown key %#x" key; coe self
2939 method key key mask =
2940 match state.mode with
2941 | Textentry te -> textentrykeyboard key mask te; coe self
2942 | _ -> self#key1 key mask
2944 method button button down x y _ =
2945 let opt =
2946 match button with
2947 | 1 when x > conf.winw - conf.scrollbw ->
2948 G.postRedisplay "listview scroll";
2949 if down
2950 then
2951 let _, position, sh = self#scrollph in
2952 if y > truncate position && y < truncate (position +. sh)
2953 then (
2954 state.mstate <- Mscrolly;
2955 Some (coe self)
2957 else
2958 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2959 let first = truncate (s *. float source#getitemcount) in
2960 let first = min source#getitemcount first in
2961 Some (coe {< m_first = first; m_active = first >})
2962 else (
2963 state.mstate <- Mnone;
2964 Some (coe self);
2966 | 1 when not down ->
2967 begin match self#elemunder y with
2968 | Some n ->
2969 G.postRedisplay "listview click";
2970 source#exit
2971 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2972 | _ ->
2973 Some (coe self)
2975 | n when (n == 4 || n == 5) && not down ->
2976 let len = source#getitemcount in
2977 let first =
2978 if n = 5 && m_first + fstate.maxrows >= len
2979 then
2980 m_first
2981 else
2982 let first = m_first + (if n == 3 then -1 else 1) in
2983 bound first 0 (len - 1)
2985 G.postRedisplay "listview wheel";
2986 Some (coe {< m_first = first >})
2987 | _ ->
2988 Some (coe self)
2990 match opt with
2991 | None -> m_prev_uioh
2992 | Some uioh -> uioh
2994 method motion _ y =
2995 match state.mstate with
2996 | Mscrolly ->
2997 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2998 let first = truncate (s *. float source#getitemcount) in
2999 let first = min source#getitemcount first in
3000 G.postRedisplay "listview motion";
3001 coe {< m_first = first; m_active = first >}
3002 | _ -> coe self
3004 method pmotion x y =
3005 if x < conf.winw - conf.scrollbw
3006 then
3007 let n =
3008 match self#elemunder y with
3009 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3010 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3012 let o =
3013 if n != m_active
3014 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3015 else self
3017 coe o
3018 else (
3019 Wsi.setcursor Wsi.CURSOR_INHERIT;
3020 coe self
3023 method infochanged _ = ()
3025 method scrollpw = (0, 0.0, 0.0)
3026 method scrollph =
3027 let nfs = fstate.fontsize + 1 in
3028 let y = m_first * nfs in
3029 let itemcount = source#getitemcount in
3030 let maxi = max 0 (itemcount - fstate.maxrows) in
3031 let maxy = maxi * nfs in
3032 let p, h = scrollph y maxy in
3033 conf.scrollbw, p, h
3034 end;;
3036 class outlinelistview ~source =
3037 object (self)
3038 inherit listview ~source:(source :> lvsource) ~trusted:false as super
3040 method key key mask =
3041 let calcfirst first active =
3042 if active > first
3043 then
3044 let rows = active - first in
3045 if rows > fstate.maxrows then active - fstate.maxrows else first
3046 else active
3048 let navigate incr =
3049 let active = m_active + incr in
3050 let active = bound active 0 (source#getitemcount - 1) in
3051 let first = calcfirst m_first active in
3052 G.postRedisplay "outline navigate";
3053 coe {< m_active = active; m_first = first >}
3055 match key with
3056 | 110 when Wsi.withctrl mask -> (* ctrl-n *)
3057 source#narrow m_qsearch;
3058 G.postRedisplay "outline ctrl-n";
3059 coe {< m_first = 0; m_active = 0 >}
3061 | 117 when Wsi.withctrl mask -> (* ctrl-u *)
3062 source#denarrow;
3063 G.postRedisplay "outline ctrl-u";
3064 state.text <- "";
3065 coe {< m_first = 0; m_active = 0 >}
3067 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
3068 let first = m_active - (fstate.maxrows / 2) in
3069 G.postRedisplay "outline ctrl-l";
3070 coe {< m_first = first >}
3072 | 0xff9f | 0xffff -> (* delete *)
3073 source#remove m_active;
3074 G.postRedisplay "outline delete";
3075 let active = max 0 (m_active-1) in
3076 coe {< m_first = firstof m_first active;
3077 m_active = active >}
3079 | 0xff52 -> navigate ~-1 (* up *)
3080 | 0xff54 -> navigate 1 (* down *)
3081 | 0xff55 -> (* prior *)
3082 navigate ~-(fstate.maxrows)
3083 | 0xff56 -> (* next *)
3084 navigate fstate.maxrows
3086 | 0xff53 -> (* [ctrl-]right *)
3087 let o =
3088 if Wsi.withctrl mask
3089 then (
3090 G.postRedisplay "outline ctrl right";
3091 {< m_pan = m_pan + 1 >}
3093 else self#updownlevel 1
3095 coe o
3097 | 0xff51 -> (* [ctrl-]left *)
3098 let o =
3099 if Wsi.withctrl mask
3100 then (
3101 G.postRedisplay "outline ctrl left";
3102 {< m_pan = m_pan - 1 >}
3104 else self#updownlevel ~-1
3106 coe o
3108 | 0xff50 -> (* home *)
3109 G.postRedisplay "outline home";
3110 coe {< m_first = 0; m_active = 0 >}
3112 | 0xff57 -> (* end *)
3113 let active = source#getitemcount - 1 in
3114 let first = max 0 (active - fstate.maxrows) in
3115 G.postRedisplay "outline end";
3116 coe {< m_active = active; m_first = first >}
3118 | _ -> super#key key mask
3121 let outlinesource usebookmarks =
3122 let empty = [||] in
3123 (object
3124 inherit lvsourcebase
3125 val mutable m_items = empty
3126 val mutable m_orig_items = empty
3127 val mutable m_prev_items = empty
3128 val mutable m_narrow_pattern = ""
3129 val mutable m_hadremovals = false
3131 method getitemcount =
3132 Array.length m_items + (if m_hadremovals then 1 else 0)
3134 method getitem n =
3135 if n == Array.length m_items && m_hadremovals
3136 then
3137 ("[Confirm removal]", 0)
3138 else
3139 let s, n, _ = m_items.(n) in
3140 (s, n)
3142 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3143 ignore (uioh, first, qsearch);
3144 let confrimremoval = m_hadremovals && active = Array.length m_items in
3145 let items =
3146 if String.length m_narrow_pattern = 0
3147 then m_orig_items
3148 else m_items
3150 if not cancel
3151 then (
3152 if not confrimremoval
3153 then(
3154 let _, _, anchor = m_items.(active) in
3155 gotoanchor anchor;
3156 m_items <- items;
3158 else (
3159 state.bookmarks <- Array.to_list m_items;
3160 m_orig_items <- m_items;
3163 else m_items <- items;
3164 m_pan <- pan;
3165 None
3167 method hasaction _ = true
3169 method greetmsg =
3170 if Array.length m_items != Array.length m_orig_items
3171 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3172 else ""
3174 method narrow pattern =
3175 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3176 match reopt with
3177 | None -> ()
3178 | Some re ->
3179 let rec loop accu n =
3180 if n = -1
3181 then (
3182 m_narrow_pattern <- pattern;
3183 m_items <- Array.of_list accu
3185 else
3186 let (s, _, _) as o = m_items.(n) in
3187 let accu =
3188 if (try ignore (Str.search_forward re s 0); true
3189 with Not_found -> false)
3190 then o :: accu
3191 else accu
3193 loop accu (n-1)
3195 loop [] (Array.length m_items - 1)
3197 method denarrow =
3198 m_orig_items <- (
3199 if usebookmarks
3200 then Array.of_list state.bookmarks
3201 else state.outlines
3203 m_items <- m_orig_items
3205 method remove m =
3206 if usebookmarks
3207 then
3208 if m >= 0 && m < Array.length m_items
3209 then (
3210 m_hadremovals <- true;
3211 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3212 let n = if n >= m then n+1 else n in
3213 m_items.(n)
3217 method reset anchor items =
3218 m_hadremovals <- false;
3219 if m_orig_items == empty || m_prev_items != items
3220 then (
3221 m_orig_items <- items;
3222 if String.length m_narrow_pattern = 0
3223 then m_items <- items;
3225 m_prev_items <- items;
3226 let rely = getanchory anchor in
3227 let active =
3228 let rec loop n best bestd =
3229 if n = Array.length m_items
3230 then best
3231 else
3232 let (_, _, anchor) = m_items.(n) in
3233 let orely = getanchory anchor in
3234 let d = abs (orely - rely) in
3235 if d < bestd
3236 then loop (n+1) n d
3237 else loop (n+1) best bestd
3239 loop 0 ~-1 max_int
3241 m_active <- active;
3242 m_first <- firstof m_first active
3243 end)
3246 let enterselector usebookmarks =
3247 let source = outlinesource usebookmarks in
3248 fun errmsg ->
3249 let outlines =
3250 if usebookmarks
3251 then Array.of_list state.bookmarks
3252 else state.outlines
3254 if Array.length outlines = 0
3255 then (
3256 showtext ' ' errmsg;
3258 else (
3259 state.text <- source#greetmsg;
3260 Wsi.setcursor Wsi.CURSOR_INHERIT;
3261 let anchor = getanchor () in
3262 source#reset anchor outlines;
3263 state.uioh <- coe (new outlinelistview ~source);
3264 G.postRedisplay "enter selector";
3268 let enteroutlinemode =
3269 let f = enterselector false in
3270 fun ()-> f "Document has no outline";
3273 let enterbookmarkmode =
3274 let f = enterselector true in
3275 fun () -> f "Document has no bookmarks (yet)";
3278 let color_of_string s =
3279 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3280 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3284 let color_to_string (r, g, b) =
3285 let r = truncate (r *. 256.0)
3286 and g = truncate (g *. 256.0)
3287 and b = truncate (b *. 256.0) in
3288 Printf.sprintf "%d/%d/%d" r g b
3291 let irect_of_string s =
3292 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3295 let irect_to_string (x0,y0,x1,y1) =
3296 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3299 let makecheckers () =
3300 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3301 following to say:
3302 converted by Issac Trotts. July 25, 2002 *)
3303 let image_height = 64
3304 and image_width = 64 in
3306 let make_image () =
3307 let image =
3308 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3310 for i = 0 to image_width - 1 do
3311 for j = 0 to image_height - 1 do
3312 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3313 (if (i land 8 ) lxor (j land 8) = 0
3314 then [|255;255;255|] else [|200;200;200|])
3315 done
3316 done;
3317 image
3319 let image = make_image () in
3320 let id = GlTex.gen_texture () in
3321 GlTex.bind_texture `texture_2d id;
3322 GlPix.store (`unpack_alignment 1);
3323 GlTex.image2d image;
3324 List.iter (GlTex.parameter ~target:`texture_2d)
3325 [ `wrap_s `repeat;
3326 `wrap_t `repeat;
3327 `mag_filter `nearest;
3328 `min_filter `nearest ];
3332 let setcheckers enabled =
3333 match state.texid with
3334 | None ->
3335 if enabled then state.texid <- Some (makecheckers ())
3337 | Some texid ->
3338 if not enabled
3339 then (
3340 GlTex.delete_texture texid;
3341 state.texid <- None;
3345 let int_of_string_with_suffix s =
3346 let l = String.length s in
3347 let s1, shift =
3348 if l > 1
3349 then
3350 let suffix = Char.lowercase s.[l-1] in
3351 match suffix with
3352 | 'k' -> String.sub s 0 (l-1), 10
3353 | 'm' -> String.sub s 0 (l-1), 20
3354 | 'g' -> String.sub s 0 (l-1), 30
3355 | _ -> s, 0
3356 else s, 0
3358 let n = int_of_string s1 in
3359 let m = n lsl shift in
3360 if m < 0 || m < n
3361 then raise (Failure "value too large")
3362 else m
3365 let string_with_suffix_of_int n =
3366 if n = 0
3367 then "0"
3368 else
3369 let n, s =
3370 if n = 0
3371 then 0, ""
3372 else (
3373 if n land ((1 lsl 20) - 1) = 0
3374 then n lsr 20, "M"
3375 else (
3376 if n land ((1 lsl 10) - 1) = 0
3377 then n lsr 10, "K"
3378 else n, ""
3382 let rec loop s n =
3383 let h = n mod 1000 in
3384 let n = n / 1000 in
3385 if n = 0
3386 then string_of_int h ^ s
3387 else (
3388 let s = Printf.sprintf "_%03d%s" h s in
3389 loop s n
3392 loop "" n ^ s;
3395 let defghyllscroll = (40, 8, 32);;
3396 let ghyllscroll_of_string s =
3397 let (n, a, b) as nab =
3398 if s = "default"
3399 then defghyllscroll
3400 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3402 if n <= a || n <= b || a >= b
3403 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3404 nab;
3407 let ghyllscroll_to_string ((n, a, b) as nab) =
3408 if nab = defghyllscroll
3409 then "default"
3410 else Printf.sprintf "%d,%d,%d" n a b;
3413 let describe_location () =
3414 let f (fn, _) l =
3415 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3417 let fn, ln = List.fold_left f (-1, -1) state.layout in
3418 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3419 let percent =
3420 if maxy <= 0
3421 then 100.
3422 else (100. *. (float state.y /. float maxy))
3424 if fn = ln
3425 then
3426 Printf.sprintf "page %d of %d [%.2f%%]"
3427 (fn+1) state.pagecount percent
3428 else
3429 Printf.sprintf
3430 "pages %d-%d of %d [%.2f%%]"
3431 (fn+1) (ln+1) state.pagecount percent
3434 let enterinfomode =
3435 let btos b = if b then "\xe2\x88\x9a" else "" in
3436 let showextended = ref false in
3437 let leave mode = function
3438 | Confirm -> state.mode <- mode
3439 | Cancel -> state.mode <- mode in
3440 let src =
3441 (object
3442 val mutable m_first_time = true
3443 val mutable m_l = []
3444 val mutable m_a = [||]
3445 val mutable m_prev_uioh = nouioh
3446 val mutable m_prev_mode = View
3448 inherit lvsourcebase
3450 method reset prev_mode prev_uioh =
3451 m_a <- Array.of_list (List.rev m_l);
3452 m_l <- [];
3453 m_prev_mode <- prev_mode;
3454 m_prev_uioh <- prev_uioh;
3455 if m_first_time
3456 then (
3457 let rec loop n =
3458 if n >= Array.length m_a
3459 then ()
3460 else
3461 match m_a.(n) with
3462 | _, _, _, Action _ -> m_active <- n
3463 | _ -> loop (n+1)
3465 loop 0;
3466 m_first_time <- false;
3469 method int name get set =
3470 m_l <-
3471 (name, `int get, 1, Action (
3472 fun u ->
3473 let ondone s =
3474 try set (int_of_string s)
3475 with exn ->
3476 state.text <- Printf.sprintf "bad integer `%s': %s"
3477 s (Printexc.to_string exn)
3479 state.text <- "";
3480 let te = name ^ ": ", "", None, intentry, ondone in
3481 state.mode <- Textentry (te, leave m_prev_mode);
3483 )) :: m_l
3485 method int_with_suffix name get set =
3486 m_l <-
3487 (name, `intws get, 1, Action (
3488 fun u ->
3489 let ondone s =
3490 try set (int_of_string_with_suffix s)
3491 with exn ->
3492 state.text <- Printf.sprintf "bad integer `%s': %s"
3493 s (Printexc.to_string exn)
3495 state.text <- "";
3496 let te =
3497 name ^ ": ", "", None, intentry_with_suffix, ondone
3499 state.mode <- Textentry (te, leave m_prev_mode);
3501 )) :: m_l
3503 method bool ?(offset=1) ?(btos=btos) name get set =
3504 m_l <-
3505 (name, `bool (btos, get), offset, Action (
3506 fun u ->
3507 let v = get () in
3508 set (not v);
3510 )) :: m_l
3512 method color name get set =
3513 m_l <-
3514 (name, `color get, 1, Action (
3515 fun u ->
3516 let invalid = (nan, nan, nan) in
3517 let ondone s =
3518 let c =
3519 try color_of_string s
3520 with exn ->
3521 state.text <- Printf.sprintf "bad color `%s': %s"
3522 s (Printexc.to_string exn);
3523 invalid
3525 if c <> invalid
3526 then set c;
3528 let te = name ^ ": ", "", None, textentry, ondone in
3529 state.text <- color_to_string (get ());
3530 state.mode <- Textentry (te, leave m_prev_mode);
3532 )) :: m_l
3534 method string name get set =
3535 m_l <-
3536 (name, `string get, 1, Action (
3537 fun u ->
3538 let ondone s = set s in
3539 let te = name ^ ": ", "", None, textentry, ondone in
3540 state.mode <- Textentry (te, leave m_prev_mode);
3542 )) :: m_l
3544 method colorspace name get set =
3545 m_l <-
3546 (name, `string get, 1, Action (
3547 fun _ ->
3548 let source =
3549 let vals = [| "rgb"; "bgr"; "gray" |] in
3550 (object
3551 inherit lvsourcebase
3553 initializer
3554 m_active <- int_of_colorspace conf.colorspace;
3555 m_first <- 0;
3557 method getitemcount = Array.length vals
3558 method getitem n = (vals.(n), 0)
3559 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3560 ignore (uioh, first, pan, qsearch);
3561 if not cancel then set active;
3562 None
3563 method hasaction _ = true
3564 end)
3566 state.text <- "";
3567 coe (new listview ~source ~trusted:true)
3568 )) :: m_l
3570 method caption s offset =
3571 m_l <- (s, `empty, offset, Noaction) :: m_l
3573 method caption2 s f offset =
3574 m_l <- (s, `string f, offset, Noaction) :: m_l
3576 method getitemcount = Array.length m_a
3578 method getitem n =
3579 let tostr = function
3580 | `int f -> string_of_int (f ())
3581 | `intws f -> string_with_suffix_of_int (f ())
3582 | `string f -> f ()
3583 | `color f -> color_to_string (f ())
3584 | `bool (btos, f) -> btos (f ())
3585 | `empty -> ""
3587 let name, t, offset, _ = m_a.(n) in
3588 ((let s = tostr t in
3589 if String.length s > 0
3590 then Printf.sprintf "%s\t%s" name s
3591 else name),
3592 offset)
3594 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3595 let uiohopt =
3596 if not cancel
3597 then (
3598 m_qsearch <- qsearch;
3599 let uioh =
3600 match m_a.(active) with
3601 | _, _, _, Action f -> f uioh
3602 | _ -> uioh
3604 Some uioh
3606 else None
3608 m_active <- active;
3609 m_first <- first;
3610 m_pan <- pan;
3611 uiohopt
3613 method hasaction n =
3614 match m_a.(n) with
3615 | _, _, _, Action _ -> true
3616 | _ -> false
3617 end)
3619 let rec fillsrc prevmode prevuioh =
3620 let sep () = src#caption "" 0 in
3621 let colorp name get set =
3622 src#string name
3623 (fun () -> color_to_string (get ()))
3624 (fun v ->
3626 let c = color_of_string v in
3627 set c
3628 with exn ->
3629 state.text <- Printf.sprintf "bad color `%s': %s"
3630 v (Printexc.to_string exn);
3633 let oldmode = state.mode in
3634 let birdseye = isbirdseye state.mode in
3636 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3638 src#bool "presentation mode"
3639 (fun () -> conf.presentation)
3640 (fun v ->
3641 conf.presentation <- v;
3642 state.anchor <- getanchor ();
3643 represent ());
3645 src#bool "ignore case in searches"
3646 (fun () -> conf.icase)
3647 (fun v -> conf.icase <- v);
3649 src#bool "preload"
3650 (fun () -> conf.preload)
3651 (fun v -> conf.preload <- v);
3653 src#bool "highlight links"
3654 (fun () -> conf.hlinks)
3655 (fun v -> conf.hlinks <- v);
3657 src#bool "under info"
3658 (fun () -> conf.underinfo)
3659 (fun v -> conf.underinfo <- v);
3661 src#bool "persistent bookmarks"
3662 (fun () -> conf.savebmarks)
3663 (fun v -> conf.savebmarks <- v);
3665 src#bool "proportional display"
3666 (fun () -> conf.proportional)
3667 (fun v -> reqlayout conf.angle v);
3669 src#bool "trim margins"
3670 (fun () -> conf.trimmargins)
3671 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3673 src#bool "persistent location"
3674 (fun () -> conf.jumpback)
3675 (fun v -> conf.jumpback <- v);
3677 sep ();
3678 src#int "inter-page space"
3679 (fun () -> conf.interpagespace)
3680 (fun n ->
3681 conf.interpagespace <- n;
3682 let pageno, py =
3683 match state.layout with
3684 | [] -> 0, 0
3685 | l :: _ ->
3686 l.pageno, l.pagey
3688 state.maxy <- calcheight ();
3689 let y = getpagey pageno in
3690 gotoy (y + py)
3693 src#int "page bias"
3694 (fun () -> conf.pagebias)
3695 (fun v -> conf.pagebias <- v);
3697 src#int "scroll step"
3698 (fun () -> conf.scrollstep)
3699 (fun n -> conf.scrollstep <- n);
3701 src#int "auto scroll step"
3702 (fun () ->
3703 match state.autoscroll with
3704 | Some step -> step
3705 | _ -> conf.autoscrollstep)
3706 (fun n ->
3707 if state.autoscroll <> None
3708 then state.autoscroll <- Some n;
3709 conf.autoscrollstep <- n);
3711 src#int "zoom"
3712 (fun () -> truncate (conf.zoom *. 100.))
3713 (fun v -> setzoom ((float v) /. 100.));
3715 src#int "rotation"
3716 (fun () -> conf.angle)
3717 (fun v -> reqlayout v conf.proportional);
3719 src#int "scroll bar width"
3720 (fun () -> state.scrollw)
3721 (fun v ->
3722 state.scrollw <- v;
3723 conf.scrollbw <- v;
3724 reshape conf.winw conf.winh;
3727 src#int "scroll handle height"
3728 (fun () -> conf.scrollh)
3729 (fun v -> conf.scrollh <- v;);
3731 src#int "thumbnail width"
3732 (fun () -> conf.thumbw)
3733 (fun v ->
3734 conf.thumbw <- min 4096 v;
3735 match oldmode with
3736 | Birdseye beye ->
3737 leavebirdseye beye false;
3738 enterbirdseye ()
3739 | _ -> ()
3742 src#string "columns"
3743 (fun () ->
3744 match conf.columns with
3745 | None -> "1"
3746 | Some (multicol, _) -> columns_to_string multicol)
3747 (fun v ->
3748 let n, a, b = columns_of_string v in
3749 setcolumns n a b);
3751 sep ();
3752 src#caption "Presentation mode" 0;
3753 src#bool "scrollbar visible"
3754 (fun () -> conf.scrollbarinpm)
3755 (fun v ->
3756 if v != conf.scrollbarinpm
3757 then (
3758 conf.scrollbarinpm <- v;
3759 if conf.presentation
3760 then (
3761 state.scrollw <- if v then conf.scrollbw else 0;
3762 reshape conf.winw conf.winh;
3767 sep ();
3768 src#caption "Pixmap cache" 0;
3769 src#int_with_suffix "size (advisory)"
3770 (fun () -> conf.memlimit)
3771 (fun v -> conf.memlimit <- v);
3773 src#caption2 "used"
3774 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3775 (string_with_suffix_of_int state.memused)
3776 (Hashtbl.length state.tilemap)) 1;
3778 sep ();
3779 src#caption "Layout" 0;
3780 src#caption2 "Dimension"
3781 (fun () ->
3782 Printf.sprintf "%dx%d (virtual %dx%d)"
3783 conf.winw conf.winh
3784 state.w state.maxy)
3786 if conf.debug
3787 then
3788 src#caption2 "Position" (fun () ->
3789 Printf.sprintf "%dx%d" state.x state.y
3791 else
3792 src#caption2 "Visible" (fun () -> describe_location ()) 1
3795 sep ();
3796 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3797 "Save these parameters as global defaults at exit"
3798 (fun () -> conf.bedefault)
3799 (fun v -> conf.bedefault <- v)
3802 sep ();
3803 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3804 src#bool ~offset:0 ~btos "Extended parameters"
3805 (fun () -> !showextended)
3806 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3807 if !showextended
3808 then (
3809 src#bool "checkers"
3810 (fun () -> conf.checkers)
3811 (fun v -> conf.checkers <- v; setcheckers v);
3812 src#bool "verbose"
3813 (fun () -> conf.verbose)
3814 (fun v -> conf.verbose <- v);
3815 src#bool "invert colors"
3816 (fun () -> conf.invert)
3817 (fun v -> conf.invert <- v);
3818 src#bool "max fit"
3819 (fun () -> conf.maxhfit)
3820 (fun v -> conf.maxhfit <- v);
3821 src#bool "redirect stderr"
3822 (fun () -> conf.redirectstderr)
3823 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3824 src#string "uri launcher"
3825 (fun () -> conf.urilauncher)
3826 (fun v -> conf.urilauncher <- v);
3827 src#string "tile size"
3828 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3829 (fun v ->
3831 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3832 conf.tileh <- max 64 w;
3833 conf.tilew <- max 64 h;
3834 flushtiles ();
3835 with exn ->
3836 state.text <- Printf.sprintf "bad tile size `%s': %s"
3837 v (Printexc.to_string exn));
3838 src#int "texture count"
3839 (fun () -> conf.texcount)
3840 (fun v ->
3841 if realloctexts v
3842 then conf.texcount <- v
3843 else showtext '!' " Failed to set texture count please retry later"
3845 src#int "slice height"
3846 (fun () -> conf.sliceheight)
3847 (fun v ->
3848 conf.sliceheight <- v;
3849 wcmd "sliceh" [`i conf.sliceheight];
3851 src#int "anti-aliasing level"
3852 (fun () -> conf.aalevel)
3853 (fun v ->
3854 conf.aalevel <- bound v 0 8;
3855 state.anchor <- getanchor ();
3856 opendoc state.path state.password;
3858 src#int "ui font size"
3859 (fun () -> fstate.fontsize)
3860 (fun v -> setfontsize (bound v 5 100));
3861 colorp "background color"
3862 (fun () -> conf.bgcolor)
3863 (fun v -> conf.bgcolor <- v);
3864 src#bool "crop hack"
3865 (fun () -> conf.crophack)
3866 (fun v -> conf.crophack <- v);
3867 src#string "trim fuzz"
3868 (fun () -> irect_to_string conf.trimfuzz)
3869 (fun v ->
3871 conf.trimfuzz <- irect_of_string v;
3872 if conf.trimmargins
3873 then settrim true conf.trimfuzz;
3874 with exn ->
3875 state.text <- Printf.sprintf "bad irect `%s': %s"
3876 v (Printexc.to_string exn)
3878 src#string "throttle"
3879 (fun () ->
3880 match conf.maxwait with
3881 | None -> "show place holder if page is not ready"
3882 | Some time ->
3883 if time = infinity
3884 then "wait for page to fully render"
3885 else
3886 "wait " ^ string_of_float time
3887 ^ " seconds before showing placeholder"
3889 (fun v ->
3891 let f = float_of_string v in
3892 if f <= 0.0
3893 then conf.maxwait <- None
3894 else conf.maxwait <- Some f
3895 with exn ->
3896 state.text <- Printf.sprintf "bad time `%s': %s"
3897 v (Printexc.to_string exn)
3899 src#string "ghyll scroll"
3900 (fun () ->
3901 match conf.ghyllscroll with
3902 | None -> ""
3903 | Some nab -> ghyllscroll_to_string nab
3905 (fun v ->
3907 let gs =
3908 if String.length v = 0
3909 then None
3910 else Some (ghyllscroll_of_string v)
3912 conf.ghyllscroll <- gs
3913 with exn ->
3914 state.text <- Printf.sprintf "bad ghyll `%s': %s"
3915 v (Printexc.to_string exn)
3917 src#string "selection command"
3918 (fun () -> conf.selcmd)
3919 (fun v -> conf.selcmd <- v);
3920 src#colorspace "color space"
3921 (fun () -> colorspace_to_string conf.colorspace)
3922 (fun v ->
3923 conf.colorspace <- colorspace_of_int v;
3924 wcmd "cs" [`i v];
3925 load state.layout;
3929 sep ();
3930 src#caption "Document" 0;
3931 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3932 src#caption2 "Pages"
3933 (fun () -> string_of_int state.pagecount) 1;
3934 src#caption2 "Dimensions"
3935 (fun () -> string_of_int (List.length state.pdims)) 1;
3936 if conf.trimmargins
3937 then (
3938 sep ();
3939 src#caption "Trimmed margins" 0;
3940 src#caption2 "Dimensions"
3941 (fun () -> string_of_int (List.length state.pdims)) 1;
3944 src#reset prevmode prevuioh;
3946 fun () ->
3947 state.text <- "";
3948 let prevmode = state.mode
3949 and prevuioh = state.uioh in
3950 fillsrc prevmode prevuioh;
3951 let source = (src :> lvsource) in
3952 state.uioh <- coe (object (self)
3953 inherit listview ~source ~trusted:true as super
3954 val mutable m_prevmemused = 0
3955 method infochanged = function
3956 | Memused ->
3957 if m_prevmemused != state.memused
3958 then (
3959 m_prevmemused <- state.memused;
3960 G.postRedisplay "memusedchanged";
3962 | Pdim -> G.postRedisplay "pdimchanged"
3963 | Docinfo -> fillsrc prevmode prevuioh
3965 method key key mask =
3966 if not (Wsi.withctrl mask)
3967 then
3968 match key with
3969 | 0xff51 -> coe (self#updownlevel ~-1)
3970 | 0xff53 -> coe (self#updownlevel 1)
3971 | _ -> super#key key mask
3972 else super#key key mask
3973 end);
3974 G.postRedisplay "info";
3977 let enterhelpmode =
3978 let source =
3979 (object
3980 inherit lvsourcebase
3981 method getitemcount = Array.length state.help
3982 method getitem n =
3983 let s, n, _ = state.help.(n) in
3984 (s, n)
3986 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3987 let optuioh =
3988 if not cancel
3989 then (
3990 m_qsearch <- qsearch;
3991 match state.help.(active) with
3992 | _, _, Action f -> Some (f uioh)
3993 | _ -> Some (uioh)
3995 else None
3997 m_active <- active;
3998 m_first <- first;
3999 m_pan <- pan;
4000 optuioh
4002 method hasaction n =
4003 match state.help.(n) with
4004 | _, _, Action _ -> true
4005 | _ -> false
4007 initializer
4008 m_active <- -1
4009 end)
4010 in fun () ->
4011 state.uioh <- coe (new listview ~source ~trusted:true);
4012 G.postRedisplay "help";
4015 let entermsgsmode =
4016 let msgsource =
4017 let re = Str.regexp "[\r\n]" in
4018 (object
4019 inherit lvsourcebase
4020 val mutable m_items = [||]
4022 method getitemcount = 1 + Array.length m_items
4024 method getitem n =
4025 if n = 0
4026 then "[Clear]", 0
4027 else m_items.(n-1), 0
4029 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4030 ignore uioh;
4031 if not cancel
4032 then (
4033 if active = 0
4034 then Buffer.clear state.errmsgs;
4035 m_qsearch <- qsearch;
4037 m_active <- active;
4038 m_first <- first;
4039 m_pan <- pan;
4040 None
4042 method hasaction n =
4043 n = 0
4045 method reset =
4046 state.newerrmsgs <- false;
4047 let l = Str.split re (Buffer.contents state.errmsgs) in
4048 m_items <- Array.of_list l
4050 initializer
4051 m_active <- 0
4052 end)
4053 in fun () ->
4054 state.text <- "";
4055 msgsource#reset;
4056 let source = (msgsource :> lvsource) in
4057 state.uioh <- coe (object
4058 inherit listview ~source ~trusted:false as super
4059 method display =
4060 if state.newerrmsgs
4061 then msgsource#reset;
4062 super#display
4063 end);
4064 G.postRedisplay "msgs";
4067 let quickbookmark ?title () =
4068 match state.layout with
4069 | [] -> ()
4070 | l :: _ ->
4071 let title =
4072 match title with
4073 | None ->
4074 let sec = Unix.gettimeofday () in
4075 let tm = Unix.localtime sec in
4076 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4077 (l.pageno+1)
4078 tm.Unix.tm_mday
4079 tm.Unix.tm_mon
4080 (tm.Unix.tm_year + 1900)
4081 tm.Unix.tm_hour
4082 tm.Unix.tm_min
4083 | Some title -> title
4085 state.bookmarks <-
4086 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4087 :: state.bookmarks
4090 let doreshape w h =
4091 state.fullscreen <- None;
4092 Wsi.reshape w h;
4095 let setautoscrollspeed step goingdown =
4096 let incr = max 1 ((abs step) / 2) in
4097 let incr = if goingdown then incr else -incr in
4098 let astep = step + incr in
4099 state.autoscroll <- Some astep;
4102 let viewkeyboard key mask =
4103 let enttext te =
4104 let mode = state.mode in
4105 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4106 state.text <- "";
4107 enttext ();
4108 G.postRedisplay "view:enttext"
4110 match key with
4111 | 81 -> (* Q *)
4112 exit 0
4114 | 0xff1b | 113 -> (* escape / q *)
4115 begin match state.mstate with
4116 | Mzoomrect _ ->
4117 state.mstate <- Mnone;
4118 Wsi.setcursor Wsi.CURSOR_INHERIT;
4119 G.postRedisplay "kill zoom rect";
4120 | _ ->
4121 match state.ranchors with
4122 | [] -> raise Wsi.Quit
4123 | (path, password, anchor) :: rest ->
4124 state.ranchors <- rest;
4125 state.anchor <- anchor;
4126 opendoc path password
4127 end;
4129 | 0xff08 -> (* backspace *)
4130 let y = getnav ~-1 in
4131 gotoy_and_clear_text y
4133 | 111 -> (* o *)
4134 enteroutlinemode ()
4136 | 117 -> (* u *)
4137 state.rects <- [];
4138 state.text <- "";
4139 G.postRedisplay "dehighlight";
4141 | 47 | 63 -> (* / ? *)
4142 let ondone isforw s =
4143 cbput state.hists.pat s;
4144 state.searchpattern <- s;
4145 search s isforw
4147 let s = String.create 1 in
4148 s.[0] <- Char.chr key;
4149 enttext (s, "", Some (onhist state.hists.pat),
4150 textentry, ondone (key = 47))
4152 | 43 | 0xffab when Wsi.withctrl mask -> (* + *)
4153 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4154 setzoom (conf.zoom +. incr)
4156 | 43 | 0xffab -> (* + *)
4157 let ondone s =
4158 let n =
4159 try int_of_string s with exc ->
4160 state.text <- Printf.sprintf "bad integer `%s': %s"
4161 s (Printexc.to_string exc);
4162 max_int
4164 if n != max_int
4165 then (
4166 conf.pagebias <- n;
4167 state.text <- "page bias is now " ^ string_of_int n;
4170 enttext ("page bias: ", "", None, intentry, ondone)
4172 | 45 | 0xffad when Wsi.withctrl mask -> (* - *)
4173 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4174 setzoom (max 0.01 (conf.zoom -. decr))
4176 | 45 | 0xffad -> (* - *)
4177 let ondone msg = state.text <- msg in
4178 enttext (
4179 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4180 optentry state.mode, ondone
4183 | 48 when Wsi.withctrl mask -> (* 0 *)
4184 setzoom 1.0
4186 | 49 when Wsi.withctrl mask -> (* 1 *)
4187 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4188 if zoom < 1.0
4189 then setzoom zoom
4191 | 0xffc6 -> (* f9 *)
4192 togglebirdseye ()
4194 | 57 when Wsi.withctrl mask -> (* ctrl-9 *)
4195 togglebirdseye ()
4197 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4198 when not (Wsi.withctrl mask) -> (* 0..9 *)
4199 let ondone s =
4200 let n =
4201 try int_of_string s with exc ->
4202 state.text <- Printf.sprintf "bad integer `%s': %s"
4203 s (Printexc.to_string exc);
4206 if n >= 0
4207 then (
4208 addnav ();
4209 cbput state.hists.pag (string_of_int n);
4210 gotopage1 (n + conf.pagebias - 1) 0;
4213 let pageentry text key =
4214 match Char.unsafe_chr key with
4215 | 'g' -> TEdone text
4216 | _ -> intentry text key
4218 let text = "x" in text.[0] <- Char.chr key;
4219 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4221 | 98 -> (* b *)
4222 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4223 reshape conf.winw conf.winh;
4225 | 108 -> (* l *)
4226 conf.hlinks <- not conf.hlinks;
4227 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4228 G.postRedisplay "toggle highlightlinks";
4230 | 97 -> (* a *)
4231 begin match state.autoscroll with
4232 | Some step ->
4233 conf.autoscrollstep <- step;
4234 state.autoscroll <- None
4235 | None ->
4236 if conf.autoscrollstep = 0
4237 then state.autoscroll <- Some 1
4238 else state.autoscroll <- Some conf.autoscrollstep
4241 | 80 -> (* P *)
4242 conf.presentation <- not conf.presentation;
4243 if conf.presentation
4244 then (
4245 if not conf.scrollbarinpm
4246 then state.scrollw <- 0;
4248 else
4249 state.scrollw <- conf.scrollbw;
4251 showtext ' ' ("presentation mode " ^
4252 if conf.presentation then "on" else "off");
4253 state.anchor <- getanchor ();
4254 represent ()
4256 | 102 -> (* f *)
4257 begin match state.fullscreen with
4258 | None ->
4259 state.fullscreen <- Some (conf.winw, conf.winh);
4260 Wsi.fullscreen ()
4261 | Some (w, h) ->
4262 state.fullscreen <- None;
4263 doreshape w h
4266 | 103 -> (* g *)
4267 gotoy_and_clear_text 0
4269 | 71 -> (* G *)
4270 gotopage1 (state.pagecount - 1) 0
4272 | 112 | 78 -> (* p|N *)
4273 search state.searchpattern false
4275 | 110 -> (* n *)
4276 search state.searchpattern true
4278 | 116 -> (* t *)
4279 begin match state.layout with
4280 | [] -> ()
4281 | l :: _ ->
4282 gotoy_and_clear_text (getpagey l.pageno)
4285 | 32 -> (* ' ' *)
4286 begin match List.rev state.layout with
4287 | [] -> ()
4288 | l :: _ ->
4289 let pageno = min (l.pageno+1) (state.pagecount-1) in
4290 gotoy_and_clear_text (getpagey pageno)
4293 | 0xff9f | 0xffff -> (* delete *)
4294 begin match state.layout with
4295 | [] -> ()
4296 | l :: _ ->
4297 let pageno = max 0 (l.pageno-1) in
4298 gotoy_and_clear_text (getpagey pageno)
4301 | 61 -> (* = *)
4302 showtext ' ' (describe_location ());
4304 | 119 -> (* w *)
4305 begin match state.layout with
4306 | [] -> ()
4307 | l :: _ ->
4308 doreshape (l.pagew + state.scrollw) l.pageh;
4309 G.postRedisplay "w"
4312 | 39 -> (* ' *)
4313 enterbookmarkmode ()
4315 | 104 -> (* h *)
4316 enterhelpmode ()
4318 | 105 -> (* i *)
4319 enterinfomode ()
4321 | 101 when conf.redirectstderr -> (* e *)
4322 entermsgsmode ()
4324 | 109 -> (* m *)
4325 let ondone s =
4326 match state.layout with
4327 | l :: _ ->
4328 state.bookmarks <-
4329 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4330 :: state.bookmarks
4331 | _ -> ()
4333 enttext ("bookmark: ", "", None, textentry, ondone)
4335 | 126 -> (* ~ *)
4336 quickbookmark ();
4337 showtext ' ' "Quick bookmark added";
4339 | 122 -> (* z *)
4340 begin match state.layout with
4341 | l :: _ ->
4342 let rect = getpdimrect l.pagedimno in
4343 let w, h =
4344 if conf.crophack
4345 then
4346 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4347 truncate (1.2 *. (rect.(3) -. rect.(0))))
4348 else
4349 (truncate (rect.(1) -. rect.(0)),
4350 truncate (rect.(3) -. rect.(0)))
4352 let w = truncate ((float w)*.conf.zoom)
4353 and h = truncate ((float h)*.conf.zoom) in
4354 if w != 0 && h != 0
4355 then (
4356 state.anchor <- getanchor ();
4357 doreshape (w + state.scrollw) (h + conf.interpagespace)
4359 G.postRedisplay "z";
4361 | [] -> ()
4364 | 50 when Wsi.withctrl mask -> (* ctrl-2 *)
4365 let maxw = getmaxw () in
4366 if maxw > 0.0
4367 then setzoom (maxw /. float conf.winw)
4369 | 60 | 62 -> (* < > *)
4370 reqlayout (conf.angle + (if key = 60 then 30 else -30)) conf.proportional
4372 | 91 | 93 -> (* [ ] *)
4373 conf.colorscale <-
4374 bound (conf.colorscale +. (if key = 92 then 0.1 else -0.1)) 0.0 1.0
4376 G.postRedisplay "brightness";
4378 | 107 | 0xff52 -> (* k UP *)
4379 begin match state.autoscroll with
4380 | None ->
4381 begin match state.mode with
4382 | Birdseye beye -> upbirdseye 1 beye
4383 | _ ->
4384 if Wsi.withctrl mask
4385 then gotoy (-clamp (conf.winh/2))
4386 else gotoy (clamp (-conf.scrollstep))
4388 | Some n ->
4389 setautoscrollspeed n false
4392 | 106 | 0xff54 -> (* j DOWN *)
4393 begin match state.autoscroll with
4394 | None ->
4395 begin match state.mode with
4396 | Birdseye beye -> downbirdseye 1 beye
4397 | _ ->
4398 if Wsi.withctrl mask
4399 then gotoy (clamp (conf.winh/2))
4400 else gotoy (clamp conf.scrollstep)
4402 | Some n ->
4403 setautoscrollspeed n true
4406 | 0xff51 | 0xff53 (* left / right *)
4407 when conf.zoom > 1.0->
4408 let dx =
4409 if Wsi.withctrl mask
4410 then conf.winw / 2
4411 else 10
4413 let dx = if key = 0xff51 then dx else -dx in
4414 state.x <- state.x + dx;
4415 gotoy state.y
4417 | 0xff55 -> (* prior *)
4418 let y =
4419 if Wsi.withctrl mask
4420 then
4421 match state.layout with
4422 | [] -> state.y
4423 | l :: _ -> state.y - l.pagey
4424 else
4425 clamp (-conf.winh)
4427 gotoghyll y
4429 | 0xff56 -> (* next *)
4430 let y =
4431 if Wsi.withctrl mask
4432 then
4433 match List.rev state.layout with
4434 | [] -> state.y
4435 | l :: _ -> getpagey l.pageno
4436 else
4437 clamp conf.winh
4439 gotoghyll y
4441 | 0xff50 -> gotoghyll 0
4442 | 0xff57 -> gotoghyll (clamp state.maxy)
4443 | 0xff53 when Wsi.withalt mask ->
4444 gotoghyll (getnav ~-1)
4445 | 0xff51 when Wsi.withalt mask ->
4446 gotoghyll (getnav 1)
4448 | 114 -> (* r *)
4449 state.anchor <- getanchor ();
4450 opendoc state.path state.password
4452 | 118 when conf.debug -> (* v *)
4453 state.rects <- [];
4454 List.iter (fun l ->
4455 match getopaque l.pageno with
4456 | None -> ()
4457 | Some opaque ->
4458 let x0, y0, x1, y1 = pagebbox opaque in
4459 let a,b = float x0, float y0 in
4460 let c,d = float x1, float y0 in
4461 let e,f = float x1, float y1 in
4462 let h,j = float x0, float y1 in
4463 let rect = (a,b,c,d,e,f,h,j) in
4464 debugrect rect;
4465 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4466 ) state.layout;
4467 G.postRedisplay "v";
4469 | _ ->
4470 vlog "huh? %d" key
4473 let keyboard key mask =
4474 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4475 then wcmd "interrupt" []
4476 else state.uioh <- state.uioh#key key mask
4479 let birdseyekeyboard key mask
4480 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4481 let incr =
4482 match conf.columns with
4483 | None -> 1
4484 | Some ((c, _, _), _) -> c
4486 match key with
4487 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4488 let y, h = getpageyh pageno in
4489 let top = (conf.winh - h) / 2 in
4490 gotoy (max 0 (y - top))
4491 | 0xff0d -> leavebirdseye beye false
4492 | 0xff1b -> leavebirdseye beye true (* escape *)
4493 | 0xff52 -> upbirdseye incr beye (* prior *)
4494 | 0xff54 -> downbirdseye incr beye (* next *)
4495 | 0xff51 -> upbirdseye 1 beye (* up *)
4496 | 0xff53 -> downbirdseye 1 beye (* down *)
4498 | 0xff55 ->
4499 begin match state.layout with
4500 | l :: _ ->
4501 if l.pagey != 0
4502 then (
4503 state.mode <- Birdseye (
4504 oconf, leftx, l.pageno, hooverpageno, anchor
4506 gotopage1 l.pageno 0;
4508 else (
4509 let layout = layout (state.y-conf.winh) conf.winh in
4510 match layout with
4511 | [] -> gotoy (clamp (-conf.winh))
4512 | l :: _ ->
4513 state.mode <- Birdseye (
4514 oconf, leftx, l.pageno, hooverpageno, anchor
4516 gotopage1 l.pageno 0
4519 | [] -> gotoy (clamp (-conf.winh))
4520 end;
4522 | 0xff56 ->
4523 begin match List.rev state.layout with
4524 | l :: _ ->
4525 let layout = layout (state.y + conf.winh) conf.winh in
4526 begin match layout with
4527 | [] ->
4528 let incr = l.pageh - l.pagevh in
4529 if incr = 0
4530 then (
4531 state.mode <-
4532 Birdseye (
4533 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4535 G.postRedisplay "birdseye pagedown";
4537 else gotoy (clamp (incr + conf.interpagespace*2));
4539 | l :: _ ->
4540 state.mode <-
4541 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4542 gotopage1 l.pageno 0;
4545 | [] -> gotoy (clamp conf.winh)
4546 end;
4548 | 0xff50 ->
4549 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4550 gotopage1 0 0
4552 | 0xff57 ->
4553 let pageno = state.pagecount - 1 in
4554 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4555 if not (pagevisible state.layout pageno)
4556 then
4557 let h =
4558 match List.rev state.pdims with
4559 | [] -> conf.winh
4560 | (_, _, h, _) :: _ -> h
4562 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4563 else G.postRedisplay "birdseye end";
4564 | _ -> viewkeyboard key mask
4567 let drawpage l =
4568 let color =
4569 match state.mode with
4570 | Textentry _ -> scalecolor 0.4
4571 | View -> scalecolor 1.0
4572 | Birdseye (_, _, pageno, hooverpageno, _) ->
4573 if l.pageno = hooverpageno
4574 then scalecolor 0.9
4575 else (
4576 if l.pageno = pageno
4577 then scalecolor 1.0
4578 else scalecolor 0.8
4581 drawtiles l color;
4582 begin match getopaque l.pageno with
4583 | Some opaque ->
4584 if tileready l l.pagex l.pagey
4585 then
4586 let x = l.pagedispx - l.pagex
4587 and y = l.pagedispy - l.pagey in
4588 postprocess opaque conf.hlinks x y;
4590 | _ -> ()
4591 end;
4594 let scrollindicator () =
4595 let sbw, ph, sh = state.uioh#scrollph in
4596 let sbh, pw, sw = state.uioh#scrollpw in
4598 GlDraw.color (0.64, 0.64, 0.64);
4599 GlDraw.rect
4600 (float (conf.winw - sbw), 0.)
4601 (float conf.winw, float conf.winh)
4603 GlDraw.rect
4604 (0., float (conf.winh - sbh))
4605 (float (conf.winw - state.scrollw - 1), float conf.winh)
4607 GlDraw.color (0.0, 0.0, 0.0);
4609 GlDraw.rect
4610 (float (conf.winw - sbw), ph)
4611 (float conf.winw, ph +. sh)
4613 GlDraw.rect
4614 (pw, float (conf.winh - sbh))
4615 (pw +. sw, float conf.winh)
4619 let pagetranslatepoint l x y =
4620 let dy = y - l.pagedispy in
4621 let y = dy + l.pagey in
4622 let dx = x - l.pagedispx in
4623 let x = dx + l.pagex in
4624 (x, y);
4627 let showsel () =
4628 match state.mstate with
4629 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4632 | Msel ((x0, y0), (x1, y1)) ->
4633 let rec loop = function
4634 | l :: ls ->
4635 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4636 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4637 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4638 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4639 then
4640 match getopaque l.pageno with
4641 | Some opaque ->
4642 let dx, dy = pagetranslatepoint l 0 0 in
4643 let x0 = x0 + dx
4644 and y0 = y0 + dy
4645 and x1 = x1 + dx
4646 and y1 = y1 + dy in
4647 GlMat.mode `modelview;
4648 GlMat.push ();
4649 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4650 seltext opaque (x0, y0, x1, y1);
4651 GlMat.pop ();
4652 | _ -> ()
4653 else loop ls
4654 | [] -> ()
4656 loop state.layout
4659 let showrects () =
4660 Gl.enable `blend;
4661 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4662 GlDraw.polygon_mode `both `fill;
4663 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4664 List.iter
4665 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4666 List.iter (fun l ->
4667 if l.pageno = pageno
4668 then (
4669 let dx = float (l.pagedispx - l.pagex) in
4670 let dy = float (l.pagedispy - l.pagey) in
4671 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4672 GlDraw.begins `quads;
4674 GlDraw.vertex2 (x0+.dx, y0+.dy);
4675 GlDraw.vertex2 (x1+.dx, y1+.dy);
4676 GlDraw.vertex2 (x2+.dx, y2+.dy);
4677 GlDraw.vertex2 (x3+.dx, y3+.dy);
4679 GlDraw.ends ();
4681 ) state.layout
4682 ) state.rects
4684 Gl.disable `blend;
4687 let display () =
4688 GlClear.color (scalecolor2 conf.bgcolor);
4689 GlClear.clear [`color];
4690 List.iter drawpage state.layout;
4691 showrects ();
4692 showsel ();
4693 state.uioh#display;
4694 scrollindicator ();
4695 begin match state.mstate with
4696 | Mzoomrect ((x0, y0), (x1, y1)) ->
4697 Gl.enable `blend;
4698 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4699 GlDraw.polygon_mode `both `fill;
4700 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4701 GlDraw.rect (float x0, float y0)
4702 (float x1, float y1);
4703 Gl.disable `blend;
4704 | _ -> ()
4705 end;
4706 enttext ();
4707 Wsi.swapb ();
4710 let getunder x y =
4711 let rec f = function
4712 | l :: rest ->
4713 begin match getopaque l.pageno with
4714 | Some opaque ->
4715 let x0 = l.pagedispx in
4716 let x1 = x0 + l.pagevw in
4717 let y0 = l.pagedispy in
4718 let y1 = y0 + l.pagevh in
4719 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4720 then
4721 let px, py = pagetranslatepoint l x y in
4722 match whatsunder opaque px py with
4723 | Unone -> f rest
4724 | under -> under
4725 else f rest
4726 | _ ->
4727 f rest
4729 | [] -> Unone
4731 f state.layout
4734 let zoomrect x y x1 y1 =
4735 let x0 = min x x1
4736 and x1 = max x x1
4737 and y0 = min y y1 in
4738 gotoy (state.y + y0);
4739 state.anchor <- getanchor ();
4740 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4741 let margin =
4742 if state.w < conf.winw - state.scrollw
4743 then (conf.winw - state.scrollw - state.w) / 2
4744 else 0
4746 state.x <- (state.x + margin) - x0;
4747 setzoom zoom;
4748 Wsi.setcursor Wsi.CURSOR_INHERIT;
4749 state.mstate <- Mnone;
4752 let scrollx x =
4753 let winw = conf.winw - state.scrollw - 1 in
4754 let s = float x /. float winw in
4755 let destx = truncate (float (state.w + winw) *. s) in
4756 state.x <- winw - destx;
4757 gotoy_and_clear_text state.y;
4758 state.mstate <- Mscrollx;
4761 let scrolly y =
4762 let s = float y /. float conf.winh in
4763 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4764 gotoy_and_clear_text desty;
4765 state.mstate <- Mscrolly;
4768 let viewmouse button down x y mask =
4769 match button with
4770 | n when (n == 4 || n == 5) && not down ->
4771 if Wsi.withctrl mask
4772 then (
4773 match state.mstate with
4774 | Mzoom (oldn, i) ->
4775 if oldn = n
4776 then (
4777 if i = 2
4778 then
4779 let incr =
4780 match n with
4781 | 5 ->
4782 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4783 | _ ->
4784 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4786 let zoom = conf.zoom -. incr in
4787 setzoom zoom;
4788 state.mstate <- Mzoom (n, 0);
4789 else
4790 state.mstate <- Mzoom (n, i+1);
4792 else state.mstate <- Mzoom (n, 0)
4794 | _ -> state.mstate <- Mzoom (n, 0)
4796 else (
4797 match state.autoscroll with
4798 | Some step -> setautoscrollspeed step (n=4)
4799 | None ->
4800 let incr =
4801 if n = 4
4802 then -conf.scrollstep
4803 else conf.scrollstep
4805 let incr = incr * 2 in
4806 let y = clamp incr in
4807 gotoy_and_clear_text y
4810 | 1 when Wsi.withctrl mask ->
4811 if down
4812 then (
4813 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
4814 state.mstate <- Mpan (x, y)
4816 else
4817 state.mstate <- Mnone
4819 | 3 ->
4820 if down
4821 then (
4822 Wsi.setcursor Wsi.CURSOR_CYCLE;
4823 let p = (x, y) in
4824 state.mstate <- Mzoomrect (p, p)
4826 else (
4827 match state.mstate with
4828 | Mzoomrect ((x0, y0), _) ->
4829 if abs (x-x0) > 10 && abs (y - y0) > 10
4830 then zoomrect x0 y0 x y
4831 else (
4832 state.mstate <- Mnone;
4833 Wsi.setcursor Wsi.CURSOR_INHERIT;
4834 G.postRedisplay "kill accidental zoom rect";
4836 | _ ->
4837 Wsi.setcursor Wsi.CURSOR_INHERIT;
4838 state.mstate <- Mnone
4841 | 1 when x > conf.winw - state.scrollw ->
4842 if down
4843 then
4844 let _, position, sh = state.uioh#scrollph in
4845 if y > truncate position && y < truncate (position +. sh)
4846 then state.mstate <- Mscrolly
4847 else scrolly y
4848 else
4849 state.mstate <- Mnone
4851 | 1 when y > conf.winh - state.hscrollh ->
4852 if down
4853 then
4854 let _, position, sw = state.uioh#scrollpw in
4855 if x > truncate position && x < truncate (position +. sw)
4856 then state.mstate <- Mscrollx
4857 else scrollx x
4858 else
4859 state.mstate <- Mnone
4861 | 1 ->
4862 let dest = if down then getunder x y else Unone in
4863 begin match dest with
4864 | Ulinkgoto (pageno, top) ->
4865 if pageno >= 0
4866 then (
4867 addnav ();
4868 gotopage1 pageno top;
4871 | Ulinkuri s ->
4872 gotouri s
4874 | Uremote (filename, pageno) ->
4875 let path =
4876 if Sys.file_exists filename
4877 then filename
4878 else
4879 let dir = Filename.dirname state.path in
4880 let path = Filename.concat dir filename in
4881 if Sys.file_exists path
4882 then path
4883 else ""
4885 if String.length path > 0
4886 then (
4887 let anchor = getanchor () in
4888 let ranchor = state.path, state.password, anchor in
4889 state.anchor <- (pageno, 0.0);
4890 state.ranchors <- ranchor :: state.ranchors;
4891 opendoc path "";
4893 else showtext '!' ("Could not find " ^ filename)
4895 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
4897 | Unone when down ->
4898 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
4899 state.mstate <- Mpan (x, y);
4901 | Unone | Utext _ ->
4902 if down
4903 then (
4904 if conf.angle mod 360 = 0
4905 then (
4906 state.mstate <- Msel ((x, y), (x, y));
4907 G.postRedisplay "mouse select";
4910 else (
4911 match state.mstate with
4912 | Mnone -> ()
4914 | Mzoom _ | Mscrollx | Mscrolly ->
4915 state.mstate <- Mnone
4917 | Mzoomrect ((x0, y0), _) ->
4918 zoomrect x0 y0 x y
4920 | Mpan _ ->
4921 Wsi.setcursor Wsi.CURSOR_INHERIT;
4922 state.mstate <- Mnone
4924 | Msel ((_, y0), (_, y1)) ->
4925 let rec loop = function
4926 | [] -> ()
4927 | l :: rest ->
4928 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4929 || ((y1 >= l.pagedispy
4930 && y1 <= (l.pagedispy + l.pagevh)))
4931 then
4932 match getopaque l.pageno with
4933 | Some opaque ->
4934 copysel conf.selcmd opaque;
4935 G.postRedisplay "copysel"
4936 | _ -> ()
4937 else loop rest
4939 loop state.layout;
4940 Wsi.setcursor Wsi.CURSOR_INHERIT;
4941 state.mstate <- Mnone;
4945 | _ -> ()
4948 let birdseyemouse button down x y mask
4949 (conf, leftx, _, hooverpageno, anchor) =
4950 match button with
4951 | 1 when down ->
4952 let rec loop = function
4953 | [] -> ()
4954 | l :: rest ->
4955 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4956 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4957 then (
4958 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4960 else loop rest
4962 loop state.layout
4963 | 3 -> ()
4964 | _ -> viewmouse button down x y mask
4967 let mouse button down x y mask =
4968 state.uioh <- state.uioh#button button down x y mask;
4971 let motion ~x ~y =
4972 state.uioh <- state.uioh#motion x y
4975 let pmotion ~x ~y =
4976 state.uioh <- state.uioh#pmotion x y;
4979 let uioh = object
4980 method display = ()
4982 method key key mask =
4983 begin match state.mode with
4984 | Textentry textentry -> textentrykeyboard key mask textentry
4985 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
4986 | View -> viewkeyboard key mask
4987 end;
4988 state.uioh
4990 method button button bstate x y mask =
4991 begin match state.mode with
4992 | View -> viewmouse button bstate x y mask
4993 | Birdseye beye -> birdseyemouse button bstate x y mask beye
4994 | Textentry _ -> ()
4995 end;
4996 state.uioh
4998 method motion x y =
4999 begin match state.mode with
5000 | Textentry _ -> ()
5001 | View | Birdseye _ ->
5002 match state.mstate with
5003 | Mzoom _ | Mnone -> ()
5005 | Mpan (x0, y0) ->
5006 let dx = x - x0
5007 and dy = y0 - y in
5008 state.mstate <- Mpan (x, y);
5009 if conf.zoom > 1.0 then state.x <- state.x + dx;
5010 let y = clamp dy in
5011 gotoy_and_clear_text y
5013 | Msel (a, _) ->
5014 state.mstate <- Msel (a, (x, y));
5015 G.postRedisplay "motion select";
5017 | Mscrolly ->
5018 let y = min conf.winh (max 0 y) in
5019 scrolly y
5021 | Mscrollx ->
5022 let x = min conf.winw (max 0 x) in
5023 scrollx x
5025 | Mzoomrect (p0, _) ->
5026 state.mstate <- Mzoomrect (p0, (x, y));
5027 G.postRedisplay "motion zoomrect";
5028 end;
5029 state.uioh
5031 method pmotion x y =
5032 begin match state.mode with
5033 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5034 let rec loop = function
5035 | [] ->
5036 if hooverpageno != -1
5037 then (
5038 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5039 G.postRedisplay "pmotion birdseye no hoover";
5041 | l :: rest ->
5042 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5043 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5044 then (
5045 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5046 G.postRedisplay "pmotion birdseye hoover";
5048 else loop rest
5050 loop state.layout
5052 | Textentry _ -> ()
5054 | View ->
5055 match state.mstate with
5056 | Mnone ->
5057 begin match getunder x y with
5058 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
5059 | Ulinkuri uri ->
5060 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
5061 Wsi.setcursor Wsi.CURSOR_INFO
5062 | Ulinkgoto (page, _) ->
5063 if conf.underinfo
5064 then showtext 'p' ("age: " ^ string_of_int (page+1));
5065 Wsi.setcursor Wsi.CURSOR_INFO
5066 | Utext s ->
5067 if conf.underinfo then showtext 'f' ("ont: " ^ s);
5068 Wsi.setcursor Wsi.CURSOR_TEXT
5069 | Uunexpected s ->
5070 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
5071 Wsi.setcursor Wsi.CURSOR_INHERIT
5072 | Ulaunch s ->
5073 if conf.underinfo then showtext 'l' ("launch: " ^ s);
5074 Wsi.setcursor Wsi.CURSOR_INHERIT
5075 | Unamed s ->
5076 if conf.underinfo then showtext 'n' ("named: " ^ s);
5077 Wsi.setcursor Wsi.CURSOR_INHERIT
5078 | Uremote (filename, pageno) ->
5079 if conf.underinfo then showtext 'r'
5080 (Printf.sprintf "emote: %s (%d)" filename pageno);
5081 Wsi.setcursor Wsi.CURSOR_INFO
5084 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5086 end;
5087 state.uioh
5089 method infochanged _ = ()
5091 method scrollph =
5092 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5093 let p, h = scrollph state.y maxy in
5094 state.scrollw, p, h
5096 method scrollpw =
5097 let winw = conf.winw - state.scrollw - 1 in
5098 let fwinw = float winw in
5099 let sw =
5100 let sw = fwinw /. float state.w in
5101 let sw = fwinw *. sw in
5102 max sw (float conf.scrollh)
5104 let position, sw =
5105 let f = state.w+winw in
5106 let r = float (winw-state.x) /. float f in
5107 let p = fwinw *. r in
5108 p-.sw/.2., sw
5110 let sw =
5111 if position +. sw > fwinw
5112 then fwinw -. position
5113 else sw
5115 state.hscrollh, position, sw
5116 end;;
5118 module Config =
5119 struct
5120 open Parser
5122 let fontpath = ref "";;
5124 let unent s =
5125 let l = String.length s in
5126 let b = Buffer.create l in
5127 unent b s 0 l;
5128 Buffer.contents b;
5131 let home =
5132 try Sys.getenv "HOME"
5133 with exn ->
5134 prerr_endline
5135 ("Can not determine home directory location: " ^
5136 Printexc.to_string exn);
5140 let config_of c attrs =
5141 let apply c k v =
5143 match k with
5144 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5145 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5146 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5147 | "preload" -> { c with preload = bool_of_string v }
5148 | "page-bias" -> { c with pagebias = int_of_string v }
5149 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5150 | "auto-scroll-step" ->
5151 { c with autoscrollstep = max 0 (int_of_string v) }
5152 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5153 | "crop-hack" -> { c with crophack = bool_of_string v }
5154 | "throttle" ->
5155 let mw =
5156 match String.lowercase v with
5157 | "true" -> Some infinity
5158 | "false" -> None
5159 | f -> Some (float_of_string f)
5161 { c with maxwait = mw}
5162 | "highlight-links" -> { c with hlinks = bool_of_string v }
5163 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5164 | "vertical-margin" ->
5165 { c with interpagespace = max 0 (int_of_string v) }
5166 | "zoom" ->
5167 let zoom = float_of_string v /. 100. in
5168 let zoom = max zoom 0.0 in
5169 { c with zoom = zoom }
5170 | "presentation" -> { c with presentation = bool_of_string v }
5171 | "rotation-angle" -> { c with angle = int_of_string v }
5172 | "width" -> { c with winw = max 20 (int_of_string v) }
5173 | "height" -> { c with winh = max 20 (int_of_string v) }
5174 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5175 | "proportional-display" -> { c with proportional = bool_of_string v }
5176 | "pixmap-cache-size" ->
5177 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5178 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5179 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5180 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5181 | "persistent-location" -> { c with jumpback = bool_of_string v }
5182 | "background-color" -> { c with bgcolor = color_of_string v }
5183 | "scrollbar-in-presentation" ->
5184 { c with scrollbarinpm = bool_of_string v }
5185 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5186 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5187 | "mupdf-store-size" ->
5188 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5189 | "checkers" -> { c with checkers = bool_of_string v }
5190 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5191 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5192 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5193 | "uri-launcher" -> { c with urilauncher = unent v }
5194 | "color-space" -> { c with colorspace = colorspace_of_string v }
5195 | "invert-colors" -> { c with invert = bool_of_string v }
5196 | "brightness" -> { c with colorscale = float_of_string v }
5197 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5198 | "ghyllscroll" ->
5199 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5200 | "columns" ->
5201 let nab = columns_of_string v in
5202 { c with columns = Some (nab, [||]) }
5203 | "birds-eye-columns" ->
5204 { c with beyecolumns = Some (max (int_of_string v) 2) }
5205 | "selection-command" -> { c with selcmd = unent v }
5206 | _ -> c
5207 with exn ->
5208 prerr_endline ("Error processing attribute (`" ^
5209 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5212 let rec fold c = function
5213 | [] -> c
5214 | (k, v) :: rest ->
5215 let c = apply c k v in
5216 fold c rest
5218 fold c attrs;
5221 let fromstring f pos n v d =
5222 try f v
5223 with exn ->
5224 dolog "Error processing attribute (%S=%S) at %d\n%s"
5225 n v pos (Printexc.to_string exn)
5230 let bookmark_of attrs =
5231 let rec fold title page rely = function
5232 | ("title", v) :: rest -> fold v page rely rest
5233 | ("page", v) :: rest -> fold title v rely rest
5234 | ("rely", v) :: rest -> fold title page v rest
5235 | _ :: rest -> fold title page rely rest
5236 | [] -> title, page, rely
5238 fold "invalid" "0" "0" attrs
5241 let doc_of attrs =
5242 let rec fold path page rely pan = function
5243 | ("path", v) :: rest -> fold v page rely pan rest
5244 | ("page", v) :: rest -> fold path v rely pan rest
5245 | ("rely", v) :: rest -> fold path page v pan rest
5246 | ("pan", v) :: rest -> fold path page rely v rest
5247 | _ :: rest -> fold path page rely pan rest
5248 | [] -> path, page, rely, pan
5250 fold "" "0" "0" "0" attrs
5253 let setconf dst src =
5254 dst.scrollbw <- src.scrollbw;
5255 dst.scrollh <- src.scrollh;
5256 dst.icase <- src.icase;
5257 dst.preload <- src.preload;
5258 dst.pagebias <- src.pagebias;
5259 dst.verbose <- src.verbose;
5260 dst.scrollstep <- src.scrollstep;
5261 dst.maxhfit <- src.maxhfit;
5262 dst.crophack <- src.crophack;
5263 dst.autoscrollstep <- src.autoscrollstep;
5264 dst.maxwait <- src.maxwait;
5265 dst.hlinks <- src.hlinks;
5266 dst.underinfo <- src.underinfo;
5267 dst.interpagespace <- src.interpagespace;
5268 dst.zoom <- src.zoom;
5269 dst.presentation <- src.presentation;
5270 dst.angle <- src.angle;
5271 dst.winw <- src.winw;
5272 dst.winh <- src.winh;
5273 dst.savebmarks <- src.savebmarks;
5274 dst.memlimit <- src.memlimit;
5275 dst.proportional <- src.proportional;
5276 dst.texcount <- src.texcount;
5277 dst.sliceheight <- src.sliceheight;
5278 dst.thumbw <- src.thumbw;
5279 dst.jumpback <- src.jumpback;
5280 dst.bgcolor <- src.bgcolor;
5281 dst.scrollbarinpm <- src.scrollbarinpm;
5282 dst.tilew <- src.tilew;
5283 dst.tileh <- src.tileh;
5284 dst.mustoresize <- src.mustoresize;
5285 dst.checkers <- src.checkers;
5286 dst.aalevel <- src.aalevel;
5287 dst.trimmargins <- src.trimmargins;
5288 dst.trimfuzz <- src.trimfuzz;
5289 dst.urilauncher <- src.urilauncher;
5290 dst.colorspace <- src.colorspace;
5291 dst.invert <- src.invert;
5292 dst.colorscale <- src.colorscale;
5293 dst.redirectstderr <- src.redirectstderr;
5294 dst.ghyllscroll <- src.ghyllscroll;
5295 dst.columns <- src.columns;
5296 dst.beyecolumns <- src.beyecolumns;
5297 dst.selcmd <- src.selcmd;
5300 let get s =
5301 let h = Hashtbl.create 10 in
5302 let dc = { defconf with angle = defconf.angle } in
5303 let rec toplevel v t spos _ =
5304 match t with
5305 | Vdata | Vcdata | Vend -> v
5306 | Vopen ("llppconfig", _, closed) ->
5307 if closed
5308 then v
5309 else { v with f = llppconfig }
5310 | Vopen _ ->
5311 error "unexpected subelement at top level" s spos
5312 | Vclose _ -> error "unexpected close at top level" s spos
5314 and llppconfig v t spos _ =
5315 match t with
5316 | Vdata | Vcdata -> v
5317 | Vend -> error "unexpected end of input in llppconfig" s spos
5318 | Vopen ("defaults", attrs, closed) ->
5319 let c = config_of dc attrs in
5320 setconf dc c;
5321 if closed
5322 then v
5323 else { v with f = skip "defaults" (fun () -> v) }
5325 | Vopen ("ui-font", attrs, closed) ->
5326 let rec getsize size = function
5327 | [] -> size
5328 | ("size", v) :: rest ->
5329 let size =
5330 fromstring int_of_string spos "size" v fstate.fontsize in
5331 getsize size rest
5332 | l -> getsize size l
5334 fstate.fontsize <- getsize fstate.fontsize attrs;
5335 if closed
5336 then v
5337 else { v with f = uifont (Buffer.create 10) }
5339 | Vopen ("doc", attrs, closed) ->
5340 let pathent, spage, srely, span = doc_of attrs in
5341 let path = unent pathent
5342 and pageno = fromstring int_of_string spos "page" spage 0
5343 and rely = fromstring float_of_string spos "rely" srely 0.0
5344 and pan = fromstring int_of_string spos "pan" span 0 in
5345 let c = config_of dc attrs in
5346 let anchor = (pageno, rely) in
5347 if closed
5348 then (Hashtbl.add h path (c, [], pan, anchor); v)
5349 else { v with f = doc path pan anchor c [] }
5351 | Vopen _ ->
5352 error "unexpected subelement in llppconfig" s spos
5354 | Vclose "llppconfig" -> { v with f = toplevel }
5355 | Vclose _ -> error "unexpected close in llppconfig" s spos
5357 and uifont b v t spos epos =
5358 match t with
5359 | Vdata | Vcdata ->
5360 Buffer.add_substring b s spos (epos - spos);
5362 | Vopen (_, _, _) ->
5363 error "unexpected subelement in ui-font" s spos
5364 | Vclose "ui-font" ->
5365 if String.length !fontpath = 0
5366 then fontpath := Buffer.contents b;
5367 { v with f = llppconfig }
5368 | Vclose _ -> error "unexpected close in ui-font" s spos
5369 | Vend -> error "unexpected end of input in ui-font" s spos
5371 and doc path pan anchor c bookmarks v t spos _ =
5372 match t with
5373 | Vdata | Vcdata -> v
5374 | Vend -> error "unexpected end of input in doc" s spos
5375 | Vopen ("bookmarks", _, closed) ->
5376 if closed
5377 then v
5378 else { v with f = pbookmarks path pan anchor c bookmarks }
5380 | Vopen (_, _, _) ->
5381 error "unexpected subelement in doc" s spos
5383 | Vclose "doc" ->
5384 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5385 { v with f = llppconfig }
5387 | Vclose _ -> error "unexpected close in doc" s spos
5389 and pbookmarks path pan anchor c bookmarks v t spos _ =
5390 match t with
5391 | Vdata | Vcdata -> v
5392 | Vend -> error "unexpected end of input in bookmarks" s spos
5393 | Vopen ("item", attrs, closed) ->
5394 let titleent, spage, srely = bookmark_of attrs in
5395 let page = fromstring int_of_string spos "page" spage 0
5396 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5397 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5398 if closed
5399 then { v with f = pbookmarks path pan anchor c bookmarks }
5400 else
5401 let f () = v in
5402 { v with f = skip "item" f }
5404 | Vopen _ ->
5405 error "unexpected subelement in bookmarks" s spos
5407 | Vclose "bookmarks" ->
5408 { v with f = doc path pan anchor c bookmarks }
5410 | Vclose _ -> error "unexpected close in bookmarks" s spos
5412 and skip tag f v t spos _ =
5413 match t with
5414 | Vdata | Vcdata -> v
5415 | Vend ->
5416 error ("unexpected end of input in skipped " ^ tag) s spos
5417 | Vopen (tag', _, closed) ->
5418 if closed
5419 then v
5420 else
5421 let f' () = { v with f = skip tag f } in
5422 { v with f = skip tag' f' }
5423 | Vclose ctag ->
5424 if tag = ctag
5425 then f ()
5426 else error ("unexpected close in skipped " ^ tag) s spos
5429 parse { f = toplevel; accu = () } s;
5430 h, dc;
5433 let do_load f ic =
5435 let len = in_channel_length ic in
5436 let s = String.create len in
5437 really_input ic s 0 len;
5438 f s;
5439 with
5440 | Parse_error (msg, s, pos) ->
5441 let subs = subs s pos in
5442 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5443 failwith ("parse error: " ^ s)
5445 | exn ->
5446 failwith ("config load error: " ^ Printexc.to_string exn)
5449 let defconfpath =
5450 let dir =
5452 let dir = Filename.concat home ".config" in
5453 if Sys.is_directory dir then dir else home
5454 with _ -> home
5456 Filename.concat dir "llpp.conf"
5459 let confpath = ref defconfpath;;
5461 let load1 f =
5462 if Sys.file_exists !confpath
5463 then
5464 match
5465 (try Some (open_in_bin !confpath)
5466 with exn ->
5467 prerr_endline
5468 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5469 Printexc.to_string exn);
5470 None
5472 with
5473 | Some ic ->
5474 begin try
5475 f (do_load get ic)
5476 with exn ->
5477 prerr_endline
5478 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5479 Printexc.to_string exn);
5480 end;
5481 close_in ic;
5483 | None -> ()
5484 else
5485 f (Hashtbl.create 0, defconf)
5488 let load () =
5489 let f (h, dc) =
5490 let pc, pb, px, pa =
5492 Hashtbl.find h (Filename.basename state.path)
5493 with Not_found -> dc, [], 0, (0, 0.0)
5495 setconf defconf dc;
5496 setconf conf pc;
5497 state.bookmarks <- pb;
5498 state.x <- px;
5499 state.scrollw <- conf.scrollbw;
5500 if conf.jumpback
5501 then state.anchor <- pa;
5502 cbput state.hists.nav pa;
5504 load1 f
5507 let add_attrs bb always dc c =
5508 let ob s a b =
5509 if always || a != b
5510 then Printf.bprintf bb "\n %s='%b'" s a
5511 and oi s a b =
5512 if always || a != b
5513 then Printf.bprintf bb "\n %s='%d'" s a
5514 and oI s a b =
5515 if always || a != b
5516 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5517 and oz s a b =
5518 if always || a <> b
5519 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5520 and oF s a b =
5521 if always || a <> b
5522 then Printf.bprintf bb "\n %s='%f'" s a
5523 and oc s a b =
5524 if always || a <> b
5525 then
5526 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5527 and oC s a b =
5528 if always || a <> b
5529 then
5530 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5531 and oR s a b =
5532 if always || a <> b
5533 then
5534 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5535 and os s a b =
5536 if always || a <> b
5537 then
5538 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5539 and og s a b =
5540 if always || a <> b
5541 then
5542 match a with
5543 | None -> ()
5544 | Some (_N, _A, _B) ->
5545 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5546 and oW s a b =
5547 if always || a <> b
5548 then
5549 let v =
5550 match a with
5551 | None -> "false"
5552 | Some f ->
5553 if f = infinity
5554 then "true"
5555 else string_of_float f
5557 Printf.bprintf bb "\n %s='%s'" s v
5558 and oco s a b =
5559 if always || a <> b
5560 then
5561 match a with
5562 | Some ((n, a, b), _) when n > 1 ->
5563 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5564 | _ -> ()
5565 and obeco s a b =
5566 if always || a <> b
5567 then
5568 match a with
5569 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5570 | _ -> ()
5572 let w, h =
5573 if always
5574 then dc.winw, dc.winh
5575 else
5576 match state.fullscreen with
5577 | Some wh -> wh
5578 | None -> c.winw, c.winh
5580 let zoom, presentation, interpagespace, maxwait =
5581 if always
5582 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5583 else
5584 match state.mode with
5585 | Birdseye (bc, _, _, _, _) ->
5586 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5587 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5589 oi "width" w dc.winw;
5590 oi "height" h dc.winh;
5591 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5592 oi "scroll-handle-height" c.scrollh dc.scrollh;
5593 ob "case-insensitive-search" c.icase dc.icase;
5594 ob "preload" c.preload dc.preload;
5595 oi "page-bias" c.pagebias dc.pagebias;
5596 oi "scroll-step" c.scrollstep dc.scrollstep;
5597 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5598 ob "max-height-fit" c.maxhfit dc.maxhfit;
5599 ob "crop-hack" c.crophack dc.crophack;
5600 oW "throttle" maxwait dc.maxwait;
5601 ob "highlight-links" c.hlinks dc.hlinks;
5602 ob "under-cursor-info" c.underinfo dc.underinfo;
5603 oi "vertical-margin" interpagespace dc.interpagespace;
5604 oz "zoom" zoom dc.zoom;
5605 ob "presentation" presentation dc.presentation;
5606 oi "rotation-angle" c.angle dc.angle;
5607 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5608 ob "proportional-display" c.proportional dc.proportional;
5609 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5610 oi "tex-count" c.texcount dc.texcount;
5611 oi "slice-height" c.sliceheight dc.sliceheight;
5612 oi "thumbnail-width" c.thumbw dc.thumbw;
5613 ob "persistent-location" c.jumpback dc.jumpback;
5614 oc "background-color" c.bgcolor dc.bgcolor;
5615 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5616 oi "tile-width" c.tilew dc.tilew;
5617 oi "tile-height" c.tileh dc.tileh;
5618 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5619 ob "checkers" c.checkers dc.checkers;
5620 oi "aalevel" c.aalevel dc.aalevel;
5621 ob "trim-margins" c.trimmargins dc.trimmargins;
5622 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5623 os "uri-launcher" c.urilauncher dc.urilauncher;
5624 oC "color-space" c.colorspace dc.colorspace;
5625 ob "invert-colors" c.invert dc.invert;
5626 oF "brightness" c.colorscale dc.colorscale;
5627 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5628 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
5629 oco "columns" c.columns dc.columns;
5630 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
5631 os "selection-command" c.selcmd dc.selcmd;
5634 let save () =
5635 let uifontsize = fstate.fontsize in
5636 let bb = Buffer.create 32768 in
5637 let f (h, dc) =
5638 let dc = if conf.bedefault then conf else dc in
5639 Buffer.add_string bb "<llppconfig>\n";
5641 if String.length !fontpath > 0
5642 then
5643 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5644 uifontsize
5645 !fontpath
5646 else (
5647 if uifontsize <> 14
5648 then
5649 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5652 Buffer.add_string bb "<defaults ";
5653 add_attrs bb true dc dc;
5654 Buffer.add_string bb "/>\n";
5656 let adddoc path pan anchor c bookmarks =
5657 if bookmarks == [] && c = dc && anchor = emptyanchor
5658 then ()
5659 else (
5660 Printf.bprintf bb "<doc path='%s'"
5661 (enent path 0 (String.length path));
5663 if anchor <> emptyanchor
5664 then (
5665 let n, y = anchor in
5666 Printf.bprintf bb " page='%d'" n;
5667 if y > 1e-6
5668 then
5669 Printf.bprintf bb " rely='%f'" y
5673 if pan != 0
5674 then Printf.bprintf bb " pan='%d'" pan;
5676 add_attrs bb false dc c;
5678 begin match bookmarks with
5679 | [] -> Buffer.add_string bb "/>\n"
5680 | _ ->
5681 Buffer.add_string bb ">\n<bookmarks>\n";
5682 List.iter (fun (title, _level, (page, rely)) ->
5683 Printf.bprintf bb
5684 "<item title='%s' page='%d'"
5685 (enent title 0 (String.length title))
5686 page
5688 if rely > 1e-6
5689 then
5690 Printf.bprintf bb " rely='%f'" rely
5692 Buffer.add_string bb "/>\n";
5693 ) bookmarks;
5694 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5695 end;
5699 let pan, conf =
5700 match state.mode with
5701 | Birdseye (c, pan, _, _, _) ->
5702 let beyecolumns =
5703 match conf.columns with
5704 | Some ((c, _, _), _) -> Some c
5705 | None -> None
5706 and columns =
5707 match c.columns with
5708 | Some (c, _) -> Some (c, [||])
5709 | None -> None
5711 pan, { c with beyecolumns = beyecolumns; columns = columns }
5712 | _ -> state.x, conf
5714 let basename = Filename.basename state.path in
5715 adddoc basename pan (getanchor ())
5716 { conf with
5717 autoscrollstep =
5718 match state.autoscroll with
5719 | Some step -> step
5720 | None -> conf.autoscrollstep }
5721 (if conf.savebmarks then state.bookmarks else []);
5723 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5724 if basename <> path
5725 then adddoc path x y c bookmarks
5726 ) h;
5727 Buffer.add_string bb "</llppconfig>";
5729 load1 f;
5730 if Buffer.length bb > 0
5731 then
5733 let tmp = !confpath ^ ".tmp" in
5734 let oc = open_out_bin tmp in
5735 Buffer.output_buffer oc bb;
5736 close_out oc;
5737 Unix.rename tmp !confpath;
5738 with exn ->
5739 prerr_endline
5740 ("error while saving configuration: " ^ Printexc.to_string exn)
5742 end;;
5744 let () =
5745 Arg.parse
5746 (Arg.align
5747 [("-p", Arg.String (fun s -> state.password <- s) ,
5748 "<password> Set password");
5750 ("-f", Arg.String (fun s -> Config.fontpath := s),
5751 "<path> Set path to the user interface font");
5753 ("-c", Arg.String (fun s -> Config.confpath := s),
5754 "<path> Set path to the configuration file");
5756 ("-v", Arg.Unit (fun () ->
5757 Printf.printf
5758 "%s\nconfiguration path: %s\n"
5759 (version ())
5760 Config.defconfpath
5762 exit 0), " Print version and exit");
5765 (fun s -> state.path <- s)
5766 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5768 if String.length state.path = 0
5769 then (prerr_endline "file name missing"; exit 1);
5771 Config.load ();
5773 state.wsfd <- Wsi.init (object
5774 method display = display ()
5775 method reshape w h = reshape w h
5776 method mouse b d x y m = mouse b d x y m
5777 method motion x y = motion x y
5778 method pmotion x y = pmotion x y
5779 method key c m = keyboard c m
5780 end);
5782 if not (
5783 List.exists GlMisc.check_extension
5784 [ "GL_ARB_texture_rectangle"
5785 ; "GL_EXT_texture_recangle"
5786 ; "GL_NV_texture_rectangle" ]
5788 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5790 let cr, sw = Unix.pipe ()
5791 and sr, cw = Unix.pipe () in
5793 cloexec cr;
5794 cloexec sw;
5795 cloexec sr;
5796 cloexec cw;
5798 setcheckers conf.checkers;
5799 redirectstderr ();
5801 init (cr, cw) (
5802 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5803 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5804 !Config.fontpath
5806 state.sr <- sr;
5807 state.sw <- sw;
5808 state.text <- "Opening " ^ state.path;
5809 setaalevel conf.aalevel;
5810 writeopen state.path state.password;
5811 state.uioh <- uioh;
5812 setfontsize fstate.fontsize;
5813 doreshape conf.winw conf.winh;
5815 let rec loop deadline =
5816 let r =
5817 match state.errfd with
5818 | None -> [state.sr; state.wsfd]
5819 | Some fd -> [state.sr; state.wsfd; fd]
5821 if state.redisplay
5822 then (
5823 state.redisplay <- false;
5824 display ();
5826 let timeout =
5827 let now = now () in
5828 if deadline > now
5829 then (
5830 if deadline = infinity
5831 then ~-.1.0
5832 else max 0.0 (deadline -. now)
5834 else 0.0
5836 let r, _, _ =
5837 try Unix.select r [] [] timeout
5838 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [] ,[]
5840 begin match r with
5841 | [] ->
5842 state.ghyll None;
5843 let newdeadline =
5844 match state.autoscroll with
5845 | Some step when step != 0 ->
5846 let y = state.y + step in
5847 let y =
5848 if y < 0
5849 then state.maxy
5850 else if y >= state.maxy then 0 else y
5852 gotoy y;
5853 if state.mode = View
5854 then state.text <- "";
5855 deadline +. 0.01
5856 | _ ->
5857 if state.ghyll == noghyll then infinity else deadline +. 0.01
5859 loop newdeadline
5861 | l ->
5862 let rec checkfds = function
5863 | [] -> ()
5864 | fd :: rest when fd = state.sr ->
5865 let cmd = readcmd state.sr in
5866 act cmd;
5867 checkfds rest
5869 | fd :: rest when fd = state.wsfd ->
5870 Wsi.readresp fd;
5871 checkfds rest
5873 | fd :: rest ->
5874 let s = String.create 80 in
5875 let n = Unix.read fd s 0 80 in
5876 if conf.redirectstderr
5877 then (
5878 Buffer.add_substring state.errmsgs s 0 n;
5879 state.newerrmsgs <- true;
5880 state.redisplay <- true;
5882 else (
5883 prerr_string (String.sub s 0 n);
5884 flush stderr;
5886 checkfds rest
5888 checkfds l;
5889 let newdeadline =
5890 let deadline1 =
5891 if deadline = infinity
5892 then now () +. 0.01
5893 else deadline
5895 match state.autoscroll with
5896 | Some step when step != 0 -> deadline1
5897 | _ -> if state.ghyll == noghyll then infinity else deadline1
5899 loop newdeadline
5900 end;
5903 loop infinity;
5904 with Wsi.Quit ->
5905 wcmd "quit" [];
5906 Config.save ();
5907 exit 0;