Ignore gravity
[llpp.git] / main.ml
bloba752ca4115903a05ddd7d94414012fff306beca8
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 external init : Unix.file_descr -> initparams -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external getpdimrect : int -> float array = "ml_getpdimrect";;
10 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
11 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
12 external clearmark : opaque -> unit = "ml_clearmark";;
13 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
14 external getmaxw : unit -> float = "ml_getmaxw";;
15 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
16 external measurestr : int -> string -> float = "ml_measure_string";;
17 external postprocess : opaque -> int -> int -> int -> (int * string * int)
18 -> int = "ml_postprocess";;
19 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
20 external setaalevel : int -> unit = "ml_setaalevel";;
21 external realloctexts : int -> bool = "ml_realloctexts";;
22 external findlink : opaque -> linkdir -> link = "ml_findlink";;
23 external getlink : opaque -> int -> under = "ml_getlink";;
24 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
25 external getlinkcount : opaque -> int = "ml_getlinkcount";;
26 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
27 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
28 external freepbo : opaque -> unit = "ml_freepbo";;
29 external unmappbo : opaque -> unit = "ml_unmappbo";;
30 external bousable : unit -> bool = "ml_bo_usable";;
31 external unproject : opaque -> int -> int
32 -> (int * int) option = "ml_unproject";;
33 external project : opaque -> int -> int -> float -> float
34 -> (float * float) = "ml_project";;
35 external drawtile : tileparams -> opaque
36 -> unit = "ml_drawtile";;
37 external rectofblock : opaque -> int -> int
38 -> float array option = "ml_rectofblock";;
39 external begintiles : unit -> unit = "ml_begintiles";;
40 external endtiles : unit -> unit = "ml_endtiles";;
41 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
42 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
43 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
44 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
45 external savedoc : string -> unit = "ml_savedoc";;
46 external getannotcontents : opaque -> slinkindex
47 -> string = "ml_getannotcontents";;
48 external drawprect : opaque -> int -> int -> float array
49 -> unit = "ml_drawprect";;
50 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
51 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
52 external uritolocation : string
53 -> (pageno * float * float) = "ml_uritolocation";;
54 external isexternallink : string -> bool = "ml_isexternallink";;
56 (* copysel _will_ close the supplied descriptor *)
57 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
59 let selfexec = ref E.s;;
60 let opengl_has_pbo = ref false;;
62 let drawstring size x y s =
63 Gl.enable `blend;
64 Gl.enable `texture_2d;
65 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
66 ignore (drawstr size x y s);
67 Gl.disable `blend;
68 Gl.disable `texture_2d;
71 let drawstring1 size x y s =
72 drawstr size x y s;
75 let drawstring2 size x y fmt =
76 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
79 module UniSyms = struct
80 let ellipsis = "\xe2\x80\xa6";;
81 let radical = "\xe2\x88\x9a";;
82 let lguillemet = "\xc2\xab";;
83 let rguillemet = "\xc2\xbb";;
84 end;;
86 let _debugl l =
87 dolog {|l %d dim=%d {
88 WxH %dx%d
89 vWxH %dx%d
90 pagex,y %d,%d
91 dispx,y %d,%d
92 column %d\n
93 }|}
94 l.pageno l.pagedimno
95 l.pagew l.pageh
96 l.pagevw l.pagevh
97 l.pagex l.pagey
98 l.pagedispx l.pagedispy
99 l.pagecol
102 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
103 dolog {|rect {
104 x0,y0=(% f, % f)
105 x1,y1=(% f, % f)
106 x2,y2=(% f, % f)
107 x3,y3=(% f, % f)
108 }|} x0 y0 x1 y1 x2 y2 x3 y3;
111 let isbirdseye = function
112 | Birdseye _ -> true
113 | Textentry _ | View | LinkNav _ -> false
116 let istextentry = function
117 | Textentry _ -> true
118 | Birdseye _ | View | LinkNav _ -> false
121 let wtmode = ref false;;
122 let cxack = ref false;;
124 let pgscale h = truncate (float h *. conf.pgscale);;
126 let hscrollh () =
127 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
128 && (state.w > state.winw))
129 then conf.scrollbw
130 else 0
133 let vscrollw () =
134 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
135 && (state.maxy > state.winh))
136 then conf.scrollbw
137 else 0
140 let vscrollhit x =
141 if conf.leftscroll
142 then x < vscrollw ()
143 else x > state.winw - vscrollw ()
146 let setfontsize n =
147 fstate.fontsize <- n;
148 fstate.wwidth <- measurestr fstate.fontsize "w";
149 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
152 let vlog fmt =
153 if conf.verbose
154 then dolog fmt
155 else Printf.kprintf ignore fmt
158 let launchpath () =
159 if emptystr conf.pathlauncher
160 then dolog "%s" state.path
161 else (
162 let command = Str.global_replace percentsre state.path conf.pathlauncher in
163 match spawn command [] with
164 | _pid -> ()
165 | exception exn ->
166 dolog "failed to execute `%s': %s" command @@ exntos exn
170 module G =
171 struct
172 let postRedisplay who =
173 vlog "redisplay for [%S]" who;
174 state.redisplay <- true;
176 end;;
178 let getopaque pageno =
179 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
180 with Not_found -> None
183 let pagetranslatepoint l x y =
184 let dy = y - l.pagedispy in
185 let y = dy + l.pagey in
186 let dx = x - l.pagedispx in
187 let x = dx + l.pagex in
188 (x, y);
191 let onppundermouse g x y d =
192 let rec f = function
193 | l :: rest ->
194 begin match getopaque l.pageno with
195 | Some opaque ->
196 let x0 = l.pagedispx in
197 let x1 = x0 + l.pagevw in
198 let y0 = l.pagedispy in
199 let y1 = y0 + l.pagevh in
200 if y >= y0 && y <= y1 && x >= x0 && x <= x1
201 then
202 let px, py = pagetranslatepoint l x y in
203 match g opaque l px py with
204 | Some res -> res
205 | None -> f rest
206 else f rest
207 | _ ->
208 f rest
210 | [] -> d
212 f state.layout
215 let getunder x y =
216 let g opaque l px py =
217 if state.bzoom
218 then (
219 match rectofblock opaque px py with
220 | Some [|x0;x1;y0;y1|] ->
221 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
222 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
223 state.rects <- [l.pageno, color, rect];
224 G.postRedisplay "getunder";
225 | _ -> ()
227 let under = whatsunder opaque px py in
228 if under = Unone then None else Some under
230 onppundermouse g x y Unone
233 let unproject x y =
234 let g opaque l x y =
235 match unproject opaque x y with
236 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
237 | None -> None
239 onppundermouse g x y None;
242 let showtext c s =
243 state.text <- Printf.sprintf "%c%s" c s;
244 G.postRedisplay "showtext";
247 let impmsg fmt =
248 Format.ksprintf (fun s -> showtext '!' s) fmt;
251 let pipef ?(closew=true) cap f cmd =
252 match Unix.pipe () with
253 | exception exn -> dolog "%s cannot create pipe: %S" cap @@ exntos exn
254 | (r, w) ->
255 begin match spawn cmd [r, 0; w, -1] with
256 | exception exn -> dolog "%s: cannot execute %S: %s" cap cmd @@ exntos exn
257 | _pid -> f w
258 end;
259 Ne.clo r (dolog "%s failed to close r: %s" cap);
260 if closew then Ne.clo w (dolog "%s failed to close w: %s" cap);
263 let pipesel opaque cmd =
264 if hassel opaque
265 then pipef ~closew:false "pipesel"
266 (fun w ->
267 copysel w opaque;
268 G.postRedisplay "pipesel"
269 ) cmd
272 let paxunder x y =
273 let g opaque l px py =
274 if markunder opaque px py conf.paxmark
275 then (
276 Some (fun () ->
277 match getopaque l.pageno with
278 | None -> ()
279 | Some opaque -> pipesel opaque conf.paxcmd
282 else None
284 G.postRedisplay "paxunder";
285 if conf.paxmark = Mark_page
286 then
287 List.iter (fun l ->
288 match getopaque l.pageno with
289 | None -> ()
290 | Some opaque -> clearmark opaque) state.layout;
291 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
294 let selstring s =
295 pipef
296 "selstring" (fun w ->
298 let l = String.length s in
299 let bytes = Bytes.unsafe_of_string s in
300 let n = tempfailureretry (Unix.write w bytes 0) l in
301 if n != l
302 then impmsg "failed to write %d characters to sel pipe, wrote %d" l n;
303 with exn -> impmsg "failed to write to sel pipe: %s" @@ exntos exn
304 ) conf.selcmd
307 let undertext = function
308 | Unone -> "none"
309 | Ulinkuri s -> s
310 | Utext s -> "font: " ^ s
311 | Uannotation (opaque, slinkindex) ->
312 "annotation: " ^ getannotcontents opaque slinkindex
315 let updateunder x y =
316 match getunder x y with
317 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
318 | Ulinkuri uri ->
319 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
320 Wsi.setcursor Wsi.CURSOR_INFO
321 | Utext s ->
322 if conf.underinfo then showtext 'f' ("ont: " ^ s);
323 Wsi.setcursor Wsi.CURSOR_TEXT
324 | Uannotation _ ->
325 if conf.underinfo then showtext 'a' "nnotation";
326 Wsi.setcursor Wsi.CURSOR_INFO
329 let showlinktype under =
330 if conf.underinfo && under != Unone
331 then showtext ' ' @@ undertext under
334 let [@warning "-4"] intentry_with_suffix text key =
335 let text =
336 match key with
337 | Keys.Ascii ('0'..'9' as c) -> addchar text c
338 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
339 addchar text @@ asciilower c
340 | _ ->
341 state.text <- Printf.sprintf "invalid key";
342 text
344 TEcont text
347 let wcmd fmt =
348 let b = Buffer.create 16 in
349 Printf.kbprintf
350 (fun b ->
351 let b = Buffer.to_bytes b in
352 wcmd state.ss b @@ Bytes.length b
353 ) b fmt
356 let nogeomcmds cmds =
357 match cmds with
358 | s, [] -> emptystr s
359 | _ -> false
362 let layoutN ((columns, coverA, coverB), b) x y sw sh =
363 let rec fold accu n =
364 if n = Array.length b
365 then accu
366 else
367 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
368 if (vy - y) > sh &&
369 (n = coverA - 1
370 || n = state.pagecount - coverB
371 || (n - coverA) mod columns = columns - 1)
372 then accu
373 else
374 let accu =
375 if vy + h > y
376 then
377 let pagey = max 0 (y - vy) in
378 let pagedispy = if pagey > 0 then 0 else vy - y in
379 let pagedispx, pagex =
380 let pdx =
381 if n = coverA - 1 || n = state.pagecount - coverB
382 then x + (sw - w) / 2
383 else dx + xoff + x
385 if pdx < 0
386 then 0, -pdx
387 else pdx, 0
389 let pagevw =
390 let vw = sw - pagedispx in
391 let pw = w - pagex in
392 min vw pw
394 let pagevh = min (h - pagey) (sh - pagedispy) in
395 if pagevw > 0 && pagevh > 0
396 then
397 let e =
398 { pageno = n
399 ; pagedimno = pdimno
400 ; pagew = w
401 ; pageh = h
402 ; pagex = pagex
403 ; pagey = pagey
404 ; pagevw = pagevw
405 ; pagevh = pagevh
406 ; pagedispx = pagedispx
407 ; pagedispy = pagedispy
408 ; pagecol = 0
411 e :: accu
412 else
413 accu
414 else
415 accu
417 fold accu (n+1)
419 if Array.length b = 0
420 then []
421 else List.rev (fold [] (page_of_y y))
424 let layoutS (columns, b) x y sw sh =
425 let rec fold accu n =
426 if n = Array.length b
427 then accu
428 else
429 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
430 if (vy - y) > sh
431 then accu
432 else
433 let accu =
434 if vy + pageh > y
435 then
436 let x = xoff + x in
437 let pagey = max 0 (y - vy) in
438 let pagedispy = if pagey > 0 then 0 else vy - y in
439 let pagedispx, pagex =
440 if px = 0
441 then (
442 if x < 0
443 then 0, -x
444 else x, 0
446 else (
447 let px = px - x in
448 if px < 0
449 then -px, 0
450 else 0, px
453 let pagecolw = pagew/columns in
454 let pagedispx =
455 if pagecolw < sw
456 then pagedispx + ((sw - pagecolw) / 2)
457 else pagedispx
459 let pagevw =
460 let vw = sw - pagedispx in
461 let pw = pagew - pagex in
462 min vw pw
464 let pagevw = min pagevw pagecolw in
465 let pagevh = min (pageh - pagey) (sh - pagedispy) in
466 if pagevw > 0 && pagevh > 0
467 then
468 let e =
469 { pageno = n/columns
470 ; pagedimno = pdimno
471 ; pagew = pagew
472 ; pageh = pageh
473 ; pagex = pagex
474 ; pagey = pagey
475 ; pagevw = pagevw
476 ; pagevh = pagevh
477 ; pagedispx = pagedispx
478 ; pagedispy = pagedispy
479 ; pagecol = n mod columns
482 e :: accu
483 else
484 accu
485 else
486 accu
488 fold accu (n+1)
490 List.rev (fold [] 0)
493 let layout x y sw sh =
494 if nogeomcmds state.geomcmds
495 then
496 match conf.columns with
497 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
498 | Cmulti c -> layoutN c x y sw sh
499 | Csplit s -> layoutS s x y sw sh
500 else []
503 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
505 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
507 let itertiles l f =
508 let tilex = l.pagex mod conf.tilew in
509 let tiley = l.pagey mod conf.tileh in
511 let col = l.pagex / conf.tilew in
512 let row = l.pagey / conf.tileh in
514 let rec rowloop row y0 dispy h =
515 if h = 0
516 then ()
517 else (
518 let dh = conf.tileh - y0 in
519 let dh = min h dh in
520 let rec colloop col x0 dispx w =
521 if w = 0
522 then ()
523 else (
524 let dw = conf.tilew - x0 in
525 let dw = min w dw in
526 f col row dispx dispy x0 y0 dw dh;
527 colloop (col+1) 0 (dispx+dw) (w-dw)
530 colloop col tilex l.pagedispx l.pagevw;
531 rowloop (row+1) 0 (dispy+dh) (h-dh)
534 if l.pagevw > 0 && l.pagevh > 0
535 then rowloop row tiley l.pagedispy l.pagevh;
538 let gettileopaque l col row =
539 let key =
540 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
542 try Some (Hashtbl.find state.tilemap key)
543 with Not_found -> None
546 let puttileopaque l col row gen colorspace angle opaque size elapsed =
547 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
548 Hashtbl.add state.tilemap key (opaque, size, elapsed)
551 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
552 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
553 GlArray.vertex `two state.vraw;
554 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
557 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
559 let filledrect x0 y0 x1 y1 =
560 GlArray.disable `texture_coord;
561 filledrect1 x0 y0 x1 y1;
562 GlArray.enable `texture_coord;
565 let linerect x0 y0 x1 y1 =
566 GlArray.disable `texture_coord;
567 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
568 GlArray.vertex `two state.vraw;
569 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
570 GlArray.enable `texture_coord;
573 let drawtiles l color =
574 GlDraw.color color;
575 begintiles ();
576 let f col row x y tilex tiley w h =
577 match gettileopaque l col row with
578 | Some (opaque, _, t) ->
579 let params = x, y, w, h, tilex, tiley in
580 if conf.invert
581 then GlTex.env (`mode `blend);
582 drawtile params opaque;
583 if conf.invert
584 then GlTex.env (`mode `modulate);
585 if conf.debug
586 then (
587 endtiles ();
588 let s = Printf.sprintf
589 "%d[%d,%d] %f sec"
590 l.pageno col row t
592 let w = measurestr fstate.fontsize s in
593 GlDraw.color (0.0, 0.0, 0.0);
594 filledrect (float (x-2))
595 (float (y-2))
596 (float (x+2) +. w)
597 (float (y + fstate.fontsize + 2));
598 GlDraw.color color;
599 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
600 begintiles ();
603 | None ->
604 endtiles ();
605 let w =
606 let lw = state.winw - x in
607 min lw w
608 and h =
609 let lh = state.winh - y in
610 min lh h
612 if conf.invert
613 then GlTex.env (`mode `blend);
614 begin match state.checkerstexid with
615 | Some id ->
616 Gl.enable `texture_2d;
617 GlTex.bind_texture ~target:`texture_2d id;
618 let x0 = float x
619 and y0 = float y
620 and x1 = float (x+w)
621 and y1 = float (y+h) in
623 let tw = float w /. 16.0
624 and th = float h /. 16.0 in
625 let tx0 = float tilex /. 16.0
626 and ty0 = float tiley /. 16.0 in
627 let tx1 = tx0 +. tw
628 and ty1 = ty0 +. th in
629 Raw.sets_float state.vraw ~pos:0
630 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
631 Raw.sets_float state.traw ~pos:0
632 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
633 GlArray.vertex `two state.vraw;
634 GlArray.tex_coord `two state.traw;
635 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
636 Gl.disable `texture_2d;
638 | None ->
639 GlDraw.color (1.0, 1.0, 1.0);
640 filledrect (float x) (float y) (float (x+w)) (float (y+h));
641 end;
642 if conf.invert
643 then GlTex.env (`mode `modulate);
644 if w > 128 && h > fstate.fontsize + 10
645 then (
646 let c = if conf.invert then 1.0 else 0.0 in
647 GlDraw.color (c, c, c);
648 let c, r =
649 if conf.verbose
650 then (col*conf.tilew, row*conf.tileh)
651 else col, row
653 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
655 GlDraw.color color;
656 begintiles ();
658 itertiles l f;
659 endtiles ();
662 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
664 let tilevisible1 l x y =
665 let ax0 = l.pagex
666 and ax1 = l.pagex + l.pagevw
667 and ay0 = l.pagey
668 and ay1 = l.pagey + l.pagevh in
670 let bx0 = x
671 and by0 = y in
672 let bx1 = min (bx0 + conf.tilew) l.pagew
673 and by1 = min (by0 + conf.tileh) l.pageh in
675 let rx0 = max ax0 bx0
676 and ry0 = max ay0 by0
677 and rx1 = min ax1 bx1
678 and ry1 = min ay1 by1 in
680 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
681 nonemptyintersection
684 let tilevisible layout n x y =
685 let rec findpageinlayout m = function
686 | l :: rest when l.pageno = n ->
687 tilevisible1 l x y || (
688 match conf.columns with
689 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
690 | Csplit _ | Csingle _ | Cmulti _ -> false
692 | _ :: rest -> findpageinlayout 0 rest
693 | [] -> false
695 findpageinlayout 0 layout;
698 let tileready l x y =
699 tilevisible1 l x y &&
700 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
703 let tilepage n p layout =
704 let rec loop = function
705 | l :: rest ->
706 if l.pageno = n
707 then
708 let f col row _ _ _ _ _ _ =
709 if state.currently = Idle
710 then
711 match gettileopaque l col row with
712 | Some _ -> ()
713 | None ->
714 let x = col*conf.tilew
715 and y = row*conf.tileh in
716 let w =
717 let w = l.pagew - x in
718 min w conf.tilew
720 let h =
721 let h = l.pageh - y in
722 min h conf.tileh
724 let pbo =
725 if conf.usepbo
726 then getpbo w h conf.colorspace
727 else ~< "0"
729 wcmd "tile %s %d %d %d %d %s"
730 (~> p) x y w h (~> pbo);
731 state.currently <-
732 Tiling (
733 l, p, conf.colorspace, conf.angle,
734 state.gen, col, row, conf.tilew, conf.tileh
737 itertiles l f;
738 else
739 loop rest
741 | [] -> ()
743 if nogeomcmds state.geomcmds
744 then loop layout;
747 let preloadlayout x y sw sh =
748 let y = if y < sh then 0 else y - sh in
749 let x = min 0 (x + sw) in
750 let h = sh*3 in
751 let w = sw*3 in
752 layout x y w h;
755 let load pages =
756 let rec loop pages =
757 if state.currently != Idle
758 then ()
759 else
760 match pages with
761 | l :: rest ->
762 begin match getopaque l.pageno with
763 | None ->
764 wcmd "page %d %d" l.pageno l.pagedimno;
765 state.currently <- Loading (l, state.gen);
766 | Some opaque ->
767 tilepage l.pageno opaque pages;
768 loop rest
769 end;
770 | _ -> ()
772 if nogeomcmds state.geomcmds
773 then loop pages
776 let preload pages =
777 load pages;
778 if conf.preload && state.currently = Idle
779 then load (preloadlayout state.x state.y state.winw state.winh);
782 let layoutready layout =
783 let rec fold all ls =
784 all && match ls with
785 | l :: rest ->
786 let seen = ref false in
787 let allvisible = ref true in
788 let foo col row _ _ _ _ _ _ =
789 seen := true;
790 allvisible := !allvisible &&
791 begin match gettileopaque l col row with
792 | Some _ -> true
793 | None -> false
796 itertiles l foo;
797 fold (!seen && !allvisible) rest
798 | [] -> true
800 let alltilesvisible = fold true layout in
801 alltilesvisible;
804 let gotoxy x y =
805 let y = bound y 0 state.maxy in
806 let y, layout, proceed =
807 match conf.maxwait with
808 | Some time when state.ghyll == noghyll ->
809 begin match state.throttle with
810 | None ->
811 let layout = layout x y state.winw state.winh in
812 let ready = layoutready layout in
813 if not ready
814 then (
815 load layout;
816 state.throttle <- Some (layout, y, now ());
818 else G.postRedisplay "gotoxy showall (None)";
819 y, layout, ready
820 | Some (_, _, started) ->
821 let dt = now () -. started in
822 if dt > time
823 then (
824 state.throttle <- None;
825 let layout = layout x y state.winw state.winh in
826 load layout;
827 G.postRedisplay "maxwait";
828 y, layout, true
830 else -1, [], false
833 | _ ->
834 let layout = layout x y state.winw state.winh in
835 if not !wtmode || layoutready layout
836 then G.postRedisplay "gotoxy ready";
837 y, layout, true
839 if proceed
840 then (
841 state.x <- x;
842 state.y <- y;
843 state.layout <- layout;
844 begin match state.mode with
845 | LinkNav ln ->
846 begin match ln with
847 | Ltexact (pageno, linkno) ->
848 let rec loop = function
849 | [] ->
850 state.lnava <- Some (pageno, linkno);
851 state.mode <- LinkNav (Ltgendir 0)
852 | l :: _ when l.pageno = pageno ->
853 begin match getopaque pageno with
854 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
855 | Some opaque ->
856 let x0, y0, x1, y1 = getlinkrect opaque linkno in
857 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
858 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
859 then state.mode <- LinkNav (Ltgendir 0)
861 | _ :: rest -> loop rest
863 loop layout
864 | Ltnotready _ | Ltgendir _ -> ()
866 | Birdseye _ | Textentry _ | View -> ()
867 end;
868 begin match state.mode with
869 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
870 if not (pagevisible layout pageno)
871 then (
872 match state.layout with
873 | [] -> ()
874 | l :: _ ->
875 state.mode <- Birdseye (
876 conf, leftx, l.pageno, hooverpageno, anchor
879 | LinkNav lt ->
880 begin match lt with
881 | Ltnotready (_, dir)
882 | Ltgendir dir ->
883 let linknav =
884 let rec loop = function
885 | [] -> lt
886 | l :: rest ->
887 match getopaque l.pageno with
888 | None -> Ltnotready (l.pageno, dir)
889 | Some opaque ->
890 let link =
891 let ld =
892 if dir = 0
893 then LDfirstvisible (l.pagex, l.pagey, dir)
894 else (
895 if dir > 0 then LDfirst else LDlast
898 findlink opaque ld
900 match link with
901 | Lnotfound -> loop rest
902 | Lfound n ->
903 showlinktype (getlink opaque n);
904 Ltexact (l.pageno, n)
906 loop state.layout
908 state.mode <- LinkNav linknav
909 | Ltexact _ -> ()
911 | Textentry _ | View -> ()
912 end;
913 preload layout;
915 state.ghyll <- noghyll;
916 if conf.updatecurs
917 then (
918 let mx, my = state.mpos in
919 updateunder mx my;
923 let conttiling pageno opaque =
924 tilepage pageno opaque
925 (if conf.preload
926 then preloadlayout state.x state.y state.winw state.winh
927 else state.layout)
930 let gotoxy_and_clear_text x y =
931 if not conf.verbose then state.text <- E.s;
932 gotoxy x y;
935 let getanchory (n, top, dtop) =
936 let y, h = getpageyh n in
937 if conf.presentation
938 then
939 let ips = calcips h in
940 y + truncate (top*.float h -. dtop*.float ips) + ips;
941 else
942 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
945 let gotoanchor anchor =
946 gotoxy state.x (getanchory anchor);
949 let addnav () =
950 cbput state.hists.nav (getanchor ());
953 let getnav dir =
954 let anchor = cbgetc state.hists.nav dir in
955 getanchory anchor;
958 let gotoghyll1 single y =
959 let scroll f n a b =
960 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
961 let snake f a b =
962 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
963 if f < a
964 then s (float f /. float a)
965 else (
966 if f > b
967 then 1.0 -. s ((float (f-b) /. float (n-b)))
968 else 1.0
971 snake f a b
972 and summa n a b =
973 let ins = float a *. 0.5
974 and outs = float (n-b) *. 0.5 in
975 let ones = b - a in
976 ins +. outs +. float ones
978 let rec set nab y sy =
979 let (_N, _A, _B), y =
980 if single
981 then
982 let scl = if y > sy then 2 else -2 in
983 let _N, _, _ = nab in
984 (_N,0,_N), y+conf.scrollstep*scl
985 else nab,y in
986 let sum = summa _N _A _B in
987 let dy = float (y - sy) in
988 state.ghyll <- (
989 let rec gf n y1 o =
990 if n >= _N
991 then state.ghyll <- noghyll
992 else
993 let go n =
994 let s = scroll n _N _A _B in
995 let y1 = y1 +. ((s *. dy) /. sum) in
996 gotoxy_and_clear_text state.x (truncate y1);
997 state.ghyll <- gf (n+1) y1;
999 match o with
1000 | None -> go n
1001 | Some y' when single -> set nab y' state.y
1002 | Some y' -> set (_N/2, 1, 1) y' state.y
1004 gf 0 (float state.y)
1007 match conf.ghyllscroll with
1008 | Some nab when not conf.presentation ->
1009 if state.ghyll == noghyll
1010 then set nab y state.y
1011 else state.ghyll (Some y)
1012 | _ ->
1013 gotoxy_and_clear_text state.x y
1016 let gotoghyll = gotoghyll1 false;;
1018 let gotopage n top =
1019 let y, h = getpageyh n in
1020 let y = y + (truncate (top *. float h)) in
1021 gotoghyll y
1024 let gotopage1 n top =
1025 let y = getpagey n in
1026 let y = y + top in
1027 gotoghyll y
1030 let invalidate ?(redisplay=false) s f =
1031 state.redisplay <- redisplay;
1032 state.layout <- [];
1033 state.pdims <- [];
1034 state.rects <- [];
1035 state.rects1 <- [];
1036 match state.geomcmds with
1037 | ps, [] when emptystr ps ->
1038 f ();
1039 state.geomcmds <- s, [];
1041 | ps, [] ->
1042 state.geomcmds <- ps, [s, f];
1044 | ps, (s', _) :: rest when s' = s ->
1045 state.geomcmds <- ps, ((s, f) :: rest);
1047 | ps, cmds ->
1048 state.geomcmds <- ps, ((s, f) :: cmds);
1051 let flushpages () =
1052 Hashtbl.iter (fun _ opaque ->
1053 wcmd "freepage %s" (~> opaque);
1054 ) state.pagemap;
1055 Hashtbl.clear state.pagemap;
1058 let flushtiles () =
1059 if not (Queue.is_empty state.tilelru)
1060 then (
1061 Queue.iter (fun (k, p, s) ->
1062 wcmd "freetile %s" (~> p);
1063 state.memused <- state.memused - s;
1064 Hashtbl.remove state.tilemap k;
1065 ) state.tilelru;
1066 state.uioh#infochanged Memused;
1067 Queue.clear state.tilelru;
1069 load state.layout;
1072 let stateh h =
1073 let h = truncate (float h*.conf.zoom) in
1074 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1075 h - d
1078 let opendoc path password =
1079 state.path <- path;
1080 state.password <- password;
1081 state.gen <- state.gen + 1;
1082 state.docinfo <- [];
1083 state.outlines <- [||];
1085 flushpages ();
1086 setaalevel conf.aalevel;
1087 let titlepath =
1088 if emptystr state.origin
1089 then path
1090 else state.origin
1092 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1093 wcmd "open %d %d %d %s\000%s\000%s\000"
1094 (btod !wtmode) (btod !cxack) (btod conf.usedoccss)
1095 path password conf.css;
1096 invalidate "reqlayout"
1097 (fun () ->
1098 wcmd "reqlayout %d %d %d %s\000"
1099 conf.angle (FMTE.to_int conf.fitmodel)
1100 (stateh state.winh) state.nameddest
1102 state.help <-
1103 let sl = keystostrlist conf in
1104 let rec loop accu =
1105 function | [] -> accu
1106 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1107 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1110 let reload () =
1111 state.anchor <- getanchor ();
1112 opendoc state.path state.password;
1115 let scalecolor c =
1116 let c = c *. conf.colorscale in
1117 (c, c, c);
1120 let scalecolor2 (r, g, b) =
1121 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1124 let docolumns columns =
1125 match columns with
1126 | Csingle _ ->
1127 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1128 let rec loop pageno pdimno pdim y ph pdims =
1129 if pageno = state.pagecount
1130 then ()
1131 else
1132 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1133 match pdims with
1134 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1135 pdimno+1, pdim, rest
1136 | _ ->
1137 pdimno, pdim, pdims
1139 let x = max 0 (((state.winw - w) / 2) - xoff) in
1140 let y =
1141 y + (if conf.presentation
1142 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1143 else (if pageno = 0 then 0 else conf.interpagespace)
1146 a.(pageno) <- (pdimno, x, y, pdim);
1147 loop (pageno+1) pdimno pdim (y + h) h pdims
1149 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1150 conf.columns <- Csingle a;
1152 | Cmulti ((columns, coverA, coverB), _) ->
1153 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1154 let rec loop pageno pdimno pdim x y rowh pdims =
1155 let rec fixrow m =
1156 if m = pageno then () else
1157 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1158 if h < rowh
1159 then (
1160 let y = y + (rowh - h) / 2 in
1161 a.(m) <- (pdimno, x, y, pdim);
1163 fixrow (m+1)
1165 if pageno = state.pagecount
1166 then fixrow (((pageno - 1) / columns) * columns)
1167 else
1168 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1169 match pdims with
1170 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1171 pdimno+1, pdim, rest
1172 | _ ->
1173 pdimno, pdim, pdims
1175 let x, y, rowh' =
1176 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1177 then (
1178 let x = (state.winw - w) / 2 in
1179 let ips =
1180 if conf.presentation then calcips h else conf.interpagespace in
1181 x, y + ips + rowh, h
1183 else (
1184 if (pageno - coverA) mod columns = 0
1185 then (
1186 let x = max 0 (state.winw - state.w) / 2 in
1187 let y =
1188 if conf.presentation
1189 then
1190 let ips = calcips h in
1191 y + (if pageno = 0 then 0 else calcips rowh + ips)
1192 else
1193 y + (if pageno = 0 then 0 else conf.interpagespace)
1195 x, y + rowh, h
1197 else x, y, max rowh h
1200 let y =
1201 if pageno > 1 && (pageno - coverA) mod columns = 0
1202 then (
1203 let y =
1204 if pageno = columns && conf.presentation
1205 then (
1206 let ips = calcips rowh in
1207 for i = 0 to pred columns
1209 let (pdimno, x, y, pdim) = a.(i) in
1210 a.(i) <- (pdimno, x, y+ips, pdim)
1211 done;
1212 y+ips;
1214 else y
1216 fixrow (pageno - columns);
1219 else y
1221 a.(pageno) <- (pdimno, x, y, pdim);
1222 let x = x + w + xoff*2 + conf.interpagespace in
1223 loop (pageno+1) pdimno pdim x y rowh' pdims
1225 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1226 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1228 | Csplit (c, _) ->
1229 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1230 let rec loop pageno pdimno pdim y pdims =
1231 if pageno = state.pagecount
1232 then ()
1233 else
1234 let pdimno, ((_, w, h, _) as pdim), pdims =
1235 match pdims with
1236 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1237 pdimno+1, pdim, rest
1238 | _ ->
1239 pdimno, pdim, pdims
1241 let cw = w / c in
1242 let rec loop1 n x y =
1243 if n = c then y else (
1244 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1245 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1248 let y = loop1 0 0 y in
1249 loop (pageno+1) pdimno pdim y pdims
1251 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1252 conf.columns <- Csplit (c, a);
1255 let represent () =
1256 docolumns conf.columns;
1257 state.maxy <- calcheight ();
1258 if state.reprf == noreprf
1259 then (
1260 match state.mode with
1261 | Birdseye (_, _, pageno, _, _) ->
1262 let y, h = getpageyh pageno in
1263 let top = (state.winh - h) / 2 in
1264 gotoxy state.x (max 0 (y - top))
1265 | Textentry _ | View | LinkNav _ ->
1266 let y = getanchory state.anchor in
1267 let y = min y (state.maxy - state.winh) in
1268 gotoxy state.x y;
1270 else (
1271 state.reprf ();
1272 state.reprf <- noreprf;
1276 let reshape ?(firsttime=false) w h =
1277 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1278 if not firsttime && nogeomcmds state.geomcmds
1279 then state.anchor <- getanchor ();
1281 state.winw <- w;
1282 let w = truncate (float w *. conf.zoom) in
1283 let w = max w 2 in
1284 state.winh <- h;
1285 setfontsize fstate.fontsize;
1286 GlMat.mode `modelview;
1287 GlMat.load_identity ();
1289 GlMat.mode `projection;
1290 GlMat.load_identity ();
1291 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1292 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1293 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1295 let relx =
1296 if conf.zoom <= 1.0
1297 then 0.0
1298 else float state.x /. float state.w
1300 invalidate ~redisplay:true "geometry"
1301 (fun () ->
1302 state.w <- w;
1303 if not firsttime
1304 then state.x <- truncate (relx *. float w);
1305 let w =
1306 match conf.columns with
1307 | Csingle _ -> w
1308 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1309 | Csplit (c, _) -> w * c
1311 wcmd "geometry %d %d %d"
1312 w (stateh h) (FMTE.to_int conf.fitmodel)
1316 let enttext () =
1317 let len = String.length state.text in
1318 let x0 = if conf.leftscroll then vscrollw () else 0 in
1319 let drawstring s =
1320 let hscrollh =
1321 match state.mode with
1322 | Textentry _ | View | LinkNav _ ->
1323 let h, _, _ = state.uioh#scrollpw in
1325 | Birdseye _ -> 0
1327 let rect x w =
1328 filledrect
1329 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1330 (x+.w) (float (state.winh - hscrollh))
1333 let w = float (state.winw - 1 - vscrollw ()) in
1334 if state.progress >= 0.0 && state.progress < 1.0
1335 then (
1336 GlDraw.color (0.3, 0.3, 0.3);
1337 let w1 = w *. state.progress in
1338 rect (float x0) w1;
1339 GlDraw.color (0.0, 0.0, 0.0);
1340 rect (float x0+.w1) (float x0+.w-.w1)
1342 else (
1343 GlDraw.color (0.0, 0.0, 0.0);
1344 rect (float x0) w;
1347 GlDraw.color (1.0, 1.0, 1.0);
1348 drawstring
1349 fstate.fontsize
1350 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1351 (state.winh - hscrollh - 5) s;
1353 let s =
1354 match state.mode with
1355 | Textentry ((prefix, text, _, _, _, _), _) ->
1356 let s =
1357 if len > 0
1358 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1359 else Printf.sprintf "%s%s_" prefix text
1363 | Birdseye _ | View | LinkNav _ -> state.text
1365 let s =
1366 if state.newerrmsgs
1367 then (
1368 if not (istextentry state.mode) && state.uioh#eformsgs
1369 then
1370 let s1 = "(press 'e' to review error messasges)" in
1371 if nonemptystr s then s ^ " " ^ s1 else s1
1372 else s
1374 else s
1376 if nonemptystr s
1377 then drawstring s
1380 let gctiles () =
1381 let len = Queue.length state.tilelru in
1382 let layout = lazy (
1383 match state.throttle with
1384 | None ->
1385 if conf.preload
1386 then preloadlayout state.x state.y state.winw state.winh
1387 else state.layout
1388 | Some (layout, _, _) ->
1389 layout
1390 ) in
1391 let rec loop qpos =
1392 if state.memused > conf.memlimit
1393 then (
1394 if qpos < len
1395 then
1396 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1397 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1398 let (_, pw, ph, _) = getpagedim n in
1399 if gen = state.gen
1400 && colorspace = conf.colorspace
1401 && angle = conf.angle
1402 && pagew = pw
1403 && pageh = ph
1404 && (
1405 let x = col*conf.tilew
1406 and y = row*conf.tileh in
1407 tilevisible (Lazy.force_val layout) n x y
1409 then Queue.push lruitem state.tilelru
1410 else (
1411 freepbo p;
1412 wcmd "freetile %s" (~> p);
1413 state.memused <- state.memused - s;
1414 state.uioh#infochanged Memused;
1415 Hashtbl.remove state.tilemap k;
1417 loop (qpos+1)
1420 loop 0
1423 let onpagerect pageno f =
1424 let b =
1425 match conf.columns with
1426 | Cmulti (_, b) -> b
1427 | Csingle b -> b
1428 | Csplit (_, b) -> b
1430 if pageno >= 0 && pageno < Array.length b
1431 then
1432 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1433 f w h
1436 let gotopagexy1 wtmode pageno x y =
1437 let _,w1,h1,leftx = getpagedim pageno in
1438 let top = y /. (float h1) in
1439 let left = x /. (float w1) in
1440 let py, w, h = getpageywh pageno in
1441 let wh = state.winh in
1442 let x = left *. (float w) in
1443 let x = leftx + state.x + truncate x in
1444 let sx =
1445 if x < 0 || x >= state.winw
1446 then state.x - x
1447 else state.x
1449 let pdy = truncate (top *. float h) in
1450 let y' = py + pdy in
1451 let dy = y' - state.y in
1452 let sy =
1453 if x != state.x || not (dy > 0 && dy < wh)
1454 then (
1455 if conf.presentation
1456 then
1457 if abs (py - y') > wh
1458 then y'
1459 else py
1460 else y';
1462 else state.y
1464 if state.x != sx || state.y != sy
1465 then (
1466 let x, y =
1467 if wtmode
1468 then (
1469 let ww = state.winw in
1470 let qx = sx / ww
1471 and qy = pdy / wh in
1472 let x = qx * ww
1473 and y = py + qy * wh in
1474 let x = if -x + ww > w1 then -(w1-ww) else x
1475 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1476 let y =
1477 if conf.presentation
1478 then
1479 if abs (py - y') > wh
1480 then y'
1481 else py
1482 else y';
1484 (x, y)
1486 else (sx, sy)
1488 gotoxy_and_clear_text x y;
1490 else gotoxy_and_clear_text state.x state.y;
1493 let gotopagexy wtmode pageno x y =
1494 match state.mode with
1495 | Birdseye _ -> gotopage pageno 0.0
1496 | Textentry _ | View | LinkNav _ -> gotopagexy1 wtmode pageno x y
1499 let getpassword () =
1500 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1501 if emptystr passcmd
1502 then E.s
1503 else getcmdoutput
1504 (fun s ->
1505 impmsg "error getting password: %s" s;
1506 dolog "%s" s) passcmd;
1509 let pgoto opaque pageno x y =
1510 let pdimno = getpdimno pageno in
1511 let x, y = project opaque pageno pdimno x y in
1512 gotopagexy false pageno x y;
1515 let act cmds =
1516 (* dolog "%S" cmds; *)
1517 let spl = splitatchar cmds ' ' in
1518 let scan s fmt f =
1519 try Scanf.sscanf s fmt f
1520 with exn ->
1521 dolog "error processing '%S': %s" cmds @@ exntos exn;
1522 exit 1
1524 let addoutline outline =
1525 match state.currently with
1526 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1527 | Idle -> state.currently <- Outlining [outline]
1528 | Loading _ | Tiling _ ->
1529 dolog "invalid outlining state";
1530 logcurrently state.currently
1532 match spl with
1533 | "clear", "" ->
1534 state.pdims <- [];
1535 state.uioh#infochanged Pdim;
1537 | "clearrects", "" ->
1538 state.rects <- state.rects1;
1539 G.postRedisplay "clearrects";
1541 | "continue", args ->
1542 let n = scan args "%u" (fun n -> n) in
1543 state.pagecount <- n;
1544 begin match state.currently with
1545 | Outlining l ->
1546 state.currently <- Idle;
1547 state.outlines <- Array.of_list (List.rev l)
1548 | Idle | Loading _ | Tiling _ -> ()
1549 end;
1551 let cur, cmds = state.geomcmds in
1552 if emptystr cur
1553 then failwith "umpossible";
1555 begin match List.rev cmds with
1556 | [] ->
1557 state.geomcmds <- E.s, [];
1558 state.throttle <- None;
1559 represent ();
1560 | (s, f) :: rest ->
1561 f ();
1562 state.geomcmds <- s, List.rev rest;
1563 end;
1564 if conf.maxwait = None && not !wtmode
1565 then G.postRedisplay "continue";
1567 | "msg", args ->
1568 showtext ' ' args
1570 | "vmsg", args ->
1571 if conf.verbose
1572 then showtext ' ' args
1574 | "emsg", args ->
1575 Buffer.add_string state.errmsgs args;
1576 state.newerrmsgs <- true;
1577 G.postRedisplay "error message"
1579 | "progress", args ->
1580 let progress, text =
1581 scan args "%f %n"
1582 (fun f pos ->
1583 f, String.sub args pos (String.length args - pos))
1585 state.text <- text;
1586 state.progress <- progress;
1587 G.postRedisplay "progress"
1589 | "firstmatch", args ->
1590 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1591 scan args "%u %d %f %f %f %f %f %f %f %f"
1592 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1593 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1595 let y = (getpagey pageno) + truncate y0 in
1596 let x =
1597 if conf.zoom > 1.0
1598 then state.winw/2
1599 else state.x
1601 addnav ();
1602 gotoxy x y;
1603 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1604 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1606 | "match", args ->
1607 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1608 scan args "%u %d %f %f %f %f %f %f %f %f"
1609 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1610 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1612 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1613 state.rects1 <-
1614 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1616 | "page", args ->
1617 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1618 let pageopaque = ~< pageopaques in
1619 begin match state.currently with
1620 | Loading (l, gen) ->
1621 vlog "page %d took %f sec" l.pageno t;
1622 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1623 begin match state.throttle with
1624 | None ->
1625 let preloadedpages =
1626 if conf.preload
1627 then preloadlayout state.x state.y state.winw state.winh
1628 else state.layout
1630 let evict () =
1631 let set =
1632 List.fold_left (fun s l -> IntSet.add l.pageno s)
1633 IntSet.empty preloadedpages
1635 let evictedpages =
1636 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1637 if not (IntSet.mem pageno set)
1638 then (
1639 wcmd "freepage %s" (~> opaque);
1640 key :: accu
1642 else accu
1643 ) state.pagemap []
1645 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1647 evict ();
1648 state.currently <- Idle;
1649 if gen = state.gen
1650 then (
1651 tilepage l.pageno pageopaque state.layout;
1652 load state.layout;
1653 load preloadedpages;
1654 let visible = pagevisible state.layout l.pageno in
1655 if visible
1656 then (
1657 match state.mode with
1658 | LinkNav (Ltnotready (pageno, dir)) ->
1659 if pageno = l.pageno
1660 then (
1661 let link =
1662 let ld =
1663 if dir = 0
1664 then LDfirstvisible (l.pagex, l.pagey, dir)
1665 else (
1666 if dir > 0 then LDfirst else LDlast
1669 findlink pageopaque ld
1671 match link with
1672 | Lnotfound -> ()
1673 | Lfound n ->
1674 showlinktype (getlink pageopaque n);
1675 state.mode <- LinkNav (Ltexact (l.pageno, n))
1677 | LinkNav (Ltgendir _)
1678 | LinkNav (Ltexact _)
1679 | View
1680 | Birdseye _
1681 | Textentry _ -> ()
1684 if visible && layoutready state.layout
1685 then (
1686 G.postRedisplay "page";
1690 | Some (layout, _, _) ->
1691 state.currently <- Idle;
1692 tilepage l.pageno pageopaque layout;
1693 load state.layout
1694 end;
1696 | Idle | Tiling _ | Outlining _ ->
1697 dolog "Inconsistent loading state";
1698 logcurrently state.currently;
1699 exit 1
1702 | "tile" , args ->
1703 let (x, y, opaques, size, t) =
1704 scan args "%u %u %s %u %f"
1705 (fun x y p size t -> (x, y, p, size, t))
1707 let opaque = ~< opaques in
1708 begin match state.currently with
1709 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1710 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1712 unmappbo opaque;
1713 if tilew != conf.tilew || tileh != conf.tileh
1714 then (
1715 wcmd "freetile %s" (~> opaque);
1716 state.currently <- Idle;
1717 load state.layout;
1719 else (
1720 puttileopaque l col row gen cs angle opaque size t;
1721 state.memused <- state.memused + size;
1722 state.uioh#infochanged Memused;
1723 gctiles ();
1724 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1725 opaque, size) state.tilelru;
1727 let layout =
1728 match state.throttle with
1729 | None -> state.layout
1730 | Some (layout, _, _) -> layout
1733 state.currently <- Idle;
1734 if gen = state.gen
1735 && conf.colorspace = cs
1736 && conf.angle = angle
1737 && tilevisible layout l.pageno x y
1738 then conttiling l.pageno pageopaque;
1740 begin match state.throttle with
1741 | None ->
1742 preload state.layout;
1743 if gen = state.gen
1744 && conf.colorspace = cs
1745 && conf.angle = angle
1746 && tilevisible state.layout l.pageno x y
1747 && (not !wtmode || layoutready state.layout)
1748 then G.postRedisplay "tile nothrottle";
1750 | Some (layout, y, _) ->
1751 let ready = layoutready layout in
1752 if ready
1753 then (
1754 state.y <- y;
1755 state.layout <- layout;
1756 state.throttle <- None;
1757 G.postRedisplay "throttle";
1759 else load layout;
1760 end;
1763 | Idle | Loading _ | Outlining _ ->
1764 dolog "Inconsistent tiling state";
1765 logcurrently state.currently;
1766 exit 1
1769 | "pdim", args ->
1770 let (n, w, h, _) as pdim =
1771 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1773 let pdim =
1774 match conf.fitmodel with
1775 | FitWidth -> pdim
1776 | FitPage | FitProportional ->
1777 match conf.columns with
1778 | Csplit _ -> (n, w, h, 0)
1779 | Csingle _ | Cmulti _ -> pdim
1781 state.pdims <- pdim :: state.pdims;
1782 state.uioh#infochanged Pdim
1784 | "o", args ->
1785 let (l, n, t, h, pos) =
1786 scan args "%u %u %d %u %n"
1787 (fun l n t h pos -> l, n, t, h, pos)
1789 let s = String.sub args pos (String.length args - pos) in
1790 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1792 | "ou", args ->
1793 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1794 let s = String.sub args pos len in
1795 let pos2 = pos + len + 1 in
1796 let uri = String.sub args pos2 (String.length args - pos2) in
1797 addoutline (s, l, Ouri uri)
1799 | "on", args ->
1800 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1801 let s = String.sub args pos (String.length args - pos) in
1802 addoutline (s, l, Onone)
1804 | "a", args ->
1805 let (n, l, t) =
1806 scan args "%u %d %d" (fun n l t -> n, l, t)
1808 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1810 | "info", args ->
1811 let c, v = splitatchar args '\t' in
1812 let s =
1813 if nonemptystr v
1814 then
1815 if c = "Title"
1816 then (
1817 conf.title <- v;
1818 Wsi.settitle v;
1819 args
1821 else
1822 if let len = String.length c in
1823 len > 6 && ((String.sub c (len-4) 4) = "date")
1824 then (
1825 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1826 then
1827 let b = Buffer.create 10 in
1828 Printf.bprintf b "%s\t" c;
1829 let sub p l c =
1831 Buffer.add_substring b v p l;
1832 Buffer.add_char b c;
1833 with exn -> Buffer.add_string b @@ exntos exn
1835 sub 2 4 '/';
1836 sub 6 2 '/';
1837 sub 8 2 ' ';
1838 sub 10 2 ':';
1839 sub 12 2 ':';
1840 sub 14 2 ' ';
1841 Buffer.add_char b '[';
1842 Buffer.add_string b v;
1843 Buffer.add_char b ']';
1844 Buffer.contents b
1845 else args
1847 else args
1848 else args
1850 state.docinfo <- (1, s) :: state.docinfo
1852 | "infoend", "" ->
1853 state.docinfo <- List.rev state.docinfo;
1854 state.uioh#infochanged Docinfo
1856 | "pass", args ->
1857 if args = "fail"
1858 then Wsi.settitle "Wrong password";
1859 let password = getpassword () in
1860 if emptystr password
1861 then error "document is password protected"
1862 else opendoc state.path password
1864 | _ ->
1865 error "unknown cmd `%S'" cmds
1868 let onhist cb =
1869 let rc = cb.rc in
1870 let action = function
1871 | HCprev -> cbget cb ~-1
1872 | HCnext -> cbget cb 1
1873 | HCfirst -> cbget cb ~-(cb.rc)
1874 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1875 and cancel () = cb.rc <- rc
1876 in (action, cancel)
1879 let search pattern forward =
1880 match conf.columns with
1881 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1882 | Csingle _ | Cmulti _ ->
1883 if nonemptystr pattern
1884 then
1885 let pn, py =
1886 match state.layout with
1887 | [] -> 0, 0
1888 | l :: _ ->
1889 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1891 wcmd "search %d %d %d %d,%s\000"
1892 (btod conf.icase) pn py (btod forward) pattern;
1895 let [@warning "-4"] intentry text key =
1896 let text =
1897 if emptystr text && key = Keys.Ascii '-'
1898 then addchar text '-'
1899 else
1900 match key with
1901 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1902 | _ ->
1903 state.text <- "invalid key";
1904 text
1906 TEcont text
1909 let linknact f s =
1910 if nonemptystr s
1911 then (
1912 let n =
1913 let l = String.length s in
1914 let rec loop pos n =
1915 if pos = l
1916 then n
1917 else
1918 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1919 loop (pos+1) (n*26 + m)
1920 in loop 0 0
1922 let rec loop n = function
1923 | [] -> ()
1924 | l :: rest ->
1925 match getopaque l.pageno with
1926 | None -> loop n rest
1927 | Some opaque ->
1928 let m = getlinkcount opaque in
1929 if n < m
1930 then (
1931 let under = getlink opaque n in
1932 f under
1934 else loop (n-m) rest
1936 loop n state.layout;
1940 let [@warning "-4"] linknentry text = function
1941 | Keys.Ascii c ->
1942 let text = addchar text c in
1943 linknact (fun under -> state.text <- undertext under) text;
1944 TEcont text
1945 | _ ->
1946 state.text <- Printf.sprintf "invalid key";
1947 TEcont text
1950 let [@warning "-4"] textentry text = function
1951 | Keys.Ascii c -> TEcont (addchar text c)
1952 | Keys.Code c -> TEcont (text ^ toutf8 c)
1953 | _ -> TEcont text
1956 let reqlayout angle fitmodel =
1957 match state.throttle with
1958 | None ->
1959 if nogeomcmds state.geomcmds
1960 then state.anchor <- getanchor ();
1961 conf.angle <- angle mod 360;
1962 if conf.angle != 0
1963 then (
1964 match state.mode with
1965 | LinkNav _ -> state.mode <- View
1966 | Birdseye _ | Textentry _ | View -> ()
1968 conf.fitmodel <- fitmodel;
1969 invalidate
1970 "reqlayout"
1971 (fun () ->
1972 wcmd "reqlayout %d %d %d"
1973 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1975 | _ -> ()
1978 let settrim trimmargins trimfuzz =
1979 if nogeomcmds state.geomcmds
1980 then state.anchor <- getanchor ();
1981 conf.trimmargins <- trimmargins;
1982 conf.trimfuzz <- trimfuzz;
1983 let x0, y0, x1, y1 = trimfuzz in
1984 invalidate
1985 "settrim" (fun () ->
1986 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1987 flushpages ();
1990 let setzoom zoom =
1991 match state.throttle with
1992 | None ->
1993 let zoom = max 0.0001 zoom in
1994 if zoom <> conf.zoom
1995 then (
1996 state.prevzoom <- (conf.zoom, state.x);
1997 conf.zoom <- zoom;
1998 reshape state.winw state.winh;
1999 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2002 | Some (layout, y, started) ->
2003 let time =
2004 match conf.maxwait with
2005 | None -> 0.0
2006 | Some t -> t
2008 let dt = now () -. started in
2009 if dt > time
2010 then (
2011 state.y <- y;
2012 load layout;
2016 let pivotzoom ?(vw=min state.w state.winw)
2017 ?(vh=min (state.maxy-state.y) state.winh)
2018 ?(x=vw/2) ?(y=vh/2) zoom =
2019 let w = float state.w /. zoom in
2020 let hw = w /. 2.0 in
2021 let ratio = float vh /. float vw in
2022 let hh = hw *. ratio in
2023 let x0 = float x -. hw
2024 and y0 = float y -. hh in
2025 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2026 setzoom zoom;
2029 let pivotzoom ?vw ?vh ?x ?y zoom =
2030 if nogeomcmds state.geomcmds
2031 then
2032 if zoom > 1.0
2033 then pivotzoom ?vw ?vh ?x ?y zoom
2034 else setzoom zoom
2037 let setcolumns mode columns coverA coverB =
2038 state.prevcolumns <- Some (conf.columns, conf.zoom);
2039 if columns < 0
2040 then (
2041 if isbirdseye mode
2042 then impmsg "split mode doesn't work in bird's eye"
2043 else (
2044 conf.columns <- Csplit (-columns, E.a);
2045 state.x <- 0;
2046 conf.zoom <- 1.0;
2049 else (
2050 if columns < 2
2051 then (
2052 conf.columns <- Csingle E.a;
2053 state.x <- 0;
2054 setzoom 1.0;
2056 else (
2057 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2058 conf.zoom <- 1.0;
2061 reshape state.winw state.winh;
2064 let resetmstate () =
2065 state.mstate <- Mnone;
2066 Wsi.setcursor Wsi.CURSOR_INHERIT;
2069 let enterbirdseye () =
2070 let zoom = float conf.thumbw /. float state.winw in
2071 let birdseyepageno =
2072 let cy = state.winh / 2 in
2073 let fold = function
2074 | [] -> 0
2075 | l :: rest ->
2076 let rec fold best = function
2077 | [] -> best.pageno
2078 | l :: rest ->
2079 let d = cy - (l.pagedispy + l.pagevh/2)
2080 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2081 if abs d < abs dbest
2082 then fold l rest
2083 else best.pageno
2084 in fold l rest
2086 fold state.layout
2088 state.mode <-
2089 Birdseye (
2090 { conf with zoom = conf.zoom },
2091 state.x, birdseyepageno, -1, getanchor ()
2093 resetmstate ();
2094 conf.zoom <- zoom;
2095 conf.presentation <- false;
2096 conf.interpagespace <- 10;
2097 conf.hlinks <- false;
2098 conf.fitmodel <- FitPage;
2099 state.x <- 0;
2100 conf.maxwait <- None;
2101 conf.columns <- (
2102 match conf.beyecolumns with
2103 | Some c ->
2104 conf.zoom <- 1.0;
2105 Cmulti ((c, 0, 0), E.a)
2106 | None -> Csingle E.a
2108 if conf.verbose
2109 then
2110 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2111 (100.0*.zoom)
2112 else
2113 state.text <- E.s
2115 reshape state.winw state.winh;
2118 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2119 state.mode <- View;
2120 conf.zoom <- c.zoom;
2121 conf.presentation <- c.presentation;
2122 conf.interpagespace <- c.interpagespace;
2123 conf.maxwait <- c.maxwait;
2124 conf.hlinks <- c.hlinks;
2125 conf.fitmodel <- c.fitmodel;
2126 conf.beyecolumns <- (
2127 match conf.columns with
2128 | Cmulti ((c, _, _), _) -> Some c
2129 | Csingle _ -> None
2130 | Csplit _ -> failwith "leaving bird's eye split mode"
2132 conf.columns <- (
2133 match c.columns with
2134 | Cmulti (c, _) -> Cmulti (c, E.a)
2135 | Csingle _ -> Csingle E.a
2136 | Csplit (c, _) -> Csplit (c, E.a)
2138 if conf.verbose
2139 then
2140 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2141 (100.0*.conf.zoom)
2143 reshape state.winw state.winh;
2144 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2145 state.x <- leftx;
2148 let togglebirdseye () =
2149 match state.mode with
2150 | Birdseye vals -> leavebirdseye vals true
2151 | View -> enterbirdseye ()
2152 | Textentry _ | LinkNav _ -> ()
2155 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2156 let pageno = max 0 (pageno - incr) in
2157 let rec loop = function
2158 | [] -> gotopage1 pageno 0
2159 | l :: _ when l.pageno = pageno ->
2160 if l.pagedispy >= 0 && l.pagey = 0
2161 then G.postRedisplay "upbirdseye"
2162 else gotopage1 pageno 0
2163 | _ :: rest -> loop rest
2165 loop state.layout;
2166 state.text <- E.s;
2167 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2170 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2171 let pageno = min (state.pagecount - 1) (pageno + incr) in
2172 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2173 let rec loop = function
2174 | [] ->
2175 let y, h = getpageyh pageno in
2176 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2177 gotoxy state.x (clamp dy)
2178 | l :: _ when l.pageno = pageno ->
2179 if l.pagevh != l.pageh
2180 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2181 else G.postRedisplay "downbirdseye"
2182 | _ :: rest -> loop rest
2184 loop state.layout;
2185 state.text <- E.s;
2188 let [@warning "-4"] optentry mode _ key =
2189 let btos b = if b then "on" else "off" in
2190 match key with
2191 | Keys.Ascii 's' ->
2192 let ondone s =
2193 try conf.scrollstep <- int_of_string s with exn ->
2194 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2196 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2198 | Keys.Ascii 'A' ->
2199 let ondone s =
2201 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2202 if state.autoscroll <> None
2203 then state.autoscroll <- Some conf.autoscrollstep
2204 with exn ->
2205 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2207 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2209 | Keys.Ascii 'C' ->
2210 let ondone s =
2212 let n, a, b = multicolumns_of_string s in
2213 setcolumns mode n a b;
2214 with exn ->
2215 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2217 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2219 | Keys.Ascii 'Z' ->
2220 let ondone s =
2222 let zoom = float (int_of_string s) /. 100.0 in
2223 pivotzoom zoom
2224 with exn ->
2225 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2227 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2229 | Keys.Ascii 't' ->
2230 let ondone s =
2232 conf.thumbw <- bound (int_of_string s) 2 4096;
2233 state.text <-
2234 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2235 begin match mode with
2236 | Birdseye beye ->
2237 leavebirdseye beye false;
2238 enterbirdseye ();
2239 | Textentry _ | View | LinkNav _ -> ();
2241 with exn ->
2242 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2244 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2246 | Keys.Ascii 'R' ->
2247 let ondone s =
2248 match int_of_string s with
2249 | angle -> reqlayout angle conf.fitmodel
2250 | exception exn ->
2251 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2253 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2255 | Keys.Ascii 'i' ->
2256 conf.icase <- not conf.icase;
2257 TEdone ("case insensitive search " ^ (btos conf.icase))
2259 | Keys.Ascii 'p' ->
2260 conf.preload <- not conf.preload;
2261 gotoxy state.x state.y;
2262 TEdone ("preload " ^ (btos conf.preload))
2264 | Keys.Ascii 'v' ->
2265 conf.verbose <- not conf.verbose;
2266 TEdone ("verbose " ^ (btos conf.verbose))
2268 | Keys.Ascii 'd' ->
2269 conf.debug <- not conf.debug;
2270 TEdone ("debug " ^ (btos conf.debug))
2272 | Keys.Ascii 'h' ->
2273 conf.maxhfit <- not conf.maxhfit;
2274 state.maxy <- calcheight ();
2275 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2277 | Keys.Ascii 'c' ->
2278 conf.crophack <- not conf.crophack;
2279 TEdone ("crophack " ^ btos conf.crophack)
2281 | Keys.Ascii 'a' ->
2282 let s =
2283 match conf.maxwait with
2284 | None ->
2285 conf.maxwait <- Some infinity;
2286 "always wait for page to complete"
2287 | Some _ ->
2288 conf.maxwait <- None;
2289 "show placeholder if page is not ready"
2291 TEdone s
2293 | Keys.Ascii 'f' ->
2294 conf.underinfo <- not conf.underinfo;
2295 TEdone ("underinfo " ^ btos conf.underinfo)
2297 | Keys.Ascii 'P' ->
2298 conf.savebmarks <- not conf.savebmarks;
2299 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2301 | Keys.Ascii 'S' ->
2302 let ondone s =
2304 let pageno, py =
2305 match state.layout with
2306 | [] -> 0, 0
2307 | l :: _ ->
2308 l.pageno, l.pagey
2310 conf.interpagespace <- int_of_string s;
2311 docolumns conf.columns;
2312 state.maxy <- calcheight ();
2313 let y = getpagey pageno in
2314 gotoxy state.x (y + py)
2315 with exn ->
2316 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2318 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2320 | Keys.Ascii 'l' ->
2321 let fm =
2322 match conf.fitmodel with
2323 | FitProportional -> FitWidth
2324 | FitWidth | FitPage -> FitProportional
2326 reqlayout conf.angle fm;
2327 TEdone ("proportional display " ^ btos (fm == FitProportional))
2329 | Keys.Ascii 'T' ->
2330 settrim (not conf.trimmargins) conf.trimfuzz;
2331 TEdone ("trim margins " ^ btos conf.trimmargins)
2333 | Keys.Ascii 'I' ->
2334 conf.invert <- not conf.invert;
2335 TEdone ("invert colors " ^ btos conf.invert)
2337 | Keys.Ascii 'x' ->
2338 let ondone s =
2339 cbput state.hists.sel s;
2340 conf.selcmd <- s;
2342 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2343 textentry, ondone, true)
2345 | Keys.Ascii 'M' ->
2346 if conf.pax == None
2347 then conf.pax <- Some 0.0
2348 else conf.pax <- None;
2349 TEdone ("PAX " ^ btos (conf.pax != None))
2351 | (Keys.Ascii c) ->
2352 state.text <- Printf.sprintf "bad option %d `%c'"
2353 (Char.code c) c;
2354 TEstop
2356 | _ ->
2357 TEcont state.text
2360 class type lvsource =
2361 object
2362 method getitemcount : int
2363 method getitem : int -> (string * int)
2364 method hasaction : int -> bool
2365 method exit : uioh:uioh ->
2366 cancel:bool ->
2367 active:int ->
2368 first:int ->
2369 pan:int ->
2370 uioh option
2371 method getactive : int
2372 method getfirst : int
2373 method getpan : int
2374 method getminfo : (int * int) array
2375 end;;
2377 class virtual lvsourcebase = object
2378 val mutable m_active = 0
2379 val mutable m_first = 0
2380 val mutable m_pan = 0
2381 method getactive = m_active
2382 method getfirst = m_first
2383 method getpan = m_pan
2384 method getminfo : (int * int) array = E.a
2385 end;;
2387 let [@warning "-4"]
2388 textentrykeyboard
2389 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2390 state.text <- E.s;
2391 let enttext te =
2392 state.mode <- Textentry (te, onleave);
2393 enttext ();
2394 G.postRedisplay "textentrykeyboard enttext";
2396 let histaction cmd =
2397 match opthist with
2398 | None -> ()
2399 | Some (action, _) ->
2400 state.mode <-
2401 Textentry (
2402 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2404 G.postRedisplay "textentry histaction"
2406 let open Keys in
2407 let kt = Wsi.kc2kt key in
2408 match kt with
2409 | Backspace ->
2410 if emptystr text && cancelonempty
2411 then (
2412 onleave Cancel;
2413 G.postRedisplay "textentrykeyboard after cancel";
2415 else
2416 let s = withoutlastutf8 text in
2417 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2419 | Enter ->
2420 ondone text;
2421 onleave Confirm;
2422 G.postRedisplay "textentrykeyboard after confirm"
2424 | Up -> histaction HCprev
2425 | Down -> histaction HCnext
2426 | Home -> histaction HCfirst
2427 | End -> histaction HClast
2429 | Escape ->
2430 if emptystr text
2431 then (
2432 begin match opthist with
2433 | None -> ()
2434 | Some (_, onhistcancel) -> onhistcancel ()
2435 end;
2436 onleave Cancel;
2437 state.text <- E.s;
2438 G.postRedisplay "textentrykeyboard after cancel2"
2440 else (
2441 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2444 | Delete -> ()
2446 | Code _ | Ascii _ ->
2447 begin match onkey text kt with
2448 | TEdone text ->
2449 ondone text;
2450 onleave Confirm;
2451 G.postRedisplay "textentrykeyboard after confirm2";
2453 | TEcont text ->
2454 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2456 | TEstop ->
2457 onleave Cancel;
2458 G.postRedisplay "textentrykeyboard after cancel3"
2460 | TEswitch te ->
2461 state.mode <- Textentry (te, onleave);
2462 G.postRedisplay "textentrykeyboard switch";
2464 | _ -> vlog "unhandled key"
2467 let firstof first active =
2468 if first > active || abs (first - active) > fstate.maxrows - 1
2469 then max 0 (active - (fstate.maxrows/2))
2470 else first
2473 let calcfirst first active =
2474 if active > first
2475 then
2476 let rows = active - first in
2477 if rows > fstate.maxrows then active - fstate.maxrows else first
2478 else active
2481 let scrollph y maxy =
2482 let sh = float (maxy + state.winh) /. float state.winh in
2483 let sh = float state.winh /. sh in
2484 let sh = max sh (float conf.scrollh) in
2486 let percent = float y /. float maxy in
2487 let position = (float state.winh -. sh) *. percent in
2489 let position =
2490 if position +. sh > float state.winh
2491 then float state.winh -. sh
2492 else position
2494 position, sh;
2497 let adderrmsg src msg =
2498 Buffer.add_string state.errmsgs msg;
2499 state.newerrmsgs <- true;
2500 G.postRedisplay src
2503 let adderrfmt src fmt =
2504 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2507 let coe s = (s :> uioh);;
2509 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2510 object (self)
2511 val m_pan = source#getpan
2512 val m_first = source#getfirst
2513 val m_active = source#getactive
2514 val m_qsearch = E.s
2515 val m_prev_uioh = state.uioh
2517 method private elemunder y =
2518 if y < 0
2519 then None
2520 else
2521 let n = y / (fstate.fontsize+1) in
2522 if m_first + n < source#getitemcount
2523 then (
2524 if source#hasaction (m_first + n)
2525 then Some (m_first + n)
2526 else None
2528 else None
2530 method display =
2531 Gl.enable `blend;
2532 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2533 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2534 filledrect 0. 0. (float state.winw) (float state.winh);
2535 GlDraw.color (1., 1., 1.);
2536 Gl.enable `texture_2d;
2537 let fs = fstate.fontsize in
2538 let nfs = fs + 1 in
2539 let hw = state.winw/3 in
2540 let ww = fstate.wwidth in
2541 let tabw = 17.0*.ww in
2542 let itemcount = source#getitemcount in
2543 let minfo = source#getminfo in
2544 if conf.leftscroll
2545 then (
2546 GlMat.push ();
2547 GlMat.translate ~x:(float conf.scrollbw) ();
2549 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2550 let rec loop row =
2551 if (row - m_first) > fstate.maxrows
2552 then ()
2553 else (
2554 if row >= 0 && row < itemcount
2555 then (
2556 let (s, level) = source#getitem row in
2557 let y = (row - m_first) * nfs in
2558 let x = 5.0 +. (float (level + m_pan)) *. ww in
2559 if helpmode
2560 then GlDraw.color
2561 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2563 if row = m_active
2564 then (
2565 Gl.disable `texture_2d;
2566 let alpha = if source#hasaction row then 0.9 else 0.3 in
2567 GlDraw.color (1., 1., 1.) ~alpha;
2568 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2569 Gl.enable `texture_2d;
2571 let c =
2572 if zebra && row land 1 = 1
2573 then 0.8
2574 else 1.0
2576 GlDraw.color (c,c,c);
2577 let drawtabularstring s =
2578 let drawstr x s =
2579 let x' = truncate (x0 +. x) in
2580 let s1, s2 = splitatchar s '\000' in
2581 if emptystr s2
2582 then drawstring1 fs x' (y+nfs) s
2583 else
2584 let rec e s =
2585 if emptystr s
2586 then s
2587 else
2588 let s' = withoutlastutf8 s in
2589 let s = s' ^ UniSyms.ellipsis in
2590 let w = measurestr fs s in
2591 if float x' +. w +. ww < float (hw + x')
2592 then s
2593 else e s'
2595 let s1 =
2596 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2597 then e s1
2598 else s1
2600 ignore (drawstring1 fs x' (y+nfs) s1);
2601 drawstring1 fs (hw + x') (y+nfs) s2
2603 if trusted
2604 then
2605 let x = if helpmode && row > 0 then x +. ww else x in
2606 let s1, s2 = splitatchar s '\t' in
2607 if nonemptystr s2
2608 then
2609 let nx = drawstr x s1 in
2610 let sw = nx -. x in
2611 let x = x +. (max tabw sw) in
2612 drawstr x s2
2613 else
2614 let len = String.length s - 2 in
2615 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2616 then
2617 let s = String.sub s 2 len in
2618 let x = if not helpmode then x +. ww else x in
2619 GlDraw.color (1.2, 1.2, 1.2);
2620 let vinc = drawstring1 (fs+fs/4)
2621 (truncate (x -. ww)) (y+nfs) s in
2622 GlDraw.color (1., 1., 1.);
2623 vinc +. (float fs *. 0.8)
2624 else
2625 drawstr x s
2626 else
2627 drawstr x s
2629 ignore (drawtabularstring s);
2630 loop (row+1)
2634 loop m_first;
2635 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2636 let xadj = 5.0 in
2637 let rec loop row =
2638 if (row - m_first) <= fstate.maxrows
2639 then
2640 if row >= 0 && row < itemcount
2641 then (
2642 let (s, level) = source#getitem row in
2643 let pos0 = nindex s '\000' in
2644 let y = (row - m_first) * nfs in
2645 let x = float (level + m_pan) *. ww in
2646 let (first, last) = minfo.(row) in
2647 let prefix =
2648 if pos0 > 0 && first > pos0
2649 then String.sub s (pos0+1) (first-pos0-1)
2650 else String.sub s 0 first
2652 let suffix = String.sub s first (last - first) in
2653 let w1 = measurestr fstate.fontsize prefix in
2654 let w2 = measurestr fstate.fontsize suffix in
2655 let x = x +. if conf.leftscroll then xadj else 5.0 in
2656 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2657 let x0 = x +. w1
2658 and y0 = float (y+2) in
2659 let x1 = x0 +. w2
2660 and y1 = float (y+fs+3) in
2661 filledrect x0 y0 x1 y1;
2662 loop (row+1)
2665 Gl.disable `texture_2d;
2666 if Array.length minfo > 0 then loop m_first;
2667 Gl.disable `blend;
2668 if conf.leftscroll
2669 then GlMat.pop ()
2671 method updownlevel incr =
2672 let len = source#getitemcount in
2673 let curlevel =
2674 if m_active >= 0 && m_active < len
2675 then snd (source#getitem m_active)
2676 else -1
2678 let rec flow i =
2679 if i = len then i-1 else if i = -1 then 0 else
2680 let _, l = source#getitem i in
2681 if l != curlevel then i else flow (i+incr)
2683 let active = flow m_active in
2684 let first = calcfirst m_first active in
2685 G.postRedisplay "outline updownlevel";
2686 {< m_active = active; m_first = first >}
2688 method private key1 key mask =
2689 let set1 active first qsearch =
2690 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2692 let search active pattern incr =
2693 let active = if active = -1 then m_first else active in
2694 let dosearch re =
2695 let rec loop n =
2696 if n >= 0 && n < source#getitemcount
2697 then (
2698 let s, _ = source#getitem n in
2699 match Str.search_forward re s 0 with
2700 | exception Not_found -> loop (n + incr)
2701 | _ -> Some n
2703 else None
2705 loop active
2707 let qpat = Str.quote pattern in
2708 match Str.regexp_case_fold qpat with
2709 | s -> dosearch s
2710 | exception exn ->
2711 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2712 qpat @@ Printexc.to_string exn;
2713 None
2715 let itemcount = source#getitemcount in
2716 let find start incr =
2717 let rec find i =
2718 if i = -1 || i = itemcount
2719 then -1
2720 else (
2721 if source#hasaction i
2722 then i
2723 else find (i + incr)
2726 find start
2728 let set active first =
2729 let first = bound first 0 (itemcount - fstate.maxrows) in
2730 state.text <- E.s;
2731 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2733 let navigate incr =
2734 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2735 let active, first =
2736 let incr1 = if incr > 0 then 1 else -1 in
2737 if isvisible m_first m_active
2738 then
2739 let next =
2740 let next = m_active + incr in
2741 let next =
2742 if next < 0 || next >= itemcount
2743 then -1
2744 else find next incr1
2746 if abs (m_active - next) > fstate.maxrows
2747 then -1
2748 else next
2750 if next = -1
2751 then
2752 let first = m_first + incr in
2753 let first = bound first 0 (itemcount - fstate.maxrows) in
2754 let next =
2755 let next = m_active + incr in
2756 let next = bound next 0 (itemcount - 1) in
2757 find next ~-incr1
2759 let active =
2760 if next = -1
2761 then m_active
2762 else (
2763 if isvisible first next
2764 then next
2765 else m_active
2768 active, first
2769 else
2770 let first = min next m_first in
2771 let first =
2772 if abs (next - first) > fstate.maxrows
2773 then first + incr
2774 else first
2776 next, first
2777 else
2778 let first = m_first + incr in
2779 let first = bound first 0 (itemcount - 1) in
2780 let active =
2781 let next = m_active + incr in
2782 let next = bound next 0 (itemcount - 1) in
2783 let next = find next incr1 in
2784 let active =
2785 if next = -1 || abs (m_active - first) > fstate.maxrows
2786 then (
2787 let active = if m_active = -1 then next else m_active in
2788 active
2790 else next
2792 if isvisible first active
2793 then active
2794 else -1
2796 active, first
2798 G.postRedisplay "listview navigate";
2799 set active first;
2801 let open Keys in
2802 let kt = Wsi.kc2kt key in
2803 match [@warning "-4"] kt with
2804 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2805 let incr = if c = 'r' then -1 else 1 in
2806 let active, first =
2807 match search (m_active + incr) m_qsearch incr with
2808 | None ->
2809 state.text <- m_qsearch ^ " [not found]";
2810 m_active, m_first
2811 | Some active ->
2812 state.text <- m_qsearch;
2813 active, firstof m_first active
2815 G.postRedisplay "listview ctrl-r/s";
2816 set1 active first m_qsearch;
2818 | Insert when Wsi.withctrl mask ->
2819 if m_active >= 0 && m_active < source#getitemcount
2820 then (
2821 let s, _ = source#getitem m_active in
2822 selstring s;
2824 coe self
2826 | Backspace ->
2827 if emptystr m_qsearch
2828 then coe self
2829 else (
2830 let qsearch = withoutlastutf8 m_qsearch in
2831 if emptystr qsearch
2832 then (
2833 state.text <- E.s;
2834 G.postRedisplay "listview empty qsearch";
2835 set1 m_active m_first E.s;
2837 else
2838 let active, first =
2839 match search m_active qsearch ~-1 with
2840 | None ->
2841 state.text <- qsearch ^ " [not found]";
2842 m_active, m_first
2843 | Some active ->
2844 state.text <- qsearch;
2845 active, firstof m_first active
2847 G.postRedisplay "listview backspace qsearch";
2848 set1 active first qsearch
2851 | Ascii _ | Code _ ->
2852 let utf8 =
2853 match [@warning "-8"] kt with
2854 | Ascii c -> String.make 1 c
2855 | Code code -> toutf8 code
2857 let pattern = m_qsearch ^ utf8 in
2858 let active, first =
2859 match search m_active pattern 1 with
2860 | None ->
2861 state.text <- pattern ^ " [not found]";
2862 m_active, m_first
2863 | Some active ->
2864 state.text <- pattern;
2865 active, firstof m_first active
2867 G.postRedisplay "listview qsearch add";
2868 set1 active first pattern;
2870 | Escape ->
2871 state.text <- E.s;
2872 if emptystr m_qsearch
2873 then (
2874 G.postRedisplay "list view escape";
2875 let mx, my = state.mpos in
2876 updateunder mx my;
2877 match source#exit ~uioh:(coe self) ~cancel:true ~active:m_active
2878 ~first:m_first ~pan:m_pan with
2879 | None -> m_prev_uioh
2880 | Some uioh -> uioh
2882 else (
2883 G.postRedisplay "list view kill qsearch";
2884 coe {< m_qsearch = E.s >}
2887 | Enter ->
2888 state.text <- E.s;
2889 let self = {< m_qsearch = E.s >} in
2890 let opt =
2891 G.postRedisplay "listview enter";
2892 let cancel = not (m_active >= 0 && m_active < source#getitemcount) in
2893 source#exit ~uioh:(coe self) ~cancel
2894 ~active:m_active ~first:m_first ~pan:m_pan;
2896 begin match opt with
2897 | None -> m_prev_uioh
2898 | Some uioh -> uioh
2901 | Delete ->
2902 coe self
2904 | Up -> navigate ~-1
2905 | Down -> navigate 1
2906 | Prior -> navigate ~-(fstate.maxrows)
2907 | Next -> navigate fstate.maxrows
2909 | Right ->
2910 state.text <- E.s;
2911 G.postRedisplay "listview right";
2912 coe {< m_pan = m_pan - 1 >}
2914 | Left ->
2915 state.text <- E.s;
2916 G.postRedisplay "listview left";
2917 coe {< m_pan = m_pan + 1 >}
2919 | Home ->
2920 let active = find 0 1 in
2921 G.postRedisplay "listview home";
2922 set active 0;
2924 | End ->
2925 let first = max 0 (itemcount - fstate.maxrows) in
2926 let active = find (itemcount - 1) ~-1 in
2927 G.postRedisplay "listview end";
2928 set active first;
2930 | _ -> coe self
2932 method key key mask =
2933 match state.mode with
2934 | Textentry te ->
2935 textentrykeyboard key mask te;
2936 coe self
2937 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2939 method button button down x y _ =
2940 let opt =
2941 match button with
2942 | 1 when vscrollhit x ->
2943 G.postRedisplay "listview scroll";
2944 if down
2945 then
2946 let _, position, sh = self#scrollph in
2947 if y > truncate position && y < truncate (position +. sh)
2948 then (
2949 state.mstate <- Mscrolly;
2950 Some (coe self)
2952 else
2953 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2954 let first = truncate (s *. float source#getitemcount) in
2955 let first = min source#getitemcount first in
2956 Some (coe {< m_first = first; m_active = first >})
2957 else (
2958 state.mstate <- Mnone;
2959 Some (coe self);
2961 | 1 when down ->
2962 begin match self#elemunder y with
2963 | Some n ->
2964 G.postRedisplay "listview click";
2965 source#exit ~uioh:(coe {< m_active = n >})
2966 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
2967 | _ ->
2968 Some (coe self)
2970 | n when (n == 4 || n == 5) && not down ->
2971 let len = source#getitemcount in
2972 let first =
2973 if n = 5 && m_first + fstate.maxrows >= len
2974 then
2975 m_first
2976 else
2977 let first = m_first + (if n == 4 then -1 else 1) in
2978 bound first 0 (len - 1)
2980 G.postRedisplay "listview wheel";
2981 Some (coe {< m_first = first >})
2982 | n when (n = 6 || n = 7) && not down ->
2983 let inc = if n = 7 then -1 else 1 in
2984 G.postRedisplay "listview hwheel";
2985 Some (coe {< m_pan = m_pan + inc >})
2986 | _ ->
2987 Some (coe self)
2989 match opt with
2990 | None -> m_prev_uioh
2991 | Some uioh -> uioh
2993 method multiclick _ x y = self#button 1 true x y
2995 method motion _ y =
2996 match state.mstate with
2997 | Mscrolly ->
2998 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2999 let first = truncate (s *. float source#getitemcount) in
3000 let first = min source#getitemcount first in
3001 G.postRedisplay "listview motion";
3002 coe {< m_first = first; m_active = first >}
3003 | Msel _
3004 | Mpan _
3005 | Mscrollx
3006 | Mzoom _
3007 | Mzoomrect _
3008 | Mnone -> coe self
3010 method pmotion x y =
3011 if x < state.winw - conf.scrollbw
3012 then
3013 let n =
3014 match self#elemunder y with
3015 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3016 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3018 let o =
3019 if n != m_active
3020 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3021 else self
3023 coe o
3024 else (
3025 Wsi.setcursor Wsi.CURSOR_INHERIT;
3026 coe self
3029 method infochanged _ = ()
3031 method scrollpw = (0, 0.0, 0.0)
3032 method scrollph =
3033 let nfs = fstate.fontsize + 1 in
3034 let y = m_first * nfs in
3035 let itemcount = source#getitemcount in
3036 let maxi = max 0 (itemcount - fstate.maxrows) in
3037 let maxy = maxi * nfs in
3038 let p, h = scrollph y maxy in
3039 conf.scrollbw, p, h
3041 method modehash = modehash
3042 method eformsgs = false
3043 method alwaysscrolly = true
3044 method scroll _ dy =
3045 let self =
3046 if dy != 0 then begin
3047 let len = source#getitemcount in
3048 let first =
3049 if dy > 0 && m_first + fstate.maxrows >= len
3050 then
3051 m_first
3052 else
3053 let first = m_first + dy / 10 in
3054 bound first 0 (len - 1)
3056 G.postRedisplay "listview wheel";
3057 {< m_first = first >}
3058 end else
3059 self
3061 coe self
3063 method zoom _ _ _ = ()
3064 end;;
3066 class outlinelistview ~zebra ~source =
3067 let settext autonarrow s =
3068 if autonarrow
3069 then
3070 let ss = source#statestr in
3071 state.text <-
3072 if emptystr ss
3073 then "[" ^ s ^ "]"
3074 else "{" ^ ss ^ "} [" ^ s ^ "]"
3075 else state.text <- s
3077 object (self)
3078 inherit listview
3079 ~zebra
3080 ~helpmode:false
3081 ~source:(source :> lvsource)
3082 ~trusted:false
3083 ~modehash:(findkeyhash conf "outline")
3084 as super
3086 val m_autonarrow = false
3088 method! key key mask =
3089 let maxrows =
3090 if emptystr state.text
3091 then fstate.maxrows
3092 else fstate.maxrows - 2
3094 let calcfirst first active =
3095 if active > first
3096 then
3097 let rows = active - first in
3098 if rows > maxrows then active - maxrows else first
3099 else active
3101 let navigate incr =
3102 let active = m_active + incr in
3103 let active = bound active 0 (source#getitemcount - 1) in
3104 let first = calcfirst m_first active in
3105 G.postRedisplay "outline navigate";
3106 coe {< m_active = active; m_first = first >}
3108 let navscroll first =
3109 let active =
3110 let dist = m_active - first in
3111 if dist < 0
3112 then first
3113 else (
3114 if dist < maxrows
3115 then m_active
3116 else first + maxrows
3119 G.postRedisplay "outline navscroll";
3120 coe {< m_first = first; m_active = active >}
3122 let ctrl = Wsi.withctrl mask in
3123 let open Keys in
3124 match Wsi.kc2kt key with
3125 | Ascii 'a' when ctrl ->
3126 let text =
3127 if m_autonarrow
3128 then (source#denarrow; E.s)
3129 else (
3130 let pattern = source#renarrow in
3131 if nonemptystr m_qsearch
3132 then (source#narrow m_qsearch; m_qsearch)
3133 else pattern
3136 settext (not m_autonarrow) text;
3137 G.postRedisplay "toggle auto narrowing";
3138 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3140 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
3141 settext true E.s;
3142 G.postRedisplay "toggle auto narrowing";
3143 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3145 | Ascii 'n' when ctrl ->
3146 source#narrow m_qsearch;
3147 if not m_autonarrow
3148 then source#add_narrow_pattern m_qsearch;
3149 G.postRedisplay "outline ctrl-n";
3150 coe {< m_first = 0; m_active = 0 >}
3152 | Ascii 'S' when ctrl ->
3153 let active = source#calcactive (getanchor ()) in
3154 let first = firstof m_first active in
3155 G.postRedisplay "outline ctrl-s";
3156 coe {< m_first = first; m_active = active >}
3158 | Ascii 'u' when ctrl ->
3159 G.postRedisplay "outline ctrl-u";
3160 if m_autonarrow && nonemptystr m_qsearch
3161 then (
3162 ignore (source#renarrow);
3163 settext m_autonarrow E.s;
3164 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3166 else (
3167 source#del_narrow_pattern;
3168 let pattern = source#renarrow in
3169 let text =
3170 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3172 settext m_autonarrow text;
3173 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3176 | Ascii 'l' when ctrl ->
3177 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3178 G.postRedisplay "outline ctrl-l";
3179 coe {< m_first = first >}
3181 | Ascii '\t' when m_autonarrow ->
3182 if nonemptystr m_qsearch
3183 then (
3184 G.postRedisplay "outline list view tab";
3185 source#add_narrow_pattern m_qsearch;
3186 settext true E.s;
3187 coe {< m_qsearch = E.s >}
3189 else coe self
3191 | Escape when m_autonarrow ->
3192 if nonemptystr m_qsearch
3193 then source#add_narrow_pattern m_qsearch;
3194 super#key key mask
3196 | Enter when m_autonarrow ->
3197 if nonemptystr m_qsearch
3198 then source#add_narrow_pattern m_qsearch;
3199 super#key key mask
3201 | (Ascii _ | Code _) when m_autonarrow ->
3202 let pattern = m_qsearch ^ toutf8 key in
3203 G.postRedisplay "outlinelistview autonarrow add";
3204 source#narrow pattern;
3205 settext true pattern;
3206 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3208 | Backspace when m_autonarrow ->
3209 if emptystr m_qsearch
3210 then coe self
3211 else
3212 let pattern = withoutlastutf8 m_qsearch in
3213 G.postRedisplay "outlinelistview autonarrow backspace";
3214 ignore (source#renarrow);
3215 source#narrow pattern;
3216 settext true pattern;
3217 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3219 | Up when ctrl ->
3220 navscroll (max 0 (m_first - 1))
3222 | Down when ctrl ->
3223 navscroll (min (source#getitemcount - 1) (m_first + 1))
3225 | Up -> navigate ~-1
3226 | Down -> navigate 1
3227 | Prior -> navigate ~-(fstate.maxrows)
3228 | Next -> navigate fstate.maxrows
3230 | Right ->
3231 let o =
3232 if ctrl
3233 then (
3234 G.postRedisplay "outline ctrl right";
3235 {< m_pan = m_pan + 1 >}
3237 else self#updownlevel 1
3239 coe o
3241 | Left ->
3242 let o =
3243 if ctrl
3244 then (
3245 G.postRedisplay "outline ctrl left";
3246 {< m_pan = m_pan - 1 >}
3248 else self#updownlevel ~-1
3250 coe o
3252 | Home ->
3253 G.postRedisplay "outline home";
3254 coe {< m_first = 0; m_active = 0 >}
3256 | End ->
3257 let active = source#getitemcount - 1 in
3258 let first = max 0 (active - fstate.maxrows) in
3259 G.postRedisplay "outline end";
3260 coe {< m_active = active; m_first = first >}
3262 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
3263 super#key key mask
3264 end;;
3266 let genhistoutlines () =
3267 Config.gethist ()
3268 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3269 compare c2.lastvisit c1.lastvisit)
3270 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
3271 let path = if nonemptystr origin then origin else path in
3272 let base = mbtoutf8 @@ Filename.basename path in
3273 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3277 let gotohist (path, c, bookmarks, x, anchor, origin) =
3278 Config.save leavebirdseye;
3279 state.anchor <- anchor;
3280 state.bookmarks <- bookmarks;
3281 state.origin <- origin;
3282 state.x <- x;
3283 setconf conf c;
3284 let x0, y0, x1, y1 = conf.trimfuzz in
3285 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3286 reshape ~firsttime:true state.winw state.winh;
3287 opendoc path origin;
3288 setzoom c.zoom;
3291 let makecheckers () =
3292 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3293 following to say:
3294 converted by Issac Trotts. July 25, 2002 *)
3295 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3296 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3297 let id = GlTex.gen_texture () in
3298 GlTex.bind_texture ~target:`texture_2d id;
3299 GlPix.store (`unpack_alignment 1);
3300 GlTex.image2d image;
3301 List.iter (GlTex.parameter ~target:`texture_2d)
3302 [ `mag_filter `nearest; `min_filter `nearest ];
3306 let setcheckers enabled =
3307 match state.checkerstexid with
3308 | None ->
3309 if enabled then state.checkerstexid <- Some (makecheckers ())
3311 | Some checkerstexid ->
3312 if not enabled
3313 then (
3314 GlTex.delete_texture checkerstexid;
3315 state.checkerstexid <- None;
3319 let describe_layout layout =
3320 let d =
3321 match layout with
3322 | [] -> "Page 0"
3323 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
3324 | l :: rest ->
3325 let rangestr a b =
3326 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
3327 else
3328 let sep = if a.pageno+1 = b.pageno then ", " else UniSyms.ellipsis in
3329 Printf.sprintf "%d%s%d" (a.pageno+1) sep (b.pageno+1)
3331 let rec fold s la lb = function
3332 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
3333 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
3334 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
3336 fold "Pages" l l rest
3338 let percent =
3339 let maxy = maxy () in
3340 if maxy <= 0
3341 then 100.
3342 else 100. *. (float state.y /. float maxy)
3344 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
3347 let setpresentationmode v =
3348 let n = page_of_y state.y in
3349 state.anchor <- (n, 0.0, 1.0);
3350 conf.presentation <- v;
3351 if conf.fitmodel = FitPage
3352 then reqlayout conf.angle conf.fitmodel;
3353 represent ();
3356 let setbgcol (r, g, b) =
3357 let col =
3358 let r = r *. 255.0 |> truncate
3359 and g = g *. 255.0 |> truncate
3360 and b = b *. 255.0 |> truncate in
3361 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3363 Wsi.setwinbgcol col;
3366 let enterinfomode =
3367 let btos b = if b then UniSyms.radical else E.s in
3368 let showextended = ref false in
3369 let showcolors = ref false in
3370 let leave mode _ = state.mode <- mode in
3371 let src =
3372 (object
3373 val mutable m_l = []
3374 val mutable m_a = E.a
3375 val mutable m_prev_uioh = nouioh
3376 val mutable m_prev_mode = View
3378 inherit lvsourcebase
3380 method reset prev_mode prev_uioh =
3381 m_a <- Array.of_list (List.rev m_l);
3382 m_l <- [];
3383 m_prev_mode <- prev_mode;
3384 m_prev_uioh <- prev_uioh;
3386 method int name get set =
3387 m_l <-
3388 (name, `int get, 1,
3389 Action (
3390 fun u ->
3391 let ondone s =
3392 try set (int_of_string s)
3393 with exn ->
3394 state.text <- Printf.sprintf "bad integer `%s': %s"
3395 s @@ exntos exn
3397 state.text <- E.s;
3398 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3399 state.mode <- Textentry (te, leave m_prev_mode);
3401 )) :: m_l
3403 method int_with_suffix name get set =
3404 m_l <-
3405 (name, `intws get, 1,
3406 Action (
3407 fun u ->
3408 let ondone s =
3409 try set (int_of_string_with_suffix s)
3410 with exn ->
3411 state.text <- Printf.sprintf "bad integer `%s': %s"
3412 s @@ exntos exn
3414 state.text <- E.s;
3415 let te =
3416 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3418 state.mode <- Textentry (te, leave m_prev_mode);
3420 )) :: m_l
3422 method bool ?(offset=1) ?(btos=btos) name get set =
3423 m_l <-
3424 (name, `bool (btos, get), offset, Action (
3425 fun u ->
3426 let v = get () in
3427 set (not v);
3429 )) :: m_l
3431 method color name get set =
3432 m_l <-
3433 (name, `color get, 1,
3434 Action (
3435 fun u ->
3436 let invalid = (nan, nan, nan) in
3437 let ondone s =
3438 let c =
3439 try color_of_string s
3440 with exn ->
3441 state.text <- Printf.sprintf "bad color `%s': %s"
3442 s @@ exntos exn;
3443 invalid
3445 if c <> invalid
3446 then set c;
3448 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3449 state.text <- color_to_string (get ());
3450 state.mode <- Textentry (te, leave m_prev_mode);
3452 )) :: m_l
3454 method string name get set =
3455 m_l <-
3456 (name, `string get, 1,
3457 Action (
3458 fun u ->
3459 let ondone s = set s in
3460 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3461 state.mode <- Textentry (te, leave m_prev_mode);
3463 )) :: m_l
3465 method colorspace name get set =
3466 m_l <-
3467 (name, `string get, 1,
3468 Action (
3469 fun _ ->
3470 let source =
3471 (object
3472 inherit lvsourcebase
3474 initializer
3475 m_active <- CSTE.to_int conf.colorspace;
3476 m_first <- 0;
3478 method getitemcount =
3479 Array.length CSTE.names
3480 method getitem n =
3481 (CSTE.names.(n), 0)
3482 method exit ~uioh ~cancel ~active ~first ~pan =
3483 ignore (uioh, first, pan);
3484 if not cancel then set active;
3485 None
3486 method hasaction _ = true
3487 end)
3489 state.text <- E.s;
3490 let modehash = findkeyhash conf "info" in
3491 coe (new listview ~zebra:false ~helpmode:false
3492 ~source ~trusted:true ~modehash)
3493 )) :: m_l
3495 method paxmark name get set =
3496 m_l <-
3497 (name, `string get, 1,
3498 Action (
3499 fun _ ->
3500 let source =
3501 (object
3502 inherit lvsourcebase
3504 initializer
3505 m_active <- MTE.to_int conf.paxmark;
3506 m_first <- 0;
3508 method getitemcount = Array.length MTE.names
3509 method getitem n = (MTE.names.(n), 0)
3510 method exit ~uioh ~cancel ~active ~first ~pan =
3511 ignore (uioh, first, pan);
3512 if not cancel then set active;
3513 None
3514 method hasaction _ = true
3515 end)
3517 state.text <- E.s;
3518 let modehash = findkeyhash conf "info" in
3519 coe (new listview ~zebra:false ~helpmode:false
3520 ~source ~trusted:true ~modehash)
3521 )) :: m_l
3523 method fitmodel name get set =
3524 m_l <-
3525 (name, `string get, 1,
3526 Action (
3527 fun _ ->
3528 let source =
3529 (object
3530 inherit lvsourcebase
3532 initializer
3533 m_active <- FMTE.to_int conf.fitmodel;
3534 m_first <- 0;
3536 method getitemcount = Array.length FMTE.names
3537 method getitem n = (FMTE.names.(n), 0)
3538 method exit ~uioh ~cancel ~active ~first ~pan =
3539 ignore (uioh, first, pan);
3540 if not cancel then set active;
3541 None
3542 method hasaction _ = true
3543 end)
3545 state.text <- E.s;
3546 let modehash = findkeyhash conf "info" in
3547 coe (new listview ~zebra:false ~helpmode:false
3548 ~source ~trusted:true ~modehash)
3549 )) :: m_l
3551 method caption s offset =
3552 m_l <- (s, `empty, offset, Noaction) :: m_l
3554 method caption2 s f offset =
3555 m_l <- (s, `string f, offset, Noaction) :: m_l
3557 method getitemcount = Array.length m_a
3559 method getitem n =
3560 let tostr = function
3561 | `int f -> string_of_int (f ())
3562 | `intws f -> string_with_suffix_of_int (f ())
3563 | `string f -> f ()
3564 | `color f -> color_to_string (f ())
3565 | `bool (btos, f) -> btos (f ())
3566 | `empty -> E.s
3568 let name, t, offset, _ = m_a.(n) in
3569 ((let s = tostr t in
3570 if nonemptystr s
3571 then Printf.sprintf "%s\t%s" name s
3572 else name),
3573 offset)
3575 method exit ~uioh ~cancel ~active ~first ~pan =
3576 let uiohopt =
3577 if not cancel
3578 then (
3579 let uioh =
3580 match m_a.(active) with
3581 | _, _, _, Action f -> f uioh
3582 | _, _, _, Noaction -> uioh
3584 Some uioh
3586 else None
3588 m_active <- active;
3589 m_first <- first;
3590 m_pan <- pan;
3591 uiohopt
3593 method hasaction n =
3594 match m_a.(n) with
3595 | _, _, _, Action _ -> true
3596 | _, _, _, Noaction -> false
3598 initializer m_active <- 1
3599 end)
3601 let rec fillsrc prevmode prevuioh =
3602 let sep () = src#caption E.s 0 in
3603 let colorp name get set =
3604 src#string name
3605 (fun () -> color_to_string (get ()))
3606 (fun v ->
3608 let c = color_of_string v in
3609 set c
3610 with exn ->
3611 state.text <-
3612 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3615 let rgba name get set =
3616 src#string name
3617 (fun () -> rgba_to_string (get ()))
3618 (fun v ->
3620 let c = rgba_of_string v in
3621 set c
3622 with exn ->
3623 state.text <-
3624 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3627 let oldmode = state.mode in
3628 let birdseye = isbirdseye state.mode in
3630 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3632 src#bool "presentation mode"
3633 (fun () -> conf.presentation)
3634 (fun v -> setpresentationmode v);
3636 src#bool "ignore case in searches"
3637 (fun () -> conf.icase)
3638 (fun v -> conf.icase <- v);
3640 src#bool "preload"
3641 (fun () -> conf.preload)
3642 (fun v -> conf.preload <- v);
3644 src#bool "highlight links"
3645 (fun () -> conf.hlinks)
3646 (fun v -> conf.hlinks <- v);
3648 src#bool "under info"
3649 (fun () -> conf.underinfo)
3650 (fun v -> conf.underinfo <- v);
3652 src#bool "persistent bookmarks"
3653 (fun () -> conf.savebmarks)
3654 (fun v -> conf.savebmarks <- v);
3656 src#fitmodel "fit model"
3657 (fun () -> FMTE.to_string conf.fitmodel)
3658 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3660 src#bool "trim margins"
3661 (fun () -> conf.trimmargins)
3662 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3664 src#bool "persistent location"
3665 (fun () -> conf.jumpback)
3666 (fun v -> conf.jumpback <- v);
3668 sep ();
3669 src#int "inter-page space"
3670 (fun () -> conf.interpagespace)
3671 (fun n ->
3672 conf.interpagespace <- n;
3673 docolumns conf.columns;
3674 let pageno, py =
3675 match state.layout with
3676 | [] -> 0, 0
3677 | l :: _ ->
3678 l.pageno, l.pagey
3680 state.maxy <- calcheight ();
3681 let y = getpagey pageno in
3682 gotoxy state.x (y + py)
3685 src#int "page bias"
3686 (fun () -> conf.pagebias)
3687 (fun v -> conf.pagebias <- v);
3689 src#int "scroll step"
3690 (fun () -> conf.scrollstep)
3691 (fun n -> conf.scrollstep <- n);
3693 src#int "horizontal scroll step"
3694 (fun () -> conf.hscrollstep)
3695 (fun v -> conf.hscrollstep <- v);
3697 src#int "auto scroll step"
3698 (fun () ->
3699 match state.autoscroll with
3700 | Some step -> step
3701 | _ -> conf.autoscrollstep)
3702 (fun n ->
3703 let n = boundastep state.winh n in
3704 if state.autoscroll <> None
3705 then state.autoscroll <- Some n;
3706 conf.autoscrollstep <- n);
3708 src#int "zoom"
3709 (fun () -> truncate (conf.zoom *. 100.))
3710 (fun v -> pivotzoom ((float v) /. 100.));
3712 src#int "rotation"
3713 (fun () -> conf.angle)
3714 (fun v -> reqlayout v conf.fitmodel);
3716 src#int "scroll bar width"
3717 (fun () -> conf.scrollbw)
3718 (fun v ->
3719 conf.scrollbw <- v;
3720 reshape state.winw state.winh;
3723 src#int "scroll handle height"
3724 (fun () -> conf.scrollh)
3725 (fun v -> conf.scrollh <- v;);
3727 src#int "thumbnail width"
3728 (fun () -> conf.thumbw)
3729 (fun v ->
3730 conf.thumbw <- min 4096 v;
3731 match oldmode with
3732 | Birdseye beye ->
3733 leavebirdseye beye false;
3734 enterbirdseye ()
3735 | Textentry _
3736 | View
3737 | LinkNav _ -> ()
3740 let mode = state.mode in
3741 src#string "columns"
3742 (fun () ->
3743 match conf.columns with
3744 | Csingle _ -> "1"
3745 | Cmulti (multi, _) -> multicolumns_to_string multi
3746 | Csplit (count, _) -> "-" ^ string_of_int count
3748 (fun v ->
3749 let n, a, b = multicolumns_of_string v in
3750 setcolumns mode n a b);
3752 sep ();
3753 src#caption "Pixmap cache" 0;
3754 src#int_with_suffix "size (advisory)"
3755 (fun () -> conf.memlimit)
3756 (fun v -> conf.memlimit <- v);
3758 src#caption2 "used"
3759 (fun () ->
3760 Printf.sprintf "%s bytes, %d tiles"
3761 (string_with_suffix_of_int state.memused)
3762 (Hashtbl.length state.tilemap)) 1;
3764 sep ();
3765 src#caption "Layout" 0;
3766 src#caption2 "Dimension"
3767 (fun () ->
3768 Printf.sprintf "%dx%d (virtual %dx%d)"
3769 state.winw state.winh
3770 state.w state.maxy)
3772 if conf.debug
3773 then
3774 src#caption2 "Position" (fun () ->
3775 Printf.sprintf "%dx%d" state.x state.y
3777 else
3778 src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
3780 sep ();
3781 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3782 "Save these parameters as global defaults at exit"
3783 (fun () -> conf.bedefault)
3784 (fun v -> conf.bedefault <- v);
3786 sep ();
3787 let btos b = if b then UniSyms.lguillemet else UniSyms.rguillemet in
3788 src#bool ~offset:0 ~btos "Extended parameters"
3789 (fun () -> !showextended)
3790 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3791 if !showextended
3792 then (
3793 src#bool "checkers"
3794 (fun () -> conf.checkers)
3795 (fun v -> conf.checkers <- v; setcheckers v);
3796 src#bool "update cursor"
3797 (fun () -> conf.updatecurs)
3798 (fun v -> conf.updatecurs <- v);
3799 src#bool "scroll-bar on the left"
3800 (fun () -> conf.leftscroll)
3801 (fun v -> conf.leftscroll <- v);
3802 src#bool "verbose"
3803 (fun () -> conf.verbose)
3804 (fun v -> conf.verbose <- v);
3805 src#bool "invert colors"
3806 (fun () -> conf.invert)
3807 (fun v -> conf.invert <- v);
3808 src#bool "max fit"
3809 (fun () -> conf.maxhfit)
3810 (fun v -> conf.maxhfit <- v);
3811 src#bool "pax mode"
3812 (fun () -> conf.pax != None)
3813 (fun v ->
3814 if v
3815 then conf.pax <- Some (now ())
3816 else conf.pax <- None);
3817 src#string "uri launcher"
3818 (fun () -> conf.urilauncher)
3819 (fun v -> conf.urilauncher <- v);
3820 src#string "path launcher"
3821 (fun () -> conf.pathlauncher)
3822 (fun v -> conf.pathlauncher <- v);
3823 src#string "tile size"
3824 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3825 (fun v ->
3827 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3828 conf.tilew <- max 64 w;
3829 conf.tileh <- max 64 h;
3830 flushtiles ();
3831 with exn ->
3832 state.text <- Printf.sprintf "bad tile size `%s': %s"
3833 v @@ exntos exn
3835 src#int "texture count"
3836 (fun () -> conf.texcount)
3837 (fun v ->
3838 if realloctexts v
3839 then conf.texcount <- v
3840 else impmsg "failed to set texture count please retry later"
3842 src#int "slice height"
3843 (fun () -> conf.sliceheight)
3844 (fun v ->
3845 conf.sliceheight <- v;
3846 wcmd "sliceh %d" conf.sliceheight;
3848 src#int "anti-aliasing level"
3849 (fun () -> conf.aalevel)
3850 (fun v ->
3851 conf.aalevel <- bound v 0 8;
3852 state.anchor <- getanchor ();
3853 opendoc state.path state.password;
3855 src#string "page scroll scaling factor"
3856 (fun () -> string_of_float conf.pgscale)
3857 (fun v ->
3859 let s = float_of_string v in
3860 conf.pgscale <- s
3861 with exn ->
3862 state.text <- Printf.sprintf
3863 "bad page scroll scaling factor `%s': %s" v
3864 @@ exntos exn
3867 src#int "ui font size"
3868 (fun () -> fstate.fontsize)
3869 (fun v -> setfontsize (bound v 5 100));
3870 src#int "hint font size"
3871 (fun () -> conf.hfsize)
3872 (fun v -> conf.hfsize <- bound v 5 100);
3873 src#bool "crop hack"
3874 (fun () -> conf.crophack)
3875 (fun v -> conf.crophack <- v);
3876 src#string "trim fuzz"
3877 (fun () -> irect_to_string conf.trimfuzz)
3878 (fun v ->
3880 conf.trimfuzz <- irect_of_string v;
3881 if conf.trimmargins
3882 then settrim true conf.trimfuzz;
3883 with exn ->
3884 state.text <- Printf.sprintf "bad irect `%s': %s" v
3885 @@ exntos exn
3887 src#string "throttle"
3888 (fun () ->
3889 match conf.maxwait with
3890 | None -> "show place holder if page is not ready"
3891 | Some time ->
3892 if time = infinity
3893 then "wait for page to fully render"
3894 else
3895 "wait " ^ string_of_float time
3896 ^ " seconds before showing placeholder"
3898 (fun v ->
3900 let f = float_of_string v in
3901 if f <= 0.0
3902 then conf.maxwait <- None
3903 else conf.maxwait <- Some f
3904 with exn ->
3905 state.text <- Printf.sprintf "bad time `%s': %s" v
3906 @@ exntos exn
3908 src#string "ghyll scroll"
3909 (fun () ->
3910 match conf.ghyllscroll with
3911 | None -> E.s
3912 | Some nab -> ghyllscroll_to_string nab
3914 (fun v ->
3915 try conf.ghyllscroll <- ghyllscroll_of_string v
3916 with
3917 | Failure msg ->
3918 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3919 | exn ->
3920 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3921 @@ exntos exn
3923 src#string "selection command"
3924 (fun () -> conf.selcmd)
3925 (fun v -> conf.selcmd <- v);
3926 src#string "synctex command"
3927 (fun () -> conf.stcmd)
3928 (fun v -> conf.stcmd <- v);
3929 src#string "pax command"
3930 (fun () -> conf.paxcmd)
3931 (fun v -> conf.paxcmd <- v);
3932 src#string "ask password command"
3933 (fun () -> conf.passcmd)
3934 (fun v -> conf.passcmd <- v);
3935 src#string "save path command"
3936 (fun () -> conf.savecmd)
3937 (fun v -> conf.savecmd <- v);
3938 src#colorspace "color space"
3939 (fun () -> CSTE.to_string conf.colorspace)
3940 (fun v ->
3941 conf.colorspace <- CSTE.of_int v;
3942 wcmd "cs %d" v;
3943 load state.layout;
3945 src#paxmark "pax mark method"
3946 (fun () -> MTE.to_string conf.paxmark)
3947 (fun v -> conf.paxmark <- MTE.of_int v);
3948 if bousable () && !opengl_has_pbo
3949 then
3950 src#bool "use PBO"
3951 (fun () -> conf.usepbo)
3952 (fun v -> conf.usepbo <- v);
3953 src#bool "mouse wheel scrolls pages"
3954 (fun () -> conf.wheelbypage)
3955 (fun v -> conf.wheelbypage <- v);
3956 src#bool "open remote links in a new instance"
3957 (fun () -> conf.riani)
3958 (fun v -> conf.riani <- v);
3959 src#bool "edit annotations inline"
3960 (fun () -> conf.annotinline)
3961 (fun v -> conf.annotinline <- v);
3962 src#bool "coarse positioning in presentation mode"
3963 (fun () -> conf.coarseprespos)
3964 (fun v -> conf.coarseprespos <- v);
3965 src#bool "use document CSS"
3966 (fun () -> conf.usedoccss)
3967 (fun v ->
3968 conf.usedoccss <- v;
3969 state.anchor <- getanchor ();
3970 opendoc state.path state.password;
3972 src#bool ~btos "colors"
3973 (fun () -> !showcolors)
3974 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
3975 if !showcolors
3976 then (
3977 colorp " background"
3978 (fun () -> conf.bgcolor)
3979 (fun v -> conf.bgcolor <- v; setbgcol v);
3980 rgba " scrollbar"
3981 (fun () -> conf.sbarcolor)
3982 (fun v -> conf.sbarcolor <- v);
3983 rgba " scrollbar handle"
3984 (fun () -> conf.sbarhndlcolor)
3985 (fun v -> conf.sbarhndlcolor <- v);
3989 sep ();
3990 src#caption "Document" 0;
3991 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3992 src#caption2 "Pages"
3993 (fun () -> string_of_int state.pagecount) 1;
3994 src#caption2 "Dimensions"
3995 (fun () -> string_of_int (List.length state.pdims)) 1;
3996 if nonemptystr conf.css
3997 then src#caption2 "CSS" (fun () -> conf.css) 1;
3998 if conf.trimmargins
3999 then (
4000 sep ();
4001 src#caption "Trimmed margins" 0;
4002 src#caption2 "Dimensions"
4003 (fun () -> string_of_int (List.length state.pdims)) 1;
4006 sep ();
4007 src#caption "OpenGL" 0;
4008 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4009 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4011 sep ();
4012 src#caption "Location" 0;
4013 if nonemptystr state.origin
4014 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4015 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4017 src#reset prevmode prevuioh;
4019 fun () ->
4020 state.text <- E.s;
4021 resetmstate ();
4022 let prevmode = state.mode
4023 and prevuioh = state.uioh in
4024 fillsrc prevmode prevuioh;
4025 let source = (src :> lvsource) in
4026 let modehash = findkeyhash conf "info" in
4027 state.uioh <-
4028 coe (object (self)
4029 inherit listview ~zebra:false ~helpmode:false
4030 ~source ~trusted:true ~modehash as super
4031 val mutable m_prevmemused = 0
4032 method! infochanged = function
4033 | Memused ->
4034 if m_prevmemused != state.memused
4035 then (
4036 m_prevmemused <- state.memused;
4037 G.postRedisplay "memusedchanged";
4039 | Pdim -> G.postRedisplay "pdimchanged"
4040 | Docinfo -> fillsrc prevmode prevuioh
4042 method! key key mask =
4043 if not (Wsi.withctrl mask)
4044 then
4045 match [@warning "-4"] Wsi.kc2kt key with
4046 | Keys.Left -> coe (self#updownlevel ~-1)
4047 | Keys.Right -> coe (self#updownlevel 1)
4048 | _ -> super#key key mask
4049 else super#key key mask
4050 end);
4051 G.postRedisplay "info";
4054 let enterhelpmode =
4055 let source =
4056 (object
4057 inherit lvsourcebase
4058 method getitemcount = Array.length state.help
4059 method getitem n =
4060 let s, l, _ = state.help.(n) in
4061 (s, l)
4063 method exit ~uioh ~cancel ~active ~first ~pan =
4064 let optuioh =
4065 if not cancel
4066 then (
4067 match state.help.(active) with
4068 | _, _, Action f -> Some (f uioh)
4069 | _, _, Noaction -> Some uioh
4071 else None
4073 m_active <- active;
4074 m_first <- first;
4075 m_pan <- pan;
4076 optuioh
4078 method hasaction n =
4079 match state.help.(n) with
4080 | _, _, Action _ -> true
4081 | _, _, Noaction -> false
4083 initializer
4084 m_active <- -1
4085 end)
4086 in fun () ->
4087 let modehash = findkeyhash conf "help" in
4088 resetmstate ();
4089 state.uioh <- coe (new listview
4090 ~zebra:false ~helpmode:true
4091 ~source ~trusted:true ~modehash);
4092 G.postRedisplay "help";
4095 let entermsgsmode =
4096 let msgsource =
4097 (object
4098 inherit lvsourcebase
4099 val mutable m_items = E.a
4101 method getitemcount = 1 + Array.length m_items
4103 method getitem n =
4104 if n = 0
4105 then "[Clear]", 0
4106 else m_items.(n-1), 0
4108 method exit ~uioh ~cancel ~active ~first ~pan =
4109 ignore uioh;
4110 if not cancel
4111 then (
4112 if active = 0
4113 then Buffer.clear state.errmsgs;
4115 m_active <- active;
4116 m_first <- first;
4117 m_pan <- pan;
4118 None
4120 method hasaction n =
4121 n = 0
4123 method reset =
4124 state.newerrmsgs <- false;
4125 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4126 m_items <- Array.of_list l
4128 initializer
4129 m_active <- 0
4130 end)
4131 in fun () ->
4132 state.text <- E.s;
4133 resetmstate ();
4134 msgsource#reset;
4135 let source = (msgsource :> lvsource) in
4136 let modehash = findkeyhash conf "listview" in
4137 state.uioh <-
4138 coe (object
4139 inherit listview ~zebra:false ~helpmode:false
4140 ~source ~trusted:false ~modehash as super
4141 method! display =
4142 if state.newerrmsgs
4143 then msgsource#reset;
4144 super#display
4145 end);
4146 G.postRedisplay "msgs";
4149 let getusertext s =
4150 let editor = getenvwithdef "EDITOR" E.s in
4151 if emptystr editor
4152 then E.s
4153 else
4154 let tmppath = Filename.temp_file "llpp" "note" in
4155 if nonemptystr s
4156 then (
4157 let oc = open_out tmppath in
4158 output_string oc s;
4159 close_out oc;
4161 let execstr = editor ^ " " ^ tmppath in
4162 let s =
4163 match spawn execstr [] with
4164 | exception exn ->
4165 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4167 | pid ->
4168 match Unix.waitpid [] pid with
4169 | exception exn ->
4170 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4172 | (_pid, status) ->
4173 match status with
4174 | Unix.WEXITED 0 -> filecontents tmppath
4175 | Unix.WEXITED n ->
4176 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4178 | Unix.WSIGNALED n ->
4179 impmsg "editor process(%s) was killed by signal %d" execstr n;
4181 | Unix.WSTOPPED n ->
4182 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4185 match Unix.unlink tmppath with
4186 | exception exn ->
4187 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4189 | () -> s
4192 let enterannotmode opaque slinkindex =
4193 let msgsource =
4194 (object
4195 inherit lvsourcebase
4196 val mutable m_text = E.s
4197 val mutable m_items = E.a
4199 method getitemcount = Array.length m_items
4201 method getitem n =
4202 let label, _func = m_items.(n) in
4203 label, 0
4205 method exit ~uioh ~cancel ~active ~first ~pan =
4206 ignore (uioh, first, pan);
4207 if not cancel
4208 then (
4209 let _label, func = m_items.(active) in
4210 func ()
4212 None
4214 method hasaction n = nonemptystr @@ fst m_items.(n)
4216 method reset s =
4217 let rec split accu b i =
4218 let p = b+i in
4219 if p = String.length s
4220 then (String.sub s b (p-b), unit) :: accu
4221 else
4222 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4223 then
4224 let ss = if i = 0 then E.s else String.sub s b i in
4225 split ((ss, unit)::accu) (p+1) 0
4226 else
4227 split accu b (i+1)
4229 let cleanup () =
4230 wcmd "freepage %s" (~> opaque);
4231 let keys =
4232 Hashtbl.fold (fun key opaque' accu ->
4233 if opaque' = opaque'
4234 then key :: accu else accu) state.pagemap []
4236 List.iter (Hashtbl.remove state.pagemap) keys;
4237 flushtiles ();
4238 gotoxy state.x state.y
4240 let dele () =
4241 delannot opaque slinkindex;
4242 cleanup ();
4244 let edit inline () =
4245 let update s =
4246 if emptystr s
4247 then dele ()
4248 else (
4249 modannot opaque slinkindex s;
4250 cleanup ();
4253 if inline
4254 then
4255 let mode = state.mode in
4256 state.mode <-
4257 Textentry (
4258 ("annotation: ", m_text, None, textentry, update, true),
4259 fun _ -> state.mode <- mode);
4260 state.text <- E.s;
4261 enttext ();
4262 else
4263 let s = getusertext m_text in
4264 update s
4266 m_text <- s;
4267 m_items <-
4268 ( "[Copy]", fun () -> selstring m_text)
4269 :: ("[Delete]", dele)
4270 :: ("[Edit]", edit conf.annotinline)
4271 :: (E.s, unit)
4272 :: split [] 0 0 |> List.rev |> Array.of_list
4274 initializer
4275 m_active <- 0
4276 end)
4278 state.text <- E.s;
4279 let s = getannotcontents opaque slinkindex in
4280 resetmstate ();
4281 msgsource#reset s;
4282 let source = (msgsource :> lvsource) in
4283 let modehash = findkeyhash conf "listview" in
4284 state.uioh <- coe (object
4285 inherit listview ~zebra:false ~helpmode:false
4286 ~source ~trusted:false ~modehash
4287 end);
4288 G.postRedisplay "enterannotmode";
4291 let gotoremote spec =
4292 let filename, dest = splitatchar spec '#' in
4293 let getpath filename =
4294 let path =
4295 if nonemptystr filename
4296 then
4297 if Filename.is_relative filename
4298 then
4299 let dir = Filename.dirname state.path in
4300 let dir =
4301 if Filename.is_implicit dir
4302 then Filename.concat (Sys.getcwd ()) dir
4303 else dir
4305 Filename.concat dir filename
4306 else filename
4307 else E.s
4309 if Sys.file_exists path
4310 then path
4311 else E.s
4313 let path = getpath filename in
4314 let dospawn lcmd =
4315 if conf.riani
4316 then
4317 let cmd = Lazy.force_val lcmd in
4318 match spawn cmd with
4319 | _pid -> ()
4320 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
4321 else
4322 let anchor = getanchor () in
4323 let ranchor = state.path, state.password, anchor, state.origin in
4324 state.origin <- E.s;
4325 state.ranchors <- ranchor :: state.ranchors;
4326 opendoc path E.s;
4328 if substratis spec 0 "page="
4329 then
4330 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4331 | pageno ->
4332 state.anchor <- (pageno, 0.0, 0.0);
4333 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4334 | exception exn ->
4335 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4336 else (
4337 state.nameddest <- dest;
4338 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4342 let gotounder = function
4343 | Ulinkuri s when isexternallink s ->
4344 if substratis s 0 "file://"
4345 then gotoremote @@ String.sub s 7 (String.length s - 7)
4346 else gotouri s
4347 | Ulinkuri s ->
4348 let pageno, x, y = uritolocation s in
4349 addnav ();
4350 gotopagexy !wtmode pageno x y
4351 | Utext _ | Unone -> ()
4352 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4355 let gotooutline (_, _, kind) =
4356 match kind with
4357 | Onone -> ()
4358 | Oanchor anchor ->
4359 let (pageno, y, _) = anchor in
4360 let y = getanchory
4361 (if conf.presentation then (pageno, y, 1.0) else anchor)
4363 addnav ();
4364 gotoghyll y
4365 | Ouri uri -> gotounder (Ulinkuri uri)
4366 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4367 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4368 | Ohistory hist -> gotohist hist
4369 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4372 class outlinesoucebase fetchoutlines = object (self)
4373 inherit lvsourcebase
4374 val mutable m_items = E.a
4375 val mutable m_minfo = E.a
4376 val mutable m_orig_items = E.a
4377 val mutable m_orig_minfo = E.a
4378 val mutable m_narrow_patterns = []
4379 val mutable m_gen = -1
4381 method getitemcount = Array.length m_items
4383 method getitem n =
4384 let s, n, _ = m_items.(n) in
4385 (s, n+0)
4387 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4388 ignore (uioh, first);
4389 let items, minfo =
4390 if m_narrow_patterns = []
4391 then m_orig_items, m_orig_minfo
4392 else m_items, m_minfo
4394 m_pan <- pan;
4395 if not cancel
4396 then (
4397 m_items <- items;
4398 m_minfo <- minfo;
4399 gotooutline m_items.(active);
4401 else (
4402 m_items <- items;
4403 m_minfo <- minfo;
4405 None
4407 method hasaction (_:int) = true
4409 method greetmsg =
4410 if Array.length m_items != Array.length m_orig_items
4411 then
4412 let s =
4413 match m_narrow_patterns with
4414 | one :: [] -> one
4415 | many -> String.concat UniSyms.ellipsis (List.rev many)
4417 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4418 else E.s
4420 method statestr =
4421 match m_narrow_patterns with
4422 | [] -> E.s
4423 | one :: [] -> one
4424 | head :: _ -> UniSyms.ellipsis ^ head
4426 method narrow pattern =
4427 match Str.regexp_case_fold pattern with
4428 | exception _ -> ()
4429 | re ->
4430 let rec loop accu minfo n =
4431 if n = -1
4432 then (
4433 m_items <- Array.of_list accu;
4434 m_minfo <- Array.of_list minfo;
4436 else
4437 let (s, _, _) as o = m_items.(n) in
4438 let accu, minfo =
4439 match Str.search_forward re s 0 with
4440 | exception Not_found -> accu, minfo
4441 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4443 loop accu minfo (n-1)
4445 loop [] [] (Array.length m_items - 1)
4447 method! getminfo = m_minfo
4449 method denarrow =
4450 m_orig_items <- fetchoutlines ();
4451 m_minfo <- m_orig_minfo;
4452 m_items <- m_orig_items
4454 method add_narrow_pattern pattern =
4455 m_narrow_patterns <- pattern :: m_narrow_patterns
4457 method del_narrow_pattern =
4458 match m_narrow_patterns with
4459 | _ :: rest -> m_narrow_patterns <- rest
4460 | [] -> ()
4462 method renarrow =
4463 self#denarrow;
4464 match m_narrow_patterns with
4465 | pattern :: [] -> self#narrow pattern; pattern
4466 | list ->
4467 List.fold_left (fun accu pattern ->
4468 self#narrow pattern;
4469 pattern ^ UniSyms.ellipsis ^ accu) E.s list
4471 method calcactive (_:anchor) = 0
4473 method reset anchor items =
4474 if state.gen != m_gen
4475 then (
4476 m_orig_items <- items;
4477 m_items <- items;
4478 m_narrow_patterns <- [];
4479 m_minfo <- E.a;
4480 m_orig_minfo <- E.a;
4481 m_gen <- state.gen;
4483 else (
4484 if items != m_orig_items
4485 then (
4486 m_orig_items <- items;
4487 if m_narrow_patterns == []
4488 then m_items <- items;
4491 let active = self#calcactive anchor in
4492 m_active <- active;
4493 m_first <- firstof m_first active
4497 let outlinesource fetchoutlines =
4498 (object
4499 inherit outlinesoucebase fetchoutlines
4500 method! calcactive anchor =
4501 let rely = getanchory anchor in
4502 let rec loop n best bestd =
4503 if n = Array.length m_items
4504 then best
4505 else
4506 let _, _, kind = m_items.(n) in
4507 match kind with
4508 | Oanchor anchor ->
4509 let orely = getanchory anchor in
4510 let d = abs (orely - rely) in
4511 if d < bestd
4512 then loop (n+1) n d
4513 else loop (n+1) best bestd
4514 | Onone | Oremote _ | Olaunch _
4515 | Oremotedest _ | Ouri _ | Ohistory _ ->
4516 loop (n+1) best bestd
4518 loop 0 ~-1 max_int
4519 end)
4522 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4523 let mkselector sourcetype =
4524 let fetchoutlines () =
4525 match sourcetype with
4526 | `bookmarks -> Array.of_list state.bookmarks
4527 | `outlines -> state.outlines
4528 | `history -> genhistoutlines () |> Array.of_list
4530 let source =
4531 if sourcetype = `history
4532 then new outlinesoucebase fetchoutlines
4533 else outlinesource fetchoutlines
4535 (fun errmsg ->
4536 let outlines = fetchoutlines () in
4537 if Array.length outlines = 0
4538 then showtext ' ' errmsg
4539 else (
4540 resetmstate ();
4541 Wsi.setcursor Wsi.CURSOR_INHERIT;
4542 let anchor = getanchor () in
4543 source#reset anchor outlines;
4544 state.text <- source#greetmsg;
4545 state.uioh <-
4546 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4547 G.postRedisplay "enter selector";
4551 let mkenter sourcetype errmsg =
4552 let enter = mkselector sourcetype in
4553 fun () -> enter errmsg
4555 ( mkenter `outlines "document has no outline"
4556 , mkenter `bookmarks "document has no bookmarks (yet)"
4557 , mkenter `history "history is empty" )
4560 let quickbookmark ?title () =
4561 match state.layout with
4562 | [] -> ()
4563 | l :: _ ->
4564 let title =
4565 match title with
4566 | None ->
4567 Unix.(
4568 let tm = localtime (now ()) in
4569 Printf.sprintf
4570 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
4571 (l.pageno+1)
4572 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
4574 | Some title -> title
4576 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4579 let setautoscrollspeed step goingdown =
4580 let incr = max 1 ((abs step) / 2) in
4581 let incr = if goingdown then incr else -incr in
4582 let astep = boundastep state.winh (step + incr) in
4583 state.autoscroll <- Some astep;
4586 let canpan () =
4587 match conf.columns with
4588 | Csplit _ -> true
4589 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4592 let panbound x = bound x (-state.w) state.winw;;
4594 let existsinrow pageno (columns, coverA, coverB) p =
4595 let last = ((pageno - coverA) mod columns) + columns in
4596 let rec any = function
4597 | [] -> false
4598 | l :: rest ->
4599 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4600 then p l
4601 else (
4602 if not (p l)
4603 then (if l.pageno = last then false else any rest)
4604 else true
4607 any state.layout
4610 let nextpage () =
4611 match state.layout with
4612 | [] ->
4613 let pageno = page_of_y state.y in
4614 gotoghyll (getpagey (pageno+1))
4615 | l :: rest ->
4616 match conf.columns with
4617 | Csingle _ ->
4618 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4619 then
4620 let y = clamp (pgscale state.winh) in
4621 gotoghyll y
4622 else
4623 let pageno = min (l.pageno+1) (state.pagecount-1) in
4624 gotoghyll (getpagey pageno)
4625 | Cmulti ((c, _, _) as cl, _) ->
4626 if conf.presentation
4627 && (existsinrow l.pageno cl
4628 (fun l -> l.pageh > l.pagey + l.pagevh))
4629 then
4630 let y = clamp (pgscale state.winh) in
4631 gotoghyll y
4632 else
4633 let pageno = min (l.pageno+c) (state.pagecount-1) in
4634 gotoghyll (getpagey pageno)
4635 | Csplit (n, _) ->
4636 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4637 then
4638 let pagey, pageh = getpageyh l.pageno in
4639 let pagey = pagey + pageh * l.pagecol in
4640 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4641 gotoghyll (pagey + pageh + ips)
4644 let prevpage () =
4645 match state.layout with
4646 | [] ->
4647 let pageno = page_of_y state.y in
4648 gotoghyll (getpagey (pageno-1))
4649 | l :: _ ->
4650 match conf.columns with
4651 | Csingle _ ->
4652 if conf.presentation && l.pagey != 0
4653 then
4654 gotoghyll (clamp (pgscale ~-(state.winh)))
4655 else
4656 let pageno = max 0 (l.pageno-1) in
4657 gotoghyll (getpagey pageno)
4658 | Cmulti ((c, _, coverB) as cl, _) ->
4659 if conf.presentation &&
4660 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4661 then
4662 gotoghyll (clamp (pgscale ~-(state.winh)))
4663 else
4664 let decr =
4665 if l.pageno = state.pagecount - coverB
4666 then 1
4667 else c
4669 let pageno = max 0 (l.pageno-decr) in
4670 gotoghyll (getpagey pageno)
4671 | Csplit (n, _) ->
4672 let y =
4673 if l.pagecol = 0
4674 then
4675 if l.pageno = 0
4676 then l.pagey
4677 else
4678 let pageno = max 0 (l.pageno-1) in
4679 let pagey, pageh = getpageyh pageno in
4680 pagey + (n-1)*pageh
4681 else
4682 let pagey, pageh = getpageyh l.pageno in
4683 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4685 gotoghyll y
4688 let save () =
4689 if emptystr conf.savecmd
4690 then adderrmsg "savepath-command is empty"
4691 "don't know where to save modified document"
4692 else
4693 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4694 let path =
4695 getcmdoutput
4696 (fun exn ->
4697 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
4698 savecmd
4700 if nonemptystr path
4701 then
4702 let tmp = path ^ ".tmp" in
4703 savedoc tmp;
4704 Unix.rename tmp path;
4707 let viewkeyboard key mask =
4708 let enttext te =
4709 let mode = state.mode in
4710 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4711 state.text <- E.s;
4712 enttext ();
4713 G.postRedisplay "view:enttext"
4715 let ctrl = Wsi.withctrl mask in
4716 let open Keys in
4717 match Wsi.kc2kt key with
4718 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
4720 | Ascii 'Q' -> exit 0
4722 | Ascii 'W' ->
4723 if hasunsavedchanges ()
4724 then save ()
4726 | Insert ->
4727 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4728 then (
4729 state.mode <- (
4730 match state.lnava with
4731 | None -> LinkNav (Ltgendir 0)
4732 | Some pn -> LinkNav (Ltexact pn)
4734 gotoxy state.x state.y;
4736 else impmsg "keyboard link navigation does not work under rotation"
4738 | Escape | Ascii 'q' ->
4739 begin match state.mstate with
4740 | Mzoomrect _ ->
4741 resetmstate ();
4742 G.postRedisplay "kill rect";
4743 | Msel _
4744 | Mpan _
4745 | Mscrolly | Mscrollx
4746 | Mzoom _
4747 | Mnone ->
4748 begin match state.mode with
4749 | LinkNav ln ->
4750 begin match ln with
4751 | Ltexact pl -> state.lnava <- Some pl
4752 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4753 end;
4754 state.mode <- View;
4755 G.postRedisplay "esc leave linknav"
4756 | Birdseye _ | Textentry _ | View ->
4757 match state.ranchors with
4758 | [] -> raise Quit
4759 | (path, password, anchor, origin) :: rest ->
4760 state.ranchors <- rest;
4761 state.anchor <- anchor;
4762 state.origin <- origin;
4763 state.nameddest <- E.s;
4764 opendoc path password
4765 end;
4766 end;
4768 | Backspace ->
4769 gotoghyll (getnav ~-1)
4771 | Ascii 'o' ->
4772 enteroutlinemode ()
4774 | Ascii 'H' ->
4775 enterhistmode ()
4777 | Ascii 'u' ->
4778 state.rects <- [];
4779 state.text <- E.s;
4780 Hashtbl.iter (fun _ opaque ->
4781 clearmark opaque;
4782 Hashtbl.clear state.prects) state.pagemap;
4783 G.postRedisplay "dehighlight";
4785 | Ascii (('/' | '?') as c) ->
4786 let ondone isforw s =
4787 cbput state.hists.pat s;
4788 state.searchpattern <- s;
4789 search s isforw
4791 let s = String.make 1 c in
4792 enttext (s, E.s, Some (onhist state.hists.pat),
4793 textentry, ondone (c = '/'), true)
4795 | Ascii '+' | Ascii '=' when ctrl ->
4796 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4797 pivotzoom (conf.zoom +. incr)
4799 | Ascii '+' ->
4800 let ondone s =
4801 let n =
4802 try int_of_string s with exn ->
4803 state.text <-
4804 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4805 max_int
4807 if n != max_int
4808 then (
4809 conf.pagebias <- n;
4810 state.text <- "page bias is now " ^ string_of_int n;
4813 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4815 | Ascii '-' when ctrl ->
4816 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4817 pivotzoom (max 0.01 (conf.zoom -. decr))
4819 | Ascii '-' ->
4820 let ondone msg = state.text <- msg in
4821 enttext (
4822 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4823 optentry state.mode, ondone, true
4826 | Ascii '0' when ctrl ->
4827 if conf.zoom = 1.0
4828 then gotoxy 0 state.y
4829 else setzoom 1.0
4831 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
4832 let cols =
4833 match conf.columns with
4834 | Csingle _ | Cmulti _ -> 1
4835 | Csplit (n, _) -> n
4837 let h = state.winh -
4838 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4840 let zoom = zoomforh state.winw h 0 cols in
4841 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4842 then setzoom zoom
4844 | Ascii '3' when ctrl ->
4845 let fm =
4846 match conf.fitmodel with
4847 | FitWidth -> FitProportional
4848 | FitProportional -> FitPage
4849 | FitPage -> FitWidth
4851 state.text <- "fit model: " ^ FMTE.to_string fm;
4852 reqlayout conf.angle fm
4854 | Ascii '4' when ctrl ->
4855 let zoom = getmaxw () /. float state.winw in
4856 if zoom > 0.0 then setzoom zoom
4858 | Fn 9 ->
4859 togglebirdseye ()
4861 | Ascii '9' when ctrl ->
4862 togglebirdseye ()
4864 | Ascii ('0'..'9' as c) when not ctrl ->
4865 let ondone s =
4866 let n =
4867 try int_of_string s with exn ->
4868 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4871 if n >= 0
4872 then (
4873 addnav ();
4874 cbput state.hists.pag (string_of_int n);
4875 gotopage1 (n + conf.pagebias - 1) 0;
4878 let [@warning "-4"] pageentry text = function
4879 | Keys.Ascii 'g' -> TEdone text
4880 | key -> intentry text key
4882 let text = String.make 1 c in
4883 enttext (":", text, Some (onhist state.hists.pag),
4884 pageentry, ondone, true)
4886 | Ascii 'b' ->
4887 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4888 G.postRedisplay "toggle scrollbar";
4890 | Ascii 'B' ->
4891 state.bzoom <- not state.bzoom;
4892 state.rects <- [];
4893 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4895 | Ascii 'l' ->
4896 conf.hlinks <- not conf.hlinks;
4897 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4898 G.postRedisplay "toggle highlightlinks";
4900 | Ascii 'F' ->
4901 if conf.angle mod 360 = 0
4902 then (
4903 state.glinks <- true;
4904 let mode = state.mode in
4905 state.mode <-
4906 Textentry (
4907 (":", E.s, None, linknentry, linknact gotounder, false),
4908 (fun _ ->
4909 state.glinks <- false;
4910 state.mode <- mode)
4912 state.text <- E.s;
4913 G.postRedisplay "view:linkent(F)"
4915 else impmsg "hint mode does not work under rotation"
4917 | Ascii 'y' ->
4918 state.glinks <- true;
4919 let mode = state.mode in
4920 state.mode <-
4921 Textentry (
4922 (":", E.s, None, linknentry,
4923 linknact (fun under -> selstring (undertext under)), false),
4924 (fun _ ->
4925 state.glinks <- false;
4926 state.mode <- mode)
4928 state.text <- E.s;
4929 G.postRedisplay "view:linkent"
4931 | Ascii 'a' ->
4932 begin match state.autoscroll with
4933 | Some step ->
4934 conf.autoscrollstep <- step;
4935 state.autoscroll <- None
4936 | None ->
4937 state.autoscroll <- Some conf.autoscrollstep;
4938 state.slideshow <- state.slideshow land lnot 2
4941 | Ascii 'p' when ctrl ->
4942 launchpath () (* XXX where do error messages go? *)
4944 | Ascii 'P' ->
4945 setpresentationmode (not conf.presentation);
4946 showtext ' ' ("presentation mode " ^
4947 if conf.presentation then "on" else "off");
4949 | Ascii 'f' ->
4950 if List.mem Wsi.Fullscreen state.winstate
4951 then Wsi.reshape conf.cwinw conf.cwinh
4952 else Wsi.fullscreen ()
4954 | Ascii ('p'|'N') ->
4955 search state.searchpattern false
4957 | Ascii 'n' | Fn 3 ->
4958 search state.searchpattern true
4960 | Ascii 't' ->
4961 begin match state.layout with
4962 | [] -> ()
4963 | l :: _ ->
4964 gotoghyll (getpagey l.pageno)
4967 | Ascii ' ' ->
4968 nextpage ()
4970 | Delete ->
4971 prevpage ()
4973 | Ascii '=' ->
4974 showtext ' ' (describe_layout state.layout);
4976 | Ascii 'w' ->
4977 begin match state.layout with
4978 | [] -> ()
4979 | l :: _ ->
4980 Wsi.reshape l.pagew l.pageh;
4981 G.postRedisplay "w"
4984 | Ascii '\'' ->
4985 enterbookmarkmode ()
4987 | Ascii 'h' | Fn 1 ->
4988 enterhelpmode ()
4990 | Ascii 'i' ->
4991 enterinfomode ()
4993 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
4994 entermsgsmode ()
4996 | Ascii 'm' ->
4997 let ondone s =
4998 match state.layout with
4999 | l :: _ ->
5000 if nonemptystr s
5001 then
5002 state.bookmarks <-
5003 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5004 | _ -> ()
5006 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5008 | Ascii '~' ->
5009 quickbookmark ();
5010 showtext ' ' "Quick bookmark added";
5012 | Ascii 'z' ->
5013 begin match state.layout with
5014 | l :: _ ->
5015 let rect = getpdimrect l.pagedimno in
5016 let w, h =
5017 if conf.crophack
5018 then
5019 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5020 truncate (1.2 *. (rect.(3) -. rect.(0))))
5021 else
5022 (truncate (rect.(1) -. rect.(0)),
5023 truncate (rect.(3) -. rect.(0)))
5025 let w = truncate ((float w)*.conf.zoom)
5026 and h = truncate ((float h)*.conf.zoom) in
5027 if w != 0 && h != 0
5028 then (
5029 state.anchor <- getanchor ();
5030 Wsi.reshape w (h + conf.interpagespace)
5032 G.postRedisplay "z";
5034 | [] -> ()
5037 | Ascii 'x' -> state.roam ()
5039 | Ascii ('<'|'>' as c) ->
5040 reqlayout
5041 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
5043 | Ascii ('['|']' as c) ->
5044 conf.colorscale <-
5045 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
5046 G.postRedisplay "brightness";
5048 | Ascii 'c' when state.mode = View ->
5049 if Wsi.withalt mask
5050 then (
5051 if conf.zoom > 1.0
5052 then
5053 let m = (state.winw - state.w) / 2 in
5054 gotoxy_and_clear_text m state.y
5056 else
5057 let (c, a, b), z =
5058 match state.prevcolumns with
5059 | None -> (1, 0, 0), 1.0
5060 | Some (columns, z) ->
5061 let cab =
5062 match columns with
5063 | Csplit (c, _) -> -c, 0, 0
5064 | Cmulti ((c, a, b), _) -> c, a, b
5065 | Csingle _ -> 1, 0, 0
5067 cab, z
5069 setcolumns View c a b;
5070 setzoom z
5072 | Down | Up when ctrl && Wsi.withshift mask ->
5073 let zoom, x = state.prevzoom in
5074 setzoom zoom;
5075 state.x <- x;
5077 | Ascii 'k' | Up ->
5078 begin match state.autoscroll with
5079 | None ->
5080 begin match state.mode with
5081 | Birdseye beye -> upbirdseye 1 beye
5082 | Textentry _ | View | LinkNav _ ->
5083 if ctrl
5084 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5085 else (
5086 if not (Wsi.withshift mask) && conf.presentation
5087 then prevpage ()
5088 else gotoghyll1 true (clamp (-conf.scrollstep))
5091 | Some n ->
5092 setautoscrollspeed n false
5095 | Ascii 'j' | Down ->
5096 begin match state.autoscroll with
5097 | None ->
5098 begin match state.mode with
5099 | Birdseye beye -> downbirdseye 1 beye
5100 | Textentry _ | View | LinkNav _ ->
5101 if ctrl
5102 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5103 else (
5104 if not (Wsi.withshift mask) && conf.presentation
5105 then nextpage ()
5106 else gotoghyll1 true (clamp (conf.scrollstep))
5109 | Some n ->
5110 setautoscrollspeed n true
5113 | Left | Right when not (Wsi.withalt mask) ->
5114 if canpan ()
5115 then
5116 let dx =
5117 if ctrl
5118 then state.winw / 2
5119 else conf.hscrollstep
5121 let dx =
5122 let pv = Wsi.kc2kt key in
5123 if pv = Keys.Left then dx else -dx
5125 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5126 else (
5127 state.text <- E.s;
5128 G.postRedisplay "left/right"
5131 | Prior ->
5132 let y =
5133 if ctrl
5134 then
5135 match state.layout with
5136 | [] -> state.y
5137 | l :: _ -> state.y - l.pagey
5138 else
5139 clamp (pgscale (-state.winh))
5141 gotoghyll y
5143 | Next ->
5144 let y =
5145 if ctrl
5146 then
5147 match List.rev state.layout with
5148 | [] -> state.y
5149 | l :: _ -> getpagey l.pageno
5150 else
5151 clamp (pgscale state.winh)
5153 gotoghyll y
5155 | Ascii 'g' | Home ->
5156 addnav ();
5157 gotoghyll 0
5158 | Ascii 'G' | End ->
5159 addnav ();
5160 gotoghyll (clamp state.maxy)
5162 | Right when Wsi.withalt mask ->
5163 gotoghyll (getnav 1)
5164 | Left when Wsi.withalt mask ->
5165 gotoghyll (getnav ~-1)
5167 | Ascii 'r' ->
5168 reload ()
5170 | Ascii 'v' when conf.debug ->
5171 state.rects <- [];
5172 List.iter (fun l ->
5173 match getopaque l.pageno with
5174 | None -> ()
5175 | Some opaque ->
5176 let x0, y0, x1, y1 = pagebbox opaque in
5177 let rect = (float x0, float y0,
5178 float x1, float y0,
5179 float x1, float y1,
5180 float x0, float y1) in
5181 debugrect rect;
5182 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5183 state.rects <- (l.pageno, color, rect) :: state.rects;
5184 ) state.layout;
5185 G.postRedisplay "v";
5187 | Ascii '|' ->
5188 let mode = state.mode in
5189 let cmd = ref E.s in
5190 let onleave = function
5191 | Cancel -> state.mode <- mode
5192 | Confirm ->
5193 List.iter (fun l ->
5194 match getopaque l.pageno with
5195 | Some opaque -> pipesel opaque !cmd
5196 | None -> ()) state.layout;
5197 state.mode <- mode
5199 let ondone s =
5200 cbput state.hists.sel s;
5201 cmd := s
5203 let te =
5204 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5206 G.postRedisplay "|";
5207 state.mode <- Textentry (te, onleave);
5209 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
5210 vlog "huh? %s" (Wsi.keyname key)
5213 let linknavkeyboard key mask linknav =
5214 let pv = Wsi.kc2kt key in
5215 let getpage pageno =
5216 let rec loop = function
5217 | [] -> None
5218 | l :: _ when l.pageno = pageno -> Some l
5219 | _ :: rest -> loop rest
5220 in loop state.layout
5222 let doexact (pageno, n) =
5223 match getopaque pageno, getpage pageno with
5224 | Some opaque, Some l ->
5225 if pv = Keys.Enter
5226 then
5227 let under = getlink opaque n in
5228 G.postRedisplay "link gotounder";
5229 gotounder under;
5230 state.mode <- View;
5231 else
5232 let opt, dir =
5233 let open Keys in
5234 match pv with
5235 | Home -> Some (findlink opaque LDfirst), -1
5236 | End -> Some (findlink opaque LDlast), 1
5237 | Left -> Some (findlink opaque (LDleft n)), -1
5238 | Right -> Some (findlink opaque (LDright n)), 1
5239 | Up -> Some (findlink opaque (LDup n)), -1
5240 | Down -> Some (findlink opaque (LDdown n)), 1
5242 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
5243 | Code _|Fn _|Ctrl _|Backspace -> None, 0
5245 let pwl l dir =
5246 begin match findpwl l.pageno dir with
5247 | Pwlnotfound -> ()
5248 | Pwl pageno ->
5249 let notfound dir =
5250 state.mode <- LinkNav (Ltgendir dir);
5251 let y, h = getpageyh pageno in
5252 let y =
5253 if dir < 0
5254 then y + h - state.winh
5255 else y
5257 gotoxy state.x y
5259 begin match getopaque pageno, getpage pageno with
5260 | Some opaque, Some _ ->
5261 let link =
5262 let ld = if dir > 0 then LDfirst else LDlast in
5263 findlink opaque ld
5265 begin match link with
5266 | Lfound m ->
5267 showlinktype (getlink opaque m);
5268 state.mode <- LinkNav (Ltexact (pageno, m));
5269 G.postRedisplay "linknav jpage";
5270 | Lnotfound -> notfound dir
5271 end;
5272 | _ -> notfound dir
5273 end;
5274 end;
5276 begin match opt with
5277 | Some Lnotfound -> pwl l dir;
5278 | Some (Lfound m) ->
5279 if m = n
5280 then pwl l dir
5281 else (
5282 let _, y0, _, y1 = getlinkrect opaque m in
5283 if y0 < l.pagey
5284 then gotopage1 l.pageno y0
5285 else (
5286 let d = fstate.fontsize + 1 in
5287 if y1 - l.pagey > l.pagevh - d
5288 then gotopage1 l.pageno (y1 - state.winh + d)
5289 else G.postRedisplay "linknav";
5291 showlinktype (getlink opaque m);
5292 state.mode <- LinkNav (Ltexact (l.pageno, m));
5295 | None -> viewkeyboard key mask
5296 end;
5297 | _ -> viewkeyboard key mask
5299 if pv = Keys.Insert
5300 then (
5301 begin match linknav with
5302 | Ltexact pa -> state.lnava <- Some pa
5303 | Ltgendir _ | Ltnotready _ -> ()
5304 end;
5305 state.mode <- View;
5306 G.postRedisplay "leave linknav"
5308 else
5309 match linknav with
5310 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5311 | Ltexact exact -> doexact exact
5314 let keyboard key mask =
5315 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5316 then wcmd "interrupt"
5317 else state.uioh <- state.uioh#key key mask
5320 let birdseyekeyboard key mask
5321 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5322 let incr =
5323 match conf.columns with
5324 | Csingle _ -> 1
5325 | Cmulti ((c, _, _), _) -> c
5326 | Csplit _ -> failwith "bird's eye split mode"
5328 let pgh layout = List.fold_left
5329 (fun m l -> max l.pageh m) state.winh layout in
5330 let open Keys in
5331 match Wsi.kc2kt key with
5332 | Ascii 'l' when Wsi.withctrl mask ->
5333 let y, h = getpageyh pageno in
5334 let top = (state.winh - h) / 2 in
5335 gotoxy state.x (max 0 (y - top))
5336 | Enter -> leavebirdseye beye false
5337 | Escape -> leavebirdseye beye true
5338 | Up -> upbirdseye incr beye
5339 | Down -> downbirdseye incr beye
5340 | Left -> upbirdseye 1 beye
5341 | Right -> downbirdseye 1 beye
5343 | Prior ->
5344 begin match state.layout with
5345 | l :: _ ->
5346 if l.pagey != 0
5347 then (
5348 state.mode <- Birdseye (
5349 oconf, leftx, l.pageno, hooverpageno, anchor
5351 gotopage1 l.pageno 0;
5353 else (
5354 let layout = layout state.x (state.y-state.winh)
5355 state.winw
5356 (pgh state.layout) in
5357 match layout with
5358 | [] -> gotoxy state.x (clamp (-state.winh))
5359 | l :: _ ->
5360 state.mode <- Birdseye (
5361 oconf, leftx, l.pageno, hooverpageno, anchor
5363 gotopage1 l.pageno 0
5366 | [] -> gotoxy state.x (clamp (-state.winh))
5367 end;
5369 | Next ->
5370 begin match List.rev state.layout with
5371 | l :: _ ->
5372 let layout = layout state.x
5373 (state.y + (pgh state.layout))
5374 state.winw state.winh in
5375 begin match layout with
5376 | [] ->
5377 let incr = l.pageh - l.pagevh in
5378 if incr = 0
5379 then (
5380 state.mode <-
5381 Birdseye (
5382 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5384 G.postRedisplay "birdseye pagedown";
5386 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5388 | l :: _ ->
5389 state.mode <-
5390 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5391 gotopage1 l.pageno 0;
5394 | [] -> gotoxy state.x (clamp state.winh)
5395 end;
5397 | Home ->
5398 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5399 gotopage1 0 0
5401 | End ->
5402 let pageno = state.pagecount - 1 in
5403 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5404 if not (pagevisible state.layout pageno)
5405 then
5406 let h =
5407 match List.rev state.pdims with
5408 | [] -> state.winh
5409 | (_, _, h, _) :: _ -> h
5411 gotoxy
5412 state.x
5413 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5414 else G.postRedisplay "birdseye end";
5416 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
5419 let drawpage l =
5420 let color =
5421 match state.mode with
5422 | Textentry _ -> scalecolor 0.4
5423 | LinkNav _ | View -> scalecolor 1.0
5424 | Birdseye (_, _, pageno, hooverpageno, _) ->
5425 if l.pageno = hooverpageno
5426 then scalecolor 0.9
5427 else (
5428 if l.pageno = pageno
5429 then (
5430 let c = scalecolor 1.0 in
5431 GlDraw.color c;
5432 GlDraw.line_width 3.0;
5433 let dispx = l.pagedispx in
5434 linerect
5435 (float (dispx-1)) (float (l.pagedispy-1))
5436 (float (dispx+l.pagevw+1))
5437 (float (l.pagedispy+l.pagevh+1))
5439 GlDraw.line_width 1.0;
5442 else scalecolor 0.8
5445 drawtiles l color;
5448 let postdrawpage l linkindexbase =
5449 match getopaque l.pageno with
5450 | Some opaque ->
5451 if tileready l l.pagex l.pagey
5452 then
5453 let x = l.pagedispx - l.pagex
5454 and y = l.pagedispy - l.pagey in
5455 let hlmask =
5456 match conf.columns with
5457 | Csingle _ | Cmulti _ ->
5458 (if conf.hlinks then 1 else 0)
5459 + (if state.glinks
5460 && not (isbirdseye state.mode) then 2 else 0)
5461 | Csplit _ -> 0
5463 let s =
5464 match state.mode with
5465 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5466 | Textentry _
5467 | Birdseye _
5468 | View
5469 | LinkNav _ -> E.s
5471 Hashtbl.find_all state.prects l.pageno |>
5472 List.iter (fun vals -> drawprect opaque x y vals);
5473 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5474 if n < 0
5475 then (state.redisplay <- true; 0)
5476 else n
5477 else 0
5478 | _ -> 0
5481 let scrollindicator () =
5482 let sbw, ph, sh = state.uioh#scrollph in
5483 let sbh, pw, sw = state.uioh#scrollpw in
5485 let x0,x1,hx0 =
5486 if conf.leftscroll
5487 then (0, sbw, sbw)
5488 else ((state.winw - sbw), state.winw, 0)
5491 Gl.enable `blend;
5492 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5493 let (r, g, b, alpha) = conf.sbarcolor in
5494 GlDraw.color (r, g, b) ~alpha;
5495 filledrect (float x0) 0. (float x1) (float state.winh);
5496 filledrect
5497 (float hx0) (float (state.winh - sbh))
5498 (float (hx0 + state.winw)) (float state.winh);
5499 let (r, g, b, alpha) = conf.sbarhndlcolor in
5500 GlDraw.color (r, g, b) ~alpha;
5502 filledrect (float x0) ph (float x1) (ph +. sh);
5503 let pw = pw +. float hx0 in
5504 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5505 Gl.disable `blend;
5508 let showsel () =
5509 match state.mstate with
5510 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5513 | Msel ((x0, y0), (x1, y1)) ->
5514 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5515 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5516 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5517 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5520 let showrects =
5521 function [] -> ()
5522 | rects ->
5523 Gl.enable `blend;
5524 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5525 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5526 List.iter
5527 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5528 List.iter (fun l ->
5529 if l.pageno = pageno
5530 then (
5531 let dx = float (l.pagedispx - l.pagex) in
5532 let dy = float (l.pagedispy - l.pagey) in
5533 let r, g, b, alpha = c in
5534 GlDraw.color (r, g, b) ~alpha;
5535 filledrect2 (x0+.dx) (y0+.dy)
5536 (x1+.dx) (y1+.dy)
5537 (x3+.dx) (y3+.dy)
5538 (x2+.dx) (y2+.dy);
5540 ) state.layout
5541 ) rects;
5542 Gl.disable `blend;
5545 let display () =
5546 GlDraw.color (scalecolor2 conf.bgcolor);
5547 GlClear.color (scalecolor2 conf.bgcolor);
5548 GlClear.clear [`color];
5549 List.iter drawpage state.layout;
5550 let rects =
5551 match state.mode with
5552 | LinkNav (Ltexact (pageno, linkno)) ->
5553 begin match getopaque pageno with
5554 | Some opaque ->
5555 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5556 let color = (0.0, 0.0, 0.5, 0.5) in
5557 (pageno, color,
5558 (float x0, float y0,
5559 float x1, float y0,
5560 float x1, float y1,
5561 float x0, float y1)
5562 ) :: state.rects
5563 | None -> state.rects
5565 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5566 | Birdseye _
5567 | Textentry _
5568 | View -> state.rects
5570 showrects rects;
5571 let rec postloop linkindexbase = function
5572 | l :: rest ->
5573 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5574 postloop linkindexbase rest
5575 | [] -> ()
5577 showsel ();
5578 postloop 0 state.layout;
5579 state.uioh#display;
5580 begin match state.mstate with
5581 | Mzoomrect ((x0, y0), (x1, y1)) ->
5582 Gl.enable `blend;
5583 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5584 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5585 filledrect (float x0) (float y0) (float x1) (float y1);
5586 Gl.disable `blend;
5587 | Msel _
5588 | Mpan _
5589 | Mscrolly | Mscrollx
5590 | Mzoom _
5591 | Mnone -> ()
5592 end;
5593 enttext ();
5594 scrollindicator ();
5595 Wsi.swapb ();
5598 let zoomrect x y x1 y1 =
5599 let x0 = min x x1
5600 and x1 = max x x1
5601 and y0 = min y y1 in
5602 let zoom = (float state.w) /. float (x1 - x0) in
5603 let margin =
5604 let simple () =
5605 if state.w < state.winw
5606 then (state.winw - state.w) / 2
5607 else 0
5609 match conf.fitmodel with
5610 | FitWidth | FitProportional -> simple ()
5611 | FitPage ->
5612 match conf.columns with
5613 | Csplit _ ->
5614 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5615 | Cmulti _ | Csingle _ -> simple ()
5617 gotoxy ((state.x + margin) - x0) (state.y + y0);
5618 state.anchor <- getanchor ();
5619 setzoom zoom;
5620 resetmstate ();
5623 let annot inline x y =
5624 match unproject x y with
5625 | Some (opaque, n, ux, uy) ->
5626 let add text =
5627 addannot opaque ux uy text;
5628 wcmd "freepage %s" (~> opaque);
5629 Hashtbl.remove state.pagemap (n, state.gen);
5630 flushtiles ();
5631 gotoxy state.x state.y
5633 if inline
5634 then
5635 let ondone s = add s in
5636 let mode = state.mode in
5637 state.mode <- Textentry (
5638 ("annotation: ", E.s, None, textentry, ondone, true),
5639 fun _ -> state.mode <- mode);
5640 state.text <- E.s;
5641 enttext ();
5642 G.postRedisplay "annot"
5643 else
5644 add @@ getusertext E.s
5645 | _ -> ()
5648 let zoomblock x y =
5649 let g opaque l px py =
5650 match rectofblock opaque px py with
5651 | Some a ->
5652 let x0 = a.(0) -. 20. in
5653 let x1 = a.(1) +. 20. in
5654 let y0 = a.(2) -. 20. in
5655 let zoom = (float state.w) /. (x1 -. x0) in
5656 let pagey = getpagey l.pageno in
5657 let margin = (state.w - l.pagew)/2 in
5658 let nx = -truncate x0 - margin in
5659 gotoxy_and_clear_text nx (pagey + truncate y0);
5660 state.anchor <- getanchor ();
5661 setzoom zoom;
5662 None
5663 | None -> None
5665 match conf.columns with
5666 | Csplit _ ->
5667 impmsg "block zooming does not work properly in split columns mode"
5668 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5671 let scrollx x =
5672 let winw = state.winw - 1 in
5673 let s = float x /. float winw in
5674 let destx = truncate (float (state.w + winw) *. s) in
5675 gotoxy_and_clear_text (winw - destx) state.y;
5676 state.mstate <- Mscrollx;
5679 let scrolly y =
5680 let s = float y /. float state.winh in
5681 let desty = truncate (s *. float (maxy ())) in
5682 gotoxy_and_clear_text state.x desty;
5683 state.mstate <- Mscrolly;
5686 let viewmulticlick clicks x y mask =
5687 let g opaque l px py =
5688 let mark =
5689 match clicks with
5690 | 2 -> Mark_word
5691 | 3 -> Mark_line
5692 | 4 -> Mark_block
5693 | _ -> Mark_page
5695 if markunder opaque px py mark
5696 then (
5697 Some (fun () ->
5698 let dopipe cmd =
5699 match getopaque l.pageno with
5700 | None -> ()
5701 | Some opaque -> pipesel opaque cmd
5703 state.roam <- (fun () -> dopipe conf.paxcmd);
5704 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5707 else None
5709 G.postRedisplay "viewmulticlick";
5710 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5713 let canselect () =
5714 match conf.columns with
5715 | Csplit _ -> false
5716 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5719 let viewmouse button down x y mask =
5720 match button with
5721 | n when (n == 4 || n == 5) && not down ->
5722 if Wsi.withctrl mask
5723 then (
5724 let incr =
5725 if n = 5
5726 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5727 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5729 let fx, fy =
5730 match state.mstate with
5731 | Mzoom (oldn, _, pos) when n = oldn -> pos
5732 | Mzoomrect _ | Mnone | Mpan _
5733 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
5735 let zoom = conf.zoom -. incr in
5736 state.mstate <- Mzoom (n, 0, (x, y));
5737 if false && abs (fx - x) > 5 || abs (fy - y) > 5
5738 then pivotzoom ~x ~y zoom
5739 else pivotzoom zoom
5741 else (
5742 match state.autoscroll with
5743 | Some step -> setautoscrollspeed step (n=4)
5744 | None ->
5745 if conf.wheelbypage || conf.presentation
5746 then (
5747 if n = 4
5748 then prevpage ()
5749 else nextpage ()
5751 else
5752 let incr =
5753 if n = 4
5754 then -conf.scrollstep
5755 else conf.scrollstep
5757 let incr = incr * 2 in
5758 let y = clamp incr in
5759 gotoxy_and_clear_text state.x y
5762 | n when (n = 6 || n = 7) && not down && canpan () ->
5763 let x =
5764 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5765 gotoxy_and_clear_text x state.y
5767 | 1 when Wsi.withshift mask ->
5768 state.mstate <- Mnone;
5769 if not down
5770 then (
5771 match unproject x y with
5772 | None -> ()
5773 | Some (_, pageno, ux, uy) ->
5774 let cmd = Printf.sprintf
5775 "%s %s %d %d %d"
5776 conf.stcmd state.path pageno ux uy
5778 match spawn cmd [] with
5779 | exception exn ->
5780 impmsg "execution of synctex command(%S) failed: %S"
5781 conf.stcmd @@ exntos exn
5782 | _pid -> ()
5785 | 1 when Wsi.withctrl mask ->
5786 if down
5787 then (
5788 Wsi.setcursor Wsi.CURSOR_FLEUR;
5789 state.mstate <- Mpan (x, y)
5791 else
5792 state.mstate <- Mnone
5794 | 3 ->
5795 if down
5796 then (
5797 if Wsi.withshift mask
5798 then (
5799 annot conf.annotinline x y;
5800 G.postRedisplay "addannot"
5802 else
5803 let p = (x, y) in
5804 Wsi.setcursor Wsi.CURSOR_CYCLE;
5805 state.mstate <- Mzoomrect (p, p)
5807 else (
5808 match state.mstate with
5809 | Mzoomrect ((x0, y0), _) ->
5810 if abs (x-x0) > 10 && abs (y - y0) > 10
5811 then zoomrect x0 y0 x y
5812 else (
5813 resetmstate ();
5814 G.postRedisplay "kill accidental zoom rect";
5816 | Msel _
5817 | Mpan _
5818 | Mscrolly | Mscrollx
5819 | Mzoom _
5820 | Mnone -> resetmstate ()
5823 | 1 when vscrollhit x ->
5824 if down
5825 then
5826 let _, position, sh = state.uioh#scrollph in
5827 if y > truncate position && y < truncate (position +. sh)
5828 then state.mstate <- Mscrolly
5829 else scrolly y
5830 else
5831 state.mstate <- Mnone
5833 | 1 when y > state.winh - hscrollh () ->
5834 if down
5835 then
5836 let _, position, sw = state.uioh#scrollpw in
5837 if x > truncate position && x < truncate (position +. sw)
5838 then state.mstate <- Mscrollx
5839 else scrollx x
5840 else
5841 state.mstate <- Mnone
5843 | 1 when state.bzoom -> if not down then zoomblock x y
5845 | 1 ->
5846 let dest = if down then getunder x y else Unone in
5847 begin match dest with
5848 | Ulinkuri _ ->
5849 gotounder dest
5851 | Unone when down ->
5852 Wsi.setcursor Wsi.CURSOR_FLEUR;
5853 state.mstate <- Mpan (x, y);
5855 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5857 | Unone | Utext _ ->
5858 if down
5859 then (
5860 if canselect ()
5861 then (
5862 state.mstate <- Msel ((x, y), (x, y));
5863 G.postRedisplay "mouse select";
5866 else (
5867 match state.mstate with
5868 | Mnone -> ()
5870 | Mzoom _ | Mscrollx | Mscrolly ->
5871 state.mstate <- Mnone
5873 | Mzoomrect ((x0, y0), _) ->
5874 zoomrect x0 y0 x y
5876 | Mpan _ ->
5877 Wsi.setcursor Wsi.CURSOR_INHERIT;
5878 state.mstate <- Mnone
5880 | Msel ((x0, y0), (x1, y1)) ->
5881 let rec loop = function
5882 | [] -> ()
5883 | l :: rest ->
5884 let inside =
5885 let a0 = l.pagedispy in
5886 let a1 = a0 + l.pagevh in
5887 let b0 = l.pagedispx in
5888 let b1 = b0 + l.pagevw in
5889 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5890 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5892 if inside
5893 then
5894 match getopaque l.pageno with
5895 | Some opaque ->
5896 let dosel cmd () =
5897 pipef ~closew:false "Msel"
5898 (fun w ->
5899 copysel w opaque;
5900 G.postRedisplay "Msel") cmd
5902 dosel conf.selcmd ();
5903 state.roam <- dosel conf.paxcmd;
5904 | None -> ()
5905 else loop rest
5907 loop state.layout;
5908 resetmstate ();
5912 | _ -> ()
5915 let birdseyemouse button down x y mask
5916 (conf, leftx, _, hooverpageno, anchor) =
5917 match button with
5918 | 1 when down ->
5919 let rec loop = function
5920 | [] -> ()
5921 | l :: rest ->
5922 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5923 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5924 then (
5925 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5927 else loop rest
5929 loop state.layout
5930 | 3 -> ()
5931 | _ -> viewmouse button down x y mask
5934 let uioh = object
5935 method display = ()
5937 method key key mask =
5938 begin match state.mode with
5939 | Textentry textentry -> textentrykeyboard key mask textentry
5940 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5941 | View -> viewkeyboard key mask
5942 | LinkNav linknav -> linknavkeyboard key mask linknav
5943 end;
5944 state.uioh
5946 method button button bstate x y mask =
5947 begin match state.mode with
5948 | LinkNav _ | View -> viewmouse button bstate x y mask
5949 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5950 | Textentry _ -> ()
5951 end;
5952 state.uioh
5954 method multiclick clicks x y mask =
5955 begin match state.mode with
5956 | LinkNav _ | View -> viewmulticlick clicks x y mask
5957 | Birdseye _ | Textentry _ -> ()
5958 end;
5959 state.uioh
5961 method motion x y =
5962 begin match state.mode with
5963 | Textentry _ -> ()
5964 | View | Birdseye _ | LinkNav _ ->
5965 match state.mstate with
5966 | Mzoom _ | Mnone -> ()
5968 | Mpan (x0, y0) ->
5969 let dx = x - x0
5970 and dy = y0 - y in
5971 state.mstate <- Mpan (x, y);
5972 let x = if canpan () then panbound (state.x + dx) else state.x in
5973 let y = clamp dy in
5974 gotoxy_and_clear_text x y
5976 | Msel (a, _) ->
5977 state.mstate <- Msel (a, (x, y));
5978 G.postRedisplay "motion select";
5980 | Mscrolly ->
5981 let y = min state.winh (max 0 y) in
5982 scrolly y
5984 | Mscrollx ->
5985 let x = min state.winw (max 0 x) in
5986 scrollx x
5988 | Mzoomrect (p0, _) ->
5989 state.mstate <- Mzoomrect (p0, (x, y));
5990 G.postRedisplay "motion zoomrect";
5991 end;
5992 state.uioh
5994 method pmotion x y =
5995 begin match state.mode with
5996 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5997 let rec loop = function
5998 | [] ->
5999 if hooverpageno != -1
6000 then (
6001 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6002 G.postRedisplay "pmotion birdseye no hoover";
6004 | l :: rest ->
6005 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6006 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6007 then (
6008 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6009 G.postRedisplay "pmotion birdseye hoover";
6011 else loop rest
6013 loop state.layout
6015 | Textentry _ -> ()
6017 | LinkNav _ | View ->
6018 match state.mstate with
6019 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6020 | Mnone ->
6021 updateunder x y;
6022 if canselect ()
6023 then
6024 match conf.pax with
6025 | None -> ()
6026 | Some past ->
6027 let now = now () in
6028 let delta = now -. past in
6029 if delta > 0.01
6030 then paxunder x y
6031 else conf.pax <- Some now
6032 end;
6033 state.uioh
6035 method infochanged _ = ()
6037 method scrollph =
6038 let maxy = maxy () in
6039 let p, h =
6040 if maxy = 0
6041 then 0.0, float state.winh
6042 else scrollph state.y maxy
6044 vscrollw (), p, h
6046 method scrollpw =
6047 let fwinw = float (state.winw - vscrollw ()) in
6048 let sw =
6049 let sw = fwinw /. float state.w in
6050 let sw = fwinw *. sw in
6051 max sw (float conf.scrollh)
6053 let position =
6054 let maxx = state.w + state.winw in
6055 let x = state.winw - state.x in
6056 let percent = float x /. float maxx in
6057 (fwinw -. sw) *. percent
6059 hscrollh (), position, sw
6061 method modehash =
6062 let modename =
6063 match state.mode with
6064 | LinkNav _ -> "links"
6065 | Textentry _ -> "textentry"
6066 | Birdseye _ -> "birdseye"
6067 | View -> "view"
6069 findkeyhash conf modename
6071 method eformsgs = true
6072 method alwaysscrolly = false
6073 method scroll dx dy =
6074 let x = if canpan () then panbound (state.x + dx) else state.x in
6075 gotoxy_and_clear_text x (clamp (2 * dy));
6076 state.uioh
6077 method zoom z x y =
6078 pivotzoom ~x ~y (conf.zoom *. exp z);
6079 end;;
6081 let addrect pageno r g b a x0 y0 x1 y1 =
6082 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6085 let ract cmds =
6086 let cl = splitatchar cmds ' ' in
6087 let scan s fmt f =
6088 try Scanf.sscanf s fmt f
6089 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
6090 cmds @@ exntos exn
6092 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6093 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6094 s pageno r g b a x0 y0 x1 y1;
6095 onpagerect
6096 pageno
6097 (fun w h ->
6098 let _,w1,h1,_ = getpagedim pageno in
6099 let sw = float w1 /. float w
6100 and sh = float h1 /. float h in
6101 let x0s = x0 *. sw
6102 and x1s = x1 *. sw
6103 and y0s = y0 *. sh
6104 and y1s = y1 *. sh in
6105 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6106 let color = (r, g, b, a) in
6107 if conf.verbose then debugrect rect;
6108 state.rects <- (pageno, color, rect) :: state.rects;
6109 G.postRedisplay s;
6112 match cl with
6113 | "reload", "" -> reload ()
6114 | "goto", args ->
6115 scan args "%u %f %f"
6116 (fun pageno x y ->
6117 let cmd, _ = state.geomcmds in
6118 if emptystr cmd
6119 then gotopagexy !wtmode pageno x y
6120 else
6121 let f prevf () =
6122 gotopagexy !wtmode pageno x y;
6123 prevf ()
6125 state.reprf <- f state.reprf
6127 | "goto1", args -> scan args "%u %f" gotopage
6128 | "gotor", args -> scan args "%S" gotoremote
6129 | "rect", args ->
6130 scan args "%u %u %f %f %f %f"
6131 (fun pageno c x0 y0 x1 y1 ->
6132 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6133 rectx "rect" pageno color x0 y0 x1 y1;
6135 | "prect", args ->
6136 scan args "%u %f %f %f %f %f %f %f %f"
6137 (fun pageno r g b alpha x0 y0 x1 y1 ->
6138 addrect pageno r g b alpha x0 y0 x1 y1;
6139 G.postRedisplay "prect"
6141 | "pgoto", args ->
6142 scan args "%u %f %f"
6143 (fun pageno x y ->
6144 let optopaque =
6145 match getopaque pageno with
6146 | Some opaque -> opaque
6147 | None -> ~< E.s
6149 pgoto optopaque pageno x y;
6150 let rec fixx = function
6151 | [] -> ()
6152 | l :: rest ->
6153 if l.pageno = pageno
6154 then gotoxy (state.x - l.pagedispx) state.y
6155 else fixx rest
6157 let layout =
6158 let mult =
6159 match conf.columns with
6160 | Csingle _ | Csplit _ -> 1
6161 | Cmulti ((n, _, _), _) -> n
6163 layout 0 state.y (state.winw * mult) state.winh
6165 fixx layout
6167 | "activatewin", "" -> Wsi.activatewin ()
6168 | "quit", "" -> raise Quit
6169 | "keys", keys ->
6170 begin try
6171 let l = Config.keys_of_string keys in
6172 List.iter (fun (k, m) -> keyboard k m) l
6173 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
6174 cmds @@ exntos exn
6176 | "clearrects", "" ->
6177 Hashtbl.clear state.prects;
6178 G.postRedisplay "clearrects"
6179 | _ ->
6180 adderrfmt "remote command"
6181 "error processing remote command: %S\n" cmds;
6184 let remote =
6185 let scratch = Bytes.create 80 in
6186 let buf = Buffer.create 80 in
6187 fun fd ->
6188 match tempfailureretry (Unix.read fd scratch 0) 80 with
6189 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6190 | 0 ->
6191 Unix.close fd;
6192 if Buffer.length buf > 0
6193 then (
6194 let s = Buffer.contents buf in
6195 Buffer.clear buf;
6196 ract s;
6198 None
6199 | n ->
6200 let rec eat ppos =
6201 let nlpos =
6202 match Bytes.index_from scratch ppos '\n' with
6203 | pos -> if pos >= n then -1 else pos
6204 | exception Not_found -> -1
6206 if nlpos >= 0
6207 then (
6208 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6209 let s = Buffer.contents buf in
6210 Buffer.clear buf;
6211 ract s;
6212 eat (nlpos+1);
6214 else (
6215 Buffer.add_subbytes buf scratch ppos (n-ppos);
6216 Some fd
6218 in eat 0
6221 let remoteopen path =
6222 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6223 with exn ->
6224 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6225 None
6228 let () =
6229 let gcconfig = ref false in
6230 let trimcachepath = ref E.s in
6231 let rcmdpath = ref E.s in
6232 let pageno = ref None in
6233 let rootwid = ref 0 in
6234 let openlast = ref false in
6235 let doreap = ref false in
6236 let csspath = ref None in
6237 selfexec := Sys.executable_name;
6238 Arg.parse
6239 (Arg.align
6240 [("-p", Arg.String (fun s -> state.password <- s),
6241 "<password> Set password");
6243 ("-f", Arg.String
6244 (fun s ->
6245 Config.fontpath := s;
6246 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6248 "<path> Set path to the user interface font");
6250 ("-c", Arg.String
6251 (fun s ->
6252 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6253 Config.confpath := s),
6254 "<path> Set path to the configuration file");
6256 ("-last", Arg.Set openlast, " Open last document");
6258 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6259 "<page-number> Jump to page");
6261 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6262 "<path> Set path to the trim cache file");
6264 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6265 "<named-destination> Set named destination");
6267 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6268 ("-cxack", Arg.Set cxack, " Cut corners");
6270 ("-remote", Arg.String (fun s -> rcmdpath := s),
6271 "<path> Set path to the source of remote commands");
6273 ("-gc", Arg.Set gcconfig, " Collect config garbage");
6275 ("-v", Arg.Unit (fun () ->
6276 Printf.printf
6277 "%s\nconfiguration path: %s\n"
6278 (version ())
6279 Config.defconfpath;
6280 exit 0), " Print version and exit");
6282 ("-css", Arg.String (fun s -> csspath := Some s),
6283 "<path> Set path to the style sheet to use with EPUB/HTML");
6285 ("-embed", Arg.Set_int rootwid, "<window-id> Embed into window");
6287 ("-origin", Arg.String (fun s -> state.origin <- s),
6288 "<origin> <undocumented>");
6291 (fun s -> state.path <- s)
6292 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6294 if !wtmode
6295 then selfexec := !selfexec ^ " -wtmode";
6297 let histmode = emptystr state.path && not !openlast in
6299 if not (Config.load !openlast)
6300 then dolog "failed to load configuration";
6302 begin match !pageno with
6303 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6304 | None -> ()
6305 end;
6307 if !gcconfig
6308 then (
6309 Config.gc ();
6310 exit 0
6313 let mu =
6314 object (self)
6315 val mutable m_clicks = 0
6316 val mutable m_click_x = 0
6317 val mutable m_click_y = 0
6318 val mutable m_lastclicktime = infinity
6320 method private cleanup =
6321 state.roam <- noroam;
6322 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6323 method expose = G.postRedisplay "expose"
6324 method visible v =
6325 let name =
6326 match v with
6327 | Wsi.Unobscured -> "unobscured"
6328 | Wsi.PartiallyObscured -> "partiallyobscured"
6329 | Wsi.FullyObscured -> "fullyobscured"
6331 vlog "visibility change %s" name
6332 method display = display ()
6333 method map mapped = vlog "mapped %b" mapped
6334 method reshape w h =
6335 self#cleanup;
6336 reshape w h
6337 method mouse b d x y m =
6338 if d && canselect ()
6339 then (
6341 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6343 m_click_x <- x;
6344 m_click_y <- y;
6345 if b = 1
6346 then (
6347 let t = now () in
6348 if abs x - m_click_x > 10
6349 || abs y - m_click_y > 10
6350 || abs_float (t -. m_lastclicktime) > 0.3
6351 then m_clicks <- 0;
6352 m_clicks <- m_clicks + 1;
6353 m_lastclicktime <- t;
6354 if m_clicks = 1
6355 then (
6356 self#cleanup;
6357 G.postRedisplay "cleanup";
6358 state.uioh <- state.uioh#button b d x y m;
6360 else state.uioh <- state.uioh#multiclick m_clicks x y m
6362 else (
6363 self#cleanup;
6364 m_clicks <- 0;
6365 m_lastclicktime <- infinity;
6366 state.uioh <- state.uioh#button b d x y m
6369 else (
6370 state.uioh <- state.uioh#button b d x y m
6372 method motion x y =
6373 state.mpos <- (x, y);
6374 state.uioh <- state.uioh#motion x y
6375 method pmotion x y =
6376 state.mpos <- (x, y);
6377 state.uioh <- state.uioh#pmotion x y
6378 method key k m =
6379 vlog "k=%#x m=%#x" k m;
6380 let mascm = m land (
6381 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6382 ) in
6383 let keyboard k m =
6384 let x = state.x and y = state.y in
6385 keyboard k m;
6386 if x != state.x || y != state.y then self#cleanup
6388 match state.keystate with
6389 | KSnone ->
6390 let km = k, mascm in
6391 begin
6392 match
6393 let modehash = state.uioh#modehash in
6394 try Hashtbl.find modehash km
6395 with Not_found ->
6396 try Hashtbl.find (findkeyhash conf "global") km
6397 with Not_found -> KMinsrt (k, m)
6398 with
6399 | KMinsrt (k, m) -> keyboard k m
6400 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6401 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6403 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6404 List.iter (fun (k, m) -> keyboard k m) insrt;
6405 state.keystate <- KSnone
6406 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6407 state.keystate <- KSinto (keys, insrt)
6408 | KSinto _ -> state.keystate <- KSnone
6410 method enter x y =
6411 state.mpos <- (x, y);
6412 state.uioh <- state.uioh#pmotion x y
6413 method leave = state.mpos <- (-1, -1)
6414 method winstate wsl = state.winstate <- wsl
6415 method quit : 'a. 'a = raise Quit
6416 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
6417 method zoom z x y = state.uioh#zoom z x y
6418 method opendoc path =
6419 state.mode <- View;
6420 state.uioh <- uioh;
6421 G.postRedisplay "opendoc";
6422 opendoc path state.password
6425 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6427 setbgcol conf.bgcolor;
6428 state.wsfd <- wsfd;
6430 if not @@ List.exists GlMisc.check_extension
6431 [ "GL_ARB_texture_rectangle"
6432 ; "GL_EXT_texture_recangle"
6433 ; "GL_NV_texture_rectangle" ]
6434 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6436 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6437 then (
6438 defconf.sliceheight <- 1024;
6439 defconf.texcount <- 32;
6440 defconf.usepbo <- true;
6443 let cs, ss =
6444 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6445 | exception exn ->
6446 dolog "socketpair failed: %s" @@ exntos exn;
6447 exit 1
6448 | (r, w) ->
6449 cloexec r;
6450 cloexec w;
6451 r, w
6454 setcheckers conf.checkers;
6456 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6458 begin match !csspath with
6459 | None -> ()
6460 | Some "" -> conf.css <- E.s
6461 | Some path ->
6462 let css = filecontents path in
6463 let l = String.length css in
6464 conf.css <-
6465 if substratis css (l-2) "\r\n"
6466 then String.sub css 0 (l-2)
6467 else (if css.[l-1] = '\n'
6468 then String.sub css 0 (l-1)
6469 else css);
6470 end;
6471 init cs (
6472 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6473 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6474 !Config.fontpath, !trimcachepath, !opengl_has_pbo
6476 List.iter GlArray.enable [`texture_coord; `vertex];
6477 state.ss <- ss;
6478 reshape ~firsttime:true winw winh;
6479 state.uioh <- uioh;
6480 if histmode
6481 then (
6482 Wsi.settitle "llpp (history)";
6483 enterhistmode ();
6485 else (
6486 state.text <- "Opening " ^ (mbtoutf8 state.path);
6487 opendoc state.path state.password;
6489 display ();
6490 Wsi.mapwin ();
6491 Wsi.setcursor Wsi.CURSOR_INHERIT;
6492 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6494 let rec reap () =
6495 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6496 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
6497 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
6498 | 0, _ -> ()
6499 | _pid, _status -> reap ()
6501 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6503 let optrfd =
6504 ref (
6505 if nonemptystr !rcmdpath
6506 then remoteopen !rcmdpath
6507 else None
6511 let rec loop deadline =
6512 if !doreap
6513 then (
6514 doreap := false;
6515 reap ()
6517 let r = [state.ss; state.wsfd] in
6518 let r =
6519 match !optrfd with
6520 | None -> r
6521 | Some fd -> fd :: r
6523 if state.redisplay
6524 then (
6525 state.redisplay <- false;
6526 display ();
6528 let timeout =
6529 let now = now () in
6530 if deadline > now
6531 then (
6532 if deadline = infinity
6533 then ~-.1.0
6534 else max 0.0 (deadline -. now)
6536 else 0.0
6538 let r, _, _ =
6539 try Unix.select r [] [] timeout
6540 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6542 begin match r with
6543 | [] ->
6544 state.ghyll None;
6545 let newdeadline =
6546 if state.ghyll == noghyll
6547 then
6548 match state.autoscroll with
6549 | Some step when step != 0 ->
6550 if state.slideshow land 1 = 1
6551 then (
6552 if state.slideshow land 2 = 0
6553 then state.slideshow <- state.slideshow lor 2
6554 else if step < 0 then prevpage () else nextpage ();
6555 deadline +. (float (abs step))
6557 else
6558 let y = state.y + step in
6559 let fy = if conf.maxhfit then state.winh else 0 in
6560 let y =
6561 if y < 0
6562 then state.maxy - fy
6563 else if y >= state.maxy - fy then 0 else y
6565 if state.mode = View
6566 then gotoxy_and_clear_text state.x y
6567 else gotoxy state.x y;
6568 deadline +. 0.01
6569 | _ -> infinity
6570 else deadline +. 0.01
6572 loop newdeadline
6574 | l ->
6575 let rec checkfds = function
6576 | [] -> ()
6577 | fd :: rest when fd = state.ss ->
6578 let cmd = rcmd state.ss in
6579 act cmd;
6580 checkfds rest
6582 | fd :: rest when fd = state.wsfd ->
6583 Wsi.readresp fd;
6584 checkfds rest
6586 | fd :: rest when Some fd = !optrfd ->
6587 begin match remote fd with
6588 | None -> optrfd := remoteopen !rcmdpath;
6589 | opt -> optrfd := opt
6590 end;
6591 checkfds rest
6593 | _ :: rest ->
6594 dolog "select returned unknown descriptor";
6595 checkfds rest
6597 checkfds l;
6598 let newdeadline =
6599 let deadline1 =
6600 if deadline = infinity
6601 then now () +. 0.01
6602 else deadline
6604 match state.autoscroll with
6605 | Some step when step != 0 -> deadline1
6606 | _ -> if state.ghyll == noghyll then infinity else deadline1
6608 loop newdeadline
6609 end;
6611 match loop infinity with
6612 | exception Quit ->
6613 Config.save leavebirdseye;
6614 if hasunsavedchanges ()
6615 then save ()
6616 | _ -> error "umpossible - infinity reached"