Remove cygwin support
[llpp.git] / main.ml
blobbd2340387ac260f2932a92572bf46f302d76e4dc
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
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 fillhelp () =
1084 state.help <-
1085 let sl = keystostrlist conf in
1086 let rec loop accu =
1087 function | [] -> accu
1088 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1089 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1092 let opendoc path password =
1093 state.path <- path;
1094 state.password <- password;
1095 state.gen <- state.gen + 1;
1096 state.docinfo <- [];
1097 state.outlines <- [||];
1099 flushpages ();
1100 setaalevel conf.aalevel;
1101 let titlepath =
1102 if emptystr state.origin
1103 then path
1104 else state.origin
1106 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1107 wcmd "open %d %d %d %s\000%s\000%s\000"
1108 (btod !wtmode) (btod !cxack) (btod conf.usedoccss)
1109 path password conf.css;
1110 invalidate "reqlayout"
1111 (fun () ->
1112 wcmd "reqlayout %d %d %d %s\000"
1113 conf.angle (FMTE.to_int conf.fitmodel)
1114 (stateh state.winh) state.nameddest
1116 fillhelp ();
1119 let reload () =
1120 state.anchor <- getanchor ();
1121 opendoc state.path state.password;
1124 let scalecolor c =
1125 let c = c *. conf.colorscale in
1126 (c, c, c);
1129 let scalecolor2 (r, g, b) =
1130 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1133 let docolumns columns =
1134 match columns with
1135 | Csingle _ ->
1136 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1137 let rec loop pageno pdimno pdim y ph pdims =
1138 if pageno = state.pagecount
1139 then ()
1140 else
1141 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1142 match pdims with
1143 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1144 pdimno+1, pdim, rest
1145 | _ ->
1146 pdimno, pdim, pdims
1148 let x = max 0 (((state.winw - w) / 2) - xoff) in
1149 let y =
1150 y + (if conf.presentation
1151 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1152 else (if pageno = 0 then 0 else conf.interpagespace)
1155 a.(pageno) <- (pdimno, x, y, pdim);
1156 loop (pageno+1) pdimno pdim (y + h) h pdims
1158 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1159 conf.columns <- Csingle a;
1161 | Cmulti ((columns, coverA, coverB), _) ->
1162 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1163 let rec loop pageno pdimno pdim x y rowh pdims =
1164 let rec fixrow m =
1165 if m = pageno then () else
1166 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1167 if h < rowh
1168 then (
1169 let y = y + (rowh - h) / 2 in
1170 a.(m) <- (pdimno, x, y, pdim);
1172 fixrow (m+1)
1174 if pageno = state.pagecount
1175 then fixrow (((pageno - 1) / columns) * columns)
1176 else
1177 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1178 match pdims with
1179 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1180 pdimno+1, pdim, rest
1181 | _ ->
1182 pdimno, pdim, pdims
1184 let x, y, rowh' =
1185 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1186 then (
1187 let x = (state.winw - w) / 2 in
1188 let ips =
1189 if conf.presentation then calcips h else conf.interpagespace in
1190 x, y + ips + rowh, h
1192 else (
1193 if (pageno - coverA) mod columns = 0
1194 then (
1195 let x = max 0 (state.winw - state.w) / 2 in
1196 let y =
1197 if conf.presentation
1198 then
1199 let ips = calcips h in
1200 y + (if pageno = 0 then 0 else calcips rowh + ips)
1201 else
1202 y + (if pageno = 0 then 0 else conf.interpagespace)
1204 x, y + rowh, h
1206 else x, y, max rowh h
1209 let y =
1210 if pageno > 1 && (pageno - coverA) mod columns = 0
1211 then (
1212 let y =
1213 if pageno = columns && conf.presentation
1214 then (
1215 let ips = calcips rowh in
1216 for i = 0 to pred columns
1218 let (pdimno, x, y, pdim) = a.(i) in
1219 a.(i) <- (pdimno, x, y+ips, pdim)
1220 done;
1221 y+ips;
1223 else y
1225 fixrow (pageno - columns);
1228 else y
1230 a.(pageno) <- (pdimno, x, y, pdim);
1231 let x = x + w + xoff*2 + conf.interpagespace in
1232 loop (pageno+1) pdimno pdim x y rowh' pdims
1234 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1235 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1237 | Csplit (c, _) ->
1238 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1239 let rec loop pageno pdimno pdim y pdims =
1240 if pageno = state.pagecount
1241 then ()
1242 else
1243 let pdimno, ((_, w, h, _) as pdim), pdims =
1244 match pdims with
1245 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1246 pdimno+1, pdim, rest
1247 | _ ->
1248 pdimno, pdim, pdims
1250 let cw = w / c in
1251 let rec loop1 n x y =
1252 if n = c then y else (
1253 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1254 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1257 let y = loop1 0 0 y in
1258 loop (pageno+1) pdimno pdim y pdims
1260 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1261 conf.columns <- Csplit (c, a);
1264 let represent () =
1265 docolumns conf.columns;
1266 state.maxy <- calcheight ();
1267 if state.reprf == noreprf
1268 then (
1269 match state.mode with
1270 | Birdseye (_, _, pageno, _, _) ->
1271 let y, h = getpageyh pageno in
1272 let top = (state.winh - h) / 2 in
1273 gotoxy state.x (max 0 (y - top))
1274 | Textentry _ | View | LinkNav _ ->
1275 let y = getanchory state.anchor in
1276 let y = min y (state.maxy - state.winh) in
1277 gotoxy state.x y;
1279 else (
1280 state.reprf ();
1281 state.reprf <- noreprf;
1285 let reshape ?(firsttime=false) w h =
1286 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1287 if not firsttime && nogeomcmds state.geomcmds
1288 then state.anchor <- getanchor ();
1290 state.winw <- w;
1291 let w = truncate (float w *. conf.zoom) in
1292 let w = max w 2 in
1293 state.winh <- h;
1294 setfontsize fstate.fontsize;
1295 GlMat.mode `modelview;
1296 GlMat.load_identity ();
1298 GlMat.mode `projection;
1299 GlMat.load_identity ();
1300 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1301 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1302 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1304 let relx =
1305 if conf.zoom <= 1.0
1306 then 0.0
1307 else float state.x /. float state.w
1309 invalidate ~redisplay:true "geometry"
1310 (fun () ->
1311 state.w <- w;
1312 if not firsttime
1313 then state.x <- truncate (relx *. float w);
1314 let w =
1315 match conf.columns with
1316 | Csingle _ -> w
1317 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1318 | Csplit (c, _) -> w * c
1320 wcmd "geometry %d %d %d"
1321 w (stateh h) (FMTE.to_int conf.fitmodel)
1325 let enttext () =
1326 let len = String.length state.text in
1327 let x0 = if conf.leftscroll then vscrollw () else 0 in
1328 let drawstring s =
1329 let hscrollh =
1330 match state.mode with
1331 | Textentry _ | View | LinkNav _ ->
1332 let h, _, _ = state.uioh#scrollpw in
1334 | Birdseye _ -> 0
1336 let rect x w =
1337 filledrect
1338 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1339 (x+.w) (float (state.winh - hscrollh))
1342 let w = float (state.winw - 1 - vscrollw ()) in
1343 if state.progress >= 0.0 && state.progress < 1.0
1344 then (
1345 GlDraw.color (0.3, 0.3, 0.3);
1346 let w1 = w *. state.progress in
1347 rect (float x0) w1;
1348 GlDraw.color (0.0, 0.0, 0.0);
1349 rect (float x0+.w1) (float x0+.w-.w1)
1351 else (
1352 GlDraw.color (0.0, 0.0, 0.0);
1353 rect (float x0) w;
1356 GlDraw.color (1.0, 1.0, 1.0);
1357 drawstring
1358 fstate.fontsize
1359 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1360 (state.winh - hscrollh - 5) s;
1362 let s =
1363 match state.mode with
1364 | Textentry ((prefix, text, _, _, _, _), _) ->
1365 let s =
1366 if len > 0
1367 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1368 else Printf.sprintf "%s%s_" prefix text
1372 | Birdseye _ | View | LinkNav _ -> state.text
1374 let s =
1375 if state.newerrmsgs
1376 then (
1377 if not (istextentry state.mode) && state.uioh#eformsgs
1378 then
1379 let s1 = "(press 'e' to review error messasges)" in
1380 if nonemptystr s then s ^ " " ^ s1 else s1
1381 else s
1383 else s
1385 if nonemptystr s
1386 then drawstring s
1389 let gctiles () =
1390 let len = Queue.length state.tilelru in
1391 let layout = lazy (
1392 match state.throttle with
1393 | None ->
1394 if conf.preload
1395 then preloadlayout state.x state.y state.winw state.winh
1396 else state.layout
1397 | Some (layout, _, _) ->
1398 layout
1399 ) in
1400 let rec loop qpos =
1401 if state.memused > conf.memlimit
1402 then (
1403 if qpos < len
1404 then
1405 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1406 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1407 let (_, pw, ph, _) = getpagedim n in
1408 if gen = state.gen
1409 && colorspace = conf.colorspace
1410 && angle = conf.angle
1411 && pagew = pw
1412 && pageh = ph
1413 && (
1414 let x = col*conf.tilew
1415 and y = row*conf.tileh in
1416 tilevisible (Lazy.force_val layout) n x y
1418 then Queue.push lruitem state.tilelru
1419 else (
1420 freepbo p;
1421 wcmd "freetile %s" (~> p);
1422 state.memused <- state.memused - s;
1423 state.uioh#infochanged Memused;
1424 Hashtbl.remove state.tilemap k;
1426 loop (qpos+1)
1429 loop 0
1432 let onpagerect pageno f =
1433 let b =
1434 match conf.columns with
1435 | Cmulti (_, b) -> b
1436 | Csingle b -> b
1437 | Csplit (_, b) -> b
1439 if pageno >= 0 && pageno < Array.length b
1440 then
1441 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1442 f w h
1445 let gotopagexy1 wtmode pageno x y =
1446 let _,w1,h1,leftx = getpagedim pageno in
1447 let top = y /. (float h1) in
1448 let left = x /. (float w1) in
1449 let py, w, h = getpageywh pageno in
1450 let wh = state.winh in
1451 let x = left *. (float w) in
1452 let x = leftx + state.x + truncate x in
1453 let sx =
1454 if x < 0 || x >= state.winw
1455 then state.x - x
1456 else state.x
1458 let pdy = truncate (top *. float h) in
1459 let y' = py + pdy in
1460 let dy = y' - state.y in
1461 let sy =
1462 if x != state.x || not (dy > 0 && dy < wh)
1463 then (
1464 if conf.presentation
1465 then
1466 if abs (py - y') > wh
1467 then y'
1468 else py
1469 else y';
1471 else state.y
1473 if state.x != sx || state.y != sy
1474 then (
1475 let x, y =
1476 if wtmode
1477 then (
1478 let ww = state.winw in
1479 let qx = sx / ww
1480 and qy = pdy / wh in
1481 let x = qx * ww
1482 and y = py + qy * wh in
1483 let x = if -x + ww > w1 then -(w1-ww) else x
1484 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1485 let y =
1486 if conf.presentation
1487 then
1488 if abs (py - y') > wh
1489 then y'
1490 else py
1491 else y';
1493 (x, y)
1495 else (sx, sy)
1497 gotoxy_and_clear_text x y;
1499 else gotoxy_and_clear_text state.x state.y;
1502 let gotopagexy wtmode pageno x y =
1503 match state.mode with
1504 | Birdseye _ -> gotopage pageno 0.0
1505 | Textentry _ | View | LinkNav _ -> gotopagexy1 wtmode pageno x y
1508 let getpassword () =
1509 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1510 if emptystr passcmd
1511 then E.s
1512 else getcmdoutput
1513 (fun s ->
1514 impmsg "error getting password: %s" s;
1515 dolog "%s" s) passcmd;
1518 let pgoto opaque pageno x y =
1519 let pdimno = getpdimno pageno in
1520 let x, y = project opaque pageno pdimno x y in
1521 gotopagexy false pageno x y;
1524 let act cmds =
1525 (* dolog "%S" cmds; *)
1526 let spl = splitatchar cmds ' ' in
1527 let scan s fmt f =
1528 try Scanf.sscanf s fmt f
1529 with exn ->
1530 dolog "error processing '%S': %s" cmds @@ exntos exn;
1531 exit 1
1533 let addoutline outline =
1534 match state.currently with
1535 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1536 | Idle -> state.currently <- Outlining [outline]
1537 | Loading _ | Tiling _ ->
1538 dolog "invalid outlining state";
1539 logcurrently state.currently
1541 match spl with
1542 | "clear", "" ->
1543 state.pdims <- [];
1544 state.uioh#infochanged Pdim;
1546 | "clearrects", "" ->
1547 state.rects <- state.rects1;
1548 G.postRedisplay "clearrects";
1550 | "continue", args ->
1551 let n = scan args "%u" (fun n -> n) in
1552 state.pagecount <- n;
1553 begin match state.currently with
1554 | Outlining l ->
1555 state.currently <- Idle;
1556 state.outlines <- Array.of_list (List.rev l)
1557 | Idle | Loading _ | Tiling _ -> ()
1558 end;
1560 let cur, cmds = state.geomcmds in
1561 if emptystr cur
1562 then failwith "umpossible";
1564 begin match List.rev cmds with
1565 | [] ->
1566 state.geomcmds <- E.s, [];
1567 state.throttle <- None;
1568 represent ();
1569 | (s, f) :: rest ->
1570 f ();
1571 state.geomcmds <- s, List.rev rest;
1572 end;
1573 if conf.maxwait = None && not !wtmode
1574 then G.postRedisplay "continue";
1576 | "msg", args ->
1577 showtext ' ' args
1579 | "vmsg", args ->
1580 if conf.verbose
1581 then showtext ' ' args
1583 | "emsg", args ->
1584 Buffer.add_string state.errmsgs args;
1585 state.newerrmsgs <- true;
1586 G.postRedisplay "error message"
1588 | "progress", args ->
1589 let progress, text =
1590 scan args "%f %n"
1591 (fun f pos ->
1592 f, String.sub args pos (String.length args - pos))
1594 state.text <- text;
1595 state.progress <- progress;
1596 G.postRedisplay "progress"
1598 | "firstmatch", args ->
1599 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1600 scan args "%u %d %f %f %f %f %f %f %f %f"
1601 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1602 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1604 let y = (getpagey pageno) + truncate y0 in
1605 let x =
1606 if conf.zoom > 1.0
1607 then state.winw/2
1608 else state.x
1610 addnav ();
1611 gotoxy x y;
1612 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1613 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1615 | "match", args ->
1616 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1617 scan args "%u %d %f %f %f %f %f %f %f %f"
1618 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1619 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1621 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1622 state.rects1 <-
1623 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1625 | "page", args ->
1626 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1627 let pageopaque = ~< pageopaques in
1628 begin match state.currently with
1629 | Loading (l, gen) ->
1630 vlog "page %d took %f sec" l.pageno t;
1631 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1632 begin match state.throttle with
1633 | None ->
1634 let preloadedpages =
1635 if conf.preload
1636 then preloadlayout state.x state.y state.winw state.winh
1637 else state.layout
1639 let evict () =
1640 let set =
1641 List.fold_left (fun s l -> IntSet.add l.pageno s)
1642 IntSet.empty preloadedpages
1644 let evictedpages =
1645 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1646 if not (IntSet.mem pageno set)
1647 then (
1648 wcmd "freepage %s" (~> opaque);
1649 key :: accu
1651 else accu
1652 ) state.pagemap []
1654 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1656 evict ();
1657 state.currently <- Idle;
1658 if gen = state.gen
1659 then (
1660 tilepage l.pageno pageopaque state.layout;
1661 load state.layout;
1662 load preloadedpages;
1663 let visible = pagevisible state.layout l.pageno in
1664 if visible
1665 then (
1666 match state.mode with
1667 | LinkNav (Ltnotready (pageno, dir)) ->
1668 if pageno = l.pageno
1669 then (
1670 let link =
1671 let ld =
1672 if dir = 0
1673 then LDfirstvisible (l.pagex, l.pagey, dir)
1674 else (
1675 if dir > 0 then LDfirst else LDlast
1678 findlink pageopaque ld
1680 match link with
1681 | Lnotfound -> ()
1682 | Lfound n ->
1683 showlinktype (getlink pageopaque n);
1684 state.mode <- LinkNav (Ltexact (l.pageno, n))
1686 | LinkNav (Ltgendir _)
1687 | LinkNav (Ltexact _)
1688 | View
1689 | Birdseye _
1690 | Textentry _ -> ()
1693 if visible && layoutready state.layout
1694 then (
1695 G.postRedisplay "page";
1699 | Some (layout, _, _) ->
1700 state.currently <- Idle;
1701 tilepage l.pageno pageopaque layout;
1702 load state.layout
1703 end;
1705 | Idle | Tiling _ | Outlining _ ->
1706 dolog "Inconsistent loading state";
1707 logcurrently state.currently;
1708 exit 1
1711 | "tile" , args ->
1712 let (x, y, opaques, size, t) =
1713 scan args "%u %u %s %u %f"
1714 (fun x y p size t -> (x, y, p, size, t))
1716 let opaque = ~< opaques in
1717 begin match state.currently with
1718 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1719 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1721 unmappbo opaque;
1722 if tilew != conf.tilew || tileh != conf.tileh
1723 then (
1724 wcmd "freetile %s" (~> opaque);
1725 state.currently <- Idle;
1726 load state.layout;
1728 else (
1729 puttileopaque l col row gen cs angle opaque size t;
1730 state.memused <- state.memused + size;
1731 state.uioh#infochanged Memused;
1732 gctiles ();
1733 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1734 opaque, size) state.tilelru;
1736 let layout =
1737 match state.throttle with
1738 | None -> state.layout
1739 | Some (layout, _, _) -> layout
1742 state.currently <- Idle;
1743 if gen = state.gen
1744 && conf.colorspace = cs
1745 && conf.angle = angle
1746 && tilevisible layout l.pageno x y
1747 then conttiling l.pageno pageopaque;
1749 begin match state.throttle with
1750 | None ->
1751 preload state.layout;
1752 if gen = state.gen
1753 && conf.colorspace = cs
1754 && conf.angle = angle
1755 && tilevisible state.layout l.pageno x y
1756 && (not !wtmode || layoutready state.layout)
1757 then G.postRedisplay "tile nothrottle";
1759 | Some (layout, y, _) ->
1760 let ready = layoutready layout in
1761 if ready
1762 then (
1763 state.y <- y;
1764 state.layout <- layout;
1765 state.throttle <- None;
1766 G.postRedisplay "throttle";
1768 else load layout;
1769 end;
1772 | Idle | Loading _ | Outlining _ ->
1773 dolog "Inconsistent tiling state";
1774 logcurrently state.currently;
1775 exit 1
1778 | "pdim", args ->
1779 let (n, w, h, _) as pdim =
1780 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1782 let pdim =
1783 match conf.fitmodel with
1784 | FitWidth -> pdim
1785 | FitPage | FitProportional ->
1786 match conf.columns with
1787 | Csplit _ -> (n, w, h, 0)
1788 | Csingle _ | Cmulti _ -> pdim
1790 state.pdims <- pdim :: state.pdims;
1791 state.uioh#infochanged Pdim
1793 | "o", args ->
1794 let (l, n, t, h, pos) =
1795 scan args "%u %u %d %u %n"
1796 (fun l n t h pos -> l, n, t, h, pos)
1798 let s = String.sub args pos (String.length args - pos) in
1799 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1801 | "ou", args ->
1802 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1803 let s = String.sub args pos len in
1804 let pos2 = pos + len + 1 in
1805 let uri = String.sub args pos2 (String.length args - pos2) in
1806 addoutline (s, l, Ouri uri)
1808 | "on", args ->
1809 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1810 let s = String.sub args pos (String.length args - pos) in
1811 addoutline (s, l, Onone)
1813 | "a", args ->
1814 let (n, l, t) =
1815 scan args "%u %d %d" (fun n l t -> n, l, t)
1817 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1819 | "info", args ->
1820 let c, v = splitatchar args '\t' in
1821 let s =
1822 if nonemptystr v
1823 then
1824 if c = "Title"
1825 then (
1826 conf.title <- v;
1827 if not !ignoredoctitlte
1828 then Wsi.settitle v;
1829 args
1831 else
1832 if let len = String.length c in
1833 len > 6 && ((String.sub c (len-4) 4) = "date")
1834 then (
1835 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1836 then
1837 let b = Buffer.create 10 in
1838 Printf.bprintf b "%s\t" c;
1839 let sub p l c =
1841 Buffer.add_substring b v p l;
1842 Buffer.add_char b c;
1843 with exn -> Buffer.add_string b @@ exntos exn
1845 sub 2 4 '/';
1846 sub 6 2 '/';
1847 sub 8 2 ' ';
1848 sub 10 2 ':';
1849 sub 12 2 ':';
1850 sub 14 2 ' ';
1851 Buffer.add_char b '[';
1852 Buffer.add_string b v;
1853 Buffer.add_char b ']';
1854 Buffer.contents b
1855 else args
1857 else args
1858 else args
1860 state.docinfo <- (1, s) :: state.docinfo
1862 | "infoend", "" ->
1863 state.docinfo <- List.rev state.docinfo;
1864 state.uioh#infochanged Docinfo
1866 | "pass", args ->
1867 if args = "fail"
1868 then Wsi.settitle "Wrong password";
1869 let password = getpassword () in
1870 if emptystr password
1871 then error "document is password protected"
1872 else opendoc state.path password
1874 | _ ->
1875 error "unknown cmd `%S'" cmds
1878 let onhist cb =
1879 let rc = cb.rc in
1880 let action = function
1881 | HCprev -> cbget cb ~-1
1882 | HCnext -> cbget cb 1
1883 | HCfirst -> cbget cb ~-(cb.rc)
1884 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1885 and cancel () = cb.rc <- rc
1886 in (action, cancel)
1889 let search pattern forward =
1890 match conf.columns with
1891 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1892 | Csingle _ | Cmulti _ ->
1893 if nonemptystr pattern
1894 then
1895 let pn, py =
1896 match state.layout with
1897 | [] -> 0, 0
1898 | l :: _ ->
1899 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1901 wcmd "search %d %d %d %d,%s\000"
1902 (btod conf.icase) pn py (btod forward) pattern;
1905 let [@warning "-4"] intentry text key =
1906 let text =
1907 if emptystr text && key = Keys.Ascii '-'
1908 then addchar text '-'
1909 else
1910 match key with
1911 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1912 | _ ->
1913 state.text <- "invalid key";
1914 text
1916 TEcont text
1919 let linknact f s =
1920 if nonemptystr s
1921 then (
1922 let n =
1923 let l = String.length s in
1924 let rec loop pos n =
1925 if pos = l
1926 then n
1927 else
1928 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1929 loop (pos+1) (n*26 + m)
1930 in loop 0 0
1932 let rec loop n = function
1933 | [] -> ()
1934 | l :: rest ->
1935 match getopaque l.pageno with
1936 | None -> loop n rest
1937 | Some opaque ->
1938 let m = getlinkcount opaque in
1939 if n < m
1940 then (
1941 let under = getlink opaque n in
1942 f under
1944 else loop (n-m) rest
1946 loop n state.layout;
1950 let [@warning "-4"] linknentry text = function
1951 | Keys.Ascii c ->
1952 let text = addchar text c in
1953 linknact (fun under -> state.text <- undertext under) text;
1954 TEcont text
1955 | _ ->
1956 state.text <- Printf.sprintf "invalid key";
1957 TEcont text
1960 let [@warning "-4"] textentry text = function
1961 | Keys.Ascii c -> TEcont (addchar text c)
1962 | Keys.Code c -> TEcont (text ^ toutf8 c)
1963 | _ -> TEcont text
1966 let reqlayout angle fitmodel =
1967 match state.throttle with
1968 | None ->
1969 if nogeomcmds state.geomcmds
1970 then state.anchor <- getanchor ();
1971 conf.angle <- angle mod 360;
1972 if conf.angle != 0
1973 then (
1974 match state.mode with
1975 | LinkNav _ -> state.mode <- View
1976 | Birdseye _ | Textentry _ | View -> ()
1978 conf.fitmodel <- fitmodel;
1979 invalidate
1980 "reqlayout"
1981 (fun () ->
1982 wcmd "reqlayout %d %d %d"
1983 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1985 | _ -> ()
1988 let settrim trimmargins trimfuzz =
1989 if nogeomcmds state.geomcmds
1990 then state.anchor <- getanchor ();
1991 conf.trimmargins <- trimmargins;
1992 conf.trimfuzz <- trimfuzz;
1993 let x0, y0, x1, y1 = trimfuzz in
1994 invalidate
1995 "settrim" (fun () ->
1996 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1997 flushpages ();
2000 let setzoom zoom =
2001 match state.throttle with
2002 | None ->
2003 let zoom = max 0.0001 zoom in
2004 if zoom <> conf.zoom
2005 then (
2006 state.prevzoom <- (conf.zoom, state.x);
2007 conf.zoom <- zoom;
2008 reshape state.winw state.winh;
2009 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2012 | Some (layout, y, started) ->
2013 let time =
2014 match conf.maxwait with
2015 | None -> 0.0
2016 | Some t -> t
2018 let dt = now () -. started in
2019 if dt > time
2020 then (
2021 state.y <- y;
2022 load layout;
2026 let pivotzoom ?(vw=min state.w state.winw)
2027 ?(vh=min (state.maxy-state.y) state.winh)
2028 ?(x=vw/2) ?(y=vh/2) zoom =
2029 let w = float state.w /. zoom in
2030 let hw = w /. 2.0 in
2031 let ratio = float vh /. float vw in
2032 let hh = hw *. ratio in
2033 let x0 = float x -. hw
2034 and y0 = float y -. hh in
2035 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2036 setzoom zoom;
2039 let pivotzoom ?vw ?vh ?x ?y zoom =
2040 if nogeomcmds state.geomcmds
2041 then
2042 if zoom > 1.0
2043 then pivotzoom ?vw ?vh ?x ?y zoom
2044 else setzoom zoom
2047 let setcolumns mode columns coverA coverB =
2048 state.prevcolumns <- Some (conf.columns, conf.zoom);
2049 if columns < 0
2050 then (
2051 if isbirdseye mode
2052 then impmsg "split mode doesn't work in bird's eye"
2053 else (
2054 conf.columns <- Csplit (-columns, E.a);
2055 state.x <- 0;
2056 conf.zoom <- 1.0;
2059 else (
2060 if columns < 2
2061 then (
2062 conf.columns <- Csingle E.a;
2063 state.x <- 0;
2064 setzoom 1.0;
2066 else (
2067 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2068 conf.zoom <- 1.0;
2071 reshape state.winw state.winh;
2074 let resetmstate () =
2075 state.mstate <- Mnone;
2076 Wsi.setcursor Wsi.CURSOR_INHERIT;
2079 let enterbirdseye () =
2080 let zoom = float conf.thumbw /. float state.winw in
2081 let birdseyepageno =
2082 let cy = state.winh / 2 in
2083 let fold = function
2084 | [] -> 0
2085 | l :: rest ->
2086 let rec fold best = function
2087 | [] -> best.pageno
2088 | l :: rest ->
2089 let d = cy - (l.pagedispy + l.pagevh/2)
2090 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2091 if abs d < abs dbest
2092 then fold l rest
2093 else best.pageno
2094 in fold l rest
2096 fold state.layout
2098 state.mode <-
2099 Birdseye (
2100 { conf with zoom = conf.zoom },
2101 state.x, birdseyepageno, -1, getanchor ()
2103 resetmstate ();
2104 conf.zoom <- zoom;
2105 conf.presentation <- false;
2106 conf.interpagespace <- 10;
2107 conf.hlinks <- false;
2108 conf.fitmodel <- FitPage;
2109 state.x <- 0;
2110 conf.maxwait <- None;
2111 conf.columns <- (
2112 match conf.beyecolumns with
2113 | Some c ->
2114 conf.zoom <- 1.0;
2115 Cmulti ((c, 0, 0), E.a)
2116 | None -> Csingle E.a
2118 if conf.verbose
2119 then
2120 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2121 (100.0*.zoom)
2122 else
2123 state.text <- E.s
2125 reshape state.winw state.winh;
2128 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2129 state.mode <- View;
2130 conf.zoom <- c.zoom;
2131 conf.presentation <- c.presentation;
2132 conf.interpagespace <- c.interpagespace;
2133 conf.maxwait <- c.maxwait;
2134 conf.hlinks <- c.hlinks;
2135 conf.fitmodel <- c.fitmodel;
2136 conf.beyecolumns <- (
2137 match conf.columns with
2138 | Cmulti ((c, _, _), _) -> Some c
2139 | Csingle _ -> None
2140 | Csplit _ -> failwith "leaving bird's eye split mode"
2142 conf.columns <- (
2143 match c.columns with
2144 | Cmulti (c, _) -> Cmulti (c, E.a)
2145 | Csingle _ -> Csingle E.a
2146 | Csplit (c, _) -> Csplit (c, E.a)
2148 if conf.verbose
2149 then
2150 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2151 (100.0*.conf.zoom)
2153 reshape state.winw state.winh;
2154 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2155 state.x <- leftx;
2158 let togglebirdseye () =
2159 match state.mode with
2160 | Birdseye vals -> leavebirdseye vals true
2161 | View -> enterbirdseye ()
2162 | Textentry _ | LinkNav _ -> ()
2165 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2166 let pageno = max 0 (pageno - incr) in
2167 let rec loop = function
2168 | [] -> gotopage1 pageno 0
2169 | l :: _ when l.pageno = pageno ->
2170 if l.pagedispy >= 0 && l.pagey = 0
2171 then G.postRedisplay "upbirdseye"
2172 else gotopage1 pageno 0
2173 | _ :: rest -> loop rest
2175 loop state.layout;
2176 state.text <- E.s;
2177 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2180 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2181 let pageno = min (state.pagecount - 1) (pageno + incr) in
2182 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2183 let rec loop = function
2184 | [] ->
2185 let y, h = getpageyh pageno in
2186 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2187 gotoxy state.x (clamp dy)
2188 | l :: _ when l.pageno = pageno ->
2189 if l.pagevh != l.pageh
2190 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2191 else G.postRedisplay "downbirdseye"
2192 | _ :: rest -> loop rest
2194 loop state.layout;
2195 state.text <- E.s;
2198 let [@warning "-4"] optentry mode _ key =
2199 let btos b = if b then "on" else "off" in
2200 match key with
2201 | Keys.Ascii 's' ->
2202 let ondone s =
2203 try conf.scrollstep <- int_of_string s with exn ->
2204 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2206 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2208 | Keys.Ascii 'A' ->
2209 let ondone s =
2211 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2212 if state.autoscroll <> None
2213 then state.autoscroll <- Some conf.autoscrollstep
2214 with exn ->
2215 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2217 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2219 | Keys.Ascii 'C' ->
2220 let ondone s =
2222 let n, a, b = multicolumns_of_string s in
2223 setcolumns mode n a b;
2224 with exn ->
2225 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2227 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2229 | Keys.Ascii 'Z' ->
2230 let ondone s =
2232 let zoom = float (int_of_string s) /. 100.0 in
2233 pivotzoom zoom
2234 with exn ->
2235 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2237 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2239 | Keys.Ascii 't' ->
2240 let ondone s =
2242 conf.thumbw <- bound (int_of_string s) 2 4096;
2243 state.text <-
2244 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2245 begin match mode with
2246 | Birdseye beye ->
2247 leavebirdseye beye false;
2248 enterbirdseye ();
2249 | Textentry _ | View | LinkNav _ -> ();
2251 with exn ->
2252 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2254 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2256 | Keys.Ascii 'R' ->
2257 let ondone s =
2258 match int_of_string s with
2259 | angle -> reqlayout angle conf.fitmodel
2260 | exception exn ->
2261 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2263 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2265 | Keys.Ascii 'i' ->
2266 conf.icase <- not conf.icase;
2267 TEdone ("case insensitive search " ^ (btos conf.icase))
2269 | Keys.Ascii 'p' ->
2270 conf.preload <- not conf.preload;
2271 gotoxy state.x state.y;
2272 TEdone ("preload " ^ (btos conf.preload))
2274 | Keys.Ascii 'v' ->
2275 conf.verbose <- not conf.verbose;
2276 TEdone ("verbose " ^ (btos conf.verbose))
2278 | Keys.Ascii 'd' ->
2279 conf.debug <- not conf.debug;
2280 TEdone ("debug " ^ (btos conf.debug))
2282 | Keys.Ascii 'h' ->
2283 conf.maxhfit <- not conf.maxhfit;
2284 state.maxy <- calcheight ();
2285 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2287 | Keys.Ascii 'c' ->
2288 conf.crophack <- not conf.crophack;
2289 TEdone ("crophack " ^ btos conf.crophack)
2291 | Keys.Ascii 'a' ->
2292 let s =
2293 match conf.maxwait with
2294 | None ->
2295 conf.maxwait <- Some infinity;
2296 "always wait for page to complete"
2297 | Some _ ->
2298 conf.maxwait <- None;
2299 "show placeholder if page is not ready"
2301 TEdone s
2303 | Keys.Ascii 'f' ->
2304 conf.underinfo <- not conf.underinfo;
2305 TEdone ("underinfo " ^ btos conf.underinfo)
2307 | Keys.Ascii 'P' ->
2308 conf.savebmarks <- not conf.savebmarks;
2309 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2311 | Keys.Ascii 'S' ->
2312 let ondone s =
2314 let pageno, py =
2315 match state.layout with
2316 | [] -> 0, 0
2317 | l :: _ ->
2318 l.pageno, l.pagey
2320 conf.interpagespace <- int_of_string s;
2321 docolumns conf.columns;
2322 state.maxy <- calcheight ();
2323 let y = getpagey pageno in
2324 gotoxy state.x (y + py)
2325 with exn ->
2326 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2328 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2330 | Keys.Ascii 'l' ->
2331 let fm =
2332 match conf.fitmodel with
2333 | FitProportional -> FitWidth
2334 | FitWidth | FitPage -> FitProportional
2336 reqlayout conf.angle fm;
2337 TEdone ("proportional display " ^ btos (fm == FitProportional))
2339 | Keys.Ascii 'T' ->
2340 settrim (not conf.trimmargins) conf.trimfuzz;
2341 TEdone ("trim margins " ^ btos conf.trimmargins)
2343 | Keys.Ascii 'I' ->
2344 conf.invert <- not conf.invert;
2345 TEdone ("invert colors " ^ btos conf.invert)
2347 | Keys.Ascii 'x' ->
2348 let ondone s =
2349 cbput state.hists.sel s;
2350 conf.selcmd <- s;
2352 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2353 textentry, ondone, true)
2355 | Keys.Ascii 'M' ->
2356 if conf.pax == None
2357 then conf.pax <- Some 0.0
2358 else conf.pax <- None;
2359 TEdone ("PAX " ^ btos (conf.pax != None))
2361 | (Keys.Ascii c) ->
2362 state.text <- Printf.sprintf "bad option %d `%c'"
2363 (Char.code c) c;
2364 TEstop
2366 | _ ->
2367 TEcont state.text
2370 class type lvsource =
2371 object
2372 method getitemcount : int
2373 method getitem : int -> (string * int)
2374 method hasaction : int -> bool
2375 method exit : uioh:uioh ->
2376 cancel:bool ->
2377 active:int ->
2378 first:int ->
2379 pan:int ->
2380 uioh option
2381 method getactive : int
2382 method getfirst : int
2383 method getpan : int
2384 method getminfo : (int * int) array
2385 end;;
2387 class virtual lvsourcebase = object
2388 val mutable m_active = 0
2389 val mutable m_first = 0
2390 val mutable m_pan = 0
2391 method getactive = m_active
2392 method getfirst = m_first
2393 method getpan = m_pan
2394 method getminfo : (int * int) array = E.a
2395 end;;
2397 let [@warning "-4"]
2398 textentrykeyboard
2399 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2400 state.text <- E.s;
2401 let enttext te =
2402 state.mode <- Textentry (te, onleave);
2403 enttext ();
2404 G.postRedisplay "textentrykeyboard enttext";
2406 let histaction cmd =
2407 match opthist with
2408 | None -> ()
2409 | Some (action, _) ->
2410 state.mode <-
2411 Textentry (
2412 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2414 G.postRedisplay "textentry histaction"
2416 let open Keys in
2417 let kt = Wsi.kc2kt key in
2418 match kt with
2419 | Backspace ->
2420 if emptystr text && cancelonempty
2421 then (
2422 onleave Cancel;
2423 G.postRedisplay "textentrykeyboard after cancel";
2425 else
2426 let s = withoutlastutf8 text in
2427 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2429 | Enter ->
2430 ondone text;
2431 onleave Confirm;
2432 G.postRedisplay "textentrykeyboard after confirm"
2434 | Up -> histaction HCprev
2435 | Down -> histaction HCnext
2436 | Home -> histaction HCfirst
2437 | End -> histaction HClast
2439 | Escape ->
2440 if emptystr text
2441 then (
2442 begin match opthist with
2443 | None -> ()
2444 | Some (_, onhistcancel) -> onhistcancel ()
2445 end;
2446 onleave Cancel;
2447 state.text <- E.s;
2448 G.postRedisplay "textentrykeyboard after cancel2"
2450 else (
2451 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2454 | Delete -> ()
2456 | Code _ | Ascii _ ->
2457 begin match onkey text kt with
2458 | TEdone text ->
2459 ondone text;
2460 onleave Confirm;
2461 G.postRedisplay "textentrykeyboard after confirm2";
2463 | TEcont text ->
2464 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2466 | TEstop ->
2467 onleave Cancel;
2468 G.postRedisplay "textentrykeyboard after cancel3"
2470 | TEswitch te ->
2471 state.mode <- Textentry (te, onleave);
2472 G.postRedisplay "textentrykeyboard switch";
2474 | _ -> vlog "unhandled key"
2477 let firstof first active =
2478 if first > active || abs (first - active) > fstate.maxrows - 1
2479 then max 0 (active - (fstate.maxrows/2))
2480 else first
2483 let calcfirst first active =
2484 if active > first
2485 then
2486 let rows = active - first in
2487 if rows > fstate.maxrows then active - fstate.maxrows else first
2488 else active
2491 let scrollph y maxy =
2492 let sh = float (maxy + state.winh) /. float state.winh in
2493 let sh = float state.winh /. sh in
2494 let sh = max sh (float conf.scrollh) in
2496 let percent = float y /. float maxy in
2497 let position = (float state.winh -. sh) *. percent in
2499 let position =
2500 if position +. sh > float state.winh
2501 then float state.winh -. sh
2502 else position
2504 position, sh;
2507 let adderrmsg src msg =
2508 Buffer.add_string state.errmsgs msg;
2509 state.newerrmsgs <- true;
2510 G.postRedisplay src
2513 let adderrfmt src fmt =
2514 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2517 let coe s = (s :> uioh);;
2519 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2520 object (self)
2521 val m_pan = source#getpan
2522 val m_first = source#getfirst
2523 val m_active = source#getactive
2524 val m_qsearch = E.s
2525 val m_prev_uioh = state.uioh
2527 method private elemunder y =
2528 if y < 0
2529 then None
2530 else
2531 let n = y / (fstate.fontsize+1) in
2532 if m_first + n < source#getitemcount
2533 then (
2534 if source#hasaction (m_first + n)
2535 then Some (m_first + n)
2536 else None
2538 else None
2540 method display =
2541 Gl.enable `blend;
2542 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2543 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2544 filledrect 0. 0. (float state.winw) (float state.winh);
2545 GlDraw.color (1., 1., 1.);
2546 Gl.enable `texture_2d;
2547 let fs = fstate.fontsize in
2548 let nfs = fs + 1 in
2549 let hw = state.winw/3 in
2550 let ww = fstate.wwidth in
2551 let tabw = 17.0*.ww in
2552 let itemcount = source#getitemcount in
2553 let minfo = source#getminfo in
2554 if conf.leftscroll
2555 then (
2556 GlMat.push ();
2557 GlMat.translate ~x:(float conf.scrollbw) ();
2559 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2560 let rec loop row =
2561 if (row - m_first) > fstate.maxrows
2562 then ()
2563 else (
2564 if row >= 0 && row < itemcount
2565 then (
2566 let (s, level) = source#getitem row in
2567 let y = (row - m_first) * nfs in
2568 let x = 5.0 +. (float (level + m_pan)) *. ww in
2569 if helpmode
2570 then GlDraw.color
2571 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2573 if row = m_active
2574 then (
2575 Gl.disable `texture_2d;
2576 let alpha = if source#hasaction row then 0.9 else 0.3 in
2577 GlDraw.color (1., 1., 1.) ~alpha;
2578 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2579 Gl.enable `texture_2d;
2581 let c =
2582 if zebra && row land 1 = 1
2583 then 0.8
2584 else 1.0
2586 GlDraw.color (c,c,c);
2587 let drawtabularstring s =
2588 let drawstr x s =
2589 let x' = truncate (x0 +. x) in
2590 let s1, s2 = splitatchar s '\000' in
2591 if emptystr s2
2592 then drawstring1 fs x' (y+nfs) s
2593 else
2594 let rec e s =
2595 if emptystr s
2596 then s
2597 else
2598 let s' = withoutlastutf8 s in
2599 let s = s' ^ UniSyms.ellipsis in
2600 let w = measurestr fs s in
2601 if float x' +. w +. ww < float (hw + x')
2602 then s
2603 else e s'
2605 let s1 =
2606 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2607 then e s1
2608 else s1
2610 ignore (drawstring1 fs x' (y+nfs) s1);
2611 drawstring1 fs (hw + x') (y+nfs) s2
2613 if trusted
2614 then
2615 let x = if helpmode && row > 0 then x +. ww else x in
2616 let s1, s2 = splitatchar s '\t' in
2617 if nonemptystr s2
2618 then
2619 let nx = drawstr x s1 in
2620 let sw = nx -. x in
2621 let x = x +. (max tabw sw) in
2622 drawstr x s2
2623 else
2624 let len = String.length s - 2 in
2625 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2626 then
2627 let s = String.sub s 2 len in
2628 let x = if not helpmode then x +. ww else x in
2629 GlDraw.color (1.2, 1.2, 1.2);
2630 let vinc = drawstring1 (fs+fs/4)
2631 (truncate (x -. ww)) (y+nfs) s in
2632 GlDraw.color (1., 1., 1.);
2633 vinc +. (float fs *. 0.8)
2634 else
2635 drawstr x s
2636 else
2637 drawstr x s
2639 ignore (drawtabularstring s);
2640 loop (row+1)
2644 loop m_first;
2645 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2646 let xadj = 5.0 in
2647 let rec loop row =
2648 if (row - m_first) <= fstate.maxrows
2649 then
2650 if row >= 0 && row < itemcount
2651 then (
2652 let (s, level) = source#getitem row in
2653 let pos0 = nindex s '\000' in
2654 let y = (row - m_first) * nfs in
2655 let x = float (level + m_pan) *. ww in
2656 let (first, last) = minfo.(row) in
2657 let prefix =
2658 if pos0 > 0 && first > pos0
2659 then String.sub s (pos0+1) (first-pos0-1)
2660 else String.sub s 0 first
2662 let suffix = String.sub s first (last - first) in
2663 let w1 = measurestr fstate.fontsize prefix in
2664 let w2 = measurestr fstate.fontsize suffix in
2665 let x = x +. if conf.leftscroll then xadj else 5.0 in
2666 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2667 let x0 = x +. w1
2668 and y0 = float (y+2) in
2669 let x1 = x0 +. w2
2670 and y1 = float (y+fs+3) in
2671 filledrect x0 y0 x1 y1;
2672 loop (row+1)
2675 Gl.disable `texture_2d;
2676 if Array.length minfo > 0 then loop m_first;
2677 Gl.disable `blend;
2678 if conf.leftscroll
2679 then GlMat.pop ()
2681 method updownlevel incr =
2682 let len = source#getitemcount in
2683 let curlevel =
2684 if m_active >= 0 && m_active < len
2685 then snd (source#getitem m_active)
2686 else -1
2688 let rec flow i =
2689 if i = len then i-1 else if i = -1 then 0 else
2690 let _, l = source#getitem i in
2691 if l != curlevel then i else flow (i+incr)
2693 let active = flow m_active in
2694 let first = calcfirst m_first active in
2695 G.postRedisplay "outline updownlevel";
2696 {< m_active = active; m_first = first >}
2698 method private key1 key mask =
2699 let set1 active first qsearch =
2700 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2702 let search active pattern incr =
2703 let active = if active = -1 then m_first else active in
2704 let dosearch re =
2705 let rec loop n =
2706 if n >= 0 && n < source#getitemcount
2707 then (
2708 let s, _ = source#getitem n in
2709 match Str.search_forward re s 0 with
2710 | exception Not_found -> loop (n + incr)
2711 | _ -> Some n
2713 else None
2715 loop active
2717 let qpat = Str.quote pattern in
2718 match Str.regexp_case_fold qpat with
2719 | s -> dosearch s
2720 | exception exn ->
2721 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2722 qpat @@ Printexc.to_string exn;
2723 None
2725 let itemcount = source#getitemcount in
2726 let find start incr =
2727 let rec find i =
2728 if i = -1 || i = itemcount
2729 then -1
2730 else (
2731 if source#hasaction i
2732 then i
2733 else find (i + incr)
2736 find start
2738 let set active first =
2739 let first = bound first 0 (itemcount - fstate.maxrows) in
2740 state.text <- E.s;
2741 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2743 let navigate incr =
2744 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2745 let active, first =
2746 let incr1 = if incr > 0 then 1 else -1 in
2747 if isvisible m_first m_active
2748 then
2749 let next =
2750 let next = m_active + incr in
2751 let next =
2752 if next < 0 || next >= itemcount
2753 then -1
2754 else find next incr1
2756 if abs (m_active - next) > fstate.maxrows
2757 then -1
2758 else next
2760 if next = -1
2761 then
2762 let first = m_first + incr in
2763 let first = bound first 0 (itemcount - fstate.maxrows) in
2764 let next =
2765 let next = m_active + incr in
2766 let next = bound next 0 (itemcount - 1) in
2767 find next ~-incr1
2769 let active =
2770 if next = -1
2771 then m_active
2772 else (
2773 if isvisible first next
2774 then next
2775 else m_active
2778 active, first
2779 else
2780 let first = min next m_first in
2781 let first =
2782 if abs (next - first) > fstate.maxrows
2783 then first + incr
2784 else first
2786 next, first
2787 else
2788 let first = m_first + incr in
2789 let first = bound first 0 (itemcount - 1) in
2790 let active =
2791 let next = m_active + incr in
2792 let next = bound next 0 (itemcount - 1) in
2793 let next = find next incr1 in
2794 let active =
2795 if next = -1 || abs (m_active - first) > fstate.maxrows
2796 then (
2797 let active = if m_active = -1 then next else m_active in
2798 active
2800 else next
2802 if isvisible first active
2803 then active
2804 else -1
2806 active, first
2808 G.postRedisplay "listview navigate";
2809 set active first;
2811 let open Keys in
2812 let kt = Wsi.kc2kt key in
2813 match [@warning "-4"] kt with
2814 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2815 let incr = if c = 'r' then -1 else 1 in
2816 let active, first =
2817 match search (m_active + incr) m_qsearch incr with
2818 | None ->
2819 state.text <- m_qsearch ^ " [not found]";
2820 m_active, m_first
2821 | Some active ->
2822 state.text <- m_qsearch;
2823 active, firstof m_first active
2825 G.postRedisplay "listview ctrl-r/s";
2826 set1 active first m_qsearch;
2828 | Insert when Wsi.withctrl mask ->
2829 if m_active >= 0 && m_active < source#getitemcount
2830 then (
2831 let s, _ = source#getitem m_active in
2832 selstring s;
2834 coe self
2836 | Backspace ->
2837 if emptystr m_qsearch
2838 then coe self
2839 else (
2840 let qsearch = withoutlastutf8 m_qsearch in
2841 if emptystr qsearch
2842 then (
2843 state.text <- E.s;
2844 G.postRedisplay "listview empty qsearch";
2845 set1 m_active m_first E.s;
2847 else
2848 let active, first =
2849 match search m_active qsearch ~-1 with
2850 | None ->
2851 state.text <- qsearch ^ " [not found]";
2852 m_active, m_first
2853 | Some active ->
2854 state.text <- qsearch;
2855 active, firstof m_first active
2857 G.postRedisplay "listview backspace qsearch";
2858 set1 active first qsearch
2861 | Ascii _ | Code _ ->
2862 let utf8 =
2863 match [@warning "-8"] kt with
2864 | Ascii c -> String.make 1 c
2865 | Code code -> toutf8 code
2867 let pattern = m_qsearch ^ utf8 in
2868 let active, first =
2869 match search m_active pattern 1 with
2870 | None ->
2871 state.text <- pattern ^ " [not found]";
2872 m_active, m_first
2873 | Some active ->
2874 state.text <- pattern;
2875 active, firstof m_first active
2877 G.postRedisplay "listview qsearch add";
2878 set1 active first pattern;
2880 | Escape ->
2881 state.text <- E.s;
2882 if emptystr m_qsearch
2883 then (
2884 G.postRedisplay "list view escape";
2885 let mx, my = state.mpos in
2886 updateunder mx my;
2887 match source#exit ~uioh:(coe self) ~cancel:true ~active:m_active
2888 ~first:m_first ~pan:m_pan with
2889 | None -> m_prev_uioh
2890 | Some uioh -> uioh
2892 else (
2893 G.postRedisplay "list view kill qsearch";
2894 coe {< m_qsearch = E.s >}
2897 | Enter ->
2898 state.text <- E.s;
2899 let self = {< m_qsearch = E.s >} in
2900 let opt =
2901 G.postRedisplay "listview enter";
2902 let cancel = not (m_active >= 0 && m_active < source#getitemcount) in
2903 source#exit ~uioh:(coe self) ~cancel
2904 ~active:m_active ~first:m_first ~pan:m_pan;
2906 begin match opt with
2907 | None -> m_prev_uioh
2908 | Some uioh -> uioh
2911 | Delete ->
2912 coe self
2914 | Up -> navigate ~-1
2915 | Down -> navigate 1
2916 | Prior -> navigate ~-(fstate.maxrows)
2917 | Next -> navigate fstate.maxrows
2919 | Right ->
2920 state.text <- E.s;
2921 G.postRedisplay "listview right";
2922 coe {< m_pan = m_pan - 1 >}
2924 | Left ->
2925 state.text <- E.s;
2926 G.postRedisplay "listview left";
2927 coe {< m_pan = m_pan + 1 >}
2929 | Home ->
2930 let active = find 0 1 in
2931 G.postRedisplay "listview home";
2932 set active 0;
2934 | End ->
2935 let first = max 0 (itemcount - fstate.maxrows) in
2936 let active = find (itemcount - 1) ~-1 in
2937 G.postRedisplay "listview end";
2938 set active first;
2940 | _ -> coe self
2942 method key key mask =
2943 match state.mode with
2944 | Textentry te ->
2945 textentrykeyboard key mask te;
2946 coe self
2947 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2949 method button button down x y _ =
2950 let opt =
2951 match button with
2952 | 1 when vscrollhit x ->
2953 G.postRedisplay "listview scroll";
2954 if down
2955 then
2956 let _, position, sh = self#scrollph in
2957 if y > truncate position && y < truncate (position +. sh)
2958 then (
2959 state.mstate <- Mscrolly;
2960 Some (coe self)
2962 else
2963 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2964 let first = truncate (s *. float source#getitemcount) in
2965 let first = min source#getitemcount first in
2966 Some (coe {< m_first = first; m_active = first >})
2967 else (
2968 state.mstate <- Mnone;
2969 Some (coe self);
2971 | 1 when down ->
2972 begin match self#elemunder y with
2973 | Some n ->
2974 G.postRedisplay "listview click";
2975 source#exit ~uioh:(coe {< m_active = n >})
2976 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
2977 | _ ->
2978 Some (coe self)
2980 | n when (n == 4 || n == 5) && not down ->
2981 let len = source#getitemcount in
2982 let first =
2983 if n = 5 && m_first + fstate.maxrows >= len
2984 then
2985 m_first
2986 else
2987 let first = m_first + (if n == 4 then -1 else 1) in
2988 bound first 0 (len - 1)
2990 G.postRedisplay "listview wheel";
2991 Some (coe {< m_first = first >})
2992 | n when (n = 6 || n = 7) && not down ->
2993 let inc = if n = 7 then -1 else 1 in
2994 G.postRedisplay "listview hwheel";
2995 Some (coe {< m_pan = m_pan + inc >})
2996 | _ ->
2997 Some (coe self)
2999 match opt with
3000 | None -> m_prev_uioh
3001 | Some uioh -> uioh
3003 method multiclick _ x y = self#button 1 true x y
3005 method motion _ y =
3006 match state.mstate with
3007 | Mscrolly ->
3008 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3009 let first = truncate (s *. float source#getitemcount) in
3010 let first = min source#getitemcount first in
3011 G.postRedisplay "listview motion";
3012 coe {< m_first = first; m_active = first >}
3013 | Msel _
3014 | Mpan _
3015 | Mscrollx
3016 | Mzoom _
3017 | Mzoomrect _
3018 | Mnone -> coe self
3020 method pmotion x y =
3021 if x < state.winw - conf.scrollbw
3022 then
3023 let n =
3024 match self#elemunder y with
3025 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3026 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3028 let o =
3029 if n != m_active
3030 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3031 else self
3033 coe o
3034 else (
3035 Wsi.setcursor Wsi.CURSOR_INHERIT;
3036 coe self
3039 method infochanged _ = ()
3041 method scrollpw = (0, 0.0, 0.0)
3042 method scrollph =
3043 let nfs = fstate.fontsize + 1 in
3044 let y = m_first * nfs in
3045 let itemcount = source#getitemcount in
3046 let maxi = max 0 (itemcount - fstate.maxrows) in
3047 let maxy = maxi * nfs in
3048 let p, h = scrollph y maxy in
3049 conf.scrollbw, p, h
3051 method modehash = modehash
3052 method eformsgs = false
3053 method alwaysscrolly = true
3054 method scroll _ dy =
3055 let self =
3056 if dy != 0 then begin
3057 let len = source#getitemcount in
3058 let first =
3059 if dy > 0 && m_first + fstate.maxrows >= len
3060 then
3061 m_first
3062 else
3063 let first = m_first + dy / 10 in
3064 bound first 0 (len - 1)
3066 G.postRedisplay "listview wheel";
3067 {< m_first = first >}
3068 end else
3069 self
3071 coe self
3073 method zoom _ _ _ = ()
3074 end;;
3076 class outlinelistview ~zebra ~source =
3077 let settext autonarrow s =
3078 if autonarrow
3079 then
3080 let ss = source#statestr in
3081 state.text <-
3082 if emptystr ss
3083 then "[" ^ s ^ "]"
3084 else "{" ^ ss ^ "} [" ^ s ^ "]"
3085 else state.text <- s
3087 object (self)
3088 inherit listview
3089 ~zebra
3090 ~helpmode:false
3091 ~source:(source :> lvsource)
3092 ~trusted:false
3093 ~modehash:(findkeyhash conf "outline")
3094 as super
3096 val m_autonarrow = false
3098 method! key key mask =
3099 let maxrows =
3100 if emptystr state.text
3101 then fstate.maxrows
3102 else fstate.maxrows - 2
3104 let calcfirst first active =
3105 if active > first
3106 then
3107 let rows = active - first in
3108 if rows > maxrows then active - maxrows else first
3109 else active
3111 let navigate incr =
3112 let active = m_active + incr in
3113 let active = bound active 0 (source#getitemcount - 1) in
3114 let first = calcfirst m_first active in
3115 G.postRedisplay "outline navigate";
3116 coe {< m_active = active; m_first = first >}
3118 let navscroll first =
3119 let active =
3120 let dist = m_active - first in
3121 if dist < 0
3122 then first
3123 else (
3124 if dist < maxrows
3125 then m_active
3126 else first + maxrows
3129 G.postRedisplay "outline navscroll";
3130 coe {< m_first = first; m_active = active >}
3132 let ctrl = Wsi.withctrl mask in
3133 let open Keys in
3134 match Wsi.kc2kt key with
3135 | Ascii 'a' when ctrl ->
3136 let text =
3137 if m_autonarrow
3138 then (source#denarrow; E.s)
3139 else (
3140 let pattern = source#renarrow in
3141 if nonemptystr m_qsearch
3142 then (source#narrow m_qsearch; m_qsearch)
3143 else pattern
3146 settext (not m_autonarrow) text;
3147 G.postRedisplay "toggle auto narrowing";
3148 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3150 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
3151 settext true E.s;
3152 G.postRedisplay "toggle auto narrowing";
3153 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3155 | Ascii 'n' when ctrl ->
3156 source#narrow m_qsearch;
3157 if not m_autonarrow
3158 then source#add_narrow_pattern m_qsearch;
3159 G.postRedisplay "outline ctrl-n";
3160 coe {< m_first = 0; m_active = 0 >}
3162 | Ascii 'S' when ctrl ->
3163 let active = source#calcactive (getanchor ()) in
3164 let first = firstof m_first active in
3165 G.postRedisplay "outline ctrl-s";
3166 coe {< m_first = first; m_active = active >}
3168 | Ascii 'u' when ctrl ->
3169 G.postRedisplay "outline ctrl-u";
3170 if m_autonarrow && nonemptystr m_qsearch
3171 then (
3172 ignore (source#renarrow);
3173 settext m_autonarrow E.s;
3174 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3176 else (
3177 source#del_narrow_pattern;
3178 let pattern = source#renarrow in
3179 let text =
3180 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3182 settext m_autonarrow text;
3183 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3186 | Ascii 'l' when ctrl ->
3187 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3188 G.postRedisplay "outline ctrl-l";
3189 coe {< m_first = first >}
3191 | Ascii '\t' when m_autonarrow ->
3192 if nonemptystr m_qsearch
3193 then (
3194 G.postRedisplay "outline list view tab";
3195 source#add_narrow_pattern m_qsearch;
3196 settext true E.s;
3197 coe {< m_qsearch = E.s >}
3199 else coe self
3201 | Escape when m_autonarrow ->
3202 if nonemptystr m_qsearch
3203 then source#add_narrow_pattern m_qsearch;
3204 super#key key mask
3206 | Enter when m_autonarrow ->
3207 if nonemptystr m_qsearch
3208 then source#add_narrow_pattern m_qsearch;
3209 super#key key mask
3211 | (Ascii _ | Code _) when m_autonarrow ->
3212 let pattern = m_qsearch ^ toutf8 key in
3213 G.postRedisplay "outlinelistview autonarrow add";
3214 source#narrow pattern;
3215 settext true pattern;
3216 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3218 | Backspace when m_autonarrow ->
3219 if emptystr m_qsearch
3220 then coe self
3221 else
3222 let pattern = withoutlastutf8 m_qsearch in
3223 G.postRedisplay "outlinelistview autonarrow backspace";
3224 ignore (source#renarrow);
3225 source#narrow pattern;
3226 settext true pattern;
3227 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3229 | Up when ctrl ->
3230 navscroll (max 0 (m_first - 1))
3232 | Down when ctrl ->
3233 navscroll (min (source#getitemcount - 1) (m_first + 1))
3235 | Up -> navigate ~-1
3236 | Down -> navigate 1
3237 | Prior -> navigate ~-(fstate.maxrows)
3238 | Next -> navigate fstate.maxrows
3240 | Right ->
3241 let o =
3242 if ctrl
3243 then (
3244 G.postRedisplay "outline ctrl right";
3245 {< m_pan = m_pan + 1 >}
3247 else self#updownlevel 1
3249 coe o
3251 | Left ->
3252 let o =
3253 if ctrl
3254 then (
3255 G.postRedisplay "outline ctrl left";
3256 {< m_pan = m_pan - 1 >}
3258 else self#updownlevel ~-1
3260 coe o
3262 | Home ->
3263 G.postRedisplay "outline home";
3264 coe {< m_first = 0; m_active = 0 >}
3266 | End ->
3267 let active = source#getitemcount - 1 in
3268 let first = max 0 (active - fstate.maxrows) in
3269 G.postRedisplay "outline end";
3270 coe {< m_active = active; m_first = first >}
3272 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
3273 super#key key mask
3274 end;;
3276 let genhistoutlines () =
3277 Config.gethist ()
3278 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3279 compare c2.lastvisit c1.lastvisit)
3280 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
3281 let path = if nonemptystr origin then origin else path in
3282 let base = mbtoutf8 @@ Filename.basename path in
3283 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3287 let gotohist (path, c, bookmarks, x, anchor, origin) =
3288 Config.save leavebirdseye;
3289 state.anchor <- anchor;
3290 state.bookmarks <- bookmarks;
3291 state.origin <- origin;
3292 state.x <- x;
3293 setconf conf c;
3294 let x0, y0, x1, y1 = conf.trimfuzz in
3295 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3296 reshape ~firsttime:true state.winw state.winh;
3297 opendoc path origin;
3298 setzoom c.zoom;
3301 let makecheckers () =
3302 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3303 following to say:
3304 converted by Issac Trotts. July 25, 2002 *)
3305 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3306 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3307 let id = GlTex.gen_texture () in
3308 GlTex.bind_texture ~target:`texture_2d id;
3309 GlPix.store (`unpack_alignment 1);
3310 GlTex.image2d image;
3311 List.iter (GlTex.parameter ~target:`texture_2d)
3312 [ `mag_filter `nearest; `min_filter `nearest ];
3316 let setcheckers enabled =
3317 match state.checkerstexid with
3318 | None ->
3319 if enabled then state.checkerstexid <- Some (makecheckers ())
3321 | Some checkerstexid ->
3322 if not enabled
3323 then (
3324 GlTex.delete_texture checkerstexid;
3325 state.checkerstexid <- None;
3329 let describe_layout layout =
3330 let d =
3331 match layout with
3332 | [] -> "Page 0"
3333 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
3334 | l :: rest ->
3335 let rangestr a b =
3336 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
3337 else
3338 let sep = if a.pageno+1 = b.pageno then ", " else UniSyms.ellipsis in
3339 Printf.sprintf "%d%s%d" (a.pageno+1) sep (b.pageno+1)
3341 let rec fold s la lb = function
3342 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
3343 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
3344 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
3346 fold "Pages" l l rest
3348 let percent =
3349 let maxy = maxy () in
3350 if maxy <= 0
3351 then 100.
3352 else 100. *. (float state.y /. float maxy)
3354 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
3357 let setpresentationmode v =
3358 let n = page_of_y state.y in
3359 state.anchor <- (n, 0.0, 1.0);
3360 conf.presentation <- v;
3361 if conf.fitmodel = FitPage
3362 then reqlayout conf.angle conf.fitmodel;
3363 represent ();
3366 let setbgcol (r, g, b) =
3367 let col =
3368 let r = r *. 255.0 |> truncate
3369 and g = g *. 255.0 |> truncate
3370 and b = b *. 255.0 |> truncate in
3371 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3373 Wsi.setwinbgcol col;
3376 let enterinfomode =
3377 let btos b = if b then UniSyms.radical else E.s in
3378 let showextended = ref false in
3379 let showcolors = ref false in
3380 let leave mode _ = state.mode <- mode in
3381 let src =
3382 (object
3383 val mutable m_l = []
3384 val mutable m_a = E.a
3385 val mutable m_prev_uioh = nouioh
3386 val mutable m_prev_mode = View
3388 inherit lvsourcebase
3390 method reset prev_mode prev_uioh =
3391 m_a <- Array.of_list (List.rev m_l);
3392 m_l <- [];
3393 m_prev_mode <- prev_mode;
3394 m_prev_uioh <- prev_uioh;
3396 method int name get set =
3397 m_l <-
3398 (name, `int get, 1,
3399 Action (
3400 fun u ->
3401 let ondone s =
3402 try set (int_of_string s)
3403 with exn ->
3404 state.text <- Printf.sprintf "bad integer `%s': %s"
3405 s @@ exntos exn
3407 state.text <- E.s;
3408 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3409 state.mode <- Textentry (te, leave m_prev_mode);
3411 )) :: m_l
3413 method int_with_suffix name get set =
3414 m_l <-
3415 (name, `intws get, 1,
3416 Action (
3417 fun u ->
3418 let ondone s =
3419 try set (int_of_string_with_suffix s)
3420 with exn ->
3421 state.text <- Printf.sprintf "bad integer `%s': %s"
3422 s @@ exntos exn
3424 state.text <- E.s;
3425 let te =
3426 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3428 state.mode <- Textentry (te, leave m_prev_mode);
3430 )) :: m_l
3432 method bool ?(offset=1) ?(btos=btos) name get set =
3433 m_l <-
3434 (name, `bool (btos, get), offset, Action (
3435 fun u ->
3436 let v = get () in
3437 set (not v);
3439 )) :: m_l
3441 method color name get set =
3442 m_l <-
3443 (name, `color get, 1,
3444 Action (
3445 fun u ->
3446 let invalid = (nan, nan, nan) in
3447 let ondone s =
3448 let c =
3449 try color_of_string s
3450 with exn ->
3451 state.text <- Printf.sprintf "bad color `%s': %s"
3452 s @@ exntos exn;
3453 invalid
3455 if c <> invalid
3456 then set c;
3458 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3459 state.text <- color_to_string (get ());
3460 state.mode <- Textentry (te, leave m_prev_mode);
3462 )) :: m_l
3464 method string name get set =
3465 m_l <-
3466 (name, `string get, 1,
3467 Action (
3468 fun u ->
3469 let ondone s = set s in
3470 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3471 state.mode <- Textentry (te, leave m_prev_mode);
3473 )) :: m_l
3475 method colorspace name get set =
3476 m_l <-
3477 (name, `string get, 1,
3478 Action (
3479 fun _ ->
3480 let source =
3481 (object
3482 inherit lvsourcebase
3484 initializer
3485 m_active <- CSTE.to_int conf.colorspace;
3486 m_first <- 0;
3488 method getitemcount =
3489 Array.length CSTE.names
3490 method getitem n =
3491 (CSTE.names.(n), 0)
3492 method exit ~uioh ~cancel ~active ~first ~pan =
3493 ignore (uioh, first, pan);
3494 if not cancel then set active;
3495 None
3496 method hasaction _ = true
3497 end)
3499 state.text <- E.s;
3500 let modehash = findkeyhash conf "info" in
3501 coe (new listview ~zebra:false ~helpmode:false
3502 ~source ~trusted:true ~modehash)
3503 )) :: m_l
3505 method paxmark name get set =
3506 m_l <-
3507 (name, `string get, 1,
3508 Action (
3509 fun _ ->
3510 let source =
3511 (object
3512 inherit lvsourcebase
3514 initializer
3515 m_active <- MTE.to_int conf.paxmark;
3516 m_first <- 0;
3518 method getitemcount = Array.length MTE.names
3519 method getitem n = (MTE.names.(n), 0)
3520 method exit ~uioh ~cancel ~active ~first ~pan =
3521 ignore (uioh, first, pan);
3522 if not cancel then set active;
3523 None
3524 method hasaction _ = true
3525 end)
3527 state.text <- E.s;
3528 let modehash = findkeyhash conf "info" in
3529 coe (new listview ~zebra:false ~helpmode:false
3530 ~source ~trusted:true ~modehash)
3531 )) :: m_l
3533 method fitmodel name get set =
3534 m_l <-
3535 (name, `string get, 1,
3536 Action (
3537 fun _ ->
3538 let source =
3539 (object
3540 inherit lvsourcebase
3542 initializer
3543 m_active <- FMTE.to_int conf.fitmodel;
3544 m_first <- 0;
3546 method getitemcount = Array.length FMTE.names
3547 method getitem n = (FMTE.names.(n), 0)
3548 method exit ~uioh ~cancel ~active ~first ~pan =
3549 ignore (uioh, first, pan);
3550 if not cancel then set active;
3551 None
3552 method hasaction _ = true
3553 end)
3555 state.text <- E.s;
3556 let modehash = findkeyhash conf "info" in
3557 coe (new listview ~zebra:false ~helpmode:false
3558 ~source ~trusted:true ~modehash)
3559 )) :: m_l
3561 method caption s offset =
3562 m_l <- (s, `empty, offset, Noaction) :: m_l
3564 method caption2 s f offset =
3565 m_l <- (s, `string f, offset, Noaction) :: m_l
3567 method getitemcount = Array.length m_a
3569 method getitem n =
3570 let tostr = function
3571 | `int f -> string_of_int (f ())
3572 | `intws f -> string_with_suffix_of_int (f ())
3573 | `string f -> f ()
3574 | `color f -> color_to_string (f ())
3575 | `bool (btos, f) -> btos (f ())
3576 | `empty -> E.s
3578 let name, t, offset, _ = m_a.(n) in
3579 ((let s = tostr t in
3580 if nonemptystr s
3581 then Printf.sprintf "%s\t%s" name s
3582 else name),
3583 offset)
3585 method exit ~uioh ~cancel ~active ~first ~pan =
3586 let uiohopt =
3587 if not cancel
3588 then (
3589 let uioh =
3590 match m_a.(active) with
3591 | _, _, _, Action f -> f uioh
3592 | _, _, _, Noaction -> uioh
3594 Some uioh
3596 else None
3598 m_active <- active;
3599 m_first <- first;
3600 m_pan <- pan;
3601 uiohopt
3603 method hasaction n =
3604 match m_a.(n) with
3605 | _, _, _, Action _ -> true
3606 | _, _, _, Noaction -> false
3608 initializer m_active <- 1
3609 end)
3611 let rec fillsrc prevmode prevuioh =
3612 let sep () = src#caption E.s 0 in
3613 let colorp name get set =
3614 src#string name
3615 (fun () -> color_to_string (get ()))
3616 (fun v ->
3618 let c = color_of_string v in
3619 set c
3620 with exn ->
3621 state.text <-
3622 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3625 let rgba name get set =
3626 src#string name
3627 (fun () -> rgba_to_string (get ()))
3628 (fun v ->
3630 let c = rgba_of_string v in
3631 set c
3632 with exn ->
3633 state.text <-
3634 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3637 let oldmode = state.mode in
3638 let birdseye = isbirdseye state.mode in
3640 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3642 src#bool "presentation mode"
3643 (fun () -> conf.presentation)
3644 (fun v -> setpresentationmode v);
3646 src#bool "ignore case in searches"
3647 (fun () -> conf.icase)
3648 (fun v -> conf.icase <- v);
3650 src#bool "preload"
3651 (fun () -> conf.preload)
3652 (fun v -> conf.preload <- v);
3654 src#bool "highlight links"
3655 (fun () -> conf.hlinks)
3656 (fun v -> conf.hlinks <- v);
3658 src#bool "under info"
3659 (fun () -> conf.underinfo)
3660 (fun v -> conf.underinfo <- v);
3662 src#bool "persistent bookmarks"
3663 (fun () -> conf.savebmarks)
3664 (fun v -> conf.savebmarks <- v);
3666 src#fitmodel "fit model"
3667 (fun () -> FMTE.to_string conf.fitmodel)
3668 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3670 src#bool "trim margins"
3671 (fun () -> conf.trimmargins)
3672 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3674 src#bool "persistent location"
3675 (fun () -> conf.jumpback)
3676 (fun v -> conf.jumpback <- v);
3678 sep ();
3679 src#int "inter-page space"
3680 (fun () -> conf.interpagespace)
3681 (fun n ->
3682 conf.interpagespace <- n;
3683 docolumns conf.columns;
3684 let pageno, py =
3685 match state.layout with
3686 | [] -> 0, 0
3687 | l :: _ ->
3688 l.pageno, l.pagey
3690 state.maxy <- calcheight ();
3691 let y = getpagey pageno in
3692 gotoxy state.x (y + py)
3695 src#int "page bias"
3696 (fun () -> conf.pagebias)
3697 (fun v -> conf.pagebias <- v);
3699 src#int "scroll step"
3700 (fun () -> conf.scrollstep)
3701 (fun n -> conf.scrollstep <- n);
3703 src#int "horizontal scroll step"
3704 (fun () -> conf.hscrollstep)
3705 (fun v -> conf.hscrollstep <- v);
3707 src#int "auto scroll step"
3708 (fun () ->
3709 match state.autoscroll with
3710 | Some step -> step
3711 | _ -> conf.autoscrollstep)
3712 (fun n ->
3713 let n = boundastep state.winh n in
3714 if state.autoscroll <> None
3715 then state.autoscroll <- Some n;
3716 conf.autoscrollstep <- n);
3718 src#int "zoom"
3719 (fun () -> truncate (conf.zoom *. 100.))
3720 (fun v -> pivotzoom ((float v) /. 100.));
3722 src#int "rotation"
3723 (fun () -> conf.angle)
3724 (fun v -> reqlayout v conf.fitmodel);
3726 src#int "scroll bar width"
3727 (fun () -> conf.scrollbw)
3728 (fun v ->
3729 conf.scrollbw <- v;
3730 reshape state.winw state.winh;
3733 src#int "scroll handle height"
3734 (fun () -> conf.scrollh)
3735 (fun v -> conf.scrollh <- v;);
3737 src#int "thumbnail width"
3738 (fun () -> conf.thumbw)
3739 (fun v ->
3740 conf.thumbw <- min 4096 v;
3741 match oldmode with
3742 | Birdseye beye ->
3743 leavebirdseye beye false;
3744 enterbirdseye ()
3745 | Textentry _
3746 | View
3747 | LinkNav _ -> ()
3750 let mode = state.mode in
3751 src#string "columns"
3752 (fun () ->
3753 match conf.columns with
3754 | Csingle _ -> "1"
3755 | Cmulti (multi, _) -> multicolumns_to_string multi
3756 | Csplit (count, _) -> "-" ^ string_of_int count
3758 (fun v ->
3759 let n, a, b = multicolumns_of_string v in
3760 setcolumns mode n a b);
3762 sep ();
3763 src#caption "Pixmap cache" 0;
3764 src#int_with_suffix "size (advisory)"
3765 (fun () -> conf.memlimit)
3766 (fun v -> conf.memlimit <- v);
3768 src#caption2 "used"
3769 (fun () ->
3770 Printf.sprintf "%s bytes, %d tiles"
3771 (string_with_suffix_of_int state.memused)
3772 (Hashtbl.length state.tilemap)) 1;
3774 sep ();
3775 src#caption "Layout" 0;
3776 src#caption2 "Dimension"
3777 (fun () ->
3778 Printf.sprintf "%dx%d (virtual %dx%d)"
3779 state.winw state.winh
3780 state.w state.maxy)
3782 if conf.debug
3783 then
3784 src#caption2 "Position" (fun () ->
3785 Printf.sprintf "%dx%d" state.x state.y
3787 else
3788 src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
3790 sep ();
3791 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3792 "Save these parameters as global defaults at exit"
3793 (fun () -> conf.bedefault)
3794 (fun v -> conf.bedefault <- v);
3796 sep ();
3797 let btos b = if b then UniSyms.lguillemet else UniSyms.rguillemet in
3798 src#bool ~offset:0 ~btos "Extended parameters"
3799 (fun () -> !showextended)
3800 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3801 if !showextended
3802 then (
3803 src#bool "checkers"
3804 (fun () -> conf.checkers)
3805 (fun v -> conf.checkers <- v; setcheckers v);
3806 src#bool "update cursor"
3807 (fun () -> conf.updatecurs)
3808 (fun v -> conf.updatecurs <- v);
3809 src#bool "scroll-bar on the left"
3810 (fun () -> conf.leftscroll)
3811 (fun v -> conf.leftscroll <- v);
3812 src#bool "verbose"
3813 (fun () -> conf.verbose)
3814 (fun v -> conf.verbose <- v);
3815 src#bool "invert colors"
3816 (fun () -> conf.invert)
3817 (fun v -> conf.invert <- v);
3818 src#bool "max fit"
3819 (fun () -> conf.maxhfit)
3820 (fun v -> conf.maxhfit <- v);
3821 src#bool "pax mode"
3822 (fun () -> conf.pax != None)
3823 (fun v ->
3824 if v
3825 then conf.pax <- Some (now ())
3826 else conf.pax <- None);
3827 src#string "uri launcher"
3828 (fun () -> conf.urilauncher)
3829 (fun v -> conf.urilauncher <- v);
3830 src#string "path launcher"
3831 (fun () -> conf.pathlauncher)
3832 (fun v -> conf.pathlauncher <- v);
3833 src#string "tile size"
3834 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3835 (fun v ->
3837 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3838 conf.tilew <- max 64 w;
3839 conf.tileh <- max 64 h;
3840 flushtiles ();
3841 with exn ->
3842 state.text <- Printf.sprintf "bad tile size `%s': %s"
3843 v @@ exntos exn
3845 src#int "texture count"
3846 (fun () -> conf.texcount)
3847 (fun v ->
3848 if realloctexts v
3849 then conf.texcount <- v
3850 else impmsg "failed to set texture count please retry later"
3852 src#int "slice height"
3853 (fun () -> conf.sliceheight)
3854 (fun v ->
3855 conf.sliceheight <- v;
3856 wcmd "sliceh %d" conf.sliceheight;
3858 src#int "anti-aliasing level"
3859 (fun () -> conf.aalevel)
3860 (fun v ->
3861 conf.aalevel <- bound v 0 8;
3862 state.anchor <- getanchor ();
3863 opendoc state.path state.password;
3865 src#string "page scroll scaling factor"
3866 (fun () -> string_of_float conf.pgscale)
3867 (fun v ->
3869 let s = float_of_string v in
3870 conf.pgscale <- s
3871 with exn ->
3872 state.text <- Printf.sprintf
3873 "bad page scroll scaling factor `%s': %s" v
3874 @@ exntos exn
3877 src#int "ui font size"
3878 (fun () -> fstate.fontsize)
3879 (fun v -> setfontsize (bound v 5 100));
3880 src#int "hint font size"
3881 (fun () -> conf.hfsize)
3882 (fun v -> conf.hfsize <- bound v 5 100);
3883 src#bool "crop hack"
3884 (fun () -> conf.crophack)
3885 (fun v -> conf.crophack <- v);
3886 src#string "trim fuzz"
3887 (fun () -> irect_to_string conf.trimfuzz)
3888 (fun v ->
3890 conf.trimfuzz <- irect_of_string v;
3891 if conf.trimmargins
3892 then settrim true conf.trimfuzz;
3893 with exn ->
3894 state.text <- Printf.sprintf "bad irect `%s': %s" v
3895 @@ exntos exn
3897 src#string "throttle"
3898 (fun () ->
3899 match conf.maxwait with
3900 | None -> "show place holder if page is not ready"
3901 | Some time ->
3902 if time = infinity
3903 then "wait for page to fully render"
3904 else
3905 "wait " ^ string_of_float time
3906 ^ " seconds before showing placeholder"
3908 (fun v ->
3910 let f = float_of_string v in
3911 if f <= 0.0
3912 then conf.maxwait <- None
3913 else conf.maxwait <- Some f
3914 with exn ->
3915 state.text <- Printf.sprintf "bad time `%s': %s" v
3916 @@ exntos exn
3918 src#string "ghyll scroll"
3919 (fun () ->
3920 match conf.ghyllscroll with
3921 | None -> E.s
3922 | Some nab -> ghyllscroll_to_string nab
3924 (fun v ->
3925 try conf.ghyllscroll <- ghyllscroll_of_string v
3926 with
3927 | Failure msg ->
3928 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3929 | exn ->
3930 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3931 @@ exntos exn
3933 src#string "selection command"
3934 (fun () -> conf.selcmd)
3935 (fun v -> conf.selcmd <- v);
3936 src#string "synctex command"
3937 (fun () -> conf.stcmd)
3938 (fun v -> conf.stcmd <- v);
3939 src#string "pax command"
3940 (fun () -> conf.paxcmd)
3941 (fun v -> conf.paxcmd <- v);
3942 src#string "ask password command"
3943 (fun () -> conf.passcmd)
3944 (fun v -> conf.passcmd <- v);
3945 src#string "save path command"
3946 (fun () -> conf.savecmd)
3947 (fun v -> conf.savecmd <- v);
3948 src#colorspace "color space"
3949 (fun () -> CSTE.to_string conf.colorspace)
3950 (fun v ->
3951 conf.colorspace <- CSTE.of_int v;
3952 wcmd "cs %d" v;
3953 load state.layout;
3955 src#paxmark "pax mark method"
3956 (fun () -> MTE.to_string conf.paxmark)
3957 (fun v -> conf.paxmark <- MTE.of_int v);
3958 if bousable () && !opengl_has_pbo
3959 then
3960 src#bool "use PBO"
3961 (fun () -> conf.usepbo)
3962 (fun v -> conf.usepbo <- v);
3963 src#bool "mouse wheel scrolls pages"
3964 (fun () -> conf.wheelbypage)
3965 (fun v -> conf.wheelbypage <- v);
3966 src#bool "open remote links in a new instance"
3967 (fun () -> conf.riani)
3968 (fun v -> conf.riani <- v);
3969 src#bool "edit annotations inline"
3970 (fun () -> conf.annotinline)
3971 (fun v -> conf.annotinline <- v);
3972 src#bool "coarse positioning in presentation mode"
3973 (fun () -> conf.coarseprespos)
3974 (fun v -> conf.coarseprespos <- v);
3975 src#bool "use document CSS"
3976 (fun () -> conf.usedoccss)
3977 (fun v ->
3978 conf.usedoccss <- v;
3979 state.anchor <- getanchor ();
3980 opendoc state.path state.password;
3982 src#bool ~btos "colors"
3983 (fun () -> !showcolors)
3984 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
3985 if !showcolors
3986 then (
3987 colorp " background"
3988 (fun () -> conf.bgcolor)
3989 (fun v -> conf.bgcolor <- v; setbgcol v);
3990 rgba " scrollbar"
3991 (fun () -> conf.sbarcolor)
3992 (fun v -> conf.sbarcolor <- v);
3993 rgba " scrollbar handle"
3994 (fun () -> conf.sbarhndlcolor)
3995 (fun v -> conf.sbarhndlcolor <- v);
3999 sep ();
4000 src#caption "Document" 0;
4001 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4002 src#caption2 "Pages"
4003 (fun () -> string_of_int state.pagecount) 1;
4004 src#caption2 "Dimensions"
4005 (fun () -> string_of_int (List.length state.pdims)) 1;
4006 if nonemptystr conf.css
4007 then src#caption2 "CSS" (fun () -> conf.css) 1;
4008 if conf.trimmargins
4009 then (
4010 sep ();
4011 src#caption "Trimmed margins" 0;
4012 src#caption2 "Dimensions"
4013 (fun () -> string_of_int (List.length state.pdims)) 1;
4016 sep ();
4017 src#caption "OpenGL" 0;
4018 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4019 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4021 sep ();
4022 src#caption "Location" 0;
4023 if nonemptystr state.origin
4024 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4025 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4027 src#reset prevmode prevuioh;
4029 fun () ->
4030 state.text <- E.s;
4031 resetmstate ();
4032 let prevmode = state.mode
4033 and prevuioh = state.uioh in
4034 fillsrc prevmode prevuioh;
4035 let source = (src :> lvsource) in
4036 let modehash = findkeyhash conf "info" in
4037 state.uioh <-
4038 coe (object (self)
4039 inherit listview ~zebra:false ~helpmode:false
4040 ~source ~trusted:true ~modehash as super
4041 val mutable m_prevmemused = 0
4042 method! infochanged = function
4043 | Memused ->
4044 if m_prevmemused != state.memused
4045 then (
4046 m_prevmemused <- state.memused;
4047 G.postRedisplay "memusedchanged";
4049 | Pdim -> G.postRedisplay "pdimchanged"
4050 | Docinfo -> fillsrc prevmode prevuioh
4052 method! key key mask =
4053 if not (Wsi.withctrl mask)
4054 then
4055 match [@warning "-4"] Wsi.kc2kt key with
4056 | Keys.Left -> coe (self#updownlevel ~-1)
4057 | Keys.Right -> coe (self#updownlevel 1)
4058 | _ -> super#key key mask
4059 else super#key key mask
4060 end);
4061 G.postRedisplay "info";
4064 let enterhelpmode =
4065 let source =
4066 (object
4067 inherit lvsourcebase
4068 method getitemcount = Array.length state.help
4069 method getitem n =
4070 let s, l, _ = state.help.(n) in
4071 (s, l)
4073 method exit ~uioh ~cancel ~active ~first ~pan =
4074 let optuioh =
4075 if not cancel
4076 then (
4077 match state.help.(active) with
4078 | _, _, Action f -> Some (f uioh)
4079 | _, _, Noaction -> Some uioh
4081 else None
4083 m_active <- active;
4084 m_first <- first;
4085 m_pan <- pan;
4086 optuioh
4088 method hasaction n =
4089 match state.help.(n) with
4090 | _, _, Action _ -> true
4091 | _, _, Noaction -> false
4093 initializer
4094 m_active <- -1
4095 end)
4096 in fun () ->
4097 let modehash = findkeyhash conf "help" in
4098 resetmstate ();
4099 state.uioh <- coe (new listview
4100 ~zebra:false ~helpmode:true
4101 ~source ~trusted:true ~modehash);
4102 G.postRedisplay "help";
4105 let entermsgsmode =
4106 let msgsource =
4107 (object
4108 inherit lvsourcebase
4109 val mutable m_items = E.a
4111 method getitemcount = 1 + Array.length m_items
4113 method getitem n =
4114 if n = 0
4115 then "[Clear]", 0
4116 else m_items.(n-1), 0
4118 method exit ~uioh ~cancel ~active ~first ~pan =
4119 ignore uioh;
4120 if not cancel
4121 then (
4122 if active = 0
4123 then Buffer.clear state.errmsgs;
4125 m_active <- active;
4126 m_first <- first;
4127 m_pan <- pan;
4128 None
4130 method hasaction n =
4131 n = 0
4133 method reset =
4134 state.newerrmsgs <- false;
4135 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4136 m_items <- Array.of_list l
4138 initializer
4139 m_active <- 0
4140 end)
4141 in fun () ->
4142 state.text <- E.s;
4143 resetmstate ();
4144 msgsource#reset;
4145 let source = (msgsource :> lvsource) in
4146 let modehash = findkeyhash conf "listview" in
4147 state.uioh <-
4148 coe (object
4149 inherit listview ~zebra:false ~helpmode:false
4150 ~source ~trusted:false ~modehash as super
4151 method! display =
4152 if state.newerrmsgs
4153 then msgsource#reset;
4154 super#display
4155 end);
4156 G.postRedisplay "msgs";
4159 let getusertext s =
4160 let editor = getenvwithdef "EDITOR" E.s in
4161 if emptystr editor
4162 then E.s
4163 else
4164 let tmppath = Filename.temp_file "llpp" "note" in
4165 if nonemptystr s
4166 then (
4167 let oc = open_out tmppath in
4168 output_string oc s;
4169 close_out oc;
4171 let execstr = editor ^ " " ^ tmppath in
4172 let s =
4173 match spawn execstr [] with
4174 | exception exn ->
4175 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4177 | pid ->
4178 match Unix.waitpid [] pid with
4179 | exception exn ->
4180 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4182 | (_pid, status) ->
4183 match status with
4184 | Unix.WEXITED 0 -> filecontents tmppath
4185 | Unix.WEXITED n ->
4186 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4188 | Unix.WSIGNALED n ->
4189 impmsg "editor process(%s) was killed by signal %d" execstr n;
4191 | Unix.WSTOPPED n ->
4192 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4195 match Unix.unlink tmppath with
4196 | exception exn ->
4197 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4199 | () -> s
4202 let enterannotmode opaque slinkindex =
4203 let msgsource =
4204 (object
4205 inherit lvsourcebase
4206 val mutable m_text = E.s
4207 val mutable m_items = E.a
4209 method getitemcount = Array.length m_items
4211 method getitem n =
4212 let label, _func = m_items.(n) in
4213 label, 0
4215 method exit ~uioh ~cancel ~active ~first ~pan =
4216 ignore (uioh, first, pan);
4217 if not cancel
4218 then (
4219 let _label, func = m_items.(active) in
4220 func ()
4222 None
4224 method hasaction n = nonemptystr @@ fst m_items.(n)
4226 method reset s =
4227 let rec split accu b i =
4228 let p = b+i in
4229 if p = String.length s
4230 then (String.sub s b (p-b), unit) :: accu
4231 else
4232 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4233 then
4234 let ss = if i = 0 then E.s else String.sub s b i in
4235 split ((ss, unit)::accu) (p+1) 0
4236 else
4237 split accu b (i+1)
4239 let cleanup () =
4240 wcmd "freepage %s" (~> opaque);
4241 let keys =
4242 Hashtbl.fold (fun key opaque' accu ->
4243 if opaque' = opaque'
4244 then key :: accu else accu) state.pagemap []
4246 List.iter (Hashtbl.remove state.pagemap) keys;
4247 flushtiles ();
4248 gotoxy state.x state.y
4250 let dele () =
4251 delannot opaque slinkindex;
4252 cleanup ();
4254 let edit inline () =
4255 let update s =
4256 if emptystr s
4257 then dele ()
4258 else (
4259 modannot opaque slinkindex s;
4260 cleanup ();
4263 if inline
4264 then
4265 let mode = state.mode in
4266 state.mode <-
4267 Textentry (
4268 ("annotation: ", m_text, None, textentry, update, true),
4269 fun _ -> state.mode <- mode);
4270 state.text <- E.s;
4271 enttext ();
4272 else
4273 let s = getusertext m_text in
4274 update s
4276 m_text <- s;
4277 m_items <-
4278 ( "[Copy]", fun () -> selstring m_text)
4279 :: ("[Delete]", dele)
4280 :: ("[Edit]", edit conf.annotinline)
4281 :: (E.s, unit)
4282 :: split [] 0 0 |> List.rev |> Array.of_list
4284 initializer
4285 m_active <- 0
4286 end)
4288 state.text <- E.s;
4289 let s = getannotcontents opaque slinkindex in
4290 resetmstate ();
4291 msgsource#reset s;
4292 let source = (msgsource :> lvsource) in
4293 let modehash = findkeyhash conf "listview" in
4294 state.uioh <- coe (object
4295 inherit listview ~zebra:false ~helpmode:false
4296 ~source ~trusted:false ~modehash
4297 end);
4298 G.postRedisplay "enterannotmode";
4301 let gotoremote spec =
4302 let filename, dest = splitatchar spec '#' in
4303 let getpath filename =
4304 let path =
4305 if nonemptystr filename
4306 then
4307 if Filename.is_relative filename
4308 then
4309 let dir = Filename.dirname state.path in
4310 let dir =
4311 if Filename.is_implicit dir
4312 then Filename.concat (Sys.getcwd ()) dir
4313 else dir
4315 Filename.concat dir filename
4316 else filename
4317 else E.s
4319 if Sys.file_exists path
4320 then path
4321 else E.s
4323 let path = getpath filename in
4324 let dospawn lcmd =
4325 if conf.riani
4326 then
4327 let cmd = Lazy.force_val lcmd in
4328 match spawn cmd with
4329 | _pid -> ()
4330 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
4331 else
4332 let anchor = getanchor () in
4333 let ranchor = state.path, state.password, anchor, state.origin in
4334 state.origin <- E.s;
4335 state.ranchors <- ranchor :: state.ranchors;
4336 opendoc path E.s;
4338 if substratis spec 0 "page="
4339 then
4340 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4341 | pageno ->
4342 state.anchor <- (pageno, 0.0, 0.0);
4343 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4344 | exception exn ->
4345 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4346 else (
4347 state.nameddest <- dest;
4348 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4352 let gotounder = function
4353 | Ulinkuri s when isexternallink s ->
4354 if substratis s 0 "file://"
4355 then gotoremote @@ String.sub s 7 (String.length s - 7)
4356 else gotouri s
4357 | Ulinkuri s ->
4358 let pageno, x, y = uritolocation s in
4359 addnav ();
4360 gotopagexy !wtmode pageno x y
4361 | Utext _ | Unone -> ()
4362 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4365 let gotooutline (_, _, kind) =
4366 match kind with
4367 | Onone -> ()
4368 | Oanchor anchor ->
4369 let (pageno, y, _) = anchor in
4370 let y = getanchory
4371 (if conf.presentation then (pageno, y, 1.0) else anchor)
4373 addnav ();
4374 gotoghyll y
4375 | Ouri uri -> gotounder (Ulinkuri uri)
4376 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4377 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4378 | Ohistory hist -> gotohist hist
4379 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4382 class outlinesoucebase fetchoutlines = object (self)
4383 inherit lvsourcebase
4384 val mutable m_items = E.a
4385 val mutable m_minfo = E.a
4386 val mutable m_orig_items = E.a
4387 val mutable m_orig_minfo = E.a
4388 val mutable m_narrow_patterns = []
4389 val mutable m_gen = -1
4391 method getitemcount = Array.length m_items
4393 method getitem n =
4394 let s, n, _ = m_items.(n) in
4395 (s, n+0)
4397 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4398 ignore (uioh, first);
4399 let items, minfo =
4400 if m_narrow_patterns = []
4401 then m_orig_items, m_orig_minfo
4402 else m_items, m_minfo
4404 m_pan <- pan;
4405 if not cancel
4406 then (
4407 m_items <- items;
4408 m_minfo <- minfo;
4409 gotooutline m_items.(active);
4411 else (
4412 m_items <- items;
4413 m_minfo <- minfo;
4415 None
4417 method hasaction (_:int) = true
4419 method greetmsg =
4420 if Array.length m_items != Array.length m_orig_items
4421 then
4422 let s =
4423 match m_narrow_patterns with
4424 | one :: [] -> one
4425 | many -> String.concat UniSyms.ellipsis (List.rev many)
4427 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4428 else E.s
4430 method statestr =
4431 match m_narrow_patterns with
4432 | [] -> E.s
4433 | one :: [] -> one
4434 | head :: _ -> UniSyms.ellipsis ^ head
4436 method narrow pattern =
4437 match Str.regexp_case_fold pattern with
4438 | exception _ -> ()
4439 | re ->
4440 let rec loop accu minfo n =
4441 if n = -1
4442 then (
4443 m_items <- Array.of_list accu;
4444 m_minfo <- Array.of_list minfo;
4446 else
4447 let (s, _, _) as o = m_items.(n) in
4448 let accu, minfo =
4449 match Str.search_forward re s 0 with
4450 | exception Not_found -> accu, minfo
4451 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4453 loop accu minfo (n-1)
4455 loop [] [] (Array.length m_items - 1)
4457 method! getminfo = m_minfo
4459 method denarrow =
4460 m_orig_items <- fetchoutlines ();
4461 m_minfo <- m_orig_minfo;
4462 m_items <- m_orig_items
4464 method add_narrow_pattern pattern =
4465 m_narrow_patterns <- pattern :: m_narrow_patterns
4467 method del_narrow_pattern =
4468 match m_narrow_patterns with
4469 | _ :: rest -> m_narrow_patterns <- rest
4470 | [] -> ()
4472 method renarrow =
4473 self#denarrow;
4474 match m_narrow_patterns with
4475 | pattern :: [] -> self#narrow pattern; pattern
4476 | list ->
4477 List.fold_left (fun accu pattern ->
4478 self#narrow pattern;
4479 pattern ^ UniSyms.ellipsis ^ accu) E.s list
4481 method calcactive (_:anchor) = 0
4483 method reset anchor items =
4484 if state.gen != m_gen
4485 then (
4486 m_orig_items <- items;
4487 m_items <- items;
4488 m_narrow_patterns <- [];
4489 m_minfo <- E.a;
4490 m_orig_minfo <- E.a;
4491 m_gen <- state.gen;
4493 else (
4494 if items != m_orig_items
4495 then (
4496 m_orig_items <- items;
4497 if m_narrow_patterns == []
4498 then m_items <- items;
4501 let active = self#calcactive anchor in
4502 m_active <- active;
4503 m_first <- firstof m_first active
4507 let outlinesource fetchoutlines =
4508 (object
4509 inherit outlinesoucebase fetchoutlines
4510 method! calcactive anchor =
4511 let rely = getanchory anchor in
4512 let rec loop n best bestd =
4513 if n = Array.length m_items
4514 then best
4515 else
4516 let _, _, kind = m_items.(n) in
4517 match kind with
4518 | Oanchor anchor ->
4519 let orely = getanchory anchor in
4520 let d = abs (orely - rely) in
4521 if d < bestd
4522 then loop (n+1) n d
4523 else loop (n+1) best bestd
4524 | Onone | Oremote _ | Olaunch _
4525 | Oremotedest _ | Ouri _ | Ohistory _ ->
4526 loop (n+1) best bestd
4528 loop 0 ~-1 max_int
4529 end)
4532 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4533 let mkselector sourcetype =
4534 let fetchoutlines () =
4535 match sourcetype with
4536 | `bookmarks -> Array.of_list state.bookmarks
4537 | `outlines -> state.outlines
4538 | `history -> genhistoutlines () |> Array.of_list
4540 let source =
4541 if sourcetype = `history
4542 then new outlinesoucebase fetchoutlines
4543 else outlinesource fetchoutlines
4545 (fun errmsg ->
4546 let outlines = fetchoutlines () in
4547 if Array.length outlines = 0
4548 then showtext ' ' errmsg
4549 else (
4550 resetmstate ();
4551 Wsi.setcursor Wsi.CURSOR_INHERIT;
4552 let anchor = getanchor () in
4553 source#reset anchor outlines;
4554 state.text <- source#greetmsg;
4555 state.uioh <-
4556 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4557 G.postRedisplay "enter selector";
4561 let mkenter sourcetype errmsg =
4562 let enter = mkselector sourcetype in
4563 fun () -> enter errmsg
4565 ( mkenter `outlines "document has no outline"
4566 , mkenter `bookmarks "document has no bookmarks (yet)"
4567 , mkenter `history "history is empty" )
4570 let quickbookmark ?title () =
4571 match state.layout with
4572 | [] -> ()
4573 | l :: _ ->
4574 let title =
4575 match title with
4576 | None ->
4577 Unix.(
4578 let tm = localtime (now ()) in
4579 Printf.sprintf
4580 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
4581 (l.pageno+1)
4582 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
4584 | Some title -> title
4586 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4589 let setautoscrollspeed step goingdown =
4590 let incr = max 1 ((abs step) / 2) in
4591 let incr = if goingdown then incr else -incr in
4592 let astep = boundastep state.winh (step + incr) in
4593 state.autoscroll <- Some astep;
4596 let canpan () =
4597 match conf.columns with
4598 | Csplit _ -> true
4599 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4602 let panbound x = bound x (-state.w) state.winw;;
4604 let existsinrow pageno (columns, coverA, coverB) p =
4605 let last = ((pageno - coverA) mod columns) + columns in
4606 let rec any = function
4607 | [] -> false
4608 | l :: rest ->
4609 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4610 then p l
4611 else (
4612 if not (p l)
4613 then (if l.pageno = last then false else any rest)
4614 else true
4617 any state.layout
4620 let nextpage () =
4621 match state.layout with
4622 | [] ->
4623 let pageno = page_of_y state.y in
4624 gotoghyll (getpagey (pageno+1))
4625 | l :: rest ->
4626 match conf.columns with
4627 | Csingle _ ->
4628 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4629 then
4630 let y = clamp (pgscale state.winh) in
4631 gotoghyll y
4632 else
4633 let pageno = min (l.pageno+1) (state.pagecount-1) in
4634 gotoghyll (getpagey pageno)
4635 | Cmulti ((c, _, _) as cl, _) ->
4636 if conf.presentation
4637 && (existsinrow l.pageno cl
4638 (fun l -> l.pageh > l.pagey + l.pagevh))
4639 then
4640 let y = clamp (pgscale state.winh) in
4641 gotoghyll y
4642 else
4643 let pageno = min (l.pageno+c) (state.pagecount-1) in
4644 gotoghyll (getpagey pageno)
4645 | Csplit (n, _) ->
4646 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4647 then
4648 let pagey, pageh = getpageyh l.pageno in
4649 let pagey = pagey + pageh * l.pagecol in
4650 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4651 gotoghyll (pagey + pageh + ips)
4654 let prevpage () =
4655 match state.layout with
4656 | [] ->
4657 let pageno = page_of_y state.y in
4658 gotoghyll (getpagey (pageno-1))
4659 | l :: _ ->
4660 match conf.columns with
4661 | Csingle _ ->
4662 if conf.presentation && l.pagey != 0
4663 then
4664 gotoghyll (clamp (pgscale ~-(state.winh)))
4665 else
4666 let pageno = max 0 (l.pageno-1) in
4667 gotoghyll (getpagey pageno)
4668 | Cmulti ((c, _, coverB) as cl, _) ->
4669 if conf.presentation &&
4670 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4671 then
4672 gotoghyll (clamp (pgscale ~-(state.winh)))
4673 else
4674 let decr =
4675 if l.pageno = state.pagecount - coverB
4676 then 1
4677 else c
4679 let pageno = max 0 (l.pageno-decr) in
4680 gotoghyll (getpagey pageno)
4681 | Csplit (n, _) ->
4682 let y =
4683 if l.pagecol = 0
4684 then
4685 if l.pageno = 0
4686 then l.pagey
4687 else
4688 let pageno = max 0 (l.pageno-1) in
4689 let pagey, pageh = getpageyh pageno in
4690 pagey + (n-1)*pageh
4691 else
4692 let pagey, pageh = getpageyh l.pageno in
4693 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4695 gotoghyll y
4698 let save () =
4699 if emptystr conf.savecmd
4700 then adderrmsg "savepath-command is empty"
4701 "don't know where to save modified document"
4702 else
4703 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4704 let path =
4705 getcmdoutput
4706 (fun exn ->
4707 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
4708 savecmd
4710 if nonemptystr path
4711 then
4712 let tmp = path ^ ".tmp" in
4713 savedoc tmp;
4714 Unix.rename tmp path;
4717 let viewkeyboard key mask =
4718 let enttext te =
4719 let mode = state.mode in
4720 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4721 state.text <- E.s;
4722 enttext ();
4723 G.postRedisplay "view:enttext"
4725 let ctrl = Wsi.withctrl mask in
4726 let open Keys in
4727 match Wsi.kc2kt key with
4728 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
4730 | Ascii 'Q' -> exit 0
4732 | Ascii 'W' ->
4733 if hasunsavedchanges ()
4734 then save ()
4736 | Insert ->
4737 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4738 then (
4739 state.mode <- (
4740 match state.lnava with
4741 | None -> LinkNav (Ltgendir 0)
4742 | Some pn -> LinkNav (Ltexact pn)
4744 gotoxy state.x state.y;
4746 else impmsg "keyboard link navigation does not work under rotation"
4748 | Escape | Ascii 'q' ->
4749 begin match state.mstate with
4750 | Mzoomrect _ ->
4751 resetmstate ();
4752 G.postRedisplay "kill rect";
4753 | Msel _
4754 | Mpan _
4755 | Mscrolly | Mscrollx
4756 | Mzoom _
4757 | Mnone ->
4758 begin match state.mode with
4759 | LinkNav ln ->
4760 begin match ln with
4761 | Ltexact pl -> state.lnava <- Some pl
4762 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4763 end;
4764 state.mode <- View;
4765 G.postRedisplay "esc leave linknav"
4766 | Birdseye _ | Textentry _ | View ->
4767 match state.ranchors with
4768 | [] -> raise Quit
4769 | (path, password, anchor, origin) :: rest ->
4770 state.ranchors <- rest;
4771 state.anchor <- anchor;
4772 state.origin <- origin;
4773 state.nameddest <- E.s;
4774 opendoc path password
4775 end;
4776 end;
4778 | Backspace ->
4779 addnavnorc ();
4780 gotoghyll (getnav ~-1)
4782 | Ascii 'o' ->
4783 enteroutlinemode ()
4785 | Ascii 'H' ->
4786 enterhistmode ()
4788 | Ascii 'u' ->
4789 state.rects <- [];
4790 state.text <- E.s;
4791 Hashtbl.iter (fun _ opaque ->
4792 clearmark opaque;
4793 Hashtbl.clear state.prects) state.pagemap;
4794 G.postRedisplay "dehighlight";
4796 | Ascii (('/' | '?') as c) ->
4797 let ondone isforw s =
4798 cbput state.hists.pat s;
4799 state.searchpattern <- s;
4800 search s isforw
4802 let s = String.make 1 c in
4803 enttext (s, E.s, Some (onhist state.hists.pat),
4804 textentry, ondone (c = '/'), true)
4806 | Ascii '+' | Ascii '=' when ctrl ->
4807 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4808 pivotzoom (conf.zoom +. incr)
4810 | Ascii '+' ->
4811 let ondone s =
4812 let n =
4813 try int_of_string s with exn ->
4814 state.text <-
4815 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4816 max_int
4818 if n != max_int
4819 then (
4820 conf.pagebias <- n;
4821 state.text <- "page bias is now " ^ string_of_int n;
4824 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4826 | Ascii '-' when ctrl ->
4827 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4828 pivotzoom (max 0.01 (conf.zoom -. decr))
4830 | Ascii '-' ->
4831 let ondone msg = state.text <- msg in
4832 enttext (
4833 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4834 optentry state.mode, ondone, true
4837 | Ascii '0' when ctrl ->
4838 if conf.zoom = 1.0
4839 then gotoxy 0 state.y
4840 else setzoom 1.0
4842 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
4843 let cols =
4844 match conf.columns with
4845 | Csingle _ | Cmulti _ -> 1
4846 | Csplit (n, _) -> n
4848 let h = state.winh -
4849 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4851 let zoom = zoomforh state.winw h 0 cols in
4852 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4853 then setzoom zoom
4855 | Ascii '3' when ctrl ->
4856 let fm =
4857 match conf.fitmodel with
4858 | FitWidth -> FitProportional
4859 | FitProportional -> FitPage
4860 | FitPage -> FitWidth
4862 state.text <- "fit model: " ^ FMTE.to_string fm;
4863 reqlayout conf.angle fm
4865 | Ascii '4' when ctrl ->
4866 let zoom = getmaxw () /. float state.winw in
4867 if zoom > 0.0 then setzoom zoom
4869 | Fn 9 ->
4870 togglebirdseye ()
4872 | Ascii '9' when ctrl ->
4873 togglebirdseye ()
4875 | Ascii ('0'..'9' as c) when not ctrl ->
4876 let ondone s =
4877 let n =
4878 try int_of_string s with exn ->
4879 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4882 if n >= 0
4883 then (
4884 addnav ();
4885 cbput state.hists.pag (string_of_int n);
4886 gotopage1 (n + conf.pagebias - 1) 0;
4889 let [@warning "-4"] pageentry text = function
4890 | Keys.Ascii 'g' -> TEdone text
4891 | key -> intentry text key
4893 let text = String.make 1 c in
4894 enttext (":", text, Some (onhist state.hists.pag),
4895 pageentry, ondone, true)
4897 | Ascii 'b' ->
4898 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4899 G.postRedisplay "toggle scrollbar";
4901 | Ascii 'B' ->
4902 state.bzoom <- not state.bzoom;
4903 state.rects <- [];
4904 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4906 | Ascii 'l' ->
4907 conf.hlinks <- not conf.hlinks;
4908 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4909 G.postRedisplay "toggle highlightlinks";
4911 | Ascii 'F' ->
4912 if conf.angle mod 360 = 0
4913 then (
4914 state.glinks <- true;
4915 let mode = state.mode in
4916 state.mode <-
4917 Textentry (
4918 (":", E.s, None, linknentry, linknact gotounder, false),
4919 (fun _ ->
4920 state.glinks <- false;
4921 state.mode <- mode)
4923 state.text <- E.s;
4924 G.postRedisplay "view:linkent(F)"
4926 else impmsg "hint mode does not work under rotation"
4928 | Ascii 'y' ->
4929 state.glinks <- true;
4930 let mode = state.mode in
4931 state.mode <-
4932 Textentry (
4933 (":", E.s, None, linknentry,
4934 linknact (fun under -> selstring (undertext under)), false),
4935 (fun _ ->
4936 state.glinks <- false;
4937 state.mode <- mode)
4939 state.text <- E.s;
4940 G.postRedisplay "view:linkent"
4942 | Ascii 'a' ->
4943 begin match state.autoscroll with
4944 | Some step ->
4945 conf.autoscrollstep <- step;
4946 state.autoscroll <- None
4947 | None ->
4948 state.autoscroll <- Some conf.autoscrollstep;
4949 state.slideshow <- state.slideshow land lnot 2
4952 | Ascii 'p' when ctrl ->
4953 launchpath () (* XXX where do error messages go? *)
4955 | Ascii 'P' ->
4956 setpresentationmode (not conf.presentation);
4957 showtext ' ' ("presentation mode " ^
4958 if conf.presentation then "on" else "off");
4960 | Ascii 'f' ->
4961 if List.mem Wsi.Fullscreen state.winstate
4962 then Wsi.reshape conf.cwinw conf.cwinh
4963 else Wsi.fullscreen ()
4965 | Ascii ('p'|'N') ->
4966 search state.searchpattern false
4968 | Ascii 'n' | Fn 3 ->
4969 search state.searchpattern true
4971 | Ascii 't' ->
4972 begin match state.layout with
4973 | [] -> ()
4974 | l :: _ ->
4975 gotoghyll (getpagey l.pageno)
4978 | Ascii ' ' ->
4979 nextpage ()
4981 | Delete ->
4982 prevpage ()
4984 | Ascii '=' ->
4985 showtext ' ' (describe_layout state.layout);
4987 | Ascii 'w' ->
4988 begin match state.layout with
4989 | [] -> ()
4990 | l :: _ ->
4991 Wsi.reshape l.pagew l.pageh;
4992 G.postRedisplay "w"
4995 | Ascii '\'' ->
4996 enterbookmarkmode ()
4998 | Ascii 'h' | Fn 1 ->
4999 enterhelpmode ()
5001 | Ascii 'i' ->
5002 enterinfomode ()
5004 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
5005 entermsgsmode ()
5007 | Ascii 'm' ->
5008 let ondone s =
5009 match state.layout with
5010 | l :: _ ->
5011 if nonemptystr s
5012 then
5013 state.bookmarks <-
5014 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5015 | _ -> ()
5017 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5019 | Ascii '~' ->
5020 quickbookmark ();
5021 showtext ' ' "Quick bookmark added";
5023 | Ascii 'z' ->
5024 begin match state.layout with
5025 | l :: _ ->
5026 let rect = getpdimrect l.pagedimno in
5027 let w, h =
5028 if conf.crophack
5029 then
5030 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5031 truncate (1.2 *. (rect.(3) -. rect.(0))))
5032 else
5033 (truncate (rect.(1) -. rect.(0)),
5034 truncate (rect.(3) -. rect.(0)))
5036 let w = truncate ((float w)*.conf.zoom)
5037 and h = truncate ((float h)*.conf.zoom) in
5038 if w != 0 && h != 0
5039 then (
5040 state.anchor <- getanchor ();
5041 Wsi.reshape w (h + conf.interpagespace)
5043 G.postRedisplay "z";
5045 | [] -> ()
5048 | Ascii 'x' -> state.roam ()
5050 | Ascii ('<'|'>' as c) ->
5051 reqlayout
5052 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
5054 | Ascii ('['|']' as c) ->
5055 conf.colorscale <-
5056 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
5057 G.postRedisplay "brightness";
5059 | Ascii 'c' when state.mode = View ->
5060 if Wsi.withalt mask
5061 then (
5062 if conf.zoom > 1.0
5063 then
5064 let m = (state.winw - state.w) / 2 in
5065 gotoxy_and_clear_text m state.y
5067 else
5068 let (c, a, b), z =
5069 match state.prevcolumns with
5070 | None -> (1, 0, 0), 1.0
5071 | Some (columns, z) ->
5072 let cab =
5073 match columns with
5074 | Csplit (c, _) -> -c, 0, 0
5075 | Cmulti ((c, a, b), _) -> c, a, b
5076 | Csingle _ -> 1, 0, 0
5078 cab, z
5080 setcolumns View c a b;
5081 setzoom z
5083 | Down | Up when ctrl && Wsi.withshift mask ->
5084 let zoom, x = state.prevzoom in
5085 setzoom zoom;
5086 state.x <- x;
5088 | Ascii 'k' | Up ->
5089 begin match state.autoscroll with
5090 | None ->
5091 begin match state.mode with
5092 | Birdseye beye -> upbirdseye 1 beye
5093 | Textentry _ | View | LinkNav _ ->
5094 if ctrl
5095 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5096 else (
5097 if not (Wsi.withshift mask) && conf.presentation
5098 then prevpage ()
5099 else gotoghyll1 true (clamp (-conf.scrollstep))
5102 | Some n ->
5103 setautoscrollspeed n false
5106 | Ascii 'j' | Down ->
5107 begin match state.autoscroll with
5108 | None ->
5109 begin match state.mode with
5110 | Birdseye beye -> downbirdseye 1 beye
5111 | Textentry _ | View | LinkNav _ ->
5112 if ctrl
5113 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5114 else (
5115 if not (Wsi.withshift mask) && conf.presentation
5116 then nextpage ()
5117 else gotoghyll1 true (clamp (conf.scrollstep))
5120 | Some n ->
5121 setautoscrollspeed n true
5124 | Left | Right when not (Wsi.withalt mask) ->
5125 if canpan ()
5126 then
5127 let dx =
5128 if ctrl
5129 then state.winw / 2
5130 else conf.hscrollstep
5132 let dx =
5133 let pv = Wsi.kc2kt key in
5134 if pv = Keys.Left then dx else -dx
5136 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5137 else (
5138 state.text <- E.s;
5139 G.postRedisplay "left/right"
5142 | Prior ->
5143 let y =
5144 if ctrl
5145 then
5146 match state.layout with
5147 | [] -> state.y
5148 | l :: _ -> state.y - l.pagey
5149 else
5150 clamp (pgscale (-state.winh))
5152 gotoghyll y
5154 | Next ->
5155 let y =
5156 if ctrl
5157 then
5158 match List.rev state.layout with
5159 | [] -> state.y
5160 | l :: _ -> getpagey l.pageno
5161 else
5162 clamp (pgscale state.winh)
5164 gotoghyll y
5166 | Ascii 'g' | Home ->
5167 addnav ();
5168 gotoghyll 0
5169 | Ascii 'G' | End ->
5170 addnav ();
5171 gotoghyll (clamp state.maxy)
5173 | Right when Wsi.withalt mask ->
5174 addnavnorc ();
5175 gotoghyll (getnav 1)
5176 | Left when Wsi.withalt mask ->
5177 addnavnorc ();
5178 gotoghyll (getnav ~-1)
5180 | Ascii 'r' ->
5181 reload ()
5183 | Ascii 'v' when conf.debug ->
5184 state.rects <- [];
5185 List.iter (fun l ->
5186 match getopaque l.pageno with
5187 | None -> ()
5188 | Some opaque ->
5189 let x0, y0, x1, y1 = pagebbox opaque in
5190 let rect = (float x0, float y0,
5191 float x1, float y0,
5192 float x1, float y1,
5193 float x0, float y1) in
5194 debugrect rect;
5195 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5196 state.rects <- (l.pageno, color, rect) :: state.rects;
5197 ) state.layout;
5198 G.postRedisplay "v";
5200 | Ascii '|' ->
5201 let mode = state.mode in
5202 let cmd = ref E.s in
5203 let onleave = function
5204 | Cancel -> state.mode <- mode
5205 | Confirm ->
5206 List.iter (fun l ->
5207 match getopaque l.pageno with
5208 | Some opaque -> pipesel opaque !cmd
5209 | None -> ()) state.layout;
5210 state.mode <- mode
5212 let ondone s =
5213 cbput state.hists.sel s;
5214 cmd := s
5216 let te =
5217 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5219 G.postRedisplay "|";
5220 state.mode <- Textentry (te, onleave);
5222 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
5223 vlog "huh? %s" (Wsi.keyname key)
5226 let linknavkeyboard key mask linknav =
5227 let pv = Wsi.kc2kt key in
5228 let getpage pageno =
5229 let rec loop = function
5230 | [] -> None
5231 | l :: _ when l.pageno = pageno -> Some l
5232 | _ :: rest -> loop rest
5233 in loop state.layout
5235 let doexact (pageno, n) =
5236 match getopaque pageno, getpage pageno with
5237 | Some opaque, Some l ->
5238 if pv = Keys.Enter
5239 then
5240 let under = getlink opaque n in
5241 G.postRedisplay "link gotounder";
5242 gotounder under;
5243 state.mode <- View;
5244 else
5245 let opt, dir =
5246 let open Keys in
5247 match pv with
5248 | Home -> Some (findlink opaque LDfirst), -1
5249 | End -> Some (findlink opaque LDlast), 1
5250 | Left -> Some (findlink opaque (LDleft n)), -1
5251 | Right -> Some (findlink opaque (LDright n)), 1
5252 | Up -> Some (findlink opaque (LDup n)), -1
5253 | Down -> Some (findlink opaque (LDdown n)), 1
5255 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
5256 | Code _|Fn _|Ctrl _|Backspace -> None, 0
5258 let pwl l dir =
5259 begin match findpwl l.pageno dir with
5260 | Pwlnotfound -> ()
5261 | Pwl pageno ->
5262 let notfound dir =
5263 state.mode <- LinkNav (Ltgendir dir);
5264 let y, h = getpageyh pageno in
5265 let y =
5266 if dir < 0
5267 then y + h - state.winh
5268 else y
5270 gotoxy state.x y
5272 begin match getopaque pageno, getpage pageno with
5273 | Some opaque, Some _ ->
5274 let link =
5275 let ld = if dir > 0 then LDfirst else LDlast in
5276 findlink opaque ld
5278 begin match link with
5279 | Lfound m ->
5280 showlinktype (getlink opaque m);
5281 state.mode <- LinkNav (Ltexact (pageno, m));
5282 G.postRedisplay "linknav jpage";
5283 | Lnotfound -> notfound dir
5284 end;
5285 | _ -> notfound dir
5286 end;
5287 end;
5289 begin match opt with
5290 | Some Lnotfound -> pwl l dir;
5291 | Some (Lfound m) ->
5292 if m = n
5293 then pwl l dir
5294 else (
5295 let _, y0, _, y1 = getlinkrect opaque m in
5296 if y0 < l.pagey
5297 then gotopage1 l.pageno y0
5298 else (
5299 let d = fstate.fontsize + 1 in
5300 if y1 - l.pagey > l.pagevh - d
5301 then gotopage1 l.pageno (y1 - state.winh + d)
5302 else G.postRedisplay "linknav";
5304 showlinktype (getlink opaque m);
5305 state.mode <- LinkNav (Ltexact (l.pageno, m));
5308 | None -> viewkeyboard key mask
5309 end;
5310 | _ -> viewkeyboard key mask
5312 if pv = Keys.Insert
5313 then (
5314 begin match linknav with
5315 | Ltexact pa -> state.lnava <- Some pa
5316 | Ltgendir _ | Ltnotready _ -> ()
5317 end;
5318 state.mode <- View;
5319 G.postRedisplay "leave linknav"
5321 else
5322 match linknav with
5323 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5324 | Ltexact exact -> doexact exact
5327 let keyboard key mask =
5328 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5329 then wcmd "interrupt"
5330 else state.uioh <- state.uioh#key key mask
5333 let birdseyekeyboard key mask
5334 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5335 let incr =
5336 match conf.columns with
5337 | Csingle _ -> 1
5338 | Cmulti ((c, _, _), _) -> c
5339 | Csplit _ -> failwith "bird's eye split mode"
5341 let pgh layout = List.fold_left
5342 (fun m l -> max l.pageh m) state.winh layout in
5343 let open Keys in
5344 match Wsi.kc2kt key with
5345 | Ascii 'l' when Wsi.withctrl mask ->
5346 let y, h = getpageyh pageno in
5347 let top = (state.winh - h) / 2 in
5348 gotoxy state.x (max 0 (y - top))
5349 | Enter -> leavebirdseye beye false
5350 | Escape -> leavebirdseye beye true
5351 | Up -> upbirdseye incr beye
5352 | Down -> downbirdseye incr beye
5353 | Left -> upbirdseye 1 beye
5354 | Right -> downbirdseye 1 beye
5356 | Prior ->
5357 begin match state.layout with
5358 | l :: _ ->
5359 if l.pagey != 0
5360 then (
5361 state.mode <- Birdseye (
5362 oconf, leftx, l.pageno, hooverpageno, anchor
5364 gotopage1 l.pageno 0;
5366 else (
5367 let layout = layout state.x (state.y-state.winh)
5368 state.winw
5369 (pgh state.layout) in
5370 match layout with
5371 | [] -> gotoxy state.x (clamp (-state.winh))
5372 | l :: _ ->
5373 state.mode <- Birdseye (
5374 oconf, leftx, l.pageno, hooverpageno, anchor
5376 gotopage1 l.pageno 0
5379 | [] -> gotoxy state.x (clamp (-state.winh))
5380 end;
5382 | Next ->
5383 begin match List.rev state.layout with
5384 | l :: _ ->
5385 let layout = layout state.x
5386 (state.y + (pgh state.layout))
5387 state.winw state.winh in
5388 begin match layout with
5389 | [] ->
5390 let incr = l.pageh - l.pagevh in
5391 if incr = 0
5392 then (
5393 state.mode <-
5394 Birdseye (
5395 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5397 G.postRedisplay "birdseye pagedown";
5399 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5401 | l :: _ ->
5402 state.mode <-
5403 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5404 gotopage1 l.pageno 0;
5407 | [] -> gotoxy state.x (clamp state.winh)
5408 end;
5410 | Home ->
5411 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5412 gotopage1 0 0
5414 | End ->
5415 let pageno = state.pagecount - 1 in
5416 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5417 if not (pagevisible state.layout pageno)
5418 then
5419 let h =
5420 match List.rev state.pdims with
5421 | [] -> state.winh
5422 | (_, _, h, _) :: _ -> h
5424 gotoxy
5425 state.x
5426 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5427 else G.postRedisplay "birdseye end";
5429 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
5432 let drawpage l =
5433 let color =
5434 match state.mode with
5435 | Textentry _ -> scalecolor 0.4
5436 | LinkNav _ | View -> scalecolor 1.0
5437 | Birdseye (_, _, pageno, hooverpageno, _) ->
5438 if l.pageno = hooverpageno
5439 then scalecolor 0.9
5440 else (
5441 if l.pageno = pageno
5442 then (
5443 let c = scalecolor 1.0 in
5444 GlDraw.color c;
5445 GlDraw.line_width 3.0;
5446 let dispx = l.pagedispx in
5447 linerect
5448 (float (dispx-1)) (float (l.pagedispy-1))
5449 (float (dispx+l.pagevw+1))
5450 (float (l.pagedispy+l.pagevh+1))
5452 GlDraw.line_width 1.0;
5455 else scalecolor 0.8
5458 drawtiles l color;
5461 let postdrawpage l linkindexbase =
5462 match getopaque l.pageno with
5463 | Some opaque ->
5464 if tileready l l.pagex l.pagey
5465 then
5466 let x = l.pagedispx - l.pagex
5467 and y = l.pagedispy - l.pagey in
5468 let hlmask =
5469 match conf.columns with
5470 | Csingle _ | Cmulti _ ->
5471 (if conf.hlinks then 1 else 0)
5472 + (if state.glinks
5473 && not (isbirdseye state.mode) then 2 else 0)
5474 | Csplit _ -> 0
5476 let s =
5477 match state.mode with
5478 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5479 | Textentry _
5480 | Birdseye _
5481 | View
5482 | LinkNav _ -> E.s
5484 Hashtbl.find_all state.prects l.pageno |>
5485 List.iter (fun vals -> drawprect opaque x y vals);
5486 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5487 if n < 0
5488 then (state.redisplay <- true; 0)
5489 else n
5490 else 0
5491 | _ -> 0
5494 let scrollindicator () =
5495 let sbw, ph, sh = state.uioh#scrollph in
5496 let sbh, pw, sw = state.uioh#scrollpw in
5498 let x0,x1,hx0 =
5499 if conf.leftscroll
5500 then (0, sbw, sbw)
5501 else ((state.winw - sbw), state.winw, 0)
5504 Gl.enable `blend;
5505 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5506 let (r, g, b, alpha) = conf.sbarcolor in
5507 GlDraw.color (r, g, b) ~alpha;
5508 filledrect (float x0) 0. (float x1) (float state.winh);
5509 filledrect
5510 (float hx0) (float (state.winh - sbh))
5511 (float (hx0 + state.winw)) (float state.winh);
5512 let (r, g, b, alpha) = conf.sbarhndlcolor in
5513 GlDraw.color (r, g, b) ~alpha;
5515 filledrect (float x0) ph (float x1) (ph +. sh);
5516 let pw = pw +. float hx0 in
5517 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5518 Gl.disable `blend;
5521 let showsel () =
5522 match state.mstate with
5523 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5526 | Msel ((x0, y0), (x1, y1)) ->
5527 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5528 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5529 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5530 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5533 let showrects =
5534 function [] -> ()
5535 | rects ->
5536 Gl.enable `blend;
5537 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5538 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5539 List.iter
5540 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5541 List.iter (fun l ->
5542 if l.pageno = pageno
5543 then (
5544 let dx = float (l.pagedispx - l.pagex) in
5545 let dy = float (l.pagedispy - l.pagey) in
5546 let r, g, b, alpha = c in
5547 GlDraw.color (r, g, b) ~alpha;
5548 filledrect2 (x0+.dx) (y0+.dy)
5549 (x1+.dx) (y1+.dy)
5550 (x3+.dx) (y3+.dy)
5551 (x2+.dx) (y2+.dy);
5553 ) state.layout
5554 ) rects;
5555 Gl.disable `blend;
5558 let display () =
5559 GlDraw.color (scalecolor2 conf.bgcolor);
5560 GlClear.color (scalecolor2 conf.bgcolor);
5561 GlClear.clear [`color];
5562 List.iter drawpage state.layout;
5563 let rects =
5564 match state.mode with
5565 | LinkNav (Ltexact (pageno, linkno)) ->
5566 begin match getopaque pageno with
5567 | Some opaque ->
5568 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5569 let color = (0.0, 0.0, 0.5, 0.5) in
5570 (pageno, color,
5571 (float x0, float y0,
5572 float x1, float y0,
5573 float x1, float y1,
5574 float x0, float y1)
5575 ) :: state.rects
5576 | None -> state.rects
5578 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5579 | Birdseye _
5580 | Textentry _
5581 | View -> state.rects
5583 showrects rects;
5584 let rec postloop linkindexbase = function
5585 | l :: rest ->
5586 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5587 postloop linkindexbase rest
5588 | [] -> ()
5590 showsel ();
5591 postloop 0 state.layout;
5592 state.uioh#display;
5593 begin match state.mstate with
5594 | Mzoomrect ((x0, y0), (x1, y1)) ->
5595 Gl.enable `blend;
5596 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5597 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5598 filledrect (float x0) (float y0) (float x1) (float y1);
5599 Gl.disable `blend;
5600 | Msel _
5601 | Mpan _
5602 | Mscrolly | Mscrollx
5603 | Mzoom _
5604 | Mnone -> ()
5605 end;
5606 enttext ();
5607 scrollindicator ();
5608 Wsi.swapb ();
5611 let zoomrect x y x1 y1 =
5612 let x0 = min x x1
5613 and x1 = max x x1
5614 and y0 = min y y1 in
5615 let zoom = (float state.w) /. float (x1 - x0) in
5616 let margin =
5617 let simple () =
5618 if state.w < state.winw
5619 then (state.winw - state.w) / 2
5620 else 0
5622 match conf.fitmodel with
5623 | FitWidth | FitProportional -> simple ()
5624 | FitPage ->
5625 match conf.columns with
5626 | Csplit _ ->
5627 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5628 | Cmulti _ | Csingle _ -> simple ()
5630 gotoxy ((state.x + margin) - x0) (state.y + y0);
5631 state.anchor <- getanchor ();
5632 setzoom zoom;
5633 resetmstate ();
5636 let annot inline x y =
5637 match unproject x y with
5638 | Some (opaque, n, ux, uy) ->
5639 let add text =
5640 addannot opaque ux uy text;
5641 wcmd "freepage %s" (~> opaque);
5642 Hashtbl.remove state.pagemap (n, state.gen);
5643 flushtiles ();
5644 gotoxy state.x state.y
5646 if inline
5647 then
5648 let ondone s = add s in
5649 let mode = state.mode in
5650 state.mode <- Textentry (
5651 ("annotation: ", E.s, None, textentry, ondone, true),
5652 fun _ -> state.mode <- mode);
5653 state.text <- E.s;
5654 enttext ();
5655 G.postRedisplay "annot"
5656 else
5657 add @@ getusertext E.s
5658 | _ -> ()
5661 let zoomblock x y =
5662 let g opaque l px py =
5663 match rectofblock opaque px py with
5664 | Some a ->
5665 let x0 = a.(0) -. 20. in
5666 let x1 = a.(1) +. 20. in
5667 let y0 = a.(2) -. 20. in
5668 let zoom = (float state.w) /. (x1 -. x0) in
5669 let pagey = getpagey l.pageno in
5670 let margin = (state.w - l.pagew)/2 in
5671 let nx = -truncate x0 - margin in
5672 gotoxy_and_clear_text nx (pagey + truncate y0);
5673 state.anchor <- getanchor ();
5674 setzoom zoom;
5675 None
5676 | None -> None
5678 match conf.columns with
5679 | Csplit _ ->
5680 impmsg "block zooming does not work properly in split columns mode"
5681 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5684 let scrollx x =
5685 let winw = state.winw - 1 in
5686 let s = float x /. float winw in
5687 let destx = truncate (float (state.w + winw) *. s) in
5688 gotoxy_and_clear_text (winw - destx) state.y;
5689 state.mstate <- Mscrollx;
5692 let scrolly y =
5693 let s = float y /. float state.winh in
5694 let desty = truncate (s *. float (maxy ())) in
5695 gotoxy_and_clear_text state.x desty;
5696 state.mstate <- Mscrolly;
5699 let viewmulticlick clicks x y mask =
5700 let g opaque l px py =
5701 let mark =
5702 match clicks with
5703 | 2 -> Mark_word
5704 | 3 -> Mark_line
5705 | 4 -> Mark_block
5706 | _ -> Mark_page
5708 if markunder opaque px py mark
5709 then (
5710 Some (fun () ->
5711 let dopipe cmd =
5712 match getopaque l.pageno with
5713 | None -> ()
5714 | Some opaque -> pipesel opaque cmd
5716 state.roam <- (fun () -> dopipe conf.paxcmd);
5717 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5720 else None
5722 G.postRedisplay "viewmulticlick";
5723 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5726 let canselect () =
5727 match conf.columns with
5728 | Csplit _ -> false
5729 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5732 let viewmouse button down x y mask =
5733 match button with
5734 | n when (n == 4 || n == 5) && not down ->
5735 if Wsi.withctrl mask
5736 then (
5737 let incr =
5738 if n = 5
5739 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5740 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5742 let fx, fy =
5743 match state.mstate with
5744 | Mzoom (oldn, _, pos) when n = oldn -> pos
5745 | Mzoomrect _ | Mnone | Mpan _
5746 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
5748 let zoom = conf.zoom -. incr in
5749 state.mstate <- Mzoom (n, 0, (x, y));
5750 if false && abs (fx - x) > 5 || abs (fy - y) > 5
5751 then pivotzoom ~x ~y zoom
5752 else pivotzoom zoom
5754 else (
5755 match state.autoscroll with
5756 | Some step -> setautoscrollspeed step (n=4)
5757 | None ->
5758 if conf.wheelbypage || conf.presentation
5759 then (
5760 if n = 4
5761 then prevpage ()
5762 else nextpage ()
5764 else
5765 let incr =
5766 if n = 4
5767 then -conf.scrollstep
5768 else conf.scrollstep
5770 let incr = incr * 2 in
5771 let y = clamp incr in
5772 gotoxy_and_clear_text state.x y
5775 | n when (n = 6 || n = 7) && not down && canpan () ->
5776 let x =
5777 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5778 gotoxy_and_clear_text x state.y
5780 | 1 when Wsi.withshift mask ->
5781 state.mstate <- Mnone;
5782 if not down
5783 then (
5784 match unproject x y with
5785 | None -> ()
5786 | Some (_, pageno, ux, uy) ->
5787 let cmd = Printf.sprintf
5788 "%s %s %d %d %d"
5789 conf.stcmd state.path pageno ux uy
5791 match spawn cmd [] with
5792 | exception exn ->
5793 impmsg "execution of synctex command(%S) failed: %S"
5794 conf.stcmd @@ exntos exn
5795 | _pid -> ()
5798 | 1 when Wsi.withctrl mask ->
5799 if down
5800 then (
5801 Wsi.setcursor Wsi.CURSOR_FLEUR;
5802 state.mstate <- Mpan (x, y)
5804 else
5805 state.mstate <- Mnone
5807 | 3 ->
5808 if down
5809 then (
5810 if Wsi.withshift mask
5811 then (
5812 annot conf.annotinline x y;
5813 G.postRedisplay "addannot"
5815 else
5816 let p = (x, y) in
5817 Wsi.setcursor Wsi.CURSOR_CYCLE;
5818 state.mstate <- Mzoomrect (p, p)
5820 else (
5821 match state.mstate with
5822 | Mzoomrect ((x0, y0), _) ->
5823 if abs (x-x0) > 10 && abs (y - y0) > 10
5824 then zoomrect x0 y0 x y
5825 else (
5826 resetmstate ();
5827 G.postRedisplay "kill accidental zoom rect";
5829 | Msel _
5830 | Mpan _
5831 | Mscrolly | Mscrollx
5832 | Mzoom _
5833 | Mnone -> resetmstate ()
5836 | 1 when vscrollhit x ->
5837 if down
5838 then
5839 let _, position, sh = state.uioh#scrollph in
5840 if y > truncate position && y < truncate (position +. sh)
5841 then state.mstate <- Mscrolly
5842 else scrolly y
5843 else
5844 state.mstate <- Mnone
5846 | 1 when y > state.winh - hscrollh () ->
5847 if down
5848 then
5849 let _, position, sw = state.uioh#scrollpw in
5850 if x > truncate position && x < truncate (position +. sw)
5851 then state.mstate <- Mscrollx
5852 else scrollx x
5853 else
5854 state.mstate <- Mnone
5856 | 1 when state.bzoom -> if not down then zoomblock x y
5858 | 1 ->
5859 let dest = if down then getunder x y else Unone in
5860 begin match dest with
5861 | Ulinkuri _ ->
5862 gotounder dest
5864 | Unone when down ->
5865 Wsi.setcursor Wsi.CURSOR_FLEUR;
5866 state.mstate <- Mpan (x, y);
5868 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5870 | Unone | Utext _ ->
5871 if down
5872 then (
5873 if canselect ()
5874 then (
5875 state.mstate <- Msel ((x, y), (x, y));
5876 G.postRedisplay "mouse select";
5879 else (
5880 match state.mstate with
5881 | Mnone -> ()
5883 | Mzoom _ | Mscrollx | Mscrolly ->
5884 state.mstate <- Mnone
5886 | Mzoomrect ((x0, y0), _) ->
5887 zoomrect x0 y0 x y
5889 | Mpan _ ->
5890 Wsi.setcursor Wsi.CURSOR_INHERIT;
5891 state.mstate <- Mnone
5893 | Msel ((x0, y0), (x1, y1)) ->
5894 let rec loop = function
5895 | [] -> ()
5896 | l :: rest ->
5897 let inside =
5898 let a0 = l.pagedispy in
5899 let a1 = a0 + l.pagevh in
5900 let b0 = l.pagedispx in
5901 let b1 = b0 + l.pagevw in
5902 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5903 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5905 if inside
5906 then
5907 match getopaque l.pageno with
5908 | Some opaque ->
5909 let dosel cmd () =
5910 pipef ~closew:false "Msel"
5911 (fun w ->
5912 copysel w opaque;
5913 G.postRedisplay "Msel") cmd
5915 dosel conf.selcmd ();
5916 state.roam <- dosel conf.paxcmd;
5917 | None -> ()
5918 else loop rest
5920 loop state.layout;
5921 resetmstate ();
5925 | _ -> ()
5928 let birdseyemouse button down x y mask
5929 (conf, leftx, _, hooverpageno, anchor) =
5930 match button with
5931 | 1 when down ->
5932 let rec loop = function
5933 | [] -> ()
5934 | l :: rest ->
5935 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5936 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5937 then (
5938 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5940 else loop rest
5942 loop state.layout
5943 | 3 -> ()
5944 | _ -> viewmouse button down x y mask
5947 let uioh = object
5948 method display = ()
5950 method key key mask =
5951 begin match state.mode with
5952 | Textentry textentry -> textentrykeyboard key mask textentry
5953 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5954 | View -> viewkeyboard key mask
5955 | LinkNav linknav -> linknavkeyboard key mask linknav
5956 end;
5957 state.uioh
5959 method button button bstate x y mask =
5960 begin match state.mode with
5961 | LinkNav _ | View -> viewmouse button bstate x y mask
5962 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5963 | Textentry _ -> ()
5964 end;
5965 state.uioh
5967 method multiclick clicks x y mask =
5968 begin match state.mode with
5969 | LinkNav _ | View -> viewmulticlick clicks x y mask
5970 | Birdseye _ | Textentry _ -> ()
5971 end;
5972 state.uioh
5974 method motion x y =
5975 begin match state.mode with
5976 | Textentry _ -> ()
5977 | View | Birdseye _ | LinkNav _ ->
5978 match state.mstate with
5979 | Mzoom _ | Mnone -> ()
5981 | Mpan (x0, y0) ->
5982 let dx = x - x0
5983 and dy = y0 - y in
5984 state.mstate <- Mpan (x, y);
5985 let x = if canpan () then panbound (state.x + dx) else state.x in
5986 let y = clamp dy in
5987 gotoxy_and_clear_text x y
5989 | Msel (a, _) ->
5990 state.mstate <- Msel (a, (x, y));
5991 G.postRedisplay "motion select";
5993 | Mscrolly ->
5994 let y = min state.winh (max 0 y) in
5995 scrolly y
5997 | Mscrollx ->
5998 let x = min state.winw (max 0 x) in
5999 scrollx x
6001 | Mzoomrect (p0, _) ->
6002 state.mstate <- Mzoomrect (p0, (x, y));
6003 G.postRedisplay "motion zoomrect";
6004 end;
6005 state.uioh
6007 method pmotion x y =
6008 begin match state.mode with
6009 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6010 let rec loop = function
6011 | [] ->
6012 if hooverpageno != -1
6013 then (
6014 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6015 G.postRedisplay "pmotion birdseye no hoover";
6017 | l :: rest ->
6018 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6019 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6020 then (
6021 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6022 G.postRedisplay "pmotion birdseye hoover";
6024 else loop rest
6026 loop state.layout
6028 | Textentry _ -> ()
6030 | LinkNav _ | View ->
6031 match state.mstate with
6032 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6033 | Mnone ->
6034 updateunder x y;
6035 if canselect ()
6036 then
6037 match conf.pax with
6038 | None -> ()
6039 | Some past ->
6040 let now = now () in
6041 let delta = now -. past in
6042 if delta > 0.01
6043 then paxunder x y
6044 else conf.pax <- Some now
6045 end;
6046 state.uioh
6048 method infochanged _ = ()
6050 method scrollph =
6051 let maxy = maxy () in
6052 let p, h =
6053 if maxy = 0
6054 then 0.0, float state.winh
6055 else scrollph state.y maxy
6057 vscrollw (), p, h
6059 method scrollpw =
6060 let fwinw = float (state.winw - vscrollw ()) in
6061 let sw =
6062 let sw = fwinw /. float state.w in
6063 let sw = fwinw *. sw in
6064 max sw (float conf.scrollh)
6066 let position =
6067 let maxx = state.w + state.winw in
6068 let x = state.winw - state.x in
6069 let percent = float x /. float maxx in
6070 (fwinw -. sw) *. percent
6072 hscrollh (), position, sw
6074 method modehash =
6075 let modename =
6076 match state.mode with
6077 | LinkNav _ -> "links"
6078 | Textentry _ -> "textentry"
6079 | Birdseye _ -> "birdseye"
6080 | View -> "view"
6082 findkeyhash conf modename
6084 method eformsgs = true
6085 method alwaysscrolly = false
6086 method scroll dx dy =
6087 let x = if canpan () then panbound (state.x + dx) else state.x in
6088 gotoxy_and_clear_text x (clamp (2 * dy));
6089 state.uioh
6090 method zoom z x y =
6091 pivotzoom ~x ~y (conf.zoom *. exp z);
6092 end;;
6094 let addrect pageno r g b a x0 y0 x1 y1 =
6095 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6098 let ract cmds =
6099 let cl = splitatchar cmds ' ' in
6100 let scan s fmt f =
6101 try Scanf.sscanf s fmt f
6102 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
6103 cmds @@ exntos exn
6105 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6106 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6107 s pageno r g b a x0 y0 x1 y1;
6108 onpagerect
6109 pageno
6110 (fun w h ->
6111 let _,w1,h1,_ = getpagedim pageno in
6112 let sw = float w1 /. float w
6113 and sh = float h1 /. float h in
6114 let x0s = x0 *. sw
6115 and x1s = x1 *. sw
6116 and y0s = y0 *. sh
6117 and y1s = y1 *. sh in
6118 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6119 let color = (r, g, b, a) in
6120 if conf.verbose then debugrect rect;
6121 state.rects <- (pageno, color, rect) :: state.rects;
6122 G.postRedisplay s;
6125 match cl with
6126 | "reload", "" -> reload ()
6127 | "goto", args ->
6128 scan args "%u %f %f"
6129 (fun pageno x y ->
6130 let cmd, _ = state.geomcmds in
6131 if emptystr cmd
6132 then gotopagexy !wtmode pageno x y
6133 else
6134 let f prevf () =
6135 gotopagexy !wtmode pageno x y;
6136 prevf ()
6138 state.reprf <- f state.reprf
6140 | "goto1", args -> scan args "%u %f" gotopage
6141 | "gotor", args -> scan args "%S" gotoremote
6142 | "rect", args ->
6143 scan args "%u %u %f %f %f %f"
6144 (fun pageno c x0 y0 x1 y1 ->
6145 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6146 rectx "rect" pageno color x0 y0 x1 y1;
6148 | "prect", args ->
6149 scan args "%u %f %f %f %f %f %f %f %f"
6150 (fun pageno r g b alpha x0 y0 x1 y1 ->
6151 addrect pageno r g b alpha x0 y0 x1 y1;
6152 G.postRedisplay "prect"
6154 | "pgoto", args ->
6155 scan args "%u %f %f"
6156 (fun pageno x y ->
6157 let optopaque =
6158 match getopaque pageno with
6159 | Some opaque -> opaque
6160 | None -> ~< E.s
6162 pgoto optopaque pageno x y;
6163 let rec fixx = function
6164 | [] -> ()
6165 | l :: rest ->
6166 if l.pageno = pageno
6167 then gotoxy (state.x - l.pagedispx) state.y
6168 else fixx rest
6170 let layout =
6171 let mult =
6172 match conf.columns with
6173 | Csingle _ | Csplit _ -> 1
6174 | Cmulti ((n, _, _), _) -> n
6176 layout 0 state.y (state.winw * mult) state.winh
6178 fixx layout
6180 | "activatewin", "" -> Wsi.activatewin ()
6181 | "quit", "" -> raise Quit
6182 | "keys", keys ->
6183 begin try
6184 let l = Config.keys_of_string keys in
6185 List.iter (fun (k, m) -> keyboard k m) l
6186 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
6187 cmds @@ exntos exn
6189 | "clearrects", "" ->
6190 Hashtbl.clear state.prects;
6191 G.postRedisplay "clearrects"
6192 | _ ->
6193 adderrfmt "remote command"
6194 "error processing remote command: %S\n" cmds;
6197 let remote =
6198 let scratch = Bytes.create 80 in
6199 let buf = Buffer.create 80 in
6200 fun fd ->
6201 match tempfailureretry (Unix.read fd scratch 0) 80 with
6202 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6203 | 0 ->
6204 Unix.close fd;
6205 if Buffer.length buf > 0
6206 then (
6207 let s = Buffer.contents buf in
6208 Buffer.clear buf;
6209 ract s;
6211 None
6212 | n ->
6213 let rec eat ppos =
6214 let nlpos =
6215 match Bytes.index_from scratch ppos '\n' with
6216 | pos -> if pos >= n then -1 else pos
6217 | exception Not_found -> -1
6219 if nlpos >= 0
6220 then (
6221 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6222 let s = Buffer.contents buf in
6223 Buffer.clear buf;
6224 ract s;
6225 eat (nlpos+1);
6227 else (
6228 Buffer.add_subbytes buf scratch ppos (n-ppos);
6229 Some fd
6231 in eat 0
6234 let remoteopen path =
6235 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6236 with exn ->
6237 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6238 None
6241 let () =
6242 let gcconfig = ref false in
6243 let trimcachepath = ref E.s in
6244 let rcmdpath = ref E.s in
6245 let pageno = ref None in
6246 let rootwid = ref 0 in
6247 let openlast = ref false in
6248 let doreap = ref false in
6249 let csspath = ref None in
6250 selfexec := Sys.executable_name;
6251 Arg.parse
6252 (Arg.align
6253 [("-p", Arg.String (fun s -> state.password <- s),
6254 "<password> Set password");
6256 ("-f", Arg.String
6257 (fun s ->
6258 Config.fontpath := s;
6259 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6261 "<path> Set path to the user interface font");
6263 ("-c", Arg.String
6264 (fun s ->
6265 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6266 Config.confpath := s),
6267 "<path> Set path to the configuration file");
6269 ("-last", Arg.Set openlast, " Open last document");
6271 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6272 "<page-number> Jump to page");
6274 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6275 "<path> Set path to the trim cache file");
6277 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6278 "<named-destination> Set named destination");
6280 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6281 ("-cxack", Arg.Set cxack, " Cut corners");
6283 ("-remote", Arg.String (fun s -> rcmdpath := s),
6284 "<path> Set path to the source of remote commands");
6286 ("-gc", Arg.Set gcconfig, " Collect config garbage");
6288 ("-v", Arg.Unit (fun () ->
6289 Printf.printf
6290 "%s\nconfiguration path: %s\n"
6291 (version ())
6292 Config.defconfpath;
6293 exit 0), " Print version and exit");
6295 ("-css", Arg.String (fun s -> csspath := Some s),
6296 "<path> Set path to the style sheet to use with EPUB/HTML");
6298 ("-embed", Arg.Set_int rootwid, "<window-id> Embed into window");
6300 ("-origin", Arg.String (fun s -> state.origin <- s),
6301 "<origin> <undocumented>");
6303 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title")
6306 (fun s -> state.path <- s)
6307 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6309 if !wtmode
6310 then selfexec := !selfexec ^ " -wtmode";
6312 let histmode = emptystr state.path && not !openlast in
6314 if not (Config.load !openlast)
6315 then dolog "failed to load configuration";
6317 begin match !pageno with
6318 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6319 | None -> ()
6320 end;
6322 fillhelp ();
6323 if !gcconfig
6324 then (
6325 Config.gc ();
6326 exit 0
6329 let mu =
6330 object (self)
6331 val mutable m_clicks = 0
6332 val mutable m_click_x = 0
6333 val mutable m_click_y = 0
6334 val mutable m_lastclicktime = infinity
6336 method private cleanup =
6337 state.roam <- noroam;
6338 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6339 method expose = G.postRedisplay "expose"
6340 method visible v =
6341 let name =
6342 match v with
6343 | Wsi.Unobscured -> "unobscured"
6344 | Wsi.PartiallyObscured -> "partiallyobscured"
6345 | Wsi.FullyObscured -> "fullyobscured"
6347 vlog "visibility change %s" name
6348 method display = display ()
6349 method map mapped = vlog "mapped %b" mapped
6350 method reshape w h =
6351 self#cleanup;
6352 reshape w h
6353 method mouse b d x y m =
6354 if d && canselect ()
6355 then (
6357 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6359 m_click_x <- x;
6360 m_click_y <- y;
6361 if b = 1
6362 then (
6363 let t = now () in
6364 if abs x - m_click_x > 10
6365 || abs y - m_click_y > 10
6366 || abs_float (t -. m_lastclicktime) > 0.3
6367 then m_clicks <- 0;
6368 m_clicks <- m_clicks + 1;
6369 m_lastclicktime <- t;
6370 if m_clicks = 1
6371 then (
6372 self#cleanup;
6373 G.postRedisplay "cleanup";
6374 state.uioh <- state.uioh#button b d x y m;
6376 else state.uioh <- state.uioh#multiclick m_clicks x y m
6378 else (
6379 self#cleanup;
6380 m_clicks <- 0;
6381 m_lastclicktime <- infinity;
6382 state.uioh <- state.uioh#button b d x y m
6385 else (
6386 state.uioh <- state.uioh#button b d x y m
6388 method motion x y =
6389 state.mpos <- (x, y);
6390 state.uioh <- state.uioh#motion x y
6391 method pmotion x y =
6392 state.mpos <- (x, y);
6393 state.uioh <- state.uioh#pmotion x y
6394 method key k m =
6395 vlog "k=%#x m=%#x" k m;
6396 let mascm = m land (
6397 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6398 ) in
6399 let keyboard k m =
6400 let x = state.x and y = state.y in
6401 keyboard k m;
6402 if x != state.x || y != state.y then self#cleanup
6404 match state.keystate with
6405 | KSnone ->
6406 let km = k, mascm in
6407 begin
6408 match
6409 let modehash = state.uioh#modehash in
6410 try Hashtbl.find modehash km
6411 with Not_found ->
6412 try Hashtbl.find (findkeyhash conf "global") km
6413 with Not_found -> KMinsrt (k, m)
6414 with
6415 | KMinsrt (k, m) -> keyboard k m
6416 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6417 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6419 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6420 List.iter (fun (k, m) -> keyboard k m) insrt;
6421 state.keystate <- KSnone
6422 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6423 state.keystate <- KSinto (keys, insrt)
6424 | KSinto _ -> state.keystate <- KSnone
6426 method enter x y =
6427 state.mpos <- (x, y);
6428 state.uioh <- state.uioh#pmotion x y
6429 method leave = state.mpos <- (-1, -1)
6430 method winstate wsl = state.winstate <- wsl
6431 method quit : 'a. 'a = raise Quit
6432 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
6433 method zoom z x y = state.uioh#zoom z x y
6434 method opendoc path =
6435 state.mode <- View;
6436 state.uioh <- uioh;
6437 G.postRedisplay "opendoc";
6438 opendoc path state.password
6441 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6443 setbgcol conf.bgcolor;
6444 state.wsfd <- wsfd;
6446 if not @@ List.exists GlMisc.check_extension
6447 [ "GL_ARB_texture_rectangle"
6448 ; "GL_EXT_texture_recangle"
6449 ; "GL_NV_texture_rectangle" ]
6450 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6452 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6453 then (
6454 defconf.sliceheight <- 1024;
6455 defconf.texcount <- 32;
6456 defconf.usepbo <- true;
6459 let cs, ss =
6460 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6461 | exception exn ->
6462 dolog "socketpair failed: %s" @@ exntos exn;
6463 exit 1
6464 | (r, w) ->
6465 cloexec r;
6466 cloexec w;
6467 r, w
6470 setcheckers conf.checkers;
6472 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6474 begin match !csspath with
6475 | None -> ()
6476 | Some "" -> conf.css <- E.s
6477 | Some path ->
6478 let css = filecontents path in
6479 let l = String.length css in
6480 conf.css <-
6481 if substratis css (l-2) "\r\n"
6482 then String.sub css 0 (l-2)
6483 else (if css.[l-1] = '\n'
6484 then String.sub css 0 (l-1)
6485 else css);
6486 end;
6487 init cs (
6488 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6489 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6490 !Config.fontpath, !trimcachepath, !opengl_has_pbo
6492 List.iter GlArray.enable [`texture_coord; `vertex];
6493 state.ss <- ss;
6494 reshape ~firsttime:true winw winh;
6495 state.uioh <- uioh;
6496 if histmode
6497 then (
6498 Wsi.settitle "llpp (history)";
6499 enterhistmode ();
6501 else (
6502 state.text <- "Opening " ^ (mbtoutf8 state.path);
6503 opendoc state.path state.password;
6505 display ();
6506 Wsi.mapwin ();
6507 Wsi.setcursor Wsi.CURSOR_INHERIT;
6508 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6510 let rec reap () =
6511 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6512 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
6513 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
6514 | 0, _ -> ()
6515 | _pid, _status -> reap ()
6517 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6519 let optrfd =
6520 ref (
6521 if nonemptystr !rcmdpath
6522 then remoteopen !rcmdpath
6523 else None
6527 let rec loop deadline =
6528 if !doreap
6529 then (
6530 doreap := false;
6531 reap ()
6533 let r = [state.ss; state.wsfd] in
6534 let r =
6535 match !optrfd with
6536 | None -> r
6537 | Some fd -> fd :: r
6539 if state.redisplay
6540 then (
6541 state.redisplay <- false;
6542 display ();
6544 let timeout =
6545 let now = now () in
6546 if deadline > now
6547 then (
6548 if deadline = infinity
6549 then ~-.1.0
6550 else max 0.0 (deadline -. now)
6552 else 0.0
6554 let r, _, _ =
6555 try Unix.select r [] [] timeout
6556 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6558 begin match r with
6559 | [] ->
6560 state.ghyll None;
6561 let newdeadline =
6562 if state.ghyll == noghyll
6563 then
6564 match state.autoscroll with
6565 | Some step when step != 0 ->
6566 if state.slideshow land 1 = 1
6567 then (
6568 if state.slideshow land 2 = 0
6569 then state.slideshow <- state.slideshow lor 2
6570 else if step < 0 then prevpage () else nextpage ();
6571 deadline +. (float (abs step))
6573 else
6574 let y = state.y + step in
6575 let fy = if conf.maxhfit then state.winh else 0 in
6576 let y =
6577 if y < 0
6578 then state.maxy - fy
6579 else if y >= state.maxy - fy then 0 else y
6581 if state.mode = View
6582 then gotoxy_and_clear_text state.x y
6583 else gotoxy state.x y;
6584 deadline +. 0.01
6585 | _ -> infinity
6586 else deadline +. 0.01
6588 loop newdeadline
6590 | l ->
6591 let rec checkfds = function
6592 | [] -> ()
6593 | fd :: rest when fd = state.ss ->
6594 let cmd = rcmd state.ss in
6595 act cmd;
6596 checkfds rest
6598 | fd :: rest when fd = state.wsfd ->
6599 Wsi.readresp fd;
6600 checkfds rest
6602 | fd :: rest when Some fd = !optrfd ->
6603 begin match remote fd with
6604 | None -> optrfd := remoteopen !rcmdpath;
6605 | opt -> optrfd := opt
6606 end;
6607 checkfds rest
6609 | _ :: rest ->
6610 dolog "select returned unknown descriptor";
6611 checkfds rest
6613 checkfds l;
6614 let newdeadline =
6615 let deadline1 =
6616 if deadline = infinity
6617 then now () +. 0.01
6618 else deadline
6620 match state.autoscroll with
6621 | Some step when step != 0 -> deadline1
6622 | _ -> if state.ghyll == noghyll then infinity else deadline1
6624 loop newdeadline
6625 end;
6627 match loop infinity with
6628 | exception Quit ->
6629 Config.save leavebirdseye;
6630 if hasunsavedchanges ()
6631 then save ()
6632 | _ -> error "umpossible - infinity reached"