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