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