Reject empty keys
[llpp.git] / main.ml
blobbc431124db4206cb5f10c586f3d916085a41e24e
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 ignoredoctitlte = ref false;;
61 let opengl_has_pbo = ref false;;
63 let drawstring size x y s =
64 Gl.enable `blend;
65 Gl.enable `texture_2d;
66 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
67 ignore (drawstr size x y s);
68 Gl.disable `blend;
69 Gl.disable `texture_2d;
72 let drawstring1 size x y s =
73 drawstr size x y s;
76 let drawstring2 size x y fmt =
77 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
80 module UniSyms = struct
81 let ellipsis = "\xe2\x80\xa6";;
82 let radical = "\xe2\x88\x9a";;
83 let lguillemet = "\xc2\xab";;
84 let rguillemet = "\xc2\xbb";;
85 end;;
87 let _debugl l =
88 dolog {|l %d dim=%d {
89 WxH %dx%d
90 vWxH %dx%d
91 pagex,y %d,%d
92 dispx,y %d,%d
93 column %d\n
94 }|}
95 l.pageno l.pagedimno
96 l.pagew l.pageh
97 l.pagevw l.pagevh
98 l.pagex l.pagey
99 l.pagedispx l.pagedispy
100 l.pagecol
103 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
104 dolog {|rect {
105 x0,y0=(% f, % f)
106 x1,y1=(% f, % f)
107 x2,y2=(% f, % f)
108 x3,y3=(% f, % f)
109 }|} x0 y0 x1 y1 x2 y2 x3 y3;
112 let isbirdseye = function
113 | Birdseye _ -> true
114 | Textentry _ | View | LinkNav _ -> false
117 let istextentry = function
118 | Textentry _ -> true
119 | Birdseye _ | View | LinkNav _ -> false
122 let wtmode = ref false;;
123 let cxack = ref false;;
125 let pgscale h = truncate (float h *. conf.pgscale);;
127 let hscrollh () =
128 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
129 && (state.w > state.winw))
130 then conf.scrollbw
131 else 0
134 let vscrollw () =
135 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
136 && (state.maxy > state.winh))
137 then conf.scrollbw
138 else 0
141 let vscrollhit x =
142 if conf.leftscroll
143 then x < vscrollw ()
144 else x > state.winw - vscrollw ()
147 let setfontsize n =
148 fstate.fontsize <- n;
149 fstate.wwidth <- measurestr fstate.fontsize "w";
150 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
153 let vlog fmt =
154 if conf.verbose
155 then dolog fmt
156 else Printf.kprintf ignore fmt
159 let launchpath () =
160 if emptystr conf.pathlauncher
161 then dolog "%s" state.path
162 else (
163 let command = Str.global_replace percentsre state.path conf.pathlauncher in
164 match spawn command [] with
165 | _pid -> ()
166 | exception exn ->
167 dolog "failed to execute `%s': %s" command @@ exntos exn
171 module G =
172 struct
173 let postRedisplay who =
174 vlog "redisplay for [%S]" who;
175 state.redisplay <- true;
177 end;;
179 let getopaque pageno =
180 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
181 with Not_found -> None
184 let pagetranslatepoint l x y =
185 let dy = y - l.pagedispy in
186 let y = dy + l.pagey in
187 let dx = x - l.pagedispx in
188 let x = dx + l.pagex in
189 (x, y);
192 let onppundermouse g x y d =
193 let rec f = function
194 | l :: rest ->
195 begin match getopaque l.pageno with
196 | Some opaque ->
197 let x0 = l.pagedispx in
198 let x1 = x0 + l.pagevw in
199 let y0 = l.pagedispy in
200 let y1 = y0 + l.pagevh in
201 if y >= y0 && y <= y1 && x >= x0 && x <= x1
202 then
203 let px, py = pagetranslatepoint l x y in
204 match g opaque l px py with
205 | Some res -> res
206 | None -> f rest
207 else f rest
208 | _ ->
209 f rest
211 | [] -> d
213 f state.layout
216 let getunder x y =
217 let g opaque l px py =
218 if state.bzoom
219 then (
220 match rectofblock opaque px py with
221 | Some [|x0;x1;y0;y1|] ->
222 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
223 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
224 state.rects <- [l.pageno, color, rect];
225 G.postRedisplay "getunder";
226 | _ -> ()
228 let under = whatsunder opaque px py in
229 if under = Unone then None else Some under
231 onppundermouse g x y Unone
234 let unproject x y =
235 let g opaque l x y =
236 match unproject opaque x y with
237 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
238 | None -> None
240 onppundermouse g x y None;
243 let showtext c s =
244 state.text <- Printf.sprintf "%c%s" c s;
245 G.postRedisplay "showtext";
248 let impmsg fmt =
249 Format.ksprintf (fun s -> showtext '!' s) fmt;
252 let pipef ?(closew=true) cap f cmd =
253 match Unix.pipe () with
254 | exception exn -> dolog "%s cannot create pipe: %S" cap @@ exntos exn
255 | (r, w) ->
256 begin match spawn cmd [r, 0; w, -1] with
257 | exception exn -> dolog "%s: cannot execute %S: %s" cap cmd @@ exntos exn
258 | _pid -> f w
259 end;
260 Ne.clo r (dolog "%s failed to close r: %s" cap);
261 if closew then Ne.clo w (dolog "%s failed to close w: %s" cap);
264 let pipesel opaque cmd =
265 if hassel opaque
266 then pipef ~closew:false "pipesel"
267 (fun w ->
268 copysel w opaque;
269 G.postRedisplay "pipesel"
270 ) cmd
273 let paxunder x y =
274 let g opaque l px py =
275 if markunder opaque px py conf.paxmark
276 then (
277 Some (fun () ->
278 match getopaque l.pageno with
279 | None -> ()
280 | Some opaque -> pipesel opaque conf.paxcmd
283 else None
285 G.postRedisplay "paxunder";
286 if conf.paxmark = Mark_page
287 then
288 List.iter (fun l ->
289 match getopaque l.pageno with
290 | None -> ()
291 | Some opaque -> clearmark opaque) state.layout;
292 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
295 let selstring s =
296 pipef
297 "selstring" (fun w ->
299 let l = String.length s in
300 let bytes = Bytes.unsafe_of_string s in
301 let n = tempfailureretry (Unix.write w bytes 0) l in
302 if n != l
303 then impmsg "failed to write %d characters to sel pipe, wrote %d" l n;
304 with exn -> impmsg "failed to write to sel pipe: %s" @@ exntos exn
305 ) conf.selcmd
308 let undertext = function
309 | Unone -> "none"
310 | Ulinkuri s -> s
311 | Utext s -> "font: " ^ s
312 | Uannotation (opaque, slinkindex) ->
313 "annotation: " ^ getannotcontents opaque slinkindex
316 let updateunder x y =
317 match getunder x y with
318 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
319 | Ulinkuri uri ->
320 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
321 Wsi.setcursor Wsi.CURSOR_INFO
322 | Utext s ->
323 if conf.underinfo then showtext 'f' ("ont: " ^ s);
324 Wsi.setcursor Wsi.CURSOR_TEXT
325 | Uannotation _ ->
326 if conf.underinfo then showtext 'a' "nnotation";
327 Wsi.setcursor Wsi.CURSOR_INFO
330 let showlinktype under =
331 if conf.underinfo && under != Unone
332 then showtext ' ' @@ undertext under
335 let [@warning "-4"] intentry_with_suffix text key =
336 let text =
337 match key with
338 | Keys.Ascii ('0'..'9' as c) -> addchar text c
339 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
340 addchar text @@ asciilower c
341 | _ ->
342 state.text <- Printf.sprintf "invalid key";
343 text
345 TEcont text
348 let wcmd fmt =
349 let b = Buffer.create 16 in
350 Printf.kbprintf
351 (fun b ->
352 let b = Buffer.to_bytes b in
353 wcmd state.ss b @@ Bytes.length b
354 ) b fmt
357 let nogeomcmds cmds =
358 match cmds with
359 | s, [] -> emptystr s
360 | _ -> false
363 let layoutN ((columns, coverA, coverB), b) x y sw sh =
364 let rec fold accu n =
365 if n = Array.length b
366 then accu
367 else
368 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
369 if (vy - y) > sh &&
370 (n = coverA - 1
371 || n = state.pagecount - coverB
372 || (n - coverA) mod columns = columns - 1)
373 then accu
374 else
375 let accu =
376 if vy + h > y
377 then
378 let pagey = max 0 (y - vy) in
379 let pagedispy = if pagey > 0 then 0 else vy - y in
380 let pagedispx, pagex =
381 let pdx =
382 if n = coverA - 1 || n = state.pagecount - coverB
383 then x + (sw - w) / 2
384 else dx + xoff + x
386 if pdx < 0
387 then 0, -pdx
388 else pdx, 0
390 let pagevw =
391 let vw = sw - pagedispx in
392 let pw = w - pagex in
393 min vw pw
395 let pagevh = min (h - pagey) (sh - pagedispy) in
396 if pagevw > 0 && pagevh > 0
397 then
398 let e =
399 { pageno = n
400 ; pagedimno = pdimno
401 ; pagew = w
402 ; pageh = h
403 ; pagex = pagex
404 ; pagey = pagey
405 ; pagevw = pagevw
406 ; pagevh = pagevh
407 ; pagedispx = pagedispx
408 ; pagedispy = pagedispy
409 ; pagecol = 0
412 e :: accu
413 else
414 accu
415 else
416 accu
418 fold accu (n+1)
420 if Array.length b = 0
421 then []
422 else List.rev (fold [] (page_of_y y))
425 let layoutS (columns, b) x y sw sh =
426 let rec fold accu n =
427 if n = Array.length b
428 then accu
429 else
430 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
431 if (vy - y) > sh
432 then accu
433 else
434 let accu =
435 if vy + pageh > y
436 then
437 let x = xoff + x in
438 let pagey = max 0 (y - vy) in
439 let pagedispy = if pagey > 0 then 0 else vy - y in
440 let pagedispx, pagex =
441 if px = 0
442 then (
443 if x < 0
444 then 0, -x
445 else x, 0
447 else (
448 let px = px - x in
449 if px < 0
450 then -px, 0
451 else 0, px
454 let pagecolw = pagew/columns in
455 let pagedispx =
456 if pagecolw < sw
457 then pagedispx + ((sw - pagecolw) / 2)
458 else pagedispx
460 let pagevw =
461 let vw = sw - pagedispx in
462 let pw = pagew - pagex in
463 min vw pw
465 let pagevw = min pagevw pagecolw in
466 let pagevh = min (pageh - pagey) (sh - pagedispy) in
467 if pagevw > 0 && pagevh > 0
468 then
469 let e =
470 { pageno = n/columns
471 ; pagedimno = pdimno
472 ; pagew = pagew
473 ; pageh = pageh
474 ; pagex = pagex
475 ; pagey = pagey
476 ; pagevw = pagevw
477 ; pagevh = pagevh
478 ; pagedispx = pagedispx
479 ; pagedispy = pagedispy
480 ; pagecol = n mod columns
483 e :: accu
484 else
485 accu
486 else
487 accu
489 fold accu (n+1)
491 List.rev (fold [] 0)
494 let layout x y sw sh =
495 if nogeomcmds state.geomcmds
496 then
497 match conf.columns with
498 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
499 | Cmulti c -> layoutN c x y sw sh
500 | Csplit s -> layoutS s x y sw sh
501 else []
504 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
506 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
508 let itertiles l f =
509 let tilex = l.pagex mod conf.tilew in
510 let tiley = l.pagey mod conf.tileh in
512 let col = l.pagex / conf.tilew in
513 let row = l.pagey / conf.tileh in
515 let rec rowloop row y0 dispy h =
516 if h = 0
517 then ()
518 else (
519 let dh = conf.tileh - y0 in
520 let dh = min h dh in
521 let rec colloop col x0 dispx w =
522 if w = 0
523 then ()
524 else (
525 let dw = conf.tilew - x0 in
526 let dw = min w dw in
527 f col row dispx dispy x0 y0 dw dh;
528 colloop (col+1) 0 (dispx+dw) (w-dw)
531 colloop col tilex l.pagedispx l.pagevw;
532 rowloop (row+1) 0 (dispy+dh) (h-dh)
535 if l.pagevw > 0 && l.pagevh > 0
536 then rowloop row tiley l.pagedispy l.pagevh;
539 let gettileopaque l col row =
540 let key =
541 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
543 try Some (Hashtbl.find state.tilemap key)
544 with Not_found -> None
547 let puttileopaque l col row gen colorspace angle opaque size elapsed =
548 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
549 Hashtbl.add state.tilemap key (opaque, size, elapsed)
552 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
553 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
554 GlArray.vertex `two state.vraw;
555 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
558 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
560 let filledrect x0 y0 x1 y1 =
561 GlArray.disable `texture_coord;
562 filledrect1 x0 y0 x1 y1;
563 GlArray.enable `texture_coord;
566 let linerect x0 y0 x1 y1 =
567 GlArray.disable `texture_coord;
568 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
569 GlArray.vertex `two state.vraw;
570 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
571 GlArray.enable `texture_coord;
574 let drawtiles l color =
575 GlDraw.color color;
576 begintiles ();
577 let f col row x y tilex tiley w h =
578 match gettileopaque l col row with
579 | Some (opaque, _, t) ->
580 let params = x, y, w, h, tilex, tiley in
581 if conf.invert
582 then GlTex.env (`mode `blend);
583 drawtile params opaque;
584 if conf.invert
585 then GlTex.env (`mode `modulate);
586 if conf.debug
587 then (
588 endtiles ();
589 let s = Printf.sprintf
590 "%d[%d,%d] %f sec"
591 l.pageno col row t
593 let w = measurestr fstate.fontsize s in
594 GlDraw.color (0.0, 0.0, 0.0);
595 filledrect (float (x-2))
596 (float (y-2))
597 (float (x+2) +. w)
598 (float (y + fstate.fontsize + 2));
599 GlDraw.color color;
600 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
601 begintiles ();
604 | None ->
605 endtiles ();
606 let w =
607 let lw = state.winw - x in
608 min lw w
609 and h =
610 let lh = state.winh - y in
611 min lh h
613 if conf.invert
614 then GlTex.env (`mode `blend);
615 begin match state.checkerstexid with
616 | Some id ->
617 Gl.enable `texture_2d;
618 GlTex.bind_texture ~target:`texture_2d id;
619 let x0 = float x
620 and y0 = float y
621 and x1 = float (x+w)
622 and y1 = float (y+h) in
624 let tw = float w /. 16.0
625 and th = float h /. 16.0 in
626 let tx0 = float tilex /. 16.0
627 and ty0 = float tiley /. 16.0 in
628 let tx1 = tx0 +. tw
629 and ty1 = ty0 +. th in
630 Raw.sets_float state.vraw ~pos:0
631 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
632 Raw.sets_float state.traw ~pos:0
633 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
634 GlArray.vertex `two state.vraw;
635 GlArray.tex_coord `two state.traw;
636 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
637 Gl.disable `texture_2d;
639 | None ->
640 GlDraw.color (1.0, 1.0, 1.0);
641 filledrect (float x) (float y) (float (x+w)) (float (y+h));
642 end;
643 if conf.invert
644 then GlTex.env (`mode `modulate);
645 if w > 128 && h > fstate.fontsize + 10
646 then (
647 let c = if conf.invert then 1.0 else 0.0 in
648 GlDraw.color (c, c, c);
649 let c, r =
650 if conf.verbose
651 then (col*conf.tilew, row*conf.tileh)
652 else col, row
654 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
656 GlDraw.color color;
657 begintiles ();
659 itertiles l f;
660 endtiles ();
663 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
665 let tilevisible1 l x y =
666 let ax0 = l.pagex
667 and ax1 = l.pagex + l.pagevw
668 and ay0 = l.pagey
669 and ay1 = l.pagey + l.pagevh in
671 let bx0 = x
672 and by0 = y in
673 let bx1 = min (bx0 + conf.tilew) l.pagew
674 and by1 = min (by0 + conf.tileh) l.pageh in
676 let rx0 = max ax0 bx0
677 and ry0 = max ay0 by0
678 and rx1 = min ax1 bx1
679 and ry1 = min ay1 by1 in
681 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
682 nonemptyintersection
685 let tilevisible layout n x y =
686 let rec findpageinlayout m = function
687 | l :: rest when l.pageno = n ->
688 tilevisible1 l x y || (
689 match conf.columns with
690 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
691 | Csplit _ | Csingle _ | Cmulti _ -> false
693 | _ :: rest -> findpageinlayout 0 rest
694 | [] -> false
696 findpageinlayout 0 layout;
699 let tileready l x y =
700 tilevisible1 l x y &&
701 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
704 let tilepage n p layout =
705 let rec loop = function
706 | l :: rest ->
707 if l.pageno = n
708 then
709 let f col row _ _ _ _ _ _ =
710 if state.currently = Idle
711 then
712 match gettileopaque l col row with
713 | Some _ -> ()
714 | None ->
715 let x = col*conf.tilew
716 and y = row*conf.tileh in
717 let w =
718 let w = l.pagew - x in
719 min w conf.tilew
721 let h =
722 let h = l.pageh - y in
723 min h conf.tileh
725 let pbo =
726 if conf.usepbo
727 then getpbo w h conf.colorspace
728 else ~< "0"
730 wcmd "tile %s %d %d %d %d %s"
731 (~> p) x y w h (~> pbo);
732 state.currently <-
733 Tiling (
734 l, p, conf.colorspace, conf.angle,
735 state.gen, col, row, conf.tilew, conf.tileh
738 itertiles l f;
739 else
740 loop rest
742 | [] -> ()
744 if nogeomcmds state.geomcmds
745 then loop layout;
748 let preloadlayout x y sw sh =
749 let y = if y < sh then 0 else y - sh in
750 let x = min 0 (x + sw) in
751 let h = sh*3 in
752 let w = sw*3 in
753 layout x y w h;
756 let load pages =
757 let rec loop pages =
758 if state.currently != Idle
759 then ()
760 else
761 match pages with
762 | l :: rest ->
763 begin match getopaque l.pageno with
764 | None ->
765 wcmd "page %d %d" l.pageno l.pagedimno;
766 state.currently <- Loading (l, state.gen);
767 | Some opaque ->
768 tilepage l.pageno opaque pages;
769 loop rest
770 end;
771 | _ -> ()
773 if nogeomcmds state.geomcmds
774 then loop pages
777 let preload pages =
778 load pages;
779 if conf.preload && state.currently = Idle
780 then load (preloadlayout state.x state.y state.winw state.winh);
783 let layoutready layout =
784 let rec fold all ls =
785 all && match ls with
786 | l :: rest ->
787 let seen = ref false in
788 let allvisible = ref true in
789 let foo col row _ _ _ _ _ _ =
790 seen := true;
791 allvisible := !allvisible &&
792 begin match gettileopaque l col row with
793 | Some _ -> true
794 | None -> false
797 itertiles l foo;
798 fold (!seen && !allvisible) rest
799 | [] -> true
801 let alltilesvisible = fold true layout in
802 alltilesvisible;
805 let gotoxy x y =
806 let y = bound y 0 state.maxy in
807 let y, layout, proceed =
808 match conf.maxwait with
809 | Some time when state.ghyll == noghyll ->
810 begin match state.throttle with
811 | None ->
812 let layout = layout x y state.winw state.winh in
813 let ready = layoutready layout in
814 if not ready
815 then (
816 load layout;
817 state.throttle <- Some (layout, y, now ());
819 else G.postRedisplay "gotoxy showall (None)";
820 y, layout, ready
821 | Some (_, _, started) ->
822 let dt = now () -. started in
823 if dt > time
824 then (
825 state.throttle <- None;
826 let layout = layout x y state.winw state.winh in
827 load layout;
828 G.postRedisplay "maxwait";
829 y, layout, true
831 else -1, [], false
834 | _ ->
835 let layout = layout x y state.winw state.winh in
836 if not !wtmode || layoutready layout
837 then G.postRedisplay "gotoxy ready";
838 y, layout, true
840 if proceed
841 then (
842 state.x <- x;
843 state.y <- y;
844 state.layout <- layout;
845 begin match state.mode with
846 | LinkNav ln ->
847 begin match ln with
848 | Ltexact (pageno, linkno) ->
849 let rec loop = function
850 | [] ->
851 state.lnava <- Some (pageno, linkno);
852 state.mode <- LinkNav (Ltgendir 0)
853 | l :: _ when l.pageno = pageno ->
854 begin match getopaque pageno with
855 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
856 | Some opaque ->
857 let x0, y0, x1, y1 = getlinkrect opaque linkno in
858 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
859 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
860 then state.mode <- LinkNav (Ltgendir 0)
862 | _ :: rest -> loop rest
864 loop layout
865 | Ltnotready _ | Ltgendir _ -> ()
867 | Birdseye _ | Textentry _ | View -> ()
868 end;
869 begin match state.mode with
870 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
871 if not (pagevisible layout pageno)
872 then (
873 match state.layout with
874 | [] -> ()
875 | l :: _ ->
876 state.mode <- Birdseye (
877 conf, leftx, l.pageno, hooverpageno, anchor
880 | LinkNav lt ->
881 begin match lt with
882 | Ltnotready (_, dir)
883 | Ltgendir dir ->
884 let linknav =
885 let rec loop = function
886 | [] -> lt
887 | l :: rest ->
888 match getopaque l.pageno with
889 | None -> Ltnotready (l.pageno, dir)
890 | Some opaque ->
891 let link =
892 let ld =
893 if dir = 0
894 then LDfirstvisible (l.pagex, l.pagey, dir)
895 else (
896 if dir > 0 then LDfirst else LDlast
899 findlink opaque ld
901 match link with
902 | Lnotfound -> loop rest
903 | Lfound n ->
904 showlinktype (getlink opaque n);
905 Ltexact (l.pageno, n)
907 loop state.layout
909 state.mode <- LinkNav linknav
910 | Ltexact _ -> ()
912 | Textentry _ | View -> ()
913 end;
914 preload layout;
916 state.ghyll <- noghyll;
917 if conf.updatecurs
918 then (
919 let mx, my = state.mpos in
920 updateunder mx my;
924 let conttiling pageno opaque =
925 tilepage pageno opaque
926 (if conf.preload
927 then preloadlayout state.x state.y state.winw state.winh
928 else state.layout)
931 let gotoxy_and_clear_text x y =
932 if not conf.verbose then state.text <- E.s;
933 gotoxy x y;
936 let getanchory (n, top, dtop) =
937 let y, h = getpageyh n in
938 if conf.presentation
939 then
940 let ips = calcips h in
941 y + truncate (top*.float h -. dtop*.float ips) + ips;
942 else
943 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
946 let gotoanchor anchor =
947 gotoxy state.x (getanchory anchor);
950 let addnav () =
951 getanchor () |> cbput state.hists.nav;
954 let addnavnorc () =
955 getanchor () |> cbput_dont_update_rc state.hists.nav;
958 let getnav dir =
959 let anchor = cbgetc state.hists.nav dir in
960 getanchory anchor;
963 let gotoghyll1 single y =
964 let scroll f n a b =
965 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
966 let snake f a b =
967 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
968 if f < a
969 then s (float f /. float a)
970 else (
971 if f > b
972 then 1.0 -. s ((float (f-b) /. float (n-b)))
973 else 1.0
976 snake f a b
977 and summa n a b =
978 let ins = float a *. 0.5
979 and outs = float (n-b) *. 0.5 in
980 let ones = b - a in
981 ins +. outs +. float ones
983 let rec set nab y sy =
984 let (_N, _A, _B), y =
985 if single
986 then
987 let scl = if y > sy then 2 else -2 in
988 let _N, _, _ = nab in
989 (_N,0,_N), y+conf.scrollstep*scl
990 else nab,y in
991 let sum = summa _N _A _B in
992 let dy = float (y - sy) in
993 state.ghyll <- (
994 let rec gf n y1 o =
995 if n >= _N
996 then state.ghyll <- noghyll
997 else
998 let go n =
999 let s = scroll n _N _A _B in
1000 let y1 = y1 +. ((s *. dy) /. sum) in
1001 gotoxy_and_clear_text state.x (truncate y1);
1002 state.ghyll <- gf (n+1) y1;
1004 match o with
1005 | None -> go n
1006 | Some y' when single -> set nab y' state.y
1007 | Some y' -> set (_N/2, 1, 1) y' state.y
1009 gf 0 (float state.y)
1012 match conf.ghyllscroll with
1013 | Some nab when not conf.presentation ->
1014 if state.ghyll == noghyll
1015 then set nab y state.y
1016 else state.ghyll (Some y)
1017 | _ ->
1018 gotoxy_and_clear_text state.x y
1021 let gotoghyll = gotoghyll1 false;;
1023 let gotopage n top =
1024 let y, h = getpageyh n in
1025 let y = y + (truncate (top *. float h)) in
1026 gotoghyll y
1029 let gotopage1 n top =
1030 let y = getpagey n in
1031 let y = y + top in
1032 gotoghyll y
1035 let invalidate ?(redisplay=false) s f =
1036 state.redisplay <- redisplay;
1037 state.layout <- [];
1038 state.pdims <- [];
1039 state.rects <- [];
1040 state.rects1 <- [];
1041 match state.geomcmds with
1042 | ps, [] when emptystr ps ->
1043 f ();
1044 state.geomcmds <- s, [];
1046 | ps, [] ->
1047 state.geomcmds <- ps, [s, f];
1049 | ps, (s', _) :: rest when s' = s ->
1050 state.geomcmds <- ps, ((s, f) :: rest);
1052 | ps, cmds ->
1053 state.geomcmds <- ps, ((s, f) :: cmds);
1056 let flushpages () =
1057 Hashtbl.iter (fun _ opaque ->
1058 wcmd "freepage %s" (~> opaque);
1059 ) state.pagemap;
1060 Hashtbl.clear state.pagemap;
1063 let flushtiles () =
1064 if not (Queue.is_empty state.tilelru)
1065 then (
1066 Queue.iter (fun (k, p, s) ->
1067 wcmd "freetile %s" (~> p);
1068 state.memused <- state.memused - s;
1069 Hashtbl.remove state.tilemap k;
1070 ) state.tilelru;
1071 state.uioh#infochanged Memused;
1072 Queue.clear state.tilelru;
1074 load state.layout;
1077 let stateh h =
1078 let h = truncate (float h*.conf.zoom) in
1079 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1080 h - d
1083 let opendoc path password =
1084 state.path <- path;
1085 state.password <- password;
1086 state.gen <- state.gen + 1;
1087 state.docinfo <- [];
1088 state.outlines <- [||];
1090 flushpages ();
1091 setaalevel conf.aalevel;
1092 let titlepath =
1093 if emptystr state.origin
1094 then path
1095 else state.origin
1097 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1098 wcmd "open %d %d %d %s\000%s\000%s\000"
1099 (btod !wtmode) (btod !cxack) (btod conf.usedoccss)
1100 path password conf.css;
1101 invalidate "reqlayout"
1102 (fun () ->
1103 wcmd "reqlayout %d %d %d %s\000"
1104 conf.angle (FMTE.to_int conf.fitmodel)
1105 (stateh state.winh) state.nameddest
1107 state.help <-
1108 let sl = keystostrlist conf in
1109 let rec loop accu =
1110 function | [] -> accu
1111 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1112 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1115 let reload () =
1116 state.anchor <- getanchor ();
1117 opendoc state.path state.password;
1120 let scalecolor c =
1121 let c = c *. conf.colorscale in
1122 (c, c, c);
1125 let scalecolor2 (r, g, b) =
1126 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1129 let docolumns columns =
1130 match columns with
1131 | Csingle _ ->
1132 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1133 let rec loop pageno pdimno pdim y ph pdims =
1134 if pageno = state.pagecount
1135 then ()
1136 else
1137 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1138 match pdims with
1139 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1140 pdimno+1, pdim, rest
1141 | _ ->
1142 pdimno, pdim, pdims
1144 let x = max 0 (((state.winw - w) / 2) - xoff) in
1145 let y =
1146 y + (if conf.presentation
1147 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1148 else (if pageno = 0 then 0 else conf.interpagespace)
1151 a.(pageno) <- (pdimno, x, y, pdim);
1152 loop (pageno+1) pdimno pdim (y + h) h pdims
1154 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1155 conf.columns <- Csingle a;
1157 | Cmulti ((columns, coverA, coverB), _) ->
1158 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1159 let rec loop pageno pdimno pdim x y rowh pdims =
1160 let rec fixrow m =
1161 if m = pageno then () else
1162 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1163 if h < rowh
1164 then (
1165 let y = y + (rowh - h) / 2 in
1166 a.(m) <- (pdimno, x, y, pdim);
1168 fixrow (m+1)
1170 if pageno = state.pagecount
1171 then fixrow (((pageno - 1) / columns) * columns)
1172 else
1173 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1174 match pdims with
1175 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1176 pdimno+1, pdim, rest
1177 | _ ->
1178 pdimno, pdim, pdims
1180 let x, y, rowh' =
1181 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1182 then (
1183 let x = (state.winw - w) / 2 in
1184 let ips =
1185 if conf.presentation then calcips h else conf.interpagespace in
1186 x, y + ips + rowh, h
1188 else (
1189 if (pageno - coverA) mod columns = 0
1190 then (
1191 let x = max 0 (state.winw - state.w) / 2 in
1192 let y =
1193 if conf.presentation
1194 then
1195 let ips = calcips h in
1196 y + (if pageno = 0 then 0 else calcips rowh + ips)
1197 else
1198 y + (if pageno = 0 then 0 else conf.interpagespace)
1200 x, y + rowh, h
1202 else x, y, max rowh h
1205 let y =
1206 if pageno > 1 && (pageno - coverA) mod columns = 0
1207 then (
1208 let y =
1209 if pageno = columns && conf.presentation
1210 then (
1211 let ips = calcips rowh in
1212 for i = 0 to pred columns
1214 let (pdimno, x, y, pdim) = a.(i) in
1215 a.(i) <- (pdimno, x, y+ips, pdim)
1216 done;
1217 y+ips;
1219 else y
1221 fixrow (pageno - columns);
1224 else y
1226 a.(pageno) <- (pdimno, x, y, pdim);
1227 let x = x + w + xoff*2 + conf.interpagespace in
1228 loop (pageno+1) pdimno pdim x y rowh' pdims
1230 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1231 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1233 | Csplit (c, _) ->
1234 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1235 let rec loop pageno pdimno pdim y pdims =
1236 if pageno = state.pagecount
1237 then ()
1238 else
1239 let pdimno, ((_, w, h, _) as pdim), pdims =
1240 match pdims with
1241 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1242 pdimno+1, pdim, rest
1243 | _ ->
1244 pdimno, pdim, pdims
1246 let cw = w / c in
1247 let rec loop1 n x y =
1248 if n = c then y else (
1249 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1250 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1253 let y = loop1 0 0 y in
1254 loop (pageno+1) pdimno pdim y pdims
1256 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1257 conf.columns <- Csplit (c, a);
1260 let represent () =
1261 docolumns conf.columns;
1262 state.maxy <- calcheight ();
1263 if state.reprf == noreprf
1264 then (
1265 match state.mode with
1266 | Birdseye (_, _, pageno, _, _) ->
1267 let y, h = getpageyh pageno in
1268 let top = (state.winh - h) / 2 in
1269 gotoxy state.x (max 0 (y - top))
1270 | Textentry _ | View | LinkNav _ ->
1271 let y = getanchory state.anchor in
1272 let y = min y (state.maxy - state.winh) in
1273 gotoxy state.x y;
1275 else (
1276 state.reprf ();
1277 state.reprf <- noreprf;
1281 let reshape ?(firsttime=false) w h =
1282 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1283 if not firsttime && nogeomcmds state.geomcmds
1284 then state.anchor <- getanchor ();
1286 state.winw <- w;
1287 let w = truncate (float w *. conf.zoom) in
1288 let w = max w 2 in
1289 state.winh <- h;
1290 setfontsize fstate.fontsize;
1291 GlMat.mode `modelview;
1292 GlMat.load_identity ();
1294 GlMat.mode `projection;
1295 GlMat.load_identity ();
1296 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1297 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1298 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1300 let relx =
1301 if conf.zoom <= 1.0
1302 then 0.0
1303 else float state.x /. float state.w
1305 invalidate ~redisplay:true "geometry"
1306 (fun () ->
1307 state.w <- w;
1308 if not firsttime
1309 then state.x <- truncate (relx *. float w);
1310 let w =
1311 match conf.columns with
1312 | Csingle _ -> w
1313 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1314 | Csplit (c, _) -> w * c
1316 wcmd "geometry %d %d %d"
1317 w (stateh h) (FMTE.to_int conf.fitmodel)
1321 let enttext () =
1322 let len = String.length state.text in
1323 let x0 = if conf.leftscroll then vscrollw () else 0 in
1324 let drawstring s =
1325 let hscrollh =
1326 match state.mode with
1327 | Textentry _ | View | LinkNav _ ->
1328 let h, _, _ = state.uioh#scrollpw in
1330 | Birdseye _ -> 0
1332 let rect x w =
1333 filledrect
1334 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1335 (x+.w) (float (state.winh - hscrollh))
1338 let w = float (state.winw - 1 - vscrollw ()) in
1339 if state.progress >= 0.0 && state.progress < 1.0
1340 then (
1341 GlDraw.color (0.3, 0.3, 0.3);
1342 let w1 = w *. state.progress in
1343 rect (float x0) w1;
1344 GlDraw.color (0.0, 0.0, 0.0);
1345 rect (float x0+.w1) (float x0+.w-.w1)
1347 else (
1348 GlDraw.color (0.0, 0.0, 0.0);
1349 rect (float x0) w;
1352 GlDraw.color (1.0, 1.0, 1.0);
1353 drawstring
1354 fstate.fontsize
1355 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1356 (state.winh - hscrollh - 5) s;
1358 let s =
1359 match state.mode with
1360 | Textentry ((prefix, text, _, _, _, _), _) ->
1361 let s =
1362 if len > 0
1363 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1364 else Printf.sprintf "%s%s_" prefix text
1368 | Birdseye _ | View | LinkNav _ -> state.text
1370 let s =
1371 if state.newerrmsgs
1372 then (
1373 if not (istextentry state.mode) && state.uioh#eformsgs
1374 then
1375 let s1 = "(press 'e' to review error messasges)" in
1376 if nonemptystr s then s ^ " " ^ s1 else s1
1377 else s
1379 else s
1381 if nonemptystr s
1382 then drawstring s
1385 let gctiles () =
1386 let len = Queue.length state.tilelru in
1387 let layout = lazy (
1388 match state.throttle with
1389 | None ->
1390 if conf.preload
1391 then preloadlayout state.x state.y state.winw state.winh
1392 else state.layout
1393 | Some (layout, _, _) ->
1394 layout
1395 ) in
1396 let rec loop qpos =
1397 if state.memused > conf.memlimit
1398 then (
1399 if qpos < len
1400 then
1401 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1402 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1403 let (_, pw, ph, _) = getpagedim n in
1404 if gen = state.gen
1405 && colorspace = conf.colorspace
1406 && angle = conf.angle
1407 && pagew = pw
1408 && pageh = ph
1409 && (
1410 let x = col*conf.tilew
1411 and y = row*conf.tileh in
1412 tilevisible (Lazy.force_val layout) n x y
1414 then Queue.push lruitem state.tilelru
1415 else (
1416 freepbo p;
1417 wcmd "freetile %s" (~> p);
1418 state.memused <- state.memused - s;
1419 state.uioh#infochanged Memused;
1420 Hashtbl.remove state.tilemap k;
1422 loop (qpos+1)
1425 loop 0
1428 let onpagerect pageno f =
1429 let b =
1430 match conf.columns with
1431 | Cmulti (_, b) -> b
1432 | Csingle b -> b
1433 | Csplit (_, b) -> b
1435 if pageno >= 0 && pageno < Array.length b
1436 then
1437 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1438 f w h
1441 let gotopagexy1 wtmode pageno x y =
1442 let _,w1,h1,leftx = getpagedim pageno in
1443 let top = y /. (float h1) in
1444 let left = x /. (float w1) in
1445 let py, w, h = getpageywh pageno in
1446 let wh = state.winh in
1447 let x = left *. (float w) in
1448 let x = leftx + state.x + truncate x in
1449 let sx =
1450 if x < 0 || x >= state.winw
1451 then state.x - x
1452 else state.x
1454 let pdy = truncate (top *. float h) in
1455 let y' = py + pdy in
1456 let dy = y' - state.y in
1457 let sy =
1458 if x != state.x || not (dy > 0 && dy < wh)
1459 then (
1460 if conf.presentation
1461 then
1462 if abs (py - y') > wh
1463 then y'
1464 else py
1465 else y';
1467 else state.y
1469 if state.x != sx || state.y != sy
1470 then (
1471 let x, y =
1472 if wtmode
1473 then (
1474 let ww = state.winw in
1475 let qx = sx / ww
1476 and qy = pdy / wh in
1477 let x = qx * ww
1478 and y = py + qy * wh in
1479 let x = if -x + ww > w1 then -(w1-ww) else x
1480 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1481 let y =
1482 if conf.presentation
1483 then
1484 if abs (py - y') > wh
1485 then y'
1486 else py
1487 else y';
1489 (x, y)
1491 else (sx, sy)
1493 gotoxy_and_clear_text x y;
1495 else gotoxy_and_clear_text state.x state.y;
1498 let gotopagexy wtmode pageno x y =
1499 match state.mode with
1500 | Birdseye _ -> gotopage pageno 0.0
1501 | Textentry _ | View | LinkNav _ -> gotopagexy1 wtmode pageno x y
1504 let getpassword () =
1505 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1506 if emptystr passcmd
1507 then E.s
1508 else getcmdoutput
1509 (fun s ->
1510 impmsg "error getting password: %s" s;
1511 dolog "%s" s) passcmd;
1514 let pgoto opaque pageno x y =
1515 let pdimno = getpdimno pageno in
1516 let x, y = project opaque pageno pdimno x y in
1517 gotopagexy false pageno x y;
1520 let act cmds =
1521 (* dolog "%S" cmds; *)
1522 let spl = splitatchar cmds ' ' in
1523 let scan s fmt f =
1524 try Scanf.sscanf s fmt f
1525 with exn ->
1526 dolog "error processing '%S': %s" cmds @@ exntos exn;
1527 exit 1
1529 let addoutline outline =
1530 match state.currently with
1531 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1532 | Idle -> state.currently <- Outlining [outline]
1533 | Loading _ | Tiling _ ->
1534 dolog "invalid outlining state";
1535 logcurrently state.currently
1537 match spl with
1538 | "clear", "" ->
1539 state.pdims <- [];
1540 state.uioh#infochanged Pdim;
1542 | "clearrects", "" ->
1543 state.rects <- state.rects1;
1544 G.postRedisplay "clearrects";
1546 | "continue", args ->
1547 let n = scan args "%u" (fun n -> n) in
1548 state.pagecount <- n;
1549 begin match state.currently with
1550 | Outlining l ->
1551 state.currently <- Idle;
1552 state.outlines <- Array.of_list (List.rev l)
1553 | Idle | Loading _ | Tiling _ -> ()
1554 end;
1556 let cur, cmds = state.geomcmds in
1557 if emptystr cur
1558 then failwith "umpossible";
1560 begin match List.rev cmds with
1561 | [] ->
1562 state.geomcmds <- E.s, [];
1563 state.throttle <- None;
1564 represent ();
1565 | (s, f) :: rest ->
1566 f ();
1567 state.geomcmds <- s, List.rev rest;
1568 end;
1569 if conf.maxwait = None && not !wtmode
1570 then G.postRedisplay "continue";
1572 | "msg", args ->
1573 showtext ' ' args
1575 | "vmsg", args ->
1576 if conf.verbose
1577 then showtext ' ' args
1579 | "emsg", args ->
1580 Buffer.add_string state.errmsgs args;
1581 state.newerrmsgs <- true;
1582 G.postRedisplay "error message"
1584 | "progress", args ->
1585 let progress, text =
1586 scan args "%f %n"
1587 (fun f pos ->
1588 f, String.sub args pos (String.length args - pos))
1590 state.text <- text;
1591 state.progress <- progress;
1592 G.postRedisplay "progress"
1594 | "firstmatch", args ->
1595 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1596 scan args "%u %d %f %f %f %f %f %f %f %f"
1597 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1598 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1600 let y = (getpagey pageno) + truncate y0 in
1601 let x =
1602 if conf.zoom > 1.0
1603 then state.winw/2
1604 else state.x
1606 addnav ();
1607 gotoxy x y;
1608 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1609 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1611 | "match", args ->
1612 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1613 scan args "%u %d %f %f %f %f %f %f %f %f"
1614 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1615 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1617 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1618 state.rects1 <-
1619 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1621 | "page", args ->
1622 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1623 let pageopaque = ~< pageopaques in
1624 begin match state.currently with
1625 | Loading (l, gen) ->
1626 vlog "page %d took %f sec" l.pageno t;
1627 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1628 begin match state.throttle with
1629 | None ->
1630 let preloadedpages =
1631 if conf.preload
1632 then preloadlayout state.x state.y state.winw state.winh
1633 else state.layout
1635 let evict () =
1636 let set =
1637 List.fold_left (fun s l -> IntSet.add l.pageno s)
1638 IntSet.empty preloadedpages
1640 let evictedpages =
1641 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1642 if not (IntSet.mem pageno set)
1643 then (
1644 wcmd "freepage %s" (~> opaque);
1645 key :: accu
1647 else accu
1648 ) state.pagemap []
1650 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1652 evict ();
1653 state.currently <- Idle;
1654 if gen = state.gen
1655 then (
1656 tilepage l.pageno pageopaque state.layout;
1657 load state.layout;
1658 load preloadedpages;
1659 let visible = pagevisible state.layout l.pageno in
1660 if visible
1661 then (
1662 match state.mode with
1663 | LinkNav (Ltnotready (pageno, dir)) ->
1664 if pageno = l.pageno
1665 then (
1666 let link =
1667 let ld =
1668 if dir = 0
1669 then LDfirstvisible (l.pagex, l.pagey, dir)
1670 else (
1671 if dir > 0 then LDfirst else LDlast
1674 findlink pageopaque ld
1676 match link with
1677 | Lnotfound -> ()
1678 | Lfound n ->
1679 showlinktype (getlink pageopaque n);
1680 state.mode <- LinkNav (Ltexact (l.pageno, n))
1682 | LinkNav (Ltgendir _)
1683 | LinkNav (Ltexact _)
1684 | View
1685 | Birdseye _
1686 | Textentry _ -> ()
1689 if visible && layoutready state.layout
1690 then (
1691 G.postRedisplay "page";
1695 | Some (layout, _, _) ->
1696 state.currently <- Idle;
1697 tilepage l.pageno pageopaque layout;
1698 load state.layout
1699 end;
1701 | Idle | Tiling _ | Outlining _ ->
1702 dolog "Inconsistent loading state";
1703 logcurrently state.currently;
1704 exit 1
1707 | "tile" , args ->
1708 let (x, y, opaques, size, t) =
1709 scan args "%u %u %s %u %f"
1710 (fun x y p size t -> (x, y, p, size, t))
1712 let opaque = ~< opaques in
1713 begin match state.currently with
1714 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1715 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1717 unmappbo opaque;
1718 if tilew != conf.tilew || tileh != conf.tileh
1719 then (
1720 wcmd "freetile %s" (~> opaque);
1721 state.currently <- Idle;
1722 load state.layout;
1724 else (
1725 puttileopaque l col row gen cs angle opaque size t;
1726 state.memused <- state.memused + size;
1727 state.uioh#infochanged Memused;
1728 gctiles ();
1729 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1730 opaque, size) state.tilelru;
1732 let layout =
1733 match state.throttle with
1734 | None -> state.layout
1735 | Some (layout, _, _) -> layout
1738 state.currently <- Idle;
1739 if gen = state.gen
1740 && conf.colorspace = cs
1741 && conf.angle = angle
1742 && tilevisible layout l.pageno x y
1743 then conttiling l.pageno pageopaque;
1745 begin match state.throttle with
1746 | None ->
1747 preload state.layout;
1748 if gen = state.gen
1749 && conf.colorspace = cs
1750 && conf.angle = angle
1751 && tilevisible state.layout l.pageno x y
1752 && (not !wtmode || layoutready state.layout)
1753 then G.postRedisplay "tile nothrottle";
1755 | Some (layout, y, _) ->
1756 let ready = layoutready layout in
1757 if ready
1758 then (
1759 state.y <- y;
1760 state.layout <- layout;
1761 state.throttle <- None;
1762 G.postRedisplay "throttle";
1764 else load layout;
1765 end;
1768 | Idle | Loading _ | Outlining _ ->
1769 dolog "Inconsistent tiling state";
1770 logcurrently state.currently;
1771 exit 1
1774 | "pdim", args ->
1775 let (n, w, h, _) as pdim =
1776 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1778 let pdim =
1779 match conf.fitmodel with
1780 | FitWidth -> pdim
1781 | FitPage | FitProportional ->
1782 match conf.columns with
1783 | Csplit _ -> (n, w, h, 0)
1784 | Csingle _ | Cmulti _ -> pdim
1786 state.pdims <- pdim :: state.pdims;
1787 state.uioh#infochanged Pdim
1789 | "o", args ->
1790 let (l, n, t, h, pos) =
1791 scan args "%u %u %d %u %n"
1792 (fun l n t h pos -> l, n, t, h, pos)
1794 let s = String.sub args pos (String.length args - pos) in
1795 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1797 | "ou", args ->
1798 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1799 let s = String.sub args pos len in
1800 let pos2 = pos + len + 1 in
1801 let uri = String.sub args pos2 (String.length args - pos2) in
1802 addoutline (s, l, Ouri uri)
1804 | "on", args ->
1805 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1806 let s = String.sub args pos (String.length args - pos) in
1807 addoutline (s, l, Onone)
1809 | "a", args ->
1810 let (n, l, t) =
1811 scan args "%u %d %d" (fun n l t -> n, l, t)
1813 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1815 | "info", args ->
1816 let c, v = splitatchar args '\t' in
1817 let s =
1818 if nonemptystr v
1819 then
1820 if c = "Title"
1821 then (
1822 conf.title <- v;
1823 if not !ignoredoctitlte
1824 then Wsi.settitle v;
1825 args
1827 else
1828 if let len = String.length c in
1829 len > 6 && ((String.sub c (len-4) 4) = "date")
1830 then (
1831 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1832 then
1833 let b = Buffer.create 10 in
1834 Printf.bprintf b "%s\t" c;
1835 let sub p l c =
1837 Buffer.add_substring b v p l;
1838 Buffer.add_char b c;
1839 with exn -> Buffer.add_string b @@ exntos exn
1841 sub 2 4 '/';
1842 sub 6 2 '/';
1843 sub 8 2 ' ';
1844 sub 10 2 ':';
1845 sub 12 2 ':';
1846 sub 14 2 ' ';
1847 Buffer.add_char b '[';
1848 Buffer.add_string b v;
1849 Buffer.add_char b ']';
1850 Buffer.contents b
1851 else args
1853 else args
1854 else args
1856 state.docinfo <- (1, s) :: state.docinfo
1858 | "infoend", "" ->
1859 state.docinfo <- List.rev state.docinfo;
1860 state.uioh#infochanged Docinfo
1862 | "pass", args ->
1863 if args = "fail"
1864 then Wsi.settitle "Wrong password";
1865 let password = getpassword () in
1866 if emptystr password
1867 then error "document is password protected"
1868 else opendoc state.path password
1870 | _ ->
1871 error "unknown cmd `%S'" cmds
1874 let onhist cb =
1875 let rc = cb.rc in
1876 let action = function
1877 | HCprev -> cbget cb ~-1
1878 | HCnext -> cbget cb 1
1879 | HCfirst -> cbget cb ~-(cb.rc)
1880 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1881 and cancel () = cb.rc <- rc
1882 in (action, cancel)
1885 let search pattern forward =
1886 match conf.columns with
1887 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1888 | Csingle _ | Cmulti _ ->
1889 if nonemptystr pattern
1890 then
1891 let pn, py =
1892 match state.layout with
1893 | [] -> 0, 0
1894 | l :: _ ->
1895 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1897 wcmd "search %d %d %d %d,%s\000"
1898 (btod conf.icase) pn py (btod forward) pattern;
1901 let [@warning "-4"] intentry text key =
1902 let text =
1903 if emptystr text && key = Keys.Ascii '-'
1904 then addchar text '-'
1905 else
1906 match key with
1907 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1908 | _ ->
1909 state.text <- "invalid key";
1910 text
1912 TEcont text
1915 let linknact f s =
1916 if nonemptystr s
1917 then (
1918 let n =
1919 let l = String.length s in
1920 let rec loop pos n =
1921 if pos = l
1922 then n
1923 else
1924 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1925 loop (pos+1) (n*26 + m)
1926 in loop 0 0
1928 let rec loop n = function
1929 | [] -> ()
1930 | l :: rest ->
1931 match getopaque l.pageno with
1932 | None -> loop n rest
1933 | Some opaque ->
1934 let m = getlinkcount opaque in
1935 if n < m
1936 then (
1937 let under = getlink opaque n in
1938 f under
1940 else loop (n-m) rest
1942 loop n state.layout;
1946 let [@warning "-4"] linknentry text = function
1947 | Keys.Ascii c ->
1948 let text = addchar text c in
1949 linknact (fun under -> state.text <- undertext under) text;
1950 TEcont text
1951 | _ ->
1952 state.text <- Printf.sprintf "invalid key";
1953 TEcont text
1956 let [@warning "-4"] textentry text = function
1957 | Keys.Ascii c -> TEcont (addchar text c)
1958 | Keys.Code c -> TEcont (text ^ toutf8 c)
1959 | _ -> TEcont text
1962 let reqlayout angle fitmodel =
1963 match state.throttle with
1964 | None ->
1965 if nogeomcmds state.geomcmds
1966 then state.anchor <- getanchor ();
1967 conf.angle <- angle mod 360;
1968 if conf.angle != 0
1969 then (
1970 match state.mode with
1971 | LinkNav _ -> state.mode <- View
1972 | Birdseye _ | Textentry _ | View -> ()
1974 conf.fitmodel <- fitmodel;
1975 invalidate
1976 "reqlayout"
1977 (fun () ->
1978 wcmd "reqlayout %d %d %d"
1979 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1981 | _ -> ()
1984 let settrim trimmargins trimfuzz =
1985 if nogeomcmds state.geomcmds
1986 then state.anchor <- getanchor ();
1987 conf.trimmargins <- trimmargins;
1988 conf.trimfuzz <- trimfuzz;
1989 let x0, y0, x1, y1 = trimfuzz in
1990 invalidate
1991 "settrim" (fun () ->
1992 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1993 flushpages ();
1996 let setzoom zoom =
1997 match state.throttle with
1998 | None ->
1999 let zoom = max 0.0001 zoom in
2000 if zoom <> conf.zoom
2001 then (
2002 state.prevzoom <- (conf.zoom, state.x);
2003 conf.zoom <- zoom;
2004 reshape state.winw state.winh;
2005 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2008 | Some (layout, y, started) ->
2009 let time =
2010 match conf.maxwait with
2011 | None -> 0.0
2012 | Some t -> t
2014 let dt = now () -. started in
2015 if dt > time
2016 then (
2017 state.y <- y;
2018 load layout;
2022 let pivotzoom ?(vw=min state.w state.winw)
2023 ?(vh=min (state.maxy-state.y) state.winh)
2024 ?(x=vw/2) ?(y=vh/2) zoom =
2025 let w = float state.w /. zoom in
2026 let hw = w /. 2.0 in
2027 let ratio = float vh /. float vw in
2028 let hh = hw *. ratio in
2029 let x0 = float x -. hw
2030 and y0 = float y -. hh in
2031 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2032 setzoom zoom;
2035 let pivotzoom ?vw ?vh ?x ?y zoom =
2036 if nogeomcmds state.geomcmds
2037 then
2038 if zoom > 1.0
2039 then pivotzoom ?vw ?vh ?x ?y zoom
2040 else setzoom zoom
2043 let setcolumns mode columns coverA coverB =
2044 state.prevcolumns <- Some (conf.columns, conf.zoom);
2045 if columns < 0
2046 then (
2047 if isbirdseye mode
2048 then impmsg "split mode doesn't work in bird's eye"
2049 else (
2050 conf.columns <- Csplit (-columns, E.a);
2051 state.x <- 0;
2052 conf.zoom <- 1.0;
2055 else (
2056 if columns < 2
2057 then (
2058 conf.columns <- Csingle E.a;
2059 state.x <- 0;
2060 setzoom 1.0;
2062 else (
2063 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2064 conf.zoom <- 1.0;
2067 reshape state.winw state.winh;
2070 let resetmstate () =
2071 state.mstate <- Mnone;
2072 Wsi.setcursor Wsi.CURSOR_INHERIT;
2075 let enterbirdseye () =
2076 let zoom = float conf.thumbw /. float state.winw in
2077 let birdseyepageno =
2078 let cy = state.winh / 2 in
2079 let fold = function
2080 | [] -> 0
2081 | l :: rest ->
2082 let rec fold best = function
2083 | [] -> best.pageno
2084 | l :: rest ->
2085 let d = cy - (l.pagedispy + l.pagevh/2)
2086 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2087 if abs d < abs dbest
2088 then fold l rest
2089 else best.pageno
2090 in fold l rest
2092 fold state.layout
2094 state.mode <-
2095 Birdseye (
2096 { conf with zoom = conf.zoom },
2097 state.x, birdseyepageno, -1, getanchor ()
2099 resetmstate ();
2100 conf.zoom <- zoom;
2101 conf.presentation <- false;
2102 conf.interpagespace <- 10;
2103 conf.hlinks <- false;
2104 conf.fitmodel <- FitPage;
2105 state.x <- 0;
2106 conf.maxwait <- None;
2107 conf.columns <- (
2108 match conf.beyecolumns with
2109 | Some c ->
2110 conf.zoom <- 1.0;
2111 Cmulti ((c, 0, 0), E.a)
2112 | None -> Csingle E.a
2114 if conf.verbose
2115 then
2116 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2117 (100.0*.zoom)
2118 else
2119 state.text <- E.s
2121 reshape state.winw state.winh;
2124 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2125 state.mode <- View;
2126 conf.zoom <- c.zoom;
2127 conf.presentation <- c.presentation;
2128 conf.interpagespace <- c.interpagespace;
2129 conf.maxwait <- c.maxwait;
2130 conf.hlinks <- c.hlinks;
2131 conf.fitmodel <- c.fitmodel;
2132 conf.beyecolumns <- (
2133 match conf.columns with
2134 | Cmulti ((c, _, _), _) -> Some c
2135 | Csingle _ -> None
2136 | Csplit _ -> failwith "leaving bird's eye split mode"
2138 conf.columns <- (
2139 match c.columns with
2140 | Cmulti (c, _) -> Cmulti (c, E.a)
2141 | Csingle _ -> Csingle E.a
2142 | Csplit (c, _) -> Csplit (c, E.a)
2144 if conf.verbose
2145 then
2146 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2147 (100.0*.conf.zoom)
2149 reshape state.winw state.winh;
2150 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2151 state.x <- leftx;
2154 let togglebirdseye () =
2155 match state.mode with
2156 | Birdseye vals -> leavebirdseye vals true
2157 | View -> enterbirdseye ()
2158 | Textentry _ | LinkNav _ -> ()
2161 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2162 let pageno = max 0 (pageno - incr) in
2163 let rec loop = function
2164 | [] -> gotopage1 pageno 0
2165 | l :: _ when l.pageno = pageno ->
2166 if l.pagedispy >= 0 && l.pagey = 0
2167 then G.postRedisplay "upbirdseye"
2168 else gotopage1 pageno 0
2169 | _ :: rest -> loop rest
2171 loop state.layout;
2172 state.text <- E.s;
2173 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2176 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2177 let pageno = min (state.pagecount - 1) (pageno + incr) in
2178 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2179 let rec loop = function
2180 | [] ->
2181 let y, h = getpageyh pageno in
2182 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2183 gotoxy state.x (clamp dy)
2184 | l :: _ when l.pageno = pageno ->
2185 if l.pagevh != l.pageh
2186 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2187 else G.postRedisplay "downbirdseye"
2188 | _ :: rest -> loop rest
2190 loop state.layout;
2191 state.text <- E.s;
2194 let [@warning "-4"] optentry mode _ key =
2195 let btos b = if b then "on" else "off" in
2196 match key with
2197 | Keys.Ascii 's' ->
2198 let ondone s =
2199 try conf.scrollstep <- int_of_string s with exn ->
2200 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2202 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2204 | Keys.Ascii 'A' ->
2205 let ondone s =
2207 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2208 if state.autoscroll <> None
2209 then state.autoscroll <- Some conf.autoscrollstep
2210 with exn ->
2211 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2213 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2215 | Keys.Ascii 'C' ->
2216 let ondone s =
2218 let n, a, b = multicolumns_of_string s in
2219 setcolumns mode n a b;
2220 with exn ->
2221 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2223 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2225 | Keys.Ascii 'Z' ->
2226 let ondone s =
2228 let zoom = float (int_of_string s) /. 100.0 in
2229 pivotzoom zoom
2230 with exn ->
2231 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2233 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2235 | Keys.Ascii 't' ->
2236 let ondone s =
2238 conf.thumbw <- bound (int_of_string s) 2 4096;
2239 state.text <-
2240 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2241 begin match mode with
2242 | Birdseye beye ->
2243 leavebirdseye beye false;
2244 enterbirdseye ();
2245 | Textentry _ | View | LinkNav _ -> ();
2247 with exn ->
2248 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2250 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2252 | Keys.Ascii 'R' ->
2253 let ondone s =
2254 match int_of_string s with
2255 | angle -> reqlayout angle conf.fitmodel
2256 | exception exn ->
2257 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2259 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2261 | Keys.Ascii 'i' ->
2262 conf.icase <- not conf.icase;
2263 TEdone ("case insensitive search " ^ (btos conf.icase))
2265 | Keys.Ascii 'p' ->
2266 conf.preload <- not conf.preload;
2267 gotoxy state.x state.y;
2268 TEdone ("preload " ^ (btos conf.preload))
2270 | Keys.Ascii 'v' ->
2271 conf.verbose <- not conf.verbose;
2272 TEdone ("verbose " ^ (btos conf.verbose))
2274 | Keys.Ascii 'd' ->
2275 conf.debug <- not conf.debug;
2276 TEdone ("debug " ^ (btos conf.debug))
2278 | Keys.Ascii 'h' ->
2279 conf.maxhfit <- not conf.maxhfit;
2280 state.maxy <- calcheight ();
2281 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2283 | Keys.Ascii 'c' ->
2284 conf.crophack <- not conf.crophack;
2285 TEdone ("crophack " ^ btos conf.crophack)
2287 | Keys.Ascii 'a' ->
2288 let s =
2289 match conf.maxwait with
2290 | None ->
2291 conf.maxwait <- Some infinity;
2292 "always wait for page to complete"
2293 | Some _ ->
2294 conf.maxwait <- None;
2295 "show placeholder if page is not ready"
2297 TEdone s
2299 | Keys.Ascii 'f' ->
2300 conf.underinfo <- not conf.underinfo;
2301 TEdone ("underinfo " ^ btos conf.underinfo)
2303 | Keys.Ascii 'P' ->
2304 conf.savebmarks <- not conf.savebmarks;
2305 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2307 | Keys.Ascii 'S' ->
2308 let ondone s =
2310 let pageno, py =
2311 match state.layout with
2312 | [] -> 0, 0
2313 | l :: _ ->
2314 l.pageno, l.pagey
2316 conf.interpagespace <- int_of_string s;
2317 docolumns conf.columns;
2318 state.maxy <- calcheight ();
2319 let y = getpagey pageno in
2320 gotoxy state.x (y + py)
2321 with exn ->
2322 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2324 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2326 | Keys.Ascii 'l' ->
2327 let fm =
2328 match conf.fitmodel with
2329 | FitProportional -> FitWidth
2330 | FitWidth | FitPage -> FitProportional
2332 reqlayout conf.angle fm;
2333 TEdone ("proportional display " ^ btos (fm == FitProportional))
2335 | Keys.Ascii 'T' ->
2336 settrim (not conf.trimmargins) conf.trimfuzz;
2337 TEdone ("trim margins " ^ btos conf.trimmargins)
2339 | Keys.Ascii 'I' ->
2340 conf.invert <- not conf.invert;
2341 TEdone ("invert colors " ^ btos conf.invert)
2343 | Keys.Ascii 'x' ->
2344 let ondone s =
2345 cbput state.hists.sel s;
2346 conf.selcmd <- s;
2348 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2349 textentry, ondone, true)
2351 | Keys.Ascii 'M' ->
2352 if conf.pax == None
2353 then conf.pax <- Some 0.0
2354 else conf.pax <- None;
2355 TEdone ("PAX " ^ btos (conf.pax != None))
2357 | (Keys.Ascii c) ->
2358 state.text <- Printf.sprintf "bad option %d `%c'"
2359 (Char.code c) c;
2360 TEstop
2362 | _ ->
2363 TEcont state.text
2366 class type lvsource =
2367 object
2368 method getitemcount : int
2369 method getitem : int -> (string * int)
2370 method hasaction : int -> bool
2371 method exit : uioh:uioh ->
2372 cancel:bool ->
2373 active:int ->
2374 first:int ->
2375 pan:int ->
2376 uioh option
2377 method getactive : int
2378 method getfirst : int
2379 method getpan : int
2380 method getminfo : (int * int) array
2381 end;;
2383 class virtual lvsourcebase = object
2384 val mutable m_active = 0
2385 val mutable m_first = 0
2386 val mutable m_pan = 0
2387 method getactive = m_active
2388 method getfirst = m_first
2389 method getpan = m_pan
2390 method getminfo : (int * int) array = E.a
2391 end;;
2393 let [@warning "-4"]
2394 textentrykeyboard
2395 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2396 state.text <- E.s;
2397 let enttext te =
2398 state.mode <- Textentry (te, onleave);
2399 enttext ();
2400 G.postRedisplay "textentrykeyboard enttext";
2402 let histaction cmd =
2403 match opthist with
2404 | None -> ()
2405 | Some (action, _) ->
2406 state.mode <-
2407 Textentry (
2408 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2410 G.postRedisplay "textentry histaction"
2412 let open Keys in
2413 let kt = Wsi.kc2kt key in
2414 match kt with
2415 | Backspace ->
2416 if emptystr text && cancelonempty
2417 then (
2418 onleave Cancel;
2419 G.postRedisplay "textentrykeyboard after cancel";
2421 else
2422 let s = withoutlastutf8 text in
2423 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2425 | Enter ->
2426 ondone text;
2427 onleave Confirm;
2428 G.postRedisplay "textentrykeyboard after confirm"
2430 | Up -> histaction HCprev
2431 | Down -> histaction HCnext
2432 | Home -> histaction HCfirst
2433 | End -> histaction HClast
2435 | Escape ->
2436 if emptystr text
2437 then (
2438 begin match opthist with
2439 | None -> ()
2440 | Some (_, onhistcancel) -> onhistcancel ()
2441 end;
2442 onleave Cancel;
2443 state.text <- E.s;
2444 G.postRedisplay "textentrykeyboard after cancel2"
2446 else (
2447 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2450 | Delete -> ()
2452 | Code _ | Ascii _ ->
2453 begin match onkey text kt with
2454 | TEdone text ->
2455 ondone text;
2456 onleave Confirm;
2457 G.postRedisplay "textentrykeyboard after confirm2";
2459 | TEcont text ->
2460 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2462 | TEstop ->
2463 onleave Cancel;
2464 G.postRedisplay "textentrykeyboard after cancel3"
2466 | TEswitch te ->
2467 state.mode <- Textentry (te, onleave);
2468 G.postRedisplay "textentrykeyboard switch";
2470 | _ -> vlog "unhandled key"
2473 let firstof first active =
2474 if first > active || abs (first - active) > fstate.maxrows - 1
2475 then max 0 (active - (fstate.maxrows/2))
2476 else first
2479 let calcfirst first active =
2480 if active > first
2481 then
2482 let rows = active - first in
2483 if rows > fstate.maxrows then active - fstate.maxrows else first
2484 else active
2487 let scrollph y maxy =
2488 let sh = float (maxy + state.winh) /. float state.winh in
2489 let sh = float state.winh /. sh in
2490 let sh = max sh (float conf.scrollh) in
2492 let percent = float y /. float maxy in
2493 let position = (float state.winh -. sh) *. percent in
2495 let position =
2496 if position +. sh > float state.winh
2497 then float state.winh -. sh
2498 else position
2500 position, sh;
2503 let adderrmsg src msg =
2504 Buffer.add_string state.errmsgs msg;
2505 state.newerrmsgs <- true;
2506 G.postRedisplay src
2509 let adderrfmt src fmt =
2510 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2513 let coe s = (s :> uioh);;
2515 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2516 object (self)
2517 val m_pan = source#getpan
2518 val m_first = source#getfirst
2519 val m_active = source#getactive
2520 val m_qsearch = E.s
2521 val m_prev_uioh = state.uioh
2523 method private elemunder y =
2524 if y < 0
2525 then None
2526 else
2527 let n = y / (fstate.fontsize+1) in
2528 if m_first + n < source#getitemcount
2529 then (
2530 if source#hasaction (m_first + n)
2531 then Some (m_first + n)
2532 else None
2534 else None
2536 method display =
2537 Gl.enable `blend;
2538 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2539 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2540 filledrect 0. 0. (float state.winw) (float state.winh);
2541 GlDraw.color (1., 1., 1.);
2542 Gl.enable `texture_2d;
2543 let fs = fstate.fontsize in
2544 let nfs = fs + 1 in
2545 let hw = state.winw/3 in
2546 let ww = fstate.wwidth in
2547 let tabw = 17.0*.ww in
2548 let itemcount = source#getitemcount in
2549 let minfo = source#getminfo in
2550 if conf.leftscroll
2551 then (
2552 GlMat.push ();
2553 GlMat.translate ~x:(float conf.scrollbw) ();
2555 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2556 let rec loop row =
2557 if (row - m_first) > fstate.maxrows
2558 then ()
2559 else (
2560 if row >= 0 && row < itemcount
2561 then (
2562 let (s, level) = source#getitem row in
2563 let y = (row - m_first) * nfs in
2564 let x = 5.0 +. (float (level + m_pan)) *. ww in
2565 if helpmode
2566 then GlDraw.color
2567 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2569 if row = m_active
2570 then (
2571 Gl.disable `texture_2d;
2572 let alpha = if source#hasaction row then 0.9 else 0.3 in
2573 GlDraw.color (1., 1., 1.) ~alpha;
2574 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2575 Gl.enable `texture_2d;
2577 let c =
2578 if zebra && row land 1 = 1
2579 then 0.8
2580 else 1.0
2582 GlDraw.color (c,c,c);
2583 let drawtabularstring s =
2584 let drawstr x s =
2585 let x' = truncate (x0 +. x) in
2586 let s1, s2 = splitatchar s '\000' in
2587 if emptystr s2
2588 then drawstring1 fs x' (y+nfs) s
2589 else
2590 let rec e s =
2591 if emptystr s
2592 then s
2593 else
2594 let s' = withoutlastutf8 s in
2595 let s = s' ^ UniSyms.ellipsis in
2596 let w = measurestr fs s in
2597 if float x' +. w +. ww < float (hw + x')
2598 then s
2599 else e s'
2601 let s1 =
2602 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2603 then e s1
2604 else s1
2606 ignore (drawstring1 fs x' (y+nfs) s1);
2607 drawstring1 fs (hw + x') (y+nfs) s2
2609 if trusted
2610 then
2611 let x = if helpmode && row > 0 then x +. ww else x in
2612 let s1, s2 = splitatchar s '\t' in
2613 if nonemptystr s2
2614 then
2615 let nx = drawstr x s1 in
2616 let sw = nx -. x in
2617 let x = x +. (max tabw sw) in
2618 drawstr x s2
2619 else
2620 let len = String.length s - 2 in
2621 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2622 then
2623 let s = String.sub s 2 len in
2624 let x = if not helpmode then x +. ww else x in
2625 GlDraw.color (1.2, 1.2, 1.2);
2626 let vinc = drawstring1 (fs+fs/4)
2627 (truncate (x -. ww)) (y+nfs) s in
2628 GlDraw.color (1., 1., 1.);
2629 vinc +. (float fs *. 0.8)
2630 else
2631 drawstr x s
2632 else
2633 drawstr x s
2635 ignore (drawtabularstring s);
2636 loop (row+1)
2640 loop m_first;
2641 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2642 let xadj = 5.0 in
2643 let rec loop row =
2644 if (row - m_first) <= fstate.maxrows
2645 then
2646 if row >= 0 && row < itemcount
2647 then (
2648 let (s, level) = source#getitem row in
2649 let pos0 = nindex s '\000' in
2650 let y = (row - m_first) * nfs in
2651 let x = float (level + m_pan) *. ww in
2652 let (first, last) = minfo.(row) in
2653 let prefix =
2654 if pos0 > 0 && first > pos0
2655 then String.sub s (pos0+1) (first-pos0-1)
2656 else String.sub s 0 first
2658 let suffix = String.sub s first (last - first) in
2659 let w1 = measurestr fstate.fontsize prefix in
2660 let w2 = measurestr fstate.fontsize suffix in
2661 let x = x +. if conf.leftscroll then xadj else 5.0 in
2662 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2663 let x0 = x +. w1
2664 and y0 = float (y+2) in
2665 let x1 = x0 +. w2
2666 and y1 = float (y+fs+3) in
2667 filledrect x0 y0 x1 y1;
2668 loop (row+1)
2671 Gl.disable `texture_2d;
2672 if Array.length minfo > 0 then loop m_first;
2673 Gl.disable `blend;
2674 if conf.leftscroll
2675 then GlMat.pop ()
2677 method updownlevel incr =
2678 let len = source#getitemcount in
2679 let curlevel =
2680 if m_active >= 0 && m_active < len
2681 then snd (source#getitem m_active)
2682 else -1
2684 let rec flow i =
2685 if i = len then i-1 else if i = -1 then 0 else
2686 let _, l = source#getitem i in
2687 if l != curlevel then i else flow (i+incr)
2689 let active = flow m_active in
2690 let first = calcfirst m_first active in
2691 G.postRedisplay "outline updownlevel";
2692 {< m_active = active; m_first = first >}
2694 method private key1 key mask =
2695 let set1 active first qsearch =
2696 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2698 let search active pattern incr =
2699 let active = if active = -1 then m_first else active in
2700 let dosearch re =
2701 let rec loop n =
2702 if n >= 0 && n < source#getitemcount
2703 then (
2704 let s, _ = source#getitem n in
2705 match Str.search_forward re s 0 with
2706 | exception Not_found -> loop (n + incr)
2707 | _ -> Some n
2709 else None
2711 loop active
2713 let qpat = Str.quote pattern in
2714 match Str.regexp_case_fold qpat with
2715 | s -> dosearch s
2716 | exception exn ->
2717 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2718 qpat @@ Printexc.to_string exn;
2719 None
2721 let itemcount = source#getitemcount in
2722 let find start incr =
2723 let rec find i =
2724 if i = -1 || i = itemcount
2725 then -1
2726 else (
2727 if source#hasaction i
2728 then i
2729 else find (i + incr)
2732 find start
2734 let set active first =
2735 let first = bound first 0 (itemcount - fstate.maxrows) in
2736 state.text <- E.s;
2737 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2739 let navigate incr =
2740 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2741 let active, first =
2742 let incr1 = if incr > 0 then 1 else -1 in
2743 if isvisible m_first m_active
2744 then
2745 let next =
2746 let next = m_active + incr in
2747 let next =
2748 if next < 0 || next >= itemcount
2749 then -1
2750 else find next incr1
2752 if abs (m_active - next) > fstate.maxrows
2753 then -1
2754 else next
2756 if next = -1
2757 then
2758 let first = m_first + incr in
2759 let first = bound first 0 (itemcount - fstate.maxrows) in
2760 let next =
2761 let next = m_active + incr in
2762 let next = bound next 0 (itemcount - 1) in
2763 find next ~-incr1
2765 let active =
2766 if next = -1
2767 then m_active
2768 else (
2769 if isvisible first next
2770 then next
2771 else m_active
2774 active, first
2775 else
2776 let first = min next m_first in
2777 let first =
2778 if abs (next - first) > fstate.maxrows
2779 then first + incr
2780 else first
2782 next, first
2783 else
2784 let first = m_first + incr in
2785 let first = bound first 0 (itemcount - 1) in
2786 let active =
2787 let next = m_active + incr in
2788 let next = bound next 0 (itemcount - 1) in
2789 let next = find next incr1 in
2790 let active =
2791 if next = -1 || abs (m_active - first) > fstate.maxrows
2792 then (
2793 let active = if m_active = -1 then next else m_active in
2794 active
2796 else next
2798 if isvisible first active
2799 then active
2800 else -1
2802 active, first
2804 G.postRedisplay "listview navigate";
2805 set active first;
2807 let open Keys in
2808 let kt = Wsi.kc2kt key in
2809 match [@warning "-4"] kt with
2810 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2811 let incr = if c = 'r' then -1 else 1 in
2812 let active, first =
2813 match search (m_active + incr) m_qsearch incr with
2814 | None ->
2815 state.text <- m_qsearch ^ " [not found]";
2816 m_active, m_first
2817 | Some active ->
2818 state.text <- m_qsearch;
2819 active, firstof m_first active
2821 G.postRedisplay "listview ctrl-r/s";
2822 set1 active first m_qsearch;
2824 | Insert when Wsi.withctrl mask ->
2825 if m_active >= 0 && m_active < source#getitemcount
2826 then (
2827 let s, _ = source#getitem m_active in
2828 selstring s;
2830 coe self
2832 | Backspace ->
2833 if emptystr m_qsearch
2834 then coe self
2835 else (
2836 let qsearch = withoutlastutf8 m_qsearch in
2837 if emptystr qsearch
2838 then (
2839 state.text <- E.s;
2840 G.postRedisplay "listview empty qsearch";
2841 set1 m_active m_first E.s;
2843 else
2844 let active, first =
2845 match search m_active qsearch ~-1 with
2846 | None ->
2847 state.text <- qsearch ^ " [not found]";
2848 m_active, m_first
2849 | Some active ->
2850 state.text <- qsearch;
2851 active, firstof m_first active
2853 G.postRedisplay "listview backspace qsearch";
2854 set1 active first qsearch
2857 | Ascii _ | Code _ ->
2858 let utf8 =
2859 match [@warning "-8"] kt with
2860 | Ascii c -> String.make 1 c
2861 | Code code -> toutf8 code
2863 let pattern = m_qsearch ^ utf8 in
2864 let active, first =
2865 match search m_active pattern 1 with
2866 | None ->
2867 state.text <- pattern ^ " [not found]";
2868 m_active, m_first
2869 | Some active ->
2870 state.text <- pattern;
2871 active, firstof m_first active
2873 G.postRedisplay "listview qsearch add";
2874 set1 active first pattern;
2876 | Escape ->
2877 state.text <- E.s;
2878 if emptystr m_qsearch
2879 then (
2880 G.postRedisplay "list view escape";
2881 let mx, my = state.mpos in
2882 updateunder mx my;
2883 match source#exit ~uioh:(coe self) ~cancel:true ~active:m_active
2884 ~first:m_first ~pan:m_pan with
2885 | None -> m_prev_uioh
2886 | Some uioh -> uioh
2888 else (
2889 G.postRedisplay "list view kill qsearch";
2890 coe {< m_qsearch = E.s >}
2893 | Enter ->
2894 state.text <- E.s;
2895 let self = {< m_qsearch = E.s >} in
2896 let opt =
2897 G.postRedisplay "listview enter";
2898 let cancel = not (m_active >= 0 && m_active < source#getitemcount) in
2899 source#exit ~uioh:(coe self) ~cancel
2900 ~active:m_active ~first:m_first ~pan:m_pan;
2902 begin match opt with
2903 | None -> m_prev_uioh
2904 | Some uioh -> uioh
2907 | Delete ->
2908 coe self
2910 | Up -> navigate ~-1
2911 | Down -> navigate 1
2912 | Prior -> navigate ~-(fstate.maxrows)
2913 | Next -> navigate fstate.maxrows
2915 | Right ->
2916 state.text <- E.s;
2917 G.postRedisplay "listview right";
2918 coe {< m_pan = m_pan - 1 >}
2920 | Left ->
2921 state.text <- E.s;
2922 G.postRedisplay "listview left";
2923 coe {< m_pan = m_pan + 1 >}
2925 | Home ->
2926 let active = find 0 1 in
2927 G.postRedisplay "listview home";
2928 set active 0;
2930 | End ->
2931 let first = max 0 (itemcount - fstate.maxrows) in
2932 let active = find (itemcount - 1) ~-1 in
2933 G.postRedisplay "listview end";
2934 set active first;
2936 | _ -> coe self
2938 method key key mask =
2939 match state.mode with
2940 | Textentry te ->
2941 textentrykeyboard key mask te;
2942 coe self
2943 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2945 method button button down x y _ =
2946 let opt =
2947 match button with
2948 | 1 when vscrollhit x ->
2949 G.postRedisplay "listview scroll";
2950 if down
2951 then
2952 let _, position, sh = self#scrollph in
2953 if y > truncate position && y < truncate (position +. sh)
2954 then (
2955 state.mstate <- Mscrolly;
2956 Some (coe self)
2958 else
2959 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2960 let first = truncate (s *. float source#getitemcount) in
2961 let first = min source#getitemcount first in
2962 Some (coe {< m_first = first; m_active = first >})
2963 else (
2964 state.mstate <- Mnone;
2965 Some (coe self);
2967 | 1 when down ->
2968 begin match self#elemunder y with
2969 | Some n ->
2970 G.postRedisplay "listview click";
2971 source#exit ~uioh:(coe {< m_active = n >})
2972 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
2973 | _ ->
2974 Some (coe self)
2976 | n when (n == 4 || n == 5) && not down ->
2977 let len = source#getitemcount in
2978 let first =
2979 if n = 5 && m_first + fstate.maxrows >= len
2980 then
2981 m_first
2982 else
2983 let first = m_first + (if n == 4 then -1 else 1) in
2984 bound first 0 (len - 1)
2986 G.postRedisplay "listview wheel";
2987 Some (coe {< m_first = first >})
2988 | n when (n = 6 || n = 7) && not down ->
2989 let inc = if n = 7 then -1 else 1 in
2990 G.postRedisplay "listview hwheel";
2991 Some (coe {< m_pan = m_pan + inc >})
2992 | _ ->
2993 Some (coe self)
2995 match opt with
2996 | None -> m_prev_uioh
2997 | Some uioh -> uioh
2999 method multiclick _ x y = self#button 1 true x y
3001 method motion _ y =
3002 match state.mstate with
3003 | Mscrolly ->
3004 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3005 let first = truncate (s *. float source#getitemcount) in
3006 let first = min source#getitemcount first in
3007 G.postRedisplay "listview motion";
3008 coe {< m_first = first; m_active = first >}
3009 | Msel _
3010 | Mpan _
3011 | Mscrollx
3012 | Mzoom _
3013 | Mzoomrect _
3014 | Mnone -> coe self
3016 method pmotion x y =
3017 if x < state.winw - conf.scrollbw
3018 then
3019 let n =
3020 match self#elemunder y with
3021 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3022 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3024 let o =
3025 if n != m_active
3026 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3027 else self
3029 coe o
3030 else (
3031 Wsi.setcursor Wsi.CURSOR_INHERIT;
3032 coe self
3035 method infochanged _ = ()
3037 method scrollpw = (0, 0.0, 0.0)
3038 method scrollph =
3039 let nfs = fstate.fontsize + 1 in
3040 let y = m_first * nfs in
3041 let itemcount = source#getitemcount in
3042 let maxi = max 0 (itemcount - fstate.maxrows) in
3043 let maxy = maxi * nfs in
3044 let p, h = scrollph y maxy in
3045 conf.scrollbw, p, h
3047 method modehash = modehash
3048 method eformsgs = false
3049 method alwaysscrolly = true
3050 method scroll _ dy =
3051 let self =
3052 if dy != 0 then begin
3053 let len = source#getitemcount in
3054 let first =
3055 if dy > 0 && m_first + fstate.maxrows >= len
3056 then
3057 m_first
3058 else
3059 let first = m_first + dy / 10 in
3060 bound first 0 (len - 1)
3062 G.postRedisplay "listview wheel";
3063 {< m_first = first >}
3064 end else
3065 self
3067 coe self
3069 method zoom _ _ _ = ()
3070 end;;
3072 class outlinelistview ~zebra ~source =
3073 let settext autonarrow s =
3074 if autonarrow
3075 then
3076 let ss = source#statestr in
3077 state.text <-
3078 if emptystr ss
3079 then "[" ^ s ^ "]"
3080 else "{" ^ ss ^ "} [" ^ s ^ "]"
3081 else state.text <- s
3083 object (self)
3084 inherit listview
3085 ~zebra
3086 ~helpmode:false
3087 ~source:(source :> lvsource)
3088 ~trusted:false
3089 ~modehash:(findkeyhash conf "outline")
3090 as super
3092 val m_autonarrow = false
3094 method! key key mask =
3095 let maxrows =
3096 if emptystr state.text
3097 then fstate.maxrows
3098 else fstate.maxrows - 2
3100 let calcfirst first active =
3101 if active > first
3102 then
3103 let rows = active - first in
3104 if rows > maxrows then active - maxrows else first
3105 else active
3107 let navigate incr =
3108 let active = m_active + incr in
3109 let active = bound active 0 (source#getitemcount - 1) in
3110 let first = calcfirst m_first active in
3111 G.postRedisplay "outline navigate";
3112 coe {< m_active = active; m_first = first >}
3114 let navscroll first =
3115 let active =
3116 let dist = m_active - first in
3117 if dist < 0
3118 then first
3119 else (
3120 if dist < maxrows
3121 then m_active
3122 else first + maxrows
3125 G.postRedisplay "outline navscroll";
3126 coe {< m_first = first; m_active = active >}
3128 let ctrl = Wsi.withctrl mask in
3129 let open Keys in
3130 match Wsi.kc2kt key with
3131 | Ascii 'a' when ctrl ->
3132 let text =
3133 if m_autonarrow
3134 then (source#denarrow; E.s)
3135 else (
3136 let pattern = source#renarrow in
3137 if nonemptystr m_qsearch
3138 then (source#narrow m_qsearch; m_qsearch)
3139 else pattern
3142 settext (not m_autonarrow) text;
3143 G.postRedisplay "toggle auto narrowing";
3144 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3146 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
3147 settext true E.s;
3148 G.postRedisplay "toggle auto narrowing";
3149 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3151 | Ascii 'n' when ctrl ->
3152 source#narrow m_qsearch;
3153 if not m_autonarrow
3154 then source#add_narrow_pattern m_qsearch;
3155 G.postRedisplay "outline ctrl-n";
3156 coe {< m_first = 0; m_active = 0 >}
3158 | Ascii 'S' when ctrl ->
3159 let active = source#calcactive (getanchor ()) in
3160 let first = firstof m_first active in
3161 G.postRedisplay "outline ctrl-s";
3162 coe {< m_first = first; m_active = active >}
3164 | Ascii 'u' when ctrl ->
3165 G.postRedisplay "outline ctrl-u";
3166 if m_autonarrow && nonemptystr m_qsearch
3167 then (
3168 ignore (source#renarrow);
3169 settext m_autonarrow E.s;
3170 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3172 else (
3173 source#del_narrow_pattern;
3174 let pattern = source#renarrow in
3175 let text =
3176 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3178 settext m_autonarrow text;
3179 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3182 | Ascii 'l' when ctrl ->
3183 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3184 G.postRedisplay "outline ctrl-l";
3185 coe {< m_first = first >}
3187 | Ascii '\t' when m_autonarrow ->
3188 if nonemptystr m_qsearch
3189 then (
3190 G.postRedisplay "outline list view tab";
3191 source#add_narrow_pattern m_qsearch;
3192 settext true E.s;
3193 coe {< m_qsearch = E.s >}
3195 else coe self
3197 | Escape when m_autonarrow ->
3198 if nonemptystr m_qsearch
3199 then source#add_narrow_pattern m_qsearch;
3200 super#key key mask
3202 | Enter when m_autonarrow ->
3203 if nonemptystr m_qsearch
3204 then source#add_narrow_pattern m_qsearch;
3205 super#key key mask
3207 | (Ascii _ | Code _) when m_autonarrow ->
3208 let pattern = m_qsearch ^ toutf8 key in
3209 G.postRedisplay "outlinelistview autonarrow add";
3210 source#narrow pattern;
3211 settext true pattern;
3212 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3214 | Backspace when m_autonarrow ->
3215 if emptystr m_qsearch
3216 then coe self
3217 else
3218 let pattern = withoutlastutf8 m_qsearch in
3219 G.postRedisplay "outlinelistview autonarrow backspace";
3220 ignore (source#renarrow);
3221 source#narrow pattern;
3222 settext true pattern;
3223 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3225 | Up when ctrl ->
3226 navscroll (max 0 (m_first - 1))
3228 | Down when ctrl ->
3229 navscroll (min (source#getitemcount - 1) (m_first + 1))
3231 | Up -> navigate ~-1
3232 | Down -> navigate 1
3233 | Prior -> navigate ~-(fstate.maxrows)
3234 | Next -> navigate fstate.maxrows
3236 | Right ->
3237 let o =
3238 if ctrl
3239 then (
3240 G.postRedisplay "outline ctrl right";
3241 {< m_pan = m_pan + 1 >}
3243 else self#updownlevel 1
3245 coe o
3247 | Left ->
3248 let o =
3249 if ctrl
3250 then (
3251 G.postRedisplay "outline ctrl left";
3252 {< m_pan = m_pan - 1 >}
3254 else self#updownlevel ~-1
3256 coe o
3258 | Home ->
3259 G.postRedisplay "outline home";
3260 coe {< m_first = 0; m_active = 0 >}
3262 | End ->
3263 let active = source#getitemcount - 1 in
3264 let first = max 0 (active - fstate.maxrows) in
3265 G.postRedisplay "outline end";
3266 coe {< m_active = active; m_first = first >}
3268 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
3269 super#key key mask
3270 end;;
3272 let genhistoutlines () =
3273 Config.gethist ()
3274 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3275 compare c2.lastvisit c1.lastvisit)
3276 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
3277 let path = if nonemptystr origin then origin else path in
3278 let base = mbtoutf8 @@ Filename.basename path in
3279 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3283 let gotohist (path, c, bookmarks, x, anchor, origin) =
3284 Config.save leavebirdseye;
3285 state.anchor <- anchor;
3286 state.bookmarks <- bookmarks;
3287 state.origin <- origin;
3288 state.x <- x;
3289 setconf conf c;
3290 let x0, y0, x1, y1 = conf.trimfuzz in
3291 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3292 reshape ~firsttime:true state.winw state.winh;
3293 opendoc path origin;
3294 setzoom c.zoom;
3297 let makecheckers () =
3298 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3299 following to say:
3300 converted by Issac Trotts. July 25, 2002 *)
3301 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3302 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3303 let id = GlTex.gen_texture () in
3304 GlTex.bind_texture ~target:`texture_2d id;
3305 GlPix.store (`unpack_alignment 1);
3306 GlTex.image2d image;
3307 List.iter (GlTex.parameter ~target:`texture_2d)
3308 [ `mag_filter `nearest; `min_filter `nearest ];
3312 let setcheckers enabled =
3313 match state.checkerstexid with
3314 | None ->
3315 if enabled then state.checkerstexid <- Some (makecheckers ())
3317 | Some checkerstexid ->
3318 if not enabled
3319 then (
3320 GlTex.delete_texture checkerstexid;
3321 state.checkerstexid <- None;
3325 let describe_layout layout =
3326 let d =
3327 match layout with
3328 | [] -> "Page 0"
3329 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
3330 | l :: rest ->
3331 let rangestr a b =
3332 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
3333 else
3334 let sep = if a.pageno+1 = b.pageno then ", " else UniSyms.ellipsis in
3335 Printf.sprintf "%d%s%d" (a.pageno+1) sep (b.pageno+1)
3337 let rec fold s la lb = function
3338 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
3339 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
3340 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
3342 fold "Pages" l l rest
3344 let percent =
3345 let maxy = maxy () in
3346 if maxy <= 0
3347 then 100.
3348 else 100. *. (float state.y /. float maxy)
3350 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
3353 let setpresentationmode v =
3354 let n = page_of_y state.y in
3355 state.anchor <- (n, 0.0, 1.0);
3356 conf.presentation <- v;
3357 if conf.fitmodel = FitPage
3358 then reqlayout conf.angle conf.fitmodel;
3359 represent ();
3362 let setbgcol (r, g, b) =
3363 let col =
3364 let r = r *. 255.0 |> truncate
3365 and g = g *. 255.0 |> truncate
3366 and b = b *. 255.0 |> truncate in
3367 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3369 Wsi.setwinbgcol col;
3372 let enterinfomode =
3373 let btos b = if b then UniSyms.radical else E.s in
3374 let showextended = ref false in
3375 let showcolors = ref false in
3376 let leave mode _ = state.mode <- mode in
3377 let src =
3378 (object
3379 val mutable m_l = []
3380 val mutable m_a = E.a
3381 val mutable m_prev_uioh = nouioh
3382 val mutable m_prev_mode = View
3384 inherit lvsourcebase
3386 method reset prev_mode prev_uioh =
3387 m_a <- Array.of_list (List.rev m_l);
3388 m_l <- [];
3389 m_prev_mode <- prev_mode;
3390 m_prev_uioh <- prev_uioh;
3392 method int name get set =
3393 m_l <-
3394 (name, `int get, 1,
3395 Action (
3396 fun u ->
3397 let ondone s =
3398 try set (int_of_string s)
3399 with exn ->
3400 state.text <- Printf.sprintf "bad integer `%s': %s"
3401 s @@ exntos exn
3403 state.text <- E.s;
3404 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3405 state.mode <- Textentry (te, leave m_prev_mode);
3407 )) :: m_l
3409 method int_with_suffix name get set =
3410 m_l <-
3411 (name, `intws get, 1,
3412 Action (
3413 fun u ->
3414 let ondone s =
3415 try set (int_of_string_with_suffix s)
3416 with exn ->
3417 state.text <- Printf.sprintf "bad integer `%s': %s"
3418 s @@ exntos exn
3420 state.text <- E.s;
3421 let te =
3422 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3424 state.mode <- Textentry (te, leave m_prev_mode);
3426 )) :: m_l
3428 method bool ?(offset=1) ?(btos=btos) name get set =
3429 m_l <-
3430 (name, `bool (btos, get), offset, Action (
3431 fun u ->
3432 let v = get () in
3433 set (not v);
3435 )) :: m_l
3437 method color name get set =
3438 m_l <-
3439 (name, `color get, 1,
3440 Action (
3441 fun u ->
3442 let invalid = (nan, nan, nan) in
3443 let ondone s =
3444 let c =
3445 try color_of_string s
3446 with exn ->
3447 state.text <- Printf.sprintf "bad color `%s': %s"
3448 s @@ exntos exn;
3449 invalid
3451 if c <> invalid
3452 then set c;
3454 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3455 state.text <- color_to_string (get ());
3456 state.mode <- Textentry (te, leave m_prev_mode);
3458 )) :: m_l
3460 method string name get set =
3461 m_l <-
3462 (name, `string get, 1,
3463 Action (
3464 fun u ->
3465 let ondone s = set s in
3466 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3467 state.mode <- Textentry (te, leave m_prev_mode);
3469 )) :: m_l
3471 method colorspace name get set =
3472 m_l <-
3473 (name, `string get, 1,
3474 Action (
3475 fun _ ->
3476 let source =
3477 (object
3478 inherit lvsourcebase
3480 initializer
3481 m_active <- CSTE.to_int conf.colorspace;
3482 m_first <- 0;
3484 method getitemcount =
3485 Array.length CSTE.names
3486 method getitem n =
3487 (CSTE.names.(n), 0)
3488 method exit ~uioh ~cancel ~active ~first ~pan =
3489 ignore (uioh, first, pan);
3490 if not cancel then set active;
3491 None
3492 method hasaction _ = true
3493 end)
3495 state.text <- E.s;
3496 let modehash = findkeyhash conf "info" in
3497 coe (new listview ~zebra:false ~helpmode:false
3498 ~source ~trusted:true ~modehash)
3499 )) :: m_l
3501 method paxmark name get set =
3502 m_l <-
3503 (name, `string get, 1,
3504 Action (
3505 fun _ ->
3506 let source =
3507 (object
3508 inherit lvsourcebase
3510 initializer
3511 m_active <- MTE.to_int conf.paxmark;
3512 m_first <- 0;
3514 method getitemcount = Array.length MTE.names
3515 method getitem n = (MTE.names.(n), 0)
3516 method exit ~uioh ~cancel ~active ~first ~pan =
3517 ignore (uioh, first, pan);
3518 if not cancel then set active;
3519 None
3520 method hasaction _ = true
3521 end)
3523 state.text <- E.s;
3524 let modehash = findkeyhash conf "info" in
3525 coe (new listview ~zebra:false ~helpmode:false
3526 ~source ~trusted:true ~modehash)
3527 )) :: m_l
3529 method fitmodel name get set =
3530 m_l <-
3531 (name, `string get, 1,
3532 Action (
3533 fun _ ->
3534 let source =
3535 (object
3536 inherit lvsourcebase
3538 initializer
3539 m_active <- FMTE.to_int conf.fitmodel;
3540 m_first <- 0;
3542 method getitemcount = Array.length FMTE.names
3543 method getitem n = (FMTE.names.(n), 0)
3544 method exit ~uioh ~cancel ~active ~first ~pan =
3545 ignore (uioh, first, pan);
3546 if not cancel then set active;
3547 None
3548 method hasaction _ = true
3549 end)
3551 state.text <- E.s;
3552 let modehash = findkeyhash conf "info" in
3553 coe (new listview ~zebra:false ~helpmode:false
3554 ~source ~trusted:true ~modehash)
3555 )) :: m_l
3557 method caption s offset =
3558 m_l <- (s, `empty, offset, Noaction) :: m_l
3560 method caption2 s f offset =
3561 m_l <- (s, `string f, offset, Noaction) :: m_l
3563 method getitemcount = Array.length m_a
3565 method getitem n =
3566 let tostr = function
3567 | `int f -> string_of_int (f ())
3568 | `intws f -> string_with_suffix_of_int (f ())
3569 | `string f -> f ()
3570 | `color f -> color_to_string (f ())
3571 | `bool (btos, f) -> btos (f ())
3572 | `empty -> E.s
3574 let name, t, offset, _ = m_a.(n) in
3575 ((let s = tostr t in
3576 if nonemptystr s
3577 then Printf.sprintf "%s\t%s" name s
3578 else name),
3579 offset)
3581 method exit ~uioh ~cancel ~active ~first ~pan =
3582 let uiohopt =
3583 if not cancel
3584 then (
3585 let uioh =
3586 match m_a.(active) with
3587 | _, _, _, Action f -> f uioh
3588 | _, _, _, Noaction -> uioh
3590 Some uioh
3592 else None
3594 m_active <- active;
3595 m_first <- first;
3596 m_pan <- pan;
3597 uiohopt
3599 method hasaction n =
3600 match m_a.(n) with
3601 | _, _, _, Action _ -> true
3602 | _, _, _, Noaction -> false
3604 initializer m_active <- 1
3605 end)
3607 let rec fillsrc prevmode prevuioh =
3608 let sep () = src#caption E.s 0 in
3609 let colorp name get set =
3610 src#string name
3611 (fun () -> color_to_string (get ()))
3612 (fun v ->
3614 let c = color_of_string v in
3615 set c
3616 with exn ->
3617 state.text <-
3618 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3621 let rgba name get set =
3622 src#string name
3623 (fun () -> rgba_to_string (get ()))
3624 (fun v ->
3626 let c = rgba_of_string v in
3627 set c
3628 with exn ->
3629 state.text <-
3630 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3633 let oldmode = state.mode in
3634 let birdseye = isbirdseye state.mode in
3636 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3638 src#bool "presentation mode"
3639 (fun () -> conf.presentation)
3640 (fun v -> setpresentationmode v);
3642 src#bool "ignore case in searches"
3643 (fun () -> conf.icase)
3644 (fun v -> conf.icase <- v);
3646 src#bool "preload"
3647 (fun () -> conf.preload)
3648 (fun v -> conf.preload <- v);
3650 src#bool "highlight links"
3651 (fun () -> conf.hlinks)
3652 (fun v -> conf.hlinks <- v);
3654 src#bool "under info"
3655 (fun () -> conf.underinfo)
3656 (fun v -> conf.underinfo <- v);
3658 src#bool "persistent bookmarks"
3659 (fun () -> conf.savebmarks)
3660 (fun v -> conf.savebmarks <- v);
3662 src#fitmodel "fit model"
3663 (fun () -> FMTE.to_string conf.fitmodel)
3664 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3666 src#bool "trim margins"
3667 (fun () -> conf.trimmargins)
3668 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3670 src#bool "persistent location"
3671 (fun () -> conf.jumpback)
3672 (fun v -> conf.jumpback <- v);
3674 sep ();
3675 src#int "inter-page space"
3676 (fun () -> conf.interpagespace)
3677 (fun n ->
3678 conf.interpagespace <- n;
3679 docolumns conf.columns;
3680 let pageno, py =
3681 match state.layout with
3682 | [] -> 0, 0
3683 | l :: _ ->
3684 l.pageno, l.pagey
3686 state.maxy <- calcheight ();
3687 let y = getpagey pageno in
3688 gotoxy state.x (y + py)
3691 src#int "page bias"
3692 (fun () -> conf.pagebias)
3693 (fun v -> conf.pagebias <- v);
3695 src#int "scroll step"
3696 (fun () -> conf.scrollstep)
3697 (fun n -> conf.scrollstep <- n);
3699 src#int "horizontal scroll step"
3700 (fun () -> conf.hscrollstep)
3701 (fun v -> conf.hscrollstep <- v);
3703 src#int "auto scroll step"
3704 (fun () ->
3705 match state.autoscroll with
3706 | Some step -> step
3707 | _ -> conf.autoscrollstep)
3708 (fun n ->
3709 let n = boundastep state.winh n in
3710 if state.autoscroll <> None
3711 then state.autoscroll <- Some n;
3712 conf.autoscrollstep <- n);
3714 src#int "zoom"
3715 (fun () -> truncate (conf.zoom *. 100.))
3716 (fun v -> pivotzoom ((float v) /. 100.));
3718 src#int "rotation"
3719 (fun () -> conf.angle)
3720 (fun v -> reqlayout v conf.fitmodel);
3722 src#int "scroll bar width"
3723 (fun () -> conf.scrollbw)
3724 (fun v ->
3725 conf.scrollbw <- v;
3726 reshape state.winw state.winh;
3729 src#int "scroll handle height"
3730 (fun () -> conf.scrollh)
3731 (fun v -> conf.scrollh <- v;);
3733 src#int "thumbnail width"
3734 (fun () -> conf.thumbw)
3735 (fun v ->
3736 conf.thumbw <- min 4096 v;
3737 match oldmode with
3738 | Birdseye beye ->
3739 leavebirdseye beye false;
3740 enterbirdseye ()
3741 | Textentry _
3742 | View
3743 | LinkNav _ -> ()
3746 let mode = state.mode in
3747 src#string "columns"
3748 (fun () ->
3749 match conf.columns with
3750 | Csingle _ -> "1"
3751 | Cmulti (multi, _) -> multicolumns_to_string multi
3752 | Csplit (count, _) -> "-" ^ string_of_int count
3754 (fun v ->
3755 let n, a, b = multicolumns_of_string v in
3756 setcolumns mode n a b);
3758 sep ();
3759 src#caption "Pixmap cache" 0;
3760 src#int_with_suffix "size (advisory)"
3761 (fun () -> conf.memlimit)
3762 (fun v -> conf.memlimit <- v);
3764 src#caption2 "used"
3765 (fun () ->
3766 Printf.sprintf "%s bytes, %d tiles"
3767 (string_with_suffix_of_int state.memused)
3768 (Hashtbl.length state.tilemap)) 1;
3770 sep ();
3771 src#caption "Layout" 0;
3772 src#caption2 "Dimension"
3773 (fun () ->
3774 Printf.sprintf "%dx%d (virtual %dx%d)"
3775 state.winw state.winh
3776 state.w state.maxy)
3778 if conf.debug
3779 then
3780 src#caption2 "Position" (fun () ->
3781 Printf.sprintf "%dx%d" state.x state.y
3783 else
3784 src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
3786 sep ();
3787 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3788 "Save these parameters as global defaults at exit"
3789 (fun () -> conf.bedefault)
3790 (fun v -> conf.bedefault <- v);
3792 sep ();
3793 let btos b = if b then UniSyms.lguillemet else UniSyms.rguillemet in
3794 src#bool ~offset:0 ~btos "Extended parameters"
3795 (fun () -> !showextended)
3796 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3797 if !showextended
3798 then (
3799 src#bool "checkers"
3800 (fun () -> conf.checkers)
3801 (fun v -> conf.checkers <- v; setcheckers v);
3802 src#bool "update cursor"
3803 (fun () -> conf.updatecurs)
3804 (fun v -> conf.updatecurs <- v);
3805 src#bool "scroll-bar on the left"
3806 (fun () -> conf.leftscroll)
3807 (fun v -> conf.leftscroll <- v);
3808 src#bool "verbose"
3809 (fun () -> conf.verbose)
3810 (fun v -> conf.verbose <- v);
3811 src#bool "invert colors"
3812 (fun () -> conf.invert)
3813 (fun v -> conf.invert <- v);
3814 src#bool "max fit"
3815 (fun () -> conf.maxhfit)
3816 (fun v -> conf.maxhfit <- v);
3817 src#bool "pax mode"
3818 (fun () -> conf.pax != None)
3819 (fun v ->
3820 if v
3821 then conf.pax <- Some (now ())
3822 else conf.pax <- None);
3823 src#string "uri launcher"
3824 (fun () -> conf.urilauncher)
3825 (fun v -> conf.urilauncher <- v);
3826 src#string "path launcher"
3827 (fun () -> conf.pathlauncher)
3828 (fun v -> conf.pathlauncher <- v);
3829 src#string "tile size"
3830 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3831 (fun v ->
3833 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3834 conf.tilew <- max 64 w;
3835 conf.tileh <- max 64 h;
3836 flushtiles ();
3837 with exn ->
3838 state.text <- Printf.sprintf "bad tile size `%s': %s"
3839 v @@ exntos exn
3841 src#int "texture count"
3842 (fun () -> conf.texcount)
3843 (fun v ->
3844 if realloctexts v
3845 then conf.texcount <- v
3846 else impmsg "failed to set texture count please retry later"
3848 src#int "slice height"
3849 (fun () -> conf.sliceheight)
3850 (fun v ->
3851 conf.sliceheight <- v;
3852 wcmd "sliceh %d" conf.sliceheight;
3854 src#int "anti-aliasing level"
3855 (fun () -> conf.aalevel)
3856 (fun v ->
3857 conf.aalevel <- bound v 0 8;
3858 state.anchor <- getanchor ();
3859 opendoc state.path state.password;
3861 src#string "page scroll scaling factor"
3862 (fun () -> string_of_float conf.pgscale)
3863 (fun v ->
3865 let s = float_of_string v in
3866 conf.pgscale <- s
3867 with exn ->
3868 state.text <- Printf.sprintf
3869 "bad page scroll scaling factor `%s': %s" v
3870 @@ exntos exn
3873 src#int "ui font size"
3874 (fun () -> fstate.fontsize)
3875 (fun v -> setfontsize (bound v 5 100));
3876 src#int "hint font size"
3877 (fun () -> conf.hfsize)
3878 (fun v -> conf.hfsize <- bound v 5 100);
3879 src#bool "crop hack"
3880 (fun () -> conf.crophack)
3881 (fun v -> conf.crophack <- v);
3882 src#string "trim fuzz"
3883 (fun () -> irect_to_string conf.trimfuzz)
3884 (fun v ->
3886 conf.trimfuzz <- irect_of_string v;
3887 if conf.trimmargins
3888 then settrim true conf.trimfuzz;
3889 with exn ->
3890 state.text <- Printf.sprintf "bad irect `%s': %s" v
3891 @@ exntos exn
3893 src#string "throttle"
3894 (fun () ->
3895 match conf.maxwait with
3896 | None -> "show place holder if page is not ready"
3897 | Some time ->
3898 if time = infinity
3899 then "wait for page to fully render"
3900 else
3901 "wait " ^ string_of_float time
3902 ^ " seconds before showing placeholder"
3904 (fun v ->
3906 let f = float_of_string v in
3907 if f <= 0.0
3908 then conf.maxwait <- None
3909 else conf.maxwait <- Some f
3910 with exn ->
3911 state.text <- Printf.sprintf "bad time `%s': %s" v
3912 @@ exntos exn
3914 src#string "ghyll scroll"
3915 (fun () ->
3916 match conf.ghyllscroll with
3917 | None -> E.s
3918 | Some nab -> ghyllscroll_to_string nab
3920 (fun v ->
3921 try conf.ghyllscroll <- ghyllscroll_of_string v
3922 with
3923 | Failure msg ->
3924 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3925 | exn ->
3926 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3927 @@ exntos exn
3929 src#string "selection command"
3930 (fun () -> conf.selcmd)
3931 (fun v -> conf.selcmd <- v);
3932 src#string "synctex command"
3933 (fun () -> conf.stcmd)
3934 (fun v -> conf.stcmd <- v);
3935 src#string "pax command"
3936 (fun () -> conf.paxcmd)
3937 (fun v -> conf.paxcmd <- v);
3938 src#string "ask password command"
3939 (fun () -> conf.passcmd)
3940 (fun v -> conf.passcmd <- v);
3941 src#string "save path command"
3942 (fun () -> conf.savecmd)
3943 (fun v -> conf.savecmd <- v);
3944 src#colorspace "color space"
3945 (fun () -> CSTE.to_string conf.colorspace)
3946 (fun v ->
3947 conf.colorspace <- CSTE.of_int v;
3948 wcmd "cs %d" v;
3949 load state.layout;
3951 src#paxmark "pax mark method"
3952 (fun () -> MTE.to_string conf.paxmark)
3953 (fun v -> conf.paxmark <- MTE.of_int v);
3954 if bousable () && !opengl_has_pbo
3955 then
3956 src#bool "use PBO"
3957 (fun () -> conf.usepbo)
3958 (fun v -> conf.usepbo <- v);
3959 src#bool "mouse wheel scrolls pages"
3960 (fun () -> conf.wheelbypage)
3961 (fun v -> conf.wheelbypage <- v);
3962 src#bool "open remote links in a new instance"
3963 (fun () -> conf.riani)
3964 (fun v -> conf.riani <- v);
3965 src#bool "edit annotations inline"
3966 (fun () -> conf.annotinline)
3967 (fun v -> conf.annotinline <- v);
3968 src#bool "coarse positioning in presentation mode"
3969 (fun () -> conf.coarseprespos)
3970 (fun v -> conf.coarseprespos <- v);
3971 src#bool "use document CSS"
3972 (fun () -> conf.usedoccss)
3973 (fun v ->
3974 conf.usedoccss <- v;
3975 state.anchor <- getanchor ();
3976 opendoc state.path state.password;
3978 src#bool ~btos "colors"
3979 (fun () -> !showcolors)
3980 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
3981 if !showcolors
3982 then (
3983 colorp " background"
3984 (fun () -> conf.bgcolor)
3985 (fun v -> conf.bgcolor <- v; setbgcol v);
3986 rgba " scrollbar"
3987 (fun () -> conf.sbarcolor)
3988 (fun v -> conf.sbarcolor <- v);
3989 rgba " scrollbar handle"
3990 (fun () -> conf.sbarhndlcolor)
3991 (fun v -> conf.sbarhndlcolor <- v);
3995 sep ();
3996 src#caption "Document" 0;
3997 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3998 src#caption2 "Pages"
3999 (fun () -> string_of_int state.pagecount) 1;
4000 src#caption2 "Dimensions"
4001 (fun () -> string_of_int (List.length state.pdims)) 1;
4002 if nonemptystr conf.css
4003 then src#caption2 "CSS" (fun () -> conf.css) 1;
4004 if conf.trimmargins
4005 then (
4006 sep ();
4007 src#caption "Trimmed margins" 0;
4008 src#caption2 "Dimensions"
4009 (fun () -> string_of_int (List.length state.pdims)) 1;
4012 sep ();
4013 src#caption "OpenGL" 0;
4014 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4015 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4017 sep ();
4018 src#caption "Location" 0;
4019 if nonemptystr state.origin
4020 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4021 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4023 src#reset prevmode prevuioh;
4025 fun () ->
4026 state.text <- E.s;
4027 resetmstate ();
4028 let prevmode = state.mode
4029 and prevuioh = state.uioh in
4030 fillsrc prevmode prevuioh;
4031 let source = (src :> lvsource) in
4032 let modehash = findkeyhash conf "info" in
4033 state.uioh <-
4034 coe (object (self)
4035 inherit listview ~zebra:false ~helpmode:false
4036 ~source ~trusted:true ~modehash as super
4037 val mutable m_prevmemused = 0
4038 method! infochanged = function
4039 | Memused ->
4040 if m_prevmemused != state.memused
4041 then (
4042 m_prevmemused <- state.memused;
4043 G.postRedisplay "memusedchanged";
4045 | Pdim -> G.postRedisplay "pdimchanged"
4046 | Docinfo -> fillsrc prevmode prevuioh
4048 method! key key mask =
4049 if not (Wsi.withctrl mask)
4050 then
4051 match [@warning "-4"] Wsi.kc2kt key with
4052 | Keys.Left -> coe (self#updownlevel ~-1)
4053 | Keys.Right -> coe (self#updownlevel 1)
4054 | _ -> super#key key mask
4055 else super#key key mask
4056 end);
4057 G.postRedisplay "info";
4060 let enterhelpmode =
4061 let source =
4062 (object
4063 inherit lvsourcebase
4064 method getitemcount = Array.length state.help
4065 method getitem n =
4066 let s, l, _ = state.help.(n) in
4067 (s, l)
4069 method exit ~uioh ~cancel ~active ~first ~pan =
4070 let optuioh =
4071 if not cancel
4072 then (
4073 match state.help.(active) with
4074 | _, _, Action f -> Some (f uioh)
4075 | _, _, Noaction -> Some uioh
4077 else None
4079 m_active <- active;
4080 m_first <- first;
4081 m_pan <- pan;
4082 optuioh
4084 method hasaction n =
4085 match state.help.(n) with
4086 | _, _, Action _ -> true
4087 | _, _, Noaction -> false
4089 initializer
4090 m_active <- -1
4091 end)
4092 in fun () ->
4093 let modehash = findkeyhash conf "help" in
4094 resetmstate ();
4095 state.uioh <- coe (new listview
4096 ~zebra:false ~helpmode:true
4097 ~source ~trusted:true ~modehash);
4098 G.postRedisplay "help";
4101 let entermsgsmode =
4102 let msgsource =
4103 (object
4104 inherit lvsourcebase
4105 val mutable m_items = E.a
4107 method getitemcount = 1 + Array.length m_items
4109 method getitem n =
4110 if n = 0
4111 then "[Clear]", 0
4112 else m_items.(n-1), 0
4114 method exit ~uioh ~cancel ~active ~first ~pan =
4115 ignore uioh;
4116 if not cancel
4117 then (
4118 if active = 0
4119 then Buffer.clear state.errmsgs;
4121 m_active <- active;
4122 m_first <- first;
4123 m_pan <- pan;
4124 None
4126 method hasaction n =
4127 n = 0
4129 method reset =
4130 state.newerrmsgs <- false;
4131 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4132 m_items <- Array.of_list l
4134 initializer
4135 m_active <- 0
4136 end)
4137 in fun () ->
4138 state.text <- E.s;
4139 resetmstate ();
4140 msgsource#reset;
4141 let source = (msgsource :> lvsource) in
4142 let modehash = findkeyhash conf "listview" in
4143 state.uioh <-
4144 coe (object
4145 inherit listview ~zebra:false ~helpmode:false
4146 ~source ~trusted:false ~modehash as super
4147 method! display =
4148 if state.newerrmsgs
4149 then msgsource#reset;
4150 super#display
4151 end);
4152 G.postRedisplay "msgs";
4155 let getusertext s =
4156 let editor = getenvwithdef "EDITOR" E.s in
4157 if emptystr editor
4158 then E.s
4159 else
4160 let tmppath = Filename.temp_file "llpp" "note" in
4161 if nonemptystr s
4162 then (
4163 let oc = open_out tmppath in
4164 output_string oc s;
4165 close_out oc;
4167 let execstr = editor ^ " " ^ tmppath in
4168 let s =
4169 match spawn execstr [] with
4170 | exception exn ->
4171 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4173 | pid ->
4174 match Unix.waitpid [] pid with
4175 | exception exn ->
4176 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4178 | (_pid, status) ->
4179 match status with
4180 | Unix.WEXITED 0 -> filecontents tmppath
4181 | Unix.WEXITED n ->
4182 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4184 | Unix.WSIGNALED n ->
4185 impmsg "editor process(%s) was killed by signal %d" execstr n;
4187 | Unix.WSTOPPED n ->
4188 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4191 match Unix.unlink tmppath with
4192 | exception exn ->
4193 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4195 | () -> s
4198 let enterannotmode opaque slinkindex =
4199 let msgsource =
4200 (object
4201 inherit lvsourcebase
4202 val mutable m_text = E.s
4203 val mutable m_items = E.a
4205 method getitemcount = Array.length m_items
4207 method getitem n =
4208 let label, _func = m_items.(n) in
4209 label, 0
4211 method exit ~uioh ~cancel ~active ~first ~pan =
4212 ignore (uioh, first, pan);
4213 if not cancel
4214 then (
4215 let _label, func = m_items.(active) in
4216 func ()
4218 None
4220 method hasaction n = nonemptystr @@ fst m_items.(n)
4222 method reset s =
4223 let rec split accu b i =
4224 let p = b+i in
4225 if p = String.length s
4226 then (String.sub s b (p-b), unit) :: accu
4227 else
4228 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4229 then
4230 let ss = if i = 0 then E.s else String.sub s b i in
4231 split ((ss, unit)::accu) (p+1) 0
4232 else
4233 split accu b (i+1)
4235 let cleanup () =
4236 wcmd "freepage %s" (~> opaque);
4237 let keys =
4238 Hashtbl.fold (fun key opaque' accu ->
4239 if opaque' = opaque'
4240 then key :: accu else accu) state.pagemap []
4242 List.iter (Hashtbl.remove state.pagemap) keys;
4243 flushtiles ();
4244 gotoxy state.x state.y
4246 let dele () =
4247 delannot opaque slinkindex;
4248 cleanup ();
4250 let edit inline () =
4251 let update s =
4252 if emptystr s
4253 then dele ()
4254 else (
4255 modannot opaque slinkindex s;
4256 cleanup ();
4259 if inline
4260 then
4261 let mode = state.mode in
4262 state.mode <-
4263 Textentry (
4264 ("annotation: ", m_text, None, textentry, update, true),
4265 fun _ -> state.mode <- mode);
4266 state.text <- E.s;
4267 enttext ();
4268 else
4269 let s = getusertext m_text in
4270 update s
4272 m_text <- s;
4273 m_items <-
4274 ( "[Copy]", fun () -> selstring m_text)
4275 :: ("[Delete]", dele)
4276 :: ("[Edit]", edit conf.annotinline)
4277 :: (E.s, unit)
4278 :: split [] 0 0 |> List.rev |> Array.of_list
4280 initializer
4281 m_active <- 0
4282 end)
4284 state.text <- E.s;
4285 let s = getannotcontents opaque slinkindex in
4286 resetmstate ();
4287 msgsource#reset s;
4288 let source = (msgsource :> lvsource) in
4289 let modehash = findkeyhash conf "listview" in
4290 state.uioh <- coe (object
4291 inherit listview ~zebra:false ~helpmode:false
4292 ~source ~trusted:false ~modehash
4293 end);
4294 G.postRedisplay "enterannotmode";
4297 let gotoremote spec =
4298 let filename, dest = splitatchar spec '#' in
4299 let getpath filename =
4300 let path =
4301 if nonemptystr filename
4302 then
4303 if Filename.is_relative filename
4304 then
4305 let dir = Filename.dirname state.path in
4306 let dir =
4307 if Filename.is_implicit dir
4308 then Filename.concat (Sys.getcwd ()) dir
4309 else dir
4311 Filename.concat dir filename
4312 else filename
4313 else E.s
4315 if Sys.file_exists path
4316 then path
4317 else E.s
4319 let path = getpath filename in
4320 let dospawn lcmd =
4321 if conf.riani
4322 then
4323 let cmd = Lazy.force_val lcmd in
4324 match spawn cmd with
4325 | _pid -> ()
4326 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
4327 else
4328 let anchor = getanchor () in
4329 let ranchor = state.path, state.password, anchor, state.origin in
4330 state.origin <- E.s;
4331 state.ranchors <- ranchor :: state.ranchors;
4332 opendoc path E.s;
4334 if substratis spec 0 "page="
4335 then
4336 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4337 | pageno ->
4338 state.anchor <- (pageno, 0.0, 0.0);
4339 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4340 | exception exn ->
4341 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4342 else (
4343 state.nameddest <- dest;
4344 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4348 let gotounder = function
4349 | Ulinkuri s when isexternallink s ->
4350 if substratis s 0 "file://"
4351 then gotoremote @@ String.sub s 7 (String.length s - 7)
4352 else gotouri s
4353 | Ulinkuri s ->
4354 let pageno, x, y = uritolocation s in
4355 addnav ();
4356 gotopagexy !wtmode pageno x y
4357 | Utext _ | Unone -> ()
4358 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4361 let gotooutline (_, _, kind) =
4362 match kind with
4363 | Onone -> ()
4364 | Oanchor anchor ->
4365 let (pageno, y, _) = anchor in
4366 let y = getanchory
4367 (if conf.presentation then (pageno, y, 1.0) else anchor)
4369 addnav ();
4370 gotoghyll y
4371 | Ouri uri -> gotounder (Ulinkuri uri)
4372 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4373 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4374 | Ohistory hist -> gotohist hist
4375 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4378 class outlinesoucebase fetchoutlines = object (self)
4379 inherit lvsourcebase
4380 val mutable m_items = E.a
4381 val mutable m_minfo = E.a
4382 val mutable m_orig_items = E.a
4383 val mutable m_orig_minfo = E.a
4384 val mutable m_narrow_patterns = []
4385 val mutable m_gen = -1
4387 method getitemcount = Array.length m_items
4389 method getitem n =
4390 let s, n, _ = m_items.(n) in
4391 (s, n+0)
4393 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4394 ignore (uioh, first);
4395 let items, minfo =
4396 if m_narrow_patterns = []
4397 then m_orig_items, m_orig_minfo
4398 else m_items, m_minfo
4400 m_pan <- pan;
4401 if not cancel
4402 then (
4403 m_items <- items;
4404 m_minfo <- minfo;
4405 gotooutline m_items.(active);
4407 else (
4408 m_items <- items;
4409 m_minfo <- minfo;
4411 None
4413 method hasaction (_:int) = true
4415 method greetmsg =
4416 if Array.length m_items != Array.length m_orig_items
4417 then
4418 let s =
4419 match m_narrow_patterns with
4420 | one :: [] -> one
4421 | many -> String.concat UniSyms.ellipsis (List.rev many)
4423 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4424 else E.s
4426 method statestr =
4427 match m_narrow_patterns with
4428 | [] -> E.s
4429 | one :: [] -> one
4430 | head :: _ -> UniSyms.ellipsis ^ head
4432 method narrow pattern =
4433 match Str.regexp_case_fold pattern with
4434 | exception _ -> ()
4435 | re ->
4436 let rec loop accu minfo n =
4437 if n = -1
4438 then (
4439 m_items <- Array.of_list accu;
4440 m_minfo <- Array.of_list minfo;
4442 else
4443 let (s, _, _) as o = m_items.(n) in
4444 let accu, minfo =
4445 match Str.search_forward re s 0 with
4446 | exception Not_found -> accu, minfo
4447 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4449 loop accu minfo (n-1)
4451 loop [] [] (Array.length m_items - 1)
4453 method! getminfo = m_minfo
4455 method denarrow =
4456 m_orig_items <- fetchoutlines ();
4457 m_minfo <- m_orig_minfo;
4458 m_items <- m_orig_items
4460 method add_narrow_pattern pattern =
4461 m_narrow_patterns <- pattern :: m_narrow_patterns
4463 method del_narrow_pattern =
4464 match m_narrow_patterns with
4465 | _ :: rest -> m_narrow_patterns <- rest
4466 | [] -> ()
4468 method renarrow =
4469 self#denarrow;
4470 match m_narrow_patterns with
4471 | pattern :: [] -> self#narrow pattern; pattern
4472 | list ->
4473 List.fold_left (fun accu pattern ->
4474 self#narrow pattern;
4475 pattern ^ UniSyms.ellipsis ^ accu) E.s list
4477 method calcactive (_:anchor) = 0
4479 method reset anchor items =
4480 if state.gen != m_gen
4481 then (
4482 m_orig_items <- items;
4483 m_items <- items;
4484 m_narrow_patterns <- [];
4485 m_minfo <- E.a;
4486 m_orig_minfo <- E.a;
4487 m_gen <- state.gen;
4489 else (
4490 if items != m_orig_items
4491 then (
4492 m_orig_items <- items;
4493 if m_narrow_patterns == []
4494 then m_items <- items;
4497 let active = self#calcactive anchor in
4498 m_active <- active;
4499 m_first <- firstof m_first active
4503 let outlinesource fetchoutlines =
4504 (object
4505 inherit outlinesoucebase fetchoutlines
4506 method! calcactive anchor =
4507 let rely = getanchory anchor in
4508 let rec loop n best bestd =
4509 if n = Array.length m_items
4510 then best
4511 else
4512 let _, _, kind = m_items.(n) in
4513 match kind with
4514 | Oanchor anchor ->
4515 let orely = getanchory anchor in
4516 let d = abs (orely - rely) in
4517 if d < bestd
4518 then loop (n+1) n d
4519 else loop (n+1) best bestd
4520 | Onone | Oremote _ | Olaunch _
4521 | Oremotedest _ | Ouri _ | Ohistory _ ->
4522 loop (n+1) best bestd
4524 loop 0 ~-1 max_int
4525 end)
4528 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4529 let mkselector sourcetype =
4530 let fetchoutlines () =
4531 match sourcetype with
4532 | `bookmarks -> Array.of_list state.bookmarks
4533 | `outlines -> state.outlines
4534 | `history -> genhistoutlines () |> Array.of_list
4536 let source =
4537 if sourcetype = `history
4538 then new outlinesoucebase fetchoutlines
4539 else outlinesource fetchoutlines
4541 (fun errmsg ->
4542 let outlines = fetchoutlines () in
4543 if Array.length outlines = 0
4544 then showtext ' ' errmsg
4545 else (
4546 resetmstate ();
4547 Wsi.setcursor Wsi.CURSOR_INHERIT;
4548 let anchor = getanchor () in
4549 source#reset anchor outlines;
4550 state.text <- source#greetmsg;
4551 state.uioh <-
4552 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4553 G.postRedisplay "enter selector";
4557 let mkenter sourcetype errmsg =
4558 let enter = mkselector sourcetype in
4559 fun () -> enter errmsg
4561 ( mkenter `outlines "document has no outline"
4562 , mkenter `bookmarks "document has no bookmarks (yet)"
4563 , mkenter `history "history is empty" )
4566 let quickbookmark ?title () =
4567 match state.layout with
4568 | [] -> ()
4569 | l :: _ ->
4570 let title =
4571 match title with
4572 | None ->
4573 Unix.(
4574 let tm = localtime (now ()) in
4575 Printf.sprintf
4576 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
4577 (l.pageno+1)
4578 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
4580 | Some title -> title
4582 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4585 let setautoscrollspeed step goingdown =
4586 let incr = max 1 ((abs step) / 2) in
4587 let incr = if goingdown then incr else -incr in
4588 let astep = boundastep state.winh (step + incr) in
4589 state.autoscroll <- Some astep;
4592 let canpan () =
4593 match conf.columns with
4594 | Csplit _ -> true
4595 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4598 let panbound x = bound x (-state.w) state.winw;;
4600 let existsinrow pageno (columns, coverA, coverB) p =
4601 let last = ((pageno - coverA) mod columns) + columns in
4602 let rec any = function
4603 | [] -> false
4604 | l :: rest ->
4605 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4606 then p l
4607 else (
4608 if not (p l)
4609 then (if l.pageno = last then false else any rest)
4610 else true
4613 any state.layout
4616 let nextpage () =
4617 match state.layout with
4618 | [] ->
4619 let pageno = page_of_y state.y in
4620 gotoghyll (getpagey (pageno+1))
4621 | l :: rest ->
4622 match conf.columns with
4623 | Csingle _ ->
4624 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4625 then
4626 let y = clamp (pgscale state.winh) in
4627 gotoghyll y
4628 else
4629 let pageno = min (l.pageno+1) (state.pagecount-1) in
4630 gotoghyll (getpagey pageno)
4631 | Cmulti ((c, _, _) as cl, _) ->
4632 if conf.presentation
4633 && (existsinrow l.pageno cl
4634 (fun l -> l.pageh > l.pagey + l.pagevh))
4635 then
4636 let y = clamp (pgscale state.winh) in
4637 gotoghyll y
4638 else
4639 let pageno = min (l.pageno+c) (state.pagecount-1) in
4640 gotoghyll (getpagey pageno)
4641 | Csplit (n, _) ->
4642 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4643 then
4644 let pagey, pageh = getpageyh l.pageno in
4645 let pagey = pagey + pageh * l.pagecol in
4646 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4647 gotoghyll (pagey + pageh + ips)
4650 let prevpage () =
4651 match state.layout with
4652 | [] ->
4653 let pageno = page_of_y state.y in
4654 gotoghyll (getpagey (pageno-1))
4655 | l :: _ ->
4656 match conf.columns with
4657 | Csingle _ ->
4658 if conf.presentation && l.pagey != 0
4659 then
4660 gotoghyll (clamp (pgscale ~-(state.winh)))
4661 else
4662 let pageno = max 0 (l.pageno-1) in
4663 gotoghyll (getpagey pageno)
4664 | Cmulti ((c, _, coverB) as cl, _) ->
4665 if conf.presentation &&
4666 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4667 then
4668 gotoghyll (clamp (pgscale ~-(state.winh)))
4669 else
4670 let decr =
4671 if l.pageno = state.pagecount - coverB
4672 then 1
4673 else c
4675 let pageno = max 0 (l.pageno-decr) in
4676 gotoghyll (getpagey pageno)
4677 | Csplit (n, _) ->
4678 let y =
4679 if l.pagecol = 0
4680 then
4681 if l.pageno = 0
4682 then l.pagey
4683 else
4684 let pageno = max 0 (l.pageno-1) in
4685 let pagey, pageh = getpageyh pageno in
4686 pagey + (n-1)*pageh
4687 else
4688 let pagey, pageh = getpageyh l.pageno in
4689 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4691 gotoghyll y
4694 let save () =
4695 if emptystr conf.savecmd
4696 then adderrmsg "savepath-command is empty"
4697 "don't know where to save modified document"
4698 else
4699 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4700 let path =
4701 getcmdoutput
4702 (fun exn ->
4703 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
4704 savecmd
4706 if nonemptystr path
4707 then
4708 let tmp = path ^ ".tmp" in
4709 savedoc tmp;
4710 Unix.rename tmp path;
4713 let viewkeyboard key mask =
4714 let enttext te =
4715 let mode = state.mode in
4716 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4717 state.text <- E.s;
4718 enttext ();
4719 G.postRedisplay "view:enttext"
4721 let ctrl = Wsi.withctrl mask in
4722 let open Keys in
4723 match Wsi.kc2kt key with
4724 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
4726 | Ascii 'Q' -> exit 0
4728 | Ascii 'W' ->
4729 if hasunsavedchanges ()
4730 then save ()
4732 | Insert ->
4733 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4734 then (
4735 state.mode <- (
4736 match state.lnava with
4737 | None -> LinkNav (Ltgendir 0)
4738 | Some pn -> LinkNav (Ltexact pn)
4740 gotoxy state.x state.y;
4742 else impmsg "keyboard link navigation does not work under rotation"
4744 | Escape | Ascii 'q' ->
4745 begin match state.mstate with
4746 | Mzoomrect _ ->
4747 resetmstate ();
4748 G.postRedisplay "kill rect";
4749 | Msel _
4750 | Mpan _
4751 | Mscrolly | Mscrollx
4752 | Mzoom _
4753 | Mnone ->
4754 begin match state.mode with
4755 | LinkNav ln ->
4756 begin match ln with
4757 | Ltexact pl -> state.lnava <- Some pl
4758 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4759 end;
4760 state.mode <- View;
4761 G.postRedisplay "esc leave linknav"
4762 | Birdseye _ | Textentry _ | View ->
4763 match state.ranchors with
4764 | [] -> raise Quit
4765 | (path, password, anchor, origin) :: rest ->
4766 state.ranchors <- rest;
4767 state.anchor <- anchor;
4768 state.origin <- origin;
4769 state.nameddest <- E.s;
4770 opendoc path password
4771 end;
4772 end;
4774 | Backspace ->
4775 addnavnorc ();
4776 gotoghyll (getnav ~-1)
4778 | Ascii 'o' ->
4779 enteroutlinemode ()
4781 | Ascii 'H' ->
4782 enterhistmode ()
4784 | Ascii 'u' ->
4785 state.rects <- [];
4786 state.text <- E.s;
4787 Hashtbl.iter (fun _ opaque ->
4788 clearmark opaque;
4789 Hashtbl.clear state.prects) state.pagemap;
4790 G.postRedisplay "dehighlight";
4792 | Ascii (('/' | '?') as c) ->
4793 let ondone isforw s =
4794 cbput state.hists.pat s;
4795 state.searchpattern <- s;
4796 search s isforw
4798 let s = String.make 1 c in
4799 enttext (s, E.s, Some (onhist state.hists.pat),
4800 textentry, ondone (c = '/'), true)
4802 | Ascii '+' | Ascii '=' when ctrl ->
4803 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4804 pivotzoom (conf.zoom +. incr)
4806 | Ascii '+' ->
4807 let ondone s =
4808 let n =
4809 try int_of_string s with exn ->
4810 state.text <-
4811 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4812 max_int
4814 if n != max_int
4815 then (
4816 conf.pagebias <- n;
4817 state.text <- "page bias is now " ^ string_of_int n;
4820 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4822 | Ascii '-' when ctrl ->
4823 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4824 pivotzoom (max 0.01 (conf.zoom -. decr))
4826 | Ascii '-' ->
4827 let ondone msg = state.text <- msg in
4828 enttext (
4829 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4830 optentry state.mode, ondone, true
4833 | Ascii '0' when ctrl ->
4834 if conf.zoom = 1.0
4835 then gotoxy 0 state.y
4836 else setzoom 1.0
4838 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
4839 let cols =
4840 match conf.columns with
4841 | Csingle _ | Cmulti _ -> 1
4842 | Csplit (n, _) -> n
4844 let h = state.winh -
4845 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4847 let zoom = zoomforh state.winw h 0 cols in
4848 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4849 then setzoom zoom
4851 | Ascii '3' when ctrl ->
4852 let fm =
4853 match conf.fitmodel with
4854 | FitWidth -> FitProportional
4855 | FitProportional -> FitPage
4856 | FitPage -> FitWidth
4858 state.text <- "fit model: " ^ FMTE.to_string fm;
4859 reqlayout conf.angle fm
4861 | Ascii '4' when ctrl ->
4862 let zoom = getmaxw () /. float state.winw in
4863 if zoom > 0.0 then setzoom zoom
4865 | Fn 9 ->
4866 togglebirdseye ()
4868 | Ascii '9' when ctrl ->
4869 togglebirdseye ()
4871 | Ascii ('0'..'9' as c) when not ctrl ->
4872 let ondone s =
4873 let n =
4874 try int_of_string s with exn ->
4875 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4878 if n >= 0
4879 then (
4880 addnav ();
4881 cbput state.hists.pag (string_of_int n);
4882 gotopage1 (n + conf.pagebias - 1) 0;
4885 let [@warning "-4"] pageentry text = function
4886 | Keys.Ascii 'g' -> TEdone text
4887 | key -> intentry text key
4889 let text = String.make 1 c in
4890 enttext (":", text, Some (onhist state.hists.pag),
4891 pageentry, ondone, true)
4893 | Ascii 'b' ->
4894 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4895 G.postRedisplay "toggle scrollbar";
4897 | Ascii 'B' ->
4898 state.bzoom <- not state.bzoom;
4899 state.rects <- [];
4900 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4902 | Ascii 'l' ->
4903 conf.hlinks <- not conf.hlinks;
4904 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4905 G.postRedisplay "toggle highlightlinks";
4907 | Ascii 'F' ->
4908 if conf.angle mod 360 = 0
4909 then (
4910 state.glinks <- true;
4911 let mode = state.mode in
4912 state.mode <-
4913 Textentry (
4914 (":", E.s, None, linknentry, linknact gotounder, false),
4915 (fun _ ->
4916 state.glinks <- false;
4917 state.mode <- mode)
4919 state.text <- E.s;
4920 G.postRedisplay "view:linkent(F)"
4922 else impmsg "hint mode does not work under rotation"
4924 | Ascii 'y' ->
4925 state.glinks <- true;
4926 let mode = state.mode in
4927 state.mode <-
4928 Textentry (
4929 (":", E.s, None, linknentry,
4930 linknact (fun under -> selstring (undertext under)), false),
4931 (fun _ ->
4932 state.glinks <- false;
4933 state.mode <- mode)
4935 state.text <- E.s;
4936 G.postRedisplay "view:linkent"
4938 | Ascii 'a' ->
4939 begin match state.autoscroll with
4940 | Some step ->
4941 conf.autoscrollstep <- step;
4942 state.autoscroll <- None
4943 | None ->
4944 state.autoscroll <- Some conf.autoscrollstep;
4945 state.slideshow <- state.slideshow land lnot 2
4948 | Ascii 'p' when ctrl ->
4949 launchpath () (* XXX where do error messages go? *)
4951 | Ascii 'P' ->
4952 setpresentationmode (not conf.presentation);
4953 showtext ' ' ("presentation mode " ^
4954 if conf.presentation then "on" else "off");
4956 | Ascii 'f' ->
4957 if List.mem Wsi.Fullscreen state.winstate
4958 then Wsi.reshape conf.cwinw conf.cwinh
4959 else Wsi.fullscreen ()
4961 | Ascii ('p'|'N') ->
4962 search state.searchpattern false
4964 | Ascii 'n' | Fn 3 ->
4965 search state.searchpattern true
4967 | Ascii 't' ->
4968 begin match state.layout with
4969 | [] -> ()
4970 | l :: _ ->
4971 gotoghyll (getpagey l.pageno)
4974 | Ascii ' ' ->
4975 nextpage ()
4977 | Delete ->
4978 prevpage ()
4980 | Ascii '=' ->
4981 showtext ' ' (describe_layout state.layout);
4983 | Ascii 'w' ->
4984 begin match state.layout with
4985 | [] -> ()
4986 | l :: _ ->
4987 Wsi.reshape l.pagew l.pageh;
4988 G.postRedisplay "w"
4991 | Ascii '\'' ->
4992 enterbookmarkmode ()
4994 | Ascii 'h' | Fn 1 ->
4995 enterhelpmode ()
4997 | Ascii 'i' ->
4998 enterinfomode ()
5000 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
5001 entermsgsmode ()
5003 | Ascii 'm' ->
5004 let ondone s =
5005 match state.layout with
5006 | l :: _ ->
5007 if nonemptystr s
5008 then
5009 state.bookmarks <-
5010 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5011 | _ -> ()
5013 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5015 | Ascii '~' ->
5016 quickbookmark ();
5017 showtext ' ' "Quick bookmark added";
5019 | Ascii 'z' ->
5020 begin match state.layout with
5021 | l :: _ ->
5022 let rect = getpdimrect l.pagedimno in
5023 let w, h =
5024 if conf.crophack
5025 then
5026 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5027 truncate (1.2 *. (rect.(3) -. rect.(0))))
5028 else
5029 (truncate (rect.(1) -. rect.(0)),
5030 truncate (rect.(3) -. rect.(0)))
5032 let w = truncate ((float w)*.conf.zoom)
5033 and h = truncate ((float h)*.conf.zoom) in
5034 if w != 0 && h != 0
5035 then (
5036 state.anchor <- getanchor ();
5037 Wsi.reshape w (h + conf.interpagespace)
5039 G.postRedisplay "z";
5041 | [] -> ()
5044 | Ascii 'x' -> state.roam ()
5046 | Ascii ('<'|'>' as c) ->
5047 reqlayout
5048 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
5050 | Ascii ('['|']' as c) ->
5051 conf.colorscale <-
5052 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
5053 G.postRedisplay "brightness";
5055 | Ascii 'c' when state.mode = View ->
5056 if Wsi.withalt mask
5057 then (
5058 if conf.zoom > 1.0
5059 then
5060 let m = (state.winw - state.w) / 2 in
5061 gotoxy_and_clear_text m state.y
5063 else
5064 let (c, a, b), z =
5065 match state.prevcolumns with
5066 | None -> (1, 0, 0), 1.0
5067 | Some (columns, z) ->
5068 let cab =
5069 match columns with
5070 | Csplit (c, _) -> -c, 0, 0
5071 | Cmulti ((c, a, b), _) -> c, a, b
5072 | Csingle _ -> 1, 0, 0
5074 cab, z
5076 setcolumns View c a b;
5077 setzoom z
5079 | Down | Up when ctrl && Wsi.withshift mask ->
5080 let zoom, x = state.prevzoom in
5081 setzoom zoom;
5082 state.x <- x;
5084 | Ascii 'k' | Up ->
5085 begin match state.autoscroll with
5086 | None ->
5087 begin match state.mode with
5088 | Birdseye beye -> upbirdseye 1 beye
5089 | Textentry _ | View | LinkNav _ ->
5090 if ctrl
5091 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5092 else (
5093 if not (Wsi.withshift mask) && conf.presentation
5094 then prevpage ()
5095 else gotoghyll1 true (clamp (-conf.scrollstep))
5098 | Some n ->
5099 setautoscrollspeed n false
5102 | Ascii 'j' | Down ->
5103 begin match state.autoscroll with
5104 | None ->
5105 begin match state.mode with
5106 | Birdseye beye -> downbirdseye 1 beye
5107 | Textentry _ | View | LinkNav _ ->
5108 if ctrl
5109 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5110 else (
5111 if not (Wsi.withshift mask) && conf.presentation
5112 then nextpage ()
5113 else gotoghyll1 true (clamp (conf.scrollstep))
5116 | Some n ->
5117 setautoscrollspeed n true
5120 | Left | Right when not (Wsi.withalt mask) ->
5121 if canpan ()
5122 then
5123 let dx =
5124 if ctrl
5125 then state.winw / 2
5126 else conf.hscrollstep
5128 let dx =
5129 let pv = Wsi.kc2kt key in
5130 if pv = Keys.Left then dx else -dx
5132 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5133 else (
5134 state.text <- E.s;
5135 G.postRedisplay "left/right"
5138 | Prior ->
5139 let y =
5140 if ctrl
5141 then
5142 match state.layout with
5143 | [] -> state.y
5144 | l :: _ -> state.y - l.pagey
5145 else
5146 clamp (pgscale (-state.winh))
5148 gotoghyll y
5150 | Next ->
5151 let y =
5152 if ctrl
5153 then
5154 match List.rev state.layout with
5155 | [] -> state.y
5156 | l :: _ -> getpagey l.pageno
5157 else
5158 clamp (pgscale state.winh)
5160 gotoghyll y
5162 | Ascii 'g' | Home ->
5163 addnav ();
5164 gotoghyll 0
5165 | Ascii 'G' | End ->
5166 addnav ();
5167 gotoghyll (clamp state.maxy)
5169 | Right when Wsi.withalt mask ->
5170 addnavnorc ();
5171 gotoghyll (getnav 1)
5172 | Left when Wsi.withalt mask ->
5173 addnavnorc ();
5174 gotoghyll (getnav ~-1)
5176 | Ascii 'r' ->
5177 reload ()
5179 | Ascii 'v' when conf.debug ->
5180 state.rects <- [];
5181 List.iter (fun l ->
5182 match getopaque l.pageno with
5183 | None -> ()
5184 | Some opaque ->
5185 let x0, y0, x1, y1 = pagebbox opaque in
5186 let rect = (float x0, float y0,
5187 float x1, float y0,
5188 float x1, float y1,
5189 float x0, float y1) in
5190 debugrect rect;
5191 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5192 state.rects <- (l.pageno, color, rect) :: state.rects;
5193 ) state.layout;
5194 G.postRedisplay "v";
5196 | Ascii '|' ->
5197 let mode = state.mode in
5198 let cmd = ref E.s in
5199 let onleave = function
5200 | Cancel -> state.mode <- mode
5201 | Confirm ->
5202 List.iter (fun l ->
5203 match getopaque l.pageno with
5204 | Some opaque -> pipesel opaque !cmd
5205 | None -> ()) state.layout;
5206 state.mode <- mode
5208 let ondone s =
5209 cbput state.hists.sel s;
5210 cmd := s
5212 let te =
5213 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5215 G.postRedisplay "|";
5216 state.mode <- Textentry (te, onleave);
5218 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
5219 vlog "huh? %s" (Wsi.keyname key)
5222 let linknavkeyboard key mask linknav =
5223 let pv = Wsi.kc2kt key in
5224 let getpage pageno =
5225 let rec loop = function
5226 | [] -> None
5227 | l :: _ when l.pageno = pageno -> Some l
5228 | _ :: rest -> loop rest
5229 in loop state.layout
5231 let doexact (pageno, n) =
5232 match getopaque pageno, getpage pageno with
5233 | Some opaque, Some l ->
5234 if pv = Keys.Enter
5235 then
5236 let under = getlink opaque n in
5237 G.postRedisplay "link gotounder";
5238 gotounder under;
5239 state.mode <- View;
5240 else
5241 let opt, dir =
5242 let open Keys in
5243 match pv with
5244 | Home -> Some (findlink opaque LDfirst), -1
5245 | End -> Some (findlink opaque LDlast), 1
5246 | Left -> Some (findlink opaque (LDleft n)), -1
5247 | Right -> Some (findlink opaque (LDright n)), 1
5248 | Up -> Some (findlink opaque (LDup n)), -1
5249 | Down -> Some (findlink opaque (LDdown n)), 1
5251 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
5252 | Code _|Fn _|Ctrl _|Backspace -> None, 0
5254 let pwl l dir =
5255 begin match findpwl l.pageno dir with
5256 | Pwlnotfound -> ()
5257 | Pwl pageno ->
5258 let notfound dir =
5259 state.mode <- LinkNav (Ltgendir dir);
5260 let y, h = getpageyh pageno in
5261 let y =
5262 if dir < 0
5263 then y + h - state.winh
5264 else y
5266 gotoxy state.x y
5268 begin match getopaque pageno, getpage pageno with
5269 | Some opaque, Some _ ->
5270 let link =
5271 let ld = if dir > 0 then LDfirst else LDlast in
5272 findlink opaque ld
5274 begin match link with
5275 | Lfound m ->
5276 showlinktype (getlink opaque m);
5277 state.mode <- LinkNav (Ltexact (pageno, m));
5278 G.postRedisplay "linknav jpage";
5279 | Lnotfound -> notfound dir
5280 end;
5281 | _ -> notfound dir
5282 end;
5283 end;
5285 begin match opt with
5286 | Some Lnotfound -> pwl l dir;
5287 | Some (Lfound m) ->
5288 if m = n
5289 then pwl l dir
5290 else (
5291 let _, y0, _, y1 = getlinkrect opaque m in
5292 if y0 < l.pagey
5293 then gotopage1 l.pageno y0
5294 else (
5295 let d = fstate.fontsize + 1 in
5296 if y1 - l.pagey > l.pagevh - d
5297 then gotopage1 l.pageno (y1 - state.winh + d)
5298 else G.postRedisplay "linknav";
5300 showlinktype (getlink opaque m);
5301 state.mode <- LinkNav (Ltexact (l.pageno, m));
5304 | None -> viewkeyboard key mask
5305 end;
5306 | _ -> viewkeyboard key mask
5308 if pv = Keys.Insert
5309 then (
5310 begin match linknav with
5311 | Ltexact pa -> state.lnava <- Some pa
5312 | Ltgendir _ | Ltnotready _ -> ()
5313 end;
5314 state.mode <- View;
5315 G.postRedisplay "leave linknav"
5317 else
5318 match linknav with
5319 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5320 | Ltexact exact -> doexact exact
5323 let keyboard key mask =
5324 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5325 then wcmd "interrupt"
5326 else state.uioh <- state.uioh#key key mask
5329 let birdseyekeyboard key mask
5330 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5331 let incr =
5332 match conf.columns with
5333 | Csingle _ -> 1
5334 | Cmulti ((c, _, _), _) -> c
5335 | Csplit _ -> failwith "bird's eye split mode"
5337 let pgh layout = List.fold_left
5338 (fun m l -> max l.pageh m) state.winh layout in
5339 let open Keys in
5340 match Wsi.kc2kt key with
5341 | Ascii 'l' when Wsi.withctrl mask ->
5342 let y, h = getpageyh pageno in
5343 let top = (state.winh - h) / 2 in
5344 gotoxy state.x (max 0 (y - top))
5345 | Enter -> leavebirdseye beye false
5346 | Escape -> leavebirdseye beye true
5347 | Up -> upbirdseye incr beye
5348 | Down -> downbirdseye incr beye
5349 | Left -> upbirdseye 1 beye
5350 | Right -> downbirdseye 1 beye
5352 | Prior ->
5353 begin match state.layout with
5354 | l :: _ ->
5355 if l.pagey != 0
5356 then (
5357 state.mode <- Birdseye (
5358 oconf, leftx, l.pageno, hooverpageno, anchor
5360 gotopage1 l.pageno 0;
5362 else (
5363 let layout = layout state.x (state.y-state.winh)
5364 state.winw
5365 (pgh state.layout) in
5366 match layout with
5367 | [] -> gotoxy state.x (clamp (-state.winh))
5368 | l :: _ ->
5369 state.mode <- Birdseye (
5370 oconf, leftx, l.pageno, hooverpageno, anchor
5372 gotopage1 l.pageno 0
5375 | [] -> gotoxy state.x (clamp (-state.winh))
5376 end;
5378 | Next ->
5379 begin match List.rev state.layout with
5380 | l :: _ ->
5381 let layout = layout state.x
5382 (state.y + (pgh state.layout))
5383 state.winw state.winh in
5384 begin match layout with
5385 | [] ->
5386 let incr = l.pageh - l.pagevh in
5387 if incr = 0
5388 then (
5389 state.mode <-
5390 Birdseye (
5391 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5393 G.postRedisplay "birdseye pagedown";
5395 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5397 | l :: _ ->
5398 state.mode <-
5399 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5400 gotopage1 l.pageno 0;
5403 | [] -> gotoxy state.x (clamp state.winh)
5404 end;
5406 | Home ->
5407 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5408 gotopage1 0 0
5410 | End ->
5411 let pageno = state.pagecount - 1 in
5412 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5413 if not (pagevisible state.layout pageno)
5414 then
5415 let h =
5416 match List.rev state.pdims with
5417 | [] -> state.winh
5418 | (_, _, h, _) :: _ -> h
5420 gotoxy
5421 state.x
5422 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5423 else G.postRedisplay "birdseye end";
5425 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
5428 let drawpage l =
5429 let color =
5430 match state.mode with
5431 | Textentry _ -> scalecolor 0.4
5432 | LinkNav _ | View -> scalecolor 1.0
5433 | Birdseye (_, _, pageno, hooverpageno, _) ->
5434 if l.pageno = hooverpageno
5435 then scalecolor 0.9
5436 else (
5437 if l.pageno = pageno
5438 then (
5439 let c = scalecolor 1.0 in
5440 GlDraw.color c;
5441 GlDraw.line_width 3.0;
5442 let dispx = l.pagedispx in
5443 linerect
5444 (float (dispx-1)) (float (l.pagedispy-1))
5445 (float (dispx+l.pagevw+1))
5446 (float (l.pagedispy+l.pagevh+1))
5448 GlDraw.line_width 1.0;
5451 else scalecolor 0.8
5454 drawtiles l color;
5457 let postdrawpage l linkindexbase =
5458 match getopaque l.pageno with
5459 | Some opaque ->
5460 if tileready l l.pagex l.pagey
5461 then
5462 let x = l.pagedispx - l.pagex
5463 and y = l.pagedispy - l.pagey in
5464 let hlmask =
5465 match conf.columns with
5466 | Csingle _ | Cmulti _ ->
5467 (if conf.hlinks then 1 else 0)
5468 + (if state.glinks
5469 && not (isbirdseye state.mode) then 2 else 0)
5470 | Csplit _ -> 0
5472 let s =
5473 match state.mode with
5474 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5475 | Textentry _
5476 | Birdseye _
5477 | View
5478 | LinkNav _ -> E.s
5480 Hashtbl.find_all state.prects l.pageno |>
5481 List.iter (fun vals -> drawprect opaque x y vals);
5482 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5483 if n < 0
5484 then (state.redisplay <- true; 0)
5485 else n
5486 else 0
5487 | _ -> 0
5490 let scrollindicator () =
5491 let sbw, ph, sh = state.uioh#scrollph in
5492 let sbh, pw, sw = state.uioh#scrollpw in
5494 let x0,x1,hx0 =
5495 if conf.leftscroll
5496 then (0, sbw, sbw)
5497 else ((state.winw - sbw), state.winw, 0)
5500 Gl.enable `blend;
5501 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5502 let (r, g, b, alpha) = conf.sbarcolor in
5503 GlDraw.color (r, g, b) ~alpha;
5504 filledrect (float x0) 0. (float x1) (float state.winh);
5505 filledrect
5506 (float hx0) (float (state.winh - sbh))
5507 (float (hx0 + state.winw)) (float state.winh);
5508 let (r, g, b, alpha) = conf.sbarhndlcolor in
5509 GlDraw.color (r, g, b) ~alpha;
5511 filledrect (float x0) ph (float x1) (ph +. sh);
5512 let pw = pw +. float hx0 in
5513 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5514 Gl.disable `blend;
5517 let showsel () =
5518 match state.mstate with
5519 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5522 | Msel ((x0, y0), (x1, y1)) ->
5523 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5524 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5525 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5526 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5529 let showrects =
5530 function [] -> ()
5531 | rects ->
5532 Gl.enable `blend;
5533 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5534 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5535 List.iter
5536 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5537 List.iter (fun l ->
5538 if l.pageno = pageno
5539 then (
5540 let dx = float (l.pagedispx - l.pagex) in
5541 let dy = float (l.pagedispy - l.pagey) in
5542 let r, g, b, alpha = c in
5543 GlDraw.color (r, g, b) ~alpha;
5544 filledrect2 (x0+.dx) (y0+.dy)
5545 (x1+.dx) (y1+.dy)
5546 (x3+.dx) (y3+.dy)
5547 (x2+.dx) (y2+.dy);
5549 ) state.layout
5550 ) rects;
5551 Gl.disable `blend;
5554 let display () =
5555 GlDraw.color (scalecolor2 conf.bgcolor);
5556 GlClear.color (scalecolor2 conf.bgcolor);
5557 GlClear.clear [`color];
5558 List.iter drawpage state.layout;
5559 let rects =
5560 match state.mode with
5561 | LinkNav (Ltexact (pageno, linkno)) ->
5562 begin match getopaque pageno with
5563 | Some opaque ->
5564 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5565 let color = (0.0, 0.0, 0.5, 0.5) in
5566 (pageno, color,
5567 (float x0, float y0,
5568 float x1, float y0,
5569 float x1, float y1,
5570 float x0, float y1)
5571 ) :: state.rects
5572 | None -> state.rects
5574 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5575 | Birdseye _
5576 | Textentry _
5577 | View -> state.rects
5579 showrects rects;
5580 let rec postloop linkindexbase = function
5581 | l :: rest ->
5582 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5583 postloop linkindexbase rest
5584 | [] -> ()
5586 showsel ();
5587 postloop 0 state.layout;
5588 state.uioh#display;
5589 begin match state.mstate with
5590 | Mzoomrect ((x0, y0), (x1, y1)) ->
5591 Gl.enable `blend;
5592 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5593 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5594 filledrect (float x0) (float y0) (float x1) (float y1);
5595 Gl.disable `blend;
5596 | Msel _
5597 | Mpan _
5598 | Mscrolly | Mscrollx
5599 | Mzoom _
5600 | Mnone -> ()
5601 end;
5602 enttext ();
5603 scrollindicator ();
5604 Wsi.swapb ();
5607 let zoomrect x y x1 y1 =
5608 let x0 = min x x1
5609 and x1 = max x x1
5610 and y0 = min y y1 in
5611 let zoom = (float state.w) /. float (x1 - x0) in
5612 let margin =
5613 let simple () =
5614 if state.w < state.winw
5615 then (state.winw - state.w) / 2
5616 else 0
5618 match conf.fitmodel with
5619 | FitWidth | FitProportional -> simple ()
5620 | FitPage ->
5621 match conf.columns with
5622 | Csplit _ ->
5623 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5624 | Cmulti _ | Csingle _ -> simple ()
5626 gotoxy ((state.x + margin) - x0) (state.y + y0);
5627 state.anchor <- getanchor ();
5628 setzoom zoom;
5629 resetmstate ();
5632 let annot inline x y =
5633 match unproject x y with
5634 | Some (opaque, n, ux, uy) ->
5635 let add text =
5636 addannot opaque ux uy text;
5637 wcmd "freepage %s" (~> opaque);
5638 Hashtbl.remove state.pagemap (n, state.gen);
5639 flushtiles ();
5640 gotoxy state.x state.y
5642 if inline
5643 then
5644 let ondone s = add s in
5645 let mode = state.mode in
5646 state.mode <- Textentry (
5647 ("annotation: ", E.s, None, textentry, ondone, true),
5648 fun _ -> state.mode <- mode);
5649 state.text <- E.s;
5650 enttext ();
5651 G.postRedisplay "annot"
5652 else
5653 add @@ getusertext E.s
5654 | _ -> ()
5657 let zoomblock x y =
5658 let g opaque l px py =
5659 match rectofblock opaque px py with
5660 | Some a ->
5661 let x0 = a.(0) -. 20. in
5662 let x1 = a.(1) +. 20. in
5663 let y0 = a.(2) -. 20. in
5664 let zoom = (float state.w) /. (x1 -. x0) in
5665 let pagey = getpagey l.pageno in
5666 let margin = (state.w - l.pagew)/2 in
5667 let nx = -truncate x0 - margin in
5668 gotoxy_and_clear_text nx (pagey + truncate y0);
5669 state.anchor <- getanchor ();
5670 setzoom zoom;
5671 None
5672 | None -> None
5674 match conf.columns with
5675 | Csplit _ ->
5676 impmsg "block zooming does not work properly in split columns mode"
5677 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5680 let scrollx x =
5681 let winw = state.winw - 1 in
5682 let s = float x /. float winw in
5683 let destx = truncate (float (state.w + winw) *. s) in
5684 gotoxy_and_clear_text (winw - destx) state.y;
5685 state.mstate <- Mscrollx;
5688 let scrolly y =
5689 let s = float y /. float state.winh in
5690 let desty = truncate (s *. float (maxy ())) in
5691 gotoxy_and_clear_text state.x desty;
5692 state.mstate <- Mscrolly;
5695 let viewmulticlick clicks x y mask =
5696 let g opaque l px py =
5697 let mark =
5698 match clicks with
5699 | 2 -> Mark_word
5700 | 3 -> Mark_line
5701 | 4 -> Mark_block
5702 | _ -> Mark_page
5704 if markunder opaque px py mark
5705 then (
5706 Some (fun () ->
5707 let dopipe cmd =
5708 match getopaque l.pageno with
5709 | None -> ()
5710 | Some opaque -> pipesel opaque cmd
5712 state.roam <- (fun () -> dopipe conf.paxcmd);
5713 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5716 else None
5718 G.postRedisplay "viewmulticlick";
5719 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5722 let canselect () =
5723 match conf.columns with
5724 | Csplit _ -> false
5725 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5728 let viewmouse button down x y mask =
5729 match button with
5730 | n when (n == 4 || n == 5) && not down ->
5731 if Wsi.withctrl mask
5732 then (
5733 let incr =
5734 if n = 5
5735 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5736 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5738 let fx, fy =
5739 match state.mstate with
5740 | Mzoom (oldn, _, pos) when n = oldn -> pos
5741 | Mzoomrect _ | Mnone | Mpan _
5742 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
5744 let zoom = conf.zoom -. incr in
5745 state.mstate <- Mzoom (n, 0, (x, y));
5746 if false && abs (fx - x) > 5 || abs (fy - y) > 5
5747 then pivotzoom ~x ~y zoom
5748 else pivotzoom zoom
5750 else (
5751 match state.autoscroll with
5752 | Some step -> setautoscrollspeed step (n=4)
5753 | None ->
5754 if conf.wheelbypage || conf.presentation
5755 then (
5756 if n = 4
5757 then prevpage ()
5758 else nextpage ()
5760 else
5761 let incr =
5762 if n = 4
5763 then -conf.scrollstep
5764 else conf.scrollstep
5766 let incr = incr * 2 in
5767 let y = clamp incr in
5768 gotoxy_and_clear_text state.x y
5771 | n when (n = 6 || n = 7) && not down && canpan () ->
5772 let x =
5773 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5774 gotoxy_and_clear_text x state.y
5776 | 1 when Wsi.withshift mask ->
5777 state.mstate <- Mnone;
5778 if not down
5779 then (
5780 match unproject x y with
5781 | None -> ()
5782 | Some (_, pageno, ux, uy) ->
5783 let cmd = Printf.sprintf
5784 "%s %s %d %d %d"
5785 conf.stcmd state.path pageno ux uy
5787 match spawn cmd [] with
5788 | exception exn ->
5789 impmsg "execution of synctex command(%S) failed: %S"
5790 conf.stcmd @@ exntos exn
5791 | _pid -> ()
5794 | 1 when Wsi.withctrl mask ->
5795 if down
5796 then (
5797 Wsi.setcursor Wsi.CURSOR_FLEUR;
5798 state.mstate <- Mpan (x, y)
5800 else
5801 state.mstate <- Mnone
5803 | 3 ->
5804 if down
5805 then (
5806 if Wsi.withshift mask
5807 then (
5808 annot conf.annotinline x y;
5809 G.postRedisplay "addannot"
5811 else
5812 let p = (x, y) in
5813 Wsi.setcursor Wsi.CURSOR_CYCLE;
5814 state.mstate <- Mzoomrect (p, p)
5816 else (
5817 match state.mstate with
5818 | Mzoomrect ((x0, y0), _) ->
5819 if abs (x-x0) > 10 && abs (y - y0) > 10
5820 then zoomrect x0 y0 x y
5821 else (
5822 resetmstate ();
5823 G.postRedisplay "kill accidental zoom rect";
5825 | Msel _
5826 | Mpan _
5827 | Mscrolly | Mscrollx
5828 | Mzoom _
5829 | Mnone -> resetmstate ()
5832 | 1 when vscrollhit x ->
5833 if down
5834 then
5835 let _, position, sh = state.uioh#scrollph in
5836 if y > truncate position && y < truncate (position +. sh)
5837 then state.mstate <- Mscrolly
5838 else scrolly y
5839 else
5840 state.mstate <- Mnone
5842 | 1 when y > state.winh - hscrollh () ->
5843 if down
5844 then
5845 let _, position, sw = state.uioh#scrollpw in
5846 if x > truncate position && x < truncate (position +. sw)
5847 then state.mstate <- Mscrollx
5848 else scrollx x
5849 else
5850 state.mstate <- Mnone
5852 | 1 when state.bzoom -> if not down then zoomblock x y
5854 | 1 ->
5855 let dest = if down then getunder x y else Unone in
5856 begin match dest with
5857 | Ulinkuri _ ->
5858 gotounder dest
5860 | Unone when down ->
5861 Wsi.setcursor Wsi.CURSOR_FLEUR;
5862 state.mstate <- Mpan (x, y);
5864 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5866 | Unone | Utext _ ->
5867 if down
5868 then (
5869 if canselect ()
5870 then (
5871 state.mstate <- Msel ((x, y), (x, y));
5872 G.postRedisplay "mouse select";
5875 else (
5876 match state.mstate with
5877 | Mnone -> ()
5879 | Mzoom _ | Mscrollx | Mscrolly ->
5880 state.mstate <- Mnone
5882 | Mzoomrect ((x0, y0), _) ->
5883 zoomrect x0 y0 x y
5885 | Mpan _ ->
5886 Wsi.setcursor Wsi.CURSOR_INHERIT;
5887 state.mstate <- Mnone
5889 | Msel ((x0, y0), (x1, y1)) ->
5890 let rec loop = function
5891 | [] -> ()
5892 | l :: rest ->
5893 let inside =
5894 let a0 = l.pagedispy in
5895 let a1 = a0 + l.pagevh in
5896 let b0 = l.pagedispx in
5897 let b1 = b0 + l.pagevw in
5898 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5899 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5901 if inside
5902 then
5903 match getopaque l.pageno with
5904 | Some opaque ->
5905 let dosel cmd () =
5906 pipef ~closew:false "Msel"
5907 (fun w ->
5908 copysel w opaque;
5909 G.postRedisplay "Msel") cmd
5911 dosel conf.selcmd ();
5912 state.roam <- dosel conf.paxcmd;
5913 | None -> ()
5914 else loop rest
5916 loop state.layout;
5917 resetmstate ();
5921 | _ -> ()
5924 let birdseyemouse button down x y mask
5925 (conf, leftx, _, hooverpageno, anchor) =
5926 match button with
5927 | 1 when down ->
5928 let rec loop = function
5929 | [] -> ()
5930 | l :: rest ->
5931 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5932 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5933 then (
5934 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5936 else loop rest
5938 loop state.layout
5939 | 3 -> ()
5940 | _ -> viewmouse button down x y mask
5943 let uioh = object
5944 method display = ()
5946 method key key mask =
5947 begin match state.mode with
5948 | Textentry textentry -> textentrykeyboard key mask textentry
5949 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5950 | View -> viewkeyboard key mask
5951 | LinkNav linknav -> linknavkeyboard key mask linknav
5952 end;
5953 state.uioh
5955 method button button bstate x y mask =
5956 begin match state.mode with
5957 | LinkNav _ | View -> viewmouse button bstate x y mask
5958 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5959 | Textentry _ -> ()
5960 end;
5961 state.uioh
5963 method multiclick clicks x y mask =
5964 begin match state.mode with
5965 | LinkNav _ | View -> viewmulticlick clicks x y mask
5966 | Birdseye _ | Textentry _ -> ()
5967 end;
5968 state.uioh
5970 method motion x y =
5971 begin match state.mode with
5972 | Textentry _ -> ()
5973 | View | Birdseye _ | LinkNav _ ->
5974 match state.mstate with
5975 | Mzoom _ | Mnone -> ()
5977 | Mpan (x0, y0) ->
5978 let dx = x - x0
5979 and dy = y0 - y in
5980 state.mstate <- Mpan (x, y);
5981 let x = if canpan () then panbound (state.x + dx) else state.x in
5982 let y = clamp dy in
5983 gotoxy_and_clear_text x y
5985 | Msel (a, _) ->
5986 state.mstate <- Msel (a, (x, y));
5987 G.postRedisplay "motion select";
5989 | Mscrolly ->
5990 let y = min state.winh (max 0 y) in
5991 scrolly y
5993 | Mscrollx ->
5994 let x = min state.winw (max 0 x) in
5995 scrollx x
5997 | Mzoomrect (p0, _) ->
5998 state.mstate <- Mzoomrect (p0, (x, y));
5999 G.postRedisplay "motion zoomrect";
6000 end;
6001 state.uioh
6003 method pmotion x y =
6004 begin match state.mode with
6005 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6006 let rec loop = function
6007 | [] ->
6008 if hooverpageno != -1
6009 then (
6010 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6011 G.postRedisplay "pmotion birdseye no hoover";
6013 | l :: rest ->
6014 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6015 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6016 then (
6017 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6018 G.postRedisplay "pmotion birdseye hoover";
6020 else loop rest
6022 loop state.layout
6024 | Textentry _ -> ()
6026 | LinkNav _ | View ->
6027 match state.mstate with
6028 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6029 | Mnone ->
6030 updateunder x y;
6031 if canselect ()
6032 then
6033 match conf.pax with
6034 | None -> ()
6035 | Some past ->
6036 let now = now () in
6037 let delta = now -. past in
6038 if delta > 0.01
6039 then paxunder x y
6040 else conf.pax <- Some now
6041 end;
6042 state.uioh
6044 method infochanged _ = ()
6046 method scrollph =
6047 let maxy = maxy () in
6048 let p, h =
6049 if maxy = 0
6050 then 0.0, float state.winh
6051 else scrollph state.y maxy
6053 vscrollw (), p, h
6055 method scrollpw =
6056 let fwinw = float (state.winw - vscrollw ()) in
6057 let sw =
6058 let sw = fwinw /. float state.w in
6059 let sw = fwinw *. sw in
6060 max sw (float conf.scrollh)
6062 let position =
6063 let maxx = state.w + state.winw in
6064 let x = state.winw - state.x in
6065 let percent = float x /. float maxx in
6066 (fwinw -. sw) *. percent
6068 hscrollh (), position, sw
6070 method modehash =
6071 let modename =
6072 match state.mode with
6073 | LinkNav _ -> "links"
6074 | Textentry _ -> "textentry"
6075 | Birdseye _ -> "birdseye"
6076 | View -> "view"
6078 findkeyhash conf modename
6080 method eformsgs = true
6081 method alwaysscrolly = false
6082 method scroll dx dy =
6083 let x = if canpan () then panbound (state.x + dx) else state.x in
6084 gotoxy_and_clear_text x (clamp (2 * dy));
6085 state.uioh
6086 method zoom z x y =
6087 pivotzoom ~x ~y (conf.zoom *. exp z);
6088 end;;
6090 let addrect pageno r g b a x0 y0 x1 y1 =
6091 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6094 let ract cmds =
6095 let cl = splitatchar cmds ' ' in
6096 let scan s fmt f =
6097 try Scanf.sscanf s fmt f
6098 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
6099 cmds @@ exntos exn
6101 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6102 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6103 s pageno r g b a x0 y0 x1 y1;
6104 onpagerect
6105 pageno
6106 (fun w h ->
6107 let _,w1,h1,_ = getpagedim pageno in
6108 let sw = float w1 /. float w
6109 and sh = float h1 /. float h in
6110 let x0s = x0 *. sw
6111 and x1s = x1 *. sw
6112 and y0s = y0 *. sh
6113 and y1s = y1 *. sh in
6114 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6115 let color = (r, g, b, a) in
6116 if conf.verbose then debugrect rect;
6117 state.rects <- (pageno, color, rect) :: state.rects;
6118 G.postRedisplay s;
6121 match cl with
6122 | "reload", "" -> reload ()
6123 | "goto", args ->
6124 scan args "%u %f %f"
6125 (fun pageno x y ->
6126 let cmd, _ = state.geomcmds in
6127 if emptystr cmd
6128 then gotopagexy !wtmode pageno x y
6129 else
6130 let f prevf () =
6131 gotopagexy !wtmode pageno x y;
6132 prevf ()
6134 state.reprf <- f state.reprf
6136 | "goto1", args -> scan args "%u %f" gotopage
6137 | "gotor", args -> scan args "%S" gotoremote
6138 | "rect", args ->
6139 scan args "%u %u %f %f %f %f"
6140 (fun pageno c x0 y0 x1 y1 ->
6141 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6142 rectx "rect" pageno color x0 y0 x1 y1;
6144 | "prect", args ->
6145 scan args "%u %f %f %f %f %f %f %f %f"
6146 (fun pageno r g b alpha x0 y0 x1 y1 ->
6147 addrect pageno r g b alpha x0 y0 x1 y1;
6148 G.postRedisplay "prect"
6150 | "pgoto", args ->
6151 scan args "%u %f %f"
6152 (fun pageno x y ->
6153 let optopaque =
6154 match getopaque pageno with
6155 | Some opaque -> opaque
6156 | None -> ~< E.s
6158 pgoto optopaque pageno x y;
6159 let rec fixx = function
6160 | [] -> ()
6161 | l :: rest ->
6162 if l.pageno = pageno
6163 then gotoxy (state.x - l.pagedispx) state.y
6164 else fixx rest
6166 let layout =
6167 let mult =
6168 match conf.columns with
6169 | Csingle _ | Csplit _ -> 1
6170 | Cmulti ((n, _, _), _) -> n
6172 layout 0 state.y (state.winw * mult) state.winh
6174 fixx layout
6176 | "activatewin", "" -> Wsi.activatewin ()
6177 | "quit", "" -> raise Quit
6178 | "keys", keys ->
6179 begin try
6180 let l = Config.keys_of_string keys in
6181 List.iter (fun (k, m) -> keyboard k m) l
6182 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
6183 cmds @@ exntos exn
6185 | "clearrects", "" ->
6186 Hashtbl.clear state.prects;
6187 G.postRedisplay "clearrects"
6188 | _ ->
6189 adderrfmt "remote command"
6190 "error processing remote command: %S\n" cmds;
6193 let remote =
6194 let scratch = Bytes.create 80 in
6195 let buf = Buffer.create 80 in
6196 fun fd ->
6197 match tempfailureretry (Unix.read fd scratch 0) 80 with
6198 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6199 | 0 ->
6200 Unix.close fd;
6201 if Buffer.length buf > 0
6202 then (
6203 let s = Buffer.contents buf in
6204 Buffer.clear buf;
6205 ract s;
6207 None
6208 | n ->
6209 let rec eat ppos =
6210 let nlpos =
6211 match Bytes.index_from scratch ppos '\n' with
6212 | pos -> if pos >= n then -1 else pos
6213 | exception Not_found -> -1
6215 if nlpos >= 0
6216 then (
6217 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6218 let s = Buffer.contents buf in
6219 Buffer.clear buf;
6220 ract s;
6221 eat (nlpos+1);
6223 else (
6224 Buffer.add_subbytes buf scratch ppos (n-ppos);
6225 Some fd
6227 in eat 0
6230 let remoteopen path =
6231 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6232 with exn ->
6233 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6234 None
6237 let () =
6238 let gcconfig = ref false in
6239 let trimcachepath = ref E.s in
6240 let rcmdpath = ref E.s in
6241 let pageno = ref None in
6242 let rootwid = ref 0 in
6243 let openlast = ref false in
6244 let doreap = ref false in
6245 let csspath = ref None in
6246 selfexec := Sys.executable_name;
6247 Arg.parse
6248 (Arg.align
6249 [("-p", Arg.String (fun s -> state.password <- s),
6250 "<password> Set password");
6252 ("-f", Arg.String
6253 (fun s ->
6254 Config.fontpath := s;
6255 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6257 "<path> Set path to the user interface font");
6259 ("-c", Arg.String
6260 (fun s ->
6261 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6262 Config.confpath := s),
6263 "<path> Set path to the configuration file");
6265 ("-last", Arg.Set openlast, " Open last document");
6267 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6268 "<page-number> Jump to page");
6270 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6271 "<path> Set path to the trim cache file");
6273 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6274 "<named-destination> Set named destination");
6276 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6277 ("-cxack", Arg.Set cxack, " Cut corners");
6279 ("-remote", Arg.String (fun s -> rcmdpath := s),
6280 "<path> Set path to the source of remote commands");
6282 ("-gc", Arg.Set gcconfig, " Collect config garbage");
6284 ("-v", Arg.Unit (fun () ->
6285 Printf.printf
6286 "%s\nconfiguration path: %s\n"
6287 (version ())
6288 Config.defconfpath;
6289 exit 0), " Print version and exit");
6291 ("-css", Arg.String (fun s -> csspath := Some s),
6292 "<path> Set path to the style sheet to use with EPUB/HTML");
6294 ("-embed", Arg.Set_int rootwid, "<window-id> Embed into window");
6296 ("-origin", Arg.String (fun s -> state.origin <- s),
6297 "<origin> <undocumented>");
6299 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title")
6302 (fun s -> state.path <- s)
6303 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6305 if !wtmode
6306 then selfexec := !selfexec ^ " -wtmode";
6308 let histmode = emptystr state.path && not !openlast in
6310 if not (Config.load !openlast)
6311 then dolog "failed to load configuration";
6313 begin match !pageno with
6314 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6315 | None -> ()
6316 end;
6318 if !gcconfig
6319 then (
6320 Config.gc ();
6321 exit 0
6324 let mu =
6325 object (self)
6326 val mutable m_clicks = 0
6327 val mutable m_click_x = 0
6328 val mutable m_click_y = 0
6329 val mutable m_lastclicktime = infinity
6331 method private cleanup =
6332 state.roam <- noroam;
6333 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6334 method expose = G.postRedisplay "expose"
6335 method visible v =
6336 let name =
6337 match v with
6338 | Wsi.Unobscured -> "unobscured"
6339 | Wsi.PartiallyObscured -> "partiallyobscured"
6340 | Wsi.FullyObscured -> "fullyobscured"
6342 vlog "visibility change %s" name
6343 method display = display ()
6344 method map mapped = vlog "mapped %b" mapped
6345 method reshape w h =
6346 self#cleanup;
6347 reshape w h
6348 method mouse b d x y m =
6349 if d && canselect ()
6350 then (
6352 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6354 m_click_x <- x;
6355 m_click_y <- y;
6356 if b = 1
6357 then (
6358 let t = now () in
6359 if abs x - m_click_x > 10
6360 || abs y - m_click_y > 10
6361 || abs_float (t -. m_lastclicktime) > 0.3
6362 then m_clicks <- 0;
6363 m_clicks <- m_clicks + 1;
6364 m_lastclicktime <- t;
6365 if m_clicks = 1
6366 then (
6367 self#cleanup;
6368 G.postRedisplay "cleanup";
6369 state.uioh <- state.uioh#button b d x y m;
6371 else state.uioh <- state.uioh#multiclick m_clicks x y m
6373 else (
6374 self#cleanup;
6375 m_clicks <- 0;
6376 m_lastclicktime <- infinity;
6377 state.uioh <- state.uioh#button b d x y m
6380 else (
6381 state.uioh <- state.uioh#button b d x y m
6383 method motion x y =
6384 state.mpos <- (x, y);
6385 state.uioh <- state.uioh#motion x y
6386 method pmotion x y =
6387 state.mpos <- (x, y);
6388 state.uioh <- state.uioh#pmotion x y
6389 method key k m =
6390 vlog "k=%#x m=%#x" k m;
6391 let mascm = m land (
6392 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6393 ) in
6394 let keyboard k m =
6395 let x = state.x and y = state.y in
6396 keyboard k m;
6397 if x != state.x || y != state.y then self#cleanup
6399 match state.keystate with
6400 | KSnone ->
6401 let km = k, mascm in
6402 begin
6403 match
6404 let modehash = state.uioh#modehash in
6405 try Hashtbl.find modehash km
6406 with Not_found ->
6407 try Hashtbl.find (findkeyhash conf "global") km
6408 with Not_found -> KMinsrt (k, m)
6409 with
6410 | KMinsrt (k, m) -> keyboard k m
6411 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6412 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6414 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6415 List.iter (fun (k, m) -> keyboard k m) insrt;
6416 state.keystate <- KSnone
6417 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6418 state.keystate <- KSinto (keys, insrt)
6419 | KSinto _ -> state.keystate <- KSnone
6421 method enter x y =
6422 state.mpos <- (x, y);
6423 state.uioh <- state.uioh#pmotion x y
6424 method leave = state.mpos <- (-1, -1)
6425 method winstate wsl = state.winstate <- wsl
6426 method quit : 'a. 'a = raise Quit
6427 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
6428 method zoom z x y = state.uioh#zoom z x y
6429 method opendoc path =
6430 state.mode <- View;
6431 state.uioh <- uioh;
6432 G.postRedisplay "opendoc";
6433 opendoc path state.password
6436 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6438 setbgcol conf.bgcolor;
6439 state.wsfd <- wsfd;
6441 if not @@ List.exists GlMisc.check_extension
6442 [ "GL_ARB_texture_rectangle"
6443 ; "GL_EXT_texture_recangle"
6444 ; "GL_NV_texture_rectangle" ]
6445 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6447 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6448 then (
6449 defconf.sliceheight <- 1024;
6450 defconf.texcount <- 32;
6451 defconf.usepbo <- true;
6454 let cs, ss =
6455 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6456 | exception exn ->
6457 dolog "socketpair failed: %s" @@ exntos exn;
6458 exit 1
6459 | (r, w) ->
6460 cloexec r;
6461 cloexec w;
6462 r, w
6465 setcheckers conf.checkers;
6467 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6469 begin match !csspath with
6470 | None -> ()
6471 | Some "" -> conf.css <- E.s
6472 | Some path ->
6473 let css = filecontents path in
6474 let l = String.length css in
6475 conf.css <-
6476 if substratis css (l-2) "\r\n"
6477 then String.sub css 0 (l-2)
6478 else (if css.[l-1] = '\n'
6479 then String.sub css 0 (l-1)
6480 else css);
6481 end;
6482 init cs (
6483 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6484 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6485 !Config.fontpath, !trimcachepath, !opengl_has_pbo
6487 List.iter GlArray.enable [`texture_coord; `vertex];
6488 state.ss <- ss;
6489 reshape ~firsttime:true winw winh;
6490 state.uioh <- uioh;
6491 if histmode
6492 then (
6493 Wsi.settitle "llpp (history)";
6494 enterhistmode ();
6496 else (
6497 state.text <- "Opening " ^ (mbtoutf8 state.path);
6498 opendoc state.path state.password;
6500 display ();
6501 Wsi.mapwin ();
6502 Wsi.setcursor Wsi.CURSOR_INHERIT;
6503 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6505 let rec reap () =
6506 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6507 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
6508 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
6509 | 0, _ -> ()
6510 | _pid, _status -> reap ()
6512 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6514 let optrfd =
6515 ref (
6516 if nonemptystr !rcmdpath
6517 then remoteopen !rcmdpath
6518 else None
6522 let rec loop deadline =
6523 if !doreap
6524 then (
6525 doreap := false;
6526 reap ()
6528 let r = [state.ss; state.wsfd] in
6529 let r =
6530 match !optrfd with
6531 | None -> r
6532 | Some fd -> fd :: r
6534 if state.redisplay
6535 then (
6536 state.redisplay <- false;
6537 display ();
6539 let timeout =
6540 let now = now () in
6541 if deadline > now
6542 then (
6543 if deadline = infinity
6544 then ~-.1.0
6545 else max 0.0 (deadline -. now)
6547 else 0.0
6549 let r, _, _ =
6550 try Unix.select r [] [] timeout
6551 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6553 begin match r with
6554 | [] ->
6555 state.ghyll None;
6556 let newdeadline =
6557 if state.ghyll == noghyll
6558 then
6559 match state.autoscroll with
6560 | Some step when step != 0 ->
6561 if state.slideshow land 1 = 1
6562 then (
6563 if state.slideshow land 2 = 0
6564 then state.slideshow <- state.slideshow lor 2
6565 else if step < 0 then prevpage () else nextpage ();
6566 deadline +. (float (abs step))
6568 else
6569 let y = state.y + step in
6570 let fy = if conf.maxhfit then state.winh else 0 in
6571 let y =
6572 if y < 0
6573 then state.maxy - fy
6574 else if y >= state.maxy - fy then 0 else y
6576 if state.mode = View
6577 then gotoxy_and_clear_text state.x y
6578 else gotoxy state.x y;
6579 deadline +. 0.01
6580 | _ -> infinity
6581 else deadline +. 0.01
6583 loop newdeadline
6585 | l ->
6586 let rec checkfds = function
6587 | [] -> ()
6588 | fd :: rest when fd = state.ss ->
6589 let cmd = rcmd state.ss in
6590 act cmd;
6591 checkfds rest
6593 | fd :: rest when fd = state.wsfd ->
6594 Wsi.readresp fd;
6595 checkfds rest
6597 | fd :: rest when Some fd = !optrfd ->
6598 begin match remote fd with
6599 | None -> optrfd := remoteopen !rcmdpath;
6600 | opt -> optrfd := opt
6601 end;
6602 checkfds rest
6604 | _ :: rest ->
6605 dolog "select returned unknown descriptor";
6606 checkfds rest
6608 checkfds l;
6609 let newdeadline =
6610 let deadline1 =
6611 if deadline = infinity
6612 then now () +. 0.01
6613 else deadline
6615 match state.autoscroll with
6616 | Some step when step != 0 -> deadline1
6617 | _ -> if state.ghyll == noghyll then infinity else deadline1
6619 loop newdeadline
6620 end;
6622 match loop infinity with
6623 | exception Quit ->
6624 Config.save leavebirdseye;
6625 if hasunsavedchanges ()
6626 then save ()
6627 | _ -> error "umpossible - infinity reached"