Refactor
[llpp.git] / main.ml
blob1f4c4468a491232a012e074479cc927180348956
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 copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
10 external getpdimrect : int -> float array = "ml_getpdimrect";;
11 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
12 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
13 external clearmark : opaque -> unit = "ml_clearmark";;
14 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
15 external getmaxw : unit -> float = "ml_getmaxw";;
16 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
17 external measurestr : int -> string -> float = "ml_measure_string";;
18 external postprocess :
19 opaque -> int -> int -> int -> (int * string * int) -> int
20 = "ml_postprocess";;
21 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
22 external setaalevel : int -> unit = "ml_setaalevel";;
23 external realloctexts : int -> bool = "ml_realloctexts";;
24 external findlink : opaque -> linkdir -> link = "ml_findlink";;
25 external getlink : opaque -> int -> under = "ml_getlink";;
26 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
27 external getlinkcount : opaque -> int = "ml_getlinkcount";;
28 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
29 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
30 external freepbo : opaque -> unit = "ml_freepbo";;
31 external unmappbo : opaque -> unit = "ml_unmappbo";;
32 external bousable : unit -> bool = "ml_bo_usable";;
33 external unproject : opaque -> int -> int -> (int * int) option
34 = "ml_unproject";;
35 external project : opaque -> int -> int -> float -> float -> (float * float)
36 = "ml_project";;
37 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
38 external rectofblock : opaque -> int -> int -> float array option
39 = "ml_rectofblock";;
40 external begintiles : unit -> unit = "ml_begintiles";;
41 external endtiles : unit -> unit = "ml_endtiles";;
42 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
43 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
44 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
45 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
46 external savedoc : string -> unit = "ml_savedoc";;
47 external getannotcontents : opaque -> slinkindex -> string
48 = "ml_getannotcontents";;
49 external drawprect : opaque -> int -> int -> float array -> unit
50 = "ml_drawprect";;
51 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
52 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
53 external uritolocation : string -> (pageno * float * float)
54 = "ml_uritolocation";;
55 external isexternallink : string -> bool = "ml_isexternallink";;
57 let selfexec = ref E.s;;
58 let opengl_has_pbo = ref false;;
60 let drawstring size x y s =
61 Gl.enable `blend;
62 Gl.enable `texture_2d;
63 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
64 ignore (drawstr size x y s);
65 Gl.disable `blend;
66 Gl.disable `texture_2d;
69 let drawstring1 size x y s =
70 drawstr size x y s;
73 let drawstring2 size x y fmt =
74 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
77 module UniSyms = struct
78 let ellipsis = "\xe2\x80\xa6";;
79 let radical = "\xe2\x88\x9a";;
80 let lguillemet = "\xc2\xab";;
81 let rguillemet = "\xc2\xbb";;
82 end;;
84 let _debugl l =
85 dolog {|l %d dim=%d {
86 WxH %dx%d
87 vWxH %dx%d
88 pagex,y %d,%d
89 dispx,y %d,%d
90 column %d
91 }|}
92 l.pageno l.pagedimno
93 l.pagew l.pageh
94 l.pagevw l.pagevh
95 l.pagex l.pagey
96 l.pagedispx l.pagedispy
97 l.pagecol
100 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
101 dolog {|rect {
102 x0,y0=(% f, % f)
103 x1,y1=(% f, % f)
104 x2,y2=(% f, % f)
105 x3,y3=(% f, % f)
106 }|} x0 y0 x1 y1 x2 y2 x3 y3;
109 let isbirdseye = function
110 | Birdseye _ -> true
111 | Textentry _ | View | LinkNav _ -> false
114 let istextentry = function
115 | Textentry _ -> true
116 | Birdseye _ | View | LinkNav _ -> false
119 let wtmode = ref false;;
120 let cxack = ref false;;
122 let pgscale h = truncate (float h *. conf.pgscale);;
124 let hscrollh () =
125 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
126 && (state.w > state.winw))
127 then conf.scrollbw
128 else 0
131 let vscrollw () =
132 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
133 && (state.maxy > state.winh))
134 then conf.scrollbw
135 else 0
138 let vscrollhit x =
139 if conf.leftscroll
140 then x < vscrollw ()
141 else x > state.winw - vscrollw ()
144 let setfontsize n =
145 fstate.fontsize <- n;
146 fstate.wwidth <- measurestr fstate.fontsize "w";
147 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
150 let vlog fmt =
151 if conf.verbose
152 then dolog fmt
153 else Printf.kprintf ignore fmt
156 let launchpath () =
157 if emptystr conf.pathlauncher
158 then dolog "%s" state.path
159 else (
160 let command = Str.global_replace percentsre state.path conf.pathlauncher in
161 match spawn command [] with
162 | _pid -> ()
163 | exception exn ->
164 dolog "failed to execute `%s': %s" command @@ exntos exn
168 module G =
169 struct
170 let postRedisplay who =
171 vlog "redisplay for [%S]" who;
172 state.redisplay <- true;
174 end;;
176 let getopaque pageno =
177 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
178 with Not_found -> None
181 let pagetranslatepoint l x y =
182 let dy = y - l.pagedispy in
183 let y = dy + l.pagey in
184 let dx = x - l.pagedispx in
185 let x = dx + l.pagex in
186 (x, y);
189 let onppundermouse g x y d =
190 let rec f = function
191 | l :: rest ->
192 begin match getopaque l.pageno with
193 | Some opaque ->
194 let x0 = l.pagedispx in
195 let x1 = x0 + l.pagevw in
196 let y0 = l.pagedispy in
197 let y1 = y0 + l.pagevh in
198 if y >= y0 && y <= y1 && x >= x0 && x <= x1
199 then
200 let px, py = pagetranslatepoint l x y in
201 match g opaque l px py with
202 | Some res -> res
203 | None -> f rest
204 else f rest
205 | _ ->
206 f rest
208 | [] -> d
210 f state.layout
213 let getunder x y =
214 let g opaque l px py =
215 if state.bzoom
216 then (
217 match rectofblock opaque px py with
218 | Some [|x0;x1;y0;y1|] ->
219 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
220 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
221 state.rects <- [l.pageno, color, rect];
222 G.postRedisplay "getunder";
223 | _otherwise -> ()
225 let under = whatsunder opaque px py in
226 if under = Unone then None else Some under
228 onppundermouse g x y Unone
231 let unproject x y =
232 let g opaque l x y =
233 match unproject opaque x y with
234 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
235 | None -> None
237 onppundermouse g x y None;
240 let showtext c s =
241 state.text <- Printf.sprintf "%c%s" c s;
242 G.postRedisplay "showtext";
245 let impmsg fmt =
246 Format.ksprintf (fun s -> showtext '!' s) fmt;
249 let pipesel opaque cmd =
250 if hassel opaque
251 then
252 match Unix.pipe () with
253 | exception exn -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
254 | (r, w) ->
255 let doclose what fd =
256 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
258 begin match spawn cmd [r, 0; w, -1] with
259 | exception exn ->
260 doclose "pipesel pipe/w" w;
261 dolog "cannot execute %S: %s" cmd @@ exntos exn
262 | _pid ->
263 copysel w opaque;
264 G.postRedisplay "pipesel";
265 end;
266 doclose "pipesel pipe/r" r;
269 let paxunder x y =
270 let g opaque l px py =
271 if markunder opaque px py conf.paxmark
272 then (
273 Some (fun () ->
274 match getopaque l.pageno with
275 | None -> ()
276 | Some opaque -> pipesel opaque conf.paxcmd
279 else None
281 G.postRedisplay "paxunder";
282 if conf.paxmark = Mark_page
283 then
284 List.iter (fun l ->
285 match getopaque l.pageno with
286 | None -> ()
287 | Some opaque -> clearmark opaque) state.layout;
288 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
291 let selstring s =
292 match Unix.pipe () with
293 | exception exn -> impmsg "pipe failed: %s" @@ exntos exn
294 | (r, w) ->
295 let clo cap fd =
296 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
298 begin match spawn conf.selcmd [r, 0; w, -1] with
299 | exception exn ->
300 clo "selstring pipe/w" w;
301 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn
302 | _pid ->
304 let l = String.length s in
305 let bytes = Bytes.unsafe_of_string s in
306 let n = tempfailureretry (Unix.write w bytes 0) l in
307 if n != l
308 then impmsg "failed to write %d characters to sel pipe, wrote %d" l n
309 with exn -> impmsg "failed to write to sel pipe: %s" @@ exntos exn
310 end;
311 clo "selstring pipe/r" r;
314 let undertext = function
315 | Unone -> "none"
316 | Ulinkuri s -> s
317 | Utext s -> "font: " ^ s
318 | Uannotation (opaque, slinkindex) ->
319 "annotation: " ^ getannotcontents opaque slinkindex
322 let updateunder x y =
323 match getunder x y with
324 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
325 | Ulinkuri uri ->
326 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
327 Wsi.setcursor Wsi.CURSOR_INFO
328 | Utext s ->
329 if conf.underinfo then showtext 'f' ("ont: " ^ s);
330 Wsi.setcursor Wsi.CURSOR_TEXT
331 | Uannotation _ ->
332 if conf.underinfo then showtext 'a' "nnotation";
333 Wsi.setcursor Wsi.CURSOR_INFO
336 let showlinktype under =
337 if conf.underinfo && under != Unone
338 then showtext ' ' @@ undertext under
341 let intentry_with_suffix text key =
342 let text =
343 if key >= 32 && key < 127
344 then
345 let c = Char.chr key in
346 match c with
347 | '0' .. '9' ->
348 addchar text c
350 | 'k' | 'm' | 'g' | 'K' | 'M' | 'G' ->
351 addchar text @@ asciilower c
352 | _ ->
353 state.text <- Printf.sprintf "invalid key (%d, `%c')" key c;
354 text
355 else (
356 state.text <- Printf.sprintf "invalid key %d" key;
357 text
360 TEcont text
363 let wcmd fmt =
364 let b = Buffer.create 16 in
365 Printf.kbprintf
366 (fun b ->
367 let b = Buffer.to_bytes b in
368 wcmd state.ss b @@ Bytes.length b
369 ) b fmt
372 let nogeomcmds cmds =
373 match cmds with
374 | s, [] -> emptystr s
375 | _ -> false
378 let layoutN ((columns, coverA, coverB), b) x y sw sh =
379 let rec fold accu n =
380 if n = Array.length b
381 then accu
382 else
383 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
384 if (vy - y) > sh &&
385 (n = coverA - 1
386 || n = state.pagecount - coverB
387 || (n - coverA) mod columns = columns - 1)
388 then accu
389 else
390 let accu =
391 if vy + h > y
392 then
393 let pagey = max 0 (y - vy) in
394 let pagedispy = if pagey > 0 then 0 else vy - y in
395 let pagedispx, pagex =
396 let pdx =
397 if n = coverA - 1 || n = state.pagecount - coverB
398 then x + (sw - w) / 2
399 else dx + xoff + x
401 if pdx < 0
402 then 0, -pdx
403 else pdx, 0
405 let pagevw =
406 let vw = sw - pagedispx in
407 let pw = w - pagex in
408 min vw pw
410 let pagevh = min (h - pagey) (sh - pagedispy) in
411 if pagevw > 0 && pagevh > 0
412 then
413 let e =
414 { pageno = n
415 ; pagedimno = pdimno
416 ; pagew = w
417 ; pageh = h
418 ; pagex = pagex
419 ; pagey = pagey
420 ; pagevw = pagevw
421 ; pagevh = pagevh
422 ; pagedispx = pagedispx
423 ; pagedispy = pagedispy
424 ; pagecol = 0
427 e :: accu
428 else
429 accu
430 else
431 accu
433 fold accu (n+1)
435 if Array.length b = 0
436 then []
437 else List.rev (fold [] (page_of_y y))
440 let layoutS (columns, b) x y sw sh =
441 let rec fold accu n =
442 if n = Array.length b
443 then accu
444 else
445 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
446 if (vy - y) > sh
447 then accu
448 else
449 let accu =
450 if vy + pageh > y
451 then
452 let x = xoff + x in
453 let pagey = max 0 (y - vy) in
454 let pagedispy = if pagey > 0 then 0 else vy - y in
455 let pagedispx, pagex =
456 if px = 0
457 then (
458 if x < 0
459 then 0, -x
460 else x, 0
462 else (
463 let px = px - x in
464 if px < 0
465 then -px, 0
466 else 0, px
469 let pagecolw = pagew/columns in
470 let pagedispx =
471 if pagecolw < sw
472 then pagedispx + ((sw - pagecolw) / 2)
473 else pagedispx
475 let pagevw =
476 let vw = sw - pagedispx in
477 let pw = pagew - pagex in
478 min vw pw
480 let pagevw = min pagevw pagecolw in
481 let pagevh = min (pageh - pagey) (sh - pagedispy) in
482 if pagevw > 0 && pagevh > 0
483 then
484 let e =
485 { pageno = n/columns
486 ; pagedimno = pdimno
487 ; pagew = pagew
488 ; pageh = pageh
489 ; pagex = pagex
490 ; pagey = pagey
491 ; pagevw = pagevw
492 ; pagevh = pagevh
493 ; pagedispx = pagedispx
494 ; pagedispy = pagedispy
495 ; pagecol = n mod columns
498 e :: accu
499 else
500 accu
501 else
502 accu
504 fold accu (n+1)
506 List.rev (fold [] 0)
509 let layout x y sw sh =
510 if nogeomcmds state.geomcmds
511 then
512 match conf.columns with
513 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
514 | Cmulti c -> layoutN c x y sw sh
515 | Csplit s -> layoutS s x y sw sh
516 else []
519 let clamp incr =
520 let y = state.y + incr in
521 let y = max 0 y in
522 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
526 let itertiles l f =
527 let tilex = l.pagex mod conf.tilew in
528 let tiley = l.pagey mod conf.tileh in
530 let col = l.pagex / conf.tilew in
531 let row = l.pagey / conf.tileh in
533 let rec rowloop row y0 dispy h =
534 if h = 0
535 then ()
536 else (
537 let dh = conf.tileh - y0 in
538 let dh = min h dh in
539 let rec colloop col x0 dispx w =
540 if w = 0
541 then ()
542 else (
543 let dw = conf.tilew - x0 in
544 let dw = min w dw in
545 f col row dispx dispy x0 y0 dw dh;
546 colloop (col+1) 0 (dispx+dw) (w-dw)
549 colloop col tilex l.pagedispx l.pagevw;
550 rowloop (row+1) 0 (dispy+dh) (h-dh)
553 if l.pagevw > 0 && l.pagevh > 0
554 then rowloop row tiley l.pagedispy l.pagevh;
557 let gettileopaque l col row =
558 let key =
559 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
561 try Some (Hashtbl.find state.tilemap key)
562 with Not_found -> None
565 let puttileopaque l col row gen colorspace angle opaque size elapsed =
566 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
567 Hashtbl.add state.tilemap key (opaque, size, elapsed)
570 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
571 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
572 GlArray.vertex `two state.vraw;
573 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
576 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
578 let filledrect x0 y0 x1 y1 =
579 GlArray.disable `texture_coord;
580 filledrect1 x0 y0 x1 y1;
581 GlArray.enable `texture_coord;
584 let linerect x0 y0 x1 y1 =
585 GlArray.disable `texture_coord;
586 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
587 GlArray.vertex `two state.vraw;
588 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
589 GlArray.enable `texture_coord;
592 let drawtiles l color =
593 GlDraw.color color;
594 begintiles ();
595 let f col row x y tilex tiley w h =
596 match gettileopaque l col row with
597 | Some (opaque, _, t) ->
598 let params = x, y, w, h, tilex, tiley in
599 if conf.invert
600 then GlTex.env (`mode `blend);
601 drawtile params opaque;
602 if conf.invert
603 then GlTex.env (`mode `modulate);
604 if conf.debug
605 then (
606 endtiles ();
607 let s = Printf.sprintf
608 "%d[%d,%d] %f sec"
609 l.pageno col row t
611 let w = measurestr fstate.fontsize s in
612 GlDraw.color (0.0, 0.0, 0.0);
613 filledrect (float (x-2))
614 (float (y-2))
615 (float (x+2) +. w)
616 (float (y + fstate.fontsize + 2));
617 GlDraw.color color;
618 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
619 begintiles ();
622 | None ->
623 endtiles ();
624 let w =
625 let lw = state.winw - x in
626 min lw w
627 and h =
628 let lh = state.winh - y in
629 min lh h
631 if conf.invert
632 then GlTex.env (`mode `blend);
633 begin match state.checkerstexid with
634 | Some id ->
635 Gl.enable `texture_2d;
636 GlTex.bind_texture ~target:`texture_2d id;
637 let x0 = float x
638 and y0 = float y
639 and x1 = float (x+w)
640 and y1 = float (y+h) in
642 let tw = float w /. 16.0
643 and th = float h /. 16.0 in
644 let tx0 = float tilex /. 16.0
645 and ty0 = float tiley /. 16.0 in
646 let tx1 = tx0 +. tw
647 and ty1 = ty0 +. th in
648 Raw.sets_float state.vraw ~pos:0
649 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
650 Raw.sets_float state.traw ~pos:0
651 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
652 GlArray.vertex `two state.vraw;
653 GlArray.tex_coord `two state.traw;
654 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
655 Gl.disable `texture_2d;
657 | None ->
658 GlDraw.color (1.0, 1.0, 1.0);
659 filledrect (float x) (float y) (float (x+w)) (float (y+h));
660 end;
661 if conf.invert
662 then GlTex.env (`mode `modulate);
663 if w > 128 && h > fstate.fontsize + 10
664 then (
665 let c = if conf.invert then 1.0 else 0.0 in
666 GlDraw.color (c, c, c);
667 let c, r =
668 if conf.verbose
669 then (col*conf.tilew, row*conf.tileh)
670 else col, row
672 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
674 GlDraw.color color;
675 begintiles ();
677 itertiles l f;
678 endtiles ();
681 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
683 let tilevisible1 l x y =
684 let ax0 = l.pagex
685 and ax1 = l.pagex + l.pagevw
686 and ay0 = l.pagey
687 and ay1 = l.pagey + l.pagevh in
689 let bx0 = x
690 and by0 = y in
691 let bx1 = min (bx0 + conf.tilew) l.pagew
692 and by1 = min (by0 + conf.tileh) l.pageh in
694 let rx0 = max ax0 bx0
695 and ry0 = max ay0 by0
696 and rx1 = min ax1 bx1
697 and ry1 = min ay1 by1 in
699 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
700 nonemptyintersection
703 let tilevisible layout n x y =
704 let rec findpageinlayout m = function
705 | l :: rest when l.pageno = n ->
706 tilevisible1 l x y || (
707 match conf.columns with
708 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
709 | Csplit _ | Csingle _ | Cmulti _ -> false
711 | _ :: rest -> findpageinlayout 0 rest
712 | [] -> false
714 findpageinlayout 0 layout;
717 let tileready l x y =
718 tilevisible1 l x y &&
719 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
722 let tilepage n p layout =
723 let rec loop = function
724 | l :: rest ->
725 if l.pageno = n
726 then
727 let f col row _ _ _ _ _ _ =
728 if state.currently = Idle
729 then
730 match gettileopaque l col row with
731 | Some _ -> ()
732 | None ->
733 let x = col*conf.tilew
734 and y = row*conf.tileh in
735 let w =
736 let w = l.pagew - x in
737 min w conf.tilew
739 let h =
740 let h = l.pageh - y in
741 min h conf.tileh
743 let pbo =
744 if conf.usepbo
745 then getpbo w h conf.colorspace
746 else ~< "0"
748 wcmd "tile %s %d %d %d %d %s"
749 (~> p) x y w h (~> pbo);
750 state.currently <-
751 Tiling (
752 l, p, conf.colorspace, conf.angle,
753 state.gen, col, row, conf.tilew, conf.tileh
756 itertiles l f;
757 else
758 loop rest
760 | [] -> ()
762 if nogeomcmds state.geomcmds
763 then loop layout;
766 let preloadlayout x y sw sh =
767 let y = if y < sh then 0 else y - sh in
768 let x = min 0 (x + sw) in
769 let h = sh*3 in
770 let w = sw*3 in
771 layout x y w h;
774 let load pages =
775 let rec loop pages =
776 if state.currently != Idle
777 then ()
778 else
779 match pages with
780 | l :: rest ->
781 begin match getopaque l.pageno with
782 | None ->
783 wcmd "page %d %d" l.pageno l.pagedimno;
784 state.currently <- Loading (l, state.gen);
785 | Some opaque ->
786 tilepage l.pageno opaque pages;
787 loop rest
788 end;
789 | _ -> ()
791 if nogeomcmds state.geomcmds
792 then loop pages
795 let preload pages =
796 load pages;
797 if conf.preload && state.currently = Idle
798 then load (preloadlayout state.x state.y state.winw state.winh);
801 let layoutready layout =
802 let rec fold all ls =
803 all && match ls with
804 | l :: rest ->
805 let seen = ref false in
806 let allvisible = ref true in
807 let foo col row _ _ _ _ _ _ =
808 seen := true;
809 allvisible := !allvisible &&
810 begin match gettileopaque l col row with
811 | Some _ -> true
812 | None -> false
815 itertiles l foo;
816 fold (!seen && !allvisible) rest
817 | [] -> true
819 let alltilesvisible = fold true layout in
820 alltilesvisible;
823 let gotoxy x y =
824 let y = bound y 0 state.maxy in
825 let y, layout, proceed =
826 match conf.maxwait with
827 | Some time when state.ghyll == noghyll ->
828 begin match state.throttle with
829 | None ->
830 let layout = layout x y state.winw state.winh in
831 let ready = layoutready layout in
832 if not ready
833 then (
834 load layout;
835 state.throttle <- Some (layout, y, now ());
837 else G.postRedisplay "gotoxy showall (None)";
838 y, layout, ready
839 | Some (_, _, started) ->
840 let dt = now () -. started in
841 if dt > time
842 then (
843 state.throttle <- None;
844 let layout = layout x y state.winw state.winh in
845 load layout;
846 G.postRedisplay "maxwait";
847 y, layout, true
849 else -1, [], false
852 | _ ->
853 let layout = layout x y state.winw state.winh in
854 if not !wtmode || layoutready layout
855 then G.postRedisplay "gotoxy ready";
856 y, layout, true
858 if proceed
859 then (
860 state.x <- x;
861 state.y <- y;
862 state.layout <- layout;
863 begin match state.mode with
864 | LinkNav ln ->
865 begin match ln with
866 | Ltexact (pageno, linkno) ->
867 let rec loop = function
868 | [] ->
869 state.lnava <- Some (pageno, linkno);
870 state.mode <- LinkNav (Ltgendir 0)
871 | l :: _ when l.pageno = pageno ->
872 begin match getopaque pageno with
873 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
874 | Some opaque ->
875 let x0, y0, x1, y1 = getlinkrect opaque linkno in
876 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
877 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
878 then state.mode <- LinkNav (Ltgendir 0)
880 | _ :: rest -> loop rest
882 loop layout
883 | Ltnotready _ | Ltgendir _ -> ()
885 | Birdseye _ | Textentry _ | View -> ()
886 end;
887 begin match state.mode with
888 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
889 if not (pagevisible layout pageno)
890 then (
891 match state.layout with
892 | [] -> ()
893 | l :: _ ->
894 state.mode <- Birdseye (
895 conf, leftx, l.pageno, hooverpageno, anchor
898 | LinkNav lt ->
899 begin match lt with
900 | Ltnotready (_, dir)
901 | Ltgendir dir ->
902 let linknav =
903 let rec loop = function
904 | [] -> lt
905 | l :: rest ->
906 match getopaque l.pageno with
907 | None -> Ltnotready (l.pageno, dir)
908 | Some opaque ->
909 let link =
910 let ld =
911 if dir = 0
912 then LDfirstvisible (l.pagex, l.pagey, dir)
913 else (
914 if dir > 0 then LDfirst else LDlast
917 findlink opaque ld
919 match link with
920 | Lnotfound -> loop rest
921 | Lfound n ->
922 showlinktype (getlink opaque n);
923 Ltexact (l.pageno, n)
925 loop state.layout
927 state.mode <- LinkNav linknav
928 | Ltexact _ -> ()
930 | Textentry _ | View -> ()
931 end;
932 preload layout;
934 state.ghyll <- noghyll;
935 if conf.updatecurs
936 then (
937 let mx, my = state.mpos in
938 updateunder mx my;
942 let conttiling pageno opaque =
943 tilepage pageno opaque
944 (if conf.preload
945 then preloadlayout state.x state.y state.winw state.winh
946 else state.layout)
949 let gotoxy_and_clear_text x y =
950 if not conf.verbose then state.text <- E.s;
951 gotoxy x y;
954 let getanchory (n, top, dtop) =
955 let y, h = getpageyh n in
956 if conf.presentation
957 then
958 let ips = calcips h in
959 y + truncate (top*.float h -. dtop*.float ips) + ips;
960 else
961 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
964 let gotoanchor anchor =
965 gotoxy state.x (getanchory anchor);
968 let addnav () =
969 cbput state.hists.nav (getanchor ());
972 let getnav dir =
973 let anchor = cbgetc state.hists.nav dir in
974 getanchory anchor;
977 let gotoghyll1 single y =
978 let scroll f n a b =
979 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
980 let snake f a b =
981 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
982 if f < a
983 then s (float f /. float a)
984 else (
985 if f > b
986 then 1.0 -. s ((float (f-b) /. float (n-b)))
987 else 1.0
990 snake f a b
991 and summa n a b =
992 let ins = float a *. 0.5
993 and outs = float (n-b) *. 0.5 in
994 let ones = b - a in
995 ins +. outs +. float ones
997 let rec set nab y sy =
998 let (_N, _A, _B), y =
999 if single
1000 then
1001 let scl = if y > sy then 2 else -2 in
1002 let _N, _, _ = nab in
1003 (_N,0,_N), y+conf.scrollstep*scl
1004 else nab,y in
1005 let sum = summa _N _A _B in
1006 let dy = float (y - sy) in
1007 state.ghyll <- (
1008 let rec gf n y1 o =
1009 if n >= _N
1010 then state.ghyll <- noghyll
1011 else
1012 let go n =
1013 let s = scroll n _N _A _B in
1014 let y1 = y1 +. ((s *. dy) /. sum) in
1015 gotoxy_and_clear_text state.x (truncate y1);
1016 state.ghyll <- gf (n+1) y1;
1018 match o with
1019 | None -> go n
1020 | Some y' when single -> set nab y' state.y
1021 | Some y' -> set (_N/2, 1, 1) y' state.y
1023 gf 0 (float state.y)
1026 match conf.ghyllscroll with
1027 | Some nab when not conf.presentation ->
1028 if state.ghyll == noghyll
1029 then set nab y state.y
1030 else state.ghyll (Some y)
1031 | _ ->
1032 gotoxy_and_clear_text state.x y
1035 let gotoghyll = gotoghyll1 false;;
1037 let gotopage n top =
1038 let y, h = getpageyh n in
1039 let y = y + (truncate (top *. float h)) in
1040 gotoghyll y
1043 let gotopage1 n top =
1044 let y = getpagey n in
1045 let y = y + top in
1046 gotoghyll y
1049 let invalidate s f =
1050 state.redisplay <- false;
1051 state.layout <- [];
1052 state.pdims <- [];
1053 state.rects <- [];
1054 state.rects1 <- [];
1055 match state.geomcmds with
1056 | ps, [] when emptystr ps ->
1057 f ();
1058 state.geomcmds <- s, [];
1060 | ps, [] ->
1061 state.geomcmds <- ps, [s, f];
1063 | ps, (s', _) :: rest when s' = s ->
1064 state.geomcmds <- ps, ((s, f) :: rest);
1066 | ps, cmds ->
1067 state.geomcmds <- ps, ((s, f) :: cmds);
1070 let flushpages () =
1071 Hashtbl.iter (fun _ opaque ->
1072 wcmd "freepage %s" (~> opaque);
1073 ) state.pagemap;
1074 Hashtbl.clear state.pagemap;
1077 let flushtiles () =
1078 if not (Queue.is_empty state.tilelru)
1079 then (
1080 Queue.iter (fun (k, p, s) ->
1081 wcmd "freetile %s" (~> p);
1082 state.memused <- state.memused - s;
1083 Hashtbl.remove state.tilemap k;
1084 ) state.tilelru;
1085 state.uioh#infochanged Memused;
1086 Queue.clear state.tilelru;
1088 load state.layout;
1091 let stateh h =
1092 let h = truncate (float h*.conf.zoom) in
1093 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1094 h - d
1097 let opendoc path password =
1098 state.path <- path;
1099 state.password <- password;
1100 state.gen <- state.gen + 1;
1101 state.docinfo <- [];
1102 state.outlines <- [||];
1104 flushpages ();
1105 setaalevel conf.aalevel;
1106 let titlepath =
1107 if emptystr state.origin
1108 then path
1109 else state.origin
1111 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1112 wcmd "open %d %d %d %s\000%s\000%s\000"
1113 (btod !wtmode) (btod !cxack) (btod conf.usedoccss)
1114 path password conf.css;
1115 invalidate "reqlayout"
1116 (fun () ->
1117 wcmd "reqlayout %d %d %d %s\000"
1118 conf.angle (FMTE.to_int conf.fitmodel)
1119 (stateh state.winh) state.nameddest
1121 state.help <-
1122 let sl = keystostrlist conf in
1123 let rec loop accu =
1124 function | [] -> accu
1125 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1126 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1129 let reload () =
1130 state.anchor <- getanchor ();
1131 opendoc state.path state.password;
1134 let scalecolor c =
1135 let c = c *. conf.colorscale in
1136 (c, c, c);
1139 let scalecolor2 (r, g, b) =
1140 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1143 let docolumns columns =
1144 match columns with
1145 | Csingle _ ->
1146 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1147 let rec loop pageno pdimno pdim y ph pdims =
1148 if pageno = state.pagecount
1149 then ()
1150 else
1151 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1152 match pdims with
1153 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1154 pdimno+1, pdim, rest
1155 | _ ->
1156 pdimno, pdim, pdims
1158 let x = max 0 (((state.winw - w) / 2) - xoff) in
1159 let y =
1160 y + (if conf.presentation
1161 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1162 else (if pageno = 0 then 0 else conf.interpagespace)
1165 a.(pageno) <- (pdimno, x, y, pdim);
1166 loop (pageno+1) pdimno pdim (y + h) h pdims
1168 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1169 conf.columns <- Csingle a;
1171 | Cmulti ((columns, coverA, coverB), _) ->
1172 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1173 let rec loop pageno pdimno pdim x y rowh pdims =
1174 let rec fixrow m =
1175 if m = pageno then () else
1176 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1177 if h < rowh
1178 then (
1179 let y = y + (rowh - h) / 2 in
1180 a.(m) <- (pdimno, x, y, pdim);
1182 fixrow (m+1)
1184 if pageno = state.pagecount
1185 then fixrow (((pageno - 1) / columns) * columns)
1186 else
1187 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1188 match pdims with
1189 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1190 pdimno+1, pdim, rest
1191 | _ ->
1192 pdimno, pdim, pdims
1194 let x, y, rowh' =
1195 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1196 then (
1197 let x = (state.winw - w) / 2 in
1198 let ips =
1199 if conf.presentation then calcips h else conf.interpagespace in
1200 x, y + ips + rowh, h
1202 else (
1203 if (pageno - coverA) mod columns = 0
1204 then (
1205 let x = max 0 (state.winw - state.w) / 2 in
1206 let y =
1207 if conf.presentation
1208 then
1209 let ips = calcips h in
1210 y + (if pageno = 0 then 0 else calcips rowh + ips)
1211 else
1212 y + (if pageno = 0 then 0 else conf.interpagespace)
1214 x, y + rowh, h
1216 else x, y, max rowh h
1219 let y =
1220 if pageno > 1 && (pageno - coverA) mod columns = 0
1221 then (
1222 let y =
1223 if pageno = columns && conf.presentation
1224 then (
1225 let ips = calcips rowh in
1226 for i = 0 to pred columns
1228 let (pdimno, x, y, pdim) = a.(i) in
1229 a.(i) <- (pdimno, x, y+ips, pdim)
1230 done;
1231 y+ips;
1233 else y
1235 fixrow (pageno - columns);
1238 else y
1240 a.(pageno) <- (pdimno, x, y, pdim);
1241 let x = x + w + xoff*2 + conf.interpagespace in
1242 loop (pageno+1) pdimno pdim x y rowh' pdims
1244 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1245 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1247 | Csplit (c, _) ->
1248 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1249 let rec loop pageno pdimno pdim y pdims =
1250 if pageno = state.pagecount
1251 then ()
1252 else
1253 let pdimno, ((_, w, h, _) as pdim), pdims =
1254 match pdims with
1255 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1256 pdimno+1, pdim, rest
1257 | _ ->
1258 pdimno, pdim, pdims
1260 let cw = w / c in
1261 let rec loop1 n x y =
1262 if n = c then y else (
1263 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1264 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1267 let y = loop1 0 0 y in
1268 loop (pageno+1) pdimno pdim y pdims
1270 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1271 conf.columns <- Csplit (c, a);
1274 let represent () =
1275 docolumns conf.columns;
1276 state.maxy <- calcheight ();
1277 if state.reprf == noreprf
1278 then (
1279 match state.mode with
1280 | Birdseye (_, _, pageno, _, _) ->
1281 let y, h = getpageyh pageno in
1282 let top = (state.winh - h) / 2 in
1283 gotoxy state.x (max 0 (y - top))
1284 | Textentry _ | View | LinkNav _ ->
1285 let y = getanchory state.anchor in
1286 let y = min y (state.maxy - state.winh) in
1287 gotoxy state.x y;
1289 else (
1290 state.reprf ();
1291 state.reprf <- noreprf;
1295 let reshape ?(firsttime=false) w h =
1296 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1297 if not firsttime && nogeomcmds state.geomcmds
1298 then state.anchor <- getanchor ();
1300 state.winw <- w;
1301 let w = truncate (float w *. conf.zoom) in
1302 let w = max w 2 in
1303 state.winh <- h;
1304 setfontsize fstate.fontsize;
1305 GlMat.mode `modelview;
1306 GlMat.load_identity ();
1308 GlMat.mode `projection;
1309 GlMat.load_identity ();
1310 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1311 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1312 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1314 let relx =
1315 if conf.zoom <= 1.0
1316 then 0.0
1317 else float state.x /. float state.w
1319 invalidate "geometry"
1320 (fun () ->
1321 state.w <- w;
1322 if not firsttime
1323 then state.x <- truncate (relx *. float w);
1324 let w =
1325 match conf.columns with
1326 | Csingle _ -> w
1327 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1328 | Csplit (c, _) -> w * c
1330 wcmd "geometry %d %d %d"
1331 w (stateh h) (FMTE.to_int conf.fitmodel)
1335 let enttext () =
1336 let len = String.length state.text in
1337 let x0 = if conf.leftscroll then vscrollw () else 0 in
1338 let drawstring s =
1339 let hscrollh =
1340 match state.mode with
1341 | Textentry _ | View | LinkNav _ ->
1342 let h, _, _ = state.uioh#scrollpw in
1344 | Birdseye _ -> 0
1346 let rect x w =
1347 filledrect
1348 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1349 (x+.w) (float (state.winh - hscrollh))
1352 let w = float (state.winw - 1 - vscrollw ()) in
1353 if state.progress >= 0.0 && state.progress < 1.0
1354 then (
1355 GlDraw.color (0.3, 0.3, 0.3);
1356 let w1 = w *. state.progress in
1357 rect (float x0) w1;
1358 GlDraw.color (0.0, 0.0, 0.0);
1359 rect (float x0+.w1) (float x0+.w-.w1)
1361 else (
1362 GlDraw.color (0.0, 0.0, 0.0);
1363 rect (float x0) w;
1366 GlDraw.color (1.0, 1.0, 1.0);
1367 drawstring
1368 fstate.fontsize
1369 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1370 (state.winh - hscrollh - 5) s;
1372 let s =
1373 match state.mode with
1374 | Textentry ((prefix, text, _, _, _, _), _) ->
1375 let s =
1376 if len > 0
1377 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1378 else Printf.sprintf "%s%s_" prefix text
1382 | Birdseye _ | View | LinkNav _ -> state.text
1384 let s =
1385 if state.newerrmsgs
1386 then (
1387 if not (istextentry state.mode) && state.uioh#eformsgs
1388 then
1389 let s1 = "(press 'e' to review error messasges)" in
1390 if nonemptystr s then s ^ " " ^ s1 else s1
1391 else s
1393 else s
1395 if nonemptystr s
1396 then drawstring s
1399 let gctiles () =
1400 let len = Queue.length state.tilelru in
1401 let layout = lazy (
1402 match state.throttle with
1403 | None ->
1404 if conf.preload
1405 then preloadlayout state.x state.y state.winw state.winh
1406 else state.layout
1407 | Some (layout, _, _) ->
1408 layout
1409 ) in
1410 let rec loop qpos =
1411 if state.memused > conf.memlimit
1412 then (
1413 if qpos < len
1414 then
1415 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1416 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1417 let (_, pw, ph, _) = getpagedim n in
1418 if gen = state.gen
1419 && colorspace = conf.colorspace
1420 && angle = conf.angle
1421 && pagew = pw
1422 && pageh = ph
1423 && (
1424 let x = col*conf.tilew
1425 and y = row*conf.tileh in
1426 tilevisible (Lazy.force_val layout) n x y
1428 then Queue.push lruitem state.tilelru
1429 else (
1430 freepbo p;
1431 wcmd "freetile %s" (~> p);
1432 state.memused <- state.memused - s;
1433 state.uioh#infochanged Memused;
1434 Hashtbl.remove state.tilemap k;
1436 loop (qpos+1)
1439 loop 0
1442 let onpagerect pageno f =
1443 let b =
1444 match conf.columns with
1445 | Cmulti (_, b) -> b
1446 | Csingle b -> b
1447 | Csplit (_, b) -> b
1449 if pageno >= 0 && pageno < Array.length b
1450 then
1451 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1452 f w h
1455 let gotopagexy1 wtmode pageno x y =
1456 let _,w1,h1,leftx = getpagedim pageno in
1457 let top = y /. (float h1) in
1458 let left = x /. (float w1) in
1459 let py, w, h = getpageywh pageno in
1460 let wh = state.winh in
1461 let x = left *. (float w) in
1462 let x = leftx + state.x + truncate x in
1463 let sx =
1464 if x < 0 || x >= state.winw
1465 then state.x - x
1466 else state.x
1468 let pdy = truncate (top *. float h) in
1469 let y' = py + pdy in
1470 let dy = y' - state.y in
1471 let sy =
1472 if x != state.x || not (dy > 0 && dy < wh)
1473 then (
1474 if conf.presentation
1475 then
1476 if abs (py - y') > wh
1477 then y'
1478 else py
1479 else y';
1481 else state.y
1483 if state.x != sx || state.y != sy
1484 then (
1485 let x, y =
1486 if wtmode
1487 then (
1488 let ww = state.winw in
1489 let qx = sx / ww
1490 and qy = pdy / wh in
1491 let x = qx * ww
1492 and y = py + qy * wh in
1493 let x = if -x + ww > w1 then -(w1-ww) else x
1494 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1495 let y =
1496 if conf.presentation
1497 then
1498 if abs (py - y') > wh
1499 then y'
1500 else py
1501 else y';
1503 (x, y)
1505 else (sx, sy)
1507 gotoxy_and_clear_text x y;
1509 else gotoxy_and_clear_text state.x state.y;
1512 let gotopagexy wtmode pageno x y =
1513 match state.mode with
1514 | Birdseye _ -> gotopage pageno 0.0
1515 | Textentry _ | View | LinkNav _ -> gotopagexy1 wtmode pageno x y
1518 let getpassword () =
1519 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1520 if emptystr passcmd
1521 then E.s
1522 else getcmdoutput
1523 (fun s ->
1524 impmsg "error getting password: %s" s;
1525 dolog "%s" s) passcmd;
1528 let pgoto opaque pageno x y =
1529 let pdimno = getpdimno pageno in
1530 let x, y = project opaque pageno pdimno x y in
1531 gotopagexy false pageno x y;
1534 let act cmds =
1535 (* dolog "%S" cmds; *)
1536 let spl = splitatchar cmds ' ' in
1537 let scan s fmt f =
1538 try Scanf.sscanf s fmt f
1539 with exn ->
1540 dolog "error processing '%S': %s" cmds @@ exntos exn;
1541 exit 1
1543 let addoutline outline =
1544 match state.currently with
1545 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1546 | Idle -> state.currently <- Outlining [outline]
1547 | Loading _ | Tiling _ ->
1548 dolog "invalid outlining state";
1549 logcurrently state.currently
1551 match spl with
1552 | "clear", "" ->
1553 state.pdims <- [];
1554 state.uioh#infochanged Pdim;
1556 | "clearrects", "" ->
1557 state.rects <- state.rects1;
1558 G.postRedisplay "clearrects";
1560 | "continue", args ->
1561 let n = scan args "%u" (fun n -> n) in
1562 state.pagecount <- n;
1563 begin match state.currently with
1564 | Outlining l ->
1565 state.currently <- Idle;
1566 state.outlines <- Array.of_list (List.rev l)
1567 | Idle | Loading _ | Tiling _ -> ()
1568 end;
1570 let cur, cmds = state.geomcmds in
1571 if emptystr cur
1572 then failwith "umpossible";
1574 begin match List.rev cmds with
1575 | [] ->
1576 state.geomcmds <- E.s, [];
1577 state.throttle <- None;
1578 represent ();
1579 | (s, f) :: rest ->
1580 f ();
1581 state.geomcmds <- s, List.rev rest;
1582 end;
1583 if conf.maxwait = None && not !wtmode
1584 then G.postRedisplay "continue";
1586 | "msg", args ->
1587 showtext ' ' args
1589 | "vmsg", args ->
1590 if conf.verbose
1591 then showtext ' ' args
1593 | "emsg", args ->
1594 Buffer.add_string state.errmsgs args;
1595 state.newerrmsgs <- true;
1596 G.postRedisplay "error message"
1598 | "progress", args ->
1599 let progress, text =
1600 scan args "%f %n"
1601 (fun f pos ->
1602 f, String.sub args pos (String.length args - pos))
1604 state.text <- text;
1605 state.progress <- progress;
1606 G.postRedisplay "progress"
1608 | "firstmatch", args ->
1609 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1610 scan args "%u %d %f %f %f %f %f %f %f %f"
1611 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1612 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1614 let y = (getpagey pageno) + truncate y0 in
1615 let x =
1616 if conf.zoom > 1.0
1617 then state.winw/2
1618 else state.x
1620 addnav ();
1621 gotoxy x y;
1622 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1623 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1625 | "match", args ->
1626 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1627 scan args "%u %d %f %f %f %f %f %f %f %f"
1628 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1629 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1631 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1632 state.rects1 <-
1633 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1635 | "page", args ->
1636 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1637 let pageopaque = ~< pageopaques in
1638 begin match state.currently with
1639 | Loading (l, gen) ->
1640 vlog "page %d took %f sec" l.pageno t;
1641 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1642 begin match state.throttle with
1643 | None ->
1644 let preloadedpages =
1645 if conf.preload
1646 then preloadlayout state.x state.y state.winw state.winh
1647 else state.layout
1649 let evict () =
1650 let set =
1651 List.fold_left (fun s l -> IntSet.add l.pageno s)
1652 IntSet.empty preloadedpages
1654 let evictedpages =
1655 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1656 if not (IntSet.mem pageno set)
1657 then (
1658 wcmd "freepage %s" (~> opaque);
1659 key :: accu
1661 else accu
1662 ) state.pagemap []
1664 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1666 evict ();
1667 state.currently <- Idle;
1668 if gen = state.gen
1669 then (
1670 tilepage l.pageno pageopaque state.layout;
1671 load state.layout;
1672 load preloadedpages;
1673 let visible = pagevisible state.layout l.pageno in
1674 if visible
1675 then (
1676 match state.mode with
1677 | LinkNav (Ltnotready (pageno, dir)) ->
1678 if pageno = l.pageno
1679 then (
1680 let link =
1681 let ld =
1682 if dir = 0
1683 then LDfirstvisible (l.pagex, l.pagey, dir)
1684 else (
1685 if dir > 0 then LDfirst else LDlast
1688 findlink pageopaque ld
1690 match link with
1691 | Lnotfound -> ()
1692 | Lfound n ->
1693 showlinktype (getlink pageopaque n);
1694 state.mode <- LinkNav (Ltexact (l.pageno, n))
1696 | LinkNav (Ltgendir _)
1697 | LinkNav (Ltexact _)
1698 | View
1699 | Birdseye _
1700 | Textentry _ -> ()
1703 if visible && layoutready state.layout
1704 then (
1705 G.postRedisplay "page";
1709 | Some (layout, _, _) ->
1710 state.currently <- Idle;
1711 tilepage l.pageno pageopaque layout;
1712 load state.layout
1713 end;
1715 | Idle | Tiling _ | Outlining _ ->
1716 dolog "Inconsistent loading state";
1717 logcurrently state.currently;
1718 exit 1
1721 | "tile" , args ->
1722 let (x, y, opaques, size, t) =
1723 scan args "%u %u %s %u %f"
1724 (fun x y p size t -> (x, y, p, size, t))
1726 let opaque = ~< opaques in
1727 begin match state.currently with
1728 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1729 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1731 unmappbo opaque;
1732 if tilew != conf.tilew || tileh != conf.tileh
1733 then (
1734 wcmd "freetile %s" (~> opaque);
1735 state.currently <- Idle;
1736 load state.layout;
1738 else (
1739 puttileopaque l col row gen cs angle opaque size t;
1740 state.memused <- state.memused + size;
1741 state.uioh#infochanged Memused;
1742 gctiles ();
1743 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1744 opaque, size) state.tilelru;
1746 let layout =
1747 match state.throttle with
1748 | None -> state.layout
1749 | Some (layout, _, _) -> layout
1752 state.currently <- Idle;
1753 if gen = state.gen
1754 && conf.colorspace = cs
1755 && conf.angle = angle
1756 && tilevisible layout l.pageno x y
1757 then conttiling l.pageno pageopaque;
1759 begin match state.throttle with
1760 | None ->
1761 preload state.layout;
1762 if gen = state.gen
1763 && conf.colorspace = cs
1764 && conf.angle = angle
1765 && tilevisible state.layout l.pageno x y
1766 && (not !wtmode || layoutready state.layout)
1767 then G.postRedisplay "tile nothrottle";
1769 | Some (layout, y, _) ->
1770 let ready = layoutready layout in
1771 if ready
1772 then (
1773 state.y <- y;
1774 state.layout <- layout;
1775 state.throttle <- None;
1776 G.postRedisplay "throttle";
1778 else load layout;
1779 end;
1782 | Idle | Loading _ | Outlining _ ->
1783 dolog "Inconsistent tiling state";
1784 logcurrently state.currently;
1785 exit 1
1788 | "pdim", args ->
1789 let (n, w, h, _) as pdim =
1790 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1792 let pdim =
1793 match conf.fitmodel with
1794 | FitWidth -> pdim
1795 | FitPage | FitProportional ->
1796 match conf.columns with
1797 | Csplit _ -> (n, w, h, 0)
1798 | Csingle _ | Cmulti _ -> pdim
1800 state.pdims <- pdim :: state.pdims;
1801 state.uioh#infochanged Pdim
1803 | "o", args ->
1804 let (l, n, t, h, pos) =
1805 scan args "%u %u %d %u %n"
1806 (fun l n t h pos -> l, n, t, h, pos)
1808 let s = String.sub args pos (String.length args - pos) in
1809 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1811 | "ou", args ->
1812 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1813 let s = String.sub args pos len in
1814 let pos2 = pos + len + 1 in
1815 let uri = String.sub args pos2 (String.length args - pos2) in
1816 addoutline (s, l, Ouri uri)
1818 | "on", args ->
1819 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1820 let s = String.sub args pos (String.length args - pos) in
1821 addoutline (s, l, Onone)
1823 | "a", args ->
1824 let (n, l, t) =
1825 scan args "%u %d %d" (fun n l t -> n, l, t)
1827 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1829 | "info", args ->
1830 let c, v = splitatchar args '\t' in
1831 let s =
1832 if nonemptystr v
1833 then
1834 if c = "Title"
1835 then (
1836 conf.title <- v;
1837 Wsi.settitle v;
1838 args
1840 else
1841 if let len = String.length c in
1842 len > 6 && ((String.sub c (len-4) 4) = "date")
1843 then (
1844 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1845 then
1846 let b = Buffer.create 10 in
1847 Printf.bprintf b "%s\t" c;
1848 let sub p l c =
1850 Buffer.add_substring b v p l;
1851 Buffer.add_char b c;
1852 with exn -> Buffer.add_string b @@ exntos exn
1854 sub 2 4 '/';
1855 sub 6 2 '/';
1856 sub 8 2 ' ';
1857 sub 10 2 ':';
1858 sub 12 2 ':';
1859 sub 14 2 ' ';
1860 Buffer.add_char b '[';
1861 Buffer.add_string b v;
1862 Buffer.add_char b ']';
1863 Buffer.contents b
1864 else args
1866 else args
1867 else args
1869 state.docinfo <- (1, s) :: state.docinfo
1871 | "infoend", "" ->
1872 state.docinfo <- List.rev state.docinfo;
1873 state.uioh#infochanged Docinfo
1875 | "pass", args ->
1876 if args = "fail"
1877 then Wsi.settitle "Wrong password";
1878 let password = getpassword () in
1879 if emptystr password
1880 then error "document is password protected"
1881 else opendoc state.path password
1883 | _ ->
1884 error "unknown cmd `%S'" cmds
1887 let onhist cb =
1888 let rc = cb.rc in
1889 let action = function
1890 | HCprev -> cbget cb ~-1
1891 | HCnext -> cbget cb 1
1892 | HCfirst -> cbget cb ~-(cb.rc)
1893 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1894 and cancel () = cb.rc <- rc
1895 in (action, cancel)
1898 let search pattern forward =
1899 match conf.columns with
1900 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1901 | Csingle _ | Cmulti _ ->
1902 if nonemptystr pattern
1903 then
1904 let pn, py =
1905 match state.layout with
1906 | [] -> 0, 0
1907 | l :: _ ->
1908 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1910 wcmd "search %d %d %d %d,%s\000"
1911 (btod conf.icase) pn py (btod forward) pattern;
1914 let intentry text key =
1915 let text =
1916 if key >= 32 && key < 127
1917 then
1918 let c = Char.chr key in
1919 match c with
1920 | '0' .. '9' -> addchar text c
1921 | _ ->
1922 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1923 text
1924 else (
1925 state.text <- Printf.sprintf "invalid key (%d)" key;
1926 text
1929 TEcont text
1932 let linknact f s =
1933 if nonemptystr s
1934 then (
1935 let n =
1936 let l = String.length s in
1937 let rec loop pos n =
1938 if pos = l
1939 then n
1940 else
1941 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1942 loop (pos+1) (n*26 + m)
1943 in loop 0 0
1945 let rec loop n = function
1946 | [] -> ()
1947 | l :: rest ->
1948 match getopaque l.pageno with
1949 | None -> loop n rest
1950 | Some opaque ->
1951 let m = getlinkcount opaque in
1952 if n < m
1953 then (
1954 let under = getlink opaque n in
1955 f under
1957 else loop (n-m) rest
1959 loop n state.layout;
1963 let linknentry text key =
1964 if key >= 32 && key < 127
1965 then
1966 let text = addchar text (Char.chr key) in
1967 linknact (fun under -> state.text <- undertext under) text;
1968 TEcont text
1969 else (
1970 state.text <- Printf.sprintf "invalid key %d" key;
1971 TEcont text
1975 let textentry text key =
1976 if Wsi.isspecialkey key
1977 then TEcont text
1978 else TEcont (text ^ toutf8 key)
1981 let reqlayout angle fitmodel =
1982 match state.throttle with
1983 | None ->
1984 if nogeomcmds state.geomcmds
1985 then state.anchor <- getanchor ();
1986 conf.angle <- angle mod 360;
1987 if conf.angle != 0
1988 then (
1989 match state.mode with
1990 | LinkNav _ -> state.mode <- View
1991 | Birdseye _ | Textentry _ | View -> ()
1993 conf.fitmodel <- fitmodel;
1994 invalidate
1995 "reqlayout"
1996 (fun () ->
1997 wcmd "reqlayout %d %d %d"
1998 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2000 | _ -> ()
2003 let settrim trimmargins trimfuzz =
2004 if nogeomcmds state.geomcmds
2005 then state.anchor <- getanchor ();
2006 conf.trimmargins <- trimmargins;
2007 conf.trimfuzz <- trimfuzz;
2008 let x0, y0, x1, y1 = trimfuzz in
2009 invalidate
2010 "settrim" (fun () ->
2011 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2012 flushpages ();
2015 let setzoom zoom =
2016 match state.throttle with
2017 | None ->
2018 let zoom = max 0.0001 zoom in
2019 if zoom <> conf.zoom
2020 then (
2021 state.prevzoom <- (conf.zoom, state.x);
2022 conf.zoom <- zoom;
2023 reshape state.winw state.winh;
2024 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2027 | Some (layout, y, started) ->
2028 let time =
2029 match conf.maxwait with
2030 | None -> 0.0
2031 | Some t -> t
2033 let dt = now () -. started in
2034 if dt > time
2035 then (
2036 state.y <- y;
2037 load layout;
2041 let pivotzoom ?(vw=min state.w state.winw)
2042 ?(vh=min (state.maxy-state.y) state.winh)
2043 ?(x=vw/2) ?(y=vh/2) zoom =
2044 let w = float state.w /. zoom in
2045 let hw = w /. 2.0 in
2046 let ratio = float vh /. float vw in
2047 let hh = hw *. ratio in
2048 let x0 = if zoom < 1.0 then 0.0 else float x -. hw in
2049 let y0 = float y -. hh in
2050 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2051 setzoom zoom;
2054 let pivotzoom ?vw ?vh ?x ?y zoom =
2055 if nogeomcmds state.geomcmds then pivotzoom ?vw ?vh ?x ?y zoom
2058 let setcolumns mode columns coverA coverB =
2059 state.prevcolumns <- Some (conf.columns, conf.zoom);
2060 if columns < 0
2061 then (
2062 if isbirdseye mode
2063 then impmsg "split mode doesn't work in bird's eye"
2064 else (
2065 conf.columns <- Csplit (-columns, E.a);
2066 state.x <- 0;
2067 conf.zoom <- 1.0;
2070 else (
2071 if columns < 2
2072 then (
2073 conf.columns <- Csingle E.a;
2074 state.x <- 0;
2075 setzoom 1.0;
2077 else (
2078 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2079 conf.zoom <- 1.0;
2082 reshape state.winw state.winh;
2085 let resetmstate () =
2086 state.mstate <- Mnone;
2087 Wsi.setcursor Wsi.CURSOR_INHERIT;
2090 let enterbirdseye () =
2091 let zoom = float conf.thumbw /. float state.winw in
2092 let birdseyepageno =
2093 let cy = state.winh / 2 in
2094 let fold = function
2095 | [] -> 0
2096 | l :: rest ->
2097 let rec fold best = function
2098 | [] -> best.pageno
2099 | l :: rest ->
2100 let d = cy - (l.pagedispy + l.pagevh/2)
2101 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2102 if abs d < abs dbest
2103 then fold l rest
2104 else best.pageno
2105 in fold l rest
2107 fold state.layout
2109 state.mode <-
2110 Birdseye (
2111 { conf with zoom = conf.zoom },
2112 state.x, birdseyepageno, -1, getanchor ()
2114 resetmstate ();
2115 conf.zoom <- zoom;
2116 conf.presentation <- false;
2117 conf.interpagespace <- 10;
2118 conf.hlinks <- false;
2119 conf.fitmodel <- FitPage;
2120 state.x <- 0;
2121 conf.maxwait <- None;
2122 conf.columns <- (
2123 match conf.beyecolumns with
2124 | Some c ->
2125 conf.zoom <- 1.0;
2126 Cmulti ((c, 0, 0), E.a)
2127 | None -> Csingle E.a
2129 if conf.verbose
2130 then
2131 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2132 (100.0*.zoom)
2133 else
2134 state.text <- E.s
2136 reshape state.winw state.winh;
2139 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2140 state.mode <- View;
2141 conf.zoom <- c.zoom;
2142 conf.presentation <- c.presentation;
2143 conf.interpagespace <- c.interpagespace;
2144 conf.maxwait <- c.maxwait;
2145 conf.hlinks <- c.hlinks;
2146 conf.fitmodel <- c.fitmodel;
2147 conf.beyecolumns <- (
2148 match conf.columns with
2149 | Cmulti ((c, _, _), _) -> Some c
2150 | Csingle _ -> None
2151 | Csplit _ -> failwith "leaving bird's eye split mode"
2153 conf.columns <- (
2154 match c.columns with
2155 | Cmulti (c, _) -> Cmulti (c, E.a)
2156 | Csingle _ -> Csingle E.a
2157 | Csplit (c, _) -> Csplit (c, E.a)
2159 if conf.verbose
2160 then
2161 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2162 (100.0*.conf.zoom)
2164 reshape state.winw state.winh;
2165 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2166 state.x <- leftx;
2169 let togglebirdseye () =
2170 match state.mode with
2171 | Birdseye vals -> leavebirdseye vals true
2172 | View -> enterbirdseye ()
2173 | Textentry _ | LinkNav _ -> ()
2176 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2177 let pageno = max 0 (pageno - incr) in
2178 let rec loop = function
2179 | [] -> gotopage1 pageno 0
2180 | l :: _ when l.pageno = pageno ->
2181 if l.pagedispy >= 0 && l.pagey = 0
2182 then G.postRedisplay "upbirdseye"
2183 else gotopage1 pageno 0
2184 | _ :: rest -> loop rest
2186 loop state.layout;
2187 state.text <- E.s;
2188 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2191 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2192 let pageno = min (state.pagecount - 1) (pageno + incr) in
2193 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2194 let rec loop = function
2195 | [] ->
2196 let y, h = getpageyh pageno in
2197 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2198 gotoxy state.x (clamp dy)
2199 | l :: _ when l.pageno = pageno ->
2200 if l.pagevh != l.pageh
2201 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2202 else G.postRedisplay "downbirdseye"
2203 | _ :: rest -> loop rest
2205 loop state.layout;
2206 state.text <- E.s;
2209 let optentry mode _ key =
2210 let btos b = if b then "on" else "off" in
2211 if key >= 32 && key < 127
2212 then
2213 let c = Char.chr key in
2214 match c with
2215 | 's' ->
2216 let ondone s =
2217 try conf.scrollstep <- int_of_string s with exn ->
2218 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2220 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2222 | 'A' ->
2223 let ondone s =
2225 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2226 if state.autoscroll <> None
2227 then state.autoscroll <- Some conf.autoscrollstep
2228 with exn ->
2229 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2231 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2233 | 'C' ->
2234 let ondone s =
2236 let n, a, b = multicolumns_of_string s in
2237 setcolumns mode n a b;
2238 with exn ->
2239 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2241 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2243 | 'Z' ->
2244 let ondone s =
2246 let zoom = float (int_of_string s) /. 100.0 in
2247 pivotzoom zoom
2248 with exn ->
2249 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2251 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2253 | 't' ->
2254 let ondone s =
2256 conf.thumbw <- bound (int_of_string s) 2 4096;
2257 state.text <-
2258 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2259 begin match mode with
2260 | Birdseye beye ->
2261 leavebirdseye beye false;
2262 enterbirdseye ();
2263 | Textentry _ | View | LinkNav _ -> ();
2265 with exn ->
2266 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2268 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2270 | 'R' ->
2271 let ondone s =
2272 match int_of_string s with
2273 | angle -> reqlayout angle conf.fitmodel
2274 | exception exn ->
2275 state.text <-
2276 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2278 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2280 | 'i' ->
2281 conf.icase <- not conf.icase;
2282 TEdone ("case insensitive search " ^ (btos conf.icase))
2284 | 'p' ->
2285 conf.preload <- not conf.preload;
2286 gotoxy state.x state.y;
2287 TEdone ("preload " ^ (btos conf.preload))
2289 | 'v' ->
2290 conf.verbose <- not conf.verbose;
2291 TEdone ("verbose " ^ (btos conf.verbose))
2293 | 'd' ->
2294 conf.debug <- not conf.debug;
2295 TEdone ("debug " ^ (btos conf.debug))
2297 | 'h' ->
2298 conf.maxhfit <- not conf.maxhfit;
2299 state.maxy <- calcheight ();
2300 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2302 | 'c' ->
2303 conf.crophack <- not conf.crophack;
2304 TEdone ("crophack " ^ btos conf.crophack)
2306 | 'a' ->
2307 let s =
2308 match conf.maxwait with
2309 | None ->
2310 conf.maxwait <- Some infinity;
2311 "always wait for page to complete"
2312 | Some _ ->
2313 conf.maxwait <- None;
2314 "show placeholder if page is not ready"
2316 TEdone s
2318 | 'f' ->
2319 conf.underinfo <- not conf.underinfo;
2320 TEdone ("underinfo " ^ btos conf.underinfo)
2322 | 'P' ->
2323 conf.savebmarks <- not conf.savebmarks;
2324 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2326 | 'S' ->
2327 let ondone s =
2329 let pageno, py =
2330 match state.layout with
2331 | [] -> 0, 0
2332 | l :: _ ->
2333 l.pageno, l.pagey
2335 conf.interpagespace <- int_of_string s;
2336 docolumns conf.columns;
2337 state.maxy <- calcheight ();
2338 let y = getpagey pageno in
2339 gotoxy state.x (y + py)
2340 with exn ->
2341 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2343 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2345 | 'l' ->
2346 let fm =
2347 match conf.fitmodel with
2348 | FitProportional -> FitWidth
2349 | FitWidth | FitPage -> FitProportional
2351 reqlayout conf.angle fm;
2352 TEdone ("proportional display " ^ btos (fm == FitProportional))
2354 | 'T' ->
2355 settrim (not conf.trimmargins) conf.trimfuzz;
2356 TEdone ("trim margins " ^ btos conf.trimmargins)
2358 | 'I' ->
2359 conf.invert <- not conf.invert;
2360 TEdone ("invert colors " ^ btos conf.invert)
2362 | 'x' ->
2363 let ondone s =
2364 cbput state.hists.sel s;
2365 conf.selcmd <- s;
2367 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2368 textentry, ondone, true)
2370 | 'M' ->
2371 if conf.pax == None
2372 then conf.pax <- Some (ref (0.0, 0, 0))
2373 else conf.pax <- None;
2374 TEdone ("PAX " ^ btos (conf.pax != None))
2376 | _ ->
2377 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2378 TEstop
2379 else
2380 TEcont state.text
2383 class type lvsource =
2384 object
2385 method getitemcount : int
2386 method getitem : int -> (string * int)
2387 method hasaction : int -> bool
2388 method exit : uioh:uioh ->
2389 cancel:bool ->
2390 active:int ->
2391 first:int ->
2392 pan:int ->
2393 uioh option
2394 method getactive : int
2395 method getfirst : int
2396 method getpan : int
2397 method getminfo : (int * int) array
2398 end;;
2400 class virtual lvsourcebase = object
2401 val mutable m_active = 0
2402 val mutable m_first = 0
2403 val mutable m_pan = 0
2404 method getactive = m_active
2405 method getfirst = m_first
2406 method getpan = m_pan
2407 method getminfo : (int * int) array = E.a
2408 end;;
2410 let textentrykeyboard
2411 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2412 state.text <- E.s;
2413 let enttext te =
2414 state.mode <- Textentry (te, onleave);
2415 enttext ();
2416 G.postRedisplay "textentrykeyboard enttext";
2418 let histaction cmd =
2419 match opthist with
2420 | None -> ()
2421 | Some (action, _) ->
2422 state.mode <-
2423 Textentry (
2424 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2426 G.postRedisplay "textentry histaction"
2428 let open Keys in
2429 match Wsi.kc2kt key with
2430 | Backspace ->
2431 if emptystr text && cancelonempty
2432 then (
2433 onleave Cancel;
2434 G.postRedisplay "textentrykeyboard after cancel";
2436 else
2437 let s = withoutlastutf8 text in
2438 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2440 | Enter | KPenter ->
2441 ondone text;
2442 onleave Confirm;
2443 G.postRedisplay "textentrykeyboard after confirm"
2445 | Up | KPup -> histaction HCprev
2446 | Down | KPdown -> histaction HCnext
2447 | Home | KPhome -> histaction HCfirst
2448 | End | KPend -> histaction HClast
2450 | Escape ->
2451 if emptystr text
2452 then (
2453 begin match opthist with
2454 | None -> ()
2455 | Some (_, onhistcancel) -> onhistcancel ()
2456 end;
2457 onleave Cancel;
2458 state.text <- E.s;
2459 G.postRedisplay "textentrykeyboard after cancel2"
2461 else (
2462 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2465 | Delete | KPdelete -> ()
2467 | Code _ | Ascii _->
2468 begin match onkey text key with
2469 | TEdone text ->
2470 ondone text;
2471 onleave Confirm;
2472 G.postRedisplay "textentrykeyboard after confirm2";
2474 | TEcont text ->
2475 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2477 | TEstop ->
2478 onleave Cancel;
2479 G.postRedisplay "textentrykeyboard after cancel3"
2481 | TEswitch te ->
2482 state.mode <- Textentry (te, onleave);
2483 G.postRedisplay "textentrykeyboard switch";
2485 | Insert|KPleft|KPminus|KPnext|KPplus|KPprior|KPright|Left|Right|Next|Prior
2486 | Fn _ ->
2487 vlog "unhandled key %s" (Wsi.keyname key)
2490 let firstof first active =
2491 if first > active || abs (first - active) > fstate.maxrows - 1
2492 then max 0 (active - (fstate.maxrows/2))
2493 else first
2496 let calcfirst first active =
2497 if active > first
2498 then
2499 let rows = active - first in
2500 if rows > fstate.maxrows then active - fstate.maxrows else first
2501 else active
2504 let scrollph y maxy =
2505 let sh = float (maxy + state.winh) /. float state.winh in
2506 let sh = float state.winh /. sh in
2507 let sh = max sh (float conf.scrollh) in
2509 let percent = float y /. float maxy in
2510 let position = (float state.winh -. sh) *. percent in
2512 let position =
2513 if position +. sh > float state.winh
2514 then float state.winh -. sh
2515 else position
2517 position, sh;
2520 let adderrmsg src msg =
2521 Buffer.add_string state.errmsgs msg;
2522 state.newerrmsgs <- true;
2523 G.postRedisplay src
2526 let adderrfmt src fmt =
2527 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2530 let coe s = (s :> uioh);;
2532 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2533 object (self)
2534 val m_pan = source#getpan
2535 val m_first = source#getfirst
2536 val m_active = source#getactive
2537 val m_qsearch = E.s
2538 val m_prev_uioh = state.uioh
2540 method private elemunder y =
2541 if y < 0
2542 then None
2543 else
2544 let n = y / (fstate.fontsize+1) in
2545 if m_first + n < source#getitemcount
2546 then (
2547 if source#hasaction (m_first + n)
2548 then Some (m_first + n)
2549 else None
2551 else None
2553 method display =
2554 Gl.enable `blend;
2555 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2556 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2557 filledrect 0. 0. (float state.winw) (float state.winh);
2558 GlDraw.color (1., 1., 1.);
2559 Gl.enable `texture_2d;
2560 let fs = fstate.fontsize in
2561 let nfs = fs + 1 in
2562 let hw = state.winw/3 in
2563 let ww = fstate.wwidth in
2564 let tabw = 17.0*.ww in
2565 let itemcount = source#getitemcount in
2566 let minfo = source#getminfo in
2567 if conf.leftscroll
2568 then (
2569 GlMat.push ();
2570 GlMat.translate ~x:(float conf.scrollbw) ();
2572 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2573 let rec loop row =
2574 if (row - m_first) > fstate.maxrows
2575 then ()
2576 else (
2577 if row >= 0 && row < itemcount
2578 then (
2579 let (s, level) = source#getitem row in
2580 let y = (row - m_first) * nfs in
2581 let x = 5.0 +. (float (level + m_pan)) *. ww in
2582 if helpmode
2583 then GlDraw.color
2584 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2586 if row = m_active
2587 then (
2588 Gl.disable `texture_2d;
2589 let alpha = if source#hasaction row then 0.9 else 0.3 in
2590 GlDraw.color (1., 1., 1.) ~alpha;
2591 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2592 Gl.enable `texture_2d;
2594 let c =
2595 if zebra && row land 1 = 1
2596 then 0.8
2597 else 1.0
2599 GlDraw.color (c,c,c);
2600 let drawtabularstring s =
2601 let drawstr x s =
2602 let x' = truncate (x0 +. x) in
2603 let s1, s2 = splitatchar s '\000' in
2604 if emptystr s2
2605 then drawstring1 fs x' (y+nfs) s
2606 else
2607 let rec e s =
2608 if emptystr s
2609 then s
2610 else
2611 let s' = withoutlastutf8 s in
2612 let s = s' ^ UniSyms.ellipsis in
2613 let w = measurestr fs s in
2614 if float x' +. w +. ww < float (hw + x')
2615 then s
2616 else e s'
2618 let s1 =
2619 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2620 then e s1
2621 else s1
2623 ignore (drawstring1 fs x' (y+nfs) s1);
2624 drawstring1 fs (hw + x') (y+nfs) s2
2626 if trusted
2627 then
2628 let x = if helpmode && row > 0 then x +. ww else x in
2629 let s1, s2 = splitatchar s '\t' in
2630 if nonemptystr s2
2631 then
2632 let nx = drawstr x s1 in
2633 let sw = nx -. x in
2634 let x = x +. (max tabw sw) in
2635 drawstr x s2
2636 else
2637 let len = String.length s - 2 in
2638 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2639 then
2640 let s = String.sub s 2 len in
2641 let x = if not helpmode then x +. ww else x in
2642 GlDraw.color (1.2, 1.2, 1.2);
2643 let vinc = drawstring1 (fs+fs/4)
2644 (truncate (x -. ww)) (y+nfs) s in
2645 GlDraw.color (1., 1., 1.);
2646 vinc +. (float fs *. 0.8)
2647 else
2648 drawstr x s
2649 else
2650 drawstr x s
2652 ignore (drawtabularstring s);
2653 loop (row+1)
2657 loop m_first;
2658 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2659 let xadj = 5.0 in
2660 let rec loop row =
2661 if (row - m_first) <= fstate.maxrows
2662 then
2663 if row >= 0 && row < itemcount
2664 then (
2665 let (s, level) = source#getitem row in
2666 let pos0 = nindex s '\000' in
2667 let y = (row - m_first) * nfs in
2668 let x = float (level + m_pan) *. ww in
2669 let (first, last) = minfo.(row) in
2670 let prefix =
2671 if pos0 > 0 && first > pos0
2672 then String.sub s (pos0+1) (first-pos0-1)
2673 else String.sub s 0 first
2675 let suffix = String.sub s first (last - first) in
2676 let w1 = measurestr fstate.fontsize prefix in
2677 let w2 = measurestr fstate.fontsize suffix in
2678 let x = x +. if conf.leftscroll then xadj else 5.0 in
2679 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2680 let x0 = x +. w1
2681 and y0 = float (y+2) in
2682 let x1 = x0 +. w2
2683 and y1 = float (y+fs+3) in
2684 filledrect x0 y0 x1 y1;
2685 loop (row+1)
2688 Gl.disable `texture_2d;
2689 if Array.length minfo > 0 then loop m_first;
2690 Gl.disable `blend;
2691 if conf.leftscroll
2692 then GlMat.pop ()
2694 method updownlevel incr =
2695 let len = source#getitemcount in
2696 let curlevel =
2697 if m_active >= 0 && m_active < len
2698 then snd (source#getitem m_active)
2699 else -1
2701 let rec flow i =
2702 if i = len then i-1 else if i = -1 then 0 else
2703 let _, l = source#getitem i in
2704 if l != curlevel then i else flow (i+incr)
2706 let active = flow m_active in
2707 let first = calcfirst m_first active in
2708 G.postRedisplay "outline updownlevel";
2709 {< m_active = active; m_first = first >}
2711 method private key1 key mask =
2712 let set1 active first qsearch =
2713 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2715 let search active pattern incr =
2716 let active = if active = -1 then m_first else active in
2717 let dosearch re =
2718 let rec loop n =
2719 if n >= 0 && n < source#getitemcount
2720 then (
2721 let s, _ = source#getitem n in
2722 match Str.search_forward re s 0 with
2723 | exception Not_found -> loop (n + incr)
2724 | _ -> Some n
2726 else None
2728 loop active
2730 let qpat = Str.quote pattern in
2731 match Str.regexp_case_fold qpat with
2732 | s -> dosearch s
2733 | exception exn ->
2734 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2735 qpat @@ Printexc.to_string exn;
2736 None
2738 let itemcount = source#getitemcount in
2739 let find start incr =
2740 let rec find i =
2741 if i = -1 || i = itemcount
2742 then -1
2743 else (
2744 if source#hasaction i
2745 then i
2746 else find (i + incr)
2749 find start
2751 let set active first =
2752 let first = bound first 0 (itemcount - fstate.maxrows) in
2753 state.text <- E.s;
2754 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2756 let navigate incr =
2757 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2758 let active, first =
2759 let incr1 = if incr > 0 then 1 else -1 in
2760 if isvisible m_first m_active
2761 then
2762 let next =
2763 let next = m_active + incr in
2764 let next =
2765 if next < 0 || next >= itemcount
2766 then -1
2767 else find next incr1
2769 if abs (m_active - next) > fstate.maxrows
2770 then -1
2771 else next
2773 if next = -1
2774 then
2775 let first = m_first + incr in
2776 let first = bound first 0 (itemcount - fstate.maxrows) in
2777 let next =
2778 let next = m_active + incr in
2779 let next = bound next 0 (itemcount - 1) in
2780 find next ~-incr1
2782 let active =
2783 if next = -1
2784 then m_active
2785 else (
2786 if isvisible first next
2787 then next
2788 else m_active
2791 active, first
2792 else
2793 let first = min next m_first in
2794 let first =
2795 if abs (next - first) > fstate.maxrows
2796 then first + incr
2797 else first
2799 next, first
2800 else
2801 let first = m_first + incr in
2802 let first = bound first 0 (itemcount - 1) in
2803 let active =
2804 let next = m_active + incr in
2805 let next = bound next 0 (itemcount - 1) in
2806 let next = find next incr1 in
2807 let active =
2808 if next = -1 || abs (m_active - first) > fstate.maxrows
2809 then (
2810 let active = if m_active = -1 then next else m_active in
2811 active
2813 else next
2815 if isvisible first active
2816 then active
2817 else -1
2819 active, first
2821 G.postRedisplay "listview navigate";
2822 set active first;
2824 let open Keys in
2825 match Wsi.kc2kt key with
2826 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2827 let incr = if c = 'r' then -1 else 1 in
2828 let active, first =
2829 match search (m_active + incr) m_qsearch incr with
2830 | None ->
2831 state.text <- m_qsearch ^ " [not found]";
2832 m_active, m_first
2833 | Some active ->
2834 state.text <- m_qsearch;
2835 active, firstof m_first active
2837 G.postRedisplay "listview ctrl-r/s";
2838 set1 active first m_qsearch;
2840 | Insert when Wsi.withctrl mask ->
2841 if m_active >= 0 && m_active < source#getitemcount
2842 then (
2843 let s, _ = source#getitem m_active in
2844 selstring s;
2846 coe self
2848 | Backspace ->
2849 if emptystr m_qsearch
2850 then coe self
2851 else (
2852 let qsearch = withoutlastutf8 m_qsearch in
2853 if emptystr qsearch
2854 then (
2855 state.text <- E.s;
2856 G.postRedisplay "listview empty qsearch";
2857 set1 m_active m_first E.s;
2859 else
2860 let active, first =
2861 match search m_active qsearch ~-1 with
2862 | None ->
2863 state.text <- qsearch ^ " [not found]";
2864 m_active, m_first
2865 | Some active ->
2866 state.text <- qsearch;
2867 active, firstof m_first active
2869 G.postRedisplay "listview backspace qsearch";
2870 set1 active first qsearch
2873 | _ when not (Wsi.isspecialkey key) ->
2874 let pattern = m_qsearch ^ toutf8 key in
2875 let active, first =
2876 match search m_active pattern 1 with
2877 | None ->
2878 state.text <- pattern ^ " [not found]";
2879 m_active, m_first
2880 | Some active ->
2881 state.text <- pattern;
2882 active, firstof m_first active
2884 G.postRedisplay "listview qsearch add";
2885 set1 active first pattern;
2887 | Escape ->
2888 state.text <- E.s;
2889 if emptystr m_qsearch
2890 then (
2891 G.postRedisplay "list view escape";
2892 let mx, my = state.mpos in
2893 updateunder mx my;
2894 begin
2895 match
2896 source#exit ~uioh:(coe self)
2897 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2898 with
2899 | None -> m_prev_uioh
2900 | Some uioh -> uioh
2903 else (
2904 G.postRedisplay "list view kill qsearch";
2905 coe {< m_qsearch = E.s >}
2908 | Enter | KPenter ->
2909 state.text <- E.s;
2910 let self = {< m_qsearch = E.s >} in
2911 let opt =
2912 G.postRedisplay "listview enter";
2913 if m_active >= 0 && m_active < source#getitemcount
2914 then (
2915 source#exit ~uioh:(coe self) ~cancel:false
2916 ~active:m_active ~first:m_first ~pan:m_pan;
2918 else (
2919 source#exit ~uioh:(coe self) ~cancel:true
2920 ~active:m_active ~first:m_first ~pan:m_pan;
2923 begin match opt with
2924 | None -> m_prev_uioh
2925 | Some uioh -> uioh
2928 | Delete | KPdelete ->
2929 coe self
2931 | Up | KPup -> navigate ~-1
2932 | Down | KPdown -> navigate 1
2933 | Prior | KPprior -> navigate ~-(fstate.maxrows)
2934 | Next | KPnext -> navigate fstate.maxrows
2936 | Right | KPright ->
2937 state.text <- E.s;
2938 G.postRedisplay "listview right";
2939 coe {< m_pan = m_pan - 1 >}
2941 | Left | KPleft ->
2942 state.text <- E.s;
2943 G.postRedisplay "listview left";
2944 coe {< m_pan = m_pan + 1 >}
2946 | Home | KPhome ->
2947 let active = find 0 1 in
2948 G.postRedisplay "listview home";
2949 set active 0;
2951 | End | KPend ->
2952 let first = max 0 (itemcount - fstate.maxrows) in
2953 let active = find (itemcount - 1) ~-1 in
2954 G.postRedisplay "listview end";
2955 set active first;
2957 | _ when (key = 0 || Wsi.isspecialkey key) ->
2958 coe self
2960 | (Insert|KPminus|KPplus|Ascii _|Code _|Fn _) ->
2961 dolog "listview unknown key %#x" key; coe self
2963 method key key mask =
2964 match state.mode with
2965 | Textentry te ->
2966 textentrykeyboard key mask te;
2967 coe self
2968 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2970 method button button down x y _ =
2971 let opt =
2972 match button with
2973 | 1 when vscrollhit x ->
2974 G.postRedisplay "listview scroll";
2975 if down
2976 then
2977 let _, position, sh = self#scrollph in
2978 if y > truncate position && y < truncate (position +. sh)
2979 then (
2980 state.mstate <- Mscrolly;
2981 Some (coe self)
2983 else
2984 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2985 let first = truncate (s *. float source#getitemcount) in
2986 let first = min source#getitemcount first in
2987 Some (coe {< m_first = first; m_active = first >})
2988 else (
2989 state.mstate <- Mnone;
2990 Some (coe self);
2992 | 1 when down ->
2993 begin match self#elemunder y with
2994 | Some n ->
2995 G.postRedisplay "listview click";
2996 source#exit ~uioh:(coe {< m_active = n >})
2997 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
2998 | _ ->
2999 Some (coe self)
3001 | n when (n == 4 || n == 5) && not down ->
3002 let len = source#getitemcount in
3003 let first =
3004 if n = 5 && m_first + fstate.maxrows >= len
3005 then
3006 m_first
3007 else
3008 let first = m_first + (if n == 4 then -1 else 1) in
3009 bound first 0 (len - 1)
3011 G.postRedisplay "listview wheel";
3012 Some (coe {< m_first = first >})
3013 | n when (n = 6 || n = 7) && not down ->
3014 let inc = if n = 7 then -1 else 1 in
3015 G.postRedisplay "listview hwheel";
3016 Some (coe {< m_pan = m_pan + inc >})
3017 | _ ->
3018 Some (coe self)
3020 match opt with
3021 | None -> m_prev_uioh
3022 | Some uioh -> uioh
3024 method multiclick _ x y = self#button 1 true x y
3026 method motion _ y =
3027 match state.mstate with
3028 | Mscrolly ->
3029 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3030 let first = truncate (s *. float source#getitemcount) in
3031 let first = min source#getitemcount first in
3032 G.postRedisplay "listview motion";
3033 coe {< m_first = first; m_active = first >}
3034 | Msel _
3035 | Mpan _
3036 | Mscrollx
3037 | Mzoom _
3038 | Mzoomrect _
3039 | Mnone -> coe self
3041 method pmotion x y =
3042 if x < state.winw - conf.scrollbw
3043 then
3044 let n =
3045 match self#elemunder y with
3046 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3047 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3049 let o =
3050 if n != m_active
3051 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3052 else self
3054 coe o
3055 else (
3056 Wsi.setcursor Wsi.CURSOR_INHERIT;
3057 coe self
3060 method infochanged _ = ()
3062 method scrollpw = (0, 0.0, 0.0)
3063 method scrollph =
3064 let nfs = fstate.fontsize + 1 in
3065 let y = m_first * nfs in
3066 let itemcount = source#getitemcount in
3067 let maxi = max 0 (itemcount - fstate.maxrows) in
3068 let maxy = maxi * nfs in
3069 let p, h = scrollph y maxy in
3070 conf.scrollbw, p, h
3072 method modehash = modehash
3073 method eformsgs = false
3074 method alwaysscrolly = true
3075 end;;
3077 class outlinelistview ~zebra ~source =
3078 let settext autonarrow s =
3079 if autonarrow
3080 then
3081 let ss = source#statestr in
3082 state.text <-
3083 if emptystr ss
3084 then "[" ^ s ^ "]"
3085 else "{" ^ ss ^ "} [" ^ s ^ "]"
3086 else state.text <- s
3088 object (self)
3089 inherit listview
3090 ~zebra
3091 ~helpmode:false
3092 ~source:(source :> lvsource)
3093 ~trusted:false
3094 ~modehash:(findkeyhash conf "outline")
3095 as super
3097 val m_autonarrow = false
3099 method! key key mask =
3100 let maxrows =
3101 if emptystr state.text
3102 then fstate.maxrows
3103 else fstate.maxrows - 2
3105 let calcfirst first active =
3106 if active > first
3107 then
3108 let rows = active - first in
3109 if rows > maxrows then active - maxrows else first
3110 else active
3112 let navigate incr =
3113 let active = m_active + incr in
3114 let active = bound active 0 (source#getitemcount - 1) in
3115 let first = calcfirst m_first active in
3116 G.postRedisplay "outline navigate";
3117 coe {< m_active = active; m_first = first >}
3119 let navscroll first =
3120 let active =
3121 let dist = m_active - first in
3122 if dist < 0
3123 then first
3124 else (
3125 if dist < maxrows
3126 then m_active
3127 else first + maxrows
3130 G.postRedisplay "outline navscroll";
3131 coe {< m_first = first; m_active = active >}
3133 let ctrl = Wsi.withctrl mask in
3134 let open Keys in
3135 match Wsi.kc2kt key with
3136 | Ascii 'a' when ctrl ->
3137 let text =
3138 if m_autonarrow
3139 then (source#denarrow; E.s)
3140 else (
3141 let pattern = source#renarrow in
3142 if nonemptystr m_qsearch
3143 then (source#narrow m_qsearch; m_qsearch)
3144 else pattern
3147 settext (not m_autonarrow) text;
3148 G.postRedisplay "toggle auto narrowing";
3149 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3151 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
3152 settext true E.s;
3153 G.postRedisplay "toggle auto narrowing";
3154 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3156 | Ascii 'n' when ctrl ->
3157 source#narrow m_qsearch;
3158 if not m_autonarrow
3159 then source#add_narrow_pattern m_qsearch;
3160 G.postRedisplay "outline ctrl-n";
3161 coe {< m_first = 0; m_active = 0 >}
3163 | Ascii 'S' when ctrl ->
3164 let active = source#calcactive (getanchor ()) in
3165 let first = firstof m_first active in
3166 G.postRedisplay "outline ctrl-s";
3167 coe {< m_first = first; m_active = active >}
3169 | Ascii 'u' when ctrl ->
3170 G.postRedisplay "outline ctrl-u";
3171 if m_autonarrow && nonemptystr m_qsearch
3172 then (
3173 ignore (source#renarrow);
3174 settext m_autonarrow E.s;
3175 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3177 else (
3178 source#del_narrow_pattern;
3179 let pattern = source#renarrow in
3180 let text =
3181 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3183 settext m_autonarrow text;
3184 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3187 | Ascii 'l' when ctrl ->
3188 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3189 G.postRedisplay "outline ctrl-l";
3190 coe {< m_first = first >}
3192 | Ascii '\t' when m_autonarrow ->
3193 if nonemptystr m_qsearch
3194 then (
3195 G.postRedisplay "outline list view tab";
3196 source#add_narrow_pattern m_qsearch;
3197 settext true E.s;
3198 coe {< m_qsearch = E.s >}
3200 else coe self
3202 | Escape when m_autonarrow ->
3203 if nonemptystr m_qsearch
3204 then source#add_narrow_pattern m_qsearch;
3205 super#key key mask
3207 | Enter | KPenter when m_autonarrow ->
3208 if nonemptystr m_qsearch
3209 then source#add_narrow_pattern m_qsearch;
3210 super#key key mask
3212 | _ when m_autonarrow && (not (Wsi.isspecialkey key)) ->
3213 let pattern = m_qsearch ^ toutf8 key in
3214 G.postRedisplay "outlinelistview autonarrow add";
3215 source#narrow pattern;
3216 settext true pattern;
3217 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3219 | key when m_autonarrow && key = Backspace ->
3220 if emptystr m_qsearch
3221 then coe self
3222 else
3223 let pattern = withoutlastutf8 m_qsearch in
3224 G.postRedisplay "outlinelistview autonarrow backspace";
3225 ignore (source#renarrow);
3226 source#narrow pattern;
3227 settext true pattern;
3228 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3230 | Up | KPup when ctrl ->
3231 navscroll (max 0 (m_first - 1))
3233 | Down | KPdown when ctrl ->
3234 navscroll (min (source#getitemcount - 1) (m_first + 1))
3236 | Up | KPup -> navigate ~-1
3237 | Down | KPdown -> navigate 1
3238 | Prior | KPprior -> navigate ~-(fstate.maxrows)
3239 | Next | KPnext -> navigate fstate.maxrows
3241 | Right | KPright ->
3242 let o =
3243 if ctrl
3244 then (
3245 G.postRedisplay "outline ctrl right";
3246 {< m_pan = m_pan + 1 >}
3248 else self#updownlevel 1
3250 coe o
3252 | Left | KPleft ->
3253 let o =
3254 if ctrl
3255 then (
3256 G.postRedisplay "outline ctrl left";
3257 {< m_pan = m_pan - 1 >}
3259 else self#updownlevel ~-1
3261 coe o
3263 | Home | KPhome ->
3264 G.postRedisplay "outline home";
3265 coe {< m_first = 0; m_active = 0 >}
3267 | End | KPend ->
3268 let active = source#getitemcount - 1 in
3269 let first = max 0 (active - fstate.maxrows) in
3270 G.postRedisplay "outline end";
3271 coe {< m_active = active; m_first = first >}
3273 | Delete|Escape|Insert|Enter|KPdelete|KPenter|KPminus
3274 | KPplus|Ascii _|Code _|Backspace|Fn _ -> super#key key mask
3275 end;;
3277 let genhistoutlines () =
3278 Config.gethist ()
3279 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3280 compare c2.lastvisit c1.lastvisit)
3281 |> List.map
3282 (fun ((path, c, _, _, _, origin) as hist) ->
3283 let path = if nonemptystr origin then origin else path in
3284 let base = mbtoutf8 @@ Filename.basename path in
3285 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3287 |> Array.of_list
3290 let gotohist (path, c, bookmarks, x, anchor, origin) =
3291 Config.save leavebirdseye;
3292 state.anchor <- anchor;
3293 state.bookmarks <- bookmarks;
3294 state.origin <- origin;
3295 state.x <- x;
3296 setconf conf c;
3297 let x0, y0, x1, y1 = conf.trimfuzz in
3298 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3299 reshape ~firsttime:true state.winw state.winh;
3300 opendoc path origin;
3301 setzoom c.zoom;
3304 let makecheckers () =
3305 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3306 following to say:
3307 converted by Issac Trotts. July 25, 2002 *)
3308 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3309 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3310 let id = GlTex.gen_texture () in
3311 GlTex.bind_texture ~target:`texture_2d id;
3312 GlPix.store (`unpack_alignment 1);
3313 GlTex.image2d image;
3314 List.iter (GlTex.parameter ~target:`texture_2d)
3315 [ `mag_filter `nearest; `min_filter `nearest ];
3319 let setcheckers enabled =
3320 match state.checkerstexid with
3321 | None ->
3322 if enabled then state.checkerstexid <- Some (makecheckers ())
3324 | Some checkerstexid ->
3325 if not enabled
3326 then (
3327 GlTex.delete_texture checkerstexid;
3328 state.checkerstexid <- None;
3332 let describe_location () =
3333 let fn = page_of_y state.y in
3334 let ln = page_of_y (state.y + state.winh - 1) in
3335 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3336 let percent =
3337 if maxy <= 0
3338 then 100.
3339 else (100. *. (float state.y /. float maxy))
3341 if fn = ln
3342 then
3343 Printf.sprintf "page %d of %d [%.2f%%]"
3344 (fn+1) state.pagecount percent
3345 else
3346 Printf.sprintf
3347 "pages %d-%d of %d [%.2f%%]"
3348 (fn+1) (ln+1) state.pagecount percent
3351 let setpresentationmode v =
3352 let n = page_of_y state.y in
3353 state.anchor <- (n, 0.0, 1.0);
3354 conf.presentation <- v;
3355 if conf.fitmodel = FitPage
3356 then reqlayout conf.angle conf.fitmodel;
3357 represent ();
3360 let setbgcol (r, g, b) =
3361 let col =
3362 let r = r *. 255.0 |> truncate
3363 and g = g *. 255.0 |> truncate
3364 and b = b *. 255.0 |> truncate in
3365 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3367 Wsi.setwinbgcol col;
3370 let enterinfomode =
3371 let btos b = if b then UniSyms.radical else E.s in
3372 let showextended = ref false in
3373 let leave mode _ = state.mode <- mode in
3374 let src =
3375 (object
3376 val mutable m_l = []
3377 val mutable m_a = E.a
3378 val mutable m_prev_uioh = nouioh
3379 val mutable m_prev_mode = View
3381 inherit lvsourcebase
3383 method reset prev_mode prev_uioh =
3384 m_a <- Array.of_list (List.rev m_l);
3385 m_l <- [];
3386 m_prev_mode <- prev_mode;
3387 m_prev_uioh <- prev_uioh;
3389 method int name get set =
3390 m_l <-
3391 (name, `int get, 1,
3392 Action (
3393 fun u ->
3394 let ondone s =
3395 try set (int_of_string s)
3396 with exn ->
3397 state.text <- Printf.sprintf "bad integer `%s': %s"
3398 s @@ exntos exn
3400 state.text <- E.s;
3401 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3402 state.mode <- Textentry (te, leave m_prev_mode);
3404 )) :: m_l
3406 method int_with_suffix name get set =
3407 m_l <-
3408 (name, `intws get, 1,
3409 Action (
3410 fun u ->
3411 let ondone s =
3412 try set (int_of_string_with_suffix s)
3413 with exn ->
3414 state.text <- Printf.sprintf "bad integer `%s': %s"
3415 s @@ exntos exn
3417 state.text <- E.s;
3418 let te =
3419 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3421 state.mode <- Textentry (te, leave m_prev_mode);
3423 )) :: m_l
3425 method bool ?(offset=1) ?(btos=btos) name get set =
3426 m_l <-
3427 (name, `bool (btos, get), offset, Action (
3428 fun u ->
3429 let v = get () in
3430 set (not v);
3432 )) :: m_l
3434 method color name get set =
3435 m_l <-
3436 (name, `color get, 1,
3437 Action (
3438 fun u ->
3439 let invalid = (nan, nan, nan) in
3440 let ondone s =
3441 let c =
3442 try color_of_string s
3443 with exn ->
3444 state.text <- Printf.sprintf "bad color `%s': %s"
3445 s @@ exntos exn;
3446 invalid
3448 if c <> invalid
3449 then set c;
3451 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3452 state.text <- color_to_string (get ());
3453 state.mode <- Textentry (te, leave m_prev_mode);
3455 )) :: m_l
3457 method string name get set =
3458 m_l <-
3459 (name, `string get, 1,
3460 Action (
3461 fun u ->
3462 let ondone s = set s in
3463 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3464 state.mode <- Textentry (te, leave m_prev_mode);
3466 )) :: m_l
3468 method colorspace name get set =
3469 m_l <-
3470 (name, `string get, 1,
3471 Action (
3472 fun _ ->
3473 let source =
3474 (object
3475 inherit lvsourcebase
3477 initializer
3478 m_active <- CSTE.to_int conf.colorspace;
3479 m_first <- 0;
3481 method getitemcount =
3482 Array.length CSTE.names
3483 method getitem n =
3484 (CSTE.names.(n), 0)
3485 method exit ~uioh ~cancel ~active ~first ~pan =
3486 ignore (uioh, first, pan);
3487 if not cancel then set active;
3488 None
3489 method hasaction _ = true
3490 end)
3492 state.text <- E.s;
3493 let modehash = findkeyhash conf "info" in
3494 coe (new listview ~zebra:false ~helpmode:false
3495 ~source ~trusted:true ~modehash)
3496 )) :: m_l
3498 method paxmark name get set =
3499 m_l <-
3500 (name, `string get, 1,
3501 Action (
3502 fun _ ->
3503 let source =
3504 (object
3505 inherit lvsourcebase
3507 initializer
3508 m_active <- MTE.to_int conf.paxmark;
3509 m_first <- 0;
3511 method getitemcount = Array.length MTE.names
3512 method getitem n = (MTE.names.(n), 0)
3513 method exit ~uioh ~cancel ~active ~first ~pan =
3514 ignore (uioh, first, pan);
3515 if not cancel then set active;
3516 None
3517 method hasaction _ = true
3518 end)
3520 state.text <- E.s;
3521 let modehash = findkeyhash conf "info" in
3522 coe (new listview ~zebra:false ~helpmode:false
3523 ~source ~trusted:true ~modehash)
3524 )) :: m_l
3526 method fitmodel name get set =
3527 m_l <-
3528 (name, `string get, 1,
3529 Action (
3530 fun _ ->
3531 let source =
3532 (object
3533 inherit lvsourcebase
3535 initializer
3536 m_active <- FMTE.to_int conf.fitmodel;
3537 m_first <- 0;
3539 method getitemcount = Array.length FMTE.names
3540 method getitem n = (FMTE.names.(n), 0)
3541 method exit ~uioh ~cancel ~active ~first ~pan =
3542 ignore (uioh, first, pan);
3543 if not cancel then set active;
3544 None
3545 method hasaction _ = true
3546 end)
3548 state.text <- E.s;
3549 let modehash = findkeyhash conf "info" in
3550 coe (new listview ~zebra:false ~helpmode:false
3551 ~source ~trusted:true ~modehash)
3552 )) :: m_l
3554 method caption s offset =
3555 m_l <- (s, `empty, offset, Noaction) :: m_l
3557 method caption2 s f offset =
3558 m_l <- (s, `string f, offset, Noaction) :: m_l
3560 method getitemcount = Array.length m_a
3562 method getitem n =
3563 let tostr = function
3564 | `int f -> string_of_int (f ())
3565 | `intws f -> string_with_suffix_of_int (f ())
3566 | `string f -> f ()
3567 | `color f -> color_to_string (f ())
3568 | `bool (btos, f) -> btos (f ())
3569 | `empty -> E.s
3571 let name, t, offset, _ = m_a.(n) in
3572 ((let s = tostr t in
3573 if nonemptystr s
3574 then Printf.sprintf "%s\t%s" name s
3575 else name),
3576 offset)
3578 method exit ~uioh ~cancel ~active ~first ~pan =
3579 let uiohopt =
3580 if not cancel
3581 then (
3582 let uioh =
3583 match m_a.(active) with
3584 | _, _, _, Action f -> f uioh
3585 | _, _, _, Noaction -> uioh
3587 Some uioh
3589 else None
3591 m_active <- active;
3592 m_first <- first;
3593 m_pan <- pan;
3594 uiohopt
3596 method hasaction n =
3597 match m_a.(n) with
3598 | _, _, _, Action _ -> true
3599 | _, _, _, Noaction -> false
3601 initializer m_active <- 1
3602 end)
3604 let rec fillsrc prevmode prevuioh =
3605 let sep () = src#caption E.s 0 in
3606 let colorp name get set =
3607 src#string name
3608 (fun () -> color_to_string (get ()))
3609 (fun v ->
3611 let c = color_of_string v in
3612 set c
3613 with exn ->
3614 state.text <-
3615 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3618 let oldmode = state.mode in
3619 let birdseye = isbirdseye state.mode in
3621 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3623 src#bool "presentation mode"
3624 (fun () -> conf.presentation)
3625 (fun v -> setpresentationmode v);
3627 src#bool "ignore case in searches"
3628 (fun () -> conf.icase)
3629 (fun v -> conf.icase <- v);
3631 src#bool "preload"
3632 (fun () -> conf.preload)
3633 (fun v -> conf.preload <- v);
3635 src#bool "highlight links"
3636 (fun () -> conf.hlinks)
3637 (fun v -> conf.hlinks <- v);
3639 src#bool "under info"
3640 (fun () -> conf.underinfo)
3641 (fun v -> conf.underinfo <- v);
3643 src#bool "persistent bookmarks"
3644 (fun () -> conf.savebmarks)
3645 (fun v -> conf.savebmarks <- v);
3647 src#fitmodel "fit model"
3648 (fun () -> FMTE.to_string conf.fitmodel)
3649 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3651 src#bool "trim margins"
3652 (fun () -> conf.trimmargins)
3653 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3655 src#bool "persistent location"
3656 (fun () -> conf.jumpback)
3657 (fun v -> conf.jumpback <- v);
3659 sep ();
3660 src#int "inter-page space"
3661 (fun () -> conf.interpagespace)
3662 (fun n ->
3663 conf.interpagespace <- n;
3664 docolumns conf.columns;
3665 let pageno, py =
3666 match state.layout with
3667 | [] -> 0, 0
3668 | l :: _ ->
3669 l.pageno, l.pagey
3671 state.maxy <- calcheight ();
3672 let y = getpagey pageno in
3673 gotoxy state.x (y + py)
3676 src#int "page bias"
3677 (fun () -> conf.pagebias)
3678 (fun v -> conf.pagebias <- v);
3680 src#int "scroll step"
3681 (fun () -> conf.scrollstep)
3682 (fun n -> conf.scrollstep <- n);
3684 src#int "horizontal scroll step"
3685 (fun () -> conf.hscrollstep)
3686 (fun v -> conf.hscrollstep <- v);
3688 src#int "auto scroll step"
3689 (fun () ->
3690 match state.autoscroll with
3691 | Some step -> step
3692 | _ -> conf.autoscrollstep)
3693 (fun n ->
3694 let n = boundastep state.winh n in
3695 if state.autoscroll <> None
3696 then state.autoscroll <- Some n;
3697 conf.autoscrollstep <- n);
3699 src#int "zoom"
3700 (fun () -> truncate (conf.zoom *. 100.))
3701 (fun v -> pivotzoom ((float v) /. 100.));
3703 src#int "rotation"
3704 (fun () -> conf.angle)
3705 (fun v -> reqlayout v conf.fitmodel);
3707 src#int "scroll bar width"
3708 (fun () -> conf.scrollbw)
3709 (fun v ->
3710 conf.scrollbw <- v;
3711 reshape state.winw state.winh;
3714 src#int "scroll handle height"
3715 (fun () -> conf.scrollh)
3716 (fun v -> conf.scrollh <- v;);
3718 src#int "thumbnail width"
3719 (fun () -> conf.thumbw)
3720 (fun v ->
3721 conf.thumbw <- min 4096 v;
3722 match oldmode with
3723 | Birdseye beye ->
3724 leavebirdseye beye false;
3725 enterbirdseye ()
3726 | Textentry _
3727 | View
3728 | LinkNav _ -> ()
3731 let mode = state.mode in
3732 src#string "columns"
3733 (fun () ->
3734 match conf.columns with
3735 | Csingle _ -> "1"
3736 | Cmulti (multi, _) -> multicolumns_to_string multi
3737 | Csplit (count, _) -> "-" ^ string_of_int count
3739 (fun v ->
3740 let n, a, b = multicolumns_of_string v in
3741 setcolumns mode n a b);
3743 sep ();
3744 src#caption "Pixmap cache" 0;
3745 src#int_with_suffix "size (advisory)"
3746 (fun () -> conf.memlimit)
3747 (fun v -> conf.memlimit <- v);
3749 src#caption2 "used"
3750 (fun () ->
3751 Printf.sprintf "%s bytes, %d tiles"
3752 (string_with_suffix_of_int state.memused)
3753 (Hashtbl.length state.tilemap)) 1;
3755 sep ();
3756 src#caption "Layout" 0;
3757 src#caption2 "Dimension"
3758 (fun () ->
3759 Printf.sprintf "%dx%d (virtual %dx%d)"
3760 state.winw state.winh
3761 state.w state.maxy)
3763 if conf.debug
3764 then
3765 src#caption2 "Position" (fun () ->
3766 Printf.sprintf "%dx%d" state.x state.y
3768 else
3769 src#caption2 "Position" (fun () -> describe_location ()) 1
3772 sep ();
3773 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3774 "Save these parameters as global defaults at exit"
3775 (fun () -> conf.bedefault)
3776 (fun v -> conf.bedefault <- v)
3779 sep ();
3780 let btos b = if b then UniSyms.lguillemet else UniSyms.rguillemet in
3781 src#bool ~offset:0 ~btos "Extended parameters"
3782 (fun () -> !showextended)
3783 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3784 if !showextended
3785 then (
3786 src#bool "checkers"
3787 (fun () -> conf.checkers)
3788 (fun v -> conf.checkers <- v; setcheckers v);
3789 src#bool "update cursor"
3790 (fun () -> conf.updatecurs)
3791 (fun v -> conf.updatecurs <- v);
3792 src#bool "scroll-bar on the left"
3793 (fun () -> conf.leftscroll)
3794 (fun v -> conf.leftscroll <- v);
3795 src#bool "verbose"
3796 (fun () -> conf.verbose)
3797 (fun v -> conf.verbose <- v);
3798 src#bool "invert colors"
3799 (fun () -> conf.invert)
3800 (fun v -> conf.invert <- v);
3801 src#bool "max fit"
3802 (fun () -> conf.maxhfit)
3803 (fun v -> conf.maxhfit <- v);
3804 src#bool "pax mode"
3805 (fun () -> conf.pax != None)
3806 (fun v ->
3807 if v
3808 then conf.pax <- Some (ref (now (), 0, 0))
3809 else conf.pax <- None);
3810 src#string "uri launcher"
3811 (fun () -> conf.urilauncher)
3812 (fun v -> conf.urilauncher <- v);
3813 src#string "path launcher"
3814 (fun () -> conf.pathlauncher)
3815 (fun v -> conf.pathlauncher <- v);
3816 src#string "tile size"
3817 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3818 (fun v ->
3820 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3821 conf.tilew <- max 64 w;
3822 conf.tileh <- max 64 h;
3823 flushtiles ();
3824 with exn ->
3825 state.text <- Printf.sprintf "bad tile size `%s': %s"
3826 v @@ exntos exn
3828 src#int "texture count"
3829 (fun () -> conf.texcount)
3830 (fun v ->
3831 if realloctexts v
3832 then conf.texcount <- v
3833 else impmsg "failed to set texture count please retry later"
3835 src#int "slice height"
3836 (fun () -> conf.sliceheight)
3837 (fun v ->
3838 conf.sliceheight <- v;
3839 wcmd "sliceh %d" conf.sliceheight;
3841 src#int "anti-aliasing level"
3842 (fun () -> conf.aalevel)
3843 (fun v ->
3844 conf.aalevel <- bound v 0 8;
3845 state.anchor <- getanchor ();
3846 opendoc state.path state.password;
3848 src#string "page scroll scaling factor"
3849 (fun () -> string_of_float conf.pgscale)
3850 (fun v ->
3852 let s = float_of_string v in
3853 conf.pgscale <- s
3854 with exn ->
3855 state.text <- Printf.sprintf
3856 "bad page scroll scaling factor `%s': %s" v
3857 @@ exntos exn
3860 src#int "ui font size"
3861 (fun () -> fstate.fontsize)
3862 (fun v -> setfontsize (bound v 5 100));
3863 src#int "hint font size"
3864 (fun () -> conf.hfsize)
3865 (fun v -> conf.hfsize <- bound v 5 100);
3866 colorp "background color"
3867 (fun () -> conf.bgcolor)
3868 (fun v -> conf.bgcolor <- v; setbgcol v);
3869 src#bool "crop hack"
3870 (fun () -> conf.crophack)
3871 (fun v -> conf.crophack <- v);
3872 src#string "trim fuzz"
3873 (fun () -> irect_to_string conf.trimfuzz)
3874 (fun v ->
3876 conf.trimfuzz <- irect_of_string v;
3877 if conf.trimmargins
3878 then settrim true conf.trimfuzz;
3879 with exn ->
3880 state.text <- Printf.sprintf "bad irect `%s': %s" v
3881 @@ exntos exn
3883 src#string "throttle"
3884 (fun () ->
3885 match conf.maxwait with
3886 | None -> "show place holder if page is not ready"
3887 | Some time ->
3888 if time = infinity
3889 then "wait for page to fully render"
3890 else
3891 "wait " ^ string_of_float time
3892 ^ " seconds before showing placeholder"
3894 (fun v ->
3896 let f = float_of_string v in
3897 if f <= 0.0
3898 then conf.maxwait <- None
3899 else conf.maxwait <- Some f
3900 with exn ->
3901 state.text <- Printf.sprintf "bad time `%s': %s" v
3902 @@ exntos exn
3904 src#string "ghyll scroll"
3905 (fun () ->
3906 match conf.ghyllscroll with
3907 | None -> E.s
3908 | Some nab -> ghyllscroll_to_string nab
3910 (fun v ->
3911 try conf.ghyllscroll <- ghyllscroll_of_string v
3912 with
3913 | Failure msg ->
3914 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3915 | exn ->
3916 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3917 @@ exntos exn
3919 src#string "selection command"
3920 (fun () -> conf.selcmd)
3921 (fun v -> conf.selcmd <- v);
3922 src#string "synctex command"
3923 (fun () -> conf.stcmd)
3924 (fun v -> conf.stcmd <- v);
3925 src#string "pax command"
3926 (fun () -> conf.paxcmd)
3927 (fun v -> conf.paxcmd <- v);
3928 src#string "ask password command"
3929 (fun () -> conf.passcmd)
3930 (fun v -> conf.passcmd <- v);
3931 src#string "save path command"
3932 (fun () -> conf.savecmd)
3933 (fun v -> conf.savecmd <- v);
3934 src#colorspace "color space"
3935 (fun () -> CSTE.to_string conf.colorspace)
3936 (fun v ->
3937 conf.colorspace <- CSTE.of_int v;
3938 wcmd "cs %d" v;
3939 load state.layout;
3941 src#paxmark "pax mark method"
3942 (fun () -> MTE.to_string conf.paxmark)
3943 (fun v -> conf.paxmark <- MTE.of_int v);
3944 if bousable () && !opengl_has_pbo
3945 then
3946 src#bool "use PBO"
3947 (fun () -> conf.usepbo)
3948 (fun v -> conf.usepbo <- v);
3949 src#bool "mouse wheel scrolls pages"
3950 (fun () -> conf.wheelbypage)
3951 (fun v -> conf.wheelbypage <- v);
3952 src#bool "open remote links in a new instance"
3953 (fun () -> conf.riani)
3954 (fun v -> conf.riani <- v);
3955 src#bool "edit annotations inline"
3956 (fun () -> conf.annotinline)
3957 (fun v -> conf.annotinline <- v);
3958 src#bool "coarse positioning in presentation mode"
3959 (fun () -> conf.coarseprespos)
3960 (fun v -> conf.coarseprespos <- v);
3961 src#bool "use document CSS"
3962 (fun () -> conf.usedoccss)
3963 (fun v ->
3964 conf.usedoccss <- v;
3965 state.anchor <- getanchor ();
3966 opendoc state.path state.password;
3970 sep ();
3971 src#caption "Document" 0;
3972 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3973 src#caption2 "Pages"
3974 (fun () -> string_of_int state.pagecount) 1;
3975 src#caption2 "Dimensions"
3976 (fun () -> string_of_int (List.length state.pdims)) 1;
3977 if nonemptystr conf.css
3978 then src#caption2 "CSS" (fun () -> conf.css) 1;
3979 if conf.trimmargins
3980 then (
3981 sep ();
3982 src#caption "Trimmed margins" 0;
3983 src#caption2 "Dimensions"
3984 (fun () -> string_of_int (List.length state.pdims)) 1;
3987 sep ();
3988 src#caption "OpenGL" 0;
3989 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3990 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3992 sep ();
3993 src#caption "Location" 0;
3994 if nonemptystr state.origin
3995 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3996 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3998 src#reset prevmode prevuioh;
4000 fun () ->
4001 state.text <- E.s;
4002 resetmstate ();
4003 let prevmode = state.mode
4004 and prevuioh = state.uioh in
4005 fillsrc prevmode prevuioh;
4006 let source = (src :> lvsource) in
4007 let modehash = findkeyhash conf "info" in
4008 state.uioh <-
4009 coe (object (self)
4010 inherit listview ~zebra:false ~helpmode:false
4011 ~source ~trusted:true ~modehash as super
4012 val mutable m_prevmemused = 0
4013 method! infochanged = function
4014 | Memused ->
4015 if m_prevmemused != state.memused
4016 then (
4017 m_prevmemused <- state.memused;
4018 G.postRedisplay "memusedchanged";
4020 | Pdim -> G.postRedisplay "pdimchanged"
4021 | Docinfo -> fillsrc prevmode prevuioh
4023 method! key key mask =
4024 if not (Wsi.withctrl mask)
4025 then
4026 let open Keys in
4027 begin [@warning "-4"]
4028 match Wsi.kc2kt key with
4029 | Left | KPleft -> coe (self#updownlevel ~-1)
4030 | Right | KPright -> coe (self#updownlevel 1)
4031 | _ -> super#key key mask
4033 else super#key key mask
4034 end);
4035 G.postRedisplay "info";
4038 let enterhelpmode =
4039 let source =
4040 (object
4041 inherit lvsourcebase
4042 method getitemcount = Array.length state.help
4043 method getitem n =
4044 let s, l, _ = state.help.(n) in
4045 (s, l)
4047 method exit ~uioh ~cancel ~active ~first ~pan =
4048 let optuioh =
4049 if not cancel
4050 then (
4051 match state.help.(active) with
4052 | _, _, Action f -> Some (f uioh)
4053 | _, _, Noaction -> Some uioh
4055 else None
4057 m_active <- active;
4058 m_first <- first;
4059 m_pan <- pan;
4060 optuioh
4062 method hasaction n =
4063 match state.help.(n) with
4064 | _, _, Action _ -> true
4065 | _, _, Noaction -> false
4067 initializer
4068 m_active <- -1
4069 end)
4070 in fun () ->
4071 let modehash = findkeyhash conf "help" in
4072 resetmstate ();
4073 state.uioh <- coe (new listview
4074 ~zebra:false ~helpmode:true
4075 ~source ~trusted:true ~modehash);
4076 G.postRedisplay "help";
4079 let entermsgsmode =
4080 let msgsource =
4081 (object
4082 inherit lvsourcebase
4083 val mutable m_items = E.a
4085 method getitemcount = 1 + Array.length m_items
4087 method getitem n =
4088 if n = 0
4089 then "[Clear]", 0
4090 else m_items.(n-1), 0
4092 method exit ~uioh ~cancel ~active ~first ~pan =
4093 ignore uioh;
4094 if not cancel
4095 then (
4096 if active = 0
4097 then Buffer.clear state.errmsgs;
4099 m_active <- active;
4100 m_first <- first;
4101 m_pan <- pan;
4102 None
4104 method hasaction n =
4105 n = 0
4107 method reset =
4108 state.newerrmsgs <- false;
4109 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4110 m_items <- Array.of_list l
4112 initializer
4113 m_active <- 0
4114 end)
4115 in fun () ->
4116 state.text <- E.s;
4117 resetmstate ();
4118 msgsource#reset;
4119 let source = (msgsource :> lvsource) in
4120 let modehash = findkeyhash conf "listview" in
4121 state.uioh <-
4122 coe (object
4123 inherit listview ~zebra:false ~helpmode:false
4124 ~source ~trusted:false ~modehash as super
4125 method! display =
4126 if state.newerrmsgs
4127 then msgsource#reset;
4128 super#display
4129 end);
4130 G.postRedisplay "msgs";
4133 let getusertext s =
4134 let editor = getenvwithdef "EDITOR" E.s in
4135 if emptystr editor
4136 then E.s
4137 else
4138 let tmppath = Filename.temp_file "llpp" "note" in
4139 if nonemptystr s
4140 then (
4141 let oc = open_out tmppath in
4142 output_string oc s;
4143 close_out oc;
4145 let execstr = editor ^ " " ^ tmppath in
4146 let s =
4147 match spawn execstr [] with
4148 | exception exn ->
4149 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4151 | pid ->
4152 match Unix.waitpid [] pid with
4153 | exception exn ->
4154 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4156 | (_pid, status) ->
4157 match status with
4158 | Unix.WEXITED 0 -> filecontents tmppath
4159 | Unix.WEXITED n ->
4160 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4162 | Unix.WSIGNALED n ->
4163 impmsg "editor process(%s) was killed by signal %d" execstr n;
4165 | Unix.WSTOPPED n ->
4166 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4169 match Unix.unlink tmppath with
4170 | exception exn ->
4171 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4173 | () -> s
4176 let enterannotmode opaque slinkindex =
4177 let msgsource =
4178 (object
4179 inherit lvsourcebase
4180 val mutable m_text = E.s
4181 val mutable m_items = E.a
4183 method getitemcount = Array.length m_items
4185 method getitem n =
4186 let label, _func = m_items.(n) in
4187 label, 0
4189 method exit ~uioh ~cancel ~active ~first ~pan =
4190 ignore (uioh, first, pan);
4191 if not cancel
4192 then (
4193 let _label, func = m_items.(active) in
4194 func ()
4196 None
4198 method hasaction n = nonemptystr @@ fst m_items.(n)
4200 method reset s =
4201 let rec split accu b i =
4202 let p = b+i in
4203 if p = String.length s
4204 then (String.sub s b (p-b), unit) :: accu
4205 else
4206 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4207 then
4208 let ss = if i = 0 then E.s else String.sub s b i in
4209 split ((ss, unit)::accu) (p+1) 0
4210 else
4211 split accu b (i+1)
4213 let cleanup () =
4214 wcmd "freepage %s" (~> opaque);
4215 let keys =
4216 Hashtbl.fold (fun key opaque' accu ->
4217 if opaque' = opaque'
4218 then key :: accu else accu) state.pagemap []
4220 List.iter (Hashtbl.remove state.pagemap) keys;
4221 flushtiles ();
4222 gotoxy state.x state.y
4224 let dele () =
4225 delannot opaque slinkindex;
4226 cleanup ();
4228 let edit inline () =
4229 let update s =
4230 if emptystr s
4231 then dele ()
4232 else (
4233 modannot opaque slinkindex s;
4234 cleanup ();
4237 if inline
4238 then
4239 let mode = state.mode in
4240 state.mode <-
4241 Textentry (
4242 ("annotation: ", m_text, None, textentry, update, true),
4243 fun _ -> state.mode <- mode);
4244 state.text <- E.s;
4245 enttext ();
4246 else
4247 let s = getusertext m_text in
4248 update s
4250 m_text <- s;
4251 m_items <-
4252 ( "[Copy]", fun () -> selstring m_text)
4253 :: ("[Delete]", dele)
4254 :: ("[Edit]", edit conf.annotinline)
4255 :: (E.s, unit)
4256 :: split [] 0 0 |> List.rev |> Array.of_list
4258 initializer
4259 m_active <- 0
4260 end)
4262 state.text <- E.s;
4263 let s = getannotcontents opaque slinkindex in
4264 resetmstate ();
4265 msgsource#reset s;
4266 let source = (msgsource :> lvsource) in
4267 let modehash = findkeyhash conf "listview" in
4268 state.uioh <- coe (object
4269 inherit listview ~zebra:false ~helpmode:false
4270 ~source ~trusted:false ~modehash
4271 end);
4272 G.postRedisplay "enterannotmode";
4275 let gotoremote spec =
4276 let filename, dest = splitatchar spec '#' in
4277 let getpath filename =
4278 let path =
4279 if nonemptystr filename
4280 then
4281 if Filename.is_relative filename
4282 then
4283 let dir = Filename.dirname state.path in
4284 let dir =
4285 if Filename.is_implicit dir
4286 then Filename.concat (Sys.getcwd ()) dir
4287 else dir
4289 Filename.concat dir filename
4290 else filename
4291 else E.s
4293 if Sys.file_exists path
4294 then path
4295 else E.s
4297 let path = getpath filename in
4298 let dospawn lcmd =
4299 if conf.riani
4300 then
4301 let cmd = Lazy.force_val lcmd in
4302 match spawn cmd with
4303 | _pid -> ()
4304 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
4305 else
4306 let anchor = getanchor () in
4307 let ranchor = state.path, state.password, anchor, state.origin in
4308 state.origin <- E.s;
4309 state.ranchors <- ranchor :: state.ranchors;
4310 opendoc path E.s;
4312 if substratis spec 0 "page="
4313 then
4314 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4315 | pageno ->
4316 state.anchor <- (pageno, 0.0, 0.0);
4317 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4318 | exception exn ->
4319 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4320 else (
4321 state.nameddest <- dest;
4322 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4326 let gotounder = function
4327 | Ulinkuri s when isexternallink s ->
4328 if substratis s 0 "file://"
4329 then gotoremote @@ String.sub s 7 (String.length s - 7)
4330 else gotouri s
4331 | Ulinkuri s ->
4332 let pageno, x, y = uritolocation s in
4333 addnav ();
4334 gotopagexy !wtmode pageno x y
4335 | Utext _ | Unone -> ()
4336 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4339 let gotooutline (_, _, kind) =
4340 match kind with
4341 | Onone -> ()
4342 | Oanchor anchor ->
4343 let (pageno, y, _) = anchor in
4344 let y = getanchory
4345 (if conf.presentation then (pageno, y, 1.0) else anchor)
4347 addnav ();
4348 gotoghyll y
4349 | Ouri uri -> gotounder (Ulinkuri uri)
4350 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4351 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4352 | Ohistory hist -> gotohist hist
4353 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4356 class outlinesoucebase fetchoutlines = object (self)
4357 inherit lvsourcebase
4358 val mutable m_items = E.a
4359 val mutable m_minfo = E.a
4360 val mutable m_orig_items = E.a
4361 val mutable m_orig_minfo = E.a
4362 val mutable m_narrow_patterns = []
4363 val mutable m_gen = -1
4365 method getitemcount = Array.length m_items
4367 method getitem n =
4368 let s, n, _ = m_items.(n) in
4369 (s, n+0)
4371 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4372 ignore (uioh, first);
4373 let items, minfo =
4374 if m_narrow_patterns = []
4375 then m_orig_items, m_orig_minfo
4376 else m_items, m_minfo
4378 m_pan <- pan;
4379 if not cancel
4380 then (
4381 m_items <- items;
4382 m_minfo <- minfo;
4383 gotooutline m_items.(active);
4385 else (
4386 m_items <- items;
4387 m_minfo <- minfo;
4389 None
4391 method hasaction (_:int) = true
4393 method greetmsg =
4394 if Array.length m_items != Array.length m_orig_items
4395 then
4396 let s =
4397 match m_narrow_patterns with
4398 | one :: [] -> one
4399 | many -> String.concat UniSyms.ellipsis (List.rev many)
4401 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4402 else E.s
4404 method statestr =
4405 match m_narrow_patterns with
4406 | [] -> E.s
4407 | one :: [] -> one
4408 | head :: _ -> UniSyms.ellipsis ^ head
4410 method narrow pattern =
4411 match Str.regexp_case_fold pattern with
4412 | exception _ -> ()
4413 | re ->
4414 let rec loop accu minfo n =
4415 if n = -1
4416 then (
4417 m_items <- Array.of_list accu;
4418 m_minfo <- Array.of_list minfo;
4420 else
4421 let (s, _, _) as o = m_items.(n) in
4422 let accu, minfo =
4423 match Str.search_forward re s 0 with
4424 | exception Not_found -> accu, minfo
4425 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4427 loop accu minfo (n-1)
4429 loop [] [] (Array.length m_items - 1)
4431 method! getminfo = m_minfo
4433 method denarrow =
4434 m_orig_items <- fetchoutlines ();
4435 m_minfo <- m_orig_minfo;
4436 m_items <- m_orig_items
4438 method add_narrow_pattern pattern =
4439 m_narrow_patterns <- pattern :: m_narrow_patterns
4441 method del_narrow_pattern =
4442 match m_narrow_patterns with
4443 | _ :: rest -> m_narrow_patterns <- rest
4444 | [] -> ()
4446 method renarrow =
4447 self#denarrow;
4448 match m_narrow_patterns with
4449 | pattern :: [] -> self#narrow pattern; pattern
4450 | list ->
4451 List.fold_left (fun accu pattern ->
4452 self#narrow pattern;
4453 pattern ^ UniSyms.ellipsis ^ accu) E.s list
4455 method calcactive (_:anchor) = 0
4457 method reset anchor items =
4458 if state.gen != m_gen
4459 then (
4460 m_orig_items <- items;
4461 m_items <- items;
4462 m_narrow_patterns <- [];
4463 m_minfo <- E.a;
4464 m_orig_minfo <- E.a;
4465 m_gen <- state.gen;
4467 else (
4468 if items != m_orig_items
4469 then (
4470 m_orig_items <- items;
4471 if m_narrow_patterns == []
4472 then m_items <- items;
4475 let active = self#calcactive anchor in
4476 m_active <- active;
4477 m_first <- firstof m_first active
4481 let outlinesource fetchoutlines =
4482 (object
4483 inherit outlinesoucebase fetchoutlines
4484 method! calcactive anchor =
4485 let rely = getanchory anchor in
4486 let rec loop n best bestd =
4487 if n = Array.length m_items
4488 then best
4489 else
4490 let _, _, kind = m_items.(n) in
4491 match kind with
4492 | Oanchor anchor ->
4493 let orely = getanchory anchor in
4494 let d = abs (orely - rely) in
4495 if d < bestd
4496 then loop (n+1) n d
4497 else loop (n+1) best bestd
4498 | Onone | Oremote _ | Olaunch _
4499 | Oremotedest _ | Ouri _ | Ohistory _ ->
4500 loop (n+1) best bestd
4502 loop 0 ~-1 max_int
4503 end)
4506 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4507 let mkselector sourcetype =
4508 let fetchoutlines () =
4509 match sourcetype with
4510 | `bookmarks -> Array.of_list state.bookmarks
4511 | `outlines -> state.outlines
4512 | `history -> genhistoutlines ()
4514 let source =
4515 if sourcetype = `history
4516 then new outlinesoucebase fetchoutlines
4517 else outlinesource fetchoutlines
4519 fun errmsg ->
4520 let outlines = fetchoutlines () in
4521 if Array.length outlines = 0
4522 then (
4523 showtext ' ' errmsg;
4525 else (
4526 resetmstate ();
4527 Wsi.setcursor Wsi.CURSOR_INHERIT;
4528 let anchor = getanchor () in
4529 source#reset anchor outlines;
4530 state.text <- source#greetmsg;
4531 state.uioh <-
4532 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4533 G.postRedisplay "enter selector";
4536 let mkenter sourcetype errmsg =
4537 let enter = mkselector sourcetype in
4538 fun () -> enter errmsg
4540 mkenter `outlines "document has no outline"
4541 , mkenter `bookmarks "document has no bookmarks (yet)"
4542 , mkenter `history "history is empty"
4545 let quickbookmark ?title () =
4546 match state.layout with
4547 | [] -> ()
4548 | l :: _ ->
4549 let title =
4550 match title with
4551 | None ->
4552 let tm = Unix.localtime (now ()) in
4553 Printf.sprintf
4554 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4555 (l.pageno+1)
4556 tm.Unix.tm_mday
4557 (tm.Unix.tm_mon+1)
4558 (tm.Unix.tm_year + 1900)
4559 tm.Unix.tm_hour
4560 tm.Unix.tm_min
4561 | Some title -> title
4563 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4566 let setautoscrollspeed step goingdown =
4567 let incr = max 1 ((abs step) / 2) in
4568 let incr = if goingdown then incr else -incr in
4569 let astep = boundastep state.winh (step + incr) in
4570 state.autoscroll <- Some astep;
4573 let canpan () =
4574 match conf.columns with
4575 | Csplit _ -> true
4576 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4579 let panbound x = bound x (-state.w) state.winw;;
4581 let existsinrow pageno (columns, coverA, coverB) p =
4582 let last = ((pageno - coverA) mod columns) + columns in
4583 let rec any = function
4584 | [] -> false
4585 | l :: rest ->
4586 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4587 then p l
4588 else (
4589 if not (p l)
4590 then (if l.pageno = last then false else any rest)
4591 else true
4594 any state.layout
4597 let nextpage () =
4598 match state.layout with
4599 | [] ->
4600 let pageno = page_of_y state.y in
4601 gotoghyll (getpagey (pageno+1))
4602 | l :: rest ->
4603 match conf.columns with
4604 | Csingle _ ->
4605 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4606 then
4607 let y = clamp (pgscale state.winh) in
4608 gotoghyll y
4609 else
4610 let pageno = min (l.pageno+1) (state.pagecount-1) in
4611 gotoghyll (getpagey pageno)
4612 | Cmulti ((c, _, _) as cl, _) ->
4613 if conf.presentation
4614 && (existsinrow l.pageno cl
4615 (fun l -> l.pageh > l.pagey + l.pagevh))
4616 then
4617 let y = clamp (pgscale state.winh) in
4618 gotoghyll y
4619 else
4620 let pageno = min (l.pageno+c) (state.pagecount-1) in
4621 gotoghyll (getpagey pageno)
4622 | Csplit (n, _) ->
4623 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4624 then
4625 let pagey, pageh = getpageyh l.pageno in
4626 let pagey = pagey + pageh * l.pagecol in
4627 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4628 gotoghyll (pagey + pageh + ips)
4631 let prevpage () =
4632 match state.layout with
4633 | [] ->
4634 let pageno = page_of_y state.y in
4635 gotoghyll (getpagey (pageno-1))
4636 | l :: _ ->
4637 match conf.columns with
4638 | Csingle _ ->
4639 if conf.presentation && l.pagey != 0
4640 then
4641 gotoghyll (clamp (pgscale ~-(state.winh)))
4642 else
4643 let pageno = max 0 (l.pageno-1) in
4644 gotoghyll (getpagey pageno)
4645 | Cmulti ((c, _, coverB) as cl, _) ->
4646 if conf.presentation &&
4647 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4648 then
4649 gotoghyll (clamp (pgscale ~-(state.winh)))
4650 else
4651 let decr =
4652 if l.pageno = state.pagecount - coverB
4653 then 1
4654 else c
4656 let pageno = max 0 (l.pageno-decr) in
4657 gotoghyll (getpagey pageno)
4658 | Csplit (n, _) ->
4659 let y =
4660 if l.pagecol = 0
4661 then
4662 if l.pageno = 0
4663 then l.pagey
4664 else
4665 let pageno = max 0 (l.pageno-1) in
4666 let pagey, pageh = getpageyh pageno in
4667 pagey + (n-1)*pageh
4668 else
4669 let pagey, pageh = getpageyh l.pageno in
4670 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4672 gotoghyll y
4675 let save () =
4676 if emptystr conf.savecmd
4677 then error "don't know where to save modified document"
4678 else
4679 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4680 let path =
4681 getcmdoutput
4682 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4683 savecmd
4685 if nonemptystr path
4686 then
4687 let tmp = path ^ ".tmp" in
4688 savedoc tmp;
4689 Unix.rename tmp path;
4692 let viewkeyboard key mask =
4693 let enttext te =
4694 let mode = state.mode in
4695 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4696 state.text <- E.s;
4697 enttext ();
4698 G.postRedisplay "view:enttext"
4700 let ctrl = Wsi.withctrl mask in
4701 let key = Wsi.keypadtodigitkey key in
4702 let open Keys in
4703 match Wsi.kc2kt key with
4704 | Ascii 'Q' -> exit 0
4706 | Ascii 'W' ->
4707 if hasunsavedchanges ()
4708 then save ()
4710 | Insert ->
4711 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4712 then (
4713 state.mode <- (
4714 match state.lnava with
4715 | None -> LinkNav (Ltgendir 0)
4716 | Some pn -> LinkNav (Ltexact pn)
4718 gotoxy state.x state.y;
4720 else impmsg "keyboard link navigation does not work under rotation"
4722 | Escape | Ascii 'q' ->
4723 begin match state.mstate with
4724 | Mzoomrect _ ->
4725 resetmstate ();
4726 G.postRedisplay "kill rect";
4727 | Msel _
4728 | Mpan _
4729 | Mscrolly | Mscrollx
4730 | Mzoom _
4731 | Mnone ->
4732 begin match state.mode with
4733 | LinkNav ln ->
4734 begin match ln with
4735 | Ltexact pl -> state.lnava <- Some pl
4736 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4737 end;
4738 state.mode <- View;
4739 G.postRedisplay "esc leave linknav"
4740 | Birdseye _ | Textentry _ | View ->
4741 match state.ranchors with
4742 | [] -> raise Quit
4743 | (path, password, anchor, origin) :: rest ->
4744 state.ranchors <- rest;
4745 state.anchor <- anchor;
4746 state.origin <- origin;
4747 state.nameddest <- E.s;
4748 opendoc path password
4749 end;
4750 end;
4752 | Backspace ->
4753 gotoghyll (getnav ~-1)
4755 | Ascii 'o' ->
4756 enteroutlinemode ()
4758 | Ascii 'H' ->
4759 enterhistmode ()
4761 | Ascii 'u' ->
4762 state.rects <- [];
4763 state.text <- E.s;
4764 Hashtbl.iter (fun _ opaque ->
4765 clearmark opaque;
4766 Hashtbl.clear state.prects) state.pagemap;
4767 G.postRedisplay "dehighlight";
4769 | (Ascii '/' | Ascii '?') as pv ->
4770 let ondone isforw s =
4771 cbput state.hists.pat s;
4772 state.searchpattern <- s;
4773 search s isforw
4775 let s = String.make 1 (Char.chr key) in
4776 enttext (s, E.s, Some (onhist state.hists.pat),
4777 textentry, ondone (pv = Ascii '/'), true)
4779 | Ascii '+' | KPplus | Ascii '=' when ctrl ->
4780 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4781 pivotzoom (conf.zoom +. incr)
4783 | Ascii '+' | KPplus ->
4784 let ondone s =
4785 let n =
4786 try int_of_string s with exn ->
4787 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4788 max_int
4790 if n != max_int
4791 then (
4792 conf.pagebias <- n;
4793 state.text <- "page bias is now " ^ string_of_int n;
4796 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4798 | Ascii '-' | KPminus when ctrl ->
4799 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4800 pivotzoom (max 0.01 (conf.zoom -. decr))
4802 | Ascii '-' | KPminus ->
4803 let ondone msg = state.text <- msg in
4804 enttext (
4805 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4806 optentry state.mode, ondone, true
4809 | Ascii '0' when ctrl ->
4810 if conf.zoom = 1.0
4811 then gotoxy 0 state.y
4812 else setzoom 1.0
4814 | Ascii ('1'|'2' as c)
4815 when ctrl && conf.fitmodel != FitPage ->
4816 let cols =
4817 match conf.columns with
4818 | Csingle _ | Cmulti _ -> 1
4819 | Csplit (n, _) -> n
4821 let h = state.winh -
4822 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4824 let zoom = zoomforh state.winw h 0 cols in
4825 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4826 then setzoom zoom
4828 | Ascii '3' when ctrl ->
4829 let fm =
4830 match conf.fitmodel with
4831 | FitWidth -> FitProportional
4832 | FitProportional -> FitPage
4833 | FitPage -> FitWidth
4835 state.text <- "fit model: " ^ FMTE.to_string fm;
4836 reqlayout conf.angle fm
4838 | Ascii '4' when ctrl ->
4839 let zoom = getmaxw () /. float state.winw in
4840 if zoom > 0.0 then setzoom zoom
4842 | Fn 9 ->
4843 togglebirdseye ()
4845 | Ascii '9' when ctrl ->
4846 togglebirdseye ()
4848 | Ascii ('0'..'9')
4849 when not ctrl ->
4850 let ondone s =
4851 let n =
4852 try int_of_string s with exn ->
4853 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4856 if n >= 0
4857 then (
4858 addnav ();
4859 cbput state.hists.pag (string_of_int n);
4860 gotopage1 (n + conf.pagebias - 1) 0;
4863 let pageentry text key =
4864 match Char.unsafe_chr key with
4865 | 'g' -> TEdone text
4866 | _ -> intentry text key
4868 let text = String.make 1 (Char.chr key) in
4869 enttext (":", text, Some (onhist state.hists.pag),
4870 pageentry, ondone, true)
4872 | Ascii 'b' ->
4873 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4874 G.postRedisplay "toggle scrollbar";
4876 | Ascii 'B' ->
4877 state.bzoom <- not state.bzoom;
4878 state.rects <- [];
4879 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4881 | Ascii 'l' ->
4882 conf.hlinks <- not conf.hlinks;
4883 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4884 G.postRedisplay "toggle highlightlinks";
4886 | Ascii 'F' ->
4887 if conf.angle mod 360 = 0
4888 then (
4889 state.glinks <- true;
4890 let mode = state.mode in
4891 state.mode <-
4892 Textentry (
4893 (":", E.s, None, linknentry, linknact gotounder, false),
4894 (fun _ ->
4895 state.glinks <- false;
4896 state.mode <- mode)
4898 state.text <- E.s;
4899 G.postRedisplay "view:linkent(F)"
4901 else impmsg "hint mode does not work under rotation"
4903 | Ascii 'y' ->
4904 state.glinks <- true;
4905 let mode = state.mode in
4906 state.mode <-
4907 Textentry (
4909 ":", E.s, None, linknentry, linknact (fun under ->
4910 selstring (undertext under);
4911 ), false
4913 fun _ ->
4914 state.glinks <- false;
4915 state.mode <- mode
4917 state.text <- E.s;
4918 G.postRedisplay "view:linkent"
4920 | Ascii 'a' ->
4921 begin match state.autoscroll with
4922 | Some step ->
4923 conf.autoscrollstep <- step;
4924 state.autoscroll <- None
4925 | None ->
4926 if conf.autoscrollstep = 0
4927 then state.autoscroll <- Some 1
4928 else state.autoscroll <- Some conf.autoscrollstep
4931 | Ascii 'p' when ctrl ->
4932 launchpath () (* XXX where do error messages go? *)
4934 | Ascii 'P' ->
4935 setpresentationmode (not conf.presentation);
4936 showtext ' ' ("presentation mode " ^
4937 if conf.presentation then "on" else "off");
4939 | Ascii 'f' ->
4940 if List.mem Wsi.Fullscreen state.winstate
4941 then Wsi.reshape conf.cwinw conf.cwinh
4942 else Wsi.fullscreen ()
4944 | Ascii ('p'|'N') ->
4945 search state.searchpattern false
4947 | Ascii 'n' | Fn 3 ->
4948 search state.searchpattern true
4950 | Ascii 't' ->
4951 begin match state.layout with
4952 | [] -> ()
4953 | l :: _ ->
4954 gotoghyll (getpagey l.pageno)
4957 | Ascii ' ' ->
4958 nextpage ()
4960 | Delete | KPdelete ->
4961 prevpage ()
4963 | Ascii '=' ->
4964 showtext ' ' (describe_location ());
4966 | Ascii 'w' ->
4967 begin match state.layout with
4968 | [] -> ()
4969 | l :: _ ->
4970 Wsi.reshape l.pagew l.pageh;
4971 G.postRedisplay "w"
4974 | Ascii '\'' ->
4975 enterbookmarkmode ()
4977 | Ascii 'h' | Fn 1 ->
4978 enterhelpmode ()
4980 | Ascii 'i' ->
4981 enterinfomode ()
4983 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
4984 entermsgsmode ()
4986 | Ascii 'm' ->
4987 let ondone s =
4988 match state.layout with
4989 | l :: _ ->
4990 if nonemptystr s
4991 then
4992 state.bookmarks <-
4993 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4994 | _ -> ()
4996 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4998 | Ascii '~' ->
4999 quickbookmark ();
5000 showtext ' ' "Quick bookmark added";
5002 | Ascii 'z' ->
5003 begin match state.layout with
5004 | l :: _ ->
5005 let rect = getpdimrect l.pagedimno in
5006 let w, h =
5007 if conf.crophack
5008 then
5009 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5010 truncate (1.2 *. (rect.(3) -. rect.(0))))
5011 else
5012 (truncate (rect.(1) -. rect.(0)),
5013 truncate (rect.(3) -. rect.(0)))
5015 let w = truncate ((float w)*.conf.zoom)
5016 and h = truncate ((float h)*.conf.zoom) in
5017 if w != 0 && h != 0
5018 then (
5019 state.anchor <- getanchor ();
5020 Wsi.reshape w (h + conf.interpagespace)
5022 G.postRedisplay "z";
5024 | [] -> ()
5027 | Ascii 'x' -> state.roam ()
5029 | Ascii ('<'|'>' as c) ->
5030 reqlayout
5031 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
5033 | Ascii ('['|']' as c) ->
5034 conf.colorscale <-
5035 bound (conf.colorscale +. (if c = '>' then 0.1 else -0.1)) 0.0 1.0;
5036 G.postRedisplay "brightness";
5038 | Ascii 'c' when state.mode = View ->
5039 if Wsi.withalt mask
5040 then (
5041 if conf.zoom > 1.0
5042 then
5043 let m = (state.winw - state.w) / 2 in
5044 gotoxy_and_clear_text m state.y
5046 else
5047 let (c, a, b), z =
5048 match state.prevcolumns with
5049 | None -> (1, 0, 0), 1.0
5050 | Some (columns, z) ->
5051 let cab =
5052 match columns with
5053 | Csplit (c, _) -> -c, 0, 0
5054 | Cmulti ((c, a, b), _) -> c, a, b
5055 | Csingle _ -> 1, 0, 0
5057 cab, z
5059 setcolumns View c a b;
5060 setzoom z
5062 | Down | Up when ctrl && Wsi.withshift mask ->
5063 let zoom, x = state.prevzoom in
5064 setzoom zoom;
5065 state.x <- x;
5067 | Ascii 'k' | Up | KPup ->
5068 begin match state.autoscroll with
5069 | None ->
5070 begin match state.mode with
5071 | Birdseye beye -> upbirdseye 1 beye
5072 | Textentry _ | View | LinkNav _ ->
5073 if ctrl
5074 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5075 else (
5076 if not (Wsi.withshift mask) && conf.presentation
5077 then prevpage ()
5078 else gotoghyll1 true (clamp (-conf.scrollstep))
5081 | Some n ->
5082 setautoscrollspeed n false
5085 | Ascii 'j' | Down | KPdown ->
5086 begin match state.autoscroll with
5087 | None ->
5088 begin match state.mode with
5089 | Birdseye beye -> downbirdseye 1 beye
5090 | Textentry _ | View | LinkNav _ ->
5091 if ctrl
5092 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5093 else (
5094 if not (Wsi.withshift mask) && conf.presentation
5095 then nextpage ()
5096 else gotoghyll1 true (clamp (conf.scrollstep))
5099 | Some n ->
5100 setautoscrollspeed n true
5103 | Left | Right | KPleft | KPright when not (Wsi.withalt mask) ->
5104 if canpan ()
5105 then
5106 let dx =
5107 if ctrl
5108 then state.winw / 2
5109 else conf.hscrollstep
5111 let dx =
5112 let pv = Wsi.kc2kt key in
5113 if pv = Keys.Left || pv = Keys.KPleft then dx else -dx
5115 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5116 else (
5117 state.text <- E.s;
5118 G.postRedisplay "left/right"
5121 | Prior | KPprior ->
5122 let y =
5123 if ctrl
5124 then
5125 match state.layout with
5126 | [] -> state.y
5127 | l :: _ -> state.y - l.pagey
5128 else
5129 clamp (pgscale (-state.winh))
5131 gotoghyll y
5133 | Next | KPnext ->
5134 let y =
5135 if ctrl
5136 then
5137 match List.rev state.layout with
5138 | [] -> state.y
5139 | l :: _ -> getpagey l.pageno
5140 else
5141 clamp (pgscale state.winh)
5143 gotoghyll y
5145 | Ascii 'g' | Home | KPhome ->
5146 addnav ();
5147 gotoghyll 0
5148 | Ascii 'G' | End | KPend ->
5149 addnav ();
5150 gotoghyll (clamp state.maxy)
5152 | Right | KPright when Wsi.withalt mask ->
5153 gotoghyll (getnav 1)
5154 | Left | KPleft when Wsi.withalt mask ->
5155 gotoghyll (getnav ~-1)
5157 | Ascii 'r' ->
5158 reload ()
5160 | Ascii 'v' when conf.debug ->
5161 state.rects <- [];
5162 List.iter (fun l ->
5163 match getopaque l.pageno with
5164 | None -> ()
5165 | Some opaque ->
5166 let x0, y0, x1, y1 = pagebbox opaque in
5167 let rect = (float x0, float y0,
5168 float x1, float y0,
5169 float x1, float y1,
5170 float x0, float y1) in
5171 debugrect rect;
5172 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5173 state.rects <- (l.pageno, color, rect) :: state.rects;
5174 ) state.layout;
5175 G.postRedisplay "v";
5177 | Ascii '|' ->
5178 let mode = state.mode in
5179 let cmd = ref E.s in
5180 let onleave = function
5181 | Cancel -> state.mode <- mode
5182 | Confirm ->
5183 List.iter (fun l ->
5184 match getopaque l.pageno with
5185 | Some opaque -> pipesel opaque !cmd
5186 | None -> ()) state.layout;
5187 state.mode <- mode
5189 let ondone s =
5190 cbput state.hists.sel s;
5191 cmd := s
5193 let te =
5194 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5196 G.postRedisplay "|";
5197 state.mode <- Textentry (te, onleave);
5199 | (Ascii _|Fn _|Enter|KPenter|KPleft|KPright|Left|Right|Code _) ->
5200 vlog "huh? %s" (Wsi.keyname key)
5203 let linknavkeyboard key mask linknav =
5204 let pv = Wsi.kc2kt key in
5205 let getpage pageno =
5206 let rec loop = function
5207 | [] -> None
5208 | l :: _ when l.pageno = pageno -> Some l
5209 | _ :: rest -> loop rest
5210 in loop state.layout
5212 let doexact (pageno, n) =
5213 match getopaque pageno, getpage pageno with
5214 | Some opaque, Some l ->
5215 if pv = Keys.Enter || pv = Keys.KPenter
5216 then
5217 let under = getlink opaque n in
5218 G.postRedisplay "link gotounder";
5219 gotounder under;
5220 state.mode <- View;
5221 else
5222 let opt, dir =
5223 let open Keys in
5224 match pv with
5225 | Home ->
5226 Some (findlink opaque LDfirst), -1
5228 | End ->
5229 Some (findlink opaque LDlast), 1
5231 | Left ->
5232 Some (findlink opaque (LDleft n)), -1
5234 | Right ->
5235 Some (findlink opaque (LDright n)), 1
5237 | Up ->
5238 Some (findlink opaque (LDup n)), -1
5240 | Down ->
5241 Some (findlink opaque (LDdown n)), 1
5243 | Delete|Escape|Insert|Enter|KPdelete|KPdown|KPend|KPenter
5244 | KPhome|KPleft|KPminus|KPnext|KPplus|KPprior|KPright|KPup
5245 | Next|Prior|Ascii _|Code _|Fn _|Backspace -> None, 0
5247 let pwl l dir =
5248 begin match findpwl l.pageno dir with
5249 | Pwlnotfound -> ()
5250 | Pwl pageno ->
5251 let notfound dir =
5252 state.mode <- LinkNav (Ltgendir dir);
5253 let y, h = getpageyh pageno in
5254 let y =
5255 if dir < 0
5256 then y + h - state.winh
5257 else y
5259 gotoxy state.x y
5261 begin match getopaque pageno, getpage pageno with
5262 | Some opaque, Some _ ->
5263 let link =
5264 let ld = if dir > 0 then LDfirst else LDlast in
5265 findlink opaque ld
5267 begin match link with
5268 | Lfound m ->
5269 showlinktype (getlink opaque m);
5270 state.mode <- LinkNav (Ltexact (pageno, m));
5271 G.postRedisplay "linknav jpage";
5272 | Lnotfound -> notfound dir
5273 end;
5274 | _ -> notfound dir
5275 end;
5276 end;
5278 begin match opt with
5279 | Some Lnotfound -> pwl l dir;
5280 | Some (Lfound m) ->
5281 if m = n
5282 then pwl l dir
5283 else (
5284 let _, y0, _, y1 = getlinkrect opaque m in
5285 if y0 < l.pagey
5286 then gotopage1 l.pageno y0
5287 else (
5288 let d = fstate.fontsize + 1 in
5289 if y1 - l.pagey > l.pagevh - d
5290 then gotopage1 l.pageno (y1 - state.winh + d)
5291 else G.postRedisplay "linknav";
5293 showlinktype (getlink opaque m);
5294 state.mode <- LinkNav (Ltexact (l.pageno, m));
5297 | None -> viewkeyboard key mask
5298 end;
5299 | _ -> viewkeyboard key mask
5301 if pv = Keys.Insert
5302 then (
5303 begin match linknav with
5304 | Ltexact pa -> state.lnava <- Some pa
5305 | Ltgendir _ | Ltnotready _ -> ()
5306 end;
5307 state.mode <- View;
5308 G.postRedisplay "leave linknav"
5310 else
5311 match linknav with
5312 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5313 | Ltexact exact -> doexact exact
5316 let keyboard key mask =
5317 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5318 then wcmd "interrupt"
5319 else state.uioh <- state.uioh#key key mask
5322 let birdseyekeyboard key mask
5323 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5324 let incr =
5325 match conf.columns with
5326 | Csingle _ -> 1
5327 | Cmulti ((c, _, _), _) -> c
5328 | Csplit _ -> failwith "bird's eye split mode"
5330 let pgh layout = List.fold_left
5331 (fun m l -> max l.pageh m) state.winh layout in
5332 let open Keys in
5333 match Wsi.kc2kt key with
5334 | Ascii 'l' when Wsi.withctrl mask ->
5335 let y, h = getpageyh pageno in
5336 let top = (state.winh - h) / 2 in
5337 gotoxy state.x (max 0 (y - top))
5338 | Enter | KPenter -> leavebirdseye beye false
5339 | Escape -> leavebirdseye beye true
5340 | Up -> upbirdseye incr beye
5341 | Down -> downbirdseye incr beye
5342 | Left -> upbirdseye 1 beye
5343 | Right -> downbirdseye 1 beye
5345 | Prior ->
5346 begin match state.layout with
5347 | l :: _ ->
5348 if l.pagey != 0
5349 then (
5350 state.mode <- Birdseye (
5351 oconf, leftx, l.pageno, hooverpageno, anchor
5353 gotopage1 l.pageno 0;
5355 else (
5356 let layout = layout state.x (state.y-state.winh)
5357 state.winw
5358 (pgh state.layout) in
5359 match layout with
5360 | [] -> gotoxy state.x (clamp (-state.winh))
5361 | l :: _ ->
5362 state.mode <- Birdseye (
5363 oconf, leftx, l.pageno, hooverpageno, anchor
5365 gotopage1 l.pageno 0
5368 | [] -> gotoxy state.x (clamp (-state.winh))
5369 end;
5371 | Next ->
5372 begin match List.rev state.layout with
5373 | l :: _ ->
5374 let layout = layout state.x
5375 (state.y + (pgh state.layout))
5376 state.winw state.winh in
5377 begin match layout with
5378 | [] ->
5379 let incr = l.pageh - l.pagevh in
5380 if incr = 0
5381 then (
5382 state.mode <-
5383 Birdseye (
5384 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5386 G.postRedisplay "birdseye pagedown";
5388 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5390 | l :: _ ->
5391 state.mode <-
5392 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5393 gotopage1 l.pageno 0;
5396 | [] -> gotoxy state.x (clamp state.winh)
5397 end;
5399 | Home ->
5400 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5401 gotopage1 0 0
5403 | End ->
5404 let pageno = state.pagecount - 1 in
5405 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5406 if not (pagevisible state.layout pageno)
5407 then
5408 let h =
5409 match List.rev state.pdims with
5410 | [] -> state.winh
5411 | (_, _, h, _) :: _ -> h
5413 gotoxy
5414 state.x
5415 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5416 else G.postRedisplay "birdseye end";
5418 | Delete|Insert|KPdelete|KPdown|KPend|KPhome|KPleft
5419 | KPminus|KPnext|KPplus|KPprior|KPright|KPup|Ascii _
5420 | Code _|Fn _|Backspace -> viewkeyboard key mask
5423 let drawpage l =
5424 let color =
5425 match state.mode with
5426 | Textentry _ -> scalecolor 0.4
5427 | LinkNav _ | View -> scalecolor 1.0
5428 | Birdseye (_, _, pageno, hooverpageno, _) ->
5429 if l.pageno = hooverpageno
5430 then scalecolor 0.9
5431 else (
5432 if l.pageno = pageno
5433 then (
5434 let c = scalecolor 1.0 in
5435 GlDraw.color c;
5436 GlDraw.line_width 3.0;
5437 let dispx = l.pagedispx in
5438 linerect
5439 (float (dispx-1)) (float (l.pagedispy-1))
5440 (float (dispx+l.pagevw+1))
5441 (float (l.pagedispy+l.pagevh+1))
5443 GlDraw.line_width 1.0;
5446 else scalecolor 0.8
5449 drawtiles l color;
5452 let postdrawpage l linkindexbase =
5453 match getopaque l.pageno with
5454 | Some opaque ->
5455 if tileready l l.pagex l.pagey
5456 then
5457 let x = l.pagedispx - l.pagex
5458 and y = l.pagedispy - l.pagey in
5459 let hlmask =
5460 match conf.columns with
5461 | Csingle _ | Cmulti _ ->
5462 (if conf.hlinks then 1 else 0)
5463 + (if state.glinks
5464 && not (isbirdseye state.mode) then 2 else 0)
5465 | Csplit _ -> 0
5467 let s =
5468 match state.mode with
5469 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5470 | Textentry _
5471 | Birdseye _
5472 | View
5473 | LinkNav _ -> E.s
5475 Hashtbl.find_all state.prects l.pageno |>
5476 List.iter (fun vals -> drawprect opaque x y vals);
5477 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5478 if n < 0
5479 then (state.redisplay <- true; 0)
5480 else n
5481 else 0
5482 | _ -> 0
5485 let scrollindicator () =
5486 let sbw, ph, sh = state.uioh#scrollph in
5487 let sbh, pw, sw = state.uioh#scrollpw in
5489 let x0,x1,hx0 =
5490 if conf.leftscroll
5491 then (0, sbw, sbw)
5492 else ((state.winw - sbw), state.winw, 0)
5495 Gl.enable `blend;
5496 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5497 GlDraw.color (0.64, 0.64, 0.64) ~alpha:0.7;
5498 filledrect (float x0) 0. (float x1) (float state.winh);
5499 filledrect
5500 (float hx0) (float (state.winh - sbh))
5501 (float (hx0 + state.winw)) (float state.winh)
5503 GlDraw.color (0.0, 0.0, 0.0) ~alpha:0.7;
5505 filledrect (float x0) ph (float x1) (ph +. sh);
5506 let pw = pw +. float hx0 in
5507 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5508 Gl.disable `blend;
5511 let showsel () =
5512 match state.mstate with
5513 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5516 | Msel ((x0, y0), (x1, y1)) ->
5517 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5518 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5519 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5520 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5523 let showrects =
5524 function [] -> ()
5525 | rects ->
5526 Gl.enable `blend;
5527 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5528 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5529 List.iter
5530 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5531 List.iter (fun l ->
5532 if l.pageno = pageno
5533 then (
5534 let dx = float (l.pagedispx - l.pagex) in
5535 let dy = float (l.pagedispy - l.pagey) in
5536 let r, g, b, alpha = c in
5537 GlDraw.color (r, g, b) ~alpha;
5538 filledrect2 (x0+.dx) (y0+.dy)
5539 (x1+.dx) (y1+.dy)
5540 (x3+.dx) (y3+.dy)
5541 (x2+.dx) (y2+.dy);
5543 ) state.layout
5544 ) rects
5546 Gl.disable `blend;
5549 let display () =
5550 begin match conf.columns, state.layout with
5551 | Csingle _, _ :: _ ->
5552 GlDraw.color (scalecolor2 conf.bgcolor);
5553 let y =
5554 List.fold_left (fun y l ->
5555 let x0 = 0 in
5556 let y0 = y in
5557 let x1 = l.pagedispx in
5558 let y1 = (l.pagedispy + l.pagevh) in
5559 filledrect (float x0) (float y0) (float x1) (float y1);
5560 let x0 = x1 + l.pagevw in
5561 let x1 = state.winw in
5562 filledrect1 (float x0) (float y0) (float x1) (float y1);
5563 if y != l.pagedispy
5564 then (
5565 let x0 = 0
5566 and x1 = state.winw in
5567 let y0 = y
5568 and y1 = l.pagedispy in
5569 filledrect1 (float x0) (float y0) (float x1) (float y1);
5571 l.pagedispy + l.pagevh) 0 state.layout
5573 let x0 = 0
5574 and x1 = state.winw in
5575 let y0 = y
5576 and y1 = state.winh in
5577 filledrect1 (float x0) (float y0) (float x1) (float y1)
5578 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5579 GlClear.color (scalecolor2 conf.bgcolor);
5580 GlClear.clear [`color];
5581 end;
5582 List.iter drawpage state.layout;
5583 let rects =
5584 match state.mode with
5585 | LinkNav (Ltexact (pageno, linkno)) ->
5586 begin match getopaque pageno with
5587 | Some opaque ->
5588 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5589 let color = (0.0, 0.0, 0.5, 0.5) in
5590 (pageno, color, (
5591 float x0, float y0,
5592 float x1, float y0,
5593 float x1, float y1,
5594 float x0, float y1)
5595 ) :: state.rects
5596 | None -> state.rects
5598 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5599 | Birdseye _
5600 | Textentry _
5601 | View -> state.rects
5603 showrects rects;
5604 let rec postloop linkindexbase = function
5605 | l :: rest ->
5606 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5607 postloop linkindexbase rest
5608 | [] -> ()
5610 showsel ();
5611 postloop 0 state.layout;
5612 state.uioh#display;
5613 begin match state.mstate with
5614 | Mzoomrect ((x0, y0), (x1, y1)) ->
5615 Gl.enable `blend;
5616 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5617 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5618 filledrect (float x0) (float y0) (float x1) (float y1);
5619 Gl.disable `blend;
5620 | Msel _
5621 | Mpan _
5622 | Mscrolly | Mscrollx
5623 | Mzoom _
5624 | Mnone -> ()
5625 end;
5626 enttext ();
5627 scrollindicator ();
5628 Wsi.swapb ();
5631 let zoomrect x y x1 y1 =
5632 let x0 = min x x1
5633 and x1 = max x x1
5634 and y0 = min y y1 in
5635 let zoom = (float state.w) /. float (x1 - x0) in
5636 let margin =
5637 let simple () =
5638 if state.w < state.winw
5639 then (state.winw - state.w) / 2
5640 else 0
5642 match conf.fitmodel with
5643 | FitWidth | FitProportional -> simple ()
5644 | FitPage ->
5645 match conf.columns with
5646 | Csplit _ ->
5647 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5648 | Cmulti _ | Csingle _ -> simple ()
5650 gotoxy ((state.x + margin) - x0) (state.y + y0);
5651 state.anchor <- getanchor ();
5652 setzoom zoom;
5653 resetmstate ();
5656 let annot inline x y =
5657 match unproject x y with
5658 | Some (opaque, n, ux, uy) ->
5659 let add text =
5660 addannot opaque ux uy text;
5661 wcmd "freepage %s" (~> opaque);
5662 Hashtbl.remove state.pagemap (n, state.gen);
5663 flushtiles ();
5664 gotoxy state.x state.y
5666 if inline
5667 then
5668 let ondone s = add s in
5669 let mode = state.mode in
5670 state.mode <- Textentry (
5671 ("annotation: ", E.s, None, textentry, ondone, true),
5672 fun _ -> state.mode <- mode);
5673 state.text <- E.s;
5674 enttext ();
5675 G.postRedisplay "annot"
5676 else
5677 add @@ getusertext E.s
5678 | _ -> ()
5681 let zoomblock x y =
5682 let g opaque l px py =
5683 match rectofblock opaque px py with
5684 | Some a ->
5685 let x0 = a.(0) -. 20. in
5686 let x1 = a.(1) +. 20. in
5687 let y0 = a.(2) -. 20. in
5688 let zoom = (float state.w) /. (x1 -. x0) in
5689 let pagey = getpagey l.pageno in
5690 let margin = (state.w - l.pagew)/2 in
5691 let nx = -truncate x0 - margin in
5692 gotoxy_and_clear_text nx (pagey + truncate y0);
5693 state.anchor <- getanchor ();
5694 setzoom zoom;
5695 None
5696 | None -> None
5698 match conf.columns with
5699 | Csplit _ ->
5700 impmsg "block zooming does not work properly in split columns mode"
5701 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5704 let scrollx x =
5705 let winw = state.winw - 1 in
5706 let s = float x /. float winw in
5707 let destx = truncate (float (state.w + winw) *. s) in
5708 gotoxy_and_clear_text (winw - destx) state.y;
5709 state.mstate <- Mscrollx;
5712 let scrolly y =
5713 let s = float y /. float state.winh in
5714 let desty = truncate (float (state.maxy -
5715 (if conf.maxhfit then state.winh else 0))
5716 *. s) in
5717 gotoxy_and_clear_text state.x desty;
5718 state.mstate <- Mscrolly;
5721 let viewmulticlick clicks x y mask =
5722 let g opaque l px py =
5723 let mark =
5724 match clicks with
5725 | 2 -> Mark_word
5726 | 3 -> Mark_line
5727 | 4 -> Mark_block
5728 | _ -> Mark_page
5730 if markunder opaque px py mark
5731 then (
5732 Some (fun () ->
5733 let dopipe cmd =
5734 match getopaque l.pageno with
5735 | None -> ()
5736 | Some opaque -> pipesel opaque cmd
5738 state.roam <- (fun () -> dopipe conf.paxcmd);
5739 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5742 else None
5744 G.postRedisplay "viewmulticlick";
5745 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5748 let canselect () =
5749 match conf.columns with
5750 | Csplit _ -> false
5751 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5754 let viewmouse button down x y mask =
5755 match button with
5756 | n when (n == 4 || n == 5) && not down ->
5757 if Wsi.withctrl mask
5758 then (
5759 match state.mstate with
5760 | Mzoom (oldn, i, (ftx, fty)) ->
5761 let recenter =
5762 if false
5763 then abs (ftx - x) > 5 || abs (fty - y) > 5
5764 else false
5766 if oldn = n
5767 then (
5768 if i = 2
5769 then
5770 let incr =
5771 match n with
5772 | 5 ->
5773 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5774 | _ ->
5775 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5777 let zoom = conf.zoom -. incr in
5778 if recenter
5779 then pivotzoom ~x ~y zoom
5780 else pivotzoom zoom;
5781 state.mstate <- Mzoom (n, 0, (x, y));
5782 else
5783 state.mstate <- Mzoom (n, i+1, (ftx, fty));
5785 else state.mstate <- Mzoom (n, 0, (ftx, fty))
5787 | Msel _
5788 | Mpan _
5789 | Mscrolly | Mscrollx
5790 | Mzoomrect _
5791 | Mnone -> state.mstate <- Mzoom (n, 0, (0, 0))
5793 else (
5794 match state.autoscroll with
5795 | Some step -> setautoscrollspeed step (n=4)
5796 | None ->
5797 if conf.wheelbypage || conf.presentation
5798 then (
5799 if n = 4
5800 then prevpage ()
5801 else nextpage ()
5803 else
5804 let incr =
5805 if n = 4
5806 then -conf.scrollstep
5807 else conf.scrollstep
5809 let incr = incr * 2 in
5810 let y = clamp incr in
5811 gotoxy_and_clear_text state.x y
5814 | n when (n = 6 || n = 7) && not down && canpan () ->
5815 let x =
5816 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5817 gotoxy_and_clear_text x state.y
5819 | 1 when Wsi.withshift mask ->
5820 state.mstate <- Mnone;
5821 if not down
5822 then (
5823 match unproject x y with
5824 | None -> ()
5825 | Some (_, pageno, ux, uy) ->
5826 let cmd = Printf.sprintf
5827 "%s %s %d %d %d"
5828 conf.stcmd state.path pageno ux uy
5830 match spawn cmd [] with
5831 | exception exn ->
5832 impmsg "execution of synctex command(%S) failed: %S"
5833 conf.stcmd @@ exntos exn
5834 | _pid -> ()
5837 | 1 when Wsi.withctrl mask ->
5838 if down
5839 then (
5840 Wsi.setcursor Wsi.CURSOR_FLEUR;
5841 state.mstate <- Mpan (x, y)
5843 else
5844 state.mstate <- Mnone
5846 | 3 ->
5847 if down
5848 then (
5849 if Wsi.withshift mask
5850 then (
5851 annot conf.annotinline x y;
5852 G.postRedisplay "addannot"
5854 else
5855 let p = (x, y) in
5856 Wsi.setcursor Wsi.CURSOR_CYCLE;
5857 state.mstate <- Mzoomrect (p, p)
5859 else (
5860 match state.mstate with
5861 | Mzoomrect ((x0, y0), _) ->
5862 if abs (x-x0) > 10 && abs (y - y0) > 10
5863 then zoomrect x0 y0 x y
5864 else (
5865 resetmstate ();
5866 G.postRedisplay "kill accidental zoom rect";
5868 | Msel _
5869 | Mpan _
5870 | Mscrolly | Mscrollx
5871 | Mzoom _
5872 | Mnone -> resetmstate ()
5875 | 1 when vscrollhit x ->
5876 if down
5877 then
5878 let _, position, sh = state.uioh#scrollph in
5879 if y > truncate position && y < truncate (position +. sh)
5880 then state.mstate <- Mscrolly
5881 else scrolly y
5882 else
5883 state.mstate <- Mnone
5885 | 1 when y > state.winh - hscrollh () ->
5886 if down
5887 then
5888 let _, position, sw = state.uioh#scrollpw in
5889 if x > truncate position && x < truncate (position +. sw)
5890 then state.mstate <- Mscrollx
5891 else scrollx x
5892 else
5893 state.mstate <- Mnone
5895 | 1 when state.bzoom -> if not down then zoomblock x y
5897 | 1 ->
5898 let dest = if down then getunder x y else Unone in
5899 begin match dest with
5900 | Ulinkuri _ ->
5901 gotounder dest
5903 | Unone when down ->
5904 Wsi.setcursor Wsi.CURSOR_FLEUR;
5905 state.mstate <- Mpan (x, y);
5907 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5909 | Unone | Utext _ ->
5910 if down
5911 then (
5912 if canselect ()
5913 then (
5914 state.mstate <- Msel ((x, y), (x, y));
5915 G.postRedisplay "mouse select";
5918 else (
5919 match state.mstate with
5920 | Mnone -> ()
5922 | Mzoom _ | Mscrollx | Mscrolly ->
5923 state.mstate <- Mnone
5925 | Mzoomrect ((x0, y0), _) ->
5926 zoomrect x0 y0 x y
5928 | Mpan _ ->
5929 Wsi.setcursor Wsi.CURSOR_INHERIT;
5930 state.mstate <- Mnone
5932 | Msel ((x0, y0), (x1, y1)) ->
5933 let rec loop = function
5934 | [] -> ()
5935 | l :: rest ->
5936 let inside =
5937 let a0 = l.pagedispy in
5938 let a1 = a0 + l.pagevh in
5939 let b0 = l.pagedispx in
5940 let b1 = b0 + l.pagevw in
5941 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5942 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5944 if inside
5945 then
5946 match getopaque l.pageno with
5947 | Some opaque ->
5948 let dosel cmd () =
5949 match Unix.pipe () with
5950 | exception exn ->
5951 impmsg "cannot create sel pipe: %s" @@
5952 exntos exn;
5953 | (r, w) ->
5954 let clo what fd =
5955 Ne.clo fd (fun msg ->
5956 dolog "%s close failed: %s" what msg)
5958 begin match spawn cmd [r, 0; w, -1] with
5959 | exception exn ->
5960 clo "Msel pipe/w" w;
5961 dolog "cannot execute %S: %s" cmd @@ exntos exn
5962 | _pid ->
5963 copysel w opaque;
5964 G.postRedisplay "copysel";
5965 end;
5966 clo "Msel pipe/r" r;
5968 dosel conf.selcmd ();
5969 state.roam <- dosel conf.paxcmd;
5970 | None -> ()
5971 else loop rest
5973 loop state.layout;
5974 resetmstate ();
5978 | _ -> ()
5981 let birdseyemouse button down x y mask
5982 (conf, leftx, _, hooverpageno, anchor) =
5983 match button with
5984 | 1 when down ->
5985 let rec loop = function
5986 | [] -> ()
5987 | l :: rest ->
5988 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5989 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5990 then (
5991 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5993 else loop rest
5995 loop state.layout
5996 | 3 -> ()
5997 | _ -> viewmouse button down x y mask
6000 let uioh = object
6001 method display = ()
6003 method key key mask =
6004 begin match state.mode with
6005 | Textentry textentry -> textentrykeyboard key mask textentry
6006 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6007 | View -> viewkeyboard key mask
6008 | LinkNav linknav -> linknavkeyboard key mask linknav
6009 end;
6010 state.uioh
6012 method button button bstate x y mask =
6013 begin match state.mode with
6014 | LinkNav _ | View -> viewmouse button bstate x y mask
6015 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6016 | Textentry _ -> ()
6017 end;
6018 state.uioh
6020 method multiclick clicks x y mask =
6021 begin match state.mode with
6022 | LinkNav _ | View -> viewmulticlick clicks x y mask
6023 | Birdseye _ | Textentry _ -> ()
6024 end;
6025 state.uioh
6027 method motion x y =
6028 begin match state.mode with
6029 | Textentry _ -> ()
6030 | View | Birdseye _ | LinkNav _ ->
6031 match state.mstate with
6032 | Mzoom _ | Mnone -> ()
6034 | Mpan (x0, y0) ->
6035 let dx = x - x0
6036 and dy = y0 - y in
6037 state.mstate <- Mpan (x, y);
6038 let x = if canpan () then panbound (state.x + dx) else state.x in
6039 let y = clamp dy in
6040 gotoxy_and_clear_text x y
6042 | Msel (a, _) ->
6043 state.mstate <- Msel (a, (x, y));
6044 G.postRedisplay "motion select";
6046 | Mscrolly ->
6047 let y = min state.winh (max 0 y) in
6048 scrolly y
6050 | Mscrollx ->
6051 let x = min state.winw (max 0 x) in
6052 scrollx x
6054 | Mzoomrect (p0, _) ->
6055 state.mstate <- Mzoomrect (p0, (x, y));
6056 G.postRedisplay "motion zoomrect";
6057 end;
6058 state.uioh
6060 method pmotion x y =
6061 begin match state.mode with
6062 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6063 let rec loop = function
6064 | [] ->
6065 if hooverpageno != -1
6066 then (
6067 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6068 G.postRedisplay "pmotion birdseye no hoover";
6070 | l :: rest ->
6071 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6072 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6073 then (
6074 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6075 G.postRedisplay "pmotion birdseye hoover";
6077 else loop rest
6079 loop state.layout
6081 | Textentry _ -> ()
6083 | LinkNav _ | View ->
6084 match state.mstate with
6085 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6086 | Mnone ->
6087 updateunder x y;
6088 if canselect ()
6089 then
6090 match conf.pax with
6091 | None -> ()
6092 | Some r ->
6093 let past, _, _ = !r in
6094 let now = now () in
6095 let delta = now -. past in
6096 if delta > 0.01
6097 then paxunder x y
6098 else r := (now, x, y)
6099 end;
6100 state.uioh
6102 method infochanged _ = ()
6104 method scrollph =
6105 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6106 let p, h =
6107 if maxy = 0
6108 then 0.0, float state.winh
6109 else scrollph state.y maxy
6111 vscrollw (), p, h
6113 method scrollpw =
6114 let fwinw = float (state.winw - vscrollw ()) in
6115 let sw =
6116 let sw = fwinw /. float state.w in
6117 let sw = fwinw *. sw in
6118 max sw (float conf.scrollh)
6120 let position =
6121 let maxx = state.w + state.winw in
6122 let x = state.winw - state.x in
6123 let percent = float x /. float maxx in
6124 (fwinw -. sw) *. percent
6126 hscrollh (), position, sw
6128 method modehash =
6129 let modename =
6130 match state.mode with
6131 | LinkNav _ -> "links"
6132 | Textentry _ -> "textentry"
6133 | Birdseye _ -> "birdseye"
6134 | View -> "view"
6136 findkeyhash conf modename
6138 method eformsgs = true
6139 method alwaysscrolly = false
6140 end;;
6142 let addrect pageno r g b a x0 y0 x1 y1 =
6143 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6146 let ract cmds =
6147 let cl = splitatchar cmds ' ' in
6148 let scan s fmt f =
6149 try Scanf.sscanf s fmt f
6150 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
6151 cmds @@ exntos exn
6153 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6154 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6155 s pageno r g b a x0 y0 x1 y1;
6156 onpagerect
6157 pageno
6158 (fun w h ->
6159 let _,w1,h1,_ = getpagedim pageno in
6160 let sw = float w1 /. float w
6161 and sh = float h1 /. float h in
6162 let x0s = x0 *. sw
6163 and x1s = x1 *. sw
6164 and y0s = y0 *. sh
6165 and y1s = y1 *. sh in
6166 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6167 let color = (r, g, b, a) in
6168 if conf.verbose then debugrect rect;
6169 state.rects <- (pageno, color, rect) :: state.rects;
6170 G.postRedisplay s;
6173 match cl with
6174 | "reload", "" -> reload ()
6175 | "goto", args ->
6176 scan args "%u %f %f"
6177 (fun pageno x y ->
6178 let cmd, _ = state.geomcmds in
6179 if emptystr cmd
6180 then gotopagexy !wtmode pageno x y
6181 else
6182 let f prevf () =
6183 gotopagexy !wtmode pageno x y;
6184 prevf ()
6186 state.reprf <- f state.reprf
6188 | "goto1", args -> scan args "%u %f" gotopage
6189 | "gotor", args ->
6190 scan args "%S %u"
6191 (fun _filename _pageno ->
6192 failwith "gotounder (Uremote (filename, pageno))")
6193 | "gotord", args ->
6194 scan args "%S %S"
6195 (fun _filename _dest ->
6196 failwith "gotounder (Uremotedest (filename, dest))")
6197 | "rect", args ->
6198 scan args "%u %u %f %f %f %f"
6199 (fun pageno c x0 y0 x1 y1 ->
6200 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6201 rectx "rect" pageno color x0 y0 x1 y1;
6203 | "prect", args ->
6204 scan args "%u %f %f %f %f %f %f %f %f"
6205 (fun pageno r g b alpha x0 y0 x1 y1 ->
6206 addrect pageno r g b alpha x0 y0 x1 y1;
6207 G.postRedisplay "prect"
6209 | "pgoto", args ->
6210 scan args "%u %f %f"
6211 (fun pageno x y ->
6212 let optopaque =
6213 match getopaque pageno with
6214 | Some opaque -> opaque
6215 | None -> ~< E.s
6217 pgoto optopaque pageno x y;
6218 let rec fixx = function
6219 | [] -> ()
6220 | l :: rest ->
6221 if l.pageno = pageno
6222 then gotoxy (state.x - l.pagedispx) state.y
6223 else fixx rest
6225 let layout =
6226 let mult =
6227 match conf.columns with
6228 | Csingle _ | Csplit _ -> 1
6229 | Cmulti ((n, _, _), _) -> n
6231 layout 0 state.y (state.winw * mult) state.winh
6233 fixx layout
6235 | "activatewin", "" -> Wsi.activatewin ()
6236 | "quit", "" -> raise Quit
6237 | "keys", keys ->
6238 begin try
6239 let l = Config.keys_of_string keys in
6240 List.iter (fun (k, m) -> keyboard k m) l
6241 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
6242 cmds @@ exntos exn
6244 | "clearrects", "" ->
6245 Hashtbl.clear state.prects;
6246 G.postRedisplay "clearrects"
6247 | _ ->
6248 adderrfmt "remote command"
6249 "error processing remote command: %S\n" cmds;
6252 let remote =
6253 let scratch = Bytes.create 80 in
6254 let buf = Buffer.create 80 in
6255 fun fd ->
6256 match tempfailureretry (Unix.read fd scratch 0) 80 with
6257 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6258 | 0 ->
6259 Unix.close fd;
6260 if Buffer.length buf > 0
6261 then (
6262 let s = Buffer.contents buf in
6263 Buffer.clear buf;
6264 ract s;
6266 None
6267 | n ->
6268 let rec eat ppos =
6269 let nlpos =
6270 match Bytes.index_from scratch ppos '\n' with
6271 | pos -> if pos >= n then -1 else pos
6272 | exception Not_found -> -1
6274 if nlpos >= 0
6275 then (
6276 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6277 let s = Buffer.contents buf in
6278 Buffer.clear buf;
6279 ract s;
6280 eat (nlpos+1);
6282 else (
6283 Buffer.add_subbytes buf scratch ppos (n-ppos);
6284 Some fd
6286 in eat 0
6289 let remoteopen path =
6290 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6291 with exn ->
6292 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6293 None
6296 let () =
6297 let gcconfig = ref E.s in
6298 let trimcachepath = ref E.s in
6299 let rcmdpath = ref E.s in
6300 let pageno = ref None in
6301 let rootwid = ref 0 in
6302 let openlast = ref false in
6303 let nofc = ref false in
6304 let doreap = ref false in
6305 let csspath = ref None in
6306 selfexec := Sys.executable_name;
6307 Arg.parse
6308 (Arg.align
6309 [("-p", Arg.String (fun s -> state.password <- s),
6310 "<password> Set password");
6312 ("-f", Arg.String
6313 (fun s ->
6314 Config.fontpath := s;
6315 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6317 "<path> Set path to the user interface font");
6319 ("-c", Arg.String
6320 (fun s ->
6321 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6322 Config.confpath := s),
6323 "<path> Set path to the configuration file");
6325 ("-last", Arg.Set openlast, " Open last document");
6327 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6328 "<page-number> Jump to page");
6330 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6331 "<path> Set path to the trim cache file");
6333 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6334 "<named-destination> Set named destination");
6336 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6337 ("-cxack", Arg.Set cxack, " Cut corners");
6339 ("-remote", Arg.String (fun s -> rcmdpath := s),
6340 "<path> Set path to the source of remote commands");
6342 ("-gc", Arg.Set_string gcconfig,
6343 "<path> Collect garbage with the help of a script");
6345 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6347 ("-v", Arg.Unit (fun () ->
6348 Printf.printf
6349 "%s\nconfiguration path: %s\n"
6350 (version ())
6351 Config.defconfpath;
6352 exit 0), " Print version and exit");
6354 ("-css", Arg.String (fun s -> csspath := Some s),
6355 "<path> Set path to the style sheet to use with EPUB/HTML");
6357 ("-embed", Arg.Set_int rootwid, "<window-id> Embed into window");
6359 ("-origin", Arg.String (fun s -> state.origin <- s),
6360 "<origin> <undocumented>");
6363 (fun s -> state.path <- s)
6364 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6366 if !wtmode
6367 then selfexec := !selfexec ^ " -wtmode";
6369 let histmode = emptystr state.path && not !openlast in
6371 if not (Config.load !openlast)
6372 then dolog "failed to load configuration";
6374 begin match !pageno with
6375 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6376 | None -> ()
6377 end;
6379 if nonemptystr !gcconfig
6380 then (
6381 let (c, s) =
6382 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6383 | exception exn -> error "socketpair for gc failed: %s" @@ exntos exn
6384 | fds -> fds
6386 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6387 | exception exn -> error "failed to execute gc script: %s" @@ exntos exn
6388 | _pid ->
6389 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6390 Config.gc s;
6391 exit 0
6394 let mu =
6395 object (self)
6396 val mutable m_clicks = 0
6397 val mutable m_click_x = 0
6398 val mutable m_click_y = 0
6399 val mutable m_lastclicktime = infinity
6401 method private cleanup =
6402 state.roam <- noroam;
6403 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6404 method expose = G.postRedisplay "expose"
6405 method visible v =
6406 let name =
6407 match v with
6408 | Wsi.Unobscured -> "unobscured"
6409 | Wsi.PartiallyObscured -> "partiallyobscured"
6410 | Wsi.FullyObscured -> "fullyobscured"
6412 vlog "visibility change %s" name
6413 method display = display ()
6414 method map mapped = vlog "mapped %b" mapped
6415 method reshape w h =
6416 self#cleanup;
6417 reshape w h
6418 method mouse b d x y m =
6419 if d && canselect ()
6420 then (
6422 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6424 m_click_x <- x;
6425 m_click_y <- y;
6426 if b = 1
6427 then (
6428 let t = now () in
6429 if abs x - m_click_x > 10
6430 || abs y - m_click_y > 10
6431 || abs_float (t -. m_lastclicktime) > 0.3
6432 then m_clicks <- 0;
6433 m_clicks <- m_clicks + 1;
6434 m_lastclicktime <- t;
6435 if m_clicks = 1
6436 then (
6437 self#cleanup;
6438 G.postRedisplay "cleanup";
6439 state.uioh <- state.uioh#button b d x y m;
6441 else state.uioh <- state.uioh#multiclick m_clicks x y m
6443 else (
6444 self#cleanup;
6445 m_clicks <- 0;
6446 m_lastclicktime <- infinity;
6447 state.uioh <- state.uioh#button b d x y m
6450 else (
6451 state.uioh <- state.uioh#button b d x y m
6453 method motion x y =
6454 state.mpos <- (x, y);
6455 state.uioh <- state.uioh#motion x y
6456 method pmotion x y =
6457 state.mpos <- (x, y);
6458 state.uioh <- state.uioh#pmotion x y
6459 method key k m =
6460 let mascm = m land (
6461 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6462 ) in
6463 let keyboard k m =
6464 let x = state.x and y = state.y in
6465 keyboard k m;
6466 if x != state.x || y != state.y then self#cleanup
6468 match state.keystate with
6469 | KSnone ->
6470 let km = k, mascm in
6471 begin
6472 match
6473 let modehash = state.uioh#modehash in
6474 try Hashtbl.find modehash km
6475 with Not_found ->
6476 try Hashtbl.find (findkeyhash conf "global") km
6477 with Not_found -> KMinsrt (k, m)
6478 with
6479 | KMinsrt (k, m) -> keyboard k m
6480 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6481 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6483 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6484 List.iter (fun (k, m) -> keyboard k m) insrt;
6485 state.keystate <- KSnone
6486 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6487 state.keystate <- KSinto (keys, insrt)
6488 | KSinto _ -> state.keystate <- KSnone
6490 method enter x y =
6491 state.mpos <- (x, y);
6492 state.uioh <- state.uioh#pmotion x y
6493 method leave = state.mpos <- (-1, -1)
6494 method winstate wsl = state.winstate <- wsl
6495 method quit = raise Quit
6498 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6500 setbgcol conf.bgcolor;
6501 state.wsfd <- wsfd;
6503 if not @@ List.exists GlMisc.check_extension
6504 [ "GL_ARB_texture_rectangle"
6505 ; "GL_EXT_texture_recangle"
6506 ; "GL_NV_texture_rectangle" ]
6507 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6509 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6510 then (
6511 defconf.sliceheight <- 1024;
6512 defconf.texcount <- 32;
6513 defconf.usepbo <- true;
6516 let cs, ss =
6517 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6518 | exception exn ->
6519 dolog "socketpair failed: %s" @@ exntos exn;
6520 exit 1
6521 | (r, w) ->
6522 cloexec r;
6523 cloexec w;
6524 r, w
6527 setcheckers conf.checkers;
6529 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6531 begin match !csspath with
6532 | None -> ()
6533 | Some "" -> conf.css <- E.s
6534 | Some path ->
6535 let css = filecontents path in
6536 let l = String.length css in
6537 conf.css <-
6538 if substratis css (l-2) "\r\n"
6539 then String.sub css 0 (l-2)
6540 else (if css.[l-1] = '\n'
6541 then String.sub css 0 (l-1)
6542 else css);
6543 end;
6544 init cs (
6545 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6546 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6547 !Config.fontpath, !trimcachepath, !opengl_has_pbo, not !nofc
6549 List.iter GlArray.enable [`texture_coord; `vertex];
6550 state.ss <- ss;
6551 reshape ~firsttime:true winw winh;
6552 state.uioh <- uioh;
6553 if histmode
6554 then (
6555 Wsi.settitle "llpp (history)";
6556 enterhistmode ();
6558 else (
6559 state.text <- "Opening " ^ (mbtoutf8 state.path);
6560 opendoc state.path state.password;
6562 display ();
6563 Wsi.mapwin ();
6564 Wsi.setcursor Wsi.CURSOR_INHERIT;
6565 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6567 let rec reap () =
6568 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6569 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
6570 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
6571 | 0, _ -> ()
6572 | _pid, _status -> reap ()
6574 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6576 let optrfd =
6577 ref (
6578 if nonemptystr !rcmdpath
6579 then remoteopen !rcmdpath
6580 else None
6584 let rec loop deadline =
6585 if !doreap
6586 then (
6587 doreap := false;
6588 reap ()
6590 let r = [state.ss; state.wsfd] in
6591 let r =
6592 match !optrfd with
6593 | None -> r
6594 | Some fd -> fd :: r
6596 if state.redisplay
6597 then (
6598 state.redisplay <- false;
6599 display ();
6601 let timeout =
6602 let now = now () in
6603 if deadline > now
6604 then (
6605 if deadline = infinity
6606 then ~-.1.0
6607 else max 0.0 (deadline -. now)
6609 else 0.0
6611 let r, _, _ =
6612 try Unix.select r [] [] timeout
6613 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6615 begin match r with
6616 | [] ->
6617 state.ghyll None;
6618 let newdeadline =
6619 if state.ghyll == noghyll
6620 then
6621 match state.autoscroll with
6622 | Some step when step != 0 ->
6623 let y = state.y + step in
6624 let fy = if conf.maxhfit then state.winh else 0 in
6625 let y =
6626 if y < 0
6627 then state.maxy - fy
6628 else if y >= state.maxy - fy then 0 else y
6630 if state.mode = View
6631 then gotoxy_and_clear_text state.x y
6632 else gotoxy state.x y;
6633 deadline +. 0.01
6634 | _ -> infinity
6635 else deadline +. 0.01
6637 loop newdeadline
6639 | l ->
6640 let rec checkfds = function
6641 | [] -> ()
6642 | fd :: rest when fd = state.ss ->
6643 let cmd = rcmd state.ss in
6644 act cmd;
6645 checkfds rest
6647 | fd :: rest when fd = state.wsfd ->
6648 Wsi.readresp fd;
6649 checkfds rest
6651 | fd :: rest when Some fd = !optrfd ->
6652 begin match remote fd with
6653 | None -> optrfd := remoteopen !rcmdpath;
6654 | opt -> optrfd := opt
6655 end;
6656 checkfds rest
6658 | _ :: rest ->
6659 dolog "select returned unknown descriptor";
6660 checkfds rest
6662 checkfds l;
6663 let newdeadline =
6664 let deadline1 =
6665 if deadline = infinity
6666 then now () +. 0.01
6667 else deadline
6669 match state.autoscroll with
6670 | Some step when step != 0 -> deadline1
6671 | _ -> if state.ghyll == noghyll then infinity else deadline1
6673 loop newdeadline
6674 end;
6676 match loop infinity with
6677 | exception Quit ->
6678 Config.save leavebirdseye;
6679 if hasunsavedchanges ()
6680 then save ()
6681 | _ -> error "umpossible - infinity reached"