Remove wtmode
[llpp.git] / main.ml
blob605b39f1dd03d7997a05281ad8b8cdc929534d60
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 external init : Unix.file_descr -> initparams -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external getpdimrect : int -> float array = "ml_getpdimrect";;
10 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
11 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
12 external clearmark : opaque -> unit = "ml_clearmark";;
13 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
14 external getmaxw : unit -> float = "ml_getmaxw";;
15 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
16 external measurestr : int -> string -> float = "ml_measure_string";;
17 external postprocess : opaque -> int -> int -> int -> (int * string * int)
18 -> int = "ml_postprocess";;
19 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
20 external setaalevel : int -> unit = "ml_setaalevel";;
21 external realloctexts : int -> bool = "ml_realloctexts";;
22 external findlink : opaque -> linkdir -> link = "ml_findlink";;
23 external getlink : opaque -> int -> under = "ml_getlink";;
24 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
25 external getlinkcount : opaque -> int = "ml_getlinkcount";;
26 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
27 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
28 external freepbo : opaque -> unit = "ml_freepbo";;
29 external unmappbo : opaque -> unit = "ml_unmappbo";;
30 external bousable : unit -> bool = "ml_bo_usable";;
31 external unproject : opaque -> int -> int
32 -> (int * int) option = "ml_unproject";;
33 external project : opaque -> int -> int -> float -> float
34 -> (float * float) = "ml_project";;
35 external drawtile : tileparams -> opaque
36 -> unit = "ml_drawtile";;
37 external rectofblock : opaque -> int -> int
38 -> float array option = "ml_rectofblock";;
39 external begintiles : unit -> unit = "ml_begintiles";;
40 external endtiles : unit -> unit = "ml_endtiles";;
41 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
42 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
43 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
44 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
45 external savedoc : string -> unit = "ml_savedoc";;
46 external getannotcontents : opaque -> slinkindex
47 -> string = "ml_getannotcontents";;
48 external drawprect : opaque -> int -> int -> float array
49 -> unit = "ml_drawprect";;
50 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
51 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
52 external uritolocation : string
53 -> (pageno * float * float) = "ml_uritolocation";;
54 external isexternallink : string -> bool = "ml_isexternallink";;
56 (* copysel _will_ close the supplied descriptor *)
57 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
59 let selfexec = ref E.s;;
60 let ignoredoctitlte = ref false;;
61 let opengl_has_pbo = ref false;;
63 let drawstring size x y s =
64 Gl.enable `blend;
65 Gl.enable `texture_2d;
66 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
67 ignore (drawstr size x y s);
68 Gl.disable `blend;
69 Gl.disable `texture_2d;
72 let drawstring1 size x y s =
73 drawstr size x y s;
76 let drawstring2 size x y fmt =
77 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
80 module UniSyms = struct
81 let ellipsis = "\xe2\x80\xa6";;
82 let radical = "\xe2\x88\x9a";;
83 let lguillemet = "\xc2\xab";;
84 let rguillemet = "\xc2\xbb";;
85 end;;
87 let _debugl l =
88 dolog {|l %d dim=%d {
89 WxH %dx%d
90 vWxH %dx%d
91 pagex,y %d,%d
92 dispx,y %d,%d
93 column %d
94 }|}
95 l.pageno l.pagedimno
96 l.pagew l.pageh
97 l.pagevw l.pagevh
98 l.pagex l.pagey
99 l.pagedispx l.pagedispy
100 l.pagecol
103 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
104 dolog {|rect {
105 x0,y0=(% f, % f)
106 x1,y1=(% f, % f)
107 x2,y2=(% f, % f)
108 x3,y3=(% f, % f)
109 }|} x0 y0 x1 y1 x2 y2 x3 y3;
112 let isbirdseye = function
113 | Birdseye _ -> true
114 | Textentry _ | View | LinkNav _ -> false
117 let istextentry = function
118 | Textentry _ -> true
119 | Birdseye _ | View | LinkNav _ -> false
122 let 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 G.postRedisplay "gotoxy ready";
836 y, layout, true
838 if proceed
839 then (
840 state.x <- x;
841 state.y <- y;
842 state.layout <- layout;
843 begin match state.mode with
844 | LinkNav ln ->
845 begin match ln with
846 | Ltexact (pageno, linkno) ->
847 let rec loop = function
848 | [] ->
849 state.lnava <- Some (pageno, linkno);
850 state.mode <- LinkNav (Ltgendir 0)
851 | l :: _ when l.pageno = pageno ->
852 begin match getopaque pageno with
853 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
854 | Some opaque ->
855 let x0, y0, x1, y1 = getlinkrect opaque linkno in
856 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
857 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
858 then state.mode <- LinkNav (Ltgendir 0)
860 | _ :: rest -> loop rest
862 loop layout
863 | Ltnotready _ | Ltgendir _ -> ()
865 | Birdseye _ | Textentry _ | View -> ()
866 end;
867 begin match state.mode with
868 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
869 if not (pagevisible layout pageno)
870 then (
871 match state.layout with
872 | [] -> ()
873 | l :: _ ->
874 state.mode <- Birdseye (
875 conf, leftx, l.pageno, hooverpageno, anchor
878 | LinkNav lt ->
879 begin match lt with
880 | Ltnotready (_, dir)
881 | Ltgendir dir ->
882 let linknav =
883 let rec loop = function
884 | [] -> lt
885 | l :: rest ->
886 match getopaque l.pageno with
887 | None -> Ltnotready (l.pageno, dir)
888 | Some opaque ->
889 let link =
890 let ld =
891 if dir = 0
892 then LDfirstvisible (l.pagex, l.pagey, dir)
893 else (
894 if dir > 0 then LDfirst else LDlast
897 findlink opaque ld
899 match link with
900 | Lnotfound -> loop rest
901 | Lfound n ->
902 showlinktype (getlink opaque n);
903 Ltexact (l.pageno, n)
905 loop state.layout
907 state.mode <- LinkNav linknav
908 | Ltexact _ -> ()
910 | Textentry _ | View -> ()
911 end;
912 preload layout;
914 state.ghyll <- noghyll;
915 if conf.updatecurs
916 then (
917 let mx, my = state.mpos in
918 updateunder mx my;
922 let conttiling pageno opaque =
923 tilepage pageno opaque
924 (if conf.preload
925 then preloadlayout state.x state.y state.winw state.winh
926 else state.layout)
929 let gotoxy_and_clear_text x y =
930 if not conf.verbose then state.text <- E.s;
931 gotoxy x y;
934 let getanchory (n, top, dtop) =
935 let y, h = getpageyh n in
936 if conf.presentation
937 then
938 let ips = calcips h in
939 y + truncate (top*.float h -. dtop*.float ips) + ips;
940 else
941 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
944 let gotoanchor anchor =
945 gotoxy state.x (getanchory anchor);
948 let addnav () =
949 getanchor () |> cbput state.hists.nav;
952 let addnavnorc () =
953 getanchor () |> cbput_dont_update_rc state.hists.nav;
956 let getnav dir =
957 let anchor = cbgetc state.hists.nav dir in
958 getanchory anchor;
961 let gotoghyll1 single y =
962 let scroll f n a b =
963 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
964 let snake f a b =
965 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
966 if f < a
967 then s (float f /. float a)
968 else (
969 if f > b
970 then 1.0 -. s ((float (f-b) /. float (n-b)))
971 else 1.0
974 snake f a b
975 and summa n a b =
976 let ins = float a *. 0.5
977 and outs = float (n-b) *. 0.5 in
978 let ones = b - a in
979 ins +. outs +. float ones
981 let rec set nab y sy =
982 let (_N, _A, _B), y =
983 if single
984 then
985 let scl = if y > sy then 2 else -2 in
986 let _N, _, _ = nab in
987 (_N,0,_N), y+conf.scrollstep*scl
988 else nab,y in
989 let sum = summa _N _A _B in
990 let dy = float (y - sy) in
991 state.ghyll <- (
992 let rec gf n y1 o =
993 if n >= _N
994 then state.ghyll <- noghyll
995 else
996 let go n =
997 let s = scroll n _N _A _B in
998 let y1 = y1 +. ((s *. dy) /. sum) in
999 gotoxy_and_clear_text state.x (truncate y1);
1000 state.ghyll <- gf (n+1) y1;
1002 match o with
1003 | None -> go n
1004 | Some y' when single -> set nab y' state.y
1005 | Some y' -> set (_N/2, 1, 1) y' state.y
1007 gf 0 (float state.y)
1010 match conf.ghyllscroll with
1011 | Some nab when not conf.presentation ->
1012 if state.ghyll == noghyll
1013 then set nab y state.y
1014 else state.ghyll (Some y)
1015 | _ ->
1016 gotoxy_and_clear_text state.x y
1019 let gotoghyll = gotoghyll1 false;;
1021 let gotopage n top =
1022 let y, h = getpageyh n in
1023 let y = y + (truncate (top *. float h)) in
1024 gotoghyll y
1027 let gotopage1 n top =
1028 let y = getpagey n in
1029 let y = y + top in
1030 gotoghyll y
1033 let invalidate s f =
1034 state.redisplay <- false;
1035 state.layout <- [];
1036 state.pdims <- [];
1037 state.rects <- [];
1038 state.rects1 <- [];
1039 match state.geomcmds with
1040 | ps, [] when emptystr ps ->
1041 f ();
1042 state.geomcmds <- s, [];
1044 | ps, [] ->
1045 state.geomcmds <- ps, [s, f];
1047 | ps, (s', _) :: rest when s' = s ->
1048 state.geomcmds <- ps, ((s, f) :: rest);
1050 | ps, cmds ->
1051 state.geomcmds <- ps, ((s, f) :: cmds);
1054 let flushpages () =
1055 Hashtbl.iter (fun _ opaque ->
1056 wcmd "freepage %s" (~> opaque);
1057 ) state.pagemap;
1058 Hashtbl.clear state.pagemap;
1061 let flushtiles () =
1062 if not (Queue.is_empty state.tilelru)
1063 then (
1064 Queue.iter (fun (k, p, s) ->
1065 wcmd "freetile %s" (~> p);
1066 state.memused <- state.memused - s;
1067 Hashtbl.remove state.tilemap k;
1068 ) state.tilelru;
1069 state.uioh#infochanged Memused;
1070 Queue.clear state.tilelru;
1072 load state.layout;
1075 let stateh h =
1076 let h = truncate (float h*.conf.zoom) in
1077 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1078 h - d
1081 let fillhelp () =
1082 state.help <-
1083 let sl = keystostrlist conf in
1084 let rec loop accu =
1085 function | [] -> accu
1086 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1087 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1090 let opendoc path password =
1091 state.path <- path;
1092 state.password <- password;
1093 state.gen <- state.gen + 1;
1094 state.docinfo <- [];
1095 state.outlines <- [||];
1097 flushpages ();
1098 setaalevel conf.aalevel;
1099 let titlepath =
1100 if emptystr state.origin
1101 then path
1102 else state.origin
1104 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1105 wcmd "open %d %d %s\000%s\000%s\000"
1106 (btod !cxack) (btod conf.usedoccss)
1107 path password conf.css;
1108 invalidate "reqlayout"
1109 (fun () ->
1110 wcmd "reqlayout %d %d %d %s\000"
1111 conf.angle (FMTE.to_int conf.fitmodel)
1112 (stateh state.winh) state.nameddest
1114 fillhelp ();
1117 let reload () =
1118 state.anchor <- getanchor ();
1119 opendoc state.path state.password;
1122 let scalecolor c =
1123 let c = c *. conf.colorscale in
1124 (c, c, c);
1127 let scalecolor2 (r, g, b) =
1128 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1131 let docolumns columns =
1132 match columns with
1133 | Csingle _ ->
1134 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1135 let rec loop pageno pdimno pdim y ph pdims =
1136 if pageno = state.pagecount
1137 then ()
1138 else
1139 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1140 match pdims with
1141 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1142 pdimno+1, pdim, rest
1143 | _ ->
1144 pdimno, pdim, pdims
1146 let x = max 0 (((state.winw - w) / 2) - xoff) in
1147 let y =
1148 y + (if conf.presentation
1149 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1150 else (if pageno = 0 then 0 else conf.interpagespace)
1153 a.(pageno) <- (pdimno, x, y, pdim);
1154 loop (pageno+1) pdimno pdim (y + h) h pdims
1156 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1157 conf.columns <- Csingle a;
1159 | Cmulti ((columns, coverA, coverB), _) ->
1160 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1161 let rec loop pageno pdimno pdim x y rowh pdims =
1162 let rec fixrow m =
1163 if m = pageno then () else
1164 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1165 if h < rowh
1166 then (
1167 let y = y + (rowh - h) / 2 in
1168 a.(m) <- (pdimno, x, y, pdim);
1170 fixrow (m+1)
1172 if pageno = state.pagecount
1173 then fixrow (((pageno - 1) / columns) * columns)
1174 else
1175 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1176 match pdims with
1177 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1178 pdimno+1, pdim, rest
1179 | _ ->
1180 pdimno, pdim, pdims
1182 let x, y, rowh' =
1183 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1184 then (
1185 let x = (state.winw - w) / 2 in
1186 let ips =
1187 if conf.presentation then calcips h else conf.interpagespace in
1188 x, y + ips + rowh, h
1190 else (
1191 if (pageno - coverA) mod columns = 0
1192 then (
1193 let x = max 0 (state.winw - state.w) / 2 in
1194 let y =
1195 if conf.presentation
1196 then
1197 let ips = calcips h in
1198 y + (if pageno = 0 then 0 else calcips rowh + ips)
1199 else
1200 y + (if pageno = 0 then 0 else conf.interpagespace)
1202 x, y + rowh, h
1204 else x, y, max rowh h
1207 let y =
1208 if pageno > 1 && (pageno - coverA) mod columns = 0
1209 then (
1210 let y =
1211 if pageno = columns && conf.presentation
1212 then (
1213 let ips = calcips rowh in
1214 for i = 0 to pred columns
1216 let (pdimno, x, y, pdim) = a.(i) in
1217 a.(i) <- (pdimno, x, y+ips, pdim)
1218 done;
1219 y+ips;
1221 else y
1223 fixrow (pageno - columns);
1226 else y
1228 a.(pageno) <- (pdimno, x, y, pdim);
1229 let x = x + w + xoff*2 + conf.interpagespace in
1230 loop (pageno+1) pdimno pdim x y rowh' pdims
1232 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1233 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1235 | Csplit (c, _) ->
1236 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1237 let rec loop pageno pdimno pdim y pdims =
1238 if pageno = state.pagecount
1239 then ()
1240 else
1241 let pdimno, ((_, w, h, _) as pdim), pdims =
1242 match pdims with
1243 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1244 pdimno+1, pdim, rest
1245 | _ ->
1246 pdimno, pdim, pdims
1248 let cw = w / c in
1249 let rec loop1 n x y =
1250 if n = c then y else (
1251 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1252 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1255 let y = loop1 0 0 y in
1256 loop (pageno+1) pdimno pdim y pdims
1258 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1259 conf.columns <- Csplit (c, a);
1262 let represent () =
1263 docolumns conf.columns;
1264 state.maxy <- calcheight ();
1265 if state.reprf == noreprf
1266 then (
1267 match state.mode with
1268 | Birdseye (_, _, pageno, _, _) ->
1269 let y, h = getpageyh pageno in
1270 let top = (state.winh - h) / 2 in
1271 gotoxy state.x (max 0 (y - top))
1272 | Textentry _ | View | LinkNav _ ->
1273 let y = getanchory state.anchor in
1274 let y = min y (state.maxy - state.winh) in
1275 gotoxy state.x y;
1277 else (
1278 state.reprf ();
1279 state.reprf <- noreprf;
1283 let reshape ?(firsttime=false) w h =
1284 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1285 if not firsttime && nogeomcmds state.geomcmds
1286 then state.anchor <- getanchor ();
1288 state.winw <- w;
1289 let w = truncate (float w *. conf.zoom) in
1290 let w = max w 2 in
1291 state.winh <- h;
1292 setfontsize fstate.fontsize;
1293 GlMat.mode `modelview;
1294 GlMat.load_identity ();
1296 GlMat.mode `projection;
1297 GlMat.load_identity ();
1298 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1299 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1300 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1302 let relx =
1303 if conf.zoom <= 1.0
1304 then 0.0
1305 else float state.x /. float state.w
1307 invalidate "geometry"
1308 (fun () ->
1309 state.w <- w;
1310 if not firsttime
1311 then state.x <- truncate (relx *. float w);
1312 let w =
1313 match conf.columns with
1314 | Csingle _ -> w
1315 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1316 | Csplit (c, _) -> w * c
1318 wcmd "geometry %d %d %d"
1319 w (stateh h) (FMTE.to_int conf.fitmodel)
1323 let enttext () =
1324 let len = String.length state.text in
1325 let x0 = if conf.leftscroll then vscrollw () else 0 in
1326 let drawstring s =
1327 let hscrollh =
1328 match state.mode with
1329 | Textentry _ | View | LinkNav _ ->
1330 let h, _, _ = state.uioh#scrollpw in
1332 | Birdseye _ -> 0
1334 let rect x w =
1335 filledrect
1336 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1337 (x+.w) (float (state.winh - hscrollh))
1340 let w = float (state.winw - 1 - vscrollw ()) in
1341 if state.progress >= 0.0 && state.progress < 1.0
1342 then (
1343 GlDraw.color (0.3, 0.3, 0.3);
1344 let w1 = w *. state.progress in
1345 rect (float x0) w1;
1346 GlDraw.color (0.0, 0.0, 0.0);
1347 rect (float x0+.w1) (float x0+.w-.w1)
1349 else (
1350 GlDraw.color (0.0, 0.0, 0.0);
1351 rect (float x0) w;
1354 GlDraw.color (1.0, 1.0, 1.0);
1355 drawstring
1356 fstate.fontsize
1357 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1358 (state.winh - hscrollh - 5) s;
1360 let s =
1361 match state.mode with
1362 | Textentry ((prefix, text, _, _, _, _), _) ->
1363 let s =
1364 if len > 0
1365 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1366 else Printf.sprintf "%s%s_" prefix text
1370 | Birdseye _ | View | LinkNav _ -> state.text
1372 let s =
1373 if state.newerrmsgs
1374 then (
1375 if not (istextentry state.mode) && state.uioh#eformsgs
1376 then
1377 let s1 = "(press 'e' to review error messasges)" in
1378 if nonemptystr s then s ^ " " ^ s1 else s1
1379 else s
1381 else s
1383 if nonemptystr s
1384 then drawstring s
1387 let gctiles () =
1388 let len = Queue.length state.tilelru in
1389 let layout = lazy (
1390 match state.throttle with
1391 | None ->
1392 if conf.preload
1393 then preloadlayout state.x state.y state.winw state.winh
1394 else state.layout
1395 | Some (layout, _, _) ->
1396 layout
1397 ) in
1398 let rec loop qpos =
1399 if state.memused > conf.memlimit
1400 then (
1401 if qpos < len
1402 then
1403 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1404 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1405 let (_, pw, ph, _) = getpagedim n in
1406 if gen = state.gen
1407 && colorspace = conf.colorspace
1408 && angle = conf.angle
1409 && pagew = pw
1410 && pageh = ph
1411 && (
1412 let x = col*conf.tilew
1413 and y = row*conf.tileh in
1414 tilevisible (Lazy.force_val layout) n x y
1416 then Queue.push lruitem state.tilelru
1417 else (
1418 freepbo p;
1419 wcmd "freetile %s" (~> p);
1420 state.memused <- state.memused - s;
1421 state.uioh#infochanged Memused;
1422 Hashtbl.remove state.tilemap k;
1424 loop (qpos+1)
1427 loop 0
1430 let onpagerect pageno f =
1431 let b =
1432 match conf.columns with
1433 | Cmulti (_, b) -> b
1434 | Csingle b -> b
1435 | Csplit (_, b) -> b
1437 if pageno >= 0 && pageno < Array.length b
1438 then
1439 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1440 f w h
1443 let gotopagexy1 pageno x y =
1444 let _,w1,h1,leftx = getpagedim pageno in
1445 let top = y /. (float h1) in
1446 let left = x /. (float w1) in
1447 let py, w, h = getpageywh pageno in
1448 let wh = state.winh in
1449 let x = left *. (float w) in
1450 let x = leftx + state.x + truncate x in
1451 let sx =
1452 if x < 0 || x >= state.winw
1453 then state.x - x
1454 else state.x
1456 let pdy = truncate (top *. float h) in
1457 let y' = py + pdy in
1458 let dy = y' - state.y in
1459 let sy =
1460 if x != state.x || not (dy > 0 && dy < wh)
1461 then (
1462 if conf.presentation
1463 then
1464 if abs (py - y') > wh
1465 then y'
1466 else py
1467 else y';
1469 else state.y
1471 if state.x != sx || state.y != sy
1472 then gotoxy_and_clear_text sx sy
1473 else gotoxy_and_clear_text state.x state.y;
1476 let gotopagexy pageno x y =
1477 match state.mode with
1478 | Birdseye _ -> gotopage pageno 0.0
1479 | Textentry _ | View | LinkNav _ -> gotopagexy1 pageno x y
1482 let getpassword () =
1483 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1484 if emptystr passcmd
1485 then E.s
1486 else getcmdoutput
1487 (fun s ->
1488 impmsg "error getting password: %s" s;
1489 dolog "%s" s) passcmd;
1492 let pgoto opaque pageno x y =
1493 let pdimno = getpdimno pageno in
1494 let x, y = project opaque pageno pdimno x y in
1495 gotopagexy pageno x y;
1498 let act cmds =
1499 (* dolog "%S" cmds; *)
1500 let spl = splitatchar cmds ' ' in
1501 let scan s fmt f =
1502 try Scanf.sscanf s fmt f
1503 with exn ->
1504 dolog "error processing '%S': %s" cmds @@ exntos exn;
1505 exit 1
1507 let addoutline outline =
1508 match state.currently with
1509 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1510 | Idle -> state.currently <- Outlining [outline]
1511 | Loading _ | Tiling _ ->
1512 dolog "invalid outlining state";
1513 logcurrently state.currently
1515 match spl with
1516 | "clear", "" ->
1517 state.pdims <- [];
1518 state.uioh#infochanged Pdim;
1520 | "clearrects", "" ->
1521 state.rects <- state.rects1;
1522 G.postRedisplay "clearrects";
1524 | "continue", args ->
1525 let n = scan args "%u" (fun n -> n) in
1526 state.pagecount <- n;
1527 begin match state.currently with
1528 | Outlining l ->
1529 state.currently <- Idle;
1530 state.outlines <- Array.of_list (List.rev l)
1531 | Idle | Loading _ | Tiling _ -> ()
1532 end;
1534 let cur, cmds = state.geomcmds in
1535 if emptystr cur
1536 then failwith "umpossible";
1538 begin match List.rev cmds with
1539 | [] ->
1540 state.geomcmds <- E.s, [];
1541 state.throttle <- None;
1542 represent ();
1543 | (s, f) :: rest ->
1544 f ();
1545 state.geomcmds <- s, List.rev rest;
1546 end;
1547 if conf.maxwait = None
1548 then G.postRedisplay "continue";
1550 | "msg", args ->
1551 showtext ' ' args
1553 | "vmsg", args ->
1554 if conf.verbose
1555 then showtext ' ' args
1557 | "emsg", args ->
1558 Buffer.add_string state.errmsgs args;
1559 state.newerrmsgs <- true;
1560 G.postRedisplay "error message"
1562 | "progress", args ->
1563 let progress, text =
1564 scan args "%f %n"
1565 (fun f pos ->
1566 f, String.sub args pos (String.length args - pos))
1568 state.text <- text;
1569 state.progress <- progress;
1570 G.postRedisplay "progress"
1572 | "firstmatch", args ->
1573 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1574 scan args "%u %d %f %f %f %f %f %f %f %f"
1575 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1576 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1578 let y = (getpagey pageno) + truncate y0 in
1579 let x =
1580 if (state.x < - truncate x0) || (state.x > state.winw - truncate x1)
1581 then state.winw/2 - truncate (x0 /. 2. +. x1 /. 2.)
1582 else state.x
1584 addnav ();
1585 gotoxy x y;
1586 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1587 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1589 | "match", 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 color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1596 state.rects1 <-
1597 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1599 | "page", args ->
1600 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1601 let pageopaque = ~< pageopaques in
1602 begin match state.currently with
1603 | Loading (l, gen) ->
1604 vlog "page %d took %f sec" l.pageno t;
1605 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1606 begin match state.throttle with
1607 | None ->
1608 let preloadedpages =
1609 if conf.preload
1610 then preloadlayout state.x state.y state.winw state.winh
1611 else state.layout
1613 let evict () =
1614 let set =
1615 List.fold_left (fun s l -> IntSet.add l.pageno s)
1616 IntSet.empty preloadedpages
1618 let evictedpages =
1619 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1620 if not (IntSet.mem pageno set)
1621 then (
1622 wcmd "freepage %s" (~> opaque);
1623 key :: accu
1625 else accu
1626 ) state.pagemap []
1628 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1630 evict ();
1631 state.currently <- Idle;
1632 if gen = state.gen
1633 then (
1634 tilepage l.pageno pageopaque state.layout;
1635 load state.layout;
1636 load preloadedpages;
1637 let visible = pagevisible state.layout l.pageno in
1638 if visible
1639 then (
1640 match state.mode with
1641 | LinkNav (Ltnotready (pageno, dir)) ->
1642 if pageno = l.pageno
1643 then (
1644 let link =
1645 let ld =
1646 if dir = 0
1647 then LDfirstvisible (l.pagex, l.pagey, dir)
1648 else (
1649 if dir > 0 then LDfirst else LDlast
1652 findlink pageopaque ld
1654 match link with
1655 | Lnotfound -> ()
1656 | Lfound n ->
1657 showlinktype (getlink pageopaque n);
1658 state.mode <- LinkNav (Ltexact (l.pageno, n))
1660 | LinkNav (Ltgendir _)
1661 | LinkNav (Ltexact _)
1662 | View
1663 | Birdseye _
1664 | Textentry _ -> ()
1667 if visible && layoutready state.layout
1668 then (
1669 G.postRedisplay "page";
1673 | Some (layout, _, _) ->
1674 state.currently <- Idle;
1675 tilepage l.pageno pageopaque layout;
1676 load state.layout
1677 end;
1679 | Idle | Tiling _ | Outlining _ ->
1680 dolog "Inconsistent loading state";
1681 logcurrently state.currently;
1682 exit 1
1685 | "tile" , args ->
1686 let (x, y, opaques, size, t) =
1687 scan args "%u %u %s %u %f"
1688 (fun x y p size t -> (x, y, p, size, t))
1690 let opaque = ~< opaques in
1691 begin match state.currently with
1692 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1693 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1695 unmappbo opaque;
1696 if tilew != conf.tilew || tileh != conf.tileh
1697 then (
1698 wcmd "freetile %s" (~> opaque);
1699 state.currently <- Idle;
1700 load state.layout;
1702 else (
1703 puttileopaque l col row gen cs angle opaque size t;
1704 state.memused <- state.memused + size;
1705 state.uioh#infochanged Memused;
1706 gctiles ();
1707 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1708 opaque, size) state.tilelru;
1710 let layout =
1711 match state.throttle with
1712 | None -> state.layout
1713 | Some (layout, _, _) -> layout
1716 state.currently <- Idle;
1717 if gen = state.gen
1718 && conf.colorspace = cs
1719 && conf.angle = angle
1720 && tilevisible layout l.pageno x y
1721 then conttiling l.pageno pageopaque;
1723 begin match state.throttle with
1724 | None ->
1725 preload state.layout;
1726 if gen = state.gen
1727 && conf.colorspace = cs
1728 && conf.angle = angle
1729 && tilevisible state.layout l.pageno x y
1730 && layoutready state.layout
1731 then G.postRedisplay "tile nothrottle";
1733 | Some (layout, y, _) ->
1734 let ready = layoutready layout in
1735 if ready
1736 then (
1737 state.y <- y;
1738 state.layout <- layout;
1739 state.throttle <- None;
1740 G.postRedisplay "throttle";
1742 else load layout;
1743 end;
1746 | Idle | Loading _ | Outlining _ ->
1747 dolog "Inconsistent tiling state";
1748 logcurrently state.currently;
1749 exit 1
1752 | "pdim", args ->
1753 let (n, w, h, _) as pdim =
1754 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1756 let pdim =
1757 match conf.fitmodel with
1758 | FitWidth -> pdim
1759 | FitPage | FitProportional ->
1760 match conf.columns with
1761 | Csplit _ -> (n, w, h, 0)
1762 | Csingle _ | Cmulti _ -> pdim
1764 state.pdims <- pdim :: state.pdims;
1765 state.uioh#infochanged Pdim
1767 | "o", args ->
1768 let (l, n, t, h, pos) =
1769 scan args "%u %u %d %u %n"
1770 (fun l n t h pos -> l, n, t, h, pos)
1772 let s = String.sub args pos (String.length args - pos) in
1773 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1775 | "ou", args ->
1776 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1777 let s = String.sub args pos len in
1778 let pos2 = pos + len + 1 in
1779 let uri = String.sub args pos2 (String.length args - pos2) in
1780 addoutline (s, l, Ouri uri)
1782 | "on", args ->
1783 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1784 let s = String.sub args pos (String.length args - pos) in
1785 addoutline (s, l, Onone)
1787 | "a", args ->
1788 let (n, l, t) =
1789 scan args "%u %d %d" (fun n l t -> n, l, t)
1791 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1793 | "info", args ->
1794 let c, v = splitatchar args '\t' in
1795 let s =
1796 if nonemptystr v
1797 then
1798 if c = "Title"
1799 then (
1800 conf.title <- v;
1801 if not !ignoredoctitlte
1802 then Wsi.settitle v;
1803 args
1805 else
1806 if let len = String.length c in
1807 len > 6 && ((String.sub c (len-4) 4) = "date")
1808 then (
1809 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1810 then
1811 let b = Buffer.create 10 in
1812 Printf.bprintf b "%s\t" c;
1813 let sub p l c =
1815 Buffer.add_substring b v p l;
1816 Buffer.add_char b c;
1817 with exn -> Buffer.add_string b @@ exntos exn
1819 sub 2 4 '/';
1820 sub 6 2 '/';
1821 sub 8 2 ' ';
1822 sub 10 2 ':';
1823 sub 12 2 ':';
1824 sub 14 2 ' ';
1825 Buffer.add_char b '[';
1826 Buffer.add_string b v;
1827 Buffer.add_char b ']';
1828 Buffer.contents b
1829 else args
1831 else args
1832 else args
1834 state.docinfo <- (1, s) :: state.docinfo
1836 | "infoend", "" ->
1837 state.docinfo <- List.rev state.docinfo;
1838 state.uioh#infochanged Docinfo
1840 | "pass", args ->
1841 if args = "fail"
1842 then Wsi.settitle "Wrong password";
1843 let password = getpassword () in
1844 if emptystr password
1845 then error "document is password protected"
1846 else opendoc state.path password
1848 | _ ->
1849 error "unknown cmd `%S'" cmds
1852 let onhist cb =
1853 let rc = cb.rc in
1854 let action = function
1855 | HCprev -> cbget cb ~-1
1856 | HCnext -> cbget cb 1
1857 | HCfirst -> cbget cb ~-(cb.rc)
1858 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1859 and cancel () = cb.rc <- rc
1860 in (action, cancel)
1863 let search pattern forward =
1864 match conf.columns with
1865 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1866 | Csingle _ | Cmulti _ ->
1867 if nonemptystr pattern
1868 then
1869 let pn, py =
1870 match state.layout with
1871 | [] -> 0, 0
1872 | l :: _ ->
1873 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1875 wcmd "search %d %d %d %d,%s\000"
1876 (btod conf.icase) pn py (btod forward) pattern;
1879 let [@warning "-4"] intentry text key =
1880 let text =
1881 if emptystr text && key = Keys.Ascii '-'
1882 then addchar text '-'
1883 else
1884 match key with
1885 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1886 | _ ->
1887 state.text <- "invalid key";
1888 text
1890 TEcont text
1893 let linknact f s =
1894 if nonemptystr s
1895 then (
1896 let n =
1897 let l = String.length s in
1898 let rec loop pos n =
1899 if pos = l
1900 then n
1901 else
1902 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1903 loop (pos+1) (n*26 + m)
1904 in loop 0 0
1906 let rec loop n = function
1907 | [] -> ()
1908 | l :: rest ->
1909 match getopaque l.pageno with
1910 | None -> loop n rest
1911 | Some opaque ->
1912 let m = getlinkcount opaque in
1913 if n < m
1914 then (
1915 let under = getlink opaque n in
1916 f under
1918 else loop (n-m) rest
1920 loop n state.layout;
1924 let [@warning "-4"] linknentry text = function
1925 | Keys.Ascii c ->
1926 let text = addchar text c in
1927 linknact (fun under -> state.text <- undertext under) text;
1928 TEcont text
1929 | _ ->
1930 state.text <- Printf.sprintf "invalid key";
1931 TEcont text
1934 let [@warning "-4"] textentry text = function
1935 | Keys.Ascii c -> TEcont (addchar text c)
1936 | Keys.Code c -> TEcont (text ^ toutf8 c)
1937 | _ -> TEcont text
1940 let reqlayout angle fitmodel =
1941 match state.throttle with
1942 | None ->
1943 if nogeomcmds state.geomcmds
1944 then state.anchor <- getanchor ();
1945 conf.angle <- angle mod 360;
1946 if conf.angle != 0
1947 then (
1948 match state.mode with
1949 | LinkNav _ -> state.mode <- View
1950 | Birdseye _ | Textentry _ | View -> ()
1952 conf.fitmodel <- fitmodel;
1953 invalidate
1954 "reqlayout"
1955 (fun () ->
1956 wcmd "reqlayout %d %d %d"
1957 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1959 | _ -> ()
1962 let settrim trimmargins trimfuzz =
1963 if nogeomcmds state.geomcmds
1964 then state.anchor <- getanchor ();
1965 conf.trimmargins <- trimmargins;
1966 conf.trimfuzz <- trimfuzz;
1967 let x0, y0, x1, y1 = trimfuzz in
1968 invalidate
1969 "settrim" (fun () ->
1970 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1971 flushpages ();
1974 let setzoom zoom =
1975 match state.throttle with
1976 | None ->
1977 let zoom = max 0.0001 zoom in
1978 if zoom <> conf.zoom
1979 then (
1980 state.prevzoom <- (conf.zoom, state.x);
1981 conf.zoom <- zoom;
1982 reshape state.winw state.winh;
1983 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
1986 | Some (layout, y, started) ->
1987 let time =
1988 match conf.maxwait with
1989 | None -> 0.0
1990 | Some t -> t
1992 let dt = now () -. started in
1993 if dt > time
1994 then (
1995 state.y <- y;
1996 load layout;
2000 let pivotzoom ?(vw=min state.w state.winw)
2001 ?(vh=min (state.maxy-state.y) state.winh)
2002 ?(x=vw/2) ?(y=vh/2) zoom =
2003 let w = float state.w /. zoom in
2004 let hw = w /. 2.0 in
2005 let ratio = float vh /. float vw in
2006 let hh = hw *. ratio in
2007 let x0 = float x -. hw
2008 and y0 = float y -. hh in
2009 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2010 setzoom zoom;
2013 let pivotzoom ?vw ?vh ?x ?y zoom =
2014 if nogeomcmds state.geomcmds
2015 then
2016 if zoom > 1.0
2017 then pivotzoom ?vw ?vh ?x ?y zoom
2018 else setzoom zoom
2021 let setcolumns mode columns coverA coverB =
2022 state.prevcolumns <- Some (conf.columns, conf.zoom);
2023 if columns < 0
2024 then (
2025 if isbirdseye mode
2026 then impmsg "split mode doesn't work in bird's eye"
2027 else (
2028 conf.columns <- Csplit (-columns, E.a);
2029 state.x <- 0;
2030 conf.zoom <- 1.0;
2033 else (
2034 if columns < 2
2035 then (
2036 conf.columns <- Csingle E.a;
2037 state.x <- 0;
2038 setzoom 1.0;
2040 else (
2041 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2042 conf.zoom <- 1.0;
2045 reshape state.winw state.winh;
2048 let resetmstate () =
2049 state.mstate <- Mnone;
2050 Wsi.setcursor Wsi.CURSOR_INHERIT;
2053 let enterbirdseye () =
2054 let zoom = float conf.thumbw /. float state.winw in
2055 let birdseyepageno =
2056 let cy = state.winh / 2 in
2057 let fold = function
2058 | [] -> 0
2059 | l :: rest ->
2060 let rec fold best = function
2061 | [] -> best.pageno
2062 | l :: rest ->
2063 let d = cy - (l.pagedispy + l.pagevh/2)
2064 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2065 if abs d < abs dbest
2066 then fold l rest
2067 else best.pageno
2068 in fold l rest
2070 fold state.layout
2072 state.mode <-
2073 Birdseye (
2074 { conf with zoom = conf.zoom },
2075 state.x, birdseyepageno, -1, getanchor ()
2077 resetmstate ();
2078 conf.zoom <- zoom;
2079 conf.presentation <- false;
2080 conf.interpagespace <- 10;
2081 conf.hlinks <- false;
2082 conf.fitmodel <- FitPage;
2083 state.x <- 0;
2084 conf.maxwait <- None;
2085 conf.columns <- (
2086 match conf.beyecolumns with
2087 | Some c ->
2088 conf.zoom <- 1.0;
2089 Cmulti ((c, 0, 0), E.a)
2090 | None -> Csingle E.a
2092 if conf.verbose
2093 then
2094 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2095 (100.0*.zoom)
2096 else
2097 state.text <- E.s
2099 reshape state.winw state.winh;
2102 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2103 state.mode <- View;
2104 conf.zoom <- c.zoom;
2105 conf.presentation <- c.presentation;
2106 conf.interpagespace <- c.interpagespace;
2107 conf.maxwait <- c.maxwait;
2108 conf.hlinks <- c.hlinks;
2109 conf.fitmodel <- c.fitmodel;
2110 conf.beyecolumns <- (
2111 match conf.columns with
2112 | Cmulti ((c, _, _), _) -> Some c
2113 | Csingle _ -> None
2114 | Csplit _ -> failwith "leaving bird's eye split mode"
2116 conf.columns <- (
2117 match c.columns with
2118 | Cmulti (c, _) -> Cmulti (c, E.a)
2119 | Csingle _ -> Csingle E.a
2120 | Csplit (c, _) -> Csplit (c, E.a)
2122 if conf.verbose
2123 then
2124 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2125 (100.0*.conf.zoom)
2127 reshape state.winw state.winh;
2128 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2129 state.x <- leftx;
2132 let togglebirdseye () =
2133 match state.mode with
2134 | Birdseye vals -> leavebirdseye vals true
2135 | View -> enterbirdseye ()
2136 | Textentry _ | LinkNav _ -> ()
2139 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2140 let pageno = max 0 (pageno - incr) in
2141 let rec loop = function
2142 | [] -> gotopage1 pageno 0
2143 | l :: _ when l.pageno = pageno ->
2144 if l.pagedispy >= 0 && l.pagey = 0
2145 then G.postRedisplay "upbirdseye"
2146 else gotopage1 pageno 0
2147 | _ :: rest -> loop rest
2149 loop state.layout;
2150 state.text <- E.s;
2151 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2154 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2155 let pageno = min (state.pagecount - 1) (pageno + incr) in
2156 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2157 let rec loop = function
2158 | [] ->
2159 let y, h = getpageyh pageno in
2160 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2161 gotoxy state.x (clamp dy)
2162 | l :: _ when l.pageno = pageno ->
2163 if l.pagevh != l.pageh
2164 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2165 else G.postRedisplay "downbirdseye"
2166 | _ :: rest -> loop rest
2168 loop state.layout;
2169 state.text <- E.s;
2172 let [@warning "-4"] optentry mode _ key =
2173 let btos b = if b then "on" else "off" in
2174 match key with
2175 | Keys.Ascii 's' ->
2176 let ondone s =
2177 try conf.scrollstep <- int_of_string s with exn ->
2178 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2180 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2182 | Keys.Ascii 'A' ->
2183 let ondone s =
2185 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2186 if state.autoscroll <> None
2187 then state.autoscroll <- Some conf.autoscrollstep
2188 with exn ->
2189 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2191 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2193 | Keys.Ascii 'C' ->
2194 let ondone s =
2196 let n, a, b = multicolumns_of_string s in
2197 setcolumns mode n a b;
2198 with exn ->
2199 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2201 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2203 | Keys.Ascii 'Z' ->
2204 let ondone s =
2206 let zoom = float (int_of_string s) /. 100.0 in
2207 pivotzoom zoom
2208 with exn ->
2209 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2211 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2213 | Keys.Ascii 't' ->
2214 let ondone s =
2216 conf.thumbw <- bound (int_of_string s) 2 4096;
2217 state.text <-
2218 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2219 begin match mode with
2220 | Birdseye beye ->
2221 leavebirdseye beye false;
2222 enterbirdseye ();
2223 | Textentry _ | View | LinkNav _ -> ();
2225 with exn ->
2226 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2228 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2230 | Keys.Ascii 'R' ->
2231 let ondone s =
2232 match int_of_string s with
2233 | angle -> reqlayout angle conf.fitmodel
2234 | exception exn ->
2235 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2237 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2239 | Keys.Ascii 'i' ->
2240 conf.icase <- not conf.icase;
2241 TEdone ("case insensitive search " ^ (btos conf.icase))
2243 | Keys.Ascii 'p' ->
2244 conf.preload <- not conf.preload;
2245 gotoxy state.x state.y;
2246 TEdone ("preload " ^ (btos conf.preload))
2248 | Keys.Ascii 'v' ->
2249 conf.verbose <- not conf.verbose;
2250 TEdone ("verbose " ^ (btos conf.verbose))
2252 | Keys.Ascii 'd' ->
2253 conf.debug <- not conf.debug;
2254 TEdone ("debug " ^ (btos conf.debug))
2256 | Keys.Ascii 'h' ->
2257 conf.maxhfit <- not conf.maxhfit;
2258 state.maxy <- calcheight ();
2259 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2261 | Keys.Ascii 'c' ->
2262 conf.crophack <- not conf.crophack;
2263 TEdone ("crophack " ^ btos conf.crophack)
2265 | Keys.Ascii 'a' ->
2266 let s =
2267 match conf.maxwait with
2268 | None ->
2269 conf.maxwait <- Some infinity;
2270 "always wait for page to complete"
2271 | Some _ ->
2272 conf.maxwait <- None;
2273 "show placeholder if page is not ready"
2275 TEdone s
2277 | Keys.Ascii 'f' ->
2278 conf.underinfo <- not conf.underinfo;
2279 TEdone ("underinfo " ^ btos conf.underinfo)
2281 | Keys.Ascii 'P' ->
2282 conf.savebmarks <- not conf.savebmarks;
2283 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2285 | Keys.Ascii 'S' ->
2286 let ondone s =
2288 let pageno, py =
2289 match state.layout with
2290 | [] -> 0, 0
2291 | l :: _ ->
2292 l.pageno, l.pagey
2294 conf.interpagespace <- int_of_string s;
2295 docolumns conf.columns;
2296 state.maxy <- calcheight ();
2297 let y = getpagey pageno in
2298 gotoxy state.x (y + py)
2299 with exn ->
2300 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2302 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2304 | Keys.Ascii 'l' ->
2305 let fm =
2306 match conf.fitmodel with
2307 | FitProportional -> FitWidth
2308 | FitWidth | FitPage -> FitProportional
2310 reqlayout conf.angle fm;
2311 TEdone ("proportional display " ^ btos (fm == FitProportional))
2313 | Keys.Ascii 'T' ->
2314 settrim (not conf.trimmargins) conf.trimfuzz;
2315 TEdone ("trim margins " ^ btos conf.trimmargins)
2317 | Keys.Ascii 'I' ->
2318 conf.invert <- not conf.invert;
2319 TEdone ("invert colors " ^ btos conf.invert)
2321 | Keys.Ascii 'x' ->
2322 let ondone s =
2323 cbput state.hists.sel s;
2324 conf.selcmd <- s;
2326 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2327 textentry, ondone, true)
2329 | Keys.Ascii 'M' ->
2330 if conf.pax == None
2331 then conf.pax <- Some 0.0
2332 else conf.pax <- None;
2333 TEdone ("PAX " ^ btos (conf.pax != None))
2335 | (Keys.Ascii c) ->
2336 state.text <- Printf.sprintf "bad option %d `%c'"
2337 (Char.code c) c;
2338 TEstop
2340 | _ ->
2341 TEcont state.text
2344 class type lvsource =
2345 object
2346 method getitemcount : int
2347 method getitem : int -> (string * int)
2348 method hasaction : int -> bool
2349 method exit : uioh:uioh ->
2350 cancel:bool ->
2351 active:int ->
2352 first:int ->
2353 pan:int ->
2354 uioh option
2355 method getactive : int
2356 method getfirst : int
2357 method getpan : int
2358 method getminfo : (int * int) array
2359 end;;
2361 class virtual lvsourcebase = object
2362 val mutable m_active = 0
2363 val mutable m_first = 0
2364 val mutable m_pan = 0
2365 method getactive = m_active
2366 method getfirst = m_first
2367 method getpan = m_pan
2368 method getminfo : (int * int) array = E.a
2369 end;;
2371 let [@warning "-4"]
2372 textentrykeyboard
2373 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2374 state.text <- E.s;
2375 let enttext te =
2376 state.mode <- Textentry (te, onleave);
2377 enttext ();
2378 G.postRedisplay "textentrykeyboard enttext";
2380 let histaction cmd =
2381 match opthist with
2382 | None -> ()
2383 | Some (action, _) ->
2384 state.mode <-
2385 Textentry (
2386 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2388 G.postRedisplay "textentry histaction"
2390 let open Keys in
2391 let kt = Wsi.kc2kt key in
2392 match kt with
2393 | Backspace ->
2394 if emptystr text && cancelonempty
2395 then (
2396 onleave Cancel;
2397 G.postRedisplay "textentrykeyboard after cancel";
2399 else
2400 let s = withoutlastutf8 text in
2401 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2403 | Enter ->
2404 ondone text;
2405 onleave Confirm;
2406 G.postRedisplay "textentrykeyboard after confirm"
2408 | Up -> histaction HCprev
2409 | Down -> histaction HCnext
2410 | Home -> histaction HCfirst
2411 | End -> histaction HClast
2413 | Escape ->
2414 if emptystr text
2415 then (
2416 begin match opthist with
2417 | None -> ()
2418 | Some (_, onhistcancel) -> onhistcancel ()
2419 end;
2420 onleave Cancel;
2421 state.text <- E.s;
2422 G.postRedisplay "textentrykeyboard after cancel2"
2424 else (
2425 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2428 | Delete -> ()
2430 | Code _ | Ascii _ ->
2431 begin match onkey text kt with
2432 | TEdone text ->
2433 ondone text;
2434 onleave Confirm;
2435 G.postRedisplay "textentrykeyboard after confirm2";
2437 | TEcont text ->
2438 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2440 | TEstop ->
2441 onleave Cancel;
2442 G.postRedisplay "textentrykeyboard after cancel3"
2444 | TEswitch te ->
2445 state.mode <- Textentry (te, onleave);
2446 G.postRedisplay "textentrykeyboard switch";
2448 | _ -> vlog "unhandled key"
2451 let firstof first active =
2452 if first > active || abs (first - active) > fstate.maxrows - 1
2453 then max 0 (active - (fstate.maxrows/2))
2454 else first
2457 let calcfirst first active =
2458 if active > first
2459 then
2460 let rows = active - first in
2461 if rows > fstate.maxrows then active - fstate.maxrows else first
2462 else active
2465 let scrollph y maxy =
2466 let sh = float (maxy + state.winh) /. float state.winh in
2467 let sh = float state.winh /. sh in
2468 let sh = max sh (float conf.scrollh) in
2470 let percent = float y /. float maxy in
2471 let position = (float state.winh -. sh) *. percent in
2473 let position =
2474 if position +. sh > float state.winh
2475 then float state.winh -. sh
2476 else position
2478 position, sh;
2481 let adderrmsg src msg =
2482 Buffer.add_string state.errmsgs msg;
2483 state.newerrmsgs <- true;
2484 G.postRedisplay src
2487 let adderrfmt src fmt =
2488 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2491 let coe s = (s :> uioh);;
2493 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2494 object (self)
2495 val m_pan = source#getpan
2496 val m_first = source#getfirst
2497 val m_active = source#getactive
2498 val m_qsearch = E.s
2499 val m_prev_uioh = state.uioh
2501 method private elemunder y =
2502 if y < 0
2503 then None
2504 else
2505 let n = y / (fstate.fontsize+1) in
2506 if m_first + n < source#getitemcount
2507 then (
2508 if source#hasaction (m_first + n)
2509 then Some (m_first + n)
2510 else None
2512 else None
2514 method display =
2515 Gl.enable `blend;
2516 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2517 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2518 filledrect 0. 0. (float state.winw) (float state.winh);
2519 GlDraw.color (1., 1., 1.);
2520 Gl.enable `texture_2d;
2521 let fs = fstate.fontsize in
2522 let nfs = fs + 1 in
2523 let hw = state.winw/3 in
2524 let ww = fstate.wwidth in
2525 let tabw = 17.0*.ww in
2526 let itemcount = source#getitemcount in
2527 let minfo = source#getminfo in
2528 if conf.leftscroll
2529 then (
2530 GlMat.push ();
2531 GlMat.translate ~x:(float conf.scrollbw) ();
2533 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2534 let rec loop row =
2535 if (row - m_first) > fstate.maxrows
2536 then ()
2537 else (
2538 if row >= 0 && row < itemcount
2539 then (
2540 let (s, level) = source#getitem row in
2541 let y = (row - m_first) * nfs in
2542 let x = 5.0 +. (float (level + m_pan)) *. ww in
2543 if helpmode
2544 then GlDraw.color
2545 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2547 if row = m_active
2548 then (
2549 Gl.disable `texture_2d;
2550 let alpha = if source#hasaction row then 0.9 else 0.3 in
2551 GlDraw.color (1., 1., 1.) ~alpha;
2552 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2553 Gl.enable `texture_2d;
2555 let c =
2556 if zebra && row land 1 = 1
2557 then 0.8
2558 else 1.0
2560 GlDraw.color (c,c,c);
2561 let drawtabularstring s =
2562 let drawstr x s =
2563 let x' = truncate (x0 +. x) in
2564 let s1, s2 = splitatchar s '\000' in
2565 if emptystr s2
2566 then drawstring1 fs x' (y+nfs) s
2567 else
2568 let rec e s =
2569 if emptystr s
2570 then s
2571 else
2572 let s' = withoutlastutf8 s in
2573 let s = s' ^ UniSyms.ellipsis in
2574 let w = measurestr fs s in
2575 if float x' +. w +. ww < float (hw + x')
2576 then s
2577 else e s'
2579 let s1 =
2580 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2581 then e s1
2582 else s1
2584 ignore (drawstring1 fs x' (y+nfs) s1);
2585 drawstring1 fs (hw + x') (y+nfs) s2
2587 if trusted
2588 then
2589 let x = if helpmode && row > 0 then x +. ww else x in
2590 let s1, s2 = splitatchar s '\t' in
2591 if nonemptystr s2
2592 then
2593 let nx = drawstr x s1 in
2594 let sw = nx -. x in
2595 let x = x +. (max tabw sw) in
2596 drawstr x s2
2597 else
2598 let len = String.length s - 2 in
2599 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2600 then
2601 let s = String.sub s 2 len in
2602 let x = if not helpmode then x +. ww else x in
2603 GlDraw.color (1.2, 1.2, 1.2);
2604 let vinc = drawstring1 (fs+fs/4)
2605 (truncate (x -. ww)) (y+nfs) s in
2606 GlDraw.color (1., 1., 1.);
2607 vinc +. (float fs *. 0.8)
2608 else
2609 drawstr x s
2610 else
2611 drawstr x s
2613 ignore (drawtabularstring s);
2614 loop (row+1)
2618 loop m_first;
2619 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2620 let xadj = 5.0 in
2621 let rec loop row =
2622 if (row - m_first) <= fstate.maxrows
2623 then
2624 if row >= 0 && row < itemcount
2625 then (
2626 let (s, level) = source#getitem row in
2627 let pos0 = nindex s '\000' in
2628 let y = (row - m_first) * nfs in
2629 let x = float (level + m_pan) *. ww in
2630 let (first, last) = minfo.(row) in
2631 let prefix =
2632 if pos0 > 0 && first > pos0
2633 then String.sub s (pos0+1) (first-pos0-1)
2634 else String.sub s 0 first
2636 let suffix = String.sub s first (last - first) in
2637 let w1 = measurestr fstate.fontsize prefix in
2638 let w2 = measurestr fstate.fontsize suffix in
2639 let x = x +. if conf.leftscroll then xadj else 5.0 in
2640 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2641 let x0 = x +. w1
2642 and y0 = float (y+2) in
2643 let x1 = x0 +. w2
2644 and y1 = float (y+fs+3) in
2645 filledrect x0 y0 x1 y1;
2646 loop (row+1)
2649 Gl.disable `texture_2d;
2650 if Array.length minfo > 0 then loop m_first;
2651 Gl.disable `blend;
2652 if conf.leftscroll
2653 then GlMat.pop ()
2655 method updownlevel incr =
2656 let len = source#getitemcount in
2657 let curlevel =
2658 if m_active >= 0 && m_active < len
2659 then snd (source#getitem m_active)
2660 else -1
2662 let rec flow i =
2663 if i = len then i-1 else if i = -1 then 0 else
2664 let _, l = source#getitem i in
2665 if l != curlevel then i else flow (i+incr)
2667 let active = flow m_active in
2668 let first = calcfirst m_first active in
2669 G.postRedisplay "outline updownlevel";
2670 {< m_active = active; m_first = first >}
2672 method private key1 key mask =
2673 let set1 active first qsearch =
2674 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2676 let search active pattern incr =
2677 let active = if active = -1 then m_first else active in
2678 let dosearch re =
2679 let rec loop n =
2680 if n >= 0 && n < source#getitemcount
2681 then (
2682 let s, _ = source#getitem n in
2683 match Str.search_forward re s 0 with
2684 | exception Not_found -> loop (n + incr)
2685 | _ -> Some n
2687 else None
2689 loop active
2691 let qpat = Str.quote pattern in
2692 match Str.regexp_case_fold qpat with
2693 | s -> dosearch s
2694 | exception exn ->
2695 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2696 qpat @@ Printexc.to_string exn;
2697 None
2699 let itemcount = source#getitemcount in
2700 let find start incr =
2701 let rec find i =
2702 if i = -1 || i = itemcount
2703 then -1
2704 else (
2705 if source#hasaction i
2706 then i
2707 else find (i + incr)
2710 find start
2712 let set active first =
2713 let first = bound first 0 (itemcount - fstate.maxrows) in
2714 state.text <- E.s;
2715 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2717 let navigate incr =
2718 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2719 let active, first =
2720 let incr1 = if incr > 0 then 1 else -1 in
2721 if isvisible m_first m_active
2722 then
2723 let next =
2724 let next = m_active + incr in
2725 let next =
2726 if next < 0 || next >= itemcount
2727 then -1
2728 else find next incr1
2730 if abs (m_active - next) > fstate.maxrows
2731 then -1
2732 else next
2734 if next = -1
2735 then
2736 let first = m_first + incr in
2737 let first = bound first 0 (itemcount - fstate.maxrows) in
2738 let next =
2739 let next = m_active + incr in
2740 let next = bound next 0 (itemcount - 1) in
2741 find next ~-incr1
2743 let active =
2744 if next = -1
2745 then m_active
2746 else (
2747 if isvisible first next
2748 then next
2749 else m_active
2752 active, first
2753 else
2754 let first = min next m_first in
2755 let first =
2756 if abs (next - first) > fstate.maxrows
2757 then first + incr
2758 else first
2760 next, first
2761 else
2762 let first = m_first + incr in
2763 let first = bound first 0 (itemcount - 1) in
2764 let active =
2765 let next = m_active + incr in
2766 let next = bound next 0 (itemcount - 1) in
2767 let next = find next incr1 in
2768 let active =
2769 if next = -1 || abs (m_active - first) > fstate.maxrows
2770 then (
2771 let active = if m_active = -1 then next else m_active in
2772 active
2774 else next
2776 if isvisible first active
2777 then active
2778 else -1
2780 active, first
2782 G.postRedisplay "listview navigate";
2783 set active first;
2785 let open Keys in
2786 let kt = Wsi.kc2kt key in
2787 match [@warning "-4"] kt with
2788 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2789 let incr = if c = 'r' then -1 else 1 in
2790 let active, first =
2791 match search (m_active + incr) m_qsearch incr with
2792 | None ->
2793 state.text <- m_qsearch ^ " [not found]";
2794 m_active, m_first
2795 | Some active ->
2796 state.text <- m_qsearch;
2797 active, firstof m_first active
2799 G.postRedisplay "listview ctrl-r/s";
2800 set1 active first m_qsearch;
2802 | Insert when Wsi.withctrl mask ->
2803 if m_active >= 0 && m_active < source#getitemcount
2804 then (
2805 let s, _ = source#getitem m_active in
2806 selstring s;
2808 coe self
2810 | Backspace ->
2811 if emptystr m_qsearch
2812 then coe self
2813 else (
2814 let qsearch = withoutlastutf8 m_qsearch in
2815 if emptystr qsearch
2816 then (
2817 state.text <- E.s;
2818 G.postRedisplay "listview empty qsearch";
2819 set1 m_active m_first E.s;
2821 else
2822 let active, first =
2823 match search m_active qsearch ~-1 with
2824 | None ->
2825 state.text <- qsearch ^ " [not found]";
2826 m_active, m_first
2827 | Some active ->
2828 state.text <- qsearch;
2829 active, firstof m_first active
2831 G.postRedisplay "listview backspace qsearch";
2832 set1 active first qsearch
2835 | Ascii _ | Code _ ->
2836 let utf8 =
2837 match [@warning "-8"] kt with
2838 | Ascii c -> String.make 1 c
2839 | Code code -> toutf8 code
2841 let pattern = m_qsearch ^ utf8 in
2842 let active, first =
2843 match search m_active pattern 1 with
2844 | None ->
2845 state.text <- pattern ^ " [not found]";
2846 m_active, m_first
2847 | Some active ->
2848 state.text <- pattern;
2849 active, firstof m_first active
2851 G.postRedisplay "listview qsearch add";
2852 set1 active first pattern;
2854 | Escape ->
2855 state.text <- E.s;
2856 if emptystr m_qsearch
2857 then (
2858 G.postRedisplay "list view escape";
2859 let mx, my = state.mpos in
2860 updateunder mx my;
2861 match source#exit ~uioh:(coe self) ~cancel:true ~active:m_active
2862 ~first:m_first ~pan:m_pan with
2863 | None -> m_prev_uioh
2864 | Some uioh -> uioh
2866 else (
2867 G.postRedisplay "list view kill qsearch";
2868 coe {< m_qsearch = E.s >}
2871 | Enter ->
2872 state.text <- E.s;
2873 let self = {< m_qsearch = E.s >} in
2874 let opt =
2875 G.postRedisplay "listview enter";
2876 let cancel = not (m_active >= 0 && m_active < source#getitemcount) in
2877 source#exit ~uioh:(coe self) ~cancel
2878 ~active:m_active ~first:m_first ~pan:m_pan;
2880 begin match opt with
2881 | None -> m_prev_uioh
2882 | Some uioh -> uioh
2885 | Delete ->
2886 coe self
2888 | Up -> navigate ~-1
2889 | Down -> navigate 1
2890 | Prior -> navigate ~-(fstate.maxrows)
2891 | Next -> navigate fstate.maxrows
2893 | Right ->
2894 state.text <- E.s;
2895 G.postRedisplay "listview right";
2896 coe {< m_pan = m_pan - 1 >}
2898 | Left ->
2899 state.text <- E.s;
2900 G.postRedisplay "listview left";
2901 coe {< m_pan = m_pan + 1 >}
2903 | Home ->
2904 let active = find 0 1 in
2905 G.postRedisplay "listview home";
2906 set active 0;
2908 | End ->
2909 let first = max 0 (itemcount - fstate.maxrows) in
2910 let active = find (itemcount - 1) ~-1 in
2911 G.postRedisplay "listview end";
2912 set active first;
2914 | _ -> coe self
2916 method key key mask =
2917 match state.mode with
2918 | Textentry te ->
2919 textentrykeyboard key mask te;
2920 coe self
2921 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2923 method button button down x y _ =
2924 let opt =
2925 match button with
2926 | 1 when vscrollhit x ->
2927 G.postRedisplay "listview scroll";
2928 if down
2929 then
2930 let _, position, sh = self#scrollph in
2931 if y > truncate position && y < truncate (position +. sh)
2932 then (
2933 state.mstate <- Mscrolly;
2934 Some (coe self)
2936 else
2937 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2938 let first = truncate (s *. float source#getitemcount) in
2939 let first = min source#getitemcount first in
2940 Some (coe {< m_first = first; m_active = first >})
2941 else (
2942 state.mstate <- Mnone;
2943 Some (coe self);
2945 | 1 when down ->
2946 begin match self#elemunder y with
2947 | Some n ->
2948 G.postRedisplay "listview click";
2949 source#exit ~uioh:(coe {< m_active = n >})
2950 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
2951 | _ ->
2952 Some (coe self)
2954 | n when (n == 4 || n == 5) && not down ->
2955 let len = source#getitemcount in
2956 let first =
2957 if n = 5 && m_first + fstate.maxrows >= len
2958 then
2959 m_first
2960 else
2961 let first = m_first + (if n == 4 then -1 else 1) in
2962 bound first 0 (len - 1)
2964 G.postRedisplay "listview wheel";
2965 Some (coe {< m_first = first >})
2966 | n when (n = 6 || n = 7) && not down ->
2967 let inc = if n = 7 then -1 else 1 in
2968 G.postRedisplay "listview hwheel";
2969 Some (coe {< m_pan = m_pan + inc >})
2970 | _ ->
2971 Some (coe self)
2973 match opt with
2974 | None -> m_prev_uioh
2975 | Some uioh -> uioh
2977 method multiclick _ x y = self#button 1 true x y
2979 method motion _ y =
2980 match state.mstate with
2981 | Mscrolly ->
2982 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2983 let first = truncate (s *. float source#getitemcount) in
2984 let first = min source#getitemcount first in
2985 G.postRedisplay "listview motion";
2986 coe {< m_first = first; m_active = first >}
2987 | Msel _
2988 | Mpan _
2989 | Mscrollx
2990 | Mzoom _
2991 | Mzoomrect _
2992 | Mnone -> coe self
2994 method pmotion x y =
2995 if x < state.winw - conf.scrollbw
2996 then
2997 let n =
2998 match self#elemunder y with
2999 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3000 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3002 let o =
3003 if n != m_active
3004 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3005 else self
3007 coe o
3008 else (
3009 Wsi.setcursor Wsi.CURSOR_INHERIT;
3010 coe self
3013 method infochanged _ = ()
3015 method scrollpw = (0, 0.0, 0.0)
3016 method scrollph =
3017 let nfs = fstate.fontsize + 1 in
3018 let y = m_first * nfs in
3019 let itemcount = source#getitemcount in
3020 let maxi = max 0 (itemcount - fstate.maxrows) in
3021 let maxy = maxi * nfs in
3022 let p, h = scrollph y maxy in
3023 conf.scrollbw, p, h
3025 method modehash = modehash
3026 method eformsgs = false
3027 method alwaysscrolly = true
3028 method scroll _ dy =
3029 let self =
3030 if dy != 0 then begin
3031 let len = source#getitemcount in
3032 let first =
3033 if dy > 0 && m_first + fstate.maxrows >= len
3034 then
3035 m_first
3036 else
3037 let first = m_first + dy / 10 in
3038 bound first 0 (len - 1)
3040 G.postRedisplay "listview wheel";
3041 {< m_first = first >}
3042 end else
3043 self
3045 coe self
3047 method zoom _ _ _ = ()
3048 end;;
3050 class outlinelistview ~zebra ~source =
3051 let settext autonarrow s =
3052 if autonarrow
3053 then
3054 let ss = source#statestr in
3055 state.text <-
3056 if emptystr ss
3057 then "[" ^ s ^ "]"
3058 else "{" ^ ss ^ "} [" ^ s ^ "]"
3059 else state.text <- s
3061 object (self)
3062 inherit listview
3063 ~zebra
3064 ~helpmode:false
3065 ~source:(source :> lvsource)
3066 ~trusted:false
3067 ~modehash:(findkeyhash conf "outline")
3068 as super
3070 val m_autonarrow = false
3072 method! key key mask =
3073 let maxrows =
3074 if emptystr state.text
3075 then fstate.maxrows
3076 else fstate.maxrows - 2
3078 let calcfirst first active =
3079 if active > first
3080 then
3081 let rows = active - first in
3082 if rows > maxrows then active - maxrows else first
3083 else active
3085 let navigate incr =
3086 let active = m_active + incr in
3087 let active = bound active 0 (source#getitemcount - 1) in
3088 let first = calcfirst m_first active in
3089 G.postRedisplay "outline navigate";
3090 coe {< m_active = active; m_first = first >}
3092 let navscroll first =
3093 let active =
3094 let dist = m_active - first in
3095 if dist < 0
3096 then first
3097 else (
3098 if dist < maxrows
3099 then m_active
3100 else first + maxrows
3103 G.postRedisplay "outline navscroll";
3104 coe {< m_first = first; m_active = active >}
3106 let ctrl = Wsi.withctrl mask in
3107 let open Keys in
3108 match Wsi.kc2kt key with
3109 | Ascii 'a' when ctrl ->
3110 let text =
3111 if m_autonarrow
3112 then (source#denarrow; E.s)
3113 else (
3114 let pattern = source#renarrow in
3115 if nonemptystr m_qsearch
3116 then (source#narrow m_qsearch; m_qsearch)
3117 else pattern
3120 settext (not m_autonarrow) text;
3121 G.postRedisplay "toggle auto narrowing";
3122 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3124 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
3125 settext true E.s;
3126 G.postRedisplay "toggle auto narrowing";
3127 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3129 | Ascii 'n' when ctrl ->
3130 source#narrow m_qsearch;
3131 if not m_autonarrow
3132 then source#add_narrow_pattern m_qsearch;
3133 G.postRedisplay "outline ctrl-n";
3134 coe {< m_first = 0; m_active = 0 >}
3136 | Ascii 'S' when ctrl ->
3137 let active = source#calcactive (getanchor ()) in
3138 let first = firstof m_first active in
3139 G.postRedisplay "outline ctrl-s";
3140 coe {< m_first = first; m_active = active >}
3142 | Ascii 'u' when ctrl ->
3143 G.postRedisplay "outline ctrl-u";
3144 if m_autonarrow && nonemptystr m_qsearch
3145 then (
3146 ignore (source#renarrow);
3147 settext m_autonarrow E.s;
3148 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3150 else (
3151 source#del_narrow_pattern;
3152 let pattern = source#renarrow in
3153 let text =
3154 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3156 settext m_autonarrow text;
3157 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3160 | Ascii 'l' when ctrl ->
3161 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3162 G.postRedisplay "outline ctrl-l";
3163 coe {< m_first = first >}
3165 | Ascii '\t' when m_autonarrow ->
3166 if nonemptystr m_qsearch
3167 then (
3168 G.postRedisplay "outline list view tab";
3169 source#add_narrow_pattern m_qsearch;
3170 settext true E.s;
3171 coe {< m_qsearch = E.s >}
3173 else coe self
3175 | Escape when m_autonarrow ->
3176 if nonemptystr m_qsearch
3177 then source#add_narrow_pattern m_qsearch;
3178 super#key key mask
3180 | Enter when m_autonarrow ->
3181 if nonemptystr m_qsearch
3182 then source#add_narrow_pattern m_qsearch;
3183 super#key key mask
3185 | (Ascii _ | Code _) when m_autonarrow ->
3186 let pattern = m_qsearch ^ toutf8 key in
3187 G.postRedisplay "outlinelistview autonarrow add";
3188 source#narrow pattern;
3189 settext true pattern;
3190 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3192 | Backspace when m_autonarrow ->
3193 if emptystr m_qsearch
3194 then coe self
3195 else
3196 let pattern = withoutlastutf8 m_qsearch in
3197 G.postRedisplay "outlinelistview autonarrow backspace";
3198 ignore (source#renarrow);
3199 source#narrow pattern;
3200 settext true pattern;
3201 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3203 | Up when ctrl ->
3204 navscroll (max 0 (m_first - 1))
3206 | Down when ctrl ->
3207 navscroll (min (source#getitemcount - 1) (m_first + 1))
3209 | Up -> navigate ~-1
3210 | Down -> navigate 1
3211 | Prior -> navigate ~-(fstate.maxrows)
3212 | Next -> navigate fstate.maxrows
3214 | Right ->
3215 let o =
3216 if ctrl
3217 then (
3218 G.postRedisplay "outline ctrl right";
3219 {< m_pan = m_pan + 1 >}
3221 else self#updownlevel 1
3223 coe o
3225 | Left ->
3226 let o =
3227 if ctrl
3228 then (
3229 G.postRedisplay "outline ctrl left";
3230 {< m_pan = m_pan - 1 >}
3232 else self#updownlevel ~-1
3234 coe o
3236 | Home ->
3237 G.postRedisplay "outline home";
3238 coe {< m_first = 0; m_active = 0 >}
3240 | End ->
3241 let active = source#getitemcount - 1 in
3242 let first = max 0 (active - fstate.maxrows) in
3243 G.postRedisplay "outline end";
3244 coe {< m_active = active; m_first = first >}
3246 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
3247 super#key key mask
3248 end;;
3250 let genhistoutlines () =
3251 Config.gethist ()
3252 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3253 compare c2.lastvisit c1.lastvisit)
3254 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
3255 let path = if nonemptystr origin then origin else path in
3256 let base = mbtoutf8 @@ Filename.basename path in
3257 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3261 let gotohist (path, c, bookmarks, x, anchor, origin) =
3262 Config.save leavebirdseye;
3263 state.anchor <- anchor;
3264 state.bookmarks <- bookmarks;
3265 state.origin <- origin;
3266 state.x <- x;
3267 setconf conf c;
3268 let x0, y0, x1, y1 = conf.trimfuzz in
3269 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3270 reshape ~firsttime:true state.winw state.winh;
3271 opendoc path origin;
3272 setzoom c.zoom;
3275 let makecheckers () =
3276 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3277 following to say:
3278 converted by Issac Trotts. July 25, 2002 *)
3279 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3280 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3281 let id = GlTex.gen_texture () in
3282 GlTex.bind_texture ~target:`texture_2d id;
3283 GlPix.store (`unpack_alignment 1);
3284 GlTex.image2d image;
3285 List.iter (GlTex.parameter ~target:`texture_2d)
3286 [ `mag_filter `nearest; `min_filter `nearest ];
3290 let setcheckers enabled =
3291 match state.checkerstexid with
3292 | None ->
3293 if enabled then state.checkerstexid <- Some (makecheckers ())
3295 | Some checkerstexid ->
3296 if not enabled
3297 then (
3298 GlTex.delete_texture checkerstexid;
3299 state.checkerstexid <- None;
3303 let describe_layout layout =
3304 let d =
3305 match layout with
3306 | [] -> "Page 0"
3307 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
3308 | l :: rest ->
3309 let rangestr a b =
3310 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
3311 else
3312 let sep = if a.pageno+1 = b.pageno then ", " else UniSyms.ellipsis in
3313 Printf.sprintf "%d%s%d" (a.pageno+1) sep (b.pageno+1)
3315 let rec fold s la lb = function
3316 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
3317 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
3318 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
3320 fold "Pages" l l rest
3322 let percent =
3323 let maxy = maxy () in
3324 if maxy <= 0
3325 then 100.
3326 else 100. *. (float state.y /. float maxy)
3328 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
3331 let setpresentationmode v =
3332 let n = page_of_y state.y in
3333 state.anchor <- (n, 0.0, 1.0);
3334 conf.presentation <- v;
3335 if conf.fitmodel = FitPage
3336 then reqlayout conf.angle conf.fitmodel;
3337 represent ();
3340 let enterinfomode =
3341 let btos b = if b then UniSyms.radical else E.s in
3342 let showextended = ref false in
3343 let showcolors = ref false in
3344 let leave mode _ = state.mode <- mode in
3345 let src =
3346 (object
3347 val mutable m_l = []
3348 val mutable m_a = E.a
3349 val mutable m_prev_uioh = nouioh
3350 val mutable m_prev_mode = View
3352 inherit lvsourcebase
3354 method reset prev_mode prev_uioh =
3355 m_a <- Array.of_list (List.rev m_l);
3356 m_l <- [];
3357 m_prev_mode <- prev_mode;
3358 m_prev_uioh <- prev_uioh;
3360 method int name get set =
3361 m_l <-
3362 (name, `int get, 1,
3363 Action (
3364 fun u ->
3365 let ondone s =
3366 try set (int_of_string s)
3367 with exn ->
3368 state.text <- Printf.sprintf "bad integer `%s': %s"
3369 s @@ exntos exn
3371 state.text <- E.s;
3372 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3373 state.mode <- Textentry (te, leave m_prev_mode);
3375 )) :: m_l
3377 method int_with_suffix name get set =
3378 m_l <-
3379 (name, `intws get, 1,
3380 Action (
3381 fun u ->
3382 let ondone s =
3383 try set (int_of_string_with_suffix s)
3384 with exn ->
3385 state.text <- Printf.sprintf "bad integer `%s': %s"
3386 s @@ exntos exn
3388 state.text <- E.s;
3389 let te =
3390 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3392 state.mode <- Textentry (te, leave m_prev_mode);
3394 )) :: m_l
3396 method bool ?(offset=1) ?(btos=btos) name get set =
3397 m_l <-
3398 (name, `bool (btos, get), offset, Action (
3399 fun u ->
3400 let v = get () in
3401 set (not v);
3403 )) :: m_l
3405 method color name get set =
3406 m_l <-
3407 (name, `color get, 1,
3408 Action (
3409 fun u ->
3410 let invalid = (nan, nan, nan) in
3411 let ondone s =
3412 let c =
3413 try color_of_string s
3414 with exn ->
3415 state.text <- Printf.sprintf "bad color `%s': %s"
3416 s @@ exntos exn;
3417 invalid
3419 if c <> invalid
3420 then set c;
3422 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3423 state.text <- color_to_string (get ());
3424 state.mode <- Textentry (te, leave m_prev_mode);
3426 )) :: m_l
3428 method string name get set =
3429 m_l <-
3430 (name, `string get, 1,
3431 Action (
3432 fun u ->
3433 let ondone s = set s in
3434 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3435 state.mode <- Textentry (te, leave m_prev_mode);
3437 )) :: m_l
3439 method colorspace name get set =
3440 m_l <-
3441 (name, `string get, 1,
3442 Action (
3443 fun _ ->
3444 let source =
3445 (object
3446 inherit lvsourcebase
3448 initializer
3449 m_active <- CSTE.to_int conf.colorspace;
3450 m_first <- 0;
3452 method getitemcount =
3453 Array.length CSTE.names
3454 method getitem n =
3455 (CSTE.names.(n), 0)
3456 method exit ~uioh ~cancel ~active ~first ~pan =
3457 ignore (uioh, first, pan);
3458 if not cancel then set active;
3459 None
3460 method hasaction _ = true
3461 end)
3463 state.text <- E.s;
3464 let modehash = findkeyhash conf "info" in
3465 coe (new listview ~zebra:false ~helpmode:false
3466 ~source ~trusted:true ~modehash)
3467 )) :: m_l
3469 method paxmark name get set =
3470 m_l <-
3471 (name, `string get, 1,
3472 Action (
3473 fun _ ->
3474 let source =
3475 (object
3476 inherit lvsourcebase
3478 initializer
3479 m_active <- MTE.to_int conf.paxmark;
3480 m_first <- 0;
3482 method getitemcount = Array.length MTE.names
3483 method getitem n = (MTE.names.(n), 0)
3484 method exit ~uioh ~cancel ~active ~first ~pan =
3485 ignore (uioh, first, pan);
3486 if not cancel then set active;
3487 None
3488 method hasaction _ = true
3489 end)
3491 state.text <- E.s;
3492 let modehash = findkeyhash conf "info" in
3493 coe (new listview ~zebra:false ~helpmode:false
3494 ~source ~trusted:true ~modehash)
3495 )) :: m_l
3497 method fitmodel name get set =
3498 m_l <-
3499 (name, `string get, 1,
3500 Action (
3501 fun _ ->
3502 let source =
3503 (object
3504 inherit lvsourcebase
3506 initializer
3507 m_active <- FMTE.to_int conf.fitmodel;
3508 m_first <- 0;
3510 method getitemcount = Array.length FMTE.names
3511 method getitem n = (FMTE.names.(n), 0)
3512 method exit ~uioh ~cancel ~active ~first ~pan =
3513 ignore (uioh, first, pan);
3514 if not cancel then set active;
3515 None
3516 method hasaction _ = true
3517 end)
3519 state.text <- E.s;
3520 let modehash = findkeyhash conf "info" in
3521 coe (new listview ~zebra:false ~helpmode:false
3522 ~source ~trusted:true ~modehash)
3523 )) :: m_l
3525 method caption s offset =
3526 m_l <- (s, `empty, offset, Noaction) :: m_l
3528 method caption2 s f offset =
3529 m_l <- (s, `string f, offset, Noaction) :: m_l
3531 method getitemcount = Array.length m_a
3533 method getitem n =
3534 let tostr = function
3535 | `int f -> string_of_int (f ())
3536 | `intws f -> string_with_suffix_of_int (f ())
3537 | `string f -> f ()
3538 | `color f -> color_to_string (f ())
3539 | `bool (btos, f) -> btos (f ())
3540 | `empty -> E.s
3542 let name, t, offset, _ = m_a.(n) in
3543 ((let s = tostr t in
3544 if nonemptystr s
3545 then Printf.sprintf "%s\t%s" name s
3546 else name),
3547 offset)
3549 method exit ~uioh ~cancel ~active ~first ~pan =
3550 let uiohopt =
3551 if not cancel
3552 then (
3553 let uioh =
3554 match m_a.(active) with
3555 | _, _, _, Action f -> f uioh
3556 | _, _, _, Noaction -> uioh
3558 Some uioh
3560 else None
3562 m_active <- active;
3563 m_first <- first;
3564 m_pan <- pan;
3565 uiohopt
3567 method hasaction n =
3568 match m_a.(n) with
3569 | _, _, _, Action _ -> true
3570 | _, _, _, Noaction -> false
3572 initializer m_active <- 1
3573 end)
3575 let rec fillsrc prevmode prevuioh =
3576 let sep () = src#caption E.s 0 in
3577 let colorp name get set =
3578 src#string name
3579 (fun () -> color_to_string (get ()))
3580 (fun v ->
3582 let c = color_of_string v in
3583 set c
3584 with exn ->
3585 state.text <-
3586 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3589 let rgba name get set =
3590 src#string name
3591 (fun () -> rgba_to_string (get ()))
3592 (fun v ->
3594 let c = rgba_of_string v in
3595 set c
3596 with exn ->
3597 state.text <-
3598 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3601 let oldmode = state.mode in
3602 let birdseye = isbirdseye state.mode in
3604 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3606 src#bool "presentation mode"
3607 (fun () -> conf.presentation)
3608 (fun v -> setpresentationmode v);
3610 src#bool "ignore case in searches"
3611 (fun () -> conf.icase)
3612 (fun v -> conf.icase <- v);
3614 src#bool "preload"
3615 (fun () -> conf.preload)
3616 (fun v -> conf.preload <- v);
3618 src#bool "highlight links"
3619 (fun () -> conf.hlinks)
3620 (fun v -> conf.hlinks <- v);
3622 src#bool "under info"
3623 (fun () -> conf.underinfo)
3624 (fun v -> conf.underinfo <- v);
3626 src#bool "persistent bookmarks"
3627 (fun () -> conf.savebmarks)
3628 (fun v -> conf.savebmarks <- v);
3630 src#fitmodel "fit model"
3631 (fun () -> FMTE.to_string conf.fitmodel)
3632 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3634 src#bool "trim margins"
3635 (fun () -> conf.trimmargins)
3636 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3638 src#bool "persistent location"
3639 (fun () -> conf.jumpback)
3640 (fun v -> conf.jumpback <- v);
3642 sep ();
3643 src#int "inter-page space"
3644 (fun () -> conf.interpagespace)
3645 (fun n ->
3646 conf.interpagespace <- n;
3647 docolumns conf.columns;
3648 let pageno, py =
3649 match state.layout with
3650 | [] -> 0, 0
3651 | l :: _ ->
3652 l.pageno, l.pagey
3654 state.maxy <- calcheight ();
3655 let y = getpagey pageno in
3656 gotoxy state.x (y + py)
3659 src#int "page bias"
3660 (fun () -> conf.pagebias)
3661 (fun v -> conf.pagebias <- v);
3663 src#int "scroll step"
3664 (fun () -> conf.scrollstep)
3665 (fun n -> conf.scrollstep <- n);
3667 src#int "horizontal scroll step"
3668 (fun () -> conf.hscrollstep)
3669 (fun v -> conf.hscrollstep <- v);
3671 src#int "auto scroll step"
3672 (fun () ->
3673 match state.autoscroll with
3674 | Some step -> step
3675 | _ -> conf.autoscrollstep)
3676 (fun n ->
3677 let n = boundastep state.winh n in
3678 if state.autoscroll <> None
3679 then state.autoscroll <- Some n;
3680 conf.autoscrollstep <- n);
3682 src#int "zoom"
3683 (fun () -> truncate (conf.zoom *. 100.))
3684 (fun v -> pivotzoom ((float v) /. 100.));
3686 src#int "rotation"
3687 (fun () -> conf.angle)
3688 (fun v -> reqlayout v conf.fitmodel);
3690 src#int "scroll bar width"
3691 (fun () -> conf.scrollbw)
3692 (fun v ->
3693 conf.scrollbw <- v;
3694 reshape state.winw state.winh;
3697 src#int "scroll handle height"
3698 (fun () -> conf.scrollh)
3699 (fun v -> conf.scrollh <- v;);
3701 src#int "thumbnail width"
3702 (fun () -> conf.thumbw)
3703 (fun v ->
3704 conf.thumbw <- min 4096 v;
3705 match oldmode with
3706 | Birdseye beye ->
3707 leavebirdseye beye false;
3708 enterbirdseye ()
3709 | Textentry _
3710 | View
3711 | LinkNav _ -> ()
3714 let mode = state.mode in
3715 src#string "columns"
3716 (fun () ->
3717 match conf.columns with
3718 | Csingle _ -> "1"
3719 | Cmulti (multi, _) -> multicolumns_to_string multi
3720 | Csplit (count, _) -> "-" ^ string_of_int count
3722 (fun v ->
3723 let n, a, b = multicolumns_of_string v in
3724 setcolumns mode n a b);
3726 sep ();
3727 src#caption "Pixmap cache" 0;
3728 src#int_with_suffix "size (advisory)"
3729 (fun () -> conf.memlimit)
3730 (fun v -> conf.memlimit <- v);
3732 src#caption2 "used"
3733 (fun () ->
3734 Printf.sprintf "%s bytes, %d tiles"
3735 (string_with_suffix_of_int state.memused)
3736 (Hashtbl.length state.tilemap)) 1;
3738 sep ();
3739 src#caption "Layout" 0;
3740 src#caption2 "Dimension"
3741 (fun () ->
3742 Printf.sprintf "%dx%d (virtual %dx%d)"
3743 state.winw state.winh
3744 state.w state.maxy)
3746 if conf.debug
3747 then
3748 src#caption2 "Position" (fun () ->
3749 Printf.sprintf "%dx%d" state.x state.y
3751 else
3752 src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
3754 sep ();
3755 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3756 "Save these parameters as global defaults at exit"
3757 (fun () -> conf.bedefault)
3758 (fun v -> conf.bedefault <- v);
3760 sep ();
3761 let btos b = if b then UniSyms.lguillemet else UniSyms.rguillemet in
3762 src#bool ~offset:0 ~btos "Extended parameters"
3763 (fun () -> !showextended)
3764 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3765 if !showextended
3766 then (
3767 src#bool "checkers"
3768 (fun () -> conf.checkers)
3769 (fun v -> conf.checkers <- v; setcheckers v);
3770 src#bool "update cursor"
3771 (fun () -> conf.updatecurs)
3772 (fun v -> conf.updatecurs <- v);
3773 src#bool "scroll-bar on the left"
3774 (fun () -> conf.leftscroll)
3775 (fun v -> conf.leftscroll <- v);
3776 src#bool "verbose"
3777 (fun () -> conf.verbose)
3778 (fun v -> conf.verbose <- v);
3779 src#bool "invert colors"
3780 (fun () -> conf.invert)
3781 (fun v -> conf.invert <- v);
3782 src#bool "max fit"
3783 (fun () -> conf.maxhfit)
3784 (fun v -> conf.maxhfit <- v);
3785 src#bool "pax mode"
3786 (fun () -> conf.pax != None)
3787 (fun v ->
3788 if v
3789 then conf.pax <- Some (now ())
3790 else conf.pax <- None);
3791 src#string "uri launcher"
3792 (fun () -> conf.urilauncher)
3793 (fun v -> conf.urilauncher <- v);
3794 src#string "path launcher"
3795 (fun () -> conf.pathlauncher)
3796 (fun v -> conf.pathlauncher <- v);
3797 src#string "tile size"
3798 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3799 (fun v ->
3801 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3802 conf.tilew <- max 64 w;
3803 conf.tileh <- max 64 h;
3804 flushtiles ();
3805 with exn ->
3806 state.text <- Printf.sprintf "bad tile size `%s': %s"
3807 v @@ exntos exn
3809 src#int "texture count"
3810 (fun () -> conf.texcount)
3811 (fun v ->
3812 if realloctexts v
3813 then conf.texcount <- v
3814 else impmsg "failed to set texture count please retry later"
3816 src#int "slice height"
3817 (fun () -> conf.sliceheight)
3818 (fun v ->
3819 conf.sliceheight <- v;
3820 wcmd "sliceh %d" conf.sliceheight;
3822 src#int "anti-aliasing level"
3823 (fun () -> conf.aalevel)
3824 (fun v ->
3825 conf.aalevel <- bound v 0 8;
3826 state.anchor <- getanchor ();
3827 opendoc state.path state.password;
3829 src#string "page scroll scaling factor"
3830 (fun () -> string_of_float conf.pgscale)
3831 (fun v ->
3833 let s = float_of_string v in
3834 conf.pgscale <- s
3835 with exn ->
3836 state.text <- Printf.sprintf
3837 "bad page scroll scaling factor `%s': %s" v
3838 @@ exntos exn
3841 src#int "ui font size"
3842 (fun () -> fstate.fontsize)
3843 (fun v -> setfontsize (bound v 5 100));
3844 src#int "hint font size"
3845 (fun () -> conf.hfsize)
3846 (fun v -> conf.hfsize <- bound v 5 100);
3847 src#bool "crop hack"
3848 (fun () -> conf.crophack)
3849 (fun v -> conf.crophack <- v);
3850 src#string "trim fuzz"
3851 (fun () -> irect_to_string conf.trimfuzz)
3852 (fun v ->
3854 conf.trimfuzz <- irect_of_string v;
3855 if conf.trimmargins
3856 then settrim true conf.trimfuzz;
3857 with exn ->
3858 state.text <- Printf.sprintf "bad irect `%s': %s" v
3859 @@ exntos exn
3861 src#string "throttle"
3862 (fun () ->
3863 match conf.maxwait with
3864 | None -> "show place holder if page is not ready"
3865 | Some time ->
3866 if time = infinity
3867 then "wait for page to fully render"
3868 else
3869 "wait " ^ string_of_float time
3870 ^ " seconds before showing placeholder"
3872 (fun v ->
3874 let f = float_of_string v in
3875 if f <= 0.0
3876 then conf.maxwait <- None
3877 else conf.maxwait <- Some f
3878 with exn ->
3879 state.text <- Printf.sprintf "bad time `%s': %s" v
3880 @@ exntos exn
3882 src#string "ghyll scroll"
3883 (fun () ->
3884 match conf.ghyllscroll with
3885 | None -> E.s
3886 | Some nab -> ghyllscroll_to_string nab
3888 (fun v ->
3889 try conf.ghyllscroll <- ghyllscroll_of_string v
3890 with
3891 | Failure msg ->
3892 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3893 | exn ->
3894 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3895 @@ exntos exn
3897 src#string "selection command"
3898 (fun () -> conf.selcmd)
3899 (fun v -> conf.selcmd <- v);
3900 src#string "synctex command"
3901 (fun () -> conf.stcmd)
3902 (fun v -> conf.stcmd <- v);
3903 src#string "pax command"
3904 (fun () -> conf.paxcmd)
3905 (fun v -> conf.paxcmd <- v);
3906 src#string "ask password command"
3907 (fun () -> conf.passcmd)
3908 (fun v -> conf.passcmd <- v);
3909 src#string "save path command"
3910 (fun () -> conf.savecmd)
3911 (fun v -> conf.savecmd <- v);
3912 src#colorspace "color space"
3913 (fun () -> CSTE.to_string conf.colorspace)
3914 (fun v ->
3915 conf.colorspace <- CSTE.of_int v;
3916 wcmd "cs %d" v;
3917 load state.layout;
3919 src#paxmark "pax mark method"
3920 (fun () -> MTE.to_string conf.paxmark)
3921 (fun v -> conf.paxmark <- MTE.of_int v);
3922 if bousable () && !opengl_has_pbo
3923 then
3924 src#bool "use PBO"
3925 (fun () -> conf.usepbo)
3926 (fun v -> conf.usepbo <- v);
3927 src#bool "mouse wheel scrolls pages"
3928 (fun () -> conf.wheelbypage)
3929 (fun v -> conf.wheelbypage <- v);
3930 src#bool "open remote links in a new instance"
3931 (fun () -> conf.riani)
3932 (fun v -> conf.riani <- v);
3933 src#bool "edit annotations inline"
3934 (fun () -> conf.annotinline)
3935 (fun v -> conf.annotinline <- v);
3936 src#bool "coarse positioning in presentation mode"
3937 (fun () -> conf.coarseprespos)
3938 (fun v -> conf.coarseprespos <- v);
3939 src#bool "use document CSS"
3940 (fun () -> conf.usedoccss)
3941 (fun v ->
3942 conf.usedoccss <- v;
3943 state.anchor <- getanchor ();
3944 opendoc state.path state.password;
3946 src#bool ~btos "colors"
3947 (fun () -> !showcolors)
3948 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
3949 if !showcolors
3950 then (
3951 colorp " background"
3952 (fun () -> conf.bgcolor)
3953 (fun v -> conf.bgcolor <- v);
3954 rgba " scrollbar"
3955 (fun () -> conf.sbarcolor)
3956 (fun v -> conf.sbarcolor <- v);
3957 rgba " scrollbar handle"
3958 (fun () -> conf.sbarhndlcolor)
3959 (fun v -> conf.sbarhndlcolor <- v);
3963 sep ();
3964 src#caption "Document" 0;
3965 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3966 src#caption2 "Pages"
3967 (fun () -> string_of_int state.pagecount) 1;
3968 src#caption2 "Dimensions"
3969 (fun () -> string_of_int (List.length state.pdims)) 1;
3970 if nonemptystr conf.css
3971 then src#caption2 "CSS" (fun () -> conf.css) 1;
3972 if conf.trimmargins
3973 then (
3974 sep ();
3975 src#caption "Trimmed margins" 0;
3976 src#caption2 "Dimensions"
3977 (fun () -> string_of_int (List.length state.pdims)) 1;
3980 sep ();
3981 src#caption "OpenGL" 0;
3982 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3983 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3985 sep ();
3986 src#caption "Location" 0;
3987 if nonemptystr state.origin
3988 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3989 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3991 src#reset prevmode prevuioh;
3993 fun () ->
3994 state.text <- E.s;
3995 resetmstate ();
3996 let prevmode = state.mode
3997 and prevuioh = state.uioh in
3998 fillsrc prevmode prevuioh;
3999 let source = (src :> lvsource) in
4000 let modehash = findkeyhash conf "info" in
4001 state.uioh <-
4002 coe (object (self)
4003 inherit listview ~zebra:false ~helpmode:false
4004 ~source ~trusted:true ~modehash as super
4005 val mutable m_prevmemused = 0
4006 method! infochanged = function
4007 | Memused ->
4008 if m_prevmemused != state.memused
4009 then (
4010 m_prevmemused <- state.memused;
4011 G.postRedisplay "memusedchanged";
4013 | Pdim -> G.postRedisplay "pdimchanged"
4014 | Docinfo -> fillsrc prevmode prevuioh
4016 method! key key mask =
4017 if not (Wsi.withctrl mask)
4018 then
4019 match [@warning "-4"] Wsi.kc2kt key with
4020 | Keys.Left -> coe (self#updownlevel ~-1)
4021 | Keys.Right -> coe (self#updownlevel 1)
4022 | _ -> super#key key mask
4023 else super#key key mask
4024 end);
4025 G.postRedisplay "info";
4028 let enterhelpmode =
4029 let source =
4030 (object
4031 inherit lvsourcebase
4032 method getitemcount = Array.length state.help
4033 method getitem n =
4034 let s, l, _ = state.help.(n) in
4035 (s, l)
4037 method exit ~uioh ~cancel ~active ~first ~pan =
4038 let optuioh =
4039 if not cancel
4040 then (
4041 match state.help.(active) with
4042 | _, _, Action f -> Some (f uioh)
4043 | _, _, Noaction -> Some uioh
4045 else None
4047 m_active <- active;
4048 m_first <- first;
4049 m_pan <- pan;
4050 optuioh
4052 method hasaction n =
4053 match state.help.(n) with
4054 | _, _, Action _ -> true
4055 | _, _, Noaction -> false
4057 initializer
4058 m_active <- -1
4059 end)
4060 in fun () ->
4061 let modehash = findkeyhash conf "help" in
4062 resetmstate ();
4063 state.uioh <- coe (new listview
4064 ~zebra:false ~helpmode:true
4065 ~source ~trusted:true ~modehash);
4066 G.postRedisplay "help";
4069 let entermsgsmode =
4070 let msgsource =
4071 (object
4072 inherit lvsourcebase
4073 val mutable m_items = E.a
4075 method getitemcount = 1 + Array.length m_items
4077 method getitem n =
4078 if n = 0
4079 then "[Clear]", 0
4080 else m_items.(n-1), 0
4082 method exit ~uioh ~cancel ~active ~first ~pan =
4083 ignore uioh;
4084 if not cancel
4085 then (
4086 if active = 0
4087 then Buffer.clear state.errmsgs;
4089 m_active <- active;
4090 m_first <- first;
4091 m_pan <- pan;
4092 None
4094 method hasaction n =
4095 n = 0
4097 method reset =
4098 state.newerrmsgs <- false;
4099 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4100 m_items <- Array.of_list l
4102 initializer
4103 m_active <- 0
4104 end)
4105 in fun () ->
4106 state.text <- E.s;
4107 resetmstate ();
4108 msgsource#reset;
4109 let source = (msgsource :> lvsource) in
4110 let modehash = findkeyhash conf "listview" in
4111 state.uioh <-
4112 coe (object
4113 inherit listview ~zebra:false ~helpmode:false
4114 ~source ~trusted:false ~modehash as super
4115 method! display =
4116 if state.newerrmsgs
4117 then msgsource#reset;
4118 super#display
4119 end);
4120 G.postRedisplay "msgs";
4123 let getusertext s =
4124 let editor = getenvwithdef "EDITOR" E.s in
4125 if emptystr editor
4126 then E.s
4127 else
4128 let tmppath = Filename.temp_file "llpp" "note" in
4129 if nonemptystr s
4130 then (
4131 let oc = open_out tmppath in
4132 output_string oc s;
4133 close_out oc;
4135 let execstr = editor ^ " " ^ tmppath in
4136 let s =
4137 match spawn execstr [] with
4138 | exception exn ->
4139 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4141 | pid ->
4142 match Unix.waitpid [] pid with
4143 | exception exn ->
4144 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4146 | (_pid, status) ->
4147 match status with
4148 | Unix.WEXITED 0 -> filecontents tmppath
4149 | Unix.WEXITED n ->
4150 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4152 | Unix.WSIGNALED n ->
4153 impmsg "editor process(%s) was killed by signal %d" execstr n;
4155 | Unix.WSTOPPED n ->
4156 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4159 match Unix.unlink tmppath with
4160 | exception exn ->
4161 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4163 | () -> s
4166 let enterannotmode opaque slinkindex =
4167 let msgsource =
4168 (object
4169 inherit lvsourcebase
4170 val mutable m_text = E.s
4171 val mutable m_items = E.a
4173 method getitemcount = Array.length m_items
4175 method getitem n =
4176 let label, _func = m_items.(n) in
4177 label, 0
4179 method exit ~uioh ~cancel ~active ~first ~pan =
4180 ignore (uioh, first, pan);
4181 if not cancel
4182 then (
4183 let _label, func = m_items.(active) in
4184 func ()
4186 None
4188 method hasaction n = nonemptystr @@ fst m_items.(n)
4190 method reset s =
4191 let rec split accu b i =
4192 let p = b+i in
4193 if p = String.length s
4194 then (String.sub s b (p-b), unit) :: accu
4195 else
4196 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4197 then
4198 let ss = if i = 0 then E.s else String.sub s b i in
4199 split ((ss, unit)::accu) (p+1) 0
4200 else
4201 split accu b (i+1)
4203 let cleanup () =
4204 wcmd "freepage %s" (~> opaque);
4205 let keys =
4206 Hashtbl.fold (fun key opaque' accu ->
4207 if opaque' = opaque'
4208 then key :: accu else accu) state.pagemap []
4210 List.iter (Hashtbl.remove state.pagemap) keys;
4211 flushtiles ();
4212 gotoxy state.x state.y
4214 let dele () =
4215 delannot opaque slinkindex;
4216 cleanup ();
4218 let edit inline () =
4219 let update s =
4220 if emptystr s
4221 then dele ()
4222 else (
4223 modannot opaque slinkindex s;
4224 cleanup ();
4227 if inline
4228 then
4229 let mode = state.mode in
4230 state.mode <-
4231 Textentry (
4232 ("annotation: ", m_text, None, textentry, update, true),
4233 fun _ -> state.mode <- mode);
4234 state.text <- E.s;
4235 enttext ();
4236 else
4237 let s = getusertext m_text in
4238 update s
4240 m_text <- s;
4241 m_items <-
4242 ( "[Copy]", fun () -> selstring m_text)
4243 :: ("[Delete]", dele)
4244 :: ("[Edit]", edit conf.annotinline)
4245 :: (E.s, unit)
4246 :: split [] 0 0 |> List.rev |> Array.of_list
4248 initializer
4249 m_active <- 0
4250 end)
4252 state.text <- E.s;
4253 let s = getannotcontents opaque slinkindex in
4254 resetmstate ();
4255 msgsource#reset s;
4256 let source = (msgsource :> lvsource) in
4257 let modehash = findkeyhash conf "listview" in
4258 state.uioh <- coe (object
4259 inherit listview ~zebra:false ~helpmode:false
4260 ~source ~trusted:false ~modehash
4261 end);
4262 G.postRedisplay "enterannotmode";
4265 let gotoremote spec =
4266 let filename, dest = splitatchar spec '#' in
4267 let getpath filename =
4268 let path =
4269 if nonemptystr filename
4270 then
4271 if Filename.is_relative filename
4272 then
4273 let dir = Filename.dirname state.path in
4274 let dir =
4275 if Filename.is_implicit dir
4276 then Filename.concat (Sys.getcwd ()) dir
4277 else dir
4279 Filename.concat dir filename
4280 else filename
4281 else E.s
4283 if Sys.file_exists path
4284 then path
4285 else E.s
4287 let path = getpath filename in
4288 let dospawn lcmd =
4289 if conf.riani
4290 then
4291 let cmd = Lazy.force_val lcmd in
4292 match spawn cmd with
4293 | _pid -> ()
4294 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
4295 else
4296 let anchor = getanchor () in
4297 let ranchor = state.path, state.password, anchor, state.origin in
4298 state.origin <- E.s;
4299 state.ranchors <- ranchor :: state.ranchors;
4300 opendoc path E.s;
4302 if substratis spec 0 "page="
4303 then
4304 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4305 | pageno ->
4306 state.anchor <- (pageno, 0.0, 0.0);
4307 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4308 | exception exn ->
4309 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4310 else (
4311 state.nameddest <- dest;
4312 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4316 let gotounder = function
4317 | Ulinkuri s when isexternallink s ->
4318 if substratis s 0 "file://"
4319 then gotoremote @@ String.sub s 7 (String.length s - 7)
4320 else gotouri s
4321 | Ulinkuri s ->
4322 let pageno, x, y = uritolocation s in
4323 addnav ();
4324 gotopagexy pageno x y
4325 | Utext _ | Unone -> ()
4326 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4329 let gotooutline (_, _, kind) =
4330 match kind with
4331 | Onone -> ()
4332 | Oanchor anchor ->
4333 let (pageno, y, _) = anchor in
4334 let y = getanchory
4335 (if conf.presentation then (pageno, y, 1.0) else anchor)
4337 addnav ();
4338 gotoghyll y
4339 | Ouri uri -> gotounder (Ulinkuri uri)
4340 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4341 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4342 | Ohistory hist -> gotohist hist
4343 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4346 class outlinesoucebase fetchoutlines = object (self)
4347 inherit lvsourcebase
4348 val mutable m_items = E.a
4349 val mutable m_minfo = E.a
4350 val mutable m_orig_items = E.a
4351 val mutable m_orig_minfo = E.a
4352 val mutable m_narrow_patterns = []
4353 val mutable m_gen = -1
4355 method getitemcount = Array.length m_items
4357 method getitem n =
4358 let s, n, _ = m_items.(n) in
4359 (s, n+0)
4361 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4362 ignore (uioh, first);
4363 let items, minfo =
4364 if m_narrow_patterns = []
4365 then m_orig_items, m_orig_minfo
4366 else m_items, m_minfo
4368 m_pan <- pan;
4369 if not cancel
4370 then (
4371 m_items <- items;
4372 m_minfo <- minfo;
4373 gotooutline m_items.(active);
4375 else (
4376 m_items <- items;
4377 m_minfo <- minfo;
4379 None
4381 method hasaction (_:int) = true
4383 method greetmsg =
4384 if Array.length m_items != Array.length m_orig_items
4385 then
4386 let s =
4387 match m_narrow_patterns with
4388 | one :: [] -> one
4389 | many -> String.concat UniSyms.ellipsis (List.rev many)
4391 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4392 else E.s
4394 method statestr =
4395 match m_narrow_patterns with
4396 | [] -> E.s
4397 | one :: [] -> one
4398 | head :: _ -> UniSyms.ellipsis ^ head
4400 method narrow pattern =
4401 match Str.regexp_case_fold pattern with
4402 | exception _ -> ()
4403 | re ->
4404 let rec loop accu minfo n =
4405 if n = -1
4406 then (
4407 m_items <- Array.of_list accu;
4408 m_minfo <- Array.of_list minfo;
4410 else
4411 let (s, _, _) as o = m_items.(n) in
4412 let accu, minfo =
4413 match Str.search_forward re s 0 with
4414 | exception Not_found -> accu, minfo
4415 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4417 loop accu minfo (n-1)
4419 loop [] [] (Array.length m_items - 1)
4421 method! getminfo = m_minfo
4423 method denarrow =
4424 m_orig_items <- fetchoutlines ();
4425 m_minfo <- m_orig_minfo;
4426 m_items <- m_orig_items
4428 method add_narrow_pattern pattern =
4429 m_narrow_patterns <- pattern :: m_narrow_patterns
4431 method del_narrow_pattern =
4432 match m_narrow_patterns with
4433 | _ :: rest -> m_narrow_patterns <- rest
4434 | [] -> ()
4436 method renarrow =
4437 self#denarrow;
4438 match m_narrow_patterns with
4439 | pattern :: [] -> self#narrow pattern; pattern
4440 | list ->
4441 List.fold_left (fun accu pattern ->
4442 self#narrow pattern;
4443 pattern ^ UniSyms.ellipsis ^ accu) E.s list
4445 method calcactive (_:anchor) = 0
4447 method reset anchor items =
4448 if state.gen != m_gen
4449 then (
4450 m_orig_items <- items;
4451 m_items <- items;
4452 m_narrow_patterns <- [];
4453 m_minfo <- E.a;
4454 m_orig_minfo <- E.a;
4455 m_gen <- state.gen;
4457 else (
4458 if items != m_orig_items
4459 then (
4460 m_orig_items <- items;
4461 if m_narrow_patterns == []
4462 then m_items <- items;
4465 let active = self#calcactive anchor in
4466 m_active <- active;
4467 m_first <- firstof m_first active
4471 let outlinesource fetchoutlines =
4472 (object
4473 inherit outlinesoucebase fetchoutlines
4474 method! calcactive anchor =
4475 let rely = getanchory anchor in
4476 let rec loop n best bestd =
4477 if n = Array.length m_items
4478 then best
4479 else
4480 let _, _, kind = m_items.(n) in
4481 match kind with
4482 | Oanchor anchor ->
4483 let orely = getanchory anchor in
4484 let d = abs (orely - rely) in
4485 if d < bestd
4486 then loop (n+1) n d
4487 else loop (n+1) best bestd
4488 | Onone | Oremote _ | Olaunch _
4489 | Oremotedest _ | Ouri _ | Ohistory _ ->
4490 loop (n+1) best bestd
4492 loop 0 ~-1 max_int
4493 end)
4496 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4497 let mkselector sourcetype =
4498 let fetchoutlines () =
4499 match sourcetype with
4500 | `bookmarks -> Array.of_list state.bookmarks
4501 | `outlines -> state.outlines
4502 | `history -> genhistoutlines () |> Array.of_list
4504 let source =
4505 if sourcetype = `history
4506 then new outlinesoucebase fetchoutlines
4507 else outlinesource fetchoutlines
4509 (fun errmsg ->
4510 let outlines = fetchoutlines () in
4511 if Array.length outlines = 0
4512 then showtext ' ' errmsg
4513 else (
4514 resetmstate ();
4515 Wsi.setcursor Wsi.CURSOR_INHERIT;
4516 let anchor = getanchor () in
4517 source#reset anchor outlines;
4518 state.text <- source#greetmsg;
4519 state.uioh <-
4520 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4521 G.postRedisplay "enter selector";
4525 let mkenter sourcetype errmsg =
4526 let enter = mkselector sourcetype in
4527 fun () -> enter errmsg
4529 ( mkenter `outlines "document has no outline"
4530 , mkenter `bookmarks "document has no bookmarks (yet)"
4531 , mkenter `history "history is empty" )
4534 let quickbookmark ?title () =
4535 match state.layout with
4536 | [] -> ()
4537 | l :: _ ->
4538 let title =
4539 match title with
4540 | None ->
4541 Unix.(
4542 let tm = localtime (now ()) in
4543 Printf.sprintf
4544 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
4545 (l.pageno+1)
4546 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
4548 | Some title -> title
4550 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4553 let setautoscrollspeed step goingdown =
4554 let incr = max 1 ((abs step) / 2) in
4555 let incr = if goingdown then incr else -incr in
4556 let astep = boundastep state.winh (step + incr) in
4557 state.autoscroll <- Some astep;
4560 let canpan () =
4561 match conf.columns with
4562 | Csplit _ -> true
4563 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4566 let panbound x = bound x (-state.w) state.winw;;
4568 let existsinrow pageno (columns, coverA, coverB) p =
4569 let last = ((pageno - coverA) mod columns) + columns in
4570 let rec any = function
4571 | [] -> false
4572 | l :: rest ->
4573 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4574 then p l
4575 else (
4576 if not (p l)
4577 then (if l.pageno = last then false else any rest)
4578 else true
4581 any state.layout
4584 let nextpage () =
4585 match state.layout with
4586 | [] ->
4587 let pageno = page_of_y state.y in
4588 gotoghyll (getpagey (pageno+1))
4589 | l :: rest ->
4590 match conf.columns with
4591 | Csingle _ ->
4592 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4593 then
4594 let y = clamp (pgscale state.winh) in
4595 gotoghyll y
4596 else
4597 let pageno = min (l.pageno+1) (state.pagecount-1) in
4598 gotoghyll (getpagey pageno)
4599 | Cmulti ((c, _, _) as cl, _) ->
4600 if conf.presentation
4601 && (existsinrow l.pageno cl
4602 (fun l -> l.pageh > l.pagey + l.pagevh))
4603 then
4604 let y = clamp (pgscale state.winh) in
4605 gotoghyll y
4606 else
4607 let pageno = min (l.pageno+c) (state.pagecount-1) in
4608 gotoghyll (getpagey pageno)
4609 | Csplit (n, _) ->
4610 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4611 then
4612 let pagey, pageh = getpageyh l.pageno in
4613 let pagey = pagey + pageh * l.pagecol in
4614 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4615 gotoghyll (pagey + pageh + ips)
4618 let prevpage () =
4619 match state.layout with
4620 | [] ->
4621 let pageno = page_of_y state.y in
4622 gotoghyll (getpagey (pageno-1))
4623 | l :: _ ->
4624 match conf.columns with
4625 | Csingle _ ->
4626 if conf.presentation && l.pagey != 0
4627 then
4628 gotoghyll (clamp (pgscale ~-(state.winh)))
4629 else
4630 let pageno = max 0 (l.pageno-1) in
4631 gotoghyll (getpagey pageno)
4632 | Cmulti ((c, _, coverB) as cl, _) ->
4633 if conf.presentation &&
4634 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4635 then
4636 gotoghyll (clamp (pgscale ~-(state.winh)))
4637 else
4638 let decr =
4639 if l.pageno = state.pagecount - coverB
4640 then 1
4641 else c
4643 let pageno = max 0 (l.pageno-decr) in
4644 gotoghyll (getpagey pageno)
4645 | Csplit (n, _) ->
4646 let y =
4647 if l.pagecol = 0
4648 then
4649 if l.pageno = 0
4650 then l.pagey
4651 else
4652 let pageno = max 0 (l.pageno-1) in
4653 let pagey, pageh = getpageyh pageno in
4654 pagey + (n-1)*pageh
4655 else
4656 let pagey, pageh = getpageyh l.pageno in
4657 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4659 gotoghyll y
4662 let save () =
4663 if emptystr conf.savecmd
4664 then adderrmsg "savepath-command is empty"
4665 "don't know where to save modified document"
4666 else
4667 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4668 let path =
4669 getcmdoutput
4670 (fun exn ->
4671 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
4672 savecmd
4674 if nonemptystr path
4675 then
4676 let tmp = path ^ ".tmp" in
4677 savedoc tmp;
4678 Unix.rename tmp path;
4681 let viewkeyboard key mask =
4682 let enttext te =
4683 let mode = state.mode in
4684 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4685 state.text <- E.s;
4686 enttext ();
4687 G.postRedisplay "view:enttext"
4689 let ctrl = Wsi.withctrl mask in
4690 let open Keys in
4691 match Wsi.kc2kt key with
4692 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
4694 | Ascii 'Q' -> exit 0
4696 | Ascii 'W' ->
4697 if hasunsavedchanges ()
4698 then save ()
4700 | Insert ->
4701 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4702 then (
4703 state.mode <- (
4704 match state.lnava with
4705 | None -> LinkNav (Ltgendir 0)
4706 | Some pn -> LinkNav (Ltexact pn)
4708 gotoxy state.x state.y;
4710 else impmsg "keyboard link navigation does not work under rotation"
4712 | Escape | Ascii 'q' ->
4713 begin match state.mstate with
4714 | Mzoomrect _ ->
4715 resetmstate ();
4716 G.postRedisplay "kill rect";
4717 | Msel _
4718 | Mpan _
4719 | Mscrolly | Mscrollx
4720 | Mzoom _
4721 | Mnone ->
4722 begin match state.mode with
4723 | LinkNav ln ->
4724 begin match ln with
4725 | Ltexact pl -> state.lnava <- Some pl
4726 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4727 end;
4728 state.mode <- View;
4729 G.postRedisplay "esc leave linknav"
4730 | Birdseye _ | Textentry _ | View ->
4731 match state.ranchors with
4732 | [] -> raise Quit
4733 | (path, password, anchor, origin) :: rest ->
4734 state.ranchors <- rest;
4735 state.anchor <- anchor;
4736 state.origin <- origin;
4737 state.nameddest <- E.s;
4738 opendoc path password
4739 end;
4740 end;
4742 | Backspace ->
4743 addnavnorc ();
4744 gotoghyll (getnav ~-1)
4746 | Ascii 'o' ->
4747 enteroutlinemode ()
4749 | Ascii 'H' ->
4750 enterhistmode ()
4752 | Ascii 'u' ->
4753 state.rects <- [];
4754 state.text <- E.s;
4755 Hashtbl.iter (fun _ opaque ->
4756 clearmark opaque;
4757 Hashtbl.clear state.prects) state.pagemap;
4758 G.postRedisplay "dehighlight";
4760 | Ascii (('/' | '?') as c) ->
4761 let ondone isforw s =
4762 cbput state.hists.pat s;
4763 state.searchpattern <- s;
4764 search s isforw
4766 let s = String.make 1 c in
4767 enttext (s, E.s, Some (onhist state.hists.pat),
4768 textentry, ondone (c = '/'), true)
4770 | Ascii '+' | Ascii '=' when ctrl ->
4771 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4772 pivotzoom (conf.zoom +. incr)
4774 | Ascii '+' ->
4775 let ondone s =
4776 let n =
4777 try int_of_string s with exn ->
4778 state.text <-
4779 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4780 max_int
4782 if n != max_int
4783 then (
4784 conf.pagebias <- n;
4785 state.text <- "page bias is now " ^ string_of_int n;
4788 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4790 | Ascii '-' when ctrl ->
4791 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4792 pivotzoom (max 0.01 (conf.zoom -. decr))
4794 | Ascii '-' ->
4795 let ondone msg = state.text <- msg in
4796 enttext (
4797 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4798 optentry state.mode, ondone, true
4801 | Ascii '0' when ctrl ->
4802 if conf.zoom = 1.0
4803 then gotoxy 0 state.y
4804 else setzoom 1.0
4806 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
4807 let cols =
4808 match conf.columns with
4809 | Csingle _ | Cmulti _ -> 1
4810 | Csplit (n, _) -> n
4812 let h = state.winh -
4813 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4815 let zoom = zoomforh state.winw h 0 cols in
4816 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4817 then setzoom zoom
4819 | Ascii '3' when ctrl ->
4820 let fm =
4821 match conf.fitmodel with
4822 | FitWidth -> FitProportional
4823 | FitProportional -> FitPage
4824 | FitPage -> FitWidth
4826 state.text <- "fit model: " ^ FMTE.to_string fm;
4827 reqlayout conf.angle fm
4829 | Ascii '4' when ctrl ->
4830 let zoom = getmaxw () /. float state.winw in
4831 if zoom > 0.0 then setzoom zoom
4833 | Fn 9 ->
4834 togglebirdseye ()
4836 | Ascii '9' when ctrl ->
4837 togglebirdseye ()
4839 | Ascii ('0'..'9' as c) when not ctrl ->
4840 let ondone s =
4841 let n =
4842 try int_of_string s with exn ->
4843 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4846 if n >= 0
4847 then (
4848 addnav ();
4849 cbput state.hists.pag (string_of_int n);
4850 gotopage1 (n + conf.pagebias - 1) 0;
4853 let [@warning "-4"] pageentry text = function
4854 | Keys.Ascii 'g' -> TEdone text
4855 | key -> intentry text key
4857 let text = String.make 1 c in
4858 enttext (":", text, Some (onhist state.hists.pag),
4859 pageentry, ondone, true)
4861 | Ascii 'b' ->
4862 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4863 G.postRedisplay "toggle scrollbar";
4865 | Ascii 'B' ->
4866 state.bzoom <- not state.bzoom;
4867 state.rects <- [];
4868 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4870 | Ascii 'l' ->
4871 conf.hlinks <- not conf.hlinks;
4872 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4873 G.postRedisplay "toggle highlightlinks";
4875 | Ascii 'F' ->
4876 if conf.angle mod 360 = 0
4877 then (
4878 state.glinks <- true;
4879 let mode = state.mode in
4880 state.mode <-
4881 Textentry (
4882 (":", E.s, None, linknentry, linknact gotounder, false),
4883 (fun _ ->
4884 state.glinks <- false;
4885 state.mode <- mode)
4887 state.text <- E.s;
4888 G.postRedisplay "view:linkent(F)"
4890 else impmsg "hint mode does not work under rotation"
4892 | Ascii 'y' ->
4893 state.glinks <- true;
4894 let mode = state.mode in
4895 state.mode <-
4896 Textentry (
4897 (":", E.s, None, linknentry,
4898 linknact (fun under -> selstring (undertext under)), false),
4899 (fun _ ->
4900 state.glinks <- false;
4901 state.mode <- mode)
4903 state.text <- E.s;
4904 G.postRedisplay "view:linkent"
4906 | Ascii 'a' ->
4907 begin match state.autoscroll with
4908 | Some step ->
4909 conf.autoscrollstep <- step;
4910 state.autoscroll <- None
4911 | None ->
4912 state.autoscroll <- Some conf.autoscrollstep;
4913 state.slideshow <- state.slideshow land lnot 2
4916 | Ascii 'p' when ctrl ->
4917 launchpath () (* XXX where do error messages go? *)
4919 | Ascii 'P' ->
4920 setpresentationmode (not conf.presentation);
4921 showtext ' ' ("presentation mode " ^
4922 if conf.presentation then "on" else "off");
4924 | Ascii 'f' ->
4925 if List.mem Wsi.Fullscreen state.winstate
4926 then Wsi.reshape conf.cwinw conf.cwinh
4927 else Wsi.fullscreen ()
4929 | Ascii ('p'|'N') ->
4930 search state.searchpattern false
4932 | Ascii 'n' | Fn 3 ->
4933 search state.searchpattern true
4935 | Ascii 't' ->
4936 begin match state.layout with
4937 | [] -> ()
4938 | l :: _ ->
4939 gotoghyll (getpagey l.pageno)
4942 | Ascii ' ' ->
4943 nextpage ()
4945 | Delete ->
4946 prevpage ()
4948 | Ascii '=' ->
4949 showtext ' ' (describe_layout state.layout);
4951 | Ascii 'w' ->
4952 begin match state.layout with
4953 | [] -> ()
4954 | l :: _ ->
4955 Wsi.reshape l.pagew l.pageh;
4956 G.postRedisplay "w"
4959 | Ascii '\'' ->
4960 enterbookmarkmode ()
4962 | Ascii 'h' | Fn 1 ->
4963 enterhelpmode ()
4965 | Ascii 'i' ->
4966 enterinfomode ()
4968 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
4969 entermsgsmode ()
4971 | Ascii 'm' ->
4972 let ondone s =
4973 match state.layout with
4974 | l :: _ ->
4975 if nonemptystr s
4976 then
4977 state.bookmarks <-
4978 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4979 | _ -> ()
4981 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4983 | Ascii '~' ->
4984 quickbookmark ();
4985 showtext ' ' "Quick bookmark added";
4987 | Ascii 'z' ->
4988 begin match state.layout with
4989 | l :: _ ->
4990 let rect = getpdimrect l.pagedimno in
4991 let w, h =
4992 if conf.crophack
4993 then
4994 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4995 truncate (1.2 *. (rect.(3) -. rect.(0))))
4996 else
4997 (truncate (rect.(1) -. rect.(0)),
4998 truncate (rect.(3) -. rect.(0)))
5000 let w = truncate ((float w)*.conf.zoom)
5001 and h = truncate ((float h)*.conf.zoom) in
5002 if w != 0 && h != 0
5003 then (
5004 state.anchor <- getanchor ();
5005 Wsi.reshape w (h + conf.interpagespace)
5007 G.postRedisplay "z";
5009 | [] -> ()
5012 | Ascii 'x' -> state.roam ()
5014 | Ascii ('<'|'>' as c) ->
5015 reqlayout
5016 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
5018 | Ascii ('['|']' as c) ->
5019 conf.colorscale <-
5020 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
5021 G.postRedisplay "brightness";
5023 | Ascii 'c' when state.mode = View ->
5024 if Wsi.withalt mask
5025 then (
5026 if conf.zoom > 1.0
5027 then
5028 let m = (state.winw - state.w) / 2 in
5029 gotoxy_and_clear_text m state.y
5031 else
5032 let (c, a, b), z =
5033 match state.prevcolumns with
5034 | None -> (1, 0, 0), 1.0
5035 | Some (columns, z) ->
5036 let cab =
5037 match columns with
5038 | Csplit (c, _) -> -c, 0, 0
5039 | Cmulti ((c, a, b), _) -> c, a, b
5040 | Csingle _ -> 1, 0, 0
5042 cab, z
5044 setcolumns View c a b;
5045 setzoom z
5047 | Down | Up when ctrl && Wsi.withshift mask ->
5048 let zoom, x = state.prevzoom in
5049 setzoom zoom;
5050 state.x <- x;
5052 | Ascii 'k' | Up ->
5053 begin match state.autoscroll with
5054 | None ->
5055 begin match state.mode with
5056 | Birdseye beye -> upbirdseye 1 beye
5057 | Textentry _ | View | LinkNav _ ->
5058 if ctrl
5059 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5060 else (
5061 if not (Wsi.withshift mask) && conf.presentation
5062 then prevpage ()
5063 else gotoghyll1 true (clamp (-conf.scrollstep))
5066 | Some n ->
5067 setautoscrollspeed n false
5070 | Ascii 'j' | Down ->
5071 begin match state.autoscroll with
5072 | None ->
5073 begin match state.mode with
5074 | Birdseye beye -> downbirdseye 1 beye
5075 | Textentry _ | View | LinkNav _ ->
5076 if ctrl
5077 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5078 else (
5079 if not (Wsi.withshift mask) && conf.presentation
5080 then nextpage ()
5081 else gotoghyll1 true (clamp (conf.scrollstep))
5084 | Some n ->
5085 setautoscrollspeed n true
5088 | Left | Right when not (Wsi.withalt mask) ->
5089 if canpan ()
5090 then
5091 let dx =
5092 if ctrl
5093 then state.winw / 2
5094 else conf.hscrollstep
5096 let dx =
5097 let pv = Wsi.kc2kt key in
5098 if pv = Keys.Left then dx else -dx
5100 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5101 else (
5102 state.text <- E.s;
5103 G.postRedisplay "left/right"
5106 | Prior ->
5107 let y =
5108 if ctrl
5109 then
5110 match state.layout with
5111 | [] -> state.y
5112 | l :: _ -> state.y - l.pagey
5113 else
5114 clamp (pgscale (-state.winh))
5116 gotoghyll y
5118 | Next ->
5119 let y =
5120 if ctrl
5121 then
5122 match List.rev state.layout with
5123 | [] -> state.y
5124 | l :: _ -> getpagey l.pageno
5125 else
5126 clamp (pgscale state.winh)
5128 gotoghyll y
5130 | Ascii 'g' | Home ->
5131 addnav ();
5132 gotoghyll 0
5133 | Ascii 'G' | End ->
5134 addnav ();
5135 gotoghyll (clamp state.maxy)
5137 | Right when Wsi.withalt mask ->
5138 addnavnorc ();
5139 gotoghyll (getnav 1)
5140 | Left when Wsi.withalt mask ->
5141 addnavnorc ();
5142 gotoghyll (getnav ~-1)
5144 | Ascii 'r' ->
5145 reload ()
5147 | Ascii 'v' when conf.debug ->
5148 state.rects <- [];
5149 List.iter (fun l ->
5150 match getopaque l.pageno with
5151 | None -> ()
5152 | Some opaque ->
5153 let x0, y0, x1, y1 = pagebbox opaque in
5154 let rect = (float x0, float y0,
5155 float x1, float y0,
5156 float x1, float y1,
5157 float x0, float y1) in
5158 debugrect rect;
5159 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5160 state.rects <- (l.pageno, color, rect) :: state.rects;
5161 ) state.layout;
5162 G.postRedisplay "v";
5164 | Ascii '|' ->
5165 let mode = state.mode in
5166 let cmd = ref E.s in
5167 let onleave = function
5168 | Cancel -> state.mode <- mode
5169 | Confirm ->
5170 List.iter (fun l ->
5171 match getopaque l.pageno with
5172 | Some opaque -> pipesel opaque !cmd
5173 | None -> ()) state.layout;
5174 state.mode <- mode
5176 let ondone s =
5177 cbput state.hists.sel s;
5178 cmd := s
5180 let te =
5181 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5183 G.postRedisplay "|";
5184 state.mode <- Textentry (te, onleave);
5186 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
5187 vlog "huh? %s" (Wsi.keyname key)
5190 let linknavkeyboard key mask linknav =
5191 let pv = Wsi.kc2kt key in
5192 let getpage pageno =
5193 let rec loop = function
5194 | [] -> None
5195 | l :: _ when l.pageno = pageno -> Some l
5196 | _ :: rest -> loop rest
5197 in loop state.layout
5199 let doexact (pageno, n) =
5200 match getopaque pageno, getpage pageno with
5201 | Some opaque, Some l ->
5202 if pv = Keys.Enter
5203 then
5204 let under = getlink opaque n in
5205 G.postRedisplay "link gotounder";
5206 gotounder under;
5207 state.mode <- View;
5208 else
5209 let opt, dir =
5210 let open Keys in
5211 match pv with
5212 | Home -> Some (findlink opaque LDfirst), -1
5213 | End -> Some (findlink opaque LDlast), 1
5214 | Left -> Some (findlink opaque (LDleft n)), -1
5215 | Right -> Some (findlink opaque (LDright n)), 1
5216 | Up -> Some (findlink opaque (LDup n)), -1
5217 | Down -> Some (findlink opaque (LDdown n)), 1
5219 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
5220 | Code _|Fn _|Ctrl _|Backspace -> None, 0
5222 let pwl l dir =
5223 begin match findpwl l.pageno dir with
5224 | Pwlnotfound -> ()
5225 | Pwl pageno ->
5226 let notfound dir =
5227 state.mode <- LinkNav (Ltgendir dir);
5228 let y, h = getpageyh pageno in
5229 let y =
5230 if dir < 0
5231 then y + h - state.winh
5232 else y
5234 gotoxy state.x y
5236 begin match getopaque pageno, getpage pageno with
5237 | Some opaque, Some _ ->
5238 let link =
5239 let ld = if dir > 0 then LDfirst else LDlast in
5240 findlink opaque ld
5242 begin match link with
5243 | Lfound m ->
5244 showlinktype (getlink opaque m);
5245 state.mode <- LinkNav (Ltexact (pageno, m));
5246 G.postRedisplay "linknav jpage";
5247 | Lnotfound -> notfound dir
5248 end;
5249 | _ -> notfound dir
5250 end;
5251 end;
5253 begin match opt with
5254 | Some Lnotfound -> pwl l dir;
5255 | Some (Lfound m) ->
5256 if m = n
5257 then pwl l dir
5258 else (
5259 let _, y0, _, y1 = getlinkrect opaque m in
5260 if y0 < l.pagey
5261 then gotopage1 l.pageno y0
5262 else (
5263 let d = fstate.fontsize + 1 in
5264 if y1 - l.pagey > l.pagevh - d
5265 then gotopage1 l.pageno (y1 - state.winh + d)
5266 else G.postRedisplay "linknav";
5268 showlinktype (getlink opaque m);
5269 state.mode <- LinkNav (Ltexact (l.pageno, m));
5272 | None -> viewkeyboard key mask
5273 end;
5274 | _ -> viewkeyboard key mask
5276 if pv = Keys.Insert
5277 then (
5278 begin match linknav with
5279 | Ltexact pa -> state.lnava <- Some pa
5280 | Ltgendir _ | Ltnotready _ -> ()
5281 end;
5282 state.mode <- View;
5283 G.postRedisplay "leave linknav"
5285 else
5286 match linknav with
5287 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5288 | Ltexact exact -> doexact exact
5291 let keyboard key mask =
5292 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5293 then wcmd "interrupt"
5294 else state.uioh <- state.uioh#key key mask
5297 let birdseyekeyboard key mask
5298 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5299 let incr =
5300 match conf.columns with
5301 | Csingle _ -> 1
5302 | Cmulti ((c, _, _), _) -> c
5303 | Csplit _ -> failwith "bird's eye split mode"
5305 let pgh layout = List.fold_left
5306 (fun m l -> max l.pageh m) state.winh layout in
5307 let open Keys in
5308 match Wsi.kc2kt key with
5309 | Ascii 'l' when Wsi.withctrl mask ->
5310 let y, h = getpageyh pageno in
5311 let top = (state.winh - h) / 2 in
5312 gotoxy state.x (max 0 (y - top))
5313 | Enter -> leavebirdseye beye false
5314 | Escape -> leavebirdseye beye true
5315 | Up -> upbirdseye incr beye
5316 | Down -> downbirdseye incr beye
5317 | Left -> upbirdseye 1 beye
5318 | Right -> downbirdseye 1 beye
5320 | Prior ->
5321 begin match state.layout with
5322 | l :: _ ->
5323 if l.pagey != 0
5324 then (
5325 state.mode <- Birdseye (
5326 oconf, leftx, l.pageno, hooverpageno, anchor
5328 gotopage1 l.pageno 0;
5330 else (
5331 let layout = layout state.x (state.y-state.winh)
5332 state.winw
5333 (pgh state.layout) in
5334 match layout with
5335 | [] -> gotoxy state.x (clamp (-state.winh))
5336 | l :: _ ->
5337 state.mode <- Birdseye (
5338 oconf, leftx, l.pageno, hooverpageno, anchor
5340 gotopage1 l.pageno 0
5343 | [] -> gotoxy state.x (clamp (-state.winh))
5344 end;
5346 | Next ->
5347 begin match List.rev state.layout with
5348 | l :: _ ->
5349 let layout = layout state.x
5350 (state.y + (pgh state.layout))
5351 state.winw state.winh in
5352 begin match layout with
5353 | [] ->
5354 let incr = l.pageh - l.pagevh in
5355 if incr = 0
5356 then (
5357 state.mode <-
5358 Birdseye (
5359 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5361 G.postRedisplay "birdseye pagedown";
5363 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5365 | l :: _ ->
5366 state.mode <-
5367 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5368 gotopage1 l.pageno 0;
5371 | [] -> gotoxy state.x (clamp state.winh)
5372 end;
5374 | Home ->
5375 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5376 gotopage1 0 0
5378 | End ->
5379 let pageno = state.pagecount - 1 in
5380 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5381 if not (pagevisible state.layout pageno)
5382 then
5383 let h =
5384 match List.rev state.pdims with
5385 | [] -> state.winh
5386 | (_, _, h, _) :: _ -> h
5388 gotoxy
5389 state.x
5390 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5391 else G.postRedisplay "birdseye end";
5393 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
5396 let drawpage l =
5397 let color =
5398 match state.mode with
5399 | Textentry _ -> scalecolor 0.4
5400 | LinkNav _ | View -> scalecolor 1.0
5401 | Birdseye (_, _, pageno, hooverpageno, _) ->
5402 if l.pageno = hooverpageno
5403 then scalecolor 0.9
5404 else (
5405 if l.pageno = pageno
5406 then (
5407 let c = scalecolor 1.0 in
5408 GlDraw.color c;
5409 GlDraw.line_width 3.0;
5410 let dispx = l.pagedispx in
5411 linerect
5412 (float (dispx-1)) (float (l.pagedispy-1))
5413 (float (dispx+l.pagevw+1))
5414 (float (l.pagedispy+l.pagevh+1))
5416 GlDraw.line_width 1.0;
5419 else scalecolor 0.8
5422 drawtiles l color;
5425 let postdrawpage l linkindexbase =
5426 match getopaque l.pageno with
5427 | Some opaque ->
5428 if tileready l l.pagex l.pagey
5429 then
5430 let x = l.pagedispx - l.pagex
5431 and y = l.pagedispy - l.pagey in
5432 let hlmask =
5433 match conf.columns with
5434 | Csingle _ | Cmulti _ ->
5435 (if conf.hlinks then 1 else 0)
5436 + (if state.glinks
5437 && not (isbirdseye state.mode) then 2 else 0)
5438 | Csplit _ -> 0
5440 let s =
5441 match state.mode with
5442 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5443 | Textentry _
5444 | Birdseye _
5445 | View
5446 | LinkNav _ -> E.s
5448 Hashtbl.find_all state.prects l.pageno |>
5449 List.iter (fun vals -> drawprect opaque x y vals);
5450 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5451 if n < 0
5452 then (state.redisplay <- true; 0)
5453 else n
5454 else 0
5455 | _ -> 0
5458 let scrollindicator () =
5459 let sbw, ph, sh = state.uioh#scrollph in
5460 let sbh, pw, sw = state.uioh#scrollpw in
5462 let x0,x1,hx0 =
5463 if conf.leftscroll
5464 then (0, sbw, sbw)
5465 else ((state.winw - sbw), state.winw, 0)
5468 Gl.enable `blend;
5469 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5470 let (r, g, b, alpha) = conf.sbarcolor in
5471 GlDraw.color (r, g, b) ~alpha;
5472 filledrect (float x0) 0. (float x1) (float state.winh);
5473 filledrect
5474 (float hx0) (float (state.winh - sbh))
5475 (float (hx0 + state.winw)) (float state.winh);
5476 let (r, g, b, alpha) = conf.sbarhndlcolor in
5477 GlDraw.color (r, g, b) ~alpha;
5479 filledrect (float x0) ph (float x1) (ph +. sh);
5480 let pw = pw +. float hx0 in
5481 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5482 Gl.disable `blend;
5485 let showsel () =
5486 match state.mstate with
5487 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5490 | Msel ((x0, y0), (x1, y1)) ->
5491 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5492 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5493 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5494 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5497 let showrects =
5498 function [] -> ()
5499 | rects ->
5500 Gl.enable `blend;
5501 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5502 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5503 List.iter
5504 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5505 List.iter (fun l ->
5506 if l.pageno = pageno
5507 then (
5508 let dx = float (l.pagedispx - l.pagex) in
5509 let dy = float (l.pagedispy - l.pagey) in
5510 let r, g, b, alpha = c in
5511 GlDraw.color (r, g, b) ~alpha;
5512 filledrect2 (x0+.dx) (y0+.dy)
5513 (x1+.dx) (y1+.dy)
5514 (x3+.dx) (y3+.dy)
5515 (x2+.dx) (y2+.dy);
5517 ) state.layout
5518 ) rects;
5519 Gl.disable `blend;
5522 let display () =
5523 GlDraw.color (scalecolor2 conf.bgcolor);
5524 GlClear.color (scalecolor2 conf.bgcolor);
5525 GlClear.clear [`color];
5526 List.iter drawpage state.layout;
5527 let rects =
5528 match state.mode with
5529 | LinkNav (Ltexact (pageno, linkno)) ->
5530 begin match getopaque pageno with
5531 | Some opaque ->
5532 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5533 let color = (0.0, 0.0, 0.5, 0.5) in
5534 (pageno, color,
5535 (float x0, float y0,
5536 float x1, float y0,
5537 float x1, float y1,
5538 float x0, float y1)
5539 ) :: state.rects
5540 | None -> state.rects
5542 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5543 | Birdseye _
5544 | Textentry _
5545 | View -> state.rects
5547 showrects rects;
5548 let rec postloop linkindexbase = function
5549 | l :: rest ->
5550 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5551 postloop linkindexbase rest
5552 | [] -> ()
5554 showsel ();
5555 postloop 0 state.layout;
5556 state.uioh#display;
5557 begin match state.mstate with
5558 | Mzoomrect ((x0, y0), (x1, y1)) ->
5559 Gl.enable `blend;
5560 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5561 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5562 filledrect (float x0) (float y0) (float x1) (float y1);
5563 Gl.disable `blend;
5564 | Msel _
5565 | Mpan _
5566 | Mscrolly | Mscrollx
5567 | Mzoom _
5568 | Mnone -> ()
5569 end;
5570 enttext ();
5571 scrollindicator ();
5572 Wsi.swapb ();
5575 let zoomrect x y x1 y1 =
5576 let x0 = min x x1
5577 and x1 = max x x1
5578 and y0 = min y y1 in
5579 let zoom = (float state.w) /. float (x1 - x0) in
5580 let margin =
5581 let simple () =
5582 if state.w < state.winw
5583 then (state.winw - state.w) / 2
5584 else 0
5586 match conf.fitmodel with
5587 | FitWidth | FitProportional -> simple ()
5588 | FitPage ->
5589 match conf.columns with
5590 | Csplit _ ->
5591 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5592 | Cmulti _ | Csingle _ -> simple ()
5594 gotoxy ((state.x + margin) - x0) (state.y + y0);
5595 state.anchor <- getanchor ();
5596 setzoom zoom;
5597 resetmstate ();
5600 let annot inline x y =
5601 match unproject x y with
5602 | Some (opaque, n, ux, uy) ->
5603 let add text =
5604 addannot opaque ux uy text;
5605 wcmd "freepage %s" (~> opaque);
5606 Hashtbl.remove state.pagemap (n, state.gen);
5607 flushtiles ();
5608 gotoxy state.x state.y
5610 if inline
5611 then
5612 let ondone s = add s in
5613 let mode = state.mode in
5614 state.mode <- Textentry (
5615 ("annotation: ", E.s, None, textentry, ondone, true),
5616 fun _ -> state.mode <- mode);
5617 state.text <- E.s;
5618 enttext ();
5619 G.postRedisplay "annot"
5620 else
5621 add @@ getusertext E.s
5622 | _ -> ()
5625 let zoomblock x y =
5626 let g opaque l px py =
5627 match rectofblock opaque px py with
5628 | Some a ->
5629 let x0 = a.(0) -. 20. in
5630 let x1 = a.(1) +. 20. in
5631 let y0 = a.(2) -. 20. in
5632 let zoom = (float state.w) /. (x1 -. x0) in
5633 let pagey = getpagey l.pageno in
5634 let margin = (state.w - l.pagew)/2 in
5635 let nx = -truncate x0 - margin in
5636 gotoxy_and_clear_text nx (pagey + truncate y0);
5637 state.anchor <- getanchor ();
5638 setzoom zoom;
5639 None
5640 | None -> None
5642 match conf.columns with
5643 | Csplit _ ->
5644 impmsg "block zooming does not work properly in split columns mode"
5645 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5648 let scrollx x =
5649 let winw = state.winw - 1 in
5650 let s = float x /. float winw in
5651 let destx = truncate (float (state.w + winw) *. s) in
5652 gotoxy_and_clear_text (winw - destx) state.y;
5653 state.mstate <- Mscrollx;
5656 let scrolly y =
5657 let s = float y /. float state.winh in
5658 let desty = truncate (s *. float (maxy ())) in
5659 gotoxy_and_clear_text state.x desty;
5660 state.mstate <- Mscrolly;
5663 let viewmulticlick clicks x y mask =
5664 let g opaque l px py =
5665 let mark =
5666 match clicks with
5667 | 2 -> Mark_word
5668 | 3 -> Mark_line
5669 | 4 -> Mark_block
5670 | _ -> Mark_page
5672 if markunder opaque px py mark
5673 then (
5674 Some (fun () ->
5675 let dopipe cmd =
5676 match getopaque l.pageno with
5677 | None -> ()
5678 | Some opaque -> pipesel opaque cmd
5680 state.roam <- (fun () -> dopipe conf.paxcmd);
5681 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5684 else None
5686 G.postRedisplay "viewmulticlick";
5687 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5690 let canselect () =
5691 match conf.columns with
5692 | Csplit _ -> false
5693 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5696 let viewmouse button down x y mask =
5697 match button with
5698 | n when (n == 4 || n == 5) && not down ->
5699 if Wsi.withctrl mask
5700 then (
5701 let incr =
5702 if n = 5
5703 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5704 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5706 let fx, fy =
5707 match state.mstate with
5708 | Mzoom (oldn, _, pos) when n = oldn -> pos
5709 | Mzoomrect _ | Mnone | Mpan _
5710 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
5712 let zoom = conf.zoom -. incr in
5713 state.mstate <- Mzoom (n, 0, (x, y));
5714 if false && abs (fx - x) > 5 || abs (fy - y) > 5
5715 then pivotzoom ~x ~y zoom
5716 else pivotzoom zoom
5718 else (
5719 match state.autoscroll with
5720 | Some step -> setautoscrollspeed step (n=4)
5721 | None ->
5722 if conf.wheelbypage || conf.presentation
5723 then (
5724 if n = 4
5725 then prevpage ()
5726 else nextpage ()
5728 else
5729 let incr =
5730 if n = 4
5731 then -conf.scrollstep
5732 else conf.scrollstep
5734 let incr = incr * 2 in
5735 let y = clamp incr in
5736 gotoxy_and_clear_text state.x y
5739 | n when (n = 6 || n = 7) && not down && canpan () ->
5740 let x =
5741 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5742 gotoxy_and_clear_text x state.y
5744 | 1 when Wsi.withshift mask ->
5745 state.mstate <- Mnone;
5746 if not down
5747 then (
5748 match unproject x y with
5749 | None -> ()
5750 | Some (_, pageno, ux, uy) ->
5751 let cmd = Printf.sprintf
5752 "%s %s %d %d %d"
5753 conf.stcmd state.path pageno ux uy
5755 match spawn cmd [] with
5756 | exception exn ->
5757 impmsg "execution of synctex command(%S) failed: %S"
5758 conf.stcmd @@ exntos exn
5759 | _pid -> ()
5762 | 1 when Wsi.withctrl mask ->
5763 if down
5764 then (
5765 Wsi.setcursor Wsi.CURSOR_FLEUR;
5766 state.mstate <- Mpan (x, y)
5768 else
5769 state.mstate <- Mnone
5771 | 3 ->
5772 if down
5773 then (
5774 if Wsi.withshift mask
5775 then (
5776 annot conf.annotinline x y;
5777 G.postRedisplay "addannot"
5779 else
5780 let p = (x, y) in
5781 Wsi.setcursor Wsi.CURSOR_CYCLE;
5782 state.mstate <- Mzoomrect (p, p)
5784 else (
5785 match state.mstate with
5786 | Mzoomrect ((x0, y0), _) ->
5787 if abs (x-x0) > 10 && abs (y - y0) > 10
5788 then zoomrect x0 y0 x y
5789 else (
5790 resetmstate ();
5791 G.postRedisplay "kill accidental zoom rect";
5793 | Msel _
5794 | Mpan _
5795 | Mscrolly | Mscrollx
5796 | Mzoom _
5797 | Mnone -> resetmstate ()
5800 | 1 when vscrollhit x ->
5801 if down
5802 then
5803 let _, position, sh = state.uioh#scrollph in
5804 if y > truncate position && y < truncate (position +. sh)
5805 then state.mstate <- Mscrolly
5806 else scrolly y
5807 else
5808 state.mstate <- Mnone
5810 | 1 when y > state.winh - hscrollh () ->
5811 if down
5812 then
5813 let _, position, sw = state.uioh#scrollpw in
5814 if x > truncate position && x < truncate (position +. sw)
5815 then state.mstate <- Mscrollx
5816 else scrollx x
5817 else
5818 state.mstate <- Mnone
5820 | 1 when state.bzoom -> if not down then zoomblock x y
5822 | 1 ->
5823 let dest = if down then getunder x y else Unone in
5824 begin match dest with
5825 | Ulinkuri _ ->
5826 gotounder dest
5828 | Unone when down ->
5829 Wsi.setcursor Wsi.CURSOR_FLEUR;
5830 state.mstate <- Mpan (x, y);
5832 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5834 | Unone | Utext _ ->
5835 if down
5836 then (
5837 if canselect ()
5838 then (
5839 state.mstate <- Msel ((x, y), (x, y));
5840 G.postRedisplay "mouse select";
5843 else (
5844 match state.mstate with
5845 | Mnone -> ()
5847 | Mzoom _ | Mscrollx | Mscrolly ->
5848 state.mstate <- Mnone
5850 | Mzoomrect ((x0, y0), _) ->
5851 zoomrect x0 y0 x y
5853 | Mpan _ ->
5854 Wsi.setcursor Wsi.CURSOR_INHERIT;
5855 state.mstate <- Mnone
5857 | Msel ((x0, y0), (x1, y1)) ->
5858 let rec loop = function
5859 | [] -> ()
5860 | l :: rest ->
5861 let inside =
5862 let a0 = l.pagedispy in
5863 let a1 = a0 + l.pagevh in
5864 let b0 = l.pagedispx in
5865 let b1 = b0 + l.pagevw in
5866 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5867 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5869 if inside
5870 then
5871 match getopaque l.pageno with
5872 | Some opaque ->
5873 let dosel cmd () =
5874 pipef ~closew:false "Msel"
5875 (fun w ->
5876 copysel w opaque;
5877 G.postRedisplay "Msel") cmd
5879 dosel conf.selcmd ();
5880 state.roam <- dosel conf.paxcmd;
5881 | None -> ()
5882 else loop rest
5884 loop state.layout;
5885 resetmstate ();
5889 | _ -> ()
5892 let birdseyemouse button down x y mask
5893 (conf, leftx, _, hooverpageno, anchor) =
5894 match button with
5895 | 1 when down ->
5896 let rec loop = function
5897 | [] -> ()
5898 | l :: rest ->
5899 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5900 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5901 then (
5902 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5904 else loop rest
5906 loop state.layout
5907 | 3 -> ()
5908 | _ -> viewmouse button down x y mask
5911 let uioh = object
5912 method display = ()
5914 method key key mask =
5915 begin match state.mode with
5916 | Textentry textentry -> textentrykeyboard key mask textentry
5917 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5918 | View -> viewkeyboard key mask
5919 | LinkNav linknav -> linknavkeyboard key mask linknav
5920 end;
5921 state.uioh
5923 method button button bstate x y mask =
5924 begin match state.mode with
5925 | LinkNav _ | View -> viewmouse button bstate x y mask
5926 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5927 | Textentry _ -> ()
5928 end;
5929 state.uioh
5931 method multiclick clicks x y mask =
5932 begin match state.mode with
5933 | LinkNav _ | View -> viewmulticlick clicks x y mask
5934 | Birdseye _ | Textentry _ -> ()
5935 end;
5936 state.uioh
5938 method motion x y =
5939 begin match state.mode with
5940 | Textentry _ -> ()
5941 | View | Birdseye _ | LinkNav _ ->
5942 match state.mstate with
5943 | Mzoom _ | Mnone -> ()
5945 | Mpan (x0, y0) ->
5946 let dx = x - x0
5947 and dy = y0 - y in
5948 state.mstate <- Mpan (x, y);
5949 let x = if canpan () then panbound (state.x + dx) else state.x in
5950 let y = clamp dy in
5951 gotoxy_and_clear_text x y
5953 | Msel (a, _) ->
5954 state.mstate <- Msel (a, (x, y));
5955 G.postRedisplay "motion select";
5957 | Mscrolly ->
5958 let y = min state.winh (max 0 y) in
5959 scrolly y
5961 | Mscrollx ->
5962 let x = min state.winw (max 0 x) in
5963 scrollx x
5965 | Mzoomrect (p0, _) ->
5966 state.mstate <- Mzoomrect (p0, (x, y));
5967 G.postRedisplay "motion zoomrect";
5968 end;
5969 state.uioh
5971 method pmotion x y =
5972 begin match state.mode with
5973 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5974 let rec loop = function
5975 | [] ->
5976 if hooverpageno != -1
5977 then (
5978 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5979 G.postRedisplay "pmotion birdseye no hoover";
5981 | l :: rest ->
5982 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5983 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5984 then (
5985 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5986 G.postRedisplay "pmotion birdseye hoover";
5988 else loop rest
5990 loop state.layout
5992 | Textentry _ -> ()
5994 | LinkNav _ | View ->
5995 match state.mstate with
5996 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
5997 | Mnone ->
5998 updateunder x y;
5999 if canselect ()
6000 then
6001 match conf.pax with
6002 | None -> ()
6003 | Some past ->
6004 let now = now () in
6005 let delta = now -. past in
6006 if delta > 0.01
6007 then paxunder x y
6008 else conf.pax <- Some now
6009 end;
6010 state.uioh
6012 method infochanged _ = ()
6014 method scrollph =
6015 let maxy = maxy () in
6016 let p, h =
6017 if maxy = 0
6018 then 0.0, float state.winh
6019 else scrollph state.y maxy
6021 vscrollw (), p, h
6023 method scrollpw =
6024 let fwinw = float (state.winw - vscrollw ()) in
6025 let sw =
6026 let sw = fwinw /. float state.w in
6027 let sw = fwinw *. sw in
6028 max sw (float conf.scrollh)
6030 let position =
6031 let maxx = state.w + state.winw in
6032 let x = state.winw - state.x in
6033 let percent = float x /. float maxx in
6034 (fwinw -. sw) *. percent
6036 hscrollh (), position, sw
6038 method modehash =
6039 let modename =
6040 match state.mode with
6041 | LinkNav _ -> "links"
6042 | Textentry _ -> "textentry"
6043 | Birdseye _ -> "birdseye"
6044 | View -> "view"
6046 findkeyhash conf modename
6048 method eformsgs = true
6049 method alwaysscrolly = false
6050 method scroll dx dy =
6051 let x = if canpan () then panbound (state.x + dx) else state.x in
6052 gotoxy_and_clear_text x (clamp (2 * dy));
6053 state.uioh
6054 method zoom z x y =
6055 pivotzoom ~x ~y (conf.zoom *. exp z);
6056 end;;
6058 let addrect pageno r g b a x0 y0 x1 y1 =
6059 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6062 let ract cmds =
6063 let cl = splitatchar cmds ' ' in
6064 let scan s fmt f =
6065 try Scanf.sscanf s fmt f
6066 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
6067 cmds @@ exntos exn
6069 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6070 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6071 s pageno r g b a x0 y0 x1 y1;
6072 onpagerect
6073 pageno
6074 (fun w h ->
6075 let _,w1,h1,_ = getpagedim pageno in
6076 let sw = float w1 /. float w
6077 and sh = float h1 /. float h in
6078 let x0s = x0 *. sw
6079 and x1s = x1 *. sw
6080 and y0s = y0 *. sh
6081 and y1s = y1 *. sh in
6082 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6083 let color = (r, g, b, a) in
6084 if conf.verbose then debugrect rect;
6085 state.rects <- (pageno, color, rect) :: state.rects;
6086 G.postRedisplay s;
6089 match cl with
6090 | "reload", "" -> reload ()
6091 | "goto", args ->
6092 scan args "%u %f %f"
6093 (fun pageno x y ->
6094 let cmd, _ = state.geomcmds in
6095 if emptystr cmd
6096 then gotopagexy pageno x y
6097 else
6098 let f prevf () =
6099 gotopagexy pageno x y;
6100 prevf ()
6102 state.reprf <- f state.reprf
6104 | "goto1", args -> scan args "%u %f" gotopage
6105 | "gotor", args -> scan args "%S" gotoremote
6106 | "rect", args ->
6107 scan args "%u %u %f %f %f %f"
6108 (fun pageno c x0 y0 x1 y1 ->
6109 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6110 rectx "rect" pageno color x0 y0 x1 y1;
6112 | "prect", args ->
6113 scan args "%u %f %f %f %f %f %f %f %f"
6114 (fun pageno r g b alpha x0 y0 x1 y1 ->
6115 addrect pageno r g b alpha x0 y0 x1 y1;
6116 G.postRedisplay "prect"
6118 | "pgoto", args ->
6119 scan args "%u %f %f"
6120 (fun pageno x y ->
6121 let optopaque =
6122 match getopaque pageno with
6123 | Some opaque -> opaque
6124 | None -> ~< E.s
6126 pgoto optopaque pageno x y;
6127 let rec fixx = function
6128 | [] -> ()
6129 | l :: rest ->
6130 if l.pageno = pageno
6131 then gotoxy (state.x - l.pagedispx) state.y
6132 else fixx rest
6134 let layout =
6135 let mult =
6136 match conf.columns with
6137 | Csingle _ | Csplit _ -> 1
6138 | Cmulti ((n, _, _), _) -> n
6140 layout 0 state.y (state.winw * mult) state.winh
6142 fixx layout
6144 | "activatewin", "" -> Wsi.activatewin ()
6145 | "quit", "" -> raise Quit
6146 | "keys", keys ->
6147 begin try
6148 let l = Config.keys_of_string keys in
6149 List.iter (fun (k, m) -> keyboard k m) l
6150 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
6151 cmds @@ exntos exn
6153 | "clearrects", "" ->
6154 Hashtbl.clear state.prects;
6155 G.postRedisplay "clearrects"
6156 | _ ->
6157 adderrfmt "remote command"
6158 "error processing remote command: %S\n" cmds;
6161 let remote =
6162 let scratch = Bytes.create 80 in
6163 let buf = Buffer.create 80 in
6164 fun fd ->
6165 match tempfailureretry (Unix.read fd scratch 0) 80 with
6166 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6167 | 0 ->
6168 Unix.close fd;
6169 if Buffer.length buf > 0
6170 then (
6171 let s = Buffer.contents buf in
6172 Buffer.clear buf;
6173 ract s;
6175 None
6176 | n ->
6177 let rec eat ppos =
6178 let nlpos =
6179 match Bytes.index_from scratch ppos '\n' with
6180 | pos -> if pos >= n then -1 else pos
6181 | exception Not_found -> -1
6183 if nlpos >= 0
6184 then (
6185 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6186 let s = Buffer.contents buf in
6187 Buffer.clear buf;
6188 ract s;
6189 eat (nlpos+1);
6191 else (
6192 Buffer.add_subbytes buf scratch ppos (n-ppos);
6193 Some fd
6195 in eat 0
6198 let remoteopen path =
6199 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6200 with exn ->
6201 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6202 None
6205 let () =
6206 let gcconfig = ref false in
6207 let trimcachepath = ref E.s in
6208 let rcmdpath = ref E.s in
6209 let pageno = ref None in
6210 let rootwid = ref 0 in
6211 let openlast = ref false in
6212 let doreap = ref false in
6213 let csspath = ref None in
6214 selfexec := Sys.executable_name;
6215 Arg.parse
6216 (Arg.align
6217 [("-p", Arg.String (fun s -> state.password <- s),
6218 "<password> Set password");
6220 ("-f", Arg.String
6221 (fun s ->
6222 Config.fontpath := s;
6223 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6225 "<path> Set path to the user interface font");
6227 ("-c", Arg.String
6228 (fun s ->
6229 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6230 Config.confpath := s),
6231 "<path> Set path to the configuration file");
6233 ("-last", Arg.Set openlast, " Open last document");
6235 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6236 "<page-number> Jump to page");
6238 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6239 "<path> Set path to the trim cache file");
6241 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6242 "<named-destination> Set named destination");
6244 ("-cxack", Arg.Set cxack, " Cut corners");
6246 ("-remote", Arg.String (fun s -> rcmdpath := s),
6247 "<path> Set path to the source of remote commands");
6249 ("-gc", Arg.Set gcconfig, " Collect config garbage");
6251 ("-v", Arg.Unit (fun () ->
6252 Printf.printf
6253 "%s\nconfiguration path: %s\n"
6254 (version ())
6255 Config.defconfpath;
6256 exit 0), " Print version and exit");
6258 ("-css", Arg.String (fun s -> csspath := Some s),
6259 "<path> Set path to the style sheet to use with EPUB/HTML");
6261 ("-embed", Arg.Set_int rootwid, "<window-id> Embed into window");
6263 ("-origin", Arg.String (fun s -> state.origin <- s),
6264 "<origin> <undocumented>");
6266 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title")
6269 (fun s -> state.path <- s)
6270 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6272 let histmode = emptystr state.path && not !openlast in
6274 if not (Config.load !openlast)
6275 then dolog "failed to load configuration";
6277 begin match !pageno with
6278 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6279 | None -> ()
6280 end;
6282 fillhelp ();
6283 if !gcconfig
6284 then (
6285 Config.gc ();
6286 exit 0
6289 let mu =
6290 object (self)
6291 val mutable m_clicks = 0
6292 val mutable m_click_x = 0
6293 val mutable m_click_y = 0
6294 val mutable m_lastclicktime = infinity
6296 method private cleanup =
6297 state.roam <- noroam;
6298 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6299 method expose = G.postRedisplay "expose"
6300 method visible v =
6301 let name =
6302 match v with
6303 | Wsi.Unobscured -> "unobscured"
6304 | Wsi.PartiallyObscured -> "partiallyobscured"
6305 | Wsi.FullyObscured -> "fullyobscured"
6307 vlog "visibility change %s" name
6308 method display = display ()
6309 method map mapped = vlog "mapped %b" mapped
6310 method reshape w h =
6311 self#cleanup;
6312 reshape w h
6313 method mouse b d x y m =
6314 if d && canselect ()
6315 then (
6317 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6319 m_click_x <- x;
6320 m_click_y <- y;
6321 if b = 1
6322 then (
6323 let t = now () in
6324 if abs x - m_click_x > 10
6325 || abs y - m_click_y > 10
6326 || abs_float (t -. m_lastclicktime) > 0.3
6327 then m_clicks <- 0;
6328 m_clicks <- m_clicks + 1;
6329 m_lastclicktime <- t;
6330 if m_clicks = 1
6331 then (
6332 self#cleanup;
6333 G.postRedisplay "cleanup";
6334 state.uioh <- state.uioh#button b d x y m;
6336 else state.uioh <- state.uioh#multiclick m_clicks x y m
6338 else (
6339 self#cleanup;
6340 m_clicks <- 0;
6341 m_lastclicktime <- infinity;
6342 state.uioh <- state.uioh#button b d x y m
6345 else (
6346 state.uioh <- state.uioh#button b d x y m
6348 method motion x y =
6349 state.mpos <- (x, y);
6350 state.uioh <- state.uioh#motion x y
6351 method pmotion x y =
6352 state.mpos <- (x, y);
6353 state.uioh <- state.uioh#pmotion x y
6354 method key k m =
6355 vlog "k=%#x m=%#x" k m;
6356 let mascm = m land (
6357 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6358 ) in
6359 let keyboard k m =
6360 let x = state.x and y = state.y in
6361 keyboard k m;
6362 if x != state.x || y != state.y then self#cleanup
6364 match state.keystate with
6365 | KSnone ->
6366 let km = k, mascm in
6367 begin
6368 match
6369 let modehash = state.uioh#modehash in
6370 try Hashtbl.find modehash km
6371 with Not_found ->
6372 try Hashtbl.find (findkeyhash conf "global") km
6373 with Not_found -> KMinsrt (k, m)
6374 with
6375 | KMinsrt (k, m) -> keyboard k m
6376 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6377 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6379 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6380 List.iter (fun (k, m) -> keyboard k m) insrt;
6381 state.keystate <- KSnone
6382 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6383 state.keystate <- KSinto (keys, insrt)
6384 | KSinto _ -> state.keystate <- KSnone
6386 method enter x y =
6387 state.mpos <- (x, y);
6388 state.uioh <- state.uioh#pmotion x y
6389 method leave = state.mpos <- (-1, -1)
6390 method winstate wsl = state.winstate <- wsl
6391 method quit : 'a. 'a = raise Quit
6392 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
6393 method zoom z x y = state.uioh#zoom z x y
6394 method opendoc path =
6395 state.mode <- View;
6396 state.uioh <- uioh;
6397 G.postRedisplay "opendoc";
6398 opendoc path state.password
6401 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6402 state.wsfd <- wsfd;
6404 if not @@ List.exists GlMisc.check_extension
6405 [ "GL_ARB_texture_rectangle"
6406 ; "GL_EXT_texture_recangle"
6407 ; "GL_NV_texture_rectangle" ]
6408 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6410 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6411 then (
6412 defconf.sliceheight <- 1024;
6413 defconf.texcount <- 32;
6414 defconf.usepbo <- true;
6417 let cs, ss =
6418 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6419 | exception exn ->
6420 dolog "socketpair failed: %s" @@ exntos exn;
6421 exit 1
6422 | (r, w) ->
6423 cloexec r;
6424 cloexec w;
6425 r, w
6428 setcheckers conf.checkers;
6430 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6432 begin match !csspath with
6433 | None -> ()
6434 | Some "" -> conf.css <- E.s
6435 | Some path ->
6436 let css = filecontents path in
6437 let l = String.length css in
6438 conf.css <-
6439 if substratis css (l-2) "\r\n"
6440 then String.sub css 0 (l-2)
6441 else (if css.[l-1] = '\n'
6442 then String.sub css 0 (l-1)
6443 else css);
6444 end;
6445 init cs (
6446 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6447 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6448 !Config.fontpath, !trimcachepath, !opengl_has_pbo
6450 List.iter GlArray.enable [`texture_coord; `vertex];
6451 state.ss <- ss;
6452 reshape ~firsttime:true winw winh;
6453 state.uioh <- uioh;
6454 if histmode
6455 then (
6456 Wsi.settitle "llpp (history)";
6457 enterhistmode ();
6459 else (
6460 state.text <- "Opening " ^ (mbtoutf8 state.path);
6461 opendoc state.path state.password;
6463 display ();
6464 Wsi.mapwin ();
6465 Wsi.setcursor Wsi.CURSOR_INHERIT;
6466 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6468 let rec reap () =
6469 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6470 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
6471 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
6472 | 0, _ -> ()
6473 | _pid, _status -> reap ()
6475 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6477 let optrfd =
6478 ref (
6479 if nonemptystr !rcmdpath
6480 then remoteopen !rcmdpath
6481 else None
6485 let rec loop deadline =
6486 if !doreap
6487 then (
6488 doreap := false;
6489 reap ()
6491 let r = [state.ss; state.wsfd] in
6492 let r =
6493 match !optrfd with
6494 | None -> r
6495 | Some fd -> fd :: r
6497 if state.redisplay
6498 then (
6499 state.redisplay <- false;
6500 display ();
6502 let timeout =
6503 let now = now () in
6504 if deadline > now
6505 then (
6506 if deadline = infinity
6507 then ~-.1.0
6508 else max 0.0 (deadline -. now)
6510 else 0.0
6512 let r, _, _ =
6513 try Unix.select r [] [] timeout
6514 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6516 begin match r with
6517 | [] ->
6518 state.ghyll None;
6519 let newdeadline =
6520 if state.ghyll == noghyll
6521 then
6522 match state.autoscroll with
6523 | Some step when step != 0 ->
6524 if state.slideshow land 1 = 1
6525 then (
6526 if state.slideshow land 2 = 0
6527 then state.slideshow <- state.slideshow lor 2
6528 else if step < 0 then prevpage () else nextpage ();
6529 deadline +. (float (abs step))
6531 else
6532 let y = state.y + step in
6533 let fy = if conf.maxhfit then state.winh else 0 in
6534 let y =
6535 if y < 0
6536 then state.maxy - fy
6537 else if y >= state.maxy - fy then 0 else y
6539 if state.mode = View
6540 then gotoxy_and_clear_text state.x y
6541 else gotoxy state.x y;
6542 deadline +. 0.01
6543 | _ -> infinity
6544 else deadline +. 0.01
6546 loop newdeadline
6548 | l ->
6549 let rec checkfds = function
6550 | [] -> ()
6551 | fd :: rest when fd = state.ss ->
6552 let cmd = rcmd state.ss in
6553 act cmd;
6554 checkfds rest
6556 | fd :: rest when fd = state.wsfd ->
6557 Wsi.readresp fd;
6558 checkfds rest
6560 | fd :: rest when Some fd = !optrfd ->
6561 begin match remote fd with
6562 | None -> optrfd := remoteopen !rcmdpath;
6563 | opt -> optrfd := opt
6564 end;
6565 checkfds rest
6567 | _ :: rest ->
6568 dolog "select returned unknown descriptor";
6569 checkfds rest
6571 checkfds l;
6572 let newdeadline =
6573 let deadline1 =
6574 if deadline = infinity
6575 then now () +. 0.01
6576 else deadline
6578 match state.autoscroll with
6579 | Some step when step != 0 -> deadline1
6580 | _ -> if state.ghyll == noghyll then infinity else deadline1
6582 loop newdeadline
6583 end;
6585 match loop infinity with
6586 | exception Quit ->
6587 Config.save leavebirdseye;
6588 if hasunsavedchanges ()
6589 then save ()
6590 | _ -> error "umpossible - infinity reached"