More intuitive history navigation
[llpp.git] / main.ml
blobc84288bfbcc0f63101a2dac5344692670cbd7da4
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 getanchor () |> cbput state.hists.nav;
953 let addnavnorc () =
954 getanchor () |> cbput_dont_update_rc state.hists.nav;
957 let getnav dir =
958 let anchor = cbgetc state.hists.nav dir in
959 getanchory anchor;
962 let gotoghyll1 single y =
963 let scroll f n a b =
964 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
965 let snake f a b =
966 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
967 if f < a
968 then s (float f /. float a)
969 else (
970 if f > b
971 then 1.0 -. s ((float (f-b) /. float (n-b)))
972 else 1.0
975 snake f a b
976 and summa n a b =
977 let ins = float a *. 0.5
978 and outs = float (n-b) *. 0.5 in
979 let ones = b - a in
980 ins +. outs +. float ones
982 let rec set nab y sy =
983 let (_N, _A, _B), y =
984 if single
985 then
986 let scl = if y > sy then 2 else -2 in
987 let _N, _, _ = nab in
988 (_N,0,_N), y+conf.scrollstep*scl
989 else nab,y in
990 let sum = summa _N _A _B in
991 let dy = float (y - sy) in
992 state.ghyll <- (
993 let rec gf n y1 o =
994 if n >= _N
995 then state.ghyll <- noghyll
996 else
997 let go n =
998 let s = scroll n _N _A _B in
999 let y1 = y1 +. ((s *. dy) /. sum) in
1000 gotoxy_and_clear_text state.x (truncate y1);
1001 state.ghyll <- gf (n+1) y1;
1003 match o with
1004 | None -> go n
1005 | Some y' when single -> set nab y' state.y
1006 | Some y' -> set (_N/2, 1, 1) y' state.y
1008 gf 0 (float state.y)
1011 match conf.ghyllscroll with
1012 | Some nab when not conf.presentation ->
1013 if state.ghyll == noghyll
1014 then set nab y state.y
1015 else state.ghyll (Some y)
1016 | _ ->
1017 gotoxy_and_clear_text state.x y
1020 let gotoghyll = gotoghyll1 false;;
1022 let gotopage n top =
1023 let y, h = getpageyh n in
1024 let y = y + (truncate (top *. float h)) in
1025 gotoghyll y
1028 let gotopage1 n top =
1029 let y = getpagey n in
1030 let y = y + top in
1031 gotoghyll y
1034 let invalidate ?(redisplay=false) s f =
1035 state.redisplay <- redisplay;
1036 state.layout <- [];
1037 state.pdims <- [];
1038 state.rects <- [];
1039 state.rects1 <- [];
1040 match state.geomcmds with
1041 | ps, [] when emptystr ps ->
1042 f ();
1043 state.geomcmds <- s, [];
1045 | ps, [] ->
1046 state.geomcmds <- ps, [s, f];
1048 | ps, (s', _) :: rest when s' = s ->
1049 state.geomcmds <- ps, ((s, f) :: rest);
1051 | ps, cmds ->
1052 state.geomcmds <- ps, ((s, f) :: cmds);
1055 let flushpages () =
1056 Hashtbl.iter (fun _ opaque ->
1057 wcmd "freepage %s" (~> opaque);
1058 ) state.pagemap;
1059 Hashtbl.clear state.pagemap;
1062 let flushtiles () =
1063 if not (Queue.is_empty state.tilelru)
1064 then (
1065 Queue.iter (fun (k, p, s) ->
1066 wcmd "freetile %s" (~> p);
1067 state.memused <- state.memused - s;
1068 Hashtbl.remove state.tilemap k;
1069 ) state.tilelru;
1070 state.uioh#infochanged Memused;
1071 Queue.clear state.tilelru;
1073 load state.layout;
1076 let stateh h =
1077 let h = truncate (float h*.conf.zoom) in
1078 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1079 h - d
1082 let opendoc path password =
1083 state.path <- path;
1084 state.password <- password;
1085 state.gen <- state.gen + 1;
1086 state.docinfo <- [];
1087 state.outlines <- [||];
1089 flushpages ();
1090 setaalevel conf.aalevel;
1091 let titlepath =
1092 if emptystr state.origin
1093 then path
1094 else state.origin
1096 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1097 wcmd "open %d %d %d %s\000%s\000%s\000"
1098 (btod !wtmode) (btod !cxack) (btod conf.usedoccss)
1099 path password conf.css;
1100 invalidate "reqlayout"
1101 (fun () ->
1102 wcmd "reqlayout %d %d %d %s\000"
1103 conf.angle (FMTE.to_int conf.fitmodel)
1104 (stateh state.winh) state.nameddest
1106 state.help <-
1107 let sl = keystostrlist conf in
1108 let rec loop accu =
1109 function | [] -> accu
1110 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1111 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1114 let reload () =
1115 state.anchor <- getanchor ();
1116 opendoc state.path state.password;
1119 let scalecolor c =
1120 let c = c *. conf.colorscale in
1121 (c, c, c);
1124 let scalecolor2 (r, g, b) =
1125 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1128 let docolumns columns =
1129 match columns with
1130 | Csingle _ ->
1131 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1132 let rec loop pageno pdimno pdim y ph pdims =
1133 if pageno = state.pagecount
1134 then ()
1135 else
1136 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1137 match pdims with
1138 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1139 pdimno+1, pdim, rest
1140 | _ ->
1141 pdimno, pdim, pdims
1143 let x = max 0 (((state.winw - w) / 2) - xoff) in
1144 let y =
1145 y + (if conf.presentation
1146 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1147 else (if pageno = 0 then 0 else conf.interpagespace)
1150 a.(pageno) <- (pdimno, x, y, pdim);
1151 loop (pageno+1) pdimno pdim (y + h) h pdims
1153 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1154 conf.columns <- Csingle a;
1156 | Cmulti ((columns, coverA, coverB), _) ->
1157 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1158 let rec loop pageno pdimno pdim x y rowh pdims =
1159 let rec fixrow m =
1160 if m = pageno then () else
1161 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1162 if h < rowh
1163 then (
1164 let y = y + (rowh - h) / 2 in
1165 a.(m) <- (pdimno, x, y, pdim);
1167 fixrow (m+1)
1169 if pageno = state.pagecount
1170 then fixrow (((pageno - 1) / columns) * columns)
1171 else
1172 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1173 match pdims with
1174 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1175 pdimno+1, pdim, rest
1176 | _ ->
1177 pdimno, pdim, pdims
1179 let x, y, rowh' =
1180 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1181 then (
1182 let x = (state.winw - w) / 2 in
1183 let ips =
1184 if conf.presentation then calcips h else conf.interpagespace in
1185 x, y + ips + rowh, h
1187 else (
1188 if (pageno - coverA) mod columns = 0
1189 then (
1190 let x = max 0 (state.winw - state.w) / 2 in
1191 let y =
1192 if conf.presentation
1193 then
1194 let ips = calcips h in
1195 y + (if pageno = 0 then 0 else calcips rowh + ips)
1196 else
1197 y + (if pageno = 0 then 0 else conf.interpagespace)
1199 x, y + rowh, h
1201 else x, y, max rowh h
1204 let y =
1205 if pageno > 1 && (pageno - coverA) mod columns = 0
1206 then (
1207 let y =
1208 if pageno = columns && conf.presentation
1209 then (
1210 let ips = calcips rowh in
1211 for i = 0 to pred columns
1213 let (pdimno, x, y, pdim) = a.(i) in
1214 a.(i) <- (pdimno, x, y+ips, pdim)
1215 done;
1216 y+ips;
1218 else y
1220 fixrow (pageno - columns);
1223 else y
1225 a.(pageno) <- (pdimno, x, y, pdim);
1226 let x = x + w + xoff*2 + conf.interpagespace in
1227 loop (pageno+1) pdimno pdim x y rowh' pdims
1229 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1230 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1232 | Csplit (c, _) ->
1233 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1234 let rec loop pageno pdimno pdim y pdims =
1235 if pageno = state.pagecount
1236 then ()
1237 else
1238 let pdimno, ((_, w, h, _) as pdim), pdims =
1239 match pdims with
1240 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1241 pdimno+1, pdim, rest
1242 | _ ->
1243 pdimno, pdim, pdims
1245 let cw = w / c in
1246 let rec loop1 n x y =
1247 if n = c then y else (
1248 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1249 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1252 let y = loop1 0 0 y in
1253 loop (pageno+1) pdimno pdim y pdims
1255 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1256 conf.columns <- Csplit (c, a);
1259 let represent () =
1260 docolumns conf.columns;
1261 state.maxy <- calcheight ();
1262 if state.reprf == noreprf
1263 then (
1264 match state.mode with
1265 | Birdseye (_, _, pageno, _, _) ->
1266 let y, h = getpageyh pageno in
1267 let top = (state.winh - h) / 2 in
1268 gotoxy state.x (max 0 (y - top))
1269 | Textentry _ | View | LinkNav _ ->
1270 let y = getanchory state.anchor in
1271 let y = min y (state.maxy - state.winh) in
1272 gotoxy state.x y;
1274 else (
1275 state.reprf ();
1276 state.reprf <- noreprf;
1280 let reshape ?(firsttime=false) w h =
1281 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1282 if not firsttime && nogeomcmds state.geomcmds
1283 then state.anchor <- getanchor ();
1285 state.winw <- w;
1286 let w = truncate (float w *. conf.zoom) in
1287 let w = max w 2 in
1288 state.winh <- h;
1289 setfontsize fstate.fontsize;
1290 GlMat.mode `modelview;
1291 GlMat.load_identity ();
1293 GlMat.mode `projection;
1294 GlMat.load_identity ();
1295 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1296 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1297 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1299 let relx =
1300 if conf.zoom <= 1.0
1301 then 0.0
1302 else float state.x /. float state.w
1304 invalidate ~redisplay:true "geometry"
1305 (fun () ->
1306 state.w <- w;
1307 if not firsttime
1308 then state.x <- truncate (relx *. float w);
1309 let w =
1310 match conf.columns with
1311 | Csingle _ -> w
1312 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1313 | Csplit (c, _) -> w * c
1315 wcmd "geometry %d %d %d"
1316 w (stateh h) (FMTE.to_int conf.fitmodel)
1320 let enttext () =
1321 let len = String.length state.text in
1322 let x0 = if conf.leftscroll then vscrollw () else 0 in
1323 let drawstring s =
1324 let hscrollh =
1325 match state.mode with
1326 | Textentry _ | View | LinkNav _ ->
1327 let h, _, _ = state.uioh#scrollpw in
1329 | Birdseye _ -> 0
1331 let rect x w =
1332 filledrect
1333 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1334 (x+.w) (float (state.winh - hscrollh))
1337 let w = float (state.winw - 1 - vscrollw ()) in
1338 if state.progress >= 0.0 && state.progress < 1.0
1339 then (
1340 GlDraw.color (0.3, 0.3, 0.3);
1341 let w1 = w *. state.progress in
1342 rect (float x0) w1;
1343 GlDraw.color (0.0, 0.0, 0.0);
1344 rect (float x0+.w1) (float x0+.w-.w1)
1346 else (
1347 GlDraw.color (0.0, 0.0, 0.0);
1348 rect (float x0) w;
1351 GlDraw.color (1.0, 1.0, 1.0);
1352 drawstring
1353 fstate.fontsize
1354 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1355 (state.winh - hscrollh - 5) s;
1357 let s =
1358 match state.mode with
1359 | Textentry ((prefix, text, _, _, _, _), _) ->
1360 let s =
1361 if len > 0
1362 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1363 else Printf.sprintf "%s%s_" prefix text
1367 | Birdseye _ | View | LinkNav _ -> state.text
1369 let s =
1370 if state.newerrmsgs
1371 then (
1372 if not (istextentry state.mode) && state.uioh#eformsgs
1373 then
1374 let s1 = "(press 'e' to review error messasges)" in
1375 if nonemptystr s then s ^ " " ^ s1 else s1
1376 else s
1378 else s
1380 if nonemptystr s
1381 then drawstring s
1384 let gctiles () =
1385 let len = Queue.length state.tilelru in
1386 let layout = lazy (
1387 match state.throttle with
1388 | None ->
1389 if conf.preload
1390 then preloadlayout state.x state.y state.winw state.winh
1391 else state.layout
1392 | Some (layout, _, _) ->
1393 layout
1394 ) in
1395 let rec loop qpos =
1396 if state.memused > conf.memlimit
1397 then (
1398 if qpos < len
1399 then
1400 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1401 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1402 let (_, pw, ph, _) = getpagedim n in
1403 if gen = state.gen
1404 && colorspace = conf.colorspace
1405 && angle = conf.angle
1406 && pagew = pw
1407 && pageh = ph
1408 && (
1409 let x = col*conf.tilew
1410 and y = row*conf.tileh in
1411 tilevisible (Lazy.force_val layout) n x y
1413 then Queue.push lruitem state.tilelru
1414 else (
1415 freepbo p;
1416 wcmd "freetile %s" (~> p);
1417 state.memused <- state.memused - s;
1418 state.uioh#infochanged Memused;
1419 Hashtbl.remove state.tilemap k;
1421 loop (qpos+1)
1424 loop 0
1427 let onpagerect pageno f =
1428 let b =
1429 match conf.columns with
1430 | Cmulti (_, b) -> b
1431 | Csingle b -> b
1432 | Csplit (_, b) -> b
1434 if pageno >= 0 && pageno < Array.length b
1435 then
1436 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1437 f w h
1440 let gotopagexy1 wtmode pageno x y =
1441 let _,w1,h1,leftx = getpagedim pageno in
1442 let top = y /. (float h1) in
1443 let left = x /. (float w1) in
1444 let py, w, h = getpageywh pageno in
1445 let wh = state.winh in
1446 let x = left *. (float w) in
1447 let x = leftx + state.x + truncate x in
1448 let sx =
1449 if x < 0 || x >= state.winw
1450 then state.x - x
1451 else state.x
1453 let pdy = truncate (top *. float h) in
1454 let y' = py + pdy in
1455 let dy = y' - state.y in
1456 let sy =
1457 if x != state.x || not (dy > 0 && dy < wh)
1458 then (
1459 if conf.presentation
1460 then
1461 if abs (py - y') > wh
1462 then y'
1463 else py
1464 else y';
1466 else state.y
1468 if state.x != sx || state.y != sy
1469 then (
1470 let x, y =
1471 if wtmode
1472 then (
1473 let ww = state.winw in
1474 let qx = sx / ww
1475 and qy = pdy / wh in
1476 let x = qx * ww
1477 and y = py + qy * wh in
1478 let x = if -x + ww > w1 then -(w1-ww) else x
1479 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1480 let y =
1481 if conf.presentation
1482 then
1483 if abs (py - y') > wh
1484 then y'
1485 else py
1486 else y';
1488 (x, y)
1490 else (sx, sy)
1492 gotoxy_and_clear_text x y;
1494 else gotoxy_and_clear_text state.x state.y;
1497 let gotopagexy wtmode pageno x y =
1498 match state.mode with
1499 | Birdseye _ -> gotopage pageno 0.0
1500 | Textentry _ | View | LinkNav _ -> gotopagexy1 wtmode pageno x y
1503 let getpassword () =
1504 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1505 if emptystr passcmd
1506 then E.s
1507 else getcmdoutput
1508 (fun s ->
1509 impmsg "error getting password: %s" s;
1510 dolog "%s" s) passcmd;
1513 let pgoto opaque pageno x y =
1514 let pdimno = getpdimno pageno in
1515 let x, y = project opaque pageno pdimno x y in
1516 gotopagexy false pageno x y;
1519 let act cmds =
1520 (* dolog "%S" cmds; *)
1521 let spl = splitatchar cmds ' ' in
1522 let scan s fmt f =
1523 try Scanf.sscanf s fmt f
1524 with exn ->
1525 dolog "error processing '%S': %s" cmds @@ exntos exn;
1526 exit 1
1528 let addoutline outline =
1529 match state.currently with
1530 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1531 | Idle -> state.currently <- Outlining [outline]
1532 | Loading _ | Tiling _ ->
1533 dolog "invalid outlining state";
1534 logcurrently state.currently
1536 match spl with
1537 | "clear", "" ->
1538 state.pdims <- [];
1539 state.uioh#infochanged Pdim;
1541 | "clearrects", "" ->
1542 state.rects <- state.rects1;
1543 G.postRedisplay "clearrects";
1545 | "continue", args ->
1546 let n = scan args "%u" (fun n -> n) in
1547 state.pagecount <- n;
1548 begin match state.currently with
1549 | Outlining l ->
1550 state.currently <- Idle;
1551 state.outlines <- Array.of_list (List.rev l)
1552 | Idle | Loading _ | Tiling _ -> ()
1553 end;
1555 let cur, cmds = state.geomcmds in
1556 if emptystr cur
1557 then failwith "umpossible";
1559 begin match List.rev cmds with
1560 | [] ->
1561 state.geomcmds <- E.s, [];
1562 state.throttle <- None;
1563 represent ();
1564 | (s, f) :: rest ->
1565 f ();
1566 state.geomcmds <- s, List.rev rest;
1567 end;
1568 if conf.maxwait = None && not !wtmode
1569 then G.postRedisplay "continue";
1571 | "msg", args ->
1572 showtext ' ' args
1574 | "vmsg", args ->
1575 if conf.verbose
1576 then showtext ' ' args
1578 | "emsg", args ->
1579 Buffer.add_string state.errmsgs args;
1580 state.newerrmsgs <- true;
1581 G.postRedisplay "error message"
1583 | "progress", args ->
1584 let progress, text =
1585 scan args "%f %n"
1586 (fun f pos ->
1587 f, String.sub args pos (String.length args - pos))
1589 state.text <- text;
1590 state.progress <- progress;
1591 G.postRedisplay "progress"
1593 | "firstmatch", args ->
1594 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1595 scan args "%u %d %f %f %f %f %f %f %f %f"
1596 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1597 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1599 let y = (getpagey pageno) + truncate y0 in
1600 let x =
1601 if conf.zoom > 1.0
1602 then state.winw/2
1603 else state.x
1605 addnav ();
1606 gotoxy x y;
1607 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1608 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1610 | "match", args ->
1611 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1612 scan args "%u %d %f %f %f %f %f %f %f %f"
1613 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1614 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1616 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1617 state.rects1 <-
1618 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1620 | "page", args ->
1621 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1622 let pageopaque = ~< pageopaques in
1623 begin match state.currently with
1624 | Loading (l, gen) ->
1625 vlog "page %d took %f sec" l.pageno t;
1626 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1627 begin match state.throttle with
1628 | None ->
1629 let preloadedpages =
1630 if conf.preload
1631 then preloadlayout state.x state.y state.winw state.winh
1632 else state.layout
1634 let evict () =
1635 let set =
1636 List.fold_left (fun s l -> IntSet.add l.pageno s)
1637 IntSet.empty preloadedpages
1639 let evictedpages =
1640 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1641 if not (IntSet.mem pageno set)
1642 then (
1643 wcmd "freepage %s" (~> opaque);
1644 key :: accu
1646 else accu
1647 ) state.pagemap []
1649 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1651 evict ();
1652 state.currently <- Idle;
1653 if gen = state.gen
1654 then (
1655 tilepage l.pageno pageopaque state.layout;
1656 load state.layout;
1657 load preloadedpages;
1658 let visible = pagevisible state.layout l.pageno in
1659 if visible
1660 then (
1661 match state.mode with
1662 | LinkNav (Ltnotready (pageno, dir)) ->
1663 if pageno = l.pageno
1664 then (
1665 let link =
1666 let ld =
1667 if dir = 0
1668 then LDfirstvisible (l.pagex, l.pagey, dir)
1669 else (
1670 if dir > 0 then LDfirst else LDlast
1673 findlink pageopaque ld
1675 match link with
1676 | Lnotfound -> ()
1677 | Lfound n ->
1678 showlinktype (getlink pageopaque n);
1679 state.mode <- LinkNav (Ltexact (l.pageno, n))
1681 | LinkNav (Ltgendir _)
1682 | LinkNav (Ltexact _)
1683 | View
1684 | Birdseye _
1685 | Textentry _ -> ()
1688 if visible && layoutready state.layout
1689 then (
1690 G.postRedisplay "page";
1694 | Some (layout, _, _) ->
1695 state.currently <- Idle;
1696 tilepage l.pageno pageopaque layout;
1697 load state.layout
1698 end;
1700 | Idle | Tiling _ | Outlining _ ->
1701 dolog "Inconsistent loading state";
1702 logcurrently state.currently;
1703 exit 1
1706 | "tile" , args ->
1707 let (x, y, opaques, size, t) =
1708 scan args "%u %u %s %u %f"
1709 (fun x y p size t -> (x, y, p, size, t))
1711 let opaque = ~< opaques in
1712 begin match state.currently with
1713 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1714 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1716 unmappbo opaque;
1717 if tilew != conf.tilew || tileh != conf.tileh
1718 then (
1719 wcmd "freetile %s" (~> opaque);
1720 state.currently <- Idle;
1721 load state.layout;
1723 else (
1724 puttileopaque l col row gen cs angle opaque size t;
1725 state.memused <- state.memused + size;
1726 state.uioh#infochanged Memused;
1727 gctiles ();
1728 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1729 opaque, size) state.tilelru;
1731 let layout =
1732 match state.throttle with
1733 | None -> state.layout
1734 | Some (layout, _, _) -> layout
1737 state.currently <- Idle;
1738 if gen = state.gen
1739 && conf.colorspace = cs
1740 && conf.angle = angle
1741 && tilevisible layout l.pageno x y
1742 then conttiling l.pageno pageopaque;
1744 begin match state.throttle with
1745 | None ->
1746 preload state.layout;
1747 if gen = state.gen
1748 && conf.colorspace = cs
1749 && conf.angle = angle
1750 && tilevisible state.layout l.pageno x y
1751 && (not !wtmode || layoutready state.layout)
1752 then G.postRedisplay "tile nothrottle";
1754 | Some (layout, y, _) ->
1755 let ready = layoutready layout in
1756 if ready
1757 then (
1758 state.y <- y;
1759 state.layout <- layout;
1760 state.throttle <- None;
1761 G.postRedisplay "throttle";
1763 else load layout;
1764 end;
1767 | Idle | Loading _ | Outlining _ ->
1768 dolog "Inconsistent tiling state";
1769 logcurrently state.currently;
1770 exit 1
1773 | "pdim", args ->
1774 let (n, w, h, _) as pdim =
1775 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1777 let pdim =
1778 match conf.fitmodel with
1779 | FitWidth -> pdim
1780 | FitPage | FitProportional ->
1781 match conf.columns with
1782 | Csplit _ -> (n, w, h, 0)
1783 | Csingle _ | Cmulti _ -> pdim
1785 state.pdims <- pdim :: state.pdims;
1786 state.uioh#infochanged Pdim
1788 | "o", args ->
1789 let (l, n, t, h, pos) =
1790 scan args "%u %u %d %u %n"
1791 (fun l n t h pos -> l, n, t, h, pos)
1793 let s = String.sub args pos (String.length args - pos) in
1794 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1796 | "ou", args ->
1797 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1798 let s = String.sub args pos len in
1799 let pos2 = pos + len + 1 in
1800 let uri = String.sub args pos2 (String.length args - pos2) in
1801 addoutline (s, l, Ouri uri)
1803 | "on", args ->
1804 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1805 let s = String.sub args pos (String.length args - pos) in
1806 addoutline (s, l, Onone)
1808 | "a", args ->
1809 let (n, l, t) =
1810 scan args "%u %d %d" (fun n l t -> n, l, t)
1812 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1814 | "info", args ->
1815 let c, v = splitatchar args '\t' in
1816 let s =
1817 if nonemptystr v
1818 then
1819 if c = "Title"
1820 then (
1821 conf.title <- v;
1822 Wsi.settitle v;
1823 args
1825 else
1826 if let len = String.length c in
1827 len > 6 && ((String.sub c (len-4) 4) = "date")
1828 then (
1829 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1830 then
1831 let b = Buffer.create 10 in
1832 Printf.bprintf b "%s\t" c;
1833 let sub p l c =
1835 Buffer.add_substring b v p l;
1836 Buffer.add_char b c;
1837 with exn -> Buffer.add_string b @@ exntos exn
1839 sub 2 4 '/';
1840 sub 6 2 '/';
1841 sub 8 2 ' ';
1842 sub 10 2 ':';
1843 sub 12 2 ':';
1844 sub 14 2 ' ';
1845 Buffer.add_char b '[';
1846 Buffer.add_string b v;
1847 Buffer.add_char b ']';
1848 Buffer.contents b
1849 else args
1851 else args
1852 else args
1854 state.docinfo <- (1, s) :: state.docinfo
1856 | "infoend", "" ->
1857 state.docinfo <- List.rev state.docinfo;
1858 state.uioh#infochanged Docinfo
1860 | "pass", args ->
1861 if args = "fail"
1862 then Wsi.settitle "Wrong password";
1863 let password = getpassword () in
1864 if emptystr password
1865 then error "document is password protected"
1866 else opendoc state.path password
1868 | _ ->
1869 error "unknown cmd `%S'" cmds
1872 let onhist cb =
1873 let rc = cb.rc in
1874 let action = function
1875 | HCprev -> cbget cb ~-1
1876 | HCnext -> cbget cb 1
1877 | HCfirst -> cbget cb ~-(cb.rc)
1878 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1879 and cancel () = cb.rc <- rc
1880 in (action, cancel)
1883 let search pattern forward =
1884 match conf.columns with
1885 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1886 | Csingle _ | Cmulti _ ->
1887 if nonemptystr pattern
1888 then
1889 let pn, py =
1890 match state.layout with
1891 | [] -> 0, 0
1892 | l :: _ ->
1893 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1895 wcmd "search %d %d %d %d,%s\000"
1896 (btod conf.icase) pn py (btod forward) pattern;
1899 let [@warning "-4"] intentry text key =
1900 let text =
1901 if emptystr text && key = Keys.Ascii '-'
1902 then addchar text '-'
1903 else
1904 match key with
1905 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1906 | _ ->
1907 state.text <- "invalid key";
1908 text
1910 TEcont text
1913 let linknact f s =
1914 if nonemptystr s
1915 then (
1916 let n =
1917 let l = String.length s in
1918 let rec loop pos n =
1919 if pos = l
1920 then n
1921 else
1922 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1923 loop (pos+1) (n*26 + m)
1924 in loop 0 0
1926 let rec loop n = function
1927 | [] -> ()
1928 | l :: rest ->
1929 match getopaque l.pageno with
1930 | None -> loop n rest
1931 | Some opaque ->
1932 let m = getlinkcount opaque in
1933 if n < m
1934 then (
1935 let under = getlink opaque n in
1936 f under
1938 else loop (n-m) rest
1940 loop n state.layout;
1944 let [@warning "-4"] linknentry text = function
1945 | Keys.Ascii c ->
1946 let text = addchar text c in
1947 linknact (fun under -> state.text <- undertext under) text;
1948 TEcont text
1949 | _ ->
1950 state.text <- Printf.sprintf "invalid key";
1951 TEcont text
1954 let [@warning "-4"] textentry text = function
1955 | Keys.Ascii c -> TEcont (addchar text c)
1956 | Keys.Code c -> TEcont (text ^ toutf8 c)
1957 | _ -> TEcont text
1960 let reqlayout angle fitmodel =
1961 match state.throttle with
1962 | None ->
1963 if nogeomcmds state.geomcmds
1964 then state.anchor <- getanchor ();
1965 conf.angle <- angle mod 360;
1966 if conf.angle != 0
1967 then (
1968 match state.mode with
1969 | LinkNav _ -> state.mode <- View
1970 | Birdseye _ | Textentry _ | View -> ()
1972 conf.fitmodel <- fitmodel;
1973 invalidate
1974 "reqlayout"
1975 (fun () ->
1976 wcmd "reqlayout %d %d %d"
1977 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1979 | _ -> ()
1982 let settrim trimmargins trimfuzz =
1983 if nogeomcmds state.geomcmds
1984 then state.anchor <- getanchor ();
1985 conf.trimmargins <- trimmargins;
1986 conf.trimfuzz <- trimfuzz;
1987 let x0, y0, x1, y1 = trimfuzz in
1988 invalidate
1989 "settrim" (fun () ->
1990 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1991 flushpages ();
1994 let setzoom zoom =
1995 match state.throttle with
1996 | None ->
1997 let zoom = max 0.0001 zoom in
1998 if zoom <> conf.zoom
1999 then (
2000 state.prevzoom <- (conf.zoom, state.x);
2001 conf.zoom <- zoom;
2002 reshape state.winw state.winh;
2003 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2006 | Some (layout, y, started) ->
2007 let time =
2008 match conf.maxwait with
2009 | None -> 0.0
2010 | Some t -> t
2012 let dt = now () -. started in
2013 if dt > time
2014 then (
2015 state.y <- y;
2016 load layout;
2020 let pivotzoom ?(vw=min state.w state.winw)
2021 ?(vh=min (state.maxy-state.y) state.winh)
2022 ?(x=vw/2) ?(y=vh/2) zoom =
2023 let w = float state.w /. zoom in
2024 let hw = w /. 2.0 in
2025 let ratio = float vh /. float vw in
2026 let hh = hw *. ratio in
2027 let x0 = float x -. hw
2028 and y0 = float y -. hh in
2029 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2030 setzoom zoom;
2033 let pivotzoom ?vw ?vh ?x ?y zoom =
2034 if nogeomcmds state.geomcmds
2035 then
2036 if zoom > 1.0
2037 then pivotzoom ?vw ?vh ?x ?y zoom
2038 else setzoom zoom
2041 let setcolumns mode columns coverA coverB =
2042 state.prevcolumns <- Some (conf.columns, conf.zoom);
2043 if columns < 0
2044 then (
2045 if isbirdseye mode
2046 then impmsg "split mode doesn't work in bird's eye"
2047 else (
2048 conf.columns <- Csplit (-columns, E.a);
2049 state.x <- 0;
2050 conf.zoom <- 1.0;
2053 else (
2054 if columns < 2
2055 then (
2056 conf.columns <- Csingle E.a;
2057 state.x <- 0;
2058 setzoom 1.0;
2060 else (
2061 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2062 conf.zoom <- 1.0;
2065 reshape state.winw state.winh;
2068 let resetmstate () =
2069 state.mstate <- Mnone;
2070 Wsi.setcursor Wsi.CURSOR_INHERIT;
2073 let enterbirdseye () =
2074 let zoom = float conf.thumbw /. float state.winw in
2075 let birdseyepageno =
2076 let cy = state.winh / 2 in
2077 let fold = function
2078 | [] -> 0
2079 | l :: rest ->
2080 let rec fold best = function
2081 | [] -> best.pageno
2082 | l :: rest ->
2083 let d = cy - (l.pagedispy + l.pagevh/2)
2084 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2085 if abs d < abs dbest
2086 then fold l rest
2087 else best.pageno
2088 in fold l rest
2090 fold state.layout
2092 state.mode <-
2093 Birdseye (
2094 { conf with zoom = conf.zoom },
2095 state.x, birdseyepageno, -1, getanchor ()
2097 resetmstate ();
2098 conf.zoom <- zoom;
2099 conf.presentation <- false;
2100 conf.interpagespace <- 10;
2101 conf.hlinks <- false;
2102 conf.fitmodel <- FitPage;
2103 state.x <- 0;
2104 conf.maxwait <- None;
2105 conf.columns <- (
2106 match conf.beyecolumns with
2107 | Some c ->
2108 conf.zoom <- 1.0;
2109 Cmulti ((c, 0, 0), E.a)
2110 | None -> Csingle E.a
2112 if conf.verbose
2113 then
2114 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2115 (100.0*.zoom)
2116 else
2117 state.text <- E.s
2119 reshape state.winw state.winh;
2122 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2123 state.mode <- View;
2124 conf.zoom <- c.zoom;
2125 conf.presentation <- c.presentation;
2126 conf.interpagespace <- c.interpagespace;
2127 conf.maxwait <- c.maxwait;
2128 conf.hlinks <- c.hlinks;
2129 conf.fitmodel <- c.fitmodel;
2130 conf.beyecolumns <- (
2131 match conf.columns with
2132 | Cmulti ((c, _, _), _) -> Some c
2133 | Csingle _ -> None
2134 | Csplit _ -> failwith "leaving bird's eye split mode"
2136 conf.columns <- (
2137 match c.columns with
2138 | Cmulti (c, _) -> Cmulti (c, E.a)
2139 | Csingle _ -> Csingle E.a
2140 | Csplit (c, _) -> Csplit (c, E.a)
2142 if conf.verbose
2143 then
2144 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2145 (100.0*.conf.zoom)
2147 reshape state.winw state.winh;
2148 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2149 state.x <- leftx;
2152 let togglebirdseye () =
2153 match state.mode with
2154 | Birdseye vals -> leavebirdseye vals true
2155 | View -> enterbirdseye ()
2156 | Textentry _ | LinkNav _ -> ()
2159 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2160 let pageno = max 0 (pageno - incr) in
2161 let rec loop = function
2162 | [] -> gotopage1 pageno 0
2163 | l :: _ when l.pageno = pageno ->
2164 if l.pagedispy >= 0 && l.pagey = 0
2165 then G.postRedisplay "upbirdseye"
2166 else gotopage1 pageno 0
2167 | _ :: rest -> loop rest
2169 loop state.layout;
2170 state.text <- E.s;
2171 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2174 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2175 let pageno = min (state.pagecount - 1) (pageno + incr) in
2176 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2177 let rec loop = function
2178 | [] ->
2179 let y, h = getpageyh pageno in
2180 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2181 gotoxy state.x (clamp dy)
2182 | l :: _ when l.pageno = pageno ->
2183 if l.pagevh != l.pageh
2184 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2185 else G.postRedisplay "downbirdseye"
2186 | _ :: rest -> loop rest
2188 loop state.layout;
2189 state.text <- E.s;
2192 let [@warning "-4"] optentry mode _ key =
2193 let btos b = if b then "on" else "off" in
2194 match key with
2195 | Keys.Ascii 's' ->
2196 let ondone s =
2197 try conf.scrollstep <- int_of_string s with exn ->
2198 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2200 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2202 | Keys.Ascii 'A' ->
2203 let ondone s =
2205 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2206 if state.autoscroll <> None
2207 then state.autoscroll <- Some conf.autoscrollstep
2208 with exn ->
2209 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2211 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2213 | Keys.Ascii 'C' ->
2214 let ondone s =
2216 let n, a, b = multicolumns_of_string s in
2217 setcolumns mode n a b;
2218 with exn ->
2219 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2221 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2223 | Keys.Ascii 'Z' ->
2224 let ondone s =
2226 let zoom = float (int_of_string s) /. 100.0 in
2227 pivotzoom zoom
2228 with exn ->
2229 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2231 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2233 | Keys.Ascii 't' ->
2234 let ondone s =
2236 conf.thumbw <- bound (int_of_string s) 2 4096;
2237 state.text <-
2238 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2239 begin match mode with
2240 | Birdseye beye ->
2241 leavebirdseye beye false;
2242 enterbirdseye ();
2243 | Textentry _ | View | LinkNav _ -> ();
2245 with exn ->
2246 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2248 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2250 | Keys.Ascii 'R' ->
2251 let ondone s =
2252 match int_of_string s with
2253 | angle -> reqlayout angle conf.fitmodel
2254 | exception exn ->
2255 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2257 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2259 | Keys.Ascii 'i' ->
2260 conf.icase <- not conf.icase;
2261 TEdone ("case insensitive search " ^ (btos conf.icase))
2263 | Keys.Ascii 'p' ->
2264 conf.preload <- not conf.preload;
2265 gotoxy state.x state.y;
2266 TEdone ("preload " ^ (btos conf.preload))
2268 | Keys.Ascii 'v' ->
2269 conf.verbose <- not conf.verbose;
2270 TEdone ("verbose " ^ (btos conf.verbose))
2272 | Keys.Ascii 'd' ->
2273 conf.debug <- not conf.debug;
2274 TEdone ("debug " ^ (btos conf.debug))
2276 | Keys.Ascii 'h' ->
2277 conf.maxhfit <- not conf.maxhfit;
2278 state.maxy <- calcheight ();
2279 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2281 | Keys.Ascii 'c' ->
2282 conf.crophack <- not conf.crophack;
2283 TEdone ("crophack " ^ btos conf.crophack)
2285 | Keys.Ascii 'a' ->
2286 let s =
2287 match conf.maxwait with
2288 | None ->
2289 conf.maxwait <- Some infinity;
2290 "always wait for page to complete"
2291 | Some _ ->
2292 conf.maxwait <- None;
2293 "show placeholder if page is not ready"
2295 TEdone s
2297 | Keys.Ascii 'f' ->
2298 conf.underinfo <- not conf.underinfo;
2299 TEdone ("underinfo " ^ btos conf.underinfo)
2301 | Keys.Ascii 'P' ->
2302 conf.savebmarks <- not conf.savebmarks;
2303 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2305 | Keys.Ascii 'S' ->
2306 let ondone s =
2308 let pageno, py =
2309 match state.layout with
2310 | [] -> 0, 0
2311 | l :: _ ->
2312 l.pageno, l.pagey
2314 conf.interpagespace <- int_of_string s;
2315 docolumns conf.columns;
2316 state.maxy <- calcheight ();
2317 let y = getpagey pageno in
2318 gotoxy state.x (y + py)
2319 with exn ->
2320 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2322 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2324 | Keys.Ascii 'l' ->
2325 let fm =
2326 match conf.fitmodel with
2327 | FitProportional -> FitWidth
2328 | FitWidth | FitPage -> FitProportional
2330 reqlayout conf.angle fm;
2331 TEdone ("proportional display " ^ btos (fm == FitProportional))
2333 | Keys.Ascii 'T' ->
2334 settrim (not conf.trimmargins) conf.trimfuzz;
2335 TEdone ("trim margins " ^ btos conf.trimmargins)
2337 | Keys.Ascii 'I' ->
2338 conf.invert <- not conf.invert;
2339 TEdone ("invert colors " ^ btos conf.invert)
2341 | Keys.Ascii 'x' ->
2342 let ondone s =
2343 cbput state.hists.sel s;
2344 conf.selcmd <- s;
2346 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2347 textentry, ondone, true)
2349 | Keys.Ascii 'M' ->
2350 if conf.pax == None
2351 then conf.pax <- Some 0.0
2352 else conf.pax <- None;
2353 TEdone ("PAX " ^ btos (conf.pax != None))
2355 | (Keys.Ascii c) ->
2356 state.text <- Printf.sprintf "bad option %d `%c'"
2357 (Char.code c) c;
2358 TEstop
2360 | _ ->
2361 TEcont state.text
2364 class type lvsource =
2365 object
2366 method getitemcount : int
2367 method getitem : int -> (string * int)
2368 method hasaction : int -> bool
2369 method exit : uioh:uioh ->
2370 cancel:bool ->
2371 active:int ->
2372 first:int ->
2373 pan:int ->
2374 uioh option
2375 method getactive : int
2376 method getfirst : int
2377 method getpan : int
2378 method getminfo : (int * int) array
2379 end;;
2381 class virtual lvsourcebase = object
2382 val mutable m_active = 0
2383 val mutable m_first = 0
2384 val mutable m_pan = 0
2385 method getactive = m_active
2386 method getfirst = m_first
2387 method getpan = m_pan
2388 method getminfo : (int * int) array = E.a
2389 end;;
2391 let [@warning "-4"]
2392 textentrykeyboard
2393 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2394 state.text <- E.s;
2395 let enttext te =
2396 state.mode <- Textentry (te, onleave);
2397 enttext ();
2398 G.postRedisplay "textentrykeyboard enttext";
2400 let histaction cmd =
2401 match opthist with
2402 | None -> ()
2403 | Some (action, _) ->
2404 state.mode <-
2405 Textentry (
2406 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2408 G.postRedisplay "textentry histaction"
2410 let open Keys in
2411 let kt = Wsi.kc2kt key in
2412 match kt with
2413 | Backspace ->
2414 if emptystr text && cancelonempty
2415 then (
2416 onleave Cancel;
2417 G.postRedisplay "textentrykeyboard after cancel";
2419 else
2420 let s = withoutlastutf8 text in
2421 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2423 | Enter ->
2424 ondone text;
2425 onleave Confirm;
2426 G.postRedisplay "textentrykeyboard after confirm"
2428 | Up -> histaction HCprev
2429 | Down -> histaction HCnext
2430 | Home -> histaction HCfirst
2431 | End -> histaction HClast
2433 | Escape ->
2434 if emptystr text
2435 then (
2436 begin match opthist with
2437 | None -> ()
2438 | Some (_, onhistcancel) -> onhistcancel ()
2439 end;
2440 onleave Cancel;
2441 state.text <- E.s;
2442 G.postRedisplay "textentrykeyboard after cancel2"
2444 else (
2445 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2448 | Delete -> ()
2450 | Code _ | Ascii _ ->
2451 begin match onkey text kt with
2452 | TEdone text ->
2453 ondone text;
2454 onleave Confirm;
2455 G.postRedisplay "textentrykeyboard after confirm2";
2457 | TEcont text ->
2458 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2460 | TEstop ->
2461 onleave Cancel;
2462 G.postRedisplay "textentrykeyboard after cancel3"
2464 | TEswitch te ->
2465 state.mode <- Textentry (te, onleave);
2466 G.postRedisplay "textentrykeyboard switch";
2468 | _ -> vlog "unhandled key"
2471 let firstof first active =
2472 if first > active || abs (first - active) > fstate.maxrows - 1
2473 then max 0 (active - (fstate.maxrows/2))
2474 else first
2477 let calcfirst first active =
2478 if active > first
2479 then
2480 let rows = active - first in
2481 if rows > fstate.maxrows then active - fstate.maxrows else first
2482 else active
2485 let scrollph y maxy =
2486 let sh = float (maxy + state.winh) /. float state.winh in
2487 let sh = float state.winh /. sh in
2488 let sh = max sh (float conf.scrollh) in
2490 let percent = float y /. float maxy in
2491 let position = (float state.winh -. sh) *. percent in
2493 let position =
2494 if position +. sh > float state.winh
2495 then float state.winh -. sh
2496 else position
2498 position, sh;
2501 let adderrmsg src msg =
2502 Buffer.add_string state.errmsgs msg;
2503 state.newerrmsgs <- true;
2504 G.postRedisplay src
2507 let adderrfmt src fmt =
2508 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2511 let coe s = (s :> uioh);;
2513 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2514 object (self)
2515 val m_pan = source#getpan
2516 val m_first = source#getfirst
2517 val m_active = source#getactive
2518 val m_qsearch = E.s
2519 val m_prev_uioh = state.uioh
2521 method private elemunder y =
2522 if y < 0
2523 then None
2524 else
2525 let n = y / (fstate.fontsize+1) in
2526 if m_first + n < source#getitemcount
2527 then (
2528 if source#hasaction (m_first + n)
2529 then Some (m_first + n)
2530 else None
2532 else None
2534 method display =
2535 Gl.enable `blend;
2536 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2537 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2538 filledrect 0. 0. (float state.winw) (float state.winh);
2539 GlDraw.color (1., 1., 1.);
2540 Gl.enable `texture_2d;
2541 let fs = fstate.fontsize in
2542 let nfs = fs + 1 in
2543 let hw = state.winw/3 in
2544 let ww = fstate.wwidth in
2545 let tabw = 17.0*.ww in
2546 let itemcount = source#getitemcount in
2547 let minfo = source#getminfo in
2548 if conf.leftscroll
2549 then (
2550 GlMat.push ();
2551 GlMat.translate ~x:(float conf.scrollbw) ();
2553 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2554 let rec loop row =
2555 if (row - m_first) > fstate.maxrows
2556 then ()
2557 else (
2558 if row >= 0 && row < itemcount
2559 then (
2560 let (s, level) = source#getitem row in
2561 let y = (row - m_first) * nfs in
2562 let x = 5.0 +. (float (level + m_pan)) *. ww in
2563 if helpmode
2564 then GlDraw.color
2565 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2567 if row = m_active
2568 then (
2569 Gl.disable `texture_2d;
2570 let alpha = if source#hasaction row then 0.9 else 0.3 in
2571 GlDraw.color (1., 1., 1.) ~alpha;
2572 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2573 Gl.enable `texture_2d;
2575 let c =
2576 if zebra && row land 1 = 1
2577 then 0.8
2578 else 1.0
2580 GlDraw.color (c,c,c);
2581 let drawtabularstring s =
2582 let drawstr x s =
2583 let x' = truncate (x0 +. x) in
2584 let s1, s2 = splitatchar s '\000' in
2585 if emptystr s2
2586 then drawstring1 fs x' (y+nfs) s
2587 else
2588 let rec e s =
2589 if emptystr s
2590 then s
2591 else
2592 let s' = withoutlastutf8 s in
2593 let s = s' ^ UniSyms.ellipsis in
2594 let w = measurestr fs s in
2595 if float x' +. w +. ww < float (hw + x')
2596 then s
2597 else e s'
2599 let s1 =
2600 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2601 then e s1
2602 else s1
2604 ignore (drawstring1 fs x' (y+nfs) s1);
2605 drawstring1 fs (hw + x') (y+nfs) s2
2607 if trusted
2608 then
2609 let x = if helpmode && row > 0 then x +. ww else x in
2610 let s1, s2 = splitatchar s '\t' in
2611 if nonemptystr s2
2612 then
2613 let nx = drawstr x s1 in
2614 let sw = nx -. x in
2615 let x = x +. (max tabw sw) in
2616 drawstr x s2
2617 else
2618 let len = String.length s - 2 in
2619 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2620 then
2621 let s = String.sub s 2 len in
2622 let x = if not helpmode then x +. ww else x in
2623 GlDraw.color (1.2, 1.2, 1.2);
2624 let vinc = drawstring1 (fs+fs/4)
2625 (truncate (x -. ww)) (y+nfs) s in
2626 GlDraw.color (1., 1., 1.);
2627 vinc +. (float fs *. 0.8)
2628 else
2629 drawstr x s
2630 else
2631 drawstr x s
2633 ignore (drawtabularstring s);
2634 loop (row+1)
2638 loop m_first;
2639 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2640 let xadj = 5.0 in
2641 let rec loop row =
2642 if (row - m_first) <= fstate.maxrows
2643 then
2644 if row >= 0 && row < itemcount
2645 then (
2646 let (s, level) = source#getitem row in
2647 let pos0 = nindex s '\000' in
2648 let y = (row - m_first) * nfs in
2649 let x = float (level + m_pan) *. ww in
2650 let (first, last) = minfo.(row) in
2651 let prefix =
2652 if pos0 > 0 && first > pos0
2653 then String.sub s (pos0+1) (first-pos0-1)
2654 else String.sub s 0 first
2656 let suffix = String.sub s first (last - first) in
2657 let w1 = measurestr fstate.fontsize prefix in
2658 let w2 = measurestr fstate.fontsize suffix in
2659 let x = x +. if conf.leftscroll then xadj else 5.0 in
2660 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2661 let x0 = x +. w1
2662 and y0 = float (y+2) in
2663 let x1 = x0 +. w2
2664 and y1 = float (y+fs+3) in
2665 filledrect x0 y0 x1 y1;
2666 loop (row+1)
2669 Gl.disable `texture_2d;
2670 if Array.length minfo > 0 then loop m_first;
2671 Gl.disable `blend;
2672 if conf.leftscroll
2673 then GlMat.pop ()
2675 method updownlevel incr =
2676 let len = source#getitemcount in
2677 let curlevel =
2678 if m_active >= 0 && m_active < len
2679 then snd (source#getitem m_active)
2680 else -1
2682 let rec flow i =
2683 if i = len then i-1 else if i = -1 then 0 else
2684 let _, l = source#getitem i in
2685 if l != curlevel then i else flow (i+incr)
2687 let active = flow m_active in
2688 let first = calcfirst m_first active in
2689 G.postRedisplay "outline updownlevel";
2690 {< m_active = active; m_first = first >}
2692 method private key1 key mask =
2693 let set1 active first qsearch =
2694 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2696 let search active pattern incr =
2697 let active = if active = -1 then m_first else active in
2698 let dosearch re =
2699 let rec loop n =
2700 if n >= 0 && n < source#getitemcount
2701 then (
2702 let s, _ = source#getitem n in
2703 match Str.search_forward re s 0 with
2704 | exception Not_found -> loop (n + incr)
2705 | _ -> Some n
2707 else None
2709 loop active
2711 let qpat = Str.quote pattern in
2712 match Str.regexp_case_fold qpat with
2713 | s -> dosearch s
2714 | exception exn ->
2715 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2716 qpat @@ Printexc.to_string exn;
2717 None
2719 let itemcount = source#getitemcount in
2720 let find start incr =
2721 let rec find i =
2722 if i = -1 || i = itemcount
2723 then -1
2724 else (
2725 if source#hasaction i
2726 then i
2727 else find (i + incr)
2730 find start
2732 let set active first =
2733 let first = bound first 0 (itemcount - fstate.maxrows) in
2734 state.text <- E.s;
2735 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2737 let navigate incr =
2738 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2739 let active, first =
2740 let incr1 = if incr > 0 then 1 else -1 in
2741 if isvisible m_first m_active
2742 then
2743 let next =
2744 let next = m_active + incr in
2745 let next =
2746 if next < 0 || next >= itemcount
2747 then -1
2748 else find next incr1
2750 if abs (m_active - next) > fstate.maxrows
2751 then -1
2752 else next
2754 if next = -1
2755 then
2756 let first = m_first + incr in
2757 let first = bound first 0 (itemcount - fstate.maxrows) in
2758 let next =
2759 let next = m_active + incr in
2760 let next = bound next 0 (itemcount - 1) in
2761 find next ~-incr1
2763 let active =
2764 if next = -1
2765 then m_active
2766 else (
2767 if isvisible first next
2768 then next
2769 else m_active
2772 active, first
2773 else
2774 let first = min next m_first in
2775 let first =
2776 if abs (next - first) > fstate.maxrows
2777 then first + incr
2778 else first
2780 next, first
2781 else
2782 let first = m_first + incr in
2783 let first = bound first 0 (itemcount - 1) in
2784 let active =
2785 let next = m_active + incr in
2786 let next = bound next 0 (itemcount - 1) in
2787 let next = find next incr1 in
2788 let active =
2789 if next = -1 || abs (m_active - first) > fstate.maxrows
2790 then (
2791 let active = if m_active = -1 then next else m_active in
2792 active
2794 else next
2796 if isvisible first active
2797 then active
2798 else -1
2800 active, first
2802 G.postRedisplay "listview navigate";
2803 set active first;
2805 let open Keys in
2806 let kt = Wsi.kc2kt key in
2807 match [@warning "-4"] kt with
2808 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2809 let incr = if c = 'r' then -1 else 1 in
2810 let active, first =
2811 match search (m_active + incr) m_qsearch incr with
2812 | None ->
2813 state.text <- m_qsearch ^ " [not found]";
2814 m_active, m_first
2815 | Some active ->
2816 state.text <- m_qsearch;
2817 active, firstof m_first active
2819 G.postRedisplay "listview ctrl-r/s";
2820 set1 active first m_qsearch;
2822 | Insert when Wsi.withctrl mask ->
2823 if m_active >= 0 && m_active < source#getitemcount
2824 then (
2825 let s, _ = source#getitem m_active in
2826 selstring s;
2828 coe self
2830 | Backspace ->
2831 if emptystr m_qsearch
2832 then coe self
2833 else (
2834 let qsearch = withoutlastutf8 m_qsearch in
2835 if emptystr qsearch
2836 then (
2837 state.text <- E.s;
2838 G.postRedisplay "listview empty qsearch";
2839 set1 m_active m_first E.s;
2841 else
2842 let active, first =
2843 match search m_active qsearch ~-1 with
2844 | None ->
2845 state.text <- qsearch ^ " [not found]";
2846 m_active, m_first
2847 | Some active ->
2848 state.text <- qsearch;
2849 active, firstof m_first active
2851 G.postRedisplay "listview backspace qsearch";
2852 set1 active first qsearch
2855 | Ascii _ | Code _ ->
2856 let utf8 =
2857 match [@warning "-8"] kt with
2858 | Ascii c -> String.make 1 c
2859 | Code code -> toutf8 code
2861 let pattern = m_qsearch ^ utf8 in
2862 let active, first =
2863 match search m_active pattern 1 with
2864 | None ->
2865 state.text <- pattern ^ " [not found]";
2866 m_active, m_first
2867 | Some active ->
2868 state.text <- pattern;
2869 active, firstof m_first active
2871 G.postRedisplay "listview qsearch add";
2872 set1 active first pattern;
2874 | Escape ->
2875 state.text <- E.s;
2876 if emptystr m_qsearch
2877 then (
2878 G.postRedisplay "list view escape";
2879 let mx, my = state.mpos in
2880 updateunder mx my;
2881 match source#exit ~uioh:(coe self) ~cancel:true ~active:m_active
2882 ~first:m_first ~pan:m_pan with
2883 | None -> m_prev_uioh
2884 | Some uioh -> uioh
2886 else (
2887 G.postRedisplay "list view kill qsearch";
2888 coe {< m_qsearch = E.s >}
2891 | Enter ->
2892 state.text <- E.s;
2893 let self = {< m_qsearch = E.s >} in
2894 let opt =
2895 G.postRedisplay "listview enter";
2896 let cancel = not (m_active >= 0 && m_active < source#getitemcount) in
2897 source#exit ~uioh:(coe self) ~cancel
2898 ~active:m_active ~first:m_first ~pan:m_pan;
2900 begin match opt with
2901 | None -> m_prev_uioh
2902 | Some uioh -> uioh
2905 | Delete ->
2906 coe self
2908 | Up -> navigate ~-1
2909 | Down -> navigate 1
2910 | Prior -> navigate ~-(fstate.maxrows)
2911 | Next -> navigate fstate.maxrows
2913 | Right ->
2914 state.text <- E.s;
2915 G.postRedisplay "listview right";
2916 coe {< m_pan = m_pan - 1 >}
2918 | Left ->
2919 state.text <- E.s;
2920 G.postRedisplay "listview left";
2921 coe {< m_pan = m_pan + 1 >}
2923 | Home ->
2924 let active = find 0 1 in
2925 G.postRedisplay "listview home";
2926 set active 0;
2928 | End ->
2929 let first = max 0 (itemcount - fstate.maxrows) in
2930 let active = find (itemcount - 1) ~-1 in
2931 G.postRedisplay "listview end";
2932 set active first;
2934 | _ -> coe self
2936 method key key mask =
2937 match state.mode with
2938 | Textentry te ->
2939 textentrykeyboard key mask te;
2940 coe self
2941 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2943 method button button down x y _ =
2944 let opt =
2945 match button with
2946 | 1 when vscrollhit x ->
2947 G.postRedisplay "listview scroll";
2948 if down
2949 then
2950 let _, position, sh = self#scrollph in
2951 if y > truncate position && y < truncate (position +. sh)
2952 then (
2953 state.mstate <- Mscrolly;
2954 Some (coe self)
2956 else
2957 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2958 let first = truncate (s *. float source#getitemcount) in
2959 let first = min source#getitemcount first in
2960 Some (coe {< m_first = first; m_active = first >})
2961 else (
2962 state.mstate <- Mnone;
2963 Some (coe self);
2965 | 1 when down ->
2966 begin match self#elemunder y with
2967 | Some n ->
2968 G.postRedisplay "listview click";
2969 source#exit ~uioh:(coe {< m_active = n >})
2970 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
2971 | _ ->
2972 Some (coe self)
2974 | n when (n == 4 || n == 5) && not down ->
2975 let len = source#getitemcount in
2976 let first =
2977 if n = 5 && m_first + fstate.maxrows >= len
2978 then
2979 m_first
2980 else
2981 let first = m_first + (if n == 4 then -1 else 1) in
2982 bound first 0 (len - 1)
2984 G.postRedisplay "listview wheel";
2985 Some (coe {< m_first = first >})
2986 | n when (n = 6 || n = 7) && not down ->
2987 let inc = if n = 7 then -1 else 1 in
2988 G.postRedisplay "listview hwheel";
2989 Some (coe {< m_pan = m_pan + inc >})
2990 | _ ->
2991 Some (coe self)
2993 match opt with
2994 | None -> m_prev_uioh
2995 | Some uioh -> uioh
2997 method multiclick _ x y = self#button 1 true x y
2999 method motion _ y =
3000 match state.mstate with
3001 | Mscrolly ->
3002 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3003 let first = truncate (s *. float source#getitemcount) in
3004 let first = min source#getitemcount first in
3005 G.postRedisplay "listview motion";
3006 coe {< m_first = first; m_active = first >}
3007 | Msel _
3008 | Mpan _
3009 | Mscrollx
3010 | Mzoom _
3011 | Mzoomrect _
3012 | Mnone -> coe self
3014 method pmotion x y =
3015 if x < state.winw - conf.scrollbw
3016 then
3017 let n =
3018 match self#elemunder y with
3019 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3020 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3022 let o =
3023 if n != m_active
3024 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3025 else self
3027 coe o
3028 else (
3029 Wsi.setcursor Wsi.CURSOR_INHERIT;
3030 coe self
3033 method infochanged _ = ()
3035 method scrollpw = (0, 0.0, 0.0)
3036 method scrollph =
3037 let nfs = fstate.fontsize + 1 in
3038 let y = m_first * nfs in
3039 let itemcount = source#getitemcount in
3040 let maxi = max 0 (itemcount - fstate.maxrows) in
3041 let maxy = maxi * nfs in
3042 let p, h = scrollph y maxy in
3043 conf.scrollbw, p, h
3045 method modehash = modehash
3046 method eformsgs = false
3047 method alwaysscrolly = true
3048 method scroll _ dy =
3049 let self =
3050 if dy != 0 then begin
3051 let len = source#getitemcount in
3052 let first =
3053 if dy > 0 && m_first + fstate.maxrows >= len
3054 then
3055 m_first
3056 else
3057 let first = m_first + dy / 10 in
3058 bound first 0 (len - 1)
3060 G.postRedisplay "listview wheel";
3061 {< m_first = first >}
3062 end else
3063 self
3065 coe self
3067 method zoom _ _ _ = ()
3068 end;;
3070 class outlinelistview ~zebra ~source =
3071 let settext autonarrow s =
3072 if autonarrow
3073 then
3074 let ss = source#statestr in
3075 state.text <-
3076 if emptystr ss
3077 then "[" ^ s ^ "]"
3078 else "{" ^ ss ^ "} [" ^ s ^ "]"
3079 else state.text <- s
3081 object (self)
3082 inherit listview
3083 ~zebra
3084 ~helpmode:false
3085 ~source:(source :> lvsource)
3086 ~trusted:false
3087 ~modehash:(findkeyhash conf "outline")
3088 as super
3090 val m_autonarrow = false
3092 method! key key mask =
3093 let maxrows =
3094 if emptystr state.text
3095 then fstate.maxrows
3096 else fstate.maxrows - 2
3098 let calcfirst first active =
3099 if active > first
3100 then
3101 let rows = active - first in
3102 if rows > maxrows then active - maxrows else first
3103 else active
3105 let navigate incr =
3106 let active = m_active + incr in
3107 let active = bound active 0 (source#getitemcount - 1) in
3108 let first = calcfirst m_first active in
3109 G.postRedisplay "outline navigate";
3110 coe {< m_active = active; m_first = first >}
3112 let navscroll first =
3113 let active =
3114 let dist = m_active - first in
3115 if dist < 0
3116 then first
3117 else (
3118 if dist < maxrows
3119 then m_active
3120 else first + maxrows
3123 G.postRedisplay "outline navscroll";
3124 coe {< m_first = first; m_active = active >}
3126 let ctrl = Wsi.withctrl mask in
3127 let open Keys in
3128 match Wsi.kc2kt key with
3129 | Ascii 'a' when ctrl ->
3130 let text =
3131 if m_autonarrow
3132 then (source#denarrow; E.s)
3133 else (
3134 let pattern = source#renarrow in
3135 if nonemptystr m_qsearch
3136 then (source#narrow m_qsearch; m_qsearch)
3137 else pattern
3140 settext (not m_autonarrow) text;
3141 G.postRedisplay "toggle auto narrowing";
3142 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3144 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
3145 settext true E.s;
3146 G.postRedisplay "toggle auto narrowing";
3147 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3149 | Ascii 'n' when ctrl ->
3150 source#narrow m_qsearch;
3151 if not m_autonarrow
3152 then source#add_narrow_pattern m_qsearch;
3153 G.postRedisplay "outline ctrl-n";
3154 coe {< m_first = 0; m_active = 0 >}
3156 | Ascii 'S' when ctrl ->
3157 let active = source#calcactive (getanchor ()) in
3158 let first = firstof m_first active in
3159 G.postRedisplay "outline ctrl-s";
3160 coe {< m_first = first; m_active = active >}
3162 | Ascii 'u' when ctrl ->
3163 G.postRedisplay "outline ctrl-u";
3164 if m_autonarrow && nonemptystr m_qsearch
3165 then (
3166 ignore (source#renarrow);
3167 settext m_autonarrow E.s;
3168 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3170 else (
3171 source#del_narrow_pattern;
3172 let pattern = source#renarrow in
3173 let text =
3174 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3176 settext m_autonarrow text;
3177 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3180 | Ascii 'l' when ctrl ->
3181 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3182 G.postRedisplay "outline ctrl-l";
3183 coe {< m_first = first >}
3185 | Ascii '\t' when m_autonarrow ->
3186 if nonemptystr m_qsearch
3187 then (
3188 G.postRedisplay "outline list view tab";
3189 source#add_narrow_pattern m_qsearch;
3190 settext true E.s;
3191 coe {< m_qsearch = E.s >}
3193 else coe self
3195 | Escape when m_autonarrow ->
3196 if nonemptystr m_qsearch
3197 then source#add_narrow_pattern m_qsearch;
3198 super#key key mask
3200 | Enter when m_autonarrow ->
3201 if nonemptystr m_qsearch
3202 then source#add_narrow_pattern m_qsearch;
3203 super#key key mask
3205 | (Ascii _ | Code _) when m_autonarrow ->
3206 let pattern = m_qsearch ^ toutf8 key in
3207 G.postRedisplay "outlinelistview autonarrow add";
3208 source#narrow pattern;
3209 settext true pattern;
3210 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3212 | Backspace when m_autonarrow ->
3213 if emptystr m_qsearch
3214 then coe self
3215 else
3216 let pattern = withoutlastutf8 m_qsearch in
3217 G.postRedisplay "outlinelistview autonarrow backspace";
3218 ignore (source#renarrow);
3219 source#narrow pattern;
3220 settext true pattern;
3221 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3223 | Up when ctrl ->
3224 navscroll (max 0 (m_first - 1))
3226 | Down when ctrl ->
3227 navscroll (min (source#getitemcount - 1) (m_first + 1))
3229 | Up -> navigate ~-1
3230 | Down -> navigate 1
3231 | Prior -> navigate ~-(fstate.maxrows)
3232 | Next -> navigate fstate.maxrows
3234 | Right ->
3235 let o =
3236 if ctrl
3237 then (
3238 G.postRedisplay "outline ctrl right";
3239 {< m_pan = m_pan + 1 >}
3241 else self#updownlevel 1
3243 coe o
3245 | Left ->
3246 let o =
3247 if ctrl
3248 then (
3249 G.postRedisplay "outline ctrl left";
3250 {< m_pan = m_pan - 1 >}
3252 else self#updownlevel ~-1
3254 coe o
3256 | Home ->
3257 G.postRedisplay "outline home";
3258 coe {< m_first = 0; m_active = 0 >}
3260 | End ->
3261 let active = source#getitemcount - 1 in
3262 let first = max 0 (active - fstate.maxrows) in
3263 G.postRedisplay "outline end";
3264 coe {< m_active = active; m_first = first >}
3266 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
3267 super#key key mask
3268 end;;
3270 let genhistoutlines () =
3271 Config.gethist ()
3272 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3273 compare c2.lastvisit c1.lastvisit)
3274 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
3275 let path = if nonemptystr origin then origin else path in
3276 let base = mbtoutf8 @@ Filename.basename path in
3277 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3281 let gotohist (path, c, bookmarks, x, anchor, origin) =
3282 Config.save leavebirdseye;
3283 state.anchor <- anchor;
3284 state.bookmarks <- bookmarks;
3285 state.origin <- origin;
3286 state.x <- x;
3287 setconf conf c;
3288 let x0, y0, x1, y1 = conf.trimfuzz in
3289 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3290 reshape ~firsttime:true state.winw state.winh;
3291 opendoc path origin;
3292 setzoom c.zoom;
3295 let makecheckers () =
3296 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3297 following to say:
3298 converted by Issac Trotts. July 25, 2002 *)
3299 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3300 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3301 let id = GlTex.gen_texture () in
3302 GlTex.bind_texture ~target:`texture_2d id;
3303 GlPix.store (`unpack_alignment 1);
3304 GlTex.image2d image;
3305 List.iter (GlTex.parameter ~target:`texture_2d)
3306 [ `mag_filter `nearest; `min_filter `nearest ];
3310 let setcheckers enabled =
3311 match state.checkerstexid with
3312 | None ->
3313 if enabled then state.checkerstexid <- Some (makecheckers ())
3315 | Some checkerstexid ->
3316 if not enabled
3317 then (
3318 GlTex.delete_texture checkerstexid;
3319 state.checkerstexid <- None;
3323 let describe_layout layout =
3324 let d =
3325 match layout with
3326 | [] -> "Page 0"
3327 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
3328 | l :: rest ->
3329 let rangestr a b =
3330 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
3331 else
3332 let sep = if a.pageno+1 = b.pageno then ", " else UniSyms.ellipsis in
3333 Printf.sprintf "%d%s%d" (a.pageno+1) sep (b.pageno+1)
3335 let rec fold s la lb = function
3336 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
3337 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
3338 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
3340 fold "Pages" l l rest
3342 let percent =
3343 let maxy = maxy () in
3344 if maxy <= 0
3345 then 100.
3346 else 100. *. (float state.y /. float maxy)
3348 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
3351 let setpresentationmode v =
3352 let n = page_of_y state.y in
3353 state.anchor <- (n, 0.0, 1.0);
3354 conf.presentation <- v;
3355 if conf.fitmodel = FitPage
3356 then reqlayout conf.angle conf.fitmodel;
3357 represent ();
3360 let setbgcol (r, g, b) =
3361 let col =
3362 let r = r *. 255.0 |> truncate
3363 and g = g *. 255.0 |> truncate
3364 and b = b *. 255.0 |> truncate in
3365 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3367 Wsi.setwinbgcol col;
3370 let enterinfomode =
3371 let btos b = if b then UniSyms.radical else E.s in
3372 let showextended = ref false in
3373 let showcolors = ref false in
3374 let leave mode _ = state.mode <- mode in
3375 let src =
3376 (object
3377 val mutable m_l = []
3378 val mutable m_a = E.a
3379 val mutable m_prev_uioh = nouioh
3380 val mutable m_prev_mode = View
3382 inherit lvsourcebase
3384 method reset prev_mode prev_uioh =
3385 m_a <- Array.of_list (List.rev m_l);
3386 m_l <- [];
3387 m_prev_mode <- prev_mode;
3388 m_prev_uioh <- prev_uioh;
3390 method int name get set =
3391 m_l <-
3392 (name, `int get, 1,
3393 Action (
3394 fun u ->
3395 let ondone s =
3396 try set (int_of_string s)
3397 with exn ->
3398 state.text <- Printf.sprintf "bad integer `%s': %s"
3399 s @@ exntos exn
3401 state.text <- E.s;
3402 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3403 state.mode <- Textentry (te, leave m_prev_mode);
3405 )) :: m_l
3407 method int_with_suffix name get set =
3408 m_l <-
3409 (name, `intws get, 1,
3410 Action (
3411 fun u ->
3412 let ondone s =
3413 try set (int_of_string_with_suffix s)
3414 with exn ->
3415 state.text <- Printf.sprintf "bad integer `%s': %s"
3416 s @@ exntos exn
3418 state.text <- E.s;
3419 let te =
3420 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3422 state.mode <- Textentry (te, leave m_prev_mode);
3424 )) :: m_l
3426 method bool ?(offset=1) ?(btos=btos) name get set =
3427 m_l <-
3428 (name, `bool (btos, get), offset, Action (
3429 fun u ->
3430 let v = get () in
3431 set (not v);
3433 )) :: m_l
3435 method color name get set =
3436 m_l <-
3437 (name, `color get, 1,
3438 Action (
3439 fun u ->
3440 let invalid = (nan, nan, nan) in
3441 let ondone s =
3442 let c =
3443 try color_of_string s
3444 with exn ->
3445 state.text <- Printf.sprintf "bad color `%s': %s"
3446 s @@ exntos exn;
3447 invalid
3449 if c <> invalid
3450 then set c;
3452 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3453 state.text <- color_to_string (get ());
3454 state.mode <- Textentry (te, leave m_prev_mode);
3456 )) :: m_l
3458 method string name get set =
3459 m_l <-
3460 (name, `string get, 1,
3461 Action (
3462 fun u ->
3463 let ondone s = set s in
3464 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3465 state.mode <- Textentry (te, leave m_prev_mode);
3467 )) :: m_l
3469 method colorspace name get set =
3470 m_l <-
3471 (name, `string get, 1,
3472 Action (
3473 fun _ ->
3474 let source =
3475 (object
3476 inherit lvsourcebase
3478 initializer
3479 m_active <- CSTE.to_int conf.colorspace;
3480 m_first <- 0;
3482 method getitemcount =
3483 Array.length CSTE.names
3484 method getitem n =
3485 (CSTE.names.(n), 0)
3486 method exit ~uioh ~cancel ~active ~first ~pan =
3487 ignore (uioh, first, pan);
3488 if not cancel then set active;
3489 None
3490 method hasaction _ = true
3491 end)
3493 state.text <- E.s;
3494 let modehash = findkeyhash conf "info" in
3495 coe (new listview ~zebra:false ~helpmode:false
3496 ~source ~trusted:true ~modehash)
3497 )) :: m_l
3499 method paxmark name get set =
3500 m_l <-
3501 (name, `string get, 1,
3502 Action (
3503 fun _ ->
3504 let source =
3505 (object
3506 inherit lvsourcebase
3508 initializer
3509 m_active <- MTE.to_int conf.paxmark;
3510 m_first <- 0;
3512 method getitemcount = Array.length MTE.names
3513 method getitem n = (MTE.names.(n), 0)
3514 method exit ~uioh ~cancel ~active ~first ~pan =
3515 ignore (uioh, first, pan);
3516 if not cancel then set active;
3517 None
3518 method hasaction _ = true
3519 end)
3521 state.text <- E.s;
3522 let modehash = findkeyhash conf "info" in
3523 coe (new listview ~zebra:false ~helpmode:false
3524 ~source ~trusted:true ~modehash)
3525 )) :: m_l
3527 method fitmodel name get set =
3528 m_l <-
3529 (name, `string get, 1,
3530 Action (
3531 fun _ ->
3532 let source =
3533 (object
3534 inherit lvsourcebase
3536 initializer
3537 m_active <- FMTE.to_int conf.fitmodel;
3538 m_first <- 0;
3540 method getitemcount = Array.length FMTE.names
3541 method getitem n = (FMTE.names.(n), 0)
3542 method exit ~uioh ~cancel ~active ~first ~pan =
3543 ignore (uioh, first, pan);
3544 if not cancel then set active;
3545 None
3546 method hasaction _ = true
3547 end)
3549 state.text <- E.s;
3550 let modehash = findkeyhash conf "info" in
3551 coe (new listview ~zebra:false ~helpmode:false
3552 ~source ~trusted:true ~modehash)
3553 )) :: m_l
3555 method caption s offset =
3556 m_l <- (s, `empty, offset, Noaction) :: m_l
3558 method caption2 s f offset =
3559 m_l <- (s, `string f, offset, Noaction) :: m_l
3561 method getitemcount = Array.length m_a
3563 method getitem n =
3564 let tostr = function
3565 | `int f -> string_of_int (f ())
3566 | `intws f -> string_with_suffix_of_int (f ())
3567 | `string f -> f ()
3568 | `color f -> color_to_string (f ())
3569 | `bool (btos, f) -> btos (f ())
3570 | `empty -> E.s
3572 let name, t, offset, _ = m_a.(n) in
3573 ((let s = tostr t in
3574 if nonemptystr s
3575 then Printf.sprintf "%s\t%s" name s
3576 else name),
3577 offset)
3579 method exit ~uioh ~cancel ~active ~first ~pan =
3580 let uiohopt =
3581 if not cancel
3582 then (
3583 let uioh =
3584 match m_a.(active) with
3585 | _, _, _, Action f -> f uioh
3586 | _, _, _, Noaction -> uioh
3588 Some uioh
3590 else None
3592 m_active <- active;
3593 m_first <- first;
3594 m_pan <- pan;
3595 uiohopt
3597 method hasaction n =
3598 match m_a.(n) with
3599 | _, _, _, Action _ -> true
3600 | _, _, _, Noaction -> false
3602 initializer m_active <- 1
3603 end)
3605 let rec fillsrc prevmode prevuioh =
3606 let sep () = src#caption E.s 0 in
3607 let colorp name get set =
3608 src#string name
3609 (fun () -> color_to_string (get ()))
3610 (fun v ->
3612 let c = color_of_string v in
3613 set c
3614 with exn ->
3615 state.text <-
3616 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3619 let rgba name get set =
3620 src#string name
3621 (fun () -> rgba_to_string (get ()))
3622 (fun v ->
3624 let c = rgba_of_string v in
3625 set c
3626 with exn ->
3627 state.text <-
3628 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3631 let oldmode = state.mode in
3632 let birdseye = isbirdseye state.mode in
3634 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3636 src#bool "presentation mode"
3637 (fun () -> conf.presentation)
3638 (fun v -> setpresentationmode v);
3640 src#bool "ignore case in searches"
3641 (fun () -> conf.icase)
3642 (fun v -> conf.icase <- v);
3644 src#bool "preload"
3645 (fun () -> conf.preload)
3646 (fun v -> conf.preload <- v);
3648 src#bool "highlight links"
3649 (fun () -> conf.hlinks)
3650 (fun v -> conf.hlinks <- v);
3652 src#bool "under info"
3653 (fun () -> conf.underinfo)
3654 (fun v -> conf.underinfo <- v);
3656 src#bool "persistent bookmarks"
3657 (fun () -> conf.savebmarks)
3658 (fun v -> conf.savebmarks <- v);
3660 src#fitmodel "fit model"
3661 (fun () -> FMTE.to_string conf.fitmodel)
3662 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3664 src#bool "trim margins"
3665 (fun () -> conf.trimmargins)
3666 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3668 src#bool "persistent location"
3669 (fun () -> conf.jumpback)
3670 (fun v -> conf.jumpback <- v);
3672 sep ();
3673 src#int "inter-page space"
3674 (fun () -> conf.interpagespace)
3675 (fun n ->
3676 conf.interpagespace <- n;
3677 docolumns conf.columns;
3678 let pageno, py =
3679 match state.layout with
3680 | [] -> 0, 0
3681 | l :: _ ->
3682 l.pageno, l.pagey
3684 state.maxy <- calcheight ();
3685 let y = getpagey pageno in
3686 gotoxy state.x (y + py)
3689 src#int "page bias"
3690 (fun () -> conf.pagebias)
3691 (fun v -> conf.pagebias <- v);
3693 src#int "scroll step"
3694 (fun () -> conf.scrollstep)
3695 (fun n -> conf.scrollstep <- n);
3697 src#int "horizontal scroll step"
3698 (fun () -> conf.hscrollstep)
3699 (fun v -> conf.hscrollstep <- v);
3701 src#int "auto scroll step"
3702 (fun () ->
3703 match state.autoscroll with
3704 | Some step -> step
3705 | _ -> conf.autoscrollstep)
3706 (fun n ->
3707 let n = boundastep state.winh n in
3708 if state.autoscroll <> None
3709 then state.autoscroll <- Some n;
3710 conf.autoscrollstep <- n);
3712 src#int "zoom"
3713 (fun () -> truncate (conf.zoom *. 100.))
3714 (fun v -> pivotzoom ((float v) /. 100.));
3716 src#int "rotation"
3717 (fun () -> conf.angle)
3718 (fun v -> reqlayout v conf.fitmodel);
3720 src#int "scroll bar width"
3721 (fun () -> conf.scrollbw)
3722 (fun v ->
3723 conf.scrollbw <- v;
3724 reshape state.winw state.winh;
3727 src#int "scroll handle height"
3728 (fun () -> conf.scrollh)
3729 (fun v -> conf.scrollh <- v;);
3731 src#int "thumbnail width"
3732 (fun () -> conf.thumbw)
3733 (fun v ->
3734 conf.thumbw <- min 4096 v;
3735 match oldmode with
3736 | Birdseye beye ->
3737 leavebirdseye beye false;
3738 enterbirdseye ()
3739 | Textentry _
3740 | View
3741 | LinkNav _ -> ()
3744 let mode = state.mode in
3745 src#string "columns"
3746 (fun () ->
3747 match conf.columns with
3748 | Csingle _ -> "1"
3749 | Cmulti (multi, _) -> multicolumns_to_string multi
3750 | Csplit (count, _) -> "-" ^ string_of_int count
3752 (fun v ->
3753 let n, a, b = multicolumns_of_string v in
3754 setcolumns mode n a b);
3756 sep ();
3757 src#caption "Pixmap cache" 0;
3758 src#int_with_suffix "size (advisory)"
3759 (fun () -> conf.memlimit)
3760 (fun v -> conf.memlimit <- v);
3762 src#caption2 "used"
3763 (fun () ->
3764 Printf.sprintf "%s bytes, %d tiles"
3765 (string_with_suffix_of_int state.memused)
3766 (Hashtbl.length state.tilemap)) 1;
3768 sep ();
3769 src#caption "Layout" 0;
3770 src#caption2 "Dimension"
3771 (fun () ->
3772 Printf.sprintf "%dx%d (virtual %dx%d)"
3773 state.winw state.winh
3774 state.w state.maxy)
3776 if conf.debug
3777 then
3778 src#caption2 "Position" (fun () ->
3779 Printf.sprintf "%dx%d" state.x state.y
3781 else
3782 src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
3784 sep ();
3785 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3786 "Save these parameters as global defaults at exit"
3787 (fun () -> conf.bedefault)
3788 (fun v -> conf.bedefault <- v);
3790 sep ();
3791 let btos b = if b then UniSyms.lguillemet else UniSyms.rguillemet in
3792 src#bool ~offset:0 ~btos "Extended parameters"
3793 (fun () -> !showextended)
3794 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3795 if !showextended
3796 then (
3797 src#bool "checkers"
3798 (fun () -> conf.checkers)
3799 (fun v -> conf.checkers <- v; setcheckers v);
3800 src#bool "update cursor"
3801 (fun () -> conf.updatecurs)
3802 (fun v -> conf.updatecurs <- v);
3803 src#bool "scroll-bar on the left"
3804 (fun () -> conf.leftscroll)
3805 (fun v -> conf.leftscroll <- v);
3806 src#bool "verbose"
3807 (fun () -> conf.verbose)
3808 (fun v -> conf.verbose <- v);
3809 src#bool "invert colors"
3810 (fun () -> conf.invert)
3811 (fun v -> conf.invert <- v);
3812 src#bool "max fit"
3813 (fun () -> conf.maxhfit)
3814 (fun v -> conf.maxhfit <- v);
3815 src#bool "pax mode"
3816 (fun () -> conf.pax != None)
3817 (fun v ->
3818 if v
3819 then conf.pax <- Some (now ())
3820 else conf.pax <- None);
3821 src#string "uri launcher"
3822 (fun () -> conf.urilauncher)
3823 (fun v -> conf.urilauncher <- v);
3824 src#string "path launcher"
3825 (fun () -> conf.pathlauncher)
3826 (fun v -> conf.pathlauncher <- v);
3827 src#string "tile size"
3828 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3829 (fun v ->
3831 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3832 conf.tilew <- max 64 w;
3833 conf.tileh <- max 64 h;
3834 flushtiles ();
3835 with exn ->
3836 state.text <- Printf.sprintf "bad tile size `%s': %s"
3837 v @@ exntos exn
3839 src#int "texture count"
3840 (fun () -> conf.texcount)
3841 (fun v ->
3842 if realloctexts v
3843 then conf.texcount <- v
3844 else impmsg "failed to set texture count please retry later"
3846 src#int "slice height"
3847 (fun () -> conf.sliceheight)
3848 (fun v ->
3849 conf.sliceheight <- v;
3850 wcmd "sliceh %d" conf.sliceheight;
3852 src#int "anti-aliasing level"
3853 (fun () -> conf.aalevel)
3854 (fun v ->
3855 conf.aalevel <- bound v 0 8;
3856 state.anchor <- getanchor ();
3857 opendoc state.path state.password;
3859 src#string "page scroll scaling factor"
3860 (fun () -> string_of_float conf.pgscale)
3861 (fun v ->
3863 let s = float_of_string v in
3864 conf.pgscale <- s
3865 with exn ->
3866 state.text <- Printf.sprintf
3867 "bad page scroll scaling factor `%s': %s" v
3868 @@ exntos exn
3871 src#int "ui font size"
3872 (fun () -> fstate.fontsize)
3873 (fun v -> setfontsize (bound v 5 100));
3874 src#int "hint font size"
3875 (fun () -> conf.hfsize)
3876 (fun v -> conf.hfsize <- bound v 5 100);
3877 src#bool "crop hack"
3878 (fun () -> conf.crophack)
3879 (fun v -> conf.crophack <- v);
3880 src#string "trim fuzz"
3881 (fun () -> irect_to_string conf.trimfuzz)
3882 (fun v ->
3884 conf.trimfuzz <- irect_of_string v;
3885 if conf.trimmargins
3886 then settrim true conf.trimfuzz;
3887 with exn ->
3888 state.text <- Printf.sprintf "bad irect `%s': %s" v
3889 @@ exntos exn
3891 src#string "throttle"
3892 (fun () ->
3893 match conf.maxwait with
3894 | None -> "show place holder if page is not ready"
3895 | Some time ->
3896 if time = infinity
3897 then "wait for page to fully render"
3898 else
3899 "wait " ^ string_of_float time
3900 ^ " seconds before showing placeholder"
3902 (fun v ->
3904 let f = float_of_string v in
3905 if f <= 0.0
3906 then conf.maxwait <- None
3907 else conf.maxwait <- Some f
3908 with exn ->
3909 state.text <- Printf.sprintf "bad time `%s': %s" v
3910 @@ exntos exn
3912 src#string "ghyll scroll"
3913 (fun () ->
3914 match conf.ghyllscroll with
3915 | None -> E.s
3916 | Some nab -> ghyllscroll_to_string nab
3918 (fun v ->
3919 try conf.ghyllscroll <- ghyllscroll_of_string v
3920 with
3921 | Failure msg ->
3922 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3923 | exn ->
3924 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3925 @@ exntos exn
3927 src#string "selection command"
3928 (fun () -> conf.selcmd)
3929 (fun v -> conf.selcmd <- v);
3930 src#string "synctex command"
3931 (fun () -> conf.stcmd)
3932 (fun v -> conf.stcmd <- v);
3933 src#string "pax command"
3934 (fun () -> conf.paxcmd)
3935 (fun v -> conf.paxcmd <- v);
3936 src#string "ask password command"
3937 (fun () -> conf.passcmd)
3938 (fun v -> conf.passcmd <- v);
3939 src#string "save path command"
3940 (fun () -> conf.savecmd)
3941 (fun v -> conf.savecmd <- v);
3942 src#colorspace "color space"
3943 (fun () -> CSTE.to_string conf.colorspace)
3944 (fun v ->
3945 conf.colorspace <- CSTE.of_int v;
3946 wcmd "cs %d" v;
3947 load state.layout;
3949 src#paxmark "pax mark method"
3950 (fun () -> MTE.to_string conf.paxmark)
3951 (fun v -> conf.paxmark <- MTE.of_int v);
3952 if bousable () && !opengl_has_pbo
3953 then
3954 src#bool "use PBO"
3955 (fun () -> conf.usepbo)
3956 (fun v -> conf.usepbo <- v);
3957 src#bool "mouse wheel scrolls pages"
3958 (fun () -> conf.wheelbypage)
3959 (fun v -> conf.wheelbypage <- v);
3960 src#bool "open remote links in a new instance"
3961 (fun () -> conf.riani)
3962 (fun v -> conf.riani <- v);
3963 src#bool "edit annotations inline"
3964 (fun () -> conf.annotinline)
3965 (fun v -> conf.annotinline <- v);
3966 src#bool "coarse positioning in presentation mode"
3967 (fun () -> conf.coarseprespos)
3968 (fun v -> conf.coarseprespos <- v);
3969 src#bool "use document CSS"
3970 (fun () -> conf.usedoccss)
3971 (fun v ->
3972 conf.usedoccss <- v;
3973 state.anchor <- getanchor ();
3974 opendoc state.path state.password;
3976 src#bool ~btos "colors"
3977 (fun () -> !showcolors)
3978 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
3979 if !showcolors
3980 then (
3981 colorp " background"
3982 (fun () -> conf.bgcolor)
3983 (fun v -> conf.bgcolor <- v; setbgcol v);
3984 rgba " scrollbar"
3985 (fun () -> conf.sbarcolor)
3986 (fun v -> conf.sbarcolor <- v);
3987 rgba " scrollbar handle"
3988 (fun () -> conf.sbarhndlcolor)
3989 (fun v -> conf.sbarhndlcolor <- v);
3993 sep ();
3994 src#caption "Document" 0;
3995 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3996 src#caption2 "Pages"
3997 (fun () -> string_of_int state.pagecount) 1;
3998 src#caption2 "Dimensions"
3999 (fun () -> string_of_int (List.length state.pdims)) 1;
4000 if nonemptystr conf.css
4001 then src#caption2 "CSS" (fun () -> conf.css) 1;
4002 if conf.trimmargins
4003 then (
4004 sep ();
4005 src#caption "Trimmed margins" 0;
4006 src#caption2 "Dimensions"
4007 (fun () -> string_of_int (List.length state.pdims)) 1;
4010 sep ();
4011 src#caption "OpenGL" 0;
4012 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4013 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4015 sep ();
4016 src#caption "Location" 0;
4017 if nonemptystr state.origin
4018 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4019 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4021 src#reset prevmode prevuioh;
4023 fun () ->
4024 state.text <- E.s;
4025 resetmstate ();
4026 let prevmode = state.mode
4027 and prevuioh = state.uioh in
4028 fillsrc prevmode prevuioh;
4029 let source = (src :> lvsource) in
4030 let modehash = findkeyhash conf "info" in
4031 state.uioh <-
4032 coe (object (self)
4033 inherit listview ~zebra:false ~helpmode:false
4034 ~source ~trusted:true ~modehash as super
4035 val mutable m_prevmemused = 0
4036 method! infochanged = function
4037 | Memused ->
4038 if m_prevmemused != state.memused
4039 then (
4040 m_prevmemused <- state.memused;
4041 G.postRedisplay "memusedchanged";
4043 | Pdim -> G.postRedisplay "pdimchanged"
4044 | Docinfo -> fillsrc prevmode prevuioh
4046 method! key key mask =
4047 if not (Wsi.withctrl mask)
4048 then
4049 match [@warning "-4"] Wsi.kc2kt key with
4050 | Keys.Left -> coe (self#updownlevel ~-1)
4051 | Keys.Right -> coe (self#updownlevel 1)
4052 | _ -> super#key key mask
4053 else super#key key mask
4054 end);
4055 G.postRedisplay "info";
4058 let enterhelpmode =
4059 let source =
4060 (object
4061 inherit lvsourcebase
4062 method getitemcount = Array.length state.help
4063 method getitem n =
4064 let s, l, _ = state.help.(n) in
4065 (s, l)
4067 method exit ~uioh ~cancel ~active ~first ~pan =
4068 let optuioh =
4069 if not cancel
4070 then (
4071 match state.help.(active) with
4072 | _, _, Action f -> Some (f uioh)
4073 | _, _, Noaction -> Some uioh
4075 else None
4077 m_active <- active;
4078 m_first <- first;
4079 m_pan <- pan;
4080 optuioh
4082 method hasaction n =
4083 match state.help.(n) with
4084 | _, _, Action _ -> true
4085 | _, _, Noaction -> false
4087 initializer
4088 m_active <- -1
4089 end)
4090 in fun () ->
4091 let modehash = findkeyhash conf "help" in
4092 resetmstate ();
4093 state.uioh <- coe (new listview
4094 ~zebra:false ~helpmode:true
4095 ~source ~trusted:true ~modehash);
4096 G.postRedisplay "help";
4099 let entermsgsmode =
4100 let msgsource =
4101 (object
4102 inherit lvsourcebase
4103 val mutable m_items = E.a
4105 method getitemcount = 1 + Array.length m_items
4107 method getitem n =
4108 if n = 0
4109 then "[Clear]", 0
4110 else m_items.(n-1), 0
4112 method exit ~uioh ~cancel ~active ~first ~pan =
4113 ignore uioh;
4114 if not cancel
4115 then (
4116 if active = 0
4117 then Buffer.clear state.errmsgs;
4119 m_active <- active;
4120 m_first <- first;
4121 m_pan <- pan;
4122 None
4124 method hasaction n =
4125 n = 0
4127 method reset =
4128 state.newerrmsgs <- false;
4129 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4130 m_items <- Array.of_list l
4132 initializer
4133 m_active <- 0
4134 end)
4135 in fun () ->
4136 state.text <- E.s;
4137 resetmstate ();
4138 msgsource#reset;
4139 let source = (msgsource :> lvsource) in
4140 let modehash = findkeyhash conf "listview" in
4141 state.uioh <-
4142 coe (object
4143 inherit listview ~zebra:false ~helpmode:false
4144 ~source ~trusted:false ~modehash as super
4145 method! display =
4146 if state.newerrmsgs
4147 then msgsource#reset;
4148 super#display
4149 end);
4150 G.postRedisplay "msgs";
4153 let getusertext s =
4154 let editor = getenvwithdef "EDITOR" E.s in
4155 if emptystr editor
4156 then E.s
4157 else
4158 let tmppath = Filename.temp_file "llpp" "note" in
4159 if nonemptystr s
4160 then (
4161 let oc = open_out tmppath in
4162 output_string oc s;
4163 close_out oc;
4165 let execstr = editor ^ " " ^ tmppath in
4166 let s =
4167 match spawn execstr [] with
4168 | exception exn ->
4169 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4171 | pid ->
4172 match Unix.waitpid [] pid with
4173 | exception exn ->
4174 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4176 | (_pid, status) ->
4177 match status with
4178 | Unix.WEXITED 0 -> filecontents tmppath
4179 | Unix.WEXITED n ->
4180 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4182 | Unix.WSIGNALED n ->
4183 impmsg "editor process(%s) was killed by signal %d" execstr n;
4185 | Unix.WSTOPPED n ->
4186 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4189 match Unix.unlink tmppath with
4190 | exception exn ->
4191 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4193 | () -> s
4196 let enterannotmode opaque slinkindex =
4197 let msgsource =
4198 (object
4199 inherit lvsourcebase
4200 val mutable m_text = E.s
4201 val mutable m_items = E.a
4203 method getitemcount = Array.length m_items
4205 method getitem n =
4206 let label, _func = m_items.(n) in
4207 label, 0
4209 method exit ~uioh ~cancel ~active ~first ~pan =
4210 ignore (uioh, first, pan);
4211 if not cancel
4212 then (
4213 let _label, func = m_items.(active) in
4214 func ()
4216 None
4218 method hasaction n = nonemptystr @@ fst m_items.(n)
4220 method reset s =
4221 let rec split accu b i =
4222 let p = b+i in
4223 if p = String.length s
4224 then (String.sub s b (p-b), unit) :: accu
4225 else
4226 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4227 then
4228 let ss = if i = 0 then E.s else String.sub s b i in
4229 split ((ss, unit)::accu) (p+1) 0
4230 else
4231 split accu b (i+1)
4233 let cleanup () =
4234 wcmd "freepage %s" (~> opaque);
4235 let keys =
4236 Hashtbl.fold (fun key opaque' accu ->
4237 if opaque' = opaque'
4238 then key :: accu else accu) state.pagemap []
4240 List.iter (Hashtbl.remove state.pagemap) keys;
4241 flushtiles ();
4242 gotoxy state.x state.y
4244 let dele () =
4245 delannot opaque slinkindex;
4246 cleanup ();
4248 let edit inline () =
4249 let update s =
4250 if emptystr s
4251 then dele ()
4252 else (
4253 modannot opaque slinkindex s;
4254 cleanup ();
4257 if inline
4258 then
4259 let mode = state.mode in
4260 state.mode <-
4261 Textentry (
4262 ("annotation: ", m_text, None, textentry, update, true),
4263 fun _ -> state.mode <- mode);
4264 state.text <- E.s;
4265 enttext ();
4266 else
4267 let s = getusertext m_text in
4268 update s
4270 m_text <- s;
4271 m_items <-
4272 ( "[Copy]", fun () -> selstring m_text)
4273 :: ("[Delete]", dele)
4274 :: ("[Edit]", edit conf.annotinline)
4275 :: (E.s, unit)
4276 :: split [] 0 0 |> List.rev |> Array.of_list
4278 initializer
4279 m_active <- 0
4280 end)
4282 state.text <- E.s;
4283 let s = getannotcontents opaque slinkindex in
4284 resetmstate ();
4285 msgsource#reset s;
4286 let source = (msgsource :> lvsource) in
4287 let modehash = findkeyhash conf "listview" in
4288 state.uioh <- coe (object
4289 inherit listview ~zebra:false ~helpmode:false
4290 ~source ~trusted:false ~modehash
4291 end);
4292 G.postRedisplay "enterannotmode";
4295 let gotoremote spec =
4296 let filename, dest = splitatchar spec '#' in
4297 let getpath filename =
4298 let path =
4299 if nonemptystr filename
4300 then
4301 if Filename.is_relative filename
4302 then
4303 let dir = Filename.dirname state.path in
4304 let dir =
4305 if Filename.is_implicit dir
4306 then Filename.concat (Sys.getcwd ()) dir
4307 else dir
4309 Filename.concat dir filename
4310 else filename
4311 else E.s
4313 if Sys.file_exists path
4314 then path
4315 else E.s
4317 let path = getpath filename in
4318 let dospawn lcmd =
4319 if conf.riani
4320 then
4321 let cmd = Lazy.force_val lcmd in
4322 match spawn cmd with
4323 | _pid -> ()
4324 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
4325 else
4326 let anchor = getanchor () in
4327 let ranchor = state.path, state.password, anchor, state.origin in
4328 state.origin <- E.s;
4329 state.ranchors <- ranchor :: state.ranchors;
4330 opendoc path E.s;
4332 if substratis spec 0 "page="
4333 then
4334 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4335 | pageno ->
4336 state.anchor <- (pageno, 0.0, 0.0);
4337 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4338 | exception exn ->
4339 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4340 else (
4341 state.nameddest <- dest;
4342 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4346 let gotounder = function
4347 | Ulinkuri s when isexternallink s ->
4348 if substratis s 0 "file://"
4349 then gotoremote @@ String.sub s 7 (String.length s - 7)
4350 else gotouri s
4351 | Ulinkuri s ->
4352 let pageno, x, y = uritolocation s in
4353 addnav ();
4354 gotopagexy !wtmode pageno x y
4355 | Utext _ | Unone -> ()
4356 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4359 let gotooutline (_, _, kind) =
4360 match kind with
4361 | Onone -> ()
4362 | Oanchor anchor ->
4363 let (pageno, y, _) = anchor in
4364 let y = getanchory
4365 (if conf.presentation then (pageno, y, 1.0) else anchor)
4367 addnav ();
4368 gotoghyll y
4369 | Ouri uri -> gotounder (Ulinkuri uri)
4370 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4371 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4372 | Ohistory hist -> gotohist hist
4373 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4376 class outlinesoucebase fetchoutlines = object (self)
4377 inherit lvsourcebase
4378 val mutable m_items = E.a
4379 val mutable m_minfo = E.a
4380 val mutable m_orig_items = E.a
4381 val mutable m_orig_minfo = E.a
4382 val mutable m_narrow_patterns = []
4383 val mutable m_gen = -1
4385 method getitemcount = Array.length m_items
4387 method getitem n =
4388 let s, n, _ = m_items.(n) in
4389 (s, n+0)
4391 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4392 ignore (uioh, first);
4393 let items, minfo =
4394 if m_narrow_patterns = []
4395 then m_orig_items, m_orig_minfo
4396 else m_items, m_minfo
4398 m_pan <- pan;
4399 if not cancel
4400 then (
4401 m_items <- items;
4402 m_minfo <- minfo;
4403 gotooutline m_items.(active);
4405 else (
4406 m_items <- items;
4407 m_minfo <- minfo;
4409 None
4411 method hasaction (_:int) = true
4413 method greetmsg =
4414 if Array.length m_items != Array.length m_orig_items
4415 then
4416 let s =
4417 match m_narrow_patterns with
4418 | one :: [] -> one
4419 | many -> String.concat UniSyms.ellipsis (List.rev many)
4421 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4422 else E.s
4424 method statestr =
4425 match m_narrow_patterns with
4426 | [] -> E.s
4427 | one :: [] -> one
4428 | head :: _ -> UniSyms.ellipsis ^ head
4430 method narrow pattern =
4431 match Str.regexp_case_fold pattern with
4432 | exception _ -> ()
4433 | re ->
4434 let rec loop accu minfo n =
4435 if n = -1
4436 then (
4437 m_items <- Array.of_list accu;
4438 m_minfo <- Array.of_list minfo;
4440 else
4441 let (s, _, _) as o = m_items.(n) in
4442 let accu, minfo =
4443 match Str.search_forward re s 0 with
4444 | exception Not_found -> accu, minfo
4445 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4447 loop accu minfo (n-1)
4449 loop [] [] (Array.length m_items - 1)
4451 method! getminfo = m_minfo
4453 method denarrow =
4454 m_orig_items <- fetchoutlines ();
4455 m_minfo <- m_orig_minfo;
4456 m_items <- m_orig_items
4458 method add_narrow_pattern pattern =
4459 m_narrow_patterns <- pattern :: m_narrow_patterns
4461 method del_narrow_pattern =
4462 match m_narrow_patterns with
4463 | _ :: rest -> m_narrow_patterns <- rest
4464 | [] -> ()
4466 method renarrow =
4467 self#denarrow;
4468 match m_narrow_patterns with
4469 | pattern :: [] -> self#narrow pattern; pattern
4470 | list ->
4471 List.fold_left (fun accu pattern ->
4472 self#narrow pattern;
4473 pattern ^ UniSyms.ellipsis ^ accu) E.s list
4475 method calcactive (_:anchor) = 0
4477 method reset anchor items =
4478 if state.gen != m_gen
4479 then (
4480 m_orig_items <- items;
4481 m_items <- items;
4482 m_narrow_patterns <- [];
4483 m_minfo <- E.a;
4484 m_orig_minfo <- E.a;
4485 m_gen <- state.gen;
4487 else (
4488 if items != m_orig_items
4489 then (
4490 m_orig_items <- items;
4491 if m_narrow_patterns == []
4492 then m_items <- items;
4495 let active = self#calcactive anchor in
4496 m_active <- active;
4497 m_first <- firstof m_first active
4501 let outlinesource fetchoutlines =
4502 (object
4503 inherit outlinesoucebase fetchoutlines
4504 method! calcactive anchor =
4505 let rely = getanchory anchor in
4506 let rec loop n best bestd =
4507 if n = Array.length m_items
4508 then best
4509 else
4510 let _, _, kind = m_items.(n) in
4511 match kind with
4512 | Oanchor anchor ->
4513 let orely = getanchory anchor in
4514 let d = abs (orely - rely) in
4515 if d < bestd
4516 then loop (n+1) n d
4517 else loop (n+1) best bestd
4518 | Onone | Oremote _ | Olaunch _
4519 | Oremotedest _ | Ouri _ | Ohistory _ ->
4520 loop (n+1) best bestd
4522 loop 0 ~-1 max_int
4523 end)
4526 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4527 let mkselector sourcetype =
4528 let fetchoutlines () =
4529 match sourcetype with
4530 | `bookmarks -> Array.of_list state.bookmarks
4531 | `outlines -> state.outlines
4532 | `history -> genhistoutlines () |> Array.of_list
4534 let source =
4535 if sourcetype = `history
4536 then new outlinesoucebase fetchoutlines
4537 else outlinesource fetchoutlines
4539 (fun errmsg ->
4540 let outlines = fetchoutlines () in
4541 if Array.length outlines = 0
4542 then showtext ' ' errmsg
4543 else (
4544 resetmstate ();
4545 Wsi.setcursor Wsi.CURSOR_INHERIT;
4546 let anchor = getanchor () in
4547 source#reset anchor outlines;
4548 state.text <- source#greetmsg;
4549 state.uioh <-
4550 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4551 G.postRedisplay "enter selector";
4555 let mkenter sourcetype errmsg =
4556 let enter = mkselector sourcetype in
4557 fun () -> enter errmsg
4559 ( mkenter `outlines "document has no outline"
4560 , mkenter `bookmarks "document has no bookmarks (yet)"
4561 , mkenter `history "history is empty" )
4564 let quickbookmark ?title () =
4565 match state.layout with
4566 | [] -> ()
4567 | l :: _ ->
4568 let title =
4569 match title with
4570 | None ->
4571 Unix.(
4572 let tm = localtime (now ()) in
4573 Printf.sprintf
4574 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
4575 (l.pageno+1)
4576 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
4578 | Some title -> title
4580 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4583 let setautoscrollspeed step goingdown =
4584 let incr = max 1 ((abs step) / 2) in
4585 let incr = if goingdown then incr else -incr in
4586 let astep = boundastep state.winh (step + incr) in
4587 state.autoscroll <- Some astep;
4590 let canpan () =
4591 match conf.columns with
4592 | Csplit _ -> true
4593 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4596 let panbound x = bound x (-state.w) state.winw;;
4598 let existsinrow pageno (columns, coverA, coverB) p =
4599 let last = ((pageno - coverA) mod columns) + columns in
4600 let rec any = function
4601 | [] -> false
4602 | l :: rest ->
4603 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4604 then p l
4605 else (
4606 if not (p l)
4607 then (if l.pageno = last then false else any rest)
4608 else true
4611 any state.layout
4614 let nextpage () =
4615 match state.layout with
4616 | [] ->
4617 let pageno = page_of_y state.y in
4618 gotoghyll (getpagey (pageno+1))
4619 | l :: rest ->
4620 match conf.columns with
4621 | Csingle _ ->
4622 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4623 then
4624 let y = clamp (pgscale state.winh) in
4625 gotoghyll y
4626 else
4627 let pageno = min (l.pageno+1) (state.pagecount-1) in
4628 gotoghyll (getpagey pageno)
4629 | Cmulti ((c, _, _) as cl, _) ->
4630 if conf.presentation
4631 && (existsinrow l.pageno cl
4632 (fun l -> l.pageh > l.pagey + l.pagevh))
4633 then
4634 let y = clamp (pgscale state.winh) in
4635 gotoghyll y
4636 else
4637 let pageno = min (l.pageno+c) (state.pagecount-1) in
4638 gotoghyll (getpagey pageno)
4639 | Csplit (n, _) ->
4640 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4641 then
4642 let pagey, pageh = getpageyh l.pageno in
4643 let pagey = pagey + pageh * l.pagecol in
4644 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4645 gotoghyll (pagey + pageh + ips)
4648 let prevpage () =
4649 match state.layout with
4650 | [] ->
4651 let pageno = page_of_y state.y in
4652 gotoghyll (getpagey (pageno-1))
4653 | l :: _ ->
4654 match conf.columns with
4655 | Csingle _ ->
4656 if conf.presentation && l.pagey != 0
4657 then
4658 gotoghyll (clamp (pgscale ~-(state.winh)))
4659 else
4660 let pageno = max 0 (l.pageno-1) in
4661 gotoghyll (getpagey pageno)
4662 | Cmulti ((c, _, coverB) as cl, _) ->
4663 if conf.presentation &&
4664 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4665 then
4666 gotoghyll (clamp (pgscale ~-(state.winh)))
4667 else
4668 let decr =
4669 if l.pageno = state.pagecount - coverB
4670 then 1
4671 else c
4673 let pageno = max 0 (l.pageno-decr) in
4674 gotoghyll (getpagey pageno)
4675 | Csplit (n, _) ->
4676 let y =
4677 if l.pagecol = 0
4678 then
4679 if l.pageno = 0
4680 then l.pagey
4681 else
4682 let pageno = max 0 (l.pageno-1) in
4683 let pagey, pageh = getpageyh pageno in
4684 pagey + (n-1)*pageh
4685 else
4686 let pagey, pageh = getpageyh l.pageno in
4687 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4689 gotoghyll y
4692 let save () =
4693 if emptystr conf.savecmd
4694 then adderrmsg "savepath-command is empty"
4695 "don't know where to save modified document"
4696 else
4697 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4698 let path =
4699 getcmdoutput
4700 (fun exn ->
4701 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
4702 savecmd
4704 if nonemptystr path
4705 then
4706 let tmp = path ^ ".tmp" in
4707 savedoc tmp;
4708 Unix.rename tmp path;
4711 let viewkeyboard key mask =
4712 let enttext te =
4713 let mode = state.mode in
4714 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4715 state.text <- E.s;
4716 enttext ();
4717 G.postRedisplay "view:enttext"
4719 let ctrl = Wsi.withctrl mask in
4720 let open Keys in
4721 match Wsi.kc2kt key with
4722 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
4724 | Ascii 'Q' -> exit 0
4726 | Ascii 'W' ->
4727 if hasunsavedchanges ()
4728 then save ()
4730 | Insert ->
4731 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4732 then (
4733 state.mode <- (
4734 match state.lnava with
4735 | None -> LinkNav (Ltgendir 0)
4736 | Some pn -> LinkNav (Ltexact pn)
4738 gotoxy state.x state.y;
4740 else impmsg "keyboard link navigation does not work under rotation"
4742 | Escape | Ascii 'q' ->
4743 begin match state.mstate with
4744 | Mzoomrect _ ->
4745 resetmstate ();
4746 G.postRedisplay "kill rect";
4747 | Msel _
4748 | Mpan _
4749 | Mscrolly | Mscrollx
4750 | Mzoom _
4751 | Mnone ->
4752 begin match state.mode with
4753 | LinkNav ln ->
4754 begin match ln with
4755 | Ltexact pl -> state.lnava <- Some pl
4756 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4757 end;
4758 state.mode <- View;
4759 G.postRedisplay "esc leave linknav"
4760 | Birdseye _ | Textentry _ | View ->
4761 match state.ranchors with
4762 | [] -> raise Quit
4763 | (path, password, anchor, origin) :: rest ->
4764 state.ranchors <- rest;
4765 state.anchor <- anchor;
4766 state.origin <- origin;
4767 state.nameddest <- E.s;
4768 opendoc path password
4769 end;
4770 end;
4772 | Backspace ->
4773 addnavnorc ();
4774 gotoghyll (getnav ~-1)
4776 | Ascii 'o' ->
4777 enteroutlinemode ()
4779 | Ascii 'H' ->
4780 enterhistmode ()
4782 | Ascii 'u' ->
4783 state.rects <- [];
4784 state.text <- E.s;
4785 Hashtbl.iter (fun _ opaque ->
4786 clearmark opaque;
4787 Hashtbl.clear state.prects) state.pagemap;
4788 G.postRedisplay "dehighlight";
4790 | Ascii (('/' | '?') as c) ->
4791 let ondone isforw s =
4792 cbput state.hists.pat s;
4793 state.searchpattern <- s;
4794 search s isforw
4796 let s = String.make 1 c in
4797 enttext (s, E.s, Some (onhist state.hists.pat),
4798 textentry, ondone (c = '/'), true)
4800 | Ascii '+' | Ascii '=' when ctrl ->
4801 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4802 pivotzoom (conf.zoom +. incr)
4804 | Ascii '+' ->
4805 let ondone s =
4806 let n =
4807 try int_of_string s with exn ->
4808 state.text <-
4809 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4810 max_int
4812 if n != max_int
4813 then (
4814 conf.pagebias <- n;
4815 state.text <- "page bias is now " ^ string_of_int n;
4818 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4820 | Ascii '-' when ctrl ->
4821 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4822 pivotzoom (max 0.01 (conf.zoom -. decr))
4824 | Ascii '-' ->
4825 let ondone msg = state.text <- msg in
4826 enttext (
4827 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4828 optentry state.mode, ondone, true
4831 | Ascii '0' when ctrl ->
4832 if conf.zoom = 1.0
4833 then gotoxy 0 state.y
4834 else setzoom 1.0
4836 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
4837 let cols =
4838 match conf.columns with
4839 | Csingle _ | Cmulti _ -> 1
4840 | Csplit (n, _) -> n
4842 let h = state.winh -
4843 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4845 let zoom = zoomforh state.winw h 0 cols in
4846 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4847 then setzoom zoom
4849 | Ascii '3' when ctrl ->
4850 let fm =
4851 match conf.fitmodel with
4852 | FitWidth -> FitProportional
4853 | FitProportional -> FitPage
4854 | FitPage -> FitWidth
4856 state.text <- "fit model: " ^ FMTE.to_string fm;
4857 reqlayout conf.angle fm
4859 | Ascii '4' when ctrl ->
4860 let zoom = getmaxw () /. float state.winw in
4861 if zoom > 0.0 then setzoom zoom
4863 | Fn 9 ->
4864 togglebirdseye ()
4866 | Ascii '9' when ctrl ->
4867 togglebirdseye ()
4869 | Ascii ('0'..'9' as c) when not ctrl ->
4870 let ondone s =
4871 let n =
4872 try int_of_string s with exn ->
4873 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4876 if n >= 0
4877 then (
4878 addnav ();
4879 cbput state.hists.pag (string_of_int n);
4880 gotopage1 (n + conf.pagebias - 1) 0;
4883 let [@warning "-4"] pageentry text = function
4884 | Keys.Ascii 'g' -> TEdone text
4885 | key -> intentry text key
4887 let text = String.make 1 c in
4888 enttext (":", text, Some (onhist state.hists.pag),
4889 pageentry, ondone, true)
4891 | Ascii 'b' ->
4892 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4893 G.postRedisplay "toggle scrollbar";
4895 | Ascii 'B' ->
4896 state.bzoom <- not state.bzoom;
4897 state.rects <- [];
4898 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4900 | Ascii 'l' ->
4901 conf.hlinks <- not conf.hlinks;
4902 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4903 G.postRedisplay "toggle highlightlinks";
4905 | Ascii 'F' ->
4906 if conf.angle mod 360 = 0
4907 then (
4908 state.glinks <- true;
4909 let mode = state.mode in
4910 state.mode <-
4911 Textentry (
4912 (":", E.s, None, linknentry, linknact gotounder, false),
4913 (fun _ ->
4914 state.glinks <- false;
4915 state.mode <- mode)
4917 state.text <- E.s;
4918 G.postRedisplay "view:linkent(F)"
4920 else impmsg "hint mode does not work under rotation"
4922 | Ascii 'y' ->
4923 state.glinks <- true;
4924 let mode = state.mode in
4925 state.mode <-
4926 Textentry (
4927 (":", E.s, None, linknentry,
4928 linknact (fun under -> selstring (undertext under)), false),
4929 (fun _ ->
4930 state.glinks <- false;
4931 state.mode <- mode)
4933 state.text <- E.s;
4934 G.postRedisplay "view:linkent"
4936 | Ascii 'a' ->
4937 begin match state.autoscroll with
4938 | Some step ->
4939 conf.autoscrollstep <- step;
4940 state.autoscroll <- None
4941 | None ->
4942 state.autoscroll <- Some conf.autoscrollstep;
4943 state.slideshow <- state.slideshow land lnot 2
4946 | Ascii 'p' when ctrl ->
4947 launchpath () (* XXX where do error messages go? *)
4949 | Ascii 'P' ->
4950 setpresentationmode (not conf.presentation);
4951 showtext ' ' ("presentation mode " ^
4952 if conf.presentation then "on" else "off");
4954 | Ascii 'f' ->
4955 if List.mem Wsi.Fullscreen state.winstate
4956 then Wsi.reshape conf.cwinw conf.cwinh
4957 else Wsi.fullscreen ()
4959 | Ascii ('p'|'N') ->
4960 search state.searchpattern false
4962 | Ascii 'n' | Fn 3 ->
4963 search state.searchpattern true
4965 | Ascii 't' ->
4966 begin match state.layout with
4967 | [] -> ()
4968 | l :: _ ->
4969 gotoghyll (getpagey l.pageno)
4972 | Ascii ' ' ->
4973 nextpage ()
4975 | Delete ->
4976 prevpage ()
4978 | Ascii '=' ->
4979 showtext ' ' (describe_layout state.layout);
4981 | Ascii 'w' ->
4982 begin match state.layout with
4983 | [] -> ()
4984 | l :: _ ->
4985 Wsi.reshape l.pagew l.pageh;
4986 G.postRedisplay "w"
4989 | Ascii '\'' ->
4990 enterbookmarkmode ()
4992 | Ascii 'h' | Fn 1 ->
4993 enterhelpmode ()
4995 | Ascii 'i' ->
4996 enterinfomode ()
4998 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
4999 entermsgsmode ()
5001 | Ascii 'm' ->
5002 let ondone s =
5003 match state.layout with
5004 | l :: _ ->
5005 if nonemptystr s
5006 then
5007 state.bookmarks <-
5008 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5009 | _ -> ()
5011 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5013 | Ascii '~' ->
5014 quickbookmark ();
5015 showtext ' ' "Quick bookmark added";
5017 | Ascii 'z' ->
5018 begin match state.layout with
5019 | l :: _ ->
5020 let rect = getpdimrect l.pagedimno in
5021 let w, h =
5022 if conf.crophack
5023 then
5024 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5025 truncate (1.2 *. (rect.(3) -. rect.(0))))
5026 else
5027 (truncate (rect.(1) -. rect.(0)),
5028 truncate (rect.(3) -. rect.(0)))
5030 let w = truncate ((float w)*.conf.zoom)
5031 and h = truncate ((float h)*.conf.zoom) in
5032 if w != 0 && h != 0
5033 then (
5034 state.anchor <- getanchor ();
5035 Wsi.reshape w (h + conf.interpagespace)
5037 G.postRedisplay "z";
5039 | [] -> ()
5042 | Ascii 'x' -> state.roam ()
5044 | Ascii ('<'|'>' as c) ->
5045 reqlayout
5046 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
5048 | Ascii ('['|']' as c) ->
5049 conf.colorscale <-
5050 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
5051 G.postRedisplay "brightness";
5053 | Ascii 'c' when state.mode = View ->
5054 if Wsi.withalt mask
5055 then (
5056 if conf.zoom > 1.0
5057 then
5058 let m = (state.winw - state.w) / 2 in
5059 gotoxy_and_clear_text m state.y
5061 else
5062 let (c, a, b), z =
5063 match state.prevcolumns with
5064 | None -> (1, 0, 0), 1.0
5065 | Some (columns, z) ->
5066 let cab =
5067 match columns with
5068 | Csplit (c, _) -> -c, 0, 0
5069 | Cmulti ((c, a, b), _) -> c, a, b
5070 | Csingle _ -> 1, 0, 0
5072 cab, z
5074 setcolumns View c a b;
5075 setzoom z
5077 | Down | Up when ctrl && Wsi.withshift mask ->
5078 let zoom, x = state.prevzoom in
5079 setzoom zoom;
5080 state.x <- x;
5082 | Ascii 'k' | Up ->
5083 begin match state.autoscroll with
5084 | None ->
5085 begin match state.mode with
5086 | Birdseye beye -> upbirdseye 1 beye
5087 | Textentry _ | View | LinkNav _ ->
5088 if ctrl
5089 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5090 else (
5091 if not (Wsi.withshift mask) && conf.presentation
5092 then prevpage ()
5093 else gotoghyll1 true (clamp (-conf.scrollstep))
5096 | Some n ->
5097 setautoscrollspeed n false
5100 | Ascii 'j' | Down ->
5101 begin match state.autoscroll with
5102 | None ->
5103 begin match state.mode with
5104 | Birdseye beye -> downbirdseye 1 beye
5105 | Textentry _ | View | LinkNav _ ->
5106 if ctrl
5107 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5108 else (
5109 if not (Wsi.withshift mask) && conf.presentation
5110 then nextpage ()
5111 else gotoghyll1 true (clamp (conf.scrollstep))
5114 | Some n ->
5115 setautoscrollspeed n true
5118 | Left | Right when not (Wsi.withalt mask) ->
5119 if canpan ()
5120 then
5121 let dx =
5122 if ctrl
5123 then state.winw / 2
5124 else conf.hscrollstep
5126 let dx =
5127 let pv = Wsi.kc2kt key in
5128 if pv = Keys.Left then dx else -dx
5130 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5131 else (
5132 state.text <- E.s;
5133 G.postRedisplay "left/right"
5136 | Prior ->
5137 let y =
5138 if ctrl
5139 then
5140 match state.layout with
5141 | [] -> state.y
5142 | l :: _ -> state.y - l.pagey
5143 else
5144 clamp (pgscale (-state.winh))
5146 gotoghyll y
5148 | Next ->
5149 let y =
5150 if ctrl
5151 then
5152 match List.rev state.layout with
5153 | [] -> state.y
5154 | l :: _ -> getpagey l.pageno
5155 else
5156 clamp (pgscale state.winh)
5158 gotoghyll y
5160 | Ascii 'g' | Home ->
5161 addnav ();
5162 gotoghyll 0
5163 | Ascii 'G' | End ->
5164 addnav ();
5165 gotoghyll (clamp state.maxy)
5167 | Right when Wsi.withalt mask ->
5168 addnavnorc ();
5169 gotoghyll (getnav 1)
5170 | Left when Wsi.withalt mask ->
5171 addnavnorc ();
5172 gotoghyll (getnav ~-1)
5174 | Ascii 'r' ->
5175 reload ()
5177 | Ascii 'v' when conf.debug ->
5178 state.rects <- [];
5179 List.iter (fun l ->
5180 match getopaque l.pageno with
5181 | None -> ()
5182 | Some opaque ->
5183 let x0, y0, x1, y1 = pagebbox opaque in
5184 let rect = (float x0, float y0,
5185 float x1, float y0,
5186 float x1, float y1,
5187 float x0, float y1) in
5188 debugrect rect;
5189 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5190 state.rects <- (l.pageno, color, rect) :: state.rects;
5191 ) state.layout;
5192 G.postRedisplay "v";
5194 | Ascii '|' ->
5195 let mode = state.mode in
5196 let cmd = ref E.s in
5197 let onleave = function
5198 | Cancel -> state.mode <- mode
5199 | Confirm ->
5200 List.iter (fun l ->
5201 match getopaque l.pageno with
5202 | Some opaque -> pipesel opaque !cmd
5203 | None -> ()) state.layout;
5204 state.mode <- mode
5206 let ondone s =
5207 cbput state.hists.sel s;
5208 cmd := s
5210 let te =
5211 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5213 G.postRedisplay "|";
5214 state.mode <- Textentry (te, onleave);
5216 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
5217 vlog "huh? %s" (Wsi.keyname key)
5220 let linknavkeyboard key mask linknav =
5221 let pv = Wsi.kc2kt key in
5222 let getpage pageno =
5223 let rec loop = function
5224 | [] -> None
5225 | l :: _ when l.pageno = pageno -> Some l
5226 | _ :: rest -> loop rest
5227 in loop state.layout
5229 let doexact (pageno, n) =
5230 match getopaque pageno, getpage pageno with
5231 | Some opaque, Some l ->
5232 if pv = Keys.Enter
5233 then
5234 let under = getlink opaque n in
5235 G.postRedisplay "link gotounder";
5236 gotounder under;
5237 state.mode <- View;
5238 else
5239 let opt, dir =
5240 let open Keys in
5241 match pv with
5242 | Home -> Some (findlink opaque LDfirst), -1
5243 | End -> Some (findlink opaque LDlast), 1
5244 | Left -> Some (findlink opaque (LDleft n)), -1
5245 | Right -> Some (findlink opaque (LDright n)), 1
5246 | Up -> Some (findlink opaque (LDup n)), -1
5247 | Down -> Some (findlink opaque (LDdown n)), 1
5249 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
5250 | Code _|Fn _|Ctrl _|Backspace -> None, 0
5252 let pwl l dir =
5253 begin match findpwl l.pageno dir with
5254 | Pwlnotfound -> ()
5255 | Pwl pageno ->
5256 let notfound dir =
5257 state.mode <- LinkNav (Ltgendir dir);
5258 let y, h = getpageyh pageno in
5259 let y =
5260 if dir < 0
5261 then y + h - state.winh
5262 else y
5264 gotoxy state.x y
5266 begin match getopaque pageno, getpage pageno with
5267 | Some opaque, Some _ ->
5268 let link =
5269 let ld = if dir > 0 then LDfirst else LDlast in
5270 findlink opaque ld
5272 begin match link with
5273 | Lfound m ->
5274 showlinktype (getlink opaque m);
5275 state.mode <- LinkNav (Ltexact (pageno, m));
5276 G.postRedisplay "linknav jpage";
5277 | Lnotfound -> notfound dir
5278 end;
5279 | _ -> notfound dir
5280 end;
5281 end;
5283 begin match opt with
5284 | Some Lnotfound -> pwl l dir;
5285 | Some (Lfound m) ->
5286 if m = n
5287 then pwl l dir
5288 else (
5289 let _, y0, _, y1 = getlinkrect opaque m in
5290 if y0 < l.pagey
5291 then gotopage1 l.pageno y0
5292 else (
5293 let d = fstate.fontsize + 1 in
5294 if y1 - l.pagey > l.pagevh - d
5295 then gotopage1 l.pageno (y1 - state.winh + d)
5296 else G.postRedisplay "linknav";
5298 showlinktype (getlink opaque m);
5299 state.mode <- LinkNav (Ltexact (l.pageno, m));
5302 | None -> viewkeyboard key mask
5303 end;
5304 | _ -> viewkeyboard key mask
5306 if pv = Keys.Insert
5307 then (
5308 begin match linknav with
5309 | Ltexact pa -> state.lnava <- Some pa
5310 | Ltgendir _ | Ltnotready _ -> ()
5311 end;
5312 state.mode <- View;
5313 G.postRedisplay "leave linknav"
5315 else
5316 match linknav with
5317 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5318 | Ltexact exact -> doexact exact
5321 let keyboard key mask =
5322 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5323 then wcmd "interrupt"
5324 else state.uioh <- state.uioh#key key mask
5327 let birdseyekeyboard key mask
5328 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5329 let incr =
5330 match conf.columns with
5331 | Csingle _ -> 1
5332 | Cmulti ((c, _, _), _) -> c
5333 | Csplit _ -> failwith "bird's eye split mode"
5335 let pgh layout = List.fold_left
5336 (fun m l -> max l.pageh m) state.winh layout in
5337 let open Keys in
5338 match Wsi.kc2kt key with
5339 | Ascii 'l' when Wsi.withctrl mask ->
5340 let y, h = getpageyh pageno in
5341 let top = (state.winh - h) / 2 in
5342 gotoxy state.x (max 0 (y - top))
5343 | Enter -> leavebirdseye beye false
5344 | Escape -> leavebirdseye beye true
5345 | Up -> upbirdseye incr beye
5346 | Down -> downbirdseye incr beye
5347 | Left -> upbirdseye 1 beye
5348 | Right -> downbirdseye 1 beye
5350 | Prior ->
5351 begin match state.layout with
5352 | l :: _ ->
5353 if l.pagey != 0
5354 then (
5355 state.mode <- Birdseye (
5356 oconf, leftx, l.pageno, hooverpageno, anchor
5358 gotopage1 l.pageno 0;
5360 else (
5361 let layout = layout state.x (state.y-state.winh)
5362 state.winw
5363 (pgh state.layout) in
5364 match layout with
5365 | [] -> gotoxy state.x (clamp (-state.winh))
5366 | l :: _ ->
5367 state.mode <- Birdseye (
5368 oconf, leftx, l.pageno, hooverpageno, anchor
5370 gotopage1 l.pageno 0
5373 | [] -> gotoxy state.x (clamp (-state.winh))
5374 end;
5376 | Next ->
5377 begin match List.rev state.layout with
5378 | l :: _ ->
5379 let layout = layout state.x
5380 (state.y + (pgh state.layout))
5381 state.winw state.winh in
5382 begin match layout with
5383 | [] ->
5384 let incr = l.pageh - l.pagevh in
5385 if incr = 0
5386 then (
5387 state.mode <-
5388 Birdseye (
5389 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5391 G.postRedisplay "birdseye pagedown";
5393 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5395 | l :: _ ->
5396 state.mode <-
5397 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5398 gotopage1 l.pageno 0;
5401 | [] -> gotoxy state.x (clamp state.winh)
5402 end;
5404 | Home ->
5405 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5406 gotopage1 0 0
5408 | End ->
5409 let pageno = state.pagecount - 1 in
5410 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5411 if not (pagevisible state.layout pageno)
5412 then
5413 let h =
5414 match List.rev state.pdims with
5415 | [] -> state.winh
5416 | (_, _, h, _) :: _ -> h
5418 gotoxy
5419 state.x
5420 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5421 else G.postRedisplay "birdseye end";
5423 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
5426 let drawpage l =
5427 let color =
5428 match state.mode with
5429 | Textentry _ -> scalecolor 0.4
5430 | LinkNav _ | View -> scalecolor 1.0
5431 | Birdseye (_, _, pageno, hooverpageno, _) ->
5432 if l.pageno = hooverpageno
5433 then scalecolor 0.9
5434 else (
5435 if l.pageno = pageno
5436 then (
5437 let c = scalecolor 1.0 in
5438 GlDraw.color c;
5439 GlDraw.line_width 3.0;
5440 let dispx = l.pagedispx in
5441 linerect
5442 (float (dispx-1)) (float (l.pagedispy-1))
5443 (float (dispx+l.pagevw+1))
5444 (float (l.pagedispy+l.pagevh+1))
5446 GlDraw.line_width 1.0;
5449 else scalecolor 0.8
5452 drawtiles l color;
5455 let postdrawpage l linkindexbase =
5456 match getopaque l.pageno with
5457 | Some opaque ->
5458 if tileready l l.pagex l.pagey
5459 then
5460 let x = l.pagedispx - l.pagex
5461 and y = l.pagedispy - l.pagey in
5462 let hlmask =
5463 match conf.columns with
5464 | Csingle _ | Cmulti _ ->
5465 (if conf.hlinks then 1 else 0)
5466 + (if state.glinks
5467 && not (isbirdseye state.mode) then 2 else 0)
5468 | Csplit _ -> 0
5470 let s =
5471 match state.mode with
5472 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5473 | Textentry _
5474 | Birdseye _
5475 | View
5476 | LinkNav _ -> E.s
5478 Hashtbl.find_all state.prects l.pageno |>
5479 List.iter (fun vals -> drawprect opaque x y vals);
5480 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5481 if n < 0
5482 then (state.redisplay <- true; 0)
5483 else n
5484 else 0
5485 | _ -> 0
5488 let scrollindicator () =
5489 let sbw, ph, sh = state.uioh#scrollph in
5490 let sbh, pw, sw = state.uioh#scrollpw in
5492 let x0,x1,hx0 =
5493 if conf.leftscroll
5494 then (0, sbw, sbw)
5495 else ((state.winw - sbw), state.winw, 0)
5498 Gl.enable `blend;
5499 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5500 let (r, g, b, alpha) = conf.sbarcolor in
5501 GlDraw.color (r, g, b) ~alpha;
5502 filledrect (float x0) 0. (float x1) (float state.winh);
5503 filledrect
5504 (float hx0) (float (state.winh - sbh))
5505 (float (hx0 + state.winw)) (float state.winh);
5506 let (r, g, b, alpha) = conf.sbarhndlcolor in
5507 GlDraw.color (r, g, b) ~alpha;
5509 filledrect (float x0) ph (float x1) (ph +. sh);
5510 let pw = pw +. float hx0 in
5511 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5512 Gl.disable `blend;
5515 let showsel () =
5516 match state.mstate with
5517 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5520 | Msel ((x0, y0), (x1, y1)) ->
5521 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5522 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5523 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5524 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5527 let showrects =
5528 function [] -> ()
5529 | rects ->
5530 Gl.enable `blend;
5531 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5532 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5533 List.iter
5534 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5535 List.iter (fun l ->
5536 if l.pageno = pageno
5537 then (
5538 let dx = float (l.pagedispx - l.pagex) in
5539 let dy = float (l.pagedispy - l.pagey) in
5540 let r, g, b, alpha = c in
5541 GlDraw.color (r, g, b) ~alpha;
5542 filledrect2 (x0+.dx) (y0+.dy)
5543 (x1+.dx) (y1+.dy)
5544 (x3+.dx) (y3+.dy)
5545 (x2+.dx) (y2+.dy);
5547 ) state.layout
5548 ) rects;
5549 Gl.disable `blend;
5552 let display () =
5553 GlDraw.color (scalecolor2 conf.bgcolor);
5554 GlClear.color (scalecolor2 conf.bgcolor);
5555 GlClear.clear [`color];
5556 List.iter drawpage state.layout;
5557 let rects =
5558 match state.mode with
5559 | LinkNav (Ltexact (pageno, linkno)) ->
5560 begin match getopaque pageno with
5561 | Some opaque ->
5562 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5563 let color = (0.0, 0.0, 0.5, 0.5) in
5564 (pageno, color,
5565 (float x0, float y0,
5566 float x1, float y0,
5567 float x1, float y1,
5568 float x0, float y1)
5569 ) :: state.rects
5570 | None -> state.rects
5572 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5573 | Birdseye _
5574 | Textentry _
5575 | View -> state.rects
5577 showrects rects;
5578 let rec postloop linkindexbase = function
5579 | l :: rest ->
5580 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5581 postloop linkindexbase rest
5582 | [] -> ()
5584 showsel ();
5585 postloop 0 state.layout;
5586 state.uioh#display;
5587 begin match state.mstate with
5588 | Mzoomrect ((x0, y0), (x1, y1)) ->
5589 Gl.enable `blend;
5590 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5591 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5592 filledrect (float x0) (float y0) (float x1) (float y1);
5593 Gl.disable `blend;
5594 | Msel _
5595 | Mpan _
5596 | Mscrolly | Mscrollx
5597 | Mzoom _
5598 | Mnone -> ()
5599 end;
5600 enttext ();
5601 scrollindicator ();
5602 Wsi.swapb ();
5605 let zoomrect x y x1 y1 =
5606 let x0 = min x x1
5607 and x1 = max x x1
5608 and y0 = min y y1 in
5609 let zoom = (float state.w) /. float (x1 - x0) in
5610 let margin =
5611 let simple () =
5612 if state.w < state.winw
5613 then (state.winw - state.w) / 2
5614 else 0
5616 match conf.fitmodel with
5617 | FitWidth | FitProportional -> simple ()
5618 | FitPage ->
5619 match conf.columns with
5620 | Csplit _ ->
5621 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5622 | Cmulti _ | Csingle _ -> simple ()
5624 gotoxy ((state.x + margin) - x0) (state.y + y0);
5625 state.anchor <- getanchor ();
5626 setzoom zoom;
5627 resetmstate ();
5630 let annot inline x y =
5631 match unproject x y with
5632 | Some (opaque, n, ux, uy) ->
5633 let add text =
5634 addannot opaque ux uy text;
5635 wcmd "freepage %s" (~> opaque);
5636 Hashtbl.remove state.pagemap (n, state.gen);
5637 flushtiles ();
5638 gotoxy state.x state.y
5640 if inline
5641 then
5642 let ondone s = add s in
5643 let mode = state.mode in
5644 state.mode <- Textentry (
5645 ("annotation: ", E.s, None, textentry, ondone, true),
5646 fun _ -> state.mode <- mode);
5647 state.text <- E.s;
5648 enttext ();
5649 G.postRedisplay "annot"
5650 else
5651 add @@ getusertext E.s
5652 | _ -> ()
5655 let zoomblock x y =
5656 let g opaque l px py =
5657 match rectofblock opaque px py with
5658 | Some a ->
5659 let x0 = a.(0) -. 20. in
5660 let x1 = a.(1) +. 20. in
5661 let y0 = a.(2) -. 20. in
5662 let zoom = (float state.w) /. (x1 -. x0) in
5663 let pagey = getpagey l.pageno in
5664 let margin = (state.w - l.pagew)/2 in
5665 let nx = -truncate x0 - margin in
5666 gotoxy_and_clear_text nx (pagey + truncate y0);
5667 state.anchor <- getanchor ();
5668 setzoom zoom;
5669 None
5670 | None -> None
5672 match conf.columns with
5673 | Csplit _ ->
5674 impmsg "block zooming does not work properly in split columns mode"
5675 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5678 let scrollx x =
5679 let winw = state.winw - 1 in
5680 let s = float x /. float winw in
5681 let destx = truncate (float (state.w + winw) *. s) in
5682 gotoxy_and_clear_text (winw - destx) state.y;
5683 state.mstate <- Mscrollx;
5686 let scrolly y =
5687 let s = float y /. float state.winh in
5688 let desty = truncate (s *. float (maxy ())) in
5689 gotoxy_and_clear_text state.x desty;
5690 state.mstate <- Mscrolly;
5693 let viewmulticlick clicks x y mask =
5694 let g opaque l px py =
5695 let mark =
5696 match clicks with
5697 | 2 -> Mark_word
5698 | 3 -> Mark_line
5699 | 4 -> Mark_block
5700 | _ -> Mark_page
5702 if markunder opaque px py mark
5703 then (
5704 Some (fun () ->
5705 let dopipe cmd =
5706 match getopaque l.pageno with
5707 | None -> ()
5708 | Some opaque -> pipesel opaque cmd
5710 state.roam <- (fun () -> dopipe conf.paxcmd);
5711 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5714 else None
5716 G.postRedisplay "viewmulticlick";
5717 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5720 let canselect () =
5721 match conf.columns with
5722 | Csplit _ -> false
5723 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5726 let viewmouse button down x y mask =
5727 match button with
5728 | n when (n == 4 || n == 5) && not down ->
5729 if Wsi.withctrl mask
5730 then (
5731 let incr =
5732 if n = 5
5733 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5734 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5736 let fx, fy =
5737 match state.mstate with
5738 | Mzoom (oldn, _, pos) when n = oldn -> pos
5739 | Mzoomrect _ | Mnone | Mpan _
5740 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
5742 let zoom = conf.zoom -. incr in
5743 state.mstate <- Mzoom (n, 0, (x, y));
5744 if false && abs (fx - x) > 5 || abs (fy - y) > 5
5745 then pivotzoom ~x ~y zoom
5746 else pivotzoom zoom
5748 else (
5749 match state.autoscroll with
5750 | Some step -> setautoscrollspeed step (n=4)
5751 | None ->
5752 if conf.wheelbypage || conf.presentation
5753 then (
5754 if n = 4
5755 then prevpage ()
5756 else nextpage ()
5758 else
5759 let incr =
5760 if n = 4
5761 then -conf.scrollstep
5762 else conf.scrollstep
5764 let incr = incr * 2 in
5765 let y = clamp incr in
5766 gotoxy_and_clear_text state.x y
5769 | n when (n = 6 || n = 7) && not down && canpan () ->
5770 let x =
5771 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5772 gotoxy_and_clear_text x state.y
5774 | 1 when Wsi.withshift mask ->
5775 state.mstate <- Mnone;
5776 if not down
5777 then (
5778 match unproject x y with
5779 | None -> ()
5780 | Some (_, pageno, ux, uy) ->
5781 let cmd = Printf.sprintf
5782 "%s %s %d %d %d"
5783 conf.stcmd state.path pageno ux uy
5785 match spawn cmd [] with
5786 | exception exn ->
5787 impmsg "execution of synctex command(%S) failed: %S"
5788 conf.stcmd @@ exntos exn
5789 | _pid -> ()
5792 | 1 when Wsi.withctrl mask ->
5793 if down
5794 then (
5795 Wsi.setcursor Wsi.CURSOR_FLEUR;
5796 state.mstate <- Mpan (x, y)
5798 else
5799 state.mstate <- Mnone
5801 | 3 ->
5802 if down
5803 then (
5804 if Wsi.withshift mask
5805 then (
5806 annot conf.annotinline x y;
5807 G.postRedisplay "addannot"
5809 else
5810 let p = (x, y) in
5811 Wsi.setcursor Wsi.CURSOR_CYCLE;
5812 state.mstate <- Mzoomrect (p, p)
5814 else (
5815 match state.mstate with
5816 | Mzoomrect ((x0, y0), _) ->
5817 if abs (x-x0) > 10 && abs (y - y0) > 10
5818 then zoomrect x0 y0 x y
5819 else (
5820 resetmstate ();
5821 G.postRedisplay "kill accidental zoom rect";
5823 | Msel _
5824 | Mpan _
5825 | Mscrolly | Mscrollx
5826 | Mzoom _
5827 | Mnone -> resetmstate ()
5830 | 1 when vscrollhit x ->
5831 if down
5832 then
5833 let _, position, sh = state.uioh#scrollph in
5834 if y > truncate position && y < truncate (position +. sh)
5835 then state.mstate <- Mscrolly
5836 else scrolly y
5837 else
5838 state.mstate <- Mnone
5840 | 1 when y > state.winh - hscrollh () ->
5841 if down
5842 then
5843 let _, position, sw = state.uioh#scrollpw in
5844 if x > truncate position && x < truncate (position +. sw)
5845 then state.mstate <- Mscrollx
5846 else scrollx x
5847 else
5848 state.mstate <- Mnone
5850 | 1 when state.bzoom -> if not down then zoomblock x y
5852 | 1 ->
5853 let dest = if down then getunder x y else Unone in
5854 begin match dest with
5855 | Ulinkuri _ ->
5856 gotounder dest
5858 | Unone when down ->
5859 Wsi.setcursor Wsi.CURSOR_FLEUR;
5860 state.mstate <- Mpan (x, y);
5862 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5864 | Unone | Utext _ ->
5865 if down
5866 then (
5867 if canselect ()
5868 then (
5869 state.mstate <- Msel ((x, y), (x, y));
5870 G.postRedisplay "mouse select";
5873 else (
5874 match state.mstate with
5875 | Mnone -> ()
5877 | Mzoom _ | Mscrollx | Mscrolly ->
5878 state.mstate <- Mnone
5880 | Mzoomrect ((x0, y0), _) ->
5881 zoomrect x0 y0 x y
5883 | Mpan _ ->
5884 Wsi.setcursor Wsi.CURSOR_INHERIT;
5885 state.mstate <- Mnone
5887 | Msel ((x0, y0), (x1, y1)) ->
5888 let rec loop = function
5889 | [] -> ()
5890 | l :: rest ->
5891 let inside =
5892 let a0 = l.pagedispy in
5893 let a1 = a0 + l.pagevh in
5894 let b0 = l.pagedispx in
5895 let b1 = b0 + l.pagevw in
5896 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5897 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5899 if inside
5900 then
5901 match getopaque l.pageno with
5902 | Some opaque ->
5903 let dosel cmd () =
5904 pipef ~closew:false "Msel"
5905 (fun w ->
5906 copysel w opaque;
5907 G.postRedisplay "Msel") cmd
5909 dosel conf.selcmd ();
5910 state.roam <- dosel conf.paxcmd;
5911 | None -> ()
5912 else loop rest
5914 loop state.layout;
5915 resetmstate ();
5919 | _ -> ()
5922 let birdseyemouse button down x y mask
5923 (conf, leftx, _, hooverpageno, anchor) =
5924 match button with
5925 | 1 when down ->
5926 let rec loop = function
5927 | [] -> ()
5928 | l :: rest ->
5929 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5930 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5931 then (
5932 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5934 else loop rest
5936 loop state.layout
5937 | 3 -> ()
5938 | _ -> viewmouse button down x y mask
5941 let uioh = object
5942 method display = ()
5944 method key key mask =
5945 begin match state.mode with
5946 | Textentry textentry -> textentrykeyboard key mask textentry
5947 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5948 | View -> viewkeyboard key mask
5949 | LinkNav linknav -> linknavkeyboard key mask linknav
5950 end;
5951 state.uioh
5953 method button button bstate x y mask =
5954 begin match state.mode with
5955 | LinkNav _ | View -> viewmouse button bstate x y mask
5956 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5957 | Textentry _ -> ()
5958 end;
5959 state.uioh
5961 method multiclick clicks x y mask =
5962 begin match state.mode with
5963 | LinkNav _ | View -> viewmulticlick clicks x y mask
5964 | Birdseye _ | Textentry _ -> ()
5965 end;
5966 state.uioh
5968 method motion x y =
5969 begin match state.mode with
5970 | Textentry _ -> ()
5971 | View | Birdseye _ | LinkNav _ ->
5972 match state.mstate with
5973 | Mzoom _ | Mnone -> ()
5975 | Mpan (x0, y0) ->
5976 let dx = x - x0
5977 and dy = y0 - y in
5978 state.mstate <- Mpan (x, y);
5979 let x = if canpan () then panbound (state.x + dx) else state.x in
5980 let y = clamp dy in
5981 gotoxy_and_clear_text x y
5983 | Msel (a, _) ->
5984 state.mstate <- Msel (a, (x, y));
5985 G.postRedisplay "motion select";
5987 | Mscrolly ->
5988 let y = min state.winh (max 0 y) in
5989 scrolly y
5991 | Mscrollx ->
5992 let x = min state.winw (max 0 x) in
5993 scrollx x
5995 | Mzoomrect (p0, _) ->
5996 state.mstate <- Mzoomrect (p0, (x, y));
5997 G.postRedisplay "motion zoomrect";
5998 end;
5999 state.uioh
6001 method pmotion x y =
6002 begin match state.mode with
6003 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6004 let rec loop = function
6005 | [] ->
6006 if hooverpageno != -1
6007 then (
6008 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6009 G.postRedisplay "pmotion birdseye no hoover";
6011 | l :: rest ->
6012 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6013 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6014 then (
6015 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6016 G.postRedisplay "pmotion birdseye hoover";
6018 else loop rest
6020 loop state.layout
6022 | Textentry _ -> ()
6024 | LinkNav _ | View ->
6025 match state.mstate with
6026 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6027 | Mnone ->
6028 updateunder x y;
6029 if canselect ()
6030 then
6031 match conf.pax with
6032 | None -> ()
6033 | Some past ->
6034 let now = now () in
6035 let delta = now -. past in
6036 if delta > 0.01
6037 then paxunder x y
6038 else conf.pax <- Some now
6039 end;
6040 state.uioh
6042 method infochanged _ = ()
6044 method scrollph =
6045 let maxy = maxy () in
6046 let p, h =
6047 if maxy = 0
6048 then 0.0, float state.winh
6049 else scrollph state.y maxy
6051 vscrollw (), p, h
6053 method scrollpw =
6054 let fwinw = float (state.winw - vscrollw ()) in
6055 let sw =
6056 let sw = fwinw /. float state.w in
6057 let sw = fwinw *. sw in
6058 max sw (float conf.scrollh)
6060 let position =
6061 let maxx = state.w + state.winw in
6062 let x = state.winw - state.x in
6063 let percent = float x /. float maxx in
6064 (fwinw -. sw) *. percent
6066 hscrollh (), position, sw
6068 method modehash =
6069 let modename =
6070 match state.mode with
6071 | LinkNav _ -> "links"
6072 | Textentry _ -> "textentry"
6073 | Birdseye _ -> "birdseye"
6074 | View -> "view"
6076 findkeyhash conf modename
6078 method eformsgs = true
6079 method alwaysscrolly = false
6080 method scroll dx dy =
6081 let x = if canpan () then panbound (state.x + dx) else state.x in
6082 gotoxy_and_clear_text x (clamp (2 * dy));
6083 state.uioh
6084 method zoom z x y =
6085 pivotzoom ~x ~y (conf.zoom *. exp z);
6086 end;;
6088 let addrect pageno r g b a x0 y0 x1 y1 =
6089 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6092 let ract cmds =
6093 let cl = splitatchar cmds ' ' in
6094 let scan s fmt f =
6095 try Scanf.sscanf s fmt f
6096 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
6097 cmds @@ exntos exn
6099 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6100 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6101 s pageno r g b a x0 y0 x1 y1;
6102 onpagerect
6103 pageno
6104 (fun w h ->
6105 let _,w1,h1,_ = getpagedim pageno in
6106 let sw = float w1 /. float w
6107 and sh = float h1 /. float h in
6108 let x0s = x0 *. sw
6109 and x1s = x1 *. sw
6110 and y0s = y0 *. sh
6111 and y1s = y1 *. sh in
6112 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6113 let color = (r, g, b, a) in
6114 if conf.verbose then debugrect rect;
6115 state.rects <- (pageno, color, rect) :: state.rects;
6116 G.postRedisplay s;
6119 match cl with
6120 | "reload", "" -> reload ()
6121 | "goto", args ->
6122 scan args "%u %f %f"
6123 (fun pageno x y ->
6124 let cmd, _ = state.geomcmds in
6125 if emptystr cmd
6126 then gotopagexy !wtmode pageno x y
6127 else
6128 let f prevf () =
6129 gotopagexy !wtmode pageno x y;
6130 prevf ()
6132 state.reprf <- f state.reprf
6134 | "goto1", args -> scan args "%u %f" gotopage
6135 | "gotor", args -> scan args "%S" gotoremote
6136 | "rect", args ->
6137 scan args "%u %u %f %f %f %f"
6138 (fun pageno c x0 y0 x1 y1 ->
6139 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6140 rectx "rect" pageno color x0 y0 x1 y1;
6142 | "prect", args ->
6143 scan args "%u %f %f %f %f %f %f %f %f"
6144 (fun pageno r g b alpha x0 y0 x1 y1 ->
6145 addrect pageno r g b alpha x0 y0 x1 y1;
6146 G.postRedisplay "prect"
6148 | "pgoto", args ->
6149 scan args "%u %f %f"
6150 (fun pageno x y ->
6151 let optopaque =
6152 match getopaque pageno with
6153 | Some opaque -> opaque
6154 | None -> ~< E.s
6156 pgoto optopaque pageno x y;
6157 let rec fixx = function
6158 | [] -> ()
6159 | l :: rest ->
6160 if l.pageno = pageno
6161 then gotoxy (state.x - l.pagedispx) state.y
6162 else fixx rest
6164 let layout =
6165 let mult =
6166 match conf.columns with
6167 | Csingle _ | Csplit _ -> 1
6168 | Cmulti ((n, _, _), _) -> n
6170 layout 0 state.y (state.winw * mult) state.winh
6172 fixx layout
6174 | "activatewin", "" -> Wsi.activatewin ()
6175 | "quit", "" -> raise Quit
6176 | "keys", keys ->
6177 begin try
6178 let l = Config.keys_of_string keys in
6179 List.iter (fun (k, m) -> keyboard k m) l
6180 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
6181 cmds @@ exntos exn
6183 | "clearrects", "" ->
6184 Hashtbl.clear state.prects;
6185 G.postRedisplay "clearrects"
6186 | _ ->
6187 adderrfmt "remote command"
6188 "error processing remote command: %S\n" cmds;
6191 let remote =
6192 let scratch = Bytes.create 80 in
6193 let buf = Buffer.create 80 in
6194 fun fd ->
6195 match tempfailureretry (Unix.read fd scratch 0) 80 with
6196 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6197 | 0 ->
6198 Unix.close fd;
6199 if Buffer.length buf > 0
6200 then (
6201 let s = Buffer.contents buf in
6202 Buffer.clear buf;
6203 ract s;
6205 None
6206 | n ->
6207 let rec eat ppos =
6208 let nlpos =
6209 match Bytes.index_from scratch ppos '\n' with
6210 | pos -> if pos >= n then -1 else pos
6211 | exception Not_found -> -1
6213 if nlpos >= 0
6214 then (
6215 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6216 let s = Buffer.contents buf in
6217 Buffer.clear buf;
6218 ract s;
6219 eat (nlpos+1);
6221 else (
6222 Buffer.add_subbytes buf scratch ppos (n-ppos);
6223 Some fd
6225 in eat 0
6228 let remoteopen path =
6229 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6230 with exn ->
6231 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6232 None
6235 let () =
6236 let gcconfig = ref false in
6237 let trimcachepath = ref E.s in
6238 let rcmdpath = ref E.s in
6239 let pageno = ref None in
6240 let rootwid = ref 0 in
6241 let openlast = ref false in
6242 let doreap = ref false in
6243 let csspath = ref None in
6244 selfexec := Sys.executable_name;
6245 Arg.parse
6246 (Arg.align
6247 [("-p", Arg.String (fun s -> state.password <- s),
6248 "<password> Set password");
6250 ("-f", Arg.String
6251 (fun s ->
6252 Config.fontpath := s;
6253 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6255 "<path> Set path to the user interface font");
6257 ("-c", Arg.String
6258 (fun s ->
6259 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6260 Config.confpath := s),
6261 "<path> Set path to the configuration file");
6263 ("-last", Arg.Set openlast, " Open last document");
6265 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6266 "<page-number> Jump to page");
6268 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6269 "<path> Set path to the trim cache file");
6271 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6272 "<named-destination> Set named destination");
6274 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6275 ("-cxack", Arg.Set cxack, " Cut corners");
6277 ("-remote", Arg.String (fun s -> rcmdpath := s),
6278 "<path> Set path to the source of remote commands");
6280 ("-gc", Arg.Set gcconfig, " Collect config garbage");
6282 ("-v", Arg.Unit (fun () ->
6283 Printf.printf
6284 "%s\nconfiguration path: %s\n"
6285 (version ())
6286 Config.defconfpath;
6287 exit 0), " Print version and exit");
6289 ("-css", Arg.String (fun s -> csspath := Some s),
6290 "<path> Set path to the style sheet to use with EPUB/HTML");
6292 ("-embed", Arg.Set_int rootwid, "<window-id> Embed into window");
6294 ("-origin", Arg.String (fun s -> state.origin <- s),
6295 "<origin> <undocumented>");
6298 (fun s -> state.path <- s)
6299 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6301 if !wtmode
6302 then selfexec := !selfexec ^ " -wtmode";
6304 let histmode = emptystr state.path && not !openlast in
6306 if not (Config.load !openlast)
6307 then dolog "failed to load configuration";
6309 begin match !pageno with
6310 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6311 | None -> ()
6312 end;
6314 if !gcconfig
6315 then (
6316 Config.gc ();
6317 exit 0
6320 let mu =
6321 object (self)
6322 val mutable m_clicks = 0
6323 val mutable m_click_x = 0
6324 val mutable m_click_y = 0
6325 val mutable m_lastclicktime = infinity
6327 method private cleanup =
6328 state.roam <- noroam;
6329 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6330 method expose = G.postRedisplay "expose"
6331 method visible v =
6332 let name =
6333 match v with
6334 | Wsi.Unobscured -> "unobscured"
6335 | Wsi.PartiallyObscured -> "partiallyobscured"
6336 | Wsi.FullyObscured -> "fullyobscured"
6338 vlog "visibility change %s" name
6339 method display = display ()
6340 method map mapped = vlog "mapped %b" mapped
6341 method reshape w h =
6342 self#cleanup;
6343 reshape w h
6344 method mouse b d x y m =
6345 if d && canselect ()
6346 then (
6348 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6350 m_click_x <- x;
6351 m_click_y <- y;
6352 if b = 1
6353 then (
6354 let t = now () in
6355 if abs x - m_click_x > 10
6356 || abs y - m_click_y > 10
6357 || abs_float (t -. m_lastclicktime) > 0.3
6358 then m_clicks <- 0;
6359 m_clicks <- m_clicks + 1;
6360 m_lastclicktime <- t;
6361 if m_clicks = 1
6362 then (
6363 self#cleanup;
6364 G.postRedisplay "cleanup";
6365 state.uioh <- state.uioh#button b d x y m;
6367 else state.uioh <- state.uioh#multiclick m_clicks x y m
6369 else (
6370 self#cleanup;
6371 m_clicks <- 0;
6372 m_lastclicktime <- infinity;
6373 state.uioh <- state.uioh#button b d x y m
6376 else (
6377 state.uioh <- state.uioh#button b d x y m
6379 method motion x y =
6380 state.mpos <- (x, y);
6381 state.uioh <- state.uioh#motion x y
6382 method pmotion x y =
6383 state.mpos <- (x, y);
6384 state.uioh <- state.uioh#pmotion x y
6385 method key k m =
6386 vlog "k=%#x m=%#x" k m;
6387 let mascm = m land (
6388 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6389 ) in
6390 let keyboard k m =
6391 let x = state.x and y = state.y in
6392 keyboard k m;
6393 if x != state.x || y != state.y then self#cleanup
6395 match state.keystate with
6396 | KSnone ->
6397 let km = k, mascm in
6398 begin
6399 match
6400 let modehash = state.uioh#modehash in
6401 try Hashtbl.find modehash km
6402 with Not_found ->
6403 try Hashtbl.find (findkeyhash conf "global") km
6404 with Not_found -> KMinsrt (k, m)
6405 with
6406 | KMinsrt (k, m) -> keyboard k m
6407 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6408 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6410 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6411 List.iter (fun (k, m) -> keyboard k m) insrt;
6412 state.keystate <- KSnone
6413 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6414 state.keystate <- KSinto (keys, insrt)
6415 | KSinto _ -> state.keystate <- KSnone
6417 method enter x y =
6418 state.mpos <- (x, y);
6419 state.uioh <- state.uioh#pmotion x y
6420 method leave = state.mpos <- (-1, -1)
6421 method winstate wsl = state.winstate <- wsl
6422 method quit : 'a. 'a = raise Quit
6423 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
6424 method zoom z x y = state.uioh#zoom z x y
6425 method opendoc path =
6426 state.mode <- View;
6427 state.uioh <- uioh;
6428 G.postRedisplay "opendoc";
6429 opendoc path state.password
6432 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6434 setbgcol conf.bgcolor;
6435 state.wsfd <- wsfd;
6437 if not @@ List.exists GlMisc.check_extension
6438 [ "GL_ARB_texture_rectangle"
6439 ; "GL_EXT_texture_recangle"
6440 ; "GL_NV_texture_rectangle" ]
6441 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6443 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6444 then (
6445 defconf.sliceheight <- 1024;
6446 defconf.texcount <- 32;
6447 defconf.usepbo <- true;
6450 let cs, ss =
6451 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6452 | exception exn ->
6453 dolog "socketpair failed: %s" @@ exntos exn;
6454 exit 1
6455 | (r, w) ->
6456 cloexec r;
6457 cloexec w;
6458 r, w
6461 setcheckers conf.checkers;
6463 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6465 begin match !csspath with
6466 | None -> ()
6467 | Some "" -> conf.css <- E.s
6468 | Some path ->
6469 let css = filecontents path in
6470 let l = String.length css in
6471 conf.css <-
6472 if substratis css (l-2) "\r\n"
6473 then String.sub css 0 (l-2)
6474 else (if css.[l-1] = '\n'
6475 then String.sub css 0 (l-1)
6476 else css);
6477 end;
6478 init cs (
6479 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6480 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6481 !Config.fontpath, !trimcachepath, !opengl_has_pbo
6483 List.iter GlArray.enable [`texture_coord; `vertex];
6484 state.ss <- ss;
6485 reshape ~firsttime:true winw winh;
6486 state.uioh <- uioh;
6487 if histmode
6488 then (
6489 Wsi.settitle "llpp (history)";
6490 enterhistmode ();
6492 else (
6493 state.text <- "Opening " ^ (mbtoutf8 state.path);
6494 opendoc state.path state.password;
6496 display ();
6497 Wsi.mapwin ();
6498 Wsi.setcursor Wsi.CURSOR_INHERIT;
6499 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6501 let rec reap () =
6502 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6503 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
6504 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
6505 | 0, _ -> ()
6506 | _pid, _status -> reap ()
6508 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6510 let optrfd =
6511 ref (
6512 if nonemptystr !rcmdpath
6513 then remoteopen !rcmdpath
6514 else None
6518 let rec loop deadline =
6519 if !doreap
6520 then (
6521 doreap := false;
6522 reap ()
6524 let r = [state.ss; state.wsfd] in
6525 let r =
6526 match !optrfd with
6527 | None -> r
6528 | Some fd -> fd :: r
6530 if state.redisplay
6531 then (
6532 state.redisplay <- false;
6533 display ();
6535 let timeout =
6536 let now = now () in
6537 if deadline > now
6538 then (
6539 if deadline = infinity
6540 then ~-.1.0
6541 else max 0.0 (deadline -. now)
6543 else 0.0
6545 let r, _, _ =
6546 try Unix.select r [] [] timeout
6547 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6549 begin match r with
6550 | [] ->
6551 state.ghyll None;
6552 let newdeadline =
6553 if state.ghyll == noghyll
6554 then
6555 match state.autoscroll with
6556 | Some step when step != 0 ->
6557 if state.slideshow land 1 = 1
6558 then (
6559 if state.slideshow land 2 = 0
6560 then state.slideshow <- state.slideshow lor 2
6561 else if step < 0 then prevpage () else nextpage ();
6562 deadline +. (float (abs step))
6564 else
6565 let y = state.y + step in
6566 let fy = if conf.maxhfit then state.winh else 0 in
6567 let y =
6568 if y < 0
6569 then state.maxy - fy
6570 else if y >= state.maxy - fy then 0 else y
6572 if state.mode = View
6573 then gotoxy_and_clear_text state.x y
6574 else gotoxy state.x y;
6575 deadline +. 0.01
6576 | _ -> infinity
6577 else deadline +. 0.01
6579 loop newdeadline
6581 | l ->
6582 let rec checkfds = function
6583 | [] -> ()
6584 | fd :: rest when fd = state.ss ->
6585 let cmd = rcmd state.ss in
6586 act cmd;
6587 checkfds rest
6589 | fd :: rest when fd = state.wsfd ->
6590 Wsi.readresp fd;
6591 checkfds rest
6593 | fd :: rest when Some fd = !optrfd ->
6594 begin match remote fd with
6595 | None -> optrfd := remoteopen !rcmdpath;
6596 | opt -> optrfd := opt
6597 end;
6598 checkfds rest
6600 | _ :: rest ->
6601 dolog "select returned unknown descriptor";
6602 checkfds rest
6604 checkfds l;
6605 let newdeadline =
6606 let deadline1 =
6607 if deadline = infinity
6608 then now () +. 0.01
6609 else deadline
6611 match state.autoscroll with
6612 | Some step when step != 0 -> deadline1
6613 | _ -> if state.ghyll == noghyll then infinity else deadline1
6615 loop newdeadline
6616 end;
6618 match loop infinity with
6619 | exception Quit ->
6620 Config.save leavebirdseye;
6621 if hasunsavedchanges ()
6622 then save ()
6623 | _ -> error "umpossible - infinity reached"